2 openisis - an open implementation of the CDS/ISIS database
3 Version 0.8.x (patchlevel see file Version)
4 Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org
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.
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. See the GNU
14 Lesser General Public License for more details.
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
20 see README for more information
24 $Id: openjsis.c,v 1.17 2003/04/08 00:20:53 kripke Exp $
25 main file of openjsis java native lib.
28 #include <stdlib.h> /* free */
29 #include <memory.h> /* memcpy */
32 #define index stupidgcc
34 java/jdk1.3/include/jni.h:607: warning: declaration of `index' shadows global declaration
35 jobject (JNICALL *GetObjectArrayElement)
36 (JNIEnv *env, jobjectArray array, jsize index);
38 #include "org/openisis/NativeDb.h"
43 /* ************************************************************
46 /* ************************************************************
49 /* ************************************************************
52 static jobjectArray rec2bytes (JNIEnv* env, OpenIsisRec *r )
54 jobjectArray jarr = 0;
63 jarr = (*env)->NewObjectArray( env, 1+r->len,
64 (*env)->FindClass( env, "java/lang/Object" ), 0 );
68 ia = (*env)->NewIntArray( env, 1+r->len );
69 (*env)->SetObjectArrayElement( env, jarr, 0, ia );
72 (*env)->SetIntArrayRegion( env, ia, 0, 1, &ji );
74 for ( i=0; i<r->len; i++ ) {
75 OpenIsisField *f = &r->field[i];
76 ba = (*env)->NewByteArray( env, f->len );
80 (*env)->SetIntArrayRegion( env, ia, i+1, 1, &ji );
81 (*env)->SetByteArrayRegion( env, ba, 0, f->len, (jbyte*)f->val );
82 (*env)->SetObjectArrayElement( env, jarr, i+1, ba );
87 static OpenIsisRec* bytes2rec ( JNIEnv* env, jobjectArray arr ) {
98 /* initialize record */
100 OPENISIS_RSPACE( rec, 8*1024, -1 );
102 len = (*env)->GetArrayLength( env, arr );
106 ia = (*env)->GetObjectArrayElement( env, arr, 0 );
110 (*env)->GetIntArrayRegion( env, ia, 0, 1, &ji );
113 /* strip rowid / type description from length */
115 tags = malloc( len * sizeof( jint ) );
116 (*env)->GetIntArrayRegion( env, ia, 1, len, tags );
118 for ( i = 0; i < len; i++ ) {
119 ba = (*env)->GetObjectArrayElement( env, arr, i + 1 );
120 alen = (*env)->GetArrayLength( env, ba );
121 txt = malloc( alen );
122 memset( txt, 0, alen );
123 (*env)->GetByteArrayRegion( env, ba, 0, alen, (jbyte*)txt );
124 OPENISIS_RADD( rec, tags[ i ], txt, alen, 0 );
134 /* ************************************************************
137 /* ************************************************************
140 JNIEXPORT jshort JNICALL Java_org_openisis_NativeDb_nopen
141 (JNIEnv* env, jclass cls, jstring jdbname, jobjectArray jargs)
144 const char *dbname = 0;
145 const char **argv = 0;
150 dbname = ! jdbname ? 0 :
151 (*env)->GetStringUTFChars( env, jdbname, 0 );
153 argc = (*env)->GetArrayLength( env, jargs );
154 argv = malloc( argc * sizeof(*argv) );
156 for ( i=0; i<argc; i++ )
157 argv[i] = (*env)->GetStringUTFChars( env,
158 (*env)->GetObjectArrayElement( env, jargs, i ), 0 );
160 db = openIsisOpen( dbname, argv, argc );
162 (*env)->ReleaseStringUTFChars( env, jdbname, dbname );
164 for ( i=0; i<argc; i++ )
166 (*env)->ReleaseStringUTFChars( env,
167 (*env)->GetObjectArrayElement( env, jargs, i ), argv[i] );
169 } /* Java_org_openisis_Db_open */
172 JNIEXPORT jobjectArray JNICALL Java_org_openisis_NativeDb_nreadRow
173 (JNIEnv* env, jclass cls, jshort db, jint rowid, jint tag, jbyteArray txt )
176 jobjectArray jarr = 0;
179 r = openIsisReadRow( db, rowid );
181 jsize l = (*env)->GetArrayLength( env, txt );
182 char *search = malloc( l+1 );
183 (*env)->GetByteArrayRegion( env, txt, 0, l, (jbyte*)search );
185 r = openIsisScan( db, rowid, tag, search );
188 jarr = rec2bytes( env, r );
194 } /* Java_org_openisis_Db_readRow */
197 * please leave me in for debugging ;)
198 * static int print ( OpenIsisRec *r )
202 for ( i=0; i<r->len; i++ ) {
203 if ( ! r->field[i].val ) {
204 openIsisSMsg( 1, "%d.?=%d\n", r->rowid, r->field[i].len );
207 openIsisSMsg( 1, "%d.%d=%.*s\n", r->rowid, r->field[i].tag,
208 (int)r->field[i].len, r->field[i].val );
209 if ( r->field[i].len && '^' == *r->field[i].val ) {
210 OpenIsisRec *rf = openIsisReadField( 0, r->field+i );
213 for ( j=0; j<rf->len; j++ )
214 openIsisSMsg( 1, "%d.%d.%c=%.*s\n",
215 r->rowid, r->field[i].tag,
216 (0x60 & (int)rf->field[j].tag ) ?
217 (int)rf->field[j].tag : ' ',
218 (int)rf->field[j].len, rf->field[j].val );
224 return ! r ? -1 : r->rowid;
227 JNIEXPORT jshort JNICALL Java_org_openisis_NativeDb_nwriteRow
228 (JNIEnv* env, jclass cls, jshort db, jobjectArray arr )
236 r = bytes2rec( env, arr );
239 result = openIsisWrite( db, r );
245 JNIEXPORT jshort JNICALL Java_org_openisis_NativeDb_nwriteXRow
246 (JNIEnv* env, jclass cls, jshort db, jobjectArray arr, jobjectArray idx )
253 if ( ! arr || ! idx )
255 r = bytes2rec( env, arr );
256 i = bytes2rec( env, idx );
259 result = openIsisWritex( db, r, i );
267 JNIEXPORT jobjectArray JNICALL Java_org_openisis_NativeDb_nTerms
268 (JNIEnv* env, jclass cls, jshort db, jbyteArray term, jbyteArray start )
271 jobjectArray jarr = 0;
272 jsize lterm = (*env)->GetArrayLength( env, term );
273 char *cterm = malloc( lterm+1 );
274 union { OpenIsisRec r; char buf[4096]; } x;
276 x.r.bytes = sizeof(x);
278 (*env)->GetByteArrayRegion( env, term, 0, lterm, (jbyte*)cterm );
282 jsize lstart = (*env)->GetArrayLength( env, start );
285 cstart = x.buf + sizeof(x.r);
286 (*env)->GetByteArrayRegion( env, start, 0, lstart, (jbyte*)cstart );
287 x.r.field[0].val = cstart;
288 x.r.field[0].len = lstart;
291 r = openIsisTerm( &x.r, db, cterm );
293 jarr = rec2bytes( env, r );
298 } /* Java_org_openisis_Db_Terms */
302 JNIEXPORT jintArray JNICALL Java_org_openisis_NativeDb_nsearch
303 (JNIEnv * env, jclass cls, jshort db, jbyteArray key, jint mode)
312 l = (*env)->GetArrayLength( env, key );
313 search = malloc( l+1 );
314 (*env)->GetByteArrayRegion( env, key, 0, l, (jbyte*)search );
317 openIsisQuery( &set, db, search, mode, 0 );
321 ia = (*env)->NewIntArray( env, set.len );
322 (*env)->SetIntArrayRegion( env, ia, 0, set.len, (jint*)set.id );
324 } /* Java_org_openisis_Db_search */