Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <thread>
- #include <cstdio>
- #include <string>
- #include <iostream>
- #include <cinttypes>
- #include "rocksdb/db.h"
- #include <db/memtable.h>
- #include "memtablefactory.h"
- #include <memtable/inlineskiplist.h>
- using namespace std;
- using namespace rocksdb;
- using namespace std::chrono;
- std::string kDBPath = "/repos/rocksdata";
- template<typename T>
- T swap_endian(T u) {
- static_assert(CHAR_BIT == 8, "CHAR_BIT != 8");
- union {
- T u;
- unsigned char u8[sizeof(T)];
- } source, dest;
- source.u = u;
- for (size_t k = 0; k < sizeof(T); k++)
- dest.u8[k] = source.u8[sizeof(T) - k - 1];
- return dest.u;
- }
- rocksdb::TableFactory *makeDictionaryTableFactory() {
- auto block_opts = rocksdb::BlockBasedTableOptions{};
- block_opts.checksum = ChecksumType::kCRC32c;
- block_opts.no_block_cache = true;
- return rocksdb::NewBlockBasedTableFactory(block_opts);
- }
- int main() {
- system("rm -rf /repos/rocksdata/*");
- 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.db_write_buffer_size = 10 * 1024 * 1024;
- options.compression = CompressionType::kNoCompression;
- options.statistics = rocksdb::CreateDBStatistics();
- options.write_buffer_size = 10 * 1024 * 1024;
- // open DB
- Status s = DB::Open(options, kDBPath, &db);
- if (!s.ok()) {
- std::cout << s.ToString();
- }
- assert(s.ok());
- ColumnFamilyOptions cf_options{};
- cf_options.table_factory.reset(makeDictionaryTableFactory());
- cf_options.prefix_extractor.reset(rocksdb::NewNoopTransform());
- cf_options.memtable_prefix_bloom_size_ratio = 0;
- cf_options.write_buffer_size = 10 * 1024 * 1024;
- std::string name("Name");
- ColumnFamilyHandle *cf;
- Status status = db->CreateColumnFamily(cf_options, name, &cf);
- assert(s.ok());
- u_int64_t *buffer = new u_int64_t[4];
- char *pointer = reinterpret_cast<char *>(buffer);
- WriteBatch writeBatch{};
- u_int64_t max = 10000000;
- Slice key(pointer, 32);
- Slice value(reinterpret_cast<char *>(&max), 8);
- uint64_t begin = (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
- system_clock::now().time_since_epoch()).count();
- for (u_int64_t i = 0; i < 10000000000; i++) {
- *(buffer) = swap_endian(i);
- *(buffer + 1) = i + 1;
- *(buffer + 2) = i + 2;
- *(buffer + 3) = i + 3;
- writeBatch.Put(cf, key, value);
- if (i % 1000 == 0) {
- Status s1 = db->Write(WriteOptions(), &writeBatch);
- assert(s1.ok());
- writeBatch.Clear();
- }
- if (i % 1000000 == 0) {
- uint64_t end = (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
- system_clock::now().time_since_epoch()).count();
- double time = (end - begin) / 1000000000;
- double delta = i / time;
- std::cout << "Speed=" << std::to_string(delta) << "\n\n";
- }
- }
- db->DestroyColumnFamilyHandle(cf);
- delete db;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement