Skip to Main Content

Berkeley DB Family

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Strange behaviour with custom data comparator

578286May 18 2007 — edited Sep 27 2007
I'll find this result that I cannot understand:
when I put a record with the same key and the same data (a custom comparator return 0) in a database, if there is only one record for a given key the put method correctly overrides the data, if there is more then one record with the same key the put doesn't override the data.

This is an example:

public class DBTest {


private static Environment env;
private static Database store;

/* The comparator */
static class MyComparator implements Comparator,Serializable {

public int compare(Object o1, Object o2) {
byte[] b1=(byte[]) o1;
byte[] b2=(byte[]) o2;

return b1[0]-b2[0];
}

}


/* Used for printing the result */
static String byteview(byte[] b) {
StringBuffer sb=new StringBuffer("[");
for(int i=0;i<b.length;i++) {
if(i>0) sb.append(',');
sb.append(Byte.toString(b[ i]));
}
sb.append(']');
return sb.toString();
}




public static void main(String[] args) {

EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setCacheSize(1000000);

try {
env = new Environment(new File("E:\\Knowledgebase\\DEACache\\"), envConfig);
DatabaseConfig dataConfig = new DatabaseConfig();
dataConfig.setAllowCreate(true);
dataConfig.setSortedDuplicates(true);
dataConfig.setOverrideDuplicateComparator(true);
dataConfig.setDuplicateComparator(new MyComparator());
store = env.openDatabase(null, "_data_", dataConfig);

DatabaseEntry k=new DatabaseEntry();
DatabaseEntry d=new DatabaseEntry();

k.setData(new byte[]{0,1,2});
d.setData(new byte[]{0,0,1,2,3,4});
store.put(null, k, d);

k.setData(new byte[]{0,1,2});
d.setData(new byte[]{0,0,1,4,2,3});
store.put(null, k, d);

k.setData(new byte[]{0,1,2});
d.setData(new byte[]{1,0,10,20});
store.put(null, k, d);

k.setData(new byte[]{0,1,2});
d.setData(new byte[]{1,0,25,35});
store.put(null, k, d);

// print the content of the database (2 record)
Cursor c=store.openCursor(null, null);
c.getFirst(k, d, LockMode.DEFAULT);
System.out.println("key:"+byteview(k.getData())+" data:"+byteview(d.getData()));
while(c.getNext(k, d, LockMode.DEFAULT)==OperationStatus.SUCCESS) {
System.out.println("key:"+byteview(k.getData())+" data:"+byteview(d.getData()));
}
c.close();


k.setData(new byte[]{0,1,2});
d.setData(new byte[]{0,0,4,3,2,1});
store.put(null, k, d);

k.setData(new byte[]{0,1,2});
d.setData(new byte[]{1,0,40,30,20,10});
store.put(null, k, d);

// print the content of the database (2 record)
c=store.openCursor(null, null);
c.getFirst(k, d, LockMode.DEFAULT);
System.out.println("key:"+byteview(k.getData())+" data:"+byteview(d.getData()));
while(c.getNext(k, d, LockMode.DEFAULT)==OperationStatus.SUCCESS) {
System.out.println("key:"+byteview(k.getData())+" data:"+byteview(d.getData()));
}
c.close();

}
catch (DatabaseException e) {
e.printStackTrace();
}


}

}

When I run this program I have those results:

key:[0,1,2] data:[0,0,1,4,2,3] correct, it overrides
key:[0,1,2] data:[1,0,10,20] strange, should be [1,0,25,35]
key:[0,1,2] data:[0,0,1,4,2,3] strange, should be [0,0,4,3,2,1]
key:[0,1,2] data:[1,0,10,20] strange, should be [1,0,40,30,20,10]

Do I have miss somethings?

Thank-you in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 25 2007
Added on May 18 2007
17 comments
5,483 views