diff options
author | Michael Muré <batolettre@gmail.com> | 2020-11-30 01:55:30 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2021-02-14 12:17:49 +0100 |
commit | db7074301b6af895b1a47ecd12a5028ac809abfc (patch) | |
tree | 72c9f1889aefb8c66bf4bbc027d92c6cd572bc9e /bug | |
parent | fcf43915e1736fe0b56f8f06386f68d9b56da7a8 (diff) | |
download | git-bug-db7074301b6af895b1a47ecd12a5028ac809abfc.tar.gz |
entity: generalize the combined Ids, use 64 length
Diffstat (limited to 'bug')
-rw-r--r-- | bug/comment.go | 46 | ||||
-rw-r--r-- | bug/comment_test.go | 27 | ||||
-rw-r--r-- | bug/op_add_comment.go | 2 | ||||
-rw-r--r-- | bug/op_create.go | 2 | ||||
-rw-r--r-- | bug/op_create_test.go | 3 |
5 files changed, 4 insertions, 76 deletions
diff --git a/bug/comment.go b/bug/comment.go index 1a9ca05a..4c9d118e 100644 --- a/bug/comment.go +++ b/bug/comment.go @@ -1,8 +1,6 @@ package bug import ( - "strings" - "github.com/dustin/go-humanize" "github.com/MichaelMure/git-bug/entity" @@ -33,50 +31,6 @@ func (c Comment) Id() entity.Id { return c.id } -const compiledCommentIdFormat = "BCBCBCBBBCBBBBCBBBBCBBBBCBBBBCBBBBCBBBBC" - -// DeriveCommentId compute a merged Id for a comment holding information from -// both the Bug's Id and the Comment's Id. This allow to later find efficiently -// a Comment because we can access the bug directly instead of searching for a -// Bug that has a Comment matching the Id. -// -// To allow the use of an arbitrary length prefix of this merged Id, Ids from Bug -// and Comment are interleaved with this irregular pattern to give the best chance -// to find the Comment even with a 7 character prefix. -// -// A complete merged Id hold 30 characters for the Bug and 10 for the Comment, -// which give a key space of 36^30 for the Bug (~5 * 10^46) and 36^10 for the -// Comment (~3 * 10^15). This asymmetry assume a reasonable number of Comment -// within a Bug, while still allowing for a vast key space for Bug (that is, a -// globally merged bug database) with a low risk of collision. -func DeriveCommentId(bugId entity.Id, commentId entity.Id) entity.Id { - var id strings.Builder - for _, char := range compiledCommentIdFormat { - if char == 'B' { - id.WriteByte(bugId[0]) - bugId = bugId[1:] - } else { - id.WriteByte(commentId[0]) - commentId = commentId[1:] - } - } - return entity.Id(id.String()) -} - -func SplitCommentId(prefix string) (bugPrefix string, commentPrefix string) { - var bugIdPrefix strings.Builder - var commentIdPrefix strings.Builder - - for i, char := range prefix { - if compiledCommentIdFormat[i] == 'B' { - bugIdPrefix.WriteRune(char) - } else { - commentIdPrefix.WriteRune(char) - } - } - return bugIdPrefix.String(), commentIdPrefix.String() -} - // FormatTimeRel format the UnixTime of the comment for human consumption func (c Comment) FormatTimeRel() string { return humanize.Time(c.UnixTime.Time()) diff --git a/bug/comment_test.go b/bug/comment_test.go deleted file mode 100644 index 423d10d8..00000000 --- a/bug/comment_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package bug - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/MichaelMure/git-bug/entity" -) - -func TestCommentId(t *testing.T) { - bugId := entity.Id("abcdefghijklmnopqrstuvwxyz1234__________") - opId := entity.Id("ABCDEFGHIJ______________________________") - expectedId := entity.Id("aAbBcCdefDghijEklmnFopqrGstuvHwxyzI1234J") - - mergedId := DeriveCommentId(bugId, opId) - require.Equal(t, expectedId, mergedId) - - // full length - splitBugId, splitCommentId := SplitCommentId(mergedId.String()) - require.Equal(t, string(bugId[:30]), splitBugId) - require.Equal(t, string(opId[:10]), splitCommentId) - - splitBugId, splitCommentId = SplitCommentId(string(expectedId[:6])) - require.Equal(t, string(bugId[:3]), splitBugId) - require.Equal(t, string(opId[:3]), splitCommentId) -} diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go index df426ee0..e52c46fd 100644 --- a/bug/op_add_comment.go +++ b/bug/op_add_comment.go @@ -36,7 +36,7 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) { snapshot.addActor(op.Author) snapshot.addParticipant(op.Author) - commentId := DeriveCommentId(snapshot.Id(), op.Id()) + commentId := entity.CombineIds(snapshot.Id(), op.Id()) comment := Comment{ id: commentId, Message: op.Message, diff --git a/bug/op_create.go b/bug/op_create.go index 044ddd72..1e944d13 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -66,7 +66,7 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title - commentId := DeriveCommentId(snapshot.Id(), op.Id()) + commentId := entity.CombineIds(snapshot.Id(), op.Id()) comment := Comment{ id: commentId, Message: op.Message, diff --git a/bug/op_create_test.go b/bug/op_create_test.go index 73a65778..456357c4 100644 --- a/bug/op_create_test.go +++ b/bug/op_create_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" "github.com/MichaelMure/git-bug/util/timestamp" @@ -29,7 +30,7 @@ func TestCreate(t *testing.T) { id := create.Id() require.NoError(t, id.Validate()) - commentId := DeriveCommentId(create.Id(), create.Id()) + commentId := entity.CombineIds(create.Id(), create.Id()) comment := Comment{ id: commentId, |