File: jdepot.c

package info (click to toggle)
qdbm 1.8.78-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,076 kB
  • sloc: ansic: 31,454; cpp: 3,623; perl: 2,167; java: 2,079; ruby: 1,690; makefile: 1,315; javascript: 627; sh: 396
file content (405 lines) | stat: -rw-r--r-- 10,539 bytes parent folder | download | duplicates (11)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*************************************************************************************************
 * Implementation of Depot for Java
 *                                                      Copyright (C) 2000-2006 Mikio Hirabayashi
 * This file is part of QDBM, Quick Database Manager.
 * QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License or any later version.  QDBM is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 * You should have received a copy of the GNU Lesser General Public License along with QDBM; if
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA.
 *************************************************************************************************/


#include "qdbm_Depot.h"
#include <depot.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#define MAXOPEN 1024


DEPOT *dptable[MAXOPEN];

static int getnewindex(void);
static int checkdup(const char *name);
static int getdpomode(jint omode);
static int getdpdmode(jint dmode);



/*************************************************************************************************
 * public objects
 *************************************************************************************************/


JNIEXPORT void JNICALL
Java_qdbm_Depot_dpinit(JNIEnv *env, jclass myclass)
{
  int i;
  for(i = 0; i < MAXOPEN; i++){
    dptable[i] = NULL;
  }
}


JNIEXPORT jstring JNICALL
Java_qdbm_Depot_dpversion(JNIEnv *env, jclass myclass)
{
  return (*env)->NewStringUTF(env, dpversion);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpecode(JNIEnv *env, jclass myclass){
  switch(dpecode){
  case DP_ENOERR: return qdbm_Depot_ENOERR;
  case DP_EFATAL: return qdbm_Depot_EFATAL;
  case DP_EMODE: return qdbm_Depot_EMODE;
  case DP_EBROKEN: return qdbm_Depot_EBROKEN;
  case DP_EKEEP: return qdbm_Depot_EKEEP;
  case DP_ENOITEM: return qdbm_Depot_ENOITEM;
  case DP_EALLOC: return qdbm_Depot_EALLOC;
  case DP_EMAP: return qdbm_Depot_EMAP;
  case DP_EOPEN: return qdbm_Depot_EOPEN;
  case DP_ECLOSE: return qdbm_Depot_ECLOSE;
  case DP_ETRUNC: return qdbm_Depot_ETRUNC;
  case DP_ESYNC: return qdbm_Depot_ESYNC;
  case DP_ESTAT: return qdbm_Depot_ESTAT;
  case DP_ESEEK: return qdbm_Depot_ESEEK;
  case DP_EREAD: return qdbm_Depot_EREAD;
  case DP_EWRITE: return qdbm_Depot_EWRITE;
  case DP_ELOCK: return qdbm_Depot_ELOCK;
  case DP_EUNLINK: return qdbm_Depot_EUNLINK;
  case DP_EMKDIR: return qdbm_Depot_EMKDIR;
  case DP_ERMDIR: return qdbm_Depot_ERMDIR;
  case DP_EMISC: return qdbm_Depot_EMISC;
  }
  return -1;
}


JNIEXPORT jstring JNICALL
Java_qdbm_Depot_dperrmsg(JNIEnv *env, jclass myclass, jint ecode)
{
  return (*env)->NewStringUTF(env, dperrmsg(ecode));
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpopen(JNIEnv *env, jclass myclass, jstring name, jint omode, jint bnum)
{
  DEPOT *depot;
  const char *tname;
  jboolean ic;
  int index;
  if((index = getnewindex()) == -1) return -1;
  tname = (*env)->GetStringUTFChars(env, name, &ic);
  if(checkdup(tname) == -1){
    if(ic == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
    dpecode = DP_EMISC;
    return -1;
  }
  depot = dpopen(tname, getdpomode(omode), bnum);
  if(ic == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
  if(!depot) return -1;
  dptable[index] = depot;
  return index;
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpclose(JNIEnv *env, jclass myclass, jint index)
{
  DEPOT *depot;
  depot = dptable[index];
  dptable[index] = NULL;
  return dpclose(depot);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpput(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz,
                      jbyteArray val, jint vsiz, jint dmode)
{
  jbyte *kbuf, *vbuf;
  jboolean ick, icv;
  int rv;
  kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  vbuf = (*env)->GetByteArrayElements(env, val, &icv);
  rv = dpput(dptable[index], (char *)kbuf, ksiz, (char *)vbuf, vsiz, getdpdmode(dmode));
  if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  if(icv == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, val, vbuf, JNI_ABORT);
  return rv;
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpout(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz)
{
  jbyte *kbuf;
  jboolean ick;
  int rv;
  kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  rv = dpout(dptable[index], (char *)kbuf, ksiz);
  if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  return rv;
}


JNIEXPORT jbyteArray JNICALL
Java_qdbm_Depot_dpget(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz,
                      jint start, jint max)
{
  jbyte *kbuf;
  jboolean ick;
  char *val;
  int vsiz;
  jbyteArray vbuf;
  kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  val = dpget(dptable[index], (char *)kbuf, ksiz, start, max, &vsiz);
  if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  if(val){
    vbuf = (*env)->NewByteArray(env, vsiz);
    (*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
    free(val);
  } else {
    vbuf = NULL;
  }
  return vbuf;
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpvsiz(JNIEnv *env, jclass myclass, jint index, jbyteArray key, jint ksiz)
{
  jbyte *kbuf;
  jboolean ick;
  int rv;
  kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  rv = dpvsiz(dptable[index], (char *)kbuf, ksiz);
  if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  return rv;
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpiterinit(JNIEnv *env, jclass myclass, jint index)
{
  return dpiterinit(dptable[index]);
}


JNIEXPORT jbyteArray JNICALL
Java_qdbm_Depot_dpiternext(JNIEnv *env, jclass myclass, jint index)
{
  char *val;
  int vsiz;
  jbyteArray vbuf;
  val = dpiternext(dptable[index], &vsiz);
  if(val){
    vbuf = (*env)->NewByteArray(env, vsiz);
    (*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
    free(val);
  } else {
    vbuf = NULL;
  }
  return vbuf;
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpsetalign(JNIEnv *env, jclass myclass, jint index, jint align)
{
  return dpsetalign(dptable[index], align);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpsetfbpsiz(JNIEnv *env, jclass myclass, jint index, jint size)
{
  return dpsetfbpsiz(dptable[index], size);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpsync(JNIEnv *env, jclass myclass, jint index)
{
  return dpsync(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpoptimize(JNIEnv *env, jclass myclass, jint index, jint bnum)
{
  return dpoptimize(dptable[index], bnum);
}


JNIEXPORT jstring JNICALL
Java_qdbm_Depot_dpname(JNIEnv *env, jclass myclass, jint index)
{
  char *name;
  jstring nbuf;
  name = dpname(dptable[index]);
  if(name){
    nbuf = (*env)->NewStringUTF(env, name);
    free(name);
  } else {
    nbuf = NULL;
  }
  return nbuf;
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpfsiz(JNIEnv *env, jclass myclass, jint index)
{
  return dpfsiz(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpbnum(JNIEnv *env, jclass myclass, jint index)
{
  return dpbnum(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpbusenum(JNIEnv *env, jclass myclass, jint index)
{
  return dpbusenum(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dprnum(JNIEnv *env, jclass myclass, jint index)
{
  return dprnum(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpwritable(JNIEnv *env, jclass myclass, jint index)
{
  return dpwritable(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpfatalerror(JNIEnv *env, jclass myclass, jint index)
{
  return dpfatalerror(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpinode(JNIEnv *env, jclass myclass, jint index){
  return dpinode(dptable[index]);
}


JNIEXPORT jlong JNICALL
Java_qdbm_Depot_dpmtime(JNIEnv *env, jclass myclass, jint index){
  return dpmtime(dptable[index]);
}


JNIEXPORT jint JNICALL
Java_qdbm_Depot_dpremove(JNIEnv *env, jclass myclass, jstring name)
{
  const char *tname;
  jboolean ic;
  int rv;
  tname = (*env)->GetStringUTFChars(env, name, &ic);
  rv = dpremove(tname);
  if(ic == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
  return rv;
}


JNIEXPORT jbyteArray JNICALL
Java_qdbm_Depot_dpsnaffle(JNIEnv *env, jclass myclass, jstring name, jbyteArray key, jint ksiz)
{
  jbyte *kbuf;
  jboolean icn, ick;
  const char *tname;
  char *val;
  int vsiz;
  jbyteArray vbuf;
  tname = (*env)->GetStringUTFChars(env, name, &icn);
  kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  val = dpsnaffle(tname, (char *)kbuf, ksiz, &vsiz);
  if(ick == JNI_TRUE) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  if(icn == JNI_TRUE) (*env)->ReleaseStringUTFChars(env, name, tname);
  if(val){
    vbuf = (*env)->NewByteArray(env, vsiz);
    (*env)->SetByteArrayRegion(env, vbuf, 0, vsiz, (jbyte *)val);
    free(val);
  } else {
    vbuf = NULL;
  }
  return vbuf;
}



/*************************************************************************************************
 * private objects
 *************************************************************************************************/


static int getnewindex(void){
  int i;
  for(i = 0; i < MAXOPEN; i++){
    if(dptable[i] == NULL) return i;
  }
  return -1;
}


static int checkdup(const char *name){
  struct stat sbuf;
  int i, inode;
  if(stat(name, &sbuf) == -1) return 0;
  inode = sbuf.st_ino;
  for(i = 0; i < MAXOPEN; i++){
    if(dptable[i] != NULL && dpinode(dptable[i]) == inode) return -1;
  }
  return 0;
}


static int getdpomode(jint omode){
  int dpomode;
  dpomode = DP_OREADER;
  if(omode & qdbm_Depot_OWRITER){
    dpomode = DP_OWRITER;
    if(omode & qdbm_Depot_OCREAT) dpomode |= DP_OCREAT;
    if(omode & qdbm_Depot_OTRUNC) dpomode |= DP_OTRUNC;
  }
  if(omode & qdbm_Depot_ONOLCK) dpomode |= DP_ONOLCK;
  if(omode & qdbm_Depot_OLCKNB) dpomode |= DP_OLCKNB;
  if(omode & qdbm_Depot_OSPARSE) dpomode |= DP_OSPARSE;
  return dpomode;
}


static int getdpdmode(jint dmode){
  switch(dmode){
  case qdbm_Depot_DOVER: return DP_DOVER;
  case qdbm_Depot_DKEEP: return DP_DKEEP;
  case qdbm_Depot_DCAT: return DP_DCAT;
  }
  return -1;
}



/* END OF FILE */