Skip to main content

Compaction

Compaction rebuilds collection storage to remove fragmentation and then rebuilds indexes.

You typically run compaction when:

  • the database has been running for a long time with lots of updates/deletes
  • you want to reclaim disk space
  • you want to "defragment" collections after heavy write workloads

db.compactCollection(name)

compactCollection(name: string): Promise<void>
import { LioranManager } from "@liorandb/core";

const manager = new LioranManager({ rootPath: "./.liorandb" });
const db = await manager.db("app");

await db.compactCollection("users");
await manager.close();
Sandbox output (example)
(no output)

db.compactAll()

compactAll(): Promise<void>
await db.compactAll();

collection.compact()

compact(): Promise<void>
const users = db.collection("users");
await users.compact();

What compaction does (high level)

  1. crash recovery checks
  2. snapshot rebuild to a temporary directory
  3. atomic directory swap
  4. reopen DB
  5. rebuild indexes