Created
January 24, 2015 15:10
-
-
Save fyrz/426a60978502fcd553ee to your computer and use it in GitHub Desktop.
RocksDB counter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2013, Facebook, Inc. All rights reserved. | |
// This source code is licensed under the BSD-style license found in the | |
// LICENSE file in the root directory of this source tree. An additional grant | |
// of patent rights can be found in the PATENTS file in the same directory. | |
#include <cstdio> | |
#include <string> | |
#include "rocksdb/db.h" | |
#include "rocksdb/slice.h" | |
#include "rocksdb/options.h" | |
#include "../utilities/merge_operators.h" | |
using namespace rocksdb; | |
std::string kDBPath = "/tmp/rocksdb_counter_example"; | |
inline void EncodeFixed64(char* buf, uint64_t value) { | |
#if __BYTE_ORDER == __LITTLE_ENDIAN | |
memcpy(buf, &value, sizeof(value)); | |
#else | |
buf[0] = value & 0xff; | |
buf[1] = (value >> 8) & 0xff; | |
buf[2] = (value >> 16) & 0xff; | |
buf[3] = (value >> 24) & 0xff; | |
buf[4] = (value >> 32) & 0xff; | |
buf[5] = (value >> 40) & 0xff; | |
buf[6] = (value >> 48) & 0xff; | |
buf[7] = (value >> 56) & 0xff; | |
#endif | |
} | |
inline uint32_t DecodeFixed32(const char* ptr) { | |
#if __BYTE_ORDER == __LITTLE_ENDIAN | |
// Load the raw bytes | |
uint32_t result; | |
memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load | |
return result; | |
#else | |
return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0]))) | |
| (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8) | |
| (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16) | |
| (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24)); | |
#endif | |
} | |
inline uint64_t DecodeFixed64(const char* ptr) { | |
#if __BYTE_ORDER == __LITTLE_ENDIAN | |
// Load the raw bytes | |
uint64_t result; | |
memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load | |
return result; | |
#else | |
uint64_t lo = DecodeFixed32(ptr); | |
uint64_t hi = DecodeFixed32(ptr + 4); | |
return (hi << 32) | lo; | |
#endif | |
} | |
int main() { | |
DB* db; | |
Options options; | |
// Optimize RocksDB. This is the easiest way to get RocksDB to perform well | |
options.IncreaseParallelism(); | |
options.OptimizeLevelStyleCompaction(); | |
// create the DB if it's not already present | |
options.create_if_missing = true; | |
options.merge_operator = (MergeOperators::CreateUInt64AddOperator()); | |
// open DB | |
Status s = DB::Open(options, kDBPath, &db); | |
assert(s.ok()); | |
uint64_t value = 1; | |
for (int i=0; i < 1000000000; i++) { | |
char encoded[sizeof(uint64_t)]; | |
EncodeFixed64(encoded, value); | |
Slice slice(encoded, sizeof(uint64_t)); | |
db->Merge(WriteOptions(), Slice("key"), slice); | |
} | |
std::string retStr; | |
// get value | |
s = db->Get(ReadOptions(), "key", &retStr); | |
assert(s.ok()); | |
assert(DecodeFixed64(retStr.c_str()) == 10000); | |
delete db; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment