posts - 145,comments - 23,views - 73万
复制代码
package com.jiaoyiping.berkeleydb;

import com.sleepycat.je.*;
import com.sleepycat.utilint.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: 焦一平
 * Date: 14-7-9
 * Time: 下午1:27
 * To change this template use File | Settings | File Templates.
 * 1.直接存key和value
 * 2.存储key和value组合成的对象(未使用)
 */
public class SerialNumberGenerator {
    Logger logger = LoggerFactory.getLogger(SerialNumberGenerator.class);
    private static int MAX_VALUE = 9999;
    private static int MIN_VALUE = 1;

    /**
     * 对于任何的请求,总是先取出当前的值返回,然后再将数据库里的值加一
     * @param key 业务主键
     * @return 当前值
     */
    public String getNextValue(String key){
        String currentValue = this.readCurrentValueFromDatabase(key);
        String result;
        if (currentValue == null || "".equals(currentValue)){
            result = this.formatString(MIN_VALUE);
            this.instrtIntoDataBase(key,(MIN_VALUE+1)+"");
            BerkeleyDBUtil.getDatabase().getEnvironment().sync();

        }
        else{
            int intCurrentValue = Integer.parseInt(currentValue);
            result = this.formatString(intCurrentValue);

            //存储下一个值
            String nextValue = "";
            if (intCurrentValue == MAX_VALUE){
                nextValue = MIN_VALUE+"";
            }else {
                nextValue = (intCurrentValue+1) +"";
            }
            this.instrtIntoDataBase(key,nextValue);
            BerkeleyDBUtil.getDatabase().getEnvironment().sync();

        }

        return result;
    }
    public String readCurrentValueFromDatabase(String key){
        DatabaseEntry theKey = new DatabaseEntry(StringUtils.toUTF8(key));
        DatabaseEntry theValue = new DatabaseEntry();
        TransactionConfig txnConfig = new TransactionConfig();
        txnConfig.setSerializableIsolationVoid(true);
        Database database = BerkeleyDBUtil.getDatabase();
        //Transaction txn = database.getEnvironment().beginTransaction(null,txnConfig);
        OperationStatus status = database.get(null,theKey,theValue,LockMode.DEFAULT);
        //对应的键不存在
        if (status == OperationStatus.KEYEMPTY){
            return null;
        }
        else if (status == OperationStatus.SUCCESS){
            byte[] data = theValue.getData();
            String result;
            try {
                result = new String(data,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                result = "";
            }
            return result;
        }


        return null;
    }

    public void instrtIntoDataBase(String key,String value){
        DatabaseEntry theKey = new DatabaseEntry(StringUtils.toUTF8(key));
        DatabaseEntry theValue = new DatabaseEntry(StringUtils.toUTF8(value));
        TransactionConfig txnConfig = new TransactionConfig();
        txnConfig.setSerializableIsolationVoid(true);
        Database database = BerkeleyDBUtil.getDatabase();
        //Transaction txn = database.getEnvironment().beginTransaction(null,txnConfig);
        OperationStatus status = database.put(null,theKey,theValue);
        if (status == OperationStatus.SUCCESS){
            logger.info("保存成功");
        }
//        else if (status == OperationStatus.KEYEXIST){
//            logger.info("");
//        }


    }

    public String formatString(int input){
        String result = "";
        if(input > 1000){
          result = input +"";
        }else{
            int length = (input+"").length();
            if (length == 1){
                result = "000"+input;
            }
            else if (length == 2){
                result = "00"+input;
            }else {
                result = "0"+input;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        SerialNumberGenerator generator = new SerialNumberGenerator();
        String result = generator.getNextValue("jiao");
        System.out.println("======"+result+"======");
    }
}
复制代码

 

posted on   梦中彩虹  阅读(1076)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示