use of org.commonjava.util.partyline.impl.infinispan.model.FileBlock in project partyline by Commonjava.
the class InfinispanJFS method getNextBlock.
FileBlock getNextBlock(final FileBlock prevBlock, final FileMeta metadata) throws IOException {
String next = prevBlock.getNextBlockID();
if (next == null) {
return null;
}
// setup a cache listener for the ID, and wait in a timed loop for it to return
AtomicReference<IOException> error = new AtomicReference<>();
FileBlock ret = lockManager.lockAnd(metadata.getFilePath(), (key, opLock) -> {
ClusterListener clusterListener = new ClusterListener(next, opLock);
FileBlock nextBlock = null;
while (nextBlock == null) {
nextBlock = blockCache.get(next);
if (nextBlock == null) {
try {
clusterListener.listenToCacheAndWait(blockCache);
} catch (IOException e) {
logger.error("Exception while getting next block for file: " + metadata.getFilePath(), e);
error.set(e);
}
}
}
return nextBlock;
});
if (error.get() != null) {
throw error.get();
}
return ret;
}
use of org.commonjava.util.partyline.impl.infinispan.model.FileBlock in project partyline by Commonjava.
the class Main method main.
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: $0 <input-dir> <output-dir>");
System.exit(1);
}
File indir = new File(args[0]);
File outdir = new File(args[1]);
// Configure the block size for FileBlocks
int blockSize = 1024;
Logger logger = LoggerFactory.getLogger(Main.class);
logger.info("Copying files from: " + indir + " to: " + outdir);
DefaultCacheManager cacheManager = new DefaultCacheManager(true);
ConfigurationBuilder builder = new ConfigurationBuilder();
// Pick a config - persistence or no persistence
Configuration persistence = builder.persistence().addSingleFileStore().transaction().transactionMode(TransactionMode.TRANSACTIONAL).build();
// Configuration noPersist = builder.transaction().transactionMode( TransactionMode.TRANSACTIONAL ).build();
cacheManager.defineConfiguration("blocks", persistence);
Cache<String, FileBlock> blocks = cacheManager.getCache("blocks", true);
cacheManager.defineConfiguration("files", persistence);
Cache<String, FileMeta> files = cacheManager.getCache("files", true);
Partyline partyline = new Partyline(new InfinispanJFS("single-node", files, blocks, blockSize));
AtomicInteger inCounter = new AtomicInteger();
File[] dirFiles = indir.listFiles();
// copy from input dir to ISPN
Stream.of(dirFiles).parallel().forEach(dirFile -> {
if (!dirFile.isDirectory()) {
logger.info("Copying from input: {}", dirFile);
try (InputStream in = new FileInputStream(dirFile);
OutputStream out = partyline.openOutputStream(dirFile)) {
int b = in.read();
out.write(b);
while (b != -1) {
b = in.read();
out.write(b);
}
} catch (InterruptedException e) {
logger.error("Input copy interrupted: " + dirFile.getName(), e);
} catch (IOException e) {
logger.error("Failed to copy to input: " + dirFile.getName(), e);
} finally {
inCounter.incrementAndGet();
}
}
});
AtomicInteger outCounter = new AtomicInteger();
// pull the same files from ISPN and copy to output dir
Stream.of(dirFiles).parallel().forEach(dirFile -> {
if (!dirFile.isDirectory()) {
logger.info("Copying to output: {}", dirFile);
try (InputStream in = partyline.openInputStream(dirFile);
OutputStream out = new FileOutputStream(new File(outdir, dirFile.getName()))) {
IOUtils.copy(in, out);
} catch (InterruptedException e) {
logger.error("Output copy interrupted: " + dirFile.getName(), e);
} catch (IOException e) {
logger.error("Failed to copy to output: " + dirFile.getName(), e);
} finally {
outCounter.incrementAndGet();
}
}
});
System.out.println("Copied " + inCounter.get() + " files into Infinispan, and " + outCounter + " back out to destination directory.");
}
Aggregations