Permalink
Browse files

Generalized getneighbors function

  • Loading branch information...
1 parent b2c882e commit a730214bb0e70beda29159f882f9f693c52c14fe @smargonz smargonz committed Aug 20, 2012
Showing with 26 additions and 18 deletions.
  1. +1 −1 GraphBackend.go
  2. +19 −7 GraphManager.go
  3. +6 −10 GraphManager_test.go
View
@@ -10,7 +10,7 @@ type GraphBackend interface {
UpdateNodeProp(n Node, prop string, value []byte) error
GetNode(string, NodeConstructor) (Node, error)
Attach(node1 Node, node2 Node, edge Edge)
- GetOutgoingNeighbors(node Node, constE EdgeConstructor, constN NodeConstructor) ([]Connection, error)
+ GetNeighbors(node Node, constE EdgeConstructor, constN NodeConstructor) ([]Connection, error)
AddEdge(e Edge)
DeleteEdge(e Edge)
FindEdge(id string, construct EdgeConstructor) (Edge, error)
View
@@ -163,8 +163,10 @@ func (gm *GraphManager) GetNode(index string, construct NodeConstructor) (Node,
func (gm *GraphManager) Attach(node1 Node, node2 Node, e Edge) {
gm.client.Hset(node_s+node1.GetID()+adj_s, node2.GetID(), []byte(e.GetID()))
+ gm.client.Hset(node_s+node2.GetID()+inadj_s, node1.GetID(), []byte(e.GetID()))
if !e.IsDirected() {
gm.client.Hset(node_s+node2.GetID()+adj_s, node1.GetID(), []byte(e.GetID()))
+ gm.client.Hset(node_s+node1.GetID()+inadj_s, node2.GetID(), []byte(e.GetID()))
}
node1.AddEdge(e)
@@ -174,20 +176,34 @@ func (gm *GraphManager) Attach(node1 Node, node2 Node, e Edge) {
}
-func (gm *GraphManager) GetOutgoingNeighbors(node Node, constE EdgeConstructor, constN NodeConstructor) ([]Connection, error) {
+func (gm *GraphManager) GetNeighbors(node Node, constE EdgeConstructor, constN NodeConstructor) ([]Connection, error) {
//adjArray is []byte of n,e,n,e etc
var err error
adjId := node_s + node.GetID() + adj_s
+ inadjId := node_s + node.GetID() + inadj_s
+
adjArray, err := gm.client.Hgetall(adjId)
+ inadjArray, ierr := gm.client.Hgetall(inadjId)
+
if err != nil {
return nil, err
}
- adjMap := ByteAAtoStringMap(adjArray)
+ if ierr != nil {
+ return nil, ierr
+ }
+
+ neighborArr := append(adjArray, inadjArray...)
+ neighborMap := ByteAAtoStringMap(neighborArr)
+
+ for k, v := range neighborArr {
+ fmt.Printf("%v, %v\n", k, v)
+ }
+
neighbors := make([]Connection, 0)
- for k, v := range adjMap {
+ for k, v := range neighborMap {
nb, err := gm.GetNode(k, constN)
if err != nil {
return nil, err
@@ -197,12 +213,8 @@ func (gm *GraphManager) GetOutgoingNeighbors(node Node, constE EdgeConstructor,
return nil, err
}
- fmt.Printf("%v", node.Equals(ec.GetFirstNode()))
-
- //if node.Equals(ec.GetFirstNode()) {
newConn := Connection{NodeA: node, NodeB: nb, Edg: ec}
neighbors = append(neighbors, newConn)
- //}
}
return neighbors, nil
View
@@ -381,14 +381,14 @@ func TestGetNeighbors(t *testing.T) {
gm.Attach(hgw, msg, edg)
- neighbors, err := gm.GetOutgoingNeighbors(hgw, socialGraph.SocialEdgeConst, socialGraph.SocialNodeConst)
+ neighbors, err := gm.GetNeighbors(hgw, socialGraph.SocialEdgeConst, socialGraph.SocialNodeConst)
if err != nil {
t.Error(err.Error())
}
if len(neighbors) != 1 {
- t.Errorf("GetOutgoingNeighbors Failed - %v neighbors", len(neighbors))
+ t.Errorf("GetNeighbors Failed - %v neighbors", len(neighbors))
fmt.Printf("***************%v ************\n", neighbors)
}
@@ -398,7 +398,7 @@ func TestGetNeighbors(t *testing.T) {
}
}
-func TestMultipleOutgoingNeighbors(t *testing.T) {
+func TestMultipleNeighbors(t *testing.T) {
gm = new(spiderDB.GraphManager)
gm.Initialize()
@@ -425,17 +425,13 @@ func TestMultipleOutgoingNeighbors(t *testing.T) {
gm.Attach(hgw, msg1, edg1)
gm.Attach(msg2, hgw, edg2)
- neighbors, err := gm.GetOutgoingNeighbors(hgw, socialGraph.SocialEdgeConst, socialGraph.SocialNodeConst)
+ neighbors, err := gm.GetNeighbors(hgw, socialGraph.SocialEdgeConst, socialGraph.SocialNodeConst)
if err != nil {
t.Error(err.Error())
}
- for _, con := range neighbors {
- fmt.Printf("%v ----> %v\n", con.NodeA, con.NodeB)
- }
-
- if len(neighbors) != 2 {
- t.Errorf("GetOutgoingNeighbors Failed - %v neighbors", len(neighbors))
+ if len(neighbors) != 3 {
+ t.Errorf("GetNeighbors Failed - %v neighbors", len(neighbors))
}
}

0 comments on commit a730214

Please sign in to comment.