Skip to content
Snippets Groups Projects
Commit 0c735de3 authored by Andy Kruth's avatar Andy Kruth
Browse files

[jdbc] additional checkstyle updates

* Cleaned up javadoc and comments
* Backed out unnecessary line breaks
* Updated license

Closes #494
parent 8c508dd5
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* Copyright (c) 2010 - 2016 Yahoo! Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
......@@ -39,8 +39,7 @@ public final class JdbcDBCli {
System.out.println(" -c SQL command to execute.");
}
private static void executeCommand(Properties props, String sql)
throws SQLException {
private static void executeCommand(Properties props, String sql) throws SQLException {
String driver = props.getProperty(JdbcDBClient.DRIVER_CLASS);
String username = props.getProperty(JdbcDBClient.CONNECTION_USER);
String password = props.getProperty(JdbcDBClient.CONNECTION_PASSWD, "");
......@@ -104,8 +103,7 @@ public final class JdbcDBCli {
// Issue #5 - remove call to stringPropertyNames to make compilable
// under Java 1.5
for (Enumeration<?> e = myfileprops.propertyNames(); e
.hasMoreElements();) {
for (Enumeration<?> e = myfileprops.propertyNames(); e.hasMoreElements();) {
String prop = (String) e.nextElement();
fileprops.setProperty(prop, myfileprops.getProperty(prop));
......
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* Copyright (c) 2010 - 2016 Yahoo! Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
......@@ -40,19 +40,6 @@ import java.util.concurrent.ConcurrentMap;
* This interface expects a schema <key> <field1> <field2> <field3> ... All
* attributes are of type VARCHAR. All accesses are through the primary key.
* Therefore, only one index on the primary key is needed.
*
* <p>
* The following options must be passed when using this database client.
*
* <ul>
* <li><b>db.driver</b> The JDBC driver class to use.</li>
* <li><b>db.url</b> The Database connection URL.</li>
* <li><b>db.user</b> User name for the connection.</li>
* <li><b>db.passwd</b> Password for the connection.</li>
* </ul>
*
* @author sudipto
*
*/
public class JdbcDBClient extends DB {
......@@ -135,8 +122,7 @@ public class JdbcDBClient extends DB {
final int prime = 31;
int result = 1;
result = prime * result + numFields + 100 * shardIndex;
result =
prime * result + ((tableName == null) ? 0 : tableName.hashCode());
result = prime * result + ((tableName == null) ? 0 : tableName.hashCode());
result = prime * result + ((type == null) ? 0 : type.getHashCode());
return result;
}
......@@ -176,14 +162,11 @@ public class JdbcDBClient extends DB {
/**
* For the given key, returns what shard contains data for this key.
*
* @param key
* Data key to do operation on
* @param key Data key to do operation on
* @return Shard index
*/
private int getShardIndexByKey(String key) {
int ret = Math.abs(key.hashCode()) % conns.size();
// System.out.println(conns.size() + ": Shard instance for "+ key + " (hash
// " + key.hashCode()+ " ) " + " is " + ret);
return ret;
}
......@@ -191,8 +174,7 @@ public class JdbcDBClient extends DB {
* For the given key, returns Connection object that holds connection to the
* shard that contains this key.
*
* @param key
* Data key to get information for
* @param key Data key to get information for
* @return Connection object
*/
private Connection getShardConnectionByKey(String key) {
......@@ -205,10 +187,6 @@ public class JdbcDBClient extends DB {
}
}
/**
* Initialize the database connection and set it up for sending requests to
* the database. This must be called once per client. @throws
*/
@Override
public void init() throws DBException {
if (initialized) {
......@@ -226,14 +204,12 @@ public class JdbcDBClient extends DB {
try {
this.jdbcFetchSize = Integer.parseInt(jdbcFetchSizeStr);
} catch (NumberFormatException nfe) {
System.err
.println("Invalid JDBC fetch size specified: " + jdbcFetchSizeStr);
System.err.println("Invalid JDBC fetch size specified: " + jdbcFetchSizeStr);
throw new DBException(nfe);
}
}
String autoCommitStr =
props.getProperty(JDBC_AUTO_COMMIT, Boolean.TRUE.toString());
String autoCommitStr = props.getProperty(JDBC_AUTO_COMMIT, Boolean.TRUE.toString());
Boolean autoCommit = Boolean.parseBoolean(autoCommitStr);
try {
......@@ -258,8 +234,7 @@ public class JdbcDBClient extends DB {
System.out.println("Using " + shardCount + " shards");
cachedStatements =
new ConcurrentHashMap<StatementType, PreparedStatement>();
cachedStatements = new ConcurrentHashMap<StatementType, PreparedStatement>();
} catch (ClassNotFoundException e) {
System.err.println("Error in initializing the JDBS driver: " + e);
throw new DBException(e);
......@@ -283,8 +258,7 @@ public class JdbcDBClient extends DB {
}
}
private PreparedStatement createAndCacheInsertStatement(
StatementType insertType, String key) throws SQLException {
private PreparedStatement createAndCacheInsertStatement(StatementType insertType, String key) throws SQLException {
StringBuilder insert = new StringBuilder("INSERT INTO ");
insert.append(insertType.tableName);
insert.append(" VALUES(?");
......@@ -292,53 +266,44 @@ public class JdbcDBClient extends DB {
insert.append(",?");
}
insert.append(")");
PreparedStatement insertStatement =
getShardConnectionByKey(key).prepareStatement(insert.toString());
PreparedStatement stmt =
cachedStatements.putIfAbsent(insertType, insertStatement);
PreparedStatement insertStatement = getShardConnectionByKey(key).prepareStatement(insert.toString());
PreparedStatement stmt = cachedStatements.putIfAbsent(insertType, insertStatement);
if (stmt == null) {
return insertStatement;
}
return stmt;
}
private PreparedStatement createAndCacheReadStatement(StatementType readType,
String key) throws SQLException {
private PreparedStatement createAndCacheReadStatement(StatementType readType, String key) throws SQLException {
StringBuilder read = new StringBuilder("SELECT * FROM ");
read.append(readType.tableName);
read.append(" WHERE ");
read.append(PRIMARY_KEY);
read.append(" = ");
read.append("?");
PreparedStatement readStatement =
getShardConnectionByKey(key).prepareStatement(read.toString());
PreparedStatement stmt =
cachedStatements.putIfAbsent(readType, readStatement);
PreparedStatement readStatement = getShardConnectionByKey(key).prepareStatement(read.toString());
PreparedStatement stmt = cachedStatements.putIfAbsent(readType, readStatement);
if (stmt == null) {
return readStatement;
}
return stmt;
}
private PreparedStatement createAndCacheDeleteStatement(
StatementType deleteType, String key) throws SQLException {
private PreparedStatement createAndCacheDeleteStatement(StatementType deleteType, String key) throws SQLException {
StringBuilder delete = new StringBuilder("DELETE FROM ");
delete.append(deleteType.tableName);
delete.append(" WHERE ");
delete.append(PRIMARY_KEY);
delete.append(" = ?");
PreparedStatement deleteStatement =
getShardConnectionByKey(key).prepareStatement(delete.toString());
PreparedStatement stmt =
cachedStatements.putIfAbsent(deleteType, deleteStatement);
PreparedStatement deleteStatement = getShardConnectionByKey(key).prepareStatement(delete.toString());
PreparedStatement stmt = cachedStatements.putIfAbsent(deleteType, deleteStatement);
if (stmt == null) {
return deleteStatement;
}
return stmt;
}
private PreparedStatement createAndCacheUpdateStatement(
StatementType updateType, String key) throws SQLException {
private PreparedStatement createAndCacheUpdateStatement(StatementType updateType, String key) throws SQLException {
StringBuilder update = new StringBuilder("UPDATE ");
update.append(updateType.tableName);
update.append(" SET ");
......@@ -353,18 +318,15 @@ public class JdbcDBClient extends DB {
update.append(" WHERE ");
update.append(PRIMARY_KEY);
update.append(" = ?");
PreparedStatement insertStatement =
getShardConnectionByKey(key).prepareStatement(update.toString());
PreparedStatement stmt =
cachedStatements.putIfAbsent(updateType, insertStatement);
PreparedStatement insertStatement = getShardConnectionByKey(key).prepareStatement(update.toString());
PreparedStatement stmt = cachedStatements.putIfAbsent(updateType, insertStatement);
if (stmt == null) {
return insertStatement;
}
return stmt;
}
private PreparedStatement createAndCacheScanStatement(StatementType scanType,
String key) throws SQLException {
private PreparedStatement createAndCacheScanStatement(StatementType scanType, String key) throws SQLException {
StringBuilder select = new StringBuilder("SELECT * FROM ");
select.append(scanType.tableName);
select.append(" WHERE ");
......@@ -373,13 +335,11 @@ public class JdbcDBClient extends DB {
select.append(" ORDER BY ");
select.append(PRIMARY_KEY);
select.append(" LIMIT ?");
PreparedStatement scanStatement =
getShardConnectionByKey(key).prepareStatement(select.toString());
PreparedStatement scanStatement = getShardConnectionByKey(key).prepareStatement(select.toString());
if (this.jdbcFetchSize != null) {
scanStatement.setFetchSize(this.jdbcFetchSize);
}
PreparedStatement stmt =
cachedStatements.putIfAbsent(scanType, scanStatement);
PreparedStatement stmt = cachedStatements.putIfAbsent(scanType, scanStatement);
if (stmt == null) {
return scanStatement;
}
......@@ -387,11 +347,9 @@ public class JdbcDBClient extends DB {
}
@Override
public Status read(String tableName, String key, Set<String> fields,
HashMap<String, ByteIterator> result) {
public Status read(String tableName, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
try {
StatementType type = new StatementType(StatementType.Type.READ, tableName,
1, getShardIndexByKey(key));
StatementType type = new StatementType(StatementType.Type.READ, tableName, 1, getShardIndexByKey(key));
PreparedStatement readStatement = cachedStatements.get(type);
if (readStatement == null) {
readStatement = createAndCacheReadStatement(type, key);
......@@ -411,18 +369,16 @@ public class JdbcDBClient extends DB {
resultSet.close();
return Status.OK;
} catch (SQLException e) {
System.err
.println("Error in processing read of table " + tableName + ": " + e);
System.err.println("Error in processing read of table " + tableName + ": " + e);
return Status.ERROR;
}
}
@Override
public Status scan(String tableName, String startKey, int recordcount,
Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
public Status scan(String tableName, String startKey, int recordcount, Set<String> fields,
Vector<HashMap<String, ByteIterator>> result) {
try {
StatementType type = new StatementType(StatementType.Type.SCAN, tableName,
1, getShardIndexByKey(startKey));
StatementType type = new StatementType(StatementType.Type.SCAN, tableName, 1, getShardIndexByKey(startKey));
PreparedStatement scanStatement = cachedStatements.get(type);
if (scanStatement == null) {
scanStatement = createAndCacheScanStatement(type, startKey);
......@@ -432,8 +388,7 @@ public class JdbcDBClient extends DB {
ResultSet resultSet = scanStatement.executeQuery();
for (int i = 0; i < recordcount && resultSet.next(); i++) {
if (result != null && fields != null) {
HashMap<String, ByteIterator> values =
new HashMap<String, ByteIterator>();
HashMap<String, ByteIterator> values = new HashMap<String, ByteIterator>();
for (String field : fields) {
String value = resultSet.getString(field);
values.put(field, new StringByteIterator(value));
......@@ -450,12 +405,10 @@ public class JdbcDBClient extends DB {
}
@Override
public Status update(String tableName, String key,
HashMap<String, ByteIterator> values) {
public Status update(String tableName, String key, HashMap<String, ByteIterator> values) {
try {
int numFields = values.size();
StatementType type = new StatementType(StatementType.Type.UPDATE,
tableName, numFields, getShardIndexByKey(key));
StatementType type = new StatementType(StatementType.Type.UPDATE, tableName, numFields, getShardIndexByKey(key));
PreparedStatement updateStatement = cachedStatements.get(type);
if (updateStatement == null) {
updateStatement = createAndCacheUpdateStatement(type, key);
......@@ -471,19 +424,16 @@ public class JdbcDBClient extends DB {
}
return Status.UNEXPECTED_STATE;
} catch (SQLException e) {
System.err
.println("Error in processing update to table: " + tableName + e);
System.err.println("Error in processing update to table: " + tableName + e);
return Status.ERROR;
}
}
@Override
public Status insert(String tableName, String key,
HashMap<String, ByteIterator> values) {
public Status insert(String tableName, String key, HashMap<String, ByteIterator> values) {
try {
int numFields = values.size();
StatementType type = new StatementType(StatementType.Type.INSERT,
tableName, numFields, getShardIndexByKey(key));
StatementType type = new StatementType(StatementType.Type.INSERT, tableName, numFields, getShardIndexByKey(key));
PreparedStatement insertStatement = cachedStatements.get(type);
if (insertStatement == null) {
insertStatement = createAndCacheInsertStatement(type, key);
......@@ -500,8 +450,7 @@ public class JdbcDBClient extends DB {
}
return Status.UNEXPECTED_STATE;
} catch (SQLException e) {
System.err
.println("Error in processing insert to table: " + tableName + e);
System.err.println("Error in processing insert to table: " + tableName + e);
return Status.ERROR;
}
}
......@@ -509,8 +458,7 @@ public class JdbcDBClient extends DB {
@Override
public Status delete(String tableName, String key) {
try {
StatementType type = new StatementType(StatementType.Type.DELETE,
tableName, 1, getShardIndexByKey(key));
StatementType type = new StatementType(StatementType.Type.DELETE, tableName, 1, getShardIndexByKey(key));
PreparedStatement deleteStatement = cachedStatements.get(type);
if (deleteStatement == null) {
deleteStatement = createAndCacheDeleteStatement(type, key);
......@@ -522,8 +470,7 @@ public class JdbcDBClient extends DB {
}
return Status.UNEXPECTED_STATE;
} catch (SQLException e) {
System.err
.println("Error in processing delete to table: " + tableName + e);
System.err.println("Error in processing delete to table: " + tableName + e);
return Status.ERROR;
}
}
......
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* Copyright (c) 2010 - 2016 Yahoo! Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
......@@ -40,15 +40,13 @@ public final class JdbcDBCreateTable {
System.out.println(" -f number of fields (default 10).");
}
private static void createTable(Properties props, String tablename)
throws SQLException {
private static void createTable(Properties props, String tablename) throws SQLException {
String driver = props.getProperty(JdbcDBClient.DRIVER_CLASS);
String username = props.getProperty(JdbcDBClient.CONNECTION_USER);
String password = props.getProperty(JdbcDBClient.CONNECTION_PASSWD, "");
String url = props.getProperty(JdbcDBClient.CONNECTION_URL);
int fieldcount =
Integer.parseInt(props.getProperty(JdbcDBClient.FIELD_COUNT_PROPERTY,
JdbcDBClient.FIELD_COUNT_PROPERTY_DEFAULT));
int fieldcount = Integer.parseInt(props.getProperty(JdbcDBClient.FIELD_COUNT_PROPERTY,
JdbcDBClient.FIELD_COUNT_PROPERTY_DEFAULT));
if (driver == null || username == null || url == null) {
throw new SQLException("Missing connection information.");
......@@ -129,8 +127,7 @@ public final class JdbcDBCreateTable {
// Issue #5 - remove call to stringPropertyNames to make compilable
// under Java 1.5
for (Enumeration<?> e = myfileprops.propertyNames(); e
.hasMoreElements();) {
for (Enumeration<?> e = myfileprops.propertyNames(); e.hasMoreElements();) {
String prop = (String) e.nextElement();
fileprops.setProperty(prop, myfileprops.getProperty(prop));
......@@ -207,8 +204,7 @@ public final class JdbcDBCreateTable {
}
if (fieldcount > 0) {
props.setProperty(JdbcDBClient.FIELD_COUNT_PROPERTY,
String.valueOf(fieldcount));
props.setProperty(JdbcDBClient.FIELD_COUNT_PROPERTY, String.valueOf(fieldcount));
}
try {
......
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
* Copyright (c) 2014 - 2016, Yahoo!, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment