void UnqliteCursor::next() { int rc = unqlite_kv_cursor_next_entry(this->_cursor); if (rc != UNQLITE_OK && rc != UNQLITE_DONE) throw UnqliteException(rc, this->_db); }
bool KVUnqlite::getKeys(const std::string& filter, Keys& keys) { unqlite* pDbHandle = static_cast<unqlite*>(_pDbHandle); if (!pDbHandle) { return false; } unqlite_kv_cursor* pCursor = 0; if (unqlite_kv_cursor_init(pDbHandle, &pCursor) != UNQLITE_OK || !pCursor) { log_error(); return false; } KeyConsumer consumer; consumer.filter = filter; consumer.keys = &keys; /* Point to the first record */ for ( unqlite_kv_cursor_first_entry(pCursor); unqlite_kv_cursor_valid_entry(pCursor); unqlite_kv_cursor_next_entry(pCursor)) { unqlite_kv_cursor_key_callback(pCursor, KeyConsumerCallback,(void*)&consumer); } unqlite_kv_cursor_release(pDbHandle, pCursor); return true; }
bool KVUnqlite::getRecords(const std::string& filter, Records& records) { unqlite* pDbHandle = static_cast<unqlite*>(_pDbHandle); if (!pDbHandle) { return false; } unqlite_kv_cursor* pCursor = 0; if (unqlite_kv_cursor_init(pDbHandle, &pCursor) != UNQLITE_OK || !pCursor) { log_error(); return false; } RecordConsumer consumer; consumer.filter = filter; consumer.records = &records; /* Point to the first record */ for ( unqlite_kv_cursor_first_entry(pCursor); unqlite_kv_cursor_valid_entry(pCursor); unqlite_kv_cursor_next_entry(pCursor)) { unqlite_kv_cursor_key_callback(pCursor, RecordConsumerCallback,(void*)&consumer); if (!consumer.key.empty()) { // // We got a key so consume the data // unqlite_kv_cursor_data_callback(pCursor, RecordConsumerCallback,(void*)&consumer); } } unqlite_kv_cursor_release(pDbHandle, pCursor); return true; }
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; }
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; }
void Storage::scan(const char *keyData, uint keySize, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler, const std::function<void(const Storage::Error &error)> &errorHandler) { if (!d->db) { Error error(d->name.toStdString(), -1, "Not open"); errorHandler(error); return; } unqlite_kv_cursor *cursor; int rc = unqlite_kv_cursor_init(d->db, &cursor); if (rc != UNQLITE_OK) { d->reportDbError("unqlite_kv_cursor_init", rc, errorHandler); return; } void *keyBuffer = nullptr; int keyBufferLength = 0; void *dataBuffer = nullptr; //FIXME: 64bit ints, but feeding int lenghts to the callbacks. can result in truncation unqlite_int64 dataBufferLength = 0; if (!keyData || keySize == 0) { for (unqlite_kv_cursor_first_entry(cursor); unqlite_kv_cursor_valid_entry(cursor); unqlite_kv_cursor_next_entry(cursor)) { fetchCursorData(cursor, &keyBuffer, &keyBufferLength, &dataBuffer, &dataBufferLength, resultHandler); } } else { rc = unqlite_kv_cursor_seek(cursor, keyData, keySize, UNQLITE_CURSOR_MATCH_EXACT); if (rc == UNQLITE_OK) { fetchCursorData(cursor, &keyBuffer, &keyBufferLength, &dataBuffer, &dataBufferLength, resultHandler); } else { std::cout << "couldn't find value " << std::string(keyData, keySize) << std::endl; } } free(keyBuffer); free(dataBuffer); unqlite_kv_cursor_release(d->db, cursor); }