File: DbCursorSearchTest.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (298 lines) | stat: -rw-r--r-- 10,529 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
290
291
292
293
294
295
296
297
298
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: DbCursorSearchTest.java,v 1.37.2.2 2010/01/04 15:30:43 cwl Exp $
 */

package com.sleepycat.je.dbi;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.util.StringDbt;

/**
 * Test cursor getSearch*
 */
public class DbCursorSearchTest extends DbCursorTestBase {

    public DbCursorSearchTest()
        throws DatabaseException {

        super();
    }

    /**
     * Put a small number of data items into the database
     * then make sure we can retrieve them with getSearchKey.
     */
    public void testSimpleSearchKey()
	throws DatabaseException {
        initEnv(false);
	doSimpleCursorPuts();
        verify(simpleDataMap, false);
    }

    /**
     * Put a small number of data items into the database
     * then make sure we can retrieve them with getSearchKey.
     * Delete them, and make sure we can't search for them anymore.
     */
    public void testSimpleDeleteAndSearchKey()
	throws DatabaseException {

        initEnv(false);
	doSimpleCursorPuts();
        verify(simpleDataMap, true);
    }

    /**
     * Put a large number of data items into the database,
     * then make sure we can retrieve them with getSearchKey.
     */
    public void testLargeSearchKey()
	throws DatabaseException {

        initEnv(false);
        Hashtable expectedData = new Hashtable();
	doLargePut(expectedData, N_KEYS);
        verify(expectedData, false);
    }

    /**
     * Put a large number of data items into the database,
     * then make sure we can retrieve them with getSearchKey.
     */
    public void testLargeDeleteAndSearchKey()
	throws DatabaseException {

        initEnv(false);
        Hashtable expectedData = new Hashtable();
	doLargePut(expectedData, N_KEYS);
        verify(expectedData, true);
    }

    public void testLargeSearchKeyDuplicates()
	throws DatabaseException {

        initEnv(true);
        Hashtable expectedData = new Hashtable();
	createRandomDuplicateData(expectedData, false);

        verifyDuplicates(expectedData);
    }

    /**
     * Put a small number of data items into the database
     * then make sure we can retrieve them with getSearchKey.
     * See [#9337].
     */
    public void testSimpleSearchBothWithPartialDbt()
	throws DatabaseException {

        initEnv(false);
	doSimpleCursorPuts();
	DatabaseEntry key = new DatabaseEntry("bar".getBytes());
	DatabaseEntry data = new DatabaseEntry(new byte[100]);
	data.setSize(3);
	System.arraycopy("two".getBytes(), 0, data.getData(), 0, 3);
	OperationStatus status =
	    cursor2.getSearchBoth(key, data, LockMode.DEFAULT);
	assertEquals(OperationStatus.SUCCESS, status);
    }

    public void testGetSearchBothNoDuplicatesAllowedSR9522()
	throws DatabaseException {

        initEnv(false);
	doSimpleCursorPuts();
	DatabaseEntry key = new DatabaseEntry("bar".getBytes());
	DatabaseEntry data = new DatabaseEntry("two".getBytes());
	OperationStatus status =
	    cursor2.getSearchBoth(key, data, LockMode.DEFAULT);
	assertEquals(OperationStatus.SUCCESS, status);
    }

    /**
     * Make sure the database contains the set of data we put in.
     */
    private void verify(Hashtable expectedData, boolean doDelete)
	throws DatabaseException {

        Iterator iter = expectedData.entrySet().iterator();
        StringDbt testKey = new StringDbt();
        StringDbt testData = new StringDbt();

        // Iterate over the expected values.
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            testKey.setString((String) entry.getKey());

            // search for the expected values using SET.
            OperationStatus status = cursor2.getSearchKey(testKey, testData,
							  LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals((String) entry.getValue(), testData.getString());
            assertEquals((String) entry.getKey(), testKey.getString());

            // check that getCurrent returns the same thing.
            status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals((String) entry.getValue(), testData.getString());
            assertEquals((String) entry.getKey(), testKey.getString());

	    if (doDelete) {
		// Delete the entry and make sure that getSearchKey doesn't
		// return it again.
		status = cursor2.delete();
		assertEquals(OperationStatus.SUCCESS, status);

		// search for the expected values using SET.
		status =
		    cursor2.getSearchKey(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.NOTFOUND, status);

		// search for the expected values using SET_BOTH.
		status =
		    cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.NOTFOUND, status);

		// search for the expected values using SET_RANGE - should
		// give 0 except if this is the last key in the tree, in which
		// case DB_NOTFOUND.  It should never be DB_KEYEMPTY.
		// It would be nice to be definite about the expected
		// status, but to do that we have to know whether this is the
		// highest key in the set, which we don't currently track.
		status = cursor2.getSearchKeyRange
		    (testKey, testData, LockMode.DEFAULT);
		assertTrue(status == OperationStatus.SUCCESS ||
			   status == OperationStatus.NOTFOUND);
	    } else {
		// search for the expected values using SET_BOTH.
		status =
		    cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals((String) entry.getValue(), testData.getString());
		assertEquals((String) entry.getKey(), testKey.getString());

		// check that getCurrent returns the same thing.
		status =
		    cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals((String) entry.getValue(), testData.getString());
		assertEquals((String) entry.getKey(), testKey.getString());

		// check that SET_RANGE works as expected for exact keys
		status = cursor2.getSearchKeyRange
		    (testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals((String) entry.getValue(), testData.getString());
		assertEquals((String) entry.getKey(), testKey.getString());

		// search for the expected values using SET_RANGE.
		byte[] keyBytes = testKey.getData();
		keyBytes[keyBytes.length - 1]--;
		status = cursor2.getSearchKeyRange
		    (testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals((String) entry.getValue(), testData.getString());
		assertEquals((String) entry.getKey(), testKey.getString());

		// check that getCurrent returns the same thing.
		status =
		    cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals((String) entry.getValue(), testData.getString());
		assertEquals((String) entry.getKey(), testKey.getString());
	    }
        }
    }

    private void verifyDuplicates(Hashtable expectedData)
	throws DatabaseException {

        Enumeration iter = expectedData.keys();
        StringDbt testKey = new StringDbt();
        StringDbt testData = new StringDbt();

        // Iterate over the expected values.
        while (iter.hasMoreElements()) {
	    String key = (String) iter.nextElement();
            testKey.setString(key);

            // search for the expected values using SET.
            OperationStatus status = cursor2.getSearchKey(testKey, testData,
							  LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals(key, testKey.getString());
	    String dataString = testData.getString();

            // check that getCurrent returns the same thing.
            status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals(dataString, testData.getString());
            assertEquals(key, testKey.getString());

            // search for the expected values using SET_RANGE.
	    byte[] keyBytes = testKey.getData();
	    keyBytes[keyBytes.length - 1]--;
            status =
		cursor2.getSearchKeyRange(testKey, testData, LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals(dataString, testData.getString());
            assertEquals(key, testKey.getString());

            // check that getCurrent returns the same thing.
            status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals(dataString, testData.getString());
            assertEquals(key, testKey.getString());

	    Hashtable ht = (Hashtable) expectedData.get(key);

	    Enumeration iter2 = ht.keys();
	    while (iter2.hasMoreElements()) {
		String expectedDataString = (String) iter2.nextElement();
		testData.setString(expectedDataString);

		// search for the expected values using SET_BOTH.
		status =
		    cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals(expectedDataString, testData.getString());
		assertEquals(key, testKey.getString());

		// check that getCurrent returns the same thing.
		status =
		    cursor2.getCurrent(testKey, testData, LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals(expectedDataString, testData.getString());
		assertEquals(key, testKey.getString());

		// search for the expected values using SET_RANGE_BOTH.
		byte[] dataBytes = testData.getData();
		dataBytes[dataBytes.length - 1]--;
		status = cursor2.getSearchBothRange(testKey, testData,
						    LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals(key, testKey.getString());
		assertEquals(expectedDataString, testData.getString());

		// check that getCurrent returns the same thing.
		status = cursor2.getCurrent(testKey, testData,
					    LockMode.DEFAULT);
		assertEquals(OperationStatus.SUCCESS, status);
		assertEquals(expectedDataString, testData.getString());
		assertEquals(key, testKey.getString());
	    }
        }
    }
}