Skip to content

Commit

Permalink
feat: delete radius in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
fearlessfe committed Nov 28, 2024
1 parent 6bae115 commit df48088
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
1 change: 0 additions & 1 deletion portalnetwork/storage/content_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var MaxDistance = uint256.MustFromHex("0xfffffffffffffffffffffffffffffffffffffff

type ContentType byte

var RadisuKey = []byte("radius")
var SizeKey = []byte("size")

type ContentKey struct {
Expand Down
31 changes: 19 additions & 12 deletions portalnetwork/storage/ethpepple/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ethpepple

import (
"bytes"
"encoding/binary"
"runtime"
"sync/atomic"
Expand Down Expand Up @@ -139,28 +140,32 @@ func NewPeppleStorage(config PeppleStorageConfig) (storage.ContentStorage, error
writeOptions: &pebble.WriteOptions{Sync: false},
}
cs.radius.Store(storage.MaxDistance)
radius, _, err := cs.db.Get(storage.RadisuKey)

val, _, err := cs.db.Get(storage.SizeKey)
if err != nil && err != pebble.ErrNotFound {
return nil, err
}
if err == nil {
size := binary.BigEndian.Uint64(val)
// init stage, no need to use lock
cs.size = size
}

iter, err := cs.db.NewIter(nil)
if err != nil {
return nil, err
}
defer iter.Close()
if iter.Last() && iter.Valid() {
distance := iter.Key()
dis := uint256.NewInt(0)
err = dis.UnmarshalSSZ(radius)
err = dis.UnmarshalSSZ(distance)
if err != nil {
return nil, err
}
cs.radius.Store(dis)
}

val, _, err := cs.db.Get(storage.SizeKey)
if err != nil && err != pebble.ErrNotFound {
return nil, err
}
if err == nil {
size := binary.BigEndian.Uint64(val)
// init stage, no need to use lock
cs.size = size
}
cs.sizeChan <- struct{}{}
go cs.saveCapacity()
return cs, nil
Expand Down Expand Up @@ -256,12 +261,14 @@ func (c *ContentStorage) prune() error {

batch := c.db.NewBatch()
for iter.Last(); iter.Valid(); iter.Prev() {
if bytes.Equal(iter.Key(), storage.SizeKey) {
continue
}
if curentSize < expectSize {
batch.Delete(iter.Key(), nil)
curentSize += uint64(len(iter.Key())) + uint64(len(iter.Value()))
} else {
distance := iter.Key()
c.db.Set(storage.RadisuKey, distance, c.writeOptions)
dis := uint256.NewInt(0)
err = dis.UnmarshalSSZ(distance)
if err != nil {
Expand Down

0 comments on commit df48088

Please sign in to comment.