|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.kafka.connect.sink.cdc.attunity.rdbms.oracle; |
| 18 | + |
| 19 | +import com.mongodb.client.model.WriteModel; |
| 20 | +import com.mongodb.kafka.connect.sink.MongoSinkTopicConfig; |
| 21 | +import com.mongodb.kafka.connect.sink.cdc.CdcOperation; |
| 22 | +import com.mongodb.kafka.connect.sink.cdc.debezium.OperationType; |
| 23 | +import com.mongodb.kafka.connect.sink.converter.SinkDocument; |
| 24 | +import org.apache.kafka.connect.errors.DataException; |
| 25 | +import org.bson.BsonDocument; |
| 26 | +import org.bson.BsonInvalidOperationException; |
| 27 | +import org.bson.BsonObjectId; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Optional; |
| 34 | + |
| 35 | +public class AttunityRdbmsHandler extends AttunityCdcHandler { |
| 36 | + static final String ID_FIELD = "_id"; |
| 37 | + private static final String JSON_DOC_BEFORE_FIELD = "beforeData"; |
| 38 | + private static final String JSON_DOC_AFTER_FIELD = "data"; |
| 39 | + private static final String JSON_DOC_WRAPPER_FIELD = "message"; |
| 40 | + private static final Logger LOGGER = LoggerFactory.getLogger(AttunityRdbmsHandler.class); |
| 41 | + private static final Map<OperationType, CdcOperation> DEFAULT_OPERATIONS = new HashMap<OperationType, CdcOperation>(){{ |
| 42 | + put(OperationType.CREATE, new AttunityRdbmsInsert()); |
| 43 | + put(OperationType.READ, new AttunityRdbmsInsert()); |
| 44 | + put(OperationType.UPDATE, new AttunityRdbmsUpdate()); |
| 45 | + put(OperationType.DELETE, new AttunityRdbmsDelete()); |
| 46 | + }}; |
| 47 | + |
| 48 | + public AttunityRdbmsHandler(final MongoSinkTopicConfig config) { |
| 49 | + this(config, DEFAULT_OPERATIONS); |
| 50 | + } |
| 51 | + |
| 52 | + public AttunityRdbmsHandler(final MongoSinkTopicConfig config, |
| 53 | + final Map<OperationType, CdcOperation> operations) { |
| 54 | + super(config); |
| 55 | + registerOperations(operations); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Optional<WriteModel<BsonDocument>> handle(final SinkDocument doc) { |
| 60 | + |
| 61 | + BsonDocument keyDoc = doc.getKeyDoc().orElseGet(BsonDocument::new); |
| 62 | + |
| 63 | + BsonDocument valueDoc = doc.getValueDoc().orElseGet(BsonDocument::new); |
| 64 | + |
| 65 | + if (valueDoc.isEmpty()) { |
| 66 | + LOGGER.debug("skipping attunity tombstone event for kafka topic compaction"); |
| 67 | + return Optional.empty(); |
| 68 | + } |
| 69 | + |
| 70 | + return Optional.ofNullable(getCdcOperation(valueDoc) |
| 71 | + .perform(new SinkDocument(keyDoc, valueDoc))); |
| 72 | + } |
| 73 | + |
| 74 | + static BsonDocument generateFilterDoc(final BsonDocument keyDoc, final BsonDocument valueDoc, final OperationType opType) { |
| 75 | + final boolean checkOpType = |
| 76 | + opType.equals(OperationType.CREATE) || opType.equals(OperationType.READ); |
| 77 | + if (valueDoc.containsKey(JSON_DOC_WRAPPER_FIELD)) { |
| 78 | + if (keyDoc.keySet().isEmpty()) { |
| 79 | + if (checkOpType) { |
| 80 | + // create: no PK info in keyDoc -> generate ObjectId |
| 81 | + return new BsonDocument(ID_FIELD, new BsonObjectId()); |
| 82 | + } |
| 83 | + // update or delete: no PK info in keyDoc -> take everything in 'beforeData' field |
| 84 | + try { |
| 85 | + BsonDocument filter = |
| 86 | + valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).getDocument(JSON_DOC_BEFORE_FIELD); |
| 87 | + if (filter.isEmpty()) { |
| 88 | + throw new BsonInvalidOperationException("value doc beforeData field is empty"); |
| 89 | + } |
| 90 | + return filter; |
| 91 | + } catch (BsonInvalidOperationException exc) { |
| 92 | + throw new DataException( |
| 93 | + "Error: value doc 'beforeData' field is empty or has invalid type" |
| 94 | + + " for update/delete operation which seems severely wrong -> defensive actions taken!", |
| 95 | + exc); |
| 96 | + } |
| 97 | + } |
| 98 | + } else { |
| 99 | + if (keyDoc.keySet().isEmpty()) { |
| 100 | + if (checkOpType) { |
| 101 | + // create: no PK info in keyDoc -> generate ObjectId |
| 102 | + return new BsonDocument(ID_FIELD, new BsonObjectId()); |
| 103 | + } |
| 104 | + // update or delete: no PK info in keyDoc -> take everything in 'beforeData' field |
| 105 | + try { |
| 106 | + BsonDocument filter = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD); |
| 107 | + if (filter.isEmpty()) { |
| 108 | + throw new BsonInvalidOperationException("value doc beforeData field is empty"); |
| 109 | + } |
| 110 | + return filter; |
| 111 | + } catch (BsonInvalidOperationException exc) { |
| 112 | + throw new DataException( |
| 113 | + "Error: value doc 'beforeData' field is empty or has invalid type" |
| 114 | + + " for update/delete operation which seems severely wrong -> defensive actions taken!", |
| 115 | + exc); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // build filter document composed of all PK columns |
| 121 | + BsonDocument pk = new BsonDocument(); |
| 122 | + for (String f : keyDoc.keySet()) { |
| 123 | + pk.put(f, keyDoc.get(f)); |
| 124 | + } |
| 125 | + return new BsonDocument(ID_FIELD, pk); |
| 126 | + } |
| 127 | + |
| 128 | + static BsonDocument generateUpsertOrReplaceDoc(final BsonDocument keyDoc, final BsonDocument valueDoc, final BsonDocument filterDoc) { |
| 129 | + |
| 130 | + BsonDocument afterDoc; |
| 131 | + BsonDocument upsertDoc = new BsonDocument(); |
| 132 | + if (valueDoc.containsKey(JSON_DOC_WRAPPER_FIELD)) { |
| 133 | + if (!valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).containsKey(JSON_DOC_AFTER_FIELD) |
| 134 | + || valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).get(JSON_DOC_AFTER_FIELD).isNull() |
| 135 | + || !valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).get(JSON_DOC_AFTER_FIELD).isDocument() |
| 136 | + || valueDoc |
| 137 | + .getDocument(JSON_DOC_WRAPPER_FIELD) |
| 138 | + .getDocument(JSON_DOC_AFTER_FIELD) |
| 139 | + .isEmpty()) { |
| 140 | + throw new DataException( |
| 141 | + "Error: valueDoc must contain non-empty 'data' field" |
| 142 | + + " of type document for insert/update operation"); |
| 143 | + } |
| 144 | + |
| 145 | + afterDoc = valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).getDocument(JSON_DOC_AFTER_FIELD); |
| 146 | + } else { |
| 147 | + if (!valueDoc.containsKey(JSON_DOC_AFTER_FIELD) |
| 148 | + || valueDoc.get(JSON_DOC_AFTER_FIELD).isNull() |
| 149 | + || !valueDoc.get(JSON_DOC_AFTER_FIELD).isDocument() |
| 150 | + || valueDoc.getDocument(JSON_DOC_AFTER_FIELD).isEmpty()) { |
| 151 | + throw new DataException( |
| 152 | + "Error: valueDoc must contain non-empty 'data' field" |
| 153 | + + " of type document for insert/update operation"); |
| 154 | + } |
| 155 | + |
| 156 | + afterDoc = valueDoc.getDocument(JSON_DOC_AFTER_FIELD); |
| 157 | + } |
| 158 | + |
| 159 | + if (filterDoc.containsKey(ID_FIELD)) { |
| 160 | + upsertDoc.put(ID_FIELD, filterDoc.get(ID_FIELD)); |
| 161 | + } |
| 162 | + |
| 163 | + for (String f : afterDoc.keySet()) { |
| 164 | + upsertDoc.put(f, afterDoc.get(f)); |
| 165 | + } |
| 166 | + return upsertDoc; |
| 167 | + } |
| 168 | + |
| 169 | + static BsonDocument generateUpdateDoc(final BsonDocument keyDoc, final BsonDocument valueDoc, final BsonDocument filterDoc) { |
| 170 | + |
| 171 | + BsonDocument updateDoc = new BsonDocument(); |
| 172 | + BsonDocument updates = new BsonDocument(); |
| 173 | + BsonDocument beforeDoc; |
| 174 | + BsonDocument afterDoc; |
| 175 | + if (valueDoc.containsKey(JSON_DOC_WRAPPER_FIELD)) { |
| 176 | + if (!valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).containsKey(JSON_DOC_AFTER_FIELD) |
| 177 | + || valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).get(JSON_DOC_AFTER_FIELD).isNull() |
| 178 | + || !valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).get(JSON_DOC_AFTER_FIELD).isDocument() |
| 179 | + || valueDoc |
| 180 | + .getDocument(JSON_DOC_WRAPPER_FIELD) |
| 181 | + .getDocument(JSON_DOC_AFTER_FIELD) |
| 182 | + .isEmpty()) { |
| 183 | + throw new DataException( |
| 184 | + "Error: valueDoc must contain non-empty 'data' field" |
| 185 | + + " of type document for insert/update operation"); |
| 186 | + } |
| 187 | + |
| 188 | + beforeDoc = valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).getDocument(JSON_DOC_BEFORE_FIELD); |
| 189 | + afterDoc = valueDoc.getDocument(JSON_DOC_WRAPPER_FIELD).getDocument(JSON_DOC_AFTER_FIELD); |
| 190 | + |
| 191 | + } else { |
| 192 | + if (!valueDoc.containsKey(JSON_DOC_AFTER_FIELD) |
| 193 | + || valueDoc.get(JSON_DOC_AFTER_FIELD).isNull() |
| 194 | + || !valueDoc.get(JSON_DOC_AFTER_FIELD).isDocument() |
| 195 | + || valueDoc.getDocument(JSON_DOC_AFTER_FIELD).isEmpty()) { |
| 196 | + throw new DataException( |
| 197 | + "Error: valueDoc must contain non-empty 'data' field" |
| 198 | + + " of type document for insert/update operation"); |
| 199 | + } |
| 200 | + beforeDoc = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD); |
| 201 | + afterDoc = valueDoc.getDocument(JSON_DOC_AFTER_FIELD); |
| 202 | + } |
| 203 | + |
| 204 | + for (String key : afterDoc.keySet()) { |
| 205 | + if (!afterDoc.get(key).equals(beforeDoc.get(key))) { |
| 206 | + updates.put(key, afterDoc.get(key)); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + updateDoc.append("$set", updates); |
| 211 | + return updateDoc; |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments