Skip to content

Commit

Permalink
Merge pull request #97 from qubic/i-90
Browse files Browse the repository at this point in the history
Replace for with Iter on store.GetEmptyTicksForEpochs.
  • Loading branch information
0xluk authored Jan 24, 2025
2 parents ed89b83 + cdcd41f commit 8005bbf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion rpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net"
"net/http"
"slices"
)

var _ protobuff.ArchiveServiceServer = &Server{}
Expand Down Expand Up @@ -375,7 +376,10 @@ func (s *Server) GetStatus(ctx context.Context, _ *emptypb.Empty) (*protobuff.Ge
epochs = append(epochs, epoch)
}

emptyTicksForAllEpochs, err := s.store.GetEmptyTicksForEpochs(epochs)
lowestEpoch := slices.Min(epochs)
highestEpoch := slices.Max(epochs)

emptyTicksForAllEpochs, err := s.store.GetEmptyTicksForEpochs(lowestEpoch, highestEpoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "getting empty ticks for all epochs: %v", err)
}
Expand Down
30 changes: 21 additions & 9 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,22 +671,34 @@ func (s *PebbleStore) GetEmptyTicksForEpoch(epoch uint32) (uint32, error) {
return emptyTicksCount, nil
}

func (s *PebbleStore) GetEmptyTicksForEpochs(epochs []uint32) (map[uint32]uint32, error) {
func (s *PebbleStore) GetEmptyTicksForEpochs(firstEpoch, lastEpoch uint32) (map[uint32]uint32, error) {

emptyTickMap := make(map[uint32]uint32, len(epochs))
iter, err := s.db.NewIter(&pebble.IterOptions{
LowerBound: emptyTicksPerEpochKey(firstEpoch),
UpperBound: emptyTicksPerEpochKey(lastEpoch + 1), // Increment as upper bound is exclusive
})
if err != nil {
return nil, errors.Wrap(err, "creating iter")
}
defer iter.Close()

emptyTickMap := make(map[uint32]uint32)

for iter.First(); iter.Valid(); iter.Next() {

for _, epoch := range epochs {
emptyTicks, err := s.GetEmptyTicksForEpoch(epoch)
value, err := iter.ValueAndErr()
if err != nil {
if !errors.Is(err, pebble.ErrNotFound) {
return nil, errors.Wrapf(err, "getting empty ticks for epoch %d", epoch)
}
return nil, errors.Wrap(err, "getting value from iter")
}
emptyTickMap[epoch] = emptyTicks

key := iter.Key()
epochNumber := binary.BigEndian.Uint64(key[1:])
emptyTicksCount := binary.LittleEndian.Uint32(value)

emptyTickMap[uint32(epochNumber)] = emptyTicksCount
}

return emptyTickMap, nil

}

func (s *PebbleStore) DeleteEmptyTicksKeyForEpoch(epoch uint32) error {
Expand Down

0 comments on commit 8005bbf

Please sign in to comment.