This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hash map class template | |
template <typename K, typename V, typename F = KeyHash<K>> | |
class HashMap { | |
public: | |
HashMap() { | |
// construct zero initialized hash table of size | |
table = new HashNode<K, V> *[TABLE_SIZE](); | |
} | |
~HashMap() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MyKeyHash { | |
unsigned long operator()(const int& k) const | |
{ | |
return k % 10; | |
} | |
}; | |
HashMap<int, string, MyKeyHash> hmap; | |
hmap.put(1, "val1"); | |
hmap.put(2, "val2"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hash node class template | |
template <typename K, typename V> | |
class HashNode { | |
public: | |
HashNode(const K &key, const V &value) : | |
key(key), value(value), next(NULL) { | |
} | |
K getKey() const { | |
return key; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default hash function class | |
template <typename K> | |
struct KeyHash { | |
unsigned long operator()(const K& key) const | |
{ | |
return reinterpret_cast<unsigned long>(key) % TABLE_SIZE; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <assert.h> | |
#include "rocksdb/db.h" | |
using namespace std; | |
int main() { | |
rocksdb::DB* db; | |
rocksdb::Options options; | |
options.create_if_missing = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocksdb::Slice key1 = "one"; | |
rocksdb::Slice key2 = "two"; | |
std::string bar = "bar"; | |
db->Put(rocksdb::WriteOptions(), key1, bar); | |
std::string value; | |
rocksdb::Status s = db->Get(rocksdb::ReadOptions(), key1, &value); | |
if (s.ok()) { | |
// atomically apply a set of updates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocksdb::Slice key1 = "one"; | |
std::string bar = "bar"; | |
rocksdb::WriteOptions write_options; | |
write_options.sync = true; | |
db->Put(write_options, key1, bar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocksdb::Slice key1 = "1"; | |
rocksdb::Slice key2 = "2"; | |
rocksdb::Slice key3 = "3"; | |
std::string val1 = "one"; | |
std::string val2 = "two"; | |
std::string val3 = "three"; | |
// populate the database with entries | |
db->Put(rocksdb::WriteOptions(), key1, val1); | |
db->Put(rocksdb::WriteOptions(), key2, val2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocksdb::Slice key1 = "1"; | |
rocksdb::Slice key2 = "2"; | |
std::string val1 = "one"; | |
std::string val2 = "two"; | |
// populate db first | |
db->Put(rocksdb::WriteOptions(), key1, val1); | |
db->Put(rocksdb::WriteOptions(), key2, val2); | |
rocksdb::ReadOptions readOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TwoPartComparator : public rocksdb::Comparator { | |
public: | |
// Three-way comparison function: | |
// if a < b: negative result | |
// if a > b: positive result | |
// else: zero result | |
int Compare(const rocksdb::Slice& a, const rocksdb::Slice& b) const { | |
int a1, a2, b1, b2; | |
ParseKey(a, &a1, &a2); | |
ParseKey(b, &b1, &b2); |
OlderNewer