KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > StoreTest


1 package org.apache.lucene;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.store.Directory;
20 import org.apache.lucene.store.InputStream;
21 import org.apache.lucene.store.OutputStream;
22 import org.apache.lucene.store.FSDirectory;
23 import org.apache.lucene.store.RAMDirectory;
24
25 import java.util.Date JavaDoc;
26 import java.util.Random JavaDoc;
27
28 class StoreTest {
29   public static void main(String JavaDoc[] args) {
30     try {
31       test(1000, true);
32     } catch (Exception JavaDoc e) {
33       System.out.println(" caught a " + e.getClass() +
34              "\n with message: " + e.getMessage());
35     }
36   }
37
38   public static void test(int count, boolean ram)
39        throws Exception JavaDoc {
40     Random JavaDoc gen = new Random JavaDoc(1251971);
41     int i;
42     
43     Date JavaDoc veryStart = new Date JavaDoc();
44     Date JavaDoc start = new Date JavaDoc();
45
46     Directory store;
47     if (ram)
48       store = new RAMDirectory();
49     else
50       store = FSDirectory.getDirectory("test.store", true);
51
52     final int LENGTH_MASK = 0xFFF;
53
54     for (i = 0; i < count; i++) {
55       String JavaDoc name = i + ".dat";
56       int length = gen.nextInt() & LENGTH_MASK;
57       byte b = (byte)(gen.nextInt() & 0x7F);
58       //System.out.println("filling " + name + " with " + length + " of " + b);
59

60       OutputStream file = store.createFile(name);
61
62       for (int j = 0; j < length; j++)
63     file.writeByte(b);
64       
65       file.close();
66     }
67
68     store.close();
69
70     Date JavaDoc end = new Date JavaDoc();
71
72     System.out.print(end.getTime() - start.getTime());
73     System.out.println(" total milliseconds to create");
74
75     gen = new Random JavaDoc(1251971);
76     start = new Date JavaDoc();
77
78     if (!ram)
79       store = FSDirectory.getDirectory("test.store", false);
80
81     for (i = 0; i < count; i++) {
82       String JavaDoc name = i + ".dat";
83       int length = gen.nextInt() & LENGTH_MASK;
84       byte b = (byte)(gen.nextInt() & 0x7F);
85       //System.out.println("reading " + name + " with " + length + " of " + b);
86

87       InputStream file = store.openFile(name);
88
89       if (file.length() != length)
90     throw new Exception JavaDoc("length incorrect");
91
92       for (int j = 0; j < length; j++)
93     if (file.readByte() != b)
94       throw new Exception JavaDoc("contents incorrect");
95
96       file.close();
97     }
98
99     end = new Date JavaDoc();
100
101     System.out.print(end.getTime() - start.getTime());
102     System.out.println(" total milliseconds to read");
103
104     gen = new Random JavaDoc(1251971);
105     start = new Date JavaDoc();
106
107     for (i = 0; i < count; i++) {
108       String JavaDoc name = i + ".dat";
109       //System.out.println("deleting " + name);
110
store.deleteFile(name);
111     }
112
113     end = new Date JavaDoc();
114
115     System.out.print(end.getTime() - start.getTime());
116     System.out.println(" total milliseconds to delete");
117
118     System.out.print(end.getTime() - veryStart.getTime());
119     System.out.println(" total milliseconds");
120
121     store.close();
122   }
123 }
124
Popular Tags