0

I'm attempting to write a simple vote kicking option to add to my existing server.

I have a message router using gen_server and this stores all connected clients using init([]) -> {ok, dict:new()}. The router is separate from the tcp server which is also a gen_server but handles tcp requests which are then relayed to the router.

Since "things" are being handled/stored separately I want to store the user to be kicked, a list of voters and whether it succeeded. So this would need to be in a way a global at least within the router and ideally separate form the clients dict.

Any ideas on the best/ideal approach?
I believe I could create another gen_server "router" which stores people to kick in a record structure {kick, {Votes, Passed}} but I don't know if that's ideal.

2
  • try using ets table
    – igor
    Apr 5, 2015 at 13:24
  • @kitty I haven't used ets table. I've briefly read about them and don't quite full understand them. I'm fairly new to the Erlang language so there's a lot of functionality I'm unaware of. If you could explain how an ets table could be used in this fashion?
    – SharkBytes
    Apr 6, 2015 at 7:08

1 Answer 1

0

You want "a user ID, a list of voters, and the outcome". Let's see how that looks as a tuple:

{User :: user_id(), [Voters :: user_id()], Outcome :: boolean()}

Not so hard. A list of those tuples -- also not hard. A dict of those is a bit more complicated, because you wind up with (essentially, if this were a proplist, using the types above):

{User, {[Voters], Outcome}}

Which is a bit less happy to deal with. You may want to filter by outcome, for example, but that is now now buried within a tuple within a tuple whereas a list of flat tuples permits simple filtering (either a real filter, or just guarding a list comprehension, or whatever) or use lists:keysearch/3. This makes complex searches more awkward, especially if you want to know how many times a particular voter has voted to kick someone.

The basic problem could certainly be made easier by using ETS, as it has facilities for dealing fully with tuples in a much quicker way than lists of tuples using keysearch/3 (and, I am assuming you have thousands and thousands of cases so that lists of tuples is inadequate -- but as always, try it out that way first, you might find that lists of tuples is totally adequate!). On the other hand, if the search cases get even a little more complex, or the cases need to be stored durably and still benefit from in-memory manipulation, or you need multiple indexes, etc. then you should really move on to Mnesia.

Beyond that... you are dealing with nested data, and while that is actually just fine 90% of the time, that other 10% of the time you wind up either reinventing relational data (but worse, with less actual utility, and slower) or just suffering through a bunch of baroque procedural code to achieve an effect that could be had for free with very little pain by simply using something like Postgres (I happen to be extremely familiar with this DB, so to me its a "lightweight solution" -- I understand this is not the case for everyone).

For reference, your data, when decomposed looks like this (assuming that user ids are email addresses for the sake of the example):

table user
  attributes
    id       EmailAddress
  conditions
    pk id

table kick
  attributes
    id       UUID
    user     EmailAddress
    outcome  Boolean
  conditions
    pk id
    fk user

table vote
  attributes
    kick     UUID
    user     EmailAddress
  conditions
    pk (kick user)
    fk kick
    fk user

While it is obvious that the User and [Voter] components of the tuples described above reference user records, it is not quite as obvious why trying to pull a list of all votes cast by a certain user using tuples is such a nasty process. When we look at the decomposed data we realize that this is three tables, and we really wish we could run a query on the vote one by itself if you have this requirement. But if you don't have that requirement, then don't worry about it and just use tuples in ETS or Mnesia! :-) This kind of data is pretty tiny in Erlang.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.