Skip to content

Commit 0e3665b

Browse files
committed
Improve readability of duplicate filename logic
1 parent 7681a56 commit 0e3665b

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

extensions/ql-vscode/src/databases/database-fetcher.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ async function databaseArchiveFetcher(
415415
}
416416
}
417417

418+
// The number of tries to use when generating a unique filename before
419+
// giving up and using a nanoid.
420+
const DUPLICATE_FILENAMES_TRIES = 10_000;
421+
418422
async function getStorageFolder(
419423
storagePath: string,
420424
urlStr: string,
@@ -448,14 +452,16 @@ async function getStorageFolder(
448452
let counter = 0;
449453
while (existingFiles.includes(basename(folderName))) {
450454
counter++;
451-
folderName = `${lastName}-${counter}`;
452-
if (counter > 10_000) {
455+
456+
if (counter <= DUPLICATE_FILENAMES_TRIES) {
457+
// First try to use a counter to make the name unique.
458+
folderName = `${lastName}-${counter}`;
459+
} else if (counter <= 2 * DUPLICATE_FILENAMES_TRIES) {
453460
// If there are more than 10,000 similarly named databases,
454461
// give up on using a counter and use a random string instead.
455462
folderName = `${lastName}-${nanoid()}`;
456-
}
457-
if (counter > 20_000) {
458-
// This should never happen, but just in case, we don't want to
463+
} else {
464+
// This should almost never happen, but just in case, we don't want to
459465
// get stuck in an infinite loop.
460466
throw new Error(
461467
"Could not find a unique name for downloaded database. Please remove some databases and try again.",

0 commit comments

Comments
 (0)