Search in sources :

Example 71 with ConfigurationBuilder

use of org.infinispan.configuration.cache.ConfigurationBuilder 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.");
}
Also used : DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) InfinispanJFS(org.commonjava.util.partyline.impl.infinispan.model.InfinispanJFS) Configuration(org.infinispan.configuration.cache.Configuration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) FileInputStream(java.io.FileInputStream) FileBlock(org.commonjava.util.partyline.impl.infinispan.model.FileBlock) Partyline(org.commonjava.util.partyline.Partyline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileMeta(org.commonjava.util.partyline.impl.infinispan.model.FileMeta)

Aggregations

ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)71 Configuration (org.infinispan.configuration.cache.Configuration)36 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)29 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)23 Test (org.junit.Test)19 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)9 ClusteringConfigurationBuilder (org.infinispan.configuration.cache.ClusteringConfigurationBuilder)8 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)7 Properties (java.util.Properties)6 Map (java.util.Map)5 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)5 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)5 BeforeClass (org.junit.BeforeClass)5 AdvancedCache (org.infinispan.AdvancedCache)4 BackupConfiguration (org.infinispan.configuration.cache.BackupConfiguration)3 BackupConfigurationBuilder (org.infinispan.configuration.cache.BackupConfigurationBuilder)3 CacheMode (org.infinispan.configuration.cache.CacheMode)3 SitesConfigurationBuilder (org.infinispan.configuration.cache.SitesConfigurationBuilder)3 TransportConfigurationBuilder (org.infinispan.configuration.global.TransportConfigurationBuilder)3 EmbeddedTransactionManagerLookup (org.infinispan.transaction.lookup.EmbeddedTransactionManagerLookup)3