Skip to content

Commit fee7c13

Browse files
committed
changes to api
- changed the endpoint for GET and POST requests - changed the request body and response for POST request - changed the resposne for the GET request - updated the OPEN API spec
1 parent 055abff commit fee7c13

7 files changed

Lines changed: 95 additions & 110 deletions

File tree

db/crud.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,54 +222,54 @@ func (c *DatabaseCollection) GetDocSyncData(ctx context.Context, docid string) (
222222

223223
}
224224

225-
type ChannelHistoryResp map[string]map[uint64]bool
226-
227-
func (ch ChannelHistoryResp) AddChannelHistoryEntry(name string, seq uint64) {
228-
if _, ok := ch[name]; !ok {
229-
ch[name] = make(map[uint64]bool)
230-
}
231-
if _, ok := ch[name][seq]; !ok {
232-
ch[name][seq] = true
233-
}
234-
}
235-
236225
// GetDocChannelHistory returns the channel revocation history for the given document as a map
237226
// from channel name to the sequences at which the document was removed from that channel.
238227
// It collects revocation sequences from the active Channels map, the ChannelSet, and the
239228
// ChannelSetHistory (overflow). Only channels that have been revoked at least once appear in
240229
// the result; active memberships with no revocation history are omitted, even though a currently
241230
// assigned channel can still appear if it was revoked and later re-added.
242-
func (c *DatabaseCollection) GetDocChannelHistory(ctx context.Context, docid string) (map[string][]uint64, error) {
231+
func (c *DatabaseCollection) GetDocChannelHistory(ctx context.Context, docid string) (map[string]uint64, error) {
243232

244-
chanHistory := make(ChannelHistoryResp)
233+
chanHistory := make(map[string]uint64)
245234
syncData, err := c.GetDocSyncData(ctx, docid)
246235
if err != nil {
247236
return nil, err
248237
}
249238
for chanName, chanVal := range syncData.Channels {
250239
if chanVal != nil {
251-
chanHistory.AddChannelHistoryEntry(chanName, chanVal.Seq)
240+
if _, ok := chanHistory[chanName]; !ok {
241+
chanHistory[chanName] = chanVal.Seq
242+
continue
243+
}
244+
if chanVal.Seq > chanHistory[chanName] {
245+
chanHistory[chanName] = chanVal.Seq
246+
}
252247
}
253248
}
254249
for _, chanSetEntry := range syncData.ChannelSet {
255250
if chanSetEntry.End != 0 {
256-
chanHistory.AddChannelHistoryEntry(chanSetEntry.Name, chanSetEntry.End)
251+
if _, ok := chanHistory[chanSetEntry.Name]; !ok {
252+
chanHistory[chanSetEntry.Name] = chanSetEntry.End
253+
continue
254+
}
255+
if chanSetEntry.End > chanHistory[chanSetEntry.Name] {
256+
chanHistory[chanSetEntry.Name] = chanSetEntry.End
257+
}
257258
}
258259
}
259260
for _, chanSetEntry := range syncData.ChannelSetHistory {
260261
if chanSetEntry.End != 0 {
261-
chanHistory.AddChannelHistoryEntry(chanSetEntry.Name, chanSetEntry.End)
262+
if _, ok := chanHistory[chanSetEntry.Name]; !ok {
263+
chanHistory[chanSetEntry.Name] = chanSetEntry.End
264+
continue
265+
}
266+
if chanSetEntry.End > chanHistory[chanSetEntry.Name] {
267+
chanHistory[chanSetEntry.Name] = chanSetEntry.End
268+
}
262269
}
263270
}
264271

265-
result := make(map[string][]uint64)
266-
for chanName, chanEntry := range chanHistory {
267-
result[chanName] = make([]uint64, 0)
268-
for seq, _ := range chanEntry {
269-
result[chanName] = append(result[chanName], seq)
270-
}
271-
}
272-
return result, nil
272+
return chanHistory, nil
273273
}
274274

275275
// CompactDocChannelHistory removes channel history entries that ended at or before the given sequence number.

docs/api/admin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ paths:
121121
$ref: './paths/admin/keyspace-_purge.yaml'
122122
'/{keyspace}/_channel_history/{docid}':
123123
$ref: './paths/admin/keyspace-_history.yaml'
124-
'/{keyspace}/_channel_history/compact':
124+
'/{keyspace}/_channel_history/{docid}/compact':
125125
$ref: './paths/admin/keyspace-_history-compact.yaml'
126126
'/{db}/_flush':
127127
$ref: './paths/admin/db-_flush.yaml'

docs/api/paths/admin/keyspace-_history-compact.yaml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# the file licenses/APL2.txt.
88
parameters:
99
- $ref: ../../components/parameters.yaml#/keyspace
10+
- $ref: ../../components/parameters.yaml#/docid
1011
post:
1112
summary: Compact Channel History of Document
1213
description: |-
@@ -25,18 +26,8 @@ post:
2526
schema:
2627
type: object
2728
required:
28-
- doc_ids
2929
- seq
3030
properties:
31-
doc_ids:
32-
description: |-
33-
List of document IDs whose inactive channels should be compacted.
34-
type: array
35-
items:
36-
type: string
37-
example:
38-
- doc1
39-
- doc2
4031
seq:
4132
description: |-
4233
Channel history for inactive channels having end sequences earlier than this sequence will be removed from the specified document's metadata.
@@ -64,14 +55,11 @@ post:
6455
description: |-
6556
Array of channel names that were compacted.
6657
description: |-
67-
A mapping of document IDs (keys) to arrays of compacted channels (values).
58+
A array of all the compacted channels
6859
example:
69-
doc1:
60+
compacted_channels:
7061
- channel1
7162
- channel2
72-
doc2:
73-
- channel2
74-
- channel3
7563
'400':
7664
description: 'Bad request. This could be due to invalid request parameters such as missing or malformed doc_ids array or invalid seq value.'
7765
content:

docs/api/paths/admin/keyspace-_history.yaml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ get:
1212
summary: Get Channel History of Document
1313
description: |-
1414
Returns the channel revocation history for the specified document as a map of channel
15-
names to the sequences at which the document was removed from each channel. Only channels
15+
names to the sequence at which the document was removed from each channel. Only channels
1616
that have been revoked at least once are included; channels the document is currently
1717
assigned to are excluded.
1818
@@ -24,26 +24,21 @@ get:
2424
'200':
2525
description: |-
2626
Successfully retrieved the channel revocation history for the specified document.
27-
Returns a JSON object mapping each channel name to an array of sequences at which
27+
Returns a JSON object mapping each channel name to the sequence at which
2828
the document was removed from that channel.
2929
content:
3030
application/json:
3131
schema:
3232
type: object
3333
additionalProperties:
34-
type: array
35-
items:
36-
type: integer
37-
format: int64
38-
minimum: 0
34+
type: integer
35+
format: int64
36+
minimum: 0
3937
description: Sequences at which the document was removed from this channel.
4038
description: Map of channel names to their revocation sequences.
4139
example:
42-
channel1:
43-
- 3
44-
- 7
45-
channel2:
46-
- 5
40+
channel1: 7
41+
channel2: 5
4742
'404':
4843
description: Document not found.
4944
content:

rest/api_test.go

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4121,46 +4121,45 @@ func TestDocumentChannelHistoryCompact(t *testing.T) {
41214121
version = rt.UpdateDoc("doc12", version, `{"channels": ["a"]}`)
41224122
_ = rt.UpdateDoc("doc12", version, `{"channels": ["a", "c"]}`)
41234123
req := CompactDocChannelHistoryRequest{
4124-
DocIds: []string{"doc11", "doc12"},
4125-
Seq: 999999,
4124+
Seq: 999999,
41264125
}
41274126

41284127
bodyBytes, err := base.JSONMarshal(req)
41294128
require.NoError(t, err)
4130-
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/_compact", string(bodyBytes))
4129+
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/doc11/compact", string(bodyBytes))
41314130
RequireStatus(t, resp, http.StatusOK)
41324131

4133-
var chanOutput map[string][]string
4134-
err = base.JSONUnmarshal(resp.Body.Bytes(), &chanOutput)
4132+
var chanOutput1 map[string][]string
4133+
err = base.JSONUnmarshal(resp.Body.Bytes(), &chanOutput1)
41354134
require.NoError(t, err)
41364135

4137-
expectedOutput := map[string][]string{
4138-
"doc11": []string{"test"},
4139-
"doc12": []string{"b"},
4136+
expectedOutput1 := map[string][]string{
4137+
"compacted_channels": []string{"test"},
41404138
}
41414139

4142-
assert.Equal(t, expectedOutput, chanOutput)
4143-
})
4140+
assert.Equal(t, expectedOutput1, chanOutput1)
41444141

4145-
t.Run("empty doc_ids returns 400", func(t *testing.T) {
4146-
req := CompactDocChannelHistoryRequest{
4147-
DocIds: []string{},
4148-
Seq: 1,
4149-
}
4150-
bodyBytes, err := base.JSONMarshal(req)
4142+
resp2 := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/doc12/compact", string(bodyBytes))
4143+
RequireStatus(t, resp2, http.StatusOK)
4144+
4145+
var chanOutput2 map[string][]string
4146+
err = base.JSONUnmarshal(resp2.Body.Bytes(), &chanOutput2)
41514147
require.NoError(t, err)
4152-
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/_compact", string(bodyBytes))
4153-
RequireStatus(t, resp, http.StatusBadRequest)
4148+
4149+
expectedOutput2 := map[string][]string{
4150+
"compacted_channels": []string{"b"},
4151+
}
4152+
4153+
assert.Equal(t, expectedOutput2, chanOutput2)
41544154
})
41554155

41564156
t.Run("seq zero returns 400", func(t *testing.T) {
41574157
req := CompactDocChannelHistoryRequest{
4158-
DocIds: []string{"doc1"},
4159-
Seq: 0,
4158+
Seq: 0,
41604159
}
41614160
bodyBytes, err := base.JSONMarshal(req)
41624161
require.NoError(t, err)
4163-
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/_compact", string(bodyBytes))
4162+
resp := rt.SendAdminRequest("POST", "/{{.keyspace}}/_channel_history/doc1/compact", string(bodyBytes))
41644163
RequireStatus(t, resp, http.StatusBadRequest)
41654164
})
41664165
}
@@ -4271,7 +4270,6 @@ func TestGetDocChannelHistory(t *testing.T) {
42714270
version := rt.PutDoc("doc1", `{"channels": ["chan1"]}`)
42724271

42734272
version = rt.UpdateDoc("doc1", version, `{"channels": []}`)
4274-
chanRevocationSeq1 := rt.GetDocumentSequence("doc1")
42754273

42764274
version = rt.UpdateDoc("doc1", version, `{"channels": ["chan1","chan2"]}`)
42774275

@@ -4280,25 +4278,25 @@ func TestGetDocChannelHistory(t *testing.T) {
42804278

42814279
rt.UpdateDoc("doc1", version, `{"channels": ["chan3","chan2"]}`)
42824280

4283-
expectedChanHistory := map[string][]uint64{
4284-
"chan1": {chanRevocationSeq2, chanRevocationSeq1},
4285-
"chan2": {chanRevocationSeq2},
4281+
expectedChanHistory := map[string]uint64{
4282+
"chan1": chanRevocationSeq2,
4283+
"chan2": chanRevocationSeq2,
42864284
}
42874285

42884286
chanHistory, err := collection.GetDocChannelHistory(ctx, "doc1")
42894287
require.NoError(t, err)
42904288
require.Len(t, chanHistory, len(expectedChanHistory))
4291-
for chanName, expectedSeqs := range expectedChanHistory {
4292-
assert.ElementsMatch(t, expectedSeqs, chanHistory[chanName])
4289+
for chanName, expectedSeq := range expectedChanHistory {
4290+
assert.Equal(t, expectedSeq, chanHistory[chanName])
42934291
}
42944292

42954293
resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/_channel_history/doc1", "")
42964294
RequireStatus(t, resp, http.StatusOK)
4297-
var apiResult map[string][]uint64
4295+
var apiResult map[string]uint64
42984296
require.NoError(t, json.Unmarshal(resp.BodyBytes(), &apiResult))
42994297
require.Len(t, apiResult, len(expectedChanHistory))
4300-
for chanName, expectedSeqs := range expectedChanHistory {
4301-
assert.ElementsMatch(t, expectedSeqs, apiResult[chanName])
4298+
for chanName, expectedSeq := range expectedChanHistory {
4299+
assert.Equal(t, expectedSeq, apiResult[chanName])
43024300
}
43034301
})
43044302

@@ -4329,15 +4327,15 @@ func TestGetDocChannelHistory(t *testing.T) {
43294327
version := rt.PutDoc("doc3", `{"channels": ["chan1"]}`)
43304328
rt.UpdateDoc("doc3", version, `{"channels": []}`)
43314329
revocationSeq := rt.GetDocumentSequence("doc3")
4332-
expected := map[string][]uint64{"chan1": {revocationSeq}}
4330+
expected := map[string]uint64{"chan1": revocationSeq}
43334331

43344332
chanHistory, err := collection.GetDocChannelHistory(ctx, "doc3")
43354333
require.NoError(t, err)
43364334
assert.Equal(t, expected, chanHistory)
43374335

43384336
resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/_channel_history/doc3", "")
43394337
RequireStatus(t, resp, http.StatusOK)
4340-
var apiResult map[string][]uint64
4338+
var apiResult map[string]uint64
43414339
require.NoError(t, json.Unmarshal(resp.BodyBytes(), &apiResult))
43424340
assert.Equal(t, expected, apiResult)
43434341
})
@@ -4346,10 +4344,10 @@ func TestGetDocChannelHistory(t *testing.T) {
43464344
version := rt.PutDoc("doc4", `{"channels": ["chan1", "chan2", "chan3"]}`)
43474345
rt.UpdateDoc("doc4", version, `{"channels": []}`)
43484346
revocationSeq := rt.GetDocumentSequence("doc4")
4349-
expected := map[string][]uint64{
4350-
"chan1": {revocationSeq},
4351-
"chan2": {revocationSeq},
4352-
"chan3": {revocationSeq},
4347+
expected := map[string]uint64{
4348+
"chan1": revocationSeq,
4349+
"chan2": revocationSeq,
4350+
"chan3": revocationSeq,
43534351
}
43544352

43554353
chanHistory, err := collection.GetDocChannelHistory(ctx, "doc4")
@@ -4358,7 +4356,7 @@ func TestGetDocChannelHistory(t *testing.T) {
43584356

43594357
resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/_channel_history/doc4", "")
43604358
RequireStatus(t, resp, http.StatusOK)
4361-
var apiResult map[string][]uint64
4359+
var apiResult map[string]uint64
43624360
require.NoError(t, json.Unmarshal(resp.BodyBytes(), &apiResult))
43634361
assert.Equal(t, expected, apiResult)
43644362
})
@@ -4378,7 +4376,7 @@ func TestGetDocChannelHistory(t *testing.T) {
43784376

43794377
resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/_channel_history/doc5", "")
43804378
RequireStatus(t, resp, http.StatusOK)
4381-
var apiResult map[string][]uint64
4379+
var apiResult map[string]uint64
43824380
require.NoError(t, json.Unmarshal(resp.BodyBytes(), &apiResult))
43834381
assert.NotEmpty(t, apiResult["chan1"])
43844382
})
@@ -4388,7 +4386,7 @@ func TestGetDocChannelHistory(t *testing.T) {
43884386
version := rt.PutDoc("doc6", `{"channels": ["chan1", "chan2"]}`)
43894387
rt.UpdateDoc("doc6", version, `{"channels": ["chan2"]}`)
43904388
revocationSeq := rt.GetDocumentSequence("doc6")
4391-
expected := map[string][]uint64{"chan1": {revocationSeq}}
4389+
expected := map[string]uint64{"chan1": revocationSeq}
43924390

43934391
chanHistory, err := collection.GetDocChannelHistory(ctx, "doc6")
43944392
require.NoError(t, err)
@@ -4397,7 +4395,7 @@ func TestGetDocChannelHistory(t *testing.T) {
43974395

43984396
resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/_channel_history/doc6", "")
43994397
RequireStatus(t, resp, http.StatusOK)
4400-
var apiResult map[string][]uint64
4398+
var apiResult map[string]uint64
44014399
require.NoError(t, json.Unmarshal(resp.BodyBytes(), &apiResult))
44024400
assert.Equal(t, expected, apiResult)
44034401
})
@@ -4409,15 +4407,15 @@ func TestGetDocChannelHistory(t *testing.T) {
44094407

44104408
// Re-add chan1 — it should still appear in history from the earlier revocation
44114409
rt.UpdateDoc("doc8", version, `{"channels": ["chan1"]}`)
4412-
expected := map[string][]uint64{"chan1": {revocationSeq}}
4410+
expected := map[string]uint64{"chan1": revocationSeq}
44134411

44144412
chanHistory, err := collection.GetDocChannelHistory(ctx, "doc8")
44154413
require.NoError(t, err)
44164414
assert.Equal(t, expected, chanHistory)
44174415

44184416
resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/_channel_history/doc8", "")
44194417
RequireStatus(t, resp, http.StatusOK)
4420-
var apiResult map[string][]uint64
4418+
var apiResult map[string]uint64
44214419
require.NoError(t, json.Unmarshal(resp.BodyBytes(), &apiResult))
44224420
assert.Equal(t, expected, apiResult)
44234421
})

0 commit comments

Comments
 (0)