Skip to content

Commit

Permalink
Use base 10
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmckinley committed Feb 14, 2023
1 parent f1eea64 commit e259898
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.154.0",
"@thi.ng/base-n": "^2.3.12",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
24 changes: 9 additions & 15 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 { defBase } from '@thi.ng/base-n'
import { Readable } from 'stream'
import { v4 } from 'uuid'
import { Readable } from 'stream'

import {
SQLITE_OK,
SQLITE_ACCESS_EXISTS,
Expand All @@ -11,8 +11,6 @@ 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 @@ -26,16 +24,13 @@ 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)
readonly maxFileNumber = 9999999999

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 @@ -134,11 +129,10 @@ export class S3VFS extends Base {
const objects = await this.fetchObjects(fileId)
// 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))
const fileNumber = filename ? parseInt(filename) : this.maxFileNumber;
const fileCount = this.maxFileNumber - fileNumber
const size = fileCount ? (fileCount * this.blockSize) + this.blockSize : 0;
pSize64.set(size)
return SQLITE_OK
} catch (error) {
pSize64.set(0)
Expand Down Expand Up @@ -187,8 +181,8 @@ export class S3VFS extends Base {
if(block > this.maxFileNumber) {
throw new Error(`File block number ${block} too great`)
}
const blockN = this.maxFileNumber - BigInt(block)
return base36.encodeBigInt(blockN);
const reverseBlock = this.maxFileNumber - block
return ('0000000000'+reverseBlock).slice(-10)
}

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

0 comments on commit e259898

Please sign in to comment.