-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
159 lines (137 loc) · 4.3 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package quirk
import (
"context"
"io"
"sync"
"github.com/cheggaaa/pb/v3"
"github.com/damienfamed75/yalp"
"github.com/dgraph-io/dgo/v2"
)
// Exported structures for the Client to use.
type (
// Operation is the main parameter used when calling quirk client methods.
// Note: only one of these should be filled at a time, because only one
// will be executed and taken care of as seen in client.go
Operation struct {
SetMultiStruct []interface{}
SetSingleStruct interface{}
SetStringMap map[string]string
SetDynamicMap map[string]interface{}
SetSingleDupleNode *DupleNode
SetMultiDupleNode []*DupleNode
}
// DupleNode is the container for a duple node.
DupleNode struct {
Identifier string
Duples []Duple
}
// Duple is a structural way of giving the quirk client enough information
// about a node to create triples and insert them into Dgraph.
Duple struct {
// Predicate acts as a key.
Predicate string
// Object is the data representing the predicate.
Object interface{}
// IsUnique stores whether or not to treat this as an upsert or not.
IsUnique bool
// dataType stores the xml tag for the datatype.
dataType string
}
// UID is used to identify the ID's given to the user and retrieved back to
// be put as the object of a predicate.
// This way quirk can handle the UID how they're supposed to be handled.
// Note: Use this struct as the Object for Duples to create relationships.
UID struct {
uid string
isNew bool
}
)
// non exported structures.
type (
// upsertResponse is used to cleanup the large amount of info
// that the upserting function returns.
upsertResponse struct {
new bool
err error
identifier string
uid string
}
// workerPackage is used for some common items that a worker needs
// that do not include the channels or essential UID map.
workerPackage struct {
dg *dgo.Dgraph
m *sync.Mutex
mutateSingleStruct mutateSingle
logger yalp.Logger
bar *pb.ProgressBar
}
)
// interfaces used within for testing.
type (
builder interface {
io.Writer
String() string
Reset()
}
)
// non exported aliases.
type (
// Credit: The Go Authors @ "encoding/json"
// tagOptions is the string following a comma in a struct field's "quirk"
// tag, or the empty string. It does not include the leading comma.
tagOptions string
// queryDecode is our type when unmarshalling a query response.
queryDecode map[string][]struct{ UID *string }
// mutateSingle is used to pass into a worker function to call.
mutateSingle func(context.Context, *dgo.Dgraph, interface{}, map[string]UID, *sync.Mutex) (bool, error)
)
// DupleNode ---
// Unique will loop through the Duples and return a new slice
// containing all duples that are marked as unique.
func (d *DupleNode) Unique() (duples []Duple) {
for i := 0; i < len(d.Duples); i++ {
if d.Duples[i].IsUnique {
duples = append(duples, d.Duples[i])
}
}
return duples
}
// Find will return a reference to a duple given that it is found
// in the slice of duples in the DupleNode.
func (d *DupleNode) Find(predicate string) *Duple {
for i := 0; i < len(d.Duples); i++ {
if d.Duples[i].Predicate == predicate {
return &d.Duples[i]
}
}
return nil
}
// SetOrAdd will set a pre existing duple in the DupleNode or
// if the Duple doesn't exist, then it will be added to the Node.
func (d *DupleNode) SetOrAdd(duple Duple) *DupleNode {
if found := d.Find(duple.Predicate); found != nil {
found.Object = duple.Object
found.IsUnique = duple.IsUnique
return d
}
return d.AddDuples(duple)
}
// AddDuples appends new duples given in the function.
// Then returns the reference to the DupleNode.
// This function doesn't support updating previously added Duples though.
// This should only be used when trying to optimize appending new duples.
func (d *DupleNode) AddDuples(duple ...Duple) *DupleNode {
d.Duples = append(d.Duples, duple...)
return d
}
// UID ---
// Value returns the raw string value of the UID.
// Note: Do not use this as the Object for Duples to create relationships.
func (u UID) Value() string {
return u.uid
}
// IsNew returns a simple boolean value indicating whether or not this node
// was a newly added node or if it was pre existing in Dgraph.
func (u UID) IsNew() bool {
return u.isNew
}