File: kcmrex.cc

package info (click to toggle)
kyotocabinet 1.2.76-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,492 kB
  • ctags: 3,561
  • sloc: cpp: 59,347; ansic: 1,478; makefile: 1,207; ruby: 523; sh: 190; awk: 91; perl: 61
file content (59 lines) | stat: -rw-r--r-- 1,477 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <kcpolydb.h>
#include <kcdbext.h>

using namespace std;
using namespace kyotocabinet;

// main routine
int main(int argc, char** argv) {

  // create the database object
  PolyDB db;

  // open the database
  if (!db.open()) {
    cerr << "open error: " << db.error().name() << endl;
  }

  // store records
  db.set("1", "this is a pen");
  db.set("2", "what a beautiful pen this is");
  db.set("3", "she is beautiful");

  // define the mapper and the reducer
  class MapReduceImpl : public MapReduce {
    // call back function of the mapper
    bool map(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
      vector<string> words;
      strsplit(string(vbuf, vsiz), ' ', &words);
      for (vector<string>::iterator it = words.begin();
           it != words.end(); it++) {
        emit(it->data(), it->size(), "", 0);
      }
      return true;
    }
    // call back function of the reducer
    bool reduce(const char* kbuf, size_t ksiz, ValueIterator* iter) {
      size_t count = 0;
      const char* vbuf;
      size_t vsiz;
      while ((vbuf = iter->next(&vsiz)) != NULL) {
        count++;
      }
      cout << string(kbuf, ksiz) << ": " << count << endl;
      return true;
    }
  } mr;

  // execute the MapReduce process
  if (!mr.execute(&db)) {
    cerr << "MapReduce error: " << db.error().name() << endl;
  }

  // close the database
  if (!db.close()) {
    cerr << "close error: " << db.error().name() << endl;
  }

  return 0;
}