Saturday, December 31, 2011

Scala closures vs Guava collection functions

Let me show you the same method using list transformations (aka "map" in functional programming speak), one in Java programming language:

    @GET @Path("user/{userId}/likes") @Produces(MediaType.APPLICATION_JSON)
    public Payload<Iterable<Map<String, Object>>> getUserLikes(@PathParam("userId") long userId) {
        User user = neo4j.findOne(userId, User.class);
        Iterable<Interest> likeInterests = user.getLikeInterests();
        Iterable<Map<String, Object>> likes = Iterables.transform(likeInterests, new Function<Interest, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Interest interest) {
                HashMap<String, Object> row = new HashMap<String, Object>();
                row.put("id", interest.getNodeId());
                row.put("name", interest.getName());
                return  row;
            }
        });
        return new Payload<Iterable<Map<String, Object>>>(likes);
    }


And one in Scala programming language :

    @GET @Path("user/{userId}/likes") @Produces(Array(MediaType.APPLICATION_JSON))
    def getUserLikes(@PathParam("userId") userId: Long): Payload[Iterable[Map[String, Object]]] = {
        val user = neo4j.findOne(userId, classOf[User])
        val likeInterests = user.getLikeInterests
        val likes = likeInterests.map(interest =>
          Map("id"->interest.getNodeId, "name"->interest.getName) )
        new Payload(likes)
    }

Is Scala hard to read? I'll leave it to you to decide. :-)

To learn more about Scala programming, I recommend Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition.

Coding JSON REST JAX-RS Service Application to Access Neo4j Database in Scala

Spring Data Neo4j makes it easy to accessing graph data in Neo4j graph database.

After separating the AspectJ-enhanced Spring Data Neo4j node entities, relationship entities, and repositories, it's very fun to access the entities from a Scala web application. In this case, I'll show you a JAX-RS application written in Scala programming language, consuming and producing JSON in REST-style. The application exposes Spring Data Neo4j through HTTP, which can be accessed via curl or any other web client.

Java programming language version:

@Path("node") @Stateless
public class NodeResource {

    private transient Logger logger = LoggerFactory.getLogger(NodeResource.class);
    @Inject Neo4jTemplate neo4j;
    @Inject InterestRepository interestRepo;
   
    @GET @Path("interest") @Produces(MediaType.APPLICATION_JSON)
    public Iterable<Interest> interest() {
        ClosableIterable<Interest> records = neo4j.findAll(Interest.class);
        return records;
    }

    @GET @Path("user") @Produces(MediaType.APPLICATION_JSON)
    public Payload<User> getUser() {
        return new Payload<User>(neo4j.findOne(5L, User.class));
    }

    @POST @Path("interest")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createInterest(Payload<Interest> payload) {
        logger.debug("createInterest {}", payload.data);
        Interest interest = payload.data;
        interest.persist();
        return Response.created(URI.create(String.format("interest/%d", interest.getNodeId())))
                .entity(new Payload<Interest>(interest)).build();
    }

    @DELETE @Path("interest/{id}")
    public Response deleteInterest(@PathParam("id") long id) {
        try {
            Interest node = neo4j.findOne(id,  Interest.class);
            node.remove();
            return Response.ok("deleted").build();
        } catch (DataRetrievalFailureException e) {
            return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build();
        }
    }

    @GET @Path("interest/by/facebookId/{facebookId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Payload<Interest> findInterestByFacebookId(@PathParam("facebookId") long facebookId) {
        logger.debug("findInterestByFacebookId {}", facebookId);
        Interest interest = interestRepo.findByFacebookId(facebookId);
        if (interest == null)
            throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity("Interest with facebookId="+ facebookId +" not found").build());
        return new Payload<Interest>(interest);
    }

}


Scala programming language version:

@Path("node") @Stateless
class NodeResource {

    private lazy val logger = LoggerFactory.getLogger(classOf[NodeResource])
    @Inject var neo4j: Neo4jTemplate = _
    @Inject var interestRepo: InterestRepository = _
    
    @GET @Path("interest") @Produces(Array(MediaType.APPLICATION_JSON))
    def interest: Iterable[Interest] = neo4j.findAll(classOf[Interest])

    @GET @Path("user") @Produces(Array(MediaType.APPLICATION_JSON))
    def getUser: Payload[User] = new Payload[User](neo4j.findOne(5L, classOf[User]))

    @POST @Path("interest")
    @Consumes(Array(MediaType.APPLICATION_JSON))
    @Produces(Array(MediaType.APPLICATION_JSON))
    def createInterest(payload: Payload[Interest]): Response = {
        logger.debug("createInterest {}", payload.data)
        val interest = payload.data
        interest.persist
        Response.created(URI.create(String.format("interest/%d", interest.getNodeId)))
                .entity(new Payload[Interest](interest)).build
    }

    @DELETE @Path("interest/{id}")
    def deleteInterest(@PathParam("id") id: Long): Response = {
        try {
            val node = neo4j.findOne(id, classOf[Interest])
            node.remove
            Response.ok("deleted").build
        } catch {
          case e: DataRetrievalFailureException =>
            Response.status(Status.NOT_FOUND).entity(e.getMessage).build
        }
    }

    @GET @Path("interest/by/facebookId/{facebookId}")
    @Produces(Array(MediaType.APPLICATION_JSON))
    def findInterestByFacebookId(@PathParam("facebookId") facebookId: Long): Payload[Interest] = {
        logger.debug("findInterestByFacebookId {}", facebookId)
        val interest = interestRepo.findByFacebookId(facebookId)
        if (interest == null)
            throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity("Interest with facebookId="+ facebookId +" not found").build
        new Payload[Interest](interest)
    }

}


Apart from less code and cruft, Scala code is not much different in structure.

If there are list processing functions or closures, then Scala code will read much easier, while the Java code will use Guava library and clunky syntax (at least until Java 8 arrives).

To learn more about Scala programming, I recommend Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition.

Graph Analysis with Scala and Spring Data Neo4j

Neo4j graph database is the an awesome technology for graph data persistence. With Spring Data Neo4j accessing graph data becomes even easier.

The easiest way to use Spring Data Neo4j is by enabling its AspectJ weaving. Because I wanted to use both Scala and Spring Data Neo4j in my web application, using both languages (AspectJ and Scala) in the same project isn't possible for now. A tip for you, separate the AspectJ-enhanced classes (@NodeEntity, @RelationshipEntity, and repositories) into a separate AspectJ project, then depend on the AspectJ from the Scala application.

The result is I'm able to fully utilize the versatile Scala and the excellent Spring Data libraries with Neo4j.

Now for mandatory code comparison between Java and Scala :-)

Java programming language version:

package com.satukancinta.web;

import java.util.List;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.neo4j.conversion.Result;
import org.springframework.data.neo4j.support.Neo4jTemplate;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.satukancinta.domain.User;

/**
 * @author ceefour
 * Various analysis of the social graph.
 */
@Named @ApplicationScoped
public class GraphAnalysis {
   
    private transient Logger logger = LoggerFactory.getLogger(GraphAnalysis.class);
    @Inject Neo4jTemplate neo4j;
   
    public List<MutualLikeRow> getMutualLikesLookup(User user) {
        logger.debug("getMutualLikesLookup {}/{}", user.getNodeId(), user);
        Result<Map<String, Object>> rows = neo4j.query("START x=node("+ user.getNodeId() +") MATCH x-[:LIKE]->i<-[:LIKE]-y RETURN id(y) AS id, y.name AS name, COUNT(*) AS mutualLikeCount", null);
        Iterable<MutualLikeRow> result = Iterables.transform(rows, new Function<Map<String, Object>, MutualLikeRow>() {
            @Override
            public MutualLikeRow apply(Map<String, Object> arg) {
                return new MutualLikeRow((Long)arg.get("id"), (Integer)arg.get("mutualLikeCount"),
                        (String)arg.get("name"));
            }
        });
        return Lists.newArrayList(result);
    }

}

Scala programming language version:

package com.satukancinta.web
import collection.JavaConversions._
import org.slf4j._
import javax.inject.Inject
import org.springframework.data.neo4j.support.Neo4jTemplate
import com.satukancinta.domain.User
import javax.enterprise.context.ApplicationScoped
import javax.inject.Named

/**
 * @author ceefour
 * Analysis functions of friend network graph.
 */
@Named @ApplicationScoped
class GraphAnalysis {

  private lazy val logger = LoggerFactory.getLogger(classOf[GraphAnalysis])
  @Inject private var neo4j: Neo4jTemplate = _
 
  def getMutualLikesLookup(user: User): java.util.List[MutualLikeRow] = {
    logger.debug("getMutualLikesLookup {}/{}", user.getNodeId, user)
    val rows = neo4j.query(
        "START x=node("+ user.getNodeId() +") MATCH x-[:LIKE]->i<-[:LIKE]-y RETURN id(y) AS id, y.name AS name, COUNT(*) AS mutualLikeCount", null)
    val result = rows.map( r =>
          new MutualLikeRow(r("id").asInstanceOf[Long],
            r("mutualLikeCount").asInstanceOf[Integer].longValue,
            r("name").asInstanceOf[String]) )
        .toList
    result.sortBy(-_.mutualLikeCount)
  }
 
}


As you can see, the Scala version is not only much more concise, easier to understand, but actually has added functionality (sorted using .sortBy) with less code. Thanks to collection functions and closure support.

To learn more about Scala programming, I recommend Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition.