应用程序重启后,Berkeley DB主键序列跳转100

时间:2014-06-17 05:02:47

标签: berkeley-db berkeley-db-je

(我已经在网上某个地方看到了这个问题,但搜索查询取决于&#34; 100&#34;因为搜索词显然不是很有希望的 - 所以如果已经提出这个问题请原谅我< / p>

我刚刚开始使用其DPL功能在Java中使用berkeley DB。 我创建了一个Class&#34; Client&#34;它具有以下主键和序列:

public class Client {

    // primary key
    @PrimaryKey(sequence="Client_ID")
    private long id;
[...]
}

我使用主索引添加了一些示例实体:

clientById = store.getPrimaryIndex(Long.class, Client.class);
clientById.put(client);

我第一次启动应用程序时,一切都还可以。序列从1开始并增加1.下次启动应用程序时,它从101开始(步进仍然是1)而不是继续在4(我有3个样本实体)。 是否有可能以某种方式影响这种行为? 我希望有一个持续的序列。

修改 这是我的测试代码:

public static void main(String[] args) {
        test();
        test();

    }

    public static void test() {
        // create some test clients
        Client c1 = new Client("Client 1", "Cli 1 street 1", null, "55411", "Bingen");
        Client c2 = new Client("Client 2", "Cli 1 street 2", null, "55411", "Bingen");
        Client c3 = new Client("Test Custoamer");

        // create database
        Store store = new Store();
        ClientAccessor ca = new ClientAccessor(store.getStore());

        ca.put(c1);
        ca.put(c2);
        ca.put(c3);

        List<Client> clients = ca.getAll();
        for (Client c : clients) {
            System.out.println(c);
            System.out.println("-------------------");
        }

        store.shutdown();
    }

商店看起来像这样:

public class Store {
    private File dbfile;
    private Environment env;
    private EntityStore store;

    public Store() {
        this(new File(System.getProperty("user.home"), "tmdb"));
    }

    public Store(File dbfile) {
        this.dbfile = dbfile;
        setup();
    }


    public void setup() {
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        envConfig.setTransactional(true);
        env = new Environment(dbfile, envConfig);

        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setAllowCreate(true);
        storeConfig.setTransactional(true);
        store = new EntityStore(env, "TimeManagement", storeConfig);

    }

    public void shutdown() {
        store.close();
        env.close();
    }


    public EntityStore getStore() {
        return store;
    }
}

ClientAccessor是这样的:

public class ClientAccessor {
    private EntityStore store;

    // primary index
    PrimaryIndex<Long, Client> clientById;


    public ClientAccessor(EntityStore store) {
        this.store = store;
        if (store == null)
            throw new IllegalArgumentException("EntityStore can't be null!");
        clientById = store.getPrimaryIndex(Long.class, Client.class);
    }

    public void put(Client c) {
        clientById.put(c);
    }

    public List<Client> getAll() {
        ArrayList<Client> clients = new ArrayList<Client>();
        EntityCursor<Client> cursor = clientById.entities();

        for (Client c : cursor) {
            clients.add(c);
        }

        cursor.close();

        return clients;
    }


}

客户端看起来像这样:

@Entity
public class Client {

    // primary key
    @PrimaryKey(sequence="Client_ID")
    private long id;

    // secondary keys
    @SecondaryKey(relate=Relationship.MANY_TO_ONE)
    private String name;
    @SecondaryKey(relate=Relationship.MANY_TO_ONE)
    private String address1;
    @SecondaryKey(relate=Relationship.MANY_TO_ONE)
    private String address2;
    @SecondaryKey(relate=Relationship.MANY_TO_ONE)
    private String plz;
    @SecondaryKey(relate=Relationship.MANY_TO_ONE)
    private String city;

    private Client(){}

    // address is optional
    public Client(String name) {
        this(name, null, null, null, null);
    }

    public Client(String name, String address1, String address2, String plz, String city) {
        this.setName(name);
        this.setAddress1(address1);
        this.setAddress2(address2);
        this.setPlz(plz);
        this.setCity(city);
    }

    @Override
    public String toString() {
        String str = "";
        str += id + "\n";
        str += name + "\n";
        str += (address1 != null && ! address1.isEmpty()) ? address1 + "\n" : "";
        str += (address2 != null && ! address2.isEmpty()) ? address2 + "\n" : "";
        str += (plz != null && ! plz.isEmpty()) ? plz + " " : "";
        str += (city != null &&! city.isEmpty()) ? city + "\n" : "";

        return str;
    }


    // getters and setters

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getPlz() {
        return plz;
    }

    public void setPlz(String plz) {
        this.plz = plz;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

1 个答案:

答案 0 :(得分:3)

默认情况下,序列缓存100个条目。要更改此设置,请将此代码添加到Store.setup()方法的末尾:

    SequenceConfig sequenceConfig = store.getSequenceConfig("Client_ID");
    sequenceConfig.setCacheSize(1);
    store.setSequenceConfig("Client_ID", sequenceConfig);