Berkeley DB The implementation method of using Secondkey to sort data is the main content of this article, we use the NoSQL database BDB when doing the project, and take this opportunity to study its usage. Its official examples and documents are richer and more easily learned. In the development process, there is a requirement to traverse the data according to the insertion time, which is more easily realized by the second primary key (Secondkey).
Here's my basic implementation process:
1. Add the Inserttime attribute to the Valuebean
- public class valuebean{
- Private String Inserttime;
- Private String HostName;
- Private byte[] value;
- Public String GetHostName () {
- return hostName;
- }
- public void SetHostName (String hostName) {
- This.hostname = HostName;
- }
- Public String Getinserttime () {
- return inserttime;
- }
- public void Setinserttime (String inserttime) {
- This.inserttime = Inserttime;
- }
- Public byte[] GetValue () {
- return value;
- }
- public void SetValue (byte[] value) {
- This.value = value;
- }
- }
Where the Hostname property is used when master-slave synchronization and generation of insert time, the Value property is the Key-value
2.TupleBinding class
- public class Valuebeanbinding extends tuplebinding<Valuebean> {
- @Override
- Public Valuebean entrytoobject (Tupleinput input) {
- String time = input.readstring ();
- String name = input.readstring ();
- byte[] value = new Byte[input.getbufferlength ()-input.getbufferoffset ()];//Get value length
- Input.read (value);
- Valuebean data = new Valuebean ();
- Data.setinserttime (time);
- Data.sethostname (name);
- Data.setvalue (value);
- return data;
- }
- @Override
- public void Objecttoentry (Valuebean object, tupleoutput output) {
- Valuebean value = object;
- Output.writestring (Value.getinserttime ());
- Output.writestring (Value.gethostname ());
- Output.write (Value.getvalue ());
- }
- }
This class is used to convert Valuebean and databaseentry, and the read and write order of the properties in the two methods is uniform.
3.SecondaryKeyCreator, second primary key generator
- public class Secondkeycreator implements secondarykeycreator{
- Private tuplebinding<valuebean> thebinding;
- Secondkeycreator (tuplebinding<valuebean> thebinding) {
- this.thebinding = thebinding;
- }
- @Override
- public boolean Createsecondarykey (Secondarydatabase secondary,
- Databaseentry key, databaseentry data, databaseentry result) {
- Valuebean v =
- (Valuebean) thebinding.entrytoobject (data);
- String time=v.getinserttime ();
- Result.setdata (Time.getbytes ());
- return true;
- }
- }
Specifies the Inserttime property as the second primary key.
It is critical to generate inserttime when inserting a new data, especially when the "second primary key repetition" error is extremely prone to high concurrency and inter-primary synchronization, resulting in data not being inserted, and I have used the current time in milliseconds +atomicinteger the sum of the ASC codes of the self-increment +hostname, Ensure the Inserttime order of the front and rear sizes.
System.currenttimemillis () *1000000+ (Add_num.getandincrement ()%1000) *1000 + Host_key
4. Create a second database for storing Secondkey
- Secondaryconfig mysecconfig = new Secondaryconfig ();
- Mysecconfig.setallowcreate (TRUE);
- Mysecconfig.setsortedduplicates (FALSE);
- Tuplebinding<valuebean> TB =new valuebeanbinding ();
- Secondkeycreator keycreator = new Secondkeycreator (TB);
- Mysecconfig.setkeycreator (Keycreator);
- Mysecconfig.settransactional (Envconfig.gettransactional ());
- String secdbname = "Mysecondarydatabase";
- MYSECDB = myenv.opensecondarydatabase (null, Secdbname, Storedb, mysecconfig);
Here, you can use Secondarycursor's GetNext () and GetPrev () to traverse back and forth, and Getsearchkey () can find the location you want.
About Berkeley DB using Secondkey to sort the data of the implementation of the method to introduce the relevant knowledge here, I hope this introduction can be a harvest for you!
Berkeley DB Implementation method for sorting data using Secondkey