Advertisement
Guest User

data_import.cc

a guest
Apr 4th, 2016
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <cassert>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include "leveldb/db.h"
  5. #include "leveldb/write_batch.h"
  6.  
  7. const char* DBPATH = "/home/abcdabcd987/test.db";
  8. const int KEYSIZE = 4 * 1024;
  9. const int BATCHSIZE = 1 * 1024 * 1024;
  10.  
  11. leveldb::DB* db;
  12. leveldb::Status ok;
  13. leveldb::WriteBatch *batch;
  14. leveldb::WriteOptions write_options;
  15. FILE* fp;
  16. char key[KEYSIZE], *value;
  17. bool eof, valid;
  18. int cnt_batch;
  19. int cnt_line, cnt_valid, cnt_write, cnt_fail;
  20.  
  21. inline bool is_valid_domain_char(const char c) {
  22.     return ('a' <= c && c <= 'z')
  23.         || ('A' <= c && c <= 'Z')
  24.         || ('0' <= c && c <= '9')
  25.         || (c == '.');
  26. }
  27.  
  28. inline bool is_valid_name_char(const char c) {
  29.     return ('a' <= c && c <= 'z')
  30.         || ('A' <= c && c <= 'Z')
  31.         || ('0' <= c && c <= '9')
  32.         || (c == '+')
  33.         || (c == '-')
  34.         || (c == '.')
  35.         || (c == '_');
  36. }
  37.  
  38. void getrecord() {
  39.     valid = false;
  40.     size_t len = 0;
  41.     ssize_t read;
  42.     char *pkey = key;
  43.     read = getline(&value, &len, fp);
  44.     if ((eof = read == -1)) return;
  45.     ++cnt_line;
  46.  
  47.     char *pline = value;
  48.     while (*pline && !is_valid_name_char(*pline)) ++pline;
  49.     while (*pline && is_valid_name_char(*pline)) *pkey++ = *pline++;
  50.     if (!(*pline) || *pline != '@') return;
  51.     *pkey++ = *pline++;
  52.     while (*pline && is_valid_domain_char(*pline)) *pkey++ = *pline++;
  53.     *pkey++ = '\0';
  54.  
  55.     valid = true;
  56.     ++cnt_valid;
  57. }
  58.  
  59. void batch_write() {
  60.     ++cnt_write;
  61.     ok = db->Write(write_options, batch);
  62.     if (!ok.ok()) ++cnt_fail;
  63.     delete batch;
  64.     cnt_batch = 0;
  65. }
  66.  
  67. void insert() {
  68.     getrecord();
  69.     if (!valid) return;
  70.     batch->Put(key, value);
  71.     if (++cnt_batch < BATCHSIZE) return;
  72.     batch_write();
  73.     batch = new leveldb::WriteBatch();
  74. }
  75.  
  76. int main(int argc, char** argv) {
  77.     if (argc != 2) return 1;
  78.     fp = fopen(argv[1], "r");
  79.     if (!fp) {
  80.         fprintf(stderr, "fail to open file\n");
  81.         return 1;
  82.     }
  83.  
  84.     leveldb::Options options;
  85.     options.create_if_missing = true;
  86.     ok = leveldb::DB::Open(options, DBPATH, &db);
  87.     if (!ok.ok()) {
  88.         fprintf(stderr, "%s\n", ok.ToString().c_str());
  89.         return 1;
  90.     }
  91.     batch = new leveldb::WriteBatch();
  92.  
  93.     while (!eof) insert();
  94.     batch_write();
  95.  
  96.     printf("%s\tline: %d\tvalid: %d\twrite: %d\tfail: %d\n", argv[1], cnt_line, cnt_valid, cnt_write, cnt_fail);
  97.  
  98.     delete db;
  99.     fclose(fp);
  100.     free(value);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement