/* * Copyright 2015-2016 OpenCB * * 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 org.opencb.opencga.catalog.db.mongodb; import com.mongodb.client.model.Accumulators; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Projections; import org.bson.Document; import org.bson.conversions.Bson; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; import org.opencb.commons.datastore.core.QueryResult; import org.opencb.commons.datastore.mongodb.MongoDBCollection; import org.opencb.opencga.catalog.db.AbstractDBAdaptor; import org.slf4j.Logger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Created by imedina on 08/01/16. */ @Deprecated public class AbstractMongoDBAdaptor extends AbstractDBAdaptor { protected AbstractMongoDBAdaptor(Logger logger) { super(logger); } protected void createOrQuery(Query query, String queryParam, String mongoDbField, List<Bson> andBsonList) { if (query != null && query.getString(queryParam) != null && !query.getString(queryParam).isEmpty()) { createOrQuery(query.getAsStringList(queryParam), mongoDbField, andBsonList); } } protected void createOrQuery(List<String> queryValues, String mongoDbField, List<Bson> andBsonList) { if (queryValues.size() == 1) { andBsonList.add(Filters.eq(mongoDbField, queryValues.get(0))); } else { List<Bson> orBsonList = new ArrayList<>(queryValues.size()); for (String queryItem : queryValues) { orBsonList.add(Filters.eq(mongoDbField, queryItem)); } andBsonList.add(Filters.or(orBsonList)); } } protected QueryResult groupBy(MongoDBCollection collection, Bson query, String groupByField, String idField, QueryOptions options) { if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); } if (groupByField.contains(",")) { // call to multiple groupBy if commas are present return groupBy(collection, query, Arrays.asList(groupByField.split(",")), idField, options); } else { Bson match = Aggregates.match(query); Bson project = Aggregates.project(Projections.include(groupByField, idField)); Bson group; if (options.getBoolean("count", false)) { group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1)); } else { group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + idField)); } return collection.aggregate(Arrays.asList(match, project, group), options); } } protected QueryResult groupBy(MongoDBCollection collection, Bson query, List<String> groupByField, String idField, QueryOptions options) { if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); } if (groupByField.size() == 1) { // if only one field then we call to simple groupBy return groupBy(collection, query, groupByField.get(0), idField, options); } else { Bson match = Aggregates.match(query); // add all group-by fields to the projection together with the aggregation field name List<String> groupByFields = new ArrayList<>(groupByField); groupByFields.add(idField); Bson project = Aggregates.project(Projections.include(groupByFields)); // _id document creation to have the multiple id Document id = new Document(); for (String s : groupByField) { id.append(s, "$" + s); } Bson group; if (options.getBoolean("count", false)) { group = Aggregates.group(id, Accumulators.sum("count", 1)); } else { group = Aggregates.group(id, Accumulators.addToSet("features", "$" + idField)); } return collection.aggregate(Arrays.asList(match, project, group), options); } } }