From ea7ea3b30485cb66b42286effef9eeba00cc16ff Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Mon, 29 Aug 2016 10:45:06 +0200 Subject: [PATCH] SPVBlockStore: Add constructor for a custom capacity block store. --- .../org/bitcoinj/store/SPVBlockStore.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java b/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java index 68371ee2..3d28efed 100644 --- a/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java +++ b/core/src/main/java/org/bitcoinj/store/SPVBlockStore.java @@ -44,8 +44,8 @@ public class SPVBlockStore implements BlockStore { public static final String HEADER_MAGIC = "SPVB"; protected volatile MappedByteBuffer buffer; - protected int capacity; - protected NetworkParameters params; + protected final int capacity; + protected final NetworkParameters params; protected ReentrantLock lock = Threading.lock("SPVBlockStore"); @@ -80,14 +80,28 @@ public class SPVBlockStore implements BlockStore { protected RandomAccessFile randomAccessFile = null; /** - * Creates and initializes an SPV block store. Will create the given file if it's missing. This operation - * will block on disk. + * Creates and initializes an SPV block store that can hold {@link #DEFAULT_CAPACITY} blocks. Will create the given + * file if it's missing. This operation will block on disk. + * @param file file to use for the block store + * @throws BlockStoreException if something goes wrong */ public SPVBlockStore(NetworkParameters params, File file) throws BlockStoreException { + this(params, file, DEFAULT_CAPACITY); + } + + /** + * Creates and initializes an SPV block store that can hold a given amount of blocks. Will create the given file if + * it's missing. This operation will block on disk. + * @param file file to use for the block store + * @param capacity custom capacity + * @throws BlockStoreException if something goes wrong + */ + public SPVBlockStore(NetworkParameters params, File file, int capacity) throws BlockStoreException { checkNotNull(file); this.params = checkNotNull(params); + checkArgument(capacity > 0); + this.capacity = capacity; try { - this.capacity = DEFAULT_CAPACITY; boolean exists = file.exists(); // Set up the backing file. randomAccessFile = new RandomAccessFile(file, "rw");