Skip to content

Instantly share code, notes, and snippets.

@Hecatoncheir
Last active June 6, 2017 20:27
Show Gist options
  • Select an option

  • Save Hecatoncheir/d18a97c218c6d8f5c0f14c69049f769a to your computer and use it in GitHub Desktop.

Select an option

Save Hecatoncheir/d18a97c218c6d8f5c0f14c69049f769a to your computer and use it in GitHub Desktop.
cayley
package main
import (
"database/sql"
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/graph/path"
_ "github.com/cayleygraph/cayley/graph/sql"
"github.com/cayleygraph/cayley/quad"
)
func main() {
var err error
var path *path.Path
db, err := sql.Open("postgres", "postgresql://root@MBP-Vitaliy:26257?sslmode=disable")
if err != nil {
log.Fatal(err)
}
db.Query("drop database cayley")
db.Query("create database cayley")
err = graph.InitQuadStore("sql", "postgresql://root@MBP-Vitaliy:26257/cayley?sslmode=disable", graph.Options{"flavor": "cockroach"})
store, err := cayley.NewGraph("sql", "postgresql://root@MBP-Vitaliy:26257/cayley?sslmode=disable", graph.Options{"flavor": "cockroach"})
if err != nil {
fmt.Println(err)
}
defer store.Close()
iphone6sTime := time.Now().String()
transaction := graph.NewTransaction()
transaction.AddQuad(cayley.Quad("iphone 6s", "is", "product", "Product"))
transaction.AddQuad(cayley.Quad("iphone 6s", "price", "1200", "Product"))
transaction.AddQuad(cayley.Quad("1200", "is", "price", "Price"))
transaction.AddQuad(cayley.Quad("1200", "belongs", "iphone 6s", "Price"))
transaction.AddQuad(cayley.Quad("1200", "date", iphone6sTime, "Price"))
transaction.AddQuad(cayley.Quad(iphone6sTime, "is", "date", "Time"))
err = store.ApplyTransaction(transaction)
// path := cayley.StartPath(store, quad.String("iphone 6s")).In("belongs").Out("date")
// path.Iterate(nil).EachValue(nil, func(value quad.Value) {
// native := quad.NativeOf(value)
// fmt.Println(native)
// })
iphone6sNewTime := time.Now().String()
transaction = graph.NewTransaction()
transaction.AddQuad(cayley.Quad("1700", "is", "price", "Price"))
transaction.AddQuad(cayley.Quad("1700", "belongs", "iphone 6s", "Price"))
transaction.AddQuad(cayley.Quad("1700", "date", iphone6sNewTime, "Price"))
transaction.AddQuad(cayley.Quad(iphone6sNewTime, "is", "date", "Time"))
err = store.ApplyTransaction(transaction)
// path = cayley.StartPath(store, quad.String("iphone 6s")).LabelContext("Price").In("belongs")
// path.Iterate(nil).EachValue(nil, func(value quad.Value) {
// fmt.Println(value.String())
// })
// path = cayley.StartPath(store, quad.String("iphone 6s")).In("belongs").Out("date")
// path.Iterate(nil).EachValue(nil, func(value quad.Value) {
// fmt.Println(value.String())
// })
// -----
iphone7Time := time.Now().String()
transaction = graph.NewTransaction()
transaction.AddQuad(cayley.Quad("iphone 7", "is", "product", "Product"))
transaction.AddQuad(cayley.Quad("iphone 7", "price", "100", "Product"))
transaction.AddQuad(cayley.Quad("100", "is", "price", "Price"))
transaction.AddQuad(cayley.Quad("100", "belongs", "iphone 7", "Price"))
transaction.AddQuad(cayley.Quad("100", "date", iphone7Time, "Price"))
transaction.AddQuad(cayley.Quad(iphone7Time, "is", "date", "Time"))
err = store.ApplyTransaction(transaction)
// path = cayley.StartPath(store, quad.String("iphone 7")).In("belongs")
// path.Iterate(nil).EachValue(nil, func(value quad.Value) {
// fmt.Println(value.String())
// })
// path = cayley.StartPath(store, quad.String("iphone 7")).In("belongs").Out("date")
// path.Iterate(nil).EachValue(nil, func(value quad.Value) {
// fmt.Println(value.String())
// })
fmt.Println("----RegExp for 6s----")
phone, err := regexp.Compile(strings.ToLower("6S"))
path = cayley.StartPath(store).RegexWithRefs(phone).In("belongs")
path.Iterate(nil).EachValue(nil, func(value quad.Value) {
fmt.Println(value.String())
})
fmt.Println("----Delete----")
it := iterator.NewAnd(store,
store.QuadIterator(quad.Predicate, store.ValueOf(quad.String("belongs"))),
store.QuadIterator(quad.Object, store.ValueOf(quad.String("iphone 6s"))))
defer it.Close()
for it.Next() {
f := store.Quad(it.Result()).String()
fmt.Println(f)
dd := store.Quad(it.Result()).Subject
if dd.Native() == "1700" {
store.RemoveQuad(store.Quad(it.Result()))
}
}
fmt.Println("----Delete result for 6s----")
phone, err = regexp.Compile(strings.ToLower("6S"))
path = cayley.StartPath(store).RegexWithRefs(phone).In("belongs")
path.Iterate(nil).EachValue(nil, func(value quad.Value) {
fmt.Println(value.String())
})
path = cayley.StartPath(store, quad.String("iphone 6s")).In("belongs").Out("date")
path.Iterate(nil).EachValue(nil, func(value quad.Value) {
fmt.Println(value.String())
})
fmt.Println("-----Another remove----")
path = cayley.StartPath(store, quad.String("iphone 7")).In("belongs")
path.Iterate(nil).EachValue(nil, func(value quad.Value) {
fmt.Println(value.String())
})
it = iterator.NewAnd(store,
store.QuadIterator(quad.Subject, store.ValueOf(quad.String("100"))),
store.QuadIterator(quad.Predicate, store.ValueOf(quad.String("belongs"))))
defer it.Close()
for it.Next() {
f := store.Quad(it.Result()).String()
fmt.Println(f)
store.RemoveQuad(store.Quad(it.Result()))
fmt.Println("removed")
}
path = cayley.StartPath(store, quad.String("iphone 7")).In("belongs")
fmt.Println("search")
path.Iterate(nil).EachValue(nil, func(value quad.Value) {
fmt.Println(value.String())
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment