From bfc62cd77cd13fd1c5194e37cdcab35e89fb2244 Mon Sep 17 00:00:00 2001 From: Arbel Deutsch Peled Date: Sat, 12 Dec 2015 23:18:32 +0200 Subject: [PATCH] Slight enhancement to Server performance. --- .../sqlserver/BulletinBoardSQLServer.java | 23 ++++++++++--------- .../webapp/BulletinBoardWebApp.java | 6 ++--- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java index ae2095a..52bf42b 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java @@ -395,7 +395,9 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ BulletinBoardMessageList.Builder resultListBuilder = BulletinBoardMessageList.newBuilder(); - String sql; + // SQL length is roughly 50 characters per filter + 50 for the query itself + StringBuilder sqlBuilder = new StringBuilder(50 * (filterList.getFilterCount() + 1)); + MapSqlParameterSource namedParameters; int paramNum; @@ -410,14 +412,13 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ // Check if Tag/Signature tables are required for filtering purposes - sql = sqlQueryProvider.getSQLString(SQLQueryProvider.QueryType.GET_MESSAGES); - + sqlBuilder.append(sqlQueryProvider.getSQLString(SQLQueryProvider.QueryType.GET_MESSAGES)); // Add conditions namedParameters = new MapSqlParameterSource(); if (!filters.isEmpty()) { - sql += " WHERE "; + sqlBuilder.append(" WHERE "); for (paramNum = 0 ; paramNum < filters.size() ; paramNum++) { @@ -427,11 +428,11 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ if (isFirstFilter) { isFirstFilter = false; } else { - sql += " AND "; + sqlBuilder.append(" AND "); } } - sql += sqlQueryProvider.getCondition(filter.getType(), paramNum); + sqlBuilder.append(sqlQueryProvider.getCondition(filter.getType(), paramNum)); SQLQueryProvider.FilterTypeParam filterTypeParam = SQLQueryProvider.FilterTypeParam.getFilterTypeParamName(filter.getType()); @@ -447,13 +448,10 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ // Run query - List msgBuilders = jdbcTemplate.query(sql, namedParameters, messageMapper); - + List msgBuilders = jdbcTemplate.query(sqlBuilder.toString(), namedParameters, messageMapper); // Compile list of messages - sql = sqlQueryProvider.getSQLString(SQLQueryProvider.QueryType.GET_SIGNATURES); - for (BulletinBoardMessage.Builder msgBuilder : msgBuilders) { // Retrieve signatures @@ -461,7 +459,10 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ namedParameters = new MapSqlParameterSource(); namedParameters.addValue("EntryNum", msgBuilder.getEntryNum()); - List signatures = jdbcTemplate.query(sql, namedParameters, signatureMapper); + List signatures = jdbcTemplate.query( + sqlQueryProvider.getSQLString(SQLQueryProvider.QueryType.GET_SIGNATURES), + namedParameters, + signatureMapper); // Append signatures msgBuilder.addAllSig(signatures); diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java index 779982a..b3fc03c 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -53,15 +53,15 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL String dbType = servletContext.getInitParameter("dbType"); String dbName = servletContext.getInitParameter("dbName"); - if ("SQLite".compareTo(dbType) == 0){ + if ("SQLite".equals(dbType)){ bulletinBoard = new BulletinBoardSQLServer(new SQLiteQueryProvider(dbName)); - } else if ("H2".compareTo(dbType) == 0) { + } else if ("H2".equals(dbType)) { bulletinBoard = new BulletinBoardSQLServer(new H2QueryProvider(dbName)); - } else if ("MySQL".compareTo(dbType) == 0) { + } else if ("MySQL".equals(dbType)) { String dbAddress = servletContext.getInitParameter("dbAddress"); int dbPort = Integer.parseInt(servletContext.getInitParameter("dbPort"));