Added functionality so quadstore is generated per request (if needed) for the new appengine backend \n CR : nobody \n Tests run: unit tests

This commit is contained in:
= 2014-11-27 15:32:46 +01:00 committed by panamafrancis
parent 2c74cb1657
commit 35ccfe7677
8 changed files with 130 additions and 51 deletions

View file

@ -26,6 +26,7 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/google/cayley/config"
"github.com/google/cayley/db"
"github.com/google/cayley/graph"
)
@ -55,6 +56,10 @@ func findAssetsPath() string {
return "."
}
if hasAssets("..") {
return ".."
}
gopathPath := os.ExpandEnv("$GOPATH/src/github.com/google/cayley")
if hasAssets(gopathPath) {
return gopathPath
@ -105,6 +110,25 @@ type API struct {
handle *graph.Handle
}
func (api *API) GetHandleForRequest(r *http.Request) (*graph.Handle, error) {
if !api.config.RequiresHTTPRequestContext {
return api.handle, nil
}
opts := make(graph.Options)
opts["HTTPRequest"] = r
qs, err := graph.NewQuadStoreForRequest(api.handle.QuadStore, opts)
if err != nil {
return nil, err
}
qw, err := db.OpenQuadWriter(qs, api.config)
if err != nil {
return nil, err
}
return &graph.Handle{QuadStore: qs, QuadWriter: qw}, nil
}
func (api *API) APIv1(r *httprouter.Router) {
r.POST("/api/v1/query/:query_lang", LogRequest(api.ServeV1Query))
r.POST("/api/v1/shape/:query_lang", LogRequest(api.ServeV1Shape))

View file

@ -55,7 +55,12 @@ func (api *API) ServeV1Write(w http.ResponseWriter, r *http.Request, _ httproute
if err != nil {
return jsonResponse(w, 400, err)
}
api.handle.QuadWriter.AddQuadSet(quads)
h, err := api.GetHandleForRequest(r)
if err != nil {
return jsonResponse(w, 400, err)
}
h.QuadWriter.AddQuadSet(quads)
fmt.Fprintf(w, "{\"result\": \"Successfully wrote %d quads.\"}", len(quads))
return 200
}
@ -81,9 +86,13 @@ func (api *API) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params
// TODO(kortschak) Make this configurable from the web UI.
dec := cquads.NewDecoder(formFile)
var (
n int
h, err := api.GetHandleForRequest(r)
if err != nil {
return jsonResponse(w, 400, err)
}
var (
n int
block = make([]quad.Quad, 0, blockSize)
)
for {
@ -97,11 +106,11 @@ func (api *API) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params
block = append(block, t)
n++
if len(block) == cap(block) {
api.handle.QuadWriter.AddQuadSet(block)
h.QuadWriter.AddQuadSet(block)
block = block[:0]
}
}
api.handle.QuadWriter.AddQuadSet(block)
h.QuadWriter.AddQuadSet(block)
fmt.Fprintf(w, "{\"result\": \"Successfully wrote %d quads.\"}", n)
@ -120,9 +129,13 @@ func (api *API) ServeV1Delete(w http.ResponseWriter, r *http.Request, params htt
if err != nil {
return jsonResponse(w, 400, err)
}
h, err := api.GetHandleForRequest(r)
if err != nil {
return jsonResponse(w, 400, err)
}
count := 0
for _, q := range quads {
api.handle.QuadWriter.RemoveQuad(q)
h.QuadWriter.RemoveQuad(q)
count++
}
fmt.Fprintf(w, "{\"result\": \"Successfully deleted %d quads.\"}", count)