Add 32-bit int methods to BdfList and BdfDictionary.

We use these a lot so it's useful to have built-in support.

Also refactor BdfList and BdfDictionary so the getters that take default values behave like the other getters. This simplifies the semantics and allows duplicated code to be removed.

Add comprehensive tests for BdfList and BdfDictionary.
This commit is contained in:
akwizgran
2023-02-18 15:54:47 +00:00
parent 7a854e70cb
commit 63172ef2e4
36 changed files with 820 additions and 294 deletions

View File

@@ -56,7 +56,7 @@ public class AttachmentReaderImpl implements AttachmentReader {
String contentType = meta.getString(MSG_KEY_CONTENT_TYPE);
if (!contentType.equals(h.getContentType()))
throw new NoSuchMessageException();
int offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue();
int offset = meta.getInt(MSG_KEY_DESCRIPTOR_LENGTH);
InputStream stream = new ByteArrayInputStream(body, offset,
body.length - offset);
return new Attachment(h, stream);

View File

@@ -250,7 +250,7 @@ class AvatarManagerImpl implements AvatarManager, OpenDatabaseHook, ContactHook,
try {
BdfDictionary meta =
clientHelper.getGroupMetadataAsDictionary(txn, g);
return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue());
return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID));
} catch (FormatException e) {
throw new DbException(e);
}

View File

@@ -487,7 +487,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
}
private String getPostText(BdfList message) throws FormatException {
MessageType type = MessageType.valueOf(message.getLong(0).intValue());
MessageType type = MessageType.valueOf(message.getInt(0));
if (type == POST) {
// Type, text, signature
return message.getString(1);
@@ -621,7 +621,6 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
}
private MessageType getMessageType(BdfDictionary d) throws FormatException {
Long longType = d.getLong(KEY_TYPE);
return MessageType.valueOf(longType.intValue());
return MessageType.valueOf(d.getInt(KEY_TYPE));
}
}

View File

@@ -167,6 +167,6 @@ class BlogPostFactoryImpl implements BlogPostFactory {
}
private MessageType getType(BdfList body) throws FormatException {
return MessageType.valueOf(body.getLong(0).intValue());
return MessageType.valueOf(body.getInt(0));
}
}

View File

@@ -72,7 +72,7 @@ class BlogPostValidator extends BdfMessageValidator {
BdfMessageContext c;
int type = body.getLong(0).intValue();
int type = body.getInt(0);
body.remove(0);
switch (MessageType.valueOf(type)) {
case POST:

View File

@@ -114,8 +114,8 @@ class MessageTrackerImpl implements MessageTracker {
try {
BdfDictionary d = clientHelper.getGroupMetadataAsDictionary(txn, g);
return new GroupCount(
d.getLong(GROUP_KEY_MSG_COUNT, 0L).intValue(),
d.getLong(GROUP_KEY_UNREAD_COUNT, 0L).intValue(),
d.getInt(GROUP_KEY_MSG_COUNT, 0),
d.getInt(GROUP_KEY_UNREAD_COUNT, 0),
d.getLong(GROUP_KEY_LATEST_MSG, 0L)
);
} catch (FormatException e) {

View File

@@ -47,7 +47,7 @@ class IntroductionValidator extends BdfMessageValidator {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws FormatException {
MessageType type = MessageType.fromValue(body.getLong(0).intValue());
MessageType type = MessageType.fromValue(body.getInt(0));
switch (type) {
case REQUEST:

View File

@@ -59,8 +59,8 @@ class MessageParserImpl implements MessageParser {
@Override
public MessageMetadata parseMetadata(BdfDictionary d)
throws FormatException {
MessageType type = MessageType
.fromValue(d.getLong(MSG_KEY_MESSAGE_TYPE).intValue());
MessageType type =
MessageType.fromValue(d.getInt(MSG_KEY_MESSAGE_TYPE));
byte[] sessionIdBytes = d.getOptionalRaw(MSG_KEY_SESSION_ID);
SessionId sessionId =
sessionIdBytes == null ? null : new SessionId(sessionIdBytes);

View File

@@ -72,7 +72,7 @@ class SessionParserImpl implements SessionParser {
@Override
public Role getRole(BdfDictionary d) throws FormatException {
return Role.fromValue(d.getLong(SESSION_KEY_ROLE).intValue());
return Role.fromValue(d.getInt(SESSION_KEY_ROLE));
}
@Override
@@ -97,7 +97,7 @@ class SessionParserImpl implements SessionParser {
MessageId lastRemoteMessageId =
getMessageId(d, SESSION_KEY_LAST_REMOTE_MESSAGE_ID);
long localTimestamp = d.getLong(SESSION_KEY_LOCAL_TIMESTAMP);
GroupId groupId = getGroupId(d, SESSION_KEY_GROUP_ID);
GroupId groupId = getGroupId(d);
Author author = getAuthor(d, SESSION_KEY_AUTHOR);
return new Introducee(sessionId, groupId, author, localTimestamp,
lastLocalMessageId, lastRemoteMessageId);
@@ -126,12 +126,10 @@ class SessionParserImpl implements SessionParser {
MessageId lastLocalMessageId =
getMessageId(d, SESSION_KEY_LAST_LOCAL_MESSAGE_ID);
long localTimestamp = d.getLong(SESSION_KEY_LOCAL_TIMESTAMP);
PublicKey ephemeralPublicKey =
getEphemeralPublicKey(d, SESSION_KEY_EPHEMERAL_PUBLIC_KEY);
PublicKey ephemeralPublicKey = getEphemeralPublicKey(d);
BdfDictionary tpDict =
d.getOptionalDictionary(SESSION_KEY_TRANSPORT_PROPERTIES);
PrivateKey ephemeralPrivateKey =
getEphemeralPrivateKey(d, SESSION_KEY_EPHEMERAL_PRIVATE_KEY);
PrivateKey ephemeralPrivateKey = getEphemeralPrivateKey(d);
Map<TransportId, TransportProperties> transportProperties =
tpDict == null ? null : clientHelper
.parseAndValidateTransportPropertiesMap(tpDict);
@@ -147,8 +145,7 @@ class SessionParserImpl implements SessionParser {
Author remoteAuthor = getAuthor(d, SESSION_KEY_REMOTE_AUTHOR);
MessageId lastRemoteMessageId =
getMessageId(d, SESSION_KEY_LAST_REMOTE_MESSAGE_ID);
PublicKey ephemeralPublicKey =
getEphemeralPublicKey(d, SESSION_KEY_EPHEMERAL_PUBLIC_KEY);
PublicKey ephemeralPublicKey = getEphemeralPublicKey(d);
BdfDictionary tpDict =
d.getOptionalDictionary(SESSION_KEY_TRANSPORT_PROPERTIES);
Map<TransportId, TransportProperties> transportProperties =
@@ -162,7 +159,7 @@ class SessionParserImpl implements SessionParser {
}
private int getState(BdfDictionary d) throws FormatException {
return d.getLong(SESSION_KEY_STATE).intValue();
return d.getInt(SESSION_KEY_STATE);
}
private SessionId getSessionId(BdfDictionary d) throws FormatException {
@@ -177,9 +174,8 @@ class SessionParserImpl implements SessionParser {
return b == null ? null : new MessageId(b);
}
private GroupId getGroupId(BdfDictionary d, String key)
throws FormatException {
return new GroupId(d.getRaw(key));
private GroupId getGroupId(BdfDictionary d) throws FormatException {
return new GroupId(d.getRaw(SESSION_KEY_GROUP_ID));
}
private Author getAuthor(BdfDictionary d, String key)
@@ -193,23 +189,22 @@ class SessionParserImpl implements SessionParser {
if (d == null) return null;
Map<TransportId, KeySetId> map = new HashMap<>(d.size());
for (String key : d.keySet()) {
map.put(new TransportId(key),
new KeySetId(d.getLong(key).intValue()));
map.put(new TransportId(key), new KeySetId(d.getInt(key)));
}
return map;
}
@Nullable
private PublicKey getEphemeralPublicKey(BdfDictionary d, String key)
throws FormatException {
byte[] keyBytes = d.getOptionalRaw(key);
private PublicKey getEphemeralPublicKey(BdfDictionary d)
throws FormatException {
byte[] keyBytes = d.getOptionalRaw(SESSION_KEY_EPHEMERAL_PUBLIC_KEY);
return keyBytes == null ? null : new AgreementPublicKey(keyBytes);
}
@Nullable
private PrivateKey getEphemeralPrivateKey(BdfDictionary d, String key)
throws FormatException {
byte[] keyBytes = d.getOptionalRaw(key);
private PrivateKey getEphemeralPrivateKey(BdfDictionary d)
throws FormatException {
byte[] keyBytes = d.getOptionalRaw(SESSION_KEY_EPHEMERAL_PRIVATE_KEY);
return keyBytes == null ? null : new AgreementPrivateKey(keyBytes);
}
}

View File

@@ -371,7 +371,7 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook,
try {
BdfDictionary meta =
clientHelper.getGroupMetadataAsDictionary(txn, g);
return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue());
return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID));
} catch (FormatException e) {
throw new DbException(e);
}
@@ -381,7 +381,7 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook,
public ContactId getContactId(GroupId g) throws DbException {
try {
BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(g);
return new ContactId(meta.getLong(GROUP_KEY_CONTACT_ID).intValue());
return new ContactId(meta.getInt(GROUP_KEY_CONTACT_ID));
} catch (FormatException e) {
throw new DbException(e);
}

View File

@@ -84,7 +84,7 @@ class PrivateMessageValidator implements MessageValidator {
context = validateLegacyPrivateMessage(m, list);
} else {
// Private message or attachment
int messageType = list.getLong(0).intValue();
int messageType = list.getInt(0);
if (messageType == PRIVATE_MESSAGE) {
if (!reader.eof()) throw new FormatException();
context = validatePrivateMessage(m, list);

View File

@@ -63,7 +63,7 @@ class GroupMessageValidator extends BdfMessageValidator {
checkSize(body, 4, 6);
// Message type (int)
int type = body.getLong(0).intValue();
int type = body.getInt(0);
// Member (author)
BdfList memberList = body.getList(1);

View File

@@ -530,8 +530,7 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
BdfList body, BdfDictionary meta)
throws DbException, FormatException {
MessageType type =
MessageType.valueOf(meta.getLong(KEY_TYPE).intValue());
MessageType type = MessageType.valueOf(meta.getInt(KEY_TYPE));
switch (type) {
case JOIN:
handleJoinMessage(txn, m, meta);
@@ -576,8 +575,8 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
.getMessageMetadataAsDictionary(txn, parentId);
if (timestamp <= parentMeta.getLong(KEY_TIMESTAMP))
throw new FormatException();
MessageType parentType = MessageType
.valueOf(parentMeta.getLong(KEY_TYPE).intValue());
MessageType parentType =
MessageType.valueOf(parentMeta.getInt(KEY_TYPE));
if (parentType != POST)
throw new FormatException();
}
@@ -592,8 +591,8 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
if (!getAuthor(meta).equals(getAuthor(previousMeta)))
throw new FormatException();
// previous message must be a POST or JOIN
MessageType previousType = MessageType
.valueOf(previousMeta.getLong(KEY_TYPE).intValue());
MessageType previousType =
MessageType.valueOf(previousMeta.getInt(KEY_TYPE));
if (previousType != JOIN && previousType != POST)
throw new FormatException();
// track message and broadcast event
@@ -641,8 +640,7 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
private Visibility getVisibility(BdfDictionary meta)
throws FormatException {
return Visibility
.valueOf(meta.getLong(GROUP_KEY_VISIBILITY).intValue());
return Visibility.valueOf(meta.getInt(GROUP_KEY_VISIBILITY));
}
}

View File

@@ -712,9 +712,9 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
// get all sessions and their states
Map<GroupId, DeletableSession> sessions = new HashMap<>();
for (BdfDictionary d : metadata.values()) {
if (!sessionParser.isSession(d)) continue;
Session<?> session;
try {
if (!sessionParser.isSession(d)) continue;
session = sessionParser.parseSession(g, d);
} catch (FormatException e) {
throw new DbException(e);
@@ -776,13 +776,12 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
// assign protocol messages to their sessions
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
// skip all sessions, we are only interested in messages
BdfDictionary d = entry.getValue();
if (sessionParser.isSession(d)) continue;
// parse message metadata and skip messages not visible in UI
MessageMetadata m;
try {
// skip all sessions, we are only interested in messages
BdfDictionary d = entry.getValue();
if (sessionParser.isSession(d)) continue;
m = messageParser.parseMetadata(d);
} catch (FormatException e) {
throw new DbException(e);

View File

@@ -56,7 +56,7 @@ class GroupInvitationValidator extends BdfMessageValidator {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws FormatException {
MessageType type = MessageType.fromValue(body.getLong(0).intValue());
MessageType type = MessageType.fromValue(body.getInt(0));
switch (type) {
case INVITE:
return validateInviteMessage(m, body);

View File

@@ -71,8 +71,8 @@ class MessageParserImpl implements MessageParser {
@Override
public MessageMetadata parseMetadata(BdfDictionary meta)
throws FormatException {
MessageType type = MessageType.fromValue(
meta.getLong(MSG_KEY_MESSAGE_TYPE).intValue());
MessageType type =
MessageType.fromValue(meta.getInt(MSG_KEY_MESSAGE_TYPE));
GroupId privateGroupId =
new GroupId(meta.getRaw(MSG_KEY_PRIVATE_GROUP_ID));
long timestamp = meta.getLong(MSG_KEY_TIMESTAMP);

View File

@@ -15,7 +15,7 @@ interface SessionParser {
Role getRole(BdfDictionary d) throws FormatException;
boolean isSession(BdfDictionary d);
boolean isSession(BdfDictionary d) throws FormatException;
Session parseSession(GroupId contactGroupId, BdfDictionary d)
throws FormatException;

View File

@@ -45,11 +45,11 @@ class SessionParserImpl implements SessionParser {
@Override
public Role getRole(BdfDictionary d) throws FormatException {
return Role.fromValue(d.getLong(SESSION_KEY_ROLE).intValue());
return Role.fromValue(d.getInt(SESSION_KEY_ROLE));
}
@Override
public boolean isSession(BdfDictionary d) {
public boolean isSession(BdfDictionary d) throws FormatException {
return d.getBoolean(SESSION_KEY_IS_SESSION, false);
}
@@ -101,7 +101,7 @@ class SessionParserImpl implements SessionParser {
}
private int getState(BdfDictionary d) throws FormatException {
return d.getLong(SESSION_KEY_STATE).intValue();
return d.getInt(SESSION_KEY_STATE);
}
private GroupId getPrivateGroupId(BdfDictionary d) throws FormatException {

View File

@@ -64,8 +64,8 @@ abstract class MessageParserImpl<S extends Shareable>
@Override
public MessageMetadata parseMetadata(BdfDictionary meta)
throws FormatException {
MessageType type = MessageType
.fromValue(meta.getLong(MSG_KEY_MESSAGE_TYPE).intValue());
MessageType type =
MessageType.fromValue(meta.getInt(MSG_KEY_MESSAGE_TYPE));
GroupId shareableId = new GroupId(meta.getRaw(MSG_KEY_SHAREABLE_ID));
long timestamp = meta.getLong(MSG_KEY_TIMESTAMP);
boolean local = meta.getBoolean(MSG_KEY_LOCAL);

View File

@@ -13,7 +13,7 @@ interface SessionParser {
BdfDictionary getAllSessionsQuery();
boolean isSession(BdfDictionary d);
boolean isSession(BdfDictionary d) throws FormatException;
Session parseSession(GroupId contactGroupId, BdfDictionary d)
throws FormatException;

View File

@@ -40,7 +40,7 @@ class SessionParserImpl implements SessionParser {
}
@Override
public boolean isSession(BdfDictionary d) {
public boolean isSession(BdfDictionary d) throws FormatException {
return d.getBoolean(SESSION_KEY_IS_SESSION, false);
}
@@ -54,7 +54,7 @@ class SessionParserImpl implements SessionParser {
}
private int getState(BdfDictionary d) throws FormatException {
return d.getLong(SESSION_KEY_STATE).intValue();
return d.getInt(SESSION_KEY_STATE);
}
private GroupId getShareableId(BdfDictionary d) throws FormatException {

View File

@@ -604,9 +604,9 @@ abstract class SharingManagerImpl<S extends Shareable>
// get all sessions and their states
Map<GroupId, DeletableSession> sessions = new HashMap<>();
for (BdfDictionary d : metadata.values()) {
if (!sessionParser.isSession(d)) continue;
Session session;
try {
if (!sessionParser.isSession(d)) continue;
session = sessionParser.parseSession(contactGroup, d);
} catch (FormatException e) {
throw new DbException(e);
@@ -668,13 +668,12 @@ abstract class SharingManagerImpl<S extends Shareable>
// assign protocol messages to their sessions
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
// skip all sessions, we are only interested in messages
BdfDictionary d = entry.getValue();
if (sessionParser.isSession(d)) continue;
// parse message metadata and skip messages not visible in UI
MessageMetadata m;
try {
// skip all sessions, we are only interested in messages
BdfDictionary d = entry.getValue();
if (sessionParser.isSession(d)) continue;
m = messageParser.parseMetadata(d);
} catch (FormatException e) {
throw new DbException(e);

View File

@@ -40,7 +40,7 @@ abstract class SharingValidator extends BdfMessageValidator {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws FormatException {
MessageType type = MessageType.fromValue(body.getLong(0).intValue());
MessageType type = MessageType.fromValue(body.getInt(0));
switch (type) {
case INVITE:
return validateInviteMessage(m, body);

View File

@@ -651,7 +651,7 @@ public class GroupMessageValidatorTest extends ValidatorTestCase {
MessageType type, BdfList member,
Collection<MessageId> dependencies) throws FormatException {
BdfDictionary d = c.getDictionary();
assertEquals(type.getInt(), d.getLong(KEY_TYPE).intValue());
assertEquals(type.getInt(), d.getInt(KEY_TYPE).intValue());
assertEquals(message.getTimestamp(),
d.getLong(KEY_TIMESTAMP).longValue());
assertFalse(d.getBoolean(KEY_READ));