UNPKG

@aws-amplify/core

Version:
163 lines (161 loc) 4.48 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheList = void 0; const errorHelpers_1 = require("./errorHelpers"); class DoubleLinkedNode { constructor(keyVal) { this.key = keyVal || ''; this.prevNode = null; this.nextNode = null; } } /** * double linked list plus a hash table inside * each key in the cache stored as a node in the list * recently visited node will be rotated to the head * so the Last Recently Visited node will be at the tail * * @member head - dummy head of the linked list * @member tail - dummy tail of the linked list * @member hashtable - the hashtable which maps cache key to list node * @member length - length of the list */ class CacheList { /** * initialization */ constructor() { this.head = new DoubleLinkedNode(); this.tail = new DoubleLinkedNode(); this.hashtable = {}; this.length = 0; this.head.nextNode = this.tail; this.tail.prevNode = this.head; } /** * insert node to the head of the list * * @param node */ insertNodeToHead(node) { const tmp = this.head.nextNode; this.head.nextNode = node; node.nextNode = tmp; node.prevNode = this.head; (0, errorHelpers_1.assert)(tmp !== null, errorHelpers_1.CacheErrorCode.NullPreviousNode); tmp.prevNode = node; this.length = this.length + 1; } /** * remove node * * @param node */ removeNode(node) { (0, errorHelpers_1.assert)(node.prevNode !== null, errorHelpers_1.CacheErrorCode.NullPreviousNode); (0, errorHelpers_1.assert)(node.nextNode !== null, errorHelpers_1.CacheErrorCode.NullNextNode); node.prevNode.nextNode = node.nextNode; node.nextNode.prevNode = node.prevNode; node.prevNode = null; node.nextNode = null; this.length = this.length - 1; } /** * @return true if list is empty */ isEmpty() { return this.length === 0; } /** * refresh node so it is rotated to the head * * @param key - key of the node */ refresh(key) { const node = this.hashtable[key]; this.removeNode(node); this.insertNodeToHead(node); } /** * insert new node to the head and add it in the hashtable * * @param key - the key of the node */ insertItem(key) { const node = new DoubleLinkedNode(key); this.hashtable[key] = node; this.insertNodeToHead(node); } /** * @return the LAST Recently Visited key */ getLastItem() { (0, errorHelpers_1.assert)(this.tail.prevNode !== null, errorHelpers_1.CacheErrorCode.NullPreviousNode); return this.tail.prevNode.key; } /** * remove the cache key from the list and hashtable * @param key - the key of the node */ removeItem(key) { const removedItem = this.hashtable[key]; this.removeNode(removedItem); delete this.hashtable[key]; } /** * @return length of the list */ getSize() { return this.length; } /** * @return true if the key is in the hashtable * @param key */ containsKey(key) { return key in this.hashtable; } /** * clean up the list and hashtable */ clearList() { for (const key of Object.keys(this.hashtable)) { if (Object.prototype.hasOwnProperty.call(this.hashtable, key)) { delete this.hashtable[key]; } } this.head.nextNode = this.tail; this.tail.prevNode = this.head; this.length = 0; } /** * @return all keys in the hashtable */ getKeys() { return Object.keys(this.hashtable); } /** * mainly for test * * @param key * @return true if key is the head node */ isHeadNode(key) { const node = this.hashtable[key]; return node.prevNode === this.head; } /** * mainly for test * * @param key * @return true if key is the tail node */ isTailNode(key) { const node = this.hashtable[key]; return node.nextNode === this.tail; } } exports.CacheList = CacheList; //# sourceMappingURL=CacheList.js.map