序
本文次要钻研一下 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)