File: DbCursorDupTest.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 (194 lines) | stat: -rw-r--r-- 6,576 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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: DbCursorDupTest.java,v 1.33.2.2 2010/01/04 15:30:43 cwl Exp $
 */

package com.sleepycat.je.dbi;

import java.io.IOException;

import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
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;

/**
 * Various unit tests for CursorImpl.dup().
 */
public class DbCursorDupTest extends DbCursorTestBase {

    public DbCursorDupTest()
        throws DatabaseException {

        super();
    }

    public void testCursorDupAndCloseDb()
	throws DatabaseException {

        initEnv(false);
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        Database myDb = exampleEnv.openDatabase(null, "fooDb", dbConfig);

	myDb.put(null, new StringDbt("blah"), new StringDbt("blort"));
	Cursor cursor = myDb.openCursor(null, null);
	OperationStatus status = cursor.getNext(new DatabaseEntry(),
                                                new DatabaseEntry(),
                                                LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
	Cursor cursorDup = cursor.dup(true);
	cursor.close();
	cursorDup.close();
	myDb.close();
    }

    public void testDupInitialized()
        throws DatabaseException {

        /* Open db. */
        initEnv(false);
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        Database myDb = exampleEnv.openDatabase(null, "fooDb", dbConfig);

        /* Open uninitialized cursor. */
        Cursor c1 = myDb.openCursor(null, null);
        try {
            c1.getCurrent(new DatabaseEntry(), new DatabaseEntry(), null);
            fail();
        } catch (DatabaseException expected) {}

        /* Dup uninitialized cursor with samePosition=false. */
        Cursor c2 = c1.dup(false);
        try {
            c2.getCurrent(new DatabaseEntry(), new DatabaseEntry(), null);
            fail();
        } catch (DatabaseException expected) {}

        /* Dup uninitialized cursor with samePosition=true. */
        Cursor c3 = c1.dup(true);
        try {
            c3.getCurrent(new DatabaseEntry(), new DatabaseEntry(), null);
            fail();
        } catch (DatabaseException expected) {}

        /* Ensure dup'ed cursors are usable. */
        assertEquals(OperationStatus.SUCCESS,
                     c1.put(new DatabaseEntry(new byte[0]),
                            new DatabaseEntry(new byte[0])));
        assertEquals(OperationStatus.SUCCESS,
                     c2.getFirst(new DatabaseEntry(), new DatabaseEntry(),
                                 null));
        assertEquals(OperationStatus.NOTFOUND,
                     c2.getNext(new DatabaseEntry(), new DatabaseEntry(),
                                 null));
        assertEquals(OperationStatus.SUCCESS,
                     c3.getFirst(new DatabaseEntry(), new DatabaseEntry(),
                                 null));
        assertEquals(OperationStatus.NOTFOUND,
                     c3.getNext(new DatabaseEntry(), new DatabaseEntry(),
                                 null));

        /* Close db. */
        c3.close();
        c2.close();
        c1.close();
	myDb.close();
    }

    /**
     * Create some duplicate data.
     *
     * Pass 1, walk over the data and with each iteration, dup() the
     * cursor at the same position.  Ensure that the dup points to the
     * same key/data pair.  Advance the dup'd cursor and ensure that
     * the data is different (key may be the same since it's a
     * duplicate set).  Then dup() the cursor without maintaining
     * position.  Ensure that getCurrent() throws a Cursor Not Init'd
     * exception.
     *
     * Pass 2, iterate through the data, and dup the cursor in the
     * same position.  Advance the original cursor and ensure that the
     * dup()'d points to the original data and the original cursor
     * points at new data.
     */
    public void testCursorDupSamePosition()
        throws IOException, DatabaseException {

        initEnv(true);
	createRandomDuplicateData(null, false);

	DataWalker dw = new DataWalker(null) {
		void perData(String foundKey, String foundData)
                    throws DatabaseException {
		    DatabaseEntry keyDbt = new DatabaseEntry();
		    DatabaseEntry dataDbt = new DatabaseEntry();
		    Cursor cursor2 = cursor.dup(true);
		    cursor2.getCurrent(keyDbt, dataDbt, LockMode.DEFAULT);
		    String c2Key = new String(keyDbt.getData());
		    String c2Data = new String(dataDbt.getData());
		    assertTrue(c2Key.equals(foundKey));
		    assertTrue(c2Data.equals(foundData));
		    if (cursor2.getNext(keyDbt,
					dataDbt,
					LockMode.DEFAULT) ==
                        OperationStatus.SUCCESS) {
			/* Keys can be the same because we have duplicates. */
			/*
			  assertFalse(new String(keyDbt.getData()).
			  equals(foundKey));
			*/
			assertFalse(new String(dataDbt.getData()).
				    equals(foundData));
		    }
		    cursor2.close();
		    try {
			cursor2 = cursor.dup(false);
			cursor2.getCurrent(keyDbt, dataDbt, LockMode.DEFAULT);
			fail("didn't catch Cursor not initialized exception");
		    } catch (DatabaseException DBE) {
		    }
		    cursor2.close();
		}
	    };
	dw.setIgnoreDataMap(true);
	dw.walkData();

	dw = new DataWalker(null) {
		void perData(String foundKey, String foundData)
                    throws DatabaseException {
		    DatabaseEntry keyDbt = new DatabaseEntry();
		    DatabaseEntry dataDbt = new DatabaseEntry();
		    DatabaseEntry key2Dbt = new DatabaseEntry();
		    DatabaseEntry data2Dbt = new DatabaseEntry();
		    Cursor cursor2 = cursor.dup(true);

		    OperationStatus status =
			cursor.getNext(keyDbt, dataDbt, LockMode.DEFAULT);

		    cursor2.getCurrent(key2Dbt, data2Dbt, LockMode.DEFAULT);
		    String c2Key = new String(key2Dbt.getData());
		    String c2Data = new String(data2Dbt.getData());
		    assertTrue(c2Key.equals(foundKey));
		    assertTrue(c2Data.equals(foundData));
		    if (status == OperationStatus.SUCCESS) {
			assertFalse(new String(dataDbt.getData()).
				    equals(foundData));
			assertFalse(new String(dataDbt.getData()).
				    equals(c2Data));
		    }
		    cursor2.close();
		}
	    };
	dw.setIgnoreDataMap(true);
	dw.walkData();
    }
}