mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Minor code cleanups.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user