2016-07-16 13:43:53 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package kvs
|
|
|
|
|
2017-02-09 11:16:59 +00:00
|
|
|
import (
|
2017-02-16 10:12:18 +00:00
|
|
|
"io"
|
2017-02-09 11:16:59 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/abcum/surreal/cnf"
|
|
|
|
)
|
|
|
|
|
|
|
|
var stores = make(map[string]func(*cnf.Options) (DB, error))
|
|
|
|
|
|
|
|
// DB represents a backing datastore.
|
|
|
|
type DS struct {
|
|
|
|
db DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// New sets up the underlying key-value store
|
|
|
|
func New(opts *cnf.Options) (ds *DS, err error) {
|
|
|
|
|
|
|
|
var db DB
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case opts.DB.Path == "memory":
|
|
|
|
db, err = stores["rixxdb"](opts)
|
|
|
|
case strings.HasPrefix(opts.DB.Path, "s3://"):
|
|
|
|
db, err = stores["rixxdb"](opts)
|
|
|
|
case strings.HasPrefix(opts.DB.Path, "gcs://"):
|
|
|
|
db, err = stores["rixxdb"](opts)
|
|
|
|
case strings.HasPrefix(opts.DB.Path, "logr://"):
|
|
|
|
db, err = stores["rixxdb"](opts)
|
|
|
|
case strings.HasPrefix(opts.DB.Path, "file://"):
|
|
|
|
db, err = stores["rixxdb"](opts)
|
|
|
|
case strings.HasPrefix(opts.DB.Path, "rixxdb://"):
|
|
|
|
db, err = stores["rixxdb"](opts)
|
|
|
|
case strings.HasPrefix(opts.DB.Path, "dendrodb://"):
|
|
|
|
db, err = stores["dendro"](opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ds = &DS{db: db}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:08:46 +00:00
|
|
|
// Begin begins a new read / write transaction
|
|
|
|
// with the underlying database, and returns
|
|
|
|
// the transaction, or any error which occured.
|
2017-02-09 11:16:59 +00:00
|
|
|
func (ds *DS) Begin(writable bool) (txn TX, err error) {
|
|
|
|
return ds.db.Begin(writable)
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:12:18 +00:00
|
|
|
// Import loads database operations from a reader.
|
|
|
|
// This can be used to playback a database snapshot
|
|
|
|
// into an already running database.
|
|
|
|
func (ds *DS) Import(r io.Reader) (err error) {
|
|
|
|
return ds.db.Import(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export saves all database operations to a writer.
|
|
|
|
// This can be used to save a database snapshot
|
|
|
|
// to a secondary file or stream.
|
|
|
|
func (ds *DS) Export(w io.Writer) (err error) {
|
|
|
|
return ds.db.Export(w)
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:08:46 +00:00
|
|
|
// Close closes the underlying rixxdb / dendrodb
|
|
|
|
// database connection, enabling the underlying
|
|
|
|
// database to clean up remainging transactions.
|
2017-02-09 11:16:59 +00:00
|
|
|
func (ds *DS) Close() (err error) {
|
|
|
|
return ds.db.Close()
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:08:46 +00:00
|
|
|
// Register registers a new database type with
|
|
|
|
// the kvs package, enabling it's use as a
|
|
|
|
// backing datastore within SurrealDB.
|
2017-02-09 11:16:59 +00:00
|
|
|
func Register(name string, constructor func(*cnf.Options) (DB, error)) {
|
|
|
|
stores[name] = constructor
|
2016-07-16 13:43:53 +00:00
|
|
|
}
|