UNPKG

timed-kvs

Version:

Lightweight Key-value storage with built-in TTL expiration management

123 lines (122 loc) 2.93 kB
"use strict"; /** * linked-kvs.ts */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LinkedKVS = void 0; class LinkedKVS { constructor() { this.latest = null; this.items = {}; this.length = 0; } get(key) { const item = this.getItem(key); if (item) return item.value; } set(key, value) { this.setItem(key, { value: value }); } getItem(key) { const item = this.items[key]; if (item && !item.deleted) return item; } setItem(key, value) { const item = value; // remove duplicated item this.delete(key); this.items[key] = item; this.length++; item.key = key; // append at the end of the linked list const latest = this.latest; if (latest) { item.next = latest.deleted ? latest.next : latest; } this.latest = item; } size() { return this.length; } /** * restrict maximum number of items * it costs O(n) as parsing whole of items */ shrink(size) { let item = this.latest; while (item) { if (0 >= size) { this.truncate(item); return; } if (!item.deleted) { size--; } item = item.next; // next item } } /** * remove given item */ delete(key) { let item = this.getItem(key); if (item) this._delete(item); } _delete(item) { if (!item) return; if (!item.deleted) { delete this.items[item.key]; this.length--; item.key = item.value = null; item.deleted = true; } // shortcut link let next = item.next; while (next && next.deleted) { next = item.next = next.next; } } /** * remove given item and rest of items */ truncate(value) { let item = value; while (item) { this._delete(item); item = item.next; // next item } } /** * return an array containing all items in proper sequence */ all() { const array = []; let item = this.latest; while (item) { if (!item.deleted) { const it = this.getItem(item.key); if (it) array.push(it); } item = item.next; } return array.reverse(); } /** * return an array containing all keys in proper sequence */ keys() { return this.all().map((item) => item.key); } /** * return an array containing all values in proper sequence */ values() { return this.all().map(item => item.value); } } exports.LinkedKVS = LinkedKVS;