gpt4 book ai didi

berkeley-db - 应用重启后 Berkeley DB 主键序列跳转 100

转载 作者:行者123 更新时间:2023-12-03 06:16:05 26 4
gpt4 key购买 nike

(我已经在网上某个地方看到了这个问题,但是依赖于“100”作为搜索词的搜索查询显然不是一个有希望的查询 - 所以如果这个问题已经被问到,请原谅我)

我刚刚开始使用 Java 中的 berkeley DB 使用其 DPL 功能。我创建了一个“Client”类,它具有以下主键和序列:

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;
}
}

最佳答案

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

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

关于berkeley-db - 应用重启后 Berkeley DB 主键序列跳转 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com