From 9155dc0c56d3509dae74d02160592f6c818fd426 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Tue, 23 Jun 2015 14:04:48 -0400 Subject: [PATCH] spec out the interface change --- cmd/cayleyupgrade/cayleyupgrade.go | 75 ++++++++++++++++++++++++++++++++++++++ graph/quadstore.go | 40 +++++++++++--------- 2 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 cmd/cayleyupgrade/cayleyupgrade.go diff --git a/cmd/cayleyupgrade/cayleyupgrade.go b/cmd/cayleyupgrade/cayleyupgrade.go new file mode 100644 index 0000000..e6813c9 --- /dev/null +++ b/cmd/cayleyupgrade/cayleyupgrade.go @@ -0,0 +1,75 @@ +// Copyright 2015 The Cayley Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !appengine + +package main + +import ( + "errors" + "flag" + + "github.com/google/cayley/config" + _ "github.com/google/cayley/graph" + + // Load all supported backends. + "github.com/google/cayley/db" + _ "github.com/google/cayley/graph/bolt" + _ "github.com/google/cayley/graph/leveldb" + _ "github.com/google/cayley/graph/memstore" + _ "github.com/google/cayley/graph/mongo" + "github.com/google/cayley/internal" +) + +var ( + configFile = flag.String("config", "", "Path to an explicit configuration file.") + databasePath = flag.String("dbpath", "/tmp/testdb", "Path to the database.") + databaseBackend = flag.String("db", "memstore", "Database Backend.") +) + +func configFrom(file string) *config.Config { + if cfg.DatabasePath == "" { + cfg.DatabasePath = *databasePath + } + + if cfg.DatabaseType == "" { + cfg.DatabaseType = *databaseBackend + } + return cfg +} + +func main() { + flag.Parse() + cfg := configFrom(*configFile) + + r, registered := storeRegistry[*databaseBackend] + if registered { + return r.initFunc(dbpath, opts) + } + return errors.New("quadstore: name '" + name + "' is not registered") + if err != nil { + break + } + if *quadFile != "" { + handle, err = db.Open(cfg) + if err != nil { + break + } + err = internal.Load(handle.QuadWriter, cfg, *quadFile, *quadType) + if err != nil { + break + } + handle.Close() + } +} diff --git a/graph/quadstore.go b/graph/quadstore.go index 8a89885..ebba912 100644 --- a/graph/quadstore.go +++ b/graph/quadstore.go @@ -150,27 +150,24 @@ type BulkLoader interface { type NewStoreFunc func(string, Options) (QuadStore, error) type InitStoreFunc func(string, Options) error +type UpgradeStoreFunc func(string, Options) error type NewStoreForRequestFunc func(QuadStore, Options) (QuadStore, error) -type register struct { - newFunc NewStoreFunc - newForRequestFunc NewStoreForRequestFunc - initFunc InitStoreFunc - isPersistent bool +type QuadStoreRegistration struct { + NewFunc NewStoreFunc + NewForRequestFunc NewStoreForRequestFunc + UpgradeFunc UpgradeStoreFunc + InitFunc InitStoreFunc + IsPersistent bool } -var storeRegistry = make(map[string]register) +var storeRegistry = make(map[string]QuadStoreRegistration) -func RegisterQuadStore(name string, persists bool, newFunc NewStoreFunc, initFunc InitStoreFunc, newForRequestFunc NewStoreForRequestFunc) { +func RegisterQuadStore(name string, register QuadStoreRegistration) { if _, found := storeRegistry[name]; found { panic("already registered QuadStore " + name) } - storeRegistry[name] = register{ - newFunc: newFunc, - initFunc: initFunc, - newForRequestFunc: newForRequestFunc, - isPersistent: persists, - } + storeRegistry[name] = register } func NewQuadStore(name, dbpath string, opts Options) (QuadStore, error) { @@ -178,13 +175,13 @@ func NewQuadStore(name, dbpath string, opts Options) (QuadStore, error) { if !registered { return nil, errors.New("quadstore: name '" + name + "' is not registered") } - return r.newFunc(dbpath, opts) + return r.NewFunc(dbpath, opts) } func InitQuadStore(name, dbpath string, opts Options) error { r, registered := storeRegistry[name] if registered { - return r.initFunc(dbpath, opts) + return r.InitFunc(dbpath, opts) } return errors.New("quadstore: name '" + name + "' is not registered") } @@ -192,13 +189,22 @@ func InitQuadStore(name, dbpath string, opts Options) error { func NewQuadStoreForRequest(qs QuadStore, opts Options) (QuadStore, error) { r, registered := storeRegistry[qs.Type()] if registered { - return r.newForRequestFunc(qs, opts) + return r.NewForRequestFunc(qs, opts) } return nil, errors.New("QuadStore does not support Per Request construction, check config") } +func UpgradeQuadStore(name, dbpath string, opts Options) error { + r, registered := storeRegistry[name] + if registered { + return r.UpgradeFunc(dbpath, opts) + } + return errors.New("quadstore: name '" + name + "' is not registered") + +} + func IsPersistent(name string) bool { - return storeRegistry[name].isPersistent + return storeRegistry[name].IsPersistent } func QuadStores() []string {