mirror of https://github.com/waku-org/nwaku.git
Change page size type to uint64 (#469)
* changes pageSize to uint64 * WIP * reworks tests and indexing proc to match uint64 pageSize * minor simplification Co-authored-by: Oskar Thorén <ot@oskarthoren.com>
This commit is contained in:
parent
b5f1becace
commit
5f0e6201ed
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -239,23 +239,24 @@ proc paginateWithIndex*(list: seq[IndexedWakuMessage], pinfo: PagingInfo): (seq[
|
|||
|
||||
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
|
||||
|
@ -263,18 +264,19 @@ proc paginateWithIndex*(list: seq[IndexedWakuMessage], pinfo: PagingInfo): (seq[
|
|||
of PagingDirection.BACKWARD: # backward pagination
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue