Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions operations/defs/deepcopy_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ func deepCopy(n Node, deep bool) (Node, error) {
var node Node
switch v := n.(type) {
case *MapNode:
m := NewMapNode(v.Id())
m := NewMapNode(v.GetId())
node = m
case *ListNode:
l := NewListNode(v.Id())
l := NewListNode(v.GetId())
node = l
case *RegisterNode:
return NewRegisterNode(v.Id(), v.Value()), nil
return NewRegisterNode(v.GetId(), v.GetValue()), nil
default:
return nil, errors.Errorf("malicious entry of type %T inside json tree", n)
}

// if deep flag is not set do not copy recursively
if !deep {
for id := range n.Children() {
node.Children()[id] = n.Children()[id]
for id := range n.GetChildren() {
node.GetChildren()[id] = n.GetChildren()[id]
}
return node, nil
}

// recursive deepcopy
for key, val := range n.Children() {
// recursive deep copy
for key, val := range n.GetChildren() {
deepVal, err := deepCopy(val, true)
if err != nil {
return nil, err
}
node.Children()[key] = deepVal
node.GetChildren()[key] = deepVal
}
return node, nil
}
16 changes: 8 additions & 8 deletions operations/defs/deepcopy_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func TestDeepCopy(t *testing.T) {
cloneTree, err := deepCopy(tree, true)

assert.Nil(err)
assert.Equal(cloneTree.Id(), NodeId("abc"))
assert.Equal(cloneTree.GetId(), NodeId("abc"))

ch, ok := cloneTree.Children()["def"]
ch, ok := cloneTree.GetChildren()["def"]
assert.True(ok)

// Updating subtree
Expand All @@ -41,8 +41,8 @@ func TestDeepCopy(t *testing.T) {
regC.SetValue(9)

// Should not interfere with original tree
originalRegC := tree.Children()["def"].(*RegisterNode)
assert.Equal(originalRegC.Value(), oldValue)
originalRegC := tree.GetChildren()["def"].(*RegisterNode)
assert.Equal(originalRegC.GetValue(), oldValue)
}

func TestShallowCopy(t *testing.T) {
Expand All @@ -55,9 +55,9 @@ func TestShallowCopy(t *testing.T) {
cloneTree, err := deepCopy(tree, false)

assert.Nil(err)
assert.Equal(cloneTree.Id(), NodeId("abc"))
assert.Equal(cloneTree.GetId(), NodeId("abc"))

ch, ok := cloneTree.Children()["def"]
ch, ok := cloneTree.GetChildren()["def"]
assert.True(ok)

// Updating subtree
Expand All @@ -67,6 +67,6 @@ func TestShallowCopy(t *testing.T) {
regC.SetValue(newValue)

// original reg should be updated
originalRegC := tree.Children()["def"].(*RegisterNode)
assert.Equal(originalRegC.Value(), newValue)
originalRegC := tree.GetChildren()["def"].(*RegisterNode)
assert.Equal(originalRegC.GetValue(), newValue)
}
106 changes: 53 additions & 53 deletions operations/defs/json_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func init() {
// todo: integrate with lamport counters
type (
// NodeId is of type string.
NodeId string
Children map[NodeId]Node
NodeId string
ChildNodes map[NodeId]Node

// Node interface represents the overall primary operations of an JSON node.
Node interface {
// Id returns current Node id.
Id() NodeId
// GetId returns current Node id.
GetId() NodeId
// SetId sets the node with the given id.
SetId(NodeId) error

Expand All @@ -49,115 +49,115 @@ type (
Serialize() ([]byte, error)
Deserialize([]byte) error

// DeepClone performs a deepcopy and returns a copied subtree.
// DeepClone performs a deep copy and returns a copied subtree.
DeepClone() (Node, error)
// Clone copies just the node and not the subtree.
Clone() (Node, error)

// FetchChild returns the children node reachable through the given set of ids as path.
// travserse the json tree in using []NodeId as a path
// traverse the json tree in using []NodeId as a path
FetchChild([]NodeId) (Node, error)

// Child returns the children map of current node.
Children() Children
// GetChildren returns the children map of current node.
GetChildren() ChildNodes

// Get returns child with Id if present and not marked as tombstone
Child(NodeId) (Node, error)
// GetChild returns child with Id if present and not marked as tombstone
GetChild(NodeId) (Node, error)

// Assign assigns argument node as a child of the current node.
Assign(Node, bool) error

// ListIndex is only valid if node is a child of a ListNode
// GetListIndex is only valid if node is a child of a ListNode
// Get listIndex for node
ListIndex() int
GetListIndex() int
// SetListIndex sets the index value for the node
SetListIndex(int) error

// Delete marks the given id a tombstone.
Delete(NodeId) error
}

// baseNode is a generic type that gets embedded into different Types struct.
baseNode struct {
id NodeId
tombstone bool
children Children
listIndex int
// BaseNode is a generic type that gets embedded into different Types struct.
BaseNode struct {
Id NodeId
Tombstone bool
Children ChildNodes
ListIndex int
}
)

// newBaseNode is an non-exported function and meant for internal usage only.
func newBaseNode(id NodeId) *baseNode {
return &baseNode{
id: id,
tombstone: false,
children: make(Children),
listIndex: -1,
func newBaseNode(id NodeId) *BaseNode {
return &BaseNode{
Id: id,
Tombstone: false,
Children: make(ChildNodes),
ListIndex: -1,
}
}

func (b *baseNode) Id() NodeId {
return b.id
func (b *BaseNode) GetId() NodeId {
return b.Id
}

func (b *baseNode) SetId(id NodeId) error {
if b.id != "" {
return errors.Errorf("id %v is already set for node", b.id)
func (b *BaseNode) SetId(id NodeId) error {
if b.Id != "" {
return errors.Errorf("id %v is already set for node", b.Id)
}
b.id = id
b.Id = id
return nil
}

func (b *baseNode) IsTombStone() bool {
return b.tombstone
func (b *BaseNode) IsTombStone() bool {
return b.Tombstone
}

func (b *baseNode) MarkTombstone() error {
if b.tombstone {
return errors.Errorf("node with id %v is already marked tombstone", b.id)
func (b *BaseNode) MarkTombstone() error {
if b.Tombstone {
return errors.Errorf("node with id %v is already marked tombstone", b.Id)
}
b.tombstone = true
b.Tombstone = true
return nil
}

func (b *baseNode) ListIndex() int {
return b.listIndex
func (b *BaseNode) GetListIndex() int {
return b.ListIndex
}

func (b *baseNode) SetListIndex(idx int) error {
b.listIndex = idx
func (b *BaseNode) SetListIndex(idx int) error {
b.ListIndex = idx
return nil
}

func (b *baseNode) Assign(node Node, override bool) error {
if _, ok := b.children[node.Id()]; ok && !override {
func (b *BaseNode) Assign(node Node, override bool) error {
if _, ok := b.Children[node.GetId()]; ok && !override {
return errors.New("failed to assign child to the given node, node exists with the same id")
}
b.children[node.Id()] = node
b.Children[node.GetId()] = node

return nil
}

func (b *baseNode) Children() Children {
return b.children
func (b *BaseNode) GetChildren() ChildNodes {
return b.Children
}

func (b *baseNode) Child(id NodeId) (Node, error) {
elem, ok := b.children[id]
func (b *BaseNode) GetChild(id NodeId) (Node, error) {
elem, ok := b.Children[id]
if ok && !elem.IsTombStone() {
return elem, nil
}
return nil, errors.Errorf("child with id %v doesn't exist for node %v", id, b.id)
return nil, errors.Errorf("child with id %v doesn't exist for node %v", id, b.Id)
}

func (b *baseNode) FetchChild(idList []NodeId) (Node, error) {
func (b *BaseNode) FetchChild(idList []NodeId) (Node, error) {
var node Node
children := b.children
children := b.Children

for i, id := range idList {
c, ok := children[id]
if !ok {
return nil, errors.Errorf("invalid id set, child of id %v doesn't exists for node of id %v", id, b.id)
return nil, errors.Errorf("invalid id set, child of id %v doesn't exists for node of id %v", id, b.Id)
}

if c.IsTombStone() {
Expand All @@ -167,12 +167,12 @@ func (b *baseNode) FetchChild(idList []NodeId) (Node, error) {
switch c.(type) {
case *RegisterNode:
if i != len(idList)-1 {
return nil, errors.Errorf("expected empty id[] when a RegisterNode of id %v is reached", c.Id())
return nil, errors.Errorf("expected empty id[] when a RegisterNode of id %v is reached", c.GetId())
}
default:
}
node = c
children = c.Children()
children = c.GetChildren()
}
return node, nil
}
18 changes: 9 additions & 9 deletions operations/defs/json_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (

func TestNodeSetId(t *testing.T) {
assert := assert.New(t)
tree := &baseNode{}
tree := &BaseNode{}

hehe := tree.SetId(NodeId("cat moment"))
assert.Nil(hehe)
assert.Equal(tree.Id(), NodeId("cat moment"))
assert.Equal(tree.GetId(), NodeId("cat moment"))

err := tree.SetId(NodeId("uwu"))
assert.NotNil(err)
assert.Equal(tree.Id(), NodeId("cat moment"))
assert.Equal(tree.GetId(), NodeId("cat moment"))
}

func TestMarkTombStone(t *testing.T) {
Expand All @@ -45,9 +45,9 @@ func TestMarkTombStone(t *testing.T) {
func TestSetListIndex(t *testing.T) {
assert := assert.New(t)
doc := NewMapNode("cat")
assert.Equal(doc.ListIndex(), -1)
assert.Equal(doc.GetListIndex(), -1)
doc.SetListIndex(0)
assert.Equal(doc.ListIndex(), 0)
assert.Equal(doc.GetListIndex(), 0)
}

func TestGet(t *testing.T) {
Expand All @@ -57,13 +57,13 @@ func TestGet(t *testing.T) {
doc.Assign(NewRegisterNode("onichan", ":3"), true)
doc.Assign(NewListNode("araara"), true)
doc.Delete("uwu")
uwu, _ := doc.Child("uwu")
uwu, _ := doc.GetChild("uwu")
assert.Nil(uwu)
doc.Delete("uwu")
doc.Delete("onichan")
assert.Equal(len(doc.Children()), 3)
araara, _ := doc.Child("araara")
assert.Equal(araara.Id(), NodeId("araara"))
assert.Equal(len(doc.GetChildren()), 3)
araara, _ := doc.GetChild("araara")
assert.Equal(araara.GetId(), NodeId("araara"))
}

func TestFetchChild(t *testing.T) {
Expand Down
Loading