2022-01-31 20:28:38 +01:00
|
|
|
// Client-side tag completion.
|
2024-08-13 23:16:47 +02:00
|
|
|
import { UniqueHeap } from './unique-heap';
|
2022-01-20 23:37:05 +01:00
|
|
|
import store from './store';
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
export interface Result {
|
|
|
|
aliasName: string;
|
2022-03-25 23:55:47 +01:00
|
|
|
name: string;
|
|
|
|
imageCount: number;
|
|
|
|
associations: number[];
|
|
|
|
}
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
/**
|
|
|
|
* Returns whether Result a is considered less than Result b.
|
|
|
|
*/
|
|
|
|
function compareResult(a: Result, b: Result): boolean {
|
|
|
|
return a.imageCount === b.imageCount ? a.name > b.name : a.imageCount < b.imageCount;
|
|
|
|
}
|
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
/**
|
|
|
|
* Compare two strings, C-style.
|
|
|
|
*/
|
2022-04-17 14:42:13 +02:00
|
|
|
function strcmp(a: string, b: string): number {
|
2021-12-30 01:52:15 +01:00
|
|
|
return a < b ? -1 : Number(a > b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of a tag without any namespace component.
|
|
|
|
*/
|
2022-03-25 23:55:47 +01:00
|
|
|
function nameInNamespace(s: string): string {
|
2024-08-13 23:16:47 +02:00
|
|
|
const first = s.indexOf(':');
|
|
|
|
|
|
|
|
if (first !== -1) {
|
|
|
|
return s.slice(first + 1);
|
|
|
|
}
|
2021-12-30 01:52:15 +01:00
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
return s;
|
2021-12-30 01:52:15 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-25 23:55:47 +01:00
|
|
|
private data: Uint8Array;
|
|
|
|
private view: DataView;
|
|
|
|
private decoder: TextDecoder;
|
|
|
|
private numTags: number;
|
|
|
|
private referenceStart: number;
|
|
|
|
private secondaryStart: number;
|
|
|
|
private formatVersion: number;
|
|
|
|
|
2021-12-27 01:16:21 +01:00
|
|
|
/**
|
|
|
|
* Build a new local autocompleter.
|
|
|
|
*/
|
2022-03-25 23:55:47 +01:00
|
|
|
constructor(backingStore: ArrayBuffer) {
|
2021-12-27 01:16:21 +01:00
|
|
|
this.data = new Uint8Array(backingStore);
|
|
|
|
this.view = new DataView(backingStore);
|
|
|
|
this.decoder = new TextDecoder();
|
|
|
|
this.numTags = this.view.getUint32(backingStore.byteLength - 4, true);
|
|
|
|
this.referenceStart = this.view.getUint32(backingStore.byteLength - 8, true);
|
2021-12-30 01:52:15 +01:00
|
|
|
this.secondaryStart = this.referenceStart + 8 * this.numTags;
|
|
|
|
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.
|
|
|
|
*/
|
2024-08-13 23:16:47 +02:00
|
|
|
private getTagFromLocation(location: number, imageCount: number, aliasName?: string): Result {
|
2021-12-27 01:16:21 +01:00
|
|
|
const nameLength = this.view.getUint8(location);
|
|
|
|
const assnLength = this.view.getUint8(location + 1 + nameLength);
|
|
|
|
|
2024-04-16 02:36:57 +02:00
|
|
|
const associations: number[] = [];
|
2021-12-27 01:16:21 +01:00
|
|
|
const name = this.decoder.decode(this.data.slice(location + 1, location + nameLength + 1));
|
|
|
|
|
|
|
|
for (let i = 0; i < assnLength; i++) {
|
2021-12-30 04:15:14 +01:00
|
|
|
associations.push(this.view.getUint32(location + 1 + nameLength + 1 + i * 4, true));
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
return { aliasName: aliasName || name, name, imageCount, associations };
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Result object as the ith tag inside the file.
|
|
|
|
*/
|
2024-08-13 23:16:47 +02:00
|
|
|
private getResultAt(i: number, aliasName?: string): Result {
|
|
|
|
const tagLocation = 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);
|
2024-08-13 23:16:47 +02:00
|
|
|
const result = this.getTagFromLocation(tagLocation, imageCount, aliasName);
|
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
|
2024-08-13 23:16:47 +02:00
|
|
|
return this.getResultAt(-imageCount - 1, aliasName || result.name);
|
2021-12-28 00:19:08 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
return result;
|
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.
|
|
|
|
*/
|
2024-08-13 23:16:47 +02:00
|
|
|
private getSecondaryResultAt(i: number): Result {
|
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
|
|
|
*/
|
2024-08-13 23:16:47 +02:00
|
|
|
private scanResults(
|
|
|
|
getResult: (i: number) => Result,
|
2024-07-04 02:27:59 +02:00
|
|
|
compare: (name: string) => number,
|
2024-08-13 23:16:47 +02:00
|
|
|
results: UniqueHeap<Result>,
|
|
|
|
hiddenTags: Set<number>,
|
2024-07-04 02:27:59 +02:00
|
|
|
) {
|
2024-08-13 23:16:47 +02:00
|
|
|
const filter = !store.get('unfilter_tag_suggestions');
|
2022-01-20 23:37:05 +01:00
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
let min = 0;
|
|
|
|
let max = this.numTags;
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
while (min < max - 1) {
|
2024-08-13 23:16:47 +02:00
|
|
|
const med = min + (((max - min) / 2) | 0);
|
|
|
|
const result = getResult(med);
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
if (compare(result.aliasName) >= 0) {
|
2021-12-27 01:16:21 +01:00
|
|
|
// too large, go left
|
2021-12-30 01:52:15 +01:00
|
|
|
max = med;
|
2024-07-04 02:27:59 +02:00
|
|
|
} else {
|
2021-12-27 01:16:21 +01:00
|
|
|
// 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
|
2024-08-13 23:16:47 +02:00
|
|
|
outer: while (min < this.numTags - 1) {
|
|
|
|
const result = getResult(++min);
|
|
|
|
|
|
|
|
if (compare(result.aliasName) !== 0) {
|
2021-12-27 01:16:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
// Check if any associations are filtered
|
|
|
|
if (filter) {
|
|
|
|
for (const association of result.associations) {
|
|
|
|
if (hiddenTags.has(association)) {
|
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
2024-08-13 23:16:47 +02:00
|
|
|
|
|
|
|
// Nothing was filtered, so add
|
|
|
|
results.append(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.
|
|
|
|
*/
|
2024-08-13 23:16:47 +02:00
|
|
|
matchPrefix(prefix: string): UniqueHeap<Result> {
|
|
|
|
const results = new UniqueHeap<Result>(compareResult, 'name');
|
2021-12-28 00:19:08 +01:00
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
if (prefix === '') {
|
2024-08-13 23:16:47 +02:00
|
|
|
return results;
|
2021-12-28 00:19:08 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
const hiddenTags = new Set(window.booru.hiddenTagList);
|
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
// Find normally, in full name-sorted order
|
2022-03-25 23:55:47 +01:00
|
|
|
const prefixMatch = (name: string) => strcmp(name.slice(0, prefix.length), prefix);
|
2024-08-13 23:16:47 +02:00
|
|
|
this.scanResults(this.getResultAt.bind(this), prefixMatch, results, hiddenTags);
|
2021-12-28 00:19:08 +01:00
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
// Find in secondary order
|
2022-03-25 23:55:47 +01:00
|
|
|
const namespaceMatch = (name: string) => strcmp(nameInNamespace(name).slice(0, prefix.length), prefix);
|
2024-08-13 23:16:47 +02:00
|
|
|
this.scanResults(this.getSecondaryResultAt.bind(this), namespaceMatch, results, hiddenTags);
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-13 23:16:47 +02:00
|
|
|
return results;
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
|
|
|
}
|