예제 #1
0
// handleMerge merges a list of vertices onto the final vertex in the Vertices list
func (d *Data) handleMerge(ctx *datastore.VersionedCtx, db storage.GraphDB, w http.ResponseWriter, labelgraph *LabelGraph) error {

	numverts := len(labelgraph.Vertices)
	if numverts < 2 {
		return fmt.Errorf("Must specify at least two vertices for merging")
	}

	overlapweights := make(map[dvid.VertexID]float64)
	vertweight := float64(0)
	var keepvertex dvid.GraphVertex
	allverts := make(map[dvid.VertexID]struct{})
	keepverts := make(map[dvid.VertexID]struct{})

	// accumulate weights, find common edges
	for i, vertex := range labelgraph.Vertices {
		vert, err := db.GetVertex(ctx, vertex.Id)
		if err != nil {
			return fmt.Errorf("Failed to retrieve vertex %d: %v\n", vertex.Id, err)
		}
		allverts[vert.Id] = struct{}{}
		vertweight += vert.Weight

		if i == (numverts - 1) {
			keepvertex = vert
		} else {
			for _, vert2 := range vert.Vertices {
				edge, err := db.GetEdge(ctx, vert.Id, vert2)
				if err != nil {
					return fmt.Errorf("Failed to retrieve edge %d-%d: %v\n", vertex.Id, vert2, err)
				}
				overlapweights[vert2] += edge.Weight
			}
		}
	}

	// examine keep vertex (save edges so only the weights are updated)
	for _, vert2 := range keepvertex.Vertices {
		edge, err := db.GetEdge(ctx, keepvertex.Id, vert2)
		if err != nil {
			return fmt.Errorf("Failed to retrieve edge %d-%d: %v\n", keepvertex.Id, vert2, err)
		}
		overlapweights[vert2] += edge.Weight
		keepverts[vert2] = struct{}{}
	}

	// use specified weights even if marked as 0
	for _, edge := range labelgraph.Edges {
		id := edge.Id1
		baseid := edge.Id2
		if keepvertex.Id == edge.Id1 {
			id = edge.Id2
			baseid = edge.Id1
		}
		if baseid == keepvertex.Id {
			if _, ok := overlapweights[id]; ok {
				overlapweights[id] = edge.Weight
			}
		}
	}

	// if user specifies an edge weight other than 0 (?! -- somehow allow in future)
	// use that weight
	if labelgraph.Vertices[numverts-1].Weight != 0 {
		vertweight = labelgraph.Vertices[numverts-1].Weight
	}

	for id2, newweight := range overlapweights {
		if _, ok := allverts[id2]; !ok {
			// only examine edges where the node is not internal
			if _, ok := keepverts[id2]; !ok {
				// if not in keepverts create new edge
				err := db.AddEdge(ctx, keepvertex.Id, id2, newweight)
				if err != nil {
					return fmt.Errorf("Failed to create edge %d-%d: %v\n", keepvertex.Id, id2, err)
				}
			} else {
				// else just update weight
				err := db.SetEdgeWeight(ctx, keepvertex.Id, id2, newweight)
				if err != nil {
					return fmt.Errorf("Failed to update weight on edge %d-%d: %v\n", keepvertex.Id, id2, err)
				}
			}
		}
	}

	// update vertex weight
	err := db.SetVertexWeight(ctx, labelgraph.Vertices[numverts-1].Id, vertweight)
	if err != nil {
		return fmt.Errorf("Failed to update weight on vertex %d: %v\n", keepvertex.Id, err)
	}

	// remove old vertices which will remove the old edges
	for i, vertex := range labelgraph.Vertices {
		if i == (numverts - 1) {
			break
		}
		err := db.RemoveVertex(ctx, vertex.Id)
		if err != nil {
			return fmt.Errorf("Failed to remove vertex %d: %v\n", vertex.Id, err)
		}
	}
	return nil
}
예제 #2
0
// handleSubgraph loads, retrieves, or deletes a subgraph (more description in REST interface)
func (d *Data) handleSubgraphBulk(ctx *datastore.VersionedCtx, db storage.GraphDB, w http.ResponseWriter, labelgraph *LabelGraph, method string) error {
	var err error
	if !d.setBusy() {
		return fmt.Errorf("Server busy with bulk transaction")
	}
	defer d.setNotBusy()

	// initial new graph
	labelgraph2 := new(LabelGraph)
	labelgraph2.Transactions = make([]transactionItem, 0)
	labelgraph2.Vertices = make([]labelVertex, 0)
	labelgraph2.Edges = make([]labelEdge, 0)

	// ?! do not grab edges that connect to outside vertices
	if method == "get" {
		var vertices []dvid.GraphVertex
		var edges []dvid.GraphEdge
		if len(labelgraph.Vertices) > 0 {
			used_vertices := make(map[dvid.VertexID]struct{})
			for _, vertex := range labelgraph.Vertices {
				used_vertices[vertex.Id] = struct{}{}
				storedvert, err := db.GetVertex(ctx, vertex.Id)
				if err != nil {
					return fmt.Errorf("Failed to retrieve vertix %d: %v\n", vertex.Id, err)
				}
				vertices = append(vertices, storedvert)
				for _, vert2 := range storedvert.Vertices {
					if _, ok := used_vertices[vert2]; !ok {
						edge, err := db.GetEdge(ctx, storedvert.Id, vert2)
						if err != nil {
							return fmt.Errorf("Failed to retrieve edge %d-%d: %v\n", storedvert.Id, vert2, err)
						}
						edges = append(edges, edge)
					}
				}
			}
		} else {
			// if no set of vertices are supplied, just grab the whole graph
			vertices, err = db.GetVertices(ctx)
			if err != nil {
				return fmt.Errorf("Failed to retrieve vertices: %v\n", err)
			}
			edges, err = db.GetEdges(ctx)
			if err != nil {
				return fmt.Errorf("Failed to retrieve edges: %v\n", err)
			}
		}
		for _, vertex := range vertices {
			labelgraph2.Vertices = append(labelgraph2.Vertices, labelVertex{vertex.Id, vertex.Weight})
		}
		for _, edge := range edges {
			labelgraph2.Edges = append(labelgraph2.Edges, labelEdge{edge.Vertexpair.Vertex1, edge.Vertexpair.Vertex2, edge.Weight})
		}
		m, err := json.Marshal(labelgraph2)
		if err != nil {
			return fmt.Errorf("Could not serialize graph")
		}
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintf(w, string(m))
	} else if method == "post" {
		// add list of vertices and edges from supplied JSON -- overwrite existing values
		for _, vertex := range labelgraph.Vertices {
			err := db.AddVertex(ctx, vertex.Id, vertex.Weight)
			if err != nil {
				return fmt.Errorf("Failed to add vertex: %v\n", err)
			}
		}
		for _, edge := range labelgraph.Edges {
			err := db.AddEdge(ctx, edge.Id1, edge.Id2, edge.Weight)
			if err != nil {
				return fmt.Errorf("Failed to add edge: %v\n", err)
			}
		}
	} else if method == "delete" {
		// delete the vertices supplied and all of their edges or delete the whole graph
		if len(labelgraph.Vertices) > 0 || len(labelgraph.Edges) > 0 {
			for _, vertex := range labelgraph.Vertices {
				db.RemoveVertex(ctx, vertex.Id)
			}
			for _, edge := range labelgraph.Edges {
				db.RemoveEdge(ctx, edge.Id1, edge.Id2)
			}
		} else {
			err = db.RemoveGraph(ctx)
			if err != nil {
				return fmt.Errorf("Failed to remove graph: %v\n", err)
			}
		}
	} else {
		err = fmt.Errorf("Does not support PUT")
	}

	return err
}