libQtCassandra 0.3.2
|
Hold a Cassandra keyspace definition. More...
#include <QCassandraContext.h>
Public Types | |
typedef QMap< QString, QString > | QCassandraContextOptions |
A map of context options. | |
Public Member Functions | |
virtual | ~QCassandraContext () |
Clean up the QCassandraContext object. | |
void | clearCache () |
Clear the context cache. | |
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 QCassandraContextOptions & | descriptionOptions () 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< QCassandraTable > | findTable (const QString &table_name) const |
Search for a table. | |
QCassandraTable & | operator[] (const QString &table_name) |
Retrieve a table reference from a context. | |
const QCassandraTable & | operator[] (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< QCassandraTable > | table (const QString &table_name) |
Retrieve a table definition by name. | |
const QCassandraTables & | tables () 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 | unparent () |
Unparent the context. | |
void | updateTable (const QCassandraTable *table) |
Update a Cassandra table. | |
Private Attributes | |
QCassandra * | f_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 |
This class defines objects that can hold all the necessary information for a Cassandra keyspace definition.
A keyspace is similar to a context in the sense that to work on a keyspace you need to make it the current context. Whenever you use a context, this class automatically makes it the current context. This works well in a non threaded environment. In a threaded environment, you want to either make sure that only one thread makes use of the Cassandra objects or that you protect all the calls. This library does not.
You may think of this context as one database of an SQL environment. If you have used OpenGL, this is very similar to the OpenGL context.
Definition at line 50 of file QCassandraContext.h.
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.
QtCassandra::QCassandraContext::~QCassandraContext | ( | ) | [virtual] |
This function ensures that all resources allocated by the QCassandraContext are released.
Definition at line 170 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_]*\/
[in] | cassandra | The QCassandra object owning this context. |
[in] | context_name | The name of the Cassandra context. |
Definition at line 149 of file QCassandraContext.cpp.
References f_private.
void QtCassandra::QCassandraContext::clearCache | ( | ) |
This function clears the context cache. This means all the tables, their rows, and the cells of those rows all get cleared. None of these can be used after this call even if you kept a shared pointer to any of these objects.
Definition at line 851 of file QCassandraContext.cpp.
References f_tables.
Referenced by unparent().
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.
Definition at line 183 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 make use of it whether it is attached to the Cassandra cluster or not.
If you want to include tables in your new context, then create them before calling this function. It will be faster since you'll end up with one single request.
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 ... // add tables before calling create() if you also want tables context.create(context);
Definition at line 610 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.
[in] | table | The table definition used to create the Cassandra table. |
Definition at line 712 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.
[in] | option | The name of the option to retrieve. |
Definition at line 268 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.
Definition at line 242 of file QCassandraContext.cpp.
References f_options.
void QtCassandra::QCassandraContext::drop | ( | ) |
This function drops this context in the Cassandra database.
Note that contexts are dropped by name so we really only use the name of the context in this case.
The QCassandraContext object is still valid afterward, although, obviously no data can be read from or written to the Cassandra server since the context is gone from the cluster.
You may change the parameters of the context and call create() to create a new context with the same name.
Definition at line 664 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 (which means all its rows and cells are also deleted.) Do not use the table after this call, even if you kept a shared pointer to it. You may create a new one with the same name though.
Note that tables get dropped immediately from the Cassandra database (contrary to rows.) However, it can be a slow operation since all the nodes need to be notified (i.e. consistency of ALL.)
[in] | table_name | The name of the table to drop. |
Definition at line 689 of file QCassandraContext.cpp.
References QtCassandra::QCassandraPrivate::dropTable(), f_cassandra, f_tables, QtCassandra::QCassandra::getPrivate(), makeCurrent(), and table().
bool QtCassandra::QCassandraContext::durableWrites | ( | ) | const |
This function returns the durable flag that determines whether a context is temporary (false) or permanent (true).
Definition at line 455 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.)
[in] | table_name | The name of the table to retrieve. |
Definition at line 323 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.
[in] | table_name | The name of the table where the row is check is defined. |
[in] | row_key | The row for which this data is being counted. |
[in] | column_predicate | The predicate to use to count the cells. |
Definition at line 790 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.
[in] | table | The table where the results is saved. |
[in] | row_key | The row for which this data is being counted. |
[in] | column_predicate | The predicate to use to count the cells. |
Definition at line 804 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.
[in] | table | The table which is emitting this call. |
[in] | row_predicate | The predicate used to select the rows. |
Definition at line 838 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
.
[in] | table_name | Name of the table where the value is inserted. |
[in] | row_key | The key used to identify the row. |
[in] | column_key | The key used to identify the column. |
[in] | value | The new value of the cell. |
Definition at line 774 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.
[in] | table_name | Name of the table where the value is inserted. |
[in] | row_key | The key used to identify the row. |
[in] | column_key | The key used to identify the column. |
[in] | value | The new value of the cell. |
Definition at line 758 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 (i.e. table and cell editing.)
Note that whenever you all a function that requires this context to be current, this function is called.
Definition at line 572 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;
std::runtime_error | If the table doesn't exist, this function raises an exception since otherwise the reference would be a NULL pointer. |
[in] | table_name | The name of the table to retrieve. |
Definition at line 351 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];
std::runtime_error | If the table doesn't exist, this function raises an exception since otherwise the reference would be a NULL pointer. |
[in] | table_name | The name of the table to retrieve. |
Definition at line 379 of file QCassandraContext.cpp.
References findTable(), and table().
void QtCassandra::QCassandraContext::parseContextDefinition | ( | const void * | data | ) | [private] |
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.
[out] | data | The output keyspace definition. |
Definition at line 525 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.
[in] | table_name | The name of the column where the row is defined. |
[in] | row_key | The row in which the cell is to be removed, if empty all the rows. |
[in] | column_key | The cell to be removed, if empty all the cells. |
[in] | timestamp | The time when the key to be removed was created. |
[in] | consistency_level | The consistency level to use to remove this cell. |
Definition at line 821 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.
Definition at line 419 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
.
[in] | option | The option to set. |
[in] | value | The value to set. |
Definition at line 254 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.
[in] | options | The replacing options |
Definition at line 224 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.)
[in] | durable_writes | Set whether writes are durable. |
Definition at line 432 of file QCassandraContext.cpp.
References f_private.
void QtCassandra::QCassandraContext::setReplicationFactor | ( | int32_t | factor | ) |
This function sets the replication factor of the context.
[in] | factor | The new replication factor. |
Definition at line 395 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.
[in] | strategy_class | The new strategy class. |
Definition at line 196 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.
Definition at line 209 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.
[in] | table_name | The name of the table to retrieve. |
Definition at line 285 of file QCassandraContext.cpp.
References f_tables, and QCassandraTable.
Referenced by dropTable(), 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.)
Definition at line 308 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.
[in] | table | The table to drop from the Cassandra server. |
Definition at line 742 of file QCassandraContext.cpp.
References f_cassandra, QtCassandra::QCassandra::getPrivate(), makeCurrent(), and QtCassandra::QCassandraPrivate::truncateTable().
Referenced by QtCassandra::QCassandraTable::truncate().
void QtCassandra::QCassandraContext::unparent | ( | ) | [private] |
This function is called internally to mark the context as unparented. This means you cannot use it anymore. This happens whenever you call the dropContext() funtion on a QCassandra object.
Definition at line 868 of file QCassandraContext.cpp.
References clearCache(), and f_cassandra.
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 443 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 407 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 639 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.)
[in] | table | The table to update in the Cassandra server. |
Definition at line 729 of file QCassandraContext.cpp.
References f_cassandra, QtCassandra::QCassandra::getPrivate(), makeCurrent(), and QtCassandra::QCassandraPrivate::updateTable().
Referenced by QtCassandra::QCassandraTable::update().
friend class QCassandra [friend] |
Definition at line 108 of file QCassandraContext.h.
friend class QCassandraContextPrivate [friend] |
Definition at line 110 of file QCassandraContext.h.
friend class QCassandraPrivate [friend] |
Definition at line 109 of file QCassandraContext.h.
friend class QCassandraTable [friend] |
Definition at line 111 of file QCassandraContext.h.
Referenced by table().
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.
Note that when deleting a QCassandraContext object, you may still have a shared pointer referencing the object. This means the object itself will not be deleted. In that case, the f_cassandra parameter becomes NULL and calling functions that make use of it throw an error.
Definition at line 117 of file QCassandraContext.h.
Referenced by create(), createTable(), drop(), dropTable(), getCellCount(), getColumnSlice(), getRowSlices(), getValue(), insertValue(), makeCurrent(), remove(), truncateTable(), unparent(), 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 118 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 113 of file QCassandraContext.h.
Referenced by contextName(), durableWrites(), makeCurrent(), parseContextDefinition(), prepareContextDefinition(), QCassandraContext(), replicationFactor(), setDurableWrites(), setReplicationFactor(), setStrategyClass(), strategyClass(), unsetDurableWrites(), and unsetReplicationFactor().
QtCassandra::QCassandraContext::f_tables [private] |
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 119 of file QCassandraContext.h.
Referenced by clearCache(), create(), dropTable(), findTable(), parseContextDefinition(), prepareContextDefinition(), table(), and tables().
This document is part of the libQtCassandra Project.
Copyright by Made to Order Software Corp.