mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-31 21:01:33 +02:00
Add and test InMemorySwapStore
This commit is contained in:
committed by
Michael Mazour
parent
f036ff2c8b
commit
4b014826d3
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user