资源简介
LevelDb是能够处理十亿级别规模Key-Value型数据持久性存储的C++ 程序库。LevelDb是一个持久化存储的KV系统,和Redis这种内存型的KV系统不同,LevelDb不会像Redis一样狂吃内存,而是将大部分数据存储到磁盘上。
代码片段和文件信息
/* Copyright (c) 2011 The LevelDB Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. See the AUTHORS file for names of contributors. */
#include “leveldb/c.h“
#include
#include
#include
#include
#include
#include
const char* phase = ““;
static char dbname[200];
static void StartPhase(const char* name) {
fprintf(stderr “=== Test %s\n“ name);
phase = name;
}
static const char* GetTempDir(void) {
const char* ret = getenv(“TEST_TMPDIR“);
if (ret == NULL || ret[0] == ‘\0‘)
ret = “/tmp“;
return ret;
}
#define CheckNoError(err) \
if ((err) != NULL) { \
fprintf(stderr “%s:%d: %s: %s\n“ __FILE__ __LINE__ phase (err)); \
abort(); \
}
#define CheckCondition(cond) \
if (!(cond)) { \
fprintf(stderr “%s:%d: %s: %s\n“ __FILE__ __LINE__ phase #cond); \
abort(); \
}
static void CheckEqual(const char* expected const char* v size_t n) {
if (expected == NULL && v == NULL) {
// ok
} else if (expected != NULL && v != NULL && n == strlen(expected) &&
memcmp(expected v n) == 0) {
// ok
return;
} else {
fprintf(stderr “%s: expected ‘%s‘ got ‘%s‘\n“
phase
(expected ? expected : “(null)“)
(v ? v : “(null“));
abort();
}
}
static void Free(char** ptr) {
if (*ptr) {
free(*ptr);
*ptr = NULL;
}
}
static void CheckGet(
leveldb_t* db
const leveldb_readoptions_t* options
const char* key
const char* expected) {
char* err = NULL;
size_t val_len;
char* val;
val = leveldb_get(db options key strlen(key) &val_len &err);
CheckNoError(err);
CheckEqual(expected val val_len);
Free(&val);
}
static void CheckIter(leveldb_iterator_t* iter
const char* key const char* val) {
size_t len;
const char* str;
str = leveldb_iter_key(iter &len);
CheckEqual(key str len);
str = leveldb_iter_value(iter &len);
CheckEqual(val str len);
}
// Callback from leveldb_writebatch_iterate()
static void CheckPut(void* ptr
const char* k size_t klen
const char* v size_t vlen) {
int* state = (int*) ptr;
CheckCondition(*state < 2);
switch (*state) {
case 0:
CheckEqual(“bar“ k klen);
CheckEqual(“b“ v vlen);
break;
case 1:
CheckEqual(“box“ k klen);
CheckEqual(“c“ v vlen);
break;
}
(*state)++;
}
// Callback from leveldb_writebatch_iterate()
static void CheckDel(void* ptr const char* k size_t klen) {
int* state =
- 上一篇:PC微信多开器,C#编写,C++ DLL
- 下一篇:图像融合C++/MFC
评论
共有 条评论