Created
March 21, 2016 11:34
-
-
Save batwicket/e21be25a4528eeabbfdc to your computer and use it in GitHub Desktop.
simple cayley mql for duplicate subject detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"github.com/google/cayley" | |
"github.com/google/cayley/graph" | |
"github.com/google/cayley/quad" | |
"github.com/google/cayley/query/mql" | |
"github.com/pborman/uuid" | |
_ "github.com/google/cayley/graph/bolt" | |
) | |
func main() { | |
fmt.Println("Initializing server...") | |
dbPath := "/tmp/graphdb.nq" | |
graph.InitQuadStore("bolt", dbPath, nil) | |
store, err := cayley.NewGraph("bolt", dbPath, nil) | |
if err != nil { | |
fmt.Println("[Error] could not open '", dbPath, "': ", err) | |
return | |
} | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
sess := mql.NewSession(store.QuadStore) | |
c := make(chan interface{}, 5) | |
username := "flynn" | |
query := `[{"id": null, "name": "` + username + `"}]` | |
fmt.Println("query: '", query, "'") | |
sess.Execute(query, c, -1) | |
if len(c) > 0 { | |
fmt.Println("[Error] user '", username, "' already exists") | |
return | |
} | |
fmt.Println("username is unique: ", username) | |
userid := graph.NewUniqueKey("") // we ASSUME it actually does create a NewUniqueKey | |
useridStr := userid.String() | |
if uuid.Parse(useridStr) == nil { | |
fmt.Println("[Error] unable to parse unique userid") | |
return | |
} | |
fmt.Println("userid: ", useridStr) | |
tx := graph.NewTransaction() | |
tx.AddQuad(quad.Quad{useridStr, "name", username, ""}) | |
io := graph.IgnoreOpts{IgnoreDup: false, IgnoreMissing: false} | |
err := store.ApplyDeltas(tx.Deltas, io) | |
if err != nil { | |
fmt.Println("[Error] unable to update database: ", err) | |
return | |
} | |
}) | |
fmt.Println("Server is at http://localhost:8080/") | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment