mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Add placeholder BlockSource implementation to DB.
This commit is contained in:
@@ -208,6 +208,24 @@ public interface DatabaseComponent {
|
|||||||
Collection<Message> generateRequestedBatch(Transaction txn, ContactId c,
|
Collection<Message> generateRequestedBatch(Transaction txn, ContactId c,
|
||||||
int maxLength, int maxLatency) throws DbException;
|
int maxLength, int maxLatency) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of blocks in the given message.
|
||||||
|
* <p>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
int getBlockCount(Transaction txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the given block of the given message.
|
||||||
|
* <p>
|
||||||
|
* Read-only.
|
||||||
|
*
|
||||||
|
* @throws NoSuchBlockException if 'blockNumber' is greater than or equal
|
||||||
|
* to the number of blocks in the message
|
||||||
|
*/
|
||||||
|
byte[] getBlock(Transaction txn, MessageId m, int blockNumber)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the contact with the given ID.
|
* Returns the contact with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.briarproject.bramble.api.db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when a database operation is attempted for a block that is not in
|
||||||
|
* the database. This exception may occur due to concurrent updates and does
|
||||||
|
* not indicate a database error.
|
||||||
|
*/
|
||||||
|
public class NoSuchBlockException extends DbException {
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.briarproject.bramble.db;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.io.BlockSource;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
class BlockSourceImpl implements BlockSource {
|
||||||
|
|
||||||
|
private final DatabaseComponent db;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
BlockSourceImpl(DatabaseComponent db) {
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBlockCount(MessageId m) throws DbException {
|
||||||
|
return db.transactionWithResult(true, txn -> db.getBlockCount(txn, m));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBlock(MessageId m, int blockNumber) throws DbException {
|
||||||
|
return db.transactionWithResult(true, txn ->
|
||||||
|
db.getBlock(txn, m, blockNumber));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ import org.briarproject.bramble.api.db.DbException;
|
|||||||
import org.briarproject.bramble.api.db.DbRunnable;
|
import org.briarproject.bramble.api.db.DbRunnable;
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
import org.briarproject.bramble.api.db.Metadata;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
|
import org.briarproject.bramble.api.db.NoSuchBlockException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchGroupException;
|
import org.briarproject.bramble.api.db.NoSuchGroupException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchLocalAuthorException;
|
import org.briarproject.bramble.api.db.NoSuchLocalAuthorException;
|
||||||
@@ -420,6 +421,26 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBlockCount(Transaction transaction, MessageId m)
|
||||||
|
throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsMessage(txn, m))
|
||||||
|
throw new NoSuchMessageException();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBlock(Transaction transaction, MessageId m,
|
||||||
|
int blockNumber) throws DbException {
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsMessage(txn, m))
|
||||||
|
throw new NoSuchMessageException();
|
||||||
|
if (blockNumber != 0)
|
||||||
|
throw new NoSuchBlockException();
|
||||||
|
return db.getMessage(txn, m).getBody();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Contact getContact(Transaction transaction, ContactId c)
|
public Contact getContact(Transaction transaction, ContactId c)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.briarproject.bramble.db;
|
|||||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
|
import org.briarproject.bramble.api.io.BlockSource;
|
||||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||||
import org.briarproject.bramble.api.sync.MessageFactory;
|
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
@@ -31,4 +32,9 @@ public class DatabaseModule {
|
|||||||
return new DatabaseComponentImpl<>(db, Connection.class, eventBus,
|
return new DatabaseComponentImpl<>(db, Connection.class, eventBus,
|
||||||
shutdown);
|
shutdown);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
BlockSource provideBlockSource(BlockSourceImpl blockSource) {
|
||||||
|
return blockSource;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user