001: package org.apache.lucene.store.db;
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.util.Date;
021: import java.util.Random;
022: import java.util.Arrays;
023:
024: import java.io.File;
025: import java.io.IOException;
026:
027: import junit.framework.TestCase;
028:
029: import com.sleepycat.db.EnvironmentConfig;
030: import com.sleepycat.db.Environment;
031: import com.sleepycat.db.Transaction;
032: import com.sleepycat.db.Database;
033: import com.sleepycat.db.DatabaseConfig;
034: import com.sleepycat.db.DatabaseType;
035: import com.sleepycat.db.DatabaseException;
036:
037: import org.apache.lucene.store.Directory;
038: import org.apache.lucene.store.IndexInput;
039: import org.apache.lucene.store.IndexOutput;
040:
041: /**
042: * Tests {@link DbDirectory}.
043: *
044: * Adapted from org.apache.lucene.StoreTest with larger files and random bytes.
045: * @author Andi Vajda
046: */
047: public class DbStoreTest extends TestCase {
048: protected File dbHome = new File(System
049: .getProperty("java.io.tmpdir"), "index");
050: protected Environment env;
051: protected Database index, blocks;
052:
053: public void setUp() throws Exception {
054: if (!dbHome.exists())
055: dbHome.mkdir();
056: else {
057: File[] files = dbHome.listFiles();
058:
059: for (int i = 0; i < files.length; i++) {
060: String name = files[i].getName();
061: if (name.startsWith("__") || name.startsWith("log."))
062: files[i].delete();
063: }
064: }
065:
066: EnvironmentConfig envConfig = new EnvironmentConfig();
067: DatabaseConfig dbConfig = new DatabaseConfig();
068:
069: envConfig.setTransactional(true);
070: envConfig.setInitializeCache(true);
071: envConfig.setInitializeLocking(true);
072: envConfig.setInitializeLogging(true);
073: envConfig.setAllowCreate(true);
074: envConfig.setThreaded(true);
075: dbConfig.setAllowCreate(true);
076: dbConfig.setType(DatabaseType.BTREE);
077:
078: env = new Environment(dbHome, envConfig);
079:
080: Transaction txn = null;
081:
082: try {
083: txn = env.beginTransaction(null, null);
084: index = env.openDatabase(txn, "__index__", null, dbConfig);
085: blocks = env
086: .openDatabase(txn, "__blocks__", null, dbConfig);
087: } catch (DatabaseException e) {
088: if (txn != null) {
089: txn.abort();
090: txn = null;
091: }
092: index = null;
093: blocks = null;
094: throw e;
095: } finally {
096: if (txn != null)
097: txn.commit();
098: txn = null;
099: }
100: }
101:
102: public void tearDown() throws Exception {
103: if (index != null)
104: index.close();
105: if (blocks != null)
106: blocks.close();
107: if (env != null)
108: env.close();
109: }
110:
111: public void testBytes() throws Exception {
112: final int count = 250;
113: final int LENGTH_MASK = 0xffff;
114:
115: Random gen = new Random(1251971);
116: int totalLength = 0;
117: int duration;
118: Date end;
119:
120: Date veryStart = new Date();
121: Date start = new Date();
122: Transaction txn = null;
123: Directory store = null;
124:
125: System.out.println("Writing files byte by byte");
126:
127: try {
128: txn = env.beginTransaction(null, null);
129: store = new DbDirectory(txn, index, blocks);
130:
131: for (int i = 0; i < count; i++) {
132: String name = i + ".dat";
133: int length = gen.nextInt() & LENGTH_MASK;
134: IndexOutput file = store.createOutput(name);
135:
136: totalLength += length;
137:
138: for (int j = 0; j < length; j++) {
139: byte b = (byte) (gen.nextInt() & 0x7F);
140: file.writeByte(b);
141: }
142:
143: file.close();
144: }
145: } catch (IOException e) {
146: txn.abort();
147: txn = null;
148: throw e;
149: } catch (DatabaseException e) {
150: if (txn != null) {
151: txn.abort();
152: txn = null;
153: }
154: throw e;
155: } finally {
156: if (txn != null)
157: txn.commit();
158:
159: store.close();
160: }
161:
162: end = new Date();
163:
164: duration = (int) (end.getTime() - start.getTime());
165: System.out.print(duration);
166: System.out.print(" total milliseconds to create, ");
167: System.out.print(totalLength / duration);
168: System.out.println(" kb/s");
169:
170: try {
171: txn = env.beginTransaction(null, null);
172: store = new DbDirectory(txn, index, blocks);
173:
174: gen = new Random(1251971);
175: start = new Date();
176:
177: for (int i = 0; i < count; i++) {
178: String name = i + ".dat";
179: int length = gen.nextInt() & LENGTH_MASK;
180: IndexInput file = store.openInput(name);
181:
182: if (file.length() != length)
183: throw new Exception("length incorrect");
184:
185: for (int j = 0; j < length; j++) {
186: byte b = (byte) (gen.nextInt() & 0x7F);
187:
188: if (file.readByte() != b)
189: throw new Exception("contents incorrect");
190: }
191:
192: file.close();
193: }
194: } catch (IOException e) {
195: txn.abort();
196: txn = null;
197: throw e;
198: } catch (DatabaseException e) {
199: if (txn != null) {
200: txn.abort();
201: txn = null;
202: }
203: throw e;
204: } finally {
205: if (txn != null)
206: txn.commit();
207:
208: store.close();
209: }
210:
211: end = new Date();
212:
213: duration = (int) (end.getTime() - start.getTime());
214: System.out.print(duration);
215: System.out.print(" total milliseconds to read, ");
216: System.out.print(totalLength / duration);
217: System.out.println(" kb/s");
218:
219: try {
220: txn = env.beginTransaction(null, null);
221: store = new DbDirectory(txn, index, blocks);
222:
223: gen = new Random(1251971);
224: start = new Date();
225:
226: for (int i = 0; i < count; i++) {
227: String name = i + ".dat";
228: store.deleteFile(name);
229: }
230: } catch (IOException e) {
231: txn.abort();
232: txn = null;
233: throw e;
234: } catch (DatabaseException e) {
235: if (txn != null) {
236: txn.abort();
237: txn = null;
238: }
239: throw e;
240: } finally {
241: if (txn != null)
242: txn.commit();
243:
244: store.close();
245: }
246:
247: end = new Date();
248:
249: System.out.print(end.getTime() - start.getTime());
250: System.out.println(" total milliseconds to delete");
251:
252: System.out.print(end.getTime() - veryStart.getTime());
253: System.out.println(" total milliseconds");
254: }
255:
256: public void testArrays() throws Exception {
257: final int count = 250;
258: final int LENGTH_MASK = 0xffff;
259:
260: Random gen = new Random(1251971);
261: int totalLength = 0;
262: int duration;
263: Date end;
264:
265: Date veryStart = new Date();
266: Date start = new Date();
267: Transaction txn = null;
268: Directory store = null;
269:
270: System.out.println("Writing files as one byte array");
271:
272: try {
273: txn = env.beginTransaction(null, null);
274: store = new DbDirectory(txn, index, blocks);
275:
276: for (int i = 0; i < count; i++) {
277: String name = i + ".dat";
278: int length = gen.nextInt() & LENGTH_MASK;
279: IndexOutput file = store.createOutput(name);
280: byte[] data = new byte[length];
281:
282: totalLength += length;
283: gen.nextBytes(data);
284: file.writeBytes(data, length);
285:
286: file.close();
287: }
288: } catch (IOException e) {
289: txn.abort();
290: txn = null;
291: throw e;
292: } catch (DatabaseException e) {
293: if (txn != null) {
294: txn.abort();
295: txn = null;
296: }
297: throw e;
298: } finally {
299: if (txn != null)
300: txn.commit();
301:
302: store.close();
303: }
304:
305: end = new Date();
306:
307: duration = (int) (end.getTime() - start.getTime());
308: System.out.print(duration);
309: System.out.print(" total milliseconds to create, ");
310: System.out.print(totalLength / duration);
311: System.out.println(" kb/s");
312:
313: try {
314: txn = env.beginTransaction(null, null);
315: store = new DbDirectory(txn, index, blocks);
316:
317: gen = new Random(1251971);
318: start = new Date();
319:
320: for (int i = 0; i < count; i++) {
321: String name = i + ".dat";
322: int length = gen.nextInt() & LENGTH_MASK;
323: IndexInput file = store.openInput(name);
324:
325: if (file.length() != length)
326: throw new Exception("length incorrect");
327:
328: byte[] data = new byte[length];
329: byte[] read = new byte[length];
330: gen.nextBytes(data);
331: file.readBytes(read, 0, length);
332:
333: if (!Arrays.equals(data, read))
334: throw new Exception("contents incorrect");
335:
336: file.close();
337: }
338: } catch (IOException e) {
339: txn.abort();
340: txn = null;
341: throw e;
342: } catch (DatabaseException e) {
343: if (txn != null) {
344: txn.abort();
345: txn = null;
346: }
347: throw e;
348: } finally {
349: if (txn != null)
350: txn.commit();
351:
352: store.close();
353: }
354:
355: end = new Date();
356:
357: duration = (int) (end.getTime() - start.getTime());
358: System.out.print(duration);
359: System.out.print(" total milliseconds to read, ");
360: System.out.print(totalLength / duration);
361: System.out.println(" kb/s");
362:
363: try {
364: txn = env.beginTransaction(null, null);
365: store = new DbDirectory(txn, index, blocks);
366:
367: gen = new Random(1251971);
368: start = new Date();
369:
370: for (int i = 0; i < count; i++) {
371: String name = i + ".dat";
372: store.deleteFile(name);
373: }
374: } catch (IOException e) {
375: txn.abort();
376: txn = null;
377: throw e;
378: } catch (DatabaseException e) {
379: if (txn != null) {
380: txn.abort();
381: txn = null;
382: }
383: throw e;
384: } finally {
385: if (txn != null)
386: txn.commit();
387:
388: store.close();
389: }
390:
391: end = new Date();
392:
393: System.out.print(end.getTime() - start.getTime());
394: System.out.println(" total milliseconds to delete");
395:
396: System.out.print(end.getTime() - veryStart.getTime());
397: System.out.println(" total milliseconds");
398: }
399: }
|