MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / buildChannelsQuery

Method buildChannelsQuery

db/query.go:509–539  ·  view source on GitHub ↗

buildsChannelsQuery constructs the query statement and query parameters for a channels N1QL query. Also used by unit tests to validate query is covering.

(channelName string, startSeq uint64, endSeq uint64, limit int, activeOnly bool)

Source from the content-addressed store, hash-verified

507// buildsChannelsQuery constructs the query statement and query parameters for a channels N1QL query. Also used by unit tests to validate
508// query is covering.
509func (c *DatabaseCollection) buildChannelsQuery(channelName string, startSeq uint64, endSeq uint64, limit int, activeOnly bool) (statement string, params map[string]interface{}) {
510
511 channelQuery := QueryChannels
512 index := sgIndexes[IndexChannels]
513 if channelName == channels.UserStarChannel {
514 channelQuery = QueryStarChannel
515 index = sgIndexes[IndexAllDocs]
516 }
517
518 channelQueryStatement := replaceActiveOnlyFilter(channelQuery.statement, activeOnly)
519 channelQueryStatement = replaceSyncTokensQuery(channelQueryStatement, c.UseXattrs())
520 channelQueryStatement = replaceIndexTokensQuery(channelQueryStatement, index, c.UseXattrs(), c.numIndexPartitions())
521 if limit > 0 {
522 channelQueryStatement = fmt.Sprintf("%s LIMIT %d", channelQueryStatement, limit)
523 }
524
525 // Channel queries use a prepared query
526 params = make(map[string]interface{}, 3)
527 params[QueryParamChannelName] = channelName
528 params[QueryParamStartSeq] = N1QLSafeUint64(startSeq)
529 // If endSeq isn't defined, set to max int64.
530 if endSeq == 0 {
531 endSeq = N1QLMaxInt64
532 } else if endSeq < N1QLMaxInt64 {
533 // channels query isn't based on inclusive end - add one to ensure complete result set
534 endSeq++
535 }
536 params[QueryParamEndSeq] = N1QLSafeUint64(endSeq)
537
538 return channelQueryStatement, params
539}
540
541// N1QL only supports int64 values (https://issues.couchbase.com/browse/MB-24464), so restrict parameter values to this range
542func N1QLSafeUint64(value uint64) uint64 {

Callers 2

QueryChannelsMethod · 0.95
TestCoveringQueriesFunction · 0.80

Calls 6

UseXattrsMethod · 0.95
numIndexPartitionsMethod · 0.95
replaceActiveOnlyFilterFunction · 0.85
replaceSyncTokensQueryFunction · 0.85
replaceIndexTokensQueryFunction · 0.85
N1QLSafeUint64Function · 0.85

Tested by 1

TestCoveringQueriesFunction · 0.64