Add and test InMemorySwapStore

This commit is contained in:
Winston Li
2016-08-23 23:33:01 +01:00
committed by Michael Mazour
parent f036ff2c8b
commit 4b014826d3
4 changed files with 153 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* Created by winston on 23/08/2016.
*/
public class InMemorySwapStore implements SwapStore {
private final Map<String, byte[]> store;
public InMemorySwapStore() {
store = new HashMap<>();
}
@Override
public void upload(
String projectName,
InputStream uploadStream,
long contentLength
) throws IOException {
store.put(
projectName,
IOUtils.toByteArray(uploadStream, contentLength)
);
}
@Override
public InputStream openDownloadStream(String projectName) {
byte[] buf = store.get(projectName);
if (buf == null) {
throw new IllegalArgumentException(
"no such project in swap store: " + projectName
);
}
return new ByteArrayInputStream(buf);
}
@Override
public void remove(String projectName) {
store.remove(projectName);
}
}

View File

@@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.bridge.swap;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import uk.ac.ic.wlgitbridge.util.Log;
import java.time.Duration;
import java.util.Timer;
@@ -52,7 +53,8 @@ public class SwapJobImpl implements SwapJob {
}
private void doSwap() {
swaps.incrementAndGet();
Log.info("Running {}th swap", swaps.getAndIncrement());
}
}

View File

@@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import java.io.IOException;
import java.io.InputStream;
/**
@@ -7,7 +8,11 @@ import java.io.InputStream;
*/
public interface SwapStore {
void upload(String projectName, InputStream uploadStream, long contentLength);
void upload(
String projectName,
InputStream uploadStream,
long contentLength
) throws IOException;
InputStream openDownloadStream(String projectName);