database: Update Cayley to fix slow deletions

subject, predicate, object and labels are not indexed, thus, using a where clause on these fields for the DELETE statement does a full-table scan. Using *_hash columns instead will use the indexes.
pull/18/head
Quentin Machu 9 years ago
parent ac0e68efe7
commit cfa960d619

2
Godeps/Godeps.json generated

@ -42,7 +42,7 @@
{
"ImportPath": "github.com/google/cayley",
"Comment": "v0.4.1-160-gcdf0154",
"Rev": "2f5ce909a242795d82529658ceb586863f5dd559"
"Rev": "f143602b8ae880ec975e4c154dd010047de80c1f"
},
{
"ImportPath": "github.com/julienschmidt/httprouter",

@ -66,8 +66,6 @@ func Lock(name string, duration time.Duration, owner string) (bool, time.Time) {
// Unlock unlocks a lock specified by its name if I own it
func Unlock(name, owner string) {
pruneLocks()
unlocked := 0
it, _ := cayley.StartPath(store, name).Has("locked", "locked").Has("locked_by", owner).Save("locked_until", "locked_until").BuildIterator().Optimize()
defer it.Close()
@ -133,7 +131,7 @@ func pruneLocks() {
tt, _ := strconv.ParseInt(t, 10, 64)
if now.Unix() > tt {
log.Debugf("Lock %s owned by %s has expired.", n, o)
log.Debugf("lock %s owned by %s has expired.", n, o)
tr := cayley.NewTransaction()
tr.RemoveQuad(cayley.Quad(n, "locked", "locked", ""))
@ -142,7 +140,9 @@ func pruneLocks() {
err := store.ApplyTransaction(tr)
if err != nil {
log.Errorf("failed transaction (pruneLocks): %s", err)
continue
}
log.Debugf("lock %s has been successfully pruned.", n)
}
}
if it.Err() != nil {

@ -212,8 +212,8 @@ func (qs *QuadStore) runTxPostgres(tx *sql.Tx, in []graph.Delta, opts graph.Igno
return err
}
case graph.Delete:
result, err := tx.Exec(`DELETE FROM quads WHERE subject=$1 and predicate=$2 and object=$3 and label=$4;`,
d.Quad.Subject, d.Quad.Predicate, d.Quad.Object, d.Quad.Label)
result, err := tx.Exec(`DELETE FROM quads WHERE subject_hash=$1 and predicate_hash=$2 and object_hash=$3 and label_hash=$4;`,
hashOf(d.Quad.Subject), hashOf(d.Quad.Predicate), hashOf(d.Quad.Object), hashOf(d.Quad.Label))
if err != nil {
glog.Errorf("couldn't exec DELETE statement: %v", err)
return err

Loading…
Cancel
Save