GithubHelp home page GithubHelp logo

nemo-rocksdb's Introduction

Introduction

nemo-rocksdb is compatible with rocksdb, and I added TTL feature on it, supporting to put a record with any specified TTL. The performance is close to rocksdb, so you can use it without worring about performance penalty. It uses rocksdb as a submodule, so it's easy to upgrade rocksdb to the latest version if needed, now it's using rocksdb(v5.0.1). Besides, it is going to be used in Nemo as a submodule.

Notice!!!

Please DO NOT!!! use master branch directly in your project,cause we add some non-universal features on that required by Nemo and you may not need, checkout and use Tags or public-release branch instead, that is PURE and COMPATIBLE!

Features

TTL

 DBNemo is derived from rocksdb::StackableDB, so it is compatible with rocksdb, you can use all the methods of rocksdb as usual, such as  

  Method   
DBNemo::Put()        
DBNemo::Write()      
DBNemo::Get()        
DBNemo::Delete()      
DBNemo::Merge()      
DBNemo::NewIterator()
DBNemo::CompactRange()

How to expire key?

just use Put or Write as usual, and add one more parameter(int32_t ttl) at last

db->Put(rocksdb::WriteOptions(), "persistent_key", "value"); // never to be expired
db->Put(rocksdb::WriteOptions(), "expire_key", "value", 6); // will be expired after 6s

rocksdb::WriteBatch batch;
batch.Put("expire_key", "value"); // never to be expired
db->Write(rocksdb::WriteOptions(), &batch, 6); // will be expired after 6s

you can create iterator by NewIterator() to iterate db, it will ignore the expired records automaticly.

you can also use Merge method on record which has been set TTL, and when the record expired later, the existing_value would disappear, then, you may get different result from geting before the record expired. read Test Merge example to learn more.

DBNemo uses specified inner CompactionFilter to drop the expired records in compaction process by default, you can add your own CompactionFilterFactory or CompactionFilter in rocksdb::Options, that's ok!

Usage

#include "db_nemo_impl.h"
#include <iostream>

int main() {
  rocksdb::DBNemo *db;
  rocksdb::Options options;
  options.create_if_missing = true;
  rocksdb::Status s = rocksdb::DBNemo::Open(options, "./db", &db);
  
  /*
   * 1. Insert a persistent record
   */
  s = db->Put(rocksdb::WriteOptions(), "persistent_key", "value");
  if (!s.ok()) {
    std::cout << "Put Error: " << s.ToString() << std::endl;
  }
  
  /*
   * 2. Insert a record with 6s TTL
   */
  s = db->Put(rocksdb::WriteOptions(), "expire_key", "value", 6);
  if (!s.ok()) {
    std::cout << "Put Error: " << s.ToString() << std::endl;
  }
  
  /*
   * 3. Insert three records in one batch with 8s TTL
   */
  rocksdb::WriteBatch batch;
  batch.Put("expire_key_1", "value1");
  batch.Put("expire_key_2", "value2");
  batch.Put("expire_key_3", "value3");
  s = db->Write(rocksdb::WriteOptions(), &batch, 8);
  if (!s.ok()) {
    std::cout << "Put Error: " << s.ToString() << std::endl;
  }
  
  /*
   * 4. Iterate db, the expired keys would be ignored automaticly
   */
  rocksdb::Iterator* iter = db->NewIterator(rocksdb::ReadOptions());
  while (true) {
    iter->SeekToFirst();
    std::cout << "---------------------------" << std::endl;
    while (iter->Valid()) {
      std::cout << iter->key().ToString() << " "
        << iter->value().ToString() << " "
        << static_cast<rocksdb::NemoIterator*>(iter)->timestamp()
        << std::endl;
      
      iter->Next();
    }
    std::cout << "---------------------------" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }
  
  delete db;
  return 0;
}

see More

Performance

It almost costs NOTHING to add TTL feature in NemoDB, so its performance is close to rocksdb.

In my environment, it used 390s to finish the benchmark(put 100 millions records), the benchmark code is here

nemo-rocksdb's People

Contributors

axlgrep avatar baotiao avatar kernelmaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nemo-rocksdb's Issues

RocksDB 版本更新?

我们目前需要使用最新版的 rocksdb,而 rocksdb 的最新版本某些函数有改动,与 nemo-rocksdb 当前函数接口不兼容:
file_util.h:
Status CopyFile(Env* env, const std::string& source, const std::string& destination, uint64_t size);
最新版 rocksdb 中变为:
Status CopyFile(Env* env, const std::string& source, const std::string& destination, uint64_t size**, bool use_fsync**);

db_nemo_impl.h:
virtual Status Get(const ReadOptions& options, ColumnFamilyHandle* column_family, const Slice& key, std::string* value) override;
最新版 rocksdb 中祖父类 StackableDB 中的声明是:
virtual Status Get(const ReadOptions& options, ColumnFamilyHandle* column_family, const Slice& key, PinnableSlice value*) override;

不知可否支持一下最新版本?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.