Skip to content

Commit

Permalink
database: Use an estimator in Cayley's Size() w/ PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M committed Nov 16, 2015
1 parent f229083 commit 3a1d060
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"os"

"github.com/barakmich/glog"
"github.com/coreos/pkg/capnslog"
"github.com/coreos/clair/health"
"github.com/coreos/clair/utils"
"github.com/coreos/pkg/capnslog"
"github.com/google/cayley"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/path"
Expand Down Expand Up @@ -70,23 +70,29 @@ func Open(dbType, dbPath string) error {
}

var err error
options := make(graph.Options)

// Try to create database if necessary
if dbType == "bolt" || dbType == "leveldb" {
switch dbType {
case "bolt", "leveldb":
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
// No, initialize it if possible
log.Infof("database at %s does not exist yet, creating it", dbPath)

if err = graph.InitQuadStore(dbType, dbPath, nil); err != nil {
err = graph.InitQuadStore(dbType, dbPath, options)
if err != nil {
log.Errorf("could not create database at %s : %s", dbPath, err)
return ErrCantOpen
}
}
} else if dbType == "sql" {
graph.InitQuadStore(dbType, dbPath, nil)
case "sql":
// Replaces the PostgreSQL's slow COUNT query with a fast estimator.
// See:
// Ref: https://wiki.postgresql.org/wiki/Count_estimate
options["use_estimates"] = true

graph.InitQuadStore(dbType, dbPath, options)
}

store, err = cayley.NewGraph(dbType, dbPath, nil)
store, err = cayley.NewGraph(dbType, dbPath, options)
if err != nil {
log.Errorf("could not open database at %s : %s", dbPath, err)
return ErrCantOpen
Expand Down
29 changes: 23 additions & 6 deletions vendor/github.com/google/cayley/graph/sql/quadstore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a1d060

Please sign in to comment.