Skip to content

Commit

Permalink
Revert "Replace hex.EncodeTostring with ActorID.key in Text and RichT…
Browse files Browse the repository at this point in the history
…ext (#255)"

This reverts commit 0e71f1e.
  • Loading branch information
hackerwins committed Oct 21, 2021
1 parent bc4b40a commit d685706
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 61 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.Hex()] = cli.MetadataInfo
attachment.peers[cli.ID.String()] = 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.Hex()]; ok {
if info, ok := attachment.peers[cli.ID.String()]; ok {
cli.MetadataInfo.Update(info)
}
attachment.peers[cli.ID.Hex()] = cli.MetadataInfo
attachment.peers[cli.ID.String()] = cli.MetadataInfo
} else {
delete(attachment.peers, cli.ID.Hex())
delete(attachment.peers, cli.ID.String())
}
}
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 {
actorIDKey := node.createdAt().ActorIDKey()
actorIDHex := node.createdAt().ActorIDHex()

var latestCreatedAt *time.Ticket
if latestCreatedAtMapByActor == nil {
latestCreatedAt = time.MaxTicket
} else {
createdAt, ok := latestCreatedAtMapByActor[actorIDKey]
createdAt, ok := latestCreatedAtMapByActor[actorIDHex]
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[actorIDKey]
latestCreatedAt := createdAtMapByActor[actorIDHex]
createdAt := node.id.createdAt
if latestCreatedAt == nil || createdAt.After(latestCreatedAt) {
createdAtMapByActor[actorIDKey] = createdAt
createdAtMapByActor[actorIDHex] = 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().Hex(),
executedAt.ActorID().String(),
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().Hex(),
executedAt.ActorID().String(),
t.rgaTreeSplit.AnnotatedString(),
)
}
Expand All @@ -266,13 +266,13 @@ func (t *RichText) Select(
to *RGATreeSplitNodePos,
executedAt *time.Ticket,
) {
if prev, ok := t.selectionMap[executedAt.ActorIDKey()]; !ok || executedAt.After(prev.updatedAt) {
t.selectionMap[executedAt.ActorIDKey()] = newSelection(from, to, executedAt)
if prev, ok := t.selectionMap[executedAt.ActorIDHex()]; !ok || executedAt.After(prev.updatedAt) {
t.selectionMap[executedAt.ActorIDHex()] = newSelection(from, to, executedAt)

if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(
"SELT: '%s' selects %s",
executedAt.ActorID().Hex(),
executedAt.ActorID().String(),
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().Hex(),
executedAt.ActorID().String(),
t.rgaTreeSplit.AnnotatedString(),
)
}
Expand All @@ -195,22 +195,22 @@ func (t *Text) Select(
to *RGATreeSplitNodePos,
executedAt *time.Ticket,
) {
if _, ok := t.selectionMap[executedAt.ActorIDKey()]; !ok {
t.selectionMap[executedAt.ActorIDKey()] = newSelection(from, to, executedAt)
if _, ok := t.selectionMap[executedAt.ActorIDHex()]; !ok {
t.selectionMap[executedAt.ActorIDHex()] = newSelection(from, to, executedAt)
return
}

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

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

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

// 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.
// String returns the hexadecimal encoding of ActorID.
// If the receiver is nil, it would return empty string.
func (id *ActorID) Hex() string {
func (id *ActorID) String() 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.Hex()
expectedID := actorID.String()
actualID, err := time.ActorIDFromHex(expectedID)

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

t.Run("get ActorID from hex on invalid hexadecimal string test", func(t *testing.T) {
Expand Down
11 changes: 3 additions & 8 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.Hex()[22:24],
"%d:%d:%s", t.lamport, t.delimiter, t.actorID.String()[22:24],
)
}

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

// Lamport returns the lamport value.
Expand All @@ -110,14 +110,9 @@ 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.Hex()
return t.actorID.String()
}

// ActorIDBytes returns the actorID's bytes value.
Expand Down
6 changes: 4 additions & 2 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.Hex(), ticket.ActorIDHex())
assert.Equal(t, actorID.String(), ticket.ActorIDHex())
})

t.Run("get bytes from actorId of ticket test", func(t *testing.T) {
Expand All @@ -29,7 +29,9 @@ 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()[22:24], ticket.AnnotatedString())
assert.Equal(t, "0:1:"+ticket.ActorIDHex(), ticket.Key())
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().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
c1.ID().String(): c1.Metadata(),
c2.ID().String(): 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().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
c1.ID().String(): c1.Metadata(),
c2.ID().String(): 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().Hex(): c1.Metadata(),
c1.ID().String(): 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().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
c1.ID().String(): c1.Metadata(),
c2.ID().String(): 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().Hex(): c1.Metadata(),
c2.ID().Hex(): c2.Metadata(),
c1.ID().String(): c1.Metadata(),
c2.ID().String(): 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().Hex(): c1.Metadata(),
c1.ID().String(): 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.Hex()
expectedID := actorID.String()
changeInfo := db.ChangeInfo{
Actor: db.ID(expectedID),
}

change, err := changeInfo.ToChange()
assert.NoError(t, err)
assert.Equal(t, change.ID().Actor().Hex(), expectedID)
assert.Equal(t, change.ID().Actor().String(), 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.Hex(),
subscriber.ID.String(),
)
}

Expand All @@ -113,7 +113,7 @@ func (m *PubSub) Subscribe(
log.Logger.Debugf(
`Subscribe(%s,%s) End`,
keys[0].BSONKey(),
subscriber.ID.Hex(),
subscriber.ID.String(),
)
}
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.Hex())
log.Logger.Debugf(`Publish(%s,%s) Start`, k, publisherID.String())
}

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.Hex(),
publisherID.String(),
sub.SubscriberID(),
)
}
sub.Events() <- event
}
}
if log.Core.Enabled(zap.DebugLevel) {
log.Logger.Debugf(`Publish(%s,%s) End`, k, publisherID.Hex())
log.Logger.Debugf(`Publish(%s,%s) End`, k, publisherID.String())
}
}
}
Expand All @@ -218,7 +218,7 @@ func (m *PubSub) UpdateMetadata(
m.subscriptionsMapMu.Lock()
defer m.subscriptionsMapMu.Unlock()

sub, ok := m.subscriptionMapBySubscriber[publisher.ID.Hex()]
sub, ok := m.subscriptionMapBySubscriber[publisher.ID.String()]
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.Hex()
return s.subscriber.ID.String()
}

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

0 comments on commit d685706

Please sign in to comment.