<commit_before>/*
 *  Copyright (c) 2012 Malhar, Inc.
 *  All Rights Reserved.
 */
package com.malhartech.bufferserver.internal;

import com.malhartech.bufferserver.packet.MessageType;
import com.malhartech.bufferserver.storage.Storage;
import com.malhartech.bufferserver.util.Codec;
import com.malhartech.bufferserver.util.Codec.MutableInt;
import com.malhartech.bufferserver.util.SerializedData;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author chetan
 */
class DataListIterator implements Iterator<SerializedData>
{
  Block da;
  SerializedData current;
  private final Storage storage;
  private byte[] buffer;
  private int readOffset;
  MutableInt nextOffset = new MutableInt();
  int size;

  /**
   *
   * @param da
   */
  DataListIterator(Block da, Storage storage)
  {
    da.acquire(storage, true);
    this.da = da;
    this.storage = storage;
    buffer = da.data;
    readOffset = da.readingOffset;
  }

  /**
   *
   * @return boolean
   */
  @Override
  public synchronized boolean hasNext()
  {
    while (size == 0) {
      size = Codec.readVarInt(buffer, readOffset, da.writingOffset, nextOffset);
      switch (nextOffset.integer) {
        case -5:
          throw new RuntimeException("problemo!");

        case -4:
        case -3:
        case -2:
        case -1:
        case 0:
          if (da.writingOffset == buffer.length) {
            if (da.next == null) {
              return false;
            }

            da.release(storage, false);
            da.next.acquire(storage, true);
            da = da.next;
            size = 0;
            buffer = da.data;
            readOffset = da.readingOffset;
          }
          else {
            return false;
          }
      }
    }

    while (true) {
      if (nextOffset.integer + size <= da.writingOffset) {
        current = new SerializedData(buffer, readOffset, size + nextOffset.integer - readOffset);
        current.dataOffset = nextOffset.integer;
        return true;
      }
      else {
        if (da.writingOffset == buffer.length) {
          if (da.next == null) {
            return false;
          }
          else {
            da.release(storage, false);
            da.next.acquire(storage, true);
            da = da.next;
            size = 0;
            readOffset = nextOffset.integer = da.readingOffset;
            buffer = da.data;
          }
        }
        else {
          return false;
        }
      }
    }
  }

  @Override
  @SuppressWarnings("FinalizeDeclaration")
  protected void finalize() throws Throwable
  {
    da.release(storage, false);
    super.finalize();
  }<commit_msg>added commented out debugging messages.<commit_after>/*
 *  Copyright (c) 2012 Malhar, Inc.
 *  All Rights Reserved.
 */
package com.malhartech.bufferserver.internal;

import com.malhartech.bufferserver.packet.MessageType;
import com.malhartech.bufferserver.storage.Storage;
import com.malhartech.bufferserver.util.Codec;
import com.malhartech.bufferserver.util.Codec.MutableInt;
import com.malhartech.bufferserver.util.SerializedData;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author chetan
 */
class DataListIterator implements Iterator<SerializedData>
{
  Block da;
  SerializedData current;
  private final Storage storage;
  private byte[] buffer;
  private int readOffset;
  MutableInt nextOffset = new MutableInt();
  int size;

  /**
   *
   * @param da
   */
  DataListIterator(Block da, Storage storage)
  {
    da.acquire(storage, true);
    this.da = da;
    this.storage = storage;
    buffer = da.data;
    readOffset = da.readingOffset;
  }

  /**
   *
   * @return boolean
   */
  @Override
  public synchronized boolean hasNext()
  {
    while (size == 0) {
      size = Codec.readVarInt(buffer, readOffset, da.writingOffset, nextOffset);
      switch (nextOffset.integer) {
        case -5:
          throw new RuntimeException("problemo!");

        case -4:
        case -3:
        case -2:
        case -1:
        case 0:
          if (da.writingOffset == buffer.length) {
            if (da.next == null) {
              return false;
            }

            da.release(storage, false);
            da.next.acquire(storage, true);
            da = da.next;
            size = 0;
            buffer = da.data;
            readOffset = da.readingOffset;
          }
          else {
            return false;
          }
      }
    }

    while (true) {
      if (nextOffset.integer + size <= da.writingOffset) {
        current = new SerializedData(buffer, readOffset, size + nextOffset.integer - readOffset);
        current.dataOffset = nextOffset.integer;
        //if (buffer[current.dataOffset] == MessageType.BEGIN_WINDOW_VALUE || buffer[current.dataOffset] == MessageType.END_WINDOW_VALUE) {
        //  Tuple t = Tuple.getTuple(current.bytes, current.dataOffset, current.size - current.dataOffset + current.offset);
        //  logger.debug("next t = {}", t);
        //}
        return true;
      }
      else {
        if (da.writingOffset == buffer.length) {
          if (da.next == null) {
            return false;
          }
          else {
            da.release(storage, false);
            da.next.acquire(storage, true);
            da = da.next;
            size = 0;
            readOffset = nextOffset.integer = da.readingOffset;
            buffer = da.data;
          }
        }
        else {
          return false;
        }
      }
    }
  }

  @Override
  @SuppressWarnings("FinalizeDeclaration")
  protected void finalize() throws Throwable
  {
    da.release(storage, false);
    super.finalize();
  }
{'fraction_non_alphanumeric': 0.11095480921399684, 'fraction_numerical': 0.005275189027606823, 'mean_word_length': 2.014308426073132, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6444031', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1687, 'n_words': 373}