Skip to content

Commit

Permalink
Replace hex.EncodeTostring with ActorID.key in Text and RichText (yor…
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins authored Oct 5, 2021
1 parent dc3b660 commit 0e71f1e
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 49 deletions.
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (c *Client) Watch(

attachment := c.attachments[docID]
for _, cli := range clients {
attachment.peers[cli.ID.String()] = cli.MetadataInfo
attachment.peers[cli.ID.Hex()] = cli.MetadataInfo
}
}

Expand All @@ -420,12 +420,12 @@ func (c *Client) Watch(
attachment := c.attachments[k.BSONKey()]
if eventType == types.DocumentsWatchedEvent ||
eventType == types.MetadataChangedEvent {
if info, ok := attachment.peers[cli.ID.String()]; ok {
if info, ok := attachment.peers[cli.ID.Hex()]; ok {
cli.MetadataInfo.Update(info)
}
attachment.peers[cli.ID.String()] = cli.MetadataInfo
attachment.peers[cli.ID.Hex()] = cli.MetadataInfo
} else {
delete(attachment.peers, cli.ID.String())
delete(attachment.peers, cli.ID.Hex())
}
}
return &WatchResponse{
Expand Down
8 changes: 4 additions & 4 deletions pkg/document/json/rga_tree_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,13 @@ func (s *RGATreeSplit) deleteNodes(
removedNodeMap := make(map[string]*RGATreeSplitNode)

for _, node := range candidates {
actorIDHex := node.createdAt().ActorIDHex()
actorIDKey := node.createdAt().ActorIDKey()

var latestCreatedAt *time.Ticket
if latestCreatedAtMapByActor == nil {
latestCreatedAt = time.MaxTicket
} else {
createdAt, ok := latestCreatedAtMapByActor[actorIDHex]
createdAt, ok := latestCreatedAtMapByActor[actorIDKey]
if ok {
latestCreatedAt = createdAt
} else {
Expand All @@ -491,10 +491,10 @@ func (s *RGATreeSplit) deleteNodes(

if node.Remove(editedAt, latestCreatedAt) {
s.treeByIndex.Splay(node.indexNode)
latestCreatedAt := createdAtMapByActor[actorIDHex]
latestCreatedAt := createdAtMapByActor[actorIDKey]
createdAt := node.id.createdAt
if latestCreatedAt == nil || createdAt.After(latestCreatedAt) {
createdAtMapByActor[actorIDHex] = createdAt
createdAtMapByActor[actorIDKey] = createdAt
}

removedNodeMap[node.id.key()] = node
Expand Down
10 changes: 5 additions & 5 deletions pkg/document/json/rich_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (t *RichText) Edit(
if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(
"EDIT: '%s' edits %s",
executedAt.ActorID().String(),
executedAt.ActorID().Hex(),
t.rgaTreeSplit.AnnotatedString(),
)
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func (t *RichText) SetStyle(
if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(
"STYL: '%s' styles %s",
executedAt.ActorID().String(),
executedAt.ActorID().Hex(),
t.rgaTreeSplit.AnnotatedString(),
)
}
Expand All @@ -266,13 +266,13 @@ func (t *RichText) Select(
to *RGATreeSplitNodePos,
executedAt *time.Ticket,
) {
if prev, ok := t.selectionMap[executedAt.ActorIDHex()]; !ok || executedAt.After(prev.updatedAt) {
t.selectionMap[executedAt.ActorIDHex()] = newSelection(from, to, executedAt)
if prev, ok := t.selectionMap[executedAt.ActorIDKey()]; !ok || executedAt.After(prev.updatedAt) {
t.selectionMap[executedAt.ActorIDKey()] = newSelection(from, to, executedAt)

if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(
"SELT: '%s' selects %s",
executedAt.ActorID().String(),
executedAt.ActorID().Hex(),
t.rgaTreeSplit.AnnotatedString(),
)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/document/json/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (t *Text) Edit(
if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(
"EDIT: '%s' edits %s",
executedAt.ActorID().String(),
executedAt.ActorID().Hex(),
t.rgaTreeSplit.AnnotatedString(),
)
}
Expand All @@ -195,22 +195,22 @@ func (t *Text) Select(
to *RGATreeSplitNodePos,
executedAt *time.Ticket,
) {
if _, ok := t.selectionMap[executedAt.ActorIDHex()]; !ok {
t.selectionMap[executedAt.ActorIDHex()] = newSelection(from, to, executedAt)
if _, ok := t.selectionMap[executedAt.ActorIDKey()]; !ok {
t.selectionMap[executedAt.ActorIDKey()] = newSelection(from, to, executedAt)
return
}

prevSelection := t.selectionMap[executedAt.ActorIDHex()]
prevSelection := t.selectionMap[executedAt.ActorIDKey()]
if executedAt.After(prevSelection.updatedAt) {
if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(
"SELT: '%s' selects %s",
executedAt.ActorID().String(),
executedAt.ActorID().Hex(),
t.rgaTreeSplit.AnnotatedString(),
)
}

t.selectionMap[executedAt.ActorIDHex()] = newSelection(from, to, executedAt)
t.selectionMap[executedAt.ActorIDKey()] = newSelection(from, to, executedAt)
}
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/document/time/actor_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ func ActorIDFromBytes(bytes []byte) (*ActorID, error) {
return &actorID, nil
}

// String returns the hexadecimal encoding of ActorID.
// Key returns the string of ActorID.
func (id *ActorID) Key() string {
if id == nil {
return ""
}

return string(id[:])
}

// Hex returns the hexadecimal encoding of ActorID.
// If the receiver is nil, it would return empty string.
func (id *ActorID) String() string {
func (id *ActorID) Hex() string {
if id == nil {
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/document/time/actor_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func TestActorID(t *testing.T) {
_, err := rand.Read(actorID[:])
assert.NoError(t, err)

expectedID := actorID.String()
expectedID := actorID.Hex()
actualID, err := time.ActorIDFromHex(expectedID)

assert.NoError(t, err)
assert.Equal(t, expectedID, actualID.String())
assert.Equal(t, expectedID, actualID.Hex())
})

t.Run("get ActorID from hex on invalid hexadecimal string test", func(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions pkg/document/time/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (t *Ticket) AnnotatedString() string {
}

return fmt.Sprintf(
"%d:%d:%s", t.lamport, t.delimiter, t.actorID.String()[22:24],
"%d:%d:%s", t.lamport, t.delimiter, t.actorID.Hex()[22:24],
)
}

Expand All @@ -92,7 +92,7 @@ func (t *Ticket) Key() string {
":" +
strconv.FormatUint(uint64(t.delimiter), 10) +
":" +
t.actorID.String()
t.actorID.Key()
}

// Lamport returns the lamport value.
Expand All @@ -110,9 +110,14 @@ func (t *Ticket) ActorID() *ActorID {
return t.actorID
}

// ActorIDKey returns the actorID's string value.
func (t *Ticket) ActorIDKey() string {
return t.actorID.Key()
}

// ActorIDHex returns the actorID's hex value.
func (t *Ticket) ActorIDHex() string {
return t.actorID.String()
return t.actorID.Hex()
}

// ActorIDBytes returns the actorID's bytes value.
Expand Down
6 changes: 2 additions & 4 deletions pkg/document/time/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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())
assert.Equal(t, actorID.Hex(), ticket.ActorIDHex())
})

t.Run("get bytes from actorId of ticket test", func(t *testing.T) {
Expand All @@ -29,9 +29,7 @@ func TestTicket(t *testing.T) {
assert.Equal(t, uint32(1), ticket.Delimiter())
assert.Equal(t, actorID, ticket.ActorID())

assert.Equal(t, "0:1:"+ticket.ActorIDHex(), ticket.Key())
assert.Equal(t, "0:1:"+ticket.ActorIDHex()[22:24],
ticket.AnnotatedString())
assert.Equal(t, "0:1:"+ticket.ActorIDHex()[22:24], ticket.AnnotatedString())
ticket = ticket.SetActorID(nil)
assert.Equal(t, "0:1:", ticket.Key())
assert.Equal(t, "0:1:nil", ticket.AnnotatedString())
Expand Down
10 changes: 5 additions & 5 deletions test/integration/cluster_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func TestClusterMode(t *testing.T) {
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Metadata{
c1.ID().String(): c1.Metadata(),
c2.ID().String(): c2.Metadata(),
c1.ID().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
},
})

Expand All @@ -173,16 +173,16 @@ func TestClusterMode(t *testing.T) {
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Metadata{
c1.ID().String(): c1.Metadata(),
c2.ID().String(): c2.Metadata(),
c1.ID().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
},
})

// 03. PeersChanged is triggered when another client closes the watch
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Metadata{
c1.ID().String(): c1.Metadata(),
c1.ID().Hex(): c1.Metadata(),
},
})
cancel2()
Expand Down
10 changes: 5 additions & 5 deletions test/integration/peer_awareness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func TestPeerAwareness(t *testing.T) {
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Metadata{
c1.ID().String(): c1.Metadata(),
c2.ID().String(): c2.Metadata(),
c1.ID().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
},
})
watch2Ctx, cancel2 := context.WithCancel(ctx)
Expand All @@ -105,16 +105,16 @@ func TestPeerAwareness(t *testing.T) {
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Metadata{
c1.ID().String(): c1.Metadata(),
c2.ID().String(): c2.Metadata(),
c1.ID().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
},
})

// 03. PeersChanged is triggered when another client closes the watch
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Metadata{
c1.ID().String(): c1.Metadata(),
c1.ID().Hex(): c1.Metadata(),
},
})
cancel2()
Expand Down
4 changes: 2 additions & 2 deletions yorkie/backend/db/change_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ func TestChangeInfo(t *testing.T) {
_, err := rand.Read(actorID[:])
assert.NoError(t, err)

expectedID := actorID.String()
expectedID := actorID.Hex()
changeInfo := db.ChangeInfo{
Actor: db.ID(expectedID),
}

change, err := changeInfo.ToChange()
assert.NoError(t, err)
assert.Equal(t, change.ID().Actor().String(), expectedID)
assert.Equal(t, change.ID().Actor().Hex(), expectedID)
})
}
12 changes: 6 additions & 6 deletions yorkie/backend/sync/memory/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (m *PubSub) Subscribe(
log.Logger.Debugf(
`Subscribe(%s,%s) Start`,
keys[0].BSONKey(),
subscriber.ID.String(),
subscriber.ID.Hex(),
)
}

Expand All @@ -113,7 +113,7 @@ func (m *PubSub) Subscribe(
log.Logger.Debugf(
`Subscribe(%s,%s) End`,
keys[0].BSONKey(),
subscriber.ID.String(),
subscriber.ID.Hex(),
)
}
return sub, nil
Expand Down Expand Up @@ -184,7 +184,7 @@ func (m *PubSub) Publish(
k := docKey.BSONKey()

if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(`Publish(%s,%s) Start`, k, publisherID.String())
log.Logger.Debugf(`Publish(%s,%s) Start`, k, publisherID.Hex())
}

if subs, ok := m.subscriptionsMapByDocKey[k]; ok {
Expand All @@ -197,15 +197,15 @@ func (m *PubSub) Publish(
log.Logger.Debugf(
`Publish(%s,%s) to %s`,
k,
publisherID.String(),
publisherID.Hex(),
sub.SubscriberID(),
)
}
sub.Events() <- event
}
}
if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(`Publish(%s,%s) End`, k, publisherID.String())
log.Logger.Debugf(`Publish(%s,%s) End`, k, publisherID.Hex())
}
}
}
Expand All @@ -218,7 +218,7 @@ func (m *PubSub) UpdateMetadata(
m.subscriptionsMapMu.Lock()
defer m.subscriptionsMapMu.Unlock()

sub, ok := m.subscriptionMapBySubscriber[publisher.ID.String()]
sub, ok := m.subscriptionMapBySubscriber[publisher.ID.Hex()]
if !ok {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion yorkie/backend/sync/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *Subscription) Subscriber() types.Client {

// SubscriberID returns string representation of the subscriber.
func (s *Subscription) SubscriberID() string {
return s.subscriber.ID.String()
return s.subscriber.ID.Hex()
}

// UpdateMetadata updates the metadata of the subscriber.
Expand Down

0 comments on commit 0e71f1e

Please sign in to comment.