Merge pull request #848 from travisturner/lock-to-read-lock

Lock to read lock
This commit is contained in:
Travis Turner 2017-09-26 15:29:58 -05:00 • committed by GitHub
commit 204898e612
5 changed files with 51 additions and 40 deletions

View file

@ -67,7 +67,7 @@ const (
// Fragment represents the intersection of a frame and slice in an index.
type Fragment struct {
mu sync.Mutex
mu sync.RWMutex
// Composite identifiers
index string
@ -545,11 +545,8 @@ func (f *Fragment) SetFieldValue(columnID uint64, bitDepth uint, value uint64) (
// FieldSum returns the sum of a given field as well as the number of columns involved.
// A bitmap can be passed in to optionally filter the computed columns.
func (f *Fragment) FieldSum(filter *Bitmap, bitDepth uint) (sum, count uint64, err error) {
f.mu.Lock()
defer f.mu.Unlock()
// Compute count based on the existance bit.
row := f.row(uint64(bitDepth), true, true)
row := f.Row(uint64(bitDepth))
if filter != nil {
row = row.Intersect(filter)
}
@ -563,7 +560,7 @@ func (f *Fragment) FieldSum(filter *Bitmap, bitDepth uint) (sum, count uint64, e
// 10*(2^0) + 4*(2^1) + 3*(2^2) = 30
//
for i := uint(0); i < bitDepth; i++ {
row := f.row(uint64(i), true, true)
row := f.Row(uint64(i))
if filter != nil {
row = row.Intersect(filter)
}

View file

@ -43,7 +43,7 @@ const (
// Frame represents a container for views.
type Frame struct {
mu sync.Mutex
mu sync.RWMutex
path string
index string
name string
@ -113,8 +113,8 @@ func (f *Frame) RowAttrStore() *AttrStore { return f.rowAttrStore }
// MaxSlice returns the max slice in the frame.
func (f *Frame) MaxSlice() uint64 {
f.mu.Lock()
defer f.mu.Unlock()
f.mu.RLock()
defer f.mu.RUnlock()
var max uint64
for _, view := range f.views {
@ -129,8 +129,8 @@ func (f *Frame) MaxSlice() uint64 {
// MaxInverseSlice returns the max inverse slice in the frame.
func (f *Frame) MaxInverseSlice() uint64 {
f.mu.Lock()
defer f.mu.Unlock()
f.mu.RLock()
defer f.mu.RUnlock()
view := f.views[ViewInverse]
if view == nil {
@ -166,9 +166,9 @@ func (f *Frame) SetRowLabel(v string) error {
// RowLabel returns the row label.
func (f *Frame) RowLabel() string {
f.mu.Lock()
f.mu.RLock()
v := f.rowLabel
f.mu.Unlock()
f.mu.RUnlock()
return v
}
@ -217,8 +217,8 @@ func (f *Frame) CacheSize() uint32 {
// Options returns all options for this frame.
func (f *Frame) Options() FrameOptions {
f.mu.Lock()
defer f.mu.Unlock()
f.mu.RLock()
defer f.mu.RUnlock()
return f.options()
}
@ -404,8 +404,8 @@ func (f *Frame) Close() error {
// Schema returns the frame's current schema.
func (f *Frame) Schema() *FrameSchema {
f.mu.Lock()
defer f.mu.Unlock()
f.mu.RLock()
defer f.mu.RUnlock()
return f.schema
}
@ -506,8 +506,8 @@ func (f *Frame) ViewPath(name string) string {
// View returns a view in the frame by name.
func (f *Frame) View(name string) *View {
f.mu.Lock()
defer f.mu.Unlock()
f.mu.RLock()
defer f.mu.RUnlock()
return f.view(name)
}

View file

@ -38,7 +38,7 @@ const (
// Holder represents a container for indexes.
type Holder struct {
mu sync.Mutex
mu sync.RWMutex
// Indexes by name.
indexes map[string]*Index
@ -183,8 +183,8 @@ func (h *Holder) IndexPath(name string) string { return filepath.Join(h.Path, na
// Index returns the index by name.
func (h *Holder) Index(name string) *Index {
h.mu.Lock()
defer h.mu.Unlock()
h.mu.RLock()
defer h.mu.RUnlock()
return h.index(name)
}

View file

@ -37,7 +37,7 @@ const (
// Index represents a container for frames.
type Index struct {
mu sync.Mutex
mu sync.RWMutex
path string
name string
@ -129,12 +129,26 @@ func (i *Index) SetColumnLabel(v string) error {
// ColumnLabel returns the column label.
func (i *Index) ColumnLabel() string {
i.mu.Lock()
i.mu.RLock()
v := i.columnLabel
i.mu.Unlock()
i.mu.RUnlock()
return v
}
// Options returns all options for this index.
func (i *Index) Options() IndexOptions {
i.mu.RLock()
defer i.mu.RUnlock()
return i.options()
}
func (i *Index) options() IndexOptions {
return IndexOptions{
ColumnLabel: i.columnLabel,
TimeQuantum: i.timeQuantum,
}
}
// Open opens and initializes the index.
func (i *Index) Open() error {
// Ensure the path exists.
@ -262,8 +276,8 @@ func (i *Index) MaxSlice() uint64 {
if i == nil {
return 0
}
i.mu.Lock()
defer i.mu.Unlock()
i.mu.RLock()
defer i.mu.RUnlock()
max := i.remoteMaxSlice
for _, f := range i.frames {
@ -288,8 +302,8 @@ func (i *Index) MaxInverseSlice() uint64 {
if i == nil {
return 0
}
i.mu.Lock()
defer i.mu.Unlock()
i.mu.RLock()
defer i.mu.RUnlock()
max := i.remoteMaxInverseSlice
for _, f := range i.frames {
@ -309,8 +323,8 @@ func (i *Index) SetRemoteMaxInverseSlice(v uint64) {
// TimeQuantum returns the default time quantum for the index.
func (i *Index) TimeQuantum() TimeQuantum {
i.mu.Lock()
defer i.mu.Unlock()
i.mu.RLock()
defer i.mu.RUnlock()
return i.timeQuantum
}
@ -345,8 +359,8 @@ func (i *Index) InputDefinitionPath() string {
// Frame returns a frame in the index by name.
func (i *Index) Frame(name string) *Frame {
i.mu.Lock()
defer i.mu.Unlock()
i.mu.RLock()
defer i.mu.RUnlock()
return i.frame(name)
}
@ -366,8 +380,8 @@ func (i *Index) inputDefinition(name string) *InputDefinition { return i.inputDe
// Frames returns a list of all frames in the index.
func (i *Index) Frames() []*Frame {
i.mu.Lock()
defer i.mu.Unlock()
i.mu.RLock()
defer i.mu.RUnlock()
a := make([]*Frame, 0, len(i.frames))
for _, f := range i.frames {

10
view.go
View file

@ -43,7 +43,7 @@ func IsValidView(name string) bool {
// View represents a container for frame data.
type View struct {
mu sync.Mutex
mu sync.RWMutex
path string
index string
frame string
@ -174,8 +174,8 @@ func (v *View) Close() error {
// MaxSlice returns the max slice in the view.
func (v *View) MaxSlice() uint64 {
v.mu.Lock()
defer v.mu.Unlock()
v.mu.RLock()
defer v.mu.RUnlock()
var max uint64
for slice := range v.fragments {
@ -194,8 +194,8 @@ func (v *View) FragmentPath(slice uint64) string {
// Fragment returns a fragment in the view by slice.
func (v *View) Fragment(slice uint64) *Fragment {
v.mu.Lock()
defer v.mu.Unlock()
v.mu.RLock()
defer v.mu.RUnlock()
return v.fragment(slice)
}