Skip to content

Instantly share code, notes, and snippets.

@vhxs
Last active June 8, 2022 01:41
Show Gist options
  • Save vhxs/35e06d8b8fb65e8b2c46b9bfbbfe603a to your computer and use it in GitHub Desktop.
Save vhxs/35e06d8b8fb65e8b2c46b9bfbbfe603a to your computer and use it in GitHub Desktop.
// RocksDB is a Facebook-scale key-value store designed for fast writes to SSD storage.
// How fast does this run?
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <rocksdb/db.h>
using namespace std;
#define STR_LEN 64
int main(int argc, char** argv) {
rocksdb::DB* db;
rocksdb::Options options;
options.create_if_missing = true;
rocksdb::Status status = rocksdb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
for (int i = 0; i < 10000; i++) {
if (i % 100 == 0) {
cout << i << endl;
}
// Generate hash
char str[STR_LEN + 1];
for (int j = 0; j < STR_LEN; j++) {
sprintf(str + j, "%x", rand() % 16);
}
string hash(str);
// Generate random ints
int size = rand() % 1000;
for (int j = 0; j < size; j++) {
string key = to_string(rand());
string hashes;
status = db->Get(rocksdb::ReadOptions(), key, &hashes);
if (status.IsNotFound()) {
hashes = hash;
} else {
hashes = hashes + "," + hash;
}
status = db->Put(rocksdb::WriteOptions(), key, hashes);
}
}
}
// Ran in 76 seconds.
// Total storage: 430MB
// This is a bad way of storing a list of strings.
// Can/should be combine this with Facebook's F14 hash tables?
// https://engineering.fb.com/2019/04/25/developer-tools/f14/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment