Good day. Understanding ETS tables in Erlang

I want to check the table in a brute force, and check every record (Unix time). I do not want to use matching (ets: select (food, ets: fun2ms (fun (N = #food {calories = C}) when C <600 -> N end)).)

Instead, I try to check the ets: next function. In this case, this function returns only the key of the record, which is not enough for further verification.

Tell me, or show where you can find information on how to get the full data rows in the table via ets: new / ets: first / ets: last

Maybe I generally initially incorrectly create the ets table: new (auth, [ordered_set]).

  • Can you describe the problem, not the solution? - Mikhail Vaysman
  • You need to delete old records from ETS by iterating over each record, and not through ets: select - Eugen Dubrovin
  • old in time? Or is there another criterion for old age? - Mikhail Vaysman
  • Yes, when inserting into a table - I write down the current time with the data + the lifetime of the recording (in Unicast) - Eugen Dubrovin
  • one
    And if you do not write the time of life, and the time code entry is no longer valid? Then it will be easier to filter. - Mikhail Vaysman

1 answer 1

Solution found.

FromTime / ToTime - {{2017,1,13}, {14,15,11}} translate into datetime_to_gregorian_seconds first

FromSec = calendar:datetime_to_gregorian_seconds(FromTime), ToSec = calendar:datetime_to_gregorian_seconds(ToTime), 

Next, call the function - lookup_by_date(FromTime, ToTime)

Code:

 lookup_by_date(FromTime, ToTime) -> lookup_by_date(FromTime, ToTime, ets:first(auth), []). lookup_by_date(_FromTime, _ToTime, '$end_of_table', Acc) -> {reply, Acc, ok}; lookup_by_date(FromTime, ToTime, Key, Acc) -> case ets:lookup(auth, Key) of [{Login, Pass, TTL, Unix, Unix2}] -> F = calendar:datetime_to_gregorian_seconds(Unix2), T = calendar:datetime_to_gregorian_seconds(Unix2), if F >= FromTime, T =< ToTime -> NewAcc = [{Login, Pass, TTL, Unix, Unix2}|Acc], N = ets:next(auth, Key), lookup_by_date(FromTime, ToTime, N, NewAcc); true -> N = ets:next(auth, Key), lookup_by_date(FromTime, ToTime, N, Acc) end end. 
  • Please mark the question as accepted so that others can use it - Mikhail Vaysman
  • Made. A new person is here, I don’t know all the nuances :) - Eugen Dubrovin