#include "Hash_Iterator_i.h"
#include "orbsvcs/Log/Log_Constraint_Interpreter.h"
#include "orbsvcs/Log/Log_Constraint_Visitors.h"
#include "orbsvcs/DsLogAdminC.h"

ACE_RCSID (Log,
           Hash_Iterator_i,
           "Hash_Iterator_i.cpp,v 1.3 2006/01/26 15:43:52 jtc Exp")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_Hash_Iterator_i::TAO_Hash_Iterator_i (
  ACE_Reactor* reactor,
  TAO_Hash_LogRecordStore* recordstore,
  TAO_Hash_LogRecordStore::LOG_RECORD_STORE_ITER iter,
  TAO_Hash_LogRecordStore::LOG_RECORD_STORE_ITER iter_end,
  CORBA::ULong start,
  const char *constraint,
  CORBA::ULong max_rec_list_len)
  : TAO_Iterator_i(reactor),
    recordstore_ (recordstore),
    iter_ (iter),
    iter_end_ (iter_end),
    current_position_(start),
    constraint_ (constraint),
    max_rec_list_len_ (max_rec_list_len)
{
}


TAO_Hash_Iterator_i::~TAO_Hash_Iterator_i (void)
{
}


DsLogAdmin::RecordList*
TAO_Hash_Iterator_i::get (CORBA::ULong position,
                          CORBA::ULong how_many
                          ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   DsLogAdmin::InvalidParam))
{
  ACE_READ_GUARD_THROW_EX (ACE_SYNCH_RW_MUTEX,
                           guard,
                           this->recordstore_->lock (),
                           CORBA::INTERNAL ());
  ACE_CHECK_RETURN (0);

  if (position < current_position_)
    {
      ACE_THROW_RETURN (DsLogAdmin::InvalidParam (), 0);
    }

  if (how_many == 0)
    {
      how_many = this->max_rec_list_len_;
    }

  // Use an Interpreter to build an expression tree.
  TAO_Log_Constraint_Interpreter interpreter (constraint_.in ()
                                              ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  // Sequentially iterate over all the records and pick the ones that
  // meet the constraints.

  // Allocate the list of <how_many> length.
  DsLogAdmin::RecordList* rec_list;
  ACE_NEW_THROW_EX (rec_list,
                    DsLogAdmin::RecordList (how_many),
                    CORBA::NO_MEMORY ());
  ACE_CHECK_RETURN (0);

  CORBA::ULong count = 0;
  CORBA::ULong current_position = this->current_position_;

  for ( ;
       ((this->iter_ != this->iter_end_) && (count < how_many));
       ++this->iter_)
    {
      // Use an evaluator.
      TAO_Log_Constraint_Visitor visitor ((*this->iter_).int_id_);

      // Does it match the constraint?
      if (interpreter.evaluate (visitor) == 1)
        {
          if (++current_position >= position)
            {
              (*rec_list)[count] = (*this->iter_).int_id_;
              // copy the log record.
              count++;
            }
        }
    }

  rec_list->length (count);
  this->current_position_ = current_position;

  if (count == 0 && this->iter_ == this->iter_end_)
    {
      // destroy this object..
      this->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (rec_list);
    }

  return rec_list;
}

TAO_END_VERSIONED_NAMESPACE_DECL
