Robert Campbell
Robert Campbell

Reputation: 6958

What is the easiest way to persist maps/structs in Clojure?

The obvious way is to load up JDBC support from Clojure Contrib and write some function to translate a map/struct to a table. One drawback of this is that it isn't very flexible; changes to your structure will require DDL changes. This implies either writing DDL generation (tough) or hand-coding migrations (boring).

What alternatives exist? Answers must be ACID, ruling out serializing to a file, etc.

Upvotes: 9

Views: 2751

Answers (6)

claj
claj

Reputation: 5402

MongoDB and it's framework congomongo (lein: [congomongo "0.1.3-SNAPSHOT"]) works for me. It's incredible nice with the schemaless databases, and congomongo is quite easy to get along with. MongoDB adds an _id-field in every document to keep it identified, and there is quite good transparency between clojure-maps and mongo-maps.

https://github.com/somnium/congomongo

EDIT: I would not use MongoDB today. I would suggest you use transit. I would use JSON if the backend (Postgres etc) support it or the msgpack coding if you want to have a more compact binary encoding.

Upvotes: 1

Markc
Markc

Reputation: 1057

FleetDB is a database implemented in Clojure. It has a very natural syntax for working with maps/structs, e.g. to insert:

(client ["insert" "accounts" {"id" 1, "owner" "Eve", "credits" 100}])

Then select

(client ["select" "accounts" {"where" ["=" "id" 1]}])

http://fleetdb.org/

Upvotes: 10

Eric Normand
Eric Normand

Reputation: 3836

You could try one of the Java-based graph databases, such as Neo4J. It might be easy to code up a hashmap interface to make it reasonably transparent.

Upvotes: 2

Cosmin Stejerean
Cosmin Stejerean

Reputation: 1398

One option for persisting maps in Clojure that still uses a relation database is to store the map data in an opaque blob. If you need the ability to search for records you can store indexes in separate tables. For example you can read how FriendFeed is storing schemaless data on top of MySQL - http://bret.appspot.com/entry/how-friendfeed-uses-mysql

Another option is to use the Entity-Attribute-Value model (EAV) for storing data in a database. You can read more about EAV on Wikipedia (I'd post a link but I'm a new user and can only post one link).

Yet another option is to use BerkeleyDB for Java - it's a native Java solution providing ACID and record level locking. (Same problem with posting a link).

Upvotes: 8

pmf
pmf

Reputation: 7749

Using CouchDB's Java-client lib and clojure.contrib.json.read/write works reasonably well for me. CouchDB's consistency guarantees may not be strong enough for your purposes, though.

Upvotes: 6

l0st3d
l0st3d

Reputation: 2968

Clj-record is an implementation of active record in clojure that may be of interest to you.

Upvotes: 2

Related Questions