Skip to content

Commit

Permalink
Add include-snapshot flag to ListDocuments API (#575)
Browse files Browse the repository at this point in the history
Co-authored-by: YoonKiJin <[email protected]>
  • Loading branch information
hackerwins and YoonKiJin authored Jul 6, 2023
1 parent fcfe783 commit 278452c
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 84 deletions.
10 changes: 6 additions & 4 deletions admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,16 @@ func (c *Client) ListDocuments(
previousID string,
pageSize int32,
isForward bool,
includeSnapshot bool,
) ([]*types.DocumentSummary, error) {
response, err := c.client.ListDocuments(
ctx,
&api.ListDocumentsRequest{
ProjectName: projectName,
PreviousId: previousID,
PageSize: pageSize,
IsForward: isForward,
ProjectName: projectName,
PreviousId: previousID,
PageSize: pageSize,
IsForward: isForward,
IncludeSnapshot: includeSnapshot,
},
)
if err != nil {
Expand Down
168 changes: 105 additions & 63 deletions api/yorkie/v1/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/yorkie/v1/admin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ message ListDocumentsRequest {
string previous_id = 2;
int32 page_size = 3;
bool is_forward = 4;
bool include_snapshot = 5;
}

message ListDocumentsResponse {
Expand Down
9 changes: 5 additions & 4 deletions cmd/yorkie/document/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
)

var (
previousID string
pageSize int32
isForward bool
previousID string
pageSize int32
isForward bool
includeSnapshot bool
)

func newListCommand() *cobra.Command {
Expand All @@ -58,7 +59,7 @@ func newListCommand() *cobra.Command {
}()

ctx := context.Background()
documents, err := cli.ListDocuments(ctx, projectName, previousID, pageSize, isForward)
documents, err := cli.ListDocuments(ctx, projectName, previousID, pageSize, isForward, includeSnapshot)
if err != nil {
return err
}
Expand Down
32 changes: 19 additions & 13 deletions server/documents/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ListDocumentSummaries(
be *backend.Backend,
project *types.Project,
paging types.Paging[types.ID],
includeSnapshot bool,
) ([]*types.DocumentSummary, error) {
if paging.PageSize > pageSizeLimit {
paging.PageSize = pageSizeLimit
Expand All @@ -61,24 +62,29 @@ func ListDocumentSummaries(

var summaries []*types.DocumentSummary
for _, docInfo := range docInfo {
doc, err := packs.BuildDocumentForServerSeq(ctx, be, docInfo, docInfo.ServerSeq)
if err != nil {
return nil, err
}

snapshot := doc.Marshal()
if len(snapshot) > SnapshotMaxLen {
snapshot = snapshot[:SnapshotMaxLen] + "..."
}

summaries = append(summaries, &types.DocumentSummary{
summary := &types.DocumentSummary{
ID: docInfo.ID,
Key: docInfo.Key,
CreatedAt: docInfo.CreatedAt,
AccessedAt: docInfo.AccessedAt,
UpdatedAt: docInfo.UpdatedAt,
Snapshot: snapshot,
})
}

if includeSnapshot {
doc, err := packs.BuildDocumentForServerSeq(ctx, be, docInfo, docInfo.ServerSeq)
if err != nil {
return nil, err
}

snapshot := doc.Marshal()
if len(snapshot) > SnapshotMaxLen {
snapshot = snapshot[:SnapshotMaxLen] + "..."
}

summary.Snapshot = snapshot
}

summaries = append(summaries, summary)
}

return summaries, nil
Expand Down
1 change: 1 addition & 0 deletions server/rpc/admin_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func (s *adminServer) ListDocuments(
PageSize: int(req.PageSize),
IsForward: req.IsForward,
},
req.IncludeSnapshot,
)
if err != nil {
return nil, err
Expand Down
25 changes: 25 additions & 0 deletions test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,29 @@ func TestDocumentWithProjects(t *testing.T) {
assert.Equal(t, "{\"key\":\"value\"}", d2.Marshal())
assert.Equal(t, "{\"key3\":\"value3\"}", d3.Marshal())
})

clients := activeClients(t, 1)
cli := clients[0]
defer deactivateAndCloseClients(t, clients)

t.Run("includeSnapshot test", func(t *testing.T) {
d1 := document.New(helper.TestDocKey(t))
assert.NoError(t, cli.Attach(ctx, d1))
defer func() { assert.NoError(t, cli.Detach(ctx, d1, false)) }()

assert.NoError(t, d1.Update(func(root *json.Object) error {
root.SetNewArray("testArray")
return nil
}, "add test array"))

assert.NoError(t, cli.Sync(ctx))

docs, err := adminCli.ListDocuments(ctx, "default", "000000000000000000000000", 0, true, false)
assert.NoError(t, err)
assert.Equal(t, "", docs[0].Snapshot)

docs, err = adminCli.ListDocuments(ctx, "default", "000000000000000000000000", 0, true, true)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(docs[0].Snapshot))
})
}

0 comments on commit 278452c

Please sign in to comment.