-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
177 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package blockchainDB | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
) | ||
|
||
// Shard | ||
// Holds the stuff required to access a shard. | ||
type Shard struct { | ||
BufferCnt int // Number of buffers to use | ||
Filename string // The file with the BFile | ||
BFile *BFile // The BFile | ||
Cache map[[32]byte][]byte // Cache of what is being written to the cache | ||
Mutex sync.Mutex // Keeps compression from conflict with access | ||
} | ||
|
||
// NewShard | ||
// Create and open a new Shard | ||
func NewShard(BufferCnt int, Filename string) (shard *Shard, err error) { | ||
shard = new(Shard) | ||
shard.BufferCnt = BufferCnt | ||
shard.Filename = Filename | ||
shard.Cache = make(map[[32]byte][]byte) | ||
if shard.BFile, err = NewBFile(Filename, BufferCnt); err != nil { | ||
return nil, err | ||
} | ||
return shard, err | ||
} | ||
|
||
// Close | ||
// Clean up and close the Shard | ||
func (s *Shard) Close() { | ||
s.Mutex.Lock() | ||
defer s.Mutex.Unlock() | ||
|
||
s.Cache = nil | ||
s.BFile.Close() | ||
s.BFile = nil | ||
} | ||
|
||
// Open | ||
// Open an existing Shard. If the BFile does not exist, create it | ||
func (s *Shard) Open() (err error) { | ||
if s.BFile != nil { | ||
return | ||
} | ||
if s.BFile, err = OpenBFile(s.BufferCnt, s.Filename); err != nil { | ||
if os.IsNotExist(err) { | ||
s.BFile, err = NewBFile(s.Filename, s.BufferCnt) | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Put | ||
// Put a key value pair into the shard | ||
func (s *Shard) Put (key [32]byte, value []byte) error { | ||
s.Cache[key]=value | ||
return s.BFile.Put(key,value) | ||
} | ||
|
||
// Get | ||
// Get the value for the key out of the shard | ||
func (s *Shard) Get (key [32]byte) ( value []byte, err error) { | ||
if value, ok := s.Cache[key]; ok { | ||
return value, nil | ||
} | ||
return s.BFile.Get(key) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package blockchainDB | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gitlab.com/accumulatenetwork/accumulate/internal/database/smt/common" | ||
) | ||
|
||
func TestShardDB(t *testing.T) { | ||
var shardDB ShardDB | ||
var r common.RandHash | ||
for i := 0; i < 1000000; i++ { | ||
key := r.NextA() | ||
value := r.GetRandBuff(200) | ||
shardDB.Put(key, value) | ||
} | ||
r = *new(common.RandHash) | ||
for i := 0; i < 1000000; i++ { | ||
key := r.NextA() | ||
value := r.GetRandBuff(200) | ||
v := shardDB.Get(key) | ||
assert.Equal(t, value, v, "did not get the same value back") | ||
} | ||
} | ||
|
||
func TestShard(t *testing.T) { | ||
directory := filepath.Join(os.TempDir(),"ShardTest") | ||
os.Mkdir(directory,os.ModePerm) | ||
defer os.RemoveAll(directory) | ||
filename := filepath.Join(directory,"shard") | ||
|
||
shard,err := NewShard(5,filename) | ||
assert.NoError(t,err,err) | ||
|
||
entries := make(map[[32]byte][]byte) | ||
fr := NewFastRandom([32]byte {1,2,3,4,5}) | ||
for i:= 0; i<100000; i++ { | ||
entries[fr.NextHash()] = fr.RandBuff(100,500) | ||
} | ||
for k := range entries { | ||
nv := fr.RandBuff(100,500) | ||
shard.Put(k,nv) | ||
entries[k]=nv | ||
} | ||
for i:=0;i<1000000,i++ { | ||
Check failure on line 48 in internal/database/blockchainDB/sharddb_test.go GitHub Actions / Lint
Check failure on line 48 in internal/database/blockchainDB/sharddb_test.go GitHub Actions / Build macOS
Check failure on line 48 in internal/database/blockchainDB/sharddb_test.go GitHub Actions / Build macOS
Check failure on line 48 in internal/database/blockchainDB/sharddb_test.go GitHub Actions / Build
|
||
v2,err := shard.Get(k) | ||
assert.NoError(t,err,err) | ||
assert.Equal(t,v,v2,"Didn't get the right value back") | ||
} | ||
|
||
shard.Close() | ||
} |