Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015 HaiYang Li
*
* 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 com.landawn.abacus.util;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.Document;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.DirtyMarker;
import com.landawn.abacus.parser.JSONParser;
import com.landawn.abacus.parser.ParserFactory;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.u.Optional;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
/**
* It's a simple wrapper of MongoDB Java client.
*
* We recommend to define "id" property in java entity/bean as the object "_id" in MongoDB to keep things as simple as possible.
*
*
* @since 0.8
*
* @author Haiyang Li
*
* @see com.mongodb.client.model.Filters
* @see com.mongodb.client.model.Aggregates
* @see com.mongodb.client.model.Accumulators
* @see com.mongodb.client.model.Projections
* @see com.mongodb.client.model.Sorts
*/
public final class MongoDB {
/**
* It's name of object id set in Map/Object array.
*/
public static final String _ID = "_id";
/**
* Property name of id.
*/
public static final String ID = "id";
private static final JSONParser jsonParser = ParserFactory.createJSONParser();
// private static CodecRegistry codecRegistry = CodecRegistries.fromCodecs(new CalendarCodec(), new TimeCodec(), new TimestampCodec());
private static final CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(), new GeneralCodecRegistry());
private static final Map, Method> classIdSetMethodPool = new ConcurrentHashMap<>();
private final Map collExecutorPool = new ConcurrentHashMap<>();
private final Map, MongoCollectionMapper>> collMapperPool = new ConcurrentHashMap<>();
private final MongoDatabase mongoDB;
private final AsyncExecutor asyncExecutor;
public MongoDB(final MongoDatabase mongoDB) {
this(mongoDB, new AsyncExecutor(64, 300, TimeUnit.SECONDS));
}
public MongoDB(final MongoDatabase mongoDB, final AsyncExecutor asyncExecutor) {
this.mongoDB = mongoDB.withCodecRegistry(codecRegistry);
this.asyncExecutor = asyncExecutor;
}
public MongoDatabase db() {
return mongoDB;
}
public MongoCollection collection(final String collectionName) {
return mongoDB.getCollection(collectionName);
}
public MongoCollection collection(final Class targetClass, final String collectionName) {
return mongoDB.getCollection(collectionName, targetClass);
}
public MongoCollectionExecutor collExecutor(final String collectionName) {
MongoCollectionExecutor collExecutor = collExecutorPool.get(collectionName);
if (collExecutor == null) {
synchronized (collExecutorPool) {
collExecutor = collExecutorPool.get(collectionName);
if (collExecutor == null) {
collExecutor = new MongoCollectionExecutor(this, mongoDB.getCollection(collectionName), asyncExecutor);
collExecutorPool.put(collectionName, collExecutor);
}
}
}
return collExecutor;
}
public MongoCollectionMapper collMapper(final Class targetClass) {
return collMapper(targetClass, ClassUtil.getSimpleClassName(targetClass));
}
@SuppressWarnings("rawtypes")
public MongoCollectionMapper collMapper(final Class targetClass, String collectionName) {
N.checkArgNotNull(targetClass, "targetClass");
N.checkArgNotNull(collectionName, "collectionName");
MongoCollectionMapper collMapper = collMapperPool.get(targetClass);
if (collMapper == null) {
synchronized (collMapperPool) {
collMapper = collMapperPool.get(targetClass);
if (collMapper == null) {
collMapper = new MongoCollectionMapper(collExecutor(collectionName), targetClass);
collMapperPool.put(targetClass, collMapper);
}
}
}
return collMapper;
}
/**
* The object id ("_id") property will be read from/write to the specified property
* @param cls
* @param idPropertyName
*/
public static void registerIdProeprty(final Class> cls, final String idPropertyName) {
if (ClassUtil.getPropGetMethod(cls, idPropertyName) == null || ClassUtil.getPropSetMethod(cls, idPropertyName) == null) {
throw new IllegalArgumentException("The specified class: " + ClassUtil.getCanonicalClassName(cls)
+ " doesn't have getter or setter method for the specified id propery: " + idPropertyName);
}
final Method setMethod = ClassUtil.getPropSetMethod(cls, idPropertyName);
final Class> parameterType = setMethod.getParameterTypes()[0];
if (!(String.class.isAssignableFrom(parameterType) || ObjectId.class.isAssignableFrom(parameterType))) {
throw new IllegalArgumentException(
"The parameter type of the specified id setter method must be 'String' or 'ObjectId': " + setMethod.toGenericString());
}
classIdSetMethodPool.put(cls, setMethod);
}
public static DataSet extractData(final MongoIterable> findIterable) {
return extractData(Map.class, findIterable);
}
/**
*
* @param targetClass an entity class with getter/setter method or Map.class/Document.class
* @param findIterable
* @return
*/
public static DataSet extractData(final Class> targetClass, final MongoIterable> findIterable) {
return extractData(targetClass, null, findIterable);
}
/**
*
* @param targetClass an entity class with getter/setter method or Map.class/Document.class
* @param selectPropNames
* @param findIterable
* @return
*/
static DataSet extractData(final Class> targetClass, final Collection selectPropNames, final MongoIterable> findIterable) {
checkTargetClass(targetClass);
final List