refactor(services): use Set instead of Arrays for faster lookups

This commit is contained in:
Panagiotis Papadopoulos 2025-01-02 14:59:26 +01:00
parent 6fe4027a75
commit baea3bdcdd
4 changed files with 11 additions and 11 deletions

View File

@ -9,7 +9,7 @@ import BUILTIN_ATTRIBUTES from "./builtin_attributes.js";
import BNote from "../becca/entities/bnote.js";
import { AttributeRow } from '../becca/entities/rows.js';
const ATTRIBUTE_TYPES = ['label', 'relation'];
const ATTRIBUTE_TYPES = new Set(['label', 'relation']);
function getNotesWithLabel(name: string, value?: string): BNote[] {
const query = attributeFormatter.formatAttrForSearch({type: 'label', name, value}, value !== undefined);
@ -99,7 +99,7 @@ function getAttributeNames(type: string, nameLike: string) {
}
function isAttributeType(type: string): boolean {
return ATTRIBUTE_TYPES.includes(type);
return ATTRIBUTE_TYPES.has(type);
}
function isAttributeDangerous(type: string, name: string): boolean {

View File

@ -13,7 +13,7 @@ import utils from "../../utils.js";
import sql from "../../sql.js";
const ALLOWED_OPERATORS = ['=', '!=', '*=*', '*=', '=*', '%='];
const ALLOWED_OPERATORS = new Set(['=', '!=', '*=*', '*=', '=*', '%=']);
const cachedRegexes: Record<string, RegExp> = {};
@ -50,8 +50,8 @@ class NoteContentFulltextExp extends Expression {
}
execute(inputNoteSet: NoteSet, executionContext: {}, searchContext: SearchContext) {
if (!ALLOWED_OPERATORS.includes(this.operator)) {
searchContext.addError(`Note content can be searched only with operators: ${ALLOWED_OPERATORS.join(", ")}, operator ${this.operator} given.`);
if (!ALLOWED_OPERATORS.has(this.operator)) {
searchContext.addError(`Note content can be searched only with operators: ${Array.from(ALLOWED_OPERATORS).join(", ")}, operator ${this.operator} given.`);
return inputNoteSet;
}

View File

@ -44,7 +44,7 @@ function getFulltext(_tokens: TokenData[], searchContext: SearchContext) {
}
}
const OPERATORS = [
const OPERATORS = new Set([
"=",
"!=",
"*=*",
@ -55,14 +55,14 @@ const OPERATORS = [
"<",
"<=",
"%="
];
]);
function isOperator(token: TokenData) {
if (Array.isArray(token)) {
return false;
}
return OPERATORS.includes(token.token);
return OPERATORS.has(token.token);
}
function getExpression(tokens: TokenData[], searchContext: SearchContext, level = 0) {

View File

@ -152,19 +152,19 @@ function getContentDisposition(filename: string) {
return `file; filename="${sanitizedFilename}"; filename*=UTF-8''${sanitizedFilename}`;
}
const STRING_MIME_TYPES = [
const STRING_MIME_TYPES = new Set([
"application/javascript",
"application/x-javascript",
"application/json",
"application/x-sql",
"image/svg+xml"
];
]);
function isStringNote(type: string | undefined, mime: string) {
// render and book are string note in the sense that they are expected to contain empty string
return (type && ["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas"].includes(type))
|| mime.startsWith('text/')
|| STRING_MIME_TYPES.includes(mime);
|| STRING_MIME_TYPES.has(mime);
}
function quoteRegex(url: string) {