MCDB_File.xs

filter: Statementstmt 86.4% 83 / 96 Branchbran 69.3% 61 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.2% 144 / 184 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/MCDB_File.pm ↑ Summary
1 /*
2 * Copyright (C) 2011, Glue Logic LLC. All rights reserved. code()gluelogic.com
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the same terms as Perl itself, either Perl version 5.12.4 or,
6 * at your option, any later version of Perl 5 you may have available.
7 */
8  
9 #include "EXTERN.h"
10 #include "perl.h"
11 #include "XSUB.h"
12  
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18  
19 #include <mcdb/mcdb.h>
20 #include <mcdb/mcdb_make.h>
21 #include <mcdb/mcdb_makefn.h>
22  
23 struct mcdbxs_read {
24 struct mcdb m;
25 struct mcdb_iter iter;
26 bool values; /* flag for values() processing */
27 };
28  
29 inline
30 static SV *
31 122 mcdbxs_svcp (const unsigned char * const restrict p, const STRLEN len)
32 {
33 /* newSVpvn() allocates len+1 and '\0'-terminates string in returned SV */
34 122 return newSVpvn((const char *)p, len);
35 }
36  
37 static bool
38 52 mcdbxs_is_iterkey (struct mcdb_iter * const restrict iter,
39 const char * const restrict kp, const STRLEN klen)
40 {
41 52 return (iter->ptr
42 43 ▶ && klen == mcdb_iter_keylen(iter)
Branch: 1 branch
BranchTrueFalseTotal
&& klen == mcdb_iter_keylen(iter) 43 0 43
43 43 ▶ && 0 == memcmp(mcdb_iter_keyptr(iter), kp, klen))
Branch: 1 branch
BranchTrueFalseTotal
&& 0 == memcmp(mcdb_iter_keyptr(iter), kp, klen)) 0 43 43
44 95 ▶ || ((iter->ptr = NULL), false);
Branch: 1 branch
BranchTrueFalseTotal
|| ((iter->ptr = NULL), false); 43 9 52
45 }
46  
47 static SV *
48 61 mcdbxs_nextkey (struct mcdbxs_read * const restrict this)
49 {
50 61 struct mcdb_iter * const restrict iter = &this->iter;
51 /* ? sometimes NEXTKEY gets called before FIRSTKEY if hash gets re-tied ? */
52 61 ▶ if (!iter->ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!iter->ptr) 10 51 61
53 10 mcdb_iter_init(iter, &this->m);
54 61 return mcdb_iter(iter)
55 53 ? mcdbxs_svcp(mcdb_iter_keyptr(iter), mcdb_iter_keylen(iter))
56 114 ▶ : (SV *)(iter->ptr = NULL);
Branch: 1 branch
BranchTrueFalseTotal
: (SV *)(iter->ptr = NULL); 53 8 61
57 }
58  
59 static void *
60 26 mcdbxs_malloc (size_t sz)
61 {
62 void * restrict x;
63 26 return Newx(x, sz, char);
64 }
65  
66 static void
67 26 mcdbxs_free (void *p)
68 {
69 26 Safefree(p);
70 26 }
71  
72 static void
73 9 mcdbxs_make_destroy (struct mcdb_make * const restrict mk)
74 {
75 9 mcdb_make_destroy(mk);
76 9 mcdb_makefn_cleanup(mk);
77 9 Safefree(mk);
78 9 }
79  
80 #define mcdbxs_madvise(this,advice) mcdb_mmap_madvise((this)->m.map,(advice))
81  
82  
83 MODULE = MCDB_File PACKAGE = MCDB_File PREFIX = mcdbxs_
84  
85 PROTOTYPES: DISABLED
86  
87 struct mcdbxs_read *
88 mcdbxs_TIEHASH(CLASS, filename)
89 char * CLASS;
90 char * filename;
91 CODE:
92 10 Newx(RETVAL, 1, struct mcdbxs_read);
93 10 RETVAL->m.map = RETVAL->iter.map =
94 10 mcdb_mmap_create(NULL, NULL, filename, mcdbxs_malloc, mcdbxs_free);
95 10 ▶ if (RETVAL->iter.map) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->iter.map) { 9 1 10
96 9 RETVAL->iter.ptr = NULL;
97 9 RETVAL->iter.eod = NULL;
98 9 RETVAL->values = false;
99 }
100 else {
101 1 Safefree(RETVAL);
102 1 XSRETURN_NO;
103 }
104 OUTPUT:
105 RETVAL
106  
107 U32
108 mcdbxs_SCALAR(this)
109 struct mcdbxs_read * this;
110 CODE:
111 0 RETVAL = mcdb_numrecs(&this->m);
112 OUTPUT:
113 RETVAL
114  
115 int
116 mcdbxs_EXISTS(this, k)
117 struct mcdbxs_read * this;
118 SV * k;
119 PREINIT:
120 STRLEN klen;
121 char *kp;
122 INIT:
123 6 ▶ if (!SvOK(k)) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(k)) XSRETURN_NO; 1 5 6
124 CODE:
125 5 kp = SvPV(k, klen);
126 5 ▶ RETVAL = mcdb_find(&this->m, kp, klen);
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = mcdb_find(&this->m, kp, klen); 2 3 5
RETVAL = mcdb_find(&this->m, kp, klen); 2 0 2
RETVAL = mcdb_find(&this->m, kp, klen); 0 5 5
127 OUTPUT:
128 RETVAL
129  
130 SV *
131 mcdbxs_find(this, k)
132 struct mcdbxs_read * this;
133 SV * k;
134 PREINIT:
135 STRLEN klen;
136 char *kp;
137 INIT:
138 0 ▶ if (!SvOK(k)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(k)) XSRETURN_UNDEF; 0 0 0
139 CODE:
140 0 kp = SvPV(k, klen);
141 0 ▶ if (mcdb_find(&this->m, kp, klen))
Branch: 2 branches
BranchTrueFalseTotal
if (mcdb_find(&this->m, kp, klen)) 0 0 0
if (mcdb_find(&this->m, kp, klen)) 0 0 0
142 0 RETVAL = mcdbxs_svcp(mcdb_dataptr(&this->m), mcdb_datalen(&this->m));
143 else
144 0 XSRETURN_UNDEF;
145 OUTPUT:
146 RETVAL
147  
148 SV *
149 mcdbxs_FETCH(this, k)
150 struct mcdbxs_read * this;
151 SV * k;
152 PREINIT:
153 STRLEN klen;
154 char *kp;
155 INIT:
156 53 ▶ if (!SvOK(k)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(k)) XSRETURN_UNDEF; 1 52 53
157 CODE:
158 52 kp = SvPV(k, klen);
159 52 ▶ if (mcdbxs_is_iterkey(&this->iter, kp, klen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mcdbxs_is_iterkey(&this->iter, kp, klen)) { 43 9 52
160 43 RETVAL = mcdbxs_svcp(mcdb_iter_dataptr(&this->iter),
161 43 mcdb_iter_datalen(&this->iter));
162 43 ▶ if (this->values && !mcdb_iter(&this->iter)) {
Branch: 2 branches
BranchTrueFalseTotal
if (this->values && !mcdb_iter(&this->iter)) { 13 30 43
if (this->values && !mcdb_iter(&this->iter)) { 2 11 13
163 2 this->values = false; this->iter.ptr = this->iter.eod = NULL;
164 }
165 }
166 9 ▶ else if (mcdb_find(&this->m, kp, klen)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (mcdb_find(&this->m, kp, klen)) { 8 1 9
else if (mcdb_find(&this->m, kp, klen)) { 8 0 8
167 8 RETVAL = mcdbxs_svcp(mcdb_dataptr(&this->m), mcdb_datalen(&this->m));
168 /* messiness required to detect Perl calling FETCH for values()
169 * after having obtained all FIRSTKEY/NEXTKEY; needed to support
170 * (duplicated) keys with multiple values, permitted in mcdb */
171 8 ▶ if (this->iter.eod && MCDB_HEADER_SZ == mcdb_datapos(&this->m)-klen-8) {
Branch: 2 branches
BranchTrueFalseTotal
if (this->iter.eod && MCDB_HEADER_SZ == mcdb_datapos(&this->m)-klen-8) { 3 5 8
if (this->iter.eod && MCDB_HEADER_SZ == mcdb_datapos(&this->m)-klen-8) { 2 1 3
172 2 this->values = true;
173 2 mcdb_iter_init(&this->iter, &this->m);
174 2 ▶ if (!mcdb_iter(&this->iter) || !mcdb_iter(&this->iter)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!mcdb_iter(&this->iter) || !mcdb_iter(&this->iter)) { 2 0 2
if (!mcdb_iter(&this->iter) || !mcdb_iter(&this->iter)) { 0 2 2
175 0 this->values = false; this->iter.ptr = this->iter.eod = NULL;
176 }
177 }
178 }
179 else
180 1 XSRETURN_UNDEF;
181 OUTPUT:
182 RETVAL
183  
184 SV *
185 mcdbxs_FIRSTKEY(this)
186 struct mcdbxs_read * this;
187 CODE:
188 9 this->iter.ptr = NULL; /* should already be NULL */
189 9 RETVAL = mcdbxs_nextkey(this);
190 9 ▶ if (!RETVAL) XSRETURN_UNDEF;/* empty database */
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) XSRETURN_UNDEF;/* empty database */ 1 8 9
191 OUTPUT:
192 RETVAL
193  
194 SV *
195 mcdbxs_NEXTKEY(this, k)
196 struct mcdbxs_read * this;
197 SV * k;
198 INIT:
199 52 ▶ if (!SvOK(k)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(k)) XSRETURN_UNDEF; 0 52 52
200 CODE:
201 52 RETVAL = mcdbxs_nextkey(this);
202 52 ▶ if (!RETVAL) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) XSRETURN_UNDEF; 7 45 52
203 OUTPUT:
204 RETVAL
205  
206 void
207 mcdbxs_DESTROY(sv)
208 SV * sv;
209 PREINIT:
210 struct mcdbxs_read *this;
211 CODE:
212 9 ▶ if (sv_isobject(sv) && SvTYPE(SvRV(sv)) == SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(sv) && SvTYPE(SvRV(sv)) == SVt_PVMG) { 9 0 9
if (sv_isobject(sv) && SvTYPE(SvRV(sv)) == SVt_PVMG) { 9 0 9
213 9 this = INT2PTR(struct mcdbxs_read *, SvIV((SV *)SvRV(sv)));
214 9 mcdb_mmap_destroy(this->m.map);
215 9 Safefree(this);
216 }
217  
218 AV *
219 mcdbxs_multi_get(this, k)
220 struct mcdbxs_read * this;
221 SV * k;
222 PREINIT:
223 STRLEN klen;
224 char *kp;
225 INIT:
226 12 ▶ if (!SvOK(k)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(k)) XSRETURN_UNDEF; 0 12 12
227 CODE:
228 12 kp = SvPV(k, klen);
229 12 RETVAL = newAV(); /* might be redundant (see .c generated from .xs) */
230 12 sv_2mortal((SV *)RETVAL);
231 12 ▶ if (mcdb_findstart(&this->m, kp, klen))
Branch: 1 branch
BranchTrueFalseTotal
if (mcdb_findstart(&this->m, kp, klen)) 11 1 12
232 29 ▶ while (mcdb_findnext(&this->m, kp, klen))
Branch: 1 branch
BranchTrueFalseTotal
while (mcdb_findnext(&this->m, kp, klen)) 18 11 29
233 18 av_push(RETVAL,
234 mcdbxs_svcp(mcdb_dataptr(&this->m),mcdb_datalen(&this->m)));
235 OUTPUT:
236 RETVAL
237  
238 void
239 mcdbxs_madvise(this, advice)
240 struct mcdbxs_read * this;
241 int advice;
242  
243  
244 MODULE = MCDB_File PACKAGE = MCDB_File::Make PREFIX = mcdbxs_make_
245  
246 # /*(MCDB_FILE::Make::insert instead of STORE to support multi-insert)*/
247 # /*(mcdb supports multiple records with same key, so a STORE method might be
248 # * misleading if caller incorrectly assumes value replaced for repeated key)*/
249 void
250 mcdbxs_make_insert(mk, ...)
251 struct mcdb_make * mk;
252 PREINIT:
253 SV *k, *v;
254 char *kp, *vp;
255 STRLEN klen, vlen;
256 int x;
257 PPCODE:
258 62 ▶ for (x = 1; x+1 < items; x += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 1; x+1 < items; x += 2) { 31 31 62
259 31 k = ST(x); v = ST(x+1);
260 31 ▶ if (SvOK(k) && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(k) && SvOK(v)) { 31 0 31
if (SvOK(k) && SvOK(v)) { 31 0 31
261 31 ▶ kp = SvPV(k, klen); vp = SvPV(v, vlen);
Branch: 1 branch
BranchTrueFalseTotal
kp = SvPV(k, klen); vp = SvPV(v, vlen); 18 13 31
262 31 ▶ if (mcdb_make_add(mk, kp, klen, vp, vlen) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (mcdb_make_add(mk, kp, klen, vp, vlen) != 0) 0 31 31
263 0 croak("MCDB_File::Make::insert: %s", Strerror(errno));
264 }
265 else
266 0 croak("MCDB_File::Make::insert: invalid argument");
267 }
268  
269 struct mcdb_make *
270 mcdbxs_make_new(CLASS, fname, ...)
271 char * CLASS;
272 char * fname;
273 PREINIT:
274 struct mcdb_make *mk;
275 CODE:
276 9 RETVAL = Newx(mk, 1, struct mcdb_make);
277 9 ▶ if (mcdb_makefn_start(mk, fname, mcdbxs_malloc, mcdbxs_free) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (mcdb_makefn_start(mk, fname, mcdbxs_malloc, mcdbxs_free) == 0 8 1 9
278 8 ▶ && mcdb_make_start(mk, mk->fd, mcdbxs_malloc, mcdbxs_free) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& mcdb_make_start(mk, mk->fd, mcdbxs_malloc, mcdbxs_free) == 0) { 8 0 8
279 8 ▶ if (items >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) 0 8 8
280 0 mk->st_mode = SvIV(ST(2)); /* optional mcdb perm mode */
281 }
282 else {
283 1 mcdbxs_make_destroy(mk);
284 1 XSRETURN_UNDEF;
285 }
286 OUTPUT:
287 RETVAL
288  
289 void
290 mcdbxs_make_DESTROY(sv)
291 SV * sv;
292 CODE:
293 8 ▶ if (sv_isobject(sv) && SvTYPE(SvRV(sv)) == SVt_PVMG)
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(sv) && SvTYPE(SvRV(sv)) == SVt_PVMG) 8 0 8
if (sv_isobject(sv) && SvTYPE(SvRV(sv)) == SVt_PVMG) 8 0 8
294 8 mcdbxs_make_destroy(INT2PTR(struct mcdb_make *,SvIV((SV *)SvRV(sv))));
295  
296 void
297 mcdbxs_make_finish(mk, ...)
298 struct mcdb_make * mk;
299 PREINIT:
300 8 bool do_fsync = true;
301 CODE:
302 8 ▶ if (items >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) 0 8 8
303 0 do_fsync = SvIV(ST(1)) != 0; /* optional control fsync */
304 8 ▶ if (mcdb_make_finish(mk) != 0 || mcdb_makefn_finish(mk, do_fsync) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mcdb_make_finish(mk) != 0 || mcdb_makefn_finish(mk, do_fsync) != 0) { 8 0 8
if (mcdb_make_finish(mk) != 0 || mcdb_makefn_finish(mk, do_fsync) != 0) { 0 8 8
305 /*mcdb_make_destroy(mk);*//* already called in mcdb_make_finish() */
306 0 mcdb_makefn_cleanup(mk);
307 0 croak("MCDB_File::Make::finish: %s", Strerror(errno));
308 }
« blib/lib/MCDB_File.pm ↑ Summary