KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > test > SequenceTest


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

8
9 package com.sleepycat.je.test;
10
11 import junit.framework.Test;
12
13 import com.sleepycat.je.Database;
14 import com.sleepycat.je.DatabaseConfig;
15 import com.sleepycat.je.DatabaseEntry;
16 import com.sleepycat.je.DatabaseException;
17 import com.sleepycat.je.OperationStatus;
18 import com.sleepycat.je.Sequence;
19 import com.sleepycat.je.SequenceConfig;
20 import com.sleepycat.je.SequenceStats;
21 import com.sleepycat.je.StatsConfig;
22 import com.sleepycat.je.Transaction;
23
24 public class SequenceTest extends TxnTestCase {
25
26     public static Test suite() {
27         return txnTestSuite(SequenceTest.class, null, null);
28     }
29
30     public void testIllegal()
31         throws DatabaseException {
32
33         DatabaseEntry key = new DatabaseEntry(new byte[1]);
34         SequenceConfig config = new SequenceConfig();
35         config.setAllowCreate(true);
36
37         /* Duplicates not allowed. */
38
39         Database db = openDb("dups", true);
40         Transaction txn = txnBegin();
41         try {
42             db.openSequence(txn, key, config);
43             fail();
44         } catch (IllegalArgumentException JavaDoc expected) {
45             String JavaDoc msg = expected.getMessage();
46             assertTrue(msg, msg.indexOf("duplicates") >= 0);
47         }
48         txnCommit(txn);
49         db.close();
50
51         db = openDb("foo");
52         txn = txnBegin();
53
54         /* Range min must be less than max. */
55
56         config.setRange(0, 0);
57         try {
58             db.openSequence(txn, key, config);
59             fail();
60         } catch (IllegalArgumentException JavaDoc expected) {
61             String JavaDoc msg = expected.getMessage();
62             assertTrue(msg, msg.indexOf("less than the maximum") >= 0);
63         }
64
65         /* Initial value must be within range. */
66
67         config.setRange(-10, 10);
68         config.setInitialValue(-11);
69         try {
70             db.openSequence(txn, key, config);
71             fail();
72         } catch (IllegalArgumentException JavaDoc expected) {
73             String JavaDoc msg = expected.getMessage();
74             assertTrue(msg, msg.indexOf("out of range") >= 0);
75         }
76         config.setInitialValue(11);
77         try {
78             db.openSequence(txn, key, config);
79             fail();
80         } catch (IllegalArgumentException JavaDoc expected) {
81             String JavaDoc msg = expected.getMessage();
82             assertTrue(msg, msg.indexOf("out of range") >= 0);
83         }
84
85         /* Cache size must be within range. */
86
87         config.setRange(-10, 10);
88         config.setCacheSize(21);
89         config.setInitialValue(0);
90         try {
91             db.openSequence(txn, key, config);
92             fail();
93         } catch (IllegalArgumentException JavaDoc expected) {
94             String JavaDoc msg = expected.getMessage();
95             assertTrue(msg, msg.indexOf("cache size is larger") >= 0);
96         }
97
98         /* Create with legal range values. */
99
100         config.setRange(1, 2);
101         config.setInitialValue(1);
102         config.setCacheSize(0);
103         Sequence seq = db.openSequence(txn, key, config);
104
105         /* Key must not exist if ExclusiveCreate=true. */
106
107         config.setExclusiveCreate(true);
108         try {
109             db.openSequence(txn, key, config);
110             fail();
111         } catch (DatabaseException expected) {
112             String JavaDoc msg = expected.getMessage();
113             assertTrue(msg, msg.indexOf("already exists") >= 0);
114         }
115         config.setExclusiveCreate(false);
116         seq.close();
117
118         /* Key must exist if AllowCreate=false. */
119
120         db.removeSequence(txn, key);
121         config.setAllowCreate(false);
122         try {
123             db.openSequence(txn, key, config);
124             fail();
125         } catch (DatabaseException expected) {
126             String JavaDoc msg = expected.getMessage();
127             assertTrue(msg, msg.indexOf("does not exist") >= 0);
128         }
129
130         /* Check wrapping not allowed. */
131
132         db.removeSequence(txn, key);
133         config.setAllowCreate(true);
134         config.setRange(-5, 5);
135         config.setInitialValue(-5);
136         seq = db.openSequence(txn, key, config);
137         for (long i = config.getRangeMin(); i <= config.getRangeMax(); i++) {
138             assertEquals(i, seq.get(txn, 1));
139         }
140         try {
141             seq.get(txn, 1);
142             fail();
143         } catch (DatabaseException expected) {
144             String JavaDoc msg = expected.getMessage();
145             assertTrue(msg, msg.indexOf("overflow") >= 0);
146         }
147
148         /* Check wrapping not allowed, decrement. */
149
150         db.removeSequence(txn, key);
151         config.setAllowCreate(true);
152         config.setAllowCreate(true);
153         config.setRange(-5, 5);
154         config.setInitialValue(5);
155         config.setDecrement(true);
156         seq = db.openSequence(txn, key, config);
157         for (long i = config.getRangeMax(); i >= config.getRangeMin(); i--) {
158             assertEquals(i, seq.get(txn, 1));
159         }
160         try {
161             seq.get(txn, 1);
162             fail();
163         } catch (DatabaseException expected) {
164             String JavaDoc msg = expected.getMessage();
165             assertTrue(msg, msg.indexOf("overflow") >= 0);
166         }
167
168         /* Check delta less than one. */
169         try {
170             seq.get(txn, 0);
171             fail();
172         } catch (IllegalArgumentException JavaDoc expected) {
173             String JavaDoc msg = expected.getMessage();
174             assertTrue(msg, msg.indexOf("greater than zero") >= 0);
175         }
176
177         /* Check delta greater than range. */
178         try {
179             seq.get(txn, 11);
180             fail();
181         } catch (IllegalArgumentException JavaDoc expected) {
182             String JavaDoc msg = expected.getMessage();
183             assertTrue(msg, msg.indexOf("larger than the range") >= 0);
184         }
185
186         seq.close();
187         txnCommit(txn);
188         db.close();
189     }
190
191     public void testBasic()
192         throws DatabaseException {
193
194         Database db = openDb("foo");
195         DatabaseEntry key = new DatabaseEntry(new byte[0]);
196         DatabaseEntry data = new DatabaseEntry();
197
198         SequenceConfig config = new SequenceConfig();
199         config.setAllowCreate(true);
200
201         Transaction txn = txnBegin();
202         Sequence seq = db.openSequence(txn, key, config);
203         txnCommit(txn);
204
205         txn = txnBegin();
206
207         /* Check default values before calling get(). */
208
209         SequenceStats stats = seq.getStats(null);
210         assertEquals(0, stats.getCurrent());
211         assertEquals(0, stats.getCacheSize());
212         assertEquals(0, stats.getNGets());
213         assertEquals(Long.MIN_VALUE, stats.getMin());
214         assertEquals(Long.MAX_VALUE, stats.getMax());
215
216         /* Get the first value. */
217
218         long val = seq.get(txn, 1);
219         assertEquals(0, val);
220         stats = seq.getStats(null);
221         assertEquals(1, stats.getCurrent());
222         assertEquals(1, stats.getValue());
223         assertEquals(0, stats.getLastValue());
224         assertEquals(1, stats.getNGets());
225
226         /* Use deltas greater than one. */
227
228         assertEquals(1, seq.get(txn, 2));
229         assertEquals(3, seq.get(txn, 3));
230         assertEquals(6, seq.get(txn, 1));
231         assertEquals(7, seq.get(txn, 1));
232
233         /* Remove a sequence and expect the key to be deleted. */
234
235         seq.close();
236         db.removeSequence(txn, key);
237         assertEquals(OperationStatus.NOTFOUND, db.get(txn, key, data, null));
238         txnCommit(txn);
239         assertEquals(OperationStatus.NOTFOUND, db.get(null, key, data, null));
240
241         db.close();
242     }
243
244     public void testMultipleHandles()
245         throws DatabaseException {
246
247         Database db = openDb("foo");
248         DatabaseEntry key = new DatabaseEntry(new byte[0]);
249
250         /* Create a sequence. */
251
252         SequenceConfig config = new SequenceConfig();
253         config.setAllowCreate(true);
254         config.setDecrement(true);
255         config.setRange(1, 3);
256         config.setInitialValue(3);
257
258         Transaction txn = txnBegin();
259         Sequence seq = db.openSequence(txn, key, config);
260         assertEquals(3, seq.get(txn, 1));
261         txnCommit(txn);
262
263         /* Open another handle on the same sequence -- config should match. */
264
265         txn = txnBegin();
266         Sequence seq2 = db.openSequence(txn, key, config);
267         assertEquals(2, seq2.get(txn, 1));
268         txnCommit(txn);
269
270         SequenceStats stats = seq2.getStats(null);
271         assertEquals(1, stats.getCurrent());
272         assertEquals(1, stats.getMin());
273         assertEquals(3, stats.getMax());
274
275         /* Values are assigned from a single sequence for both handles. */
276
277         assertEquals(1, seq.get(null, 1));
278
279         seq.close();
280         seq2.close();
281         db.close();
282     }
283
284     public void testRanges()
285         throws DatabaseException {
286
287         Database db = openDb("foo");
288
289         /* Positive and negative ranges. */
290
291         doRange(db, 1, 10, 1, 0);
292         doRange(db, -10, -1, 1, 0);
293         doRange(db, -10, 10, 1, 0);
294
295         /* Extreme min/max values. */
296
297         doRange(db, Integer.MIN_VALUE, Integer.MIN_VALUE + 10, 1, 0);
298         doRange(db, Integer.MAX_VALUE - 10, Integer.MAX_VALUE, 1, 0);
299
300         doRange(db, Long.MIN_VALUE, Long.MIN_VALUE + 10, 1, 0);
301         doRange(db, Long.MAX_VALUE - 10, Long.MAX_VALUE, 1, 0);
302
303         /* Deltas greater than one. */
304
305         doRange(db, -10, 10, 2, 0);
306         doRange(db, -10, 10, 3, 0);
307         doRange(db, -10, 10, 5, 0);
308         doRange(db, -10, 10, 10, 0);
309         doRange(db, -10, 10, 20, 0);
310
311         /*
312          * Cache sizes. We cheat a little by making the cache size an even
313          * multiple of the delta whenever the cache size is greater than the
314          * delta; otherwise, it is too difficult to predict caching.
315          */

316
317         doRange(db, -10, 10, 1, 1);
318         doRange(db, -10, 10, 1, 2);
319         doRange(db, -10, 10, 1, 3);
320         doRange(db, -10, 10, 1, 7);
321         doRange(db, -10, 10, 1, 20);
322         doRange(db, -10, 10, 3, 1);
323         doRange(db, -10, 10, 3, 2);
324         doRange(db, -10, 10, 3, 3);
325         doRange(db, -10, 10, 3, 9);
326         doRange(db, -10, 10, 3, 18);
327
328         db.close();
329     }
330
331     private void doRange(Database db, long min, long max, int delta, int cache)
332         throws DatabaseException {
333
334         DatabaseEntry key = new DatabaseEntry(new byte[1]);
335         boolean incr;
336         boolean wrap;
337
338         for (int option = 0; option < 4; option += 1) {
339             switch (option) {
340             case 0:
341                 incr = true;
342                 wrap = false;
343                 break;
344             case 1:
345                 incr = true;
346                 wrap = false;
347                 break;
348             case 2:
349                 incr = true;
350                 wrap = false;
351                 break;
352             case 3:
353                 incr = true;
354                 wrap = false;
355                 break;
356             default:
357                 throw new IllegalStateException JavaDoc();
358             }
359
360             SequenceConfig config = new SequenceConfig();
361             config.setAllowCreate(true);
362             config.setInitialValue(incr ? min : max);
363             config.setWrap(wrap);
364             config.setDecrement(!incr);
365             config.setRange(min, max);
366             config.setCacheSize(cache);
367
368             String JavaDoc msg =
369                 "incr=" + incr +
370                 " wrap=" + wrap +
371                 " min=" + min +
372                 " max=" + max +
373                 " delta=" + delta +
374                 " cache=" + cache;
375
376             Transaction txn = txnBegin();
377             db.removeSequence(txn, key);
378             Sequence seq = db.openSequence(txn, key, config);
379             txnCommit(txn);
380
381             txn = txnBegin();
382
383             if (incr) {
384                 for (long i = min;; i += delta) {
385
386                     boolean expectCached = false;
387                     if (cache != 0) {
388                         expectCached = delta < cache && i != max &&
389                             (((i - min) % cache) != 0);
390                     }
391
392                     doOne(msg, seq, txn, delta, i, expectCached);
393
394                     /* Test for end without causing long overflow. */
395                     if (i > max - delta) {
396                         if (delta == 1) {
397                             assertEquals(msg, i, max);
398                         }
399                         break;
400                     }
401                 }
402                 if (wrap) {
403                     assertEquals(msg, min, seq.get(txn, delta));
404                     assertEquals(msg, min + delta, seq.get(txn, delta));
405                 }
406             } else {
407                 for (long i = max;; i -= delta) {
408
409                     boolean expectCached = false;
410                     if (cache != 0) {
411                         expectCached = delta < cache && i != min &&
412                             (((max - i) % cache) != 0);
413                     }
414
415                     doOne(msg, seq, txn, delta, i, expectCached);
416
417                     /* Test for end without causing long overflow. */
418                     if (i < min + delta) {
419                         if (delta == 1) {
420                             assertEquals(msg, i, min);
421                         }
422                         break;
423                     }
424                 }
425                 if (wrap) {
426                     assertEquals(msg, max, seq.get(txn, delta));
427                     assertEquals(msg, max - delta, seq.get(txn, delta));
428                 }
429             }
430
431             if (!wrap) {
432                 try {
433                     seq.get(txn, delta);
434                     fail(msg);
435                 } catch (DatabaseException expected) {
436                     String JavaDoc emsg = expected.getMessage();
437                     assertTrue(emsg, emsg.indexOf("overflow") >= 0);
438                 }
439             }
440
441             txnCommit(txn);
442             seq.close();
443         }
444     }
445
446     private void doOne(String JavaDoc msg,
447                        Sequence seq,
448                        Transaction txn,
449                        int delta,
450                        long expectValue,
451                        boolean expectCached)
452         throws DatabaseException {
453
454         msg += " value=" + expectValue;
455
456         try {
457             assertEquals(msg, expectValue, seq.get(txn, delta));
458         } catch (DatabaseException e) {
459             fail(msg + ' ' + e);
460         }
461
462         StatsConfig clearConfig = new StatsConfig();
463         clearConfig.setFast(true);
464         clearConfig.setClear(true);
465         SequenceStats stats = seq.getStats(clearConfig);
466
467         assertEquals(msg, 1, stats.getNGets());
468         assertEquals(msg, expectCached ? 1 : 0, stats.getNCachedGets());
469     }
470
471     private Database openDb(String JavaDoc name)
472         throws DatabaseException {
473
474         return openDb(name, false);
475     }
476
477     private Database openDb(String JavaDoc name, boolean duplicates)
478         throws DatabaseException {
479
480         DatabaseConfig dbConfig = new DatabaseConfig();
481         dbConfig.setTransactional(isTransactional);
482         dbConfig.setAllowCreate(true);
483         dbConfig.setSortedDuplicates(duplicates);
484
485         Transaction txn = txnBegin();
486         try {
487             return env.openDatabase(txn, name, dbConfig);
488         } finally {
489             txnCommit(txn);
490         }
491     }
492 }
493
Popular Tags