libQtCassandra: QtCassandra::QCassandraContext Class Reference

libQtCassandra 1.0

Hold a Cassandra keyspace definition. More...

#include <QCassandraContext.h>

Inheritance diagram for QtCassandra::QCassandraContext:
Collaboration diagram for QtCassandra::QCassandraContext:

List of all members.

Public Types

typedef QMap< QString, QString > QCassandraContextOptions
 A map of context options.

Public Member Functions

virtual ~QCassandraContext ()
 Clean up the QCassandraContext object.

QString contextName () const
 Retrieve the name of this context.

void create ()
 Create a new context.

QString descriptionOption (const QString &option) const
 Retrieve a description option.

const QCassandraContextOptionsdescriptionOptions () const
 Get all the map of all description options.

void drop ()
 Drop this context.

void dropTable (const QString &table_name)
 Drop the specified table from the Cassandra database.

bool durableWrites () const
 Retrieve the durable write flag.

QSharedPointer< QCassandraTablefindTable (const QString &table_name) const
 Search for a table.

QCassandraTableoperator[] (const QString &table_name)
 Retrieve a table reference from a context.

const QCassandraTableoperator[] (const QString &table_name) const
 Retrieve a constant table reference.

int32_t replicationFactor () const
 Retrieve the current replication factor.

void setDescriptionOption (const QString &option, const QString &value)
 Add or replace one of the context description options.

void setDescriptionOptions (const QCassandraContextOptions &options)
 Replace all the context description options.

void setDurableWrites (bool durable_writes)
 Set whether the writes are durable.

void setReplicationFactor (int32_t factor)
 Set the replication factor.

void setStrategyClass (const QString &strategy_class)
 Set the context strategy class.

QString strategyClass () const
 Retrieve the name of the strategy class of this context.

QSharedPointer< QCassandraTabletable (const QString &table_name)
 Retrieve a table definition by name.

const QCassandraTablestables () const
 Retrieve a reference to the tables.

void unsetDurableWrites ()
 Unset the durable writes flag.

void unsetReplicationFactor ()
 Unset the replication factor.

void update ()
 Update a context with new properties.

Private Member Functions

 QCassandraContext (QCassandra *cassandra, const QString &context_name)
 Initialize a QCassandraContext object.

void createTable (const QCassandraTable *table)
 Create a Cassandra table.

int32_t getCellCount (const QString &table_name, const QByteArray &row_key, const QCassandraColumnPredicate &column_predicate)
 Get a table slice.

void getColumnSlice (QCassandraTable &table, const QByteArray &row_key, const QCassandraColumnPredicate &column_predicate)
 Get a table slice.

uint32_t getRowSlices (QCassandraTable &table, const QCassandraRowPredicate &row_predicate)
 Retrieve a slice of rows from Cassandra.

void getValue (const QString &table_name, const QByteArray &row_key, const QByteArray &column_key, QCassandraValue &value)
 Retrieve a value from the Cassandra database.

void insertValue (const QString &table_name, const QByteArray &row_key, const QByteArray &column_key, const QCassandraValue &value)
 Insert a new value in the Cassandra database.

void makeCurrent ()
 Make this context the current context.

void parseContextDefinition (const void *data)
 This is an internal function used to parse a KsDef structure.

void prepareContextDefinition (void *data) const
 Prepare the context.

void remove (const QString &table_name, const QByteArray &row_key, const QByteArray &column_key, int64_t timestamp, consistency_level_t consistency_level)
 Remove a cell from the Cassandra database.

void truncateTable (const QCassandraTable *table)
 Truncate a Cassandra table.

void updateTable (const QCassandraTable *table)
 Update a Cassandra table.

Private Attributes

QCassandraf_cassandra
 A pointer back to the QCassandra object.

QCassandraContextOptions f_options
 List of tables.

std::auto_ptr
< QCassandraContextPrivate
f_private
 The pointer to the QCassandraContextPrivate object.

QCassandraTables f_tables
 List of tables.

Friends

class QCassandra
class QCassandraContextPrivate
class QCassandraPrivate
class QCassandraTable

Detailed Description

This class defines objects that can hold all the necessary information for a keyspace definition.

A keyspace is similar to a context in the sense that to work on a keyspace you need make it the current keyspace. You cannot simply use the name in a query and hope the switch will work automatically.

This is very similar to a database environment where a keyspace (context) is one database in your cluster.

Definition at line 50 of file QCassandraContext.h.


Member Typedef Documentation

This map defines options as name / value pairs.

Only known otion names should be used or a protocol error may result.

Definition at line 53 of file QCassandraContext.h.


Constructor & Destructor Documentation

QtCassandra::QCassandraContext::~QCassandraContext ( ) [virtual]

This function ensures that all resources allocated by the QCassandraContext are released.

Definition at line 161 of file QCassandraContext.cpp.

QtCassandra::QCassandraContext::QCassandraContext ( QCassandra cassandra,
const QString &  context_name 
) [private]

This function initializes a QCassandraContext object.

Note that the constructor is private. To create a new context, you must use the QCassandra::context() function instead.

All the parameters are set to the defaults as defined in the Cassandra definition of the KsDef message. You can use the different functions to change the default values.

A column name must be composed of letters (A-Za-z), digits (0-9) and underscore (_). It must start with a letter. The corresponding lexical expression is: /[A-Za-z][A-Za-z0-9_]*\/

Parameters:
[in]cassandraThe QCassandra object owning this context.
[in]context_nameThe name of the Cassandra context.
See also:
QCassandra::context()

Definition at line 140 of file QCassandraContext.cpp.

References f_private.


Member Function Documentation

QString QtCassandra::QCassandraContext::contextName ( ) const

This function returns the name of this context.

Note that the name cannot be modified. It is set by the constructor as you create a QCassandraContext.

Returns:
A QString with the context name.

Definition at line 174 of file QCassandraContext.cpp.

References f_private.

Referenced by QtCassandra::QCassandraPrivate::dropContext(), and QtCassandra::QCassandraTable::QCassandraTable().

void QtCassandra::QCassandraContext::create ( )

This function is used to create a new context (keyspace) in the current Cassandra cluster. Once created, you can switch to this context with the use of the makeCurrent() function.

If you want to include tables in your new context, then create them before calling this function.

There is an example on how to create a new context with this library:

 QtCassandra::QCassandraContext context("qt_cassandra_test_context");
 // default strategy is LocalStrategy which you usually don't want
 context.setStrategyClass("org.apache.cassandra.locator.SimpleStrategy");
 context.setDurableWrites(true); // by default this is 'true'
 context.setReplicationFactor(1); // by default this is undefined
 ...  // create tables before create() if you also want tables
 context.create(context);

Once the context is created, switch to it using the makeCurrent() function:

 context.makeCurrent();

Note that while creating the tables the new context is made the current context. However, the previous context is restored before the function returned. If no previous context was defined, then the new context remains the current context.

See also:
makeCurrent()
QCassandraTable::create()

Definition at line 609 of file QCassandraContext.cpp.

References QtCassandra::QCassandraPrivate::createContext(), f_cassandra, f_tables, and QtCassandra::QCassandra::getPrivate().

void QtCassandra::QCassandraContext::createTable ( const QCassandraTable table) [private]

This function creates a Cassandra table by sending the corresponding order to the connected server.

Parameters:
[in]tableThe table definition used to create the Cassandra table.

Definition at line 679 of file QCassandraContext.cpp.

References QtCassandra::QCassandraPrivate::createTable(), f_cassandra, QtCassandra::QCassandra::getPrivate(), and makeCurrent().

Referenced by QtCassandra::QCassandraTable::create().

QString QtCassandra::QCassandraContext::descriptionOption ( const QString &  option) const

This function retrieves the description option named by the option parameter.

Parameters:
[in]optionThe name of the option to retrieve.
Returns:
The value of the named option or an empty string.

Definition at line 259 of file QCassandraContext.cpp.

References f_options.

const QCassandraContext::QCassandraContextOptions & QtCassandra::QCassandraContext::descriptionOptions ( ) const

The context maintains a map indexed by option name of all the description options of the context. This function retreives a constant reference to that list.

If you keep the return reference as such (i.e. a reference) then make sure you do not modify the options (calling one of the setDescriptionOptions() or setDescriptionOption() functions.) Otherwise the reference may become invalid. If you are going to modify the options, make a copy of the map.

Returns:
A reference to the map of context options.

Definition at line 233 of file QCassandraContext.cpp.

References f_options.

void QtCassandra::QCassandraContext::drop ( )

This function drops this context.

Note that contexts are dropped by name so we really only use the name of the context here.

The QCassandraContext object is still valid afterward, although, obviously no data can be read from the Cassandra server since the context is gone. You may change the parameters of the context and call create() to create a new context with the same name.

Definition at line 651 of file QCassandraContext.cpp.

References QtCassandra::QCassandraPrivate::dropContext(), f_cassandra, and QtCassandra::QCassandra::getPrivate().

void QtCassandra::QCassandraContext::dropTable ( const QString &  table_name)

This function sends a message to the Cassandra server so the named table gets droped from it.

The function also deletes the table from memory.

Parameters:
[in]table_nameThe name of the table to drop.

Definition at line 665 of file QCassandraContext.cpp.

References QtCassandra::QCassandraPrivate::dropTable(), f_cassandra, f_tables, QtCassandra::QCassandra::getPrivate(), and makeCurrent().

bool QtCassandra::QCassandraContext::durableWrites ( ) const

This function returns the durable flag that determines whether a context is temporary (false) or permanent (true).

Returns:
The current durable writes flag status.

Definition at line 446 of file QCassandraContext.cpp.

References f_private.

QSharedPointer< QCassandraTable > QtCassandra::QCassandraContext::findTable ( const QString &  table_name) const

This function searches for a table. If it exists, its shared pointer is returned. Otherwise, it returns a NULL pointer (i.e. the QSharedPointer::isNull() function returns true.)

Parameters:
[in]table_nameThe name of the table to retrieve.
Returns:
A shared pointer to the table.

Definition at line 314 of file QCassandraContext.cpp.

References f_tables.

Referenced by operator[]().

int32_t QtCassandra::QCassandraContext::getCellCount ( const QString &  table_name,
const QByteArray &  row_key,
const QCassandraColumnPredicate column_predicate 
) [private]

This function reads a table slice from the Cassandra database.

Parameters:
[in]table_nameThe name of the table where the row is check is defined.
[in]row_keyThe row for which this data is being counted.
[in]column_predicateThe predicate to use to count the cells.
Returns:
The number of columns defined in the specified row.

Definition at line 757 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandraPrivate::getCellCount(), QtCassandra::QCassandra::getPrivate(), and makeCurrent().

Referenced by QtCassandra::QCassandraTable::getCellCount().

void QtCassandra::QCassandraContext::getColumnSlice ( QCassandraTable table,
const QByteArray &  row_key,
const QCassandraColumnPredicate column_predicate 
) [private]

This function reads a table slice from the Cassandra database.

Parameters:
[in]tableThe table where the results is saved.
[in]row_keyThe row for which this data is being counted.
[in]column_predicateThe predicate to use to count the cells.

Definition at line 771 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandraPrivate::getColumnSlice(), QtCassandra::QCassandra::getPrivate(), and makeCurrent().

Referenced by QtCassandra::QCassandraTable::getColumnSlice().

uint32_t QtCassandra::QCassandraContext::getRowSlices ( QCassandraTable table,
const QCassandraRowPredicate row_predicate 
) [private]

This function calls the Cassandra server to retrieve a set of rows as defined by the row predicate. These rows get cells as defined by the column predicate defined inside the row predicate object.

Parameters:
[in]tableThe table which is emitting this call.
[in]row_predicateThe predicate used to select the rows.
Returns:
The number of rows read on this call.

Definition at line 805 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), QtCassandra::QCassandraPrivate::getRowSlices(), and makeCurrent().

Referenced by QtCassandra::QCassandraTable::readRows().

void QtCassandra::QCassandraContext::getValue ( const QString &  table_name,
const QByteArray &  row_key,
const QByteArray &  column_key,
QCassandraValue value 
) [private]

This function gets a value from the Cassandra database referenced by the row_key and column_key.

Parameters:
[in]table_nameName of the table where the value is inserted.
[in]row_keyThe key used to identify the row.
[in]column_keyThe key used to identify the column.
[in]valueThe new value of the cell.

Definition at line 741 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), QtCassandra::QCassandraPrivate::getValue(), and makeCurrent().

Referenced by QtCassandra::QCassandraTable::getValue().

void QtCassandra::QCassandraContext::insertValue ( const QString &  table_name,
const QByteArray &  row_key,
const QByteArray &  column_key,
const QCassandraValue value 
) [private]

This function inserts a new value in this Context of the Cassandra database referenced by the row_key and column_key.

Parameters:
[in]table_nameName of the table where the value is inserted.
[in]row_keyThe key used to identify the row.
[in]column_keyThe key used to identify the column.
[in]valueThe new value of the cell.

Definition at line 725 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), QtCassandra::QCassandraPrivate::insertValue(), and makeCurrent().

Referenced by QtCassandra::QCassandraTable::insertValue().

void QtCassandra::QCassandraContext::makeCurrent ( ) [private]

This function marks this context as the current context where further function calls will be made.

Note that if you try to read from this context, it will automatically attempt to make itself current.

See also:
QCassandra::setContext()

Definition at line 563 of file QCassandraContext.cpp.

References QtCassandra::QCassandra::context(), f_cassandra, f_private, and QtCassandra::QCassandra::setCurrentContext().

Referenced by createTable(), dropTable(), getCellCount(), getColumnSlice(), getRowSlices(), getValue(), insertValue(), remove(), truncateTable(), and updateTable().

QCassandraTable & QtCassandra::QCassandraContext::operator[] ( const QString &  table_name)

The array operator searches for a table by name and returns its reference. This is useful to access data with array like syntax as in:

 context[table_name][column_name] = value;
Exceptions:
std::runtime_errorIf the table doesn't exist, this function raises an exception since otherwise the reference would be a NULL pointer.
Parameters:
[in]table_nameThe name of the table to retrieve.
Returns:
A reference to the named table.

Definition at line 342 of file QCassandraContext.cpp.

References findTable(), and table().

const QCassandraTable & QtCassandra::QCassandraContext::operator[] ( const QString &  table_name) const

This array operator is the same as the other one, just this one deals with constant tables. It can be used to retrieve values from the Cassandra cluster you're connected to:

 value = context[table_name][column_name];
Exceptions:
std::runtime_errorIf the table doesn't exist, this function raises an exception since otherwise the reference would be a NULL pointer.
Parameters:
[in]table_nameThe name of the table to retrieve.
Returns:
A constant reference to the named table.

Definition at line 370 of file QCassandraContext.cpp.

References findTable(), and table().

void QtCassandra::QCassandraContext::parseContextDefinition ( const void *  data) [private]

This function is called internally to parse a KsDef object.

Parameters:
[in]dataThe pointer to the KsDef object.

Definition at line 457 of file QCassandraContext.cpp.

References f_options, f_private, f_tables, and table().

void QtCassandra::QCassandraContext::prepareContextDefinition ( void *  data) const [private]

This function prepares the context so it can be copied in a keyspace definition later used to create a keyspace or to update an existing keyspace.

Parameters:
[out]dataThe output keyspace definition.

Definition at line 516 of file QCassandraContext.cpp.

References f_options, f_private, and f_tables.

Referenced by QtCassandra::QCassandraPrivate::createContext(), and QtCassandra::QCassandraPrivate::updateContext().

void QtCassandra::QCassandraContext::remove ( const QString &  table_name,
const QByteArray &  row_key,
const QByteArray &  column_key,
int64_t  timestamp,
consistency_level_t  consistency_level 
) [private]

This function calls the Cassandra server to remove a cell in the Cassandra database.

Parameters:
[in]table_nameThe name of the column where the row is defined.
[in]row_keyThe row in which the cell is to be removed, if empty all the rows.
[in]column_keyThe cell to be removed, if empty all the cells.
[in]timestampThe time when the key to be removed was created.
[in]consistency_levelThe consistency level to use to remove this cell.

Definition at line 788 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), makeCurrent(), and QtCassandra::QCassandraPrivate::remove().

Referenced by QtCassandra::QCassandraTable::remove().

int32_t QtCassandra::QCassandraContext::replicationFactor ( ) const

This function reads and return the current replication factor of the context.

Returns:
The current replication factor.

Definition at line 410 of file QCassandraContext.cpp.

References f_private.

void QtCassandra::QCassandraContext::setDescriptionOption ( const QString &  option,
const QString &  value 
)

This function sets the specified option to the specified value.

Parameters:
[in]optionThe option to set.
[in]valueThe value to set.

Definition at line 245 of file QCassandraContext.cpp.

References f_options.

void QtCassandra::QCassandraContext::setDescriptionOptions ( const QCassandraContextOptions options)

This function overwrites all the description options with the ones specified in the input parameter.

This function can be used to clear all the options by passing an empty options parameter.

Parameters:
[in]optionsThe replacing options

Definition at line 215 of file QCassandraContext.cpp.

References f_options.

void QtCassandra::QCassandraContext::setDurableWrites ( bool  durable_writes)

Temporary and permanent contexts can be created. This option defines whether it is one of the other. Set to true to create a permanent context (this is the default.)

Parameters:
[in]durable_writesSet whether writes are durable.

Definition at line 423 of file QCassandraContext.cpp.

References f_private.

void QtCassandra::QCassandraContext::setReplicationFactor ( int32_t  factor)

This function sets the replication factor of the context.

Parameters:
[in]factorThe new replication factor.

Definition at line 386 of file QCassandraContext.cpp.

References f_private.

void QtCassandra::QCassandraContext::setStrategyClass ( const QString &  strategy_class)

This function is used to change the class of the context to a new strategy. The strategy class is used to handle reads and writes according to the use of the corresponding context.

Parameters:
[in]strategy_classThe new strategy class.

Definition at line 187 of file QCassandraContext.cpp.

References f_private.

QString QtCassandra::QCassandraContext::strategyClass ( ) const

This function returns the strategy class of this context. This is a name defining how the data in this context is duplicated accross the nodes of the Cassandra system.

Returns:
The strategy class.

Definition at line 200 of file QCassandraContext.cpp.

References f_private.

QSharedPointer< QCassandraTable > QtCassandra::QCassandraContext::table ( const QString &  table_name)

This function is used to retrieve a table definition by name. If the table doesn't exist, it gets created.

You can test whether the result is null with the isNull() function of the QSharedPointer class.

Parameters:
[in]table_nameThe name of the table to retrieve.
Returns:
A shared pointer to the table definition found or a null shared pointer.

Definition at line 276 of file QCassandraContext.cpp.

References f_tables, and QCassandraTable.

Referenced by operator[](), and parseContextDefinition().

const QCassandraTables & QtCassandra::QCassandraContext::tables ( ) const

This function retrieves a constant reference to the map of table definitions. The list is read-only, however, it is strongly suggested that you make a copy if your code is going to modifiy tables later (i.e. calling table() may affect the result of this call if you did not first copy the map.)

Returns:
A reference to the table definitions of this context.

Definition at line 299 of file QCassandraContext.cpp.

References f_tables.

void QtCassandra::QCassandraContext::truncateTable ( const QCassandraTable table) [private]

This function truncates a Cassandra table by sending the corresponding order to the connected server.

Parameters:
[in]tableThe table to drop from the Cassandra server.

Definition at line 709 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), makeCurrent(), and QtCassandra::QCassandraPrivate::truncateTable().

Referenced by QtCassandra::QCassandraTable::truncate().

void QtCassandra::QCassandraContext::unsetDurableWrites ( )

This function marks the durable write flag as not set. This does not otherwise change the flag. It will just not be sent over the network and the default will be used when required.

Definition at line 434 of file QCassandraContext.cpp.

References f_private.

void QtCassandra::QCassandraContext::unsetReplicationFactor ( )

This function unsets the replication factor in case it was set. In general it is not necessary to call this function unless you are initializing a new context and you want to make sure that the default replication factor is used.

Definition at line 398 of file QCassandraContext.cpp.

References f_private.

void QtCassandra::QCassandraContext::update ( )

This function defines a new set of properties in the specified context. In general, the context will be searched in the cluster definitions, updated in memory then this function called.

Definition at line 634 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), and QtCassandra::QCassandraPrivate::updateContext().

void QtCassandra::QCassandraContext::updateTable ( const QCassandraTable table) [private]

This function updates a Cassandra table by sending the corresponding order to the connected server.

It can be used to define new column types or changing existing columns (although changing existing columns may not work as expected, from what I understand.)

Parameters:
[in]tableThe table to update in the Cassandra server.

Definition at line 696 of file QCassandraContext.cpp.

References f_cassandra, QtCassandra::QCassandra::getPrivate(), makeCurrent(), and QtCassandra::QCassandraPrivate::updateTable().

Referenced by QtCassandra::QCassandraTable::update().


Friends And Related Function Documentation

friend class QCassandra [friend]

Definition at line 106 of file QCassandraContext.h.

friend class QCassandraContextPrivate [friend]

Definition at line 108 of file QCassandraContext.h.

friend class QCassandraPrivate [friend]

Definition at line 107 of file QCassandraContext.h.

friend class QCassandraTable [friend]

Definition at line 109 of file QCassandraContext.h.

Referenced by table().


Member Data Documentation

The bare pointer is used by the context to access the cassandra private object and make the context the current context. It is a bare pointer because the QCassandra object cannot be deleted without the context getting deleted first.

Definition at line 115 of file QCassandraContext.h.

Referenced by create(), createTable(), drop(), dropTable(), getCellCount(), getColumnSlice(), getRowSlices(), getValue(), insertValue(), makeCurrent(), remove(), truncateTable(), update(), and updateTable().

A map of name and value pairs representing options of the context. Each context can have many options.

The libQtCassandra doesn't make use of these options. It's only used by the Cassandra server.

Definition at line 116 of file QCassandraContext.h.

Referenced by descriptionOption(), descriptionOptions(), parseContextDefinition(), prepareContextDefinition(), setDescriptionOption(), and setDescriptionOptions().

This pointer is a shared pointer to the private definition of the Cassandra context (i.e. a keyspace definition.)

The pointer is created at the time the context is created.

Definition at line 111 of file QCassandraContext.h.

Referenced by contextName(), durableWrites(), makeCurrent(), parseContextDefinition(), prepareContextDefinition(), QCassandraContext(), replicationFactor(), setDurableWrites(), setReplicationFactor(), setStrategyClass(), strategyClass(), unsetDurableWrites(), and unsetReplicationFactor().

A list of the tables defined in this context. The tables may be created in memory only.

The list is a map using the table binary key as the its own key.

Definition at line 117 of file QCassandraContext.h.

Referenced by create(), dropTable(), findTable(), parseContextDefinition(), prepareContextDefinition(), table(), and tables().


The documentation for this class was generated from the following files:

This document is part of the libQtCassandra Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly