Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.mapper;

import com.mongodb.client.model.CreateViewOptions;

import java.util.Map;

import static io.flamingock.template.mongodb.mapper.MapperUtil.getCollation;

public final class CreateViewOptionsMapper {

private CreateViewOptionsMapper() {}

public static CreateViewOptions map(Map<String, Object> options) {
CreateViewOptions result = new CreateViewOptions();

if (options.containsKey("collation")) {
result.collation(getCollation(options, "collation"));
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.mapper;

import com.mongodb.client.model.RenameCollectionOptions;

import java.util.Map;

import static io.flamingock.template.mongodb.mapper.MapperUtil.getBoolean;

public final class RenameCollectionOptionsMapper {

private RenameCollectionOptionsMapper() {}

public static RenameCollectionOptions map(Map<String, Object> options) {
RenameCollectionOptions result = new RenameCollectionOptions();

if (options.containsKey("dropTarget")) {
result.dropTarget(getBoolean(options, "dropTarget"));
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ public Document getFilter() {
return new Document((Map<String, Object>) parameters.get("filter"));
}

public String getIndexName() {
Object value = parameters.get("indexName");
return value != null ? (String) value : null;
}

public String getTarget() {
return (String) parameters.get("target");
}

@SuppressWarnings("unchecked")
public Document getValidator() {
Object value = parameters.get("validator");
return value != null ? new Document((Map<String, Object>) value) : null;
}

public String getValidationLevel() {
return (String) parameters.get("validationLevel");
}

public String getValidationAction() {
return (String) parameters.get("validationAction");
}

public String getViewOn() {
return (String) parameters.get("viewOn");
}

@SuppressWarnings("unchecked")
public List<Document> getPipeline() {
List<Map<String, Object>> rawPipeline = (List<Map<String, Object>>) parameters.get("pipeline");
return rawPipeline != null
? rawPipeline.stream().map(Document::new).collect(Collectors.toList())
: null;
}

public MongoOperator getOperator(MongoDatabase db) {
return MongoOperationType.getFromValue(getType()).getOperator(db, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.operator.CreateCollectionOperator;
import io.flamingock.template.mongodb.model.operator.CreateIndexOperator;
import io.flamingock.template.mongodb.model.operator.CreateViewOperator;
import io.flamingock.template.mongodb.model.operator.DropCollectionOperator;
import io.flamingock.template.mongodb.model.operator.DropIndexOperator;
import io.flamingock.template.mongodb.model.operator.DropViewOperator;
import io.flamingock.template.mongodb.model.operator.InsertOperator;
import io.flamingock.template.mongodb.model.operator.ModifyCollectionOperator;
import io.flamingock.template.mongodb.model.operator.MongoOperator;
import io.flamingock.template.mongodb.model.operator.RenameCollectionOperator;

import java.util.Arrays;
import java.util.function.BiFunction;
Expand All @@ -28,7 +34,13 @@ public enum MongoOperationType {

CREATE_COLLECTION("createCollection", CreateCollectionOperator::new),
CREATE_INDEX("createIndex", CreateIndexOperator::new),
INSERT("insert", InsertOperator::new);
INSERT("insert", InsertOperator::new),
DROP_COLLECTION("dropCollection", DropCollectionOperator::new),
DROP_INDEX("dropIndex", DropIndexOperator::new),
RENAME_COLLECTION("renameCollection", RenameCollectionOperator::new),
MODIFY_COLLECTION("modifyCollection", ModifyCollectionOperator::new),
CREATE_VIEW("createView", CreateViewOperator::new),
DROP_VIEW("dropView", DropViewOperator::new);

private final String value;
private final BiFunction<MongoDatabase, MongoOperation, MongoOperator> createOperatorFunction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.CreateViewOptions;
import io.flamingock.template.mongodb.mapper.CreateViewOptionsMapper;
import io.flamingock.template.mongodb.model.MongoOperation;

public class CreateViewOperator extends MongoOperator {

public CreateViewOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
CreateViewOptions options = CreateViewOptionsMapper.map(op.getOptions());
mongoDatabase.createView(op.getCollection(), op.getViewOn(), op.getPipeline(), options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.MongoOperation;

public class DropCollectionOperator extends MongoOperator {

public DropCollectionOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
mongoDatabase.getCollection(op.getCollection()).drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.MongoOperation;

public class DropIndexOperator extends MongoOperator {

public DropIndexOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
String indexName = op.getIndexName();
if (indexName != null) {
mongoDatabase.getCollection(op.getCollection()).dropIndex(indexName);
} else {
mongoDatabase.getCollection(op.getCollection()).dropIndex(op.getKeys());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.MongoOperation;

public class DropViewOperator extends MongoOperator {

public DropViewOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
mongoDatabase.getCollection(op.getCollection()).drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.model.operator;

import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import io.flamingock.template.mongodb.model.MongoOperation;
import org.bson.Document;

public class ModifyCollectionOperator extends MongoOperator {

public ModifyCollectionOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
Document command = new Document("collMod", op.getCollection());
if (op.getValidator() != null) {
command.append("validator", op.getValidator());
}
if (op.getValidationLevel() != null) {
command.append("validationLevel", op.getValidationLevel());
}
if (op.getValidationAction() != null) {
command.append("validationAction", op.getValidationAction());
}
mongoDatabase.runCommand(command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.flamingock.template.mongodb.model.operator;

import com.mongodb.MongoNamespace;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.RenameCollectionOptions;
import io.flamingock.template.mongodb.mapper.RenameCollectionOptionsMapper;
import io.flamingock.template.mongodb.model.MongoOperation;

public class RenameCollectionOperator extends MongoOperator {

public RenameCollectionOperator(MongoDatabase mongoDatabase, MongoOperation operation) {
super(mongoDatabase, operation, false);
}

@Override
protected void applyInternal(ClientSession clientSession) {
MongoNamespace target = new MongoNamespace(mongoDatabase.getName(), op.getTarget());
RenameCollectionOptions options = RenameCollectionOptionsMapper.map(op.getOptions());
mongoDatabase.getCollection(op.getCollection()).renameCollection(target, options);
}
}
Loading
Loading