Search in sources :

Example 1 with WriteOnly

use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.

the class IndexedTable method delete.

@WriteOnly
@Override
public void delete(byte[] row, byte[][] columns) {
    Row existingRow = table.get(row, columns);
    if (existingRow.isEmpty()) {
        // no row to delete
        return;
    }
    // delete all index entries
    deleteIndexEntries(existingRow);
    // delete the row's columns
    table.delete(row, columns);
}
Also used : Row(io.cdap.cdap.api.dataset.table.Row) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 2 with WriteOnly

use of io.cdap.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);
    }
}
Also used : TreeSet(java.util.TreeSet) Row(io.cdap.cdap.api.dataset.table.Row) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 3 with WriteOnly

use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.

the class BufferingTable method delete.

/**
 * NOTE: Depending on the use-case, calling this method may be much less efficient than calling same method
 *       with columns as parameters because it will require a round trip to persistent store.
 */
@WriteOnly
@Override
public void delete(byte[] row) {
    ensureTransactionIsStarted();
    // this is going to be expensive, but the only we can do as delete implementation act on per-column level
    try {
        Map<byte[], byte[]> rowMap = getRowMap(row);
        delete(row, rowMap.keySet().toArray(new byte[rowMap.keySet().size()][]));
        // "0" because we don't know what gets deleted
        reportWrite(1, 0);
    } catch (Exception e) {
        LOG.debug("delete failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("delete failed", e);
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 4 with WriteOnly

use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.

the class IndexedTable method put.

@WriteOnly
@Override
public void put(byte[] row, byte[] column, byte[] value) {
    Put put = new Put(row);
    put.add(column, value);
    put(put);
}
Also used : Put(io.cdap.cdap.api.dataset.table.Put) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 5 with WriteOnly

use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.

the class IndexedTable method delete.

@WriteOnly
@Override
public void delete(byte[] row) {
    Row existingRow = table.get(row);
    if (existingRow.isEmpty()) {
        // no row to delete
        return;
    }
    // delete all index entries
    deleteIndexEntries(existingRow);
    // delete the row
    table.delete(row);
}
Also used : Row(io.cdap.cdap.api.dataset.table.Row) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Aggregations

WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)11 Row (io.cdap.cdap.api.dataset.table.Row)5 Put (io.cdap.cdap.api.dataset.table.Put)4 DataSetException (io.cdap.cdap.api.dataset.DataSetException)3 IOException (java.io.IOException)3 TreeSet (java.util.TreeSet)2 PartitionDetail (io.cdap.cdap.api.dataset.lib.PartitionDetail)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 Location (org.apache.twill.filesystem.Location)1