forked from eBay/Jungle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_log_store_mode.cc
88 lines (69 loc) · 2.61 KB
/
example_log_store_mode.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
80
81
82
83
84
85
86
87
/************************************************************************
Copyright 2017-2019 eBay Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#include <libjungle/jungle.h>
// For removing directory purpose, not needed in practice.
#include "test_common.h"
using namespace jungle;
int main() {
const static std::string path = "./db_example_log_store_mode";
// Remove existing DB first.
TestSuite::clearTestFile(path);
// Initialize global resources with default config.
jungle::init(GlobalConfig());
// Open an instance at the given path, with log store mode.
DB* db = nullptr;
DBConfig db_config;
db_config.logSectionOnly = true;
DB::open(&db, path, db_config);
// Write logs with sequence numbers from 1 to 10.
for (size_t ii=1; ii<=10; ++ii) {
Record rec;
rec.seqNum = ii;
rec.kv.value = SizedBuf( "log" + std::to_string(ii) );
db->setRecord(rec);
}
// Flush in-memory data to actual file
// (`true`: calling `fsync`).
db->sync(true);
// Read logs from 3 to max.
std::cout << "logs from 3 to max:" << std::endl;
Iterator itr;
itr.initSN(db, 3);
do {
Record rec_out;
Record::Holder h(rec_out); // auto free.
itr.get(rec_out);
std::cout << rec_out.seqNum << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (itr.next().ok());
itr.close();
// Log compaction up to 5 (inclusive), logs from 1 to 5 will be discarded.
db->flushLogs(FlushOptions(), 5);
// Read logs from min to max, it will print logs from 6 to 10.
std::cout << "after compaction up to 5:" << std::endl;
Iterator itr2;
itr2.initSN(db);
do {
Record rec_out;
Record::Holder h(rec_out); // auto free.
itr2.get(rec_out);
std::cout << rec_out.seqNum << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (itr2.next().ok());
itr2.close();
// Close DB instance.
DB::close(db);
// Release global resources.
jungle::shutdown();
return 0;
}