-
Notifications
You must be signed in to change notification settings - Fork 315
/
KVDocumentStore.ts
194 lines (169 loc) · 5.88 KB
/
KVDocumentStore.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import _ from "lodash";
import type { BaseNode } from "../../Node.js";
import { ObjectType } from "../../Node.js";
import { DEFAULT_NAMESPACE } from "../constants.js";
import type { BaseKVStore } from "../kvStore/types.js";
import type { RefDocInfo } from "./types.js";
import { BaseDocumentStore } from "./types.js";
import { docToJson, jsonToDoc } from "./utils.js";
type DocMetaData = { docHash: string; refDocId?: string };
export class KVDocumentStore extends BaseDocumentStore {
private kvstore: BaseKVStore;
private nodeCollection: string;
private refDocCollection: string;
private metadataCollection: string;
constructor(kvstore: BaseKVStore, namespace: string = DEFAULT_NAMESPACE) {
super();
this.kvstore = kvstore;
this.nodeCollection = `${namespace}/data`;
this.refDocCollection = `${namespace}/ref_doc_info`;
this.metadataCollection = `${namespace}/metadata`;
}
async docs(): Promise<Record<string, BaseNode>> {
const jsonDict = await this.kvstore.getAll(this.nodeCollection);
const docs: Record<string, BaseNode> = {};
for (const key in jsonDict) {
docs[key] = jsonToDoc(jsonDict[key] as Record<string, any>);
}
return docs;
}
async addDocuments(
docs: BaseNode[],
allowUpdate: boolean = true,
): Promise<void> {
for (let idx = 0; idx < docs.length; idx++) {
const doc = docs[idx];
if (doc.id_ === null) {
throw new Error("doc_id not set");
}
if (!allowUpdate && (await this.documentExists(doc.id_))) {
throw new Error(
`doc_id ${doc.id_} already exists. Set allow_update to True to overwrite.`,
);
}
const nodeKey = doc.id_;
const data = docToJson(doc);
await this.kvstore.put(nodeKey, data, this.nodeCollection);
const metadata: DocMetaData = { docHash: doc.hash };
if (doc.getType() === ObjectType.TEXT && doc.sourceNode !== undefined) {
const refDocInfo = (await this.getRefDocInfo(
doc.sourceNode.nodeId,
)) || {
nodeIds: [],
extraInfo: {},
};
refDocInfo.nodeIds.push(doc.id_);
if (_.isEmpty(refDocInfo.extraInfo)) {
refDocInfo.extraInfo = {};
}
await this.kvstore.put(
doc.sourceNode.nodeId,
refDocInfo,
this.refDocCollection,
);
metadata.refDocId = doc.sourceNode.nodeId!;
}
this.kvstore.put(nodeKey, metadata, this.metadataCollection);
}
}
async getDocument(
docId: string,
raiseError: boolean = true,
): Promise<BaseNode | undefined> {
const json = await this.kvstore.get(docId, this.nodeCollection);
if (_.isNil(json)) {
if (raiseError) {
throw new Error(`docId ${docId} not found.`);
} else {
return;
}
}
return jsonToDoc(json);
}
async getRefDocInfo(refDocId: string): Promise<RefDocInfo | undefined> {
const refDocInfo = await this.kvstore.get(refDocId, this.refDocCollection);
return refDocInfo ? (_.clone(refDocInfo) as RefDocInfo) : undefined;
}
async getAllRefDocInfo(): Promise<Record<string, RefDocInfo> | undefined> {
const refDocInfos = await this.kvstore.getAll(this.refDocCollection);
if (_.isNil(refDocInfos)) {
return;
}
return refDocInfos as Record<string, RefDocInfo>;
}
async refDocExists(refDocId: string): Promise<boolean> {
return !_.isNil(await this.getRefDocInfo(refDocId));
}
async documentExists(docId: string): Promise<boolean> {
return !_.isNil(await this.kvstore.get(docId, this.nodeCollection));
}
private async removeRefDocNode(docId: string): Promise<void> {
const metadata = await this.kvstore.get(docId, this.metadataCollection);
if (metadata === null) {
return;
}
const refDocId = metadata.refDocId;
if (_.isNil(refDocId)) {
return;
}
const refDocInfo = await this.kvstore.get(refDocId, this.refDocCollection);
if (!_.isNil(refDocInfo)) {
!_.pull(refDocInfo.nodeIds, docId);
if (refDocInfo.nodeIds.length > 0) {
this.kvstore.put(refDocId, refDocInfo, this.refDocCollection);
}
this.kvstore.delete(refDocId, this.metadataCollection);
}
}
async deleteDocument(
docId: string,
raiseError: boolean = true,
removeRefDocNode: boolean = true,
): Promise<void> {
if (removeRefDocNode) {
await this.removeRefDocNode(docId);
}
const deleteSuccess = await this.kvstore.delete(docId, this.nodeCollection);
await this.kvstore.delete(docId, this.metadataCollection);
if (!deleteSuccess && raiseError) {
throw new Error(`doc_id ${docId} not found.`);
}
}
async deleteRefDoc(
refDocId: string,
raiseError: boolean = true,
): Promise<void> {
const refDocInfo = await this.getRefDocInfo(refDocId);
if (_.isNil(refDocInfo)) {
if (raiseError) {
throw new Error(`ref_doc_id ${refDocId} not found.`);
} else {
return;
}
}
for (const docId of refDocInfo.nodeIds) {
await this.deleteDocument(docId, false, false);
}
await this.kvstore.delete(refDocId, this.metadataCollection);
await this.kvstore.delete(refDocId, this.refDocCollection);
}
async setDocumentHash(docId: string, docHash: string): Promise<void> {
const metadata = { docHash: docHash };
await this.kvstore.put(docId, metadata, this.metadataCollection);
}
async getDocumentHash(docId: string): Promise<string | undefined> {
const metadata = await this.kvstore.get(docId, this.metadataCollection);
return _.get(metadata, "docHash");
}
async getAllDocumentHashes(): Promise<Record<string, string>> {
const hashes: Record<string, string> = {};
const metadataDocs = await this.kvstore.getAll(this.metadataCollection);
for (const docId in metadataDocs) {
const hash = await this.getDocumentHash(docId);
if (hash) {
hashes[hash] = docId;
}
}
return hashes;
}
}