« get me outta code hell

search, client: write indexes to individual files - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/search.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-13 12:09:14 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:53 -0300
commit782a498333782d5230ad29390690a94ebc333b13 (patch)
tree66f633ff80488325c26f155f7cb56de78e9f67b2 /src/search.js
parentdb2ebb010aada0739773769cbaf3c34a9763c1fd (diff)
search, client: write indexes to individual files
Diffstat (limited to 'src/search.js')
-rw-r--r--src/search.js58
1 files changed, 44 insertions, 14 deletions
diff --git a/src/search.js b/src/search.js
index 33d5d838..687aa215 100644
--- a/src/search.js
+++ b/src/search.js
@@ -1,11 +1,12 @@
 'use strict';
 
+import {createHash} from 'node:crypto';
 import {mkdir, writeFile} from 'node:fs/promises';
 import * as path from 'node:path';
 
 import FlexSearch from 'flexsearch';
 
-import {logError, logInfo, logWarn} from '#cli';
+import {logWarn} from '#cli';
 import {makeSearchIndex, populateSearchIndex, searchSpec} from '#search-spec';
 import {stitchArrays} from '#sugar';
 import {checkIfImagePathHasCachedThumbnails, getThumbnailEqualOrSmaller}
@@ -15,10 +16,20 @@ async function exportIndexToJSON(index) {
   const results = {};
 
   await index.export((key, data) => {
-    results[key] = data;
-  })
+    if (data === undefined) {
+      return;
+    }
 
-  return results;
+    if (typeof data !== 'string') {
+      logWarn`Got something besides a string from index.export(), skipping:`;
+      console.warn(key, data);
+      return;
+    }
+
+    results[key] = JSON.parse(data);
+  });
+
+  return JSON.stringify(results);
 }
 
 export async function writeSearchData({
@@ -62,21 +73,40 @@ export async function writeSearchData({
   const jsonIndexes =
     await Promise.all(indexes.map(exportIndexToJSON));
 
-  const searchData =
-    Object.fromEntries(
-      stitchArrays({
-        key: keys,
-        value: jsonIndexes,
-      }).map(({key, value}) => [key, value]));
-
   const outputDirectory =
     path.join(wikiCachePath, 'search');
 
-  const outputFile =
+  const mainIndexFile =
     path.join(outputDirectory, 'index.json');
 
+  const mainIndexJSON =
+    JSON.stringify(
+      Object.fromEntries(
+        stitchArrays({
+          key: keys,
+          json: jsonIndexes,
+        }).map(({key, json}) => {
+          const md5 = createHash('md5');
+          md5.write(json);
+
+          const value = {
+            md5: md5.digest('hex'),
+          };
+
+          return [key, value];
+        })));
+
+
   await mkdir(outputDirectory, {recursive: true});
-  await writeFile(outputFile, JSON.stringify(searchData));
 
-  logInfo`Search index successfully written.`;
+  await Promise.all(
+    stitchArrays({
+      key: keys,
+      json: jsonIndexes,
+    }).map(({key, json}) =>
+        writeFile(
+          path.join(outputDirectory, key + '.json'),
+          json)));
+
+  await writeFile(mainIndexFile, mainIndexJSON);
 }