[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence的建议 #4

Open
ermao3 opened this issue Mar 17, 2020 · 0 comments
Open

Sequence的建议 #4

ermao3 opened this issue Mar 17, 2020 · 0 comments

Comments

@ermao3
Copy link
ermao3 commented Mar 17, 2020

Sequence 的incr 里面建议换成BigDecimal 这样可以支持几乎无限位自增

public class Sequence {
public final static String HEAD = KeyEnum.SEQ.getKey();
private final static byte[] HEAD_B = HEAD.getBytes();
private final byte[] key_b;
private DB db;
private BigDecimal version;

public synchronized String incr(String increments) throws RocksDBException {
    BigDecimal increment = new BigDecimal(increments);

    if (version == null) {
        byte[] value = db.rocksDB().get(key_b);
        if (value == null) {
            version = BigDecimal.ZERO;
        } else {
            version = new BigDecimal(ArrayKits.bytesToString(value));
        }
    }
    version = version.add(increment);
    db.rocksDB().put(key_b, ArrayKits.convertStringToBytes(version.toPlainString()));

    return version.toPlainString();
}

public String get() {
    return version.toPlainString();
}

public Sequence(DB db, byte[] key) {
    this.db = db;
    this.key_b = ArrayKits.addAll(HEAD_B, key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant