-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNeo4j.linq
98 lines (76 loc) · 2.53 KB
/
Neo4j.linq
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
<Query Kind="Program">
<NuGetReference>Neo4jClient</NuGetReference>
<Namespace>Neo4jClient</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main()
{
var nodes = ProcessWorkflowy(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"Workflowy.txt"));
nodes.Count().Dump("Total Nodes");
var maxNestLevel = nodes.Max(x => x.Level);
var arr = new Node[maxNestLevel + 2];
var dc = new DumpContainer();
dc.Dump();
var pb = new Util.ProgressBar();
pb.Dump();
using (var client = new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "blah"))
{
await client.ConnectAsync();
await client.Cypher
.Match("(n)")
.OptionalMatch("(n)-[r]-()")
.Delete("n, r")
.ExecuteWithoutResultsAsync();
var rootNode = new Node { Id = Guid.NewGuid(), Name = "Workflowy" };
await client.Cypher
.Create("(node:Node {node})")
.WithParam("node", rootNode)
.ExecuteWithoutResultsAsync();
arr[0] = rootNode;
var n = 0;
foreach (var node in nodes)
{
if (node.Level < 0)
{
throw new Exception($"Invalid level: {node.Level}");
}
arr[node.Level + 1] = node;
var parentNode = arr[node.Level];
await client.Cypher
.Match("(parent:Node)")
.Where((Node parent) => parent.Id == parentNode.Id)
.Create("(parent)-[:CHILD]->(child:Node {node})")
.WithParam("node", node)
.ExecuteWithoutResultsAsync();
n++;
pb.Percent = (int)(((decimal)n / (decimal)nodes.Count) * 100m);
dc.Content = $"Processed {n} out of {nodes.Count} nodes";
}
}
}
List<Node> ProcessWorkflowy(string filename)
{
var lines = File.ReadAllLines(filename);
var nodes = new List<Node>();
foreach (var line in lines)
{
var match = Regex.Match(line, @"^(\s*)- (.*?)$");
if (match.Success)
{
nodes.Add(new Node
{
Id = Guid.NewGuid(),
Name = match.Groups[2].Value,
Level = match.Groups[1].Value.Length / 2,
});
}
}
return nodes;
}
public class Node
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Level { get; set;}
}