Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direction to ListDocuments API #312

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 58 additions & 16 deletions api/admin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/admin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ service Admin {
message ListDocumentsRequest {
string previous_id = 1;
int32 page_size = 2;
bool is_forward = 3;
}

message ListDocumentsResponse {
Expand Down
1 change: 1 addition & 0 deletions yorkie/admin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (s *Server) ListDocuments(
s.backend,
db.ID(req.PreviousId),
int(req.PageSize),
req.IsForward,
)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions yorkie/backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,6 @@ type DB interface {
ctx context.Context,
previousID ID,
pageSize int,
isForward bool,
) ([]*DocInfo, error)
}
29 changes: 24 additions & 5 deletions yorkie/backend/db/memory/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,15 +567,34 @@ func (d *DB) FindDocInfosByPreviousIDAndPageSize(
ctx context.Context,
previousID db.ID,
pageSize int,
isForward bool,
) ([]*db.DocInfo, error) {
txn := d.db.Txn(false)
defer txn.Abort()

iterator, err := txn.LowerBound(
tblDocuments,
"id",
previousID.String(),
)
var iterator memdb.ResultIterator
var err error
if isForward {
iterator, err = txn.LowerBound(
tblDocuments,
"id",
previousID.String(),
)
} else {
if previousID == "" {
iterator, err = txn.GetReverse(
tblDocuments,
"id",
)
} else {
iterator, err = txn.ReverseLowerBound(
tblDocuments,
"id",
previousID.String(),
)
}
}

if err != nil {
return nil, err
}
Expand Down
56 changes: 36 additions & 20 deletions yorkie/backend/db/memory/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,33 +192,49 @@ func TestDB(t *testing.T) {
assert.Equal(t, uint64(1), snapshot.ServerSeq)
})

t.Run("paging docInfo test", func(t *testing.T) {
t.Run("docInfo pagination test", func(t *testing.T) {
localDB, err := memory.New()
assert.NoError(t, err)

clientInfo, _ := localDB.ActivateClient(ctx, t.Name())

var givenKeys []string
for i := 0; i < 11; i++ {
docInfo, err := localDB.FindDocInfoByKey(ctx, clientInfo, fmt.Sprintf("tests$%s-%d", t.Name(), i), true)
assert.NoError(t, err)
givenKeys = append(givenKeys, docInfo.CombinedKey)
assertKeys := func(expectedKeys []string, infos []*db.DocInfo) {
var keys []string
for _, info := range infos {
keys = append(keys, info.CombinedKey)
}
assert.EqualValues(t, expectedKeys, keys)
}

var keys []string
previousID := db.ID("")
for {
docInfos, err := localDB.FindDocInfosByPreviousIDAndPageSize(ctx, previousID, 10)
pageSize := 5
totalSize := 9
clientInfo, _ := localDB.ActivateClient(ctx, t.Name())
for i := 0; i < totalSize; i++ {
_, err := localDB.FindDocInfoByKey(ctx, clientInfo, fmt.Sprintf("%d", i), true)
assert.NoError(t, err)
if len(docInfos) == 0 {
break
}
for _, docInfo := range docInfos {
keys = append(keys, docInfo.CombinedKey)
}
previousID = docInfos[len(docInfos)-1].ID
}

assert.Equal(t, givenKeys, keys)
// initial page, previousID is empty
infos, err := localDB.FindDocInfosByPreviousIDAndPageSize(ctx, "", pageSize, false)
assert.NoError(t, err)
assertKeys([]string{"8", "7", "6", "5", "4"}, infos)

// backward
infos, err = localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[len(infos)-1].ID, pageSize, false)
assert.NoError(t, err)
assertKeys([]string{"3", "2", "1", "0"}, infos)

// backward again
emptyInfos, err := localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[len(infos)-1].ID, pageSize, false)
assert.NoError(t, err)
assertKeys(nil, emptyInfos)

// forward
infos, err = localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[0].ID, pageSize, true)
assert.NoError(t, err)
assertKeys([]string{"4", "5", "6", "7", "8"}, infos)

// forward again
emptyInfos, err = localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[len(infos)-1].ID, pageSize, true)
assert.NoError(t, err)
assertKeys(nil, emptyInfos)
})
}
21 changes: 13 additions & 8 deletions yorkie/backend/db/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,25 +558,30 @@ func (c *Client) FindDocInfosByPreviousIDAndPageSize(
ctx context.Context,
previousID db.ID,
pageSize int,
isForward bool,
) ([]*db.DocInfo, error) {
filter := bson.M{}
if previousID != "" {
encodedPreviousID, err := encodeID(previousID)
if err != nil {
return nil, err
}

key := "$lt"
if isForward {
key = "$gt"
}
filter = bson.M{
"_id": bson.M{
"$gt": encodedPreviousID,
},
"_id": bson.M{key: encodedPreviousID},
}
}

cursor, err := c.collection(colDocuments).Find(
ctx,
filter,
options.Find().SetLimit(int64(pageSize)),
)
opts := options.Find().SetLimit(int64(pageSize))
if !isForward {
opts = opts.SetSort(map[string]int{"_id": -1})
}

cursor, err := c.collection(colDocuments).Find(ctx, filter, opts)
if err != nil {
logging.From(ctx).Error(err)
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion yorkie/documents/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func ListDocumentSummaries(
be *backend.Backend,
previousID db.ID,
pageSize int,
isForward bool,
) ([]*types.DocumentSummary, error) {
docInfo, err := be.DB.FindDocInfosByPreviousIDAndPageSize(ctx, previousID, pageSize)
docInfo, err := be.DB.FindDocInfosByPreviousIDAndPageSize(ctx, previousID, pageSize, isForward)
if err != nil {
return nil, err
}
Expand Down