github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/leveldb-go/testdata/make-table.cc (about)

     1  // Copyright 2011 The LevelDB-Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This program adds N lines from infile to a leveldb table at outfile.
     6  // The h.txt infile was generated via:
     7  // cat hamlet-act-1.txt | tr '[:upper:]' '[:lower:]' | grep -o -E '\w+' | sort | uniq -c > infile
     8  
     9  #include <fstream>
    10  #include <iostream>
    11  #include <string>
    12  
    13  #include "leveldb/env.h"
    14  #include "leveldb/table.h"
    15  #include "leveldb/table_builder.h"
    16  
    17  const int N = 1000000;
    18  const char* infile = "h.txt";
    19  const char* outfile = "h.sst";
    20  
    21  int write() {
    22    leveldb::Status status;
    23    
    24    leveldb::WritableFile* wf;
    25    status = leveldb::Env::Default()->NewWritableFile(outfile, &wf);
    26    if (!status.ok()) {
    27      cerr << "Env::NewWritableFile: " << status.ToString() << endl;
    28      return 1;
    29    }
    30  
    31    leveldb::Options o;
    32    // o.compression = leveldb::kNoCompression;
    33    leveldb::TableBuilder* tb = new leveldb::TableBuilder(o, wf);
    34    ifstream in(infile);
    35    string s;
    36    for (int i = 0; i < N && getline(in, s); i++) {
    37      string key(s, 8);
    38      string val(s, 0, 7);
    39      val = val.substr(1 + val.rfind(' '));
    40      tb->Add(key.c_str(), val.c_str());
    41    }
    42  
    43    status = tb->Finish();
    44    if (!status.ok()) {
    45      cerr << "TableBuilder::Finish: " << status.ToString() << endl;
    46      return 1;
    47    }
    48  
    49    status = wf->Close();
    50    if (!status.ok()) {
    51      cerr << "WritableFile::Close: " << status.ToString() << endl;
    52      return 1;
    53    }
    54  
    55    cout << "wrote " << tb->NumEntries() << " entries" << endl;
    56    delete tb;
    57    delete wf;
    58    return 0;
    59  }
    60  
    61  int read() {
    62    leveldb::Status status;
    63  
    64    leveldb::RandomAccessFile* raf;
    65    status = leveldb::Env::Default()->NewRandomAccessFile(outfile, &raf);
    66    if (!status.ok()) {
    67      cerr << "Env::NewRandomAccessFile: " << status.ToString() << endl;
    68      return 1;
    69    }
    70  
    71    uint64_t file_size;
    72    status = leveldb::Env::Default()->GetFileSize(outfile, &file_size);
    73    if (!status.ok()) {
    74      cerr << "Env::GetFileSize: " << status.ToString() << endl;
    75      return 1;
    76    }
    77  
    78    leveldb::Options o;
    79    leveldb::Table* t;
    80    status = leveldb::Table::Open(o, raf, file_size, &t);
    81    if (!status.ok()) {
    82      cerr << "Table::Open: " << status.ToString() << endl;
    83      return 1;
    84    }
    85  
    86    leveldb::ReadOptions ro;
    87    leveldb::Iterator* i = t->NewIterator(ro);
    88    uint64_t n = 0;
    89    for (i->SeekToFirst(); i->Valid(); i->Next()) {
    90      n++;
    91    }
    92  
    93    cout << "read  " << n << " entries" << endl;
    94    delete i;
    95    delete t;
    96    delete raf;
    97    return 0;
    98  }
    99  
   100  int main(int argc, char** argv) {
   101    int ret = write();
   102    if (ret != 0) {
   103      return ret;
   104    }
   105    return read();
   106  }