erlang - how can I match tuple contents with qlc and mnesia?
Asked Answered
R

2

4

I have a mnesia table for this record.

-record(peer, {
    peer_key,   %% key is the tuple {FileId, PeerId}
    last_seen,
    last_event,
    uploaded = 0,
    downloaded = 0,
    left = 0,
    ip_port,
    key
}).

Peer_key is a tuple {FileId, ClientId}, now I need to extract the ip_port field from all peers that have a specific FileId.

I came up with a workable solution, but I'm not sure if this is a good approach:

qlc:q([IpPort || #peer{peer_key={FileId,_}, ip_port=IpPort} <- mnesia:table(peer), FileId=:=RequiredFileId])

Thanks.

Repetitious answered 21/7, 2009 at 21:23 Comment(0)
F
3

Using on ordered_set table type with a tuple primary key like { FileId, PeerId } and then partially binding a prefix of the tuple like { RequiredFileId, _ } will be very efficient as only the range of keys with that prefix will be examined, not a full table scan. You can use qlc:info/1 to examine the query plan and ensure that any selects that are occurring are binding the key prefix.

Farming answered 23/7, 2009 at 14:42 Comment(0)
C
0

Your query time will grow linearly with the table size, as it requires scanning through all rows. So benchmark it with realistic table data to see if it really is workable.

If you need to speed it up you should focus on being able to quickly find all peers that carry the file id. This could be done with a table of bag-type with [fileid, peerid] as attributes. Given a file-id you would get all peers ids. With that you could construct your peer table keys to look up.

Of course, you would also need to maintain that bag-type table inside every transaction that change the peer-table.

Another option would be to repeat fileid and add a mnesia index on that column. I am just not that into mnesia's own secondary indexes.

Castanets answered 22/7, 2009 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.