All files / src/storage StorageHierarchy.ts

97% Statements 97/100
87.27% Branches 48/55
100% Functions 32/32
96.93% Lines 95/98

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322  1x 1x   1x         1x                         1x     28x 28x   27x 27x       27x 27x               28x 28x 28x   27x       27x 27x               4x 8x 8x 8x     8x     4x                           16x 16x 3x     13x     12x 5x   7x 7x       1x 1x                                 18x 18x 5x     13x     12x 12x                     1x 1x                               13x 13x 3x     10x     9x 8x     1x     1x 1x                 1x             1x               1x               1x                         1x       81x 27x     54x   54x 54x 54x   54x 54x 50x     54x 54x 54x   54x 27x       54x       47x   47x 1x 46x 10x   36x         54x 4x 2x 2x     2x                 50x 3x 3x 2x     2x   1x 1x           54x 5x 5x   5x                 3x 3x                     8x                
import { IStorageProvider } from './IStorageProvider';
import { AgedCompareFunc, compareAscending, IAgedValue } from '../queue/IAgedQueue';
import { Logger } from '../shared/Logger';
import { IDisposable } from '../shared/IDisposable';
import {
  IStorageHierarchy,
  StorageHierarchyUpdatePolicy,
  IStorageHierarchyWrite,
} from './IStorageHierarchy';
import {
  StorageProviderUpdateHandler,
  isISubscribableStorageProvider,
} from './ISubscribableStorageProvider';
 
type SubscriberUpdateHandler<TKey, TValue> = (
  key: TKey,
  value?: IAgedValue<TValue>
) => Promise<boolean>;
 
/**
 * The default storage hierarchy implementation relying on IStorageProvider for actual data access
 */
export class StorageHierarchy<TKey, TValue>
  implements IStorageHierarchy<TKey, TValue>, IDisposable
{
  readonly totalLevels = this.levels.length;
  readonly isPersistable = this.levels[this.totalLevels - 1].isPersistable;
 
  private readonly logger = Logger.get(StorageHierarchy.name);
  private readonly storageChangedHandlers = new Map<
    number,
    StorageProviderUpdateHandler<TKey, TValue>
  >();
  private readonly pendingUpdates: Set<Promise<void>> = new Set();
  private readonly topLevel = this.levels[this.totalLevels - 1];
  private readonly publishLevel?: number;
 
  /**
   * @param levels The levels in the hierarchy with index 0 being the lowest level (first to read)
   * @param updatePolicy How updates from subscribed higher level storage providers should be handled
   */
  constructor(
    private readonly levels: IStorageProvider<TKey, TValue>[],
    private readonly updatePolicy: StorageHierarchyUpdatePolicy = StorageHierarchyUpdatePolicy.OnlyIfKeyExist,
    private readonly ageCompareFunc: AgedCompareFunc = compareAscending
  ) {
    Iif (levels.length < 2) {
      throw new Error('StorageHierarchy must have at least 2 storage provider');
    }
 
    this.logger.info(`Created storage hierarchy with levels: ${this.totalLevels}`);
    this.publishLevel = this.subscribeAtLevel(this.totalLevels - 1);
  }
 
  /**
   * Clean up the object when it's no longer used. After a dispose(), an object
   * is no longer guaranteed to be usable.
   */
  public dispose(): Promise<void> {
    this.storageChangedHandlers.forEach((handler, level) => {
      const currentLevel = this.levels[level];
      if (isISubscribableStorageProvider(currentLevel)) {
        currentLevel.unsubscribe(handler);
      }
 
      this.storageChangedHandlers.delete(level);
    });
 
    return Promise.all(this.pendingUpdates).then(() => undefined);
  }
 
  /**
   * @param key The key to retrieve
   * @param level The level at which to retrieve the key
   * @param isAscending To go up the hierarchy (true) or down (false) from level
   * @returns The value if it's in the hierarchy from the level going up/down or null
   */
  public getAtLevel(
    key: TKey,
    level?: number,
    isAscending = true
  ): Promise<IAgedValue<TValue> | null> {
    const rLevel = this.getCurrentLevelOrNull(isAscending, level);
    if (rLevel === null) {
      return Promise.resolve(null);
    }
 
    return this.levels[rLevel]
      .get(key)
      .then(agedValue => {
        if (agedValue) {
          return agedValue;
        } else {
          this.logger.debug(`Cache miss: level=${rLevel}, key=${key}`);
          return this.getAtLevel(key, isAscending ? rLevel + 1 : rLevel - 1, isAscending);
        }
      })
      .catch(error => {
        this.logger.debug(`Failed to Get: level=${rLevel}, key=${key}, error=${error}`);
        return this.getAtLevel(key, isAscending ? rLevel + 1 : rLevel - 1, isAscending);
      });
  }
 
  /**
   * @param key The key to set
   * @param value The value to set
   * @param level The level at which to set the key
   * @param isAscending To go up the hierarchy (true) or down (false) from level
   * @returns If the write succeeded to all levels going up/down or the error condition
   */
  public setAtLevel(
    key: TKey,
    value: IAgedValue<TValue>,
    level?: number,
    isAscending = false
  ): Promise<IStorageHierarchyWrite<TValue>> {
    const rLevel = this.getCurrentLevelOrNull(isAscending, level);
    if (rLevel === null) {
      return Promise.resolve(this.getFullWriteStatus(value));
    }
 
    return this.levels[rLevel]
      .set(key, value)
      .then(valueWritten => {
        if (valueWritten !== null) {
          return this.setAtLevel(
            key,
            valueWritten,
            isAscending ? rLevel + 1 : rLevel - 1,
            isAscending
          );
        }
 
        return this.getPartialWriteStatus(isAscending, rLevel, value);
      })
      .catch(error => {
        this.logger.warn(`Error setting: level=${rLevel}, key=${key}, error=${error}`);
        return this.getPartialWriteStatus(isAscending, rLevel, value);
      });
  }
 
  /**
   * @param key The key to delete
   * @param level The level at which to delete the key
   * @param isAscending To go up the hierarchy (true) or down (false) from level
   * @returns If the write succeeded to all levels going up/down or the error condition
   */
  public deleteAtLevel(
    key: TKey,
    level?: number,
    isAscending = false,
    writtenValue?: IAgedValue<TValue>
  ): Promise<IStorageHierarchyWrite<TValue>> {
    const rLevel = this.getCurrentLevelOrNull(isAscending, level);
    if (rLevel === null) {
      return Promise.resolve(this.getFullWriteStatus(writtenValue));
    }
 
    return this.levels[rLevel]
      .delete(key)
      .then(isSuccessful => {
        if (isSuccessful) {
          return this.deleteAtLevel(key, isAscending ? rLevel + 1 : rLevel - 1, isAscending);
        }
 
        return this.getPartialWriteStatus(isAscending, rLevel, writtenValue);
      })
      .catch(error => {
        this.logger.warn(`Error deleting: level=${rLevel}, key=${key}, error=${error}`);
        return this.getPartialWriteStatus(isAscending, rLevel, writtenValue);
      });
  }
 
  /**
   * @param level The level at which to search
   * @return The number of keys at the specified level
   */
  public getSizeAtLevel(level: number): Promise<number> {
    return this.levels[level].size();
  }
 
  /**
   * @returns The keys a the top level (should be all keys across the entire hierarchy)
   */
  public getKeysAtTopLevel(): Promise<TKey[]> {
    return this.topLevel.keys();
  }
 
  /**
   * @param key The key to retrieve
   * @returns The value at the top level only or null
   */
  public getValueAtTopLevel(key: TKey): Promise<IAgedValue<TValue> | null> {
    return this.getAtLevel(key, this.totalLevels - 1);
  }
 
  /**
   * @param key The key to retrieve
   * @returns The value at the bottom level only or null
   */
  public getValueAtBottomLevel(key: TKey): Promise<IAgedValue<TValue> | null> {
    return this.getAtLevel(key, 0, false);
  }
 
  /**
   * Set only the levels below the top level (for refresing from the top level for instance)
   * @param key The key to set
   * @param value The value to set
   * @returns If the write succeeded to all levels going up/down or the error condition
   */
  public setBelowTopLevel(
    key: TKey,
    value: IAgedValue<TValue>
  ): Promise<IStorageHierarchyWrite<TValue>> {
    return this.setAtLevel(key, value, this.totalLevels - 2);
  }
 
  private subscribeAtLevel(level: number, publishLevel?: number): number | undefined {
    if (level <= 0) {
      return publishLevel;
    }
 
    const nextLevel = level - 1;
 
    const currentLevel = this.levels[level];
    if (isISubscribableStorageProvider(currentLevel)) {
      this.logger.debug(`subscribe to level: ${level}`);
 
      let handler = this.getUpdateHandlerAlways(nextLevel);
      if (this.updatePolicy === StorageHierarchyUpdatePolicy.OnlyIfKeyExist) {
        handler = this.getUpdateHandlerOnlyIfKeyExist(nextLevel, handler);
      }
 
      const wrappedHandler = this.getManagedPromiseSubscribe(handler);
      currentLevel.subscribe(wrappedHandler);
      this.storageChangedHandlers.set(level, wrappedHandler);
 
      if (!publishLevel) {
        publishLevel = level;
      }
    }
 
    return this.subscribeAtLevel(nextLevel, publishLevel);
  }
 
  private getCurrentLevelOrNull(isAscending: boolean, level?: number): number | null {
    level = level === undefined ? (isAscending ? 0 : this.totalLevels - 1) : level;
 
    if (isAscending && level >= this.totalLevels) {
      return null;
    } else if (!isAscending && level < 0) {
      return null;
    } else {
      return level;
    }
  }
 
  private getUpdateHandlerAlways(updateLevel: number) {
    return (key: TKey, value?: IAgedValue<TValue>): Promise<boolean> => {
      if (value) {
        return this.setAtLevel(key, value, updateLevel).then(
          s => s.writtenLevels === this.totalLevels
        );
      } else {
        return this.deleteAtLevel(key, updateLevel).then(s => s.writtenLevels === this.totalLevels);
      }
    };
  }
 
  private getUpdateHandlerOnlyIfKeyExist(
    updateLevel: number,
    updateUnconditionally: SubscriberUpdateHandler<TKey, TValue>
  ) {
    return (key: TKey, value?: IAgedValue<TValue>): Promise<boolean> => {
      return this.getAtLevel(key, updateLevel, false).then(agedValue => {
        if (agedValue) {
          Iif (value !== undefined && this.ageCompareFunc(agedValue.age, value.age) >= 0) {
            return Promise.resolve(true);
          }
          return updateUnconditionally(key, value);
        }
        this.logger.debug(`Key doesn't exist, ignoring subscribed update: ${key}`);
        return Promise.resolve(false);
      });
    };
  }
 
  private getManagedPromiseSubscribe(func: SubscriberUpdateHandler<TKey, TValue>) {
    return (key: TKey, value?: IAgedValue<TValue>): void => {
      const promise = func(key, value).then(() => {
        this.pendingUpdates.delete(promise);
      });
      this.pendingUpdates.add(promise);
    };
  }
 
  private getPartialWriteStatus(
    isAscending: boolean,
    level: number,
    value?: IAgedValue<TValue>
  ): IStorageHierarchyWrite<TValue> {
    const writtenLevels = isAscending ? level : this.totalLevels - level - 1;
    return {
      isPersisted: !isAscending && level <= this.totalLevels - 2 && this.topLevel.isPersistable,
      isPublished:
        this.publishLevel !== undefined &&
        (isAscending ? level > this.publishLevel : level < this.publishLevel),
      writtenLevels,
      writtenValue: writtenLevels !== 0 ? value : undefined,
    };
  }
 
  private getFullWriteStatus(value?: IAgedValue<TValue>): IStorageHierarchyWrite<TValue> {
    return {
      isPersisted: this.topLevel.isPersistable,
      isPublished: this.publishLevel !== undefined,
      writtenLevels: this.totalLevels,
      writtenValue: value,
    };
  }
}