Skip to content

Commit 606cf02

Browse files
committed
support provenance info in node: parent and children fields and updating APIs
1 parent f3c4a21 commit 606cf02

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

errors/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
NoAuth = "No Authorization"
1616
AttrImut = "node attributes immutable"
1717
FileImut = "node file immutable"
18+
ProvenanceImut = "provenance info immutable"
1819
InvalidIndex = "Invalid Index"
1920
InvalidFileTypeForFilter = "Invalid file type for filter"
2021
)

store/node.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ type Node struct {
3232
Indexes map[string]string `bson:"indexes" json:"indexes"`
3333
Acl acl `bson:"acl" json:"-"`
3434
VersionParts map[string]string `bson:"version_parts" json:"-"`
35-
Type string `bson:"type" json:"-"`
35+
Type []string `bson:"type" json:"type"`
36+
Parent parent `bson:"parent" json:"parent"`
37+
Children map[string]string `bson:"children" json:"children"` //map[nodeid]operation
3638
}
3739

3840
type file struct {
@@ -51,6 +53,11 @@ type partsList struct {
5153
Parts []partsFile `json:"parts"`
5254
}
5355

56+
type parent struct {
57+
Operation string `bson:"operation" json:"operation"`
58+
ParentNodes []string `bson:"parent_nodes" json:"parent_nodes"`
59+
}
60+
5461
type partsFile []string
5562

5663
type FormFiles map[string]FormFile
@@ -82,6 +89,13 @@ func (node *Node) HasIndex(index string) bool {
8289
return false
8390
}
8491

92+
func (node *Node) HasParent() bool {
93+
if len(node.Parent.ParentNodes) > 0 {
94+
return true
95+
}
96+
return false
97+
}
98+
8599
// Path functions
86100
func (node *Node) Path() string {
87101
return getPath(node.Id)
@@ -422,6 +436,35 @@ func (node *Node) Update(params map[string]string, files FormFiles) (err error)
422436
}
423437
}
424438
}
439+
440+
// update parent (provenance info)
441+
if _, hasParent := params["parents"]; hasParent {
442+
if node.HasParent() {
443+
return errors.New(e.ProvenanceImut)
444+
}
445+
var operation string = ""
446+
if _, hasOp := params["operation"]; hasOp {
447+
operation = params["operation"]
448+
}
449+
if err = node.SetParents(params["parents"], operation); err != nil {
450+
return err
451+
}
452+
}
453+
454+
//add child
455+
if _, hasChild := params["child"]; hasChild {
456+
if err = node.SetChild(params["child"]); err != nil {
457+
return err
458+
}
459+
}
460+
461+
//delete child
462+
if _, deleteChild := params["deletechild"]; deleteChild {
463+
if err = node.DeleteChild(params["deletechild"]); err != nil {
464+
return err
465+
}
466+
}
467+
425468
return
426469
}
427470

@@ -501,6 +544,44 @@ func (node *Node) SetFile(file FormFile) (err error) {
501544
return
502545
}
503546

547+
func (node *Node) SetParents(parents string, operation string) (err error) {
548+
node.Parent.Operation = operation
549+
parentList := strings.Split(parents, ",")
550+
for _, parent := range parentList {
551+
node.Parent.ParentNodes = append(node.Parent.ParentNodes, parent)
552+
}
553+
err = node.Save()
554+
return
555+
}
556+
557+
func (node *Node) SetChild(child string) (err error) {
558+
segs := strings.Split(child, ",")
559+
if len(segs) > 2 {
560+
return errors.New("invalid child string")
561+
}
562+
childId := segs[0]
563+
if _, hasKey := node.Children[childId]; hasKey {
564+
return errors.New("child id already existed")
565+
}
566+
var op string
567+
if len(segs) == 1 {
568+
op = "unknown_operation"
569+
} else if len(segs) == 2 {
570+
op = segs[1]
571+
}
572+
node.Children[childId] = op
573+
err = node.Save()
574+
return
575+
}
576+
577+
func (node *Node) DeleteChild(child string) (err error) {
578+
if _, ok := node.Children[child]; ok {
579+
delete(node.Children, child)
580+
}
581+
err = node.Save()
582+
return
583+
}
584+
504585
func (node *Node) SetAttributes(attr FormFile) (err error) {
505586
attributes, err := ioutil.ReadFile(attr.Path)
506587
if err != nil {

store/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func NewNode() (node *Node) {
7474
node = new(Node)
7575
node.Indexes = make(map[string]string)
7676
node.File.Checksum = make(map[string]string)
77+
node.Children = make(map[string]string)
7778
node.setId()
7879
return
7980
}

0 commit comments

Comments
 (0)