5
5
import java .util .HashSet ;
6
6
import java .util .Map ;
7
7
import java .util .Set ;
8
+ import java .util .function .Supplier ;
8
9
9
10
import com .fasterxml .jackson .databind .JsonNode ;
10
11
import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -35,9 +36,9 @@ public class CdcMsgParser {
35
36
private final YqlQuery updateQuery ;
36
37
private final YqlQuery deleteQuery ;
37
38
38
- private CdcMsgParser (YqlQuery updateQuery , YqlQuery deleteQuery ) {
39
- this .updateQuery = updateQuery ;
40
- this .deleteQuery = deleteQuery ;
39
+ private CdcMsgParser (Supplier < YqlQuery > updateQuery , Supplier < YqlQuery > deleteQuery ) {
40
+ this .updateQuery = updateQuery . get () ;
41
+ this .deleteQuery = deleteQuery . get () ;
41
42
}
42
43
43
44
public YqlQuery parseJsonMessage (byte [] json ) throws IOException {
@@ -80,7 +81,8 @@ public YqlQuery parseJsonMessage(byte[] json) throws IOException {
80
81
return null ;
81
82
}
82
83
83
- public static Result <CdcMsgParser > parse (YdbService ydb , Map <String , XmlConfig .Query > queries , XmlConfig .Cdc cdc ) {
84
+ public static Result <Supplier <CdcMsgParser >> parseConfig (YdbService ydb ,
85
+ Map <String , XmlConfig .Query > queries , XmlConfig .Cdc cdc ) {
84
86
return new Parser (ydb , cdc , queries ).parse ();
85
87
}
86
88
@@ -95,7 +97,7 @@ public Parser(YdbService ydb, XmlConfig.Cdc cdc, Map<String, XmlConfig.Query> xm
95
97
this .xmlQueries = xmlQueries ;
96
98
}
97
99
98
- public Result <CdcMsgParser > parse () {
100
+ public Result <Supplier < CdcMsgParser > > parse () {
99
101
String changefeed = ydb .expandPath (cdc .getChangefeed ());
100
102
101
103
int index = changefeed .lastIndexOf ("/" );
@@ -112,20 +114,20 @@ public Result<CdcMsgParser> parse() {
112
114
}
113
115
TableDescription description = descRes .getValue ();
114
116
115
- Result <YqlQuery > updateQuery = findUpdateQuery (description );
117
+ Result <Supplier < YqlQuery > > updateQuery = findUpdateQuery (description );
116
118
if (!updateQuery .isSuccess ()) {
117
119
return updateQuery .map (null );
118
120
}
119
121
120
- Result <YqlQuery > deleteQuery = findDeleteQuery (description );
122
+ Result <Supplier < YqlQuery > > deleteQuery = findDeleteQuery (description );
121
123
if (!deleteQuery .isSuccess ()) {
122
124
return deleteQuery .map (null );
123
125
}
124
126
125
- return Result .success (new CdcMsgParser (updateQuery .getValue (), deleteQuery .getValue ()));
127
+ return Result .success (() -> new CdcMsgParser (updateQuery .getValue (), deleteQuery .getValue ()));
126
128
}
127
129
128
- private Result <YqlQuery > findUpdateQuery (TableDescription source ) {
130
+ private Result <Supplier < YqlQuery > > findUpdateQuery (TableDescription source ) {
129
131
if (cdc .getQuery () != null && !cdc .getQuery ().trim ().isEmpty ()) {
130
132
return validate (source , cdc .getQuery ().trim (), false );
131
133
}
@@ -140,7 +142,7 @@ private Result<YqlQuery> findUpdateQuery(TableDescription source) {
140
142
return Result .success (YqlQuery .skipMessages ("update" , "updateQueryId" , source .getPrimaryKeys (), cdc ));
141
143
}
142
144
143
- private Result <YqlQuery > findDeleteQuery (TableDescription source ) {
145
+ private Result <Supplier < YqlQuery > > findDeleteQuery (TableDescription source ) {
144
146
String queryId = cdc .getDeleteQueryId ();
145
147
if (queryId != null && xmlQueries .containsKey (queryId )) {
146
148
XmlConfig .Query query = xmlQueries .get (queryId );
@@ -152,7 +154,7 @@ private Result<YqlQuery> findDeleteQuery(TableDescription source) {
152
154
return Result .success (YqlQuery .skipMessages ("erase" , "deleteQueryId" , source .getPrimaryKeys (), cdc ));
153
155
}
154
156
155
- private Result <YqlQuery > validate (TableDescription source , String query , boolean keysOnly ) {
157
+ private Result <Supplier < YqlQuery > > validate (TableDescription source , String query , boolean keysOnly ) {
156
158
Result <DataQuery > parsed = ydb .parseQuery (query );
157
159
if (!parsed .isSuccess ()) {
158
160
logger .error ("Can't parse query for consumer {}, got status {}" , cdc .getConsumer (), parsed .getStatus ());
0 commit comments