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::get(const std::string& key, std::string& value) { unqlite* pDbHandle = static_cast<unqlite*>(_pDbHandle); if (!pDbHandle) { return false; } char buff[PERSISTENT_STORE_MAX_VALUE_SIZE]; unqlite_int64 nBytes = PERSISTENT_STORE_MAX_VALUE_SIZE; if (unqlite_kv_fetch(pDbHandle, key.c_str(),key.size(), buff, &nBytes) != UNQLITE_OK) { log_error(); return false; } value = std::string(buff, nBytes); return true; }