2021-01-06 09:35:05 +00:00
|
|
|
## 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
|
2022-02-17 15:00:15 +00:00
|
|
|
./time,
|
2022-02-17 10:00:45 +00:00
|
|
|
nimcrypto/hash,
|
|
|
|
stew/byteutils
|
|
|
|
|
|
|
|
export hash
|
2021-01-06 09:35:05 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Index* = object
|
|
|
|
## This type contains the description of an Index used in the pagination of WakuMessages
|
|
|
|
digest*: MDigest[256]
|
2022-02-17 15:00:15 +00:00
|
|
|
receiverTime*: Timestamp
|
|
|
|
senderTime*: Timestamp # 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
|