コード例 #1
0
ファイル: graphkeyvalue_test.go プロジェクト: hanslovsky/dvid
func TestBasicGraph(t *testing.T) {
	tests.UseStore()
	defer tests.CloseStore()

	graphDB, err := storage.GraphStore()
	if err != nil {
		t.Fatalf("Can't open graph store: %v\n", err)
	}

	ctx := storage.GetTestDataContext(storage.TestUUID1, "graph", dvid.InstanceID(13))

	if err = graphDB.CreateGraph(ctx); err != nil {
		t.Errorf("Can't create graph: %v\n", err)
	}

	if err = graphDB.AddVertex(ctx, 1, 5); err != nil {
		t.Errorf("Can't add vertex: %v\n", err)
	}

	if err = graphDB.AddVertex(ctx, 2, 11); err != nil {
		t.Errorf("Can't add vertex: %v\n", err)
	}

	if err = graphDB.AddEdge(ctx, 1, 2, 0.3); err != nil {
		t.Errorf("Can't add edge: %v\n", err)
	}

	vert1, err := graphDB.GetVertex(ctx, 1)
	if err != nil {
		t.Errorf("Can't get vertex: %v\n", err)
	}
	if vert1.Weight != float64(5) {
		t.Errorf("Bad weight.  Should be %f, was %f\n", 5, vert1.Weight)
	}

	vert2, err := graphDB.GetVertex(ctx, 2)
	if err != nil {
		t.Errorf("Can't get vertex: %v\n", err)
	}
	if vert2.Weight != float64(11) {
		t.Errorf("Bad weight.  Should be %f, was %f\n", 11, vert1.Weight)
	}

	edge, err := graphDB.GetEdge(ctx, 1, 2)
	if err != nil {
		t.Errorf("Can't get edge: %v\n", err)
	}
	if edge.Weight != float64(0.3) {
		t.Errorf("Bad edge.  Should be %f, was %f\n", 0.3, edge.Weight)
	}

	edge, err = graphDB.GetEdge(ctx, 2, 1)
	if err != nil {
		t.Errorf("Can't get edge: %v\n", err)
	}
	if edge.Weight != float64(0.3) {
		t.Errorf("Bad edge.  Should be %f, was %f\n", 0.3, edge.Weight)
	}

	if err = graphDB.RemoveGraph(ctx); err != nil {
		t.Errorf("Error removing graph: %v\n", err)
	}
}
コード例 #2
0
ファイル: labelgraph.go プロジェクト: hanslovsky/dvid
// ServeHTTP handles all incoming HTTP requests for this data.
func (d *Data) ServeHTTP(uuid dvid.UUID, ctx *datastore.VersionedCtx, w http.ResponseWriter, r *http.Request) {
	// --- Don't time labelgraph ops because they are very small and frequent.
	// --- TODO -- Implement monitoring system that aggregates logged ops instead of
	// ----------- printing out each one.
	// timedLog := dvid.NewTimeLog()

	db, err := storage.GraphStore()
	if err != nil {
		server.BadRequest(w, r, err)
		return
	}

	// make sure transaction log is created
	d.initializeLog()

	// Break URL request into arguments
	url := r.URL.Path[len(server.WebAPIPath):]
	parts := strings.Split(url, "/")
	if len(parts) < 4 {
		server.BadRequest(w, r, "No resource specified in URI")
		return
	}
	method := strings.ToLower(r.Method)
	if method == "put" {
		server.BadRequest(w, r, "PUT requests not supported")
		return
	}

	// Process help and info.
	switch parts[3] {
	case "help":
		w.Header().Set("Content-Type", "text/plain")
		fmt.Fprintln(w, d.Help())
		return
	case "info":
		jsonBytes, err := d.MarshalJSON()
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintf(w, string(jsonBytes))
	case "subgraph":
		// disable json schema validation (will speedup POST command)
		queryValues := r.URL.Query()
		disableSchemaT := dvid.InstanceName(queryValues.Get("unsafe"))
		disableSchema := false
		if len(disableSchemaT) != 0 && disableSchemaT == "true" {
			disableSchema = true
		}
		labelgraph, err := d.ExtractGraph(r, disableSchema)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
		err = d.handleSubgraphBulk(ctx, db, w, labelgraph, method)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
	case "neighbors":
		if method != "get" {
			server.BadRequest(w, r, "Only supports GETs")
			return
		}
		err := d.handleNeighbors(ctx, db, w, parts[4:])
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
	case "merge":
		if method != "post" {
			server.BadRequest(w, r, "Only supports POSTs")
			return
		}
		labelgraph, err := d.ExtractGraph(r, false)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
		err = d.handleMerge(ctx, db, w, labelgraph)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
	case "weight":
		if method != "post" {
			server.BadRequest(w, r, "Only supports POSTs")
			return
		}
		labelgraph, err := d.ExtractGraph(r, false)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
		err = d.handleWeightUpdate(ctx, db, w, labelgraph)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
	case "propertytransaction":
		err := d.handlePropertyTransaction(ctx, db, w, r, parts[4:], method)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
	case "property":
		err := d.handleProperty(ctx, db, w, r, parts[4:], method)
		if err != nil {
			server.BadRequest(w, r, err)
			return
		}
	case "undomerge":
		// not supported until transaction history is supported
		server.BadRequest(w, r, "undomerge not yet implemented")
		return
	default:
		server.BadRequest(w, r, "%s not found", parts[3])
		return
	}
	//timedLog.Infof("Successful labelgraph op %s on %q", parts[3], d.DataName())
}