-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add handling and test for get all doc channels * Fix lint * Add licensing * Address comments * Add docs * fix yamllint * Add DocumentHistoryMaxEntriesPerChannel test case and fix docs * Denote compacted sequence pairs with ~ * Fix goimports * Fix comment * Fix comment * Add unmarshalJSON handlign for compacted sequence pair
- Loading branch information
1 parent
346b785
commit 70a591b
Showing
7 changed files
with
165 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
docs/api/paths/diagnostic/keyspace-docid-_all_channels.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2022-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. | ||
parameters: | ||
- $ref: ../../components/parameters.yaml#/keyspace | ||
- $ref: ../../components/parameters.yaml#/docid | ||
get: | ||
summary: Get channel history for a document | ||
description: |- | ||
Retrieve all doc channels and the sequence spans showing when the doc was added to a channel and when it was removed. | ||
Required Sync Gateway RBAC roles: | ||
* Sync Gateway Application Read Only | ||
responses: | ||
'200': | ||
description: Document found successfully | ||
content: | ||
application/json: | ||
schema: | ||
additionalProperties: | ||
x-additionalPropertiesName: channel | ||
description: The channels the document has been in. | ||
type: array | ||
items: | ||
sequences: | ||
description: The sequence number that document was added to the channel. | ||
type: string | ||
example: "28-48" | ||
|
||
'404': | ||
$ref: ../../components/responses.yaml#/Not-found | ||
tags: | ||
- Document | ||
operationId: get_keyspace-docid-_all_channels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2024-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 rest | ||
|
||
import ( | ||
"github.com/couchbase/sync_gateway/auth" | ||
"github.com/couchbase/sync_gateway/db" | ||
) | ||
|
||
// HTTP handler for a GET of a document | ||
func (h *handler) handleGetDocChannels() error { | ||
docid := h.PathVar("docid") | ||
|
||
doc, err := h.collection.GetDocument(h.ctx(), docid, db.DocUnmarshalSync) | ||
if err != nil { | ||
return err | ||
} | ||
if doc == nil { | ||
return kNotFoundError | ||
} | ||
resp := make(map[string][]auth.GrantHistorySequencePair, len(doc.Channels)) | ||
|
||
for _, chanSetInfo := range doc.SyncData.ChannelSet { | ||
resp[chanSetInfo.Name] = append(resp[chanSetInfo.Name], auth.GrantHistorySequencePair{StartSeq: chanSetInfo.Start, EndSeq: chanSetInfo.End}) | ||
} | ||
for _, hist := range doc.SyncData.ChannelSetHistory { | ||
resp[hist.Name] = append(resp[hist.Name], auth.GrantHistorySequencePair{StartSeq: hist.Start, EndSeq: hist.End, Compacted: hist.Compacted}) | ||
continue | ||
} | ||
|
||
h.writeJSON(resp) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2024-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 rest | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/couchbase/sync_gateway/db" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetAlldocChannels(t *testing.T) { | ||
rt := NewRestTester(t, &RestTesterConfig{SyncFn: `function(doc) {channel(doc.channel);}`}) | ||
defer rt.Close() | ||
|
||
version := rt.PutDoc("doc", `{"channel":["CHAN1"]}`) | ||
updatedVersion := rt.UpdateDoc("doc", version, `{"channel":["CHAN2"]}`) | ||
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN1"]}`) | ||
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN1", "CHAN2"]}`) | ||
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN3"]}`) | ||
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN1"]}`) | ||
|
||
response := rt.SendDiagnosticRequest("GET", "/{{.keyspace}}/doc/_all_channels", "") | ||
RequireStatus(t, response, http.StatusOK) | ||
|
||
var channelMap map[string][]string | ||
err := json.Unmarshal(response.BodyBytes(), &channelMap) | ||
assert.NoError(t, err) | ||
assert.ElementsMatch(t, channelMap["CHAN1"], []string{"6-0", "1-2", "3-5"}) | ||
assert.ElementsMatch(t, channelMap["CHAN2"], []string{"4-5", "2-3"}) | ||
assert.ElementsMatch(t, channelMap["CHAN3"], []string{"5-6"}) | ||
|
||
for i := 1; i <= 10; i++ { | ||
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{}`) | ||
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN3"]}`) | ||
} | ||
response = rt.SendAdminRequest("GET", "/{{.keyspace}}/doc", "") | ||
RequireStatus(t, response, http.StatusOK) | ||
response = rt.SendDiagnosticRequest("GET", "/{{.keyspace}}/doc/_all_channels", "") | ||
RequireStatus(t, response, http.StatusOK) | ||
|
||
err = json.Unmarshal(response.BodyBytes(), &channelMap) | ||
assert.NoError(t, err) | ||
|
||
// If the channel is still in channel_set, then the total will be 5 entries in history and 1 in channel_set | ||
assert.Equal(t, len(channelMap["CHAN3"]), db.DocumentHistoryMaxEntriesPerChannel+1) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters