text:
using namespace std;

//#define DIAGNOSTIC_OUTPUT
//#undef DIAGNOSTIC_OUTPUT

namespace
{
  //
  // The default number of records allocated in a
  // member record buffer during a transaction.
  //
  const int DEFAULT_MEMBER_RECORD_BUFFER_UB = 1000;

  //
  // OBSOLETE: no longer used. The default number of bytes in a dof_tuple record.
  //
  const size_t DEFAULT_DOF_TUPLE_RECORD_SIZE = 1024;

  //
  // The default number of records allocated in a
  // dof_tuple record buffer during a transaction.
  //
  const int DEFAULT_DOF_TUPLE_RECORD_BUFFER_UB = 4;
}

// PUBLIC MEMBER FUNCTIONS

// CANONICAL MEMBERS

///
sheaf::storage_agent::
storage_agent()
{

  // Preconditions:

  // Body:

  _member_record_buffer_ub    = DEFAULT_MEMBER_RECORD_BUFFER_UB;
  _dof_tuple_record_size      = DEFAULT_DOF_TUPLE_RECORD_SIZE;
  _dof_tuple_record_buffer_ub = DEFAULT_DOF_TUPLE_RECORD_BUFFER_UB;

  // Reset the start time.

  reset_time();

  // Postconditions:

  ensure(invariant());
  ensure(transaction_ct() == 0);
  ensure(member_record_buffer_ub() == DEFAULT_MEMBER_RECORD_BUFFER_UB);
  ensure(dof_tuple_record_size() == DEFAULT_DOF_TUPLE_RECORD_SIZE);
  ensure(dof_tuple_record_buffer_ub() == DEFAULT_DOF_TUPLE_RECORD_BUFFER_UB);

  // Postconditions:

  ensure(invariant());

  // Exit:

  return;
}

///
sheaf::storage_agent::
storage_agent(const storage_agent& xother)
{

  // Preconditions:

  // Body:

  not_implemented();

  // postconditions:

  ensure(invariant());
}

///
sheaf::storage_agent*
sheaf::storage_agent::
clone() const
{
  storage_agent* result =0;
  // Initialized only to silence compiler warnings
  // until implemented.

  // Preconditions:

  // Body:

  not_implemented();

  // Postconditions:

  ensure(result != 0);
  ensure(is_same_type(result));

  // Exit:

  return result;
}

///
sheaf::storage_agent::
~storage_agent()
{

  // Preconditions:

  // Body:

  // Close the file if it is open

  if(_file.is_open())
  {
    _file.close();
  }

  // Postconditions:

  // Exit:

  return;
}

///
bool
sheaf::storage_agent::
invariant() const
{
  bool result = true;

  // Preconditions:

  // Body:

  ///@todo finish storage_agent::invariant

  // Must satisfy base class invariant

  result = result && any::invariant();

  if(invariant_check())
  {
    // Prevent recursive calls to invariant

    disable_invariant_check();

    // Finished, turn invariant checking back on.

    enable_invariant_check();
  }

  // Postconditions:

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
is_ancestor_of(const any* other) const
{

  // Preconditions:

  require(other != 0);

  // Body:

  // True if other conforms to this

  bool result = dynamic_cast<const storage_agent*>(other) != 0;

  // Postconditions:

  return result;

}

// STORAGE_AGENT INTERFACE

///
sheaf::storage_agent::
storage_agent(const std::string& xfile_name,
              sheaf_file::access_mode xmode,
              bool xclobber,
              bool xenable_error_report)
{

  // Preconditions:

  require(!xfile_name.empty());
  require(xfile_name.substr(xfile_name.size() - 4) == ".hdf");

  // Body:

  _member_record_buffer_ub    = DEFAULT_MEMBER_RECORD_BUFFER_UB;
  _dof_tuple_record_size      = DEFAULT_DOF_TUPLE_RECORD_SIZE;
  _dof_tuple_record_buffer_ub = DEFAULT_DOF_TUPLE_RECORD_BUFFER_UB;

  if(xenable_error_report)
  {
    // Turn on the standard HDF error reporting.

    H5Eset_auto1(reinterpret_cast<herr_t (*)(void*)>(H5Eprint), stderr);
  }
  else
  {
    // Turn off HDF error reporting.

    H5Eset_auto1(NULL, NULL);
  }

  // Open the file.

  _file.open(xfile_name, xmode, xclobber);

  // Set the file id space name.

  string::size_type lpos = xfile_name.find_last_of('/');
  lpos = (lpos == string::npos) ? 0 : lpos+1;
  string lname = xfile_name.substr(lpos);

  _file_id_space_name = poset_path::reserved_prefix() + lname;

  // Reset the start time.

  reset_time();

  // Postconditions:

  ensure(invariant());
  ensure(transaction_ct() == 0);
  ensure(member_record_buffer_ub() == DEFAULT_MEMBER_RECORD_BUFFER_UB);
  ensure(dof_tuple_record_size()   == DEFAULT_DOF_TUPLE_RECORD_SIZE);
  ensure(dof_tuple_record_buffer_ub() == DEFAULT_DOF_TUPLE_RECORD_BUFFER_UB);
  ensure(file().is_open());
}

sheaf::sheaf_file&
sheaf::storage_agent::
file()
{
  return _file;
}

///
const sheaf::sheaf_file&
sheaf::storage_agent::
file() const
{
  return _file;
}

///
bool
sheaf::storage_agent::
file_is_read_accessible() const
{
  bool result;

  // Preconditions:

  // Body:

  result = file().is_open();

  // Postconditions:

  ensure(result == file().is_open());

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
file_is_read_only_accessible() const
{
  bool result;

  // Preconditions:

  // Body:

  result = (file().mode() == sheaf_file::READ_ONLY);

  // Postconditions:

  ensure(result == (file().mode() == sheaf_file::READ_ONLY));

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
file_is_read_write_accessible() const
{
  bool result;

  // Preconditions:

  // Body:

  result = (file().mode() == sheaf_file::READ_WRITE);

  // Postconditions:

  ensure(result == (file().mode() == sheaf_file::READ_WRITE));

  // Exit

  return result;
}

const std::string&
sheaf::storage_agent::
file_id_space_name() const
{
  return _file_id_space_name;
}

void
sheaf::storage_agent::
reset_time()
{
  // Preconditions:

  // Body:

  _start_time = clock();

  // Postconditions:

  // Exit:
}

///
clock_t
sheaf::storage_agent::
time() const
{
  // Preconditions:

  // Body:

  clock_t result = clock() - _start_time;

  // Postconditions:

  // Exit:

  return result;
}

// CONSTANTS FOR ACCESS ROUTINE ARGUMENTS

const bool sheaf::storage_agent::COMMIT = true;
const bool sheaf::storage_agent::ABORT = false;
const bool sheaf::storage_agent::RETAIN_READ = true;
const bool sheaf::storage_agent::RELEASE_ALL = false;

// ===========================================================
// NAMESPACE_POSET FACET
// ===========================================================

///
void
sheaf::storage_agent::
read_entire(namespace_poset& xns)
{

  // Preconditions:

  require(xns.state_is_read_write_accessible());
  require(!transaction_is_active(xns));

  // Body:

  define_old_variable(int old_access_request_depth = xns.access_request_depth());

  // Read the namespace itself

  read(xns);

  // Read all the posets in it that have not already been read.

  read_remainder(xns);

  // Postconditions:

  ensure(!transaction_is_active(xns));
  ensure(xns.access_request_depth() == old_access_request_depth);
  ensure(unexecutable("for all members m of xns: !m.is_external()"));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
read_remainder(namespace_poset& xns)
{

  // Preconditions:

  require(xns.state_is_read_write_accessible());
  require(!transaction_is_active(xns));

  // Body:

  define_old_variable(int old_access_request_depth = xns.access_request_depth());

  // Read all the posets in the namespace that have not already been read.

  zn_to_bool_postorder_itr itr(xns.top(), "jims", DOWN, NOT_STRICT);
  while(!itr.is_done())
  {

#ifdef DIAGNOSTIC_OUTPUT
    cout << "testing is_external for poset " << xns.member_name(itr.index()) << endl;
#endif

    poset_state_handle& p = xns.member_poset(itr.index());
    if(p.is_external())
    {
      read(p);
    }
    itr.next();
  }

  // Postconditions:

  ensure(!transaction_is_active(xns));
  ensure(xns.access_request_depth() == old_access_request_depth);
  ensure(unexecutable("for all members m of xns: !m.is_external()"));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
read(namespace_poset& xns)
{

  // Preconditions:

  require(xns.state_is_read_write_accessible());
  require(!transaction_is_active(xns));

  // Body:

  define_old_variable(int old_access_request_depth = xns.access_request_depth());

  begin_read_transaction(xns);
  end_transaction(xns, ABORT);

#ifdef DIAGNOSTIC_OUTPUT
  cout << static_cast<poset_state_handle&>(xns) << endl;
#endif

  // Postconditions:

  ensure(!transaction_is_active(xns));
  ensure(xns.access_request_depth() == old_access_request_depth);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write(namespace_poset& xns)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xns.is_attached());
  require(xns.state_is_read_write_accessible());
  require(!xns.index().is_valid());
  require(!transaction_is_active(xns));

  // Body:

  initialize_namespace_id_spaces_for_write(xns);

  // Begin the transaction.

  begin_write_transaction(xns);

  // Commit and end the transaction

  end_transaction(xns, COMMIT);

  // Postconditions:

  ensure(state_is_consistent(xns));
  ensure(!transaction_is_active(xns));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write_entire(namespace_poset& xns)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xns.is_attached());
  require(xns.state_is_read_write_accessible());
  require(!transaction_is_active(xns));
  require(unexecutable("for all members m of xns( !transaction_is_active(m))"));

  // Body:

  // Write the name space itself

  write(xns);

  // Write all the posets in it.

  write_remainder(xns);

  // Postconditions:

  ensure(state_is_consistent(xns));
  ensure(!transaction_is_active(xns));
  ensure(unexecutable("for all members m of xns(state_is_consistent(m))"));
  ensure(unexecutable("for all members m of xns( !transaction_is_active(m))"));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write_remainder(const namespace_poset& xns)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xns.is_attached());
  require(xns.state_is_read_accessible());

  /// @todo make the following executable

  require(unexecutable("xns itself has already been written"));
  require(unexecutable("for all members p of xns(!p.is_external())"));
  require(!transaction_is_active(xns));
  require(unexecutable("for all members p of xns(!transaction_is_active(p))"));

  // Body:

  // Write all the posets in the namespace that
  // have not been written since the last time hey were changed.

  zn_to_bool_postorder_itr itr(xns.top(), "jims", DOWN, NOT_STRICT);
  while(!itr.is_done())
  {
    poset_state_handle& p = xns.member_poset(itr.index());

    assertion(!p.is_external());

    p.get_read_access();
    if(!state_is_consistent(p))
    {
      write(p);
    }
    p.release_access();

    itr.next();
  }

  // Postconditions:

  ensure(state_is_consistent(xns));
  ensure(!transaction_is_active(xns));
  ensure(unexecutable("for all members m of xns(state_is_consistent(m))"));
  ensure(unexecutable("for all members m of xns(!transaction_is_active(m))"));

  // Exit

  return;
}

// ===========================================================
// GENERAL POSET FACET
// ===========================================================

///
void
sheaf::storage_agent::
read(poset_state_handle& xposet,
     const poset_bounds_descriptor& xrow_bounds,
     const poset_bounds_descriptor& xcol_bounds)
{

  // Preconditions:

  /// require(xposet.is_external()); /// @todo relax this requirement
  require(!transaction_is_active(xposet));

  // Body:

  int old_access_request_depth = xposet.is_external() ? 0 : xposet.access_request_depth();

  begin_read_transaction(xposet, xrow_bounds, xcol_bounds);
  end_transaction(xposet, ABORT);

  // Postconditions:

  ensure(!xposet.is_external());
  ensure(xposet.access_request_depth() == old_access_request_depth);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
read_toc(poset_state_handle& xposet)
{

  // Preconditions:

  require(xposet.is_external());
  require(!transaction_is_active(xposet));

  // Body:

  poset_bounds_descriptor lrow_bounds(poset_bounds_descriptor::SUBPOSET_MEMBER,
                                      TOC_INDEX,
                                      TOP_INDEX);

  read(xposet, lrow_bounds);

  // Postconditions:

  ensure(!xposet.is_external());
  ensure(xposet.access_request_depth() == 0);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
read_index(poset_state_handle& xposet)
{

  // Preconditions:

  require(xposet.is_external());
  require(!transaction_is_active(xposet));

  // Body:

  read(xposet,
       poset_bounds_descriptor::BOTTOM_TOP,
       poset_bounds_descriptor::BOTTOM_BOTTOM);

  // Postconditions:

  ensure(!xposet.is_external());
  ensure(xposet.access_request_depth() == 0);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
read_row_decomposition(poset_state_handle& xposet,
		       const scoped_index& xdecomp_id)
{

  // Preconditions:

  /// @todo make the following executable.
  /// @issue may want to introduce a state machine to coordinate
  /// multiple partial read/write transactions.

  require(unexecutable("Have already read the table of contents"));
  require(!transaction_is_active(xposet));

  // Body:

  int old_access_request_depth = xposet.is_external() ? 0 : xposet.access_request_depth();

  poset_bounds_descriptor lrow_bounds(poset_bounds_descriptor::MEMBER_SUBPOSET,
                                      BOTTOM_INDEX,
                                      xdecomp_id.hub_pod());

  read(xposet, lrow_bounds);

  // Postconditions:

  ensure(!xposet.is_external());
  ensure(xposet.access_request_depth() == old_access_request_depth);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write(poset_state_handle& xposet,
      const poset_bounds_descriptor& xrow_bounds,
      const poset_bounds_descriptor& xcol_bounds,
      bool xretain_read_access)
{

#ifdef DIAGNOSTIC_OUTPUT
  cout << endl << SOURCE_CODE_LOCATION;
  cout << "writing poset " << xposet.name() << " index: " << xposet.index() << endl;
#endif

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xposet.is_attached());
  require(xposet.state_is_read_accessible());
  require(xposet.index().is_valid());
  require(!transaction_is_active(xposet));
  require(xcol_bounds.ub_member_is_not_top() ?
          xposet.schema().ge(xcol_bounds.ub_id()) :
          true);

  /// @hack the product subposet hack, version 2.
  /// See poset_scaffold::dof_tuple_schema_int_id
  /// for the origin of the following requirement.

  require(unexecutable("if xposet is a section space, col bound is base space decomposition or same fiber as xposet"));

  // Body:

  // Begin the transaction.

  begin_write_transaction(xposet, xrow_bounds, xcol_bounds);

  if(xretain_read_access)
  {
    // Commit the transaction.

    commit_transaction(xposet);

    // Release write access but not read access.

    active_transaction(xposet).is_write_transaction = false;
  }
  else
  {
    // Commit and end the transaction

    end_transaction(xposet, COMMIT);
  }

  // Postconditions:

  ensure(state_is_consistent(xposet));
  ensure(!write_transaction_is_active(xposet));
  ensure(read_transaction_is_active(xposet) == xretain_read_access);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write_toc(poset_state_handle& xposet, const scoped_index& xtoc_id)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xposet.is_attached());
  require(xposet.state_is_read_accessible());
  require(!transaction_is_active(xposet));
  require(xposet.includes_subposet(xtoc_id));

  // Body:

  poset_bounds_descriptor lrow_bounds(poset_bounds_descriptor::SUBPOSET_MEMBER,
                                      xtoc_id.hub_pod(),
                                      TOP_INDEX);

  write(xposet, lrow_bounds);

  // Postconditions:

  ensure(state_is_consistent(xposet));
  ensure(!transaction_is_active(xposet));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write_row_decomposition(poset_state_handle& xposet,
			const scoped_index& xdecomp_id)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xposet.is_attached());
  require(xposet.state_is_read_accessible());
  require(!transaction_is_active(xposet));
  require(xposet.includes_subposet(xdecomp_id));

  // Body:

  poset_bounds_descriptor lrow_bounds(poset_bounds_descriptor::MEMBER_SUBPOSET,
                                      BOTTOM_INDEX,
                                      xdecomp_id.hub_pod());

  write(xposet, lrow_bounds);

  // Postconditions:

  ensure(state_is_consistent(xposet));
  ensure(!transaction_is_active(xposet));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write_col_decomposition(poset_state_handle& xposet,
			const scoped_index& xrow_id,
                        const scoped_index& xdecomp_id)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xposet.is_attached());
  require(xposet.state_is_read_accessible());
  require(!transaction_is_active(xposet));

  /// @hack the product subposet hack.
  /// Following precondition can not be enforced because
  /// xdecomp_id may be subposet of base space, not schema itself
  ///
  /// require(xposet.schema().host()->includes_subposet(xdecomp_id));

  // Body:

  poset_bounds_descriptor lrow_bounds(poset_bounds_descriptor::MEMBER_MEMBER,
                                      xrow_id,
                                      xrow_id);

  poset_bounds_descriptor lcol_bounds(poset_bounds_descriptor::MEMBER_SUBPOSET,
                                      BOTTOM_INDEX,
                                      xdecomp_id.hub_pod());

  write(xposet, lrow_bounds, lcol_bounds);

  // Postconditions:

  // ensure(state_is_consistent(xposet));
  ensure(!transaction_is_active(xposet));

  // Exit

  return;
}

// ===========================================================
// DOF_TUPLE FACET
// ===========================================================

///
void
sheaf::storage_agent::
write_dof_tuple(const abstract_poset_member& xmbr,
                const scoped_index& xcol_ub_id,
                const scoped_index& xdecomp_id)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xmbr.state_is_read_accessible());
  require(!transaction_is_active(*(xmbr.host())));

  /// @hack the product subposet hack.
  /// Following precondition can not be enforced because
  /// xdecomp_id may be subposet of base space, not schema itself
  ///
  /// require(xposet.schema().host()->includes_subposet(xdecomp_id));

  // Body:

  poset_bounds_descriptor lrow_bounds(poset_bounds_descriptor::MEMBER_MEMBER,
                                      xmbr.index(),
                                      xmbr.index());

  poset_bounds_descriptor lcol_bounds(poset_bounds_descriptor::MEMBER_SUBPOSET,
                                      BOTTOM_INDEX,
                                      xdecomp_id.hub_pod());

  begin_write_transaction(*(xmbr.host()), lrow_bounds, lcol_bounds);

  commit_dof_tuple_transaction(xmbr, xcol_ub_id, xdecomp_id);

  remove_active_transaction(*(xmbr.host()));

  // Postconditions:

  // ensure(state_is_consistent(xposet));
  ensure(!transaction_is_active(*(xmbr.host())));

  // Exit

  return;
}

void
sheaf::storage_agent::
commit_dof_tuple_transaction(const abstract_poset_member& xmbr,
                             const scoped_index& xcol_ub_id,
                             const scoped_index& xdecomp_id)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(write_transaction_is_active(*(xmbr.host())));
  require(xmbr.state_is_read_write_accessible());

  // Body:

  poset_state_handle& lposet = *(xmbr.host());

  // Get the transaction description.

  const transaction& ltrans = active_transaction(lposet);

  // Initialize the member and dof tuple index spaces.
  // The index spaces maintain the association between
  // internal and external index spaces.

  initialize_poset_id_spaces_for_write(lposet);

  // Create the poset scaffold.
  // The scaffold buffers various data from when it
  // is computed until when it is used or written.

  poset_scaffold lscaffold(lposet,
                           ltrans,
                           file().type_map(),
                           _file_id_space_name,
                           true);

  // Write the posets on which this poset depends.

  write_prerequisites(lscaffold);

  // Write this dof tuple.

  // Open the dof tuple record set.

  dof_tuple_record_set ldof_tuple_records(file(), dof_tuple_record_size(), dof_tuple_record_buffer_ub(), lscaffold);
  ldof_tuple_records.open();

  lscaffold.evaluate_toc_alias();

  scoped_index lint_dof_tuple_id;
  xmbr.dof_tuple_id(lint_dof_tuple_id, false);

  // Get the external dof tuple id, if one has been assigned.

  scoped_index lext_dof_tuple_id =
    lscaffold.dof_tuple_ext_id(lint_dof_tuple_id);

  if(!lext_dof_tuple_id.is_valid())
  {
    // This is the first time we've encountered this dof tuple;
    // get an external id for it. External id will be the offset of
    // the beginning of the tuple in the dataset.

    lext_dof_tuple_id = ldof_tuple_records.compute_ext_id(lint_dof_tuple_id);

    // Save the dof tuple type and class name to be written in the
    // respective map; map must not already contain an entry for lext_dof_tuple_id.

    assertion(lscaffold.dof_tuple_types().find(lext_dof_tuple_id.pod()) ==
           lscaffold.dof_tuple_types().end());

    dof_tuple_type ltype_id = xmbr.dof_map().type_id();
    pair<pod_index_type, dof_tuple_type>
      lmap_pair(lext_dof_tuple_id.pod(), ltype_id);

    lscaffold.dof_tuple_types().insert(lmap_pair);

    assertion(lscaffold.dof_tuple_class_names().find(lext_dof_tuple_id.pod()) ==
           lscaffold.dof_tuple_class_names().end());

    string lclass_name = xmbr.dof_map().class_name();
    pair<pod_index_type, string> lname_map_pair(lext_dof_tuple_id.pod(), lclass_name);

    lscaffold.dof_tuple_class_names().insert(lname_map_pair);
  }

  ldof_tuple_records.externalize_domain(lext_dof_tuple_id, xcol_ub_id);

  // Close the record set.

  ldof_tuple_records.close();

  // Postconditions:

  ensure(write_transaction_is_active(lposet));

  // Exit

  return;
}

// ===========================================================
// TRANSACTION FACET
// ===========================================================

///
void
sheaf::storage_agent::
begin_read_transaction(namespace_poset& xns)
{

  // Preconditions:

  require(xns.state_is_read_write_accessible());
  require(!transaction_is_active(xns));

  // Body:

#ifdef DIAGNOSTIC_OUTPUT
  cout << endl << SOURCE_CODE_LOCATION;
  cout << "reading poset " << xns.name() << endl;
#endif

  define_old_variable(int old_access_request_depth = xns.access_request_depth());

  // Want to read all the rows and whatever columns are in the file.
  // Enter xns in the set of active read transactions

  transaction ltrans(READ, xns);
  put_active_transaction(ltrans);

  // Clear the namespace, except for the standard members
  // bottom, top, primitives, primitives schema, and primitive definitions members.
  // Also deletes all non-standard id spaces.

  xns.clear();

  // Initialize index spaces for members, subposets, and dof tuples
  // in name space and its standard posets.

  initialize_namespace_id_spaces_for_read(xns);

  // Create the poset scaffold.
  // The scaffold buffers various data from when it
  // is read until it can be used to build the poset.

  poset_scaffold lscaffold(xns,
                           ltrans,
                           file().type_map(),
                           _file_id_space_name,
                           false);

  // Read this poset

  // Open and read the general attributes record set.
  // Gets the external schema which is needed to instantiate
  // the dof tuple record set.

  attributes_record_set lattributes_records(file(), lscaffold);
  lattributes_records.open();
  lattributes_records.internalize();

  // The poset and its subposets have been created;
  // now we can attach the bounds objects.

  lscaffold.evaluate_toc_alias();

  // Open the other record sets.

  dof_tuple_record_set ldof_tuple_records(file(), dof_tuple_record_size(), dof_tuple_record_buffer_ub(), lscaffold);
  ldof_tuple_records.open();

  member_record_set lmember_records(file(), member_record_buffer_ub(), lscaffold, ldof_tuple_records);
  lmember_records.open();

  // Read and convert the other record sets to internal form.

  lmember_records.internalize(lscaffold.row_bounds().ub_id());
  ldof_tuple_records.internalize();

  // Close the record sets.

  lmember_records.close();
  ldof_tuple_records.close();
  lattributes_records.close();

  // Tranfser member names from scaffold to poset.
  // Have to do this after member_names_record in attribute record set
  // has been internalized and after all members have been internalized.

  lscaffold.put_member_names_to_poset();

  //
  // Reset the namespace name from whatever the client
  // chose initially to the name in the file.
  //
  string lns_name = lattributes_records.poset_name();
  xns.put_name(lns_name);

  // The non-standard dof tuples all have void pointers in them.
  // Allocate external handles instead.

  namespace_poset_member lns_mbr(xns.top());
  zn_to_bool_postorder_itr itr(lns_mbr, DOWN, NOT_STRICT);
  while(!itr.is_done())
  {
    lns_mbr.attach_to_state(&xns, itr.index());

    if(lns_mbr.is_jim() && lns_mbr.index().pod() >= xns.standard_member_ct())
    {
      // The current member is not a standard member;
      // create an external handle for it.
      // Handle pointer is inserted in namespace dof tuple by
      // attach_to_state.
      // Do not delete the handle!

      poset_state_handle* lmbr_poset =
        poset_state_handle::new_poset_handle(lns_mbr.poset_class(), lns_mbr.poset_type_id());

      lmbr_poset->attach_to_state(&lns_mbr);

      /// @hack all the non-standard members are restricted to
      /// schema member io; see member_record::new_row_dof_tuple.
      /// Patch this up by extending the schema to top.

      lns_mbr.dof_map().extend_to_top();
    }

    itr.next();
  }

  lns_mbr.detach_from_state();

  // Postconditions:

  ensure(read_transaction_is_active(xns));
  ensure(xns.access_request_depth() == old_access_request_depth);

  // Exit

  return;
}

///
void
sheaf::storage_agent::
begin_read_transaction(poset_state_handle& xposet,
                       const poset_bounds_descriptor& xrow_bounds,
                       const poset_bounds_descriptor& xcol_bounds)
{

  // Preconditions:

#ifdef DIAGNOSTIC_OUTPUT
  cout << endl << SOURCE_CODE_LOCATION;
  cout << "reading poset: " << xposet.name()  << " index: " << xposet.index() << endl;
#endif

  require(!transaction_is_active(xposet));

  // Body:

  int old_access_request_depth =
    xposet.is_external() ? 0 : xposet.access_request_depth();

  // Enter xposet in the set of active read transactions

  transaction ltrans(READ, xposet, xrow_bounds, xcol_bounds);
  put_active_transaction(ltrans);

  // Read all the posets this poset depends on.

  read_prerequisites(xposet, xcol_bounds);

  // Read this poset:

  // Deal with the "pointer bug":
  // Make sure we use the handle that is held by the namespace for all operations.
  // Ensures that states, members, and subposets don't hold pointers to
  // handles that will later disappear.

  poset_state_handle& lposet = xposet.name_space()->member_poset(xposet.index());

  // Create the poset scaffold.
  // The scaffold buffers various data from when it
  // is read until it can be used to build the poset.

  poset_scaffold lscaffold(lposet,
                           ltrans,
                           file().type_map(),
                           _file_id_space_name,
                           false);

  // Open and read the general attributes record set.
  // Gets the external schema and instantiates the poset state;
  // see poset_general_record::transfer_internal_buffer_to_poset.

#ifdef DIAGNOSTIC_OUTPUT

  cout << endl << SOURCE_CODE_LOCATION;
  cout << "opening record sets for poset " << lposet.name() << endl;
#endif

  attributes_record_set lattributes_records(file(), lscaffold);
  lattributes_records.open();
  lattributes_records.internalize();

  // The poset and its subposets have been created;
  // now we can attach the bounds objects.

  lscaffold.evaluate_toc_alias();

  // Open the other record sets.

  dof_tuple_record_set ldof_tuple_records(file(),
                                          dof_tuple_record_size(),
                                          dof_tuple_record_buffer_ub(),
                                          lscaffold);
  ldof_tuple_records.open();

  member_record_set lmember_records(file(),
                                    member_record_buffer_ub(),
                                    lscaffold,
                                    ldof_tuple_records);
  lmember_records.open();

  // Read and convert the other record sets to internal form.

  index_iterator* lrow_itr = lposet.bound_iterator(lscaffold.row_bounds(), true);

  while(!lrow_itr->is_done())
  {
    lmember_records.internalize(lrow_itr->index());

    ldof_tuple_records.internalize();
    lrow_itr->next();
  }
  delete lrow_itr;

  // Finished with the bounds; detach them.

  // Close the record sets.

  lmember_records.close();
  ldof_tuple_records.close();
  lattributes_records.close();

#ifdef DIAGNOSTIC_OUTPUT

  cout << endl << SOURCE_CODE_LOCATION;
  cout << "closed record sets for poset " << lposet.name() << endl;
#endif

  // Tranfser member names from scaffold to poset.
  // Have to do this after member_names_record in attribute record set
  // has been internalized and after all members have been internalized.

  lscaffold.put_member_names_to_poset();

  // Make sure client handle is attached to
  // new state created by read.

  if(xposet.is_external())
  {
    xposet.attach_to_state(&lposet);
  }

  // Release any access acquired by the read process.
  /// @todo this will have to be changed when reads supported for !is_external.
  /// @error should release access to prerequisites acquired in read_prerequisites.

  while(lposet.access_request_depth() > old_access_request_depth)
  {
    lposet.release_access();
  }

#ifdef DIAGNOSTIC_OUTPUT
  cout << lposet << endl;
#endif

  // Postconditions:

  ensure(!xposet.is_external());
  ensure(xposet.access_request_depth() == old_access_request_depth);
  ensure(read_transaction_is_active(xposet));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
begin_read_write_transaction(poset_state_handle& xposet,
                             const poset_bounds_descriptor& xrow_bounds,
                             const poset_bounds_descriptor& xcol_bounds)
{

  // Preconditions:

  require(!transaction_is_active(xposet));

  // Body:

  begin_read_transaction(xposet, xrow_bounds, xcol_bounds);

  // Mark transaction as write transaction;

  //   // have to remove, modify, and reinsert the transaction.

  //   transaction ltrans(active_transaction(xposet));
  //   remove_active_transaction(xposet);
  //   ltrans.is_write_transaction = true;
  //   put_active_transaction(ltrans);

  active_transaction(xposet).is_write_transaction = true;

  // Postconditions:

  ensure(read_transaction_is_active(xposet));
  ensure(write_transaction_is_active(xposet));

  // Exit

  return;
}

void
sheaf::storage_agent::
begin_write_transaction(namespace_poset& xns)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xns.state_is_read_write_accessible());
  require(!transaction_is_active(xns));

  // Body:

  // Enter xns in the set of active write transactions

  poset_bounds_descriptor lcol_bounds(poset_bounds_descriptor::MEMBER_MEMBER,
                                      BOTTOM_INDEX,
                                      IO_INDEX);

  transaction ltrans(WRITE, xns, poset_bounds_descriptor::BOTTOM_TOP, lcol_bounds);
  put_active_transaction(ltrans);

  // Postconditions:

  ensure(write_transaction_is_active(xns));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
begin_write_transaction(const poset_state_handle& xposet,
                        const poset_bounds_descriptor& xrow_bounds,
                        const poset_bounds_descriptor& xcol_bounds)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(xposet.is_attached());
  require(xposet.state_is_read_accessible());
  require(!transaction_is_active(xposet));
  require(xcol_bounds.ub_member_is_not_top() ?
          xposet.schema().ge(xcol_bounds.ub_id()) :
          true);

  // Body:

  // Enter xposet in the set of active write transactions

  transaction ltrans(WRITE, xposet, xrow_bounds, xcol_bounds);
  put_active_transaction(ltrans);

  // Postconditions:

  ensure(write_transaction_is_active(xposet));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
commit_transaction(poset_state_handle& xposet)
{

  // Preconditions:

  require(file_is_read_write_accessible());
  require(write_transaction_is_active(xposet));

  /// @issue Should really be read-write accessible so that the new
  ///        id space functions can require write access.  However,
  ///        writing only requires read access in general.

  require(xposet.state_is_read_accessible());

  // Body:

  // Get the transaction description.

  const transaction& ltrans = active_transaction(xposet);

  // Initialize the member and dof tuple index spaces.
  // The index spaces maintain the association between
  // internal and external index spaces.

  initialize_poset_id_spaces_for_write(xposet);

  // Create the poset scaffold.
  // The scaffold buffers various data from when it
  // is computed until when it is used or written.

  poset_scaffold lscaffold(xposet,
                           ltrans,
                           file().type_map(),
                           _file_id_space_name,
                           true);

  // Write the posets on which this poset depends.

  write_prerequisites(lscaffold);

  // Write this poset

#ifdef DIAGNOSTIC_OUTPUT
  cout << endl << SOURCE_CODE_LOCATION;
  cout << "opening record sets for poset " << xposet.name() << endl;
#endif

  // Transfeer member names from poset to scaffold.
  // Have to do this before attributes records are externalized,
  // but now is a good a time as any.

  lscaffold.get_member_names_from_poset();

  // Open all the record sets.

  dof_tuple_record_set ldof_tuple_records(file(),
                                          dof_tuple_record_size(),
                                          dof_tuple_record_buffer_ub(),
                                          lscaffold);
  ldof_tuple_records.open();

  member_record_set lmember_records(file(),
                                    member_record_buffer_ub(),
                                    lscaffold,
                                    ldof_tuple_records);
  lmember_records.open();
  //  lmember_records.initialize_external_ids();

  attributes_record_set lattributes_records(file(), lscaffold);
  lattributes_records.open();

  // Write the record sets.

  lscaffold.evaluate_toc_alias();

  index_iterator* lrow_itr = xposet.bound_iterator(lscaffold.row_bounds(), true);

  while(!lrow_itr->is_done())
  {
    lmember_records.externalize(lrow_itr->index());

    ldof_tuple_records.externalize();

    lrow_itr->next();
  }
  delete lrow_itr;

  lattributes_records.externalize();

  // Close the record sets.

  lmember_records.close();
  ldof_tuple_records.close();
  lattributes_records.close();

#ifdef DIAGNOSTIC_OUTPUT
  cout << endl << SOURCE_CODE_LOCATION;
  cout << "closed record sets for poset " << xposet.name() << endl;
#endif

  // Now the state is consistent.

  put_state_is_consistent(xposet);

  // Postconditions:

  ensure(write_transaction_is_active(xposet));
  ensure(state_is_consistent(xposet));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
end_transaction(poset_state_handle& xposet, bool xcommit)
{

  // Preconditions:

  require(transaction_is_active(xposet));
  require(xcommit ? file_is_read_write_accessible() : true);
  require(xcommit ? write_transaction_is_active(xposet) : true);

  // Body:

  define_old_variable(int old_access_request_depth = xposet.access_request_depth());

  if(xcommit)
    commit_transaction(xposet);

  // Remove the xposet from the set of active transactions

  remove_active_transaction(xposet);

  // Postconditions:

  ensure(!transaction_is_active(xposet));
  ensure( xcommit ? state_is_consistent(xposet) : true );
  ensure(xposet.access_request_depth() == old_access_request_depth);

  // Exit

  return;
}

// ===========================================================
// BUFFER SIZE CONTROL FACET
// ===========================================================

///
int
sheaf::storage_agent::
member_record_buffer_ub() const
{
  return _member_record_buffer_ub;
}

///
void
sheaf::storage_agent::
put_member_record_buffer_ub(int xub)
{
  // Preconditions:

  require(xub > 0);

  // Body:

  _member_record_buffer_ub = xub;

  // Postconditions:

  ensure(member_record_buffer_ub() == xub);

  // Exit

  return;
}

///
size_t
sheaf::storage_agent::
dof_tuple_record_size() const
{
  return _dof_tuple_record_size;
}

///
void
sheaf::storage_agent::
put_dof_tuple_record_size(size_t xsize)
{
  // Preconditions:

  // Body:

  _dof_tuple_record_size = xsize;

  // Postconditions:

  ensure(dof_tuple_record_size() == xsize);

  // Exit

  return;
}

///
int
sheaf::storage_agent::
dof_tuple_record_buffer_ub() const
{
  return _dof_tuple_record_buffer_ub;
}

///
void
sheaf::storage_agent::
put_dof_tuple_record_buffer_ub(int xub)
{
  // Preconditions:

  require(xub > 0);

  // Body:

  _dof_tuple_record_buffer_ub = xub;

  // Postconditions:

  ensure(dof_tuple_record_buffer_ub() == xub);

  // Exit

  return;
}

// TRANSACTION STATUS FACET

///
int
sheaf::storage_agent::
transaction_ct() const
{
  int result;

  // Preconditions:

  // Body:

  result = _active_transactions.size();

  // Postconditions:

  ensure(result >= 0);

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
transaction_is_active(pod_index_type xindex) const
{
  bool result;

  // Preconditions:

  // Body:

  transaction ltrans(xindex);
  set<transaction>::const_iterator itr = _active_transactions.find(ltrans);

  result = itr != _active_transactions.end();

  // Postconditions:

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
transaction_is_active(const poset_state_handle& xposet) const
{
  bool result;

  // Preconditions:

  // Body:

  transaction ltrans(READ, xposet); // type doesn't matter here.
  set<transaction>::const_iterator itr = _active_transactions.find(ltrans);

  result = itr != _active_transactions.end();

  // Postconditions:

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
read_transaction_is_active(const poset_state_handle& xposet) const
{
  bool result;

  // Preconditions:

  // Body:

  transaction ltrans(READ, xposet); // type doesn't matter here.
  set<transaction>::const_iterator itr = _active_transactions.find(ltrans);

  result = (itr != _active_transactions.end()) && itr->is_read_transaction;

  // Postconditions:

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
write_transaction_is_active(const poset_state_handle& xposet) const
{
  bool result;

  // Preconditions:

  // Body:

  transaction ltrans(WRITE, xposet); // type doesn't actually matter here.
  set<transaction>::const_iterator itr = _active_transactions.find(ltrans);

  result = (itr != _active_transactions.end()) && itr->is_write_transaction;

  // Postconditions:

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
read_write_transaction_is_active(poset_state_handle& xposet) const
{
  bool result;

  // Preconditions:

  // Body:

  result = read_transaction_is_active(xposet) &&
           write_transaction_is_active(xposet);

  // Postconditions:

  // Exit

  return result;
}

///
bool
sheaf::storage_agent::
state_is_consistent(const poset_state_handle& xposet) const
{
  bool result;

  // Preconditions:

  require(xposet.is_attached() ? xposet.state_is_read_accessible() : true);

  // Body:

  // The definition of this condition is:
  // internal state exists and
  // external state exists and
  // internal hasn't been modified since external was written.

  // internal state exists = xposet.is_attached();
  // external state exists = member index space exists
  // not modified = !xposet.state_is_modified()
  //
  // Note that is_attached() is a precondition for evaluating the
  // other two conditions.

  /// @hack use of member index space as test for external state
  /// is a temporary implementation. Need more formal support for this test.
  /// In order for this hack to work, we clear the index spaces in
  /// write(namespace_poset&). Furthermore, since this routine is typically
  /// called before any attempt to write a poset, the call to member_id_space
  /// below actually creates the index space.
  /// @todo replace with some better mechanism.

  result = xposet.is_attached();

  result =
    result &&
    xposet.member_id_spaces(false).contains(_file_id_space_name) &&
    !xposet.member_id_spaces(false).is_empty(_file_id_space_name);

  result =
    result &&
    xposet.subposet_id_spaces(false).contains(_file_id_space_name) &&
    !xposet.subposet_id_spaces(false).is_empty(_file_id_space_name);

  result =
    result &&
    xposet.dof_tuple_id_spaces(false).contains(_file_id_space_name);

  result = result && !xposet.state_is_modified();

  // Postconditions:

  // Exit

  return result;
}

// PROTECTED MEMBER FUNCTIONS

void
sheaf::storage_agent::
put_state_is_consistent(poset_state_handle& xposet)
{
  // Preconditions:

  require(xposet.is_attached());

  // Body:

  xposet.clear_state_is_modified();

  // Postconditions:

  // Exit

  return;
}

// ROUTINES TO SUPPORT INDEPENDENT ACCESS

///
void
sheaf::storage_agent::
read_prerequisites(const poset_state_handle& xposet,
                   const poset_bounds_descriptor& xcol_bounds)
{

  // Preconditions:

  require(xposet.name_space()->state_is_read_write_accessible());

  // Body:

  namespace_poset* lns = xposet.name_space();

  // Read the posets on which this poset depends:
  // Get a handle to the name space member for xposet.

  namespace_poset_member lname_space_mbr(xposet.name_space(), xposet.index());

#ifdef DIAGNOSTIC_OUTPUT
    cout << "Inspecting prerequisites of poset " << lname_space_mbr.name() << endl;
#endif

  // Read  prerequisites.

  for(int i=0; i<PREREQ_IDS_UB; ++i)
  {
    pod_index_type lprereq_id = lname_space_mbr.poset_prereq_id(i);

#ifdef DIAGNOSTIC_OUTPUT
      cout << "\tprereq_id = " << lprereq_id << endl;
#endif

    if(is_valid(lprereq_id))
    {
      // Get the poset associated with the member; has to be non-null

      poset_state_handle& lposet = lns->member_poset(lprereq_id);

      if(lposet.is_external())
      {
        // Prerequisite has not been read in; read it.

        begin_read_transaction(lposet);
      }

      // We'll need read access to the
      // prerequisite to read the poset.
      // Released in table_dof_tuple_record::release_prerequisites()
      // after we've actually created the poset state.

      lposet.get_read_access();

#ifdef DIAGNOSTIC_OUTPUT
	cout << "prereq name: " << lposet.name() << endl;
#endif

      // lposet is handle from namespace - do not detach or delete it!
    }
  }

  // Clean up

  lname_space_mbr.detach_from_state();

  // Postconditions:

  ensure(unexecutable(all prerequisites have been internalized));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
write_prerequisites(const poset_scaffold& xscaffold)
{

  // Preconditions:

  require(!xscaffold.structure().is_external());
  require(xscaffold.name_space()->state_is_read_accessible());

  // Body:

  // Make sure the posets on which this poset depends have been written to the file:

  namespace_poset* lns = xscaffold.name_space();
  if(xscaffold.structure_is_namespace())
  {
    // This poset is the namespace.
    // It's prerequisite is the namespace schema poset.

    if(!state_is_consistent(lns->namespace_schema()))
    {
      write(lns->namespace_schema());
    }
  }
  else
  {
    // This poset is an ordinary poset.

    // Prerequisites are represented by the name space order.
    // Get a handle to the name space member for xposet.

    namespace_poset_member lname_space_mbr(lns, xscaffold.index());

#ifdef DIAGNOSTIC_OUTPUT
    cout << "Inspecting prerequisites of poset " << lname_space_mbr.name() << endl;
#endif

    for(int i=0; i<PREREQ_IDS_UB; ++i)
    {
      pod_index_type lprereq_id = lname_space_mbr.poset_prereq_id(i);

#ifdef DIAGNOSTIC_OUTPUT
      cout << "\tprereq_id = " << lprereq_id << endl;
#endif

      if(is_valid(lprereq_id))
      {
        // Get the poset associated with the member

        poset_state_handle& lposet = lns->member_poset(lprereq_id);

	lposet.get_read_access();

#ifdef DIAGNOSTIC_OUTPUT
	cout << "prereq name: " << lposet.name() << endl;
#endif

        if(!state_is_consistent(lposet))
        {
          // Prerequisite has been modified since last written to file; write it out.
          // Note that this writes the entire prerequisite poset out, not just some part
          // of it. In particular, it will write out an entire schema, not just the
          // down set of the schema member of the poset that triggered this write.

          write(lposet);

          /// @issue if we retain read access in the main write, do we need
          /// to retain read access to all the prerequisites?
        }

	lposet.release_access();

        // lposet is handle from namespace - do not detach or delete it!
      }
    }

    // Clean up

    lname_space_mbr.detach_from_state();
  }

  // Postconditions:

  /// @issue we can't actually ensure the following if we've released read access.

  ensure(unexecutable("for all prerequisites p: state_is_consistent(p)"));

  // Exit

  return;
}

void
sheaf::storage_agent::
initialize_poset_id_spaces_for_write(poset_state_handle& xposet)
{
  // Preconditions:

  /// @issue Should really be read-write accessible so that the new
  ///        id space functions can require write access.  However,
  ///        writing only requires read access in general.

  require(xposet.state_is_read_accessible());

  // Body:

  // Make sure the member id space exists.
  // Don't clear the id space or initialize scratch ids;
  // may need entries from previous partial read or write.

  if(!xposet.member_id_spaces(false).contains(_file_id_space_name))
  {
    // Doesn't exist; create it.

    interval_index_space_handle::new_space(xposet.member_id_spaces(false),
					   _file_id_space_name, false, false);
  }

  // Get a subposet index id space for this poset; create it if needed or
  // clear the id space; entries get reconstructed on each read or write.

  if(!xposet.subposet_id_spaces(false).contains(_file_id_space_name))
  {
    // Doesn't exist; create it.

    interval_index_space_handle::new_space(xposet.subposet_id_spaces(false),
					   _file_id_space_name, false, false);
  }
  else
  {
    scattered_insertion_index_space_handle& lid_space =
      xposet.subposet_id_spaces(false).
      get_id_space<scattered_insertion_index_space_handle>(_file_id_space_name);
    lid_space.clear();
    lid_space.release_id_space();
  }

  // Get a dof tuple index id space for this poset; create it if needed.
  // Don't clear the id space, may need entries from previous partial read or write.

  if(!xposet.dof_tuple_id_spaces(false).contains(_file_id_space_name))
  {
    // Doesn't exist; create it.

    interval_index_space_handle::new_space(xposet.dof_tuple_id_spaces(false),
					   _file_id_space_name, false, false);
  }

  // Postconditions:

  ensure(xposet.member_id_spaces(false).contains(file_id_space_name()));
  ensure(xposet.subposet_id_spaces(false).contains(file_id_space_name()));
  ensure(xposet.dof_tuple_id_spaces(false).contains(file_id_space_name()));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
clear_all_id_spaces(namespace_poset& xns)
{
  // Preconditions:

  require(xns.state_is_read_write_accessible());

  /// @error preconditions don't accurately reflect access control

  // Body:

  // Clear the member file id spaces for the name space
  // and every poset in it.

  scattered_insertion_index_space_handle lid_space;

  zn_to_bool_postorder_itr itr0(xns.top(), "jims", DOWN, NOT_STRICT);
  while(!itr0.is_done())
  {
    poset_state_handle& lmbr_poset = xns.member_poset(itr0.index());
    if(!lmbr_poset.is_external())
    {
      lmbr_poset.get_read_write_access(true);
      if(lmbr_poset.member_id_spaces(false).contains(_file_id_space_name))
      {
	lid_space.attach_to(lmbr_poset.member_id_spaces(false), _file_id_space_name);
	lid_space.clear();
      }

      poset_powerset_state& lmbr_powerset = lmbr_poset.powerset();
      if(lmbr_poset.subposet_id_spaces(false).contains(_file_id_space_name))
      {
	lid_space.attach_to(lmbr_poset.subposet_id_spaces(false), _file_id_space_name);
	lid_space.clear();
      }

      poset_table_state& lmbr_table = lmbr_poset.table();
      if(lmbr_poset.dof_tuple_id_spaces(false).contains(_file_id_space_name))
      {
	lid_space.attach_to(lmbr_poset.dof_tuple_id_spaces(false), _file_id_space_name);
	lid_space.clear();
      }

      lmbr_poset.release_access();
    }

    itr0.next();
  }

  if(xns.member_id_spaces(false).contains(_file_id_space_name))
  {
    lid_space.attach_to(xns.member_id_spaces(false), _file_id_space_name);
    lid_space.clear();
  }

  if(xns.subposet_id_spaces(false).contains(_file_id_space_name))
  {
    lid_space.attach_to(xns.subposet_id_spaces(false), _file_id_space_name);
    lid_space.clear();
  }

  if(xns.dof_tuple_id_spaces(false).contains(_file_id_space_name))
  {
    lid_space.attach_to(xns.dof_tuple_id_spaces(false), _file_id_space_name);
    lid_space.clear();
  }

  // Postconditions:

  // Exit:

  return;
}

///
void
sheaf::storage_agent::
initialize_namespace_id_spaces_for_read(namespace_poset& xns)
{
  // Preconditions:

  require(xns.state_is_read_write_accessible());

  // Body:

  // Clear all the index spaces.
  // Will clear all the file id spaces too, but there shouldn't be any
  // at this point.

  clear_all_id_spaces(xns);

  // Index spaces for the namespace poset itself will be
  // initialized when the namespace is read.

  // Initialize the subposet, dof_tuple and member index spaces
  // for the standard posets. These posets will not be read in
  // but the spaces need to exist for consistency.

  pod_index_type lfirst_member = member_record_set::first_member_record();

  interval_index_space_handle lid_space =
    interval_index_space_handle::new_space(xns.primitives().member_id_spaces(false),
					   _file_id_space_name, false, false);

  // then assign record ids sequentially.

  pod_index_type lprim_ct = xns.primitives().standard_member_ct();
  lid_space.insert_interval(lfirst_member, lfirst_member+lprim_ct, 0, lprim_ct);

  // Initialize primitives schema space.

  lid_space =
    interval_index_space_handle::new_space(xns.primitives_schema().member_id_spaces(false),
					   _file_id_space_name, false, false);

  // Reserve 0-th record for id space,
  // then assign record ids sequentially.

  pod_index_type lprim_schema_ct =
    xns.primitives_schema().standard_member_ct();
  lid_space.insert_interval(lfirst_member, lfirst_member+lprim_schema_ct,
			    0, lprim_schema_ct);

  // Initialize namespaces schema space.

  lid_space =
    interval_index_space_handle::new_space(xns.namespace_schema().member_id_spaces(false),
					   _file_id_space_name, false, false);

  // Reserve 0-th record for id space,
  // then assign record ids sequentially.

  pod_index_type lnamespace_schema_ct =
    xns.namespace_schema().standard_member_ct();
  lid_space.insert_interval(lfirst_member, lfirst_member+lnamespace_schema_ct,
			    0, lnamespace_schema_ct);

  // Postconditions:

  // Exit:

  return;
}

///
void
sheaf::storage_agent::
initialize_namespace_id_spaces_for_write(namespace_poset& xns)
{
  // Preconditions:

  require(xns.state_is_read_write_accessible());

  // Body:

  // Clear all the file id spaces.
  // Necessary because non-existent or empty file id space
  // used by state_is_consistent
  // to determine if external state exists.

  clear_all_id_spaces(xns);

  // The id spaces will be recreated and populated as needed
  // when the posets are written.

  // Postconditions:

  // Exit:

  return;
}

// PRIVATE MEMBER FUNCTIONS

///
/// The reserved, standard external name for the namespace in the file
///
const string _name_space_ext_name("__name_space");

///
bool
sheaf::storage_agent::transaction::
operator<(const transaction& xother) const
{
  return (target < xother.target);
}

///
sheaf::storage_agent::transaction::
~transaction()
{
  // Preconditions:

  // Body:

  // Postconditions:

  // Exit

  return;
}

///
sheaf::storage_agent::transaction::
transaction()
  : row_bounds(poset_bounds_descriptor::BOTTOM_TOP),
    col_bounds(poset_bounds_descriptor::BOTTOM_TOP)
{
  // Preconditions:

  // Body:

  target = invalid_pod_index();

  is_read_transaction  = false;
  is_write_transaction = false;

  // Postconditions:

  ensure(!is_valid(target));
  ensure(!is_read_transaction);
  ensure(!is_write_transaction);
  ensure(row_bounds == poset_bounds_descriptor::BOTTOM_TOP);
  ensure(col_bounds == poset_bounds_descriptor::BOTTOM_TOP);

  // Exit

  return;
}

///
sheaf::storage_agent::transaction::
transaction(pod_index_type xindex)
  : row_bounds(poset_bounds_descriptor::BOTTOM_TOP),
    col_bounds(poset_bounds_descriptor::BOTTOM_TOP)
{
  // Preconditions:

  // Body:

  target = xindex;
  is_read_transaction  = false;
  is_write_transaction = false;

  // Postconditions:

  ensure(target == xindex);
  ensure(!is_read_transaction);
  ensure(!is_write_transaction);
  ensure(row_bounds == poset_bounds_descriptor::BOTTOM_TOP);
  ensure(col_bounds == poset_bounds_descriptor::BOTTOM_TOP);

  // Exit

  return;
}

///
sheaf::storage_agent::transaction::
transaction(transaction_type xtype,
            const poset_state_handle& xposet,
            const poset_bounds_descriptor& xrow_bounds,
            const poset_bounds_descriptor& xcol_bounds)
    : row_bounds(xrow_bounds),
    col_bounds(xcol_bounds)
{
  // Preconditions:

  // Body:

  target = xposet.index().pod();
  switch(xtype)
  {
  case READ:
    is_read_transaction  = true;
    is_write_transaction = false;
    break;

  case WRITE:
    is_read_transaction  = false;
    is_write_transaction = true;
    break;

  case READ_WRITE:
    is_read_transaction  = false;
    is_write_transaction = true;
    break;
  }

  // Postconditions:

  ensure(target == xposet.index().pod());
  ensure(is_read_transaction  == ((xtype == READ) || (xtype == READ_WRITE)));
  ensure(is_write_transaction == ((xtype == WRITE) || (xtype == READ_WRITE)));
  ensure(row_bounds == xrow_bounds);
  ensure(col_bounds == xcol_bounds);

  // Exit

  return;
}

///
const sheaf::storage_agent::transaction&
sheaf::storage_agent::
active_transaction(const poset_state_handle& xposet)
{
  // Preconditions:

  require(transaction_is_active(xposet));

  // Body:

  transaction ltrans(READ, xposet); // type doesn't matter
  set<transaction>::iterator itr = _active_transactions.find(ltrans);

  const transaction& result = *itr;

  // Postconditions:

  // Exit

  return result;
}

///
void
sheaf::storage_agent::
put_active_transaction(transaction& xtransaction)
{
  // Preconditions:

  require(!transaction_is_active(xtransaction.target));

  // Body:

  _active_transactions.insert(xtransaction);

  // Postconditions:

  ensure(transaction_is_active(xtransaction.target));

  // Exit

  return;
}

///
void
sheaf::storage_agent::
remove_active_transaction(const poset_state_handle& xposet)
{
  transaction result;

  // Preconditions:

  // Body:

  transaction ltrans(READ, xposet); // Type doesn't matter.
  (void)_active_transactions.erase(ltrans);

  // Postconditions:

  ensure(!transaction_is_active(xposet));

  // Exit

  return;
}


text:
package com.tempoiq;

import org.junit.Test;

import java.net.InetSocketAddress;
import java.util.*;

import static org.junit.Assert.*;

public class PageLoaderTest {

  private class IntegerPageLoader extends PageLoader<Integer> {
    private int innerIndex;
    private Segment<Integer> onDeck;
    private List<Segment<Integer>> segments;

    public IntegerPageLoader(List<Segment<Integer>> segments) {
      super(segments.get(0));
      segments.remove(0);
      this.segments = segments;
      this.innerIndex = 0;
    }

    @Override
      public Segment<Integer> fetchNext() {
        if (innerIndex < segments.size()) {
          onDeck = segments.get(innerIndex);
          innerIndex ++;
        } else {
          onDeck = null;
        }
        return onDeck;
      }
  }

  private static Executor runner = new Executor(new Credentials("key", "secret"),
      new InetSocketAddress("some.backend.tempo-iq.com", 443), "https");

  @Test
    public void testNextLogic() {
      List<Segment<Integer>> segments = new ArrayList<Segment<Integer>>();
      for (Integer i = 0; i < 10; i++) {
        List<Integer> data = new ArrayList<Integer>();
        for (Integer j = 0; j < 10; j++) {
          data.add(j);
        }
        segments.add(new Segment<Integer>(data, String.format("%s", i)));
      }
      PageLoader<Integer> integerPageLoader = new IntegerPageLoader(segments);

      for (int k = 0; k < 10; k++) {
        assertNotNull(integerPageLoader.next());
      }
    }

  @Test
    public void testEmptiesGracefully() {
      List<Segment<Integer>> segments = new ArrayList<Segment<Integer>>();
      for (int i = 0; i < 10; i++) {
        List<Integer> data = new ArrayList<Integer>();
        for (Integer j = 0; j < 10; j++) {
          data.add(j);
        }
        segments.add(new Segment<Integer>(data, String.format("%s", i)));
      }
      PageLoader<Integer> integerPageLoader = new IntegerPageLoader(segments);

      for (int k = 0; k < 10; k++) {
        integerPageLoader.next();
      }
      assertFalse(integerPageLoader.hasNext());
    }

  @Test
    public void testHasNextLogic() {
      List<Segment<Integer>> segments = new ArrayList<Segment<Integer>>();
      for (Integer i = 0; i < 10; i++) {
        List<Integer> data = new ArrayList<Integer>();
        for (Integer j = 0; j < 10; j++) {
          data.add(j);
        }
        segments.add(new Segment<Integer>(data, String.format("%s", i)));
      }
      PageLoader<Integer> integerPageLoader = new IntegerPageLoader(segments);

      for (int k = 0; k < 10; k++) {
        assertTrue(integerPageLoader.hasNext());
        integerPageLoader.next();
      }
    }
}

redpajama_set_name: RedPajamaGithub


text:
package org.jboss.jube.local;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.jboss.jube.KubernetesModel;
import org.jboss.jube.apimaster.ApiMasterKubernetesModel;
import org.jboss.jube.process.Installation;
import org.jboss.jube.process.ProcessManager;
import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.jboss.jube.replicator.Replicator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Monitors the current local processes and updates the local model to indicate started or stopped processes
 */
@Singleton
public class ProcessMonitor {
    private static final transient Logger LOG = LoggerFactory.getLogger(Replicator.class);

    private final KubernetesModel model;
    private final ProcessManager processManager;
    private final long pollTime;
    private Timer timer = new Timer();

    @Inject
    public ProcessMonitor(ApiMasterKubernetesModel model,
                          ProcessManager processManager,
                          @ConfigProperty(name = "processMonitor_pollTime", defaultValue = "2000")
                          long pollTime) {
        this.model = model;
        this.processManager = processManager;
        this.pollTime = pollTime;

        System.out.println("Starting the process monitor with poll time: " + pollTime);

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                LOG.debug("process monitor timer");
                processMonitor();
            }
        };
        timer.schedule(timerTask, pollTime, pollTime);
    }

    protected void processMonitor() {
        ImmutableMap<String, Installation> map = processManager.listInstallationMap();
        ImmutableSet<Map.Entry<String, Installation>> entries = map.entrySet();
        ImmutableMap<String, PodCurrentContainer> podRunningContainers = model.getPodRunningContainers();

        for (Map.Entry<String, Installation> entry : entries) {
            String id = entry.getKey();
            Installation installation = entry.getValue();
            Long pid = null;
            try {
                pid = installation.getActivePid();
            } catch (IOException e) {
                LOG.warn("Failed to access pid for " + id + ". " + e, e);
            }
            boolean alive = pid != null && pid.longValue() > 0;

            PodCurrentContainer podCurrentContainer = podRunningContainers.get(id);
            if (podCurrentContainer == null) {
                System.out.println("No pod container for id: " + id + " so lets create one!");
            } else {
                // lets mark the container as running or not...
                podCurrentContainer.containerAlive(id, alive);
            }
        }
    }

    @PreDestroy
    public void destroy() {
        if (timer != null) {
            timer.purge();
            timer.cancel();
        }
    }

    public long getPollTime() {
        return pollTime;
    }

    public KubernetesModel getModel() {
        return model;
    }
}


text:
---
id: es2015
showid: ttt
layout: shows/single-video
collection: ttt-series-2
title: "ES2015"
description: ""
published: true
showYoutubeID: he1SoCUIFW0

date: 2015-09-29 00:00:00
article:
  written_on: 2015-09-29
  updated_on: 2015-09-29

---

[ES2015](http://www.ecma-international.org/ecma-262/6.0/) (formally ES6) is a
fantastic step forward for the JavaScript language. It brings new features and
sugaring for patterns that required significant boilerplate in ES5. This
includes classes, arrow functions and modules. In this episode, we cover tools
we use to take full advantage of ES2015 when building JavaScript web-apps.

# Transpilation with Babel

Although JavaScript engines are making great
[progress](http://kangax.github.io/compat-table/es6/) in implementing ES2015
features natively, there is one gotcha. To use the complete set of features
today, you will need to transpile your code back down to ES5 so it can be
interpreted by current browsers. Thankfully, tools like Babel exist that make
this process next to painless.

## Using Babel in Your Build Process

[Babel](https://babeljs.io/) allows you to take JavaScript written using ES2015
features and transpiles it back to ES5 so that it can work in browsers that
don't support these features today. Adding Babel to your build process can be
done like so.

{% highlight javascript %}
var gulp = require('gulp');
var babel = require('gulp-babel');

gulp.task('default', function () {
    return gulp.src('src/app.js')
        .pipe(babel())
        .pipe(gulp.dest('dist'));
});
{% endhighlight %}

The one feature Babel can't transpile is Modules. Modules in ES6 allow you
create classes and export / import classes between files. To transpile modules
pass your JavaScript through
[Browserify](https://github.com/substack/node-browserify), which will merge the
files and then pass this through [Babelify](https://github.com/babel/babelify)
(a version of Babel which can handle the output from Browserify).

{% highlight javascript %}
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var browserify = require('browserify');

gulp.task('babelify', function() {
  browserify({ entries: './src.js', debug: true })
    .transform(babelify)
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('./dist/js/'));
});
{% endhighlight %}

## Multiple JavaScript Files

The above example requires you to define a specific file, which may become a bit
of a burden, Matt does the following in Gulp to search and transpile files
ending with .es6.js.

{% highlight javascript %}
var config = {
  src: 'src/scripts',
  dest: 'dist/scripts'
};
var es6FileGlob = '/**/*.es6.js';

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var glob = require('glob');
var path = require('path');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');

// Takes an array of bundles to run through browserify and babelify
function transpileES6Modules(browserifyFileEntries) {
  browserifyFileEntries.forEach(function(fileEntry) {
    var browserifyBundle = browserify({
        entries: [fileEntry.srcPath]
      })
      .transform(babelify);

    var finalStream = browserifyBundle.bundle()
      .on('log', plugins.util.log.bind(plugins.util, 'Browserify Log'))
      .on('error', plugins.util.log.bind(plugins.util, 'Browserify Error'))
      .pipe(source(fileEntry.outputFilename));

    return finalStream.pipe(gulp.dest(fileEntry.dest));
  });
}

// This takes a source path and finds all files ending
// with .es6.js and creates the bundles to run through browserify
// and babelify
function handleES6Scripts(srcPath) {
  var browserifyFileEntries = [];

  var es6Filepaths = glob.sync(srcPath + es6FileGlob);
  es6Filepaths.forEach(function(filepath) {
    var filename = path.basename(filepath);
    var directoryOfFile = path.dirname(filepath);
    var relativeDirectory = path.relative(
      srcPath,
      directoryOfFile);

    // Create an object and add to the browserify bundle array
    browserifyFileEntries.push({
      srcPath: './' + filepath,
      outputFilename: filename,
      dest: path.join(config.dest, relativeDirectory)
    });
  });

  transpileES6Modules(browserifyFileEntries);
}

gulp.task('scripts:es6', ['scripts:lint'], function(cb) {
  handleES6Scripts(config.src);

  cb();
});
{% endhighlight %}

# Linting and Style Checks

There are few options when linting your code for possible issues and style
compliance.

## JSHint and JSCS

[JSHint](http://jshint.com/) and [JSCS](http://jscs.info/) are the most common
tools used for today's JavaScript style-checking.

JSHint will highlight any possible issues in your code as well as call out any
patterns generally regarded as bad practice.

JSCS will look at the style of your code, this includes things like ensuring
just tabs or spaces are used and spaces are put in consistent places.

To use JSHint and JSCS on ES2015 code you need to add `"esnext": true` to your
`.jshintrc` and `.jscsrc` files

## ESLint

[ESLint](http://eslint.org/) is an alternative linter and style checker in one.
This tool is picking up a lot of steam and has some nice features over JSHint
such as being able to indicate environments your JavaScript is written for and
setting the error / warning level for specific problems.

ESLint is fully customizable and provides custom linting rules where you're in
the driver's seat of whether options can be switched off or toned down via its
configuration files. In addition, if you're using React, ESLint also works with
JSX.

Setting up ESLint in your build process isn't too hard either.

{% highlight javascript %}
var gulp = require('gulp'),
    eslint = require('gulp-eslint');

gulp.task('lint', function () {
    return gulp.src(['js/**/*.js'])
        // eslint() attaches the lint output to the eslint property
        // of the file object so it can be used by other modules.
        .pipe(eslint())
        // eslint.format() outputs the lint results to the console.
        // Alternatively use eslint.formatEach() (see Docs).
        .pipe(eslint.format())
        // To have the process exit with an error code (1) on
        // lint error, return the stream and pipe to failOnError last.
        .pipe(eslint.failOnError());
});

gulp.task('default', ['lint'], function () {
    // This will only run if the lint task is successful...
});
{% endhighlight %}

The Babel team maintain [babel-estlint](https://github.com/babel/babel-eslint),
a tool that lets you lint any valid Babel code using ESLint. While ESLint
supports custom parsers, some of the syntax supported by Babel isn't directly
supported by ESLint so it's another option if you require even further
flexibility. This can be setup by customising the `parser` section of your
`.eslintrc` file:

{% highlight json %}
{
  "parser": "babel-eslint",
  "rules": {
    "strict": 0
  }
}
{% endhighlight %}

Dan Abramov has a great write-up on setting up ESLint and babel-eslint in [Lint
Like It's
2015](https://medium.com/@dan_abramov/lint-like-it-s-2015-6987d44c5b48). It also
covers how to integrate
[SublimeLinter-eslint](https://github.com/roadhump/SublimeLinter-eslint) into
your workflow for ES2015 linting support in Sublime Text.

Which should you use? Try them out and stick with what works best for you.

# ES2015 Syntax Highlighting

You'll of course want your ES2015 code to be correctly syntax highlighted. We
enjoy using [babel-sublime](https://github.com/babel/babel-sublime), which can
be installed from [Package Control](https://packagecontrol.io/installation).
When setting up, we recommend making sure to set it as the default for any files
you need highlighting to work against. This will of course include JS but may
also cover JSX if using React.

# Documenting ES2015

We've historically relied on [JSDoc](http://usejsdoc.org/) quite heavily for documenting our JavaScript code. Unfortunately, it has [open issues](https://github.com/jsdoc3/jsdoc/milestones/3.4.0) for supporting ES2015 (due to be addressed in JSDoc 3), however there are a growing number of alternatives available while we wait for it to catch up. [ESDoc](https://esdoc.org/) is one such option and Jonathan Creamer has a recent [write-up](http://jonathancreamer.com/document-es6-with-esdoc/) on it worth reading.

# Authoring Gulp.js files with Babel

If you happen to be using Gulp for your build process, Gulpfiles can now be
authored using any syntax supported by Babel. We do this over in [Web Starter
Kit](https://github.com/google/web-starter-kit/blob/master/gulpfile.babel.js)
and it's relatively trivial to setup. Using a recent version of Gulp and the
Gulp CLI, simply rename your `gulpfile.js` to `gulpfile.babel.js` and Gulp will
interpret and transpile your ES2015 gulpfile using Babel automatically.

# Favorite ES2015 Features

## Modules

Modules are a way of exporting values, functions and classes from one file such
that you can import them into another file.

{% highlight javascript %}
export function exampleFunction() {
  console.log('I\'m an example. #TrueStory');
}
{% endhighlight %}

{% highlight javascript %}
import { exampleFunction } from './example-function';
import BaseController from './base-controller';

export default class ExampleController extends BaseController {
  constructor() {
    super();

    exampleFunction();
  }

  doSomething() {
    console.log('What should I do? Change the DOM? Print a dancing shark to the console?');
  }
}
{% endhighlight %}

[This site has some great examples and explanations of
Modules](http://www.2ality.com/2014/09/es6-modules-final.html).

## Template Strings

Template strings allow you to replace a port of a string with a variable.

{% highlight javascript %}
// Simple string substitution
var name = "Brendan";
console.log('Yo, ${name}!');

// => "Yo, Brendan!"
{% endhighlight %}

The nice thing with template strings is that the substition is a JavaScript
execution, meaning you can use functions or inline expressions.

{% highlight javascript %}
var a = 10;
var b = 10;
console.log('a+b = ${a+b}.');
//=> a+b = 20.

function fn() { return "I am a result. Rarr"; }
console.log('foo ${fn()} bar');
//=> foo I am a result. Rarr bar.
{% endhighlight %}

You can learn more from [this handy blog post by
Addy](https://developers.google.com/web/updates/2015/01/ES6-Template-Strings?hl=en).

## Shorthand Object Literals

Object literals allow you to avoid having to define the key and value when
creating an object, if the variable has the same name as the key you want the
object to have.

Meaning this:

{% highlight javascript %}
function createObject(name, data) {
  return { name: name, data: data };
}
{% endhighlight %}

Becomes this:

{% highlight javascript %}
function createObject(name, data) {
  return { name, data };
}
{% endhighlight %}

## Computed Property Names

This feature in ES2015 allows you to create property names dynamically on an
object. The [Mozilla docs is a great source of info and have this great
example](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer).

{% highlight javascript %}
var a = {
  ["foo" + ++i]: i,
  ["foo" + ++i]: i,
  ["foo" + ++i]: i
};

console.log(a.foo1); // 1
console.log(a.foo2); // 2
console.log(a.foo3); // 3
{% endhighlight %}

## Fat Arrow Functions

Fat arrow functions allow you to write functions in shorthand where this:

{% highlight javascript %}
button.addEventListener('click', function(event) {
  console.log('The button has recevied a click', event);
});
{% endhighlight %}

Becomes this:

{% highlight javascript %}
button.addEventListener('click', (event) => {
  console.log('The button has recevied a click', event);
});
{% endhighlight %}

Apart from having a shorter syntax, one great feature of using fat arrow
functions is that the scope of the object is the same the enclosing statement.
What this means is that you don't need to call .bind(this) on your function or
create a var that = this.

There are [plenty more examples over on
MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions).

# Links for More Info

## Addy's ES6 Tools Repo

Addy has been busy keeping a list of ES2015 tools and if the tools above aren't
right for you, perhaps you are using Grunt instead of Gulp, then this may have
an answer for you.

[https://github.com/addyosmani/es6-tools](https://github.com/addyosmani/es6-tools)

The above also includes links to additional Babel tooling that can help during
unit testing and beyond.

## Books to Read

There are two books you can check out for free online to learn more about
ES2015. [Understanding ECMAScript 6](https://leanpub.com/understandinges6)
written by Nicholas C. Zakas and [Exploring ES6](http://exploringjs.com/)
written by
Dr. Axel Rauschmayer.

## Tower of Babel

If you're interested in learning ES2015 features in your command-line,
[tower-of-babel](https://github.com/yosuke-furukawa/tower-of-babel) offers a
series of exercises that might be of interest. All of them walk through using
Babel.

**Other resources in case of interest:**

* [ES2015 equivalents authored in ES5](https://github.com/addyosmani/es6-equivalents-in-es5)
* [Voice Memos app written using ES2015 (Babel)](https://github.com/GoogleChrome/voice-memos)
* [You Don't Know JS: ES6 & Beyond](https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20&%20beyond/README.md#you-dont-know-js-es6--beyond)


text:
/**
 * Package for simple ThreadPool implementation tests.
 * @author Eugene Lazarev mailto(helycopternicht@rambler.ru)
 * @since 24.07.17
 */
package ru.elazarev.waitnotify.threadpool;


text:
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
const _excluded = ["children", "defaultOpen", "indicatorLabel", "isOpen", "label", "onBlur", "onClick", "onClose", "onFocus", "onKeyUp", "onOpen", "onMouseEnter", "onMouseLeave", "toggleOpen", "truncate"],
      _excluded2 = ["indicator", "children", "focusVisible", "onBlur", "onClick", "onKeyUp"];

let _ = t => t,
    _t;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import { ArrowDropDown } from '@styled-icons/material/ArrowDropDown';
import { ArrowRight } from '@styled-icons/material/ArrowRight';
import styled from 'styled-components';
import React, { cloneElement, useContext } from 'react';
import { partitionTreeProps, useTreeHandlers } from '../Tree/utils';
import { listItemDimensions } from '../ListItem';
import { TreeContext } from '../Tree/TreeContext';
import { useAccordion2 } from '../Accordion2';
import { createSafeRel, HoverDisclosureContext, partitionAriaProps, useWrapEvent } from '../utils';
import { List } from '../List';
import { AccordionIndicator } from '../Accordion2/AccordionIndicator';
import { createListItemPartitions } from '../ListItem/utils';
import { WindowedTreeContext } from '../Tree/WindowedTreeNode';
import { NavTreeDisclosure } from './NavTreeDisclosure';
import { NavTreeItemContent } from './NavTreeItemContent';
export const INDICATOR_SPACER = '8px';
export const NavTree = styled(_ref => {
  let {
    children,
    defaultOpen,
    indicatorLabel,
    isOpen: propsIsOpen,
    label,
    onBlur,
    onClick,
    onClose,
    onFocus,
    onKeyUp,
    onOpen,
    onMouseEnter,
    onMouseLeave,
    toggleOpen: propsToggleOpen,
    truncate = true
  } = _ref,
      restProps = _objectWithoutProperties(_ref, _excluded);

  const isIndicatorToggleOnly = !!restProps.href;
  const [treeItemInnerProps, accordionInnerProps] = partitionTreeProps(restProps);
  const {
    hovered,
    contentHandlers,
    wrapperHandlers
  } = useTreeHandlers({
    onFocus,
    onMouseEnter,
    onMouseLeave
  });
  const {
    disabled,
    href,
    icon,
    rel,
    selected,
    target
  } = treeItemInnerProps;
  const [ariaProps] = partitionAriaProps(restProps);
  const treeContext = useContext(TreeContext);
  const {
    isOpen: contextIsOpen,
    toggleNode,
    partialRender
  } = useContext(WindowedTreeContext);
  const isOpen = contextIsOpen !== null && contextIsOpen !== void 0 ? contextIsOpen : propsIsOpen;
  const toggleOpen = toggleNode !== null && toggleNode !== void 0 ? toggleNode : propsToggleOpen;
  const startingDepth = 0;
  const depth = treeContext.depth ? treeContext.depth : startingDepth;
  const [inside, outside] = createListItemPartitions(_objectSpread(_objectSpread({}, treeItemInnerProps), {}, {
    children: label,
    icon,
    truncate
  }));

  let accordionProps = _objectSpread({
    defaultOpen,
    onClose,
    onOpen
  }, accordionInnerProps);

  if (isOpen !== undefined && toggleOpen) {
    accordionProps = _objectSpread(_objectSpread({}, accordionProps), {}, {
      isOpen,
      toggleOpen
    });
  }

  const {
    contentDomProps,
    domProps,
    disclosureProps,
    isOpen: accordionIsOpen
  } = useAccordion2(_objectSpread({
    'aria-selected': selected,
    children: React.createElement(List, {
      disableKeyboardNav: true,
      role: "group",
      windowing: false
    }, children),
    disabled,
    indicatorIcons: {
      close: React.createElement(ArrowRight, {
        "aria-label": `${indicatorLabel} Close`
      }),
      open: React.createElement(ArrowDropDown, {
        "aria-label": `${indicatorLabel} Open`
      })
    },
    indicatorPosition: 'left',
    label: inside,
    onBlur,
    role: 'treeitem',
    tabIndex: -1
  }, accordionProps));

  const {
    indicator,
    children: disclosureLabel,
    focusVisible: disclosureFocusVisible,
    onBlur: onBlurDisclosureToggle,
    onClick: onClickDisclosureToggle,
    onKeyUp: onKeyUpDisclosureToggle
  } = disclosureProps,
        disclosureDomProps = _objectWithoutProperties(disclosureProps, _excluded2);

  const indicatorToggleOnlyProps = {
    onBlur: onBlurDisclosureToggle,
    onClick: onClickDisclosureToggle,
    onKeyUp: onKeyUpDisclosureToggle,
    tabIndex: -1
  };
  const renderedIndicator = cloneElement(indicator, _objectSpread({}, isIndicatorToggleOnly ? indicatorToggleOnlyProps : undefined));
  const handleContentBlur = useWrapEvent(event => {
    if (!isIndicatorToggleOnly && onBlurDisclosureToggle) onBlurDisclosureToggle(event);
  });
  const handleContentClick = useWrapEvent(event => {
    if (!isIndicatorToggleOnly && onClickDisclosureToggle) onClickDisclosureToggle(event);
  }, onClick);
  const handleContentKeyUp = useWrapEvent(event => {
    if (!isIndicatorToggleOnly && onKeyUpDisclosureToggle) onKeyUpDisclosureToggle(event);
  });
  const statefulProps = {
    color: 'key',
    disabled,
    hovered,
    ripple: true,
    selected
  };
  const content = React.createElement(React.Fragment, null, isIndicatorToggleOnly && renderedIndicator, React.createElement(NavTreeItemContent, _extends({
    "aria-selected": selected,
    href: href,
    itemRole: isIndicatorToggleOnly ? 'link' : 'none',
    onBlur: handleContentBlur,
    onClick: handleContentClick,
    onKeyUp: handleContentKeyUp,
    rel: createSafeRel(rel, target),
    ripple: false,
    target: target
  }, ariaProps, contentHandlers, disclosureDomProps), !isIndicatorToggleOnly && renderedIndicator, disclosureLabel));
  return React.createElement(HoverDisclosureContext.Provider, {
    value: {
      visible: hovered
    }
  }, React.createElement(TreeContext.Provider, {
    value: {
      color: statefulProps.color,
      depth: depth + 1
    }
  }, React.createElement("div", domProps, !partialRender && React.createElement(NavTreeDisclosure, _extends({
    depth: depth
  }, wrapperHandlers, statefulProps), content, outside), accordionIsOpen && React.createElement("div", contentDomProps))));
}).withConfig({
  displayName: "NavTree",
  componentId: "sc-1ynatxb-0"
})(_t || (_t = _`
  ${0} {
    padding-left: ${0};
    ${0}
  }
`), AccordionIndicator, INDICATOR_SPACER, ({
  icon,
  theme
}) => !icon && `margin-right: ${theme.space[listItemDimensions(theme.defaults.density).gap]};`);
//# sourceMappingURL=NavTree.js.map


text:
package main

import (
	"bytes"
	"github.com/GeoNet/kit/weft"
	"net/http"
)

var mux *http.ServeMux

func init() {
	mux = http.NewServeMux()

	mux.HandleFunc("/", weft.MakeHandler(weft.NoMatch, weft.TextError))
	mux.HandleFunc("/soh/up", weft.MakeHandler(weft.Up, weft.TextError))
	mux.HandleFunc("/soh", weft.MakeHandler(soh, weft.UseError))

	// fdsn-ws-event
	mux.HandleFunc("/fdsnws/event/1", weft.MakeHandler(fdsnEventV1Index, weft.TextError))
	mux.HandleFunc("/fdsnws/event/1/query", weft.MakeHandler(fdsnEventV1Handler, weft.TextError))
	mux.HandleFunc("/fdsnws/event/1/version", weft.MakeHandler(fdsnEventVersion, weft.TextError))
	mux.HandleFunc("/fdsnws/event/1/catalogs", weft.MakeHandler(fdsnEventCatalogs, weft.TextError))
	mux.HandleFunc("/fdsnws/event/1/contributors", weft.MakeHandler(fdsnEventContributors, weft.TextError))
	mux.HandleFunc("/fdsnws/event/1/application.wadl", weft.MakeHandler(fdsnEventWadl, weft.TextError))

	// fdsn-ws-station
	mux.HandleFunc("/fdsnws/station/1", weft.MakeHandler(fdsnStationV1Index, weft.TextError))
	mux.HandleFunc("/fdsnws/station/1/query", weft.MakeHandler(fdsnStationV1Handler, weft.TextError))
	mux.HandleFunc("/fdsnws/station/1/version", weft.MakeHandler(fdsnStationVersion, weft.TextError))
	mux.HandleFunc("/fdsnws/station/1/application.wadl", weft.MakeHandler(fdsnStationWadl, weft.TextError))

	// This service implements the dataselect spec from http://www.fdsn.org/webservices/FDSN-WS-Specifications-1.1.pdf.
	mux.HandleFunc("/fdsnws/dataselect/1", weft.MakeHandler(fdsnDataselectV1Index, weft.TextError))
	mux.HandleFunc("/fdsnws/dataselect/1/query", weft.MakeDirectHandler(fdsnDataselectV1Handler, weft.TextError))
	mux.HandleFunc("/fdsnws/dataselect/1/version", weft.MakeHandler(fdsnDataselectVersion, weft.TextError))
	mux.HandleFunc("/fdsnws/dataselect/1/application.wadl", weft.MakeHandler(fdsnDataselectWadl, weft.TextError))

	mux.HandleFunc("/metrics/fdsnws/dataselect/1/query", weft.MakeHandler(fdsnDataMetricsV1Handler, weft.TextError))

	mux.HandleFunc("/sc3ml", weft.MakeHandler(s3ml, weft.TextError))
}

func soh(r *http.Request, h http.Header, b *bytes.Buffer) error {
	err := weft.CheckQuery(r, []string{"GET"}, []string{}, []string{})
	if err != nil {
		return err
	}

	var i int

	err = db.QueryRow(`SELECT 1`).Scan(&i)
	if err != nil {
		return err
	}

	b.WriteString("ok")

	return nil
}


text:
module Azure::Kusto::Mgmt::V2020_02_15
  module Models
    #
    # The list attached database configurations operation response.
    #
    class AttachedDatabaseConfigurationListResult

      include MsRestAzure

      # @return [Array<AttachedDatabaseConfiguration>] The list of attached
      # database configurations.
      attr_accessor :value

      #
      # Mapper for AttachedDatabaseConfigurationListResult class as Ruby Hash.
      # This will be used for serialization/deserialization.
      #
      def self.mapper()
        {
          client_side_validation: true,
          required: false,
          serialized_name: 'AttachedDatabaseConfigurationListResult',
          type: {
            name: 'Composite',
            class_name: 'AttachedDatabaseConfigurationListResult',
            model_properties: {
              value: {
                client_side_validation: true,
                required: false,
                serialized_name: 'value',
                type: {
                  name: 'Sequence',
                  element: {
                      client_side_validation: true,
                      required: false,
                      serialized_name: 'AttachedDatabaseConfigurationElementType',
                      type: {
                        name: 'Composite',
                        class_name: 'AttachedDatabaseConfiguration'
                      }
                  }
                }
              }
            }
          }
        }
      end
    end
  end
end