Created
July 22, 2014 07:35
-
-
Save ochinchina/b4f4ddca8eae1a46205d to your computer and use it in GitHub Desktop.
The foundationdb C API test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define FDB_API_VERSION 200 | |
#include <foundationdb/fdb_c.h> | |
#include <iostream> | |
#include <thread> | |
#include <string> | |
#include <unistd.h> | |
void init() { | |
fdb_error_t err = fdb_select_api_version( FDB_API_VERSION ); | |
if( err ) { | |
std::cout << "error:" << fdb_get_error( err ) << std::endl; | |
return; | |
} | |
err = fdb_setup_network(); | |
if( err ) { | |
std::cout << "error:" << fdb_get_error( err ) << std::endl; | |
return; | |
} | |
err = fdb_run_network(); | |
if( err ) { | |
std::cout << "error:" << fdb_get_error( err ) << std::endl; | |
return; | |
} | |
} | |
FDBCluster* create_cluster( const std::string& configFile ) { | |
//open the cluster configuration file | |
FDBFuture* future = fdb_create_cluster( configFile.c_str() ); | |
//waiting for future ready | |
fdb_error_t err = fdb_future_block_until_ready( future ); | |
if( err ) { | |
std::cout << "fdb_create_cluster, err:" << fdb_get_error( err ) << std::endl; | |
return 0; | |
} | |
//get the cluster returned by fdb_create_cluster | |
FDBCluster* cluster = 0; | |
err = fdb_future_get_cluster( future, &cluster ); | |
fdb_future_destroy( future ); | |
if( err ) { | |
std::cout << "fdb_future_get_cluster, err:" << fdb_get_error( err ) << std::endl; | |
return 0; | |
} | |
return cluster; | |
} | |
FDBDatabase* create_database( FDBCluster* cluster, const std::string& dbName ) { | |
//open a database, currently only "DB" is supported | |
FDBFuture* future = fdb_cluster_create_database( cluster, (const uint8_t*)dbName.data(), dbName.length() ); | |
//waiting for DB open complete | |
fdb_error_t err = fdb_future_block_until_ready( future ); | |
if( err ) { | |
std::cout << "fdb_cluster_create_database, err:" << fdb_get_error( err ) << std::endl; | |
return 0; | |
} | |
//get the returned database | |
FDBDatabase* db = 0; | |
err = fdb_future_get_database( future, &db ); | |
fdb_future_destroy( future ); | |
if( err ) { | |
std::cout << "fdb_future_get_database, err:" << fdb_get_error( err ) << std::endl; | |
return 0; | |
} | |
return db; | |
} | |
FDBTransaction* create_transaction( FDBDatabase* db ) { | |
//create a transaction | |
FDBTransaction* transaction = 0; | |
fdb_error_t err = fdb_database_create_transaction( db, &transaction ); | |
if( err ) { | |
std::cout << "err:" << fdb_get_error( err ) << std::endl; | |
return 0; | |
} | |
return transaction; | |
} | |
bool set( FDBDatabase* db, const std::string& key, const std::string& value ) { | |
FDBTransaction* transaction = create_transaction( db ); | |
//save the key = "test1" and value="hello" to the database | |
fdb_transaction_set( transaction, (const uint8_t*)key.data(), key.length(), (const uint8_t*)value.data(), value.length() ); | |
//commit the saving | |
FDBFuture* future = fdb_transaction_commit( transaction ); | |
//waiting for the commit complete | |
fdb_error_t err = fdb_future_block_until_ready( future ); | |
fdb_future_destroy( future ); | |
fdb_transaction_destroy( transaction ); | |
if( err ) { | |
std::cout << "err:" << fdb_get_error( err ) << std::endl; | |
return false; | |
} | |
return true; | |
} | |
bool read( FDBDatabase* db, const std::string& key, std::string& value ) { | |
FDBTransaction* transaction = create_transaction( db ); | |
FDBFuture* future = fdb_transaction_get( transaction, (const uint8_t*)key.data(), key.length(), true ); | |
//waiting for read operation completed | |
fdb_error_t err = fdb_future_block_until_ready( future ); | |
if( err ) { | |
std::cout << "err:" << fdb_get_error( err ) << std::endl; | |
return false; | |
} | |
//get the return value | |
fdb_bool_t out_present = false; | |
const uint8_t* tmp_value = 0; | |
int tmp_value_len = 0; | |
err = fdb_future_get_value( future, &out_present, &tmp_value, &tmp_value_len ); | |
fdb_future_destroy( future ); | |
fdb_transaction_destroy( transaction ); | |
if( err ) { | |
std::cout << "err:" << fdb_get_error( err ) << std::endl; | |
return false; | |
} | |
if( out_present ) { | |
value = std::string( (const char*)tmp_value, tmp_value_len ); | |
return true; | |
} | |
return false; | |
} | |
bool clear( FDBDatabase* db, const std::string& key ) { | |
FDBTransaction* transaction = create_transaction( db ); | |
fdb_transaction_clear( transaction, (const uint8_t*)key.data(), key.length() ); | |
//commit the clear | |
FDBFuture* future = fdb_transaction_commit( transaction ); | |
//waiting for clear operation completed | |
fdb_error_t err = fdb_future_block_until_ready( future ); | |
fdb_future_destroy( future ); | |
fdb_transaction_destroy( transaction ); | |
if( err ) { | |
std::cout << "fdb_transaction_clear, err=" << fdb_get_error( err ) << std::endl; | |
return false; | |
} | |
return true; | |
} | |
void test() { | |
//open the cluster configuration file | |
FDBCluster* cluster = create_cluster( "/etc/foundationdb/fdb.cluster"); | |
//open a database, currently only "DB" is supported | |
FDBDatabase* db = create_database( cluster, "DB" ); | |
//save the key = "test1" and value="hello" to the database | |
for( int i = 0; i < 100; i++ ) { | |
set( db, "test" + std::to_string( i ), "hello" + std::to_string( i ) ); | |
} | |
std::string value; | |
if( read( db, "test66", value ) ) { | |
std::cout << value << std::endl; | |
} | |
if( clear( db, "test67") ) { | |
std::cout << "success to clear key:test67" << std::endl; | |
} | |
fdb_database_destroy( db ); | |
fdb_cluster_destroy( cluster ); | |
} | |
int main( int argc, char** argv ) { | |
std::thread th( init ); | |
::sleep( 1 ); | |
test(); | |
th.join(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment