Package com.sleepycat.je

Examples of com.sleepycat.je.Sequence


        if (storeConfig.getReadOnly()) {
            throw new IllegalStateException("Store is read-only");
        }

        Sequence seq = sequenceMap.get(name);
        if (seq == null) {
            if (sequenceDb == null) {
                String[] fileAndDbNames =
                    parseDbName(storePrefix + SEQUENCE_DB);
                DatabaseConfig dbConfig = new DatabaseConfig();
View Full Code Here


        /* Create with legal range values. */

        config.setRange(1, 2);
        config.setInitialValue(1);
        config.setCacheSize(0);
        Sequence seq = db.openSequence(txn, key, config);

        /* Key must not exist if ExclusiveCreate=true. */

        config.setExclusiveCreate(true);
        try {
            db.openSequence(txn, key, config);
            fail();
        } catch (DatabaseException expected) {
            String msg = expected.getMessage();
            assertTrue(msg, msg.indexOf("already exists") >= 0);
        }
        config.setExclusiveCreate(false);
        seq.close();

        /* Key must exist if AllowCreate=false. */

        db.removeSequence(txn, key);
        config.setAllowCreate(false);
        try {
            db.openSequence(txn, key, config);
            fail();
        } catch (DatabaseException expected) {
            String msg = expected.getMessage();
            assertTrue(msg, msg.indexOf("does not exist") >= 0);
        }

        /* Check wrapping not allowed. */

        db.removeSequence(txn, key);
        config.setAllowCreate(true);
        config.setRange(-5, 5);
        config.setInitialValue(-5);
        seq = db.openSequence(txn, key, config);
        for (long i = config.getRangeMin(); i <= config.getRangeMax(); i++) {
            assertEquals(i, seq.get(txn, 1));
        }
        try {
            seq.get(txn, 1);
            fail();
        } catch (DatabaseException expected) {
            String msg = expected.getMessage();
            assertTrue(msg, msg.indexOf("overflow") >= 0);
        }

        /* Check wrapping not allowed, decrement. */

        db.removeSequence(txn, key);
        config.setAllowCreate(true);
        config.setAllowCreate(true);
        config.setRange(-5, 5);
        config.setInitialValue(5);
        config.setDecrement(true);
        seq = db.openSequence(txn, key, config);
        for (long i = config.getRangeMax(); i >= config.getRangeMin(); i--) {
            assertEquals(i, seq.get(txn, 1));
        }
        try {
            seq.get(txn, 1);
            fail();
        } catch (DatabaseException expected) {
            String msg = expected.getMessage();
            assertTrue(msg, msg.indexOf("overflow") >= 0);
        }

        /* Check delta less than one. */
        try {
            seq.get(txn, 0);
            fail();
        } catch (IllegalArgumentException expected) {
            String msg = expected.getMessage();
            assertTrue(msg, msg.indexOf("greater than zero") >= 0);
        }

        /* Check delta greater than range. */
        try {
            seq.get(txn, 11);
            fail();
        } catch (IllegalArgumentException expected) {
            String msg = expected.getMessage();
            assertTrue(msg, msg.indexOf("larger than the range") >= 0);
        }

        seq.close();
        txnCommit(txn);
        db.close();
    }
View Full Code Here

        SequenceConfig config = new SequenceConfig();
        config.setAllowCreate(true);

        Transaction txn = txnBegin();
        Sequence seq = db.openSequence(txn, key, config);
        txnCommit(txn);

        txn = txnBegin();

        /* Check default values before calling get(). */

        SequenceStats stats = seq.getStats(null);
        assertEquals(0, stats.getCurrent());
        assertEquals(0, stats.getCacheSize());
        assertEquals(0, stats.getNGets());
        assertEquals(Long.MIN_VALUE, stats.getMin());
        assertEquals(Long.MAX_VALUE, stats.getMax());

        /* Get the first value. */

        long val = seq.get(txn, 1);
        assertEquals(0, val);
        stats = seq.getStats(null);
        assertEquals(1, stats.getCurrent());
        assertEquals(1, stats.getValue());
        assertEquals(0, stats.getLastValue());
        assertEquals(1, stats.getNGets());

        /* Use deltas greater than one. */

        assertEquals(1, seq.get(txn, 2));
        assertEquals(3, seq.get(txn, 3));
        assertEquals(6, seq.get(txn, 1));
        assertEquals(7, seq.get(txn, 1));

        /* Remove a sequence and expect the key to be deleted. */

        seq.close();
        db.removeSequence(txn, key);
        assertEquals(OperationStatus.NOTFOUND, db.get(txn, key, data, null));
        txnCommit(txn);
        assertEquals(OperationStatus.NOTFOUND, db.get(null, key, data, null));

View Full Code Here

        config.setDecrement(true);
        config.setRange(1, 3);
        config.setInitialValue(3);

        Transaction txn = txnBegin();
        Sequence seq = db.openSequence(txn, key, config);
        assertEquals(3, seq.get(txn, 1));
        txnCommit(txn);

        /* Open another handle on the same sequence -- config should match. */

        txn = txnBegin();
        Sequence seq2 = db.openSequence(txn, key, config);
        assertEquals(2, seq2.get(txn, 1));
        txnCommit(txn);

        SequenceStats stats = seq2.getStats(null);
        assertEquals(1, stats.getCurrent());
        assertEquals(1, stats.getMin());
        assertEquals(3, stats.getMax());

        /* Values are assigned from a single sequence for both handles. */

        assertEquals(1, seq.get(null, 1));

        seq.close();
        seq2.close();
        db.close();
    }
View Full Code Here

                " delta=" + delta +
                " cache=" + cache;

            Transaction txn = txnBegin();
            db.removeSequence(txn, key);
            Sequence seq = db.openSequence(txn, key, config);
            txnCommit(txn);

            txn = txnBegin();

            if (incr) {
                for (long i = min;; i += delta) {

                    boolean expectCached = false;
                    if (cache != 0) {
                        expectCached = delta < cache && i != max &&
                            (((i - min) % cache) != 0);
                    }

                    doOne(msg, seq, txn, delta, i, expectCached);

                    /* Test for end without causing long overflow. */
                    if (i > max - delta) {
                        if (delta == 1) {
                            assertEquals(msg, i, max);
                        }
                        break;
                    }
                }
                if (wrap) {
                    assertEquals(msg, min, seq.get(txn, delta));
                    assertEquals(msg, min + delta, seq.get(txn, delta));
                }
            } else {
                for (long i = max;; i -= delta) {

                    boolean expectCached = false;
                    if (cache != 0) {
                        expectCached = delta < cache && i != min &&
                            (((max - i) % cache) != 0);
                    }

                    doOne(msg, seq, txn, delta, i, expectCached);

                    /* Test for end without causing long overflow. */
                    if (i < min + delta) {
                        if (delta == 1) {
                            assertEquals(msg, i, min);
                        }
                        break;
                    }
                }
                if (wrap) {
                    assertEquals(msg, max, seq.get(txn, delta));
                    assertEquals(msg, max - delta, seq.get(txn, delta));
                }
            }

            if (!wrap) {
                try {
                    seq.get(txn, delta);
                    fail(msg);
                } catch (DatabaseException expected) {
                    String emsg = expected.getMessage();
                    assertTrue(emsg, emsg.indexOf("overflow") >= 0);
                }
            }

            txnCommit(txn);
            seq.close();
        }
    }
View Full Code Here

        /* Create the sequence oject. */
        SequenceConfig config = new SequenceConfig();
        config.setAllowCreate(true);
        DatabaseEntry key =
            new DatabaseEntry(KEY_NAME.getBytes("UTF-8"));
        Sequence seq = db.openSequence(null, key, config);

        /* Allocate a few sequence numbers. */
        for (int i = 0; i < 10; i++) {
            long seqnum = seq.get(null, 1);
            System.out.println("Got sequence number: " + seqnum);
        }

        /* Close all. */
        seq.close();
        db.close();
        env.close();
    }
View Full Code Here

    @Override
    public Sequence openSequence(final Database database,
                                 final DatabaseEntry sequenceKey,
                                 final SequenceConfig sequenceConfig)
    {
        Sequence cachedSequence = _cachedSequences.get(sequenceKey);
        if (cachedSequence == null)
        {
            Sequence handle = database.openSequence(null, sequenceKey, sequenceConfig);
            Sequence existingHandle = _cachedSequences.putIfAbsent(sequenceKey, handle);
            if (existingHandle == null)
            {
                cachedSequence = handle;
            }
            else
View Full Code Here

    }


    private void closeSequence(final DatabaseEntry sequenceKey)
    {
        Sequence cachedHandle = _cachedSequences.remove(sequenceKey);
        if (cachedHandle != null)
        {
            cachedHandle.close();
        }
    }
View Full Code Here

    private void createMessageMetadataSequence(final Database messageMetadataSeqDb,
                                               final long maximumMessageId)
    {
        SequenceConfig sequenceConfig = MESSAGE_METADATA_SEQ_CONFIG.setInitialValue(maximumMessageId + 1);

        Sequence messageMetadataSeq = messageMetadataSeqDb.openSequence(null, MESSAGE_METADATA_SEQ_KEY, sequenceConfig);
        messageMetadataSeq.close();
    }
View Full Code Here

        try
        {
            // The implementations of sequences mean that there is only a transaction
            // after every n sequence values, where n is the MESSAGE_METADATA_SEQ_CONFIG.getCacheSize()

            Sequence mmdSeq = getEnvironmentFacade().openSequence(getMessageMetaDataSeqDb(),
                                                              MESSAGE_METADATA_SEQ_KEY,
                                                              MESSAGE_METADATA_SEQ_CONFIG);
            newMessageId = mmdSeq.get(null, 1);
        }
        catch (DatabaseException de)
        {
            throw getEnvironmentFacade().handleDatabaseException("Cannot get sequence value for new message", de);
        }
View Full Code Here

TOP

Related Classes of com.sleepycat.je.Sequence

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.