Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <thread>
  2. #include <cstdio>
  3. #include <string>
  4. #include <iostream>
  5. #include <cinttypes>
  6. #include "rocksdb/db.h"
  7. #include <db/memtable.h>
  8. #include "memtablefactory.h"
  9. #include <memtable/inlineskiplist.h>
  10.  
  11. using namespace std;
  12. using namespace rocksdb;
  13. using namespace std::chrono;
  14.  
  15. std::string kDBPath = "/repos/rocksdata";
  16.  
  17. template<typename T>
  18. T swap_endian(T u) {
  19. static_assert(CHAR_BIT == 8, "CHAR_BIT != 8");
  20.  
  21. union {
  22. T u;
  23. unsigned char u8[sizeof(T)];
  24. } source, dest;
  25.  
  26. source.u = u;
  27.  
  28. for (size_t k = 0; k < sizeof(T); k++)
  29. dest.u8[k] = source.u8[sizeof(T) - k - 1];
  30.  
  31. return dest.u;
  32. }
  33.  
  34. rocksdb::TableFactory *makeDictionaryTableFactory() {
  35. auto block_opts = rocksdb::BlockBasedTableOptions{};
  36. block_opts.checksum = ChecksumType::kCRC32c;
  37. block_opts.no_block_cache = true;
  38. return rocksdb::NewBlockBasedTableFactory(block_opts);
  39. }
  40.  
  41. int main() {
  42. system("rm -rf /repos/rocksdata/*");
  43.  
  44. DB *db;
  45. Options options;
  46. // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
  47. //options.IncreaseParallelism();
  48. //options.OptimizeLevelStyleCompaction();
  49. // create the DB if it's not already present
  50.  
  51. options.create_if_missing = true;
  52. options.db_write_buffer_size = 10 * 1024 * 1024;
  53. options.compression = CompressionType::kNoCompression;
  54. options.statistics = rocksdb::CreateDBStatistics();
  55. options.write_buffer_size = 10 * 1024 * 1024;
  56.  
  57. // open DB
  58. Status s = DB::Open(options, kDBPath, &db);
  59.  
  60. if (!s.ok()) {
  61. std::cout << s.ToString();
  62. }
  63.  
  64. assert(s.ok());
  65.  
  66. ColumnFamilyOptions cf_options{};
  67. cf_options.table_factory.reset(makeDictionaryTableFactory());
  68. cf_options.prefix_extractor.reset(rocksdb::NewNoopTransform());
  69. cf_options.memtable_prefix_bloom_size_ratio = 0;
  70. cf_options.write_buffer_size = 10 * 1024 * 1024;
  71.  
  72. std::string name("Name");
  73. ColumnFamilyHandle *cf;
  74. Status status = db->CreateColumnFamily(cf_options, name, &cf);
  75.  
  76. assert(s.ok());
  77.  
  78. u_int64_t *buffer = new u_int64_t[4];
  79. char *pointer = reinterpret_cast<char *>(buffer);
  80. WriteBatch writeBatch{};
  81. u_int64_t max = 10000000;
  82.  
  83. Slice key(pointer, 32);
  84. Slice value(reinterpret_cast<char *>(&max), 8);
  85.  
  86. uint64_t begin = (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
  87. system_clock::now().time_since_epoch()).count();
  88.  
  89. for (u_int64_t i = 0; i < 10000000000; i++) {
  90. *(buffer) = swap_endian(i);
  91. *(buffer + 1) = i + 1;
  92. *(buffer + 2) = i + 2;
  93. *(buffer + 3) = i + 3;
  94.  
  95. writeBatch.Put(cf, key, value);
  96.  
  97. if (i % 1000 == 0) {
  98. Status s1 = db->Write(WriteOptions(), &writeBatch);
  99. assert(s1.ok());
  100. writeBatch.Clear();
  101. }
  102.  
  103. if (i % 1000000 == 0) {
  104. uint64_t end = (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
  105. system_clock::now().time_since_epoch()).count();
  106. double time = (end - begin) / 1000000000;
  107. double delta = i / time;
  108. std::cout << "Speed=" << std::to_string(delta) << "\n\n";
  109. }
  110. }
  111.  
  112. db->DestroyColumnFamilyHandle(cf);
  113. delete db;
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement