2020-04-20 10:35:33 +00:00
|
|
|
package ethdb
|
|
|
|
|
|
|
|
type puts struct {
|
2020-05-30 08:12:21 +00:00
|
|
|
mp map[string]putsBucket //map[dbi]putsBucket
|
2020-05-21 20:55:39 +00:00
|
|
|
size int
|
2020-05-23 10:27:05 +00:00
|
|
|
len int
|
2020-04-20 10:35:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 20:55:39 +00:00
|
|
|
func newPuts() *puts {
|
|
|
|
return &puts{
|
2020-05-23 10:27:05 +00:00
|
|
|
mp: make(map[string]putsBucket),
|
2020-04-20 10:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 23:55:32 +00:00
|
|
|
func (p *puts) set(bucket string, key, value []byte) {
|
2020-04-20 10:35:33 +00:00
|
|
|
var bucketPuts putsBucket
|
|
|
|
var ok bool
|
2020-08-10 23:55:32 +00:00
|
|
|
if bucketPuts, ok = p.mp[bucket]; !ok {
|
2020-04-20 10:35:33 +00:00
|
|
|
bucketPuts = make(putsBucket)
|
2020-08-10 23:55:32 +00:00
|
|
|
p.mp[bucket] = bucketPuts
|
2020-04-20 10:35:33 +00:00
|
|
|
}
|
2020-05-23 10:27:05 +00:00
|
|
|
oldLen := len(bucketPuts)
|
2020-05-21 20:55:39 +00:00
|
|
|
skey := string(key)
|
|
|
|
if oldVal, ok := bucketPuts[skey]; ok {
|
|
|
|
p.size -= len(oldVal)
|
|
|
|
} else {
|
|
|
|
p.size += len(skey) + 32 // Add fixed overhead per key
|
|
|
|
}
|
2020-05-23 10:27:05 +00:00
|
|
|
bucketPuts[skey] = value
|
2020-05-21 20:55:39 +00:00
|
|
|
p.size += len(value)
|
2020-05-23 10:27:05 +00:00
|
|
|
p.len += len(bucketPuts) - oldLen
|
2020-04-20 10:35:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 23:55:32 +00:00
|
|
|
func (p *puts) get(bucket string, key []byte) ([]byte, bool) {
|
2020-04-20 10:35:33 +00:00
|
|
|
var bucketPuts putsBucket
|
|
|
|
var ok bool
|
2020-08-10 23:55:32 +00:00
|
|
|
if bucketPuts, ok = p.mp[bucket]; !ok {
|
2020-04-20 10:35:33 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return bucketPuts.Get(key)
|
|
|
|
}
|
|
|
|
|
2020-08-10 23:55:32 +00:00
|
|
|
func (p *puts) Delete(bucket string, key []byte) {
|
2020-04-20 10:35:33 +00:00
|
|
|
p.set(bucket, key, nil)
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:27:05 +00:00
|
|
|
func (p *puts) Len() int {
|
|
|
|
return p.len
|
|
|
|
}
|
|
|
|
|
2020-05-21 20:55:39 +00:00
|
|
|
func (p *puts) Size() int {
|
|
|
|
return p.size
|
2020-04-20 10:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type putsBucket map[string][]byte //map[key]value
|
|
|
|
|
|
|
|
func (pb putsBucket) Get(key []byte) ([]byte, bool) {
|
|
|
|
value, ok := pb[string(key)]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == nil {
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb putsBucket) GetStr(key string) ([]byte, bool) {
|
|
|
|
value, ok := pb[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == nil {
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, true
|
|
|
|
}
|