<commit_before>//
//  Database.swift
//  MongoKitten
//
//  Created by <NAME> on 24-05-18.
//

import BSON
import Foundation
import NIO

/// A reference to a MongoDB database, over a `Connection`.
///
/// Databases hold collections of documents.
public final class Database: FutureConvenienceCallable {
    /// The name of the database
    public let name: String
    
    /// The connection the database instance uses
    let session: ClientSession
    
    /// The collection to execute commands on
    internal var cmd: Collection {
        return self["$cmd"]
    }
    
    /// The ObjectId generator tied to this datatabase
    public var objectIdGenerator: ObjectIdGenerator {
        return session.cluster.sharedGenerator
    }
    
    /// The NIO event loop.
    public var eventLoop: EventLoop {
        return session.cluster.eventLoop
    }
    
    /// A helper method that uses the normal `connect` method and awaits it. It creates an event loop group for you.
    ///
    /// It is not recommended to use `synchronousConnect` in a NIO environment (like Vapor 3), as it will create an event loop group for you.
    ///
    /// - parameter uri: A MongoDB URI that contains at least a database component
    /// - throws: Can throw for a variety of reasons, including an invalid connection string, failure to connect to the MongoDB database, etcetera.
    /// - returns: A connected database instance
    public static func synchronousConnect(_ uri: String) throws -> Database {
        let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        
        return try self.connect(uri, on: group.next()).wait()
    }
    
    /// Connect to the database at the given `uri`
    ///
    /// - parameter uri: A MongoDB URI that contains at least a database component
    /// - parameter loop: An EventLoop from NIO. If you want to use MongoKitten in a synchronous / non-NIO environment, use the `synchronousConnect` method.
    public static func connect(_ uri: String, on loop: EventLoop) -> EventLoopFuture<Database> {
        do {
            let settings = try ConnectionSettings(uri)
            
            return connect(settings: settings, on: loop)
        } catch {
            return loop.newFailedFuture(error: error)
        }
    }
    
    /// Connect to the database with the given settings. You can also use `connect(_:on:)` to connect by using a connection string.
    ///
    /// - parameter settings: The connection settings, which must include a database name
    /// - parameter loop: An EventLoop from NIO. If you want to use MongoKitten in a synchronous / non-NIO environment, use the `synchronousConnect` method.
    public static func connect(settings: ConnectionSettings, on loop: EventLoop) -> EventLoopFuture<Database> {
        do {
            guard let targetDatabase = settings.targetDatabase else {
                throw MongoKittenError(.unableToConnect, reason: .noTargetDatabaseSpecified)
            }
            
            return Cluster.connect(on: loop, settings: settings).map { cluster in
                return cluster[targetDatabase]
            }
        } catch {
            return loop.newFailedFuture(error: error)
        }
    }
    
    internal init(named name: String, session: ClientSession) {
        self.name = name
        self.session = session
    }
    
    /// Get a `Collection` by providing a collection name as a `String`
    ///
    /// - parameter collection: The collection/bucket to return
    ///
    /// - returns: The requested collection in this database
    public subscript(collection: String) -> MongoKitten.Collection {
        return Collection(named: collection, in: self)
    }
    
    /// Drops the current database, deleting the associated data files
    ///
    /// - see: https://docs.mongodb.com/manual/reference/command/dropDatabase
    public func drop() -> EventLoopFuture<Void> {<commit_msg>Lazy cluster connection on Database<commit_after>//
//  Database.swift
//  MongoKitten
//
//  Created by <NAME> on 24-05-18.
//

import BSON
import Foundation
import NIO

/// A reference to a MongoDB database, over a `Connection`.
///
/// Databases hold collections of documents.
public final class Database: FutureConvenienceCallable {
    /// The name of the database
    public let name: String
    
    /// The connection the database instance uses
    let session: ClientSession
    
    /// The collection to execute commands on
    internal var cmd: Collection {
        return self["$cmd"]
    }
    
    /// The ObjectId generator tied to this datatabase
    public var objectIdGenerator: ObjectIdGenerator {
        return session.cluster.sharedGenerator
    }
    
    /// The NIO event loop.
    public var eventLoop: EventLoop {
        return session.cluster.eventLoop
    }
    
    /// A helper method that uses the normal `connect` method and awaits it. It creates an event loop group for you.
    ///
    /// It is not recommended to use `synchronousConnect` in a NIO environment (like Vapor 3), as it will create an event loop group for you.
    ///
    /// - parameter uri: A MongoDB URI that contains at least a database component
    /// - throws: Can throw for a variety of reasons, including an invalid connection string, failure to connect to the MongoDB database, etcetera.
    /// - returns: A connected database instance
    public static func synchronousConnect(_ uri: String) throws -> Database {
        let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        
        return try self.connect(uri, on: group.next()).wait()
    }
    
    /// Connect to the database at the given `uri`
    ///
    /// - parameter uri: A MongoDB URI that contains at least a database component
    /// - parameter loop: An EventLoop from NIO. If you want to use MongoKitten in a synchronous / non-NIO environment, use the `synchronousConnect` method.
    public static func connect(_ uri: String, on loop: EventLoop) -> EventLoopFuture<Database> {
        do {
            let settings = try ConnectionSettings(uri)
            
            return connect(settings: settings, on: loop)
        } catch {
            return loop.newFailedFuture(error: error)
        }
    }
    
    
    public static func lazyConnect(_ uri: String, on loop: EventLoop) throws -> Database {
        let settings = try ConnectionSettings(uri)
        
        return try lazyConnect(settings: settings, on: loop)
    }
    
    /// Connect to the database with the given settings. You can also use `connect(_:on:)` to connect by using a connection string.
    ///
    /// - parameter settings: The connection settings, which must include a database name
    /// - parameter loop: An EventLoop from NIO. If you want to use MongoKitten in a synchronous / non-NIO environment, use the `synchronousConnect` method.
    public static func connect(settings: ConnectionSettings, on loop: EventLoop) -> EventLoopFuture<Database> {
        do {
            guard let targetDatabase = settings.targetDatabase else {
                throw MongoKittenError(.unableToConnect, reason: .noTargetDatabaseSpecified)
            }
            
            return Cluster.connect(on: loop, settings: settings).map { cluster in
                return cluster[targetDatabase]
            }
        } catch {
            return loop.newFailedFuture(error: error)
        }
    }
    
    public static func lazyConnect(settings: ConnectionSettings, on loop: EventLoop) throws -> Database {
        guard let targetDatabase = settings.targetDatabase else {
            throw MongoKittenError(.unableToConnect, reason: .noTargetDatabaseSpecified)
        }
        
        return try Cluster.lazyConnect(on: loop, settings: settings)[targetDatabase]
    }

    internal init(named name: String, session: ClientSession) {
        self.name = name
        self.session = session
    }
    
    /// Get a `Collection` by providing a collection name as a `String`
    ///
    /// - parameter collection: The collection/bucket to return
    ///
    /// - returns: The requested collection in this database
    public subscript(collection: String) -> MongoKitten.Collection {
        return Collection(named: collection, in: self)
    }
    
    /// Drops the current database, deleting the associated data files
    ///
    /// - see: https://docs.mongodb.com/manual/reference/command/dropDatabase
    public func drop() -> EventLoopFuture<Void> {
{'fraction_non_alphanumeric': 0.0849146110056926, 'fraction_numerical': 0.0018975332068311196, 'mean_word_length': 2.384028892455859, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5917062', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2093, 'n_words': 834}