/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.proxy.transport.mysql.packet.command.query.binary;

import io.shardingsphere.proxy.transport.mysql.packet.command.query.binary.execute.PreparedStatementParameterHeader;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Prepared statement registry.
 *
 * @author zhangliang
 */
@NoArgsConstructor(access = AccessLevel.NONE)
public final class PreparedStatementRegistry {
    
    private static final PreparedStatementRegistry INSTANCE = new PreparedStatementRegistry();
    
    private final ConcurrentMap<String, Integer> sqlToStatementIdMap = new ConcurrentHashMap<>(65535, 1);
    
    private final ConcurrentMap<Integer, String> statementIdToSQLMap = new ConcurrentHashMap<>(65535, 1);
    
    private final ConcurrentMap<Integer, List<PreparedStatementParameterHeader>> statementIdToParameterHeadersMap = new ConcurrentHashMap<>(65535, 1);
    
    private final AtomicInteger sequence = new AtomicInteger();
    
    /**
     * Get prepared statement registry instance.
     * 
     * @return prepared statement registry instance
     */
    public static PreparedStatementRegistry getInstance() {
        return INSTANCE;
    }
    
    /**
     * Register SQL.
     * 
     * @param sql SQL
     * @return statement ID
     */
    // TODO :yonglun thread not safe?
    public int register(final String sql) {
        Integer result = sqlToStatementIdMap.get(sql);
        if (null != result) {
            return result;
        }
        int statementId = sequence.incrementAndGet();
        statementIdToSQLMap.putIfAbsent(statementId, sql);
        sqlToStatementIdMap.putIfAbsent(sql, statementId);
        return statementId;
    }
    
    /**
     * Get SQL.
     *
     * @param statementId statement ID
     * @return SQL
     */
    public String getSQL(final int statementId) {
        return statementIdToSQLMap.get(statementId);
    }
    
    /**
     * Set parameter headers.
     *
     * @param statementId statement ID
     * @param preparedStatementParameterHeaders prepared statement parameter headers
     */
    public void setParameterHeaders(final int statementId, final List<PreparedStatementParameterHeader> preparedStatementParameterHeaders) {
        statementIdToParameterHeadersMap.putIfAbsent(statementId, preparedStatementParameterHeaders);
    }
    
    /**
     * Get parameter header.
     *
     * @param statementId statement ID
     * @return prepared statement parameters
     */
    public PreparedStatementParameterHeader getParameterHeader(final int statementId) {
        return statementIdToParameterHeadersMap.get(statementId).iterator().next();
    }
}

@@ -1094,16 +1094,40 @@
 ngliang%0A
+ * @author zhangyonglun%0A
  */%0A@NoA
@@ -2087,46 +2087,8 @@
  */%0A
-    // TODO :yonglun thread not safe?%0A
     
@@ -2123,24 +2123,78 @@
 ring sql) %7B%0A
+        int statementId = sequence.incrementAndGet();%0A
         Inte
@@ -2197,22 +2197,35 @@
 Integer 
+p
 re
-sult
+viousStatementId
  = sqlTo
@@ -2243,139 +2243,86 @@
 Map.
-get(sql);%0A        if (null != result) %7B%0A            return result;%0A        %7D%0A        int statementId = sequence.incrementAndGet();%0A
+putIfAbsent(sql, statementId);%0A        if (null == previousStatementId) %7B%0A    
     
@@ -2388,58 +2388,66 @@
     
-sqlToStatementIdMap.putIfAbsent(sql, statementId);
+%7D else %7B%0A            return previousStatementId;%0A        %7D
 %0A   

make PreparedStatementRegistry thread safe