erlang - How should I auto-expire entires in an ETS table, while also limiting its total size? -
i have lot of analytics data i'm looking aggregate every (let's 1 minute.) data being sent process stores in ets table, , every timer sends message process table , remove old data.
the problem amount of data comes in varies wildly, , need 2 things it:
- if amount of data coming in big, drop oldest data , push new data in. viewed fixed size queue, if amount of data hits limit, queue start dropping things front new data comes back.
- if queue isn't full, data has been sitting there while, automatically discard (after fixed timeout.)
if these 2 conditions kept, assume table has constant size, , in newer x.
the problem haven't found efficient way these 2 things together. know use match specs delete entires older x, should pretty fast if index timestamp. though i'm not sure if best way periodically trim table.
the second problem keeping total table size under limit, i'm not sure how do. 1 solution comes mind use auto-increment field wich each insert, , when table being trimmed, @ first , last index, calculate difference , again, use match specs delete below threshold.
having said this, feels might using ets table wasn't designed do. there better way store data this, or approaching problem correctly?
you can determine amount of data occupied using ets:info(tab, memory). result in number of words. there catch. if storing binaries heap binaries included. if storing normal erlang terms can use , timestamp described, way go. size in bytes multiply erlang:system_info(wordsize).
Comments
Post a Comment