EJDB - 類似MongoDB的嵌入式JSON數據庫引擎
EJDB是一個可以嵌在C/C++應用中使用,類似于MongoDB的JSON數據庫引擎。
EJDB是一個C庫,基于Tokyo Cabinet的一個修復版。JSON的數據查詢和展示,基于 C BSON實現。
特性:
- 類似MongoDB的查詢和整體理念。
- 集合級寫入鎖定。
- 集合 級別的事務
- 字符串標記匹配查詢。
- Node.js 綁定。 </ul>
#include <tcejdb/ejdb.h> static EJDB *jb; int main() { jb = ejdbnew(); if (!ejdbopen(jb, "addressbook", JBOWRITER | JBOCREAT | JBOTRUNC)) { return 1; } //Get or create collection 'contacts' EJCOLL *coll = ejdbcreatecoll(jb, "contacts", NULL); bson bsrec; bson_oid_t oid; //Insert one record: //JSON: {'name' : 'Bruce', 'phone' : '333-222-333', 'age' : 58} bson_init(&bsrec); bson_append_string(&bsrec, "name", "Bruce"); bson_append_string(&bsrec, "phone", "333-222-333"); bson_append_int(&bsrec, "age", 58); bson_finish(&bsrec); //Save BSON ejdbsavebson(coll, &bsrec, &oid); fprintf(stderr, "\nSaved Bruce"); bson_destroy(&bsrec); //Now execute query //QUERY: {'name' : {'$begin' : 'Bru'}} //Name starts with 'Bru' string bson bq1; bson_init_as_query(&bq1); bson_append_start_object(&bq1, "name"); bson_append_string(&bq1, "$begin", "Bru"); bson_append_finish_object(&bq1); bson_finish(&bq1); EJQ *q1 = ejdbcreatequery(jb, &bq1, NULL, 0, NULL); uint32_t count; TCLIST *res = ejdbqrysearch(coll, q1, &count, 0, NULL); fprintf(stderr, "\n\nRecords found: %d\n", count); //Now print the result set records for (int i = 0; i < TCLISTNUM(res); ++i) { void *bsdata = TCLISTVALPTR(res, i); bson_print_raw(stderr, bsdata, 0); } fprintf(stderr, "\n"); //Dispose result set tclistdel(res); //Dispose query ejdbquerydel(q1); bson_destroy(&bq1); //Close database ejdbclose(jb); ejdbdel(jb); return 0; }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!