|
23 | 23 | import com.google.protobuf.InvalidProtocolBufferException; |
24 | 24 | import com.google.protobuf.Value; |
25 | 25 | import com.google.protobuf.util.JsonFormat; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
26 | 28 | import java.util.Collections; |
27 | 29 | import java.util.HashSet; |
28 | 30 | import java.util.List; |
|
42 | 44 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.InitialPartition; |
43 | 45 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.Mod; |
44 | 46 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ModType; |
| 47 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.MoveInEvent; |
| 48 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.MoveOutEvent; |
| 49 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionEndRecord; |
| 50 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionEventRecord; |
45 | 51 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata; |
| 52 | +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionStartRecord; |
46 | 53 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.TypeCode; |
47 | 54 | import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ValueCaptureType; |
48 | 55 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Sets; |
@@ -223,12 +230,111 @@ public List<ChangeStreamRecord> toChangeStreamRecords( |
223 | 230 | return Collections.singletonList( |
224 | 231 | toChangeStreamRecordJson(partition, resultSet.getPgJsonb(0), resultSetMetadata)); |
225 | 232 | } |
| 233 | + |
| 234 | + // TODO(changliiu): wording - v2 or mutable? comments |
| 235 | + if (maybeV2ChangeRecord(resultSet.getCurrentRowAsStruct())) { |
| 236 | + return Arrays.asList( |
| 237 | + toV2ChangeStreamRecord(partition, resultSetMetadata, resultSet.getCurrentRowAsStruct())); |
| 238 | + } |
| 239 | + |
226 | 240 | // In GoogleSQL, change stream records are returned as an array of structs. |
227 | 241 | return resultSet.getCurrentRowAsStruct().getStructList(0).stream() |
228 | 242 | .flatMap(struct -> toChangeStreamRecord(partition, struct, resultSetMetadata)) |
229 | 243 | .collect(Collectors.toList()); |
230 | 244 | } |
231 | 245 |
|
| 246 | + boolean maybeV2ChangeRecord(Struct currentRow) { |
| 247 | + return currentRow.getColumnCount() == 1 |
| 248 | + && !currentRow.isNull(0) |
| 249 | + && currentRow.getColumnType(0).getCode() == com.google.cloud.spanner.Type.Code.PROTO; |
| 250 | + } |
| 251 | + |
| 252 | + ChangeStreamRecord toV2ChangeStreamRecord( |
| 253 | + PartitionMetadata partition, |
| 254 | + ChangeStreamResultSetMetadata resultSetMetadata, |
| 255 | + Struct currentRow) { |
| 256 | + com.google.spanner.v1.ChangeStreamRecord changeStreamRecordProto = |
| 257 | + currentRow.getProtoMessage( |
| 258 | + 0, com.google.spanner.v1.ChangeStreamRecord.getDefaultInstance()); |
| 259 | + if (changeStreamRecordProto.hasPartitionStartRecord()) { |
| 260 | + return parseV2PartitionStartRecord( |
| 261 | + partition, resultSetMetadata, changeStreamRecordProto.getPartitionStartRecord()); |
| 262 | + } else if (changeStreamRecordProto.hasPartitionEndRecord()) { |
| 263 | + return parseV2PartitionEndRecord( |
| 264 | + partition, resultSetMetadata, changeStreamRecordProto.getPartitionEndRecord()); |
| 265 | + } else if (changeStreamRecordProto.hasPartitionEventRecord()) { |
| 266 | + return parseV2PartitionEventRecord( |
| 267 | + partition, resultSetMetadata, changeStreamRecordProto.getPartitionEventRecord()); |
| 268 | + } else if (changeStreamRecordProto.hasHeartbeatRecord()) { |
| 269 | + return parseV2HeartbeatRecord( |
| 270 | + partition, resultSetMetadata, changeStreamRecordProto.getHeartbeatRecord()); |
| 271 | + } |
| 272 | + |
| 273 | + // todo data change record |
| 274 | + return new PartitionEndRecord( |
| 275 | + Timestamp.MIN_VALUE, |
| 276 | + "xxxx", |
| 277 | + changeStreamRecordMetadataFrom(partition, Timestamp.MIN_VALUE, resultSetMetadata)); |
| 278 | + } |
| 279 | + |
| 280 | + ChangeStreamRecord parseV2PartitionStartRecord( |
| 281 | + PartitionMetadata partition, |
| 282 | + ChangeStreamResultSetMetadata resultSetMetadata, |
| 283 | + com.google.spanner.v1.ChangeStreamRecord.PartitionStartRecord partitionStartRecordProto) { |
| 284 | + final Timestamp startTimestamp = |
| 285 | + Timestamp.fromProto(partitionStartRecordProto.getStartTimestamp()); |
| 286 | + return new PartitionStartRecord( |
| 287 | + startTimestamp, |
| 288 | + partitionStartRecordProto.getRecordSequence(), |
| 289 | + partitionStartRecordProto.getPartitionTokensList(), |
| 290 | + changeStreamRecordMetadataFrom(partition, startTimestamp, resultSetMetadata)); |
| 291 | + } |
| 292 | + |
| 293 | + ChangeStreamRecord parseV2PartitionEndRecord( |
| 294 | + PartitionMetadata partition, |
| 295 | + ChangeStreamResultSetMetadata resultSetMetadata, |
| 296 | + com.google.spanner.v1.ChangeStreamRecord.PartitionEndRecord partitionEndRecordProto) { |
| 297 | + final Timestamp endTimestamp = Timestamp.fromProto(partitionEndRecordProto.getEndTimestamp()); |
| 298 | + return new PartitionEndRecord( |
| 299 | + endTimestamp, |
| 300 | + partitionEndRecordProto.getRecordSequence(), |
| 301 | + changeStreamRecordMetadataFrom(partition, endTimestamp, resultSetMetadata)); |
| 302 | + } |
| 303 | + |
| 304 | + ChangeStreamRecord parseV2PartitionEventRecord( |
| 305 | + PartitionMetadata partition, |
| 306 | + ChangeStreamResultSetMetadata resultSetMetadata, |
| 307 | + com.google.spanner.v1.ChangeStreamRecord.PartitionEventRecord partitionEventRecordProto) { |
| 308 | + final Timestamp commitTimestamp = |
| 309 | + Timestamp.fromProto(partitionEventRecordProto.getCommitTimestamp()); |
| 310 | + List<MoveInEvent> moveInEvents = new ArrayList<>(); |
| 311 | + for (com.google.spanner.v1.ChangeStreamRecord.PartitionEventRecord.MoveInEvent |
| 312 | + moveInEventProto : partitionEventRecordProto.getMoveInEventsList()) { |
| 313 | + moveInEvents.add(new MoveInEvent(moveInEventProto.getSourcePartitionToken())); |
| 314 | + } |
| 315 | + List<MoveOutEvent> moveOutEvents = new ArrayList<>(); |
| 316 | + for (com.google.spanner.v1.ChangeStreamRecord.PartitionEventRecord.MoveOutEvent |
| 317 | + moveOutEventProto : partitionEventRecordProto.getMoveOutEventsList()) { |
| 318 | + moveOutEvents.add(new MoveOutEvent(moveOutEventProto.getDestinationPartitionToken())); |
| 319 | + } |
| 320 | + return new PartitionEventRecord( |
| 321 | + commitTimestamp, |
| 322 | + partitionEventRecordProto.getRecordSequence(), |
| 323 | + moveInEvents, |
| 324 | + moveOutEvents, |
| 325 | + changeStreamRecordMetadataFrom(partition, commitTimestamp, resultSetMetadata)); |
| 326 | + } |
| 327 | + |
| 328 | + ChangeStreamRecord parseV2HeartbeatRecord( |
| 329 | + PartitionMetadata partition, |
| 330 | + ChangeStreamResultSetMetadata resultSetMetadata, |
| 331 | + com.google.spanner.v1.ChangeStreamRecord.HeartbeatRecord heartbeatRecordProto) { |
| 332 | + final Timestamp heartbeatTimestamp = Timestamp.fromProto(heartbeatRecordProto.getTimestamp()); |
| 333 | + return new HeartbeatRecord( |
| 334 | + heartbeatTimestamp, |
| 335 | + changeStreamRecordMetadataFrom(partition, heartbeatTimestamp, resultSetMetadata)); |
| 336 | + } |
| 337 | + |
232 | 338 | Stream<ChangeStreamRecord> toChangeStreamRecord( |
233 | 339 | PartitionMetadata partition, Struct row, ChangeStreamResultSetMetadata resultSetMetadata) { |
234 | 340 |
|
|
0 commit comments