forked from speedb-io/speedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable_speedb_features_example.cc
163 lines (140 loc) · 4.65 KB
/
enable_speedb_features_example.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (C) 2022 Speedb Ltd. All rights reserved.
//
// 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
//
// http://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 <cstdio>
#include <iostream>
#include <string>
#include "rocksdb/compression_type.h"
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/slice_transform.h"
using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath1 = "C:\\Windows\\TEMP\\enable_speedb_features_example1";
std::string kDBPath2 = "C:\\Windows\\TEMP\\enable_speedb_features_example2";
std::string kDBPath3 = "C:\\Windows\\TEMP\\enable_speedb_features_example3";
std::string kDBPath4 = "C:\\Windows\\TEMP\\enable_speedb_features_example4";
#else
std::string kDBPath1 = "/tmp/enable_speedb_features_example1";
std::string kDBPath2 = "/tmp/enable_speedb_features_example2";
std::string kDBPath3 = "/tmp/enable_speedb_features_example3";
std::string kDBPath4 = "/tmp/enable_speedb_features_example4";
#endif
int main() {
DB *db1 = nullptr;
DB *db2 = nullptr;
DB *db3 = nullptr;
DB *db4 = nullptr;
Options op1;
Options op2;
Options op3;
Options op4;
size_t total_ram_size_bytes = 512 * 1024 * 1024;
size_t delayed_write_rate = 256 * 1024 * 1024;
size_t total_threads = 8;
// define SharedOptions object for each databases group
SharedOptions so1(total_ram_size_bytes, total_threads, delayed_write_rate);
// customize each options file except SpeedbSharedOptiopns members
// as listed in the definition of SpeedbSharedOptiopns in options.h
op1.create_if_missing = true;
op1.compression = rocksdb::kNoCompression;
// NOT having a prefix-extractor (the deafult) will result in the
// memtable_factory==HashSpdbRepFactory
//...
op1.EnableSpeedbFeatures(so1);
op2.create_if_missing = true;
op2.compression = rocksdb::kZlibCompression;
// Having a prefix-extractor will result in the
// memtable_factory==SkipListRepFactory
op2.prefix_extractor.reset(NewFixedPrefixTransform(4));
//...
op2.EnableSpeedbFeatures(so1);
// open the databases
Status s = DB::Open(op1, kDBPath1, &db1);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
s = DB::Open(op2, kDBPath2, &db2);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
std::cout << "DBs group 1 was created" << std::endl;
// do the same for any group of databases
total_ram_size_bytes = 1024 * 1024 * 1024;
delayed_write_rate = 128 * 1024 * 1024;
total_threads = 4;
SharedOptions so2(total_ram_size_bytes, total_threads, delayed_write_rate);
// again customize each options object except SharedOptiopns members
op3.create_if_missing = true;
op3.compaction_style = rocksdb::kCompactionStyleUniversal;
//...
op3.EnableSpeedbFeatures(so2);
op4.create_if_missing = true;
op4.compaction_style = rocksdb::kCompactionStyleLevel;
//...
op4.EnableSpeedbFeatures(so2);
// open the databases
s = DB::Open(op3, kDBPath3, &db3);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
s = DB::Open(op4, kDBPath4, &db4);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
std::cout << "DBs group 2 was created" << std::endl;
// creation of column family
rocksdb::ColumnFamilyOptions cfo3(op3);
rocksdb::ColumnFamilyHandle *cf;
// coustomize it except SpeedbSharedOptiopns members
// call EnableSpeedbFeaturesCF and supply for it the same SharedOptions
// object as the DB, so2 this time.
cfo3.EnableSpeedbFeaturesCF(so2);
// create the cf
s = db3->CreateColumnFamily(cfo3, "new_cf", &cf);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
std::cout << "new_cf was created in db3" << std::endl;
// Cleanup
s = db3->DropColumnFamily(cf);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
db3->DestroyColumnFamilyHandle(cf);
if (!s.ok()) {
std::cerr << s.ToString() << std::endl;
return 1;
}
std::cout << "new_cf was destroyed" << std::endl;
s = db1->Close();
assert(s.ok());
s = db2->Close();
assert(s.ok());
s = db3->Close();
assert(s.ok());
s = db4->Close();
assert(s.ok());
delete db1;
delete db2;
delete db3;
delete db4;
return 0;
}