4. NoSQL classification by Data model
• Key – value DB
• Riak, Vodemort, Tokyo
• Wide Columnar Store
• Hbase, Cassandra, Hypertable
• Document DB
• Mongo DB, Couch DB
• Graph DB
• Neo4J, OreientDB
6. UnQLite
• NoSQL
• Serverless
• Zero configuration
• Single DB file. (no temp files)
• ACID
• Key/value store, Document store
• Support O(1) lookup.
• Thread safe
• Cross-platform file format
9. Store data
rc = unqlite_kv_store(pDb,"test",-1,"Hello World",11);
//test => 'Hello World'
rc = unqlite_kv_store_fmt(pDb,“date",-1,
“Current date : %d:%d:%d",2016, 05, 16);
//test => 'Hello World'
참고)
-1 : length.
음수일 경우에는 null값 나올때까지 저장.
10. Append Data
rc = unqlite_kv_append(pDb,"msg",-1,"Hello, ",7);
//msg => 'Hello, '
if( rc == UNQLITE_OK ){
//The second chunk
rc = unqlite_kv_append(pDb,"msg",-1,
"Current time is: ",17);
//msg => 'Hello, Current time is: '
if( rc == UNQLITE_OK ){
//The last formatted chunk
rc = unqlite_kv_append_fmt(pDb,"msg",-1,
"%d:%d:%d",10,16,53);
//msg => 'Hello, Current time is: 10:16:53'
}
}
11. Get a value by Key
//Extract data size first
rc = unqlite_kv_fetch(pDb, "date", -1, NULL, &nBytes);
if (rc != UNQLITE_OK) { return; }
//Allocate a buffer big enough to hold the record content
zBuf = (char *)malloc(nBytes);
if (zBuf == NULL) { return; }
//Copy record content in our buffer
unqlite_kv_fetch(pDb, "date", -1, zBuf, &nBytes);
//Play with zBuf...
//Close our database handle
unqlite_close(pDb);
12. Get by a cursor – set range
// Open our database;
rc = unqlite_open(&pDb, "test.db", UNQLITE_OPEN_CREATE);
if (rc != UNQLITE_OK) { return ; }
//Store some records unqlite_kv_store(), unqlite_kv_append()...
/* Allocate a new cursor instance */
rc = unqlite_kv_cursor_init(pDb, &pCursor);
if (rc != UNQLITE_OK) { return ; }
/* Point to the last record */
rc = unqlite_kv_cursor_last_entry(pCursor);
if (rc != UNQLITE_OK) { return ; }
13. Get by a cursor – get
…
/* Iterate over the records */
while (unqlite_kv_cursor_valid_entry(pCursor)) {
/* Consume the key */
printf("nKey ==> ");
unqlite_kv_cursor_key_callback(pCursor, DataConsumerCallback, 0);
/* Extract data length */
unqlite_kv_cursor_data(pCursor, NULL, &iData);
/* Consume the data */
printf("Data => ");
unqlite_kv_cursor_data_callback(pCursor, DataConsumerCallback, 0);
printf("Data length ==> %lldnt", iData);
/* Point to the previous record */
unqlite_kv_cursor_prev_entry(pCursor);
}
14. Get by a cursor - realese
…
/* Finally, Release our cursor */
unqlite_kv_cursor_release(pDb, pCursor);
//Auto-commit the transaction and close our handle
unqlite_close(pDb);
20. Jx9 script
/* Create the collection 'users' */
if( !db_exists('users') ){
/* Try to create it */
$rc = db_create('users');
}
//The following is the JSON objects to be stored shortly in our 'users' collection
$zRec = [{
name : 'james',
age : 27,
mail : 'dude@example.com'
}];
//Store our records
$rc = db_store('users',$zRec);
//One more record
$rc = db_store('users',{ name : 'alex', age : 19, mail : 'alex@example.com' });
print "Total number of stored records: ",db_total_records('users'),JX9_EOL;
//Fetch data using db_fetch_all(), db_fetch_by_id() and db_fetch().