[erlang-questions] Noddy question on hashing

Siraaj Khandkar siraaj@REDACTED
Thu Jul 25 18:38:29 CEST 2013

On 07/25/2013 05:52 AM, Rick Payne wrote:> Hi,
 > I've been using erlang for some simple projects, and really enjoying
 > it.  However, when thinking about some of the things I'd like to do -
 > I'm struggling a bit with hashing and how to handle that in a
 > concurrent way in erlang. This is probably 101 stuff, sorry...
 > Imagine we have a set of data streams giving us objects and
 > attributes.  Attributes can be a few kB but typically are less than
 > 1kB. The objects are unique but some set of them share the attributes.
 > Typically in C, I'm used to hashing the attributes and pointing at
 > them from the objects. We hash to save memory and refcount so we know
 > when we can delete from the hash. The good news is that the attributes
 > are never updated in place, apart from the refcount.
 > However, I'm struggling to get my head around how I'd do this in
 > erlang efficiently. Any suggestions?

Erlang joke: "To have fault-tolerant objects, you must have at least two
copies of each object." :)

Generally one does not approach things this was in Erlang. Space
efficiency was not its motivation, fault-tolerance was, and thus
redundancy is the law of the land.

That said, if you have some large blob that you want several objects or
even processes to point to, instead of copying, you could use ETS, which
is basically a shared hash table.


If you're interested in correctness and want those attributes to be
enums - you could use atoms as values and tell Dialyzer what each is
expected to be, like so:

     -type attribute_a() :: a
                          | b
                          | c

     -type attribute_b() :: d
                          | e
                          | f

         { a :: attribute_a()
         , b :: attribute_b()

     -type object() :: #object{}.

This also works if those attributes are actually keys to the values
stored in ETS.

More information about the erlang-questions mailing list