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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use super::*;
use crate::error::{DbError, DbResult};
use crate::storage::index::{
extract_field_value, generate_ngrams, levenshtein_distance, tokenize, FulltextMatch,
};
use rust_rocksdb::WriteBatch;
use serde_json::Value;
use std::collections::HashMap;
impl Collection {
// ==================== Fulltext Index Operations ====================
/// Get all fulltext indexes
pub fn get_all_fulltext_indexes(&self) -> Vec<FulltextIndex> {
self.index_meta()
.expect("Column family should exist")
.fulltext
.clone()
}
/// Get a fulltext index by name
pub(crate) fn get_fulltext_index(&self, name: &str) -> Option<FulltextIndex> {
self.index_meta()?
.fulltext
.iter()
.find(|i| i.name == name)
.cloned()
}
/// Get a fulltext index that covers a specific field
pub fn get_fulltext_index_for_field(&self, field: &str) -> Option<FulltextIndex> {
let indexes = self.get_all_fulltext_indexes();
indexes
.into_iter()
.find(|idx| idx.fields.contains(&field.to_string()))
}
/// Create a fulltext index
pub fn create_fulltext_index(
&self,
name: String,
fields: Vec<String>,
min_length: Option<usize>,
) -> DbResult<()> {
let min_length = min_length.unwrap_or_else(default_min_length);
if self.get_fulltext_index(&name).is_some() {
return Err(DbError::InvalidDocument(format!(
"Fulltext Index '{}' already exists",
name
)));
}
let index = FulltextIndex {
name: name.clone(),
fields: fields.clone(),
min_length,
};
let index_bytes = serde_json::to_vec(&index)?;
// Store metadata
{
let db = &self.db;
let cf = db
.cf_handle(&self.name)
.expect("Column family should exist");
db.put_cf(&cf, Self::ft_meta_key(&name), &index_bytes)
.map_err(|e| {
DbError::InternalError(format!("Failed to create fulltext index: {}", e))
})?;
}
self.invalidate_index_meta();
// Build index
let docs = self.all();
let db = &self.db;
let cf = db
.cf_handle(&self.name)
.expect("Column family should exist");
let mut batch = WriteBatch::default();
let mut count = 0;
for doc in &docs {
let doc_value = doc.to_value();
for field in &fields {
let field_value = extract_field_value(&doc_value, field);
if let Some(text) = field_value.as_str() {
// Index terms
let terms = tokenize(text);
for term in &terms {
if term.len() >= min_length {
let term_key = Self::ft_term_key(&name, term, &doc.key);
batch.put_cf(&cf, term_key, doc.key.as_bytes());
}
}
// Index trigrams for fuzzy matching
let ngrams = generate_ngrams(text, NGRAM_SIZE);
for ngram in &ngrams {
let ngram_key = Self::ft_ngram_key(&name, ngram, &doc.key);
batch.put_cf(&cf, ngram_key, doc.key.as_bytes());
}
count += 1;
}
}
if count > 1000 {
db.write(&batch).map_err(|e| {
DbError::InternalError(format!("Failed to build fulltext index: {}", e))
})?;
batch = WriteBatch::default();
count = 0;
}
}
if count > 0 {
db.write(&batch).map_err(|e| {
DbError::InternalError(format!("Failed to build fulltext index: {}", e))
})?;
}
Ok(())
}
/// Drop a fulltext index
pub fn drop_fulltext_index(&self, name: &str) -> DbResult<()> {
if self.get_fulltext_index(name).is_none() {
return Err(DbError::InvalidDocument(format!(
"Fulltext Index '{}' not found",
name
)));
}
let db = &self.db;
let cf = db
.cf_handle(&self.name)
.expect("Column family should exist");
// Delete metadata
db.delete_cf(&cf, Self::ft_meta_key(name))
.map_err(|e| DbError::InternalError(format!("Failed to drop fulltext index: {}", e)))?;
self.invalidate_index_meta();
let mut batch = WriteBatch::default();
let mut count = 0;
// Delete ngrams
let prefix = format!("{}{}:", FT_PREFIX, name);
let iter = db.prefix_iterator_cf(&cf, prefix.as_bytes());
for result in iter {
if let Ok((key, _)) = result {
if key.starts_with(prefix.as_bytes()) {
batch.delete_cf(&cf, key);
count += 1;
} else {
break;
}
}
if count > 1000 {
db.write(&batch).map_err(|e| {
DbError::InternalError(format!("Failed to drop fulltext entries: {}", e))
})?;
batch = WriteBatch::default();
count = 0;
}
}
// Delete terms
let term_prefix = format!("{}{}:", FT_TERM_PREFIX, name);
let iter = db.prefix_iterator_cf(&cf, term_prefix.as_bytes());
for result in iter {
if let Ok((key, _)) = result {
if key.starts_with(term_prefix.as_bytes()) {
batch.delete_cf(&cf, key);
count += 1;
} else {
break; // Fixed from original loop which implied break
}
}
if count > 1000 {
db.write(&batch).map_err(|e| {
DbError::InternalError(format!("Failed to drop fulltext entries: {}", e))
})?;
batch = WriteBatch::default();
count = 0;
}
}
if count > 0 {
db.write(&batch).map_err(|e| {
DbError::InternalError(format!("Failed to drop fulltext entries: {}", e))
})?;
}
Ok(())
}
/// Update fulltext indexes on insert
#[allow(dead_code)]
pub(crate) fn update_fulltext_on_insert(
&self,
doc_key: &str,
doc_value: &Value,
) -> DbResult<()> {
let indexes = self.get_all_fulltext_indexes();
if indexes.is_empty() {
return Ok(());
}
let db = &self.db;
let cf = db
.cf_handle(&self.name)
.expect("Column family should exist");
let mut batch = WriteBatch::default();
for index in indexes {
for field in &index.fields {
let field_value = extract_field_value(doc_value, field);
if let Some(text) = field_value.as_str() {
let terms = tokenize(text);
for term in &terms {
if term.len() >= index.min_length {
let term_key = Self::ft_term_key(&index.name, term, doc_key);
batch.put_cf(&cf, term_key, doc_key.as_bytes());
}
}
let ngrams = generate_ngrams(text, NGRAM_SIZE);
for ngram in &ngrams {
let ngram_key = Self::ft_ngram_key(&index.name, ngram, doc_key);
batch.put_cf(&cf, ngram_key, doc_key.as_bytes());
}
}
}
}
db.write(&batch)
.map_err(|e| DbError::InternalError(format!("Failed to update fulltext index: {}", e)))
}
/// Update fulltext indexes on delete
#[allow(dead_code)]
pub(crate) fn update_fulltext_on_delete(
&self,
doc_key: &str,
doc_value: &Value,
) -> DbResult<()> {
let indexes = self.get_all_fulltext_indexes();
if indexes.is_empty() {
return Ok(());
}
let db = &self.db;
let cf = db
.cf_handle(&self.name)
.expect("Column family should exist");
let mut batch = WriteBatch::default();
for index in indexes {
for field in &index.fields {
let field_value = extract_field_value(doc_value, field);
if let Some(text) = field_value.as_str() {
let terms = tokenize(text);
for term in &terms {
if term.len() >= index.min_length {
let term_key = Self::ft_term_key(&index.name, term, doc_key);
batch.delete_cf(&cf, term_key);
}
}
let ngrams = generate_ngrams(text, NGRAM_SIZE);
for ngram in &ngrams {
let ngram_key = Self::ft_ngram_key(&index.name, ngram, doc_key);
batch.delete_cf(&cf, ngram_key);
}
}
}
}
db.write(&batch)
.map_err(|e| DbError::InternalError(format!("Failed to update fulltext index: {}", e)))
}
/// List fulltext indexes
pub fn list_fulltext_indexes(&self) -> Vec<FulltextIndex> {
self.get_all_fulltext_indexes()
}
/// Perform a fulltext search
pub fn fulltext_search(
&self,
query: &str,
fields: Option<Vec<String>>,
limit: usize,
) -> DbResult<Vec<FulltextMatch>> {
// 1. Identify relevant indexes
let all_indexes = self.get_all_fulltext_indexes();
let indexes: Vec<&FulltextIndex> = if let Some(target_fields) = &fields {
all_indexes
.iter()
.filter(|idx| idx.fields.iter().any(|f| target_fields.contains(f)))
.collect()
} else {
all_indexes.iter().collect()
};
if indexes.is_empty() {
return Ok(Vec::new());
}
// 2. Tokenize query
let query_terms = tokenize(query);
if query_terms.is_empty() {
return Ok(Vec::new());
}
// 3. Collect candidate documents (using term matching first)
let mut candidate_counts: HashMap<String, usize> = HashMap::new();
let db = &self.db;
let cf = db
.cf_handle(&self.name)
.expect("Column family should exist");
for index in &indexes {
for term in &query_terms {
if term.len() >= index.min_length {
// Exact term lookup
let prefix = format!("{}{}:{}:", FT_TERM_PREFIX, index.name, term);
let iter = db.prefix_iterator_cf(&cf, prefix.as_bytes());
for result in iter.flatten() {
let (key, _) = result;
if !key.starts_with(prefix.as_bytes()) {
break;
}
let key_str = String::from_utf8_lossy(&key);
// Key is "ft_term:<algo>:<term>:<doc_key>"
// Extract doc_key (last part)
let parts: Vec<&str> = key_str.split(':').collect();
if let Some(doc_key) = parts.last() {
*candidate_counts.entry(doc_key.to_string()).or_insert(0) += 1;
}
}
}
}
// Fuzzy lookup (trigrams) if strict term matching yielded few results?
// Or always? A proper implementation combines both.
// For now, let's keep it simple: if strict terms found candidates, score them.
// If not, maybe use ngrams?
// The original implementation might have been more complex.
// We'll proceed with term matching + Levenshtein re-scoring.
}
// 4. Score candidates
let mut matches = Vec::new();
for (doc_key, _count) in candidate_counts {
// Retrieve document to calculate exact score
// Optimization: Only load full document if count is promising?
// Here we assume if it matches term, it's relevant.
if let Ok(doc) = self.get(&doc_key) {
let doc_value = doc.to_value();
let mut best_score = 0;
let mut valid = false;
for index in &indexes {
for field in &index.fields {
if let Some(fields_filter) = &fields {
if !fields_filter.contains(field) {
continue;
}
}
let field_value = extract_field_value(&doc_value, field);
if let Some(text) = field_value.as_str() {
// Basic scoring: (matches / total_terms) * 100
// Minus Levenshtein penalty
// This is a simplified version of likely original logic
let doc_terms = tokenize(text);
let mut field_score = 0;
for q_term in &query_terms {
for d_term in &doc_terms {
let dist = levenshtein_distance(q_term, d_term);
if dist == 0 {
field_score += 10; // Exact match
} else if dist <= 2 {
field_score += 5; // Fuzzy match
}
}
}
if field_score > best_score {
best_score = field_score;
valid = true;
}
}
}
}
if valid {
matches.push(FulltextMatch {
doc_key: doc_key.to_string(),
score: best_score as f64,
matched_terms: Vec::new(), // Populate if needed or change logic to track terms
});
}
}
}
// 5. Sort and limit
matches.sort_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
});
matches.truncate(limit);
Ok(matches)
}
// ==================== Fulltext Index Entry Computation Helpers ====================
/// Compute fulltext index entries to add for a document insert (without writing to DB)
/// Returns Vec<(key_bytes, value_bytes)> where value is typically doc_key
pub(crate) fn compute_fulltext_entries_for_insert(
&self,
doc_key: &str,
doc_value: &Value,
) -> Vec<(Vec<u8>, Vec<u8>)> {
let indexes = self.get_all_fulltext_indexes();
if indexes.is_empty() {
return Vec::new();
}
let mut entries = Vec::new();
let doc_key_bytes = doc_key.as_bytes().to_vec();
for index in indexes {
for field in &index.fields {
let field_value = extract_field_value(doc_value, field);
if let Some(text) = field_value.as_str() {
// Add term entries
let terms = tokenize(text);
for term in &terms {
if term.len() >= index.min_length {
let term_key = Self::ft_term_key(&index.name, term, doc_key);
entries.push((term_key, doc_key_bytes.clone()));
}
}
// Add ngram entries
let ngrams = generate_ngrams(text, NGRAM_SIZE);
for ngram in &ngrams {
let ngram_key = Self::ft_ngram_key(&index.name, ngram, doc_key);
entries.push((ngram_key, doc_key_bytes.clone()));
}
}
}
}
entries
}
/// Compute fulltext index entries to remove for a document delete (without writing to DB)
/// Returns Vec<key_bytes> for entries to delete
pub(crate) fn compute_fulltext_entries_for_delete(
&self,
doc_key: &str,
doc_value: &Value,
) -> Vec<Vec<u8>> {
let indexes = self.get_all_fulltext_indexes();
if indexes.is_empty() {
return Vec::new();
}
let mut keys_to_remove = Vec::new();
for index in indexes {
for field in &index.fields {
let field_value = extract_field_value(doc_value, field);
if let Some(text) = field_value.as_str() {
// Remove term entries
let terms = tokenize(text);
for term in &terms {
if term.len() >= index.min_length {
let term_key = Self::ft_term_key(&index.name, term, doc_key);
keys_to_remove.push(term_key);
}
}
// Remove ngram entries
let ngrams = generate_ngrams(text, NGRAM_SIZE);
for ngram in &ngrams {
let ngram_key = Self::ft_ngram_key(&index.name, ngram, doc_key);
keys_to_remove.push(ngram_key);
}
}
}
}
keys_to_remove
}
}