Package com.slytechs.utils.memory

Examples of com.slytechs.utils.memory.PartialBuffer


  }

  private long getRecordLength(final long regional,
      final HeaderReader headerReader) throws IOException {

    final PartialBuffer bblock;
    try {
      bblock = this.loader.fetchBlock(regional, headerReader.getMinLength());

    } catch (BufferFetchException e) {
      e.setFlexRegion(edits);
      e.setMessage("Unable to read length from header");
      e.setHeaderReader(headerReader);
      throw e;
    }

    final ByteBuffer buffer = bblock.getByteBuffer();
    final long length = headerReader.readLength(buffer);

    if ((length < 0) || (length > 100000)) {
      AbstractRawIterator.logger.error("Invalid record length value (" + length
          + ") at record position (" + this.global + ")");
View Full Code Here


     * into memory if we only need to look at one 4K block in it. Since the
     * cache is checked first, if the block does already exist in the memory
     * mapped case, then the cached block will be used. Also note that the fetch
     * version with MemoryModel, does not cache the returned buffers.
     */
    final PartialBuffer buf = loader.fetchMinimum(regional,
        AbstractRawIterator.SEARCH_LENGTH, MemoryModel.ByteArray);

    final int p = (int) (global - segment.mapRegionalToGlobal(buf
        .getStartRegional()));
    int maxSearch = (int) buf.getLength() - p;
    maxSearch = (maxSearch < AbstractRawIterator.SEARCH_LENGTH) ? maxSearch
        : AbstractRawIterator.SEARCH_LENGTH;

    /*
     * If we don't have enough bytes in this segment for even the minLength,
     * then no more records can be found here. The next segment, must contain a
     * record at its begining, therefore we can simply align there and call
     * hasNext() to confirm and apply the recordFilter if one has been defined.
     * If hasNext() can't fullfill the request due to a recordFilter, it will
     * return false.
     */
    if (maxSearch < this.pattern.minLength()) {
      final long nextSegmentStart = segment.getEndGlobal();
      setPosition(nextSegmentStart);

      return (this.hasNext() ? OK : NOT_OK);
    }

    buf.getByteBuffer().limit(p + maxSearch);

    final long local = this.searchForRecordStart(buf.getByteBuffer(), p, maxSearch);
    if (local == -1) {
      /*
       * Current segment did not contain a beginning of a packet, therefore move
       * on to the next segment. The start of each segment should begin with a
       * record, so this we should find the next record at startNextSegment
       * position, although if the file is corrupt, then we might need to search
       * through it until the next record.
       */
      final long startNextSegment = segment.getEnd();

      if (startNextSegment == this.edits.getLength()) {
        /*
         * No more segments, so we just searched through last segment and did
         * not find start of record. Therefore seek to the end past the last
         * record.
         */
        this.seekEnd();

        return NOT_OK;
      } else {
        /*
         * Every segment always starts with atleast 1 record. It is illegal to
         * have segments with not records in them in any of the file formats.
         */
        this.setPosition(startNextSegment);
        return (this.hasNext() ? OK : NOT_OK);
      }
    }

    final long reg = buf.mapLocalToRegional(local);
    @SuppressWarnings("unused")
    final long glob = segment.mapRegionalToGlobal(reg);

    // if (TestFilter.positions.contains(glob) == false) {
    // System.out.printf("Not found in positions %d\n", glob);
View Full Code Here

   * (non-Javadoc)
   *
   * @see com.slytechs.capture.file.editor.PartialLoader#fetchBlock(long, int)
   */
  public PartialBuffer fetchBlock(long regional, int length) throws IOException {
    PartialBuffer p = fetchFromCache(regional, length);
    final boolean fromCache;

    if (p == null) {

      final int blockSize = pickBlockSize(regional, length);
      final MemoryModel model = pickMemoryModel(blockSize);

      p = fetchFromChannelAndCache(regional, blockSize, model);
      fromCache = false;

    } else {
      fromCache = true;
    }

    try {
      p.reposition(regional, length);

    } catch (IndexOutOfBoundsException e) {
      throw new BufferFetchException("Can not reposition buffer", p,
          regional, length, fromCache, e);
    }
View Full Code Here

   * @see com.slytechs.capture.file.editor.PartialLoader#fetchBlock(long, int,
   *      com.slytechs.utils.memory.MemoryModel)
   */
  public PartialBuffer fetchBlock(long regional, int length, MemoryModel model)
      throws IOException {
    PartialBuffer p = fetchFromCache(regional, length);

    if (p == null) {
      final int blockSize = pickBlockSize(regional, length);

      p = fetchFromChannelAndCache(regional, blockSize, model);
    }

    p.reposition(regional, length);

    return p;
  }
View Full Code Here

    return p;
  }

  private PartialBuffer fetchFromCache(final long start, final int size) {
    PartialBuffer blockBuffer = null;

    final long last = start + size - 1;

    /*
     * cache size is always fluctuating as weak references are retired, must use
     * the dynamic for, also note that you are not allowed to use the list
     * iterator as its disabled.
     */
    for (int i = 0; i < cache.size(); i++) {
      blockBuffer = cache.get(i);
      if (blockBuffer == null) {
        return null;
      }

      if (start >= blockBuffer.getStartRegional()
          && last < blockBuffer.getEndRegional()) {
        return blockBuffer;
      }
    }

    return null;
View Full Code Here

   *
   * @see com.slytechs.capture.file.editor.PartialLoader#fetchMinimum(long, int)
   */
  public PartialBuffer fetchMinimum(long regional, int length)
      throws IOException {
    PartialBuffer p = fetchFromCache(regional, length);
    if (p == null) {
      final MemoryModel model = pickMemoryModel(length);
      final int minimum = pickMinimumSize(regional, length);

      p = fetchFromChannelAndCache(regional, minimum, model);

    } else {
      final int minimum = pickMinimumSize(regional, length, p);

      p.reposition(regional, minimum);
    }

    return p;
  }
View Full Code Here

   * @see com.slytechs.capture.file.editor.PartialLoader#fetchMinimum(long, int,
   *      com.slytechs.utils.memory.MemoryModel)
   */
  public PartialBuffer fetchMinimum(long regional, int length, MemoryModel model)
      throws IOException {
    PartialBuffer p = fetchFromCache(regional, length);
    if (p == null) {
      final int minimum = pickMinimumSize(regional, length);

      p = fetchFromChannelAndCache(regional, minimum, model);
      p.reposition(regional, minimum);

    } else {
      final int minimum = pickMinimumSize(regional, length, p);

      p.reposition(regional, minimum);
    }

    return p;
  }
View Full Code Here

    final RegionSegment<PartialLoader> segment = this.edits.getSegment(global);
    final PartialLoader loader = segment.getData();
    final long regional = segment.mapGlobalToRegional(global);

    final PartialBuffer blockBuffer = loader.fetchBlock(regional, minLength);

    final int p = (int) (regional - blockBuffer.getStartRegional());

    /*
     * Make sure the next record we want to fetch resides in the existing shared
     * buffer, otherwise we have to prefetch another buffer.
     */
    if ((p < 0)
        || (blockBuffer.checkBoundsRegional(regional, minLength) == false)) {

      throw new IllegalStateException("Unable to prefetch buffer [" + regional
          + "/" + minLength + "]");
    }

    final ByteBuffer buffer = blockBuffer.getByteBuffer();

    buffer.limit(p + lengthGetter.getMinLength());
    buffer.position(p);

    return blockBuffer;
View Full Code Here

   * @throws IOException
   */
  public ByteBuffer get(final long global, final HeaderReader blockGetter)
      throws IOException {

    final PartialBuffer min = this.fetchPartialBuffer(blockGetter, global,
        blockGetter.getMinLength());

    final long length = blockGetter.readLength(min.getByteBuffer());
    final int regional = (int) min.getStartRegional();

    final PartialBuffer partial;
    if (min.checkBoundsRegional(regional, regional + (int) length) == false) {
      partial = fetchPartialBuffer(headerReader, global, (int) length);
    } else {
      partial = min;
    }

    partial.reposition(regional, (int) length);

    return partial.getByteBuffer();

  }
View Full Code Here

TOP

Related Classes of com.slytechs.utils.memory.PartialBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.