[object-persistor] import ProjectKey helper from history-v1 (#30600)

GitOrigin-RevId: c72aa4bf91569904a2072c74d6ed2f3c764d97bb
This commit is contained in:
Jakob Ackermann
2026-01-12 12:28:16 +00:00
committed by Copybot
parent 292230b1db
commit 18cff6e1ac
17 changed files with 17 additions and 40 deletions

View File

@@ -0,0 +1,22 @@
const path = require('node:path')
//
// The advice in http://docs.aws.amazon.com/AmazonS3/latest/dev/
// request-rate-perf-considerations.html is to avoid sequential key prefixes,
// so we reverse the project ID part of the key as they suggest.
//
function format(projectId) {
const prefix = naiveReverse(pad(projectId))
return path.join(prefix.slice(0, 3), prefix.slice(3, 6), prefix.slice(6))
}
function pad(number) {
return (number || 0).toString().padStart(9, '0')
}
function naiveReverse(string) {
return string.split('').reverse().join('')
}
exports.format = format
exports.pad = pad

View File

@@ -0,0 +1,21 @@
const { expect } = require('chai')
const { format, pad } = require('../../src/ProjectKey.js')
describe('projectKey', function () {
it('reverses padded keys', function () {
expect(format(1)).to.equal('100/000/000')
expect(format(12)).to.equal('210/000/000')
expect(format(123456789)).to.equal('987/654/321')
expect(format(9123456789)).to.equal('987/654/3219')
})
it('pads numbers with zeros to length 9', function () {
expect(pad(undefined)).to.equal('000000000')
expect(pad(null)).to.equal('000000000')
expect(pad(1)).to.equal('000000001')
expect(pad(10)).to.equal('000000010')
expect(pad(100000000)).to.equal('100000000')
expect(pad(1000000000)).to.equal('1000000000')
})
})