@@ -120,11 +120,16 @@ func (db *PersistentKB) save() error {
120
120
return os .WriteFile (db .path , data , 0644 )
121
121
}
122
122
123
- // repopulate reinitializes the persistent knowledge base with the files that were added to it.
124
- func (db * PersistentKB ) repopulate () error {
123
+ func (db * PersistentKB ) Count () int {
125
124
db .Lock ()
126
125
defer db .Unlock ()
127
126
127
+ return db .Engine .Count ()
128
+ }
129
+
130
+ // repopulate reinitializes the persistent knowledge base with the files that were added to it.
131
+ func (db * PersistentKB ) repopulate () error {
132
+
128
133
if err := db .Engine .Reset (); err != nil {
129
134
return fmt .Errorf ("failed to reset engine: %w" , err )
130
135
}
@@ -141,6 +146,13 @@ func (db *PersistentKB) repopulate() error {
141
146
return nil
142
147
}
143
148
149
+ func (db * PersistentKB ) Repopulate () error {
150
+ db .Lock ()
151
+ defer db .Unlock ()
152
+
153
+ return db .repopulate ()
154
+ }
155
+
144
156
// Store stores an entry in the persistent knowledge base.
145
157
func (db * PersistentKB ) ListDocuments () []string {
146
158
db .Lock ()
@@ -173,6 +185,10 @@ func (db *PersistentKB) Store(entry string, metadata map[string]string) error {
173
185
db .Lock ()
174
186
defer db .Unlock ()
175
187
188
+ return db .storeFile (entry , metadata )
189
+ }
190
+
191
+ func (db * PersistentKB ) storeFile (entry string , metadata map [string ]string ) error {
176
192
fileName := filepath .Base (entry )
177
193
178
194
// copy file to assetDir (if it's a file)
@@ -193,19 +209,17 @@ func (db *PersistentKB) Store(entry string, metadata map[string]string) error {
193
209
}
194
210
195
211
func (db * PersistentKB ) StoreOrReplace (entry string , metadata map [string ]string ) error {
196
- db .Lock ()
197
212
fileName := filepath .Base (entry )
198
213
_ , ok := db .index [fileName ]
199
- db .Unlock ()
200
214
// Check if we have it already in the index
201
215
if ok {
202
216
xlog .Info ("Data already exists for entry" , "entry" , entry , "index" , db .index )
203
- if err := db .RemoveEntry (fileName ); err != nil {
217
+ if err := db .removeFileEntry (fileName ); err != nil {
204
218
return fmt .Errorf ("failed to remove entry: %w" , err )
205
219
}
206
220
}
207
221
208
- return db .Store (entry , metadata )
222
+ return db .storeFile (entry , metadata )
209
223
}
210
224
211
225
func (db * PersistentKB ) store (metadata map [string ]string , files ... string ) ([]engine.Result , error ) {
@@ -231,8 +245,14 @@ func (db *PersistentKB) store(metadata map[string]string, files ...string) ([]en
231
245
return results , nil
232
246
}
233
247
234
- // RemoveEntry removes an entry from the persistent knowledge base.
235
248
func (db * PersistentKB ) RemoveEntry (entry string ) error {
249
+ db .Lock ()
250
+ defer db .Unlock ()
251
+
252
+ return db .removeFileEntry (entry )
253
+ }
254
+
255
+ func (db * PersistentKB ) removeFileEntry (entry string ) error {
236
256
237
257
xlog .Info ("Removing entry" , "entry" , entry )
238
258
if os .Getenv ("LOCALRECALL_REPOPULATE_DELETE" ) != "true" {
@@ -261,25 +281,20 @@ func (db *PersistentKB) RemoveEntry(entry string) error {
261
281
}
262
282
}
263
283
264
- db .Lock ()
265
-
266
284
xlog .Info ("Deleting entry from index" , "entry" , entry )
267
285
delete (db .index , entry )
268
286
269
287
xlog .Info ("Removing entry from disk" , "file" , e )
270
288
os .Remove (e )
271
- db .Unlock ()
272
289
return db .save ()
273
290
}
274
291
275
- db .Lock ()
276
292
for e := range db .index {
277
293
if e == entry {
278
294
os .Remove (filepath .Join (db .assetDir , e ))
279
295
break
280
296
}
281
297
}
282
- db .Unlock ()
283
298
284
299
// TODO: this is suboptimal, but currently chromem does not support deleting single entities
285
300
return db .repopulate ()
0 commit comments