-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add setBlob and deleteBlob to batched atomic
Closes #7
- Loading branch information
Showing
6 changed files
with
212 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* This is an internal module which contains some of the blob writing | ||
* functionality and is not part of the public API of kv-toolbox. | ||
* | ||
* @module | ||
*/ | ||
|
||
import { type BatchedAtomicOperation } from "./batched_atomic.ts"; | ||
|
||
export const BLOB_KEY = "__kv_toolbox_blob__"; | ||
export const CHUNK_SIZE = 63_000; | ||
|
||
function deleteKeys( | ||
operation: BatchedAtomicOperation, | ||
key: Deno.KvKey, | ||
count: number, | ||
length: number, | ||
): BatchedAtomicOperation { | ||
while (++count <= length) { | ||
operation.delete([...key, BLOB_KEY, count]); | ||
} | ||
return operation; | ||
} | ||
|
||
function writeArrayBuffer( | ||
operation: BatchedAtomicOperation, | ||
key: Deno.KvKey, | ||
blob: ArrayBufferLike, | ||
start = 0, | ||
options?: { expireIn?: number }, | ||
): [count: number, operation: BatchedAtomicOperation] { | ||
const buffer = new Uint8Array(blob); | ||
let offset = 0; | ||
let count = start; | ||
while (buffer.byteLength > offset) { | ||
count++; | ||
const chunk = buffer.subarray(offset, offset + CHUNK_SIZE); | ||
operation.set([...key, BLOB_KEY, count], chunk, options); | ||
offset += CHUNK_SIZE; | ||
} | ||
return [count, operation]; | ||
} | ||
|
||
async function writeStream( | ||
operation: BatchedAtomicOperation, | ||
key: Deno.KvKey, | ||
stream: ReadableStream<Uint8Array>, | ||
options?: { expireIn?: number }, | ||
): Promise<[count: number, operation: BatchedAtomicOperation]> { | ||
let start = 0; | ||
for await (const chunk of stream) { | ||
[start, operation] = writeArrayBuffer( | ||
operation, | ||
key, | ||
chunk, | ||
start, | ||
options, | ||
); | ||
} | ||
return [start, operation]; | ||
} | ||
|
||
export async function setBlob( | ||
operation: BatchedAtomicOperation, | ||
key: Deno.KvKey, | ||
blob: ArrayBufferLike | ReadableStream<Uint8Array>, | ||
itemCount: number, | ||
options?: { expireIn?: number }, | ||
) { | ||
let count; | ||
if (blob instanceof ReadableStream) { | ||
[count, operation] = await writeStream(operation, key, blob, options); | ||
} else { | ||
[count, operation] = writeArrayBuffer(operation, key, blob, 0, options); | ||
} | ||
operation = deleteKeys(operation, key, count, itemCount); | ||
return operation; | ||
} |