Skip to content

Commit

Permalink
Shorten filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 9, 2022
1 parent 1fa868e commit 09a2e34
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/history/data/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ export const getRawResultFilename = function (rawResult) {

// Retrieve filename from a metadatum
export const serializeFilename = function ({ id, timestamp }) {
const idStr = shortenId(id)
const timestampStr = serializeTimestamp(timestamp)
return `${timestampStr}_${id}.json`
return `${timestampStr}_${idStr}.json`
}

// We only keep the last characters of the `result.id` in the filename.
// This is to keep filenames short since some systems impose a limit.
// We keep it high enough to prevent collisions though.
// - 12 hexadecimal characters is 48 bits of entropy, which has a 50%
// probability of collision after 2e7 results with the same `id`, which is
// very unlikely
const shortenId = function (id) {
return id.length <= ID_LENGTH ? id : id.slice(0, -ID_LENGTH)
}

const ID_LENGTH = 12

const serializeTimestamp = function (timestamp) {
const date = new Date(timestamp)
const year = date.getUTCFullYear()
Expand Down Expand Up @@ -53,4 +66,4 @@ const parseTimestamp = function ({
}

const RESULT_FILENAME_REGEXP =
/^(?<year>\d{4})_(?<month>\d{2})_(?<day>\d{2})_(?<hours>\d{2})_(?<minutes>\d{2})_(?<seconds>\d{2})_(?<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\.json$/u
/^(?<year>\d{4})_(?<month>\d{2})_(?<day>\d{2})_(?<hours>\d{2})_(?<minutes>\d{2})_(?<seconds>\d{2})_(?<id>[\da-f]{12})\.json$/u
5 changes: 3 additions & 2 deletions src/history/delta/formats/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const parseId = function (delta) {
return delta
}

// Some stores shorten `id` and only keep the last 12 characters.
const findById = function (metadataGroups, id) {
return metadataGroups.findIndex(
([firstMetadatum]) => firstMetadatum.id === id,
return metadataGroups.findIndex(([firstMetadatum]) =>
id.endsWith(firstMetadatum.id),
)
}

Expand Down

0 comments on commit 09a2e34

Please sign in to comment.