1 |
/* |
2 |
The Malete project - the Z39.2/Z39.50 database framework of OpenIsis. |
3 |
Version 0.9.x (patchlevel see file Version) |
4 |
Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org |
5 |
|
6 |
This library is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU Lesser General Public |
8 |
License as published by the Free Software Foundation; either |
9 |
version 2.1 of the License, or (at your option) any later version. |
10 |
|
11 |
This library is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14 |
See the GNU Lesser General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU Lesser General Public |
17 |
License along with this library; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
see README for more information |
21 |
EOH */ |
22 |
|
23 |
/* |
24 |
$Id: test.c,v 1.22 2004/11/01 14:15:48 kripke Exp $ |
25 |
core test |
26 |
*/ |
27 |
|
28 |
|
29 |
#include <stdio.h> |
30 |
#include <stdlib.h> |
31 |
#include <string.h> |
32 |
|
33 |
#include "../core/core.h" |
34 |
|
35 |
|
36 |
static int qCb ( QLoop *self ) |
37 |
{ |
38 |
Ptr p; |
39 |
unsigned i, vsz = self->qdx->vsz; |
40 |
const unsigned char *v = self->vals; |
41 |
char buf[512]; |
42 |
int len = qRdKey(self->qdx, buf, sizeof buf, &self->cur); |
43 |
|
44 |
for ( i=self->nvals; i--; v += vsz) { |
45 |
qRdVal(&p, v, self->qdx->ptr); |
46 |
eRr( 0, "%.*s\t%u\t%d\t%u\t%u", len, buf, |
47 |
p.rid, p.tag, p.pos>>16, p.pos&0xffff); |
48 |
} |
49 |
return 0; |
50 |
} /* qCb */ |
51 |
|
52 |
|
53 |
static int qDelCb ( QLoop *self ) /* kill one value from every seventh key */ |
54 |
{ |
55 |
static int pass; |
56 |
Ptr p; |
57 |
unsigned i, vsz; |
58 |
const unsigned char *v; |
59 |
|
60 |
if ( ++pass % 7 ) |
61 |
return 0; |
62 |
vsz = self->qdx->vsz; |
63 |
i = 42 % self->nvals; |
64 |
v = self->vals + i*vsz; |
65 |
memcpy(self->cur.val.byt, v, self->cur.val.len=vsz); |
66 |
qRdVal(&p, v, self->qdx->ptr); |
67 |
eRr( 0, "%.*s\t%u\t%d\t%u\t%u", self->cur.len, self->cur.byt, |
68 |
p.rid, p.tag, p.pos>>16, p.pos&0xffff); |
69 |
qDel(self->qdx, &self->cur); |
70 |
return 0; |
71 |
} /* qDelCb */ |
72 |
|
73 |
#if 0 |
74 |
static void keygen (Key *k, unsigned n) |
75 |
{ |
76 |
} |
77 |
#endif |
78 |
|
79 |
|
80 |
|
81 |
/* ************************************************************ |
82 |
*/ |
83 |
|
84 |
int main ( int argc, const char **argv ) |
85 |
{ |
86 |
List arg; |
87 |
List rec; |
88 |
Fld opt; |
89 |
Db *db[2] = {0,0}; |
90 |
char *p; |
91 |
int i; |
92 |
|
93 |
lInit(&rec, "test"); |
94 |
CINIT(&arg); |
95 |
if ( LOG_VERBOSE >= env.log ) |
96 |
SCPY(env.err, env.opt); |
97 |
for ( i=1; i<3 && i<LLEN(env.opt); i++ ) { /* open 1 or 2 dbs */ |
98 |
if ( VPRI(&opt, env.opt->fld+i) ) { |
99 |
if ( !(db[i-1] = dOpen(p = VDUPZ( &opt ))) ) |
100 |
return 1; |
101 |
mFree( p ); |
102 |
} |
103 |
} |
104 |
|
105 |
if ( !VPRI(&opt, env.opt->fld) ) { |
106 |
eRr(0, "help"); |
107 |
return 0; |
108 |
} |
109 |
|
110 |
switch ( opt.val[0] ) { |
111 |
case 'c': |
112 |
if ( VEQZ(&opt, "coll") ) { |
113 |
const Cdx *cdx; |
114 |
FIL_DEFBUF(env.in); |
115 |
if (!db[0] || !(cdx = db[0]->qdx.cdx)) goto dbs1; |
116 |
while ( fGets(&fb) && fb.l ) { |
117 |
Key key; |
118 |
char hex[512], *h = hex; |
119 |
unsigned char *k = key.byt, *e; |
120 |
int r; |
121 |
memset( &key, 0, sizeof(key) ); |
122 |
key.len = 255; |
123 |
r = cEnc(cdx, &key, (unsigned char*)fb.p, fb.l, 0); |
124 |
e = k + key.len; |
125 |
while ( k < e ) { |
126 |
*h++ = b36dig[*k >> 4]; |
127 |
*h++ = b36dig[*k++ & 0xf]; |
128 |
} |
129 |
*h = 0; |
130 |
eRr(0, "cEnc '%.*s' = %d/%d '%s'", fb.l, fb.p, key.len, r, hex); |
131 |
r = cDec(cdx, (unsigned char*)hex, sizeof(hex), &key); |
132 |
eRr(0, "cDec = '%.*s'", r, hex); |
133 |
} |
134 |
} |
135 |
break; |
136 |
case 'p': |
137 |
if ( VEQZ(&opt, "perf") ) { |
138 |
int max; |
139 |
if ( !db[0] ) goto dbs1; |
140 |
max = db[0]->rdx.mid; |
141 |
for (i=1000000; i--;) |
142 |
dRead(lClr(&rec), db[0], 1+((int)rand() % max)); |
143 |
} |
144 |
break; |
145 |
case 'q': |
146 |
if ( VEQZ(&opt, "qdump") ) { |
147 |
QLoop ql; |
148 |
if ( !db[0] ) goto dbs1; |
149 |
memset( &ql, 0, sizeof(ql) ); |
150 |
ql.qcb = qCb; |
151 |
ql.qdx = &db[0]->qdx; |
152 |
qLoop( &ql ); |
153 |
} |
154 |
else if ( VEQZ(&opt, "qload") ) { |
155 |
if ( !db[0] ) goto dbs1; |
156 |
qLoadf(&db[0]->qdx, &env.in); |
157 |
} |
158 |
else if ( VEQZ(&opt, "qchk") ) { |
159 |
QLoop ql; |
160 |
if ( !db[0] ) goto dbs1; |
161 |
memset( &ql, 0, sizeof(ql) ); |
162 |
ql.qcb = 0; |
163 |
ql.qdx = &db[0]->qdx; |
164 |
qLoop( &ql ); |
165 |
} |
166 |
else if ( VEQZ(&opt, "qdel") ) { |
167 |
QLoop ql; |
168 |
if ( !db[0] ) goto dbs1; |
169 |
memset( &ql, 0, sizeof(ql) ); |
170 |
ql.qcb = qDelCb; |
171 |
ql.qdx = &db[0]->qdx; |
172 |
qLoop( &ql ); |
173 |
} |
174 |
else if ( VEQZ(&opt, "qset") ) { |
175 |
Key key; |
176 |
FIL_DEFBUF(env.in); |
177 |
if ( !db[0] ) goto dbs1; |
178 |
memset( &key, 0, sizeof(key) ); |
179 |
key.val.len = db[0]->qdx.vsz; |
180 |
while ( fGets(&fb) && fb.l ) { |
181 |
if ( qMkKeyVal(&db[0]->qdx, &key, fb.p, fb.l) ) { |
182 |
eRr(LOG_VERBOSE, "bad key at line %d offset %d", fb.n, fb.o); |
183 |
continue; |
184 |
} |
185 |
if ( qSet(&db[0]->qdx, &key) ) |
186 |
break; |
187 |
} |
188 |
eRr(LOG_INFO, "did %d lines %d bytes", fb.n, fb.o); |
189 |
} |
190 |
break; |
191 |
case 'r': |
192 |
if ( VEQZ(&opt, "rdump") ) { |
193 |
if ( !db[0] ) goto dbs1; |
194 |
for (i=1; i<=db[0]->rdx.mid; i++) { |
195 |
dRead(&env.out->lst, db[0], i); |
196 |
SEOR(env.out); |
197 |
} |
198 |
} |
199 |
else if ( VEQZ(&opt, "rload") ) { |
200 |
FIL_DEFBUF(env.in); |
201 |
while ( fGetr(&env.out->lst, &fb) ) { |
202 |
if ( db[0] ) |
203 |
dWrite(db[0], env.out->lst.fld, 0); |
204 |
SEOR(env.out); |
205 |
} |
206 |
} |
207 |
break; |
208 |
} |
209 |
|
210 |
return 0; |
211 |
dbs1: |
212 |
eRr(ERR_INVAL, "command %.*s needs a db", opt.len, opt.val); |
213 |
return 1; |
214 |
/* |
215 |
dbs2: |
216 |
eRr(ERR_INVAL, "command %.*s needs 2 dbs", opt.len, opt.val); |
217 |
return 1; |
218 |
*/ |
219 |
} /* main */ |