在 mnesia 中限制某个键下的记录数量的惯用方法是什么?
我使用mnesia为用户存储数据,记录是一个包,结构如下
{ username, field1, filed2, timestamp }
为了不让数据库爆炸,我想对属于某个用户的记录数设置一个限制,比如说,如果记录数如果用户达到 500,则在插入新记录之前删除具有最早时间戳的记录。
有没有有效的方法来做到这一点?
提前致谢。
I use mnesia to store data for users, and the record is a bag structured like
{ username, field1, filed2, timestamp }
In order not to let the database explode, I want to set a limit for the number of records belonging to a certain user, say, if the number of records for a user reaches 500, then the record with the oldest timestamp is deleted before a new record is inserted.
Is there an efficient way to do this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种方法可能是让您的记录位于
value_list 包含值列表的位置。 可以执行此类操作
现在,您的桌子可以是一套而不是袋子,并且无论何时插入都 。 NewList 将最多包含 500 个元素,并且由于最旧的元素位于末尾,因此如果您有 501 个元素,最后一个元素将位于 _ignore 列表中,您将...忽略。
您可以在密钥上进行恒定时间查找以进行某些列表操作,但这可能是一个很好的权衡,具体取决于您的应用程序。您还可以摆脱时间戳字段和关联的代码来维护该字段。
Another way might be to have your record be
where value_list contains a list of values. Your table can now be a set instead of a bag and you can do this type of thing
whenever you insert. NewList will contain at most 500 elements and since the oldest elements are on the end, if you have 501 elements the last will be in the _ignore list which you will... Ignore.
Your trading a constant time lookup on your key for some list manipulation, but might be a good trade-off depending on your application. You also may be able to get rid of the timestamp field and associated code to maintain that field.
我提供了两种可能性。一种适合您的设计,另一种对您的记录定义进行微小的更改。查看哪一款最适合您的需求。下面的第一个在您的设计中工作。
交易中的
length/1
函数并不是最优的。让我们想一个更好的办法。另外,我不知道你的时间戳是什么样子,所以我确信你有办法对它们进行排序并找出最新的时间戳,选出拥有该时间戳的记录,然后返回它。我给出这个解决方案只是为了适合您的设计。我还认为有些地方我们需要一些索引
。下一个实施对我来说似乎更好。如果我们可以更改记录定义并引入一个采用bool()
的字段oldest
并像这样对其进行索引,会怎么样The above implementation seems better to me. success !!
I have provided two possibilities. One that fits within your design and one that introduces a small change in your record definition. See which one best fits your needs. The first one below is working within your design.
The
length/1
function within the transaction is not optimal. Lets think of a better way. Also, i do not know how your timestamps look like so am sure you have a way of sorting them and finding out the latest timestamp, pick out the record which owns this timestamp and then return it. I have given this solution just to fit your design. I also think the some where we need someindexing
. The next implementation seems better to me. Some how if we can change the record definition and we introduce a fieldoldest
which takes abool()
and we index it like thisThe above implementation seems better to me. success !!