@@ -295,4 +295,93 @@ defmodule Nebulex.Adapters.Local.QueryHelperTest do
295295 assert Enum . sort ( result ) == [ { 4 , 100 } , { 5 , 200 } ]
296296 end
297297 end
298+
299+ describe "keyref_match_spec/2" do
300+ test "returns a valid match spec" do
301+ ms = keyref_match_spec ( :user_123 )
302+
303+ # Should return a valid ETS match spec (list of tuples)
304+ assert is_list ( ms )
305+ assert length ( ms ) == 1
306+ assert { _pattern , _guards , _select } = hd ( ms )
307+ end
308+
309+ test "returns a valid match spec with cache filter" do
310+ ms = keyref_match_spec ( :user_123 , cache: MyApp.Cache )
311+
312+ assert is_list ( ms )
313+ assert length ( ms ) == 1
314+ end
315+ end
316+
317+ describe "keyref_match_spec/2 integration with ETS" do
318+ setup do
319+ table_name = :"test_keyref_table_#{ :erlang . unique_integer ( [ :positive ] ) } "
320+ table = :ets . new ( table_name , [ :set , :public , :named_table , keypos: 2 ] )
321+
322+ # Insert some regular entries
323+ true = :ets . insert ( table , { :entry , :key1 , "value1" , 1000 , 2000 , nil } )
324+ true = :ets . insert ( table , { :entry , :key2 , "value2" , 1100 , 2100 , nil } )
325+
326+ # Insert keyref entries (cache references)
327+ # Reference to :user_123 in nil cache (local reference)
328+ true =
329+ :ets . insert (
330+ table ,
331+ { :entry , :ref1 , { :"$nbx_keyref_spec" , nil , :user_123 , nil } , 1200 , :infinity , nil }
332+ )
333+
334+ # Reference to :user_123 in MyApp.Cache
335+ true =
336+ :ets . insert (
337+ table ,
338+ { :entry , :ref2 , { :"$nbx_keyref_spec" , MyApp.Cache , :user_123 , nil } , 1300 , :infinity , nil }
339+ )
340+
341+ # Reference to :user_456 in MyApp.Cache
342+ true =
343+ :ets . insert (
344+ table ,
345+ { :entry , :ref3 , { :"$nbx_keyref_spec" , MyApp.Cache , :user_456 , nil } , 1400 , :infinity , nil }
346+ )
347+
348+ # Reference to :user_123 in AnotherCache
349+ true =
350+ :ets . insert (
351+ table ,
352+ { :entry , :ref4 , { :"$nbx_keyref_spec" , AnotherCache , :user_123 , nil } , 1500 , :infinity , nil }
353+ )
354+
355+ on_exit ( fn ->
356+ if :ets . whereis ( table_name ) != :undefined do
357+ :ets . delete ( table )
358+ end
359+ end )
360+
361+ % { table: table }
362+ end
363+
364+ test "gets all reference keys pointing to a specific key (any cache)" , % { table: table } do
365+ ms = keyref_match_spec ( :user_123 )
366+ result = :ets . select ( table , ms ) |> Enum . sort ( )
367+
368+ # Should return the keys of all reference entries
369+ assert result == [ :ref1 , :ref2 , :ref4 ]
370+ end
371+
372+ test "gets reference keys for a specific cache" , % { table: table } do
373+ ms = keyref_match_spec ( :user_123 , cache: MyApp.Cache )
374+ result = :ets . select ( table , ms )
375+
376+ # Should return only :ref2 (reference to :user_123 in MyApp.Cache)
377+ assert result == [ :ref2 ]
378+ end
379+
380+ test "returns empty when no references exist" , % { table: table } do
381+ ms = keyref_match_spec ( :nonexistent_key )
382+ result = :ets . select ( table , ms )
383+
384+ assert result == [ ]
385+ end
386+ end
298387end
0 commit comments