You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/01/27 13:24:20 UTC

[1/2] ignite git commit: IGNITE-1786: Minor reworks in OdbcCommandHandler.

Repository: ignite
Updated Branches:
  refs/heads/ignite-1786 8bd28cd0a -> d55dc3690


IGNITE-1786: Minor reworks in OdbcCommandHandler.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/8b16efcd
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/8b16efcd
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/8b16efcd

Branch: refs/heads/ignite-1786
Commit: 8b16efcd7cf363338d2124ccfca3c4b93d89dbac
Parents: 8bd28cd
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Wed Jan 27 13:56:43 2016 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Wed Jan 27 13:56:43 2016 +0300

----------------------------------------------------------------------
 .../processors/odbc/OdbcCommandHandler.java     | 178 +++++++++----------
 1 file changed, 81 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/8b16efcd/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcCommandHandler.java
index 4073168..48851ca 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcCommandHandler.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.processors.odbc;
 
 import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.cache.query.QueryCursor;
 import org.apache.ignite.cache.query.SqlFieldsQuery;
@@ -25,6 +26,7 @@ import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.processors.cache.QueryCursorImpl;
 import org.apache.ignite.internal.processors.query.GridQueryFieldMetadata;
 import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor;
+import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.lang.IgniteBiTuple;
 
 import java.util.*;
@@ -37,19 +39,21 @@ import static org.apache.ignite.internal.processors.odbc.OdbcRequest.*;
  * SQL query handler.
  */
 public class OdbcCommandHandler {
+    /** Query ID sequence. */
+    private static final AtomicLong qryIdGen = new AtomicLong();
+
     /** Kernel context. */
-    protected final GridKernalContext ctx;
+    private final GridKernalContext ctx;
 
     /** Logger. */
-    protected final IgniteLogger log;
-
-    /** Query ID sequence. */
-    private static final AtomicLong qryIdGen = new AtomicLong();
+    private final IgniteLogger log;
 
     /** Current queries cursors. */
     private final ConcurrentHashMap<Long, IgniteBiTuple<QueryCursor, Iterator>> qryCurs = new ConcurrentHashMap<>();
 
     /**
+     * Constructor.
+     *
      * @param ctx Context.
      */
     public OdbcCommandHandler(GridKernalContext ctx) {
@@ -59,108 +63,42 @@ public class OdbcCommandHandler {
     }
 
     /**
+     * Handle request.
+     *
      * @param req Request.
      * @return Response.
+     * @throws IgniteCheckedException If failed.
      */
-    public OdbcResponse handle(OdbcRequest req) {
+    public OdbcResponse handle(OdbcRequest req) throws IgniteCheckedException {
         assert req != null;
 
         switch (req.command()) {
-            case EXECUTE_SQL_QUERY: {
-                return executeQuery((OdbcQueryExecuteRequest)req, qryCurs);
-            }
+            case EXECUTE_SQL_QUERY:
+                return executeQuery((OdbcQueryExecuteRequest)req);
 
-            case FETCH_SQL_QUERY: {
-                return fetchQuery((OdbcQueryFetchRequest)req, qryCurs);
-            }
+            case FETCH_SQL_QUERY:
+                return fetchQuery((OdbcQueryFetchRequest)req);
 
-            case CLOSE_SQL_QUERY: {
-                return closeQuery((OdbcQueryCloseRequest)req, qryCurs);
-            }
+            case CLOSE_SQL_QUERY:
+                return closeQuery((OdbcQueryCloseRequest)req);
 
-            case GET_COLUMNS_META: {
+            case GET_COLUMNS_META:
                 return getColumnsMeta((OdbcQueryGetColumnsMetaRequest) req);
-            }
 
-            case GET_TABLES_META: {
+            case GET_TABLES_META:
                 return getTablesMeta((OdbcQueryGetTablesMetaRequest) req);
-            }
         }
 
         return null;
     }
 
     /**
-     * @param cur Current cursor.
-     * @param req Sql fetch request.
-     * @param qryId Query id.
-     * @return Query result with items.
-     */
-    private static OdbcQueryFetchResult createQueryResult(Iterator cur, OdbcQueryFetchRequest req, Long qryId) {
-
-        List<Object> items = new ArrayList<>();
-
-        for (int i = 0; i < req.pageSize() && cur.hasNext(); ++i)
-            items.add(cur.next());
-
-        return new OdbcQueryFetchResult(qryId, items, !cur.hasNext());
-    }
-
-    /**
-     * Convert metadata in collection from {@link GridQueryFieldMetadata} to
-     * {@link OdbcColumnMeta}.
-     *
-     * @param meta Internal query field metadata.
-     * @return Odbc query field metadata.
-     */
-    private static Collection<OdbcColumnMeta> convertMetadata(Collection<?> meta) {
-        List<OdbcColumnMeta> res = new ArrayList<>();
-
-        if (meta != null) {
-            for (Object info : meta) {
-                assert info instanceof GridQueryFieldMetadata;
-
-                res.add(new OdbcColumnMeta((GridQueryFieldMetadata)info));
-            }
-        }
-
-        return res;
-    }
-
-    /**
-     * Checks whether string matches SQL pattern.
-     *
-     * @param str String.
-     * @param ptrn Pattern.
-     * @return Whether string matches pattern.
-     */
-    private boolean matches(String str, String ptrn) {
-        return str != null && (ptrn == null || ptrn.isEmpty() ||
-                str.toUpperCase().matches(ptrn.toUpperCase().replace("%", ".*").replace("_", ".")));
-    }
-
-    /**
-     * Remove quotation marks at the beginning and end of the string if present.
-     *
-     * @param str Input string.
-     * @return String without leading and trailing quotation marks.
-     */
-    private String RemoveQuotationMarksIfNeeded(String str) {
-        if (str.startsWith("\"") && str.endsWith("\""))
-            return str.substring(1, str.length() - 1);
-
-        return str;
-    }
-
-    /**
      * {@link OdbcQueryExecuteRequest} command handler.
      *
      * @param req Execute query request.
-     * @param qryCurs Queries cursors.
      * @return Response.
      */
-    private OdbcResponse executeQuery(OdbcQueryExecuteRequest req,
-                                      ConcurrentHashMap<Long, IgniteBiTuple<QueryCursor, Iterator>> qryCurs) {
+    private OdbcResponse executeQuery(OdbcQueryExecuteRequest req) {
         long qryId = qryIdGen.getAndIncrement();
 
         try {
@@ -172,7 +110,7 @@ public class OdbcCommandHandler {
 
             if (cache == null)
                 return new OdbcResponse(OdbcResponse.STATUS_FAILED,
-                        "Failed to find cache with name: " + req.cacheName());
+                    "Failed to find cache with name: " + req.cacheName());
 
             QueryCursor qryCur = cache.query(qry);
 
@@ -199,17 +137,14 @@ public class OdbcCommandHandler {
      * {@link OdbcQueryCloseRequest} command handler.
      *
      * @param req Execute query request.
-     * @param qryCurs Queries cursors.
      * @return Response.
      */
-    private OdbcResponse closeQuery(OdbcQueryCloseRequest req,
-                                    ConcurrentHashMap<Long, IgniteBiTuple<QueryCursor, Iterator>> qryCurs) {
+    private OdbcResponse closeQuery(OdbcQueryCloseRequest req) {
         try {
             QueryCursor cur = qryCurs.get(req.queryId()).get1();
 
             if (cur == null)
-                return new OdbcResponse(OdbcResponse.STATUS_FAILED,
-                        "Failed to find query with ID: " + req.queryId());
+                return new OdbcResponse(OdbcResponse.STATUS_FAILED, "Failed to find query with ID: " + req.queryId());
 
             cur.close();
 
@@ -230,11 +165,9 @@ public class OdbcCommandHandler {
      * {@link OdbcQueryFetchRequest} command handler.
      *
      * @param req Execute query request.
-     * @param qryCurs Queries cursors.
      * @return Response.
      */
-    private OdbcResponse fetchQuery(OdbcQueryFetchRequest req,
-                                    ConcurrentHashMap<Long, IgniteBiTuple<QueryCursor, Iterator>> qryCurs) {
+    private OdbcResponse fetchQuery(OdbcQueryFetchRequest req) {
         try {
             Iterator cur = qryCurs.get(req.queryId()).get2();
 
@@ -242,7 +175,12 @@ public class OdbcCommandHandler {
                 return new OdbcResponse(OdbcResponse.STATUS_FAILED,
                         "Failed to find query with ID: " + req.queryId());
 
-            OdbcQueryFetchResult res = createQueryResult(cur, req, req.queryId());
+            List<Object> items = new ArrayList<>();
+
+            for (int i = 0; i < req.pageSize() && cur.hasNext(); ++i)
+                items.add(cur.next());
+
+            OdbcQueryFetchResult res = new OdbcQueryFetchResult(req.queryId(), items, !cur.hasNext());
 
             return new OdbcResponse(res);
         }
@@ -270,12 +208,12 @@ public class OdbcCommandHandler {
                 // Parsing two-part table name.
                 String[] parts = req.tableName().split("\\.");
 
-                cacheName = RemoveQuotationMarksIfNeeded(parts[0]);
+                cacheName = removeQuotationMarksIfNeeded(parts[0]);
 
                 tableName = parts[1];
             }
             else {
-                cacheName = RemoveQuotationMarksIfNeeded(req.cacheName());
+                cacheName = removeQuotationMarksIfNeeded(req.cacheName());
 
                 tableName = req.tableName();
             }
@@ -316,7 +254,7 @@ public class OdbcCommandHandler {
         try {
             List<OdbcTableMeta> meta = new ArrayList<>();
 
-            String realSchema = RemoveQuotationMarksIfNeeded(req.schema());
+            String realSchema = removeQuotationMarksIfNeeded(req.schema());
 
             Collection<GridQueryTypeDescriptor> tablesMeta = ctx.query().types(realSchema);
 
@@ -342,4 +280,50 @@ public class OdbcCommandHandler {
             return new OdbcResponse(OdbcResponse.STATUS_FAILED, e.getMessage());
         }
     }
+
+    /**
+     * Convert metadata in collection from {@link GridQueryFieldMetadata} to
+     * {@link OdbcColumnMeta}.
+     *
+     * @param meta Internal query field metadata.
+     * @return Odbc query field metadata.
+     */
+    private static Collection<OdbcColumnMeta> convertMetadata(Collection<?> meta) {
+        List<OdbcColumnMeta> res = new ArrayList<>();
+
+        if (meta != null) {
+            for (Object info : meta) {
+                assert info instanceof GridQueryFieldMetadata;
+
+                res.add(new OdbcColumnMeta((GridQueryFieldMetadata)info));
+            }
+        }
+
+        return res;
+    }
+
+    /**
+     * Checks whether string matches SQL pattern.
+     *
+     * @param str String.
+     * @param ptrn Pattern.
+     * @return Whether string matches pattern.
+     */
+    private static boolean matches(String str, String ptrn) {
+        return str != null && (F.isEmpty(ptrn) ||
+            str.toUpperCase().matches(ptrn.toUpperCase().replace("%", ".*").replace("_", ".")));
+    }
+
+    /**
+     * Remove quotation marks at the beginning and end of the string if present.
+     *
+     * @param str Input string.
+     * @return String without leading and trailing quotation marks.
+     */
+    private static String removeQuotationMarksIfNeeded(String str) {
+        if (str.startsWith("\"") && str.endsWith("\""))
+            return str.substring(1, str.length() - 1);
+
+        return str;
+    }
 }


[2/2] ignite git commit: IGNITE-1786: Simplifications.

Posted by vo...@apache.org.
IGNITE-1786: Simplifications.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/d55dc369
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/d55dc369
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/d55dc369

Branch: refs/heads/ignite-1786
Commit: d55dc36908a2c229de38c72d7f096a94274e74a5
Parents: 8b16efc
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Wed Jan 27 15:22:28 2016 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Wed Jan 27 15:22:28 2016 +0300

----------------------------------------------------------------------
 .../internal/processors/odbc/OdbcProcessor.java | 59 ++++++--------------
 .../processors/odbc/OdbcProtocolHandler.java    |  8 ---
 .../internal/processors/odbc/OdbcTcpServer.java |  4 +-
 3 files changed, 19 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/d55dc369/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProcessor.java
index 4d49ac7..b1044cd 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProcessor.java
@@ -19,11 +19,9 @@ package org.apache.ignite.internal.processors.odbc;
 
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.internal.GridKernalContext;
-import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.binary.BinaryMarshaller;
 import org.apache.ignite.internal.processors.GridProcessorAdapter;
 import org.apache.ignite.internal.util.GridSpinReadWriteLock;
-import org.apache.ignite.internal.util.future.GridFinishedFuture;
 import org.apache.ignite.marshaller.Marshaller;
 
 /**
@@ -45,12 +43,6 @@ public class OdbcProcessor extends GridProcessorAdapter {
         @Override public OdbcResponse handle(OdbcRequest req) throws IgniteCheckedException {
             return handle0(req);
         }
-
-        /** {@inheritDoc} */
-        @Override public IgniteInternalFuture<OdbcResponse> handleAsync(OdbcRequest req) {
-            return new GridFinishedFuture<>(
-                    new IgniteCheckedException("Failed to handle request (asynchronous handling is not implemented)."));
-        }
     };
 
     /**
@@ -63,44 +55,30 @@ public class OdbcProcessor extends GridProcessorAdapter {
         if (!busyLock.tryReadLock())
             throw new IgniteCheckedException("Failed to handle request (received request while stopping grid).");
 
-        OdbcResponse rsp = null;
-
         try {
-            rsp = handleRequest(req);
-        }
-        finally {
-            busyLock.readUnlock();
-        }
+            if (log.isDebugEnabled())
+                log.debug("Received request from client: " + req);
 
-        return rsp;
-    }
+            OdbcResponse rsp;
 
-    /**
-     * Handle request.
-     *
-     * @param req Request.
-     * @return Response.
-     */
-    private OdbcResponse handleRequest(final OdbcRequest req) throws IgniteCheckedException {
-        if (log.isDebugEnabled())
-            log.debug("Received request from client: " + req);
+            try {
+                rsp = handler == null ? null : handler.handle(req);
 
-        OdbcResponse rsp;
+                if (rsp == null)
+                    throw new IgniteCheckedException("Failed to find registered handler for command: " + req.command());
+            }
+            catch (Exception e) {
+                if (log.isDebugEnabled())
+                    log.debug("Failed to handle request [req=" + req + ", e=" + e + "]");
 
-        try {
-            rsp = handler == null ? null : handler.handle(req);
+                rsp = new OdbcResponse(OdbcResponse.STATUS_FAILED, e.getMessage());
+            }
 
-            if (rsp == null)
-                throw new IgniteCheckedException("Failed to find registered handler for command: " + req.command());
+            return rsp;
         }
-        catch (Exception e) {
-            if (log.isDebugEnabled())
-                log.debug("Failed to handle request [req=" + req + ", e=" + e + "]");
-
-            rsp = new OdbcResponse(OdbcResponse.STATUS_FAILED, e.getMessage());
+        finally {
+            busyLock.readUnlock();
         }
-
-        return rsp;
     }
 
     /**
@@ -115,12 +93,10 @@ public class OdbcProcessor extends GridProcessorAdapter {
     /** {@inheritDoc} */
     @Override public void start() throws IgniteCheckedException {
         if (isOdbcEnabled()) {
-
             Marshaller marsh = ctx.config().getMarshaller();
 
             if (marsh != null && !(marsh instanceof BinaryMarshaller))
-                throw new IgniteCheckedException("Failed to start processor " +
-                        "(ODBC may only be used with BinaryMarshaller).");
+                throw new IgniteCheckedException("ODBC may only be used with BinaryMarshaller.");
 
             // Register handler.
             handler = new OdbcCommandHandler(ctx);
@@ -139,7 +115,6 @@ public class OdbcProcessor extends GridProcessorAdapter {
     /** {@inheritDoc} */
     @Override public void onKernalStart() throws IgniteCheckedException {
         if (isOdbcEnabled()) {
-
             if (log.isDebugEnabled())
                 log.debug("ODBC processor started.");
         }

http://git-wip-us.apache.org/repos/asf/ignite/blob/d55dc369/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProtocolHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProtocolHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProtocolHandler.java
index bfc58d2..962054a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProtocolHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcProtocolHandler.java
@@ -32,12 +32,4 @@ public interface OdbcProtocolHandler {
      * @throws IgniteCheckedException In case of error.
      */
     OdbcResponse handle(OdbcRequest req) throws IgniteCheckedException;
-
-    /**
-     * Handle request asynchronously.
-     *
-     * @param req Request.
-     * @return Future.
-     */
-    IgniteInternalFuture<OdbcResponse> handleAsync(OdbcRequest req);
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/d55dc369/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcTcpServer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcTcpServer.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcTcpServer.java
index 739a22f..159b04a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcTcpServer.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/OdbcTcpServer.java
@@ -108,7 +108,7 @@ public class OdbcTcpServer {
      * @return Host address.
      * @throws IOException If failed to resolve host.
      */
-    private InetAddress resolveOdbcTcpHost(IgniteConfiguration cfg) throws IOException {
+    private static InetAddress resolveOdbcTcpHost(IgniteConfiguration cfg) throws IOException {
         String host = null;
 
         OdbcConfiguration odbcCfg = cfg.getOdbcConfiguration();
@@ -134,7 +134,7 @@ public class OdbcTcpServer {
      *      server was unable to start.
      */
     private boolean startTcpServer(InetAddress hostAddr, int port, GridNioServerListener<OdbcRequest> listener,
-                                   GridNioParser parser, OdbcConfiguration cfg) {
+        GridNioParser parser, OdbcConfiguration cfg) {
         try {
             GridNioFilter codec = new GridNioCodecFilter(parser, log, false);