mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 12:16:44 +00:00
add mutex to stores
This commit is contained in:
parent
60931dfaa3
commit
05d636d47e
2 changed files with 389 additions and 371 deletions
|
|
@ -4,14 +4,13 @@
|
||||||
package bzz
|
package bzz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "crypto/sha256"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
// "github.com/ethereum/go-ethereum/ethutil"
|
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
// "path"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const dbMaxEntries = 5000 // max number of stored (cached) blocks
|
const dbMaxEntries = 5000 // max number of stored (cached) blocks
|
||||||
|
|
@ -42,6 +41,8 @@ type dbStore struct {
|
||||||
|
|
||||||
gcPos, gcStartPos []byte
|
gcPos, gcStartPos []byte
|
||||||
gcArray []*gcItem
|
gcArray []*gcItem
|
||||||
|
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type dpaDBIndex struct {
|
type dpaDBIndex struct {
|
||||||
|
|
@ -250,6 +251,9 @@ func (s *dbStore) collectGarbage() {
|
||||||
|
|
||||||
func (s *dbStore) Put(chunk *Chunk) {
|
func (s *dbStore) Put(chunk *Chunk) {
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
ikey := getIndexKey(chunk.Key)
|
ikey := getIndexKey(chunk.Key)
|
||||||
var index dpaDBIndex
|
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) {
|
func (s *dbStore) Get(key Key) (chunk *Chunk, err error) {
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
var index dpaDBIndex
|
var index dpaDBIndex
|
||||||
|
|
||||||
if s.tryAccessIdx(getIndexKey(key), &index) {
|
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) {
|
func (s *dbStore) updateAccessCnt(key Key) {
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
var index dpaDBIndex
|
var index dpaDBIndex
|
||||||
s.tryAccessIdx(getIndexKey(key), &index) // result_chn == nil, only update access cnt
|
s.tryAccessIdx(getIndexKey(key), &index) // result_chn == nil, only update access cnt
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ package bzz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -19,6 +20,7 @@ type memStore struct {
|
||||||
accessCnt uint64 // access counter; oldest is thrown away when full
|
accessCnt uint64 // access counter; oldest is thrown away when full
|
||||||
dbAccessCnt uint64
|
dbAccessCnt uint64
|
||||||
dbStore *dbStore
|
dbStore *dbStore
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -141,6 +143,9 @@ func (node *memTree) updateAccess(a uint64) {
|
||||||
|
|
||||||
func (s *memStore) Put(entry *Chunk) {
|
func (s *memStore) Put(entry *Chunk) {
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
if s.entryCnt >= maxEntries {
|
if s.entryCnt >= maxEntries {
|
||||||
s.removeOldest()
|
s.removeOldest()
|
||||||
}
|
}
|
||||||
|
|
@ -201,6 +206,9 @@ func (s *memStore) Put(entry *Chunk) {
|
||||||
|
|
||||||
func (s *memStore) Get(hash Key) (chunk *Chunk, err error) {
|
func (s *memStore) Get(hash Key) (chunk *Chunk, err error) {
|
||||||
|
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
node := s.memtree
|
node := s.memtree
|
||||||
bitpos := uint(0)
|
bitpos := uint(0)
|
||||||
for node.entry == nil {
|
for node.entry == nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue