2020-01-12 22:55:52 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-01-16 23:48:59 +00:00
|
|
|
package postgreskv
|
2020-01-12 22:55:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-01-19 13:42:08 +00:00
|
|
|
"storj.io/storj/private/tagsql"
|
2020-01-12 22:55:52 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
2020-01-16 23:48:59 +00:00
|
|
|
type orderedPostgresIterator struct {
|
2020-01-12 22:55:52 +00:00
|
|
|
client *Client
|
|
|
|
opts *storage.IterateOptions
|
|
|
|
delimiter byte
|
|
|
|
batchSize int
|
|
|
|
curIndex int
|
2020-01-19 13:42:08 +00:00
|
|
|
curRows tagsql.Rows
|
2020-01-12 22:55:52 +00:00
|
|
|
skipPrefix bool
|
|
|
|
lastKeySeen storage.Key
|
|
|
|
largestKey storage.Key
|
|
|
|
errEncountered error
|
|
|
|
}
|
|
|
|
|
2020-01-19 19:56:51 +00:00
|
|
|
func newOrderedPostgresIterator(ctx context.Context, cli *Client, opts storage.IterateOptions) (_ *orderedPostgresIterator, err error) {
|
2020-01-12 22:55:52 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
if opts.Prefix == nil {
|
|
|
|
opts.Prefix = storage.Key("")
|
|
|
|
}
|
|
|
|
if opts.First == nil {
|
|
|
|
opts.First = storage.Key("")
|
|
|
|
}
|
|
|
|
if opts.First.Less(opts.Prefix) {
|
|
|
|
opts.First = opts.Prefix
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:48:59 +00:00
|
|
|
opi := &orderedPostgresIterator{
|
2020-01-12 22:55:52 +00:00
|
|
|
client: cli,
|
|
|
|
opts: &opts,
|
|
|
|
delimiter: byte('/'),
|
2020-01-19 19:56:51 +00:00
|
|
|
batchSize: opts.Limit,
|
2020-01-12 22:55:52 +00:00
|
|
|
curIndex: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(opts.Prefix) > 0 {
|
|
|
|
opi.largestKey = storage.AfterPrefix(opts.Prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
newRows, err := opi.doNextQuery(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opi.curRows = newRows
|
|
|
|
|
|
|
|
return opi, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:48:59 +00:00
|
|
|
func (opi *orderedPostgresIterator) Close() error {
|
2020-01-28 01:39:49 +00:00
|
|
|
defer mon.Task()(nil)(nil)
|
|
|
|
|
2020-01-19 13:42:08 +00:00
|
|
|
return errs.Combine(opi.curRows.Err(), opi.errEncountered, opi.curRows.Close())
|
2020-01-12 22:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next fills in info for the next item in an ongoing listing.
|
2020-01-16 23:48:59 +00:00
|
|
|
func (opi *orderedPostgresIterator) Next(ctx context.Context, item *storage.ListItem) bool {
|
2020-01-12 22:55:52 +00:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
|
|
|
|
for {
|
2020-05-05 21:05:57 +01:00
|
|
|
for {
|
|
|
|
nextTask := mon.TaskNamed("check_next_row")(nil)
|
|
|
|
next := opi.curRows.Next()
|
|
|
|
nextTask(nil)
|
|
|
|
if next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-01-28 01:39:49 +00:00
|
|
|
result := func() bool {
|
|
|
|
defer mon.TaskNamed("acquire_new_query")(nil)(nil)
|
|
|
|
|
|
|
|
if err := opi.curRows.Err(); err != nil && err != sql.ErrNoRows {
|
|
|
|
opi.errEncountered = errs.Wrap(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err := opi.curRows.Close(); err != nil {
|
|
|
|
opi.errEncountered = errs.Wrap(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if opi.curIndex < opi.batchSize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newRows, err := opi.doNextQuery(ctx)
|
|
|
|
if err != nil {
|
|
|
|
opi.errEncountered = errs.Wrap(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opi.curRows = newRows
|
|
|
|
opi.curIndex = 0
|
|
|
|
return true
|
|
|
|
}()
|
|
|
|
if !result {
|
|
|
|
return result
|
2020-01-12 22:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var k, v []byte
|
2020-01-28 01:39:49 +00:00
|
|
|
scanTask := mon.TaskNamed("scan_next_row")(nil)
|
2020-01-12 22:55:52 +00:00
|
|
|
err := opi.curRows.Scan(&k, &v)
|
2020-01-28 01:39:49 +00:00
|
|
|
scanTask(&err)
|
2020-01-12 22:55:52 +00:00
|
|
|
if err != nil {
|
|
|
|
opi.errEncountered = errs.Wrap(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
opi.curIndex++
|
|
|
|
|
|
|
|
if !bytes.HasPrefix(k, []byte(opi.opts.Prefix)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
item.Key = storage.Key(k)
|
|
|
|
item.Value = storage.Value(v)
|
|
|
|
item.IsPrefix = false
|
|
|
|
|
|
|
|
if !opi.opts.Recurse {
|
|
|
|
if idx := bytes.IndexByte(item.Key[len(opi.opts.Prefix):], opi.delimiter); idx >= 0 {
|
|
|
|
item.Key = item.Key[:len(opi.opts.Prefix)+idx+1]
|
|
|
|
item.Value = nil
|
|
|
|
item.IsPrefix = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opi.lastKeySeen.Equal(item.Key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
opi.skipPrefix = item.IsPrefix
|
|
|
|
opi.lastKeySeen = item.Key
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 13:42:08 +00:00
|
|
|
func (opi *orderedPostgresIterator) doNextQuery(ctx context.Context) (_ tagsql.Rows, err error) {
|
2020-01-12 22:55:52 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
gt := ">"
|
|
|
|
start := opi.lastKeySeen
|
|
|
|
|
|
|
|
if len(start) == 0 {
|
|
|
|
start = opi.opts.First
|
|
|
|
gt = ">="
|
|
|
|
} else if opi.skipPrefix {
|
|
|
|
start = storage.AfterPrefix(start)
|
|
|
|
gt = ">="
|
|
|
|
}
|
2020-06-28 04:56:29 +01:00
|
|
|
var endLimitKey = opi.largestKey
|
|
|
|
if endLimitKey == nil {
|
|
|
|
// jackc/pgx will treat nil as a NULL value, while lib/pq treats it as
|
|
|
|
// an empty string. We'll remove the ambiguity by making it a zero-length
|
|
|
|
// byte slice instead
|
|
|
|
endLimitKey = []byte{}
|
|
|
|
}
|
2020-01-12 22:55:52 +00:00
|
|
|
|
2020-01-17 20:07:00 +00:00
|
|
|
return opi.client.db.Query(ctx, fmt.Sprintf(`
|
2020-01-12 22:55:52 +00:00
|
|
|
SELECT pd.fullpath, pd.metadata
|
|
|
|
FROM pathdata pd
|
|
|
|
WHERE pd.fullpath %s $1::BYTEA
|
|
|
|
AND ($2::BYTEA = ''::BYTEA OR pd.fullpath < $2::BYTEA)
|
|
|
|
ORDER BY pd.fullpath
|
|
|
|
LIMIT $3
|
2020-06-28 04:56:29 +01:00
|
|
|
`, gt), start, endLimitKey, opi.batchSize)
|
2020-01-12 22:55:52 +00:00
|
|
|
}
|