MrSairam

ExampleProgram.cc

Jul 10th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #ifndef ROCKSDB_LITE
  2.  
  3. #include "rocksdb/db.h"
  4. #include "rocksdb/options.h"
  5. #include "rocksdb/slice.h"
  6. #include <iterator>
  7. #include <chrono>
  8. #include <cstdio>
  9. #include <fstream>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <set>
  13. #include <sstream>
  14. #include <string>
  15. #include <vector>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <pthread.h>
  20. #include <cstdlib>
  21. #include "rocksdb/db.h"
  22. #include "rocksdb/merge_operator.h"
  23. #include "rocksdb/options.h"
  24. #include "rocksdb/slice.h"
  25. #include "../utilities/merge_operators.h"
  26. #include "rocksdb/utilities/write_batch_with_index.h"
  27.  
  28. #define CHECK_STATUS(where, s)                         \
  29.   do {                                                 \
  30.     if (!s.ok()) {                                     \
  31.       std::cout << where << s.ToString() << std::endl; \
  32.       abort();                                         \
  33.     }                                                  \
  34.   } while (0)
  35.  
  36. using namespace rocksdb;
  37.  
  38. std::string kDBPath = "/tmp/rocksdb_bulkcopy_example";
  39. const char *cfName1 = "zlabs_cf_1";
  40. const char *cfName2 = "zlabs_cf_2";
  41.  
  42. DB *db;
  43. WriteOptions writeOptions;
  44. ReadOptions readoptions;
  45.  
  46. class CSVRow
  47. {
  48.     public:
  49.         std::string const& operator[](std::size_t index) const
  50.         {
  51.             return m_data[index];
  52.         }
  53.         std::size_t size() const
  54.         {
  55.             return m_data.size();
  56.         }
  57.         void readNextRow(std::istream& str)
  58.         {
  59.             std::string         line;
  60.             std::getline(str, line);
  61.  
  62.             std::stringstream   lineStream(line);
  63.             std::string         cell;
  64.  
  65.             m_data.clear();
  66.             while(std::getline(lineStream, cell, ','))
  67.             {
  68.                 m_data.push_back(cell);
  69.             }
  70.             // This checks for a trailing comma with no data after it.
  71.             if (!lineStream && cell.empty())
  72.             {
  73.                 // If there was a trailing comma then add an empty element.
  74.                 m_data.push_back("");
  75.             }
  76.         }
  77.     // private:
  78.         std::vector<std::string>    m_data;
  79. };
  80.  
  81. std::istream& operator>>(std::istream& str, CSVRow& data)
  82. {
  83.     data.readNextRow(str);
  84.     return str;
  85. }  
  86.  
  87. class CSVIterator
  88. {  
  89.     public:
  90.         typedef std::input_iterator_tag     iterator_category;
  91.         typedef CSVRow                      value_type;
  92.         typedef std::size_t                 difference_type;
  93.         typedef CSVRow*                     pointer;
  94.         typedef CSVRow&                     reference;
  95.  
  96.         CSVIterator(std::istream& str)  :m_str(str.good()?&str:NULL) { ++(*this); }
  97.         CSVIterator()                   :m_str(NULL) {}
  98.  
  99.         // Pre Increment
  100.         CSVIterator& operator++()               {if (m_str) { if (!((*m_str) >> m_row)){m_str = NULL;}}return *this;}
  101.         // Post increment
  102.         CSVIterator operator++(int)             {CSVIterator    tmp(*this);++(*this);return tmp;}
  103.         CSVRow const& operator*()   const       {return m_row;}
  104.         CSVRow const* operator->()  const       {return &m_row;}
  105.  
  106.         bool operator==(CSVIterator const& rhs) {return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));}
  107.         bool operator!=(CSVIterator const& rhs) {return !((*this) == rhs);}
  108.     private:
  109.         std::istream*       m_str;
  110.         CSVRow              m_row;
  111. };
  112.  
  113.  
  114. int main() {
  115.  
  116.   // open DB
  117.   Options options;
  118.   options.create_if_missing = true;
  119.   options.merge_operator = rocksdb::MergeOperators::CreateStringAppendOperator();
  120.  
  121.   options.max_successive_merges = 10;
  122.  
  123.   bool dbAlreadyExists = false;
  124.  
  125.   Status s;
  126.   std::ifstream file("PathToFolder/10MillionData.txt");
  127.  
  128.   if (stat(kDBPath.c_str(), new struct stat()) == -1) {
  129.     s = DB::Open(options, kDBPath, &db);
  130.  
  131.     assert(s.ok());
  132.     CHECK_STATUS("FirstDB::OPEN", s);
  133.  
  134.     ColumnFamilyHandle *cf;
  135.     s = db->CreateColumnFamily(ColumnFamilyOptions(options), cfName1, &cf);
  136.     assert(s.ok());
  137.  
  138.     s = db->CreateColumnFamily(ColumnFamilyOptions(options), cfName2, &cf);
  139.     assert(s.ok());
  140.  
  141.     delete cf;
  142.     delete db;
  143.   }
  144.   else
  145.     dbAlreadyExists = true;
  146.  
  147.   std::vector<ColumnFamilyDescriptor> column_families;
  148.  
  149.   column_families.push_back(ColumnFamilyDescriptor(
  150.       kDefaultColumnFamilyName, ColumnFamilyOptions(options)));
  151.   // open the new one, too
  152.   column_families.push_back(
  153.       ColumnFamilyDescriptor(cfName1, ColumnFamilyOptions(options)));
  154.   column_families.push_back(
  155.       ColumnFamilyDescriptor(cfName2, ColumnFamilyOptions(options)));
  156.   std::vector<ColumnFamilyHandle *> handles;
  157.   s = DB::Open(options, kDBPath, column_families, &handles, &db);
  158.   CHECK_STATUS("Second DB::Open", s);
  159.  
  160.   using std::cout;
  161.   using std::endl;
  162.  
  163.   if (!dbAlreadyExists) {
  164.     WriteBatchWithIndex *wbwi = new WriteBatchWithIndex(BytewiseComparator(), 512, false);
  165.     Status s;
  166.    
  167.     cout << "Inserting data into DB\n";
  168.     /////////////////////////////////////////////////////
  169.     for(CSVIterator loop(file); loop != CSVIterator(); ++loop) {
  170.       wbwi->Merge(handles[1], Slice((*loop)[0]), Slice((*loop)[1]));
  171.       wbwi->Merge(handles[2], Slice((*loop)[1]), Slice((*loop)[0]));
  172.     }
  173.     /////////////////////////////////////////////////////
  174.  
  175.     s = db->Write(writeOptions, wbwi->GetWriteBatch());
  176.  
  177.     wbwi->Clear();
  178.  
  179.     CHECK_STATUS("insertDataToDB Write", s);
  180.     cout << "Insert Complete\n";
  181.  
  182.     usleep(60000000);// will sleep for 60s, Gives time to check if memory is freed
  183.   }
  184.  
  185.   for (auto handle : handles) {
  186.     delete handle;
  187.   }
  188.   delete db;
  189. }
  190.  
  191. #endif  // ROCKSDB_LITE
Add Comment
Please, Sign In to add comment