Skip to content

Commit 00f68a6

Browse files
committed
add table Collector builder
1 parent 8720095 commit 00f68a6

File tree

4 files changed

+79
-56
lines changed

4 files changed

+79
-56
lines changed

src/main/java/cn/lightfish/pattern/GPattern.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public GPatternMatcher matcher(byte[] buffer) {
4949
public GPatternMatcher matcher(ByteBuffer buffer) {
5050
utf8Lexer.init(buffer, 0, buffer.limit());
5151
matcher.reset();
52+
collector.onCollectStart();
5253
while (utf8Lexer.nextToken()) {
5354
GPatternSeq token = idRecorder.toCurToken();
5455
if (matcher.accept(token)) {
@@ -58,6 +59,7 @@ public GPatternMatcher matcher(ByteBuffer buffer) {
5859
}
5960
collector.collect(token);
6061
}
62+
collector.onCollectEnd();
6163
return matcher;
6264
}
6365

src/main/java/cn/lightfish/pattern/TableCollector.java

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,30 @@
2424
* table1.column1
2525
*/
2626
public class TableCollector implements GPatternTokenCollector {
27+
private final int dotHash;
28+
private final LongObjectHashMap<TableInfo> map;
2729
private final TableInfo[] tables = new TableInfo[32];
28-
private final GPatternIdRecorder recorder;
29-
private final Map<String, Map<String, TableInfo>> schemaInfos = new HashMap<>();
30-
private final LongObjectHashMap<TableInfo> map = new LongObjectHashMap<>();
31-
State state = State.EXCPECT_ID;
32-
long currentSchemaLeftShift32;
33-
int first;
34-
int second;
35-
private int dotHash;
30+
private final TableCollectorBuilder builder;
3631
private int tableIndex;
37-
38-
39-
public TableCollector(GPatternIdRecorder recorder, Map<String, Collection<String>> schemaInfos) {
40-
this.recorder = recorder;
41-
this.dotHash = recorder.createConstToken(".").hashCode();
42-
for (Map.Entry<String, Collection<String>> stringSetEntry : schemaInfos.entrySet()) {
43-
String schemaName = stringSetEntry.getKey();
44-
List<Integer> schemaHashList = record(recorder, schemaName);
45-
Map<String, TableInfo> tableInfoMap = this.schemaInfos.computeIfAbsent(schemaName, (s) -> new HashMap<>());
46-
Collection<String> tableNames = stringSetEntry.getValue();
47-
for (String tableName : tableNames) {
48-
List<Integer> tableNameHashList = record(recorder, tableName);
49-
for (Integer schemaNameHash : schemaHashList) {
50-
for (Integer tableNameHash : tableNameHashList) {
51-
long hash = schemaNameHash;
52-
hash = hash << 32;
53-
hash = hash | tableNameHash;
54-
TableInfo tableInfo = new TableInfo(schemaName, tableName, schemaNameHash, tableNameHash, hash);
55-
tableInfoMap.put(tableName, tableInfo);
56-
map.put(hash, tableInfo);
57-
}
58-
}
59-
}
60-
}
32+
private State state = State.EXCPECT_ID;
33+
private long currentSchemaLeftShift32;
34+
private int first;
35+
private int second;
36+
37+
public TableCollector(TableCollectorBuilder builder) {
38+
this.builder = builder;
39+
this.dotHash = builder.dotHash;
40+
this.map = builder.map;
6141
}
6242

6343
public void useSchema(String schema) {
64-
Map<String, TableInfo> stringTableInfoMap = schemaInfos.get(schema);
65-
if (stringTableInfoMap == null) throw new UnsupportedOperationException();
66-
long hash = stringTableInfoMap.values().iterator().next().getSchema();
44+
Integer intHash = builder.schemaHash.get(schema);
45+
if (intHash == null) throw new UnsupportedOperationException();
46+
long hash = intHash;
6747
hash = hash << 32;
6848
currentSchemaLeftShift32 = hash;
6949
}
7050

71-
private List<Integer> record(GPatternIdRecorder recorder, String text) {
72-
String lowerCase = text.toLowerCase();
73-
String upperCase = text.toUpperCase();
74-
75-
ArrayList<Integer> list = new ArrayList<>();
76-
list.add(recorder.createConstToken(lowerCase).hashCode());
77-
list.add(recorder.createConstToken(upperCase).hashCode());
78-
list.add(recorder.createConstToken("`" + lowerCase + "`").hashCode());
79-
list.add(recorder.createConstToken("`" + upperCase + "`").hashCode());
80-
return list;
81-
}
82-
8351

8452
@Override
8553
public void onCollectStart() {
@@ -155,12 +123,11 @@ public TableInfo[] getTableArray() {
155123
}
156124

157125
public Map<String, Collection<String>> geTableMap(Map<String, Collection<String>> map) {
158-
Map<String, Collection<String>> collectionMap = new HashMap<>();
159126
for (int i = 0; i < this.tableIndex; i++) {
160127
TableInfo table = tables[i];
161-
collectionMap.computeIfAbsent(table.getSchemaName(), s -> new HashSet<>()).add(table.getTableName());
128+
map.computeIfAbsent(table.getSchemaName(), s -> new HashSet<>()).add(table.getTableName());
162129
}
163-
return collectionMap;
130+
return map;
164131
}
165132

166133
public Map<String, Collection<String>> geTableMap() {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cn.lightfish.pattern;
2+
3+
import org.eclipse.collections.impl.map.mutable.primitive.LongObjectHashMap;
4+
5+
import java.util.*;
6+
7+
public class TableCollectorBuilder {
8+
final Map<String, Integer> schemaHash = new HashMap<>();
9+
final LongObjectHashMap<TableCollector.TableInfo> map = new LongObjectHashMap<>();
10+
final int dotHash;
11+
private final GPatternIdRecorder recorder;
12+
private final Map<String, Map<String, TableCollector.TableInfo>> schemaInfos = new HashMap<>();
13+
14+
public TableCollectorBuilder(GPatternIdRecorder recorder, Map<String, Collection<String>> schemaInfos) {
15+
this.recorder = recorder;
16+
this.dotHash = recorder.createConstToken(".").hashCode();
17+
for (Map.Entry<String, Collection<String>> stringSetEntry : schemaInfos.entrySet()) {
18+
String schemaName = stringSetEntry.getKey();
19+
List<Integer> schemaHashList = record(recorder, schemaName);
20+
Map<String, TableCollector.TableInfo> tableInfoMap = this.schemaInfos.computeIfAbsent(schemaName, (s) -> new HashMap<>());
21+
Collection<String> tableNames = stringSetEntry.getValue();
22+
for (String tableName : tableNames) {
23+
List<Integer> tableNameHashList = record(recorder, tableName);
24+
for (Integer schemaNameHash : schemaHashList) {
25+
schemaHash.computeIfAbsent(schemaName, s -> schemaNameHash);
26+
for (Integer tableNameHash : tableNameHashList) {
27+
long hash = schemaNameHash;
28+
hash = hash << 32;
29+
hash = hash | tableNameHash;
30+
TableCollector.TableInfo tableInfo = new TableCollector.TableInfo(schemaName, tableName, schemaNameHash, tableNameHash, hash);
31+
tableInfoMap.put(tableName, tableInfo);
32+
map.put(hash, tableInfo);
33+
}
34+
}
35+
}
36+
}
37+
}
38+
39+
private List<Integer> record(GPatternIdRecorder recorder, String text) {
40+
String lowerCase = text.toLowerCase();
41+
String upperCase = text.toUpperCase();
42+
43+
ArrayList<Integer> list = new ArrayList<>();
44+
list.add(recorder.createConstToken(lowerCase).hashCode());
45+
list.add(recorder.createConstToken(upperCase).hashCode());
46+
list.add(recorder.createConstToken("`" + lowerCase + "`").hashCode());
47+
list.add(recorder.createConstToken("`" + upperCase + "`").hashCode());
48+
return list;
49+
}
50+
51+
public TableCollector create() {
52+
return new TableCollector(this);
53+
}
54+
55+
}

src/test/java/cn/lightfish/pattern/GPatternCollectorTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ public void test22222() {
2828
int id = patternBuilder.addRule(message);
2929

3030
GPatternIdRecorder recorder = patternBuilder.geIdRecorder();
31-
TableCollector gPatternTokenCollector = new TableCollector(recorder, infos);
32-
GPattern gPattern = patternBuilder.createGroupPattern(gPatternTokenCollector);
31+
TableCollectorBuilder builder = new TableCollectorBuilder(recorder, infos);
32+
TableCollector tableCollector = builder.create();
33+
GPattern gPattern = patternBuilder.createGroupPattern(tableCollector);
3334

34-
gPatternTokenCollector.useSchema("db1");
35-
gPatternTokenCollector.onCollectStart();
35+
tableCollector.useSchema("db1");
3636
GPatternMatcher matcher = gPattern.matcher(message);
3737
Assert.assertTrue(matcher.acceptAll());
38-
Map<String, Collection<String>> map = gPatternTokenCollector.geTableMap();
39-
Assert.assertEquals(infos, map);
38+
Assert.assertEquals(infos, tableCollector.geTableMap());
4039
}
4140

4241
private void addTable(Map<String, Collection<String>> infos, String schemaName, String tableName) {

0 commit comments

Comments
 (0)