-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection+rangescan.go
More file actions
146 lines (128 loc) · 3.68 KB
/
collection+rangescan.go
File metadata and controls
146 lines (128 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2025-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rosmar
import (
"context"
"database/sql"
"fmt"
sgbucket "github.com/couchbase/sg-bucket"
)
var _ sgbucket.RangeScanStore = &Collection{}
func (c *Collection) Scan(ctx context.Context, scanType sgbucket.ScanType, opts sgbucket.ScanOptions) (sgbucket.ScanResultIterator, error) {
rs, ok := scanType.(sgbucket.RangeScan)
if !ok {
return nil, fmt.Errorf("unsupported scan type: %T", scanType)
}
return c.rangeScan(ctx, rs, opts)
}
func (c *Collection) rangeScan(ctx context.Context, rs sgbucket.RangeScan, opts sgbucket.ScanOptions) (sgbucket.ScanResultIterator, error) {
var query string
var args []any
if opts.IDsOnly {
query = "SELECT key, cas FROM documents WHERE collection=? AND tombstone=0"
} else {
query = "SELECT key, value, cas FROM documents WHERE collection=? AND tombstone=0"
}
args = append(args, c.id)
if rs.From != nil {
if rs.From.Exclusive {
query += " AND key > ?"
} else {
query += " AND key >= ?"
}
args = append(args, rs.From.Term)
}
if rs.To != nil {
if rs.To.Exclusive {
query += " AND key < ?"
} else {
query += " AND key <= ?"
}
args = append(args, rs.To.Term)
}
// No ORDER BY: gocb's Scan interleaves per-vBucket result streams without a
// global sort, so the sgbucket interface promises no global ordering. Not
// sorting here forces consumers to be order-agnostic and keeps rosmar's
// behavior aligned with CBS.
rows, err := c.db().Query(query, args...)
if err != nil {
return nil, fmt.Errorf("range scan query failed: %w", err)
}
iter := &scanIterator{rows: rows, idsOnly: opts.IDsOnly}
if c.bucket.inMemory {
// An in-memory database has only a single connection, so leaving sql.Rows open
// can cause deadlock. Read all rows eagerly and close immediately.
return preRecordScan(ctx, iter), nil
}
return iter, nil
}
// scanIterator wraps sql.Rows for streaming scan results.
type scanIterator struct {
rows *sql.Rows
idsOnly bool
err error
}
func (it *scanIterator) Next(_ context.Context) *sgbucket.ScanResultItem {
if it.err != nil || !it.rows.Next() {
return nil
}
item := &sgbucket.ScanResultItem{}
if it.idsOnly {
it.err = it.rows.Scan(&item.ID, &item.Cas)
} else {
it.err = it.rows.Scan(&item.ID, &item.Body, &item.Cas)
}
if it.err != nil {
return nil
}
return item
}
func (it *scanIterator) Close(_ context.Context) error {
closeErr := it.rows.Close()
if it.err == nil {
it.err = closeErr
}
if it.err == nil {
it.err = it.rows.Err()
}
return it.err
}
// preRecordedScanIterator holds all scan results in memory.
type preRecordedScanIterator struct {
items []*sgbucket.ScanResultItem
err error
}
func preRecordScan(ctx context.Context, iter *scanIterator) *preRecordedScanIterator {
var items []*sgbucket.ScanResultItem
for {
item := iter.Next(ctx)
if item == nil {
break
}
items = append(items, item)
}
return &preRecordedScanIterator{
items: items,
err: iter.Close(ctx),
}
}
func (it *preRecordedScanIterator) Next(_ context.Context) *sgbucket.ScanResultItem {
if len(it.items) == 0 || it.err != nil {
return nil
}
item := it.items[0]
it.items = it.items[1:]
return item
}
func (it *preRecordedScanIterator) Close(_ context.Context) error {
return it.err
}
var (
_ sgbucket.ScanResultIterator = (*scanIterator)(nil)
_ sgbucket.ScanResultIterator = (*preRecordedScanIterator)(nil)
)