Search in sources :

Example 91 with Row

use of co.cask.cdap.api.dataset.table.Row 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(co.cask.cdap.api.dataset.table.Row) WriteOnly(co.cask.cdap.api.annotation.WriteOnly)

Example 92 with Row

use of co.cask.cdap.api.dataset.table.Row in project cdap by caskdata.

the class IndexedTable method put.

/**
   * Writes a put to the data table. If any of the columns in the {@link Put} are configured to be indexed, the
   * appropriate indexes will be updated with the indexed values referencing the data table row.
   * 
   * @param put The put operation to store
   */
@WriteOnly
@Override
public void put(Put put) {
    // if different value exists, remove current index ref
    // add a new index ref unless same value already exists
    byte[] dataRow = put.getRow();
    // find which values need to be indexed
    Map<byte[], byte[]> putColumns = put.getValues();
    Set<byte[]> colsToIndex = new TreeSet<>(Bytes.BYTES_COMPARATOR);
    for (Map.Entry<byte[], byte[]> putEntry : putColumns.entrySet()) {
        if (indexedColumns.contains(putEntry.getKey())) {
            colsToIndex.add(putEntry.getKey());
        }
    }
    if (!colsToIndex.isEmpty()) {
        // first read the existing indexed values to find which have changed and need to be updated
        Row existingRow = table.get(dataRow, colsToIndex.toArray(new byte[colsToIndex.size()][]));
        for (Map.Entry<byte[], byte[]> entry : existingRow.getColumns().entrySet()) {
            if (!Arrays.equals(entry.getValue(), putColumns.get(entry.getKey()))) {
                index.delete(createIndexKey(dataRow, entry.getKey(), entry.getValue()), IDX_COL);
            } else {
                // value already indexed
                colsToIndex.remove(entry.getKey());
            }
        }
        // add new index entries for all values that have changed or did not exist
        for (byte[] col : colsToIndex) {
            index.put(createIndexKey(dataRow, col, putColumns.get(col)), IDX_COL, dataRow);
        }
    }
    // store the data row
    table.put(put);
}
Also used : TreeSet(java.util.TreeSet) Row(co.cask.cdap.api.dataset.table.Row) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) WriteOnly(co.cask.cdap.api.annotation.WriteOnly)

Example 93 with Row

use of co.cask.cdap.api.dataset.table.Row 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(co.cask.cdap.api.dataset.table.Row) WriteOnly(co.cask.cdap.api.annotation.WriteOnly)

Example 94 with Row

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

Example 95 with Row

use of co.cask.cdap.api.dataset.table.Row in project cdap by caskdata.

the class KeyValueTable method readAll.

/**
   * Reads the values for an array of given keys.
   *
   * @param keys the keys to be read
   * @return a map of the stored values, keyed by key
   */
@ReadOnly
public Map<byte[], byte[]> readAll(byte[][] keys) {
    List<Get> gets = new ArrayList<>(keys.length);
    for (byte[] key : keys) {
        gets.add(new Get(key).add(KEY_COLUMN));
    }
    List<Row> results = table.get(gets);
    Map<byte[], byte[]> values = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    for (Row row : results) {
        if (row.get(KEY_COLUMN) != null) {
            values.put(row.getRow(), row.get(KEY_COLUMN));
        }
    }
    return values;
}
Also used : Get(co.cask.cdap.api.dataset.table.Get) ArrayList(java.util.ArrayList) Row(co.cask.cdap.api.dataset.table.Row) TreeMap(java.util.TreeMap) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Aggregations

Row (co.cask.cdap.api.dataset.table.Row)111 Scanner (co.cask.cdap.api.dataset.table.Scanner)60 Test (org.junit.Test)23 Table (co.cask.cdap.api.dataset.table.Table)20 Get (co.cask.cdap.api.dataset.table.Get)16 ArrayList (java.util.ArrayList)16 TransactionExecutor (org.apache.tephra.TransactionExecutor)16 Map (java.util.Map)15 Put (co.cask.cdap.api.dataset.table.Put)14 HashMap (java.util.HashMap)10 Scan (co.cask.cdap.api.dataset.table.Scan)9 TransactionAware (org.apache.tephra.TransactionAware)9 MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)8 QueueEntryRow (co.cask.cdap.data2.transaction.queue.QueueEntryRow)8 DatasetId (co.cask.cdap.proto.id.DatasetId)8 IOException (java.io.IOException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Transaction (org.apache.tephra.Transaction)7 WriteOnly (co.cask.cdap.api.annotation.WriteOnly)6 Schema (co.cask.cdap.api.data.schema.Schema)6