KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > tree > TreeTestBase


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

8
9 package com.sleepycat.je.tree;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.Database;
17 import com.sleepycat.je.DatabaseConfig;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.DbInternal;
20 import com.sleepycat.je.Environment;
21 import com.sleepycat.je.EnvironmentConfig;
22 import com.sleepycat.je.config.EnvironmentParams;
23 import com.sleepycat.je.dbi.NullCursor;
24 import com.sleepycat.je.log.FileManager;
25 import com.sleepycat.je.txn.LockResult;
26 import com.sleepycat.je.util.TestUtils;
27
28 public class TreeTestBase extends TestCase {
29     static protected final boolean DEBUG = true;
30
31     static protected int N_KEY_BYTES = 10;
32     static protected int N_ITERS = 1;
33     static protected int N_KEYS = 10000;
34     static protected int MAX_ENTRIES_PER_NODE = 6;
35
36     protected Tree tree = null;
37     protected byte[] minKey = null;
38     protected byte[] maxKey = null;
39     protected Database db = null;
40     protected Environment env = null;
41     protected File JavaDoc envHome = null;
42
43     public TreeTestBase() {
44         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
45     }
46
47     public void setUp()
48         throws IOException JavaDoc {
49
50         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
51     }
52
53     void initEnv(boolean duplicatesAllowed)
54         throws DatabaseException {
55
56         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
57         envConfig.setConfigParam(EnvironmentParams.ENV_RUN_EVICTOR.getName(),
58                                  "false");
59         envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(),
60                                  Integer.toString(MAX_ENTRIES_PER_NODE));
61         envConfig.setAllowCreate(true);
62         envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC));
63         env = new Environment(envHome, envConfig);
64
65         DatabaseConfig dbConfig = new DatabaseConfig();
66         dbConfig.setAllowCreate(true);
67         dbConfig.setSortedDuplicates(duplicatesAllowed);
68         db = env.openDatabase(null, "foo", dbConfig);
69
70         tree = DbInternal.dbGetDatabaseImpl(db).getTree();
71         minKey = null;
72         maxKey = null;
73     }
74
75     public void tearDown()
76     throws DatabaseException, IOException JavaDoc {
77
78         db.close();
79         if (env != null) {
80             env.close();
81         }
82         env = null;
83         db = null;
84         tree = null;
85         minKey = null;
86         maxKey = null;
87         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
88     }
89
90     protected IN makeDupIN(IN old) {
91         IN ret = new IN(DbInternal.dbGetDatabaseImpl(db),
92             old.getIdentifierKey(),
93                         MAX_ENTRIES_PER_NODE, 2);
94         ret.setNodeId(old.getNodeId());
95         ret.setIsRoot(old.isRoot());
96         for (int i = 0; i < old.getNEntries(); i++) {
97             ret.setEntry(i, old.getTarget(i), old.getKey(i),
98              old.getLsn(i), old.getState(i));
99         }
100
101         return ret;
102     }
103
104     /**
105      * Helper routine to insert a key and immediately read it back.
106      */

107     protected void insertAndRetrieve(NullCursor cursor, byte[] key, LN ln)
108         throws DatabaseException {
109
110         if (minKey == null) {
111             minKey = key;
112         } else if (Key.compareKeys(key, minKey, null) < 0) {
113             minKey = key;
114         }
115
116         if (maxKey == null) {
117             maxKey = key;
118         } else if (Key.compareKeys(maxKey, key, null) < 0) {
119             maxKey = key;
120         }
121
122         TestUtils.checkLatchCount();
123         assertTrue(tree.insert(ln, key, false, cursor,
124                    new LockResult(null, null)));
125         TestUtils.checkLatchCount();
126         assertTrue(retrieveLN(key) == ln);
127     }
128
129     /**
130      * Helper routine to read the LN referred to by key.
131      */

132     protected LN retrieveLN(byte[] key)
133         throws DatabaseException {
134
135         TestUtils.checkLatchCount();
136         IN n = tree.search(key, Tree.SearchType.NORMAL, -1, null, true);
137         if (!(n instanceof BIN)) {
138             fail("search didn't return a BIN for key: " + key);
139         }
140         BIN bin = (BIN) n;
141         try {
142             int index = bin.findEntry(key, false, true);
143             if (index == -1) {
144                 fail("Didn't read back key: " + key);
145             } else {
146                 Node node = bin.getTarget(index);
147                 if (node instanceof LN) {
148                     return (LN) node;
149                 } else {
150                     fail("Didn't read back LN for: " + key);
151                 }
152             }
153             /* We never get here, but the compiler doesn't know that. */
154             return null;
155         } finally {
156             bin.releaseLatch();
157             TestUtils.checkLatchCount();
158         }
159     }
160
161     /**
162      * Using getNextBin, count all the keys in the database. Ensure that
163      * they're returned in ascending order.
164      */

165     protected int countAndValidateKeys(Tree tree)
166         throws DatabaseException {
167
168         TestUtils.checkLatchCount();
169         BIN nextBin = (BIN) tree.getFirstNode();
170         byte[] prevKey = { 0x00 };
171
172         int cnt = 0;
173
174         while (nextBin != null) {
175             for (int i = 0; i < nextBin.getNEntries(); i++) {
176                 byte[] curKey = nextBin.getKey(i);
177                 if (Key.compareKeys(curKey, prevKey, null) <= 0) {
178                     throw new InconsistentNodeException
179                         ("keys are out of order");
180                 }
181                 cnt++;
182                 prevKey = curKey;
183             }
184             nextBin = tree.getNextBin(nextBin, false /* traverseWithinDupTree */);
185         }
186         TestUtils.checkLatchCount();
187         return cnt;
188     }
189
190     /**
191      * Using getPrevBin, count all the keys in the database. Ensure that
192      * they're returned in descending order.
193      */

194     protected int countAndValidateKeysBackwards(Tree tree)
195         throws DatabaseException {
196
197         TestUtils.checkLatchCount();
198         BIN nextBin = (BIN) tree.getLastNode();
199         byte[] prevKey = null;
200
201         int cnt = 0;
202
203         while (nextBin != null) {
204             for (int i = nextBin.getNEntries() - 1; i >= 0; i--) {
205                 byte[] curKey = nextBin.getKey(i);
206                 if (prevKey != null &&
207                     Key.compareKeys(prevKey, curKey, null) <= 0) {
208                     throw new InconsistentNodeException
209                         ("keys are out of order");
210                 }
211                 cnt++;
212                 prevKey = curKey;
213             }
214             nextBin = tree.getPrevBin(nextBin, false /* traverseWithinDupTree */);
215         }
216         return cnt;
217     }
218 }
219
Popular Tags