nwaku/waku/v2/utils/pagination.nim

39 lines
1005 B
Nim
Raw Normal View History

## Contains types and utilities for pagination.
##
## Used by both message store and store protocol.
2021-07-22 08:43:41 +00:00
{.push raises: [Defect].}
2022-02-17 10:00:45 +00:00
import
nimcrypto/hash,
stew/byteutils
export hash
type
Index* = object
## This type contains the description of an Index used in the pagination of WakuMessages
digest*: MDigest[256]
receiverTime*: float64
senderTime*: float64 # the time at which the message is generated
2022-02-17 10:00:45 +00:00
proc `==`*(x, y: Index): bool =
## receiverTime plays no role in index comparison
(x.senderTime == y.senderTime) and (x.digest == y.digest)
proc cmp*(x, y: Index): int =
## compares x and y
## returns 0 if they are equal
## returns -1 if x < y
## returns 1 if x > y
## receiverTime plays no role in index comparison
# Timestamp has a higher priority for comparison
let timecmp = cmp(x.senderTime, y.senderTime)
if timecmp != 0:
return timecmp
# Only when timestamps are equal
let digestcm = cmp(x.digest.data, y.digest.data)
return digestcm