Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Julian Dehm
briar
Commits
0fce224d
Verified
Commit
0fce224d
authored
Aug 24, 2018
by
akwizgran
Browse files
Add method for getting cooked message from DB.
parent
3db35f70
Changes
6
Hide whitespace changes
Inline
Side-by-side
bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java
View file @
0fce224d
...
...
@@ -263,6 +263,15 @@ public interface DatabaseComponent {
*/
Collection
<
LocalAuthor
>
getLocalAuthors
(
Transaction
txn
)
throws
DbException
;
/**
* Returns the message with the given ID.
* <p/>
* Read-only.
*
* @throws MessageDeletedException if the message has been deleted
*/
Message
getMessage
(
Transaction
txn
,
MessageId
m
)
throws
DbException
;
/**
* Returns the IDs of all delivered messages in the given group.
* <p/>
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/Database.java
View file @
0fce224d
...
...
@@ -298,6 +298,15 @@ interface Database<T> {
*/
Collection
<
LocalAuthor
>
getLocalAuthors
(
T
txn
)
throws
DbException
;
/**
* Returns the message with the given ID.
* <p/>
* Read-only.
*
* @throws MessageDeletedException if the message has been deleted
*/
Message
getMessage
(
T
txn
,
MessageId
m
)
throws
DbException
;
/**
* Returns the IDs and states of all dependencies of the given message.
* For missing dependencies and dependencies in other groups, the state
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java
View file @
0fce224d
...
...
@@ -457,6 +457,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return
db
.
getLocalAuthors
(
txn
);
}
@Override
public
Message
getMessage
(
Transaction
transaction
,
MessageId
m
)
throws
DbException
{
T
txn
=
unbox
(
transaction
);
if
(!
db
.
containsMessage
(
txn
,
m
))
throw
new
NoSuchMessageException
();
return
db
.
getMessage
(
txn
,
m
);
}
@Override
public
Collection
<
MessageId
>
getMessageIds
(
Transaction
transaction
,
GroupId
g
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
View file @
0fce224d
...
...
@@ -1483,6 +1483,32 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
@Override
public
Message
getMessage
(
Connection
txn
,
MessageId
m
)
throws
DbException
{
PreparedStatement
ps
=
null
;
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT groupId, timestamp, raw FROM messages"
+
" WHERE messageId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
ps
.
setBytes
(
1
,
m
.
getBytes
());
rs
=
ps
.
executeQuery
();
if
(!
rs
.
next
())
throw
new
DbStateException
();
GroupId
g
=
new
GroupId
(
rs
.
getBytes
(
1
));
long
timestamp
=
rs
.
getLong
(
2
);
byte
[]
raw
=
rs
.
getBytes
(
3
);
if
(
rs
.
next
())
throw
new
DbStateException
();
rs
.
close
();
ps
.
close
();
if
(
raw
==
null
)
throw
new
MessageDeletedException
();
return
new
Message
(
m
,
g
,
timestamp
,
raw
);
}
catch
(
SQLException
e
)
{
tryToClose
(
rs
);
tryToClose
(
ps
);
throw
new
DbException
(
e
);
}
}
@Override
public
Collection
<
MessageId
>
getMessageIds
(
Connection
txn
,
GroupId
g
)
throws
DbException
{
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java
View file @
0fce224d
...
...
@@ -613,11 +613,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
// Check whether the message is in the DB (which it's not)
exactly
(
1
1
).
of
(
database
).
startTransaction
();
exactly
(
1
2
).
of
(
database
).
startTransaction
();
will
(
returnValue
(
txn
));
exactly
(
1
1
).
of
(
database
).
containsMessage
(
txn
,
messageId
);
exactly
(
1
2
).
of
(
database
).
containsMessage
(
txn
,
messageId
);
will
(
returnValue
(
false
));
exactly
(
1
1
).
of
(
database
).
abortTransaction
(
txn
);
exactly
(
1
2
).
of
(
database
).
abortTransaction
(
txn
);
// This is needed for getMessageStatus() to proceed
exactly
(
1
).
of
(
database
).
containsContact
(
txn
,
contactId
);
will
(
returnValue
(
true
));
...
...
@@ -645,6 +645,16 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
db
.
endTransaction
(
transaction
);
}
transaction
=
db
.
startTransaction
(
false
);
try
{
db
.
getMessage
(
transaction
,
messageId
);
fail
();
}
catch
(
NoSuchMessageException
expected
)
{
// Expected
}
finally
{
db
.
endTransaction
(
transaction
);
}
transaction
=
db
.
startTransaction
(
false
);
try
{
db
.
getRawMessage
(
transaction
,
messageId
);
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java
View file @
0fce224d
...
...
@@ -1638,6 +1638,13 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
ids
=
db
.
getMessagesToOffer
(
txn
,
contactId
,
100
);
assertEquals
(
singletonList
(
messageId
),
ids
);
// The message should be available
Message
m
=
db
.
getMessage
(
txn
,
messageId
);
assertEquals
(
messageId
,
m
.
getId
());
assertEquals
(
groupId
,
m
.
getGroupId
());
assertEquals
(
message
.
getTimestamp
(),
m
.
getTimestamp
());
assertArrayEquals
(
message
.
getRaw
(),
m
.
getRaw
());
// The raw message should be available
assertArrayEquals
(
message
.
getRaw
(),
db
.
getRawMessage
(
txn
,
messageId
));
...
...
@@ -1653,6 +1660,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
ids
=
db
.
getMessagesToOffer
(
txn
,
contactId
,
100
);
assertTrue
(
ids
.
isEmpty
());
// Requesting the message should throw an exception
try
{
db
.
getMessage
(
txn
,
messageId
);
fail
();
}
catch
(
MessageDeletedException
expected
)
{
// Expected
}
// Requesting the raw message should throw an exception
try
{
db
.
getRawMessage
(
txn
,
messageId
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment