UnQLite
Embeddable NoSQL Database
NEXT 2기 김명찬
NoSQL?
• vs SQL
• 관계형 데이터베이스를 사용하지 않음
• 단순 저장, 검색에 편리.
• Scale-out 가능(vs Scale-up)
• SQL 쿼리처럼 사용할 수도 있음. (Not only SQL)
• ACID 보장성이 떨어짐..
DB-Engine Rangine
http://db-engines.com/en/ranking_trend
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
UnQLite
Self-contained C lib
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
Getting started
• https://www.unqlite.org/downloads.html
database open/close
#include <unqlite.h>
…
unqlite *pDb;
int rc;
rc = unqlite_open(&pDb,"test.db",UNQLITE_OPEN_CREATE);
//Auto-commit the transaction and close our handle
unqlite_close(pDb);
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값 나올때까지 저장.
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'
}
}
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);
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 ; }
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);
}
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);
Delete Data
unqlite_kv_delete(pDb,"test",-1);
Make random string
char zKey[12]; //Random generated key
char zData[34] = "DATA!!"; //Dummy DATA
unqlite_util_random_string(pDb, zKey, sizeof(zKey));
// Perform the insertion
rc = unqlite_kv_store(pDb, zKey, sizeof(zKey), zData,
sizeof(zData));
if (rc != UNQLITE_OK) {
break;
}
Read File
/********Read File***********/
void *pMap;
unqlite_int64 iSize;
int rc, i;
const char *zName = "license.txt"; //Name of the target file
// Obtain a read-only memory view of the target file;
rc = unqlite_util_load_mmaped_file(zName, &pMap, &iSize);
// Store the whole file in our database;
rc = unqlite_kv_store(pDb, zName, -1, pMap, iSize);
// Discard the memory view;
unqlite_util_release_mmaped_file(pMap, iSize);
Read File
rollback
if( rc != UNQLITE_BUSY && rc != UNQLITE_NOTIMPLEMENTED ){
/* Rollback */
unqlite_rollback(pDb);
}
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().
Jx9 script
unqlite_vm *pVm;
rc = unqlite_compile(pDb,JX9_PROG,sizeof(JX9_PROG)-1,&pVm);
/* Install a VM output consumer callback */
rc = unqlite_vm_config(pVm,UNQLITE_VM_CONFIG_OUTPUT,OutputConsumer,0);
/* Execute our script */
rc = unqlite_vm_exec(pVm);
/* Finally, release our VM */
unqlite_vm_release(pVm);
감사합니다.

Unqlite

  • 1.
  • 2.
    NoSQL? • vs SQL •관계형 데이터베이스를 사용하지 않음 • 단순 저장, 검색에 편리. • Scale-out 가능(vs Scale-up) • SQL 쿼리처럼 사용할 수도 있음. (Not only SQL) • ACID 보장성이 떨어짐..
  • 3.
  • 4.
    NoSQL classification byData model • Key – value DB • Riak, Vodemort, Tokyo • Wide Columnar Store • Hbase, Cassandra, Hypertable • Document DB • Mongo DB, Couch DB • Graph DB • Neo4J, OreientDB
  • 5.
  • 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
  • 7.
  • 8.
    database open/close #include <unqlite.h> … unqlite*pDb; int rc; rc = unqlite_open(&pDb,"test.db",UNQLITE_OPEN_CREATE); //Auto-commit the transaction and close our handle unqlite_close(pDb);
  • 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 valueby 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 acursor – 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 acursor – 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 acursor - realese … /* Finally, Release our cursor */ unqlite_kv_cursor_release(pDb, pCursor); //Auto-commit the transaction and close our handle unqlite_close(pDb);
  • 15.
  • 16.
    Make random string charzKey[12]; //Random generated key char zData[34] = "DATA!!"; //Dummy DATA unqlite_util_random_string(pDb, zKey, sizeof(zKey)); // Perform the insertion rc = unqlite_kv_store(pDb, zKey, sizeof(zKey), zData, sizeof(zData)); if (rc != UNQLITE_OK) { break; }
  • 17.
    Read File /********Read File***********/ void*pMap; unqlite_int64 iSize; int rc, i; const char *zName = "license.txt"; //Name of the target file // Obtain a read-only memory view of the target file; rc = unqlite_util_load_mmaped_file(zName, &pMap, &iSize); // Store the whole file in our database; rc = unqlite_kv_store(pDb, zName, -1, pMap, iSize); // Discard the memory view; unqlite_util_release_mmaped_file(pMap, iSize);
  • 18.
  • 19.
    rollback if( rc !=UNQLITE_BUSY && rc != UNQLITE_NOTIMPLEMENTED ){ /* Rollback */ unqlite_rollback(pDb); }
  • 20.
    Jx9 script /* Createthe 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().
  • 21.
    Jx9 script unqlite_vm *pVm; rc= unqlite_compile(pDb,JX9_PROG,sizeof(JX9_PROG)-1,&pVm); /* Install a VM output consumer callback */ rc = unqlite_vm_config(pVm,UNQLITE_VM_CONFIG_OUTPUT,OutputConsumer,0); /* Execute our script */ rc = unqlite_vm_exec(pVm); /* Finally, release our VM */ unqlite_vm_release(pVm);
  • 22.