Berkeley DB Implementation method for sorting data using Secondkey

Source: Internet
Author: User
Tags time in milliseconds

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

  1. public class valuebean{
  2. Private String Inserttime;
  3. Private String HostName;
  4. Private byte[] value;
  5. Public String GetHostName () {
  6. return hostName;
  7. }
  8. public void SetHostName (String hostName) {
  9. This.hostname = HostName;
  10. }
  11. Public String Getinserttime () {
  12. return inserttime;
  13. }
  14. public void Setinserttime (String inserttime) {
  15. This.inserttime = Inserttime;
  16. }
  17. Public byte[] GetValue () {
  18. return value;
  19. }
  20. public void SetValue (byte[] value) {
  21. This.value = value;
  22. }
  23. }

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

  1. public class Valuebeanbinding extends tuplebinding<Valuebean> {
  2. @Override
  3. Public Valuebean entrytoobject (Tupleinput input) {
  4. String time = input.readstring ();
  5. String name = input.readstring ();
  6. byte[] value = new Byte[input.getbufferlength ()-input.getbufferoffset ()];//Get value length
  7. Input.read (value);
  8. Valuebean data = new Valuebean ();
  9. Data.setinserttime (time);
  10. Data.sethostname (name);
  11. Data.setvalue (value);
  12. return data;
  13. }
  14. @Override
  15. public void Objecttoentry (Valuebean object, tupleoutput output) {
  16. Valuebean value = object;
  17. Output.writestring (Value.getinserttime ());
  18. Output.writestring (Value.gethostname ());
  19. Output.write (Value.getvalue ());
  20. }
  21. }

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

  1. public class Secondkeycreator implements secondarykeycreator{
  2. Private tuplebinding<valuebean> thebinding;
  3. Secondkeycreator (tuplebinding<valuebean> thebinding) {
  4. this.thebinding = thebinding;
  5. }
  6. @Override
  7. public boolean Createsecondarykey (Secondarydatabase secondary,
  8. Databaseentry key, databaseentry data, databaseentry result) {
  9. Valuebean v =
  10. (Valuebean) thebinding.entrytoobject (data);
  11. String time=v.getinserttime ();
  12. Result.setdata (Time.getbytes ());
  13. return true;
  14. }
  15. }

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

  1. Secondaryconfig mysecconfig = new Secondaryconfig ();
  2. Mysecconfig.setallowcreate (TRUE);
  3. Mysecconfig.setsortedduplicates (FALSE);
  4. Tuplebinding<valuebean> TB =new valuebeanbinding ();
  5. Secondkeycreator keycreator = new Secondkeycreator (TB);
  6. Mysecconfig.setkeycreator (Keycreator);
  7. Mysecconfig.settransactional (Envconfig.gettransactional ());
  8. String secdbname = "Mysecondarydatabase";
  9. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.