@travetto/model-memory 7.1.4 → 8.0.0-alpha.1
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 +30 -45
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-memory",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.1",
|
|
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.1",
|
|
30
|
+
"@travetto/di": "^8.0.0-alpha.1",
|
|
31
|
+
"@travetto/model": "^8.0.0-alpha.1",
|
|
32
|
+
"@travetto/schema": "^8.0.0-alpha.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/cli": "^
|
|
36
|
-
"@travetto/test": "^
|
|
35
|
+
"@travetto/cli": "^8.0.0-alpha.1",
|
|
36
|
+
"@travetto/test": "^8.0.0-alpha.1"
|
|
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
|
-
import { Injectable } from '@travetto/di';
|
|
5
|
+
import { Injectable, PostConstruct } 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 {
|
|
@@ -148,7 +130,8 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
148
130
|
throw new NotFoundError(cls, key);
|
|
149
131
|
}
|
|
150
132
|
|
|
151
|
-
|
|
133
|
+
@PostConstruct()
|
|
134
|
+
async initializeClient(): Promise<void> {
|
|
152
135
|
await ModelStorageUtil.storageInitialization(this);
|
|
153
136
|
ModelExpiryUtil.registerCull(this);
|
|
154
137
|
|
|
@@ -233,33 +216,35 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
233
216
|
}
|
|
234
217
|
|
|
235
218
|
// Blob Support
|
|
236
|
-
async upsertBlob(location: string, input:
|
|
237
|
-
if (!overwrite && await this.
|
|
219
|
+
async upsertBlob(location: string, input: BinaryType, metadata?: BinaryMetadata, overwrite = true): Promise<void> {
|
|
220
|
+
if (!overwrite && await this.getBlobMetadata(location).then(() => true, () => false)) {
|
|
238
221
|
return;
|
|
239
222
|
}
|
|
240
|
-
|
|
241
|
-
const [stream, blobMeta] = await ModelBlobUtil.getInput(input, meta);
|
|
223
|
+
const resolved = await BinaryMetadataUtil.compute(input, metadata);
|
|
242
224
|
const blobs = this.#getStore(ModelBlobNamespace);
|
|
243
225
|
const metaContent = this.#getStore(ModelBlobMetaNamespace);
|
|
244
|
-
metaContent.set(location,
|
|
245
|
-
blobs.set(location, await
|
|
226
|
+
metaContent.set(location, JSONUtil.toBinaryArray(resolved));
|
|
227
|
+
blobs.set(location, await BinaryUtil.toBinaryArray(input));
|
|
246
228
|
}
|
|
247
229
|
|
|
248
230
|
async getBlob(location: string, range?: ByteRange): Promise<Blob> {
|
|
231
|
+
|
|
249
232
|
const blobs = this.#find(ModelBlobNamespace, location, 'notfound');
|
|
250
|
-
|
|
251
|
-
|
|
233
|
+
|
|
234
|
+
let data = blobs.get(location)!;
|
|
235
|
+
const final = range ? BinaryMetadataUtil.enforceRange(range, { size: data.byteLength }) : undefined;
|
|
236
|
+
|
|
252
237
|
if (final) {
|
|
253
|
-
|
|
238
|
+
data = BinaryUtil.sliceByteArray(data, final.start, final.end + 1);
|
|
254
239
|
}
|
|
255
|
-
|
|
256
|
-
|
|
240
|
+
|
|
241
|
+
const metadata = await this.getBlobMetadata(location);
|
|
242
|
+
return BinaryMetadataUtil.makeBlob(data, { ...metadata, range: final });
|
|
257
243
|
}
|
|
258
244
|
|
|
259
|
-
async
|
|
245
|
+
async getBlobMetadata(location: string): Promise<BinaryMetadata> {
|
|
260
246
|
const metaContent = this.#find(ModelBlobMetaNamespace, location, 'notfound');
|
|
261
|
-
|
|
262
|
-
return meta;
|
|
247
|
+
return JSONUtil.fromBinaryArray(metaContent.get(location)!);
|
|
263
248
|
}
|
|
264
249
|
|
|
265
250
|
async deleteBlob(location: string): Promise<void> {
|
|
@@ -273,9 +258,9 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
273
258
|
}
|
|
274
259
|
}
|
|
275
260
|
|
|
276
|
-
async
|
|
261
|
+
async updateBlobMetadata(location: string, metadata: BinaryMetadata): Promise<void> {
|
|
277
262
|
const metaContent = this.#getStore(ModelBlobMetaNamespace);
|
|
278
|
-
metaContent.set(location,
|
|
263
|
+
metaContent.set(location, JSONUtil.toBinaryArray(metadata));
|
|
279
264
|
}
|
|
280
265
|
|
|
281
266
|
// Expiry
|