1 package net.obsearch.storage;
2
3 import net.obsearch.utils.bytes.ByteConversion;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Random;
8 import org.apache.log4j.Logger;
9
10 import junit.framework.TestCase;
11
12 import java.nio.ByteBuffer;
13
14 import net.obsearch.exception.OBStorageException;
15 import net.obsearch.exception.OBException;
16 import net.obsearch.storage.TupleDouble;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class StorageValidationDouble extends TestCase {
36
37 private static final transient Logger logger = Logger
38 .getLogger(StorageValidationDouble.class);
39
40
41
42
43
44 public static final int NUM_OF_ITEMS = 1000;
45
46 public static final int STORAGE_SIZE = 8;
47
48
49
50
51
52
53
54 public static void validate(OBStoreDouble storage) throws OBStorageException, OBException{
55 HashMap<Double, byte[]> testData = new HashMap<Double, byte[]>();
56 Random x = new Random();
57 int i = 0;
58 double min = Double.MAX_VALUE;
59 double max = Double.MIN_VALUE;
60 while(i < NUM_OF_ITEMS){
61 double ran = Math.abs(x.nextDouble());
62 if(ran < min){
63 min = ran;
64 }
65 if(ran > max){
66 max = ran;
67 }
68 ByteBuffer buf = ByteConversion.createByteBuffer(8);
69 buf.putDouble(x.nextDouble());
70 testData.put(ran, buf.array());
71 i++;
72 }
73
74
75
76 for(double j : testData.keySet()){
77
78 storage.put(j, testData.get(j));
79
80 }
81
82 for(double j : testData.keySet()){
83 assertTrue(Arrays.equals(storage.getValue(j), testData.get(j)));
84 }
85
86
87
88 CloseIterator<TupleDouble> it = storage.processRange(min, max);
89 i = 0;
90 boolean first = true;
91 double prev = Double.MIN_VALUE;
92 while(it.hasNext()){
93 TupleDouble t = it.next();
94 assertTrue(testData.get(t.getKey()) != null);
95 assertTrue("A:" + Arrays.toString(testData.get(t.getKey())) + "B:" + Arrays.toString(t.getValue()) + " i: " + i + "A-key: " + t.getKey() , Arrays.equals(testData.get(t.getKey()), t.getValue()) );
96
97 if(first){
98 prev = t.getKey();
99 first = false;
100 }else{
101
102 assertTrue( "Prev: " + prev + " t: " + t.getKey() + " i: " + i, prev < t.getKey());
103 prev = t.getKey();
104 }
105 i++;
106 }
107 assertEquals(testData.size(), i);
108 it.closeCursor();
109
110 it = storage.processRangeReverse(min, max);
111 i = 0;
112 first = true;
113 prev = Double.MIN_VALUE;
114 while(it.hasNext()){
115 TupleDouble t = it.next();
116 assertTrue(Arrays.equals(testData.get(t.getKey()), t.getValue()));
117 if(first){
118 prev = t.getKey();
119 first = false;
120 }else{
121
122 assertTrue( "Prev: " + prev + " t: " + t.getKey()+ " i: " + i, prev > t.getKey());
123 prev = t.getKey();
124 }
125 i++;
126 }
127 it.closeCursor();
128 assertEquals(testData.size(), i);
129
130
131
132
133
134 for(double j : testData.keySet()){
135 String d = x.nextDouble() + "";
136 testData.put(j, d.getBytes());
137 storage.put(j, d.getBytes());
138 }
139
140 for(double j : testData.keySet()){
141 assertTrue(Arrays.equals(storage.getValue(j), testData.get(j)));
142 }
143
144
145 for(double j : testData.keySet()){
146 assertTrue( storage.getValue(j) != null);
147 storage.delete(j);
148 assertTrue(storage.getValue(j) == null);
149 }
150
151
152 }
153
154
155
156
157
158
159
160
161 public static void validateDuplicates(OBStoreDouble storage) throws OBStorageException, OBException{
162 HashMap<Double, byte[][]> testData = new HashMap<Double, byte[][]>();
163 Random x = new Random();
164 int i = 0;
165 double min = Double.MAX_VALUE;
166 double max = Double.MIN_VALUE;
167 while(i < NUM_OF_ITEMS){
168 double ran = Math.abs(x.nextDouble());
169 if(ran < min){
170 min = ran;
171 }
172 if(ran > max){
173 max = ran;
174 }
175 byte[][] data = new byte[5][];
176 int cx = 0;
177 while(cx < data.length){
178 ByteBuffer buf = ByteConversion.createByteBuffer(8);
179 buf.putDouble(x.nextDouble());
180 data[cx] = buf.array();
181 cx++;
182 }
183
184 testData.put(ran, data);
185 i++;
186 }
187
188
189
190 for(double j : testData.keySet()){
191
192 byte[][] data = testData.get(j);
193 for(byte[] d : data){
194 storage.put(j, d);
195 }
196
197 }
198
199
200 for(double j : testData.keySet()){
201 for(byte[] d : testData.get(j)){
202 CloseIterator<TupleDouble> it = storage.processRange(j,j);
203 int found = 0;
204 while(it.hasNext()){
205 TupleDouble t = it.next();
206 if(Arrays.equals(d, t.getValue())){
207 found++;
208 }
209
210 }
211 it.closeCursor();
212 assertTrue(found == 1);
213 }
214 }
215
216
217 CloseIterator<TupleDouble> it = storage.processRange(Double.MIN_VALUE, Double.MAX_VALUE);
218 int c = 0;
219 double key = -1;
220 while(it.hasNext()){
221 TupleDouble t = it.next();
222 if(t.getKey() != key){
223 if(key != -1){
224 assert c == 5;
225 }
226 key = t.getKey();
227 c= 1;
228 }else{
229 c++;
230 }
231 }
232 it.closeCursor();
233
234
235
236
237 it = storage.processRangeReverse(Double.MIN_VALUE, Double.MAX_VALUE);
238 c = 0;
239 key = -1;
240 while(it.hasNext()){
241 TupleDouble t = it.next();
242 if(t.getKey() != key){
243 if(key != -1){
244 assert c == 5;
245 }
246 key = t.getKey();
247 c= 1;
248 }else{
249 c++;
250 }
251 }
252 it.closeCursor();
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 }
287
288
289 }