feat(storage): keep a mark per stored object no row names

storage_reclaim_marks records since when the reclaim pass has seen an object
unreferenced, keyed by category and key. A mark is never moved forward, so
its age is how long the object has been unreferenced without a break; it goes
once the object is named again or gone. Owned by the storage domain.
This commit is contained in:
Maurice
2026-11-01 13:37:44 +01:00
parent 630f15b5ea
commit 29298dcfe5
11 changed files with 208 additions and 3 deletions
@@ -0,0 +1,23 @@
import { EntityRepositoryType, PrimaryKeyProp, defineEntity, p } from '@mikro-orm/core';
import { StorageReclaimMarksRepository } from '../repositories/StorageReclaimMarks.repository';
import { DbTimestampType } from '../types';
export class StorageReclaimMarks {
[EntityRepositoryType]?: StorageReclaimMarksRepository;
[PrimaryKeyProp]?: ['category', 'key'];
category!: string;
key!: string;
first_seen_at!: string;
size!: number;
}
export const StorageReclaimMarksSchema = defineEntity({
class: StorageReclaimMarks,
repository: () => StorageReclaimMarksRepository,
properties: {
category: p.text().primary(),
key: p.text().primary(),
first_seen_at: p.type(DbTimestampType),
size: p.integer(),
},
});
+2
View File
@@ -118,6 +118,7 @@ import { SchoolHolidayPeriodsSchema } from './SchoolHolidayPeriods.entity';
import { SchoolHolidayRegionsSchema } from './SchoolHolidayRegions.entity';
import { SettingsSchema } from './Settings.entity';
import { ShareTokensSchema } from './ShareTokens.entity';
import { StorageReclaimMarksSchema } from './StorageReclaimMarks.entity';
import { TagsSchema } from './Tags.entity';
import { TodoCategoryAssigneesSchema } from './TodoCategoryAssignees.entity';
import { TodoItemsSchema } from './TodoItems.entity';
@@ -285,6 +286,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
SchoolHolidayRegionsSchema,
SettingsSchema,
ShareTokensSchema,
StorageReclaimMarksSchema,
TagsSchema,
TodoCategoryAssigneesSchema,
TodoItemsSchema,
@@ -0,0 +1,9 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
export interface StorageReclaimMarksTable {
category: string;
key: string;
first_seen_at: string;
size: number;
}
+2
View File
@@ -124,6 +124,7 @@ import type { SchoolHolidayPeriodsTable } from './SchoolHolidayPeriods.table';
import type { SchoolHolidayRegionsTable } from './SchoolHolidayRegions.table';
import type { SettingsTable } from './Settings.table';
import type { ShareTokensTable } from './ShareTokens.table';
import type { StorageReclaimMarksTable } from './StorageReclaimMarks.table';
import type { TagsTable } from './Tags.table';
import type { TodoCategoryAssigneesTable } from './TodoCategoryAssignees.table';
import type { TodoItemsTable } from './TodoItems.table';
@@ -297,6 +298,7 @@ export interface DB {
school_holiday_regions: SchoolHolidayRegionsTable;
settings: SettingsTable;
share_tokens: ShareTokensTable;
storage_reclaim_marks: StorageReclaimMarksTable;
tags: TagsTable;
todo_category_assignees: TodoCategoryAssigneesTable;
todo_items: TodoItemsTable;
@@ -0,0 +1,24 @@
import { Migration } from '@mikro-orm/migrations';
/**
* The storage reclaim pass remembers, per stored object, since when no row has
* named it. An object is deleted only once it has stayed unreferenced on every
* pass for a week (and only when the admin switched deletion on), so a value a
* client writes back late, an offline replay or a copy made in between keeps
* its object. A row goes as soon as its object is referenced again or gone.
*/
export class Migration20200101046330_an_object_no_row_names_is_marked_before_it_goes extends Migration {
override name = 'Migration20200101046330_an_object_no_row_names_is_marked_before_it_goes';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS storage_reclaim_marks (
category TEXT NOT NULL,
key TEXT NOT NULL,
first_seen_at DATETIME NOT NULL,
size INTEGER NOT NULL,
PRIMARY KEY (category, key)
)
`);
}
}
@@ -0,0 +1,63 @@
import type { StorageReclaimMarks } from '../entities/StorageReclaimMarks.entity';
import type { DB } from '../kysely/db';
import { TrekRepository } from './_shared/trek-repository';
type MarksKyselyDB = Pick<DB, 'storage_reclaim_marks'>;
/** Since when an object has been unreferenced, as the reclaim pass first saw it. */
export interface ReclaimMarkRow {
key: string;
first_seen_at: string;
size: number;
}
/**
* `storage_reclaim_marks`: one row per stored object the reclaim pass found
* no row naming, keyed by category and category-relative key. The pass reads a
* category's marks once, inserts the new ones and drops the ones whose object
* is referenced again or gone; a mark is never moved forward, so its age is how
* long the object has been unreferenced without a break.
*/
export class StorageReclaimMarksRepository extends TrekRepository<StorageReclaimMarks> {
private db() {
return this.kysely<MarksKyselyDB>();
}
async marksOf(category: string): Promise<ReclaimMarkRow[]> {
return this.db()
.selectFrom('storage_reclaim_marks')
.select(['key', 'first_seen_at', 'size'])
.where('category', '=', category)
.execute();
}
/** Marks these keys first seen at `firstSeenAt`; a key marked already keeps its first sighting. */
async insertMissing(
category: string,
rows: Array<{ key: string; size: number }>,
firstSeenAt: string,
): Promise<void> {
if (rows.length === 0) return;
await this.db()
.insertInto('storage_reclaim_marks')
.values(rows.map((row) => ({ category, key: row.key, size: row.size, first_seen_at: firstSeenAt })))
.onConflict((oc) => oc.columns(['category', 'key']).doNothing())
.execute();
}
/** Drops the marks of these keys; answers how many went. */
async deleteKeys(category: string, keys: string[]): Promise<number> {
if (keys.length === 0) return 0;
const result = await this.db()
.deleteFrom('storage_reclaim_marks')
.where('category', '=', category)
.where('key', 'in', keys)
.executeTakeFirst();
return Number(result.numDeletedRows);
}
/** Drops every mark of a category, for a pass that could not judge it: the week starts over. */
async deleteCategory(category: string): Promise<number> {
return await this.nativeDelete({ category });
}
}