MCPcopy Index your code
hub / github.com/simstudioai/sim / getOrCreateTableSnapshot

Function getOrCreateTableSnapshot

apps/sim/lib/table/snapshot-cache.ts:152–188  ·  view source on GitHub ↗
(
  table: TableDefinition,
  requestId: string
)

Source from the content-addressed store, hash-verified

150 * across the upload. Concurrent misses write the same version-pinned key (idempotent).
151 */
152export async function getOrCreateTableSnapshot(
153 table: TableDefinition,
154 requestId: string
155): Promise<TableSnapshotRef> {
156 const shapeHash = schemaFingerprint(table)
157 const version = await readRowsVersion(table.id)
158 const key = snapshotKey(table.workspaceId, table.id, version, shapeHash)
159
160 const head = await headObject(key, SNAPSHOT_STORAGE_CONTEXT)
161 if (head) {
162 logger.info(`[${requestId}] Snapshot hit`, { tableId: table.id, version, size: head.size })
163 return { key, size: head.size, version }
164 }
165
166 logger.info(`[${requestId}] Snapshot miss; materializing`, { tableId: table.id, version })
167 const size = await materialize(table, key)
168
169 const after = await readRowsVersion(table.id)
170 if (after !== version) {
171 // The table mutated mid-scan: the bytes under `key` may be torn. Re-key to the new version and
172 // rebuild once (or reuse if a concurrent writer already stored it); drop the stale object.
173 logger.info(`[${requestId}] rows_version advanced during materialize; re-keying`, {
174 tableId: table.id,
175 from: version,
176 to: after,
177 })
178 const newKey = snapshotKey(table.workspaceId, table.id, after, shapeHash)
179 const newHead = await headObject(newKey, SNAPSHOT_STORAGE_CONTEXT)
180 const newSize = newHead ? newHead.size : await materialize(table, newKey)
181 await deleteFile({ key, context: SNAPSHOT_STORAGE_CONTEXT }).catch(() => {})
182 void deletePreviousVersion(table, after, shapeHash)
183 return { key: newKey, size: newSize, version: after }
184 }
185
186 void deletePreviousVersion(table, version, shapeHash)
187 return { key, size, version }
188}

Callers 2

resolveInputFilesFunction · 0.90

Calls 8

headObjectFunction · 0.90
deleteFileFunction · 0.90
schemaFingerprintFunction · 0.85
readRowsVersionFunction · 0.85
snapshotKeyFunction · 0.85
materializeFunction · 0.85
deletePreviousVersionFunction · 0.85
infoMethod · 0.80

Tested by

no test coverage detected