public
Created — forked from jrwest/dodelete.scala

Scalaz Functional IO Post Gist Files

  • Download Gist
dodelete.scala
Scala
1 2
val deleteSomeKey: DeleteObject = bucket.delete("some-key")
deleteSomeKey.execute() // actually makes the request to delete the key
fetch.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def rawFetch(key: String): IO[RiakResponse] = {
val emptyFetchMeta = new FetchMeta.Builder().build() // how this really built is unimportant
rawClient.fetch(name, key, emptyFetchMeta).pure[IO] // 6, rawClient is equivalent to client above
}
def fetch[T](key: String): IO[Validation[Throwable, Option[T]] = {
(rawFetch(key) map {
 
riakResponseToResult(_) // 7
 
}) except { t => t.fail.pure[IO] } // 8
}
 
def riakResponseToResult[T](r: RiakResponse): Validation[Throwable, Option[T]]] = ...
fetchobject.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// https://github.com/basho/riak-java-client/blob/master/src/main/java/com/basho/riak/client/operations/FetchObject.java
public T execute() throws UnresolvedConflictException, RiakRetryFailedException, ConversionException {
// fetch, resolve
Callable<RiakResponse> command = new Callable<RiakResponse>() { // 1
public RiakResponse call() throws Exception {
return client.fetch(bucket, key, builder.build()); // setup call via underlying client
}
};
rawResponse = retrier.attempt(command); // 2
final Collection<T> siblings = new ArrayList<T>(rawResponse.numberOfValues()); // 3
 
for (IRiakObject o : rawResponse) { // 4
siblings.add(converter.toDomain(o));
}
return resolver.resolve(siblings); // 5
}
firstio1.scala
Scala
1 2 3
// remember to import scalaz.effects._
scala> val hiWorld = println("hello, world").pure[IO]
hiWorld: scalaz.effects.IO[Unit] = scalaz.effects.IO$$anon$2@77925ae
firstio2.scala
Scala
1 2
scala> hiWorld.unsafePerformIO
hello, world
riakandio.scala
Scala
1 2 3 4 5 6 7 8
sealed trait IO[A] {
def unsafePerformIO: A = ...
}
 
trait RiakOperation[T] {
@throws(classOf[RiakException])
def execute: T
}
riakop.java
Java
1 2 3 4
//https://github.com/basho/riak-java-client/blob/master/src/main/java/com/basho/riak/client/operations/RiakOperation.java
public interface RiakOperation<T> {
T execute() throws RiakException;
}
store.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def store[T](obj: T): IO[Validation[Throwable, Option[T]]] = {
val emptyStoreMeta = new StoreMeta.Builder().build() // not important how this is really built
val key = // how we get the key from the object is also just an implementation detail
(for {
resp <- rawFetch(key)
fetchRes <- riakResponseToResult(resp).pure[IO] // fetchRes is a Validation[Throwable, Option[T]]
} yield {
fetchRes flatMap {
mbFetched => {
val objToStore = // removing some implementation details here
riakResponseToResult(rawClient.store(objToStore, emptyStoreMeta)) // 9
}
}
}) except { t => t.fail.pure[IO] }
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.