nstream.adapter.mongodb.MongoDbIngestingPatch 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.FindIterable;
import nstream.adapter.common.AdapterUtils;
import nstream.adapter.common.schedule.DeferrableException;
import org.bson.Document;
import swim.json.Json;
import swim.structure.Value;
/**
* A concrete but extendable implementation of {@link MongoDbIngestingAgent}
* that uses configuration to query and relay MongoDB {@link Document}s.
*
* @see MongoDbIngestingAgent
* @see MongoDbIngressSettings
*/
public class MongoDbIngestingPatch extends MongoDbIngestingAgent {
public MongoDbIngestingPatch() {
}
/**
* The configurable {@link FindIterable} to be looped through and ingested -
* the find or 'query'.
*
* Can be overridden for bespoke queries.
*
* @return the cursor
*/
protected FindIterable find() {
return this.client.getDatabase(this.ingressSettings.database())
.getCollection(this.ingressSettings.collection())
.find()
.filter(this.ingressSettings.query() == null ? null : Document.parse(this.ingressSettings.query()))
.projection(this.ingressSettings.projection() == null ? null : Document.parse(this.ingressSettings.projection()));
}
@Override
protected void ingest(Document unstructured) throws DeferrableException {
ingest(Json.parse(unstructured.toJson()));
}
/**
* Ingest a 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 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);
}
}