TransactionalHashMap

33 views
Skip to first unread message

Mark McGranaghan

unread,
Oct 11, 2008, 9:50:11 PM10/11/08
to Clojure
I was just wondering what the intended use is for
TransactionalHashMap. Its a Java class defined in the Clojure source,
but not used anywhere else in the core code. How does this class fit
in with Clojure's PersistentHashMap and Java's ConcurrentHashMap?

- Mark

Chouser

unread,
Oct 11, 2008, 11:06:25 PM10/11/08
to clo...@googlegroups.com

"rhickey added TransactionalHashMap, an implementation of
java.util.ConcurrentHashMap that works in/with transactions"

http://clojure-log.n01se.net/date/2008-08-01.html#09:36

It looks to me like it's mutable, but all the data is actually stored
in refs, so you can only do your mutations in a transaction:

(def x
(dosync
(doto (clojure.lang.TransactionalHashMap.)
(put :a 1)
(put :b 2))))

So: thread safe, transactional, mutable ConcurrentHashMap. Maybe it'd
be useful for passing into Java libs?

Unfortunately you apparently can't print them in SVN 1063:

java.lang.ClassCastException: clojure.lang.PersistentHashMap cannot be
cast to java.util.Collection
at clojure.lang.TransactionalHashMap.entrySet(TransactionalHashMap.java:127)
at clojure.lang.RT.seqFrom(RT.java:463)
at clojure.lang.RT.seq(RT.java:452)
at clojure.seq__28.invoke(boot.clj:92)
at clojure.fn__1952$fn__1954.invoke(boot.clj:3524)
at clojure.print_ctor__1889.invoke(boot.clj:3433)
at clojure.fn__1952.invoke(boot.clj:3517)
at clojure.lang.MultiFn.invoke(MultiFn.java:152)
at clojure.lang.Var.invoke(Var.java:323)
at clojure.lang.RT.print(RT.java:1165)
at clojure.lang.Repl.main(Repl.java:88)

--Chouser

Rich Hickey

unread,
Oct 12, 2008, 11:07:48 AM10/12/08
to Clojure


On Oct 11, 11:06 pm, Chouser <chou...@gmail.com> wrote:
> On Sat, Oct 11, 2008 at 9:50 PM, Mark McGranaghan <mmcgr...@gmail.com> wrote:
>
> > I was just wondering what the intended use is for
> > TransactionalHashMap. Its a Java class defined in the Clojure source,
> > but not used anywhere else in the core code. How does this class fit
> > in with Clojure's PersistentHashMap and Java's ConcurrentHashMap?
>
> "rhickey added TransactionalHashMap, an implementation of
> java.util.ConcurrentHashMap that works in/with transactions"
>
> http://clojure-log.n01se.net/date/2008-08-01.html#09:36
>
> It looks to me like it's mutable, but all the data is actually stored
> in refs, so you can only do your mutations in a transaction:
>
> (def x
> (dosync
> (doto (clojure.lang.TransactionalHashMap.)
> (put :a 1)
> (put :b 2))))
>

Right, you _have_ to do any modifications in transactions, and you
_can_ issue reads in transactions to get snapshot consistency.

> So: thread safe, transactional, mutable ConcurrentHashMap.

It's much more powerful than a ConcurrentHashMap because it supports
atomic read-modify-write, atomic changes involving more than one item,
atomic changes involving more than one TransactionalHashMap etc. These
all fall out of its being transactional.

>Maybe it'd
> be useful for passing into Java libs?

It's useful anytime you find that a single ref holding a map of your
entire model has insufficient concurrency granularity. One option
would be a ref to a map of refs, the other is this
TransactionalHashMap.

With a ref to map of refs, any key insertions/removals still contend
for the root, and the client has to deal with refs as the values, and
you have a ref per value.

With TransactionalHashMap, key insertions/removals are distributed
across a set of bins, there are only Nbins refs, and Nbins acts as a
'knob' on the required granularity.

So, it will be very useful for larger Clojure programs, while
remaining interface-compatible with Java Map.

It's a first experiment in providing transactionally-mutable objects.
I'm sorry I haven't had time to write it up yet, but feel free to
experiment with it.

>
> Unfortunately you apparently can't print them in SVN 1063:
>
> java.lang.ClassCastException: clojure.lang.PersistentHashMap cannot be
> cast to java.util.Collection

Fixed in SVN 1064 - thanks for the report,

Rich
Reply all
Reply to author
Forward
0 new messages