Skip to content

Commit d4a08b1

Browse files
committed
[ multiple accounts ] delay full-time indexing on imported accounts during first-time import
Opening a freshly imported account is a very intensive information as Poncho Wonky loads all the feed. Having that run at the same time as the fts indexing makes it quite slow. If the app detects it is an imported account (a flag in the configuration toml for that account) it will delay the pull stream for 15 minutes, then clear that flag and start indexing.
1 parent bc4e4de commit d4a08b1

2 files changed

Lines changed: 43 additions & 121 deletions

File tree

lib/main-window.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const path = require("path");
2121
const fs = require("fs");
2222
const customScripts = require("./depject/scripts/lua/custom-scripts.js");
2323
const { isFeatureEnabled } = require("../lib/features.js");
24+
const { configurationForIdentity } = require("../lib/identities.js");
2425

2526
const localTimezone = moment.tz.guess();
2627
moment.tz.setDefault(localTimezone);
@@ -94,7 +95,12 @@ module.exports = function (config) {
9495
// that shouldn't cause any problems so this error can be ignored.
9596
}
9697

97-
if (key == null) {
98+
// do not show the edit sheet if the profile was imported
99+
const configuration = configurationForIdentity(api.keys.sync.id());
100+
101+
const importedAccount = configuration?.imported ?? false;
102+
103+
if (key == null && !importedAccount) {
98104
api.profile.sheet.edit({ usePreview: false });
99105
}
100106
});

lib/server-process.js

Lines changed: 36 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { configurationForIdentity, set } = require("./identities.js");
2+
13
const fs = require("fs");
24
const fsp = require("fs").promises;
35
const Path = require("path");
@@ -91,6 +93,18 @@ module.exports = function (ssbConfig) {
9193
"full-text-search.sqlite",
9294
);
9395

96+
// load and start search engine
97+
98+
electron.ipcRenderer.on("search", (ev, terms) => {
99+
electron.ipcRenderer.send("search-unavailable");
100+
});
101+
102+
electron.ipcRenderer.on("is-search-available", (ev, terms) => {
103+
electron.ipcRenderer.send("search-unavailable");
104+
});
105+
106+
electron.ipcRenderer.send("search-unavailable");
107+
94108
const loadOrCreateSqlite = async () => {
95109
console.log("load or create SQLITE");
96110
let indexData;
@@ -118,124 +132,6 @@ module.exports = function (ssbConfig) {
118132
return { database, lastIndexedTimestamp };
119133
};
120134

121-
// const loadOrCreateMiniSearch = async () => {
122-
// let controlData;
123-
// let indexData;
124-
// let miniSearch = new MiniSearch({
125-
// idField: "key",
126-
// fields: ["content"],
127-
// storeFields: ["key", "content", "raw", "timestamp"],
128-
// });
129-
130-
// if (
131-
// fs.existsSync(minisearchControlPath) && fs.existsSync(minisearchIndexPath)
132-
// ) {
133-
// // load previous saved work.
134-
// try {
135-
// console.log(`loading search index data from file...`);
136-
// console.log(
137-
// `\t- control: ${minisearchControlPath}\n\t- index: ${minisearchIndexPath}`,
138-
// );
139-
// controlData = decode(await fsp.readFile(minisearchControlPath));
140-
// console.time("loading file");
141-
// indexData = decode(await fsp.readFile(minisearchIndexPath));
142-
// console.timeEnd("loading file");
143-
// console.time("loadJS");
144-
// miniSearch = await MiniSearch.loadJSAsync(indexData, {
145-
// idField: "key",
146-
// fields: ["content"],
147-
// storeFields: ["key", "content", "raw", "timestamp"],
148-
// });
149-
// console.timeEnd("loadJS");
150-
// console.log(`loaded search index data.`);
151-
// console.log(
152-
// `Last indexed timestamp: ${controlData.lastIndexedTimestamp}`,
153-
// );
154-
// lastIndexedTimestamp = controlData.lastIndexedTimestamp;
155-
// } catch (e) {
156-
// console.log("error loading search data");
157-
// console.log(e);
158-
// }
159-
// }
160-
// return { miniSearch, controlData, indexData };
161-
// };
162-
163-
// const loadMessagesIntoSearchIndex = (
164-
// { miniSearch, controlData, indexData },
165-
// ) => {
166-
// console.log("Starting message indexing pull stream...");
167-
// pull(
168-
// context.sbot.messagesByType({
169-
// type: "post",
170-
// live: true,
171-
// gt: lastIndexedTimestamp,
172-
// }),
173-
// pull.map((m) => {
174-
// // console.log("mapping", m);
175-
// if (m.sync) return m;
176-
// return {
177-
// key: m.key,
178-
// timestamp: m.timestamp,
179-
// content: m.value?.content?.text,
180-
// raw: JSON.stringify(m),
181-
// };
182-
// }),
183-
// pull.drain((m) => {
184-
// if (m.sync) {
185-
// // finished indexing
186-
// console.log(`finished indexing for now ${lastIndexedTimestamp}`);
187-
188-
// controlData = {
189-
// lastIndexedTimestamp,
190-
// };
191-
192-
// indexData = miniSearch.toJSON();
193-
// const encodedIndex = encode(indexData);
194-
// const encodedControl = encode(controlData);
195-
196-
// fs.writeFileSync(minisearchIndexPath, encodedIndex);
197-
// fs.writeFileSync(minisearchControlPath, encodedControl);
198-
199-
// enableSearch(miniSearch);
200-
// return;
201-
// } else {
202-
// miniSearch.add(m);
203-
// console.log(miniSearch.documentCount);
204-
// lastIndexedTimestamp = m.timestamp;
205-
// }
206-
// }),
207-
// );
208-
// };
209-
210-
// load and start search engine
211-
212-
electron.ipcRenderer.on("search", (ev, terms) => {
213-
electron.ipcRenderer.send("search-unavailable");
214-
});
215-
216-
electron.ipcRenderer.on("is-search-available", (ev, terms) => {
217-
electron.ipcRenderer.send("search-unavailable");
218-
});
219-
220-
electron.ipcRenderer.send("search-unavailable");
221-
222-
// const enableSearch = (miniSearch) => {
223-
// // handle searches
224-
// electron.ipcRenderer.removeAllListeners("search");
225-
// electron.ipcRenderer.removeAllListeners("is-search-available");
226-
// electron.ipcRenderer.on("is-search-available", (ev, terms) => {
227-
// electron.ipcRenderer.send("search-available");
228-
// });
229-
230-
// electron.ipcRenderer.send("search-available");
231-
232-
// electron.ipcRenderer.on("search", (ev, terms) => {
233-
// const result = miniSearch.search(terms, { combineWith: "AND" });
234-
// console.log(`result count: ${result.length}`);
235-
// electron.ipcRenderer.send("search-results", result);
236-
// });
237-
// };
238-
239135
const enableSqliteSearch = (database) => {
240136
// handle searches
241137
electron.ipcRenderer.removeAllListeners("search");
@@ -266,8 +162,23 @@ module.exports = function (ssbConfig) {
266162
};
267163

268164
const loadMessagesIntoSQLite = (
269-
{ database, controlData },
165+
{ database, controlData, delayPullStream = false },
270166
) => {
167+
// if it is an imported account doing first-time sync
168+
// attempting a pull stream at the same time is very
169+
// costly for the UI.
170+
//
171+
// Let's delay that for 15 minutes.
172+
if (delayPullStream) {
173+
console.log("SQLITE: imported account, delay pulling");
174+
setTimeout(() => {
175+
console.log("SQLITE: delayed pulling about to start...");
176+
set(ssbConfig.keys.id, "imported", false);
177+
loadMessagesIntoSQLite({ database, controlData });
178+
}, 15 * 60000);
179+
return;
180+
}
181+
271182
console.log("SQLITE: Starting message indexing pull stream...");
272183
const insert = database.prepare(
273184
"INSERT OR REPLACE INTO messages (key, content, raw, timestamp) VALUES (?, ?, ?, ?)",
@@ -324,7 +235,12 @@ module.exports = function (ssbConfig) {
324235

325236
loadOrCreateSqlite().then(({ database, controlData }) => {
326237
console.log("SQLITE: callback");
327-
loadMessagesIntoSQLite({ database, controlData });
238+
let delayPullStream = false;
239+
let configuration = configurationForIdentity(ssbConfig.keys.id);
240+
if (configuration?.imported) {
241+
delayPullStream = true;
242+
}
243+
loadMessagesIntoSQLite({ database, controlData, delayPullStream });
328244
}).catch((e) => {
329245
console.log("SQLITE: ERROR");
330246
console.error(JSON.stringify(e));

0 commit comments

Comments
 (0)