philomena/assets/js/utils/local-autocompleter.js

193 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-12-27 01:16:21 +01:00
//@ts-check
/*
* Client-side tag completion.
*/
/**
* @typedef {object} Result
* @property {string} name
* @property {number} imageCount
* @property {number[]} associations
*/
2021-12-30 01:52:15 +01:00
/**
* Compare two strings, C-style.
*
* @param {string} a
* @param {string} b
* @returns {number}
*/
function strcmp(a, b) {
return a < b ? -1 : Number(a > b);
}
/**
* Returns the name of a tag without any namespace component.
*
* @param {string} s
* @returns {string}
*/
function nameInNamespace(s) {
const v = s.split(':', 2);
if (v.length === 2) return v[1];
return v[0];
}
2021-12-27 01:16:21 +01:00
/**
* See lib/philomena/autocomplete.ex for binary structure details.
*
* A binary blob is used to avoid the creation of large amounts of garbage on
* the JS heap and speed up the execution of the search.
*/
export class LocalAutocompleter {
/**
* Build a new local autocompleter.
*
* @param {ArrayBuffer} backingStore
*/
constructor(backingStore) {
/** @type {Uint8Array} */
this.data = new Uint8Array(backingStore);
/** @type {DataView} */
this.view = new DataView(backingStore);
/** @type {TextDecoder} */
this.decoder = new TextDecoder();
/** @type {number} */
this.numTags = this.view.getUint32(backingStore.byteLength - 4, true);
/** @type {number} */
this.referenceStart = this.view.getUint32(backingStore.byteLength - 8, true);
/** @type {number} */
2021-12-30 01:52:15 +01:00
this.secondaryStart = this.referenceStart + 8 * this.numTags;
2021-12-28 00:19:08 +01:00
/** @type {number} */
2021-12-30 01:52:15 +01:00
this.formatVersion = this.view.getUint32(backingStore.byteLength - 12, true);
2021-12-27 01:16:21 +01:00
2021-12-28 00:19:08 +01:00
if (this.formatVersion !== 2) {
2021-12-27 01:16:21 +01:00
throw new Error('Incompatible autocomplete format version');
}
}
/**
* Get a tag's name and its associations given a byte location inside the file.
*
* @param {number} location
* @returns {[string, number[]]}
*/
getTagFromLocation(location) {
const nameLength = this.view.getUint8(location);
const assnLength = this.view.getUint8(location + 1 + nameLength);
/** @type {number[]} */
const associations = [];
const name = this.decoder.decode(this.data.slice(location + 1, location + nameLength + 1));
for (let i = 0; i < assnLength; i++) {
associations.push(this.view.getUint32(location + 1 + nameLength + i * 4, true));
}
return [ name, associations ];
}
/**
* Get a Result object as the ith tag inside the file.
*
* @param {number} i
2021-12-30 02:08:41 +01:00
* @returns {[string, Result]}
2021-12-27 01:16:21 +01:00
*/
getResultAt(i) {
const nameLocation = this.view.getUint32(this.referenceStart + i * 8, true);
2021-12-30 01:52:15 +01:00
const imageCount = this.view.getInt32(this.referenceStart + i * 8 + 4, true);
2021-12-30 02:08:41 +01:00
const [ name, associations ] = this.getTagFromLocation(nameLocation);
2021-12-28 00:19:08 +01:00
2021-12-30 01:52:15 +01:00
if (imageCount < 0) {
2021-12-28 00:19:08 +01:00
// This is actually an alias, so follow it
2021-12-30 02:08:41 +01:00
return [ name, this.getResultAt(-imageCount)[1] ];
2021-12-28 00:19:08 +01:00
}
2021-12-30 02:08:41 +01:00
return [ name, { name, imageCount, associations } ];
2021-12-27 01:16:21 +01:00
}
2021-12-28 00:19:08 +01:00
/**
* Get a Result object as the ith tag inside the file, secondary ordering.
*
* @param {number} i
2021-12-30 02:08:41 +01:00
* @returns {[string, Result]}
2021-12-28 00:19:08 +01:00
*/
getSecondaryResultAt(i) {
2021-12-30 01:52:15 +01:00
const referenceIndex = this.view.getUint32(this.secondaryStart + i * 4, true);
2021-12-28 00:19:08 +01:00
return this.getResultAt(referenceIndex);
}
/**
2021-12-30 01:52:15 +01:00
* Perform a binary search to fetch all results matching a condition.
2021-12-28 00:19:08 +01:00
*
2021-12-30 02:08:41 +01:00
* @param {(i: number) => [string, Result]} getResult
2021-12-30 01:52:15 +01:00
* @param {(name: string) => number} compare
* @param {{[key: string]: Result}} results
2021-12-28 00:19:08 +01:00
*/
2021-12-30 01:52:15 +01:00
scanResults(getResult, compare, results) {
let min = 0;
let max = this.numTags;
2021-12-27 01:16:21 +01:00
/** @type {number[]} */
//@ts-expect-error No type for window.booru yet
const hiddenTags = window.booru.hiddenTagList;
2021-12-30 01:52:15 +01:00
while (min < max - 1) {
const med = (min + (max - min) / 2) | 0;
2021-12-30 02:08:41 +01:00
const sortKey = getResult(med)[0];
2021-12-27 01:16:21 +01:00
2021-12-30 02:08:41 +01:00
if (compare(sortKey) >= 0) {
2021-12-27 01:16:21 +01:00
// too large, go left
2021-12-30 01:52:15 +01:00
max = med;
2021-12-27 01:16:21 +01:00
}
else {
// too small, go right
2021-12-30 01:52:15 +01:00
min = med;
2021-12-27 01:16:21 +01:00
}
}
// Scan forward until no more matches occur
2021-12-30 01:52:15 +01:00
while (min < this.numTags - 1) {
2021-12-30 02:08:41 +01:00
const [ sortKey, result ] = getResult(++min);
if (compare(sortKey) !== 0) {
2021-12-27 01:16:21 +01:00
break;
}
// Add if no associations are filtered
if (hiddenTags.findIndex(ht => result.associations.includes(ht)) === -1) {
2021-12-30 01:52:15 +01:00
results[result.name] = result;
2021-12-27 01:16:21 +01:00
}
}
2021-12-30 01:52:15 +01:00
}
2021-12-27 01:16:21 +01:00
2021-12-30 01:52:15 +01:00
/**
* Find the top k results by image count which match the given string prefix.
*
* @param {string} prefix
* @param {number} k
* @returns {Result[]}
*/
topK(prefix, k) {
/** @type {{[key: string]: Result}} */
const results = {};
2021-12-28 00:19:08 +01:00
2021-12-30 01:52:15 +01:00
if (prefix === '') {
return [];
2021-12-28 00:19:08 +01:00
}
2021-12-30 01:52:15 +01:00
// Find normally, in full name-sorted order
const prefixMatch = (/** @type {string} */ name) => strcmp(name.slice(0, prefix.length), prefix);
this.scanResults(this.getResultAt.bind(this), prefixMatch, results);
2021-12-28 00:19:08 +01:00
2021-12-30 01:52:15 +01:00
// Find in secondary order
const namespaceMatch = (/** @type {string} */ name) => strcmp(nameInNamespace(name).slice(0, prefix.length), prefix);
this.scanResults(this.getSecondaryResultAt.bind(this), namespaceMatch, results);
2021-12-28 00:19:08 +01:00
2021-12-27 01:16:21 +01:00
// Sort results by image count
2021-12-30 01:52:15 +01:00
const sorted = Object.values(results).sort((a, b) => b.imageCount - a.imageCount);
2021-12-27 01:16:21 +01:00
2021-12-30 01:52:15 +01:00
return sorted.slice(0, k);
2021-12-27 01:16:21 +01:00
}
}