collocatordb: encapsulate more functions
diff --git a/Makefile b/Makefile
index 99086b1..1ad4df5 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,8 @@
PLATFORM=OS_LINUX
PLATFORM_LDFLAGS= -lpthread -lrt -lsnappy -lgflags -lz -lbz2 -llz4 -lzstd
+CXXFLAGS = -Wall -Wno-reorder -I/usr/local/include -O2 -std=c++11
+
ifneq ($(USE_RTTI), 1)
CXXFLAGS += -fno-rtti
endif
@@ -11,10 +13,10 @@
LIBOBJECTS = $(LIB_SOURCES:.cc=.o)
collocatordb: collocatordb.cc
- $(CXX) $(CXXFLAGS) -L/usr/local/lib $@.cc -o$@ -lrocksdb -I/usr/local/include -O2 -std=c++11 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
+ $(CXX) $(CXXFLAGS) -L/usr/local/lib $@.cc -o$@ -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
libcollocatordb.a: $(LIBOBJECTS)
$(AM_V_at)$(AR) $(ARFLAGS) $@ $(LIBOBJECTS)
.cc.o:
- $(CXX) $(CXXFLAGS) -c $< -o$@ -I/usr/local/include -O2 -std=c++11 $(PLATFORM_CXXFLAGS)
+ $(CXX) $(CXXFLAGS) -c $< -o$@ $(PLATFORM_CXXFLAGS)
diff --git a/collocatordb.cc b/collocatordb.cc
index b4c7679..4eb6134 100644
--- a/collocatordb.cc
+++ b/collocatordb.cc
@@ -18,6 +18,9 @@
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
#define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1)
+#define W1(key) (uint64_t)(key & 0xffffff)
+#define W2(key) (uint64_t)((key >> 24) & 0xffffff)
+#define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff))
using namespace rocksdb;
namespace {
@@ -109,11 +112,35 @@
} // namespace
class CollocatorIterator : public Iterator {
+private:
+ char prefixc[sizeof(uint64_t)];
+ Iterator *base_iterator_;
+
+
public:
- explicit CollocatorIterator() {
+ explicit CollocatorIterator(Iterator* base_iterator)
+ : base_iterator_(base_iterator)
+ {}
+
+ void setPrefix(char *prefix) {
+ memcpy(prefixc, prefix, sizeof(uint64_t));
+ }
+
+ virtual void SeekToFirst() { base_iterator_->SeekToFirst(); }
+ virtual void SeekToLast() { base_iterator_->SeekToLast(); }
+ virtual void Seek(const rocksdb::Slice& s) { base_iterator_->Seek(s); }
+ virtual void Prev() { base_iterator_->Prev(); }
+ virtual void Next() { base_iterator_->Next(); }
+ virtual Slice key() const { return base_iterator_->key(); }
+ virtual Slice value() const { return base_iterator_->value(); }
+ virtual Status status() const { return base_iterator_->status(); }
+
+ virtual bool Valid() const override {
+ return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3));
}
};
+
class Collocators {
private:
WriteOptions merge_option_; // for merge
@@ -151,7 +178,8 @@
: put_option_(),
get_option_(),
delete_option_(),
- merge_option_() {
+ merge_option_()
+{
db_ = OpenDb(db_name, false, 0);
assert(db_);
uint64_t one = 1;
@@ -225,7 +253,7 @@
char encoded_key[sizeof(uint64_t)];
EncodeFixed64(encoded_key, encodeCollocation(w1,w2,dist));
uint64_t value = default_;
- int result = get(std::string(encoded_key, 8), &value);
+ get(std::string(encoded_key, 8), &value);
return value;
}
@@ -258,36 +286,32 @@
}
}
+ virtual CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) {
+ ReadOptions options;
+ options.prefix_same_as_start = true;
+ char prefixc[sizeof(uint64_t)];
+ EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist));
+ Iterator *it = db_->NewIterator(options);
+ CollocatorIterator *cit = new CollocatorIterator(it);
+ cit->Seek(std::string(prefixc,3));// it->Valid() && it->key().starts_with(std::string(prefixc,3)); it->Next()) {
+ cit->setPrefix(prefixc);
+ return cit;
+ }
+
};
namespace {
- void dumpDb(DB* db) {
- char prefixc[sizeof(uint64_t)];
- EncodeFixed64(prefixc, encodeCollocation(1000,0,0));
- ReadOptions options;
- options.prefix_same_as_start = true;
- auto it = unique_ptr<Iterator>(db->NewIterator(options));
-
- for (it->Seek(std::string(prefixc,3)); it->Valid() && it->key().starts_with(std::string(prefixc,3)); it->Next()) {
+ void dumpDb(Collocators counters) {
+ auto it = std::unique_ptr<CollocatorIterator>(counters.SeekIterator(1000,0,0));
+ for (; it->Valid(); it->Next()) {
uint64_t value = DecodeFixed64(it->value().data());
uint64_t key = DecodeFixed64(it->key().data());
- uint64_t w1 = (uint64_t)(key & 0xffffff);
- uint64_t w2 = (uint64_t)((key >> 24) & 0xffffff);
- int8_t dist = (uint64_t)((key >> 56) & 0xff);
- std::cout << "w1:" << w1 << ", w2:" << w2 << ", dist:" << (int32_t) dist << " - count:" << value << std::endl;
+ std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value << std::endl;
}
-
-
assert(it->status().ok()); // Check for any errors found during the scan
}
void testCollocators(Collocators& counters) {
-
- FlushOptions o;
- DB *db = counters.getDb();
-
- o.wait = true;
-
counters.inc(100,200,5);
counters.inc(1000,2000,-5);
counters.inc(1000,2000,5);
@@ -297,8 +321,8 @@
counters.inc(1001,2900,3);
- for(int i=0; i<1000000; i++)
- counters.inc(rand()%1000,rand()%1000,5);
+ for(int i=0; i<10000; i++)
+ counters.inc(rand()%1010,rand()%1010,rand()%10-5);
// dumpDb(db);
@@ -309,7 +333,7 @@
counters.inc(1001,2900,3);
- dumpDb(db);
+ dumpDb(counters);
}
void runTest(int argc, const std::string& dbname, const bool use_ttl = false) {