<commit_before>// DBTCP.java

/**
 *      Copyright (C) 2008 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.
 */

package com.mongodb;

import java.io.*;
import java.net.*;
import java.nio.*;
import java.util.*;
import java.util.logging.*;

public class DBTCP extends DBMessageLayer {

    static Logger _logger = Logger.getLogger( "DBTCP" );
    static Logger _createLogger = Logger.getLogger( _logger.getName() + ".connect" );

    public DBTCP( DBAddress addr )
        throws MongoException {
        super( _checkAddress( addr )._name );

        _createLogger.info( addr.toString() );

        if ( addr.isPaired() ){
            _allHosts = new ArrayList<DBAddress>( addr.getPairedAddresses() );
            _validatePairs( _allHosts );
            _pickInitial();
            _createLogger.info( "switch to paired mode : " + _allHosts + " -> " + _curAddress  );
        }
        else {
            _set( addr );
            _allHosts = null;
        }
    }

    public DBTCP( DBAddress ... all )
        throws MongoException {
        this( Arrays.asList( all ) );
    }

    public DBTCP( List<DBAddress> all )
        throws MongoException {
        super( _checkAddress( all )._name );

        _validatePairs( all );

        _allHosts = new ArrayList<DBAddress>( all ); // make a copy so it can't be modified
        _pickInitial();

        _createLogger.info( all  + " -> " + _curAddress );
    }

    private void _validatePairs( List<DBAddress> all ){

        final String name = all.get(0)._name;
        for ( int i=1; i<all.size(); i++ ){
            if ( ! all.get(i)._name.equals( name ) )
                throw new IllegalArgumentException( " names don't match [" + all.get(i)._name + "] != [" + name + "]" );
        }

    }

    private static DBAddress _checkAddress( DBAddress addr ){
        if ( addr == null )
            throw new NullPointerException( "address can't be null" );
        return addr;
    }

    private static DBAddress _checkAddress( List<DBAddress> addrs ){
        if ( addrs == null )
            throw new NullPointerException( "addresses can't be null" );
        if ( addrs.size() == 0 )
            throw new IllegalArgumentException( "need to specify at least 1 address" );
        return addrs.get(0);
    }

    /**
     * Start a "request".
     *
     * A "request" is a group of operations in which order matters. Examples
     * include inserting a document and then performing a query which expects
     * that document to have been inserted, or performing an operation and
     * then using com.mongodb.Mongo.getLastError to perform error-checking
     * on that operation. When a thread performs operations in a "request", all
     * operations will be performed on the same socket, so they will be
     * correctly ordered.
     */
    public void requestStart(){
        _threadPort.get().requestStart();
    }

    /**
     * End the current "request", if this thread is in one.
     *
     * By ending a request when it is safe to do so the built-in connection-
     * pool is allowed to reassign requests to different sockets in order to
     * more effectively balance load. See requestStart for more information.
     */
    public void requestDone(){
        _threadPort.get().requestDone();
    }

    public void requestEnsureConnection(){
        _threadPort.get().requestEnsureConnection();
    }

    protected void say( int op , ByteBuffer buf , WriteConcern concern )
        throws MongoException {
        MyPort mp = _threadPort.get();
        DBPort port = mp.get( true );

        try {
            port.say( new DBMessage( op , buf ) );
            if ( concern == WriteConcern.STRICT ){
                DBObject e = getLastError();
                Object foo = e.get( "err" );
                if ( foo != null )
                    throw new MongoException.DuplicateKey( foo.toString() );
            }
            mp.done( port );
        }
        catch ( IOException ioe ){
            mp.error( ioe );
            _error();
            if ( concern == WriteConcern.NONE )<commit_msg>small fix for duplicate key exception<commit_after>// DBTCP.java

/**
 *      Copyright (C) 2008 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.
 */

package com.mongodb;

import java.io.*;
import java.net.*;
import java.nio.*;
import java.util.*;
import java.util.logging.*;

public class DBTCP extends DBMessageLayer {

    static Logger _logger = Logger.getLogger( "DBTCP" );
    static Logger _createLogger = Logger.getLogger( _logger.getName() + ".connect" );

    public DBTCP( DBAddress addr )
        throws MongoException {
        super( _checkAddress( addr )._name );

        _createLogger.info( addr.toString() );

        if ( addr.isPaired() ){
            _allHosts = new ArrayList<DBAddress>( addr.getPairedAddresses() );
            _validatePairs( _allHosts );
            _pickInitial();
            _createLogger.info( "switch to paired mode : " + _allHosts + " -> " + _curAddress  );
        }
        else {
            _set( addr );
            _allHosts = null;
        }
    }

    public DBTCP( DBAddress ... all )
        throws MongoException {
        this( Arrays.asList( all ) );
    }

    public DBTCP( List<DBAddress> all )
        throws MongoException {
        super( _checkAddress( all )._name );

        _validatePairs( all );

        _allHosts = new ArrayList<DBAddress>( all ); // make a copy so it can't be modified
        _pickInitial();

        _createLogger.info( all  + " -> " + _curAddress );
    }

    private void _validatePairs( List<DBAddress> all ){

        final String name = all.get(0)._name;
        for ( int i=1; i<all.size(); i++ ){
            if ( ! all.get(i)._name.equals( name ) )
                throw new IllegalArgumentException( " names don't match [" + all.get(i)._name + "] != [" + name + "]" );
        }

    }

    private static DBAddress _checkAddress( DBAddress addr ){
        if ( addr == null )
            throw new NullPointerException( "address can't be null" );
        return addr;
    }

    private static DBAddress _checkAddress( List<DBAddress> addrs ){
        if ( addrs == null )
            throw new NullPointerException( "addresses can't be null" );
        if ( addrs.size() == 0 )
            throw new IllegalArgumentException( "need to specify at least 1 address" );
        return addrs.get(0);
    }

    /**
     * Start a "request".
     *
     * A "request" is a group of operations in which order matters. Examples
     * include inserting a document and then performing a query which expects
     * that document to have been inserted, or performing an operation and
     * then using com.mongodb.Mongo.getLastError to perform error-checking
     * on that operation. When a thread performs operations in a "request", all
     * operations will be performed on the same socket, so they will be
     * correctly ordered.
     */
    public void requestStart(){
        _threadPort.get().requestStart();
    }

    /**
     * End the current "request", if this thread is in one.
     *
     * By ending a request when it is safe to do so the built-in connection-
     * pool is allowed to reassign requests to different sockets in order to
     * more effectively balance load. See requestStart for more information.
     */
    public void requestDone(){
        _threadPort.get().requestDone();
    }

    public void requestEnsureConnection(){
        _threadPort.get().requestEnsureConnection();
    }

    protected void say( int op , ByteBuffer buf , WriteConcern concern )
        throws MongoException {
        MyPort mp = _threadPort.get();
        DBPort port = mp.get( true );

        try {
            port.say( new DBMessage( op , buf ) );
            if ( concern == WriteConcern.STRICT ){
                DBObject e = getLastError();
                Object foo = e.get( "err" );
                if ( foo != null ){
                    String s = foo.toString();
                    if ( s.startsWith( "E11000" ) ||
                         s.startsWith( "E11001" ) )
                        throw new MongoException.DuplicateKey( s );
                    throw new MongoException( s );
                }
            }
            mp.done( port );
        }
        catch ( IOException ioe ){
            mp.error( ioe );
            _error();
            if ( concern == WriteConcern.NONE )
{'fraction_non_alphanumeric': 0.10017945740525705, 'fraction_numerical': 0.004222527182518737, 'mean_word_length': 2.1011456628477907, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5426863', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2521, 'n_words': 887}