Module: CassandraMapper::Indexing

Included in:
Base
Defined in:
lib/cassandra_mapper/indexing.rb

Overview

Provides indexing behavior for CassandraMapper::Base objects. Rather than maintaining indexes in Cassandra yourself, use the higher-level functionality provided by CassandraMapper::Indexing, and CassandraMapper will manage the underlying index state for you.

An index needs a standard column family into which index data is placed. A given searchable value (an indexed value) becomes a row key in the column family. The columns and values in the row provide the keys in your indexed column family that have the indexed value.

Suppose we have column family A with rows:

'foo': {
    'key'  : 'foo',
    'value': 'a',
};
'bar': {
    'key'  : 'bar',
    'value': 'b',
};
'fu': {
    'key'  : 'fu',
    'value': 'a',
}

Suppose further that in column family B we want to index on A’s value column. We would therefore expect B to have rows:

'a': {
    'foo': 'foo',
    'fu' : 'fu',
};
'b': {
    'bar': 'bar',
};

Cassandra automatically sorts columns within a row, based on the configuration for the column family in question. Therefore, while the redundant data for column keys and values shown above seems somewhat awkward, the column keys can be designed to give smarter sorting of results; for instance, were each row to have a created_at timestamp string, we could index on value as before but sort by created_at.

So, with A values:

'foo': {
    'key'       : 'foo',
    'value'     : 'a',
    'created_at': '20100601 093000',
};
'bar': {
    'key'       : 'bar',
    'value'     : 'b',
    'created_at': '20100529 172500',
};
'fu': {
    'key'       : 'fu',
    'value'     : 'a',
    'created_at': '20100602 121500',
};

We could index on value with results sorted in ascending order of created_at with B rows:

'a': {
    '20100601 093000 foo': 'foo',
    '20100602 121500 fu': 'fu',
};
'b': {
    '20100529 172500 bar': 'bar',
};

The end result is that rows in A could be looked up via data values using the desired data value as the key of B for finding identifiers. Those results can be structure (via column name) to ensure that keys come back in the desired order (in this case, by created_at order).

The column family that stores the index can be used for one index or multiple indexes, depending on your use case.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



162
163
164
# File 'lib/cassandra_mapper/indexing.rb', line 162

def self.included(klass)
  klass.extend(ClassMethods)
end