v1.10

Document

How to Start SequoiaDB

Basic Operation

Data Model

Database Management

Aggregation

Index

Replication

Cluster

Connector

Reference

Development

Release Note

FAQ

  • Content
  • Comments
  • History

C Development Foundation

This section introduce the approach to run SequiaDB in C application. First, you should make sure that you have install. Please refer to the installation page for more about installation information.

Edit client code

For the purpose of simpleness, the following sample is not complete code. Please get complete source code in the path "/sequoiadb/client/samples/C".

  • Connecting Database

    There is a complete client file called "connect.c" to show how to connect to database in a C applicaiton. The file includes a head file called "client.h".

    #include <stdio.h>
    #include "client.h"
    // Display Syntax Error
    void displaySyntax ( CHAR *pCommand )
    {
       printf ( "Syntax: %s< hostname > < servicename > < username > < password >" OSS_NEWLINE, pCommand ) ;
    }
    
    INT32 main ( INT32 argc, CHAR **argv )
    {
       // define a connecion handle; use to connect to database
       sdbConnectionHandle connection = 0 ;
       INT32 rc = SDB_OK ;
    
       // verify syntax
       if ( 5 != argc )
       {
          displaySyntax ( (CHAR*)argv[0] ) ;
          exit ( 0 ) ;
       }
    
       // read argument
       CHAR *pHostName = (CHAR*)argv[1] ;
       CHAR *pServiceName = (CHAR*)argv[2] ;
       CHAR *pUsr = (CHAR*)argv[3] ;
       CHAR *pPasswd = (CHAR*)argv[4] ;
    
       // connect to database
       rc = sdbConnect ( pHostName, pServiceName, pUsr, pPasswd, &connection ) ;
       if( rc!=SDB_OK )
       {
          printf("Fail to connet to database, rc = %d" OSS_NEWLINE, rc ) ;
          goto error ;
       }
    done:
       // disconnect from database
       sdbDisconnect ( connection ) ;
       // release connection
       sdbReleaseConnection ( connection ) ;
       return 0 ;
    error:
       goto done ;
    }

    In Linux we can compile and link to dynamically linked librarie file called "libsdbc.so" as follow:

    $gcc -o connect connect.c -I /path_to/C/include -lsdbc -L/path-to/client/C/lib
    $ ./connect localhost 11810 "" ""
    connect success!
    Note:

    This sample link to the port of 11810 of local database. It log in with null user name and null password. Users need to configurate parameters according to their own situation. But if database has created a user, it can connect to database with it and its password.

  • Inserting Data

    SequoiaDB stores data in the format of BSON. BSON is a binary object similar to JSON. In order to store data, we should create a BSON object. The following sample insert the record {name:"Tom",age:24} into a collection:

    // Firstly, a BSON object is created.
    INT32 rc = SDB_OK ;
    bson obj ;
    bson_init( &obj ) ;
    bson_append_string( &obj, "name", "tom" ) ;
    bson_append_int( &obj, "age", 24 ) ;
    rc = bson_finish( &obj ) ;
    if ( rc != SDB_OK )
       printf("Error.");
    // Secondly, insert it into a collection.
    dbInsert ( collection, &obj ) ;
  • Query
    In a query, a cursor handle is used to store the result set of a query. You can get results by manipulating the cursor. This sample uses the sdbNext interface of cursor manipulation to get one record in the result set.
    // Define a cursor handle.
    dbCursorHandle cursor = 0 ;
    …
    // Search for all records and put the results in the cursor handle.
    sdbQuery(collection, NULL, NULL, NULL, NULL, 0, -1, &cursor ) ;
    // Show every record in cursor.
    bson_init(obj);
    while( !( rc=sdbNext( cursor, &obj ) ) )
    {
       bson_print( &obj ) ;
       bson_destroy(&obj) ;
       bson_init(&obj);
    }
    bson_destroy(obj) ;
  • Index
    Here, we create an index in the collection specified by the collection handle collection . It is in ascending order on "name" and descending order on "age".
    #define INDEX_NAME "index"
    …
    // Firstly we create a bson that contains the information of the index.
    bson_init( &obj ) ;
    bson_append_int( &obj, "name", 1 ) ;
    bson_append_int( &obj, "age", -1 ) ;
    rc = bson_finish( &obj ) ;
    if ( rc != SDB_OK )
       printf("Error.");
    // Create an index with the BOSN object.
    sdbCreateIndex ( collection, &obj, INDEX_NAME, FALSE, FALSE ) ;
    bson_destroy ( &obj ) ;
  • Updating data

    Here, we create an index in the collection specified by the collection handle collection This sample doesn't contain any matching condition, so it will update all the records in the collection.

    // Create a BSON object that contains updating rules
    bson_init( &rule ) ;
    bson_append_start_object ( &rule, "$set" ) ;
    bson_append_int ( &rule, "age", 19 ) ;
    bson_append_finish_object ( &rule ) ;
    rc = bson_finish ( &rule ) ;
    if ( rc != SDB_OK )
       printf("Error.");
    // Print updating rule.
    bson_print( &rule ) ;
    // Update records.
    sdbUpdate( collection, &rule, NULL, NULL ) ;
    bson_destroy(&rule);

    Here,as no record matching conditions have specified,then this example will update all records in the collection which collection handle specified.

Cluster Operations

  • ReplicaSet Operations
    ReplicaSet Operations contains creating ReplicaSets(sdbCreateShard)、getting ReplicaSet handles(sdbGetShard)、starting ReplicaSet(sdbStartShard) and stopping ReplicaSet(sdbStopShard). The following shows the ReplicaSet Operations, and real application should include error detecting ,etc.
    // define a ReplicaSet Handle
    sdbShardHandle shard = 0 ;
    …
    // create a catalog ReplicaSet at first
    sdbCreateCataShard ( connection, HOST_NAME, SERVICE_NAME, CATALOG_SET_PATH , NULL ) ;
    // sleep 5s to wait starting catalog ReplicaSet and electing master and slave.
    sleep( 5 ) ;
    // create data ReplicaSet
    sdbCreateShard ( connection, GROUP_NAME, &rg ) ;
    // create data nodes
    sdbCreateNode ( rd, HOST_NAME1, SERVICE_NAME1, DATABASE_PATH1, NULL ) ;
    // start ReplicaSet
    sdbStartShard( shard ) ;
  • Data Node Operations
    Data Node Operations contains create data node(sdbCreateNode)、get master data node(sdbGetNodeMaster)、get slave data node(sdbGetNodeSlave)、start data node (sdbStartNode) and stop data node(sdbStopNode), etc. The following shows the Data Node Operations, and real application should include error detecting, etc.
    // define a data node handle
    sdbNodeHandle masternode = 0 ;
    sdbNodeHandle slavenode = 0 ;
    …
    // sleep 10s to wait starting data ReplicaSet and electing master and slave
    sleep( 10 ) ;
    //get master data node
    sdbGetNodeMaster ( shard, &masternode ) ;
    //get slave data node
    sdbGetNodeSlave ( shard, &slavenode ) ; 
please login to comment.
Latest Comment
  • 2015-03-14

About Us

SequoiaDB is a financial-level distributed database vendor and is the first Chinese database listed in Gartner’s Magic Quadrant OPDBMS report. SequoiaDB has recently released version 3.0.
SequoiaDB is now penetrating the vertical sector Financial Industry quickly and had more than 50 banking clients and hundreds of enterprise customers in industries including government, telecommunication, Internet and IoT.

Beijing:
Tower R, No.8 North Star East Road, Chaoyang District, Beijing,China
Guangzhou:
Tower A, No.22 Qinglan Street, Panyu District, Guangzhou,China
Shenzhen:
Tsing Hua Tech Park, Nanshan District, Shenzhen,China
Tel:400-8038-339
E-mail:contact@sequoiadb.com