-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathIndexTest.java
More file actions
762 lines (632 loc) 路 28.2 KB
/
Copy pathIndexTest.java
File metadata and controls
762 lines (632 loc) 路 28.2 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import cloud.unum.usearch.Index;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
public class IndexTest {
public static void deleteDirectoryFiles(String path) {
File directory = new File(path);
if (!directory.isDirectory()) {
return;
}
for (File f : directory.listFiles()) {
f.delete();
}
}
@Test
public void test() {
String path = "./tmp/";
deleteDirectoryFiles(path);
try (Index index = new Index.Config().metric("cos").dimensions(2).build()) {
float vec[] = {10, 20};
index.reserve(10);
index.add(42, vec);
long[] keys = index.search(vec, 5);
}
}
@AfterAll
public static void tearDown() {
System.out.println("Java Tests Passed!");
}
@Test
public void testGetSuccess() {
try (Index index = new Index.Config().metric("cos").dimensions(2).build()) {
float vec[] = {10, 20};
index.reserve(10);
index.add(42, vec);
assertArrayEquals(vec, index.get(42), 0.01f);
}
}
@Test
public void testGetFailed() {
try (Index index = new Index.Config().metric("cos").dimensions(2).build()) {
float vec[] = {10, 20};
index.reserve(10);
index.add(42, vec);
assertThrows(IllegalArgumentException.class, () -> index.get(41));
}
}
@Test
public void testUseAfterClose() {
Index index = new Index.Config().metric("cos").dimensions(2).build();
float vec[] = {10, 20};
index.reserve(10);
index.add(42, vec);
assertEquals(1, index.size());
index.close();
assertThrows(IllegalStateException.class, () -> index.size());
}
@Test
public void testLoadFromPath() throws IOException {
File indexFile = File.createTempFile("test", "uidx");
float vec[] = {10, 20};
try (Index index = new Index.Config().metric("cos").dimensions(2).build()) {
index.reserve(10);
index.add(42, vec);
index.save(indexFile.getAbsolutePath());
}
try (Index index = Index.loadFromPath(indexFile.getAbsolutePath())) {
assertArrayEquals(vec, index.get(42), 0.01f);
}
}
@Test
public void testLargeVectors() throws IOException {
File indexFile = File.createTempFile("test", "uidx");
int dimensions = 256;
int numVectors = 100;
try (Index index = new Index.Config().metric("cos").dimensions(dimensions).build()) {
index.reserve(numVectors);
for (int v = 0; v < numVectors; v++) {
index.add(v + 1, randomVector(dimensions));
}
index.save(indexFile.getAbsolutePath());
}
try (Index index = Index.loadFromPath(indexFile.getAbsolutePath())) {
for (int i = 0; i < 100; i++) {
long[] keys = index.search(randomVector(dimensions), 10);
for (long key : keys) {
assertTrue(key >= 1 && key <= numVectors);
}
}
}
}
@Test
public void testShortResults() throws IOException {
int dimensions = 256;
int numVectors = 5;
try (Index index = new Index.Config().metric("cos").dimensions(dimensions).build()) {
index.reserve(numVectors);
for (int v = 0; v < numVectors; v++) {
index.add(v + 1, randomVector(dimensions));
}
long[] keys = index.search(randomVector(dimensions), numVectors + 100);
assertEquals(numVectors, keys.length);
for (long key : keys) {
assertNotEquals(0, key);
}
}
}
private static float[] randomVector(int dimensions) {
float[] vector = new float[dimensions];
for (int i = 0; i < dimensions; i++) {
vector[i] = ThreadLocalRandom.current().nextFloat(2.f);
}
return vector;
}
@Test
public void testMemoryUsage() {
try (Index index = new Index.Config().metric("cos").dimensions(256).build()) {
// Test empty index
long initialMemory = index.memoryUsage();
assertTrue(initialMemory > 0, "Initial memory usage should be positive");
// Add some vectors
index.reserve(1000);
long afterReserve = index.memoryUsage();
assertTrue(afterReserve >= initialMemory, "Memory should increase after reserve");
// Add vectors
float[] vector = new float[256];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 256; j++) {
vector[j] = (float) Math.random();
}
index.add(i, vector);
}
long afterAdding = index.memoryUsage();
assertTrue(afterAdding > afterReserve, "Memory should increase after adding vectors");
// Memory should be reasonable (not too small, not too large)
assertTrue(
afterAdding > 1000 && afterAdding < 1_000_000_000L,
"Memory usage should be reasonable");
}
}
@Test
public void testHardwareAccelerationAPIs() {
try (Index index
= new Index.Config().metric("cos").quantization("f32").dimensions(10).build()) {
// Test hardware acceleration API
String hardwareAcceleration = index.hardwareAcceleration();
assertNotEquals(null, hardwareAcceleration, "Hardware acceleration should not be null");
assertTrue(
!hardwareAcceleration.isEmpty(), "Hardware acceleration should be non-empty");
// Test metric kind API
String metricKind = index.getMetricKind();
assertEquals("cos", metricKind, "Metric kind should be cos");
// Test scalar kind API
String scalarKind = index.getScalarKind();
assertEquals("f32", scalarKind, "Scalar kind should be f32");
System.out.println("Hardware acceleration: " + hardwareAcceleration);
System.out.println("Metric kind: " + metricKind);
System.out.println("Scalar kind: " + scalarKind);
}
}
@Test
public void testDoubleVectorWithFloat64() {
try (Index index
= new Index.Config().metric("cos").dimensions(3).quantization("f64").build()) {
double[] vec = {1.0, 2.0, 3.0};
index.reserve(10);
index.add(42, vec);
double[] retrieved = new double[3];
index.getInto(42, retrieved);
assertArrayEquals(vec, retrieved, 0.01);
}
}
@Test
public void testByteVectorWithInt8() {
try (Index index
= new Index.Config().metric("cos").dimensions(4).quantization("i8").build()) {
byte[] vec = {10, 20, 30, 40};
index.reserve(10);
index.add(42, vec);
byte[] retrieved = new byte[4];
index.getInto(42, retrieved);
assertArrayEquals(vec, retrieved);
}
}
@Test
public void testMiniFloatQuantizations() {
for (String quantization : new String[]{"e5m2", "e4m3", "e3m2", "e2m3"}) {
try (Index index = new Index.Config()
.metric("cos").dimensions(64).quantization(quantization).build()) {
float[] vec = new float[64];
for (int i = 0; i < 64; i++) vec[i] = (float) i * 0.1f;
index.reserve(10);
index.add(42, vec);
long[] keys = index.search(vec, 1);
assertEquals(42L, keys[0], "Self-match failed for " + quantization);
}
}
}
@Test
public void testGetIntoBufferMethods() {
try (Index index = new Index.Config().metric("cos").dimensions(3).build()) {
float[] vecF32 = {1.0f, 2.0f, 3.0f};
index.reserve(10);
index.add(42, vecF32);
float[] bufferF32 = new float[3];
index.getInto(42, bufferF32);
assertArrayEquals(vecF32, bufferF32, 0.01f);
}
try (Index index
= new Index.Config().metric("cos").dimensions(3).quantization("f64").build()) {
double[] vecF64 = {1.0, 2.0, 3.0};
index.reserve(10);
index.add(43, vecF64);
double[] bufferF64 = new double[3];
index.getInto(43, bufferF64);
assertArrayEquals(vecF64, bufferF64, 0.01);
}
try (Index index
= new Index.Config().metric("cos").dimensions(4).quantization("i8").build()) {
byte[] vecI8 = {10, 20, 30, 40};
index.reserve(10);
index.add(44, vecI8);
byte[] bufferI8 = new byte[4];
index.getInto(44, bufferI8);
assertArrayEquals(vecI8, bufferI8);
}
}
@Test
public void testConcurrentAdd() throws Exception {
try (Index index = new Index.Config().metric("cos").dimensions(4).build()) {
final int threadsCount = 10;
index.reserve(1000, threadsCount);
ExecutorService executor = Executors.newFixedThreadPool(threadsCount);
@SuppressWarnings("unchecked")
CompletableFuture<Void>[] futures = new CompletableFuture[threadsCount];
for (int t = 0; t < threadsCount; t++) {
final int threadId = t;
futures[t]
= CompletableFuture.runAsync(
() -> {
for (int i = 0; i < 50; i++) {
long key = threadId * 50L + i;
float[] vector = randomVector(4);
index.add(key, vector);
}
},
executor);
}
CompletableFuture.allOf(futures).get(10, TimeUnit.SECONDS);
executor.shutdown();
assertEquals(50L * threadsCount, index.size());
}
}
@Test
public void testConcurrentSearch() throws Exception {
try (Index index = new Index.Config().metric("cos").dimensions(4).build()) {
final int threadsCount = 5;
index.reserve(100, threadsCount);
// Add some vectors first
for (int i = 0; i < 100; i++) {
index.add(i, randomVector(4));
}
ExecutorService executor = Executors.newFixedThreadPool(threadsCount);
@SuppressWarnings("unchecked")
CompletableFuture<long[]>[] futures = new CompletableFuture[threadsCount];
for (int t = 0; t < threadsCount; t++) {
futures[t]
= CompletableFuture.supplyAsync(
() -> {
float[] queryVector = randomVector(4);
return index.search(queryVector, 10);
},
executor);
}
for (CompletableFuture<long[]> future : futures) {
long[] results = future.get(10, TimeUnit.SECONDS);
assertTrue(results.length > 0);
assertTrue(results.length <= 10);
}
executor.shutdown();
}
}
@Test
public void testBatchAdd() {
try (Index index = new Index.Config().metric("cos").dimensions(2).build()) {
index.reserve(10);
// Create a batch of 3 vectors concatenated
float[] batchVectors = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
index.add(100, batchVectors); // Should add keys 100, 101, 102
assertEquals(3, index.size());
// Verify each vector was added correctly
assertArrayEquals(new float[]{1.0f, 2.0f}, index.get(100), 0.01f);
assertArrayEquals(new float[]{3.0f, 4.0f}, index.get(101), 0.01f);
assertArrayEquals(new float[]{5.0f, 6.0f}, index.get(102), 0.01f);
}
}
@Test
public void testBatchAddDouble() {
try (Index index
= new Index.Config().metric("cos").dimensions(3).quantization("f64").build()) {
index.reserve(10);
// Create a batch of 2 double vectors concatenated
double[] batchVectors = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
index.add(200, batchVectors); // Should add keys 200, 201
assertEquals(2, index.size());
double[] buffer = new double[3];
index.getInto(200, buffer);
assertArrayEquals(new double[]{1.0, 2.0, 3.0}, buffer, 0.01);
index.getInto(201, buffer);
assertArrayEquals(new double[]{4.0, 5.0, 6.0}, buffer, 0.01);
}
}
@Test
public void testBatchAddByte() {
try (Index index
= new Index.Config().metric("cos").dimensions(4).quantization("i8").build()) {
index.reserve(10);
// Create a batch of 2 byte vectors concatenated
byte[] batchVectors = {10, 20, 30, 40, 50, 60, 70, 80};
index.add(300, batchVectors); // Should add keys 300, 301
assertEquals(2, index.size());
byte[] buffer = new byte[4];
index.getInto(300, buffer);
assertArrayEquals(new byte[]{10, 20, 30, 40}, buffer);
index.getInto(301, buffer);
assertArrayEquals(new byte[]{50, 60, 70, 80}, buffer);
}
}
@Test
public void testBatchDetection() {
try (Index index = new Index.Config().metric("cos").dimensions(2).build()) {
index.reserve(10);
// Valid batch: 4 elements = 2 vectors of dimension 2
float[] validBatch = {1.0f, 2.0f, 3.0f, 4.0f};
index.add(10, validBatch);
assertEquals(2, index.size());
// Invalid batch: 3 elements, not divisible by dimensions (2)
float[] invalidBatch = {1.0f, 2.0f, 3.0f};
assertThrows(IllegalArgumentException.class, () -> index.add(20, invalidBatch));
// Still should be 2 vectors (invalid batch should not have been added)
assertEquals(2, index.size());
}
}
@Test
public void testByteBufferOperations() {
try (Index index = new Index.Config().metric("cos").dimensions(256).build()) {
index.reserve(1000);
// Test FloatBuffer operations
java.nio.ByteBuffer byteBuffer
= java.nio.ByteBuffer.allocateDirect(256 * Float.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
// Fill buffer with test data
for (int i = 0; i < 256; i++) {
floatBuffer.put(i, (float) Math.sin(i * 0.1));
}
// Test add with ByteBuffer
index.add(1000L, floatBuffer);
assertEquals(1, index.size());
// Test search with ByteBuffer
long[] results = index.search(floatBuffer, 5);
assertEquals(1, results.length);
assertEquals(1000L, results[0]);
// Verify data consistency by comparing with array method
float[] arrayData = new float[256];
floatBuffer.rewind();
floatBuffer.get(arrayData);
long[] arrayResults = index.search(arrayData, 5);
assertArrayEquals(results, arrayResults);
}
}
@Test
public void testByteBufferDoubleOperations() {
try (Index index
= new Index.Config().metric("cos").dimensions(128).quantization("f64").build()) {
index.reserve(100);
java.nio.ByteBuffer byteBuffer
= java.nio.ByteBuffer.allocateDirect(128 * Double.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer();
// Fill buffer with test data
for (int i = 0; i < 128; i++) {
doubleBuffer.put(i, Math.cos(i * 0.05));
}
index.add(2000L, doubleBuffer);
long[] results = index.search(doubleBuffer, 3);
assertEquals(1, results.length);
assertEquals(2000L, results[0]);
}
}
@Test
public void testByteBufferByteOperations() {
try (Index index
= new Index.Config().metric("cos").dimensions(64).quantization("i8").build()) {
index.reserve(50);
java.nio.ByteBuffer byteBuffer
= java.nio.ByteBuffer.allocateDirect(64).order(java.nio.ByteOrder.nativeOrder());
// Fill buffer with test data
for (int i = 0; i < 64; i++) {
byteBuffer.put(i, (byte) (i % 127));
}
byteBuffer.rewind();
index.add(3000L, byteBuffer);
byteBuffer.rewind();
long[] results = index.search(byteBuffer, 2);
assertEquals(1, results.length);
assertEquals(3000L, results[0]);
}
}
@Test
public void testByteBufferPerformanceComparison() {
int dimensions = 512;
int numVectors = 1000;
int numQueries = 100;
try (Index index = new Index.Config().metric("cos").dimensions(dimensions).build()) {
index.reserve(numVectors);
// Prepare test data
float[][] vectors = new float[numVectors][dimensions];
java.nio.ByteBuffer[] buffers = new java.nio.ByteBuffer[numVectors];
for (int i = 0; i < numVectors; i++) {
vectors[i] = randomVector(dimensions);
buffers[i]
= java.nio.ByteBuffer.allocateDirect(dimensions * Float.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
buffers[i].asFloatBuffer().put(vectors[i]);
}
// Test array-based add performance
long arrayAddStart = System.nanoTime();
for (int i = 0; i < numVectors; i++) {
index.add(i, vectors[i]);
}
long arrayAddTime = System.nanoTime() - arrayAddStart;
// Clear index for ByteBuffer test
try (Index bufferIndex
= new Index.Config().metric("cos").dimensions(dimensions).build()) {
bufferIndex.reserve(numVectors);
// Test ByteBuffer-based add performance
long bufferAddStart = System.nanoTime();
for (int i = 0; i < numVectors; i++) {
bufferIndex.add(i, buffers[i].asFloatBuffer());
}
long bufferAddTime = System.nanoTime() - bufferAddStart;
// Test search performance
float[] queryVector = randomVector(dimensions);
java.nio.ByteBuffer queryBuffer
= java.nio.ByteBuffer.allocateDirect(dimensions * Float.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
queryBuffer.asFloatBuffer().put(queryVector);
// Array-based search
long arraySearchStart = System.nanoTime();
for (int i = 0; i < numQueries; i++) {
index.search(queryVector, 10);
}
long arraySearchTime = System.nanoTime() - arraySearchStart;
// ByteBuffer-based search
long bufferSearchStart = System.nanoTime();
for (int i = 0; i < numQueries; i++) {
bufferIndex.search(queryBuffer.asFloatBuffer(), 10);
}
long bufferSearchTime = System.nanoTime() - bufferSearchStart;
// Print performance results
System.out.println("Performance Comparison (ns):");
System.out.printf(
"Array Add: %,d | ByteBuffer Add: %,d (%.2fx faster)%n",
arrayAddTime, bufferAddTime, (double) arrayAddTime / bufferAddTime);
System.out.printf(
"Array Search: %,d | ByteBuffer Search: %,d (%.2fx faster)%n",
arraySearchTime,
bufferSearchTime,
(double) arraySearchTime / bufferSearchTime);
// Verify correctness - results should be equivalent
long[] arrayResults = index.search(queryVector, 10);
long[] bufferResults = bufferIndex.search(queryBuffer.asFloatBuffer(), 10);
assertEquals(
arrayResults.length,
bufferResults.length,
"Search results should be equivalent");
}
}
}
@Test
public void testSearchIntoZeroAllocation() {
try (Index index = new Index.Config().metric("cos").dimensions(128).build()) {
index.reserve(100);
// Add some test vectors
java.nio.ByteBuffer vectorBuffer
= java.nio.ByteBuffer.allocateDirect(128 * Float.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.FloatBuffer floatBuffer = vectorBuffer.asFloatBuffer();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 128; j++) {
floatBuffer.put(j, (float) (Math.sin(i + j * 0.1) + i * 0.01));
}
floatBuffer.rewind();
index.add(i, floatBuffer);
}
// Prepare query and results buffers
java.nio.ByteBuffer queryBuffer
= java.nio.ByteBuffer.allocateDirect(128 * Float.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.FloatBuffer queryFloat = queryBuffer.asFloatBuffer();
java.nio.ByteBuffer resultsBuffer
= java.nio.ByteBuffer.allocateDirect(5 * Long.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.LongBuffer resultsLong = resultsBuffer.asLongBuffer();
// Set up query vector (same as vector 3)
for (int j = 0; j < 128; j++) {
queryFloat.put(j, (float) (Math.sin(3 + j * 0.1) + 3 * 0.01));
}
queryFloat.rewind();
// Test searchInto - should find vector 3 first
int found = index.searchInto(queryFloat, resultsLong, 5);
assertTrue(found >= 1, "Should find at least 1 result");
assertTrue(found <= 5, "Should find at most 5 results");
// Verify buffer position was advanced
assertEquals(
found, resultsLong.position(), "Results buffer position should be advanced");
// First result should be key 3 (exact match)
resultsLong.rewind();
assertEquals(3L, resultsLong.get(0), "First result should be exact match");
}
}
@Test
public void testSearchIntoDoubleBuffer() {
try (Index index
= new Index.Config().metric("cos").dimensions(64).quantization("f64").build()) {
index.reserve(50);
java.nio.ByteBuffer vectorBuffer
= java.nio.ByteBuffer.allocateDirect(64 * Double.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.DoubleBuffer doubleBuffer = vectorBuffer.asDoubleBuffer();
// Add test vectors
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 64; j++) {
doubleBuffer.put(j, Math.cos(i + j * 0.05));
}
doubleBuffer.rewind();
index.add(100 + i, doubleBuffer);
}
// Query with vector similar to index 2
for (int j = 0; j < 64; j++) {
doubleBuffer.put(j, Math.cos(2 + j * 0.05));
}
doubleBuffer.rewind();
java.nio.ByteBuffer resultsBuffer
= java.nio.ByteBuffer.allocateDirect(3 * Long.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.LongBuffer resultsLong = resultsBuffer.asLongBuffer();
int found = index.searchInto(doubleBuffer, resultsLong, 3);
assertTrue(found > 0, "Should find results");
assertEquals(102L, resultsLong.get(0), "First result should be key 102");
}
}
@Test
public void testSearchIntoByteBuffer() {
try (Index index
= new Index.Config().metric("cos").dimensions(32).quantization("i8").build()) {
index.reserve(20);
java.nio.ByteBuffer vectorBuffer
= java.nio.ByteBuffer.allocateDirect(32).order(java.nio.ByteOrder.nativeOrder());
// Add test vectors
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 32; j++) {
vectorBuffer.put(j, (byte) ((i * 10 + j) % 127));
}
vectorBuffer.rewind();
index.add(200 + i, vectorBuffer);
}
// Query with exact match to vector 1
for (int j = 0; j < 32; j++) {
vectorBuffer.put(j, (byte) ((1 * 10 + j) % 127));
}
vectorBuffer.rewind();
java.nio.ByteBuffer resultsBuffer
= java.nio.ByteBuffer.allocateDirect(2 * Long.BYTES)
.order(java.nio.ByteOrder.nativeOrder());
java.nio.LongBuffer resultsLong = resultsBuffer.asLongBuffer();
int found = index.searchInto(vectorBuffer, resultsLong, 2);
assertTrue(found > 0, "Should find results");
assertEquals(201L, resultsLong.get(0), "First result should be key 201");
}
}
@Test
public void testPlatformCapabilities() {
// Test runtime hardware capabilities
String[] available = Index.hardwareAccelerationAvailable();
assertNotEquals(null, available, "Available capabilities should not be null");
assertTrue(available.length > 0, "Platform should have at least serial capability");
// Test compile-time capabilities
String[] compiled = Index.hardwareAccelerationCompiled();
assertNotEquals(null, compiled, "Compiled capabilities should not be null");
assertTrue(compiled.length > 0, "Should have at least serial compiled");
// Should always include serial as baseline in both
boolean hasAvailableSerial = false;
boolean hasCompiledSerial = false;
for (String cap : available) {
if ("serial".equals(cap)) {
hasAvailableSerial = true;
break;
}
}
for (String cap : compiled) {
if ("serial".equals(cap)) {
hasCompiledSerial = true;
break;
}
}
assertTrue(hasAvailableSerial, "Platform should always support serial capability");
assertTrue(hasCompiledSerial, "Serial should always be compiled");
// Test library version
String version = Index.version();
assertNotEquals(null, version, "Library version should not be null");
assertTrue(!version.isEmpty(), "Library version should be non-empty");
// Test dynamic dispatch detection
boolean usesDynamicDispatch = Index.usesDynamicDispatch();
System.out.println("Uses dynamic dispatch: " + usesDynamicDispatch);
System.out.println("Available capabilities: " + String.join(", ", available));
System.out.println("Compiled capabilities: " + String.join(", ", compiled));
System.out.println("Library version: " + version);
}
}