Storage::Private::Private(const QString &s, const QString &n, AccessMode m, bool duplicates) : storageRoot(s), name(n), mode(m), db(0), allowDuplicates(duplicates), //FIXME: currently does nothing ... should do what it says inTransaction(false) { const QString fullPath(storageRoot + s_unqliteDir + name); QDir dir; dir.mkpath(storageRoot + s_unqliteDir); //create file int openFlags = UNQLITE_OPEN_CREATE; if (mode == ReadOnly) { openFlags |= UNQLITE_OPEN_READONLY | UNQLITE_OPEN_MMAP; } else { openFlags |= UNQLITE_OPEN_READWRITE; } int rc = unqlite_open(&db, fullPath.toStdString().data(), openFlags); if (rc != UNQLITE_OK) { reportDbError("unqlite_open"); } }
static int edb_set(char *path, char *key, char *value) { unqlite *pDb; /* Database handle */ int rc; /* Open our database */ while (1) { rc = unqlite_open(&pDb, path, UNQLITE_OPEN_CREATE); if( rc != UNQLITE_OK ){ syslog(LOG_WARNING, "db_set: unqlite_open failed with rc: %d\n", rc); rc = -1; break; } rc = unqlite_kv_store(pDb, key, -1, value, MAX_BUF); if( rc != UNQLITE_OK ){ if( rc != UNQLITE_BUSY ){ #ifdef DEBUG syslog(LOG_WARNING, "db_set: unqlite_kv_store failed with rc: %d\n", rc); #endif rc = -1; break; } else { unqlite_close(pDb); continue; } } else { /* Auto-commit the transaction and close our database */ rc = 0; break; } } unqlite_close(pDb); return rc; }
/* No need for command line arguments, everything is stored in-memory */ int main(void) { unqlite *pDb; /* Database handle */ unqlite_vm *pVm; /* UnQLite VM resulting from successful compilation of the target Jx9 script */ int rc; puts(zBanner); /* Open our database */ rc = unqlite_open(&pDb,":mem:" /* In-mem DB */,UNQLITE_OPEN_CREATE); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); } /* Compile our Jx9 script defined above */ rc = unqlite_compile(pDb,JX9_PROG,sizeof(JX9_PROG)-1,&pVm); if( rc != UNQLITE_OK ){ /* Compile error, extract the compiler error log */ const char *zBuf; int iLen; /* Extract error log */ unqlite_config(pDb,UNQLITE_CONFIG_JX9_ERR_LOG,&zBuf,&iLen); if( iLen > 0 ){ puts(zBuf); } Fatal(0,"Jx9 compile error"); } /* Install a VM output consumer callback */ rc = unqlite_vm_config(pVm,UNQLITE_VM_CONFIG_OUTPUT,VmOutputConsumer,0); if( rc != UNQLITE_OK ){ Fatal(pDb,0); } /* Execute our script */ rc = unqlite_vm_exec(pVm); if( rc != UNQLITE_OK ){ Fatal(pDb,0); } /* Release our VM */ unqlite_vm_release(pVm); /* Auto-commit the transaction and close our database */ unqlite_close(pDb); return 0; }
static int edb_get(char *path, char *key, char *value) { int rc; unqlite *pDb; size_t nBytes; //Data length char zBuf[MAX_BUF] = {0}; while (1) { // Open our database; rc = unqlite_open(&pDb, path, UNQLITE_OPEN_READONLY|UNQLITE_OPEN_MMAP); if( rc != UNQLITE_OK ) { syslog(LOG_WARNING, "db_get: unqlite_open fails with rc: %d\n", rc); unqlite_close(pDb); return -1; } //Extract record content nBytes = MAX_BUF; memset(zBuf, 0, MAX_BUF); rc = unqlite_kv_fetch(pDb, key, -1, value, &nBytes); if( rc != UNQLITE_OK ){ if (rc == UNQLITE_NOTFOUND) { #ifdef DEBUG syslog(LOG_WARNING, "db_get: can not find the key\n"); #endif unqlite_close(pDb); return -1; } else { #ifdef DEBUG syslog(LOG_WARNING, "db_get: unqlite_key_fetch returns %d\n", rc); #endif /* Auto-commit the transaction and close our database */ unqlite_close(pDb); continue; } } else { /* Auto-commit the transaction and close our database */ unqlite_close(pDb); return 0; } } return 0; }
bool KVUnqlite::open(const std::string& path) { if (isOpen()) return false; _path = path; unqlite* pDbHandle = 0; if (unqlite_open(&pDbHandle, path.c_str(), UNQLITE_OPEN_CREATE | UNQLITE_OPEN_READWRITE) != UNQLITE_OK || !pDbHandle) { log_error(); return false; } _pDbHandle = pDbHandle; return true; }
static PyObject* wrap_unqlite_open(PyObject* self, PyObject *args) { const char *zFilename; unsigned int iMode; struct unqlite *pDb; struct unqlite_vm *pVm; int rc; if (!PyArg_ParseTuple(args,"sl",&zFilename,&iMode)) return NULL; rc = unqlite_open(&pDb,zFilename,iMode); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); return Py_None; } return Py_BuildValue("(l,l)",rc,pDb); }
int main(int argc,char *argv[]) { int db_extract = 0; /* TRUE to extract records from the dabase */ int db_store = 0; /* TRUE to store files in the database */ int db_iterate = 0; /* TRUE to iterate over the inserted elements */ unqlite *pDb; /* Database handle */ int c,i,rc; if( argc < 3 ){ /* Missing database name */ Help(); } c = argv[2][0]; if( c != '-' ){ /* Missing command */ Help(); } c = argv[2][1]; if( c == 'i' || c == 'I' ){ /* Iterate over the inserted elements */ db_iterate = 1; }else if( c == 'w' || c == 'W' ){ /* Store some files */ db_store = 1; }else{ /* Extract some records */ db_extract = 1; } /* Open our database */ rc = unqlite_open(&pDb,argv[1],db_store ? UNQLITE_OPEN_CREATE : (UNQLITE_OPEN_READONLY|UNQLITE_OPEN_MMAP) /* Read-only DB */ ); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); } if( db_store ){ void *pMap; /* Read-only memory view of the target file */ unqlite_int64 nSize; /* file size */ /* Start the insertion */ for( i = 3 ; i < argc ; ++i ){ const char *zFile = argv[i]; printf("Inserting %s\t ... ",zFile); /* Obtain a read-only memory view of the whole file */ rc = unqlite_util_load_mmaped_file(zFile,&pMap,&nSize); if( rc == UNQLITE_OK ){ /* Store the whole file */ rc = unqlite_kv_store(pDb,zFile,-1,pMap,nSize); /* Discard this view */ unqlite_util_release_mmaped_file(pMap,nSize); } puts(rc == UNQLITE_OK ? "OK" : "Fail"); } /* Mnually commit the transaction. * In fact, a call to unqlite_commit() is not necessary since UnQLite * will automatically commit the transaction during a call to unqlite_close(). */ rc = unqlite_commit(pDb); if( rc != UNQLITE_OK ){ /* Rollback the transaction */ rc = unqlite_rollback(pDb); } if( rc != UNQLITE_OK ){ /* Something goes wrong, extract the database error log and exit */ Fatal(pDb,0); } }else if( db_iterate ){ /* Iterate over the inserted records */ unqlite_kv_cursor *pCur; /* Allocate a new cursor instance */ rc = unqlite_kv_cursor_init(pDb,&pCur); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); } /* Point to the first record */ unqlite_kv_cursor_first_entry(pCur); /* Iterate over the entries */ while( unqlite_kv_cursor_valid_entry(pCur) ){ unqlite_int64 nDataLen; /* Consume the key */ unqlite_kv_cursor_key_callback(pCur,DataConsumerCallback,0); /* Extract the data size */ unqlite_kv_cursor_data(pCur,0,&nDataLen); printf(":\t %ld Bytes\n",nDataLen); /* unqlite_kv_cursor_data_callback(pCur,DataConsumerCallback,0); */ /* Point to the next entry */ unqlite_kv_cursor_next_entry(pCur); } /* Finally, Release our cursor */ unqlite_kv_cursor_release(pDb,pCur); }else{ /* Extract one more records */ for( i = 3 ; i < argc ; ++i ){ const char *zFile = argv[i]; rc = unqlite_kv_fetch_callback(pDb,zFile,-1,DataConsumerCallback,0); if( rc == UNQLITE_NOTFOUND ){ printf("No such record: %s\n",zFile); } } } /* All done, close our database */ unqlite_close(pDb); return 0; }
Producer::Config::Config( const string& file, uint32_t id ) { // set default values prefix = Name("unnamed"); sigverif_delay = NanoSeconds( 30345 ); bloom_delay = NanoSeconds( 2535 ); // database and vm structs unqlite* db; unqlite_vm* vm; // initialize database int rc = unqlite_open( &db, ":mem:", UNQLITE_OPEN_READONLY ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: creating unqlite database: " << err << endl; exit(1); } // initialize unqlite vm rc = unqlite_compile_file( db, file.c_str(), &vm ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: compiling config script: " << err << endl; exit(1); } unqlite_value* id_val = unqlite_vm_new_scalar( vm ); unqlite_value_int64( id_val, id ); rc = unqlite_vm_config( vm, UNQLITE_VM_CONFIG_CREATE_VAR, "ID", id_val ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: exporting ID to config: " << err << endl; exit(1); } unqlite_vm_release_value( vm, id_val ); // execute config script rc = unqlite_vm_exec( vm ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: executing config script: " << err << endl; exit(1); } // retrieve config values const char* str; int len; unqlite_value* val; val = unqlite_vm_extract_variable( vm, "prefix" ); if( val ) { str = unqlite_value_to_string( val, &len ); prefix = string(str, len ); } val = unqlite_vm_extract_variable( vm, "sigverif_delay" ); if( unqlite_value_is_float( val ) ) sigverif_delay = Seconds( unqlite_value_to_double( val ) ); if( unqlite_value_is_int( val ) ) sigverif_delay = Seconds( unqlite_value_to_int64( val ) ); val = unqlite_vm_extract_variable( vm, "bloom_delay" ); if( unqlite_value_is_float( val ) ) bloom_delay = Seconds( unqlite_value_to_double( val ) ); if( unqlite_value_is_int( val ) ) bloom_delay = Seconds( unqlite_value_to_int64( val ) ); val = unqlite_vm_extract_variable( vm, "contents" ); if( val && unqlite_value_is_json_array( val ) ) { size_t count = unqlite_array_count( val ); for( size_t i = 0 ; i < count ; i++ ) { stringstream ss; string key; ss << i; ss >> key; unqlite_value* elem = unqlite_array_fetch ( val, key.c_str(), key.size() ); if( elem && unqlite_value_is_json_object( elem ) ) { unqlite_value* name_val = unqlite_array_fetch ( elem, "name", -1 ); unqlite_value* size_val = unqlite_array_fetch ( elem, "size", -1 ); unqlite_value* access_val = unqlite_array_fetch( elem, "access_level", -1 ); if( name_val && size_val && access_val && unqlite_value_is_string( name_val ) && unqlite_value_is_int( size_val ) && unqlite_value_is_int( access_val ) ) { str = unqlite_value_to_string( name_val, &len ); size_t size = unqlite_value_to_int( size_val ); uint8_t access_level = unqlite_value_to_int ( access_val ); contents.emplace( string( str, len ), Config::Content { size, access_level } ); } } } }
int main(int argc,char *argv[]) { unqlite *pDb; /* Database handle */ unqlite_kv_cursor *pCur; /* Cursor handle */ char zKey[14]; /* Random generated key */ char zData[32]; /* Dummy data */ int i,rc; puts(zBanner); /* Open our database */ rc = unqlite_open(&pDb,argc > 1 ? argv[1] /* On-disk DB */ : ":mem:" /* In-mem DB */,UNQLITE_OPEN_CREATE); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); } printf("Starting insertions of %d random records...\n",MAX_RECORDS); /* Start the random insertions */ for( i = 0 ; i < MAX_RECORDS; ++i ){ /* Genearte the random key first */ 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 ){ /* Something goes wrong */ break; } } if( rc != UNQLITE_OK ){ /* Something goes wrong, extract the database error log and exit */ Fatal(pDb,0); } puts("Done...Starting the iteration process"); /* Allocate a new cursor instance */ rc = unqlite_kv_cursor_init(pDb,&pCur); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); } /* Point to the first record */ unqlite_kv_cursor_first_entry(pCur); /* To point to the last record instead of the first, simply call [unqlite_kv_cursor_last_entry()] as follows */ /* unqlite_kv_cursor_last_entry(pCur); */ /* Iterate over the entries */ while( unqlite_kv_cursor_valid_entry(pCur) ){ int nKeyLen; /* unqlite_int64 nDataLen; */ /* Consume the key */ unqlite_kv_cursor_key(pCur,0,&nKeyLen); /* Extract key length */ printf("\nKey ==> %u\n\t",nKeyLen); unqlite_kv_cursor_key_callback(pCur,DataConsumerCallback,0); /* Consume the data */ /* unqlite_kv_cursor_data(pCur,0,&nDataLen); printf("\nData ==> %lld\n\t",nDataLen); unqlite_kv_cursor_data_callback(pCur,DataConsumerCallback,0); */ /* Point to the next entry */ unqlite_kv_cursor_next_entry(pCur); /*unqlite_kv_cursor_prev_entry(pCur); //If [unqlite_kv_cursor_last_entry(pCur)] instead of [unqlite_kv_cursor_first_entry(pCur)] */ } /* Finally, Release our cursor */ unqlite_kv_cursor_release(pDb,pCur); /* Auto-commit the transaction and close our database */ unqlite_close(pDb); return 0; }
/* No need for command line arguments, everything is stored in-memory */ int main(void) { unqlite *pDb; /* Database handle */ unqlite_vm *pVm; /* UnQLite VM resulting from successful compilation of the target Jx9 script */ int rc; puts(zBanner); /* Open our database */ rc = unqlite_open(&pDb,":mem:" /* In-mem DB */,UNQLITE_OPEN_CREATE); if( rc != UNQLITE_OK ) { Fatal(0,"Out of memory"); } /* Compile our Jx9 script defined above */ rc = unqlite_compile(pDb,JX9_PROG,sizeof(JX9_PROG)-1,&pVm); if( rc != UNQLITE_OK ) { /* Compile error, extract the compiler error log */ const char *zBuf; int iLen; /* Extract error log */ unqlite_config(pDb,UNQLITE_CONFIG_JX9_ERR_LOG,&zBuf,&iLen); if( iLen > 0 ) { puts(zBuf); } Fatal(0,"Jx9 compile error"); } /* Now we have our program compiled, it's time to register our constants * and their associated C procedure. */ rc = unqlite_create_constant(pVm, "__PI__", PI_Constant, 0); if( rc != UNQLITE_OK ) { Fatal(0,"Error while installing the __PI__ constant"); } rc = unqlite_create_constant(pVm, "__TIME__", TIME_Constant, 0); if( rc != UNQLITE_OK ) { Fatal(0,"Error while installing the __TIME__ constant"); } rc = unqlite_create_constant(pVm, "__OS__", OS_Constant, 0); if( rc != UNQLITE_OK ) { Fatal(0,"Error while installing the __OS__ constant"); } /* Install a VM output consumer callback */ rc = unqlite_vm_config(pVm,UNQLITE_VM_CONFIG_OUTPUT,VmOutputConsumer,0); if( rc != UNQLITE_OK ) { Fatal(pDb,0); } /* Execute our script */ rc = unqlite_vm_exec(pVm); if( rc != UNQLITE_OK ) { Fatal(pDb,0); } /* Release our VM */ unqlite_vm_release(pVm); /* Auto-commit the transaction and close our database */ unqlite_close(pDb); return 0; }