Skip to content

Commit

Permalink
cleanup ops in repo
Browse files Browse the repository at this point in the history
  • Loading branch information
bnewbold committed Feb 17, 2025
1 parent ff418c9 commit cf5fbe2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
10 changes: 6 additions & 4 deletions atproto/repo/operation.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package mst
package repo

import (
"fmt"
"sort"

"github.com/bluesky-social/indigo/atproto/repo/mst"

"github.com/ipfs/go-cid"
)

Expand Down Expand Up @@ -35,7 +37,7 @@ func (op *Operation) IsDelete() bool {
}

// Mutates the tree, returning a full `Operation`
func ApplyOp(tree *Tree, path string, val *cid.Cid) (*Operation, error) {
func ApplyOp(tree *mst.Tree, path string, val *cid.Cid) (*Operation, error) {
if val != nil {
prev, err := tree.Insert([]byte(path), *val)
if err != nil {
Expand All @@ -62,7 +64,7 @@ func ApplyOp(tree *Tree, path string, val *cid.Cid) (*Operation, error) {
}

// Does a simple "forwards" (not inversion) check of operation
func CheckOp(tree *Tree, op *Operation) error {
func CheckOp(tree *mst.Tree, op *Operation) error {
val, err := tree.Get([]byte(op.Path))
if err != nil {
return err
Expand All @@ -83,7 +85,7 @@ func CheckOp(tree *Tree, op *Operation) error {
}

// Applies the inversion of the `op` to the `tree`. This mutates the tree.
func InvertOp(tree *Tree, op *Operation) error {
func InvertOp(tree *mst.Tree, op *Operation) error {
if op.IsCreate() {
prev, err := tree.Remove([]byte(op.Path))
if err != nil {
Expand Down
48 changes: 40 additions & 8 deletions atproto/repo/operation_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
package mst
package repo

import (
"context"
"encoding/hex"
"fmt"
"math/rand"
"testing"

"github.com/bluesky-social/indigo/atproto/repo/mst"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
)

func randomCid() cid.Cid {
buf := make([]byte, 32)
rand.Read(buf)
c, err := cid.NewPrefixV1(cid.Raw, multihash.SHA2_256).Sum(buf)
if err != nil {
panic(err)
}
return c
}

func randomStr() string {
buf := make([]byte, 16)
rand.Read(buf)
return hex.EncodeToString(buf)
}

func debugCountEntries(n *mst.Node) int {
if n == nil {
return 0
}
count := 0
for _, e := range n.Entries {
if e.IsValue() {
count++
}
if e.IsChild() && e.Child != nil {
count += debugCountEntries(e.Child)
}
}
return count
}

func TestBasicOperation(t *testing.T) {
assert := assert.New(t)

c2, _ := cid.Decode("bafkreieqq463374bbcbeq7gpmet5rvrpeqow6t4rtjzrkhnlu222222222")
c3, _ := cid.Decode("bafkreieqq463374bbcbeq7gpmet5rvrpeqow6t4rtjzrkhnlu333333333")
et := NewEmptyTree()
et := mst.NewEmptyTree()
tree := &et
var op *Operation
var err error
Expand Down Expand Up @@ -101,7 +137,7 @@ func randomOperations(t *testing.T, size, opCount, iterations int) {
rand.Shuffle(len(mapKeys), func(i, j int) {
mapKeys[i], mapKeys[j] = mapKeys[j], mapKeys[i]
})
tree, err := LoadTreeFromMap(startMap)
tree, err := mst.LoadTreeFromMap(startMap)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -157,14 +193,10 @@ func randomOperations(t *testing.T, size, opCount, iterations int) {
if err != nil {
t.Fatal(err)
}
diffNode, err := loadNodeFromStore(ctx, diffBlocks, *diffRoot)
diffTree, err := mst.LoadTreeFromStore(ctx, diffBlocks, *diffRoot)
if err != nil {
t.Fatal(err)
}
diffTree := &Tree{
Root: diffNode,
}
nodeEnsureHeights(diffTree.Root)
assert.NoError(tree.Verify())

// re-compute partial commit (not related to main test path)
Expand Down
11 changes: 5 additions & 6 deletions atproto/repo/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log/slog"

comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/atproto/repo/mst"
"github.com/bluesky-social/indigo/atproto/syntax"

"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -85,14 +84,14 @@ func VerifyCommitMessage(ctx context.Context, msg *comatproto.SyncSubscribeRepos
if err != nil {
return nil, err
}
ops, err = mst.NormalizeOps(ops)
ops, err = NormalizeOps(ops)
if err != nil {
return nil, err
}

invTree := repo.MST.Copy()
for _, op := range ops {
if err := mst.InvertOp(&invTree, &op); err != nil {
if err := InvertOp(&invTree, &op); err != nil {
// print the *non-inverted* tree
//mst.DebugPrintTree(repo.MST.Root, 0)
return nil, err
Expand All @@ -105,14 +104,14 @@ func VerifyCommitMessage(ctx context.Context, msg *comatproto.SyncSubscribeRepos
return repo, nil
}

func ParseCommitOps(ops []*comatproto.SyncSubscribeRepos_RepoOp) ([]mst.Operation, error) {
func ParseCommitOps(ops []*comatproto.SyncSubscribeRepos_RepoOp) ([]Operation, error) {
//out := make([]mst.Operation, len(ops))
out := []mst.Operation{}
out := []Operation{}
for _, rop := range ops {
switch rop.Action {
case "create":
if rop.Cid != nil {
op := mst.Operation{
op := Operation{
Path: rop.Path,
Prev: nil,
Value: (*cid.Cid)(rop.Cid),
Expand Down

0 comments on commit cf5fbe2

Please sign in to comment.