Code cleanup.

This commit is contained in:
akwizgran
2016-02-11 15:30:15 +00:00
parent 074892b677
commit 9f8baab60f
6 changed files with 71 additions and 68 deletions

View File

@@ -245,25 +245,22 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public Ack generateAck(Transaction transaction, ContactId c, public Ack generateAck(Transaction transaction, ContactId c,
int maxMessages) throws DbException { int maxMessages) throws DbException {
Collection<MessageId> ids;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
ids = db.getMessagesToAck(txn, c, maxMessages); Collection<MessageId> ids = db.getMessagesToAck(txn, c, maxMessages);
if (!ids.isEmpty()) db.lowerAckFlag(txn, c, ids);
if (ids.isEmpty()) return null; if (ids.isEmpty()) return null;
db.lowerAckFlag(txn, c, ids);
return new Ack(ids); return new Ack(ids);
} }
public Collection<byte[]> generateBatch(Transaction transaction, public Collection<byte[]> generateBatch(Transaction transaction,
ContactId c, int maxLength, int maxLatency) throws DbException { ContactId c, int maxLength, int maxLatency) throws DbException {
Collection<MessageId> ids;
List<byte[]> messages;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
ids = db.getMessagesToSend(txn, c, maxLength); Collection<MessageId> ids = db.getMessagesToSend(txn, c, maxLength);
messages = new ArrayList<byte[]>(ids.size()); List<byte[]> messages = new ArrayList<byte[]>(ids.size());
for (MessageId m : ids) { for (MessageId m : ids) {
messages.add(db.getRawMessage(txn, m)); messages.add(db.getRawMessage(txn, m));
db.updateExpiryTime(txn, c, m, maxLatency); db.updateExpiryTime(txn, c, m, maxLatency);
@@ -276,37 +273,35 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public Offer generateOffer(Transaction transaction, ContactId c, public Offer generateOffer(Transaction transaction, ContactId c,
int maxMessages, int maxLatency) throws DbException { int maxMessages, int maxLatency) throws DbException {
Collection<MessageId> ids;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
ids = db.getMessagesToOffer(txn, c, maxMessages); Collection<MessageId> ids = db.getMessagesToOffer(txn, c, maxMessages);
for (MessageId m : ids) db.updateExpiryTime(txn, c, m, maxLatency);
if (ids.isEmpty()) return null; if (ids.isEmpty()) return null;
for (MessageId m : ids) db.updateExpiryTime(txn, c, m, maxLatency);
return new Offer(ids); return new Offer(ids);
} }
public Request generateRequest(Transaction transaction, ContactId c, public Request generateRequest(Transaction transaction, ContactId c,
int maxMessages) throws DbException { int maxMessages) throws DbException {
Collection<MessageId> ids;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
ids = db.getMessagesToRequest(txn, c, maxMessages); Collection<MessageId> ids = db.getMessagesToRequest(txn, c,
if (!ids.isEmpty()) db.removeOfferedMessages(txn, c, ids); maxMessages);
if (ids.isEmpty()) return null; if (ids.isEmpty()) return null;
db.removeOfferedMessages(txn, c, ids);
return new Request(ids); return new Request(ids);
} }
public Collection<byte[]> generateRequestedBatch(Transaction transaction, public Collection<byte[]> generateRequestedBatch(Transaction transaction,
ContactId c, int maxLength, int maxLatency) throws DbException { ContactId c, int maxLength, int maxLatency) throws DbException {
Collection<MessageId> ids;
List<byte[]> messages;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
ids = db.getRequestedMessagesToSend(txn, c, maxLength); Collection<MessageId> ids = db.getRequestedMessagesToSend(txn, c,
messages = new ArrayList<byte[]>(ids.size()); maxLength);
List<byte[]> messages = new ArrayList<byte[]>(ids.size());
for (MessageId m : ids) { for (MessageId m : ids) {
messages.add(db.getRawMessage(txn, m)); messages.add(db.getRawMessage(txn, m));
db.updateExpiryTime(txn, c, m, maxLatency); db.updateExpiryTime(txn, c, m, maxLatency);
@@ -501,10 +496,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void receiveAck(Transaction transaction, ContactId c, Ack a) public void receiveAck(Transaction transaction, ContactId c, Ack a)
throws DbException { throws DbException {
Collection<MessageId> acked = new ArrayList<MessageId>();
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
Collection<MessageId> acked = new ArrayList<MessageId>();
for (MessageId m : a.getMessageIds()) { for (MessageId m : a.getMessageIds()) {
if (db.containsVisibleMessage(txn, c, m)) { if (db.containsVisibleMessage(txn, c, m)) {
db.raiseSeenFlag(txn, c, m); db.raiseSeenFlag(txn, c, m);
@@ -531,11 +526,11 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void receiveOffer(Transaction transaction, ContactId c, Offer o) public void receiveOffer(Transaction transaction, ContactId c, Offer o)
throws DbException { throws DbException {
boolean ack = false, request = false;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
int count = db.countOfferedMessages(txn, c); int count = db.countOfferedMessages(txn, c);
boolean ack = false, request = false;
for (MessageId m : o.getMessageIds()) { for (MessageId m : o.getMessageIds()) {
if (db.containsVisibleMessage(txn, c, m)) { if (db.containsVisibleMessage(txn, c, m)) {
db.raiseSeenFlag(txn, c, m); db.raiseSeenFlag(txn, c, m);
@@ -553,10 +548,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void receiveRequest(Transaction transaction, ContactId c, Request r) public void receiveRequest(Transaction transaction, ContactId c, Request r)
throws DbException { throws DbException {
boolean requested = false;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
boolean requested = false;
for (MessageId m : r.getMessageIds()) { for (MessageId m : r.getMessageIds()) {
if (db.containsVisibleMessage(txn, c, m)) { if (db.containsVisibleMessage(txn, c, m)) {
db.raiseRequestedFlag(txn, c, m); db.raiseRequestedFlag(txn, c, m);
@@ -578,12 +573,11 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void removeGroup(Transaction transaction, Group g) public void removeGroup(Transaction transaction, Group g)
throws DbException { throws DbException {
Collection<ContactId> affected;
T txn = unbox(transaction); T txn = unbox(transaction);
GroupId id = g.getId(); GroupId id = g.getId();
if (!db.containsGroup(txn, id)) if (!db.containsGroup(txn, id))
throw new NoSuchGroupException(); throw new NoSuchGroupException();
affected = db.getVisibility(txn, id); Collection<ContactId> affected = db.getVisibility(txn, id);
db.removeGroup(txn, id); db.removeGroup(txn, id);
transaction.attach(new GroupRemovedEvent(g)); transaction.attach(new GroupRemovedEvent(g));
transaction.attach(new GroupVisibilityUpdatedEvent(affected)); transaction.attach(new GroupVisibilityUpdatedEvent(affected));
@@ -638,13 +632,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void setVisibleToContact(Transaction transaction, ContactId c, public void setVisibleToContact(Transaction transaction, ContactId c,
GroupId g, boolean visible) throws DbException { GroupId g, boolean visible) throws DbException {
boolean wasVisible;
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c)) if (!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
if (!db.containsGroup(txn, g)) if (!db.containsGroup(txn, g))
throw new NoSuchGroupException(); throw new NoSuchGroupException();
wasVisible = db.containsVisibleGroup(txn, c, g); boolean wasVisible = db.containsVisibleGroup(txn, c, g);
if (visible && !wasVisible) db.addVisibility(txn, c, g); if (visible && !wasVisible) db.addVisibility(txn, c, g);
else if (!visible && wasVisible) db.removeVisibility(txn, c, g); else if (!visible && wasVisible) db.removeVisibility(txn, c, g);
if (visible != wasVisible) { if (visible != wasVisible) {
@@ -655,9 +648,9 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
public void updateTransportKeys(Transaction transaction, public void updateTransportKeys(Transaction transaction,
Map<ContactId, TransportKeys> keys) throws DbException { Map<ContactId, TransportKeys> keys) throws DbException {
T txn = unbox(transaction);
Map<ContactId, TransportKeys> filtered = Map<ContactId, TransportKeys> filtered =
new HashMap<ContactId, TransportKeys>(); new HashMap<ContactId, TransportKeys>();
T txn = unbox(transaction);
for (Entry<ContactId, TransportKeys> e : keys.entrySet()) { for (Entry<ContactId, TransportKeys> e : keys.entrySet()) {
ContactId c = e.getKey(); ContactId c = e.getKey();
TransportKeys k = e.getValue(); TransportKeys k = e.getValue();

View File

@@ -45,7 +45,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH; import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH; import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
@@ -376,8 +375,8 @@ class ForumSharingManagerImpl implements ForumSharingManager, AddContactHook,
BdfDictionary d = new BdfDictionary(); BdfDictionary d = new BdfDictionary();
d.put("version", version); d.put("version", version);
d.put("local", true); d.put("local", true);
db.addLocalMessage(txn, m, CLIENT_ID, metadataEncoder.encode(d), Metadata meta = metadataEncoder.encode(d);
true); db.addLocalMessage(txn, m, CLIENT_ID, meta, true);
} catch (FormatException e) { } catch (FormatException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -154,11 +154,11 @@ class MessagingManagerImpl implements MessagingManager, AddContactHook,
@Override @Override
public Collection<PrivateMessageHeader> getMessageHeaders(ContactId c) public Collection<PrivateMessageHeader> getMessageHeaders(ContactId c)
throws DbException { throws DbException {
GroupId g = getConversationId(c);
Map<MessageId, Metadata> metadata; Map<MessageId, Metadata> metadata;
Collection<MessageStatus> statuses; Collection<MessageStatus> statuses;
Transaction txn = db.startTransaction(); Transaction txn = db.startTransaction();
try { try {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
metadata = db.getMessageMetadata(txn, g); metadata = db.getMessageMetadata(txn, g);
statuses = db.getMessageStatus(txn, c, g); statuses = db.getMessageStatus(txn, c, g);
txn.setComplete(); txn.setComplete();

View File

@@ -91,7 +91,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
db.setVisibleToContact(txn, c.getId(), g.getId(), true); db.setVisibleToContact(txn, c.getId(), g.getId(), true);
// Copy the latest local properties into the group // Copy the latest local properties into the group
DeviceId dev = db.getDeviceId(txn); DeviceId dev = db.getDeviceId(txn);
Map<TransportId, TransportProperties> local = getLocalProperties(); Map<TransportId, TransportProperties> local = getLocalProperties(txn);
for (Entry<TransportId, TransportProperties> e : local.entrySet()) { for (Entry<TransportId, TransportProperties> e : local.entrySet()) {
storeMessage(txn, g.getId(), dev, e.getKey(), e.getValue(), 1, storeMessage(txn, g.getId(), dev, e.getKey(), e.getValue(), 1,
true, true); true, true);
@@ -122,30 +122,15 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
@Override @Override
public Map<TransportId, TransportProperties> getLocalProperties() public Map<TransportId, TransportProperties> getLocalProperties()
throws DbException { throws DbException {
Map<TransportId, TransportProperties> local;
Transaction txn = db.startTransaction();
try { try {
Map<TransportId, TransportProperties> local = local = getLocalProperties(txn);
new HashMap<TransportId, TransportProperties>(); txn.setComplete();
Transaction txn = db.startTransaction(); } finally {
try { db.endTransaction(txn);
// Find the latest local update for each transport
Map<TransportId, LatestUpdate> latest = findLatest(txn,
localGroup.getId(), true);
// Retrieve and parse the latest local properties
for (Entry<TransportId, LatestUpdate> e : latest.entrySet()) {
byte[] raw = db.getRawMessage(txn, e.getValue().messageId);
local.put(e.getKey(), parseProperties(raw));
}
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return Collections.unmodifiableMap(local);
} catch (NoSuchGroupException e) {
// Local group doesn't exist - there are no local properties
return Collections.emptyMap();
} catch (FormatException e) {
throw new DbException(e);
} }
return Collections.unmodifiableMap(local);
} }
@Override @Override
@@ -255,6 +240,28 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
return privateGroupFactory.createPrivateGroup(CLIENT_ID, c); return privateGroupFactory.createPrivateGroup(CLIENT_ID, c);
} }
private Map<TransportId, TransportProperties> getLocalProperties(
Transaction txn) throws DbException {
try {
Map<TransportId, TransportProperties> local =
new HashMap<TransportId, TransportProperties>();
// Find the latest local update for each transport
Map<TransportId, LatestUpdate> latest = findLatest(txn,
localGroup.getId(), true);
// Retrieve and parse the latest local properties
for (Entry<TransportId, LatestUpdate> e : latest.entrySet()) {
byte[] raw = db.getRawMessage(txn, e.getValue().messageId);
local.put(e.getKey(), parseProperties(raw));
}
return local;
} catch (NoSuchGroupException e) {
// Local group doesn't exist - there are no local properties
return Collections.emptyMap();
} catch (FormatException e) {
throw new DbException(e);
}
}
private void storeMessage(Transaction txn, GroupId g, DeviceId dev, private void storeMessage(Transaction txn, GroupId g, DeviceId dev,
TransportId t, TransportProperties p, long version, boolean local, TransportId t, TransportProperties p, long version, boolean local,
boolean shared) throws DbException { boolean shared) throws DbException {

View File

@@ -19,14 +19,15 @@ class SettingsManagerImpl implements SettingsManager {
@Override @Override
public Settings getSettings(String namespace) throws DbException { public Settings getSettings(String namespace) throws DbException {
Settings s;
Transaction txn = db.startTransaction(); Transaction txn = db.startTransaction();
try { try {
Settings s = db.getSettings(txn, namespace); s = db.getSettings(txn, namespace);
txn.setComplete(); txn.setComplete();
return s;
} finally { } finally {
db.endTransaction(txn); db.endTransaction(txn);
} }
return s;
} }
@Override @Override

View File

@@ -190,20 +190,23 @@ public class DatabaseComponentImplTest extends BriarTestCase {
assertFalse(db.open()); assertFalse(db.open());
Transaction transaction = db.startTransaction(); Transaction transaction = db.startTransaction();
db.addLocalAuthor(transaction, localAuthor); try {
assertEquals(contactId, db.addLocalAuthor(transaction, localAuthor);
db.addContact(transaction, author, localAuthorId)); assertEquals(contactId,
assertEquals(Collections.singletonList(contact), db.addContact(transaction, author, localAuthorId));
db.getContacts(transaction)); assertEquals(Collections.singletonList(contact),
db.addGroup(transaction, group); // First time - listeners called db.getContacts(transaction));
db.addGroup(transaction, group); // Second time - not called db.addGroup(transaction, group); // First time - listeners called
assertEquals(Collections.singletonList(group), db.addGroup(transaction, group); // Second time - not called
db.getGroups(transaction, clientId)); assertEquals(Collections.singletonList(group),
db.removeGroup(transaction, group); db.getGroups(transaction, clientId));
db.removeContact(transaction, contactId); db.removeGroup(transaction, group);
db.removeLocalAuthor(transaction, localAuthorId); db.removeContact(transaction, contactId);
transaction.setComplete(); db.removeLocalAuthor(transaction, localAuthorId);
db.endTransaction(transaction); transaction.setComplete();
} finally {
db.endTransaction(transaction);
}
db.close(); db.close();
context.assertIsSatisfied(); context.assertIsSatisfied();