Commit 1a03cea9 authored by Geofrey Ernest's avatar Geofrey Ernest Committed by GitHub

Merge pull request #170 from gernest/race

guard mem store against race condition
parents 26248eaa 971d1505
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
"sync"
"time" "time"
) )
...@@ -247,6 +248,7 @@ type mem struct { ...@@ -247,6 +248,7 @@ type mem struct {
recycler []int recycler []int
tnl int tnl int
rollback *undos rollback *undos
mu sync.RWMutex
} }
func newMemStorage() (s *mem, err error) { func newMemStorage() (s *mem, err error) {
...@@ -278,10 +280,13 @@ func (s *mem) newUndo(tag int, h int64, data []interface{}) { ...@@ -278,10 +280,13 @@ func (s *mem) newUndo(tag int, h int64, data []interface{}) {
func (s *mem) Acid() bool { return false } func (s *mem) Acid() bool { return false }
func (s *mem) Close() (err error) { func (s *mem) Close() (err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.tnl != 0 { if s.tnl != 0 {
return fmt.Errorf("cannot close DB while open transaction exist") return fmt.Errorf("cannot close DB while open transaction exist")
} }
*s = mem{} s.data, s.recycler, s.rollback = nil, nil, nil
s.id, s.tnl = 0, 0
return return
} }
...@@ -453,6 +458,8 @@ func (s *mem) Create(data ...interface{}) (h int64, err error) { ...@@ -453,6 +458,8 @@ func (s *mem) Create(data ...interface{}) (h int64, err error) {
} }
func (s *mem) Read(dst []interface{}, h int64, cols ...*col) (data []interface{}, err error) { func (s *mem) Read(dst []interface{}, h int64, cols ...*col) (data []interface{}, err error) {
s.mu.RLock()
defer s.mu.RUnlock()
if i := int(h); i != 0 && i < len(s.data) { if i := int(h); i != 0 && i < len(s.data) {
d := s.clone(s.data[h]...) d := s.clone(s.data[h]...)
if cols == nil { if cols == nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment