Search code examples
javadatabasesequenceberkeley-dbberkeley-db-je

Creating a sequence manually in BerkeleyDB JE


I want to create a sequence in BerkeleyDB, that I can operate manually, but I don't know how to do this. I would like to have something similar to an SQL sequence object. I have found a class in the API docs, but it's unclear how to create one.

Any help is greatly appreciated!


Solution

  • The code below works fine:

    @Test
    public void testSequenceCreation() throws ClassNotFoundException {
        EnvironmentConfig econf = EnvironmentConfig.DEFAULT.setAllowCreate(true);
        Environment env = new Environment(envHome, econf);
    
        StoreConfig sconf = StoreConfig.DEFAULT.setAllowCreate(true);
        EntityStore store = new EntityStore(env, "TestStore", sconf);
    
        store.setPrimaryConfig(FakeEntity.class, 
                DatabaseConfig.DEFAULT.setAllowCreate(true));
        store.setSequenceConfig("testSequence", SequenceConfig.DEFAULT.setAllowCreate(true));
    
        Sequence seq = store.getSequence("testSequence");
        Assert.assertEquals(0, seq.get(null, 1));
        Assert.assertEquals(1, seq.get(null, 1));
        Assert.assertEquals(2, seq.get(null, 1));
    
        store.sync();
    
        seq.close();
        store.close();
        env.close();        
    }
    

    All I had to do is set a configuration, and the sequence was created.