Cache leveldb stores created from specs (#1975)

This commit is contained in:
Ben Kalman
2016-07-06 15:42:21 -07:00
committed by GitHub
parent 53b9ce3c1e
commit fa496f59ef
2 changed files with 50 additions and 3 deletions
+16 -3
View File
@@ -20,6 +20,7 @@ import (
var (
datasetRe = regexp.MustCompile("^" + dataset.DatasetRe.String() + "$")
ldbStores = map[string]*refCountingLdbStore{}
)
func GetDatabase(str string) (datas.Database, error) {
@@ -38,7 +39,7 @@ func GetChunkStore(str string) (chunks.ChunkStore, error) {
switch sp.Protocol {
case "ldb":
return chunks.NewLevelDBStoreUseFlags(sp.Path, ""), nil
return getLDBStore(sp.Path), nil
case "mem":
return chunks.NewMemoryStore(), nil
default:
@@ -173,7 +174,7 @@ func (spec databaseSpec) Database() (ds datas.Database, err error) {
}))
case "ldb":
err = d.Unwrap(d.Try(func() {
ds = datas.NewDatabase(chunks.NewLevelDBStoreUseFlags(spec.Path, ""))
ds = datas.NewDatabase(getLDBStore(spec.Path))
}))
case "mem":
ds = datas.NewDatabase(chunks.NewMemoryStore())
@@ -225,7 +226,6 @@ func RegisterDatabaseFlags(flags *flag.FlagSet) {
chunks.RegisterLevelDBFlags(flags)
}
// Utility functions to create specs
func CreateDatabaseSpecString(protocol, path string) string {
return fmt.Sprintf("%s:%s", protocol, path)
}
@@ -233,3 +233,16 @@ func CreateDatabaseSpecString(protocol, path string) string {
func CreateValueSpecString(protocol, path, value string) string {
return fmt.Sprintf("%s:%s::%s", protocol, path, value)
}
func getLDBStore(path string) chunks.ChunkStore {
if store, ok := ldbStores[path]; ok {
store.AddRef()
return store
}
store := newRefCountingLdbStore(path, func() {
delete(ldbStores, path)
})
ldbStores[path] = store
return store
}
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2016 The Noms Authors. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package spec
import (
"github.com/attic-labs/noms/go/chunks"
"github.com/attic-labs/noms/go/d"
)
type refCountingLdbStore struct {
*chunks.LevelDBStore
refCount int
closeFn func()
}
func newRefCountingLdbStore(path string, closeFn func()) *refCountingLdbStore {
return &refCountingLdbStore{chunks.NewLevelDBStoreUseFlags(path, ""), 1, closeFn}
}
func (r *refCountingLdbStore) AddRef() {
r.refCount++
}
func (r *refCountingLdbStore) Close() (err error) {
d.Chk.True(r.refCount > 0)
r.refCount--
if r.refCount == 0 {
err = r.LevelDBStore.Close()
r.closeFn()
}
return
}