Talk about claudib’s keys command

Time:2022-5-2

order

This paper mainly studies the keys command of claudib

KeysCommand

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());
  }
}
  • Keyscommand implements the dbcommand interface. Its execute method first creates a globpattern through createpattern, and then traverses dB Entryset(), filter out matchpattern, then filter out non expired, and finally return

DeleteCommand

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);
  }
}
  • DeleteCommand implements the dbcommand interface, and its execute method traverses request Getparams() executes dB Remove (safekey (key)), and finally return integer (removed)

ExistsCommand

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);
  }
}
  • Existscommand implements the dbcommand interface, and its execute method passes dB Get (safekey (request. Getparam (0))

TypeCommand

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());
    }
  }
}
  • Typecommand implements the dbcommand interface, and its execute method passes dB Get (safekey (request. Getparam (0))) gets the databasevalue. If it is not null, it returns status (value. Gettype()) Text (), otherwise return status (datatype. None. Text())

RenameCommand

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");
    }
  }
}
  • Renamecommand implements the dbcommand interface, and its execute method executes dB rename

ExpireCommand

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());
  }
}
  • Expircommand implements the dbcommand interface, and its execute method passes dB Get (safekey (request. Getparam (0))) to get databasevalue. If it is not null, execute value Expiredat (parsettl (request. Getparam (1))), and then put it in

PersistCommand

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);
  }
}
  • The persistcommand method implements the dbcommand interface, and its execute method passes dB Get (safekey (request. Getparam (0))) to get databasevalue. If it is not null, execute dB put(safeKey(request.getParam(0)), value. noExpire())

TimeToLiveCommand

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);
  }
}
  • The timetolivecommand declaration implements the dbcommand interface, and its execute interface first passes through dB Get (safekey (request. Getparam (0))) gets the databasevalue. If it is null, it returns notexists. If it is not null, it executes keyexists; The keyexists method is in value Execute hasexpiredat method when getexpiredat() is not null; The hasexpiredat method is in value Integer (timetolive (value, now)) is returned when isexpired (now) is false; Timetolive method is an abstract method and needs subclass implementation

TimeToLiveMillisCommand

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);
  }
}
  • The timetolivecommand.millisecommand method implements the timetolivecommand.millivecommand interface timeToLiveMillis(now)

TimeToLiveSecondsCommand

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);
  }
}
  • Timetolivesecondscommand implements the timetolivecommand interface, and its timetolive method returns value timeToLiveSeconds(now)

Summary

The commands related to claudib keys include keyscommand, DeleteCommand, existscommand, typecommand, renamecommand, expircommand, persistcommand, timetolivemilliscommand, timetolivesecondscommand

doc

  • KeysCommand

Recommended Today

Keywords supported by Spring Data JPA query methods

Keyword Sample JPQL snippet And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2 Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2 Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = 1? Between findByStartDateBetween … where x.startDate between 1? and ?2 LessThan findByAgeLessThan … where x.age < ?1 LessThanEqual findByAgeLessThanEqual … where x.age <= […]