Skip to content

Commit

Permalink
Merge 747af9f into 8f26979
Browse files Browse the repository at this point in the history
  • Loading branch information
autumn-n authored Jan 31, 2021
2 parents 8f26979 + 747af9f commit 33b8c30
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 276 deletions.
6 changes: 3 additions & 3 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

// FromClient converts the given Protobuf format to model format.
func FromClient(pbClient *api.Client) (*types.Client, error) {
id, err := time.ActorIDFromHex(pbClient.Id)
id, err := time.ActorIDFromBytes(pbClient.Id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func fromChanges(pbChanges []*api.Change) ([]*change.Change, error) {
}

func fromChangeID(id *api.ChangeID) (*change.ID, error) {
actorID, err := time.ActorIDFromHex(id.ActorId)
actorID, err := time.ActorIDFromBytes(id.ActorId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -438,7 +438,7 @@ func fromTimeTicket(pbTicket *api.TimeTicket) (*time.Ticket, error) {
return nil, nil
}

actorID, err := time.ActorIDFromHex(pbTicket.ActorId)
actorID, err := time.ActorIDFromBytes(pbTicket.ActorId)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// ToClient converts the given model to Protobuf format.
func ToClient(client types.Client) *api.Client {
return &api.Client{
Id: client.ID.String(),
Id: client.ID.Bytes(),
Metadata: client.Metadata,
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func toChangeID(id *change.ID) *api.ChangeID {
return &api.ChangeID{
ClientSeq: id.ClientSeq(),
Lamport: id.Lamport(),
ActorId: id.Actor().String(),
ActorId: id.Actor().Bytes(),
}
}

Expand Down Expand Up @@ -367,7 +367,7 @@ func toTimeTicket(ticket *time.Ticket) *api.TimeTicket {
return &api.TimeTicket{
Lamport: ticket.Lamport(),
Delimiter: ticket.Delimiter(),
ActorId: ticket.ActorIDHex(),
ActorId: ticket.ActorIDBytes(),
}
}

Expand Down
510 changes: 266 additions & 244 deletions api/yorkie.pb.go

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions api/yorkie.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,32 @@ message ActivateClientRequest {

message ActivateClientResponse {
string client_key = 1;
string client_id = 2;
bytes client_id = 2;
}

message DeactivateClientRequest {
RequestHeader header = 1;
string client_id = 2;
bytes client_id = 2;
}

message DeactivateClientResponse {
string client_id = 1;
bytes client_id = 1;
}

message AttachDocumentRequest {
RequestHeader header = 1;
string client_id = 2;
bytes client_id = 2;
ChangePack change_pack = 3;
}

message AttachDocumentResponse {
string client_id = 1;
bytes client_id = 1;
ChangePack change_pack = 2;
}

message DetachDocumentRequest {
RequestHeader header = 1;
string client_id = 2;
bytes client_id = 2;
ChangePack change_pack = 3;
}

Expand Down Expand Up @@ -101,12 +101,12 @@ message WatchDocumentsResponse {

message PushPullRequest {
RequestHeader header = 1;
string client_id = 2;
bytes client_id = 2;
ChangePack change_pack = 3;
}

message PushPullResponse {
string client_id = 1;
bytes client_id = 1;
ChangePack change_pack = 2;
}

Expand All @@ -131,7 +131,7 @@ message Change {
message ChangeID {
uint32 client_seq = 1;
uint64 lamport = 2 [jstype = JS_STRING];
string actor_id = 3;
bytes actor_id = 3;
}

message Operation {
Expand Down Expand Up @@ -310,7 +310,7 @@ message TextNodeID {
/////////////////////////////////////////

message Client {
string id = 1;
bytes id = 1;
map<string, string> metadata = 2;
}

Expand All @@ -337,7 +337,7 @@ message TextNodePos {
message TimeTicket {
uint64 lamport = 1 [jstype = JS_STRING];
uint32 delimiter = 2;
string actor_id = 3;
bytes actor_id = 3;
}

enum ValueType {
Expand Down
10 changes: 5 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *Client) Activate(ctx context.Context) error {
return err
}

clientID, err := time.ActorIDFromHex(response.ClientId)
clientID, err := time.ActorIDFromBytes(response.ClientId)
if err != nil {
return err
}
Expand All @@ -196,7 +196,7 @@ func (c *Client) Deactivate(ctx context.Context) error {
}

_, err := c.client.DeactivateClient(ctx, &api.DeactivateClientRequest{
ClientId: c.id.String(),
ClientId: c.id.Bytes(),
})
if err != nil {
log.Logger.Error(err)
Expand All @@ -223,7 +223,7 @@ func (c *Client) Attach(ctx context.Context, doc *document.Document) error {
}

res, err := c.client.AttachDocument(ctx, &api.AttachDocumentRequest{
ClientId: c.id.String(),
ClientId: c.id.Bytes(),
ChangePack: pbChangePack,
})
if err != nil {
Expand Down Expand Up @@ -268,7 +268,7 @@ func (c *Client) Detach(ctx context.Context, doc *document.Document) error {
}

res, err := c.client.DetachDocument(ctx, &api.DetachDocumentRequest{
ClientId: c.id.String(),
ClientId: c.id.Bytes(),
ChangePack: pbChangePack,
})
if err != nil {
Expand Down Expand Up @@ -424,7 +424,7 @@ func (c *Client) sync(ctx context.Context, key *key.Key) error {
}

res, err := c.client.PushPull(ctx, &api.PushPullRequest{
ClientId: c.id.String(),
ClientId: c.id.Bytes(),
ChangePack: pbChangePack,
})
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/document/time/actor_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ func ActorIDFromHex(str string) (*ActorID, error) {
return &actorID, nil
}

// ActorIDFromBytes returns the bytes represented by the bytes of decoded hexadecimal string itself.
func ActorIDFromBytes(bytes []byte) (*ActorID, error) {
if len(bytes) == 0 {
return nil, nil
}

if len(bytes) != actorIDSize {
return nil, fmt.Errorf("bytes length %d: %w", len(bytes), ErrInvalidHexString)
}

actorID := ActorID{}
copy(actorID[:], bytes)
return &actorID, nil
}

// String returns the hexadecimal encoding of ActorID.
// If the receiver is nil, it would return empty string.
func (id *ActorID) String() string {
Expand All @@ -84,6 +99,16 @@ func (id *ActorID) String() string {
return hex.EncodeToString(id[:])
}

// Bytes returns the bytes of ActorID itself.
// If the receiver is nil, it would return empty array of byte.
func (id *ActorID) Bytes() []byte {
if id == nil {
return nil
}

return id[:]
}

// Compare returns an integer comparing two ActorID lexicographically.
// The result will be 0 if id==other, -1 if id < other, and +1 if id > other.
// If the receiver or argument is nil, it would panic at runtime.
Expand Down
18 changes: 18 additions & 0 deletions pkg/document/time/actor_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,22 @@ func TestActorID(t *testing.T) {
_, err = time.ActorIDFromHex(invalidID)
assert.ErrorIs(t, err, time.ErrInvalidHexString)
})

t.Run("get ActorID from bytes test", func(t *testing.T) {
actorID := time.ActorID{}
_, err := rand.Read(actorID[:])
assert.NoError(t, err)

expectedBytes := actorID.Bytes()
actualID, err := time.ActorIDFromBytes(expectedBytes)

assert.NoError(t, err)
assert.Equal(t, expectedBytes, actualID.Bytes())
})

t.Run("get ActorID from bytes on invalid length test", func(t *testing.T) {
invalidBytes := make([]byte, 5)
_, err := time.ActorIDFromBytes(invalidBytes)
assert.ErrorIs(t, err, time.ErrInvalidHexString)
})
}
5 changes: 5 additions & 0 deletions pkg/document/time/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (t *Ticket) ActorIDHex() string {
return t.actorID.String()
}

// ActorIDBytes returns the actorID's bytes value.
func (t *Ticket) ActorIDBytes() []byte {
return t.actorID.Bytes()
}

// After returns whether the given ticket was created later.
func (t *Ticket) After(other *Ticket) bool {
return t.Compare(other) > 0
Expand Down
24 changes: 24 additions & 0 deletions pkg/document/time/ticket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package time_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/yorkie-team/yorkie/pkg/document/time"
)

func TestTicket(t *testing.T) {
t.Run("get hex from actorId of ticket test", func(t *testing.T) {
actorID, _ := time.ActorIDFromHex("0123456789abcdef01234567")
ticket := time.NewTicket(0, 0, actorID)
assert.Equal(t, actorID.String(), ticket.ActorIDHex())
})

t.Run("get bytes from actorId of ticket test", func(t *testing.T) {
hexString := "0123456789abcdef01234567"
actorID, _ := time.ActorIDFromHex(hexString)
ticket := time.NewTicket(0, 0, actorID)
assert.Equal(t, actorID.Bytes(), ticket.ActorIDBytes())
})
}
15 changes: 15 additions & 0 deletions yorkie/backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"encoding/hex"
"errors"

"github.com/yorkie-team/yorkie/pkg/document"
Expand All @@ -28,6 +29,20 @@ func (id ID) String() string {
return string(id)
}

// Bytes returns bytes of decoded hexadecimal string representation of this ID.
func (id ID) Bytes() []byte {
decoded, err := hex.DecodeString(id.String())
if err != nil {
return nil
}
return decoded
}

// IDFromBytes returns ID represented by the encoded hexadecimal string from bytes.
func IDFromBytes(bytes []byte) ID {
return ID(hex.EncodeToString(bytes))
}

// DB represents database which reads or saves Yorkie data.
type DB interface {
// Close all resources of this database.
Expand Down
23 changes: 23 additions & 0 deletions yorkie/backend/db/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package db_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/yorkie-team/yorkie/yorkie/backend/db"
)

func TestID(t *testing.T) {
t.Run("get ID from hex test", func(t *testing.T) {
str := "0123456789abcdef01234567"
ID := db.ID(str)
assert.Equal(t, str, ID.String())
})

t.Run("get ID from bytes test", func(t *testing.T) {
bytes := make([]byte, 12)
ID := db.IDFromBytes(bytes)
assert.Equal(t, bytes, ID.Bytes())
})
}
8 changes: 4 additions & 4 deletions yorkie/clients/client_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ func Activate(
func Deactivate(
ctx context.Context,
be *backend.Backend,
clientID string,
clientID []byte,
) (*db.ClientInfo, error) {
return be.DB.DeactivateClient(ctx, db.ID(clientID))
return be.DB.DeactivateClient(ctx, db.IDFromBytes(clientID))
}

// FindClientAndDocument finds the client and the document.
func FindClientAndDocument(
ctx context.Context,
be *backend.Backend,
clientID string,
clientID []byte,
pack *change.Pack,
createDocIfNotExist bool,
) (*db.ClientInfo, *db.DocInfo, error) {
clientInfo, err := be.DB.FindClientInfoByID(ctx, db.ID(clientID))
clientInfo, err := be.DB.FindClientInfoByID(ctx, db.IDFromBytes(clientID))
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions yorkie/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (s *Server) ActivateClient(

return &api.ActivateClientResponse{
ClientKey: client.Key,
ClientId: client.ID.String(),
ClientId: client.ID.Bytes(),
}, nil
}

Expand All @@ -140,7 +140,7 @@ func (s *Server) DeactivateClient(
ctx context.Context,
req *api.DeactivateClientRequest,
) (*api.DeactivateClientResponse, error) {
if req.ClientId == "" {
if len(req.ClientId) == 0 {
return nil, statusErrorWithDetails(
codes.InvalidArgument,
"invalid client ID",
Expand All @@ -157,7 +157,7 @@ func (s *Server) DeactivateClient(
}

return &api.DeactivateClientResponse{
ClientId: client.ID.String(),
ClientId: client.ID.Bytes(),
}, nil
}

Expand Down
Loading

0 comments on commit 33b8c30

Please sign in to comment.