Example #1
0
    private BlockRead cacheBlock(
        String _lookup, BlockCache cache, BlockReader _currBlock, String block) throws IOException {

      if ((cache == null) || (_currBlock.getRawSize() > cache.getMaxSize())) {
        return new BlockRead(_currBlock, _currBlock.getRawSize());
      } else {

        /** Try to fully read block for meta data if error try to close file */
        byte b[] = null;
        try {
          b = new byte[(int) _currBlock.getRawSize()];
          _currBlock.readFully(b);
        } catch (IOException e) {
          log.debug("Error full blockRead for file " + fileName + " for block " + block, e);
          throw e;
        } finally {
          _currBlock.close();
        }

        CacheEntry ce = null;
        try {
          ce = cache.cacheBlock(_lookup, b);
        } catch (Exception e) {
          log.warn("Already cached block: " + _lookup, e);
        }

        if (ce == null)
          return new BlockRead(new DataInputStream(new ByteArrayInputStream(b)), b.length);
        else return new CachedBlockRead(ce, ce.getBuffer());
      }
    }
Example #2
0
    public BlockRead getCachedMetaBlock(String blockName) throws IOException {
      String _lookup = fileName + "M" + blockName;

      if (_iCache != null) {
        CacheEntry cacheEntry = _iCache.getBlock(_lookup);

        if (cacheEntry != null) {
          return new CachedBlockRead(cacheEntry, cacheEntry.getBuffer());
        }
      }

      return null;
    }
Example #3
0
    private BlockRead getBlock(String _lookup, BlockCache cache, BlockLoader loader)
        throws IOException {

      BlockReader _currBlock;

      if (cache != null) {
        CacheEntry cb = null;
        cb = cache.getBlock(_lookup);

        if (cb != null) {
          return new CachedBlockRead(cb, cb.getBuffer());
        }
      }
      /** grab the currBlock at this point the block is still in the data stream */
      _currBlock = loader.get();

      /** If the block is bigger than the cache just return the stream */
      return cacheBlock(_lookup, cache, _currBlock, loader.getInfo());
    }
Example #4
0
    @Override
    public <T> T getIndex(Class<T> clazz) {
      T bi = null;
      synchronized (cb) {
        @SuppressWarnings("unchecked")
        SoftReference<T> softRef = (SoftReference<T>) cb.getIndex();
        if (softRef != null) bi = softRef.get();

        if (bi == null) {
          try {
            bi = clazz.newInstance();
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
          cb.setIndex(new SoftReference<T>(bi));
        }
      }

      return bi;
    }