add mutex to stores

This commit is contained in:
zelig 2015-02-04 22:42:47 +01:00
parent 60931dfaa3
commit 05d636d47e
2 changed files with 389 additions and 371 deletions

View file

@ -4,14 +4,13 @@
package bzz
import (
// "crypto/sha256"
"bytes"
"encoding/binary"
"sync"
"github.com/ethereum/go-ethereum/ethdb"
// "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"
// "path"
)
const dbMaxEntries = 5000 // max number of stored (cached) blocks
@ -42,6 +41,8 @@ type dbStore struct {
gcPos, gcStartPos []byte
gcArray []*gcItem
lock sync.Mutex
}
type dpaDBIndex struct {
@ -250,6 +251,9 @@ func (s *dbStore) collectGarbage() {
func (s *dbStore) Put(chunk *Chunk) {
s.lock.Lock()
defer s.lock.Unlock()
ikey := getIndexKey(chunk.Key)
var index dpaDBIndex
@ -309,6 +313,9 @@ func (s *dbStore) tryAccessIdx(ikey []byte, index *dpaDBIndex) bool {
func (s *dbStore) Get(key Key) (chunk *Chunk, err error) {
s.lock.Lock()
defer s.lock.Unlock()
var index dpaDBIndex
if s.tryAccessIdx(getIndexKey(key), &index) {
@ -331,6 +338,9 @@ func (s *dbStore) Get(key Key) (chunk *Chunk, err error) {
func (s *dbStore) updateAccessCnt(key Key) {
s.lock.Lock()
defer s.lock.Unlock()
var index dpaDBIndex
s.tryAccessIdx(getIndexKey(key), &index) // result_chn == nil, only update access cnt

View file

@ -4,6 +4,7 @@ package bzz
import (
"bytes"
"sync"
)
const (
@ -19,6 +20,7 @@ type memStore struct {
accessCnt uint64 // access counter; oldest is thrown away when full
dbAccessCnt uint64
dbStore *dbStore
lock sync.Mutex
}
/*
@ -141,6 +143,9 @@ func (node *memTree) updateAccess(a uint64) {
func (s *memStore) Put(entry *Chunk) {
s.lock.Lock()
defer s.lock.Unlock()
if s.entryCnt >= maxEntries {
s.removeOldest()
}
@ -201,6 +206,9 @@ func (s *memStore) Put(entry *Chunk) {
func (s *memStore) Get(hash Key) (chunk *Chunk, err error) {
s.lock.Lock()
defer s.lock.Unlock()
node := s.memtree
bitpos := uint(0)
for node.entry == nil {