forked from eBay/Jungle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_iterator_adv.cc
143 lines (119 loc) · 4.92 KB
/
example_iterator_adv.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/************************************************************************
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_iterator_adv";
// Remove existing DB first.
TestSuite::clearTestFile(path);
// Initialize global resources with default config.
jungle::init(GlobalConfig());
// Open an instance at the given path, with default config.
DB* db = nullptr;
DB::open(&db, path, DBConfig());
// Set {"k00", "v00"}
// {"k10", "v10"}
// ...
// {"k90", "v90"} pairs.
for (size_t ii=0; ii<9; ++ii) {
std::string key_str = "k" + std::to_string(ii) + "0";
std::string val_str = "v" + std::to_string(ii) + "0";
db->set( KV(key_str, val_str) );
}
// Initialize a key-order iterator for the range between
// k15 and k85.
Iterator key_itr;
key_itr.init(db, SizedBuf("k15"), SizedBuf("k85"));
// Iterate all: since both k15 and k85 do not exist,
// it will traverse from k20 to k80.
std::cout << "iterate all:" << std::endl;
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (key_itr.next().ok());
// Seek k65: default option is GREATER,
// cursor will point to k70.
std::cout << "after seek k65:" << std::endl;
key_itr.seek( SizedBuf("k65") );
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (key_itr.next().ok());
// Seek k65 with SMALLER option:
// cursor will point to k60.
std::cout << "after seek k65 with SMALLER:" << std::endl;
key_itr.seek( SizedBuf("k65"), Iterator::SMALLER );
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (key_itr.next().ok());
// Seek k70 with SMALLER option:
// cursor will point to k70, as k70 exists.
std::cout << "after seek k70 with SMALLER:" << std::endl;
key_itr.seek( SizedBuf("k70"), Iterator::SMALLER );
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (key_itr.next().ok());
// Iterate all using prev: should print k20-k80 in reversed order.
std::cout << "iterate all using prev:" << std::endl;
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (key_itr.prev().ok());
// After goto end: cursor is located at the end.
std::cout << "after gotoEnd:" << std::endl;
key_itr.gotoEnd();
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (false); // Print the first record only.
// After goto begin: cursor is located at the beginning.
std::cout << "after gotoBegin:" << std::endl;
key_itr.gotoBegin();
do {
Record rec_out;
Record::Holder h(rec_out); // Auto free.
if (!key_itr.get(rec_out).ok()) break;
std::cout << rec_out.kv.key.toString() << ", "
<< rec_out.kv.value.toString() << std::endl;
} while (false); // Print the first record only.
// Close the key-iterator.
key_itr.close();
// Close and free DB instance.
// All iterator handles should be closed before closing DB instance.
DB::close(db);
// Release global resources.
jungle::shutdown();
return 0;
}