All files / src/resp/data database-value.ts

27.08% Statements 13/48
17.24% Branches 5/29
23.8% Functions 5/21
23.91% Lines 11/46

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 1541x     1x                                           1x   8x 8x                                               28x     28x                     5x     5x                                                                                                                               4x                     33x                  
import {DataType} from "./data-type";
import {SortedSet} from "./sorted-set";
 
export class DatabaseValue {
    public static string(value: string): DatabaseValue {
        return new DatabaseValue(
            DataType.STRING,
            value
        );
    }
 
    public static set(values?: Set<any>): DatabaseValue {
        return new DatabaseValue(
            DataType.SET,
            values
        );
    }
 
    public static zset(values: SortedSet): DatabaseValue {
        return new DatabaseValue(
            DataType.ZSET,
            values
        );
    }
 
  private static WRONG_TYPE: string = "WRONGTYPE Operation against a key holding the wrong kind of value";
 
  constructor(private type: DataType, private value: any, private expiredAt?: number) {
      Iif (!this.type || Object.values(DataType).indexOf(type) === -1) {
          throw new Error(`Cannot use ${type} to initialize DatabaseValue`);
      }
  }
 
  public getSortedSet(): SortedSet {
      if (this.type !== DataType.ZSET) {
          throw new Error(DatabaseValue.WRONG_TYPE);
      }
      return this.getValue();
  }
 
  public getType(): DataType {
      return this.type;
  }
 
  public getString(): string {
      if (this.type !== DataType.STRING) {
          throw new Error(DatabaseValue.WRONG_TYPE);
      }
      return this.getValue();
  }
 
  public getList(): string[] {
      Iif (this.type !== DataType.LIST) {
          throw new Error(DatabaseValue.WRONG_TYPE);
      }
      return this.getValue();
  }
 
  public getSet(): Set<any> {
      if (this.type !== DataType.SET) {
          throw new Error(DatabaseValue.WRONG_TYPE);
      }
      return this.getValue();
  }
 
  public getHash(): any {
      Iif (this.type !== DataType.HASH) {
          throw new Error(DatabaseValue.WRONG_TYPE);
      }
      return this.getValue();
  }
 
  public size(): number {
      switch (this.type) {
      case DataType.STRING:
          return 1;
      case DataType.NONE:
          return 0;
      default:
          return this.value.length;
      }
  }
 
  public getExpiredAt(): number {
      return this.expiredAt
          ? this.expiredAt
          : -1;
  }
 
  public isExpired(now: number): boolean {
      if (this.expiredAt != null) {
          return now > this.expiredAt;
      }
      return false;
  }
 
  public timeToLiveMillis(now: number): number {
      if (this.expiredAt != null) {
          return this.timeToLive(now);
      }
      return -1;
  }
 
  public timeToLiveSeconds(now: number): number {
      if (this.expiredAt != null) {
          /*
           * This method returns the largest (closest to positive infinity) integer value
           * that is less than or equal to the algebraic quotient.
           */
          return parseInt(
              `${this.timeToLive(now) / 1000}`,
              10
          );
      }
      return -1;
  }
 
  public setExpiredAt(ttlMillis: number): DatabaseValue {
      return new DatabaseValue(
          this.type,
          this.value,
          ttlMillis
      );
  }
 
  public noExpire(): DatabaseValue {
      return new DatabaseValue(
          this.type,
          this.value
      );
  }
 
  public toString(): string {
      return `DatabaseValue [type=${this.type}, value=${this.value
      }, expireAt=${this.expiredAt}]`;
  }
 
  private timeToLive(now: number): number {
      return Math.abs(this.expiredAt
          ? now - this.expiredAt
          : 0); // .toMillis();
  }
 
  private getValue(): any {
      return this.value;
  }
 
  private requiredType(type: DataType): void {
      if (this.type !== type) {
          throw new Error(`invalid type: ${type}`);
      }
  }
}