001: package org.apache.lucene.store.je;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Arrays;
023: import java.util.Date;
024: import java.util.Random;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.lucene.store.Directory;
029: import org.apache.lucene.store.IndexInput;
030: import org.apache.lucene.store.IndexOutput;
031:
032: import com.sleepycat.je.Cursor;
033: import com.sleepycat.je.Database;
034: import com.sleepycat.je.DatabaseConfig;
035: import com.sleepycat.je.DatabaseEntry;
036: import com.sleepycat.je.DatabaseException;
037: import com.sleepycat.je.Environment;
038: import com.sleepycat.je.EnvironmentConfig;
039: import com.sleepycat.je.LockMode;
040: import com.sleepycat.je.OperationStatus;
041: import com.sleepycat.je.Transaction;
042:
043: /**
044: * Tests {@link JEDirectory}.
045: *
046: * Adapted from Andi Vajda's org.apache.lucene.db.DbStoreTest.
047: *
048: * @author Aaron Donovan
049: */
050: public class JEStoreTest extends TestCase {
051: protected File dbHome = new File(System
052: .getProperty("java.io.tmpdir"), "index");
053:
054: protected Environment env;
055:
056: protected Database index, blocks;
057:
058: public void setUp() throws Exception {
059:
060: if (!dbHome.exists())
061: dbHome.mkdir();
062: else {
063: File[] files = dbHome.listFiles();
064:
065: for (int i = 0; i < files.length; i++) {
066: String name = files[i].getName();
067: if (name.endsWith("jdb") || name.equals("je.lck"))
068: files[i].delete();
069: }
070: }
071:
072: EnvironmentConfig envConfig = new EnvironmentConfig();
073: DatabaseConfig dbConfig = new DatabaseConfig();
074:
075: envConfig.setTransactional(true);
076: envConfig.setAllowCreate(true);
077: dbConfig.setAllowCreate(true);
078: dbConfig.setTransactional(true);
079:
080: env = new Environment(dbHome, envConfig);
081:
082: Transaction txn = null;
083:
084: try {
085: txn = env.beginTransaction(null, null);
086: index = env.openDatabase(txn, "__index__", dbConfig);
087: blocks = env.openDatabase(txn, "__blocks__", dbConfig);
088: } catch (DatabaseException e) {
089: if (txn != null) {
090: txn.abort();
091: txn = null;
092: }
093: index = null;
094: blocks = null;
095: throw e;
096: } finally {
097: if (txn != null)
098: txn.commit();
099: txn = null;
100: }
101: }
102:
103: public void tearDown() throws Exception {
104:
105: if (index != null)
106: index.close();
107: if (blocks != null)
108: blocks.close();
109: if (env != null)
110: env.close();
111: }
112:
113: public void tesBytes() throws Exception {
114: final int count = 250;
115: final int LENGTH_MASK = 0xffff;
116:
117: Random gen = new Random(1251971);
118: int totalLength = 0;
119: int duration;
120: Date end;
121:
122: Date veryStart = new Date();
123: Date start = new Date();
124: Transaction txn = null;
125: Directory store = null;
126:
127: System.out.println("Writing files byte by byte");
128:
129: try {
130: txn = env.beginTransaction(null, null);
131: store = new JEDirectory(txn, index, blocks);
132:
133: for (int i = 0; i < count; i++) {
134: String name = i + ".dat";
135: int length = gen.nextInt() & LENGTH_MASK;
136: IndexOutput file = store.createOutput(name);
137:
138: totalLength += length;
139:
140: for (int j = 0; j < length; j++) {
141: byte b = (byte) (gen.nextInt() & 0x7F);
142: file.writeByte(b);
143: }
144:
145: file.close();
146: }
147: } catch (IOException e) {
148: txn.abort();
149: txn = null;
150: throw e;
151: } catch (DatabaseException e) {
152: if (txn != null) {
153: txn.abort();
154: txn = null;
155: }
156: throw e;
157: } finally {
158: if (txn != null)
159: txn.commit();
160:
161: store.close();
162: }
163:
164: end = new Date();
165:
166: duration = (int) (end.getTime() - start.getTime());
167: System.out.print(duration);
168: System.out.print(" total milliseconds to create, ");
169: System.out.print(totalLength / duration);
170: System.out.println(" kb/s");
171:
172: try {
173: txn = env.beginTransaction(null, null);
174: store = new JEDirectory(txn, index, blocks);
175:
176: gen = new Random(1251971);
177: start = new Date();
178:
179: for (int i = 0; i < count; i++) {
180: String name = i + ".dat";
181: int length = gen.nextInt() & LENGTH_MASK;
182: IndexInput file = store.openInput(name);
183:
184: if (file.length() != length)
185: throw new Exception("length incorrect");
186:
187: for (int j = 0; j < length; j++) {
188: byte b = (byte) (gen.nextInt() & 0x7F);
189:
190: if (file.readByte() != b)
191: throw new Exception("contents incorrect");
192: }
193:
194: file.close();
195: }
196: } catch (IOException e) {
197: txn.abort();
198: txn = null;
199: throw e;
200: } catch (DatabaseException e) {
201: if (txn != null) {
202: txn.abort();
203: txn = null;
204: }
205: throw e;
206: } finally {
207: if (txn != null)
208: txn.commit();
209:
210: store.close();
211: }
212:
213: end = new Date();
214:
215: duration = (int) (end.getTime() - start.getTime());
216: System.out.print(duration);
217: System.out.print(" total milliseconds to read, ");
218: System.out.print(totalLength / duration);
219: System.out.println(" kb/s");
220:
221: try {
222: txn = env.beginTransaction(null, null);
223: store = new JEDirectory(txn, index, blocks);
224:
225: gen = new Random(1251971);
226: start = new Date();
227:
228: for (int i = 0; i < count; i++) {
229: String name = i + ".dat";
230: store.deleteFile(name);
231: }
232: } catch (IOException e) {
233: txn.abort();
234: txn = null;
235: throw e;
236: } catch (DatabaseException e) {
237: if (txn != null) {
238: txn.abort();
239: txn = null;
240: }
241: throw e;
242: } finally {
243: if (txn != null)
244: txn.commit();
245:
246: store.close();
247: }
248:
249: end = new Date();
250:
251: System.out.print(end.getTime() - start.getTime());
252: System.out.println(" total milliseconds to delete");
253:
254: System.out.print(end.getTime() - veryStart.getTime());
255: System.out.println(" total milliseconds");
256: }
257:
258: public void testDelete() throws Exception {
259: final int count = 250;
260: final int LENGTH_MASK = 0xffff;
261:
262: Random gen = new Random(1251971);
263: int totalLength = 0;
264: int duration;
265: Date end;
266:
267: Date veryStart = new Date();
268: Date start = new Date();
269: Transaction txn = null;
270: Directory store = null;
271:
272: System.out.println("Writing files byte by byte");
273:
274: try {
275: txn = env.beginTransaction(null, null);
276: store = new JEDirectory(txn, index, blocks);
277:
278: for (int i = 0; i < count; i++) {
279: String name = i + ".dat";
280: int length = gen.nextInt() & LENGTH_MASK;
281: IndexOutput file = store.createOutput(name);
282:
283: totalLength += length;
284:
285: for (int j = 0; j < length; j++) {
286: byte b = (byte) (gen.nextInt() & 0x7F);
287: file.writeByte(b);
288: }
289:
290: file.close();
291: }
292: } catch (IOException e) {
293: txn.abort();
294: txn = null;
295: throw e;
296: } catch (DatabaseException e) {
297: if (txn != null) {
298: txn.abort();
299: txn = null;
300: }
301: throw e;
302: } finally {
303: if (txn != null)
304: txn.commit();
305:
306: store.close();
307: }
308:
309: end = new Date();
310:
311: duration = (int) (end.getTime() - start.getTime());
312: System.out.print(duration);
313: System.out.print(" total milliseconds to read, ");
314: System.out.print(totalLength / duration);
315: System.out.println(" kb/s");
316:
317: try {
318: txn = env.beginTransaction(null, null);
319: store = new JEDirectory(txn, index, blocks);
320:
321: gen = new Random(1251971);
322: start = new Date();
323:
324: for (int i = 0; i < count; i++) {
325: if (i % 2 == 0) {
326: String name = i + ".dat";
327: store.deleteFile(name);
328: }
329: }
330: } catch (IOException e) {
331: txn.abort();
332: txn = null;
333: throw e;
334: } catch (DatabaseException e) {
335: if (txn != null) {
336: txn.abort();
337: txn = null;
338: }
339: throw e;
340: } finally {
341: if (txn != null)
342: txn.commit();
343:
344: store.close();
345: }
346:
347: end = new Date();
348:
349: System.out.print(end.getTime() - start.getTime());
350: System.out.println(" total milliseconds to delete even files");
351:
352: duration = (int) (end.getTime() - start.getTime());
353: System.out.print(duration);
354: System.out.print(" total milliseconds to create, ");
355: System.out.print(totalLength / duration);
356: System.out.println(" kb/s");
357:
358: try {
359: txn = env.beginTransaction(null, null);
360: store = new JEDirectory(txn, index, blocks);
361:
362: gen = new Random(1251971);
363: start = new Date();
364:
365: for (int i = 0; i < count; i++) {
366: int length = gen.nextInt() & LENGTH_MASK;
367:
368: if (i % 2 != 0) {
369: String name = i + ".dat";
370: IndexInput file = store.openInput(name);
371: if (file.length() != length)
372: throw new Exception("length incorrect");
373:
374: for (int j = 0; j < length; j++) {
375: byte b = (byte) (gen.nextInt() & 0x7F);
376:
377: if (file.readByte() != b)
378: throw new Exception("contents incorrect");
379: }
380:
381: file.close();
382: } else {
383: for (int j = 0; j < length; j++) {
384: gen.nextInt();
385: }
386: }
387: }
388: } catch (IOException e) {
389: txn.abort();
390: txn = null;
391: throw e;
392: } catch (DatabaseException e) {
393: if (txn != null) {
394: txn.abort();
395: txn = null;
396: }
397: throw e;
398: } finally {
399: if (txn != null)
400: txn.commit();
401:
402: store.close();
403: }
404:
405: end = new Date();
406:
407: duration = (int) (end.getTime() - start.getTime());
408: System.out.print(duration);
409: System.out.print(" total milliseconds to read, ");
410: System.out.print(totalLength / duration);
411: System.out.println(" kb/s");
412:
413: try {
414: txn = env.beginTransaction(null, null);
415: store = new JEDirectory(txn, index, blocks);
416:
417: gen = new Random(1251971);
418: start = new Date();
419:
420: for (int i = 0; i < count; i++) {
421: if (i % 2 != 0) {
422: String name = i + ".dat";
423: store.deleteFile(name);
424: }
425: }
426:
427: } catch (IOException e) {
428: txn.abort();
429: txn = null;
430: throw e;
431: } catch (DatabaseException e) {
432: if (txn != null) {
433: txn.abort();
434: txn = null;
435: }
436: throw e;
437: } finally {
438: if (txn != null)
439: txn.commit();
440:
441: store.close();
442: }
443:
444: end = new Date();
445:
446: System.out.print(end.getTime() - start.getTime());
447: System.out.println(" total milliseconds to delete");
448:
449: System.out.print(end.getTime() - veryStart.getTime());
450: System.out.println(" total milliseconds");
451:
452: Cursor cursor = null;
453: try {
454: cursor = index.openCursor(null, null);
455:
456: DatabaseEntry foundKey = new DatabaseEntry();
457: DatabaseEntry foundData = new DatabaseEntry();
458:
459: if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
460: fail("index database is not empty");
461: }
462: } catch (DatabaseException e) {
463: throw e;
464: } finally {
465: if (cursor != null)
466: cursor.close();
467: }
468:
469: cursor = null;
470: try {
471: cursor = blocks.openCursor(null, null);
472:
473: DatabaseEntry foundKey = new DatabaseEntry();
474: DatabaseEntry foundData = new DatabaseEntry();
475:
476: if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
477: fail("blocks database is not empty");
478: }
479: } catch (DatabaseException e) {
480: throw e;
481: } finally {
482: if (cursor != null)
483: cursor.close();
484: }
485: }
486:
487: public void tesArrays() throws Exception {
488: final int count = 250;
489: final int LENGTH_MASK = 0xffff;
490:
491: Random gen = new Random(1251971);
492: int totalLength = 0;
493: int duration;
494: Date end;
495:
496: Date veryStart = new Date();
497: Date start = new Date();
498: Transaction txn = null;
499: Directory store = null;
500:
501: System.out.println("Writing files as one byte array");
502:
503: try {
504: txn = env.beginTransaction(null, null);
505: store = new JEDirectory(txn, index, blocks);
506:
507: for (int i = 0; i < count; i++) {
508: String name = i + ".dat";
509: int length = gen.nextInt() & LENGTH_MASK;
510: IndexOutput file = store.createOutput(name);
511: byte[] data = new byte[length];
512:
513: totalLength += length;
514: gen.nextBytes(data);
515: file.writeBytes(data, length);
516:
517: file.close();
518: }
519: } catch (IOException e) {
520: txn.abort();
521: txn = null;
522: throw e;
523: } catch (DatabaseException e) {
524: if (txn != null) {
525: txn.abort();
526: txn = null;
527: }
528: throw e;
529: } finally {
530: if (txn != null)
531: txn.commit();
532:
533: store.close();
534: }
535:
536: end = new Date();
537:
538: duration = (int) (end.getTime() - start.getTime());
539: System.out.print(duration);
540: System.out.print(" total milliseconds to create, ");
541: System.out.print(totalLength / duration);
542: System.out.println(" kb/s");
543:
544: try {
545: txn = env.beginTransaction(null, null);
546: store = new JEDirectory(txn, index, blocks);
547:
548: gen = new Random(1251971);
549: start = new Date();
550:
551: for (int i = 0; i < count; i++) {
552: String name = i + ".dat";
553: int length = gen.nextInt() & LENGTH_MASK;
554: IndexInput file = store.openInput(name);
555:
556: if (file.length() != length)
557: throw new Exception("length incorrect");
558:
559: byte[] data = new byte[length];
560: byte[] read = new byte[length];
561: gen.nextBytes(data);
562: file.readBytes(read, 0, length);
563:
564: if (!Arrays.equals(data, read))
565: throw new Exception("contents incorrect");
566:
567: file.close();
568: }
569: } catch (IOException e) {
570: txn.abort();
571: txn = null;
572: throw e;
573: } catch (DatabaseException e) {
574: if (txn != null) {
575: txn.abort();
576: txn = null;
577: }
578: throw e;
579: } finally {
580: if (txn != null)
581: txn.commit();
582:
583: store.close();
584: }
585:
586: end = new Date();
587:
588: duration = (int) (end.getTime() - start.getTime());
589: System.out.print(duration);
590: System.out.print(" total milliseconds to read, ");
591: System.out.print(totalLength / duration);
592: System.out.println(" kb/s");
593:
594: try {
595: txn = env.beginTransaction(null, null);
596: store = new JEDirectory(txn, index, blocks);
597:
598: gen = new Random(1251971);
599: start = new Date();
600: for (int i = 0; i < count; i++) {
601: String name = i + ".dat";
602: store.deleteFile(name);
603: }
604:
605: } catch (IOException e) {
606: txn.abort();
607: txn = null;
608: throw e;
609: } catch (DatabaseException e) {
610: if (txn != null) {
611: txn.abort();
612: txn = null;
613: }
614: throw e;
615: } finally {
616: if (txn != null)
617: txn.commit();
618:
619: store.close();
620: }
621:
622: end = new Date();
623:
624: System.out.print(end.getTime() - start.getTime());
625: System.out.println(" total milliseconds to delete");
626:
627: System.out.print(end.getTime() - veryStart.getTime());
628: System.out.println(" total milliseconds");
629: }
630: }
|