LevelDB.xs

filter: Statementstmt 67.3% 101 / 150 Branchbran 39.9% 83 / 208 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 51.4% 184 / 358 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/dbformat.cc ↑ Summary table/table.cc »
1  
2 // Make sure C++ headers are included before PERL headers to avoid errors.
3 #include<iostream>
4 #include<leveldb/db.h>
5 #include<leveldb/slice.h>
6 #include<leveldb/iterator.h>
7 #include<leveldb/write_batch.h>
8  
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15  
16 #include "ppport.h"
17  
18 //#include <Tie::LevelDB>
19  
20 #ifdef __cplusplus
21 }
22 #endif
23  
24 // winnt.h already defines DELETE which overlaps our DELETE method.
25 #undef DELETE
26  
27 25 void status_assert(leveldb::Status s) {
28 25 ▶ if(!s.ok()) croak("%s",s.ToString().c_str());
Branch: 2 branches
BranchTrueFalseTotal
if(!s.ok()) croak("%s",s.ToString().c_str()); 1 24 25
if(!s.ok()) croak("%s",s.ToString().c_str()); 1 0 1
29 24 }
30 3 SV* newSVstring(std::string str) {
31 3 return newSVpvn(str.data(),str.length());
32 }
33 7 SV* newSVslice(leveldb::Slice slice) {
34 7 return newSVpvn(slice.data(),slice.size());
35 }
36 34 std::string SV2string(SV* sv) {
37 STRLEN len;
38 34 char * ptr = SvPV(sv, len);
39 68 ▶ return std::string(ptr,len);
Branch: 1 branch
BranchTrueFalseTotal
return std::string(ptr,len); 34 0 34
40 }
41  
42 class Iterator {
43 protected:
44 leveldb::Iterator *it;
45 public:
46 0 Iterator() {
47 0 it = NULL; // die!
48 0 }
49 0 Iterator(leveldb::Iterator *it) : it(it) { }
50 0 ▶ ~Iterator() { delete it; it = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
~Iterator() { delete it; it = NULL; } 0 0 0
51 0 void SeekToFirst() { it->SeekToFirst(); }
52 0 void SeekToLast() { it->SeekToLast(); }
53 0 void Seek(const SV* sv_target) {
54 0 leveldb::Slice target(SvPVX(sv_target), SvCUR(sv_target));
55 0 ▶ it->Seek(target);
Branch: 1 branch
BranchTrueFalseTotal
it->Seek(target); 0 0 0
56 0 }
57 0 void Next() { it->Next(); }
58 0 void Prev() { it->Prev(); }
59 0 bool Valid() { return it->Valid(); }
60 0 SV* key() {
61 0 ▶ SV* k = newSVstring(it->key().ToString());
Branch: 3 branches
BranchTrueFalseTotal
SV* k = newSVstring(it->key().ToString()); 0 0 0
SV* k = newSVstring(it->key().ToString()); 0 0 0
SV* k = newSVstring(it->key().ToString()); 0 0 0
62 0 ▶ status_assert(it->status());
Branch: 2 branches
BranchTrueFalseTotal
status_assert(it->status()); 0 0 0
status_assert(it->status()); 0 0 0
63 0 return k;
64 }
65 0 SV* value() {
66 0 ▶ SV* v = newSVstring(it->value().ToString());
Branch: 3 branches
BranchTrueFalseTotal
SV* v = newSVstring(it->value().ToString()); 0 0 0
SV* v = newSVstring(it->value().ToString()); 0 0 0
SV* v = newSVstring(it->value().ToString()); 0 0 0
67 0 ▶ status_assert(it->status());
Branch: 2 branches
BranchTrueFalseTotal
status_assert(it->status()); 0 0 0
status_assert(it->status()); 0 0 0
68 0 return v;
69 }
70 };
71  
72 class WriteBatch {
73 protected:
74 leveldb::WriteBatch *batch;
75 public:
76 0 leveldb::WriteBatch* get_batch() { return batch; }
77  
78 0 WriteBatch() {
79 0 ▶ batch = new leveldb::WriteBatch();
Branch: 2 branches
BranchTrueFalseTotal
batch = new leveldb::WriteBatch(); 0 0 0
batch = new leveldb::WriteBatch(); 0 0 0
80 0 }
81 0 ~WriteBatch() {
82 0 ▶ delete batch;
Branch: 1 branch
BranchTrueFalseTotal
delete batch; 0 0 0
83 0 }
84 0 void Put(const char* key,const char * cvalue) {
85 0 ▶ if(cvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if(cvalue) { 0 0 0
86 0 ▶ std::string* value = new std::string(cvalue);
Branch: 2 branches
BranchTrueFalseTotal
std::string* value = new std::string(cvalue); 0 0 0
std::string* value = new std::string(cvalue); 0 0 0
87 0 ▶ batch->Put(key, *value);
Branch: 1 branch
BranchTrueFalseTotal
batch->Put(key, *value); 0 0 0
88 0 } else Delete(key); // LevelDB limitation..
89 0 }
90 0 void Delete(const char * key) {
91 0 ▶ batch->Delete(key);
Branch: 1 branch
BranchTrueFalseTotal
batch->Delete(key); 0 0 0
92 0 }
93 };
94  
95 class DB {
96 protected:
97 leveldb::DB *db;
98 public:
99 DB() : db(NULL) { }
100 1 DB(const char* name,HV* hv_options=NULL ) : db(NULL) {
101 1 Open(name,hv_options);
102 1 }
103 1 ~DB() {
104 1 ▶ if(db) { delete db; db = NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if(db) { delete db; db = NULL; } 1 0 1
if(db) { delete db; db = NULL; } 1 0 1
105 1 }
106 1 void Open(const char* name,HV* hv_options=NULL) {
107 1 ▶ leveldb::Options options; // todo: construct
Branch: 1 branch
BranchTrueFalseTotal
leveldb::Options options; // todo: construct 1 0 1
108 1 options.create_if_missing = true;
109 // options.error_if_exists = true;
110 1 ▶ if(db) delete db;
Branch: 2 branches
BranchTrueFalseTotal
if(db) delete db; 0 1 1
if(db) delete db; 0 0 0
111 2 ▶ status_assert(leveldb::DB::Open(options, name, &db));
Branch: 3 branches
BranchTrueFalseTotal
status_assert(leveldb::DB::Open(options, name, &db)); 1 0 1
status_assert(leveldb::DB::Open(options, name, &db)); 1 0 1
status_assert(leveldb::DB::Open(options, name, &db)); 1 0 1
112 1 }
113 1 void Put(const char* key,const char* cvalue=NULL,
114 HV* hv_write_options=NULL) {
115 1 leveldb::WriteOptions write_options;
116 1 ▶ if(cvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if(cvalue) { 1 0 1
117 2 ▶ std::string* value = new std::string(cvalue);
Branch: 3 branches
BranchTrueFalseTotal
std::string* value = new std::string(cvalue); 1 0 1
std::string* value = new std::string(cvalue); 1 0 1
std::string* value = new std::string(cvalue); 0 0 0
118 1 ▶ status_assert(db->Put(write_options, key, *value));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Put(write_options, key, *value)); 1 0 1
status_assert(db->Put(write_options, key, *value)); 1 0 1
119 } else {
120 0 ▶ status_assert(db->Delete(leveldb::WriteOptions(), key));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Delete(leveldb::WriteOptions(), key)); 0 0 0
status_assert(db->Delete(leveldb::WriteOptions(), key)); 0 0 0
121 }
122 1 }
123 3 SV* Get(const char* key) {
124 3 std::string value;
125 3 ▶ leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value);
Branch: 1 branch
BranchTrueFalseTotal
leveldb::Status s = db->Get(leveldb::ReadOptions(), key, &value); 3 0 3
126 3 ▶ if(s.IsNotFound()) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if(s.IsNotFound()) return NULL; 3 0 3
if(s.IsNotFound()) return NULL; 2 1 3
127 1 ▶ status_assert(s);
Branch: 2 branches
BranchTrueFalseTotal
status_assert(s); 1 0 1
status_assert(s); 1 0 1
128 1 ▶ return newSVstring(value);
Branch: 2 branches
BranchTrueFalseTotal
return newSVstring(value); 1 0 1
return newSVstring(value); 1 0 1
129 3 }
130 1 void Delete(const char* key) {
131 1 ▶ status_assert(db->Delete(leveldb::WriteOptions(), key));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Delete(leveldb::WriteOptions(), key)); 1 0 1
status_assert(db->Delete(leveldb::WriteOptions(), key)); 1 0 1
132 1 }
133 0 void Write(WriteBatch* batch,HV* hv_write_options=NULL) {
134 0 leveldb::WriteOptions write_options; // todo: construct
135 0 ▶ status_assert(db->Write(write_options,batch->get_batch()));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Write(write_options,batch->get_batch())); 0 0 0
status_assert(db->Write(write_options,batch->get_batch())); 0 0 0
136 0 }
137 0 Iterator* NewIterator(HV* hv_read_options=NULL) {
138 0 leveldb::ReadOptions read_options; // todo: construct
139 0 ▶ return new Iterator(db->NewIterator(read_options));
Branch: 3 branches
BranchTrueFalseTotal
return new Iterator(db->NewIterator(read_options)); 0 0 0
return new Iterator(db->NewIterator(read_options)); 0 0 0
return new Iterator(db->NewIterator(read_options)); 0 0 0
140 }
141 };
142  
143 class LevelDB {
144 leveldb::DB* db;
145 leveldb::Iterator* it;
146 leveldb::Options options;
147 leveldb::WriteOptions write_options;
148 leveldb::ReadOptions read_options;
149 leveldb::WriteBatch batch;
150
151 public:
152 0 LevelDB() : db(NULL), it(NULL) {}
153 3 LevelDB(const char* name,HV* hv_options=NULL) {
154 3 db = NULL; it = NULL;
155 3 ▶ Open(name,hv_options);
Branch: 1 branch
BranchTrueFalseTotal
Open(name,hv_options); 2 0 2
156 2 }
157 3 void Open(const char* name,HV* hv_options=NULL) {
158 3 options.create_if_missing = true;
159 6 ▶ status_assert(leveldb::DB::Open(options,name,&db));
Branch: 3 branches
BranchTrueFalseTotal
status_assert(leveldb::DB::Open(options,name,&db)); 3 0 3
status_assert(leveldb::DB::Open(options,name,&db)); 3 0 3
status_assert(leveldb::DB::Open(options,name,&db)); 2 0 2
160 2 }
161 2 ~LevelDB() {
162 2 ▶ if(it) delete it;
Branch: 2 branches
BranchTrueFalseTotal
if(it) delete it; 2 0 2
if(it) delete it; 2 0 2
163 2 ▶ if(db) delete db;
Branch: 2 branches
BranchTrueFalseTotal
if(db) delete db; 2 0 2
if(db) delete db; 2 0 2
164 2 }
165 5 SV* FETCH(SV* sv_key) {
166 5 ▶ std::string key = SV2string(sv_key);
Branch: 1 branch
BranchTrueFalseTotal
std::string key = SV2string(sv_key); 5 0 5
167 5 std::string value;
168 5 ▶ leveldb::Status s = db->Get(read_options,key,&value);
Branch: 1 branch
BranchTrueFalseTotal
leveldb::Status s = db->Get(read_options,key,&value); 5 0 5
169 5 ▶ if(s.IsNotFound()) return newSV(0);
Branch: 3 branches
BranchTrueFalseTotal
if(s.IsNotFound()) return newSV(0); 5 0 5
if(s.IsNotFound()) return newSV(0); 3 2 5
if(s.IsNotFound()) return newSV(0); 3 0 3
170 2 ▶ status_assert(s);
Branch: 2 branches
BranchTrueFalseTotal
status_assert(s); 2 0 2
status_assert(s); 2 0 2
171 2 ▶ return newSVstring(value);
Branch: 2 branches
BranchTrueFalseTotal
return newSVstring(value); 2 0 2
return newSVstring(value); 2 0 2
172 5 }
173 8 void STORE(SV* sv_key,SV* sv_value) {
174 8 ▶ std::string key = SV2string(sv_key);
Branch: 1 branch
BranchTrueFalseTotal
std::string key = SV2string(sv_key); 8 0 8
175 8 ▶ std::string value = SV2string(sv_value);
Branch: 1 branch
BranchTrueFalseTotal
std::string value = SV2string(sv_value); 8 0 8
176 8 ▶ status_assert(db->Put(write_options,key,value));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Put(write_options,key,value)); 8 0 8
status_assert(db->Put(write_options,key,value)); 8 0 8
177 8 }
178 7 void DELETE(SV* sv_key) {
179 7 ▶ std::string key = SV2string(sv_key);
Branch: 1 branch
BranchTrueFalseTotal
std::string key = SV2string(sv_key); 7 0 7
180 7 ▶ status_assert(db->Delete(write_options,key));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Delete(write_options,key)); 7 0 7
status_assert(db->Delete(write_options,key)); 7 0 7
181 7 }
182 1 void CLEAR() {
183 1 ▶ leveldb::WriteBatch batch;
Branch: 1 branch
BranchTrueFalseTotal
leveldb::WriteBatch batch; 1 0 1
184 1 ▶ leveldb::Iterator* it = db->NewIterator(read_options);
Branch: 1 branch
BranchTrueFalseTotal
leveldb::Iterator* it = db->NewIterator(read_options); 1 0 1
185 2 ▶ for(it->SeekToFirst();it->Valid();it->Next())
Branch: 3 branches
BranchTrueFalseTotal
for(it->SeekToFirst();it->Valid();it->Next()) 1 0 1
for(it->SeekToFirst();it->Valid();it->Next()) 2 0 2
for(it->SeekToFirst();it->Valid();it->Next()) 1 1 2
186 1 ▶ batch.Delete(it->key().ToString().c_str()); // TODO: c_str()??
Branch: 4 branches
BranchTrueFalseTotal
batch.Delete(it->key().ToString().c_str()); // TODO: c_str()?? 1 0 1
batch.Delete(it->key().ToString().c_str()); // TODO: c_str()?? 1 0 1
batch.Delete(it->key().ToString().c_str()); // TODO: c_str()?? 1 0 1
batch.Delete(it->key().ToString().c_str()); // TODO: c_str()?? 1 0 1
187 1 ▶ delete it;
Branch: 1 branch
BranchTrueFalseTotal
delete it; 1 0 1
188 1 ▶ status_assert(db->Write(write_options,&batch));
Branch: 2 branches
BranchTrueFalseTotal
status_assert(db->Write(write_options,&batch)); 1 0 1
status_assert(db->Write(write_options,&batch)); 1 0 1
189 1 }
190 6 bool EXISTS(SV* sv_key) {
191 6 ▶ std::string key = SV2string(sv_key);
Branch: 1 branch
BranchTrueFalseTotal
std::string key = SV2string(sv_key); 6 0 6
192 6 ▶ leveldb::Iterator* find = db->NewIterator(read_options);
Branch: 1 branch
BranchTrueFalseTotal
leveldb::Iterator* find = db->NewIterator(read_options); 6 0 6
193 6 ▶ find->Seek(key);
Branch: 1 branch
BranchTrueFalseTotal
find->Seek(key); 6 0 6
194 6 ▶ bool valid = find->Valid();
Branch: 1 branch
BranchTrueFalseTotal
bool valid = find->Valid(); 6 0 6
195 6 ▶ delete(find);
Branch: 1 branch
BranchTrueFalseTotal
delete(find); 6 0 6
196 6 return valid;
197 6 }
198 6 SV* FIRSTKEY() {
199 6 ▶ if(it) delete it;
Branch: 2 branches
BranchTrueFalseTotal
if(it) delete it; 4 2 6
if(it) delete it; 4 0 4
200 6 it = db->NewIterator(read_options);
201 6 it->SeekToFirst();
202 6 ▶ return it->Valid() ? newSVslice(it->key()) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return it->Valid() ? newSVslice(it->key()) : newSV(0); 5 1 6
203 }
204 7 SV* NEXTKEY(SV* sv_lastkey) {
205 7 ▶ if(!it) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!it) return NULL; 0 7 7
206 7 it->Next();
207 7 ▶ return it->Valid() ? newSVslice(it->key()) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return it->Valid() ? newSVslice(it->key()) : newSV(0); 2 5 7
208 }
209 1 int SCALAR() {
210 1 int count = 0;
211 1 leveldb::Iterator* it = db->NewIterator(read_options);
212 3 ▶ for(it->SeekToFirst();it->Valid();it->Next()) count++;
Branch: 1 branch
BranchTrueFalseTotal
for(it->SeekToFirst();it->Valid();it->Next()) count++; 2 1 3
213 1 ▶ delete it;
Branch: 1 branch
BranchTrueFalseTotal
delete it; 1 0 1
214 1 return count;
215 }
216 };
217  
218 MODULE = Tie::LevelDB PACKAGE = Tie::LevelDB::DB
219  
220 DB*
221 DB::new(char* name=NULL,HV* hv_options=Nullhv)
222  
223 void
224 DB::Open(char* name,HV* hv_options=Nullhv)
225  
226 void
227 DB::DESTROY()
228  
229 void
230 DB::Put(char* key,char* value=NULL)
231  
232 SV*
233 DB::Get(const char * key)
234  
235 void
236 DB::Delete(char * key)
237  
238 Iterator*
239 DB::NewIterator(HV* hv_read_options=Nullhv);
240 CODE:
241 0 const char* CLASS = "Tie::LevelDB::Iterator";
242 0 RETVAL = THIS->NewIterator(hv_read_options);
243 OUTPUT:
244 RETVAL
245  
246 void
247 DB::Write(WriteBatch* batch, HV* hv_write_options=Nullhv)
248  
249 MODULE = Tie::LevelDB PACKAGE = Tie::LevelDB::WriteBatch
250  
251 WriteBatch*
252 WriteBatch::new()
253  
254 void
255 WriteBatch::Put(const char* key,const char* value)
256  
257 void
258 WriteBatch::Delete(const char* key)
259  
260 void
261 WriteBatch::DESTROY()
262  
263 MODULE = Tie::LevelDB PACKAGE = Tie::LevelDB::Iterator
264  
265 Iterator*
266 Iterator::new()
267  
268 void
269 Iterator::DESTROY()
270  
271 void
272 Iterator::Seek(SV* sv_target)
273  
274 void
275 Iterator::SeekToFirst()
276  
277 void
278 Iterator::SeekToLast()
279  
280 void
281 Iterator::Prev()
282  
283 void
284 Iterator::Next()
285  
286 bool
287 Iterator::Valid()
288  
289 SV*
290 Iterator::key()
291  
292 SV*
293 Iterator::value()
294  
295 MODULE = Tie::LevelDB PACKAGE = Tie::LevelDB
296  
297 LevelDB*
298 LevelDB::new()
299  
300 SV*
301 LevelDB::FETCH(SV* key)
302  
303 void
304 LevelDB::STORE(SV* sv_key,...)
305 CODE:
306 9 ▶ if(SvOK(ST(2))) THIS->STORE(sv_key,ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(ST(2))) THIS->STORE(sv_key,ST(2)); 8 1 9
307 1 else THIS->DELETE(sv_key);
308  
309 void
310 LevelDB::DELETE(SV* sv_key)
311  
312 void
313 LevelDB::CLEAR()
314  
315 bool
316 LevelDB::EXISTS(SV* sv_key)
317  
318 SV*
319 LevelDB::FIRSTKEY()
320  
321 SV*
322 LevelDB::NEXTKEY(SV* sv_lastkey)
323  
324 int
325 LevelDB::SCALAR()
326  
327 void
328 LevelDB::DESTROY()
329  
330 LevelDB*
331 TIEHASH(const char* CLASS,const char* name,HV* hv_options=Nullhv)
332 CODE:
333 3 ▶ RETVAL = new LevelDB(name,hv_options);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = new LevelDB(name,hv_options); 2 0 2
RETVAL = new LevelDB(name,hv_options); 0 0 0
334 OUTPUT:
335 RETVAL
336  
337  
« db/dbformat.cc ↑ Summary table/table.cc »