All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.common.store.CacheStore Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.common.store;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.snapscript.common.Cache;
import org.snapscript.common.SoftCache;

public class CacheStore implements Store {

   private final Cache cache;
   private final Set failures;
   private final StoreReader reader;
   private final Store store;
   
   public CacheStore(Store store) {
      this(store, 200);
   }
   
   public CacheStore(Store store, int capacity) {
      this(store, capacity, 8192);
   }
   
   public CacheStore(Store store, int capacity, int read) {
      this.cache = new SoftCache(capacity);
      this.failures = new CopyOnWriteArraySet();
      this.reader = new StoreReader(store, read);
      this.store = store;
   }
   
   public byte[] getBytes(String path) {
      if(!failures.contains(path)) {
         byte[] resource = cache.fetch(path);
         
         if(resource == null) {
            try {
               resource = reader.getBytes(path);
               cache.cache(path, resource);
            } catch(NotFoundException cause) {
               failures.add(path);
               throw cause;
            }
         }
         if(resource == null) {
            throw new NotFoundException("Could not find '" + path + "'");
         }
         return resource;
      }
      return null;
   }
   
   public String getString(String path) {
      byte[] resource = getBytes(path);
      
      try {
         if(resource != null) {
            return new String(resource, "UTF-8");
         }
      }catch(Exception e) {
         throw new StoreException("Could not read resource '" + path + "'", e);
      }
      return null;
   }
   
   @Override
   public InputStream getInputStream(String path) {
      byte[] resource = getBytes(path);
      
      try {
         if(resource != null) {
            return new ByteArrayInputStream(resource);
         }
      }catch(Exception e) {
         throw new StoreException("Could not read resource '" + path + "'", e);
      }
      return null;
   }
   
   @Override
   public OutputStream getOutputStream(String path) {
      return store.getOutputStream(path);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy