Skip to content

Commit e781916

Browse files
committed
backup
1 parent fa2f889 commit e781916

16 files changed

+1295
-144
lines changed

CypherCustomVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void executeQuery() {
3737
equalityList,
3838
pathList,
3939
retList,
40+
mem,
4041
indexer
4142
);
4243

@@ -319,8 +320,8 @@ public Value visitNodeLabels(CypherParser.NodeLabelsContext ctx) {
319320

320321
@Override
321322
public Value visitRangeLiteral(CypherParser.RangeLiteralContext ctx) {
322-
int INFINITY = 2147483647;
323-
Pair<Integer, Integer> range = new Pair<>(-INFINITY, INFINITY);
323+
324+
Pair<Integer, Integer> range = new Pair<>(0, Integer.MAX_VALUE);
324325
if (!ctx.getText().equals("*")) {
325326
if (!ctx.getText().contains("..")) {
326327
Integer val = new Integer(ctx.integerLiteral(0).getText());

Main.java

Lines changed: 84 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
33
// Deployment:
44
// Maven add commons-csv-1.4, mongodb-driver-3.4.2
55

6-
import Entities.NodeRecord;
7-
import Query.Engine.QueryIndexer;
8-
import com.fasterxml.jackson.databind.ObjectMapper;
9-
import org.antlr.v4.runtime.ANTLRFileStream;
10-
import org.antlr.v4.runtime.CommonTokenStream;
6+
import Utility.FileParser;
117

128
import java.io.IOException;
13-
import java.io.Reader;
14-
import java.sql.*;
159

1610

17-
import java.io.BufferedReader;
18-
import java.io.FileReader;
19-
import org.apache.ibatis.jdbc.ScriptRunner;
11+
import java.util.Map;
2012

2113

2214
/* Graph"
@@ -28,82 +20,88 @@ public class Main {
2820
public static void main(String[] args) throws IOException {
2921
//Parsing this CSV file:
3022

31-
boolean importData = false;
32-
try {
33-
// Connect to MySQL Database
34-
// Schema is in Tables.sql
35-
String url = "jdbc:mysql://localhost:3306/test";
36-
String username = "root";
37-
String password = "";
38-
Connection connection = DriverManager.getConnection(url, username, password);
39-
40-
if(importData) {
41-
//Create tables;
42-
ScriptRunner sr = new ScriptRunner(connection);
43-
44-
System.out.println("Creating tables...");
45-
// Give the input file to Reader
46-
Reader reader = new BufferedReader(
47-
new FileReader("src/Tables.sql"));
48-
// Exctute script
49-
sr.runScript(reader);
50-
51-
connection.close();
52-
connection = DriverManager.getConnection(url, username, password);
53-
54-
Utility.DBSetupUtil dbSetupUtil = new Utility.DBSetupUtil(connection);
55-
BufferedReader br = new BufferedReader(new FileReader("src/sample.csv"));
56-
57-
// Parse file line by line.
58-
// Ignore first line containing schema of input.
59-
br.readLine();
60-
61-
String line;
62-
System.out.println("Importing data...");
63-
64-
while ((line = br.readLine()) != null) {
65-
66-
// Carefully remove the quotes in string.
67-
line = line.substring(0, line.indexOf("biography") + 14) +
68-
line.substring(line.indexOf("biography") + 14, line.indexOf("version") - 5).replace("\"\"", "`") +
69-
line.substring(line.indexOf("version") - 5);
70-
71-
String line2 = line.replaceAll("'", "`")
72-
.replaceAll("\"\"([:,\\[\\]\\{\\}:])", "'$1")
73-
.replaceAll("([:,\\[\\]\\{\\}:])\"\"", "$1'")
74-
.replaceAll("\"\"", "`")
75-
.replaceAll("'", "\"")
76-
.substring(1);
77-
78-
line2 = line2.substring(0, line2.length() - 1);
79-
80-
// Convert json string to object.
81-
ObjectMapper mapper = new ObjectMapper();
82-
NodeRecord record = mapper.readValue(line2, NodeRecord.class);
83-
84-
// Insert into database.
85-
dbSetupUtil.insertRecord(record);
86-
87-
88-
}
89-
}
90-
System.out.println("Parsing Cypher query...");
91-
92-
CypherLexer lexer = new CypherLexer(new ANTLRFileStream("/Users/liuche/IdeaProjects/GraphDatabase/src/query.txt"));
93-
CypherParser parser = new CypherParser(new CommonTokenStream(lexer));
94-
CypherParser.CypherContext cypher = parser.cypher();
95-
CypherCustomVisitor visitor = new CypherCustomVisitor();
96-
visitor.setIndexer(new QueryIndexer(connection));
97-
visitor.visit(cypher);
98-
99-
100-
101-
connection.close();
102-
}catch(Exception e){
103-
e.printStackTrace();
104-
}
105-
106-
23+
// String url = "jdbc:mysql://localhost:3306/test";
24+
// String username = "root";
25+
// String password = "";
26+
// boolean importData = false;
27+
28+
// try {
29+
// // Connect to MySQL Database
30+
// // Schema is in Tables.sql
31+
//
32+
// Connection connection = DriverManager.getConnection(url, username, password);
33+
//
34+
// if(importData) {
35+
// //Create tables;
36+
// ScriptRunner sr = new ScriptRunner(connection);
37+
//
38+
// System.out.println("Creating tables...");
39+
// // Give the input file to Reader
40+
// Reader reader = new BufferedReader(
41+
// new FileReader("src/Tables.sql"));
42+
// // Exctute script
43+
// sr.runScript(reader);
44+
//
45+
// connection.close();
46+
// connection = DriverManager.getConnection(url, username, password);
47+
//
48+
// Utility.DBSetupUtil dbSetupUtil = new Utility.DBSetupUtil(connection);
49+
// BufferedReader br = new BufferedReader(new FileReader("src/sample.csv"));
50+
//
51+
// // Parse file line by line.
52+
// // Ignore first line containing schema of input.
53+
// br.readLine();
54+
//
55+
// String line;
56+
// System.out.println("Importing data...");
57+
//
58+
// while ((line = br.readLine()) != null) {
59+
//
60+
// // Carefully remove the quotes in string.
61+
// line = line.substring(0, line.indexOf("biography") + 14) +
62+
// line.substring(line.indexOf("biography") + 14, line.indexOf("version") - 5).replace("\"\"", "`") +
63+
// line.substring(line.indexOf("version") - 5);
64+
//
65+
// String line2 = line.replaceAll("'", "`")
66+
// .replaceAll("\"\"([:,\\[\\]\\{\\}:])", "'$1")
67+
// .replaceAll("([:,\\[\\]\\{\\}:])\"\"", "$1'")
68+
// .replaceAll("\"\"", "`")
69+
// .replaceAll("'", "\"")
70+
// .substring(1);
71+
//
72+
// line2 = line2.substring(0, line2.length() - 1);
73+
//
74+
// // Convert json string to object.
75+
// ObjectMapper mapper = new ObjectMapper();
76+
// NodeRecord record = mapper.readValue(line2, NodeRecord.class);
77+
//
78+
// // Insert into database.
79+
// dbSetupUtil.insertRecord(record);
80+
//
81+
//
82+
// }
83+
// }
84+
// System.out.println("Parsing Cypher query...");
85+
//
86+
// CypherLexer lexer = new CypherLexer(new ANTLRFileStream("/Users/liuche/IdeaProjects/GraphDatabase/src/query.txt"));
87+
// CypherParser parser = new CypherParser(new CommonTokenStream(lexer));
88+
// CypherParser.CypherContext cypher = parser.cypher();
89+
// CypherCustomVisitor visitor = new CypherCustomVisitor();
90+
// visitor.setIndexer(new QueryIndexer(connection));
91+
// visitor.visit(cypher);
92+
//
93+
//
94+
//
95+
// connection.close();
96+
// }catch(Exception e){
97+
// e.printStackTrace();
98+
// }
99+
//
100+
//
101+
//
102+
103+
FileParser fileParser = new FileParser("src/sample.csv");
104+
fileParser.run();
107105

108106

109107
return ;

Query/Engine/QueryIndexer.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package Query.Engine;
22

3-
import Entities.Person;
4-
5-
import java.lang.reflect.Field;
63
import java.sql.Connection;
74
import java.sql.PreparedStatement;
85
import java.sql.ResultSet;
@@ -20,6 +17,8 @@ public class QueryIndexer {
2017
Map<String, Integer> propertyCountOfNodes = new HashMap<>();
2118
Map<String, Integer> nodeLabelIncoming = new HashMap<>();
2219
Map<String, Integer> nodeLabelOutgoing = new HashMap<>();
20+
Map<String, Map<String, Integer>> nodeRelationInEdgeCount = new HashMap<>();
21+
Map<String, Map<String, Integer>> nodeRelationOutEdgeCount = new HashMap<>();
2322

2423
Integer numberOfNodes = 0, numberOfRelations = 0;
2524

@@ -119,7 +118,53 @@ public QueryIndexer(Connection conn) {
119118
statement = "SELECT label, COUNT(DISTINCT eid) from (Edge e LEFT JOIN NodeLabel n ON e.mid = n.mid) GROUP BY (label);";
120119
nodeLabelIncoming = getMapFromSQL(statement);
121120

121+
for(String nodeLabel : labelNodes.keySet()){
122+
nodeRelationOutEdgeCount.put(nodeLabel, new HashMap<>());
123+
for(String relationLabel : labelRelation.keySet()){
124+
statement =
125+
"SELECT COUNT(*)\n" +
126+
"from Edge LEFT JOIN Person ON Edge.pid = Person.id " +
127+
" LEFT JOIN NodeLabel ON Person.id = NodeLabel.pid " +
128+
"WHERE label = \"" + nodeLabel + "\" AND rel_type = \"" + relationLabel + "\"";
129+
Integer edges = getIntegerFromSQL(statement);
130+
nodeRelationOutEdgeCount.get(nodeLabel).put(relationLabel, edges);
131+
}
132+
}
133+
134+
for(String nodeLabel : labelNodes.keySet()){
135+
nodeRelationInEdgeCount.put(nodeLabel, new HashMap<>());
136+
for(String relationLabel : labelRelation.keySet()){
137+
statement =
138+
"SELECT COUNT(*)\n" +
139+
"from Edge LEFT JOIN Movie ON Edge.mid = Movie.id " +
140+
" LEFT JOIN NodeLabel ON Movie.id = NodeLabel.mid " +
141+
"WHERE label = \"" + nodeLabel + "\" AND rel_type = \"" + relationLabel + "\"";
142+
Integer edges = getIntegerFromSQL(statement);
143+
nodeRelationInEdgeCount.get(nodeLabel).put(relationLabel, edges);
144+
}
145+
}
146+
147+
}
122148

149+
public int getIncomingOfNodeRelation(String nodeLabel, String relationLabel){
150+
if(relationLabel.equals("")){
151+
return nodeLabel.equals("") ? numberOfRelations : nodeLabelIncoming.get(nodeLabel);
152+
}
153+
if(nodeLabel.equals("")){
154+
return labelRelation.get(relationLabel);
155+
}
156+
return nodeRelationInEdgeCount.get(nodeLabel).get(relationLabel);
157+
158+
}
159+
160+
public int getOutingOfNodeRelation(String nodeLabel, String relationLabel){
161+
if(relationLabel.equals("")){
162+
return nodeLabel.equals("") ? numberOfRelations : nodeLabelOutgoing.get(nodeLabel);
163+
}
164+
if(nodeLabel.equals("")){
165+
return labelRelation.get(relationLabel);
166+
}
167+
return nodeRelationOutEdgeCount.get(nodeLabel).get(relationLabel);
123168

124169
}
125170

Query/Engine/QueryPlanner.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class QueryPlanner {
1818
private Map<String, QueryConstraints> varToConstraint = new HashMap<>();
1919
private List<Equality> equalityList = new ArrayList<>();
2020
private List<Pair<String, String>> retList = new ArrayList<>();
21+
private Map<String, Value> retPath = new HashMap<>();
2122
private QueryIndexer indexer;
2223
private List<PlanTable> planTables = new ArrayList<>();
2324

@@ -28,6 +29,7 @@ public QueryPlanner(Map<String, QueryConstraints> varToConstraint,
2829
List<Equality> equalityList,
2930
List<Path> pathList,
3031
List<Pair<String, String>> retList,
32+
Map<String, Value> retPath,
3133
QueryIndexer indexer) {
3234
// Get node list and relation list
3335
this.varToConstraint = varToConstraint;
@@ -118,6 +120,10 @@ private void constructLeafPlan() {
118120
}
119121
}
120122

123+
private void processRetrurn(PlanTable bestTable){
124+
//TODO; Extract return variables.
125+
}
126+
121127
private List<PlanTable> candidates = new ArrayList<>();
122128
public void plan() {
123129
System.out.println("Generating Plan...");
@@ -166,10 +172,17 @@ public void plan() {
166172

167173
}while(candidates.size() > 0);
168174
assert planTables.size() == 1;
175+
176+
// TODO: Process return list
177+
178+
169179
PlanTable bestPlan = planTables.get(0);
170180
for(Plan plan : bestPlan.plans){
171181
System.out.println(plan.getName() + "|" + plan.getVariable() + "|" + plan.getParams());
172182
}
183+
184+
185+
173186
}
174187

175188
private void removeRelated(Plan plan){
@@ -282,24 +295,51 @@ private void constructExpand(PlanTable table){
282295
if(edge.used){
283296
continue;
284297
}
285-
PlanTable newTable;
298+
PlanTable newTable = new PlanTable(table);
299+
300+
boolean hasRangeExpand = false;
301+
QueryConstraints constraints = varToConstraint.get(edge.name);
302+
for(Constraint constraint : constraints.getConstraints()){
303+
if (constraint.value.type.contains("Range")){
304+
hasRangeExpand = true; break;
305+
}
306+
}
286307
if(table.nodes.contains(edge.start) && table.nodes.contains(edge.end)){
287-
QueryConstraints constraints = varToConstraint.get(edge.name);
288-
ExpandIntoPlan plan = new ExpandIntoPlan(indexer,edge,
289-
constraints,
290-
table);
291-
newTable = new PlanTable(table);
292-
plan.applyTo(newTable);
308+
if(!hasRangeExpand){
309+
ExpandIntoPlan plan = new ExpandIntoPlan(indexer,edge,
310+
constraints,
311+
table);
312+
313+
plan.applyTo(newTable);
314+
}else{
315+
RangeExpandIntoPlan plan = new RangeExpandIntoPlan(indexer,
316+
edge,
317+
constraints,
318+
table);
319+
plan.applyTo(newTable);
320+
}
321+
293322
newTable = addAdditionalFilter(newTable);
294323
candidates.add(newTable);
295324
}else if(table.nodes.contains(edge.start) || table.nodes.contains(edge.end)){
296-
String relation = edge.name;
297-
ExpandAllPlan plan = new ExpandAllPlan(indexer, edge,
298-
varToConstraint.get(relation), table);
299-
newTable = new PlanTable(table);
300-
plan.applyTo(newTable);
325+
String addedNode = "";
326+
if(!hasRangeExpand){
327+
ExpandAllPlan plan = new ExpandAllPlan(indexer, edge,
328+
constraints, table);
329+
plan.applyTo(newTable);
330+
addedNode = plan.getExpandedNode();
331+
}else{
332+
RangeExpandAllPlan plan = new RangeExpandAllPlan(indexer, edge,
333+
constraints,
334+
table);
335+
plan.applyTo(newTable);
336+
addedNode = plan.getExpandedNode();
337+
}
338+
301339
newTable = addAdditionalFilter(newTable);
302-
String addedNode = plan.getExpandedNode();
340+
341+
// If a node is added, should we add filters before or after it?
342+
303343
if(varToConstraint.get(addedNode).getConstraints().size() > 0){
304344
for(Constraint constraint : varToConstraint.get(addedNode).getConstraints()){
305345
FilterConstraintPlan plan1 = new FilterConstraintPlan(indexer, addedNode, constraint, newTable);

0 commit comments

Comments
 (0)