A generic (type-safe) wrapper around mongodb collections @morphia.internal @hidden
| 95 | * @hidden |
| 96 | */ |
| 97 | @MorphiaInternal |
| 98 | @SuppressWarnings({ "unchecked", "rawtypes", "removal" }) |
| 99 | public class DatastoreImpl implements AdvancedDatastore { |
| 100 | private static final Logger LOG = LoggerFactory.getLogger(Datastore.class); |
| 101 | |
| 102 | private static final MongoDriverInformation DRIVER_INFO = buildDriverInfo(); |
| 103 | |
| 104 | private static MongoDriverInformation buildDriverInfo() { |
| 105 | MongoDriverInformation.Builder builder = MongoDriverInformation.builder().driverName("Morphia"); |
| 106 | String version = DatastoreImpl.class.getPackage().getImplementationVersion(); |
| 107 | if (version != null) { |
| 108 | builder.driverVersion(version); |
| 109 | } |
| 110 | return builder.build(); |
| 111 | } |
| 112 | |
| 113 | private static void appendMongoClientMetadata(MongoClient mongoClient) { |
| 114 | try { |
| 115 | mongoClient.appendMetadata(DRIVER_INFO); |
| 116 | } catch (Throwable e) { |
| 117 | // appendMetadata is only available in driver 5.6+ |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private final MongoClient mongoClient; |
| 122 | private final Mapper mapper; |
| 123 | private final QueryFactory queryFactory; |
| 124 | private final CodecRegistry codecRegistry; |
| 125 | public List<MorphiaCodecProvider> morphiaCodecProviders = new ArrayList<>(); |
| 126 | private ClassLoader classLoader; |
| 127 | private MongoDatabase database; |
| 128 | private DatastoreOperations operations; |
| 129 | |
| 130 | public DatastoreImpl(MongoClient client, MorphiaConfig config) { |
| 131 | this(client, config, Thread.currentThread().getContextClassLoader()); |
| 132 | } |
| 133 | |
| 134 | public DatastoreImpl(MongoClient client, MorphiaConfig config, ClassLoader classLoader) { |
| 135 | this.classLoader = classLoader; |
| 136 | this.mongoClient = client; |
| 137 | appendMongoClientMetadata(client); |
| 138 | this.database = mongoClient.getDatabase(config.database()); |
| 139 | this.mapper = new Mapper(config, classLoader); |
| 140 | this.queryFactory = mapper.getConfig().queryFactory(); |
| 141 | importModels(); |
| 142 | |
| 143 | codecRegistry = buildRegistry(); |
| 144 | |
| 145 | this.database = database.withCodecRegistry(this.codecRegistry); |
| 146 | operations = new CollectionOperations(); |
| 147 | |
| 148 | config.packages().forEach(packageName -> { |
| 149 | Sofia.logMappingPackage(packageName); |
| 150 | mapper.map(packageName); |
| 151 | }); |
| 152 | if (config.applyCaps()) { |
| 153 | applyCaps(); |
| 154 | } |
nothing calls this directly
no test coverage detected