Skip to content

Commit aad0431

Browse files
committed
Add documentation.
1 parent 7c5baaa commit aad0431

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

internal/knowledge/query_graph.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ import (
88
"github.com/clems4ever/go-graphkb/internal/utils"
99
)
1010

11+
// ErrVariableNotFound error thrown when a variable does not exist
1112
var ErrVariableNotFound = errors.New("Unable to find variable")
1213

13-
// QueryNode represent a node and its constraints
14-
type QueryNode struct {
15-
Labels []string
16-
// Constraint expressions
17-
Constraints AndOrExpression
18-
}
19-
14+
// RelationDirection the direction of a relation
2015
type RelationDirection int
2116

2217
const (
2318
// Left relation
2419
Left RelationDirection = iota
2520
// Right relation
2621
Right RelationDirection = iota
27-
// There is a relation but we don't know in which direction
22+
// Either there is a relation but we don't know in which direction
2823
Either RelationDirection = iota
29-
// There is a relation in both directions
24+
// Both there is a relation in both directions
3025
Both RelationDirection = iota
3126
)
3227

28+
// QueryNode represent a node and its constraints
29+
type QueryNode struct {
30+
Labels []string
31+
// Constraint expressions
32+
Constraints AndOrExpression
33+
}
34+
3335
// QueryRelation represent a relation and its constraints
3436
type QueryRelation struct {
3537
Labels []string
@@ -41,25 +43,31 @@ type QueryRelation struct {
4143
Direction RelationDirection
4244
}
4345

46+
// VariableType represent the type of a variable in the cypher query.
4447
type VariableType int
4548

4649
const (
47-
NodeType VariableType = iota
50+
// NodeType variable of type node
51+
NodeType VariableType = iota
52+
// RelationType variable of type relation
4853
RelationType VariableType = iota
4954
)
5055

56+
// TypeAndIndex type and index of a variable from the cypher query
5157
type TypeAndIndex struct {
5258
Type VariableType
5359
Index int
5460
}
5561

62+
// QueryGraph the representation of a query graph. This structure helps create the relations between nodes to facilitate SQL translation and projections
5663
type QueryGraph struct {
5764
Nodes []QueryNode
5865
Relations []QueryRelation
5966

6067
VariablesIndex map[string]TypeAndIndex
6168
}
6269

70+
// NewQueryGraph create an instance of a query graph
6371
func NewQueryGraph() QueryGraph {
6472
return QueryGraph{
6573
Nodes: []QueryNode{},
@@ -68,6 +76,7 @@ func NewQueryGraph() QueryGraph {
6876
}
6977
}
7078

79+
// PushNode push a node into the registry
7180
func (qg *QueryGraph) PushNode(q query.QueryNodePattern) (*QueryNode, int, error) {
7281
// If pattern comes with a variable name, search in the index if it does not already exist
7382
if q.Variable != "" {
@@ -101,6 +110,7 @@ func (qg *QueryGraph) PushNode(q query.QueryNodePattern) (*QueryNode, int, error
101110
return &qn, newIdx, nil
102111
}
103112

113+
// PushRelation push a relation into the registry
104114
func (qg *QueryGraph) PushRelation(q query.QueryRelationshipPattern, leftIdx, rightIdx int) (*QueryRelation, int, error) {
105115
var varName string
106116
var labels []string
@@ -166,6 +176,7 @@ func (qg *QueryGraph) PushRelation(q query.QueryRelationshipPattern, leftIdx, ri
166176
return &qr, newIdx, nil
167177
}
168178

179+
// FindVariable find a variable by its name
169180
func (qg *QueryGraph) FindVariable(name string) (TypeAndIndex, error) {
170181
v, ok := qg.VariablesIndex[name]
171182
if !ok {
@@ -174,9 +185,10 @@ func (qg *QueryGraph) FindVariable(name string) (TypeAndIndex, error) {
174185
return v, nil
175186
}
176187

177-
func (gq *QueryGraph) FindNode(idx int) (*QueryNode, error) {
178-
if idx >= len(gq.Nodes) {
188+
// GetNodeByID get a node by its id
189+
func (qg *QueryGraph) GetNodeByID(idx int) (*QueryNode, error) {
190+
if idx >= len(qg.Nodes) {
179191
return nil, fmt.Errorf("Index provided to find node is invalid")
180192
}
181-
return &gq.Nodes[idx], nil
193+
return &qg.Nodes[idx], nil
182194
}

internal/knowledge/query_sql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ func (sqt *SQLQueryTranslator) Translate(query *query.QueryCypher) (*SQLTranslat
273273
}
274274
}
275275
if !nodesConstrained {
276-
n, err := sqt.QueryGraph.FindNode(r.LeftIdx)
276+
n, err := sqt.QueryGraph.GetNodeByID(r.LeftIdx)
277277
if err != nil {
278278
return nil, err
279279
}
280280
if len(n.Labels) > 0 {
281281
nodesConstrained = true
282282
}
283283

284-
n, err = sqt.QueryGraph.FindNode(r.RightIdx)
284+
n, err = sqt.QueryGraph.GetNodeByID(r.RightIdx)
285285
if err != nil {
286286
return nil, err
287287
}

0 commit comments

Comments
 (0)