kvutil: add VerifyDb*, use kvutil in sorted

http://camlistore.org/issue/393

Change-Id: If9482aa05f2346644196e3218dcde571e4d1a1a1
This commit is contained in:
mpl 2014-03-20 01:18:32 +01:00
parent 3b66e2136c
commit 05058091e9
4 changed files with 29 additions and 33 deletions

View File

@ -119,4 +119,5 @@ func (e *Env) SetCamdevVars(altkey bool) {
e.Set("CAMLI_SECRET_RING", secring)
e.Set("CAMLI_KEYID", identity)
e.Set("CAMLI_PUBKEY_BLOBREF", pubKeyRef.String())
e.Set("CAMLI_KV_VERIFY", "true")
}

View File

@ -213,7 +213,6 @@ func (c *serverCmd) setEnvVars() error {
default:
setenv("CAMLI_KVINDEX_ENABLED", "true")
setenv("CAMLI_INDEXER_PATH", "/index-kv/")
setenv("CAMLI_KV_VERIFY", "true")
if c.root == "" {
panic("no root set")
}

View File

@ -19,8 +19,10 @@ limitations under the License.
package kvutil
import (
"fmt"
"io"
"os"
"strconv"
"camlistore.org/third_party/github.com/camlistore/lock"
"camlistore.org/third_party/github.com/cznic/kv"
@ -28,19 +30,35 @@ import (
// Open opens the named kv DB file for reading/writing. It
// creates the file if it does not exist yet.
func Open(filePath string, opts *kv.Options) (*kv.DB, error) {
// TODO(mpl): use it in index pkg and such
func Open(dbFile string, opts *kv.Options) (*kv.DB, error) {
createOpen := kv.Open
if _, err := os.Stat(filePath); os.IsNotExist(err) {
verb := "opening"
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
createOpen = kv.Create
verb = "creating"
}
if opts == nil {
opts = &kv.Options{}
}
if opts.Locker == nil {
opts.Locker = func(fullPath string) (io.Closer, error) {
return lock.Lock(filePath + ".lock")
opts.Locker = func(dbFile string) (io.Closer, error) {
lkfile := dbFile + ".lock"
cl, err := lock.Lock(lkfile)
if err != nil {
return nil, fmt.Errorf("failed to acquire lock on %s: %v", lkfile, err)
}
return cl, nil
}
}
return createOpen(filePath, opts)
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_KV_VERIFY")); v {
opts.VerifyDbBeforeOpen = true
opts.VerifyDbAfterOpen = true
opts.VerifyDbBeforeClose = true
opts.VerifyDbAfterClose = true
}
db, err := createOpen(dbFile, opts)
if err != nil {
return nil, fmt.Errorf("error %s %s: %v", verb, dbFile, err)
}
return db, nil
}

View File

@ -26,13 +26,12 @@ import (
"io"
"log"
"os"
"strconv"
"sync"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/pkg/kvutil"
"camlistore.org/pkg/sorted"
"camlistore.org/third_party/github.com/camlistore/lock"
"camlistore.org/third_party/github.com/cznic/kv"
)
@ -55,31 +54,10 @@ func NewKeyValue(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
createOpen := kv.Open
verb := "opening"
if _, err := os.Stat(file); os.IsNotExist(err) {
createOpen = kv.Create
verb = "creating"
}
opts := &kv.Options{
Locker: func(dbname string) (io.Closer, error) {
lkfile := dbname + ".lock"
cl, err := lock.Lock(lkfile)
if err != nil {
return nil, fmt.Errorf("failed to acquire lock on %s: %v", lkfile, err)
}
return cl, nil
},
}
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_KV_VERIFY")); v {
opts.VerifyDbBeforeOpen = true
opts.VerifyDbAfterOpen = true
opts.VerifyDbBeforeClose = true
opts.VerifyDbAfterClose = true
}
db, err := createOpen(file, opts)
opts := &kv.Options{}
db, err := kvutil.Open(file, opts)
if err != nil {
return nil, fmt.Errorf("error %s %s: %v", verb, file, err)
return nil, err
}
is := &kvis{
db: db,