KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > DbCursorSearchTest


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

8
9 package com.sleepycat.je.dbi;
10
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import com.sleepycat.je.DatabaseEntry;
17 import com.sleepycat.je.DatabaseException;
18 import com.sleepycat.je.LockMode;
19 import com.sleepycat.je.OperationStatus;
20 import com.sleepycat.je.util.StringDbt;
21
22 /**
23  * Test cursor getSearch*
24  */

25 public class DbCursorSearchTest extends DbCursorTestBase {
26
27     public DbCursorSearchTest()
28         throws DatabaseException {
29
30         super();
31     }
32
33     /**
34      * Put a small number of data items into the database
35      * then make sure we can retrieve them with getSearchKey.
36      */

37     public void testSimpleSearchKey()
38     throws DatabaseException {
39         initEnv(false);
40     doSimpleCursorPuts();
41         verify(simpleDataMap, false);
42     }
43
44     /**
45      * Put a small number of data items into the database
46      * then make sure we can retrieve them with getSearchKey.
47      * Delete them, and make sure we can't search for them anymore.
48      */

49     public void testSimpleDeleteAndSearchKey()
50     throws DatabaseException {
51
52         initEnv(false);
53     doSimpleCursorPuts();
54         verify(simpleDataMap, true);
55     }
56
57     /**
58      * Put a large number of data items into the database,
59      * then make sure we can retrieve them with getSearchKey.
60      */

61     public void testLargeSearchKey()
62     throws DatabaseException {
63
64         initEnv(false);
65         Hashtable JavaDoc expectedData = new Hashtable JavaDoc();
66     doLargePut(expectedData, N_KEYS);
67         verify(expectedData, false);
68     }
69
70     /**
71      * Put a large number of data items into the database,
72      * then make sure we can retrieve them with getSearchKey.
73      */

74     public void testLargeDeleteAndSearchKey()
75     throws DatabaseException {
76
77         initEnv(false);
78         Hashtable JavaDoc expectedData = new Hashtable JavaDoc();
79     doLargePut(expectedData, N_KEYS);
80         verify(expectedData, true);
81     }
82
83     public void testLargeSearchKeyDuplicates()
84     throws DatabaseException {
85
86         initEnv(true);
87         Hashtable JavaDoc expectedData = new Hashtable JavaDoc();
88     createRandomDuplicateData(expectedData, false);
89
90         verifyDuplicates(expectedData);
91     }
92
93     /**
94      * Put a small number of data items into the database
95      * then make sure we can retrieve them with getSearchKey.
96      * See [#9337].
97      */

98     public void testSimpleSearchBothWithPartialDbt()
99     throws DatabaseException {
100
101         initEnv(false);
102     doSimpleCursorPuts();
103     DatabaseEntry key = new DatabaseEntry("bar".getBytes());
104     DatabaseEntry data = new DatabaseEntry(new byte[100]);
105     data.setSize(3);
106     System.arraycopy("two".getBytes(), 0, data.getData(), 0, 3);
107     OperationStatus status =
108         cursor2.getSearchBoth(key, data, LockMode.DEFAULT);
109     assertEquals(OperationStatus.SUCCESS, status);
110     }
111
112     public void testGetSearchBothNoDuplicatesAllowedSR9522()
113     throws DatabaseException {
114
115         initEnv(false);
116     doSimpleCursorPuts();
117     DatabaseEntry key = new DatabaseEntry("bar".getBytes());
118     DatabaseEntry data = new DatabaseEntry("two".getBytes());
119     OperationStatus status =
120         cursor2.getSearchBoth(key, data, LockMode.DEFAULT);
121     assertEquals(OperationStatus.SUCCESS, status);
122     }
123
124     /**
125      * Make sure the database contains the set of data we put in.
126      */

127     private void verify(Hashtable JavaDoc expectedData, boolean doDelete)
128     throws DatabaseException {
129
130         Iterator JavaDoc iter = expectedData.entrySet().iterator();
131         StringDbt testKey = new StringDbt();
132         StringDbt testData = new StringDbt();
133
134         // Iterate over the expected values.
135
while (iter.hasNext()) {
136             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
137             testKey.setString((String JavaDoc) entry.getKey());
138
139             // search for the expected values using SET.
140
OperationStatus status = cursor2.getSearchKey(testKey, testData,
141                               LockMode.DEFAULT);
142             assertEquals(OperationStatus.SUCCESS, status);
143             assertEquals((String JavaDoc) entry.getValue(), testData.getString());
144             assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
145
146             // check that getCurrent returns the same thing.
147
status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
148             assertEquals(OperationStatus.SUCCESS, status);
149             assertEquals((String JavaDoc) entry.getValue(), testData.getString());
150             assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
151
152         if (doDelete) {
153         // Delete the entry and make sure that getSearchKey doesn't
154
// return it again.
155
status = cursor2.delete();
156         assertEquals(OperationStatus.SUCCESS, status);
157
158         // search for the expected values using SET.
159
status =
160             cursor2.getSearchKey(testKey, testData, LockMode.DEFAULT);
161         assertEquals(OperationStatus.NOTFOUND, status);
162
163         // search for the expected values using SET_BOTH.
164
status =
165             cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT);
166         assertEquals(OperationStatus.NOTFOUND, status);
167
168         // search for the expected values using SET_RANGE - should
169
// give 0 except if this is the last key in the tree, in which
170
// case DB_NOTFOUND. It should never be DB_KEYEMPTY.
171
// XXX: It would be nice to be definite about the expected
172
// status, but to do that we have to know whether this is the
173
// highest key in the set, which we don't currently track.
174
status = cursor2.getSearchKeyRange
175             (testKey, testData, LockMode.DEFAULT);
176         assertTrue(status == OperationStatus.SUCCESS ||
177                status == OperationStatus.NOTFOUND);
178         } else {
179         // search for the expected values using SET_BOTH.
180
status =
181             cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT);
182         assertEquals(OperationStatus.SUCCESS, status);
183         assertEquals((String JavaDoc) entry.getValue(), testData.getString());
184         assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
185
186         // check that getCurrent returns the same thing.
187
status =
188             cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
189         assertEquals(OperationStatus.SUCCESS, status);
190         assertEquals((String JavaDoc) entry.getValue(), testData.getString());
191         assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
192
193         // check that SET_RANGE works as expected for exact keys
194
status = cursor2.getSearchKeyRange
195             (testKey, testData, LockMode.DEFAULT);
196         assertEquals(OperationStatus.SUCCESS, status);
197         assertEquals((String JavaDoc) entry.getValue(), testData.getString());
198         assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
199
200         // search for the expected values using SET_RANGE.
201
byte[] keyBytes = testKey.getData();
202         keyBytes[keyBytes.length - 1]--;
203         status = cursor2.getSearchKeyRange
204             (testKey, testData, LockMode.DEFAULT);
205         assertEquals(OperationStatus.SUCCESS, status);
206         assertEquals((String JavaDoc) entry.getValue(), testData.getString());
207         assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
208
209         // check that getCurrent returns the same thing.
210
status =
211             cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
212         assertEquals(OperationStatus.SUCCESS, status);
213         assertEquals((String JavaDoc) entry.getValue(), testData.getString());
214         assertEquals((String JavaDoc) entry.getKey(), testKey.getString());
215         }
216         }
217     }
218
219     private void verifyDuplicates(Hashtable JavaDoc expectedData)
220     throws DatabaseException {
221
222         Enumeration JavaDoc iter = expectedData.keys();
223         StringDbt testKey = new StringDbt();
224         StringDbt testData = new StringDbt();
225
226         // Iterate over the expected values.
227
while (iter.hasMoreElements()) {
228         String JavaDoc key = (String JavaDoc) iter.nextElement();
229             testKey.setString(key);
230
231             // search for the expected values using SET.
232
OperationStatus status = cursor2.getSearchKey(testKey, testData,
233                               LockMode.DEFAULT);
234             assertEquals(OperationStatus.SUCCESS, status);
235             assertEquals(key, testKey.getString());
236         String JavaDoc dataString = testData.getString();
237
238             // check that getCurrent returns the same thing.
239
status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
240             assertEquals(OperationStatus.SUCCESS, status);
241             assertEquals(dataString, testData.getString());
242             assertEquals(key, testKey.getString());
243
244             // search for the expected values using SET_RANGE.
245
byte[] keyBytes = testKey.getData();
246         keyBytes[keyBytes.length - 1]--;
247             status =
248         cursor2.getSearchKeyRange(testKey, testData, LockMode.DEFAULT);
249             assertEquals(OperationStatus.SUCCESS, status);
250             assertEquals(dataString, testData.getString());
251             assertEquals(key, testKey.getString());
252
253             // check that getCurrent returns the same thing.
254
status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
255             assertEquals(OperationStatus.SUCCESS, status);
256             assertEquals(dataString, testData.getString());
257             assertEquals(key, testKey.getString());
258
259         Hashtable JavaDoc ht = (Hashtable JavaDoc) expectedData.get(key);
260
261         Enumeration JavaDoc iter2 = ht.keys();
262         while (iter2.hasMoreElements()) {
263         String JavaDoc expectedDataString = (String JavaDoc) iter2.nextElement();
264         testData.setString(expectedDataString);
265
266         // search for the expected values using SET_BOTH.
267
status =
268             cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT);
269         assertEquals(OperationStatus.SUCCESS, status);
270         assertEquals(expectedDataString, testData.getString());
271         assertEquals(key, testKey.getString());
272
273         // check that getCurrent returns the same thing.
274
status =
275             cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
276         assertEquals(OperationStatus.SUCCESS, status);
277         assertEquals(expectedDataString, testData.getString());
278         assertEquals(key, testKey.getString());
279
280         // search for the expected values using SET_RANGE_BOTH.
281
byte[] dataBytes = testData.getData();
282         dataBytes[dataBytes.length - 1]--;
283         status = cursor2.getSearchBothRange(testKey, testData,
284                             LockMode.DEFAULT);
285         assertEquals(OperationStatus.SUCCESS, status);
286         assertEquals(key, testKey.getString());
287         assertEquals(expectedDataString, testData.getString());
288
289         // check that getCurrent returns the same thing.
290
status = cursor2.getCurrent(testKey, testData,
291                         LockMode.DEFAULT);
292         assertEquals(OperationStatus.SUCCESS, status);
293         assertEquals(expectedDataString, testData.getString());
294         assertEquals(key, testKey.getString());
295         }
296         }
297     }
298 }
299
Popular Tags