Skip to content
This repository

Reusable persistence layer for use with Basho's Riak

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 src
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE.md
Octocat-spinner-32 README.md
Octocat-spinner-32 pom.xml
README.md

riak-scala-dao

Reusable persistence layer for use with Basho's Riak

Requirements

  • Scala 2.8.2 or 2.9.1
  • riak-java-client 1.0.5

Using RiakJSONEntityDAO

case class Skateboard(id: String, brand: String, model: String, year: Int)
  extends PersistentEntity { def getKey=id }

val riakClient: IRiakClient = RiakFactory.pbcClient("localhost", 8087)
val skateboardDao = new RiakJSONEntityDAO[String, Skateboard]
  (new RiakDriver[Skateboard]("skateboards", riakClient))
skateboardDao.addStringIndex("brand")

val skateboard = new Skateboard("1", "Real", "Justin Brock", 2012)
skateboardDao.persist(skateboard.id, skateboard)

Extending AbstractRiakEntityDAO

case class Guitar(var id: String, make: String, model: String, year: Int) {}

class GuitarDAO(bucket: String, storageDriver: RiakStorageDriver[String, Guitar])
  extends AbstractRiakEntityDAO[String, Guitar]("guitars", storageDriver) with Converter[Guitar] {

  def fromDomain(guitar: Guitar, vClock: VClock): IRiakObject = {
    val dataAsString = generate(guitar)
    val data = (dataAsString).map(_.toChar).toCharArray.map(_.toByte)

    val iRiakObject = RiakObjectBuilder.newBuilder("guitars", guitar.id).withVClock(vClock)
      .withContentType(Constants.CTYPE_JSON)
      .withValue(data).build()
    iRiakObject.addIndex("make", guitar.make)

  }

  def toDomain(riakObject: IRiakObject) = {
    val data = riakObject.getValueAsString
    parse[Guitar](data)
  }
}

Examples

val riakClient: IRiakClient = RiakFactory.pbcClient("localhost", 8087)
val guitarDao = new GuitarDAO(new RiakDriver[Guitar]("guitars", riakClient))

Saving

val jazzMaster = new Guitar("1", "fender", "JazzMaster", 1963)
guitarDao.persist(jazzMaster.id, jazzMaster)

Retreiving

val jazzMaster = new Guitar("1", "fender", "JazzMaster", 1963)
guitarDao.persist(jazzMaster.id, jazzMaster)
guitarDao.getByKey("1")

Deleting

val jazzMaster = new Guitar("1", "fender", "JazzMaster", 1963)
guitarDao.persist(jazzMaster.id, jazzMaster)
guitarDao.deleteByKey("1")

Secondary Indexing

val jazzMaster = new Guitar("1", "fender", "JazzMaster", 1963)
guitarDao.persist(jazzMaster.id, jazzMaster)
guitarDao.findFor2i("make", "fender")

License

Copyright (c) 2012 Ray Jenkins

Published under The Apache License, Version 2.0, see LICENSE

Something went wrong with that request. Please try again.