@travetto/model-memory 7.1.4 → 8.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -7
- package/src/service.ts +27 -43
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-memory",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Memory backing for the travetto model module.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"directory": "module/model-memory"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/config": "^
|
|
30
|
-
"@travetto/di": "^
|
|
31
|
-
"@travetto/model": "^
|
|
32
|
-
"@travetto/schema": "^
|
|
29
|
+
"@travetto/config": "^8.0.0-alpha.0",
|
|
30
|
+
"@travetto/di": "^8.0.0-alpha.0",
|
|
31
|
+
"@travetto/model": "^8.0.0-alpha.0",
|
|
32
|
+
"@travetto/schema": "^8.0.0-alpha.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/cli": "^
|
|
36
|
-
"@travetto/test": "^
|
|
35
|
+
"@travetto/cli": "^8.0.0-alpha.0",
|
|
36
|
+
"@travetto/test": "^8.0.0-alpha.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependenciesMeta": {
|
|
39
39
|
"@travetto/cli": {
|
package/src/service.ts
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
import { Readable } from 'node:stream';
|
|
2
|
-
import { buffer as toBuffer } from 'node:stream/consumers';
|
|
3
|
-
|
|
4
1
|
import {
|
|
5
|
-
type Class, type TimeSpan, type DeepPartial, castTo, type
|
|
6
|
-
type ByteRange, type
|
|
2
|
+
type Class, type TimeSpan, type DeepPartial, castTo, type BinaryMetadata,
|
|
3
|
+
type ByteRange, type BinaryType, BinaryUtil, type BinaryArray, JSONUtil, BinaryMetadataUtil
|
|
7
4
|
} from '@travetto/runtime';
|
|
8
5
|
import { Injectable } from '@travetto/di';
|
|
9
6
|
import { Config } from '@travetto/config';
|
|
10
7
|
import {
|
|
11
8
|
type ModelType, type IndexConfig, type ModelCrudSupport, type ModelExpirySupport, type ModelStorageSupport, type ModelIndexedSupport,
|
|
12
9
|
ModelRegistryIndex, NotFoundError, ExistsError, type OptionalId, type ModelBlobSupport,
|
|
13
|
-
ModelCrudUtil, ModelExpiryUtil, ModelIndexedUtil, ModelStorageUtil
|
|
10
|
+
ModelCrudUtil, ModelExpiryUtil, ModelIndexedUtil, ModelStorageUtil
|
|
14
11
|
} from '@travetto/model';
|
|
15
12
|
|
|
16
13
|
const ModelBlobNamespace = '__blobs';
|
|
17
14
|
const ModelBlobMetaNamespace = `${ModelBlobNamespace}_meta`;
|
|
18
15
|
|
|
19
|
-
type StoreType = Map<string,
|
|
16
|
+
type StoreType = Map<string, BinaryArray>;
|
|
20
17
|
|
|
21
18
|
@Config('model.memory')
|
|
22
19
|
export class MemoryModelConfig {
|
|
@@ -60,10 +57,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
60
57
|
|
|
61
58
|
#getStore<T extends ModelType>(cls: Class<T> | string): StoreType {
|
|
62
59
|
const key = typeof cls === 'string' ? cls : ModelRegistryIndex.getStoreName(cls);
|
|
63
|
-
|
|
64
|
-
this.#store.set(key, new Map());
|
|
65
|
-
}
|
|
66
|
-
return this.#store.get(key)!;
|
|
60
|
+
return this.#store.getOrInsert(key, new Map());
|
|
67
61
|
}
|
|
68
62
|
|
|
69
63
|
#find<T extends ModelType>(cls: Class<T> | string, id?: string, errorState?: 'exists' | 'notfound'): StoreType {
|
|
@@ -95,23 +89,11 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
95
89
|
for (const idx of ModelRegistryIndex.getIndices(cls, ['sorted', 'unsorted'])) {
|
|
96
90
|
const idxName = indexName(cls, idx);
|
|
97
91
|
const { key, sort } = ModelIndexedUtil.computeIndexKey(cls, idx, castTo(item));
|
|
98
|
-
let index = this.#indices[idx.type].get(idxName)?.get(key);
|
|
99
|
-
|
|
100
|
-
if (!index) {
|
|
101
|
-
if (!this.#indices[idx.type].has(idxName)) {
|
|
102
|
-
this.#indices[idx.type].set(idxName, new Map());
|
|
103
|
-
}
|
|
104
|
-
if (idx.type === 'sorted') {
|
|
105
|
-
this.#indices[idx.type].get(idxName)!.set(key, index = new Map());
|
|
106
|
-
} else {
|
|
107
|
-
this.#indices[idx.type].get(idxName)!.set(key, index = new Set());
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
92
|
|
|
111
|
-
if (
|
|
112
|
-
|
|
93
|
+
if (idx.type === 'sorted') {
|
|
94
|
+
this.#indices[idx.type].getOrInsert(idxName, new Map()).getOrInsert(key, new Map()).set(item.id, +sort!);
|
|
113
95
|
} else {
|
|
114
|
-
|
|
96
|
+
this.#indices[idx.type].getOrInsert(idxName, new Map()).getOrInsert(key, new Set()).add(item.id);
|
|
115
97
|
}
|
|
116
98
|
}
|
|
117
99
|
}
|
|
@@ -122,7 +104,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
122
104
|
const store = this.#getStore(cls);
|
|
123
105
|
await this.#removeIndices(cls, item.id);
|
|
124
106
|
if (action === 'write') {
|
|
125
|
-
store.set(item.id,
|
|
107
|
+
store.set(item.id, JSONUtil.toBinaryArray(item));
|
|
126
108
|
await this.#writeIndices(cls, item);
|
|
127
109
|
return item;
|
|
128
110
|
} else {
|
|
@@ -233,33 +215,35 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
233
215
|
}
|
|
234
216
|
|
|
235
217
|
// Blob Support
|
|
236
|
-
async upsertBlob(location: string, input:
|
|
237
|
-
if (!overwrite && await this.
|
|
218
|
+
async upsertBlob(location: string, input: BinaryType, metadata?: BinaryMetadata, overwrite = true): Promise<void> {
|
|
219
|
+
if (!overwrite && await this.getBlobMetadata(location).then(() => true, () => false)) {
|
|
238
220
|
return;
|
|
239
221
|
}
|
|
240
|
-
|
|
241
|
-
const [stream, blobMeta] = await ModelBlobUtil.getInput(input, meta);
|
|
222
|
+
const resolved = await BinaryMetadataUtil.compute(input, metadata);
|
|
242
223
|
const blobs = this.#getStore(ModelBlobNamespace);
|
|
243
224
|
const metaContent = this.#getStore(ModelBlobMetaNamespace);
|
|
244
|
-
metaContent.set(location,
|
|
245
|
-
blobs.set(location, await
|
|
225
|
+
metaContent.set(location, JSONUtil.toBinaryArray(resolved));
|
|
226
|
+
blobs.set(location, await BinaryUtil.toBinaryArray(input));
|
|
246
227
|
}
|
|
247
228
|
|
|
248
229
|
async getBlob(location: string, range?: ByteRange): Promise<Blob> {
|
|
230
|
+
|
|
249
231
|
const blobs = this.#find(ModelBlobNamespace, location, 'notfound');
|
|
250
|
-
|
|
251
|
-
|
|
232
|
+
|
|
233
|
+
let data = blobs.get(location)!;
|
|
234
|
+
const final = range ? BinaryMetadataUtil.enforceRange(range, { size: data.byteLength }) : undefined;
|
|
235
|
+
|
|
252
236
|
if (final) {
|
|
253
|
-
|
|
237
|
+
data = BinaryUtil.sliceByteArray(data, final.start, final.end + 1);
|
|
254
238
|
}
|
|
255
|
-
|
|
256
|
-
|
|
239
|
+
|
|
240
|
+
const metadata = await this.getBlobMetadata(location);
|
|
241
|
+
return BinaryMetadataUtil.makeBlob(data, { ...metadata, range: final });
|
|
257
242
|
}
|
|
258
243
|
|
|
259
|
-
async
|
|
244
|
+
async getBlobMetadata(location: string): Promise<BinaryMetadata> {
|
|
260
245
|
const metaContent = this.#find(ModelBlobMetaNamespace, location, 'notfound');
|
|
261
|
-
|
|
262
|
-
return meta;
|
|
246
|
+
return JSONUtil.fromBinaryArray(metaContent.get(location)!);
|
|
263
247
|
}
|
|
264
248
|
|
|
265
249
|
async deleteBlob(location: string): Promise<void> {
|
|
@@ -273,9 +257,9 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
273
257
|
}
|
|
274
258
|
}
|
|
275
259
|
|
|
276
|
-
async
|
|
260
|
+
async updateBlobMetadata(location: string, metadata: BinaryMetadata): Promise<void> {
|
|
277
261
|
const metaContent = this.#getStore(ModelBlobMetaNamespace);
|
|
278
|
-
metaContent.set(location,
|
|
262
|
+
metaContent.set(location, JSONUtil.toBinaryArray(metadata));
|
|
279
263
|
}
|
|
280
264
|
|
|
281
265
|
// Expiry
|