自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Berkeley DB使用SecondKey給數(shù)據(jù)排序的實現(xiàn)方法

數(shù)據(jù)庫 其他數(shù)據(jù)庫
本文我們主要介紹了Berkeley DB使用SecondKey給數(shù)據(jù)排序的實現(xiàn)方法,希望本次的介紹能夠?qū)δ兴斋@!

Berkeley DB使用SecondKey數(shù)據(jù)排序的實現(xiàn)方法是本文我們主要要介紹的內(nèi)容,在做項目的時候用到了nosql數(shù)據(jù)庫BDB,借此機會研究了一下它的用法。它的官方示例和文檔比較豐富,感覺比較容易學(xué)習(xí)。在開發(fā)過程中出現(xiàn)了一個需求,要把數(shù)據(jù)根據(jù)插入時間遍歷,個人認為通過第二主鍵(SecondKey)比較容易實現(xiàn)。

以下是我的基本實現(xiàn)過程:

1.在ValueBean中加入insertTime屬性

 

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

 

其中的hostName屬性在主從同步和生成插入時間時用到,value屬性就是key-value中的值

2.TupleBinding類

 

  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()];//獲得value長度     
  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. }   

 

此類用于將ValueBean和DatabaseEntry進行轉(zhuǎn)換,兩個方法中的屬性讀寫順序要統(tǒng)一。

3.SecondaryKeyCreator,第二主鍵生成器

 

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

 

指定insertTime屬性作為第二主鍵。

在插入一個新數(shù)據(jù)時生成insertTime十分關(guān)鍵,尤其在高并發(fā)和互為主從同步時極易出現(xiàn)“第二主鍵重復(fù)”的錯誤,造成數(shù)據(jù)無法插入,我了使用當(dāng)前時間毫秒數(shù)+AtomicInteger自增+hostName的asc碼之和,保證insertTime的前后大小順序。

System.currentTimeMillis()*1000000+(add_num.getAndIncrement()%1000)*1000 + host_key

4.創(chuàng)建第二數(shù)據(jù)庫,用于存儲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);   

 

到此,便可以使用SecondaryCursor的getNext()和getPrev()前后遍歷了,getSearchKey()可以找到你想要的位置。

關(guān)于Berkeley DB使用SecondKey給數(shù)據(jù)排序的實現(xiàn)方法的相關(guān)知識就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. Oracle 11g R2數(shù)據(jù)庫示例用戶安裝說明
  2. Oracle 11g數(shù)據(jù)庫審計功能應(yīng)用實例解析
  3. Oracle 11g數(shù)據(jù)庫默認審計選項說明詳解
  4. Oracle 11g數(shù)據(jù)庫使用XML Table的BUG解決
  5. PowerDesigner15連接Oracle出錯的解決方案
責(zé)任編輯:趙鵬 來源: CSDN博客
相關(guān)推薦

2009-08-11 13:35:13

C# Berkeley

2011-03-25 13:27:12

Berkeley DB

2013-07-08 15:29:49

甲骨文AGPL

2010-03-25 10:16:50

Oracle Berk

2010-11-02 11:08:11

DB2循環(huán)查詢

2010-11-03 14:10:23

DB2在線備份

2012-11-12 10:30:25

IBMdw

2009-09-15 16:44:44

Linq排序

2010-11-02 10:16:22

DB2分區(qū)數(shù)據(jù)庫備份

2010-09-30 14:48:26

DB2查詢

2025-02-10 10:54:53

PostgresDBpgvector數(shù)據(jù)庫

2011-03-16 13:02:47

DB2數(shù)據(jù)復(fù)制遷移

2022-09-20 08:49:26

Java8Lambda

2010-11-03 16:32:10

DB2創(chuàng)建數(shù)據(jù)庫

2015-10-23 16:40:21

DB2刪除數(shù)據(jù)

2010-11-03 13:50:49

DB2刪除重復(fù)數(shù)據(jù)

2011-05-24 09:32:38

2010-08-27 14:39:46

db2連接數(shù)據(jù)庫

2010-11-03 11:49:15

刪除DB2數(shù)據(jù)

2010-08-27 11:28:39

DB2shell數(shù)據(jù)庫
點贊
收藏

51CTO技術(shù)棧公眾號