1 |
/* |
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 |
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. See the GNU |
14 |
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 |
package org.openisis; |
24 |
|
25 |
import java.io.IOException; |
26 |
import java.util.Date; |
27 |
import java.util.Iterator; |
28 |
import java.text.DecimalFormat; |
29 |
|
30 |
|
31 |
/** |
32 |
Test of openisis java binding. |
33 |
<p> |
34 |
$Id: Test.java,v 1.4 2003/04/08 00:20:53 kripke Exp $ |
35 |
@version $Revision: 1.4 $ |
36 |
@author $Author: kripke $ |
37 |
*/ |
38 |
class Test extends Thread { |
39 |
|
40 |
static DecimalFormat mfn4 = new DecimalFormat( "0000" ); |
41 |
|
42 |
public static boolean dumpRow ( Db db, Rec r, int m ) { |
43 |
if ( null == r ) { |
44 |
System.err.println( "could not get row " ); |
45 |
return false; |
46 |
} |
47 |
if ( 1 == m ) { // cds1 test special |
48 |
m = Db.MDL | Db.PS; |
49 |
String r13 = "\n ", s = "\n"; |
50 |
System.out.println( "MFN : "+mfn4.format(r.getRowId()) ); |
51 |
System.out.print( r.v("CONFERENCE : ",null,12,m,0,0,r13,s) ); |
52 |
System.out.print( r.v("TITLE : ",null,24,m,0,0,r13,s) ); |
53 |
System.out.print( r.v("AUTHOR(S) : ",null,14,m,0,0,"; "+r13+" ",s) ); |
54 |
System.out.print( r.v("EDITION : ",null,25,m,0,0,r13,s) ); |
55 |
System.out.print( r.v("IMPRINT : ",null,26,m,0,0,r13,s) ); |
56 |
System.out.print( r.v("COLLATION : ",null,30,m,0,0,r13,s) ); |
57 |
System.out.print( r.v("SERIES : ",null,44,m,0,0,r13,s) ); |
58 |
System.out.print( r.v("NOTES : ",null,50,m,0,0,r13,s) ); |
59 |
System.out.print( r.v("CORP. BODY : ",null,71,m,0,0,r13,s) ); |
60 |
System.out.print( r.v("ADDED CONF : ",null,72,m,0,0,r13,s) ); |
61 |
System.out.print( r.v("OTHER TITLE: ",null,74,m,0,0,r13,s) ); |
62 |
System.out.print( r.v("OTHER LANG : ",null,76,m|'*',0,0,r13,s) ); |
63 |
System.out.print( r.v("KEYWORDS : ",null,69,m,0,0,r13,s) ); |
64 |
System.out.print( "\n\n" ); |
65 |
return true; |
66 |
} |
67 |
System.out.println( "got row "+r.getRowId() ); |
68 |
int l = r.getLen(); |
69 |
for ( int i=0; i<l; i++ ) { |
70 |
Field f = r.getField(i); |
71 |
System.out.println( "<"+f.tag+">"+f.v(m)+"</"+f.tag+">" ); |
72 |
if ( 0 == (Db.MHL & m) && '^' == f.val.charAt(0) ) { |
73 |
Field[] s = f.split(); |
74 |
if ( null == s ) |
75 |
continue; |
76 |
for ( int j=0; j<s.length; j++ ) |
77 |
if ( null != s[j] ) |
78 |
System.out.println( "<"+f.tag+(char)s[j].tag+">" |
79 |
+s[j].v(m) +"</"+f.tag+(char)s[j].tag+">" ); |
80 |
} |
81 |
} |
82 |
return true; |
83 |
} // dumpRow |
84 |
|
85 |
public static boolean dumpRow ( Db db, Rec r ) { |
86 |
return dumpRow( db, r, Db.HTU ); |
87 |
} // dumpRow |
88 |
|
89 |
|
90 |
static String[] term, val; |
91 |
static Db db; |
92 |
static int[][] post; |
93 |
|
94 |
int start; |
95 |
|
96 |
Test ( int s ) { |
97 |
start = s; |
98 |
} |
99 |
|
100 |
/** implementation of runnable for crashtest. */ |
101 |
public void run () { |
102 |
long then = (new Date()).getTime(); |
103 |
for ( int i=start+1; i++!=start; ) { // read recs for term i |
104 |
if ( i >= term.length ) |
105 |
i = 0; |
106 |
// System.err.println( "post "+i ); |
107 |
for ( int j=0; j<post[i].length; j++ ) { |
108 |
int mfn = post[i][j]; |
109 |
Rec r; |
110 |
if ( null == (r = db.readRow( mfn )) ) |
111 |
System.err.println( "no rec "+mfn ); |
112 |
else if ( ! val[mfn].equals( r.getField( 0 ).val ) ) |
113 |
System.err.println( "mismatch on "+mfn ); |
114 |
} |
115 |
} |
116 |
long now = (new Date()).getTime(); |
117 |
System.out.println( "thread " + Thread.currentThread() |
118 |
+ " terminated after " + ((now - then)/1000.) + " seconds" ); |
119 |
} // run |
120 |
|
121 |
public static boolean crash ( String pre ) |
122 |
throws InterruptedException |
123 |
{ |
124 |
// collect terms array |
125 |
int n = 0; |
126 |
for ( Iterator i=db.terms(pre); i.hasNext(); n++ ) |
127 |
i.next(); |
128 |
System.out.println( n + " terms" ); |
129 |
term = new String[n]; |
130 |
post = new int[n][]; |
131 |
n = 0; |
132 |
int p = 0; |
133 |
int max = 0; |
134 |
for ( Iterator i=db.terms(pre); i.hasNext(); n++ ) { |
135 |
post[n] = db.search( term[n] = (String) i.next(), Db.QRY_KEYEQ ); |
136 |
p += post[n].length; |
137 |
for ( int j=0; j<post[n].length; j++ ) |
138 |
if ( max < post[n][j] ) |
139 |
max = post[n][j]; |
140 |
} |
141 |
System.out.println( p + " postings max mfn " + max ); |
142 |
val = new String[max+1]; |
143 |
Rec r; |
144 |
Field f; |
145 |
long then = (new Date()).getTime(); |
146 |
for ( int i=1; i<val.length; i++ ) |
147 |
if ( null == (r = db.readRow( i )) ) |
148 |
System.err.println( "no rec "+i ); |
149 |
else if ( 0 < r.getLen() ) |
150 |
val[i] = r.getField( 0 ).val; |
151 |
long now = (new Date()).getTime(); |
152 |
System.out.println( "sequential read " + max + " rows in " |
153 |
+((now - then)/1000.) + " seconds " |
154 |
+(max * 1000 / (now - then)) + " rows per sec" |
155 |
); |
156 |
|
157 |
int[] nThreads = { 8, 2, 1, 4 }; |
158 |
int[] res = new int[nThreads.length]; |
159 |
|
160 |
int passes = 3; |
161 |
|
162 |
for ( int pass=0; pass<passes; pass++ ) |
163 |
for ( int i=0; i<nThreads.length; i++ ) { |
164 |
Thread[] th = new Thread[ nThreads[i] ]; |
165 |
then = (new Date()).getTime(); |
166 |
for ( int j=0; j<nThreads[i]; j++ ) |
167 |
(th[j] = new Test( j*term.length/nThreads[i] )).start(); |
168 |
System.out.println( "started "+nThreads[i]+" threads" ); |
169 |
for ( int j=0; j<nThreads[i]; j++ ) |
170 |
th[j].join(); |
171 |
now = (new Date()).getTime(); |
172 |
int rps = (int)(nThreads[i] * p * 1000 / (now - then)); |
173 |
System.out.println( "joined "+nThreads[i]+" threads after " |
174 |
+((now - then)/1000.) + " seconds " +rps + " rows per sec" |
175 |
); |
176 |
res[i] += rps; |
177 |
} |
178 |
for ( int i=0; i<nThreads.length; i++ ) |
179 |
System.out.println( nThreads[i]+" threads " |
180 |
+(res[i]/passes) + " rows per sec" ); |
181 |
return true; |
182 |
} // crash |
183 |
|
184 |
|
185 |
public static void main ( String[] args ) |
186 |
throws IOException, InterruptedException |
187 |
{ |
188 |
int m = Db.HTU; |
189 |
int a = 0; // args offset |
190 |
db = Db.open( null, args ); |
191 |
Rec r; |
192 |
// System.err.println( "db = "+db ); |
193 |
|
194 |
while ( a < args.length ) { |
195 |
|
196 |
if ( ! args[a].startsWith("-") ) { // a number ? |
197 |
try { |
198 |
dumpRow( db, db.readRow( Integer.parseInt(args[a]) ), m ); |
199 |
} catch (NumberFormatException e) {} |
200 |
a++; |
201 |
continue; |
202 |
} |
203 |
|
204 |
if ( args[a].startsWith("-m") ) { // -mxx option |
205 |
if ( 2 < args[a].length() ) |
206 |
switch ( args[a].charAt(2) ) { |
207 |
case 'h': m = Db.MHL; break; |
208 |
case 'd': m = Db.MDL; break; |
209 |
case '1': m = 1; /* cds1 impl by dumprow */ break; |
210 |
default: m = 0; |
211 |
} |
212 |
if ( 3 < args[a].length() && 'u' == args[a].charAt(3) ) |
213 |
m |= Db.MXU; |
214 |
a++; |
215 |
continue; |
216 |
} |
217 |
|
218 |
if ( args[a].startsWith("-ht") ) { // -htx option |
219 |
m |= Db.HTU; |
220 |
if ( 3 < args[a].length() ) |
221 |
switch ( args[a].charAt(3) ) { |
222 |
case 'i': m |= Db.HTI; break; |
223 |
case 'a': m |= Db.HTA; break; |
224 |
} |
225 |
a++; |
226 |
continue; |
227 |
} |
228 |
|
229 |
if ( "-dump".equals(args[a]) ) { // full dump |
230 |
for ( int i=1; dumpRow( db, db.readRow(i), m ); i++ ) |
231 |
; |
232 |
a++; |
233 |
continue; |
234 |
} |
235 |
|
236 |
if ( "-write".equals( args[a] ) ) { // read a row, modify and write it, read again and check |
237 |
Rec r1 = db.readRow(1); |
238 |
dumpRow( db, r1, m ); |
239 |
|
240 |
Field[] fs = r1.getFields(); |
241 |
|
242 |
fs[ 0 ] = new Field( fs[ 0 ].tag, fs[ 0 ].val + "TEST" ); |
243 |
|
244 |
Rec r2 = new Rec( r1.getRowId(), fs ); |
245 |
|
246 |
System.err.println( "write result is: " + db.writeRow( r2 ) ); |
247 |
r1 = db.readRow(1); |
248 |
dumpRow( db, r1, m ); |
249 |
} |
250 |
|
251 |
if ( "-writex".equals( args[a] ) ) { // read row, modify it and write w/ index |
252 |
Rec r1 = db.readRow(1); |
253 |
dumpRow( db, r1, m ); |
254 |
|
255 |
Field[] fs = r1.getFields(); |
256 |
|
257 |
// TODO: impl some test here |
258 |
} |
259 |
|
260 |
// all other options, known or not, are assumed to need a param |
261 |
// i.e. 2 args |
262 |
if ( 2+a > args.length ) |
263 |
break; |
264 |
|
265 |
if ( "-terms".equals(args[a]) ) |
266 |
for ( Iterator i=db.terms(args[a+1]); i.hasNext(); ) |
267 |
System.out.println( i.next() ); |
268 |
else if ( "-row".equals(args[a]) ) |
269 |
dumpRow( db, db.readRow( Integer.parseInt(args[a+1]) ), m ); |
270 |
else if ( "-scan".equals(args[a]) ) |
271 |
for ( int i=1; null != (r = db.scanRow(i,-42,args[a+1])); i++ ) { |
272 |
dumpRow( db, r, m ); |
273 |
i = r.getRowId(); |
274 |
} |
275 |
else if ( "-search".equals(args[a]) || "-query".equals(args[a]) ) { |
276 |
String src = args[a+1]; |
277 |
int mode = 0; |
278 |
int[] set = db.search(src, |
279 |
"-search".equals(args[a]) ? Db.QRY_KEYAT : Db.QRY_SIMPLE |
280 |
); |
281 |
System.err.println( set.length + " rows" ); |
282 |
for ( int i=0; i<set.length; i++ ) |
283 |
System.err.print( set[i] + "\t" ); |
284 |
System.err.println( "" ); |
285 |
for ( int i=0; i<set.length; i++ ) |
286 |
dumpRow( db, db.readRow( set[i] ), m ); |
287 |
} |
288 |
else if ( "-crash".equals(args[a]) ) { // crash test |
289 |
crash( args[a+1] ); |
290 |
} |
291 |
|
292 |
|
293 |
a += 2; |
294 |
} // while |
295 |
} // main |
296 |
|
297 |
} // Test |