Skip to content
Snippets Groups Projects
Commit 84697103 authored by Manish R Jain's avatar Manish R Jain
Browse files

Add read-write locks to posting list

parent cbf2e445
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
package posting package posting
import ( import (
"sync"
"github.com/google/flatbuffers/go" "github.com/google/flatbuffers/go"
"github.com/manishrjain/dgraph/posting/types" "github.com/manishrjain/dgraph/posting/types"
"github.com/manishrjain/dgraph/x" "github.com/manishrjain/dgraph/x"
...@@ -30,6 +32,7 @@ const Set = 0x01 ...@@ -30,6 +32,7 @@ const Set = 0x01
const Del = 0x02 const Del = 0x02
type List struct { type List struct {
mutex sync.RWMutex
buffer []byte buffer []byte
mutations []byte mutations []byte
} }
...@@ -59,6 +62,9 @@ func addPosting(b *flatbuffers.Builder, p types.Posting) flatbuffers.UOffsetT { ...@@ -59,6 +62,9 @@ func addPosting(b *flatbuffers.Builder, p types.Posting) flatbuffers.UOffsetT {
var empty []byte var empty []byte
func (l *List) Init() { func (l *List) Init() {
l.mutex.Lock()
defer l.mutex.Unlock()
if len(empty) == 0 { if len(empty) == 0 {
b := flatbuffers.NewBuilder(0) b := flatbuffers.NewBuilder(0)
types.PostingListStart(b) types.PostingListStart(b)
...@@ -73,10 +79,16 @@ func (l *List) Init() { ...@@ -73,10 +79,16 @@ func (l *List) Init() {
} }
func (l *List) Root() *types.PostingList { func (l *List) Root() *types.PostingList {
l.mutex.RLock()
defer l.mutex.RUnlock()
return types.GetRootAsPostingList(l.buffer, 0) return types.GetRootAsPostingList(l.buffer, 0)
} }
func (l *List) AddMutation(t x.Triple, op byte) { func (l *List) AddMutation(t x.Triple, op byte) {
l.mutex.Lock()
defer l.mutex.Unlock()
b := flatbuffers.NewBuilder(0) b := flatbuffers.NewBuilder(0)
muts := types.GetRootAsPostingList(l.mutations, 0) muts := types.GetRootAsPostingList(l.mutations, 0)
var offsets []flatbuffers.UOffsetT var offsets []flatbuffers.UOffsetT
...@@ -135,7 +147,7 @@ func remove(ll *linked.List, p *types.Posting) { ...@@ -135,7 +147,7 @@ func remove(ll *linked.List, p *types.Posting) {
} }
} }
func (l *List) GenerateLinkedList() *linked.List { func (l *List) generateLinkedList() *linked.List {
plist := types.GetRootAsPostingList(l.buffer, 0) plist := types.GetRootAsPostingList(l.buffer, 0)
ll := linked.New() ll := linked.New()
...@@ -169,7 +181,10 @@ func (l *List) GenerateLinkedList() *linked.List { ...@@ -169,7 +181,10 @@ func (l *List) GenerateLinkedList() *linked.List {
} }
func (l *List) Commit() { func (l *List) Commit() {
ll := l.GenerateLinkedList() l.mutex.Lock()
defer l.mutex.Unlock()
ll := l.generateLinkedList()
b := flatbuffers.NewBuilder(0) b := flatbuffers.NewBuilder(0)
var offsets []flatbuffers.UOffsetT var offsets []flatbuffers.UOffsetT
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment