本文主要研究一下claudb的keys command
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/KeysCommand.java
@ReadOnly
@Command("keys")
@ParamLength(1)
public class KeysCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
GlobPattern pattern = createPattern(request.getParam(0));
ImmutableSet<SafeString> keys = db.entrySet()
.filter(matchPattern(pattern))
.filter(filterExpired(Instant.now()).negate())
.map(Tuple2::get1)
.map(DatabaseKey::getValue);
return convert(keys);
}
private GlobPattern createPattern(SafeString param) {
return new GlobPattern(param.toString());
}
private Matcher1<Tuple2<DatabaseKey, DatabaseValue>> filterExpired(Instant now) {
return entry -> entry.get2().isExpired(now);
}
private Matcher1<Tuple2<DatabaseKey, DatabaseValue>> matchPattern(GlobPattern pattern) {
return entry -> pattern.match(entry.get1().toString());
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/DeleteCommand.java
@Command("del")
@ParamLength(1)
public class DeleteCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
int removed = 0;
for (SafeString key : request.getParams()) {
DatabaseValue value = db.remove(safeKey(key));
if (value != null) {
removed += 1;
}
}
return integer(removed);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/ExistsCommand.java
@ReadOnly
@Command("exists")
@ParamLength(1)
public class ExistsCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = db.get(safeKey(request.getParam(0)));
return integer(value != null ? !value.isExpired(Instant.now()) : false);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TypeCommand.java
@ReadOnly
@Command("type")
@ParamLength(1)
public class TypeCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = db.get(safeKey(request.getParam(0)));
if (value != null) {
return status(value.getType().text());
} else {
return status(DataType.NONE.text());
}
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/RenameCommand.java
@Command("rename")
@ParamLength(2)
public class RenameCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
if (db.rename(safeKey(request.getParam(0)), safeKey(request.getParam(1)))) {
return responseOk();
} else {
return error("ERR no such key");
}
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/ExpireCommand.java
@Command("expire")
@ParamLength(2)
public class ExpireCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
try {
DatabaseValue value = db.get(safeKey(request.getParam(0)));
if (value != null) {
db.put(safeKey(request.getParam(0)), value.expiredAt(parsetTtl(request.getParam(1))));
}
return integer(value != null);
} catch (NumberFormatException e) {
return error("ERR value is not an integer or out of range");
}
}
private int parsetTtl(SafeString param) {
return Integer.parseInt(param.toString());
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/PersistCommand.java
@Command("persist")
@ParamLength(1)
public class PersistCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = db.get(safeKey(request.getParam(0)));
if (value != null) {
db.put(safeKey(request.getParam(0)), value.noExpire());
}
return integer(value != null);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TimeToLiveCommand.java
public abstract class TimeToLiveCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = db.get(safeKey(request.getParam(0)));
if (value != null) {
return keyExists(value);
} else {
return notExists();
}
}
protected abstract int timeToLive(DatabaseValue value, Instant now);
private RedisToken keyExists(DatabaseValue value) {
if (value.getExpiredAt() != null) {
return hasExpiredAt(value);
} else {
return integer(-1);
}
}
private RedisToken hasExpiredAt(DatabaseValue value) {
Instant now = Instant.now();
if (!value.isExpired(now)) {
return integer(timeToLive(value, now));
} else {
return notExists();
}
}
private RedisToken notExists() {
return integer(-2);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TimeToLiveMillisCommand.java
@Command("pttl")
@ParamLength(1)
public class TimeToLiveMillisCommand extends TimeToLiveCommand {
@Override
protected int timeToLive(DatabaseValue value, Instant now) {
return (int) value.timeToLiveMillis(now);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/key/TimeToLiveSecondsCommand.java
@Command("ttl")
@ParamLength(1)
public class TimeToLiveSecondsCommand extends TimeToLiveCommand {
@Override
protected int timeToLive(DatabaseValue value, Instant now) {
return value.timeToLiveSeconds(now);
}
}
claudb keys相关的command有KeysCommand、DeleteCommand、ExistsCommand、TypeCommand、RenameCommand、ExpireCommand、PersistCommand、TimeToLiveMillisCommand、TimeToLiveSecondsCommand
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。