Skip to content

Instantly share code, notes, and snippets.

@tucnak
Last active February 12, 2019 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tucnak/8a89886a920643a2f5d11a89a8807b68 to your computer and use it in GitHub Desktop.
Save tucnak/8a89886a920643a2f5d11a89a8807b68 to your computer and use it in GitHub Desktop.
Schema takes too long to load trivial objects with depth=0 and doesn't load certain structs
2019/02/12 05:04:03 n = 10
2019/02/12 05:04:03
2019/02/12 05:04:03 [find n critiques] started
2019/02/12 05:04:04 found 10 crits
2019/02/12 05:04:04 elapsed: 11.835827ms
2019/02/12 05:04:04
2019/02/12 05:04:04 [get n actors + depth=0] started
2019/02/12 05:04:04 found 10 actors
listening...
- 1 2019-02-09 18:27:29.214357 +0200 EET
- 2 2019-02-09 18:27:29.214357 +0200 EET
- 3 2019-02-09 18:27:29.214357 +0200 EET
- 4 2019-02-09 18:27:29.214357 +0200 EET
- 5 2019-02-09 18:27:29.214357 +0200 EET
- 6 2019-02-09 18:27:29.214357 +0200 EET
- 7 2019-02-09 18:27:29.214357 +0200 EET
- 8 2019-02-09 18:27:29.214357 +0200 EET
- 9 2019-02-09 18:27:29.214357 +0200 EET
- 10 2019-02-09 18:27:29.214357 +0200 EET
no more
2019/02/12 05:04:04 elapsed: 102.027236ms
2019/02/12 05:04:04
2019/02/12 05:04:04 [get n knols + depth=0] started
2019/02/12 05:04:04 found 8 knols
listening...
2019/02/12 05:04:04 not found
no more
2019/02/12 05:04:04 elapsed: 55.915941ms
2019/02/12 05:04:04
2019/02/12 05:04:04 [get n crits + depth=0] started
2019/02/12 05:04:04 found 10 critiques
listening...
no more
2019/02/12 05:04:04 elapsed: 66.925599ms
2019/02/12 05:04:04
package main
import (
"fmt"
"log"
"time"
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph/path"
_ "github.com/cayleygraph/cayley/graph/sql/postgres"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/schema"
"github.com/cayleygraph/cayley/voc/rdf"
)
const (
dbname = "user=veritas dbname=veritas sslmode=disable"
n int64 = 10
)
type Actor struct {
rdfType struct{} `quad:"@type > tas:Actor"`
ID quad.IRI `quad:"@id"`
Competence float64 `quad:"tas:competence,opt"`
Handle string `quad:"tas:handle,opt"`
FirstName string `quad:"tas:first_name,opt"`
LastName string `quad:"tas:last_name,opt"`
CreatedAt time.Time `quad:"tas:created_at"`
}
type Knol struct {
rdfType struct{} `quad:"@type > tas:Knol"`
ID quad.IRI `quad:"@id"`
Confidence float64 `quad:"tas:confidence,opt"`
Title string `quad:"tas:title"`
Author *Actor `quad:"tas:author"`
Text *Text `quad:"tas:text"`
CreatedAt time.Time `quad:"tas:created_at"`
}
type Text struct {
rdfType struct{} `quad:"@type > tas:Text"`
ID quad.IRI `quad:"@id"`
Confidence float64 `quad:"tas:confidence,opt"`
Plain string `quad:"tas:plaintext"`
Author *Actor `quad:"tas:author"`
Critique []Critique `quad:"tas:critique,opt"`
}
type Critique struct {
rdfType struct{} `quad:"@type > tas:Critique"`
ID quad.IRI `quad:"@id"`
Confidence float64 `quad:"tas:confidence,opt"`
Abstract *Text `quad:"tas:text,opt"`
Source *Text `quad:"tas:source_text"`
Author *Actor `quad:"tas:author"`
Remarks []Remark `quad:"tas:remarks,req"`
CreatedAt time.Time `quad:"tas:created_at"`
Score int `quad:"reddit:score,opt"`
}
type Remark struct {
rdfType struct{} `quad:"@type > tas:Remark"`
ID quad.IRI `quad:"@id"`
Type int `quad:"tas:type"`
Confidence float64 `quad:"tas:confidence"`
Sentiment float64 `quad:"tas:sentiment"`
Text *Text `quad:"tas:text,optional"`
}
func elapsed(label string, fn func()) {
log.Printf("[%s] started\n", label)
started := time.Now()
fn()
log.Printf("elapsed: %v\n", time.Since(started))
log.Println()
}
func main() {
g, err := cayley.NewGraph("postgres", dbname, nil)
if err != nil {
log.Fatalln(err)
}
sch := schema.NewConfig()
log.Println("n =", n)
log.Println()
elapsed("find n critiques", func() {
ids, _ := path.StartPath(g, quad.IRI("tas:Critique")).
In(quad.IRI(rdf.Type)).
Limit(n).
Iterate(nil).
AllValues(g)
log.Println("found", len(ids), "crits")
})
elapsed("get n actors + depth=0", func() {
ids, _ := path.StartPath(g, quad.IRI("tas:Actor")).
In(quad.IRI(rdf.Type)).
Limit(n).
Iterate(nil).
AllValues(g)
log.Println("found", len(ids), "actors")
actors, done := make(chan Actor), make(chan struct{})
go func() {
i := 0
fmt.Println("listening...")
for x := range actors {
i++
fmt.Println("-", i, x.CreatedAt)
}
fmt.Println("no more")
done <- struct{}{}
}()
if err := sch.LoadToDepth(nil, g, actors, 0, ids...); err != nil {
log.Println(err)
return
}
<-done
})
elapsed("get n knols + depth=0", func() {
ids, _ := path.StartPath(g, quad.IRI("tas:Knol")).
In(quad.IRI(rdf.Type)).
Limit(n).
Iterate(nil).
AllValues(g)
log.Println("found", len(ids), "knols")
knols, done := make(chan Knol), make(chan struct{})
go func() {
i := 0
fmt.Println("listening...")
for x := range knols {
i++
fmt.Println("-", i, x.CreatedAt)
}
fmt.Println("no more")
done <- struct{}{}
}()
if err := sch.LoadToDepth(nil, g, knols, 0, ids...); err != nil {
log.Println(err)
return
}
<-done
})
elapsed("get n crits + depth=0", func() {
ids, err := path.StartPath(g, quad.IRI("tas:Critique")).
In(quad.IRI(rdf.Type)).
Limit(n).
Iterate(nil).
AllValues(g)
if err != nil {
log.Println(err)
return
}
log.Println("found", len(ids), "critiques")
crits, done := make(chan Critique), make(chan struct{})
go func() {
i := 0
fmt.Println("listening...")
for x := range crits {
i++
fmt.Println("-", i, x.CreatedAt)
}
fmt.Println("no more")
done <- struct{}{}
}()
if err := sch.LoadToDepth(nil, g, crits, 0, ids...); err != nil {
log.Println(err)
return
}
<-done
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment