diff --git a/tests/v2/test_waku_pagination.nim b/tests/v2/test_waku_pagination.nim index ecc221292..d2ff22b15 100644 --- a/tests/v2/test_waku_pagination.nim +++ b/tests/v2/test_waku_pagination.nim @@ -126,7 +126,7 @@ procSuite "pagination": pagingInfo = PagingInfo(pageSize: MaxPageSize+1, cursor: msgList[3].index, direction: PagingDirection.FORWARD) (data, newPagingInfo) = paginateWithIndex(msgList, pagingInfo) check: - data.len <= MaxPageSize + uint64(data.len) <= MaxPageSize newPagingInfo.direction == pagingInfo.direction newPagingInfo.pageSize <= MaxPageSize @@ -194,13 +194,14 @@ procSuite "pagination": pagingInfo = PagingInfo(pageSize: MaxPageSize+1, cursor: msgList[3].index, direction: PagingDirection.BACKWARD) (data, newPagingInfo) = paginateWithIndex(msgList, pagingInfo) check: - data.len <= MaxPageSize + uint64(data.len) <= MaxPageSize newPagingInfo.direction == pagingInfo.direction newPagingInfo.pageSize <= MaxPageSize # test for a cursor pointing to the begining of the message list pagingInfo = PagingInfo(pageSize: 5, cursor: msgList[0].index, direction: PagingDirection.BACKWARD) (data, newPagingInfo) = paginateWithIndex(msgList, pagingInfo) + check: data.len == 0 newPagingInfo.cursor == msgList[0].index diff --git a/waku/v2/protocol/waku_store/waku_store.nim b/waku/v2/protocol/waku_store/waku_store.nim index 437925158..3e2c60713 100644 --- a/waku/v2/protocol/waku_store/waku_store.nim +++ b/waku/v2/protocol/waku_store/waku_store.nim @@ -96,7 +96,7 @@ proc init*(T: type PagingInfo, buffer: seq[byte]): ProtoResult[T] = var pagingInfo = PagingInfo() let pb = initProtoBuffer(buffer) - var pageSize: uint32 + var pageSize: uint64 discard ? pb.getField(1, pageSize) pagingInfo.pageSize = pageSize @@ -227,7 +227,7 @@ proc paginateWithIndex*(list: seq[IndexedWakuMessage], pinfo: PagingInfo): (seq[ pageSize = pinfo.pageSize dir = pinfo.direction - if pageSize == 0: # pageSize being zero indicates that no pagination is required + if pageSize == uint64(0): # pageSize being zero indicates that no pagination is required return (list, pinfo) if list.len == 0: # no pagination is needed for an empty list @@ -236,45 +236,47 @@ proc paginateWithIndex*(list: seq[IndexedWakuMessage], pinfo: PagingInfo): (seq[ var msgList = list # makes a copy of the list # sorts msgList based on the custom comparison proc indexedWakuMessageComparison msgList.sort(indexedWakuMessageComparison) - + var initQuery = false if cursor == Index(): - initQuery = true # an empty cursor means it is an intial query + initQuery = true # an empty cursor means it is an initial query case dir of PagingDirection.FORWARD: cursor = list[0].index # perform paging from the begining of the list of PagingDirection.BACKWARD: cursor = list[list.len - 1].index # perform paging from the end of the list var foundIndexOption = msgList.findIndex(cursor) + # echo "foundIndexOption", foundIndexOption.get() if foundIndexOption.isNone: # the cursor is not valid return (@[], PagingInfo(pageSize: 0, cursor:pinfo.cursor, direction: pinfo.direction)) - var foundIndex = foundIndexOption.get() - var retrievedPageSize, s, e: int + var foundIndex = uint64(foundIndexOption.get()) + var retrievedPageSize, s, e: uint64 var newCursor: Index # to be returned as part of the new paging info case dir of PagingDirection.FORWARD: # forward pagination - let remainingMessages= msgList.len - foundIndex - 1 + let remainingMessages= uint64(msgList.len) - uint64(foundIndex) - 1 # the number of queried messages cannot exceed the MaxPageSize and the total remaining messages i.e., msgList.len-foundIndex - retrievedPageSize = min(int(pageSize), MaxPageSize).min(remainingMessages) + retrievedPageSize = min(uint64(pageSize), MaxPageSize).min(remainingMessages) if initQuery : foundIndex = foundIndex - 1 s = foundIndex + 1 # non inclusive e = foundIndex + retrievedPageSize newCursor = msgList[e].index # the new cursor points to the end of the page of PagingDirection.BACKWARD: # backward pagination - let remainingMessages=foundIndex + let remainingMessages = foundIndex # the number of queried messages cannot exceed the MaxPageSize and the total remaining messages i.e., foundIndex-0 - retrievedPageSize = min(int(pageSize), MaxPageSize).min(remainingMessages) + retrievedPageSize = min(uint64(pageSize), MaxPageSize).min(remainingMessages) if initQuery : foundIndex = foundIndex + 1 s = foundIndex - retrievedPageSize e = foundIndex - 1 newCursor = msgList[s].index # the new cursor points to the begining of the page + if (retrievedPageSize == 0): + return (@[], PagingInfo(pageSize: 0, cursor:pinfo.cursor, direction: pinfo.direction)) + # retrieve the messages for i in s..e: result[0].add(msgList[i]) - - result[1] = PagingInfo(pageSize : uint64(retrievedPageSize), cursor : newCursor, direction : pinfo.direction) - + result[1] = PagingInfo(pageSize : retrievedPageSize, cursor : newCursor, direction : pinfo.direction) proc paginateWithoutIndex(list: seq[IndexedWakuMessage], pinfo: PagingInfo): (seq[WakuMessage], PagingInfo) = ## takes list, and perfomrs paging based on pinfo diff --git a/waku/v2/protocol/waku_store/waku_store_types.nim b/waku/v2/protocol/waku_store/waku_store_types.nim index 6d09b8845..c5dee2def 100644 --- a/waku/v2/protocol/waku_store/waku_store_types.nim +++ b/waku/v2/protocol/waku_store/waku_store_types.nim @@ -14,7 +14,7 @@ export waku_message export pagination # Constants required for pagination ------------------------------------------- -const MaxPageSize* = 100 # Maximum number of waku messages in each page +const MaxPageSize* = uint64(100) # Maximum number of waku messages in each page type