/
storage_manager.ts
148 lines (125 loc) · 5 KB
/
storage_manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import type { Dictionary, StorageClient } from '@crawlee/types';
import { Configuration } from '../configuration';
import type { Constructor } from '../typedefs';
const DEFAULT_ID_CONFIG_KEYS = {
Dataset: 'defaultDatasetId',
KeyValueStore: 'defaultKeyValueStoreId',
RequestQueue: 'defaultRequestQueueId',
} as const;
export interface IStorage {
id: string;
name?: string;
}
/**
* StorageManager takes care of opening remote or local storages.
* @ignore
*/
export class StorageManager<T extends IStorage = IStorage> {
private static readonly storageManagers = new Map<Constructor, StorageManager>();
private readonly name: 'Dataset' | 'KeyValueStore' | 'RequestQueue';
private readonly StorageConstructor: Constructor<T> & { name: string };
private readonly cache = new Map<string, T>();
constructor(
StorageConstructor: Constructor<T>,
private readonly config = Configuration.getGlobalConfig(),
) {
this.StorageConstructor = StorageConstructor;
this.name = this.StorageConstructor.name as 'Dataset' | 'KeyValueStore' | 'RequestQueue';
}
static openStorage<T extends IStorage>(
storageClass: Constructor<T>,
idOrName?: string,
client?: StorageClient,
config = Configuration.getGlobalConfig(),
): Promise<T> {
return this.getManager(storageClass, config).openStorage(idOrName, client);
}
static getManager<T extends IStorage>(
storageClass: Constructor<T>,
config = Configuration.getGlobalConfig(),
): StorageManager<T> {
if (!this.storageManagers.has(storageClass)) {
const manager = new StorageManager(storageClass, config);
this.storageManagers.set(storageClass, manager);
}
return this.storageManagers.get(storageClass) as StorageManager<T>;
}
/** @internal */
static clearCache(): void {
this.storageManagers.forEach((manager) => {
if (manager.name === 'KeyValueStore') {
manager.cache.forEach((item) => {
(item as Dictionary).clearCache?.();
});
}
});
this.storageManagers.clear();
}
async openStorage(idOrName?: string | null, client?: StorageClient): Promise<T> {
if (!idOrName) {
const defaultIdConfigKey = DEFAULT_ID_CONFIG_KEYS[this.name];
idOrName = this.config.get(defaultIdConfigKey) as string;
}
const cacheKey = idOrName;
let storage = this.cache.get(cacheKey);
if (!storage) {
client ??= this.config.getStorageClient();
const storageObject = await this._getOrCreateStorage(idOrName, this.name, client);
storage = new this.StorageConstructor({
id: storageObject.id,
name: storageObject.name,
client,
});
this._addStorageToCache(storage);
}
return storage;
}
closeStorage(storage: { id: string; name?: string }): void {
const idKey = storage.id;
this.cache.delete(idKey);
if (storage.name) {
const nameKey = storage.name;
this.cache.delete(nameKey);
}
}
/**
* Helper function that first requests storage by ID and if storage doesn't exist then gets it by name.
*/
protected async _getOrCreateStorage(storageIdOrName: string, storageConstructorName: string, apiClient: StorageClient) {
const {
createStorageClient,
createStorageCollectionClient,
} = this._getStorageClientFactories(apiClient, storageConstructorName);
const storageClient = createStorageClient(storageIdOrName);
const existingStorage = await storageClient.get();
if (existingStorage) return existingStorage;
const storageCollectionClient = createStorageCollectionClient();
return storageCollectionClient.getOrCreate(storageIdOrName);
}
protected _getStorageClientFactories(client: StorageClient, storageConstructorName: string) {
// Dataset => dataset
const clientName = storageConstructorName[0].toLowerCase() + storageConstructorName.slice(1) as ClientNames;
// dataset => datasets
const collectionClientName = `${clientName}s` as ClientCollectionNames;
return {
createStorageClient: client[clientName!].bind(client),
createStorageCollectionClient: client[collectionClientName!].bind(client),
};
}
protected _addStorageToCache(storage: T): void {
const idKey = storage.id;
this.cache.set(idKey, storage);
if (storage.name) {
const nameKey = storage.name;
this.cache.set(nameKey, storage);
}
}
}
type ClientNames = 'dataset' | 'keyValueStore' | 'requestQueue';
type ClientCollectionNames = 'datasets' | 'keyValueStores' | 'requestQueues';
export interface StorageManagerOptions {
/**
* SDK configuration instance, defaults to the static register.
*/
config?: Configuration;
}