@Override public String getStateStoreNameFromDatasetUrn(String datasetUrn) throws IOException { if (!this.sanitizedNameToDatasetURNMap.inverse().containsKey(datasetUrn)) { String guid = Guid.fromStrings(datasetUrn).toString(); this.sanitizedNameToDatasetURNMap.put(guid, datasetUrn); } return this.sanitizedNameToDatasetURNMap.inverse().get(datasetUrn); } }
/** * Generate a {@link Guid} for an array of Strings. * @param strings array of Strings. * @return a single {@link Guid} for the array. * @throws IOException */ public static Guid fromStrings(String... strings) throws IOException { if (strings == null || strings.length == 0) { throw new IOException("Attempting to compute guid for an empty array."); } return new Guid(StringUtils.join(strings).getBytes(Charsets.UTF_8)); }
/** * @param bytes byte array. * @param isSha true if bytes are already a sha. */ private Guid(byte[] bytes, boolean isSha) { if (isSha) { this.sha = bytes; } else { this.sha = computeGuid(bytes); } }
private static String computeGuid(State state, CopyEntity file) throws IOException { Optional<Guid> stateGuid = CopySource.getWorkUnitGuid(state); if (stateGuid.isPresent()) { return Guid.combine(file.guid(), stateGuid.get()).toString(); } throw new IOException("State does not contain a guid."); } }
private static void computeAndSetWorkUnitGuid(WorkUnit workUnit) throws IOException { Guid guid = Guid.fromStrings(workUnit.contains(ConfigurationKeys.CONVERTER_CLASSES_KEY) ? workUnit .getProp(ConfigurationKeys.CONVERTER_CLASSES_KEY) : ""); setWorkUnitGuid(workUnit, guid.append(deserializeCopyEntity(workUnit))); }
@Override public Guid guid() throws IOException { return Guid.fromStrings(toString()); }
/** * Set a unique, replicable guid for this work unit. Used for recovering partially successful work units. * @param state {@link State} where guid should be written. * @param guid A byte array guid. */ public static void setWorkUnitGuid(State state, Guid guid) { state.setProp(WORK_UNIT_GUID, guid.toString()); }
/** * Creates a new {@link Guid} which is a unique, replicable representation of the pair (this, guids). Equivalent to * combine(this, guid1, guid2, ...) * @param guids an array of {@link Guid}. * @return a new {@link Guid}. * @throws IOException */ public Guid append(Guid... guids) throws IOException { if (guids == null || guids.length == 0) { return this; } return combine(ArrayUtils.add(guids, this)); }
/** * Combine multiple {@link Guid}s into a single {@link Guid}. * @throws IOException */ public static Guid combine(Guid... guids) throws IOException { byte[][] byteArrays = new byte[guids.length][]; for (int i = 0; i < guids.length; i++) { byteArrays[i] = guids[i].sha; } return fromByteArrays(byteArrays); }
/** * Get guid in this state if available. This is the reverse operation of {@link #setWorkUnitGuid}. * @param state State from which guid should be extracted. * @return A byte array guid. * @throws IOException */ public static Optional<Guid> getWorkUnitGuid(State state) throws IOException { if (state.contains(WORK_UNIT_GUID)) { return Optional.of(Guid.deserialize(state.getProp(WORK_UNIT_GUID))); } return Optional.absent(); }
/** * Creates a new {@link Guid} which is a unique, replicable representation of the pair (this, objs). * @param objs an array of {@link HasGuid}. * @return a new {@link Guid}. * @throws IOException */ public Guid append(HasGuid... objs) throws IOException { if (objs == null || objs.length == 0) { return this; } return fromHasGuid(ArrayUtils.add(objs, new SimpleHasGuid(this))); }
/** * Generates a replicable guid to uniquely identify the origin of this {@link CopyableFile}. * @return a guid uniquely identifying the origin file. */ @Override public Guid guid() throws IOException { StringBuilder uniqueString = new StringBuilder(); uniqueString.append(getFileStatus().getModificationTime()); uniqueString.append(getFileStatus().getLen()); uniqueString.append(getFileStatus().getPath()); return Guid.fromStrings(uniqueString.toString()); }
/** * Creates a new {@link Guid} which is a unique, replicable representation of the pair (this, byteArrays). * @param byteArrays an array of byte arrays. * @return a new {@link Guid}. * @throws IOException */ public Guid append(byte[]... byteArrays) throws IOException { if (byteArrays == null || byteArrays.length == 0) { return this; } return fromByteArrays(ArrayUtils.add(byteArrays, this.sha)); }
throws IOException { for (String datasetUrn : datasetUrns) { String key = Guid.fromStrings(datasetUrn).toString(); if (!this.sanitizedNameToDatasetURNMap.containsKey(key)) { this.sanitizedNameToDatasetURNMap.put(key, datasetUrn);
/** * Generate a {@link Guid} for an array of byte arrays. * @param byteArrays array of byte arrays. * @return a single {@link Guid} for the array. * @throws IOException */ public static Guid fromByteArrays(byte[]... byteArrays) throws IOException { if (byteArrays == null || byteArrays.length == 0) { throw new IOException("Attempting to compute guid for an empty array."); } if (byteArrays.length == 1) { return new Guid(byteArrays[0]); } byte[] tmp = new byte[0]; for (byte[] arr : byteArrays) { tmp = ArrayUtils.addAll(tmp, arr); } return new Guid(tmp); }
/** * Generate a {@link Guid} for an array of {@link HasGuid}. * @param objs array of {@link HasGuid}. * @return a single {@link Guid} for the array. * @throws IOException */ public static Guid fromHasGuid(HasGuid... objs) throws IOException { byte[][] byteArrays = new byte[objs.length][]; for (int i = 0; i < objs.length; i++) { byteArrays[i] = objs[i].guid().sha; } return fromByteArrays(byteArrays); }
/** * Reverse of {@link #toString}. Deserializes a {@link Guid} from a previously serialized one. * @param str Serialized {@link Guid}. * @return deserialized {@link Guid}. * @throws IOException */ public static Guid deserialize(String str) throws IOException { if (str.length() != 2 * GUID_LENGTH) { throw new IOException("String is not an encoded guid."); } try { return new Guid(Hex.decodeHex(str.toCharArray()), true); } catch (DecoderException de) { throw new IOException(de); } }