Skip to content

Commit a997bf7

Browse files
committed
remove cleanups
1 parent dfbe487 commit a997bf7

21 files changed

Lines changed: 7 additions & 628 deletions

File tree

apps/desktop/src/store/tinybase/persister/chat/persister.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import { buildChatSaveOps } from "./save";
1010

1111
import { createMultiTableDirPersister } from "~/store/tinybase/persister/factories";
12-
import { CHAT_MESSAGES_FILE } from "~/store/tinybase/persister/shared";
1312
import type { Store } from "~/store/tinybase/store/main";
1413

1514
export function createChatPersister(store: Store) {
@@ -21,14 +20,6 @@ export function createChatPersister(store: Store) {
2120
{ tableName: "chat_groups", isPrimary: true },
2221
{ tableName: "chat_messages", foreignKey: "chat_group_id" },
2322
],
24-
cleanup: (tables) => [
25-
{
26-
type: "dirs",
27-
subdir: "chats",
28-
markerFile: CHAT_MESSAGES_FILE,
29-
keepIds: Object.keys(tables.chat_groups ?? {}),
30-
},
31-
],
3223
loadAll: loadAllChatGroups,
3324
loadSingle: loadSingleChatGroup,
3425
save: (_store, tables, dataDir, changedTables) => {

apps/desktop/src/store/tinybase/persister/factories/collector.ts

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,12 @@ import {
3030
} from "~/store/tinybase/persister/shared/types";
3131
import { StoreOrMergeableStore } from "~/store/tinybase/store/shared";
3232

33-
const CLEANUP_SAFEGUARD_MIN_DISK_COUNT = 5;
34-
const CLEANUP_SAFEGUARD_MIN_KEEP_RATIO = 0.5;
35-
3633
type LoadSingleFn<Schemas extends OptionalSchemas> = (
3734
entityId: string,
3835
) => Promise<
3936
PersistedChanges<Schemas, Persists.StoreOrMergeableStore> | undefined
4037
>;
4138

42-
type OrphanCleanupDirs = {
43-
type: "dirs";
44-
subdir: string;
45-
markerFile: string;
46-
keepIds: string[];
47-
};
48-
49-
type OrphanCleanupFiles = {
50-
type: "files";
51-
subdir: string;
52-
extension: string;
53-
keepIds: string[];
54-
};
55-
56-
type OrphanCleanupFilesRecursive = {
57-
type: "filesRecursive";
58-
subdir: string;
59-
markerFile: string;
60-
extension: string;
61-
keepIds: string[];
62-
};
63-
64-
export type OrphanCleanupConfig =
65-
| OrphanCleanupDirs
66-
| OrphanCleanupFiles
67-
| OrphanCleanupFilesRecursive;
68-
6939
type BaseCollectorOptions<Schemas extends OptionalSchemas> = {
7040
label: string;
7141
save: (
@@ -75,7 +45,6 @@ type BaseCollectorOptions<Schemas extends OptionalSchemas> = {
7545
changedTables?: ChangedTables,
7646
) => SaveResult;
7747
load?: () => Promise<Content<Schemas> | undefined>;
78-
cleanup?: (tables: TablesContent) => OrphanCleanupConfig[];
7948
watchPaths?: string[];
8049
watchIntervalMs?: number;
8150
};
@@ -161,11 +130,6 @@ export function createCollectorPersister<Schemas extends OptionalSchemas>(
161130
await writeDocumentBatch(documentItems, options.label);
162131
await deleteFiles(deletePaths, options.label);
163132
}
164-
165-
if (options.cleanup) {
166-
const cleanupConfigs = options.cleanup(tables ?? {});
167-
await runOrphanCleanup(cleanupConfigs, options.label);
168-
}
169133
} catch (error) {
170134
console.error(`[${options.label}] save error:`, error);
171135
}
@@ -244,100 +208,3 @@ async function deleteFiles(paths: string[], label: string): Promise<void> {
244208
}
245209
}
246210
}
247-
248-
async function countItemsOnDisk(config: OrphanCleanupConfig): Promise<number> {
249-
try {
250-
const dataDir = await getDataDir();
251-
const subdir = [dataDir, config.subdir].join("/");
252-
253-
if (config.type === "dirs") {
254-
const result = await fsSyncCommands.scanAndRead(
255-
subdir,
256-
[config.markerFile],
257-
true,
258-
null,
259-
);
260-
if (result.status === "ok") {
261-
return result.data.dirs.length;
262-
}
263-
} else if (config.type === "files") {
264-
const result = await fsSyncCommands.scanAndRead(
265-
subdir,
266-
[`*.${config.extension}`],
267-
false,
268-
null,
269-
);
270-
if (result.status === "ok") {
271-
return Object.keys(result.data.files).length;
272-
}
273-
} else if (config.type === "filesRecursive") {
274-
const result = await fsSyncCommands.scanAndRead(
275-
subdir,
276-
[`*.${config.extension}`],
277-
true,
278-
null,
279-
);
280-
if (result.status === "ok") {
281-
return Object.keys(result.data.files).length;
282-
}
283-
}
284-
} catch {
285-
// Ignore counting errors - we'll proceed with cleanup
286-
}
287-
return 0;
288-
}
289-
290-
async function runOrphanCleanup(
291-
configs: OrphanCleanupConfig[],
292-
label: string,
293-
): Promise<void> {
294-
for (const config of configs) {
295-
if (config.keepIds.length === 0) {
296-
continue;
297-
}
298-
299-
const diskCount = await countItemsOnDisk(config);
300-
const keepRatio = config.keepIds.length / Math.max(diskCount, 1);
301-
302-
if (
303-
diskCount > CLEANUP_SAFEGUARD_MIN_DISK_COUNT &&
304-
keepRatio < CLEANUP_SAFEGUARD_MIN_KEEP_RATIO
305-
) {
306-
console.warn(
307-
`[${label}] Skipping ${config.type} cleanup: keeping ${config.keepIds.length}/${diskCount} ` +
308-
`(${(keepRatio * 100).toFixed(0)}%) - possible load failure`,
309-
);
310-
continue;
311-
}
312-
313-
try {
314-
if (config.type === "dirs") {
315-
await fsSyncCommands.cleanupOrphan(
316-
{
317-
type: "dirs",
318-
subdir: config.subdir,
319-
marker_file: config.markerFile,
320-
},
321-
config.keepIds,
322-
);
323-
} else if (config.type === "files") {
324-
await fsSyncCommands.cleanupOrphan(
325-
{ type: "files", subdir: config.subdir, extension: config.extension },
326-
config.keepIds,
327-
);
328-
} else if (config.type === "filesRecursive") {
329-
await fsSyncCommands.cleanupOrphan(
330-
{
331-
type: "filesRecursive",
332-
subdir: config.subdir,
333-
marker_file: config.markerFile,
334-
extension: config.extension,
335-
},
336-
config.keepIds,
337-
);
338-
}
339-
} catch (error) {
340-
console.error(`[${label}] Cleanup error for ${config.type}:`, error);
341-
}
342-
}
343-
}

apps/desktop/src/store/tinybase/persister/factories/markdown-dir.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const fsSyncMocks = vi.hoisted(() => ({
2222
serialize: vi.fn().mockResolvedValue({ status: "ok", data: "" }),
2323
writeDocumentBatch: vi.fn().mockResolvedValue({ status: "ok", data: null }),
2424
readDocumentBatch: vi.fn(),
25-
cleanupOrphan: vi.fn().mockResolvedValue({ status: "ok", data: 0 }),
2625
}));
2726

2827
const fs2Mocks = vi.hoisted(() => ({

apps/desktop/src/store/tinybase/persister/factories/markdown-dir.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,6 @@ export function createMarkdownDirPersister<
166166
return createCollectorPersister(store, {
167167
label,
168168
watchPaths: [`${dirName}/`],
169-
cleanup: (tables) => [
170-
{
171-
type: "files",
172-
subdir: dirName,
173-
extension: "md",
174-
keepIds: Object.keys(
175-
(tables as Record<string, Record<string, unknown>>)[tableName] ?? {},
176-
),
177-
},
178-
],
179169
entityParser,
180170
loadSingle: (entityId: string) =>
181171
loadSingleEntity(config, entityId, deletionMarker),

apps/desktop/src/store/tinybase/persister/factories/multi-table-dir.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const fsSyncMocks = vi.hoisted(() => ({
1616
serialize: vi.fn().mockResolvedValue({ status: "ok", data: "" }),
1717
writeDocumentBatch: vi.fn().mockResolvedValue({ status: "ok", data: null }),
1818
readDocumentBatch: vi.fn(),
19-
cleanupOrphan: vi.fn().mockResolvedValue({ status: "ok", data: 0 }),
2019
}));
2120

2221
const fs2Mocks = vi.hoisted(() => ({
@@ -42,7 +41,6 @@ describe("createMultiTableDirPersister", () => {
4241
dirName: "test",
4342
entityParser: () => null,
4443
tables: [{ tableName: "sessions", isPrimary: true }],
45-
cleanup: () => [],
4644
loadAll: async () => ok({ sessions: {} }),
4745
loadSingle: async () => ok({ sessions: {} }),
4846
save: () => ({ operations: [] }),

apps/desktop/src/store/tinybase/persister/factories/multi-table-dir.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import type { MergeableStore, OptionalSchemas } from "tinybase/with-schemas";
22

33
import { toContent, toPersistedChanges } from "@hypr/tinybase-utils";
44

5-
import {
6-
createCollectorPersister,
7-
type OrphanCleanupConfig,
8-
} from "./collector";
5+
import { createCollectorPersister } from "./collector";
96

107
import {
118
createDeletionMarker,
@@ -30,7 +27,6 @@ export type MultiTableDirConfig<
3027
dirName: string;
3128
entityParser: (path: string) => string | null;
3229
tables: TableConfigEntry<Schemas, TLoadedData>[];
33-
cleanup: (tables: TablesContent) => OrphanCleanupConfig[];
3430
loadAll: (dataDir: string) => Promise<LoadResult<TLoadedData>>;
3531
loadSingle: (
3632
dataDir: string,
@@ -58,16 +54,8 @@ export function createMultiTableDirPersister<
5854
store: MergeableStore<Schemas>,
5955
config: MultiTableDirConfig<Schemas, TLoadedData>,
6056
): ReturnType<typeof createCollectorPersister<Schemas>> {
61-
const {
62-
label,
63-
dirName,
64-
entityParser,
65-
tables,
66-
cleanup,
67-
loadAll,
68-
loadSingle,
69-
save,
70-
} = config;
57+
const { label, dirName, entityParser, tables, loadAll, loadSingle, save } =
58+
config;
7159

7260
const deletionMarker = createDeletionMarker<TLoadedData>(
7361
store as DeletionMarkerStore,
@@ -78,7 +66,6 @@ export function createMultiTableDirPersister<
7866
return createCollectorPersister(store, {
7967
label,
8068
watchPaths: [`${dirName}/`],
81-
cleanup,
8269
entityParser,
8370
loadSingle: async (entityId: string) => {
8471
try {

apps/desktop/src/store/tinybase/persister/human/persister.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const fsSyncMocks = vi.hoisted(() => ({
1919
serialize: vi.fn().mockResolvedValue({ status: "ok", data: "" }),
2020
writeDocumentBatch: vi.fn().mockResolvedValue({ status: "ok", data: null }),
2121
readDocumentBatch: vi.fn(),
22-
cleanupOrphan: vi.fn().mockResolvedValue({ status: "ok", data: 0 }),
2322
}));
2423

2524
const fs2Mocks = vi.hoisted(() => ({

apps/desktop/src/store/tinybase/persister/organization/persister.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const fsSyncMocks = vi.hoisted(() => ({
1818
serialize: vi.fn().mockResolvedValue({ status: "ok", data: "" }),
1919
writeDocumentBatch: vi.fn().mockResolvedValue({ status: "ok", data: null }),
2020
readDocumentBatch: vi.fn(),
21-
cleanupOrphan: vi.fn().mockResolvedValue({ status: "ok", data: 0 }),
2221
}));
2322

2423
const fs2Mocks = vi.hoisted(() => ({

apps/desktop/src/store/tinybase/persister/session/persister.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import {
1313
} from "./save/index";
1414

1515
import { createMultiTableDirPersister } from "~/store/tinybase/persister/factories";
16-
import {
17-
SESSION_META_FILE,
18-
SESSION_NOTE_EXTENSION,
19-
} from "~/store/tinybase/persister/shared";
2016
import type { Store } from "~/store/tinybase/store/main";
2117

2218
export function createSessionPersister(store: Store) {
@@ -32,21 +28,6 @@ export function createSessionPersister(store: Store) {
3228
{ tableName: "transcripts", foreignKey: "session_id" },
3329
{ tableName: "enhanced_notes", foreignKey: "session_id" },
3430
],
35-
cleanup: (tables) => [
36-
{
37-
type: "dirs",
38-
subdir: "sessions",
39-
markerFile: SESSION_META_FILE,
40-
keepIds: Object.keys(tables.sessions ?? {}),
41-
},
42-
{
43-
type: "filesRecursive",
44-
subdir: "sessions",
45-
markerFile: SESSION_META_FILE,
46-
extension: SESSION_NOTE_EXTENSION.slice(1),
47-
keepIds: Object.keys(tables.enhanced_notes ?? {}),
48-
},
49-
],
5031
loadAll: loadAllSessionData,
5132
loadSingle: loadSingleSession,
5233
save: (store, tables, dataDir, changedTables) => {

0 commit comments

Comments
 (0)