-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathRespVectorSetTests.cs
More file actions
2426 lines (2034 loc) · 134 KB
/
RespVectorSetTests.cs
File metadata and controls
2426 lines (2034 loc) · 134 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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Allure.NUnit;
using Garnet.common;
using Garnet.server;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using StackExchange.Redis;
using Tsavorite.core;
namespace Garnet.test
{
[AllureNUnit]
[TestFixture]
public class RespVectorSetTests : AllureTestBase
{
private const string DefaultAOFMemorySize = "2g"; // Very large because CI boxes have low IOPS, so try and flush to disk veeeeeery rarely
GarnetServer server;
[SetUp]
public void Setup()
{
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = CreateGarnetServer(tryRecover: false);
server.Start();
}
[TearDown]
public void TearDown()
{
server.Dispose();
TestUtils.OnTearDown();
}
[Test]
public void DisabledWithFeatureFlag()
{
// Restart with Vector Sets disabled
TearDown();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = CreateGarnetServer(tryRecover: false, enableVectorSetPreview: false);
server.Start();
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
ReadOnlySpan<RespCommand> vectorSetCommands = [RespCommand.VADD, RespCommand.VCARD, RespCommand.VDIM, RespCommand.VEMB, RespCommand.VGETATTR, RespCommand.VINFO, RespCommand.VISMEMBER, RespCommand.VLINKS, RespCommand.VRANDMEMBER, RespCommand.VREM, RespCommand.VSETATTR, RespCommand.VSIM];
foreach (var cmd in vectorSetCommands)
{
// Should all fault before any validation
var exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute(cmd.ToString()));
ClassicAssert.AreEqual("ERR Vector Set (preview) commands are not enabled", exc.Message);
}
}
[Test]
public void OversizedRejected()
{
var options = GetOpts(server);
var overflowSizeBytes = (int)(GarnetServerOptions.ParseSize(options.PageSize, out _) * 2);
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var oversizedVectorData = Enumerable.Repeat<byte>(1, overflowSizeBytes).ToArray();
var oversideAttribute = Enumerable.Repeat<byte>(2, overflowSizeBytes).ToArray();
var exc1 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["foo", "XB8", oversizedVectorData, new byte[] { 0, 0, 0, 0 }, "XPREQ8"]));
ClassicAssert.AreEqual("ERR Vector exceed configured page size", exc1.Message);
var basicVectorData = Enumerable.Repeat<byte>(3, 75).ToArray();
var exc2 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["foo", "XB8", basicVectorData, new byte[] { 0, 0, 0, 1 }, "XPREQ8", "SETATTR", oversideAttribute]));
ClassicAssert.AreEqual("ERR Attribute exceed configured page size", exc2.Message);
}
[Test]
public void WrongTypeForVectorSetOpsOnNonVectorSetKeys()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var vectorSetCommands = Enum.GetValues<RespCommand>().Where(static t => t.IsLegalOnVectorSet() && !(t is RespCommand.DEL or RespCommand.UNLINK or RespCommand.DEBUG or RespCommand.RENAME or RespCommand.RENAMENX or RespCommand.TYPE));
// Strings
{
var res = db.StringSet("foo", "bar");
ClassicAssert.IsTrue(res);
foreach (var cmd in vectorSetCommands)
{
RedisServerException exc;
switch (cmd)
{
case RespCommand.VADD:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]));
break;
case RespCommand.VCARD:
// TODO: Implement when VCARD works
continue;
case RespCommand.VDIM:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VDIM", ["foo"]));
break;
case RespCommand.VEMB:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VEMB", ["foo", new byte[] { 0, 0, 0, 0 }]));
break;
case RespCommand.VGETATTR:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VGETATTR", ["foo", new byte[] { 0, 0, 0, 0 }]));
break;
case RespCommand.VINFO:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VINFO", ["foo"]));
break;
case RespCommand.VISMEMBER:
// TODO: Implement when VISMEMBER works
continue;
case RespCommand.VLINKS:
// TODO: Implement when VLINKS works
continue;
case RespCommand.VRANDMEMBER:
// TODO: Implement when VRANDMEMBER works
continue;
case RespCommand.VREM:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VREM", ["foo", new byte[] { 0, 0, 0, 0 }]));
break;
case RespCommand.VSETATTR:
// TODO: Implement when VSETATTR works
continue;
case RespCommand.VSIM:
exc = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VSIM", ["foo", "VALUES", "75", "110.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "COUNT", "5", "EPSILON", "1.0", "EF", "40"]));
break;
default:
throw new InvalidOperationException($"Unexpected Vector Set command: {cmd}");
}
ClassicAssert.AreEqual("WRONGTYPE Operation against a key holding the wrong kind of value", exc.Message, $"RESP Command: {cmd}");
}
}
// TODO: Other objects - but we can wait for store v2 for that
}
[Test]
public void VADD()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// VALUES
var res1 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "100.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 1, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res2);
var float3 = new float[75];
float3[0] = 5f;
for (var i = 1; i < float3.Length; i++)
{
float3[i] = float3[i - 1] + 1;
}
// FP32
var res3 = db.Execute("VADD", ["foo", "REDUCE", "50", "FP32", MemoryMarshal.Cast<float, byte>(float3).ToArray(), new byte[] { 2, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res3);
var byte4 = new byte[75];
byte4[0] = 9;
for (var i = 1; i < byte4.Length; i++)
{
byte4[i] = (byte)(byte4[i - 1] + 1);
}
// XB8
var res4 = db.Execute("VADD", ["foo", "REDUCE", "50", "XB8", byte4, new byte[] { 3, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res4);
// TODO: exact duplicates - what does Redis do?
// Add without specifying reductions after first vector
var res5 = db.Execute("VADD", ["fizz", "REDUCE", "50", "VALUES", "75", "150.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res5);
var exc1 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["fizz", "VALUES", "4", "5.0", "6.0", "7.0", "8.0", new byte[] { 0, 0, 0, 1 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]));
ClassicAssert.AreEqual("ERR Vector dimension mismatch - got 4 but set has 75", exc1.Message);
// Add without specifying EF after first vector
var res6 = db.Execute("VADD", ["fizz", "REDUCE", "50", "VALUES", "75", "170.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 3 }, "CAS", "NOQUANT", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res6);
// Add without specifying M after first vector
var exc2 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["fizz", "REDUCE", "50", "VALUES", "75", "180.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 4 }, "CAS", "NOQUANT", "EF", "16"]));
ClassicAssert.AreEqual("ERR asked M value mismatch with existing vector set", exc2.Message);
// Mismatch vector size for projection
var exc3 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["fizz", "REDUCE", "50", "VALUES", "5", "1.0", "2.0", "3.0", "4.0", "5.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]));
ClassicAssert.AreEqual("ERR Vector dimension mismatch - got 5 but set has 75", exc3.Message);
}
[Test]
public void VADDVariableLengthElementIds()
{
const int MinElementLength = 1;
const int MaxElementLength = 1024;
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Always put a 0 length in as a stress test
List<byte[]> ids = [[]];
for (var len = MinElementLength; len <= MaxElementLength; len *= 2)
{
ids.Add(Enumerable.Range(0, len).Select(_ => (byte)len).ToArray());
}
foreach (var id in ids)
{
var addRes = (int)db.Execute("VADD", ["foo", "VALUES", "1", ((float)(byte)id.Length).ToString(), id, "XPREQ8"]);
ClassicAssert.AreEqual(1, addRes);
}
foreach (var id in ids)
{
var embRes = (string[])db.Execute("VEMB", ["foo", id]);
ClassicAssert.AreEqual(1, embRes.Length);
ClassicAssert.AreEqual((float)(byte)id.Length, float.Parse(embRes[0]));
}
}
[Test]
public void VADDXPREQB8()
{
// Extra validation is required for this extension quantifier
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Build byte array for vector data (75 bytes)
var vectorData1 = new byte[75];
vectorData1[0] = 1;
for (var i = 1; i < vectorData1.Length; i++)
{
vectorData1[i] = (byte)(vectorData1[i - 1] + 1);
}
var vectorData2 = new byte[75];
vectorData2[0] = 100;
for (var i = 1; i < vectorData2.Length; i++)
{
vectorData2[i] = (byte)(vectorData2[i - 1] + 1);
}
// Small vector for REDUCE test
var smallVectorData = new byte[4];
for (var i = 0; i < smallVectorData.Length; i++)
{
smallVectorData[i] = (byte)(i + 1);
}
// REDUCE not allowed with XPREQ8
var exc1 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["fizz", "REDUCE", "2", "XB8", smallVectorData, new byte[] { 0, 0, 0, 0 }, "XPREQ8"]));
ClassicAssert.AreEqual("ERR asked quantization mismatch with existing vector set", exc1.Message);
// Create a vector set with XB8 + XPREQ8
var res1 = db.Execute("VADD", ["fizz", "XB8", vectorData1, new byte[] { 0, 0, 0, 0 }, "XPREQ8"]);
ClassicAssert.AreEqual(1, (int)res1);
// Add another element
var res2 = db.Execute("VADD", ["fizz", "XB8", vectorData2, new byte[] { 0, 0, 0, 1 }, "XPREQ8"]);
ClassicAssert.AreEqual(1, (int)res2);
// Verify the vector was stored correctly
var embRes = (string[])db.Execute("VEMB", ["fizz", new byte[] { 0, 0, 0, 0 }]);
ClassicAssert.AreEqual(75, embRes.Length);
for (var i = 0; i < embRes.Length; i++)
{
ClassicAssert.AreEqual((float)vectorData1[i], float.Parse(embRes[i]));
}
}
[Test]
public void VADDErrors()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var vectorSetKey = $"{nameof(VADDErrors)}_{Guid.NewGuid()}";
// Bad arity
var exc1 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD"));
ClassicAssert.AreEqual("ERR wrong number of arguments for 'VADD' command", exc1.Message);
var exc2 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey]));
ClassicAssert.AreEqual("ERR wrong number of arguments for 'VADD' command", exc2.Message);
var exc3 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "FP32"]));
ClassicAssert.AreEqual("ERR wrong number of arguments for 'VADD' command", exc3.Message);
var exc4 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES"]));
ClassicAssert.AreEqual("ERR wrong number of arguments for 'VADD' command", exc4.Message);
var exc5 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1"]));
ClassicAssert.AreEqual("ERR wrong number of arguments for 'VADD' command", exc5.Message);
var exc6 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "1.0"]));
ClassicAssert.AreEqual("ERR wrong number of arguments for 'VADD' command", exc6.Message);
// Reduce after vector
var exc7 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "2", "1.0", "2.0", "bar", "REDUCE", "1"]));
ClassicAssert.AreEqual("ERR invalid option after element", exc7.Message);
// Duplicate flags
// TODO: Redis doesn't error on these which seems... wrong, confirm with them
//var exc8 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "CAS", "CAS"]));
//var exc9 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "NOQUANT", "Q8"]));
//var exc10 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "EF", "1", "EF", "1"]));
//var exc11 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "SETATTR", "abc", "SETATTR", "abc"]));
//var exc12 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "M", "5", "M", "5"]));
// M out of range (Redis imposes M >= 4 and m <= 4096
var exc13 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "M", "1"]));
ClassicAssert.AreEqual("ERR invalid M", exc13.Message);
var exc14 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "M", "10000"]));
ClassicAssert.AreEqual("ERR invalid M", exc14.Message);
// Missing/bad option value
var exc20 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "EF"]));
ClassicAssert.AreEqual("ERR invalid option after element", exc20.Message);
var exc21 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "EF", "0"]));
ClassicAssert.AreEqual("ERR invalid EF", exc21.Message);
var exc22 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "SETATTR"]));
ClassicAssert.AreEqual("ERR invalid option after element", exc22.Message);
var exc23 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "M"]));
ClassicAssert.AreEqual("ERR invalid option after element", exc23.Message);
var exc24 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "2", "2.0", "bar"]));
ClassicAssert.AreEqual("ERR invalid vector specification", exc24.Message);
var exc25 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "0", "bar"]));
ClassicAssert.AreEqual("ERR invalid vector specification", exc25.Message);
var exc26 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "fizz", "bar"]));
ClassicAssert.AreEqual("ERR invalid vector specification", exc26.Message);
// Unknown option
var exc27 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "FOO"]));
ClassicAssert.AreEqual("ERR invalid option after element", exc27.Message);
// Malformed FP32
var binary = new float[] { 1, 2, 3 };
var blob = MemoryMarshal.Cast<float, byte>(binary)[..^1].ToArray();
var exc15 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "FP32", blob, "bar"]));
ClassicAssert.AreEqual("ERR invalid vector specification", exc15.Message);
// Mismatch after creating a vector set
_ = db.KeyDelete(vectorSetKey);
_ = db.Execute("VADD", [vectorSetKey, "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 1, 0 }, "NOQUANT", "EF", "6", "M", "10", "XDISTANCE_METRIC", "L2"]);
var exc16 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "2", "1.0", "2.0", "fizz", "NOQUANT", "EF", "6", "M", "10"]));
ClassicAssert.AreEqual("ERR Vector dimension mismatch - got 2 but set has 75", exc16.Message);
var exc17 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "fizz", "XPREQ8", "EF", "6", "M", "10"]));
ClassicAssert.AreEqual("ERR asked quantization mismatch with existing vector set", exc17.Message);
var exc18 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "fizz", "NOQUANT", "EF", "12", "M", "20"]));
ClassicAssert.AreEqual("ERR asked M value mismatch with existing vector set", exc18.Message);
// TODO: Redis doesn't appear to validate attributes... so that's weird
// Empty Vector Set keys are forbidden (TODO: Remove this constraint)
var exc19 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", ["", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "XPREQ8"]));
ClassicAssert.AreEqual("ERR Vector Set key cannot be empty", exc19.Message);
// Unsupported quantization types (Q8 and BIN are not yet supported)
var exc28 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar"]));
ClassicAssert.AreEqual("ERR Unsupported quantization type", exc28.Message);
var exc29 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "Q8"]));
ClassicAssert.AreEqual("ERR Unsupported quantization type", exc29.Message);
var exc30 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "1", "2.0", "bar", "BIN"]));
ClassicAssert.AreEqual("ERR Unsupported quantization type", exc30.Message);
// Malformed XDISTANCE_METRIC
var exc31 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "bar", "NOQUANT", "XDISTANCE_METRIC"]));
ClassicAssert.AreEqual("ERR invalid option after element", exc31.Message);
var exc32 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "bar", "NOQUANT", "XDISTANCE_METRIC", "FOO"]));
ClassicAssert.AreEqual("ERR invalid XDISTANCE_METRIC", exc32.Message);
var exc33 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VADD", [vectorSetKey, "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "bar", "NOQUANT", "XDISTANCE_METRIC", "XCOSINE_NORMALIZED"]));
ClassicAssert.AreEqual("ERR Distance metric mismatch - got XCosine_Normalized but set has L2", exc33.Message);
}
[Test]
public void VEMB_FP32Storage()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
// Add a vector using VALUES format with NOQUANT (FP32 storage)
var res1 = db.Execute("VADD", ["foo", "VALUES", "8", "1.0", "2.0", "3.0", "4.0", "5.0", "6.0", "7.0", "8.0", new byte[] { 0, 0, 0, 0 }, "NOQUANT"]);
ClassicAssert.AreEqual(1, (int)res1);
// Add a vector using XB8 format with NOQUANT (FP32 storage)
byte[] vectorBytes = new byte[8];
for (int i = 0; i < 8; i++)
{
vectorBytes[i] = (byte)(i + 10);
}
var res2 = db.Execute("VADD", ["foo", "XB8", vectorBytes, new byte[] { 0, 0, 0, 2 }, "NOQUANT"]);
ClassicAssert.AreEqual(1, (int)res2);
// Verify VEMB for XB8 input vector
var res3 = (string[])db.Execute("VEMB", ["foo", new byte[] { 0, 0, 0, 2 }]);
ClassicAssert.AreEqual(8, res3.Length);
for (var i = 0; i < 8; i++)
{
ClassicAssert.AreEqual((float)vectorBytes[i], float.Parse(res3[i]));
}
// Verify VEMB for VALUES input vector
var res4 = (string[])db.Execute("VEMB", ["foo", new byte[] { 0, 0, 0, 0 }]);
ClassicAssert.AreEqual(8, res4.Length);
for (var i = 0; i < 8; i++)
{
ClassicAssert.AreEqual((float)(i + 1), float.Parse(res4[i]));
}
// Verify non-existent element returns empty
var res5 = (string[])db.Execute("VEMB", ["foo", new byte[] { 0, 0, 0, 1 }]);
ClassicAssert.AreEqual(0, res5.Length);
}
[Test]
public void VectorSetOpacity()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = ClassicAssert.Throws<RedisServerException>(() => db.StringGet("foo"));
ClassicAssert.True(res2.Message.Contains("WRONGTYPE"));
}
[Test]
public void VectorElementOpacity()
{
// Check that we can't touch an element with GET despite it also being in the main store
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = (string)db.StringGet(new byte[] { 0, 0, 0, 0 });
ClassicAssert.IsNull(res2);
var res3 = db.KeyDelete(new byte[] { 0, 0, 0, 0 });
ClassicAssert.IsFalse(res3);
var res4 = db.StringSet(new byte[] { 0, 0, 0, 0 }, "def", when: When.NotExists);
ClassicAssert.IsTrue(res4);
// Check we haven't messed up the element
var res7 = (string[])db.Execute("VEMB", ["foo", new byte[] { 0, 0, 0, 0 }]);
ClassicAssert.AreEqual(75, res7.Length);
for (var i = 0; i < res7.Length; i++)
{
var expected =
(i % 4) switch
{
0 => float.Parse("1.0"),
1 => float.Parse("2.0"),
2 => float.Parse("3.0"),
3 => float.Parse("4.0"),
_ => throw new InvalidOperationException(),
};
ClassicAssert.AreEqual(expected, float.Parse(res7[i]));
}
}
[Test]
public void VSIM()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "100.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 1 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res2);
var res3 = (byte[][])db.Execute("VSIM", ["foo", "VALUES", "75", "110.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "COUNT", "5", "EPSILON", "1.0", "EF", "40"]);
ClassicAssert.AreEqual(2, res3.Length);
ClassicAssert.IsTrue(res3.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res3.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
var res4 = (byte[][])db.Execute("VSIM", ["foo", "ELE", new byte[] { 0, 0, 0, 0 }, "COUNT", "5", "EPSILON", "1.0", "EF", "40"]);
ClassicAssert.AreEqual(2, res4.Length);
ClassicAssert.IsTrue(res4.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res4.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
// FP32
var float5 = new float[75];
float5[0] = 3;
for (var i = 1; i < float5.Length; i++)
{
float5[i] = float5[i - 1] + 0.1f;
}
var res5 = (byte[][])db.Execute("VSIM", ["foo", "FP32", MemoryMarshal.Cast<float, byte>(float5).ToArray(), "COUNT", "5", "EPSILON", "1.0", "EF", "40"]);
ClassicAssert.AreEqual(2, res5.Length);
ClassicAssert.IsTrue(res5.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res5.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
// XB8
var byte6 = new byte[75];
byte6[0] = 10;
for (var i = 1; i < byte6.Length; i++)
{
byte6[i] = (byte)(byte6[i - 1] + 1);
}
var res6 = (byte[][])db.Execute("VSIM", ["foo", "XB8", byte6, "COUNT", "5", "EPSILON", "1.0", "EF", "40"]);
ClassicAssert.AreEqual(2, res6.Length);
ClassicAssert.IsTrue(res6.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res6.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
// COUNT > EF
var byte7 = new byte[75];
byte7[0] = 20;
for (var i = 1; i < byte7.Length; i++)
{
byte7[i] = (byte)(byte7[i - 1] + 1);
}
var res7 = (byte[][])db.Execute("VSIM", ["foo", "XB8", byte7, "COUNT", "100", "EPSILON", "1.0", "EF", "40"]);
ClassicAssert.AreEqual(2, res7.Length);
ClassicAssert.IsTrue(res7.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res7.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
// WITHSCORES
var res8 = (byte[][])db.Execute("VSIM", ["foo", "XB8", byte7, "COUNT", "100", "EPSILON", "1.0", "EF", "40", "WITHSCORES"]);
ClassicAssert.AreEqual(4, res8.Length);
ClassicAssert.IsTrue(res8.Where(static (x, ix) => (ix % 2) == 0).Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res8.Where(static (x, ix) => (ix % 2) == 0).Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
ClassicAssert.IsFalse(double.IsNaN(double.Parse(Encoding.UTF8.GetString(res8[1]))));
ClassicAssert.IsFalse(double.IsNaN(double.Parse(Encoding.UTF8.GetString(res8[3]))));
// Large Count
var res9 = (byte[][])db.Execute("VSIM", ["foo", "XB8", byte7, "COUNT", "1000"]);
ClassicAssert.AreEqual(2, res9.Length);
ClassicAssert.IsTrue(res9.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 0 })));
ClassicAssert.IsTrue(res9.Any(static x => x.SequenceEqual(new byte[] { 0, 0, 0, 1 })));
}
[Test]
public void VSIMWithAttribs()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "hello world"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "100.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 1 }, "CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "fizz buzz"]);
ClassicAssert.AreEqual(1, (int)res2);
// Equivalent to no attribute
var res3 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "110.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 2 }, "CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", ""]);
ClassicAssert.AreEqual(1, (int)res3);
// Actually no attribute
var res4 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "120.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 3 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res4);
// Very long attribute
var bigAttr = Enumerable.Repeat((byte)'a', 1_024).ToArray();
var res5 = db.Execute("VADD", ["foo", "REDUCE", "50", "VALUES", "75", "130.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 4 }, "CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", bigAttr]);
ClassicAssert.AreEqual(1, (int)res5);
var res6 = (byte[][])db.Execute("VSIM", ["foo", "VALUES", "75", "140.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "COUNT", "5", "EPSILON", "1.0", "EF", "40", "WITHATTRIBS"]);
ClassicAssert.AreEqual(10, res6.Length);
for (var i = 0; i < res6.Length; i += 2)
{
var id = res6[i];
var attr = res6[i + 1];
if (id.SequenceEqual(new byte[] { 0, 0, 0, 0 }))
{
ClassicAssert.True(attr.SequenceEqual("hello world"u8.ToArray()));
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 1 }))
{
ClassicAssert.True(attr.SequenceEqual("fizz buzz"u8.ToArray()));
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 2 }))
{
ClassicAssert.AreEqual(0, attr.Length);
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 3 }))
{
ClassicAssert.AreEqual(0, attr.Length);
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 4 }))
{
ClassicAssert.True(bigAttr.SequenceEqual(attr));
}
else
{
ClassicAssert.Fail("Unexpected id");
}
}
// WITHSCORES
var res7 = (byte[][])db.Execute("VSIM", ["foo", "VALUES", "75", "140.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "COUNT", "5", "EPSILON", "1.0", "EF", "40", "WITHATTRIBS", "WITHSCORES"]);
ClassicAssert.AreEqual(15, res7.Length);
for (var i = 0; i < res7.Length; i += 3)
{
var id = res7[i];
var score = double.Parse(Encoding.UTF8.GetString(res7[i + 1]));
var attr = res7[i + 2];
ClassicAssert.IsFalse(double.IsNaN(score));
if (id.SequenceEqual(new byte[] { 0, 0, 0, 0 }))
{
ClassicAssert.True(attr.SequenceEqual("hello world"u8.ToArray()));
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 1 }))
{
ClassicAssert.True(attr.SequenceEqual("fizz buzz"u8.ToArray()));
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 2 }))
{
ClassicAssert.AreEqual(0, attr.Length);
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 3 }))
{
ClassicAssert.AreEqual(0, attr.Length);
}
else if (id.SequenceEqual(new byte[] { 0, 0, 0, 4 }))
{
ClassicAssert.True(bigAttr.SequenceEqual(attr));
}
else
{
ClassicAssert.Fail("Unexpected id");
}
}
}
[Test]
public void VDIM()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", ["foo", "REDUCE", "3", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = db.Execute("VDIM", "foo");
ClassicAssert.AreEqual(3, (int)res2);
var res3 = db.Execute("VADD", ["bar", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res3);
var res4 = db.Execute("VDIM", "bar");
ClassicAssert.AreEqual(75, (int)res4);
var exc1 = ClassicAssert.Throws<RedisServerException>(() => db.Execute("VDIM", "fizz"));
ClassicAssert.IsTrue(exc1.Message.Contains("Key not found"));
// TODO: Add WRONGTYPE behavior check once implemented
}
[Test]
public void VSIMWithAttributeFiltering()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
_ = db.KeyDelete("foo");
// Add first vector with year=1980
var res1 = db.Execute("VADD", ["foo", "VALUES", "3", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1980}"]);
ClassicAssert.AreEqual(1, (int)res1);
// Add second vector with year=1960
var res2 = db.Execute("VADD", ["foo", "VALUES", "3", "2.0", "3.0", "4.0", new byte[] { 0, 0, 0, 1 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1960}"]);
ClassicAssert.AreEqual(1, (int)res2);
// Add third vector with year=1940
var res3 = db.Execute("VADD", ["foo", "VALUES", "3", "1.5", "2.5", "3.5", new byte[] { 0, 0, 0, 2 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1940}"]);
ClassicAssert.AreEqual(1, (int)res3);
// Search with filter for year > 1950 - should return 2 results (years 1980 and 1960)
var res5 = (byte[][])db.Execute("VSIM", ["foo", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".year > 1950", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res5.Length,
"Should return 2 results (2 pairs of id+attribute) for year > 1950");
// Verify both results have year > 1950
for (var i = 0; i < res5.Length; i += 2)
{
var attr = res5[i + 1];
var attrStr = Encoding.UTF8.GetString(attr);
ClassicAssert.IsTrue(attrStr.Contains("\"year\":1980") || attrStr.Contains("\"year\":1960"),
$"Result should have year > 1950, got: {attrStr}");
}
// Search with filter for year > 1990 - should return NO results since all years are < 1990
var res4 = (byte[][])db.Execute("VSIM", ["foo", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".year > 1990", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(0, res4.Length,
"Should return 0 results since no vectors have year > 1990");
}
[Test]
public void VSIMWithFilterButWithoutWithAttribs()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
_ = db.KeyDelete("foo");
// Add vectors with attributes
db.Execute("VADD", ["foo", "VALUES", "3", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1980}"]);
db.Execute("VADD", ["foo", "VALUES", "3", "2.0", "3.0", "4.0", new byte[] { 0, 0, 0, 1 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1960}"]);
db.Execute("VADD", ["foo", "VALUES", "3", "1.5", "2.5", "3.5", new byte[] { 0, 0, 0, 2 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1940}"]);
// FILTER without WITHATTRIBS should work: fetch attributes internally and apply filter
var res = (byte[][])db.Execute("VSIM", ["foo", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".year > 1950", "COUNT", "3"]);
// Should return only 2 element ids (no attributes since WITHATTRIBS not specified)
ClassicAssert.AreEqual(2, res.Length,
"Should return 2 element ids (year > 1950) without attributes");
}
[Test]
public void VSIMWithAdvancedFiltering()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
_ = SeedMoviesForAdvancedFiltering(db);
// Test logical AND
var res4 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".year > 1970 and .rating > 4.0", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res4.Length, "Logical AND: year > 1970 AND rating > 4.0");
// Test logical OR
var res5 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".year < 1970 or .year > 2000", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res5.Length, "Logical OR: year < 1970 OR year > 2000");
// Test string equality
var res6 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".genre == \"action\"", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res6.Length, "String equality: genre == 'action'");
// Test arithmetic expression
var res7 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".year / 10 >= 200", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(2, res7.Length, "Arithmetic: year / 10 >= 200");
// Test parentheses grouping
var res8 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", "(.year > 2000 or .year < 1970) and .rating >= 4.0", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(2, res8.Length, "Parentheses grouping");
// Test containment operator (in)
var res9 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", "\"classic\" in .tags", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res9.Length, "Containment: 'classic' in tags");
// Test NOT operator
var res10 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", "not (.genre == \"drama\")", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res10.Length, "NOT operator: not (genre == 'drama')");
// Test complex expression with multiple operators
var res11 = (byte[][])db.Execute("VSIM", ["movies", "VALUES", "3", "0.0", "0.0", "0.0",
"FILTER", ".rating * 2 > 8 and (.year >= 1980 or \"modern\" in .tags)", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res11.Length, "Complex: rating*2 > 8 AND (year>=1980 OR 'modern' in tags)");
}
[Test]
public void VSIMWithAdvancedFilteringELEWithAttribs()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var queryElementId = SeedMoviesForAdvancedFiltering(db);
var res1 = (byte[][])db.Execute("VSIM", ["movies", "ELE", queryElementId,
"FILTER", ".genre == \"action\"", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res1.Length, "ELE + FILTER + WITHATTRIBS: genre == 'action'");
var res2 = (byte[][])db.Execute("VSIM", ["movies", "ELE", queryElementId,
"FILTER", "\"classic\" in .tags", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res2.Length, "ELE + FILTER + WITHATTRIBS: 'classic' in tags");
var res3 = (byte[][])db.Execute("VSIM", ["movies", "ELE", queryElementId,
"FILTER", ".rating / 2 > 2 and .year >= 1980", "COUNT", "3", "WITHATTRIBS"]);
ClassicAssert.AreEqual(4, res3.Length, "ELE + FILTER + WITHATTRIBS: arithmetic and comparison");
}
[Test]
public void VSIMWithAdvancedFilteringELEWithoutWithAttribs()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var queryElementId = SeedMoviesForAdvancedFiltering(db);
var res1 = (byte[][])db.Execute("VSIM", ["movies", "ELE", queryElementId,
"FILTER", ".genre == \"action\"", "COUNT", "3"]);
ClassicAssert.AreEqual(2, res1.Length, "ELE + FILTER without WITHATTRIBS: genre == 'action'");
var res2 = (byte[][])db.Execute("VSIM", ["movies", "ELE", queryElementId,
"FILTER", "\"classic\" in .tags", "COUNT", "3"]);
ClassicAssert.AreEqual(2, res2.Length, "ELE + FILTER without WITHATTRIBS: 'classic' in tags");
var res3 = (byte[][])db.Execute("VSIM", ["movies", "ELE", queryElementId,
"FILTER", ".rating / 2 > 2 and .year >= 1980", "COUNT", "3"]);
ClassicAssert.AreEqual(2, res3.Length, "ELE + FILTER without WITHATTRIBS: arithmetic and comparison");
}
private static byte[] SeedMoviesForAdvancedFiltering(IDatabase db)
{
_ = db.KeyDelete("movies");
var queryElementId = new byte[] { 0, 0, 0, 0 };
var res1 = db.Execute("VADD", ["movies", "VALUES", "3", "1.0", "2.0", "3.0", queryElementId,
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1980,\"rating\":4.5,\"genre\":\"action\",\"tags\":[\"classic\",\"popular\"]}"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = db.Execute("VADD", ["movies", "VALUES", "3", "2.0", "3.0", "4.0", new byte[] { 0, 0, 0, 1 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":1960,\"rating\":3.8,\"genre\":\"drama\",\"tags\":[\"classic\"]}"]);
ClassicAssert.AreEqual(1, (int)res2);
var res3 = db.Execute("VADD", ["movies", "VALUES", "3", "1.5", "2.5", "3.5", new byte[] { 0, 0, 0, 2 },
"CAS", "NOQUANT", "EF", "16", "M", "32", "SETATTR", "{\"year\":2010,\"rating\":4.2,\"genre\":\"action\",\"tags\":[\"modern\"]}"]);
ClassicAssert.AreEqual(1, (int)res3);
return queryElementId;
}
[Test]
public void DeleteVectorSet()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", ["foo", "REDUCE", "3", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
var res2 = db.KeyDelete("foo");
ClassicAssert.IsTrue(res2);
var res3 = db.Execute("VADD", ["fizz", "REDUCE", "3", "VALUES", "75", "100.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res3);
var res4 = db.StringSet("buzz", "abc");
ClassicAssert.IsTrue(res4);
var res5 = db.KeyDelete(["fizz", "buzz"]);
ClassicAssert.AreEqual(2, res5);
}
[Test]
public void InteterruptedVectorSetDelete_AfterMark()
=> InterruptedVectorSetDelete(ExceptionInjectionType.VectorSet_Interrupt_Delete_0);
[Test]
public void InterruptedVectorSetDelete_AfterZeroingOut()
=> InterruptedVectorSetDelete(ExceptionInjectionType.VectorSet_Interrupt_Delete_1);
[Test]
public void InterruptedVectorSetDelete_AfterDelete()
=> InterruptedVectorSetDelete(ExceptionInjectionType.VectorSet_Interrupt_Delete_2);
private void InterruptedVectorSetDelete(ExceptionInjectionType faultLocation)
{
#if !DEBUG
ClassicAssert.Ignore("Relies on ExceptionInjectionHelper, disable in non-DEBUG");
#endif
var key = $"{nameof(InterruptedVectorSetDelete)}_{faultLocation}";
using (var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig()))
{
var db = redis.GetDatabase();
var res1 = db.Execute("VADD", [key, "REDUCE", "3", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 0 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res1);
// TODO: we could use EXISTS here... except not all non-Vector Set commands understand Vector Sets, so that's a bit flaky
ExceptionInjectionHelper.EnableException(faultLocation);
try
{
_ = ClassicAssert.Throws<RedisServerException>(() => db.KeyDelete(key));
}
finally
{
ExceptionInjectionHelper.DisableException(faultLocation);
}
}
using (var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig()))
{
var db = redis.GetDatabase();
var deleteWasEffective = false;
try
{
_ = (string)db.StringGet(key);
deleteWasEffective = true;
}
catch
{
}
var vectorSetCommands = Enum.GetValues<RespCommand>().Where(static x => x.IsLegalOnVectorSet() && x is not (RespCommand.DEL or RespCommand.UNLINK or RespCommand.TYPE or RespCommand.DEBUG or RespCommand.RENAME or RespCommand.RENAMENX)).OrderBy(static x => x);
if (!deleteWasEffective)
{
// Check that all Vector Set commands on a partially deleted vector set give a reasonable error message OR succeed
//
// Success is possible if the delete failed early enough that we didn't actually being a "real" delete
//
// Such cases leave some trash around, but it'll be cleaned up either at restart or the next time a Vector Set is really deleted
foreach (var cmd in vectorSetCommands)
{
RedisServerException exc = null;
switch (cmd)
{
case RespCommand.VADD:
try
{
var res = db.Execute("VADD", [key, "REDUCE", "3", "VALUES", "75", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", "4.0", "1.0", "2.0", "3.0", new byte[] { 0, 0, 0, 1 }, "CAS", "NOQUANT", "EF", "16", "M", "32"]);
ClassicAssert.AreEqual(1, (int)res);
}
catch (RedisServerException e)
{
exc = e;
}
break;
case RespCommand.VCARD:
// TODO: Implement once VCARD is implemented
continue;
case RespCommand.VDIM:
try
{
var res = db.Execute("VDIM", [key]);
ClassicAssert.AreEqual(3, (int)res);
}
catch (RedisServerException e)
{
exc = e;
}
break;
case RespCommand.VEMB:
try
{
var res = (string[])db.Execute("VEMB", [key, new byte[] { 0, 0, 0, 0 }]);
ClassicAssert.AreEqual(75, res.Length);
}
catch (RedisServerException e)
{
exc = e;
}
break;
case RespCommand.VGETATTR:
try
{
var res = db.Execute("VGETATTR", [key, "wololo"]);
ClassicAssert.IsTrue(res.IsNull);
}
catch (RedisServerException e)
{
exc = e;
}
break;
case RespCommand.VINFO: