本文主要研究一下claudb的hash command
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashSetCommand.java
@Command("hset")
@ParamLength(3)
@ParamType(DataType.HASH)
public class HashSetCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = hash(entry(request.getParam(1), request.getParam(2)));
DatabaseValue resultValue = db.merge(safeKey(request.getParam(0)), value,
(oldValue, newValue) -> {
Map<SafeString, SafeString> merge = new HashMap<>();
merge.putAll(oldValue.getHash().toMap());
merge.putAll(newValue.getHash().toMap());
return hash(ImmutableMap.from(merge));
});
ImmutableMap<SafeString, SafeString> resultMap = resultValue.getHash();
return integer(resultMap.get(request.getParam(1)) == null);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashGetCommand.java
@ReadOnly
@Command("hget")
@ParamLength(2)
@ParamType(DataType.HASH)
public class HashGetCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableMap<SafeString, SafeString> map = db.getHash(request.getParam(0));
return map.get(request.getParam(1))
.map(RedisToken::string)
.getOrElse(RedisToken::nullString);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashGetAllCommand.java
@ReadOnly
@Command("hgetall")
@ParamLength(1)
@ParamType(DataType.HASH)
public class HashGetAllCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = db.get(safeKey(request.getParam(0)));
if (value != null) {
return convert(value);
} else {
return array();
}
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashExistsCommand.java
@ReadOnly
@Command("hexists")
@ParamLength(2)
@ParamType(DataType.HASH)
public class HashExistsCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableMap<SafeString, SafeString> map = db.getHash(request.getParam(0));
return integer(map.containsKey(request.getParam(1)));
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashDeleteCommand.java
@Command("hdel")
@ParamLength(2)
@ParamType(DataType.HASH)
public class HashDeleteCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableList<SafeString> keys = request.getParams().asList().tail();
List<SafeString> removedKeys = new LinkedList<>();
db.merge(safeKey(request.getParam(0)), DatabaseValue.EMPTY_HASH, (oldValue, newValue) -> {
ImmutableMap<SafeString, SafeString> merge = oldValue.getHash();
for (SafeString key : keys) {
merge.get(key).stream().forEach(removedKeys::add);
merge = merge.remove(key);
}
return hash(merge);
});
return integer(!removedKeys.isEmpty());
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashKeysCommand.java
@ReadOnly
@Command("hkeys")
@ParamLength(1)
@ParamType(DataType.HASH)
public class HashKeysCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableMap<SafeString, SafeString> map = db.getHash(request.getParam(0));
return convert(map.keys());
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashLengthCommand.java
@ReadOnly
@Command("hlen")
@ParamLength(1)
@ParamType(DataType.HASH)
public class HashLengthCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableMap<SafeString, SafeString> hash = db.getHash(request.getParam(0));
return integer(hash.size());
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashMultiGetCommand.java
@ReadOnly
@Command("hmget")
@ParamLength(2)
@ParamType(DataType.HASH)
public class HashMultiGetCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableMap<SafeString, SafeString> map = db.getHash(request.getParam(0));
List<RedisToken> rtList = new ArrayList<>();
for (int paramNumber = 1; paramNumber < request.getParams().size(); paramNumber++) {
Option<SafeString> oss = map.get(request.getParam(paramNumber));
rtList.add(oss.map(RedisToken::string).getOrElse(RedisToken::nullString));
}
return convert(rtList);
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashMultiSetCommand.java
@Command("hmset")
@ParamLength(3)
@ParamType(DataType.HASH)
public class HashMultiSetCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
for (int paramNumber = 1; paramNumber < request.getParams().size(); paramNumber += 2) {
SafeString mapKey = request.getParam(paramNumber);
SafeString mapVal = request.getParam(paramNumber + 1);
DatabaseValue value = hash(entry(mapKey, mapVal));
db.merge(safeKey(request.getParam(0)), value,
(oldValue, newValue) -> {
Map<SafeString, SafeString> merge = new HashMap<>();
merge.putAll(oldValue.getHash().toMap());
merge.putAll(newValue.getHash().toMap());
return hash(ImmutableMap.from(merge));
}
);
}
return responseOk();
}
}
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashValuesCommand.java
@ReadOnly
@Command("hvals")
@ParamLength(1)
@ParamType(DataType.HASH)
public class HashValuesCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableMap<SafeString, SafeString> map = db.getHash(request.getParam(0));
return convert(map.values());
}
}
claudb hash相关的command有HashSetCommand、HashGetCommand、HashGetAllCommand、HashExistsCommand、HashDeleteCommand、HashKeysCommand、HashLengthCommand、HashMultiGetCommand、HashMultiSetCommand、HashValuesCommand
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。