Skip to content

Commit

Permalink
Change the structure of WatchDocument API (#491)
Browse files Browse the repository at this point in the history
1. Change to use ID instead of Key in WatchDocument
  - By introducing RemoveDocument API, multiple IDs can be mapped to a
    single key(multiple removed IDs and one active ID)
2. Change WatchDocuments API to WatchDocument API
  - In the cluster mode conversion task, one document is mapped to one
    server, and single document mapping is a simplified cluster
  - This can cause Changing the structure where the client maintains
    one WatchDocuments stream to maintain multiple streams.
3. Remove unnecessary types due to the change in step 2
  - The implementation was simplified by changing the structure in
    step 2, so unnecessary data types were cleaned up.

Co-authored-by: hackerwins <hackerwins@gmail.com>
  • Loading branch information
2 people authored and hackerwins committed Mar 17, 2023
1 parent bfdbabf commit dcf4cfb
Show file tree
Hide file tree
Showing 26 changed files with 857 additions and 1,061 deletions.
7 changes: 2 additions & 5 deletions api/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,9 @@ func TestConverter(t *testing.T) {
assert.Equal(t, cli.ID.Bytes(), decodedCli.ID.Bytes())
assert.Equal(t, cli.PresenceInfo, decodedCli.PresenceInfo)

pbClientsMap := converter.ToClientsMap(map[string][]types.Client{
"test": {cli},
})
pbClients := converter.ToClients([]types.Client{cli})

pbCli = pbClientsMap["test"].Clients[0]
decodedCli, err = converter.FromClient(pbCli)
decodedCli, err = converter.FromClient(pbClients[0])
assert.NoError(t, err)
assert.Equal(t, cli.ID.Bytes(), decodedCli.ID.Bytes())
assert.Equal(t, cli.PresenceInfo, decodedCli.PresenceInfo)
Expand Down
38 changes: 27 additions & 11 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,24 @@ func fromChangeID(id *api.ChangeID) (change.ID, error) {
), nil
}

// FromDocumentKeys converts the given Protobuf formats to model format.
func FromDocumentKeys(pbKeys []string) []key.Key {
var keys []key.Key
for _, pbKey := range pbKeys {
keys = append(keys, key.Key(pbKey))
// FromDocumentKey converts the given Protobuf formats to model format.
func FromDocumentKey(pbKey string) (key.Key, error) {
k := key.Key(pbKey)
if err := k.Validate(); err != nil {
return "", err
}
return keys

return k, nil
}

// FromDocumentID converts the given Protobuf formats to model format.
func FromDocumentID(pbID string) (types.ID, error) {
id := types.ID(pbID)
if err := id.Validate(); err != nil {
return "", err
}

return id, nil
}

// FromEventType converts the given Protobuf formats to model format.
Expand Down Expand Up @@ -247,17 +258,22 @@ func FromDocEvent(docEvent *api.DocEvent) (*sync.DocEvent, error) {
return nil, err
}

documentID, err := FromDocumentID(docEvent.DocumentId)
if err != nil {
return nil, err
}

return &sync.DocEvent{
Type: eventType,
Publisher: *client,
DocumentKeys: FromDocumentKeys(docEvent.DocumentKeys),
Type: eventType,
Publisher: *client,
DocumentID: documentID,
}, nil
}

// FromClients converts the given Protobuf formats to model format.
func FromClients(pbClients *api.Clients) ([]*types.Client, error) {
func FromClients(pbClients []*api.Client) ([]*types.Client, error) {
var clients []*types.Client
for _, pbClient := range pbClients.Clients {
for _, pbClient := range pbClients {
client, err := FromClient(pbClient)
if err != nil {
return nil, err
Expand Down
37 changes: 9 additions & 28 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
api "github.com/yorkie-team/yorkie/api/yorkie/v1"
"github.com/yorkie-team/yorkie/pkg/document/change"
"github.com/yorkie-team/yorkie/pkg/document/crdt"
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/pkg/document/operations"
"github.com/yorkie-team/yorkie/pkg/document/time"
"github.com/yorkie-team/yorkie/server/backend/sync"
Expand Down Expand Up @@ -173,31 +172,13 @@ func ToChangeID(id change.ID) *api.ChangeID {
}
}

// ToDocumentKeys converts the given model format to Protobuf format.
func ToDocumentKeys(keys []key.Key) []string {
var keyList []string
for _, k := range keys {
keyList = append(keyList, k.String())
// ToClients converts the given model to Protobuf format.
func ToClients(clients []types.Client) []*api.Client {
var pbClients []*api.Client
for _, client := range clients {
pbClients = append(pbClients, ToClient(client))
}
return keyList
}

// ToClientsMap converts the given model to Protobuf format.
func ToClientsMap(clientsMap map[string][]types.Client) map[string]*api.Clients {
pbClientsMap := make(map[string]*api.Clients)

for k, clients := range clientsMap {
var pbClients []*api.Client
for _, client := range clients {
pbClients = append(pbClients, ToClient(client))
}

pbClientsMap[k] = &api.Clients{
Clients: pbClients,
}
}

return pbClientsMap
return pbClients
}

// ToDocEventType converts the given model format to Protobuf format.
Expand All @@ -224,9 +205,9 @@ func ToDocEvent(docEvent sync.DocEvent) (*api.DocEvent, error) {
}

return &api.DocEvent{
Type: eventType,
Publisher: ToClient(docEvent.Publisher),
DocumentKeys: ToDocumentKeys(docEvent.DocumentKeys),
Type: eventType,
Publisher: ToClient(docEvent.Publisher),
DocumentId: docEvent.DocumentID.String(),
}, nil
}

Expand Down
484 changes: 143 additions & 341 deletions api/yorkie/v1/resources.pb.go

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions api/yorkie/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,6 @@ message Client {
Presence presence = 2;
}

message Clients {
repeated Client clients = 1;
}

message Checkpoint {
int64 server_seq = 1 [jstype = JS_STRING];
uint32 client_seq = 2;
Expand Down Expand Up @@ -299,5 +295,5 @@ enum DocEventType {
message DocEvent {
DocEventType type = 1;
Client publisher = 2;
repeated string document_keys = 3;
string document_id = 3;
}
Loading

0 comments on commit dcf4cfb

Please sign in to comment.