Deleted dead code.

This commit is contained in:
didier amyot committed 2023-06-13 08:53:26 -04:00
1 parent eae420a5a4
commit 8dcd8cc425
4 files changed
+51 -70

No files matched your search

+19 -2
View File
@@ -1,14 +1,31 @@
import { Context, Router } from './deps.ts';
import { FileService } from './services/mod.ts';
import { FileMetaData, FileService } from './services/mod.ts';
import { readerFromStreamReader } from './deps.ts';
import { asyncIterableToArray } from './utils.ts';
type Handler = (ctx: Context) => void;
class FileDTO {
readonly path: string;
readonly id: string;
readonly size: number;
constructor(path: string, id: string, size: number){
this.path = path;
this.id = id;
this.size = size;
}
static fromFileMetaData(fm: FileMetaData): FileDTO{
return new FileDTO();
}
}
function listFileHandlerFactory(fileService: FileService): Handler {
return async (ctx: Context) => {
ctx.response.type = 'application/json';
ctx.response.body = await asyncIterableToArray(fileService.list());
ctx.response.body = (await asyncIterableToArray(fileService.list())).map(FileDTO.fromFileMetaData);
};
}
+28 -63
View File
@@ -4,11 +4,16 @@ import { BlobService, FileMetaData, FileService } from './interfaces.ts';
import { readableStreamFromReader, readerFromStreamReader } from 'https://deno.land/std@0.191.0/streams/mod.ts';
enum CompressionAlgorithm{
UNCOMPRESSED = 'uncompressed',
GZIP = 'gzip',
}
interface SerializedFile {
id: string;
path: string;
contentSize: number;
compression: string;
compression: CompressionAlgorithm;
}
interface SerializedFilePathIndex {
@@ -24,65 +29,42 @@ export class DenoKVBlobService implements BlobService {
this.blobSize = blobSize;
}
private async sha1(msgUint8: Uint8Array): Promise<string> {
const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8);
private async sha1(content: Uint8Array): Promise<string> {
const hashBuffer = await crypto.subtle.digest('SHA-1', content);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray
.map((b) => b.toString(16).padStart(2, '0'))
.join(''); // convert bytes to hex string
}
private *splitText(content: Uint8Array): Generator<Uint8Array> {
let blobCount = 0;
let txt = content.slice(
blobCount * this.blobSize,
(blobCount + 1) * this.blobSize,
);
while (txt.length > 0) {
yield txt;
blobCount += 1;
txt = content.slice(
blobCount * this.blobSize,
(blobCount + 1) * this.blobSize,
);
}
}
private async unsafePersistBlob(mainIndex: string, index: number, content: Uint8Array): Promise<void> {
private async unsafePersistChunck(blobIndex: string, chunckIndex: number, content: Uint8Array): Promise<void> {
if (content.length > this.blobSize) {
throw new Error('Persisted a too large blob');
}
const blob_id = await this.sha1(content);
await this.kv.set(['blob_index', mainIndex, index], blob_id);
await this.kv.set(['blob_index', blobIndex, chunckIndex], blob_id);
await this.kv.set(['blob', blob_id], content);
}
async persistBlob(index: string, content: Uint8Array): Promise<number> {
let blobCount = 0;
let size = 0;
for (const txt of this.splitText(content)) {
await this.unsafePersistBlob(index, blobCount, txt);
size += txt.length;
blobCount += 1;
}
return size;
persistBlob(index: string, content: Uint8Array): Promise<number> {
return this.persistReader(index, new Buffer(content));
}
async persistReader(index: string, content: Deno.Reader): Promise<number> {
let i = 0;
let size = 0;
for await (const chunk of iterateReader(content, { bufSize: this.blobSize })) {
await this.unsafePersistBlob(index, i, chunk);
size += chunk.length;
for await (const chunck of iterateReader(content, { bufSize: this.blobSize })) {
await this.unsafePersistChunck(index, i, chunck);
size += chunck.length;
i++;
}
return size;
}
private async *iterBlob(index: string): AsyncIterable<Uint8Array> {
private async *iterBlob(blobIndex: string): AsyncIterable<Uint8Array> {
for await (
const itm of this.kv.list<string>({ prefix: ['blob_index', index] })
const itm of this.kv.list<string>({ prefix: ['blob_index', blobIndex] })
) {
const foo = await this.kv.get<Uint8Array>(['blob', itm.value]);
if (foo.value === null) {
@@ -102,20 +84,7 @@ export class DenoKVBlobService implements BlobService {
return readerFromIterable(this.iterBlob(index));
}
private async *iterRawBlobs(index: string): AsyncIterable<Uint8Array> {
const prefix = ['blob_index', index];
for await (const itm of this.kv.list<string>({ prefix })) {
const foo = await this.kv.get<Uint8Array>(['blob', itm.value]);
if (foo.value === null) {
throw new Error(
`Inconsistant blob index for key "${foo.value}", this is very bad.`,
);
}
yield foo.value;
}
}
async getNumberOfBlobs(index: string): Promise<number> {
async getNumberOfChunks(index: string): Promise<number> {
let ret = 0;
for await (
const _itm of this.kv.list<string>({ prefix: ['blob_index', index] })
@@ -124,10 +93,6 @@ export class DenoKVBlobService implements BlobService {
}
return ret;
}
streamBlob(index: string): Deno.Reader {
return readerFromIterable(this.iterRawBlobs(index));
}
}
export class DenoKVFileService implements FileService {
@@ -143,23 +108,21 @@ export class DenoKVFileService implements FileService {
path: string,
content: string | Uint8Array,
): Promise<FileMetaData> {
if (content instanceof Uint8Array) {
return this.createFromReader(path, new Buffer(content));
}
return this.createFromReader(path, new StringReader(content));
const reader = content instanceof Uint8Array ? new Buffer(content) : new StringReader(content);
return this.createFromReader(path, reader);
}
async createFromReader(path: string, content: Deno.Reader): Promise<FileMetaData> {
const newId = crypto.randomUUID();
const compressedContent = readableStreamFromReader(content).pipeThrough(new CompressionStream('gzip'))
const compressedContent = readableStreamFromReader(content).pipeThrough(new CompressionStream(CompressionAlgorithm.GZIP))
.getReader();
const size = await this.blobService.persistReader(newId, readerFromStreamReader(compressedContent));
const serializedFile: SerializedFile = {
id: newId,
path,
contentSize: size,
compression: 'gzip',
compression: CompressionAlgorithm.GZIP,
};
const idx: SerializedFilePathIndex = { fileId: newId };
await this.kv.set(['file', newId], serializedFile);
@@ -211,11 +174,13 @@ export class DenoKVFileService implements FileService {
}
private decompressContent(serializedFile: SerializedFile, content: Deno.Reader): Deno.Reader {
if (serializedFile.compression == 'gzip') {
const ret = readableStreamFromReader(content).pipeThrough(new DecompressionStream('gzip')).getReader();
return readerFromStreamReader(ret);
if(serializedFile.compression === CompressionAlgorithm.UNCOMPRESSED){
return content;
}
return content;
const decompresser = new DecompressionStream(serializedFile.compression);
const ret = readableStreamFromReader(content).pipeThrough(decompresser).getReader();
return readerFromStreamReader(ret);
}
async getRawContentByPath(path: string): Promise<Uint8Array | null> {
+1 -2
View File
@@ -35,6 +35,5 @@ export interface BlobService {
persistReader(index: string, content: Deno.Reader): Promise<number>;
readAllBlob(index: string): Promise<Uint8Array>;
getBlob(index: string): Deno.Reader;
getNumberOfBlobs(index: string): Promise<number>;
streamBlob(index: string): Deno.Reader;
getNumberOfChunks(index: string): Promise<number>;
}