<commit_before># --
# Copyright (C) 2008-2009 10gen Inc.
#
# 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.
# ++

require 'socket'
require 'digest/md5'
require 'mutex_m'
require 'mongo/collection'
require 'mongo/message'
require 'mongo/query'
require 'mongo/util/ordered_hash.rb'
require 'mongo/admin'

module XGen
  module Mongo
    module Driver

      # A Mongo database.
      class DB

        SYSTEM_NAMESPACE_COLLECTION = "system.namespaces"
        SYSTEM_INDEX_COLLECTION = "system.indexes"
        SYSTEM_PROFILE_COLLECTION = "system.profile"
        SYSTEM_USER_COLLECTION = "system.users"
        SYSTEM_COMMAND_COLLECTION = "$cmd"

        # Strict mode enforces collection existence checks. When +true+,
        # asking for a collection that does not exist or trying to create a
        # collection that already exists raises an error.
        #
        # Strict mode is off (+false+) by default. Its value can be changed at
        # any time.
        attr_writer :strict

        # Returns the value of the +strict+ flag.
        def strict?; @strict; end

        # The name of the database.
        attr_reader :name

        # Host to which we are currently connected.
        attr_reader :host
        # Port to which we are currently connected.
        attr_reader :port

        # An array of [host, port] pairs.
        attr_reader :nodes

        # The database's socket. For internal (and Cursor) use only.
        attr_reader :socket

        def slave_ok?; @slave_ok; end
        def auto_reconnect?; @auto_reconnect; end

        # A primary key factory object (or +nil+). See the README.doc file or
        # DB#new for details.
        attr_reader :pk_factory

        def pk_factory=(pk_factory)
          raise "error: can not change PK factory" if @pk_factory
          @pk_factory = pk_factory
        end

        # Instances of DB are normally obtained by calling Mongo#db.
        #
        # db_name :: The database name
        #
        # nodes :: An array of [host, port] pairs. See Mongo#new, which offers
        #          a more flexible way of defining nodes.
        #
        # options :: A hash of options.
        #
        # Options:
        #
        # :strict :: If true, collections must exist to be accessed and must
        #            not exist to be created. See #collection and
        #            #create_collection.
        #
        # :pk :: A primary key factory object that must respond to :create_pk,
        #        which should take a hash and return a hash which merges the
        #        original hash with any primary key fields the factory wishes
        #        to inject. (NOTE: if the object already has a primary key,
        #        the factory should not inject a new key; this means that the
        #        object is being used in a repsert but it already exists.) The
        #        idea here is that when ever a record is inserted, the :pk
        #        object's +create_pk+ method will be called and the new hash
        #        returned will be inserted.
        #
        # :slave_ok :: Only used if +nodes+ contains only one host/port. If
        #              false, when connecting to that host/port we check to
        #              see if the server is the master. If it is not, an error
        #              is thrown.
        #
        # :auto_reconnect :: If the connection gets closed (for example, we
        #                    have a server pair and saw the "not master"
        #                    error, which closes the connection), then
        #                    automatically try to reconnect to the master or
        #                    to the single server we have been given. Defaults
        #                    to +false+.
        #
        # When a DB object first connects to a pair, it will find the master
        # instance and connect to that one. On socket error or if we recieve a
        # "not master" error, we again find the master of the pair.
        def initialize(db_name, nodes, options={})
          raise "Invalid DB name \"#{db_name}\" (must be non-nil, non-zero-length, and can not contain \".\")" if !db_name || (db_name && db_name.length > 0 && db_name.include?("."))
          @name, @nodes = db_name, nodes
          @strict = options[:strict]
          @pk_factory = options[:pk]
          @slave_ok = options[:slave_ok] && @nodes.length == 1 # only OK if one node
          @auto_reconnect = options[:auto_reconnect]
          @semaphore = Object.new
          @semaphore.extend Mutex_m
          @socket = nil
          connect_to_master
        end

        def connect_to_master
          close if @socket
          @host = @port = nil
          @nodes.detect { |hp|
            @host, @port = *hp
            begin
              @socket = TCPSocket.new(@host, @port)
              @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

              # Check for master. Can't call master? because it uses mutex,
              # which may already be in use during this call.
              semaphore_is_locked = @semaphore.locked?
              @semaphore.unlock if semaphore_is_locked
              is_master = master?
              @semaphore.lock if semaphore_is_locked

              break if @slave_ok || is_master
            rescue SocketError => ex
              close if @socket
            end
            @socket
          }
          raise "error: failed to connect to any given host:port" unless @socket<commit_msg>rescue a few more types of exceptions<commit_after># --
# Copyright (C) 2008-2009 10gen Inc.
#
# 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.
# ++

require 'socket'
require 'digest/md5'
require 'mutex_m'
require 'mongo/collection'
require 'mongo/message'
require 'mongo/query'
require 'mongo/util/ordered_hash.rb'
require 'mongo/admin'

module XGen
  module Mongo
    module Driver

      # A Mongo database.
      class DB

        SYSTEM_NAMESPACE_COLLECTION = "system.namespaces"
        SYSTEM_INDEX_COLLECTION = "system.indexes"
        SYSTEM_PROFILE_COLLECTION = "system.profile"
        SYSTEM_USER_COLLECTION = "system.users"
        SYSTEM_COMMAND_COLLECTION = "$cmd"

        # Strict mode enforces collection existence checks. When +true+,
        # asking for a collection that does not exist or trying to create a
        # collection that already exists raises an error.
        #
        # Strict mode is off (+false+) by default. Its value can be changed at
        # any time.
        attr_writer :strict

        # Returns the value of the +strict+ flag.
        def strict?; @strict; end

        # The name of the database.
        attr_reader :name

        # Host to which we are currently connected.
        attr_reader :host
        # Port to which we are currently connected.
        attr_reader :port

        # An array of [host, port] pairs.
        attr_reader :nodes

        # The database's socket. For internal (and Cursor) use only.
        attr_reader :socket

        def slave_ok?; @slave_ok; end
        def auto_reconnect?; @auto_reconnect; end

        # A primary key factory object (or +nil+). See the README.doc file or
        # DB#new for details.
        attr_reader :pk_factory

        def pk_factory=(pk_factory)
          raise "error: can not change PK factory" if @pk_factory
          @pk_factory = pk_factory
        end

        # Instances of DB are normally obtained by calling Mongo#db.
        #
        # db_name :: The database name
        #
        # nodes :: An array of [host, port] pairs. See Mongo#new, which offers
        #          a more flexible way of defining nodes.
        #
        # options :: A hash of options.
        #
        # Options:
        #
        # :strict :: If true, collections must exist to be accessed and must
        #            not exist to be created. See #collection and
        #            #create_collection.
        #
        # :pk :: A primary key factory object that must respond to :create_pk,
        #        which should take a hash and return a hash which merges the
        #        original hash with any primary key fields the factory wishes
        #        to inject. (NOTE: if the object already has a primary key,
        #        the factory should not inject a new key; this means that the
        #        object is being used in a repsert but it already exists.) The
        #        idea here is that when ever a record is inserted, the :pk
        #        object's +create_pk+ method will be called and the new hash
        #        returned will be inserted.
        #
        # :slave_ok :: Only used if +nodes+ contains only one host/port. If
        #              false, when connecting to that host/port we check to
        #              see if the server is the master. If it is not, an error
        #              is thrown.
        #
        # :auto_reconnect :: If the connection gets closed (for example, we
        #                    have a server pair and saw the "not master"
        #                    error, which closes the connection), then
        #                    automatically try to reconnect to the master or
        #                    to the single server we have been given. Defaults
        #                    to +false+.
        #
        # When a DB object first connects to a pair, it will find the master
        # instance and connect to that one. On socket error or if we recieve a
        # "not master" error, we again find the master of the pair.
        def initialize(db_name, nodes, options={})
          raise "Invalid DB name \"#{db_name}\" (must be non-nil, non-zero-length, and can not contain \".\")" if !db_name || (db_name && db_name.length > 0 && db_name.include?("."))
          @name, @nodes = db_name, nodes
          @strict = options[:strict]
          @pk_factory = options[:pk]
          @slave_ok = options[:slave_ok] && @nodes.length == 1 # only OK if one node
          @auto_reconnect = options[:auto_reconnect]
          @semaphore = Object.new
          @semaphore.extend Mutex_m
          @socket = nil
          connect_to_master
        end

        def connect_to_master
          close if @socket
          @host = @port = nil
          @nodes.detect { |hp|
            @host, @port = *hp
            begin
              @socket = TCPSocket.new(@host, @port)
              @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

              # Check for master. Can't call master? because it uses mutex,
              # which may already be in use during this call.
              semaphore_is_locked = @semaphore.locked?
              @semaphore.unlock if semaphore_is_locked
              is_master = master?
              @semaphore.lock if semaphore_is_locked

              break if @slave_ok || is_master
            rescue SocketError, SystemCallError, IOError => ex
              close if @socket
            end
            @socket
          }
          raise "error: failed to connect to any given host:port" unless @socket
{'fraction_non_alphanumeric': 0.07242524916943521, 'fraction_numerical': 0.002990033222591362, 'mean_word_length': 1.9105632100555958, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '456400', 'n_tokens_mistral': 3227, 'n_tokens_neox': 3140, 'n_words': 1305}