nstream.adapter.mongodb.MongoDbChangeStreamIngestingPatch Maven / Gradle / Ivy
Show all versions of nstream-adapter-mongodb Show documentation
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nstream.adapter.mongodb;
import com.mongodb.client.ChangeStreamIterable;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import nstream.adapter.common.AdapterUtils;
import nstream.adapter.common.schedule.DeferrableException;
import org.bson.Document;
import swim.structure.Value;
/**
* A concrete but extendable implementation of {@link MongoDbIngestingAgent}
* that uses configuration to stream and relay changes made to a MongoDB
* collection.
*
* @see MongoDbChangeStreamIngestingAgent
* @see MongoDbIngressSettings
*/
public class MongoDbChangeStreamIngestingPatch extends MongoDbChangeStreamIngestingAgent {
public MongoDbChangeStreamIngestingPatch() {
}
/**
* The configurable {@link ChangeStreamIterable} to be looped through and ingested -
* the find or 'query'.
*
* Can be overridden for bespoke queries.
*
* @return the change stream iterable
*/
@Override
protected ChangeStreamIterable changeStream() {
return this.client.getDatabase(this.ingressSettings.database())
.getCollection(this.ingressSettings.collection())
.watch();
}
@Override
public void ingest(ChangeStreamDocument document) throws DeferrableException {
ingest(MongoDbAdapterUtils.structureChangeDocument(document));
}
/**
* Ingest a change document after it has been structured into a {@link Value} object.
* Will relay the message as configured by the relay schema in the settings.
*
* Can be overridden for custom ingestion.
*
* @param structured the received change document structured into a {@link Value}
* @throws DeferrableException an exception that will be handled without aborting ingestion
*/
protected void ingest(Value structured) throws DeferrableException {
AdapterUtils.ingressDslRelay(this.ingressSettings.relaySchema(),
agentContext(), structured);
}
}