Skip to content
File

Blob: src/worker/git/object-store/support.ts

typescript85 lines
1import type { CacheContext } from "@/worker/cache";
2import type { PackedObjectCandidate } from "./candidates";
3import type { PackedObjectResult } from "./types";
4 
5import { createLogger } from "@/worker/common";
6 
7export type ResolvedLocation = PackedObjectCandidate;
8 
9export function typeCodeToObjectType(typeCode: number) {
10 switch (typeCode) {
11 case 1:
12 return "commit" as const;
13 case 2:
14 return "tree" as const;
15 case 3:
16 return "blob" as const;
17 case 4:
18 return "tag" as const;
19 default:
20 return null;
21 }
22}
23 
24export function ensureMemo(cacheCtx: CacheContext | undefined, repoId: string) {
25 if (!cacheCtx) return;
26 if (!cacheCtx.memo || (cacheCtx.memo.repoId && cacheCtx.memo.repoId !== repoId)) {
27 cacheCtx.memo = { repoId };
28 return;
29 }
30 if (!cacheCtx.memo.repoId) cacheCtx.memo.repoId = repoId;
31}
32 
33export function logOnce(cacheCtx: CacheContext | undefined, flag: string, fn: () => void) {
34 if (!cacheCtx) {
35 fn();
36 return;
37 }
38 cacheCtx.memo = cacheCtx.memo || {};
39 cacheCtx.memo.flags = cacheCtx.memo.flags || new Set<string>();
40 if (cacheCtx.memo.flags.has(flag)) return;
41 fn();
42 cacheCtx.memo.flags.add(flag);
43}
44 
45export function getPackedObjectStoreLogger(env: Env, repoId: string) {
46 return createLogger(env.LOG_LEVEL, {
47 service: "PackedObjectStore",
48 repoId,
49 });
50}
51 
52export function toPackedObjectResult(
53 location: ResolvedLocation,
54 type: PackedObjectResult["type"],
55 payload: Uint8Array
56): PackedObjectResult {
57 return {
58 packKey: location.source.packKey,
59 objectIndex: location.objectIndex,
60 offset: location.offset,
61 nextOffset: location.nextOffset,
62 oid: location.oid,
63 type,
64 payload,
65 };
66}
67 
68export function logPackedObjectMismatch(args: {
69 env: Env;
70 repoId: string;
71 oid: string;
72 reason: string;
73 details?: Record<string, unknown>;
74}) {
75 const log = createLogger(args.env.LOG_LEVEL, {
76 service: "PackedObjectShadow",
77 repoId: args.repoId,
78 });
79 log.warn("shadow:mismatch", {
80 oid: args.oid,
81 reason: args.reason,
82 ...(args.details || {}),
83 });
84}