Skip to content

Commit

Permalink
Tweak pagination to support multiple epochs and make page index start…
Browse files Browse the repository at this point in the history
… at 1.
  • Loading branch information
LINCKODE committed Jul 29, 2024
1 parent 192edc1 commit aadd06d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
32 changes: 12 additions & 20 deletions api/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"time"
)

var EmptyPaginationData = RichListPaginationData{}

type RichListPaginationData struct {
RichListLength int32
RichListPageCount int32
}

type Cache struct {
mutexLock sync.RWMutex

Expand All @@ -14,8 +21,7 @@ type Cache struct {
spectrumData SpectrumData
lastSpectrumDataUpdate time.Time

richListLength int32
richListPageCount int32
richListPaginationDataPerEpoch map[string]RichListPaginationData
}

func (c *Cache) UpdateDataCache(spectrumData SpectrumData, qubicData QubicData) {
Expand Down Expand Up @@ -57,30 +63,16 @@ func (c *Cache) GetLastSpectrumDataUpdate() time.Time {
return c.lastSpectrumDataUpdate
}

func (c *Cache) SetRichListLength(richListLength int32) {
c.mutexLock.Lock()
defer c.mutexLock.Unlock()

c.richListLength = richListLength
}

func (c *Cache) GetRichListLength() int32 {
c.mutexLock.RLock()
defer c.mutexLock.RUnlock()

return c.richListLength
}

func (c *Cache) SetRichListPageCount(richListPageCount int32) {
func (c *Cache) SetEpochPaginationData(epoch string, data RichListPaginationData) {
c.mutexLock.Lock()
defer c.mutexLock.Unlock()

c.richListPageCount = richListPageCount
c.richListPaginationDataPerEpoch[epoch] = data
}

func (c *Cache) GetRichListPageCount() int32 {
func (c *Cache) GetEpochPaginationData(epoch string) RichListPaginationData {
c.mutexLock.RLock()
defer c.mutexLock.RUnlock()

return c.richListPageCount
return c.richListPaginationDataPerEpoch[epoch]
}
45 changes: 30 additions & 15 deletions api/cache/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -40,7 +40,9 @@ type Service struct {

func NewCacheService(configuration *ServiceConfiguration, mongoClient *mongo.Client) *Service {
return &Service{
Cache: &Cache{},
Cache: &Cache{
richListPaginationDataPerEpoch: make(map[string]RichListPaginationData),
},

mongoClient: mongoClient,
mongoDatabase: configuration.MongoDatabase,
Expand Down Expand Up @@ -138,24 +140,37 @@ func (s *Service) fetchSpectrumData(ctx context.Context) (SpectrumData, error) {

func (s *Service) calculateRichListCache(ctx context.Context) error {

if s.Cache.GetRichListLength() == 0 || s.Cache.GetRichListPageCount() == 0 {
database := s.mongoClient.Database(s.mongoDatabase)
collections, err := database.ListCollectionNames(ctx, bson.D{})
if err != nil {
return errors.Wrap(err, "getting database collections")
}

epoch := strconv.Itoa(int(s.Cache.GetQubicData().Epoch))
for _, c := range collections {
if strings.Contains(c, "rich_list_") {

collection := s.mongoClient.Database(s.mongoDatabase).Collection(s.mongoRichListCollection + "_" + epoch)
epoch := strings.Split(c, "_")[2]

count, err := collection.CountDocuments(ctx, bson.D{}, options.Count().SetHint("_id_"))
if err != nil {
return errors.Wrap(err, "getting rich list length")
}
s.Cache.SetRichListLength(int32(count))
collection := s.mongoClient.Database(s.mongoDatabase).Collection(c)

count, err := collection.CountDocuments(ctx, bson.D{}, options.Count().SetHint("_id_"))
if err != nil {
return errors.Wrap(err, "getting rich list length")
}

pageCount := int32(count) / s.richListPageSize
remainder := int32(count) % s.richListPageSize
if remainder != 0 {
pageCount += 1
}

data := RichListPaginationData{
RichListLength: int32(count),
RichListPageCount: pageCount,
}
s.Cache.SetEpochPaginationData(epoch, data)

pageCount := int32(count) / s.richListPageSize
remainder := int32(count) % s.richListPageSize
if remainder != 0 {
pageCount += 1
}
s.Cache.SetRichListPageCount(pageCount)
}

return nil
Expand Down
22 changes: 17 additions & 5 deletions api/rpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@ func (s *Server) GetLatestData(_ context.Context, _ *emptypb.Empty) (*protobuff.

func (s *Server) GetRichListSlice(ctx context.Context, request *protobuff.GetRichListSliceRequest) (*protobuff.GetRichListSliceResponse, error) {

lastPage := s.cache.GetRichListPageCount()
totalRecords := s.cache.GetRichListLength()
page := request.Page

if request.Page < 0 || request.Page > lastPage {
if page == 0 {
page = 1
}

data := s.cache.GetEpochPaginationData(request.Epoch)

if data == cache.EmptyPaginationData {
return nil, status.Errorf(codes.NotFound, "could not find the rich list for the specified epoch")
}

lastPage := data.RichListPageCount
totalRecords := data.RichListLength

if page <= 0 || page > lastPage {
return nil, status.Errorf(codes.Internal, "cannot find specified page. last page: %d", lastPage)
}
start := request.Page * s.richListPageSize
start := (page - 1) * s.richListPageSize

collection := s.dbClient.Database(s.mongoDatabase).Collection(s.mongoRichListCollection + "_" + request.Epoch)
findOptions := options.Find().SetSkip(int64(start)).SetLimit(int64(s.richListPageSize)).SetSort(bson.D{{"balance", -1}})
Expand All @@ -92,7 +104,7 @@ func (s *Server) GetRichListSlice(ctx context.Context, request *protobuff.GetRic

return &protobuff.GetRichListSliceResponse{
Pagination: &protobuff.Pagination{
CurrentPage: request.Page,
CurrentPage: page,
TotalPages: lastPage,
TotalRecords: totalRecords,
},
Expand Down

0 comments on commit aadd06d

Please sign in to comment.