use of co.cask.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class IndexedObjectStore method write.
/**
* Writes to the dataset, deleting any existing secondaryKey corresponding to the key and updates the indexTable with
* the secondaryKey that is passed.
*
* @param key key for storing the object
* @param object object to be stored
* @param secondaryKeys indices that can be used to lookup the object
*/
@WriteOnly
public void write(byte[] key, T object, byte[][] secondaryKeys) {
writeToObjectStore(key, object);
//Update the secondaryKeys
//logic:
// - Get existing secondary keys
// - Compute diff between existing secondary keys and new secondary keys
// - Remove the secondaryKeys that are removed
// - Add the new keys that are added
Row row = index.get(getPrefixedPrimaryKey(key));
Set<byte[]> existingSecondaryKeys = new TreeSet<>(Bytes.BYTES_COMPARATOR);
if (!row.isEmpty()) {
existingSecondaryKeys = row.getColumns().keySet();
}
Set<byte[]> newSecondaryKeys = new TreeSet<>(Bytes.BYTES_COMPARATOR);
newSecondaryKeys.addAll(Arrays.asList(secondaryKeys));
List<byte[]> secondaryKeysDeleted = secondaryKeysToDelete(existingSecondaryKeys, newSecondaryKeys);
if (secondaryKeysDeleted.size() > 0) {
deleteSecondaryKeys(key, secondaryKeysDeleted.toArray(new byte[secondaryKeysDeleted.size()][]));
}
List<byte[]> secondaryKeysAdded = secondaryKeysToAdd(existingSecondaryKeys, newSecondaryKeys);
//for each key store the secondaryKey. This will be used while deleting old index values.
if (secondaryKeysAdded.size() > 0) {
byte[][] fooValues = new byte[secondaryKeysAdded.size()][];
Arrays.fill(fooValues, EMPTY_VALUE);
index.put(getPrefixedPrimaryKey(key), secondaryKeysAdded.toArray(new byte[secondaryKeysAdded.size()][]), fooValues);
}
for (byte[] secondaryKey : secondaryKeysAdded) {
//update the index.
index.put(secondaryKey, key, EMPTY_VALUE);
}
}
use of co.cask.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class IndexedObjectStore method write.
@WriteOnly
public void write(byte[] key, T object) {
Row row = index.get(getPrefixedPrimaryKey(key));
if (!row.isEmpty()) {
Set<byte[]> columnsToDelete = row.getColumns().keySet();
deleteSecondaryKeys(key, columnsToDelete.toArray(new byte[columnsToDelete.size()][]));
}
writeToObjectStore(key, object);
}
Aggregations