Talk about claudib’s list command

Time:2022-5-1

order

This paper mainly studies the list command of claudib

LeftPushCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/LeftPushCommand.java

@Command("lpush")
@ParamLength(2)
@ParamType(DataType.LIST)
public class LeftPushCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    ImmutableList<SafeString> values = request.getParams().asList().tail().reverse();

    DatabaseValue result = db.merge(safeKey(request.getParam(0)), list(values),
        (oldValue, newValue) -> list(newValue.getList().appendAll(oldValue.getList())));

    return RedisToken.integer(result.size());
  }
}
  • Leftpushcommand implements the dbcommand interface. Its execute method extracts values and then executes dB Merge, at newvalue Add oldvalue after getlist() getList()

LeftPopCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/LeftPopCommand.java

@Command("lpop")
@ParamLength(1)
@ParamType(DataType.LIST)
public class LeftPopCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    List<SafeString> removed = new LinkedList<>();
    db.merge(safeKey(request.getParam(0)), DatabaseValue.EMPTY_LIST,
        (oldValue, newValue) -> {
          ImmutableList<SafeString> list = oldValue.getList();
          list.head().stream().forEach(removed::add);
          return list(list.tail());
        });

    if (removed.isEmpty()) {
      return nullString();
    } else {
      return string(removed.remove(0));
    }
  }
}
  • Leftpopcommand implements the dbcommand interface, and its execute method executes dB Merge, which first gets oldvalue GetList (), then take out the head and add it to the removed, and then pass through list Tail () discards the head as the new result

RightPushCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/RightPushCommand.java

@Command("rpush")
@ParamLength(2)
@ParamType(DataType.LIST)
public class RightPushCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    ImmutableList<SafeString> values = request.getParams().asList().tail();

    DatabaseValue result = db.merge(safeKey(request.getParam(0)), list(values),
        (oldValue, newValue) -> list(oldValue.getList().appendAll(newValue.getList())));

    return integer(result.size());
  }
}
  • Rightpushcommand implements the dbcommand interface. Its execute method extracts values and then executes dB Merge, in oldvalue Append newvalue after getlist() getList()

RightPopCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/RightPopCommand.java

@Command("rpop")
@ParamLength(1)
@ParamType(DataType.LIST)
public class RightPopCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    List<SafeString> removed = new LinkedList<>();
    db.merge(safeKey(request.getParam(0)), DatabaseValue.EMPTY_LIST,
        (oldValue, newValue) -> {
          ImmutableList<SafeString> list = oldValue.getList();
          list.reverse().head().stream().forEach(removed::add);
          return list(list.reverse().tail().reverse());
        });

    if (removed.isEmpty()) {
      return nullString();
    } else {
      return string(removed.remove(0));
    }
  }
}
  • Rightpopcommand implements the dbcommand interface, and its execute method executes dB Merge, which first gets oldvalue Getlist(), then take the head of the countdown of the list and add it to the removed, and then pass through the list reverse(). Tail() discards head and reverses as the new result

ListLengthCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/ListLengthCommand.java

@ReadOnly
@Command("llen")
@ParamLength(1)
@ParamType(DataType.LIST)
public class ListLengthCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    ImmutableList<SafeString> list = db.getList(request.getParam(0));
    return integer(list.size());
  }
}
  • Listlengthcommand implements the dbcommand interface, and its execute method passes dB GetList (request. Getparam (0)) gets the list and returns integer (list. Size())

ListRangeCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/ListRangeCommand.java

@ReadOnly
@Command("lrange")
@ParamLength(3)
@ParamType(DataType.LIST)
public class ListRangeCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    try {
      DatabaseValue value = db.getOrDefault(safeKey(request.getParam(0)), DatabaseValue.EMPTY_LIST);
      ImmutableList<SafeString> list = value.getList();

      int from = Integer.parseInt(request.getParam(1).toString());
      if (from < 0) {
        from = list.size() + from;
      }
      int to = Integer.parseInt(request.getParam(2).toString());
      if (to < 0) {
        to = list.size() + to;
      }

      int min = Math.min(from, to);
      int max = Math.max(from, to);

      // TODO: use Array
      ImmutableList<SafeString> result = ImmutableList.from(list.stream().skip(min).limit((max - min) + 1));

      return convert(result);
    } catch (NumberFormatException e) {
      return error("ERR value is not an integer or out of range");
    }
  }
}
  • Listrangecommand implements the dbcommand interface. Its execute method first obtains the list, then modifies min and Max, and then takes immutablelist from(list.stream(). skip(min). limit((max – min) + 1))

ListIndexCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/ListIndexCommand.java

@ReadOnly
@Command("lindex")
@ParamLength(2)
@ParamType(DataType.LIST)
public class ListIndexCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    try {
      ImmutableList<SafeString> list = db.getList(request.getParam(0));

      int index = Integer.parseInt(request.getParam(1).toString());
      if (index < 0) {
        index = list.size() + index;
      }

      // TODO: fix asArray
      return string(list.asArray().get(index));
    } catch (NumberFormatException e) {
      return error("ERR value is not an integer or out of range");
    } catch (IndexOutOfBoundsException e) {
      return nullString();
    }
  }
}
  • Listindexcommand implements the dbcommand interface. Its execute method first obtains the list and then returns string (list. Asarray() get(index))

ListSetCommand

claudb-1.7.1/src/main/java/com/github/tonivade/claudb/command/list/ListSetCommand.java

@Command("lset")
@ParamLength(3)
@ParamType(DataType.LIST)
public class ListSetCommand implements DBCommand {

  @Override
  public RedisToken execute(Database db, Request request) {
    try {
      int index = Integer.parseInt(request.getParam(1).toString());
      db.merge(safeKey(request.getParam(0)), DatabaseValue.EMPTY_LIST,
          (oldValue, newValue) -> {
            ImmutableList<SafeString> oldList = oldValue.getList();
            // TODO: use Array
            List<SafeString> array = new ArrayList<>(oldList.toList());
            array.set(index > -1 ? index : array.size() + index, request.getParam(2));
            return list(array);
          });
      return status("OK");
    } catch (NumberFormatException e) {
      return error("ERR value is not an integer or out of range");
    } catch (IndexOutOfBoundsException e) {
      return error("ERR index out of range");
    }
  }
}
  • Listsetcommand implements the dbcommand interface. Its execute method reads the index parameter first, and then executes dB Merge, which reads the oldlist and then executes array set(index > -1 ? index : array.size() + index, request. Getparam (2)), and finally return list (array)

Summary

The commands related to claudib list include leftpushcommand, leftpopcommand, rightpushcommand, rightpopcommand, listlengthcommand, listrangecommand, listindexcommand and listsetcommand

doc

  • command/list