Minor code cleanups.

This commit is contained in:
akwizgran
2011-11-16 18:15:32 +00:00
parent 01dd658200
commit 4da7a685cd
14 changed files with 41 additions and 136 deletions

View File

@@ -16,8 +16,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.Bytes;
import net.sf.briar.api.ContactId;
@@ -75,9 +73,6 @@ import com.google.inject.Inject;
class DatabaseComponentImpl<T> implements DatabaseComponent,
DatabaseCleaner.Callback {
private static final Logger LOG =
Logger.getLogger(DatabaseComponentImpl.class.getName());
/*
* Locks must always be acquired in alphabetical order. See the Database
* interface to find out which calls require which locks.
@@ -413,8 +408,6 @@ DatabaseCleaner.Callback {
if(!a.writeBatchId(b)) break;
sent.add(b);
}
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + sent.size() + " batch IDs to ack");
// Record the contents of the ack, unless it's empty
if(sent.isEmpty()) return false;
a.finish();
@@ -601,8 +594,6 @@ DatabaseCleaner.Callback {
sent.add(m);
}
if(!sent.isEmpty()) o.finish();
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + sent.size() + " message IDs to offer");
return sent;
}
@@ -636,11 +627,7 @@ DatabaseCleaner.Callback {
} finally {
contactLock.readLock().unlock();
}
if(subs != null) {
s.writeSubscriptions(subs, timestamp);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + subs.size() + " subscriptions to update");
}
if(subs != null) s.writeSubscriptions(subs, timestamp);
}
private boolean updateIsDue(long sent) {
@@ -678,12 +665,7 @@ DatabaseCleaner.Callback {
} finally {
contactLock.readLock().unlock();
}
if(transports != null) {
t.writeTransports(transports, timestamp);
if(LOG.isLoggable(Level.FINE))
LOG.fine("Added " + transports.size() +
" transports to update");
}
if(transports != null) t.writeTransports(transports, timestamp);
}
public TransportConfig getConfig(TransportId t) throws DbException {
@@ -1025,13 +1007,9 @@ DatabaseCleaner.Callback {
T txn = db.startTransaction();
try {
// Mark all messages in acked batches as seen
if(LOG.isLoggable(Level.FINE))
LOG.fine("Received " + acks.size() + " acks");
for(BatchId b : acks) db.removeAckedBatch(txn, c, b);
// Find any lost batches that need to be retransmitted
Collection<BatchId> lost = db.getLostBatches(txn, c);
if(LOG.isLoggable(Level.FINE))
LOG.fine(lost.size() + " lost batches");
for(BatchId b : lost) db.removeLostBatch(txn, c, b);
db.commitTransaction(txn);
} catch(DbException e) {
@@ -1167,8 +1145,6 @@ DatabaseCleaner.Callback {
try {
Map<Group, Long> subs = s.getSubscriptions();
db.setSubscriptions(txn, c, subs, s.getTimestamp());
if(LOG.isLoggable(Level.FINE))
LOG.fine("Received " + subs.size() + " subscriptions");
db.commitTransaction(txn);
} catch(DbException e) {
db.abortTransaction(txn);
@@ -1197,9 +1173,6 @@ DatabaseCleaner.Callback {
try {
Collection<Transport> transports = t.getTransports();
db.setTransports(txn, c, transports, t.getTimestamp());
if(LOG.isLoggable(Level.FINE))
LOG.fine("Received " + transports.size()
+ " transports");
db.commitTransaction(txn);
} catch(DbException e) {
db.abortTransaction(txn);
@@ -1216,7 +1189,6 @@ DatabaseCleaner.Callback {
}
public void removeContact(ContactId c) throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Removing contact " + c);
contactLock.writeLock().lock();
try {
if(!containsContact(c)) throw new NoSuchContactException();
@@ -1421,9 +1393,6 @@ DatabaseCleaner.Callback {
}
}
}
if(LOG.isLoggable(Level.FINE))
LOG.fine(direct + " messages affected directly, "
+ indirect + " indirectly");
}
public void setVisibility(GroupId g, Collection<ContactId> visible)
@@ -1470,7 +1439,6 @@ DatabaseCleaner.Callback {
}
public void subscribe(Group g) throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Subscribing to " + g);
subscriptionLock.writeLock().lock();
try {
T txn = db.startTransaction();
@@ -1489,7 +1457,6 @@ DatabaseCleaner.Callback {
}
public void unsubscribe(GroupId g) throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Unsubscribing from " + g);
Collection<ContactId> affected = null;
contactLock.readLock().lock();
try {
@@ -1602,17 +1569,8 @@ DatabaseCleaner.Callback {
public boolean shouldCheckFreeSpace() {
synchronized(spaceLock) {
long now = System.currentTimeMillis();
if(bytesStoredSinceLastCheck > MAX_BYTES_BETWEEN_SPACE_CHECKS) {
if(LOG.isLoggable(Level.FINE))
LOG.fine(bytesStoredSinceLastCheck
+ " bytes stored since last check");
bytesStoredSinceLastCheck = 0L;
timeOfLastCheck = now;
return true;
}
if(now - timeOfLastCheck > MAX_MS_BETWEEN_SPACE_CHECKS) {
if(LOG.isLoggable(Level.FINE))
LOG.fine((now - timeOfLastCheck) + " ms since last check");
if(bytesStoredSinceLastCheck > MAX_BYTES_BETWEEN_SPACE_CHECKS
|| now - timeOfLastCheck > MAX_MS_BETWEEN_SPACE_CHECKS) {
bytesStoredSinceLastCheck = 0L;
timeOfLastCheck = now;
return true;

View File

@@ -7,8 +7,6 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.crypto.Password;
import net.sf.briar.api.db.DatabaseDirectory;
@@ -26,9 +24,6 @@ import com.google.inject.Inject;
/** Contains all the H2-specific code for the database. */
class H2Database extends JdbcDatabase {
private static final Logger LOG =
Logger.getLogger(H2Database.class.getName());
private static final String HASH_TYPE = "BINARY(32)";
private static final String BINARY_TYPE = "BINARY";
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
@@ -60,7 +55,6 @@ class H2Database extends JdbcDatabase {
}
public void close() throws DbException {
if(LOG.isLoggable(Level.FINE)) LOG.fine("Closing database");
try {
super.closeAllConnections();
} catch(SQLException e) {
@@ -76,7 +70,6 @@ class H2Database extends JdbcDatabase {
long used = getDiskSpace(dir);
long quota = maxSize - used;
long min = Math.min(free, quota);
if(LOG.isLoggable(Level.FINE)) LOG.fine("Free space: " + min);
return min;
} catch(IOException e) {
throw new DbException(e);

View File

@@ -299,8 +299,6 @@ abstract class JdbcDatabase implements Database<Connection> {
if(resume) {
if(!dir.exists()) throw new DbException();
if(!dir.isDirectory()) throw new DbException();
if(LOG.isLoggable(Level.FINE))
LOG.fine("Resuming from " + dir.getPath());
} else {
if(dir.exists()) FileUtils.delete(dir);
}
@@ -314,14 +312,7 @@ abstract class JdbcDatabase implements Database<Connection> {
Connection txn = startTransaction();
try {
// If not resuming, create the tables
if(resume) {
if(LOG.isLoggable(Level.FINE))
LOG.fine(getNumberOfMessages(txn) + " messages");
} else {
if(LOG.isLoggable(Level.FINE))
LOG.fine("Creating database tables");
createTables(txn);
}
if(!resume) createTables(txn);
commitTransaction(txn);
} catch(DbException e) {
abortTransaction(txn);
@@ -415,8 +406,6 @@ abstract class JdbcDatabase implements Database<Connection> {
if(txn == null) throw new DbException();
synchronized(connections) {
openConnections++;
if(LOG.isLoggable(Level.FINE))
LOG.fine(openConnections + " open connections");
}
}
txn.setAutoCommit(false);
@@ -470,9 +459,6 @@ abstract class JdbcDatabase implements Database<Connection> {
openConnections -= connections.size();
connections.clear();
while(openConnections > 0) {
if(LOG.isLoggable(Level.FINE))
LOG.fine("Waiting for " + openConnections
+ " open connections");
try {
connections.wait();
} catch(InterruptedException e) {
@@ -1348,26 +1334,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
private int getNumberOfMessages(Connection txn) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT COUNT(messageId) FROM messages";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
if(!rs.next()) throw new DbStateException();
int count = rs.getInt(1);
if(rs.next()) throw new DbStateException();
rs.close();
ps.close();
return count;
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public int getNumberOfSendableChildren(Connection txn, MessageId m)
throws DbException {
PreparedStatement ps = null;
@@ -1422,8 +1388,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
rs.close();
ps.close();
if(LOG.isLoggable(Level.FINE))
LOG.fine(ids.size() + " old messages, " + total + " bytes");
return ids;
} catch(SQLException e) {
tryToClose(rs);
@@ -1579,8 +1543,6 @@ abstract class JdbcDatabase implements Database<Connection> {
while(rs.next()) ids.add(new MessageId(rs.getBytes(2)));
rs.close();
ps.close();
if(LOG.isLoggable(Level.FINE))
LOG.fine(ids.size() + " sendable private messages");
// Do we have any sendable group messages?
sql = "SELECT m.messageId FROM messages AS m"
+ " JOIN contactSubscriptions AS cs"
@@ -1601,8 +1563,6 @@ abstract class JdbcDatabase implements Database<Connection> {
while(rs.next()) ids.add(new MessageId(rs.getBytes(2)));
rs.close();
ps.close();
if(LOG.isLoggable(Level.FINE))
LOG.fine(ids.size() + " sendable private and group messages");
return ids;
} catch(SQLException e) {
tryToClose(rs);
@@ -1635,9 +1595,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
rs.close();
ps.close();
if(LOG.isLoggable(Level.FINE))
LOG.fine(ids.size() + " sendable private messages, " +
total + "/" + capacity + " bytes");
if(total == capacity) return ids;
// Do we have any sendable group messages?
sql = "SELECT length, m.messageId FROM messages AS m"
@@ -1664,9 +1621,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
rs.close();
ps.close();
if(LOG.isLoggable(Level.FINE))
LOG.fine(ids.size() + " sendable private and group messages, " +
total + "/" + capacity + " bytes");
return ids;
} catch(SQLException e) {
tryToClose(rs);

View File

@@ -101,7 +101,7 @@ public class FontManagerImpl implements FontManager {
private final float size;
private final String[] languages;
BundledFont(String filename, float size, String[] languages) {
private BundledFont(String filename, float size, String[] languages) {
this.filename = filename;
this.size = size;
this.languages = languages;

View File

@@ -127,8 +127,8 @@ class PluginManagerImpl implements PluginManager {
StreamPlugin plugin = factory.createPlugin(executor, callback);
if(plugin == null) {
if(LOG.isLoggable(Level.INFO))
LOG.info(factory.getClass().getSimpleName() +
" did not create a plugin");
LOG.info(factory.getClass().getSimpleName()
+ " did not create a plugin");
continue;
}
TransportId id = plugin.getId();

View File

@@ -63,8 +63,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
super.start();
localDevice = LocalDevice.getLocalDevice();
if(LOG.isLoggable(Level.INFO))
LOG.info("Local address " +
localDevice.getBluetoothAddress());
LOG.info("Local address "
+ localDevice.getBluetoothAddress());
}
} catch(UnsatisfiedLinkError e) {
// On Linux the user may need to install libbluetooth-dev

View File

@@ -87,8 +87,8 @@ class SimpleSocketPlugin extends SocketPlugin {
boolean site = addr.isSiteLocalAddress();
if(lan == (link || site)) {
if(LOG.isLoggable(Level.INFO))
LOG.info("Choosing interface " +
addr.getHostAddress());
LOG.info("Choosing interface "
+ addr.getHostAddress());
return addr;
}
}
@@ -99,8 +99,8 @@ class SimpleSocketPlugin extends SocketPlugin {
for(InetAddress addr : Collections.list(iface.getInetAddresses())) {
if(!addr.isLoopbackAddress()) {
if(LOG.isLoggable(Level.INFO))
LOG.info("Accepting interface " +
addr.getHostAddress());
LOG.info("Accepting interface "
+ addr.getHostAddress());
return addr;
}
}

View File

@@ -63,8 +63,8 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
}
ss.bind(addr);
if(LOG.isLoggable(Level.INFO)) {
LOG.info("Bound to " + ss.getInetAddress().getHostAddress() +
":" + ss.getLocalPort());
LOG.info("Bound to " + ss.getInetAddress().getHostAddress()
+ ":" + ss.getLocalPort());
}
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());

View File

@@ -55,7 +55,7 @@ class TransportUpdateReader implements ObjectReader<TransportUpdate> {
timestamp);
}
private class TransportReader implements ObjectReader<Transport> {
private static class TransportReader implements ObjectReader<Transport> {
public Transport readObject(Reader r) throws IOException {
r.readUserDefinedId(Types.TRANSPORT);

View File

@@ -15,9 +15,8 @@ class SerialComponentImpl implements SerialComponent {
public int getSerialisedUniqueIdLength(int id) {
// User-defined ID, BYTES tag, length spec, bytes
return getSerialisedUserDefinedIdLength(id) + 1 +
getSerialisedLengthSpecLength(UniqueId.LENGTH) +
UniqueId.LENGTH;
return getSerialisedUserDefinedIdLength(id) + 1
+ getSerialisedLengthSpecLength(UniqueId.LENGTH) + UniqueId.LENGTH;
}
private int getSerialisedLengthSpecLength(int length) {

View File

@@ -103,7 +103,7 @@ public class SimpleSocketPluginTest extends TestCase {
private TransportConfig config = new TransportConfig();
private TransportProperties local = new TransportProperties();
private Map<ContactId, TransportProperties> remote =
private final Map<ContactId, TransportProperties> remote =
new HashMap<ContactId, TransportProperties>();
private int incomingConnections = 0;

View File

@@ -127,9 +127,9 @@ public class ConstantsTest extends TestCase {
assertTrue(b.writeMessage(message.getSerialised()));
b.finish();
// Check the size of the serialised batch
assertTrue(out.size() > UniqueId.LENGTH + MAX_GROUP_NAME_LENGTH +
MAX_PUBLIC_KEY_LENGTH + MAX_AUTHOR_NAME_LENGTH +
MAX_PUBLIC_KEY_LENGTH + MAX_BODY_LENGTH);
assertTrue(out.size() > UniqueId.LENGTH + MAX_GROUP_NAME_LENGTH
+ MAX_PUBLIC_KEY_LENGTH + MAX_AUTHOR_NAME_LENGTH
+ MAX_PUBLIC_KEY_LENGTH + MAX_BODY_LENGTH);
assertTrue(out.size() <= MAX_PACKET_LENGTH);
}
@@ -222,8 +222,9 @@ public class ConstantsTest extends TestCase {
new TransportUpdateWriterImpl(out, writerFactory);
t.writeTransports(transports, Long.MAX_VALUE);
// Check the size of the serialised update
assertTrue(out.size() > MAX_TRANSPORTS * (UniqueId.LENGTH + 4 +
(MAX_PROPERTIES_PER_TRANSPORT * MAX_PROPERTY_LENGTH * 2)) + 8);
assertTrue(out.size() > MAX_TRANSPORTS * (UniqueId.LENGTH + 4
+ (MAX_PROPERTIES_PER_TRANSPORT * MAX_PROPERTY_LENGTH * 2))
+ 8);
assertTrue(out.size() <= MAX_PACKET_LENGTH);
}

View File

@@ -65,8 +65,8 @@ public class ReaderImplTest extends TestCase {
@Test
public void testReadInt64() throws Exception {
setContents("FA0000000000000000" + "FAFFFFFFFFFFFFFFFF" +
"FA7FFFFFFFFFFFFFFF" + "FA8000000000000000");
setContents("FA0000000000000000" + "FAFFFFFFFFFFFFFFFF"
+ "FA7FFFFFFFFFFFFFFF" + "FA8000000000000000");
assertEquals(0L, r.readInt64());
assertEquals(-1L, r.readInt64());
assertEquals(Long.MAX_VALUE, r.readInt64());
@@ -76,8 +76,8 @@ public class ReaderImplTest extends TestCase {
@Test
public void testReadIntAny() throws Exception {
setContents("00" + "7F" + "FD80" + "FDFF" + "FC0080" + "FC7FFF" +
"FB00008000" + "FB7FFFFFFF" + "FA0000000080000000");
setContents("00" + "7F" + "FD80" + "FDFF" + "FC0080" + "FC7FFF"
+ "FB00008000" + "FB7FFFFFFF" + "FA0000000080000000");
assertEquals(0L, r.readIntAny());
assertEquals(127L, r.readIntAny());
assertEquals(-128L, r.readIntAny());
@@ -94,8 +94,8 @@ public class ReaderImplTest extends TestCase {
public void testReadFloat32() throws Exception {
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html
// http://steve.hollasch.net/cgindex/coding/ieeefloat.html
setContents("F900000000" + "F93F800000" + "F940000000" + "F9BF800000" +
"F980000000" + "F9FF800000" + "F97F800000" + "F97FC00000");
setContents("F900000000" + "F93F800000" + "F940000000" + "F9BF800000"
+ "F980000000" + "F9FF800000" + "F97F800000" + "F97FC00000");
assertEquals(0F, r.readFloat32());
assertEquals(1F, r.readFloat32());
assertEquals(2F, r.readFloat32());
@@ -109,10 +109,10 @@ public class ReaderImplTest extends TestCase {
@Test
public void testReadFloat64() throws Exception {
setContents("F80000000000000000" + "F83FF0000000000000" +
"F84000000000000000" + "F8BFF0000000000000" +
"F88000000000000000" + "F8FFF0000000000000" +
"F87FF0000000000000" + "F87FF8000000000000");
setContents("F80000000000000000" + "F83FF0000000000000"
+ "F84000000000000000" + "F8BFF0000000000000"
+ "F88000000000000000" + "F8FFF0000000000000"
+ "F87FF0000000000000" + "F87FF8000000000000");
assertEquals(0.0, r.readFloat64());
assertEquals(1.0, r.readFloat64());
assertEquals(2.0, r.readFloat64());
@@ -251,8 +251,8 @@ public class ReaderImplTest extends TestCase {
@Test
public void testMapKeysMustBeUnique() throws Exception {
setContents("B" + "2" + "83666F6F" + "01" + "83626172" + "02" +
"B" + "2" + "83666F6F" + "01" + "83666F6F" + "02");
setContents("B" + "2" + "83666F6F" + "01" + "83626172" + "02"
+ "B" + "2" + "83666F6F" + "01" + "83666F6F" + "02");
// The first map has unique keys
Map<String, Byte> m = r.readMap(String.class, Byte.class);
assertNotNull(m);
@@ -353,8 +353,8 @@ public class ReaderImplTest extends TestCase {
@Test
@SuppressWarnings("unchecked")
public void testReadNestedMapsAndLists() throws Exception {
setContents("B" + "1" + "B" + "1" + "83666F6F" + "7B" +
"A" + "1" + "01");
setContents("B" + "1" + "B" + "1" + "83666F6F" + "7B"
+ "A" + "1" + "01");
Map<Object, Object> m = r.readMap(Object.class, Object.class);
assertNotNull(m);
assertEquals(1, m.size());

View File

@@ -106,7 +106,7 @@ class LanguagePanel extends WizardPanel {
private final String name, code;
Language(String name, String code) {
private Language(String name, String code) {
this.name = name;
this.code = code;
}