rocicorp / replicache

Realtime Sync for Any Backend Stack
https://doc.replicache.dev
1.05k stars 37 forks source link

refactor: Extract ref count updating logic out of dag WriteImpl so it can be reused by lazy dag store. #769

Closed grgbkr closed 2 years ago

grgbkr commented 2 years ago

Pulls Garbage Collection logic into its own module, so that it can be shared with the upcoming lazy dag store implementation for Simplified Dueling Dags.

Interface:

export type HeadChange = {
  new: Hash | undefined;
  old: Hash | undefined;
};

export type RefCountUpdates = Map<Hash, number>;

export interface GarbageCollectionDelegate {
  getRefCount: (hash: Hash) => Promise<number>;
  getRefs: (hash: Hash) => Promise<readonly Hash[] | undefined>;
}

/**
 * Computes how ref counts should be updated when a dag write is commited.
 * Does not modify the dag store.
 * @param headChanges Heads that were changed by the dag write.
 * @param putChunks Chunks that were put by the dag write.
 * @param delegate Delegate used for loading ref information from the dag store.
 * @returns Map from chunk Hash to new ref count.  Chunks with a new ref count of 0 should
 * be deleted.  All hashes in `putChunks` will have an entry (which will be zero if a
 * newly put chunk is not reachable from any head).
 */
export async function computeRefCountUpdates(
  headChanges: Iterable<HeadChange>,
  putChunks: ReadonlySet<Hash>,
  delegate: GarbageCollectionDelegate,
): Promise<RefCountUpdates> 

Part of #671

vercel[bot] commented 2 years ago

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/rocicorp/replicache/DqzvzutsfY7zNMXFw6P3h5GV5Ncj
✅ Preview: https://replicache-git-grgbkr-ssd-lazy-dag-extract-gc-rocicorp.vercel.app

arv commented 2 years ago

LGTM