-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcypherGenerator.go
83 lines (70 loc) · 3.69 KB
/
cypherGenerator.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
package main
import (
"strings"
"strconv"
"regexp"
)
var reStr = regexp.MustCompile(`\W`)
// createCypherFriendlyVarName produces a string for a filename and its nested depth that can be safely used in cypher as a variable name
func createCypherFriendlyVarName(s string, i int) string {
id := strings.Replace(s, ".", "_", -1)
id = "a_" + id
id = reStr.ReplaceAllString(id, "$1")
id += strconv.Itoa(i)
return id
}
// getLabelForFileNode returns the correct label for a given element
func getLabelForFileNode(currentFile fileInfo) string {
if (currentFile.IsDir) {
return "directory"
}
return "file"
}
// fileInfoToCypher returns a cypher statement to create a node for a given file
func fileInfoToCypher(currentFile fileInfo, label string) string {
properties := ("{ name: '" + currentFile.Name +
"', path: '" + currentFile.Path +
"', url: '" + currentFile.Url +
"', _tempId: '" + currentFile.Id + "'")
if (!currentFile.IsDir) {
properties += (", " + "size: " + strconv.FormatInt(currentFile.Size, 10) + ", " +
"commitCount: " + strconv.Itoa(currentFile.CommitCount) + ", " +
"lastModifiedDateTime: datetime({ epochseconds: " + strconv.FormatInt(currentFile.ModTime, 10) + " }), " +
"lastModifiedTimestamp: " + strconv.FormatInt(currentFile.ModTime, 10) + ", " +
"extension: '" + currentFile.Extension + "'")
}
properties += " }"
return "CREATE (" + currentFile.Id + ":" + label + " " + properties + ")"
}
// contributerToCypher returns a cypher statement to create node for a given contributer
func contributerToCypher(contributerId, contributerName, contributerEmail string) string {
return ("MERGE (" + contributerId + ":" + "person" + " { _tempId: '" + contributerId + "', name: '" + contributerName + "', email: '" + contributerEmail + "' })")
}
// contributerToCypherUpdate returns a cypher statement to update a given contributer's commitCount
func contributerToCypherUpdate(contributerId string, commitCount int) string {
return ("MATCH (c:person { _tempId: '" + contributerId + "' }) " +
"SET c.commitCount = " + strconv.Itoa(commitCount)) + " " +
"REMOVE c._tempId"
}
// contributionToCypher returns to cypher statement to create a relationship between a file and a contributer
func contributionToCypher(fileId, contributerId string, contributionId string) string {
return "CREATE (" + fileId + ")<-[" + contributionId + ":EDITED { _tempId: '" + contributionId +"' }]-(" + contributerId + ")"
}
// contributionToCypher returns to cypher statement to create a relationship between a file and a contributer
func commitToCypher(fileId, contributerId string, contribution fileContribution) string {
properties := "hash: '" + contribution.Hash + "', abbreviatedHash: '" + contribution.AbbreviatedHash + "', dateTime: datetime('" + contribution.DateTime + "')"
return "CREATE (" + fileId + ")<-[:COMMIT { " + properties + " }]-(" + contributerId + ")"
}
// contributionToCypherUpdate returns a cypher statement to update a given contribution's commitCount
func contributionToCypherUpdate(contributionId string, commitCount int) string {
return "MATCH (c:person)-[e:EDITED { _tempId: '" + contributionId + "' }]->(f:file) " +
"SET e.commitCount = " + strconv.Itoa(commitCount)
}
// folderStructureToCypher returns to cypher statement to create a relationship between a file and its parent folder
func folderStructureToCypher(currentFile fileInfo) string {
return "Match (a:directory { path: '" + currentFile.ParentPath +"' }) Match (b { path: '" + currentFile.Path +"' }) CREATE (b)-[:IN_FOLDER]->(a)"
}
// returns a cypher statement that removes a given property from all nodes
func removeProperty(propertyName string) string {
return "MATCH (a) REMOVE a." + propertyName
}