-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rdx | ||
|
||
import "github.com/drpcorg/chotki/protocol" | ||
|
||
// append-only collection of Terms | ||
type LogT []string | ||
|
||
func Amerge(tlvs [][]byte) (merged []byte) { | ||
ih := ItHeap[*AIterator]{} | ||
for _, tlv := range tlvs { | ||
ih.Push(&AIterator{FIRSTIterator{TLV: tlv}}) | ||
} | ||
for ih.Len() > 0 { | ||
merged = append(merged, ih.Next()...) | ||
} | ||
return | ||
} | ||
|
||
func Atlv(log LogT) (tlv []byte) { | ||
for n, t := range log { | ||
bare := FIRSTtlv(int64(n), 0, []byte(t)) | ||
tlv = protocol.AppendHeader(tlv, Term, len(bare)) | ||
tlv = append(tlv, bare...) | ||
} | ||
return | ||
} | ||
|
||
func AnativeT(tlv []byte) (log LogT) { | ||
it := FIRSTIterator{TLV: tlv} | ||
for it.Next() { | ||
if it.lit != Term { | ||
continue | ||
} | ||
log = append(log, string(it.val)) | ||
} | ||
return | ||
} | ||
|
||
type AIterator struct { | ||
FIRSTIterator | ||
} | ||
|
||
func (a *AIterator) Merge(bb SortedIterator) int { | ||
b := bb.(*AIterator) | ||
if a.revz > b.revz { | ||
return MergeBA | ||
} else if a.revz < b.revz { | ||
return MergeAB | ||
} | ||
cmp := FIRSTcompare(a.one, b.one) | ||
if cmp == 0 { | ||
return MergeA | ||
} else if cmp < 0 { | ||
return MergeAB | ||
} else { | ||
return MergeBA | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package rdx | ||
|
||
import ( | ||
"github.com/drpcorg/chotki/protocol" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestAmerge(t *testing.T) { | ||
tlv := Atlv(LogT{ | ||
"Ione", | ||
"Stwo", | ||
"Rthree", | ||
}) | ||
tlv2 := Atlv(LogT{ | ||
"Ione", | ||
"Stwo", | ||
"Rthree", | ||
"Tfour", | ||
}) | ||
tlv2b := Amerge(protocol.Records{tlv, tlv2}) | ||
assert.Equal(t, tlv2, tlv2b) | ||
|
||
one := protocol.Record('T', FIRSTtlv(3, 0, []byte("Tfour"))) | ||
tlv3 := Amerge(protocol.Records{tlv, one}) | ||
assert.Equal(t, tlv2, tlv3) | ||
|
||
tlv4 := Amerge(protocol.Records{tlv, one, tlv2, tlv3}) | ||
assert.Equal(t, tlv2, tlv4) | ||
} |