forked from ezimuel/PHPVector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistenceTest.php
More file actions
459 lines (368 loc) · 15.4 KB
/
PersistenceTest.php
File metadata and controls
459 lines (368 loc) · 15.4 KB
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
<?php
declare(strict_types=1);
namespace PHPVector\Tests;
use PHPUnit\Framework\TestCase;
use PHPVector\BM25\Config as BM25Config;
use PHPVector\BM25\SimpleTokenizer;
use PHPVector\Distance;
use PHPVector\Document;
use PHPVector\HNSW\Config as HNSWConfig;
use PHPVector\HybridMode;
use PHPVector\VectorDatabase;
final class PersistenceTest extends TestCase
{
private string $tmpDir;
protected function setUp(): void
{
$this->tmpDir = sys_get_temp_dir() . '/phpvtest_' . uniqid('', true);
mkdir($this->tmpDir, 0755, true);
}
protected function tearDown(): void
{
$this->rrmdir($this->tmpDir);
}
private function rrmdir(string $dir): void
{
if (!is_dir($dir)) {
return;
}
foreach ((array) glob($dir . '/*') as $item) {
is_dir($item) ? $this->rrmdir((string) $item) : unlink((string) $item);
}
rmdir($dir);
}
// ------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------
private function makeDb(): VectorDatabase
{
return new VectorDatabase(
hnswConfig: new HNSWConfig(M: 8, efConstruction: 50, efSearch: 20),
bm25Config: new BM25Config(),
tokenizer: new SimpleTokenizer([]),
path: $this->tmpDir,
);
}
private function openDb(): VectorDatabase
{
return VectorDatabase::open(
path: $this->tmpDir,
hnswConfig: new HNSWConfig(M: 8, efConstruction: 50, efSearch: 20),
bm25Config: new BM25Config(),
tokenizer: new SimpleTokenizer([]),
);
}
// ------------------------------------------------------------------
// Round-trip: vector / text / hybrid search parity
// ------------------------------------------------------------------
public function testRoundTripVectorSearch(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0, 0.0, 0.0], text: 'alpha one'));
$db->addDocument(new Document(id: 2, vector: [0.0, 1.0, 0.0, 0.0], text: 'beta two'));
$db->addDocument(new Document(id: 3, vector: [0.0, 0.0, 1.0, 0.0], text: 'gamma three'));
$db->addDocument(new Document(id: 4, vector: [0.0, 0.0, 0.0, 1.0], text: 'delta four'));
$query = [1.0, 0.1, 0.0, 0.0];
$before = $db->vectorSearch($query, k: 1);
$db->save();
$loaded = $this->openDb();
$after = $loaded->vectorSearch($query, k: 1);
self::assertSame($before[0]->document->id, $after[0]->document->id);
}
public function testRoundTripTextSearch(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 'a', vector: [0.1, 0.9], text: 'machine learning vector search'));
$db->addDocument(new Document(id: 'b', vector: [0.5, 0.5], text: 'database storage systems'));
$db->addDocument(new Document(id: 'c', vector: [0.9, 0.1], text: 'neural network deep learning'));
$query = 'machine learning';
$before = $db->textSearch($query, k: 1);
$db->save();
$loaded = $this->openDb();
$after = $loaded->textSearch($query, k: 1);
self::assertSame($before[0]->document->id, $after[0]->document->id);
}
public function testRoundTripHybridSearchRRF(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 10, vector: [1.0, 0.0], text: 'php vector database search'));
$db->addDocument(new Document(id: 20, vector: [0.0, 1.0], text: 'sql relational storage'));
$query = [1.0, 0.0];
$text = 'vector';
$before = $db->hybridSearch($query, $text, k: 1, mode: HybridMode::RRF);
$db->save();
$loaded = $this->openDb();
$after = $loaded->hybridSearch($query, $text, k: 1, mode: HybridMode::RRF);
self::assertSame($before[0]->document->id, $after[0]->document->id);
}
public function testRoundTripHybridSearchWeighted(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 10, vector: [1.0, 0.0], text: 'php vector database search'));
$db->addDocument(new Document(id: 20, vector: [0.0, 1.0], text: 'sql relational storage'));
$query = [1.0, 0.0];
$text = 'vector';
$before = $db->hybridSearch($query, $text, k: 1, mode: HybridMode::Weighted);
$db->save();
$loaded = $this->openDb();
$after = $loaded->hybridSearch($query, $text, k: 1, mode: HybridMode::Weighted);
self::assertSame($before[0]->document->id, $after[0]->document->id);
}
// ------------------------------------------------------------------
// Document fidelity
// ------------------------------------------------------------------
public function testCountMatchesAfterRoundTrip(): void
{
$db = $this->makeDb();
for ($i = 0; $i < 10; $i++) {
$db->addDocument(new Document(
id: $i,
vector: [sin($i), cos($i)],
text: "document number {$i}",
));
}
$db->save();
$loaded = $this->openDb();
self::assertSame(10, $loaded->count());
}
public function testMetadataAndTextPreservedExactly(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(
id: 'doc-meta',
vector: [0.5, 0.5],
text: 'some full-text content',
metadata: ['key' => 'value', 'nested' => ['a' => 1, 'b' => true], 'num' => 3.14],
));
$db->addDocument(new Document(
id: 'no-text',
vector: [0.1, 0.9],
text: null,
));
$db->addDocument(new Document(
id: 'empty-meta',
vector: [0.9, 0.1],
text: 'only text',
));
$db->save();
$loaded = $this->openDb();
$results = $loaded->vectorSearch([0.5, 0.5], k: 3);
$byId = [];
foreach ($results as $r) {
$byId[$r->document->id] = $r->document;
}
self::assertArrayHasKey('doc-meta', $byId);
$restored = $byId['doc-meta'];
self::assertSame('some full-text content', $restored->text);
self::assertSame('value', $restored->metadata['key']);
self::assertSame(['a' => 1, 'b' => true], $restored->metadata['nested']);
self::assertEqualsWithDelta(3.14, $restored->metadata['num'], 1e-9);
self::assertArrayHasKey('no-text', $byId);
self::assertNull($byId['no-text']->text);
self::assertSame([], $byId['no-text']->metadata);
self::assertArrayHasKey('empty-meta', $byId);
self::assertSame([], $byId['empty-meta']->metadata);
}
public function testStringAndIntDocumentIdsPreserved(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 42, vector: [1.0, 0.0]));
$db->addDocument(new Document(id: 'str', vector: [0.0, 1.0]));
$db->save();
$loaded = $this->openDb();
$intResult = $loaded->vectorSearch([1.0, 0.0], k: 1);
self::assertSame(42, $intResult[0]->document->id);
self::assertIsInt($intResult[0]->document->id);
$strResult = $loaded->vectorSearch([0.0, 1.0], k: 1);
self::assertSame('str', $strResult[0]->document->id);
self::assertIsString($strResult[0]->document->id);
}
// ------------------------------------------------------------------
// Folder structure
// ------------------------------------------------------------------
public function testFolderStructureIsCreated(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0]));
$db->save();
self::assertFileExists($this->tmpDir . '/meta.json');
self::assertFileExists($this->tmpDir . '/hnsw.bin');
self::assertFileExists($this->tmpDir . '/bm25.bin');
self::assertDirectoryExists($this->tmpDir . '/docs');
self::assertFileExists($this->tmpDir . '/docs/0.bin');
}
public function testDocFilesAreWrittenPerNode(): void
{
$db = $this->makeDb();
for ($i = 0; $i < 5; $i++) {
$db->addDocument(new Document(id: $i, vector: [(float) $i, 0.0]));
}
$db->save();
$files = glob($this->tmpDir . '/docs/*.bin');
self::assertCount(5, $files);
}
public function testEmptyDatabaseRoundTrip(): void
{
$db = $this->makeDb();
$db->save();
$loaded = $this->openDb();
self::assertSame(0, $loaded->count());
self::assertSame([], $loaded->vectorSearch([1.0, 0.0], k: 5));
self::assertSame([], $loaded->textSearch('anything', k: 5));
}
// ------------------------------------------------------------------
// Lazy loading
// ------------------------------------------------------------------
public function testLazyLoadingEnrichesStubs(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(
id: 'lazy-doc',
vector: [1.0, 0.0],
text: 'lazy loading test',
metadata: ['loaded' => true],
));
$db->save();
$loaded = $this->openDb();
$results = $loaded->vectorSearch([1.0, 0.0], k: 1);
self::assertCount(1, $results);
self::assertSame('lazy-doc', $results[0]->document->id);
self::assertSame('lazy loading test', $results[0]->document->text);
self::assertSame(['loaded' => true], $results[0]->document->metadata);
}
// ------------------------------------------------------------------
// Auto UUID
// ------------------------------------------------------------------
public function testAutoUuidAssignedWhenIdIsNull(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(vector: [1.0, 0.0]));
$db->save();
$loaded = $this->openDb();
$results = $loaded->vectorSearch([1.0, 0.0], k: 1);
self::assertCount(1, $results);
self::assertIsString($results[0]->document->id);
// UUID v4 format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
self::assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/',
(string) $results[0]->document->id,
);
}
public function testTwoNullIdDocumentsGetDistinctUuids(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(vector: [1.0, 0.0]));
$db->addDocument(new Document(vector: [0.0, 1.0]));
$db->save();
$loaded = $this->openDb();
$results1 = $loaded->vectorSearch([1.0, 0.0], k: 1);
$results2 = $loaded->vectorSearch([0.0, 1.0], k: 1);
self::assertNotSame($results1[0]->document->id, $results2[0]->document->id);
}
// ------------------------------------------------------------------
// Error cases
// ------------------------------------------------------------------
public function testSaveThrowsWhenNoPathConfigured(): void
{
$db = new VectorDatabase();
$this->expectException(\RuntimeException::class);
$db->save();
}
public function testOpenThrowsOnMissingFolder(): void
{
$this->expectException(\RuntimeException::class);
VectorDatabase::open($this->tmpDir . '/does_not_exist');
}
public function testOpenThrowsOnDistanceMismatch(): void
{
$db = new VectorDatabase(
hnswConfig: new HNSWConfig(distance: Distance::Cosine),
path: $this->tmpDir,
);
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0]));
$db->save();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageMatches('/[Dd]istance/');
VectorDatabase::open(
path: $this->tmpDir,
hnswConfig: new HNSWConfig(distance: Distance::Euclidean),
);
}
// ------------------------------------------------------------------
// Incremental save
// ------------------------------------------------------------------
public function testIncrementalSave(): void
{
// First save: 2 documents.
$db = $this->makeDb();
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0], text: 'first'));
$db->addDocument(new Document(id: 2, vector: [0.0, 1.0], text: 'second'));
$db->save();
// Add more documents to a fresh instance opened from disk.
$loaded = $this->openDb();
$loaded->addDocument(new Document(id: 3, vector: [0.5, 0.5], text: 'third'));
$loaded->save();
// Re-open and verify all three documents are accessible.
$final = $this->openDb();
self::assertSame(3, $final->count());
$results = $final->vectorSearch([1.0, 0.0], k: 3);
$ids = array_map(fn($r) => $r->document->id, $results);
self::assertContains(1, $ids);
}
// ------------------------------------------------------------------
// Delete persistence
// ------------------------------------------------------------------
public function testDeletedDocumentsArePersistedAndExcluded(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0], text: 'first'));
$db->addDocument(new Document(id: 2, vector: [0.9, 0.1], text: 'second'));
$db->addDocument(new Document(id: 3, vector: [0.0, 1.0], text: 'third'));
// Delete document 1
$db->deleteDocument(1);
$db->save();
// Reload and verify
$loaded = $this->openDb();
self::assertSame(2, $loaded->count());
// Document 1 should not appear in results
$results = $loaded->vectorSearch([1.0, 0.0], k: 3);
$ids = array_map(fn($r) => $r->document->id, $results);
self::assertNotContains(1, $ids);
self::assertContains(2, $ids);
}
public function testDeletedDocumentFileIsRemoved(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0], text: 'to delete'));
$db->addDocument(new Document(id: 2, vector: [0.0, 1.0], text: 'to keep'));
$db->save();
// Verify doc file exists
self::assertFileExists($this->tmpDir . '/docs/0.bin');
self::assertFileExists($this->tmpDir . '/docs/1.bin');
// Delete document 1 (which is nodeId 0)
$db->deleteDocument(1);
// Doc file should be removed immediately
self::assertFileDoesNotExist($this->tmpDir . '/docs/0.bin');
self::assertFileExists($this->tmpDir . '/docs/1.bin');
}
public function testUpdateDocumentPersistsCorrectly(): void
{
$db = $this->makeDb();
$db->addDocument(new Document(id: 1, vector: [1.0, 0.0], text: 'original'));
$db->save();
// Update the document
$db->updateDocument(new Document(
id: 1,
vector: [0.0, 1.0],
text: 'updated content',
metadata: ['version' => 2],
));
$db->save();
// Reload and verify
$loaded = $this->openDb();
$results = $loaded->vectorSearch([0.0, 1.0], k: 1);
self::assertSame(1, $results[0]->document->id);
self::assertSame('updated content', $results[0]->document->text);
self::assertSame(['version' => 2], $results[0]->document->metadata);
}
}