-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbtransactor.cc
79 lines (70 loc) · 1.99 KB
/
dbtransactor.cc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define _XOPEN_SOURCE 500
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unordered_set>
#include <leveldb/db.h>
/* dbtransactor [ref] [src] */
// Checks that every hash in |src| is in |ref|
int main(int argc, char* argv[])
{
std::unordered_set<std::string> ref_hashes;
int i;
int nRefFiles = 0;
int nDuplicates = 0;
int nMisses = 0;
leveldb::DB* ref;
leveldb::Options ref_options;
ref_options.create_if_missing = false;
leveldb::Status status = leveldb::DB::Open(ref_options, std::string(argv[1]), &ref);
if (!status.ok()) {
printf("Error opening ref\n");
return -1;
}
leveldb::DB* candidate;
status = leveldb::DB::Open(ref_options, std::string(argv[2]), &candidate);
if (!status.ok()) {
printf("Error opening candidate\n");
return -1;
}
leveldb::ReadOptions r_options;
r_options.verify_checksums = true;
r_options.fill_cache = false;
leveldb::Iterator* it = ref->NewIterator(r_options);
for (it->SeekToFirst(); it->Valid(); it->Next()) {
if (!it->status().ok()) {
printf("Iteration error!\n");
break;
}
bool unique = ref_hashes.insert(it->value().ToString()).second;
if (!unique)
++nDuplicates;
++nRefFiles;
}
delete it;
delete ref;
it = candidate->NewIterator(r_options);
for (it->SeekToFirst(); it->Valid(); it->Next()) {
if (!it->status().ok()) {
printf("Iteration error!\n");
break;
}
bool found = ref_hashes.count(it->value().ToString()) == 1;
if (!found) {
printf("Not found in ref: %s, %s\n",
it->key().ToString().c_str(),
it->value().ToString().c_str());
++nMisses;
}
}
delete it;
delete candidate;
printf("nRefFiles %d\n", nRefFiles); // Files in the reference db
printf("nDuplicates %d\n", nDuplicates); // Non-unique files in refdb
printf("nRefUnique %d\n", ref_hashes.size()); // nDuplicates + this == nRefFiles // Unique files in refdb
printf("nMisses %d\n", nMisses); // Files in candidate not in ref
}