前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊claudb的hash command

聊聊claudb的hash command

原创
作者头像
code4it
修改2020-09-02 10:17:29
2390
修改2020-09-02 10:17:29
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下claudb的hash command

HashSetCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashSetCommand.java

代码语言:javascript
复制
@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);
  }
}
  • HashSetCommand实现了DBCommand接口,其execute方法先获取DatabaseValue,然后执行db.merge,先添加oldValue.getHash().toMap()再添加newValue.getHash().toMap()

HashGetCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashGetCommand.java

代码语言:javascript
复制
@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);
  }
}
  • HashGetCommand实现了DBCommand接口,其execute先获取map,然后在从map取出指定key的值

HashGetAllCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashGetAllCommand.java

代码语言:javascript
复制
@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();
    }
  }
}
  • HashGetAllCommand实现了DBCommand接口,其execute方法获取DatabaseValue,然后convert一下返回

HashExistsCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashExistsCommand.java

代码语言:javascript
复制
@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)));
  }
}
  • HashExistsCommand实现了DBCommand接口,其execute方法先获取db.getHash(request.getParam(0)),然后再通过map.containsKey(request.getParam(1))判断是否存在

HashDeleteCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashDeleteCommand.java

代码语言:javascript
复制
@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());
  }
}
  • HashDeleteCommand实现了DBCommand接口,其execute方法先从请求参数提取keys,然后执行db.merge,该方法先获取oldValue.getHash(),然后遍历keys挨个remove

HashKeysCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashKeysCommand.java

代码语言:javascript
复制
@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());
  }
}
  • HashKeysCommand实现了DBCommand接口,其execute方法执行db.getHash(request.getParam(0)),然后返回map.keys()

HashLengthCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashLengthCommand.java

代码语言:javascript
复制
@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());
  }
}
  • HashLengthCommand实现了DBCommand接口,其execute方法先获取db.getHash(request.getParam(0)),然后返回hash.size()

HashMultiGetCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashMultiGetCommand.java

代码语言:javascript
复制
@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);
  }
​
}
  • HashMultiGetCommand实现了DBCommand接口,其execute方法先获取db.getHash(request.getParam(0)),然后遍历request.getParams(),挨个取出指定key的元素添加到rtList

HashMultiSetCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashMultiSetCommand.java

代码语言:javascript
复制
@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();
  }
}
  • HashMultiSetCommand实现了DBCommand接口,其execute方法遍历request.getParams(),挨个执行hash(entry(mapKey, mapVal)),然后在db.merge里头添加进去

HashValuesCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/hash/HashValuesCommand.java

代码语言:javascript
复制
@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());
  }
}
  • HashValuesCommand实现了DBCommand接口,其execute方法先获取db.getHash(request.getParam(0)),然后返回map.values()

小结

claudb hash相关的command有HashSetCommand、HashGetCommand、HashGetAllCommand、HashExistsCommand、HashDeleteCommand、HashKeysCommand、HashLengthCommand、HashMultiGetCommand、HashMultiSetCommand、HashValuesCommand

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HashSetCommand
  • HashGetCommand
  • HashGetAllCommand
  • HashExistsCommand
  • HashDeleteCommand
  • HashKeysCommand
  • HashLengthCommand
  • HashMultiGetCommand
  • HashMultiSetCommand
  • HashValuesCommand
  • 小结
  • doc
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档