@@ -2,7 +2,7 @@ package dev.mongocamp.driver.mongodb.database
22
33import dev .mongocamp .driver .mongodb ._
44import dev .mongocamp .driver .mongodb .bson .codecs .CustomCodecProvider
5- import org .bson .codecs .configuration .CodecRegistries .{fromProviders , fromRegistries }
5+ import org .bson .codecs .configuration .CodecRegistries .{ fromProviders , fromRegistries }
66import org .bson .codecs .configuration .CodecRegistry
77import org .mongodb .scala .MongoClient .DEFAULT_CODEC_REGISTRY
88import org .mongodb .scala ._
@@ -34,8 +34,7 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
3434 cachedClient = None
3535 }
3636
37- def databases : ListDatabasesObservable [Document ] =
38- client.listDatabases()
37+ def databases : ListDatabasesObservable [Document ] = client.listDatabases()
3938
4039 def databaseInfos : List [DatabaseInfo ] = databases.resultList().map(doc => DatabaseInfo (doc)).sortBy(_.name)
4140
@@ -44,30 +43,32 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
4443 def dropDatabase (databaseName : String = DefaultDatabaseName ): SingleObservable [Void ] = database(databaseName).drop()
4544
4645 def database (databaseName : String = DefaultDatabaseName ): MongoDatabase = {
47- if (! cachedDatabaseMap.contains(databaseName))
46+ if (! cachedDatabaseMap.contains(databaseName)) {
4847 cachedDatabaseMap.put(databaseName, client.getDatabase(databaseName).withCodecRegistry(registry))
48+ }
4949 cachedDatabaseMap(databaseName)
5050 }
5151
52- def addChangeObserver (
53- observer : ChangeObserver [Document ],
54- databaseName : String = DefaultDatabaseName
55- ): ChangeObserver [Document ] = {
52+ def addChangeObserver (observer : ChangeObserver [Document ], databaseName : String = DefaultDatabaseName ): ChangeObserver [Document ] = {
5653 database(databaseName).watch().subscribe(observer)
5754 observer
5855 }
5956
60- def collections (databaseName : String = DefaultDatabaseName ): ListCollectionsObservable [Document ] =
57+ def collections (databaseName : String = DefaultDatabaseName ): ListCollectionsObservable [Document ] = {
6158 database(databaseName).listCollections()
59+ }
6260
63- def collectionInfos (databaseName : String = DefaultDatabaseName ): List [CollectionInfo ] =
61+ def collectionInfos (databaseName : String = DefaultDatabaseName ): List [CollectionInfo ] = {
6462 collections(databaseName).resultList().map(doc => CollectionInfo (doc)).sortBy(_.name)
63+ }
6564
66- def collectionNames (databaseName : String = DefaultDatabaseName ): List [String ] =
65+ def collectionNames (databaseName : String = DefaultDatabaseName ): List [String ] = {
6766 collectionInfos(databaseName).map(info => info.name)
67+ }
6868
69- def runCommand (document : Document , databaseName : String = DefaultDatabaseName ): SingleObservable [Document ] =
69+ def runCommand (document : Document , databaseName : String = DefaultDatabaseName ): SingleObservable [Document ] = {
7070 database(databaseName).runCommand(document)
71+ }
7172
7273 def collectionStatus (
7374 collectionName : String ,
@@ -81,42 +82,51 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
8182 val newCollectionName : String = guessName(collectionName)
8283 database(newDatabaseName).getCollection[A ](newCollectionName)
8384 }
84- else
85+ else {
8586 database().getCollection[A ](collectionName)
87+ }
8688
87- def guessDatabaseName (maybeSeparatedName : String ): String =
88- if (maybeSeparatedName.contains(DatabaseProvider .CollectionSeparator ))
89+ def guessDatabaseName (maybeSeparatedName : String ): String = {
90+ if (maybeSeparatedName.contains(DatabaseProvider .CollectionSeparator )) {
8991 maybeSeparatedName.substring(0 , maybeSeparatedName.indexOf(DatabaseProvider .CollectionSeparator ))
90- else
92+ }
93+ else {
9194 DefaultDatabaseName
95+ }
96+ }
9297
93- def guessName (maybeSeparatedName : String ): String =
94- if (maybeSeparatedName.contains(DatabaseProvider .CollectionSeparator ))
98+ def guessName (maybeSeparatedName : String ): String = {
99+ if (maybeSeparatedName.contains(DatabaseProvider .CollectionSeparator )) {
95100 maybeSeparatedName.substring(maybeSeparatedName.indexOf(DatabaseProvider .CollectionSeparator ) + 1 )
96- else
101+ }
102+ else {
97103 maybeSeparatedName
104+ }
105+ }
98106
99- def bucket (bucketName : String ): GridFSBucket =
107+ def bucket (bucketName : String ): GridFSBucket = {
100108 if (bucketName.contains(DatabaseProvider .CollectionSeparator )) {
101109 val newDatabaseName = guessDatabaseName(bucketName)
102110 val newBucketName = guessName(bucketName)
103111 GridFSBucket (database(newDatabaseName), newBucketName)
104112 }
105- else
113+ else {
106114 GridFSBucket (database(), bucketName)
115+ }
116+ }
107117
108118 def dao (collectionName : String ): MongoDAO [Document ] = {
109- if (! cachedMongoDAOMap.contains(collectionName))
119+ if (! cachedMongoDAOMap.contains(collectionName)) {
110120 cachedMongoDAOMap.put(collectionName, DocumentDao (this , collectionName))
121+ }
111122 cachedMongoDAOMap(collectionName)
112123 }
113124
114125 def cachedDatabaseNames (): List [String ] = cachedDatabaseMap.keys.toList
115126
116127 def cachedCollectionNames (): List [String ] = cachedMongoDAOMap.keys.toList
117128
118- case class DocumentDao (provider : DatabaseProvider , collectionName : String )
119- extends MongoDAO [Document ](this , collectionName)
129+ case class DocumentDao (provider : DatabaseProvider , collectionName : String ) extends MongoDAO [Document ](this , collectionName)
120130
121131}
122132
@@ -126,16 +136,14 @@ object DatabaseProvider {
126136
127137 private val CustomRegistry = fromProviders(CustomCodecProvider ())
128138
129- private val codecRegistry : CodecRegistry =
130- fromRegistries(CustomRegistry , DEFAULT_CODEC_REGISTRY )
139+ private val codecRegistry : CodecRegistry = fromRegistries(CustomRegistry , DEFAULT_CODEC_REGISTRY )
131140
132- def apply (config : MongoConfig , registry : CodecRegistry = codecRegistry): DatabaseProvider =
141+ def apply (config : MongoConfig , registry : CodecRegistry = codecRegistry): DatabaseProvider = {
133142 new DatabaseProvider (config, fromRegistries(registry, CustomRegistry , DEFAULT_CODEC_REGISTRY ))
143+ }
134144
135- def fromPath (
136- configPath : String = MongoConfig .DefaultConfigPathPrefix ,
137- registry : CodecRegistry = codecRegistry
138- ): DatabaseProvider =
145+ def fromPath (configPath : String = MongoConfig .DefaultConfigPathPrefix , registry : CodecRegistry = codecRegistry): DatabaseProvider = {
139146 apply(MongoConfig .fromPath(configPath), fromRegistries(registry, CustomRegistry , DEFAULT_CODEC_REGISTRY ))
147+ }
140148
141149}
0 commit comments