All files / src/resp/command/set sinter-command.ts

18.75% Statements 12/64
0% Branches 0/26
16.66% Functions 1/6
19.04% Lines 12/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 1831x   1x   1x   1x 1x 1x                                                                                               1x 8x     8x 8x 8x 8x                                                                                                                                                                                                                                              
import {Logger} from "../../../logger";
import {IRequest} from "../../../server/request";
import {DataType} from "../../data/data-type";
import {Database} from "../../data/database";
import {DatabaseValue} from "../../data/database-value";
import {AbstractRedisToken} from "../../protocol/abstract-redis-token";
import {RedisToken} from "../../protocol/redis-token";
import {RedisTokenType} from "../../protocol/redis-token-type";
import {IRespCommand} from "../resp-command";
 
/**
 * ### Available since 1.0.0.
 * ### SINTER key [key ...]
 * Returns the members of the set resulting from the intersection of all the given sets.
 *
 * ### SINTERSTORE destination key [key...]
 * This command is equal to {@link resp/command/set/sinter-command.SInterCommand | SINTER}, but instead of returning the
 * resulting set, it is stored in destination.
 *
 * If destination already exists, it is overwritten
 *
 * ### For example:
 * ```
 * key1 = {a,b,c,d}
 * key2 = {c}
 * key3 = {a,c,e}
 * SINTER key1 key2 key3 = {c}
 * ```
 * Keys that do not exist are considered to be empty sets. With one of the keys being an empty
 * set, the resulting set is also empty (since set intersection with an empty set always results
 * in an empty set).
 * ### Return value
 * Array reply: list with members of the resulting set.
 * ### Examples
 * ```
 * redis> SADD key1 "a"
 * (integer) 1
 * redis> SADD key1 "b"
 * (integer) 1
 * redis> SADD key1 "c"
 * (integer) 1
 * redis> SADD key2 "c"
 * (integer) 1
 * redis> SADD key2 "d"
 * (integer) 1
 * redis> SADD key2 "e"
 * (integer) 1
 * redis> SINTER key1 key2
 * 1) "c"
 * redis>
 * ```
 */
/*
 * NOTE: We don't supply a data type because sinterstore can overwrite the first param
 * even if it is not a SET
 */
export class SInterCommand extends IRespCommand {
  private logger: Logger = new Logger(module.id);
 
  constructor(maxParams: number, minParams: number, name: string) {
      super();
      this.constructor.prototype.maxParams = maxParams;
      this.constructor.prototype.minParams = minParams;
      this.constructor.prototype.name = name;
  }
 
  public execSync(request: IRequest, db: Database): RedisToken {
      this.logger.debug(
          `${request.getCommand()}.execute(%s)`,
          ...request.getParams()
      );
      switch (request.getCommand().toLowerCase()) {
      case "sinterstore":
          const result: RedisToken = this.sinterstore(
              request,
              db
          );
          this.logger.debug(
              "sinterstore result is %s",
              `${result}`
          );
          return result;
          break;
      default:
          return this.sinter(
              request,
              db
          );
      }
  }
 
  private sinter(request: IRequest, db: Database): RedisToken {
      const result: RedisToken[] = this.intersection(
          0,
          request,
          db
      );
      if (result && result.length === 1 && result[0].getType() === RedisTokenType.ERROR) {
          return result[0];
      }
      return RedisToken.array(result);
  }
 
  private sinterstore(request: IRequest, db: Database): RedisToken {
      const result = this.intersection(
          1,
          request,
          db
      );
      this.logger.debug(`sinterstore received ${result.length} result(s)`);
      if (result && result.length === 1 && result[0].getType() === RedisTokenType.ERROR) {
          this.logger.debug(`returning error ${result[0].toString()}`);
          return result[0];
      }
      const newKey: DatabaseValue = new DatabaseValue(
          DataType.SET,
          new Set()
      );
      for (const token of result) {
          const stringRedisToken: AbstractRedisToken<string> = token as AbstractRedisToken<string>;
          newKey.getSet().add(stringRedisToken.getValue());
      }
      db.put(
          request.getParam(0),
          newKey
      );
      return RedisToken.integer(result.length);
  }
 
  private intersection(start: number, request: IRequest, db: Database): RedisToken[] {
      this.logger.debug(
          `intersection start: ${start}, params: "%s"`,
          ...request.getParams()
      );
      const result: RedisToken[] = [],
          skey: string = request.getParam(start);
      if (!db.exists(skey)) {
          return result;
      }
      const dbKey: DatabaseValue = db.get(request.getParam(start));
      if (dbKey.getType() !== DataType.SET) {
          return [RedisToken.error("WRONGTYPE Operation against a key holding the wrong kind of value")];
      }
      const dbKeys: DatabaseValue[] = [];
      for (let index = start + 1; index < request.getParams().length; index++) {
          const dbInter: DatabaseValue = db.get(request.getParam(index));
          if (dbInter && dbInter.getType() === DataType.SET) {
              dbKeys.push(dbInter);
          } else {
              this.logger.debug(
                  "dbInter is %s",
                  dbInter.getString()
              );
              if (!dbInter) {
                  return [];
              }
 
              this.logger.debug("Trying to return redistoken.error");
              return [RedisToken.error("WRONGTYPE Operation against a key holding the wrong kind of value")];
          }
      }
      this.logger.debug(
          "dbKey is %s",
          dbKey.getString()
      );
      dbKey.getSet().forEach((element) => {
          let candidate: any = null;
          for (const key of dbKeys) {
              if (key.getSet().has(element)) {
                  candidate = element;
              } else {
                  candidate = null;
                  break;
              }
          }
          if (candidate) {
              result.push(RedisToken.string(candidate));
          }
      });
      return result;
  }
}