0
0
Fork 0

enable read_only mode for boltdb indexes

fixes #405
This commit is contained in:
Marty Schoch 2016-08-06 10:47:34 -04:00
parent da794d3762
commit aa3ae3d39c
2 changed files with 71 additions and 7 deletions

View File

@ -59,19 +59,27 @@ func New(mo store.MergeOperator, config map[string]interface{}) (store.KVStore,
fillPercent = bolt.DefaultFillPercent
}
db, err := bolt.Open(path, 0600, nil)
bo := &bolt.Options{}
ro, ok := config["read_only"].(bool)
if ok {
bo.ReadOnly = ro
}
db, err := bolt.Open(path, 0600, bo)
if err != nil {
return nil, err
}
db.NoSync = noSync
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
if !bo.ReadOnly {
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
return err
})
if err != nil {
return nil, err
return err
})
if err != nil {
return nil, err
}
}
rv := Store{

View File

@ -1564,3 +1564,59 @@ func BenchmarkBatchOverhead(b *testing.B) {
batch.Reset()
}
}
func TestOpenReadonlyMultiple(t *testing.T) {
defer func() {
err := os.RemoveAll("testidx")
if err != nil {
t.Fatal(err)
}
}()
// build an index and close it
index, err := New("testidx", NewIndexMapping())
if err != nil {
t.Fatal(err)
}
doca := map[string]interface{}{
"name": "marty",
"desc": "gophercon india",
}
err = index.Index("a", doca)
if err != nil {
t.Fatal(err)
}
err = index.Close()
if err != nil {
t.Fatal(err)
}
// now open it read-only
index, err = OpenUsing("testidx", map[string]interface{}{
"read_only": true,
})
if err != nil {
t.Fatal(err)
}
// now open it again
index2, err := OpenUsing("testidx", map[string]interface{}{
"read_only": true,
})
if err != nil {
t.Fatal(err)
}
err = index.Close()
if err != nil {
t.Fatal(err)
}
err = index2.Close()
if err != nil {
t.Fatal(err)
}
}