LevelDB (1) Overview

1. LevelDB

A very efficient kv database that can support billion-level data volume. There is also very high performance in this order of magnitude, mainly due to its good design. Especially the LSM algorithm.

LevelDB is a single-process service with very high performance. On a 4 Q6600 CPU machine, the data write per second exceeds 40W, while the random read performance exceeds 10W per second. The random read here is the speed of completely hitting the memory. If it is not hit, the speed will be greatly reduced.

eg.

#include "leveldb/include/db.h"

leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());

std::string value;
leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);
if (s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value);
if (s.ok()) s = db->Delete(leveldb::WriteOptions(), key1);

eg2.

#include "leveldb/db.h"
#include <iostream>

using namespace std;

int main(){
    
    
  leveldb::DB *db;
  leveldb::Options options;

  options.create_if_missing = true;

  // 開啟數據庫
  leveldb::DB::Open(options, "/tmp/testdb", &db);

  // 鍵 = MyKey29,值 = "Hello World!"
  string key = "MyKey29", value = "Hello World!", result;

  // 儲存 鍵/值對
  db->Put(leveldb::WriteOptions(), key, value);

  // 查詢 MyKey29 鍵的值
  db->Get(leveldb::ReadOptions(), key, &result);

  // 輸出值到屏幕
  cout << "result = " << result << endl;

  // 關閉數據庫
  delete db;

  return 0;
}

Guess you like

Origin blog.csdn.net/thefist11cc/article/details/123886865