-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmain.cpp
89 lines (67 loc) · 2.42 KB
/
main.cpp
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
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "LocalMemory/HashTableService.h"
using namespace L4;
void SimpleExample() {
EpochManagerConfig epochConfig{1000, std::chrono::milliseconds(100), 1};
LocalMemory::HashTableService service{epochConfig};
auto hashTableIndex = service.AddHashTable(
HashTableConfig("Table1", HashTableConfig::Setting{1000000}));
std::vector<std::pair<std::string, std::string>> keyValuePairs = {
{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"},
{"key4", "value4"}, {"key5", "value5"},
};
// Write data.
{
auto context = service.GetContext();
auto& hashTable = context[hashTableIndex];
for (const auto& keyValuePair : keyValuePairs) {
const auto& keyStr = keyValuePair.first;
const auto& valStr = keyValuePair.second;
IWritableHashTable::Key key;
key.m_data = reinterpret_cast<const std::uint8_t*>(keyStr.c_str());
key.m_size = keyStr.size();
IWritableHashTable::Value val;
val.m_data = reinterpret_cast<const std::uint8_t*>(valStr.c_str());
val.m_size = valStr.size();
hashTable.Add(key, val);
}
}
// Read data.
{
auto context = service.GetContext();
// Once a context is retrieved, the operations such as
// operator[] on the context and Get() are lock-free.
auto& hashTable = context[hashTableIndex];
for (const auto& keyValuePair : keyValuePairs) {
const auto& keyStr = keyValuePair.first;
IWritableHashTable::Key key;
key.m_data = reinterpret_cast<const std::uint8_t*>(keyStr.c_str());
key.m_size = keyStr.size();
IWritableHashTable::Value val;
hashTable.Get(key, val);
std::cout << std::string(reinterpret_cast<const char*>(val.m_data),
val.m_size)
<< std::endl;
}
}
}
void CacheHashTableExample() {
LocalMemory::HashTableService service;
HashTableConfig::Cache cacheConfig{
1024 * 1024, // 1MB cache
std::chrono::seconds(60), // Record will exipre in 60 seconds
true // Remove any expired records during eviction.
};
auto hashTableIndex = service.AddHashTable(HashTableConfig(
"Table1", HashTableConfig::Setting{1000000}, cacheConfig));
(void)hashTableIndex;
// Use hash table similar to SimpleExample().
}
int main() {
SimpleExample();
CacheHashTableExample();
return 0;
}