KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > GetSearchBothRangeTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: GetSearchBothRangeTest.java,v 1.11 2006/10/30 21:14:41 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12
13 import junit.framework.TestCase;
14 import java.util.Comparator JavaDoc;
15
16 import com.sleepycat.bind.tuple.IntegerBinding;
17 import com.sleepycat.je.config.EnvironmentParams;
18 import com.sleepycat.je.util.TestUtils;
19
20 /**
21  * Tests getSearchBothRange when searching for a key that doesn't exist.
22  * [#11119]
23  */

24 public class GetSearchBothRangeTest extends TestCase {
25
26     private File JavaDoc envHome;
27     private Environment env;
28     private Database db;
29     private boolean dups;
30
31     public GetSearchBothRangeTest() {
32         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
33     }
34
35     public void setUp()
36     throws Exception JavaDoc {
37
38         TestUtils.removeLogFiles("Setup", envHome, false);
39     }
40     
41     public void tearDown()
42     throws Exception JavaDoc {
43
44         if (env != null) {
45             try {
46                 env.close();
47             } catch (Exception JavaDoc e) {
48                 System.out.println("Ignored during close: " + e);
49             }
50         }
51         TestUtils.removeLogFiles("TearDown", envHome, false);
52         envHome = null;
53         env = null;
54         db = null;
55     }
56
57     /**
58      * Open environment and database.
59      */

60     private void openEnv()
61         throws DatabaseException {
62
63     openEnvWithComparator(null);
64     }
65
66     private void openEnvWithComparator(Class JavaDoc comparatorClass)
67     throws DatabaseException {
68
69         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
70         envConfig.setAllowCreate(true);
71         //*
72
envConfig.setConfigParam
73             (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
74         //*/
75
env = new Environment(envHome, envConfig);
76
77         DatabaseConfig dbConfig = new DatabaseConfig();
78         dbConfig.setSortedDuplicates(dups);
79         dbConfig.setAllowCreate(true);
80
81         dbConfig.setBtreeComparator(comparatorClass);
82
83         db = env.openDatabase(null, "GetSearchBothRangeTest", dbConfig);
84     }
85
86     /**
87      * Close environment and database.
88      */

89     private void closeEnv()
90         throws DatabaseException {
91
92         db.close();
93         db = null;
94         env.close();
95         env = null;
96     }
97
98     public void testSearchKeyRangeWithDupTree()
99         throws Exception JavaDoc {
100
101         dups = true;
102         openEnv();
103
104         insert(1, 1);
105         insert(1, 2);
106         insert(3, 1);
107
108         DatabaseEntry key = entry(2);
109         DatabaseEntry data = new DatabaseEntry();
110
111         Cursor cursor = db.openCursor(null, null);
112         OperationStatus status = cursor.getSearchKeyRange(key, data, null);
113         assertEquals(OperationStatus.SUCCESS, status);
114         assertEquals(3, val(key));
115         assertEquals(1, val(data));
116         cursor.close();
117
118         closeEnv();
119     }
120
121     public void testSearchBothWithNoDupTree()
122         throws Exception JavaDoc {
123
124         dups = true;
125         openEnv();
126
127         insert(1, 1);
128
129         DatabaseEntry key = entry(1);
130         DatabaseEntry data = entry(2);
131
132         Cursor cursor = db.openCursor(null, null);
133         OperationStatus status = cursor.getSearchBoth(key, data, null);
134         assertEquals(OperationStatus.NOTFOUND, status);
135         cursor.close();
136
137         key = entry(1);
138         data = entry(1);
139
140         cursor = db.openCursor(null, null);
141         status = cursor.getSearchBoth(key, data, null);
142         assertEquals(OperationStatus.SUCCESS, status);
143         assertEquals(1, val(key));
144         assertEquals(1, val(data));
145         cursor.close();
146
147         key = entry(1);
148         data = entry(0);
149
150         cursor = db.openCursor(null, null);
151         status = cursor.getSearchBothRange(key, data, null);
152         assertEquals(OperationStatus.SUCCESS, status);
153         assertEquals(1, val(key));
154         assertEquals(1, val(data));
155         cursor.close();
156
157         closeEnv();
158     }
159
160     public void testSuccess()
161         throws DatabaseException {
162
163         openEnv();
164         insert(1, 1);
165         insert(3, 1);
166         if (dups) {
167             insert(1, 2);
168             insert(3, 2);
169         }
170
171         DatabaseEntry key = entry(3);
172         DatabaseEntry data = entry(0);
173
174         Cursor cursor = db.openCursor(null, null);
175         OperationStatus status = cursor.getSearchBothRange(key, data, null);
176         assertEquals(OperationStatus.SUCCESS, status);
177         assertEquals(3, val(key));
178         assertEquals(1, val(data));
179         cursor.close();
180
181         closeEnv();
182     }
183
184     public void testSuccessDup()
185         throws DatabaseException {
186
187         dups = true;
188         testSuccess();
189     }
190
191     public void testNotFound()
192         throws DatabaseException {
193
194         openEnv();
195         insert(1, 0);
196         if (dups) {
197             insert(1, 1);
198         }
199
200         DatabaseEntry key = entry(2);
201         DatabaseEntry data = entry(0);
202
203         Cursor cursor = db.openCursor(null, null);
204         OperationStatus status = cursor.getSearchBothRange(key, data, null);
205         assertEquals(OperationStatus.NOTFOUND, status);
206         cursor.close();
207
208         closeEnv();
209     }
210
211     public void testNotFoundDup()
212         throws DatabaseException {
213
214         dups = true;
215         testNotFound();
216     }
217
218     public void testSearchBefore()
219         throws DatabaseException {
220
221         dups = true;
222         openEnv();
223         insert(1, 0);
224
225         DatabaseEntry key = entry(1);
226         DatabaseEntry data = entry(2);
227
228         Cursor cursor = db.openCursor(null, null);
229         OperationStatus status = cursor.getSearchBothRange(key, data, null);
230         assertEquals(OperationStatus.NOTFOUND, status);
231         cursor.close();
232
233         closeEnv();
234     }
235
236     public void testSearchBeforeDups()
237         throws DatabaseException {
238
239         dups = true;
240         openEnv();
241         insert(1, 1);
242         insert(1, 2);
243
244         DatabaseEntry key = entry(1);
245         DatabaseEntry data = entry(0);
246
247         Cursor cursor = db.openCursor(null, null);
248         OperationStatus status = cursor.getSearchBothRange(key, data, null);
249         assertEquals(OperationStatus.SUCCESS, status);
250         assertEquals(1, val(key));
251         assertEquals(1, val(data));
252         cursor.close();
253
254         closeEnv();
255     }
256
257     public static class NormalComparator implements Comparator JavaDoc {
258
259     public NormalComparator() {
260     }
261
262     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
263
264             DatabaseEntry arg1 = new DatabaseEntry((byte[]) o1);
265             DatabaseEntry arg2 = new DatabaseEntry((byte[]) o2);
266             int val1 = IntegerBinding.entryToInt(arg1);
267             int val2 = IntegerBinding.entryToInt(arg2);
268
269             if (val1 < val2) {
270                 return -1;
271             } else if (val1 > val2) {
272                 return 1;
273             } else {
274                 return 0;
275             }
276     }
277     }
278
279     public void testSearchAfterDups()
280         throws DatabaseException {
281
282         dups = true;
283         openEnv();
284         insert(1, 0);
285         insert(1, 1);
286         insert(2, 0);
287         insert(2, 1);
288
289         DatabaseEntry key = entry(1);
290         DatabaseEntry data = entry(2);
291
292         Cursor cursor = db.openCursor(null, null);
293         OperationStatus status = cursor.getSearchBothRange(key, data, null);
294         assertEquals(OperationStatus.NOTFOUND, status);
295         cursor.close();
296
297         closeEnv();
298     }
299
300     public void testSearchAfterDupsWithComparator()
301         throws DatabaseException {
302
303         dups = true;
304         openEnvWithComparator(NormalComparator.class);
305         insert(1, 0);
306         insert(1, 1);
307         insert(2, 0);
308         insert(2, 1);
309
310         DatabaseEntry key = entry(1);
311         DatabaseEntry data = entry(2);
312
313         Cursor cursor = db.openCursor(null, null);
314         OperationStatus status = cursor.getSearchBothRange(key, data, null);
315         assertEquals(OperationStatus.NOTFOUND, status);
316         cursor.close();
317
318         closeEnv();
319     }
320
321     public void testSearchAfterDeletedDup()
322         throws DatabaseException {
323
324         dups = true;
325         openEnv();
326         insert(1, 1);
327         insert(1, 2);
328         insert(1, 3);
329
330         /* Delete {1,3} leaving {1,1} in dup tree. */
331         Cursor cursor = db.openCursor(null, null);
332         DatabaseEntry key = entry(1);
333         DatabaseEntry data = entry(3);
334         OperationStatus status = cursor.getSearchBothRange(key, data, null);
335         assertEquals(OperationStatus.SUCCESS, status);
336         cursor.delete();
337         cursor.close();
338         env.compress();
339
340         /* Search for {1,3} and expect NOTFOUND. */
341         cursor = db.openCursor(null, null);
342         key = entry(1);
343         data = entry(3);
344         status = cursor.getSearchBothRange(key, data, null);
345         assertEquals(OperationStatus.NOTFOUND, status);
346         cursor.close();
347
348         closeEnv();
349     }
350
351     public void testSingleDatumBug()
352         throws DatabaseException {
353
354         dups = true;
355         openEnv();
356         insert(1, 1);
357         insert(2, 2);
358
359         /* Search for {1,2} and expect NOTFOUND. */
360         Cursor cursor = db.openCursor(null, null);
361         DatabaseEntry key = entry(1);
362         DatabaseEntry data = entry(2);
363         OperationStatus status = cursor.getSearchBothRange(key, data, null);
364         assertEquals(OperationStatus.NOTFOUND, status);
365         cursor.close();
366
367         closeEnv();
368     }
369
370     private int val(DatabaseEntry entry) {
371         return IntegerBinding.entryToInt(entry);
372     }
373
374     private DatabaseEntry entry(int val) {
375         DatabaseEntry entry = new DatabaseEntry();
376         IntegerBinding.intToEntry(val, entry);
377         return entry;
378     }
379
380     private void insert(int keyVal, int dataVal)
381         throws DatabaseException {
382
383         DatabaseEntry key = new DatabaseEntry();
384         DatabaseEntry data = new DatabaseEntry();
385         IntegerBinding.intToEntry(keyVal, key);
386         IntegerBinding.intToEntry(dataVal, data);
387         OperationStatus status;
388         if (dups) {
389             status = db.putNoDupData(null, key, data);
390         } else {
391             status= db.putNoOverwrite(null, key, data);
392         }
393         assertEquals(OperationStatus.SUCCESS, status);
394     }
395 }
396
Popular Tags