Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse names for S3 sort #2

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.154.0",
"@thi.ng/base-n": "^2.3.12",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
25 changes: 20 additions & 5 deletions src/s3vfs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NoSuchKey, S3 } from '@aws-sdk/client-s3'
import { v4 } from 'uuid'
import { defBase } from '@thi.ng/base-n'
import { Readable } from 'stream'

import { v4 } from 'uuid'
import {
SQLITE_OK,
SQLITE_ACCESS_EXISTS,
Expand All @@ -11,6 +11,8 @@ import {
} from 'wa-sqlite'
import { Base } from 'wa-sqlite/src/VFS.js'

const base36 = defBase('0123456789abcdefghijklmnopqrstuvwxyz')

/**
* The default page size used by sqlite
*/
Expand All @@ -24,12 +26,16 @@ export const LOCK_PAGE_OFFSET = 1073741824
export class S3VFS extends Base {
private readonly mapIdToPrefix = new Map<number, string>()
private readonly prefixToFlags = new Map<string, number>()
readonly maxFilename = ''.padStart(this.filenameLength, 'z')
readonly maxFileNumber = base36.decodeBigInt(this.maxFilename)
readonly blockSizeN = BigInt(this.blockSize)

constructor(
private readonly s3: S3,
private readonly bucketName: string,
private readonly blockSize = DEFAULT_PAGE_SIZE,
readonly name = `s3vfs-${bucketName}-${v4()}`,
readonly filenameLength = 10,
){
super()
}
Expand Down Expand Up @@ -126,8 +132,13 @@ export class S3VFS extends Base {
return this.handleAsync(async () => {
try {
const objects = await this.fetchObjects(fileId)
const size = (objects || []).reduce((size, { Size: blockSize }) => size + (blockSize || 0), 0)
pSize64.set(size)
// Capture the last non-slash characters
const filename = objects?.[0]?.Key?.match(/([^/]*)$/)?.[0];
// console.log(filename);
// Use BigInt to avoid precision problems with Number
const fileCount = filename ? base36.decodeBigInt(filename) : 0;
const size = fileCount ? (fileCount * this.blockSizeN) + this.blockSizeN : 0;
pSize64.set(Number(size))
LoneRifle marked this conversation as resolved.
Show resolved Hide resolved
return SQLITE_OK
} catch (error) {
pSize64.set(0)
Expand Down Expand Up @@ -173,7 +184,11 @@ export class S3VFS extends Base {
}

private blockId(block: number) {
return ('0000000000'+block).slice(-10)
if(block > this.maxFileNumber) {
throw new Error(`File block number ${block} too great`)
}
const blockN = this.maxFileNumber - BigInt(block)
return base36.encodeBigInt(blockN);
}

private blockObject(prefix: string, block: number) {
Expand Down