gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
91 lines • 2.22 kB
JavaScript
import { S2FileStore } from '../file.js';
/**
* # Key-Value File Store
*
* ## Description
* A filesystem key-value store
*
* ## Usage
* ```ts
* import { FileKV } from 'gis-tools-ts/file';
*
* interface Data { name: string };
*
* const kv = new FileKV<Data>('./test.kv');
* // set a key
* kv.set(1n, { name: 'test' });
* // get a key
* const { name } = kv.get(1n); // { name: 'test' }
* // check if a key exists
* kv.has(1n); // true
* // get length of the store
* console.log(kv.length); // 1
*
* // iterate over the store
* for await (const value of kv) console.log(value);
*
* // close the store
* kv.close();
* ```
*/
export class FileKV {
#store;
/**
* Builds a new MultiMap file store
* @param fileName - the path + file name without the extension
*/
constructor(fileName) {
this.#store = new S2FileStore(fileName);
}
/** @returns - the length of the map */
get length() {
return this.#store.length;
}
/**
* Adds a value to the list of values associated with a key
* @param key - the key
* @param value - the value to store
*/
set(key, value) {
this.#store.set(BigInt(key), value);
}
/**
* Check if the key exists
* @param key - the key
* @returns true if the key exists
*/
async has(key) {
return await this.#store.has(BigInt(key));
}
/**
* Gets the list of values associated with a key
* @param key - the key
* @returns the list of values if the map contains values for the key
*/
async get(key) {
const value = await this.#store.get(BigInt(key), 0);
if (value === undefined)
return undefined;
return value[0];
}
/**
* iterate through the values
* @yields {V} - the values iterator
*/
async *values() {
for await (const { value } of this.#store.entries())
yield value;
}
/**
* iterate through the values
* @returns an iterator
*/
[Symbol.asyncIterator]() {
return this.values();
}
/** Closes the store */
close() {
this.#store.close(true);
}
}
//# sourceMappingURL=file.js.map