Eelco,

there is at least one issue that I can see -> different type + dir might have 
the same hashcode overriding each other.

I would use "Rel" as a real class.

- so implement hashcode and equals in it correctly
- remove the getters from Rel, add a toJson() method
- don't use String name in the Rel class
- then you can use a Set<Rel> instead of the map and check for 
rels.contains(new Rel(type,dir)

Alternative for the return serialization

for the serialization you could probably return a ListRepresentation from your 
method, subclasses of Representation are also allowed.
this list representation would contain a list of ObjectRepresentations for Rel, 
you can look at an example at NameDescriptionValueRepresentation.
Probably you can also have Rel extend from ObjectRepresentation, then you have 
to keep the getters :( but if you annotate them with @Mapping("key-name") you 
get your
representation generated.

Cheers

Michael


Am 13.07.2011 um 21:06 schrieb Eelco Hillenius:

> Hi,
> 
>> The unmanaged extensions simply give you access to the underlying Graph 
>> Database instance (through the annotations).
>> 
>> The managed plugins do far more than this - they also provide a way of 
>> generating JSON/HTML responses automatically (using HTTP content 
>> negotiation).
>> 
>> In either case you can host arbitrary code.
>> 
>> From your mail, it seems that JSON/HTML would be fine for you, e.g.:
>> 
>> [
>> {
>> "node_id" : 28,
>> "Incoming_friend_rels" : 156
>> },
>> "node_id" : 29,
>> "Incoming_friend_rels" : 32
>> }
>> ]
> 
> Right. I implemented this as an unmanaged extension. Now what I'm
> wondering is if I can implement the same as a server plugin.
> 
> Also, somewhat related, what would be the best way to develop a server
> plugin that is a daemon? I need to write some database synchronization
> code that would run in the background, and for the sake of efficiency,
> that would best run directly in the server.
> 
> FWIW, here's my code as it is now (and if anything doesn't make sense
> in that code, please share :-) )
> 
> @Path("/rels")
> public class RelationshipsExtApi {
> 
>    static class Rel {
>        String name; // name of the relationship
>        int count; // number of nodes the relationship is connected to
>        Direction direction; // direction of the relationship (as seen
> from the originating node)
> 
>        public Rel(String name, Direction direction) {
>            this.name = name;
>            this.direction = direction;
>        }
> 
>        public String getName() {
>            return name;
>        }
> 
>        public void setName(String name) {
>            this.name = name;
>        }
> 
>        public int getCount() {
>            return count;
>        }
> 
>        public void setCount(int count) {
>            this.count = count;
>        }
> 
>        public Direction getDirection() {
>            return direction;
>        }
> 
>        public void setDirection(Direction direction) {
>            this.direction = direction;
>        }
>    }
> 
>    private final GraphDatabaseService graphDb;
> 
>    public RelationshipsExtApi(@Context GraphDatabaseService graphDb) {
>        this.graphDb = graphDb;
>    }
> 
>    @GET
>    @Consumes(MediaType.TEXT_PLAIN)
>    @Produces(MediaType.APPLICATION_JSON)
>    @Path("{id}")
>    public String getRelationships(@PathParam("id") long id) {
>        Node node = graphDb.getNodeById(id);
>        Map<Integer, Rel> rels = new HashMap<Integer, Rel>();
>        for (Relationship r : node.getRelationships()) {
>            Direction direction = node.equals(r.getStartNode()) ?
> Direction.OUTGOING : Direction.INCOMING;
>            int hash = hash(r.getType(), direction);
>            Rel rel = rels.get(hash);
>            if (rel == null) {
>                rel = new Rel(r.getType().name(), direction);
>                rels.put(hash, rel);
>            }
>            rel.count++;
>        }
> 
>        StringBuilder b = new StringBuilder("[");
>        for (Iterator<Rel> i = rels.values().iterator(); i.hasNext(); ) {
>            Rel rel = i.next();
>            b.append("{")
>                    .append("\"name\": \"").append(rel.getName()).append("\", 
> ")
>                    .append("\"direction\":
> \"").append(rel.getDirection().name()).append("\", ")
>                    .append("\"count\": ").append(rel.getCount()).append(" }");
>            if (i.hasNext()) {
>                b.append(", ");
>            }
>        }
>        b.append("]");
>        return b.toString();
>    }
> 
>    int hash(RelationshipType type, Direction direction) {
>        return 31 * type.hashCode() + direction.hashCode();
>    }
> }
> _______________________________________________
> Neo4j mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user

_______________________________________________
Neo4j mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Reply via email to