K
- type of the row key in the main column family.public class IndexEntryHandler<K extends Serializable> extends Object implements ColumnFamilyHandler
Reading is easy with secondary indexes using regular column range operations. However updates are harder because the old index value needs to be removed before inserting the new one (Read before write).
This class offers a method to update the manual secondary indexes column families.
For example, in a users by location indexing the following clilumn families would be needed:
IndexEntriesColumnFamily:
column_family_name { "row_key_in_main_column_family": { <attr 1, timestamp 1>: old value for attr 1, <attr 1, timestamp 2>: old value for attr 1, <attr 2, timestamp 3>: old value for attr 2, ... <attr n, timestamp m>: old value for attr n, } }
We read previous index values from IndexEntryHandler rather than the main column family (or source data column family) to deal with concurrency. The timestamp is used to avoid locking for concurrent updates; old values are not override so they are deleted from the index column families.
Note: A unique id is actually used rather than a timestamp to guarantee uniqueness and avoid overriding values.
Steps to update a row in the main column family:
Some points to notice
Modifier and Type | Class and Description |
---|---|
static class |
IndexEntryHandler.IndexEntry
Composite column name to keep the indexed attribute (or column name in the main column family) and a field
to make the indexed value unique to avoid overriding previous values.
|
Constructor and Description |
---|
IndexEntryHandler(String columnFamilyName,
CassDataTypes rowKeyDataType,
com.netflix.astyanax.Serializer<K> rowKeySerializer)
Creates a column family to keep previous values of indexes.
|
Modifier and Type | Method and Description |
---|---|
void |
addIndexedValue(K rowKey,
Column<String,?> indexedColumn,
DataStoreContext context)
Adds an indexed value.
|
<D> void |
deleteIndexedValues(K rowKey,
Collection<Column<IndexEntryHandler.IndexEntry,D>> indexedValues,
DataStoreContext context)
Deletes the given indexed values (old values).
|
Collection<ColumnFamilyDefinition<?,?>> |
getColumnFamilyDefinitions()
Gets the definition of all column families this handler reads from and writes into.
|
<D> Collection<Column<IndexEntryHandler.IndexEntry,D>> |
getIndexedValues(K rowKey,
ColumnName<String,?> indexedColumnName,
ColumnDecoder<IndexEntryHandler.IndexEntry,D> decoder,
DataStoreContext context)
Gets the indexed values (old values).
|
public IndexEntryHandler(String columnFamilyName, CassDataTypes rowKeyDataType, com.netflix.astyanax.Serializer<K> rowKeySerializer)
columnFamilyName
- name for the column family.rowKeyDataType
- type of the row key in the main column family.rowKeySerializer
- row key serializer.public Collection<ColumnFamilyDefinition<?,?>> getColumnFamilyDefinitions()
ColumnFamilyHandler
getColumnFamilyDefinitions
in interface ColumnFamilyHandler
public void addIndexedValue(K rowKey, Column<String,?> indexedColumn, DataStoreContext context)
rowKey
- row key.indexedColumn
- indexed column.context
- data store context.public <D> Collection<Column<IndexEntryHandler.IndexEntry,D>> getIndexedValues(K rowKey, ColumnName<String,?> indexedColumnName, ColumnDecoder<IndexEntryHandler.IndexEntry,D> decoder, DataStoreContext context) throws ClassCastException
rowKey
- row key.indexedColumnName
- name of the indexed column to retrieve index values (old values) for.decoder
- value decoder.context
- data store context.ClassCastException
- id the type of the indexed column is not the same than the one the given decoder produces.public <D> void deleteIndexedValues(K rowKey, Collection<Column<IndexEntryHandler.IndexEntry,D>> indexedValues, DataStoreContext context)
rowKey
- row key.indexedValues
- indexed values to delete.context
- data store context.Copyright © 2016. All Rights Reserved.