序
本文主要研究一下claudb的set command
SetAddCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetAddCommand.java
@Command("sadd")
@ParamLength(2)
@ParamType(DataType.SET)
public class SetAddCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
List<SafeString> values = request.getParams().stream().skip(1).collect(toList());
DatabaseValue value = db.merge(safeKey(request.getParam(0)), set(values),
(oldValue, newValue) -> set(oldValue.getSet().appendAll(newValue.getSet())));
return integer(value.size());
}
}
- SetAddCommand实现了DBCommand接口,其execute方法先从request参数提取values,然后执行db.merge,使用oldValue.getSet().appendAll(newValue.getSet())
SetMembersCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetMembersCommand.java
@ReadOnly
@Command("smembers")
@ParamLength(1)
@ParamType(DataType.SET)
public class SetMembersCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
DatabaseValue value = db.getOrDefault(safeKey(request.getParam(0)), DatabaseValue.EMPTY_SET);
return convert(value);
}
}
- SetMembersCommand实现了DBCommand接口,其execute方法通过db.getOrDefault获取DatabaseValue
SetCardinalityCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetCardinalityCommand.java
@ReadOnly
@Command("scard")
@ParamLength(1)
@ParamType(DataType.SET)
public class SetCardinalityCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableSet<SafeString> set = db.getSet(request.getParam(0));
return integer(set.size());
}
}
- SetCardinalityCommand实现了DBCommand接口,其execute方法通过db.getSet(request.getParam(0))获取ImmutableSet,然后返回set.size()
SetIsMemberCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetIsMemberCommand.java
@ReadOnly
@Command("sismember")
@ParamLength(2)
@ParamType(DataType.SET)
public class SetIsMemberCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableSet<SafeString> set = db.getSet(request.getParam(0));
return integer(set.contains(request.getParam(1)));
}
}
- SetIsMemberCommand实现了DBCommand接口,其execute方法通过db.getSet(request.getParam(0))获取ImmutableSet,然后再通过set.contains(request.getParam(1))判断是否存在指定元素
SetRemoveCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetRemoveCommand.java
@Command("srem")
@ParamLength(2)
@ParamType(DataType.SET)
public class SetRemoveCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableList<SafeString> items = request.getParams().asList().tail();
List<SafeString> removed = new LinkedList<>();
db.merge(safeKey(request.getParam(0)), DatabaseValue.EMPTY_SET,
(oldValue, newValue) -> {
ImmutableSet<SafeString> oldSet = oldValue.getSet();
oldSet.intersection(items.asSet()).stream().forEach(removed::add);
return set(oldSet.removeAll(items));
});
return integer(removed.size());
}
}
- SetRemoveCommand实现了DBCommand接口,其execute方法先从request参数提取items,然后执行db.merge,使用oldSet.intersection取出交集,然后执行oldSet.removeAll(items)
SetUnionCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetUnionCommand.java
@ReadOnly
@Command("sunion")
@ParamLength(2)
@ParamType(DataType.SET)
public class SetUnionCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableSet<SafeString> result = db.getSet(request.getParam(0));
for (SafeString param : request.getParams().asList().tail()) {
result = result.union(db.getSet(param));
}
return convert(result);
}
}
- SetUnionCommand实现了DBCommand接口,其execute方法先获取ImmutableSet,然后遍历参数值挨个执行result.union(db.getSet(param))
SetIntersectionCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetIntersectionCommand.java
@ReadOnly
@Command("sinter")
@ParamLength(2)
@ParamType(DataType.SET)
public class SetIntersectionCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableSet<SafeString> result = db.getSet(request.getParam(0));
for (SafeString param : request.getParams().asList().tail()) {
result = result.intersection(db.getSet(param));
}
return convert(result);
}
}
- SetIntersectionCommand实现了DBCommand接口,其execute方法先获取ImmutableSet,然后遍历参数值挨个执行result.intersection(db.getSet(param))
SetDifferenceCommand
claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set/SetDifferenceCommand.java
@ReadOnly
@Command("sdiff")
@ParamLength(2)
@ParamType(DataType.SET)
public class SetDifferenceCommand implements DBCommand {
@Override
public RedisToken execute(Database db, Request request) {
ImmutableSet<SafeString> result = db.getSet(request.getParam(0));
for (SafeString param : request.getParams().asList().tail()) {
result = result.difference(db.getSet(param));
}
return convert(result);
}
}
- SetDifferenceCommand实现了DBCommand接口,其execute方法先获取ImmutableSet,然后遍历参数值挨个执行result.difference(db.getSet(param))
小结
claudb set相关的command有SetAddCommand、SetMembersCommand、SetCardinalityCommand、SetIsMemberCommand、SetRemoveCommand、SetUnionCommand、SetIntersectionCommand、SetDifferenceCommand
doc
- [command/set(claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/set)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。