Implemented basic, deno KV backed, file service.

This commit is contained in:
didier amyot committed 2023-06-10 07:53:19 -04:00
commit b0fe9f308e
13 files changed
+18885803

No files matched your search

+171
View File
@@ -0,0 +1,171 @@
import { sha1 } from "https://denopkg.com/chiefbiiko/sha1/mod.ts";
export interface FileService {
createFromFullBody(path: string, body: string): Promise<FileMetaData>;
list(): AsyncIterable<FileMetaData>;
deleteByPath(path: string): Promise<void>;
getFullBodyByPath(path: string): Promise<string|null>;
getMetaDataByID(id: string): Promise<FileMetaData| null>;
renameById(id: string, newPath: string): Promise<FileMetaData|null>;
renameByPath(originalPath: string, newPath: string): Promise<FileMetaData|null>;
}
interface SerializedFile {
id: string,
path: string,
bodySize: number,
}
export interface FileMetaData {
path: string,
id: string,
bodySize: number,
}
interface SerializedFilePathIndex {
fileId: string;
}
export class DenoKVBlobService {
private readonly kv: Deno.Kv;
private readonly blobSize: number;
constructor(kv: Deno.Kv, blobSize: number){
this.kv = kv;
this.blobSize = blobSize;
}
private *splitText(body: string): Generator<string>{
let blobCount = 0;
let txt = body.substring(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
while(txt.length > 0 ){
yield txt;
blobCount += 1;
txt = body.substring(blobCount * this.blobSize, (blobCount + 1) * this.blobSize);
}
}
async persistBlob(index: string, body: string): Promise<void>{
let blobCount = 0;
for(const txt of this.splitText(body)){
const blob_id = sha1(txt, "utf8", "hex");
await this.kv.set(['blob_index', index, blobCount], blob_id);
await this.kv.set(['blob', blob_id], txt);
blobCount += 1;
}
}
async getBlob(index: string): Promise<string>{
let ret = "";
for await(const itm of this.kv.list<string>({prefix: ['blob_index', index]})){
const foo = await this.kv.get<string>(['blob', itm.value])
if(foo.value === null){
throw new Error(`Inconsistant blob index for key "${foo.value}", this is very bad.`);
}
ret += foo.value;
}
return ret;
}
async getNumberOfBlobs(index: string): Promise<number>{
let ret = 0;
for await(const _itm of this.kv.list<string>({prefix: ['blob_index', index]})){
ret += 1;
}
return ret;
}
}
export class DenoKVFileService implements FileService {
private readonly kv: Deno.Kv;
private readonly blobService: DenoKVBlobService;
constructor(kv: Deno.Kv){
this.kv = kv;
this.blobService = new DenoKVBlobService(kv, 500);
}
async createFromFullBody(path: string, body: string): Promise<FileMetaData> {
const newId = crypto.randomUUID();
const serializedFile: SerializedFile = {id: newId, path, bodySize: body.length};
const idx: SerializedFilePathIndex = {fileId: newId};
await this.kv.set(['file', newId], serializedFile);
await this.kv.set(['file_index_path', path], idx);
await this.blobService.persistBlob(newId, body);
return {id: newId, path, bodySize: body.length};
}
async* list(): AsyncIterable<FileMetaData> {
for await (const entry of this.kv.list<SerializedFilePathIndex>({ prefix: ["file_index_path"] })) {
const ret = await this.kv.get<SerializedFile>(['file', entry.value.fileId]);
if(ret.value !== null) {
yield {path: ret.value.path, id: ret.value.id, bodySize: ret.value.bodySize};
}
}
}
async deleteByPath(path: string): Promise<void> {
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]);
if(idx.value === null){
return;
}
// todo, this will fail silently if transaction fails. we might want to put that into a loop.
await this.kv.atomic()
.check(idx)
.delete(["file_index_path", path])
.delete(idx.key)
.commit();
}
async getFullBodyByPath(path: string): Promise<string|null> {
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", path]);
if(idx.value === null){
return null;
}
return this.blobService.getBlob(idx.value.fileId);
}
async getMetaDataByID(id: string): Promise<FileMetaData | null>{
const fileObject = await this.kv.get<SerializedFile>(['file', id])
if(fileObject.value === null){
return null;
}
return {
path: fileObject.value.path,
id: fileObject.value.id,
bodySize: fileObject.value.bodySize,
};
}
async renameById(id: string, newPath: string): Promise<FileMetaData|null> {
const fileObject = await this.kv.get<SerializedFile>(['file', id]);
if(fileObject.value === null){
return null;
}
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", fileObject.value.path]);
const newFile: FileMetaData = {path: newPath, id: fileObject.value.id, bodySize: fileObject.value.bodySize};
await this.kv.atomic()
.check(fileObject)
.check(idx)
.delete(idx.key)
.set(["file_index_path", newPath], idx.value)
.set(fileObject.key, newFile)
.commit()
return this.getMetaDataByID(id);
}
async renameByPath(originalPath: string, newPath: string): Promise<FileMetaData|null> {
const idx = await this.kv.get<SerializedFilePathIndex>(["file_index_path", originalPath]);
if(idx.value == null){
return null;
}
return this.renameById(idx.value.fileId, newPath);
}
}