Berkeley DB tuple with unknown datatype

245 Views Asked by At

I'm working with a Berkeley database (in Java). I need to read a tuple where the sequence is a string, either an int or long, then another string. How can I determine if the tuple holds an int or a long? Depending on what the tuple holds, I'll do one of the following:

String s1 = input.readString();
int num1 = input.readInt();
String s2= input.readString();

or

String s1 = input.readString();
long num1 = input.readLong();
String s2= input.readString();

The int is 4 bytes and the long is 8 bytes. If the tuple holds an int and I read it in as a long, I get an invalid value as it converts the 4 byte int + 4 bytes of the following string into a long.

1

There are 1 best solutions below

0
On

You may want to ask this question on the Berkeley DB or the Berkeley DB Java Edition forum, depending on which library you are using. Also, you may want to specify which API you are using: key-value pair, Java Collections or Direct Persistence Layer. That will help to better understand if there is any way to determine what the record structure looks like.

For example, if you are using the key-value pair API, there is no "encoding" or "schema" included with the record. It's up to the calling application to know what the structure of the record looks like.

The Java Collections and Direct Persistence Layer APIs do have some notion of what the record looks like. You may find the Java Collections Tutorial useful, as well as the Java examples that are included with the product download.