Skip to content

Commit d233c0c

Browse files
committed
refactor load from storage, use reduce in getTotalCount
1 parent 9476765 commit d233c0c

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

packages/core/src/codewhisperer/nextEditPrediction/PredictionTracker.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,7 @@ export class PredictionTracker {
210210
}
211211

212212
public getTotalSnapshotCount(): number {
213-
let count = 0
214-
for (const snapshots of this.snapshots.values()) {
215-
count += snapshots.length
216-
}
217-
return count
213+
return Array.from(this.snapshots.values()).reduce((count, snapshots) => count + snapshots.length, 0)
218214
}
219215

220216
private getSnapshotsDirectoryPath(): string {
@@ -232,12 +228,6 @@ export class PredictionTracker {
232228
* @param content The content to save
233229
*/
234230
private async saveSnapshotContentToStorage(storageKey: string, content: string): Promise<void> {
235-
const snapshotsDir = this.getSnapshotsDirectoryPath()
236-
237-
if (!(await fs.existsDir(snapshotsDir))) {
238-
await fs.mkdir(snapshotsDir)
239-
}
240-
241231
const filePath = this.getSnapshotFilePath(storageKey)
242232
if (!filePath) {
243233
throw new Error('Failed to create snapshot file path')
@@ -337,9 +327,8 @@ export class PredictionTracker {
337327

338328
try {
339329
const files = await fs.readdir(snapshotsDir)
340-
const metadataFiles = new Map<string, { timestamp: number; filePath: string }>()
341330

342-
// First, collect all the metadata files
331+
// Process each file in a single pass
343332
for (const [filename, fileType] of files) {
344333
if (!filename.endsWith(snapshotFileSuffix) || fileType !== vscode.FileType.File) {
345334
continue
@@ -349,41 +338,28 @@ export class PredictionTracker {
349338
const parts = storageKey.split('-')
350339
const timestamp = parseInt(parts[parts.length - 1], 10)
351340
const originalFilename = parts.slice(0, parts.length - 1).join('-')
352-
353-
// This helps us match the files back to their original source
354-
metadataFiles.set(storageKey, {
355-
timestamp,
356-
filePath: originalFilename,
357-
})
358-
}
359-
360-
// Now process each file that we found
361-
for (const [storageKey, metadata] of metadataFiles.entries()) {
362341
const contentPath = this.getSnapshotFilePath(storageKey)
363342

364343
try {
365-
// if original file no longer exists, delete the snapshot
366-
if (!(await fs.exists(metadata.filePath))) {
344+
// If original file no longer exists, delete the snapshot
345+
if (!(await fs.exists(originalFilename))) {
367346
await fs.delete(contentPath)
368347
continue
369348
}
370349

371-
// Calculate size from the content file
372350
const stats = await fs.stat(contentPath)
373351
const size = stats.size
374352

375-
// Create a metadata-only snapshot
376353
const snapshot: FileSnapshot = {
377-
filePath: metadata.filePath,
378-
timestamp: metadata.timestamp,
354+
filePath: originalFilename,
355+
timestamp,
379356
size,
380357
storageKey,
381358
}
382359

383-
// Add to memory tracking
384-
const fileSnapshots = this.snapshots.get(metadata.filePath) || []
360+
const fileSnapshots = this.snapshots.get(originalFilename) || []
385361
fileSnapshots.push(snapshot)
386-
this.snapshots.set(metadata.filePath, fileSnapshots)
362+
this.snapshots.set(originalFilename, fileSnapshots)
387363
this.storageSize += size
388364
} catch (err) {
389365
// Remove invalid files

0 commit comments

Comments
 (0)