@@ -39,8 +39,9 @@ func TestMain(m *testing.M) {
3939}
4040
4141type testObject struct {
42- ID uint64
43- Tags part.Set [string ]
42+ ID uint64 `json:"id" yaml:"id"`
43+ Key string `json:"key,omitempty" yaml:"key,omitempty"`
44+ Tags part.Set [string ] `json:"tags" yaml:"tags"`
4445}
4546
4647func (t testObject ) getID () uint64 {
8687 Unique : true ,
8788 }
8889
90+ keyIndex = Index [testObject , string ]{
91+ Name : "key" ,
92+ FromObject : func (t testObject ) index.KeySet {
93+ return index .NewKeySet (index .String (t .Key ))
94+ },
95+ FromKey : index .String ,
96+ FromString : index .FromString ,
97+ Unique : true ,
98+ }
99+
89100 tagsIndex = Index [testObject , string ]{
90101 Name : "tags" ,
91102 FromObject : func (t testObject ) index.KeySet {
@@ -1209,6 +1220,81 @@ func TestDB_InitializationTransitions(t *testing.T) {
12091220 requireClosed (t , initWatch )
12101221}
12111222
1223+ func TestDB_EmptyKeys (t * testing.T ) {
1224+ db := New ()
1225+ table , err := NewTable (
1226+ db ,
1227+ "test" ,
1228+ keyIndex ,
1229+ tagsIndex ,
1230+ )
1231+ require .NoError (t , err , "NewTable[testObject]" )
1232+
1233+ // Do the tests twice, one with empty table and again with a non-empty
1234+ // table.
1235+ for range 2 {
1236+ // Object with an empty primary key and non-empty secondary key
1237+ wtxn := db .WriteTxn (table )
1238+ table .Insert (wtxn , testObject {Tags : part .NewSet ("test" )})
1239+ obj , _ , found := table .Get (wtxn , keyIndex .Query ("" ))
1240+ require .True (t , found )
1241+ require .Equal (t , "" , obj .Key )
1242+ require .Equal (t , []string {"test" }, slices .Collect (obj .Tags .All ()))
1243+ obj , _ , found = table .Get (wtxn , tagsIndex .Query ("test" ))
1244+ require .True (t , found )
1245+ require .Equal (t , "" , obj .Key )
1246+ require .Equal (t , []string {"test" }, slices .Collect (obj .Tags .All ()))
1247+ for obj := range table .Prefix (wtxn , keyIndex .Query ("" )) {
1248+ require .Equal (t , "" , obj .Key )
1249+ require .Equal (t , []string {"test" }, slices .Collect (obj .Tags .All ()))
1250+ break
1251+ }
1252+ wtxn .Abort ()
1253+
1254+ // Object with an empty primary key and empty secondary key
1255+ wtxn = db .WriteTxn (table )
1256+ table .Insert (wtxn , testObject {Tags : part .NewSet ("" )})
1257+ obj , _ , found = table .Get (wtxn , keyIndex .Query ("" ))
1258+ require .True (t , found )
1259+ require .Equal (t , "" , obj .Key )
1260+ require .Equal (t , []string {"" }, slices .Collect (obj .Tags .All ()))
1261+ obj , _ , found = table .Get (wtxn , tagsIndex .Query ("" ))
1262+ require .True (t , found )
1263+ require .Equal (t , "" , obj .Key )
1264+ require .Equal (t , []string {"" }, slices .Collect (obj .Tags .All ()))
1265+ for obj := range table .Prefix (wtxn , keyIndex .Query ("" )) {
1266+ require .Equal (t , "" , obj .Key )
1267+ require .Equal (t , []string {"" }, slices .Collect (obj .Tags .All ()))
1268+ break
1269+ }
1270+ for obj := range table .Prefix (wtxn , tagsIndex .Query ("" )) {
1271+ require .Equal (t , "" , obj .Key )
1272+ require .Equal (t , []string {"" }, slices .Collect (obj .Tags .All ()))
1273+ break
1274+ }
1275+ wtxn .Abort ()
1276+
1277+ // Object with non-empty primary key and empty secondary key
1278+ wtxn = db .WriteTxn (table )
1279+ table .Insert (wtxn , testObject {Key : "test" , Tags : part .NewSet ("" )})
1280+ obj , _ , found = table .Get (wtxn , keyIndex .Query ("test" ))
1281+ require .True (t , found )
1282+ require .Equal (t , "test" , obj .Key )
1283+ require .Equal (t , []string {"" }, slices .Collect (obj .Tags .All ()))
1284+ obj , _ , found = table .Get (wtxn , tagsIndex .Query ("" ))
1285+ require .True (t , found )
1286+ require .Equal (t , "test" , obj .Key )
1287+ require .Equal (t , []string {"" }, slices .Collect (obj .Tags .All ()))
1288+ wtxn .Abort ()
1289+
1290+ // Insert another object and try again.
1291+ wtxn = db .WriteTxn (table )
1292+ table .Insert (wtxn , testObject {Key : "non-empty" , Tags : part .NewSet ("non-empty" )})
1293+ wtxn .Commit ()
1294+ }
1295+
1296+ }
1297+
12121298func TestWriteJSON (t * testing.T ) {
12131299 t .Parallel ()
12141300
0 commit comments