text:
#include "graphd/graphd.h"

#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

GRAPHD_SABOTAGE_DECL;

/*  Sort -- take iterator results, sort them, return them in order.
 */

#define GRAPHD_SORT_MAGIC 0x01234567
#define GRAPHD_IS_SORT(cl, sort) \
  cl_assert(cl, (sort)->sort_magic == GRAPHD_SORT_MAGIC)

static const pdb_iterator_type sort_type;

/**
 * @brief Internal state for a sort operator.
 */
typedef struct graphd_iterator_sort_storable {
  graphd_storable sos_storable;

  /*  Context for allocation of base and its referers.
   */
  cm_handle *sos_cm;

  /*  Logging context
   */
  cl_handle *sos_cl;

  /*  The actual contents of the cache.
   */
  graph_idset *sos_idset;

  /*  The most recent add to that cache.
   */
  pdb_id sos_idset_last_added;

} graphd_iterator_sort_storable;

typedef struct graphd_iterator_sort {
  unsigned long sort_magic;

  /**
   * @brief Containing graphd.
   */
  graphd_handle *sort_graphd;

  /**
   * @brief pdb's cm_handle.  Allocate and free through this.
   */
  cm_handle *sort_cm;

  /**
   * @brief pdb's cl_handle.  Log through this.
   */
  cl_handle *sort_cl;

  /**
   * @brief Only in the original - subiterator whose
   *  	results we're sorting here.
   */
  pdb_iterator *sort_sub;

  /**
   * @brief Only in the original - IDs.
   */
  graph_idset *sort_idset;
  graphd_iterator_sort_storable *sort_idset_storable;

  /**
   * @brief The ID most recently added to the idset.
   */
  pdb_id sort_idset_last_added;

  /**
   * @brief Only in the original - the most recently
   *	returned ID.
   *
   *	If it is not PDB_ID_NONE, we need to reposition on that ID
   *	(need to draw values out of the subiterator until we're sure
   *	we've got all the ones up to that ID) before continuing.
   */
  pdb_id sort_idset_resume;

  /**
   *  @brief Only in the original, boundary
   * 	before which the idset is complete.
   */
  unsigned long long sort_horizon;

  /*  The current position in the idset.
   */
  graph_idset_position sort_idset_pos;

  /*
   *  What was the idset pointer when the sort_idset_pos was valid?
   */
  graph_idset *sort_idset_pos_ptr;

  /*  How many elements did the idset have when sort_idset_pos
   *  was valid?
   */
  size_t sort_idset_pos_n;

  /* What was the ID just before (or after, if we're backwards)
   * that position?  PDB_ID_NONE means we're at the very beginning.
   */
  pdb_id sort_idset_pos_id;

  /**
   * Temporary for checking.
   */
  pdb_iterator *sort_sub_check;

} graphd_iterator_sort;

#define osort(it) ((graphd_iterator_sort *)(it)->it_original->it_theory)

/*  We're standing on <id>.  Remember our position in a form that
 *  survives additions to the idset.
 *
 *  <id> may be PDB_ID_NONE; in that case, we're standing on the
 *  set's first element.
 */
static void sort_position_save(pdb_iterator *it, pdb_id id) {
  graphd_iterator_sort *sort = it->it_theory;

  if (osort(it)->sort_idset != NULL) {
    sort->sort_idset_pos_ptr = osort(it)->sort_idset;
    sort->sort_idset_pos_n = osort(it)->sort_idset->gi_n;
  } else {
    sort->sort_idset_pos_ptr = NULL;
    sort->sort_idset_pos_n = 0;
  }
  sort->sort_idset_pos_id = id;
}

/*  Bring our sort_idset_pos in line with where we remember being.
 */
static int sort_position_load(pdb_iterator *it) {
  graphd_iterator_sort *sort = it->it_theory;

  /*  Offset positions in an idset aren't valid after the
   *  idset has been added to or is being restored.
   *  If the idset has changed, restore our position
   *  by positioning on the place we were at last.
   */

  /*  We're done restoring, right?
   */
  cl_assert(sort->sort_cl, osort(it)->sort_idset_resume == PDB_ID_NONE);

  /* Nothing to position in?
   */
  if (osort(it)->sort_idset == NULL) return 0;

  /*  It hasn't changed size?
   */
  if (osort(it)->sort_idset == sort->sort_idset_pos_ptr &&
      osort(it)->sort_idset->gi_n == sort->sort_idset_pos_n)
    return 0;

  /* Position where we remember being.
   */
  if (sort->sort_idset_pos_id == PDB_ID_NONE) {
    if (pdb_iterator_forward(sort->sort_graphd->g_pdb, it))
      graph_idset_next_reset(osort(it)->sort_idset, &sort->sort_idset_pos);
    else
      graph_idset_prev_reset(osort(it)->sort_idset, &sort->sort_idset_pos);
  } else {
    bool found;

    found = graph_idset_locate(osort(it)->sort_idset, sort->sort_idset_pos_id,
                               &sort->sort_idset_pos);
    if (!found) {
      cl_log(sort->sort_cl, CL_LEVEL_FAIL,
             "sort_position_load: failed to find %llu at "
             "%llu.%llu in idset with %llu elements",
             (unsigned long long)sort->sort_idset_pos_id,
             (unsigned long long)sort->sort_idset_pos.gip_ull,
             (unsigned long long)sort->sort_idset_pos.gip_size,
             (unsigned long long)sort->sort_idset->gi_n);
      return GRAPHD_ERR_BADCURSOR;
    }

    cl_assert(sort->sort_cl, found);
  }

  /*  Remember the set we know our position in.
   */
  sort->sort_idset_pos_ptr = osort(it)->sort_idset;
  sort->sort_idset_pos_n = osort(it)->sort_idset->gi_n;

  return 0;
}

static unsigned long sort_storable_hash(void const *data) {
  return (unsigned long)(intptr_t)data;
}

static void sort_storable_destroy(void *data) {
  graphd_iterator_sort_storable *sos = data;
  cm_handle *cm = sos->sos_cm;

  graph_idset_free(sos->sos_idset);
  cm_free(cm, sos);
}

static bool sort_storable_equal(void const *A, void const *B) { return A == B; }

static const graphd_storable_type sort_storable_type = {
    "sort cache", sort_storable_destroy, sort_storable_equal,
    sort_storable_hash};

/*  Create a fresh sort-storable
 *  You will allocate in CM and log through CL.
 *
 *  A successful call transfers one reference to the caller.
 */
static graphd_iterator_sort_storable *graphd_iterator_sort_storable_alloc(
    cm_handle *cm, cl_handle *cl, graph_idset *idset, pdb_id idset_last_added) {
  graphd_iterator_sort_storable *sos;

  sos = cm_malloc(cm, sizeof(*sos));
  if (sos == NULL) return NULL;

  memset(sos, 0, sizeof(*sos));

  sos->sos_storable.gs_linkcount = 1;
  sos->sos_storable.gs_type = &sort_storable_type;
  sos->sos_storable.gs_size = sizeof(*sos);

  sos->sos_cm = cm;
  sos->sos_cl = cl;
  sos->sos_idset = idset;
  sos->sos_idset_last_added = idset_last_added;

  return sos;
}

static int store_idset(pdb_iterator *it) {
  graphd_iterator_sort *sort = osort(it);

  if (sort->sort_idset_storable == NULL) {
    graphd_handle *g = sort->sort_graphd;

    sort->sort_idset_storable = graphd_iterator_sort_storable_alloc(
        g->g_cm, g->g_cl, sort->sort_idset, sort->sort_idset_last_added);
    if (sort->sort_idset_storable == NULL) return ENOMEM;

    sort->sort_idset = sort->sort_idset_storable->sos_idset;
    sort->sort_idset_last_added =
        sort->sort_idset_storable->sos_idset_last_added;
  }
  return 0;
}

static void update_horizon_start(pdb_iterator *it) {
  graphd_iterator_sort *sort = osort(it);
  sort->sort_horizon =
      pdb_iterator_forward(sort->sort_pdb, it) ? it->it_low : it->it_high;

  cl_log(sort->sort_cl, CL_LEVEL_VERBOSE,
         "update_horizon_start: "
         "set horizon to %llu",
         (unsigned long long)sort->sort_horizon);
}

static void update_horizon_eof(pdb_iterator *it) {
  graphd_iterator_sort *sort = osort(it);

  sort->sort_horizon = pdb_iterator_forward(sort->sort_pdb, it)
                           ? PDB_ITERATOR_HIGH_ANY
                           : PDB_ITERATOR_LOW_ANY;
}

static int update_horizon(pdb_iterator *it) {
  graphd_iterator_sort *sort = osort(it);
  cl_handle *cl = sort->sort_cl;
  int err;
  pdb_range_estimate range;
  unsigned long long horizon_in = sort->sort_horizon;

  cl_assert(cl, it);
  cl_assert(cl, sort);
  cl_assert(cl, sort->sort_sub);

  /*  We already know everything?
   */
  if (pdb_iterator_forward(sort->sort_graphd->g_pdb, it)
          ? sort->sort_horizon >= it->it_high
          : sort->sort_horizon < it->it_low)
    return 0;

  PDB_IS_ITERATOR(cl, sort->sort_sub);
  err = pdb_iterator_range_estimate(sort->sort_graphd->g_pdb, sort->sort_sub,
                                    &range);
  if (err != 0) {
    char buf[200];
    cl_log_errno(cl, CL_LEVEL_FAIL, "pdb_iterator_range_estimate", err, "it=%s",
                 pdb_iterator_to_string(sort->sort_graphd->g_pdb,
                                        sort->sort_sub, buf, sizeof buf));
    return err;
  }

  if (range.range_high <= range.range_low || range.range_n_exact == 0)

    /*  The published range is empty.
     */
    update_horizon_eof(it);
  else {
    /*  Adjust the horizon we're interested in.
     */
    if (pdb_iterator_forward(sort->sort_graphd->g_pdb, it)) {
      if (range.range_low > sort->sort_horizon)
        range.range_low = sort->sort_horizon;
    } else {
      if (range.range_high < sort->sort_horizon)
        range.range_high = sort->sort_horizon;
    }
  }

  if (horizon_in != sort->sort_horizon)
    cl_log(cl, CL_LEVEL_VERBOSE,
           "update_horizon: "
           "changed horizon %llu->%llu",
           horizon_in, (unsigned long long)sort->sort_horizon);

  return 0;
}

static int expand_cache(pdb_handle *pdb, pdb_iterator *it,
                        pdb_budget *budget_inout) {
  cl_handle *cl = osort(it)->sort_cl;
  int err;
  pdb_id id;

  cl_assert(cl, osort(it)->sort_idset != NULL);

  err = pdb_iterator_next(pdb, osort(it)->sort_sub, &id, budget_inout);
  if (err != 0) {
    cl_log(cl, CL_LEVEL_VERBOSE,
           "expand_cache: "
           "pdb_iterator_next: error: %s",
           graphd_strerror(err));

    /*  We're completely done?  Move the horizon all the way to
     *  the end - we're now knowing everything there is to know
     *  about this iterator.
     */
    if (err == PDB_ERR_NO)
      update_horizon_eof(it);

    else if (err != PDB_ERR_MORE)
      cl_log(cl, CL_LEVEL_VERBOSE,
             "expand_cache: not updating "
             "horizon, since error %d != NO %d",
             err, PDB_ERR_NO);

    return err;
  }

  err = graph_idset_insert(osort(it)->sort_idset, id);
  if (err != 0) return err;

  if (id == osort(it)->sort_idset_resume)
    osort(it)->sort_idset_resume = PDB_ID_NONE;
  osort(it)->sort_idset_last_added = id;

  return 0;
}

static int sort_resume(pdb_handle *pdb, pdb_iterator *it,
                       pdb_budget *budget_inout) {
  graphd_iterator_sort *sort = osort(it);
  graphd_handle *g = sort->sort_graphd;
  int err = 0;

  if (sort->sort_idset_resume == PDB_ID_NONE) return 0;

  cl_log(sort->sort_cl, CL_LEVEL_VERBOSE, "sort_resume: find %llx with $%lld",
         (unsigned long long)sort->sort_idset_resume, (long long)*budget_inout);

  while (*budget_inout >= 0) {
    /*  Get the next ID out of the subiterator and add
     *  it into the idset.
     */
    if (sort->sort_idset == NULL) {
      if (sort->sort_idset_storable == NULL) {
        graph_idset *idset = graph_idset_tile_create(g->g_graph);
        if (idset == NULL) return ENOMEM;

        sort->sort_idset_storable = graphd_iterator_sort_storable_alloc(
            g->g_cm, g->g_cl, idset, PDB_ID_NONE);
        if (sort->sort_idset_storable == NULL) {
          graph_idset_free(idset);
          return ENOMEM;
        }
      }
      sort->sort_idset = sort->sort_idset_storable->sos_idset;
      sort->sort_idset_last_added =
          sort->sort_idset_storable->sos_idset_last_added;
    }

    err = expand_cache(pdb, it, budget_inout);
    if (err != 0) {
      if (err != PDB_ERR_NO) return err;

      /*  We're completely done.
       */
      if (sort->sort_idset_resume != PDB_ID_NONE) {
        /*  We were trying to resume a position that
         *  isn't in the set.  That can happen if
         *  a cursor gets reused across update
         *  boundaries - we remembered a landmark
         *  X that no longer exists - but shouldn't.
         */
        cl_log(sort->sort_cl, CL_LEVEL_FAIL,
               "sort_resume: resumption point "
               "%llu isn't in the result set anymore "
               "- outdated cursor?",
               (unsigned long long)sort->sort_idset_resume);
        return GRAPHD_ERR_BADCURSOR;
      }
      cl_assert(sort->sort_cl, sort->sort_idset_resume == PDB_ID_NONE);
      return 0;
    }

    if (sort->sort_idset_resume == PDB_ID_NONE) return update_horizon(it);
  }

  /* We ran out of budget.
   */
  err = update_horizon(it);
  if (err != 0) return err;

  return PDB_ERR_MORE;
}

static bool id_would_be_cached(pdb_handle *pdb, pdb_iterator *it, pdb_id id) {
  graphd_iterator_sort *sort = osort(it);

  return pdb_iterator_forward(pdb, it) ? sort->sort_horizon > id
                                       : sort->sort_horizon <= id;
}

static int sort_find_loc(pdb_handle *pdb, pdb_iterator *it, pdb_id id_in,
                         pdb_id *id_out, pdb_budget *budget_inout,
                         char const *file, int line) {
  pdb_budget budget_in = *budget_inout;
  graph_idset *idset;
  graph_idset_position *pos, tmp_pos;
  graphd_iterator_sort *sort = it->it_theory;
  int err = 0;
  pdb_id id;
  unsigned long long ull;

  id = id_in;
  if (pdb_iterator_forward(pdb, it)) {
    if (id_in < it->it_low) id = it->it_low;
  } else {
    if (id_in >= it->it_high) id = it->it_high - 1;
  }

  /*  If this id happens to already be in the cache,
   *  we can just position on it without being caught up.
   */
  if (graph_idset_locate(osort(it)->sort_idset, id, &tmp_pos)) {
    *id_out = id;
    sort->sort_idset_pos = tmp_pos;

    *budget_inout -= 3;
    sort_position_save(it, id);

    pdb_rxs_log(pdb, "FIND %p sort %llx -> %llx ($%lld)", (void *)it,
                (unsigned long long)id_in, (unsigned long long)*id_out,
                (long long)(budget_in - *budget_inout));
    pdb_iterator_account_charge_budget(pdb, it, find);
    return 0;
  }

  /*  If we've already gone beyond the iterator, we
   *  can just seek back to that in the idset.
   *
   *  Otherwise, we'll have to go forward until we
   *  move past the destination.
   */
  while (!id_would_be_cached(pdb, it, id)) {
    err = expand_cache(pdb, it, budget_inout);
    if (err == PDB_ERR_NO)
      break;
    else if (err != 0)
      return err;

    update_horizon(it);
  }

  /* Find exactly what we're looking for?
   */
  idset = osort(it)->sort_idset;
  pos = &sort->sort_idset_pos;

  if (graph_idset_locate(idset, id, pos)) {
    *id_out = id;
    sort_position_save(it, id);

    pdb_rxs_log(pdb, "FIND %p sort %llx -> %llx ($%lld)", (void *)it,
                (unsigned long long)id_in, (unsigned long long)*id_out,
                (long long)(budget_in - *budget_inout));
    pdb_iterator_account_charge_budget(pdb, it, find);
    return 0;
  }

  /* No?  Go to the next.
   */
  if (pdb_iterator_forward(pdb, it)) {
    if (graph_idset_next(idset, &ull, pos)) {
      if (ull >= it->it_high)
        err = GRAPHD_ERR_NO;
      else
        *id_out = ull;
      goto err;
    }
  } else {
    if (graph_idset_prev(idset, &ull, pos)) {
      if (ull < it->it_low)
        err = GRAPHD_ERR_NO;
      else
        *id_out = ull;
      goto err;
    }
  }
  err = GRAPHD_ERR_NO;

err:
  if (err == 0) {
    sort_position_save(it, *id_out);
    pdb_rxs_log(pdb, "FIND %p sort %llx %llx ($%lld)", (void *)it,
                (unsigned long long)id_in, (unsigned long long)*id_out,
                (long long)(budget_in - *budget_inout));
  } else
    pdb_rxs_log(pdb, "FIND %p sort %llx %s ($%lld)", (void *)it,
                (unsigned long long)id_in,
                (err == GRAPHD_ERR_NO ? "eof" : err == GRAPHD_ERR_MORE
                                                    ? "suspended"
                                                    : graphd_strerror(err)),
                (long long)(budget_in - *budget_inout));

  pdb_iterator_account_charge_budget(pdb, it, find);
  return err;
}

static int sort_reset(pdb_handle *pdb, pdb_iterator *it) {
  graphd_iterator_sort *sort = it->it_theory;
  graph_idset *idset = osort(it)->sort_idset;
  graph_idset_position *pos = &sort->sort_idset_pos;

  pdb_rxs_log(pdb, "RESET %p sort", (void *)it);

  if (idset == NULL) return 0;

  if (pdb_iterator_forward(pdb, it))
    graph_idset_next_reset(idset, pos);
  else
    graph_idset_prev_reset(idset, pos);

  sort_position_save(it, PDB_ID_NONE);

  return 0;
}

static int sort_statistics(pdb_handle *pdb, pdb_iterator *it,
                           pdb_budget *budget_inout) {
  graphd_iterator_sort *sort = it->it_theory;
  cl_handle *cl = sort->sort_cl;
  int err;
  pdb_budget budget_in = *budget_inout;
  char buf[200];
  bool forward;

  err = pdb_iterator_statistics(pdb, osort(it)->sort_sub, budget_inout);
  if (err != 0) return err;

  /*  If the subiterator turned sorted itself, remove us.
   */
  if (pdb_iterator_sorted(pdb, osort(it)->sort_sub) &&
      pdb_iterator_forward(pdb, it) ==
          pdb_iterator_forward(pdb, osort(it)->sort_sub))
    return pdb_iterator_substitute(pdb, it, osort(it)->sort_sub);

  /*  Inherit results from the subiterator, except for our
   *  sortedness.
   */
  forward = pdb_iterator_forward(pdb, it);
  pdb_iterator_statistics_copy(pdb, it, osort(it)->sort_sub);
  pdb_iterator_forward_set(pdb, it, forward);

  cl_log(cl, CL_LEVEL_VERBOSE | PDB_FACILITY_ITERATOR,
         "PDB STAT for SORT[#%llu] %s: n=%llu cc=%llu "
         "nc=%llu fc=%llu ($%lld)",
         (unsigned long long)it->it_id,
         pdb_iterator_to_string(pdb, it, buf, sizeof buf),
         (unsigned long long)pdb_iterator_n(pdb, it),
         (unsigned long long)pdb_iterator_check_cost(pdb, it),
         (unsigned long long)pdb_iterator_next_cost(pdb, it),
         (unsigned long long)pdb_iterator_find_cost(pdb, it),
         (long long)(budget_in - *budget_inout));

  return 0;
}

static int sort_check(pdb_handle *pdb, pdb_iterator *it, pdb_id check_id,
                      pdb_budget *budget_inout) {
  graphd_iterator_sort *const sort = it->it_theory;
  pdb_budget const budget_in = *budget_inout;
  int err = 0;

  if (id_would_be_cached(pdb, it, check_id)) {
    *budget_inout -= PDB_COST_FUNCTION_CALL;
    err = graph_idset_check(osort(it)->sort_idset, check_id) ? 0 : PDB_ERR_NO;
  } else {
    /*  We can do either of two things:
     *
     * 	- call the check function on the subiterator
     *
     *	- pull entries into our cache until the thing
     * 	  the caller is asking about is in there, and
     *	  then just consult the cache.
     *
     *  For now, let's just call the check function.
     */
    if (sort->sort_sub_check == NULL) {
      err = pdb_iterator_clone(pdb, osort(it)->sort_sub, &sort->sort_sub_check);
      if (err != 0) return err;
      cl_assert(sort->sort_cl, sort->sort_sub_check->it_call_state == 0);
    }
    err = pdb_iterator_check(pdb, sort->sort_sub_check, check_id, budget_inout);
  }
  pdb_rxs_log(pdb, "CHECK %p sort %llx: %s [$%llu]", (void *)it,
              (unsigned long long)check_id,
              err == PDB_ERR_NO ? "no" : (err ? graphd_strerror(err) : "yes"),
              budget_in - *budget_inout);
  return err;
}

static int sort_next_loc(pdb_handle *pdb, pdb_iterator *it, pdb_id *id_out,
                         pdb_budget *budget_inout, char const *file, int line) {
  pdb_budget budget_in = *budget_inout;
  graphd_iterator_sort *sort = it->it_theory;
  graph_idset_position pos;
  int err = 0;

  *budget_inout -= PDB_COST_FUNCTION_CALL;

  pdb_rxs_push(pdb, "NEXT %p sort (hor=%llu, pos-id=%llu)", (void *)it,
               (unsigned long long)osort(it)->sort_horizon,
               (unsigned long long)sort->sort_idset_pos_id);

  if ((err = sort_resume(pdb, it, budget_inout)) != 0) {
    pdb_rxs_pop(pdb, "NEXT %p sort resume: %s ($%lld)", (void *)it,
                graphd_strerror(err), (long long)(budget_in - *budget_inout));

    cl_log_errno(sort->sort_cl, CL_LEVEL_FAIL, "sort_resume", err,
                 "resume_id=%llx",
                 (unsigned long long)osort(it)->sort_idset_resume);
    return err;
  }
  cl_assert(sort->sort_cl, osort(it)->sort_idset_resume == PDB_ID_NONE);

  cl_assert(sort->sort_cl, osort(it)->sort_idset != NULL);
  for (;;) {
    unsigned long long ull;

    if (GRAPHD_SABOTAGE(sort->sort_graphd, *budget_inout < 0)) {
      err = PDB_ERR_MORE;
      break;
    }

    /*  We're done restoring, right?
     */
    cl_assert(sort->sort_cl, osort(it)->sort_idset_resume == PDB_ID_NONE);

    if ((err = sort_position_load(it)) != 0) goto err;

    pos = sort->sort_idset_pos;

    /*  What's the next value in our cached current location?
     */
    ull = 0;
    if (pdb_iterator_forward(pdb, it)
            ? graph_idset_next(osort(it)->sort_idset, &ull, &pos)
            : graph_idset_prev(osort(it)->sort_idset, &ull, &pos)) {
      cl_log(sort->sort_cl, CL_LEVEL_VERBOSE, "sort_next: got %llu from idset",
             ull);

      /*  If we know that we have everything up to
       *  (or down to) this point in the cache, the
       *  result is usable.
       */
      if (id_would_be_cached(pdb, it, ull)) {
        cl_log(sort->sort_cl, CL_LEVEL_VERBOSE,
               "sort_next: that would be cached");

        *id_out = ull;
        sort->sort_idset_pos = pos;

        /* Found something we can safely return. */

        err = 0;
        goto err;
      }
    } else {
      /*  Do we already know everything there
       *  is to know?
       */
      ull = pdb_iterator_forward(pdb, it) ? it->it_high - 1 : it->it_low;

      cl_log(sort->sort_cl, CL_LEVEL_VERBOSE,
             "sort_next: got NO from idset; try %llu", ull);

      if (id_would_be_cached(pdb, it, ull)) {
        cl_log(sort->sort_cl, CL_LEVEL_VERBOSE,
               "sort_next: we're completely cached.");
        err = PDB_ERR_NO;
        goto err;
      }
    }

    /*  Add more knowledge to the cache.
     */
    err = expand_cache(pdb, it, budget_inout);
    if (err != 0 && err != PDB_ERR_NO) goto err;

    update_horizon(it);
  }

err:
  if (err == 0) {
    sort_position_save(it, *id_out);
    pdb_rxs_pop(pdb, "NEXT %p sort %llx ($%lld)", (void *)it,
                (unsigned long long)*id_out,
                (long long)(budget_in - *budget_inout));
  } else if (err == PDB_ERR_NO)
    pdb_rxs_pop(pdb, "NEXT %p sort EOF ($%lld)", (void *)it,
                (long long)(budget_in - *budget_inout));

  else if (err == PDB_ERR_MORE)
    pdb_rxs_pop(pdb, "NEXT %p sort suspend ($%lld)", (void *)it,
                (long long)(budget_in - *budget_inout));

  else
    pdb_rxs_pop(pdb, "NEXT %p sort %s ($%lld)", (void *)it,
                graphd_strerror(err), (long long)(budget_in - *budget_inout));

  pdb_iterator_account_charge_budget(pdb, it, next);

  return err;
}

/*
 * sort:[~](SUBIT) / IDSET-POSITION / (SUBPOS/STATE)(SUBPOS/STATE)
 */
static int sort_freeze(pdb_handle *pdb, pdb_iterator *it, unsigned int flags,
                       cm_buffer *buf) {
  graphd_iterator_sort *sort = it->it_theory;
  int err = 0;
  char const *sep = "";

  /*  If the sort_sub iterator has evolved to be structurally
   *  different from the check iterator, don't save the check
   *  iterator.
   */

  if (flags & PDB_ITERATOR_FREEZE_SET) {
    err = cm_buffer_sprintf(buf, "sort:%s(",
                            pdb_iterator_forward(pdb, it) ? "" : "~");
    if (err != 0) return err;

    err = pdb_iterator_freeze(pdb, osort(it)->sort_sub, PDB_ITERATOR_FREEZE_SET,
                              buf);
    if (err != 0) return err;

    err = cm_buffer_add_string(buf, ")");
    if (err != 0) return err;

    sep = "/";
  }
  if (flags & PDB_ITERATOR_FREEZE_POSITION) {
    char b2[200], b3[200];

    err = cm_buffer_sprintf(
        buf, "%s%s:%llu:%zu:%s:%llu", sep,
        pdb_iterator_has_position(pdb, it)
            ? pdb_id_to_string(pdb, osort(it)->sort_idset_last_added, b2,
                               sizeof b2)
            : "-",
        sort->sort_idset_pos.gip_ull, sort->sort_idset_pos.gip_size,
        pdb_id_to_string(pdb, sort->sort_idset_pos_id, b3, sizeof b3),
        osort(it)->sort_idset ? (unsigned long long)osort(it)->sort_idset->gi_n
                              : 0);
    if (err != 0) return err;

    sep = "/";
  }
  if (flags & PDB_ITERATOR_FREEZE_STATE) {
    char sb[GRAPHD_ITERATOR_RESOURCE_STAMP_SIZE];

    /*  [ids:...] (subpos/substate) (checksubpos/substate)
     */
    if (osort(it)->sort_idset_storable == NULL &&
        osort(it)->sort_idset != NULL) {
      err = store_idset(it);
      if (err != 0) return err;
    }

    if (osort(it)->sort_idset_storable) {
      if ((err = graphd_iterator_resource_store(
               sort->sort_graphd,
               (graphd_storable *)osort(it)->sort_idset_storable, sb,
               sizeof sb)) != 0 ||
          (err = cm_buffer_sprintf(buf, "%s[ids:@%s]", sep, sb)) != 0)
        return err;
    } else {
      err = cm_buffer_sprintf(buf, "%s", sep);
      if (err) return err;
    }

    err = graphd_iterator_util_freeze_subiterator(
        pdb, osort(it)->sort_sub,
        PDB_ITERATOR_FREEZE_POSITION | PDB_ITERATOR_FREEZE_STATE, buf);
    if (err != 0) goto err;

    /*  Are sort_sub and sort_sub_check sibling clones?
     *  If yes, freeze sort_sub_check's state.  Otherwise,
     *  freeze a null state; we'll reclone sort_sub_check
     *  from the (more advanced) sort_sub during thaw.
     */
    err = graphd_iterator_util_freeze_subiterator(
        pdb, (sort->sort_sub_check != NULL && sort->sort_sub != NULL &&
              sort->sort_sub_check->it_original == sort->sort_sub->it_original)
                 ? sort->sort_sub_check
                 : NULL,
        PDB_ITERATOR_FREEZE_POSITION | PDB_ITERATOR_FREEZE_STATE, buf);
    if (err != 0) goto err;
  }
  return 0;
err:
  return err;
}

static int sort_clone(pdb_handle *pdb, pdb_iterator *it,
                      pdb_iterator **it_out) {
  pdb_iterator *it_orig = it->it_original;
  graphd_iterator_sort *sort = it->it_theory;
  cm_handle *cm = sort->sort_cm;
  graphd_iterator_sort *sort_out;
  int err;

  PDB_IS_ITERATOR(sort->sort_cl, it);
  GRAPHD_IS_SORT(pdb_log(pdb), sort);

  /*  If the original iterator has evolved into something
   *  other than an "sort" iterator, clone that iterator
   *  directly and reset it.  If we had a position to save,
   *  we would have already evolved.
   */
  if (it_orig->it_type != it->it_type || it->it_id != it_orig->it_id)
    return pdb_iterator_clone(pdb, it_orig, it_out);

  *it_out = NULL;
  if ((sort_out = cm_malcpy(cm, sort, sizeof(*sort))) == NULL) {
    return errno ? errno : ENOMEM;
  }

  /* Since we're a clone, all these pointers can be NULL.
   */
  sort_out->sort_idset_storable = NULL;
  sort_out->sort_idset = NULL;
  sort_out->sort_sub = NULL;
  sort_out->sort_sub_check = NULL;

  if ((err = pdb_iterator_make_clone(pdb, it_orig, it_out)) != 0) {
    cm_free(sort->sort_cm, sort_out);
    return err;
  }
  (*it_out)->it_theory = sort_out;

  if (!pdb_iterator_has_position(pdb, it) &&
      (err = pdb_iterator_reset(pdb, *it_out)) != 0) {
    pdb_iterator_destroy(pdb, it_out);
    return err;
  }
  return 0;
}

static void sort_finish(pdb_handle *pdb, pdb_iterator *it) {
  graphd_iterator_sort *sort = it->it_theory;

  if (sort != NULL) {
    cl_cover(sort->sort_cl);

    if (sort->sort_sub_check != NULL)
      pdb_iterator_destroy(pdb, &sort->sort_sub_check);

    if (it->it_original == it) {
      pdb_iterator_destroy(pdb, &sort->sort_sub);
      if (sort->sort_idset_storable != NULL) {
        cl_log(
            sort->sort_cl, CL_LEVEL_VERBOSE,
            "sort_finish: %d links from theidset  storable (before taking one)",
            ((graphd_storable *)sort->sort_idset_storable)->gs_linkcount);

        graphd_storable_unlink(sort->sort_idset_storable);
        sort->sort_idset = NULL;
      } else {
        if (sort->sort_idset != NULL) graph_idset_free(sort->sort_idset);
      }
    }

    cm_free(sort->sort_cm, it->it_displayname);
    it->it_displayname = NULL;

    cm_free(sort->sort_cm, sort);
    it->it_theory = NULL;
  }
  it->it_type = NULL;
  it->it_magic = 0;
}

static char const *sort_to_string(pdb_handle *pdb, pdb_iterator *it, char *buf,
                                  size_t size) {
  char sub[200];

  if (it->it_original->it_id != it->it_id) {
    snprintf(buf, sizeof buf, "sort**%s",
             pdb_iterator_to_string(pdb, it->it_original, sub, sizeof sub));
    return buf;
  }

  snprintf(buf, size, "%ssort:%s", it->it_forward ? "" : "~",
           pdb_iterator_to_string(pdb, osort(it)->sort_sub, sub, sizeof sub));
  return buf;
}

/**
 * @brief Will this iterator ever return a value beyond (in sort order)
 * 	the given value?
 *
 * @param pdb		module handle
 * @param it		iterator we're asking about
 * @param s		start of comparison value
 * @param e		end of comparison value
 * @param beyond_out	out: true if we're safely beyond this value.
 *
 * @return 0 on success, a nonzero error code on error.
 */
static int sort_beyond(pdb_handle *pdb, pdb_iterator *it, char const *s,
                       char const *e, bool *beyond_out) {
  pdb_range_estimate range;
  pdb_id id;
  int err;

  if (e - s != sizeof id) return PDB_ERR_NO;
  memcpy(&id, s, e - s);

  err = pdb_iterator_range_estimate(pdb, it, &range);
  if (err != 0) return err;

  *beyond_out = pdb_iterator_forward(pdb, it) ? id < range.range_low
                                              : id >= range.range_high;
  return 0;
}

static int sort_range_estimate(pdb_handle *pdb, pdb_iterator *it,
                               pdb_range_estimate *range) {
  graphd_iterator_sort *sort = it->it_theory;
  int err;
  graph_idset_position pos;
  unsigned long long next_id;
  unsigned long long n_cache_unreturned = 0;

  /*  If we're still resuming, we don't know yet.
   */
  if (osort(it)->sort_idset_resume != PDB_ID_NONE)
    return pdb_iterator_range_estimate_default(pdb, it, range);

  err = pdb_iterator_range_estimate(pdb, osort(it)->sort_sub, range);
  if (err != 0) return err;

  cl_assert(sort->sort_cl, osort(it)->sort_idset_resume == PDB_ID_NONE);
  if ((err = sort_position_load(it)) != 0) return err;

  pos = sort->sort_idset_pos;
  if (pdb_iterator_forward(pdb, it)
          ? graph_idset_next(osort(it)->sort_idset, &next_id, &pos)
          : graph_idset_prev(osort(it)->sort_idset, &next_id, &pos)) {
    /*  Expand the subiterator range to include at least <next_id>.
     */
    if (next_id < range->range_low) range->range_low = next_id;
    if (next_id >= range->range_high) range->range_high = next_id + 1;

    n_cache_unreturned =
        1 + graph_idset_offset(
                osort(it)->sort_idset, &pos,
                pdb_iterator_forward(pdb, it) ? it->it_high : it->it_low);
  }

  /*  Unless we're at the end of our range --end == GRAPH_ERR_NO--
   *  we need to add the elements in the cache, but no longer in the
   *  subiterator, to the subiterator's count.
   */
  if (range->range_n_exact != PDB_COUNT_UNBOUNDED)
    range->range_n_exact += n_cache_unreturned;
  if (range->range_n_max != PDB_COUNT_UNBOUNDED)
    range->range_n_max += n_cache_unreturned;

  return 0;
}

static const pdb_iterator_type sort_type = {
    "sort",        sort_finish,         sort_reset, sort_clone,
    sort_freeze,   sort_to_string,

    sort_next_loc, sort_find_loc,       sort_check, sort_statistics,

    NULL, /* idarray */
    NULL, /* primitive-summary */

    sort_beyond,   sort_range_estimate, NULL, /* restrict */

    NULL, /* suspend */
    NULL  /* unsuspend */
};

/**
 * @brief Create a "sort" iterator structure.
 *
 * @param greq		request for whom we're doing this
 * @param forward	sort how?  Forward (true) or backwards (false) ?
 * @param sub		pointer to subiterator.  A successful call
 *			zeroes out the pointer and takes possession of
 *			the pointed-to iterator.
 * @param it_out	assign the new iterator to this
 * @param file		filename of calling code
 * @param line		line number of calling code
 *
 * @return 0 on success, otherwise a nonzero error code.
 */
int graphd_iterator_sort_create_loc(graphd_request *greq, bool forward,
                                    pdb_iterator **sub, pdb_iterator **it_out,
                                    char const *file, int line) {
  graphd_handle *g = graphd_request_graphd(greq);
  pdb_handle *pdb = g->g_pdb;
  cl_handle *cl = pdb_log(pdb);
  cm_handle *cm = pdb_mem(pdb);
  graphd_iterator_sort *sort;

  *it_out = cm->cm_realloc_loc(cm, NULL, sizeof(**it_out), file, line);
  if (*it_out == NULL) return errno ? errno : ENOMEM;

  if ((sort = cm_zalloc(cm, sizeof(*sort))) == NULL) {
    int err = errno ? errno : ENOMEM;

    cm_free(cm, *it_out);
    *it_out = NULL;

    return err;
  }

  sort->sort_idset = graph_idset_tile_create(g->g_graph);
  if (sort->sort_idset == NULL) {
    int err = errno ? errno : ENOMEM;

    cm_free(cm, sort);
    cm_free(cm, *it_out);
    *it_out = NULL;

    return err;
  }

  sort->sort_magic = GRAPHD_SORT_MAGIC;
  sort->sort_idset_resume = PDB_ID_NONE;
  sort->sort_graphd = g;
  sort->sort_cl = cl;
  sort->sort_cm = cm;
  sort->sort_sub_check = NULL;

  pdb_iterator_make_loc(g->g_pdb, *it_out, (*sub)->it_low, (*sub)->it_high,
                        forward, file, line);
  pdb_iterator_forward_set(g->g_pdb, *it_out, forward);

  sort->sort_sub = *sub;
  *sub = NULL;

  (*it_out)->it_theory = sort;
  (*it_out)->it_type = &sort_type;

  GRAPHD_IS_SORT(cl, sort);
  (void)sort_reset(pdb, *it_out);
  update_horizon_start(*it_out);

  pdb_rxs_log(pdb, "CREATE %p sort", (void *)*it_out);

  return 0;
}

/**
 * @brief Reconstitute a frozen sort-iterator
 *
 * [~](SUB) / IDSET-POS / (SUBPOS/STATE)(SUBPOS/STATE)
 *
 * @param graphd	module handle
 * @param s		beginning of stored form
 * @param e		pointer just past the end of stored form
 * @param forward	no ~ before the name?
 * @param it_out	rebuild the iterator here.
 *
 * @return 0 on success, a nonzero error code on error.
 */
int graphd_iterator_sort_thaw_loc(graphd_handle *graphd,
                                  pdb_iterator_text const *pit,
                                  pdb_iterator_base *pib, cl_loglevel loglevel,
                                  pdb_iterator **it_out, char const *file,
                                  int line) {
  pdb_handle *pdb = graphd->g_pdb;
  pdb_iterator *sub_it = NULL, *sub_check_it = NULL;
  cl_handle *cl = pdb_log(pdb);
  graphd_iterator_sort *sort;
  int err;
  bool forward;
  char const *e, *s;
  char const *state_s, *state_e;
  pdb_iterator_text subpit;
  graphd_request *greq;
  graph_idset_position pos;
  graphd_iterator_sort_storable *store = NULL;
  pdb_id resume_id = PDB_ID_NONE, idset_pos_id = PDB_ID_NONE;
  unsigned long long idset_pos_n = 0;

  /*
   * SET      := [~](SUBSET)
   * POSITION := <idset position>:<last returned>:<n in idset>
   * STATE    := [ids:@...] (SUBPOS/STATE) (SUBPOS/STATE)
   */

  /* Initialize it with zero to make a pdb_iterator_destroy() on
   * error harmless.
   */
  *it_out = NULL;

  greq = pdb_iterator_base_lookup(graphd->g_pdb, pib, "graphd.request");
  if (greq == NULL) {
    err = errno ? errno : EINVAL;
    cl_log_errno(cl, loglevel, "graphd_iterator_sort_thaw", err,
                 "failed to look up request context");
    goto err;
  }

  /*  SET
   */
  s = pit->pit_set_s;
  e = pit->pit_set_e;
  cl_assert(cl, s != NULL && e != NULL);

  err = pdb_iterator_util_thaw(
      pdb, &s, e, "%{forward}%{(bytes)}%{extensions}%{end}", &forward,
      &subpit.pit_set_s, &subpit.pit_set_e, (pdb_iterator_property *)NULL);
  if (err != 0) {
    cl_log_errno(cl, loglevel, "pdb_iterator_util_thaw", err,
                 "could not thaw set");
    return err;
  }

  memset(&pos, 0, sizeof pos);

  /* POSITION
   */
  if ((s = pit->pit_position_s) != NULL && s < (e = pit->pit_position_e)) {
    unsigned long long llu1, llu2;

    err = pdb_iterator_util_thaw(pdb, &s, e, "%{id}:%llu:%llu:%{id}:%llu",
                                 &resume_id, &llu1, &llu2, &idset_pos_id,
                                 &idset_pos_n);
    if (err != 0) {
      cl_log(cl, loglevel,
             "graphd_iterator_sort_thaw_loc: "
             "can't thaw position \"%.*s\" [from %s:%d]",
             (int)(pit->pit_position_e - pit->pit_position_s),
             pit->pit_position_s, file, line);
      return err;
    }
    pos.gip_ull = llu1;
    pos.gip_size = llu2;
  }

  /* STATE (1) - SUBITERATOR
   */
  state_s = pit->pit_state_s;
  state_e = pit->pit_state_e;
  if (state_s != NULL && state_s < state_e) {
    char const *ids_s, *ids_e;
    char const *state_s0 = state_s;

    /*  [ids:@storage] (SUBPOS/SUBSTATE)
     */
    err = pdb_iterator_util_thaw(pdb, &state_s, state_e, "[ids:@%{bytes}]",
                                 &ids_s, &ids_e);
    if (err != 0) {
      /* [ids:..] can be omitted; in that case,
       *  set ids_s and ids_e to NULL.
       */
      state_s = state_s0;
      ids_s = NULL;
      ids_e = NULL;
    }
    err = pdb_iterator_util_thaw(pdb, &state_s, state_e,
                                 "%{extensions}%{(position/state)}",
                                 (pdb_iterator_property *)NULL, &subpit);
    if (err != 0) {
      cl_log_errno(cl, loglevel, "pdb_iterator_util_thaw", err,
                   "could not thaw state: %.*s",
                   (int)(state_e - pit->pit_state_s), pit->pit_state_s);
      return err;
    }
    err =
        graphd_iterator_thaw(graphd, &subpit, pib, 0, loglevel, &sub_it, NULL);
    if (err != 0) {
      cl_log_errno(cl, loglevel, "graphd_iterator_thaw", err, "%.*s",
                   (int)(subpit.pit_set_e - subpit.pit_set_s),
                   subpit.pit_set_s);
      goto err;
    }

    if (ids_s != NULL && ids_s < ids_e)
      store = graphd_iterator_resource_thaw(graphd, &ids_s, ids_e,
                                            &sort_storable_type);

    if (resume_id == PDB_ID_NONE)
      ;
    else if (store != NULL && store->sos_idset != NULL &&
             graph_idset_check(store->sos_idset, resume_id)) {
      /*  Only clear resume_id if it's actually in the set.
       *  Otherwise, we may have recovered an idset that
       *  was halfway through a restore in its own right.
       */
      resume_id = PDB_ID_NONE;
    } else {
      /*  Rewind the subiterator; we'll
       *  have to recover the idset by pulling
       *  ids out of it.
       */
      err = pdb_iterator_reset(pdb, sub_it);
      if (err != 0) goto err;
    }
  } else {
    subpit.pit_position_s = subpit.pit_position_e = NULL;
    subpit.pit_state_s = subpit.pit_state_e = NULL;

    err =
        graphd_iterator_thaw(graphd, &subpit, pib, 0, loglevel, &sub_it, NULL);
    if (err != 0) {
      cl_log_errno(cl, loglevel, "graphd_iterator_thaw", err, "%.*s",
                   (int)(subpit.pit_set_e - subpit.pit_set_s),
                   subpit.pit_set_s);
      goto err;
    }
  }

  /* STATE (2) - CHECK
   */
  if (state_s != NULL && state_s < state_e) {
    /*  [OPT] (SUBPOS/SUBSTATE)
     */
    err = pdb_iterator_util_thaw(pdb, &state_s, state_e, "%{extensions}",
                                 (pdb_iterator_property *)NULL);
    if (err != 0) {
      cl_log_errno(cl, loglevel, "pdb_iterator_util_thaw", err,
                   "could not thaw extensions");
      return err;
    }
    err = graphd_iterator_util_thaw_partial_subiterator(
        graphd, &state_s, state_e,
        PDB_ITERATOR_FREEZE_POSITION | PDB_ITERATOR_FREEZE_STATE, &subpit, pib,
        loglevel, &sub_check_it);
    if (err != 0) {
      cl_log_errno(
          cl, loglevel, "graphd_iterator_util_thaw_partial_subiterator", err,
          "%.*s", (int)(subpit.pit_set_e - subpit.pit_set_s), subpit.pit_set_s);
      goto err;
    }
  } else {
    sub_check_it = NULL;
  }

  err = graphd_iterator_sort_create_loc(greq, forward, &sub_it, it_out, file,
                                        line);
  pdb_iterator_destroy(graphd->g_pdb, &sub_it);

  if (err != 0) {
    cl_log_errno(cl, loglevel, "graphd_iterator_sort_create_loc", err,
                 "unexpected error");
    goto err;
  }

  /* Still a sort iterator? */
  if ((*it_out)->it_type != &sort_type) {
    if (store != NULL) graphd_storable_unlink(store);

    pdb_iterator_destroy(pdb, &sub_check_it);
    return 0;
  }

  sort = (*it_out)->it_theory;
  sort->sort_sub_check = sub_check_it;
  sub_check_it = NULL;

  /* Recover our horizon
   */
  err = update_horizon(*it_out);
  if (err != 0) goto err;

  sort->sort_idset_resume = resume_id;
  sort->sort_idset_storable = store;
  if (store != NULL) {
    /*  Replace the idset with our recovered one.
     */
    graph_idset_free(sort->sort_idset);
    sort->sort_idset = store->sos_idset;
    sort->sort_idset_last_added = store->sos_idset_last_added;
  }

  sort->sort_idset_pos_n = idset_pos_n;
  sort->sort_idset_pos_id = idset_pos_id;
  sort->sort_idset_pos = pos;

  /*  Recover our statistics informaiton, if it's cheap.
   */
  if (pdb_iterator_statistics_done(pdb, osort(*it_out)->sort_sub)) {
    bool forward;

    /*  Inherit results from the subiterator, except for our
     *  sortedness.
     */
    forward = pdb_iterator_forward(pdb, *it_out);
    pdb_iterator_statistics_copy(pdb, *it_out, osort(*it_out)->sort_sub);
    pdb_iterator_forward_set(pdb, *it_out, forward);
  }
  return 0;

err:
  pdb_iterator_destroy(pdb, it_out);
  pdb_iterator_destroy(pdb, &sub_check_it);
  pdb_iterator_destroy(pdb, &sub_it);
  if (store != NULL) graphd_storable_unlink(store);

  cl_log(cl, CL_LEVEL_VERBOSE, "graphd_iterator_sort_thaw: error %s",
         graphd_strerror(err));
  return err;
}


text:
package com.leonti.slickpm.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.leonti.slickpm.domain.TaskType;

@Service("TaskTypeService")
@Transactional
public class TaskTypeServiceImpl implements TaskTypeService {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public TaskType save(TaskType taskType) {
		sessionFactory.getCurrentSession().saveOrUpdate(taskType);
		return taskType;
	}

	@Override
	public void delete(TaskType taskType) {
		sessionFactory.getCurrentSession().delete(taskType);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TaskType> getList() {

		return (ArrayList<TaskType>) sessionFactory.getCurrentSession()
				.createQuery("FROM TaskType").list();
	}

	@Override
	public TaskType getById(Integer id) {

		return (TaskType) sessionFactory.getCurrentSession()
				.createQuery("FROM TaskType t WHERE t.id = ?").setLong(0, id)
				.setMaxResults(1).uniqueResult();
	}
}

redpajama_set_name: RedPajamaGithub


text:
class AuthEngine::UserSessionsController < AuthEngine::ApplicationController

  #
  # creates user records in the consumer applications
  #
  def create
    #request.env['omniauth.auth'] contains the Authentication Hash with all the data about a user.

    # <OmniAuth::AuthHash
    #  credentials=#<OmniAuth::AuthHash expires=true expires_at=1466038838 token="1c8a2ac55728364f9fce93fa005c0b4d3a6f56108790927d4586e5ec409a1dd8">
    #  extra=#<OmniAuth::AuthHash raw_user_info=#<OmniAuth::AuthHash admin=true city_id=nil created_at="2016-06-13T23:40:13.893Z" email="remyflatt@gmail.com" id=4 updated_at="2016-06-15T23:00:38.225Z" user_service_key=4>>
    #  info=#<OmniAuth::AuthHash::InfoHash admin=true email="remyflatt@gmail.com">
    #  provider="auth_engine"
    #  uid=4>

    omniauth = env['omniauth.auth']
    user_service_key = omniauth['uid'] #User's unique identifier

    user = User.find_or_create_by(user_service_key: user_service_key)

    Rails.cache.write("/users/#{user.user_service_key}", omniauth)

    user.save

    # set session values
    session[:user_service_key] = user.user_service_key
    session[:access_token] = omniauth.credentials.token

    # get token info
    #  HTTParty.get('http://localhost:3000/oauth/token/info?access_token=1c8a2ac55728364f9fce93fa005c0b4d3a6f56108790927d4586e5ec409a1dd8')

    redirect_to request.env['omniauth.origin'] || root_path
  end

  # Omniaytg failure callback
  def failure
    flash[:notice] = params[:message]
    redirect_to root_path
  end

  # logout - Clear our rack session BUT essentially redirect to the provider
  # to clean up the Devise session from there too !
  def destroy
    reset_session
    flash[:notice] = 'You have successfully signed out!'
    redirect_to "#{ENV['AUTH_PROVIDER_URL']}/users/sign_out"
  end
end


text:
<?php namespace Arcanesoft\Seo\Providers;

use Arcanedev\Support\Providers\AuthorizationServiceProvider as ServiceProvider;
use Arcanesoft\Seo\Policies\DashboardPolicy;
use Arcanesoft\Seo\Policies\FootersPolicy;
use Arcanesoft\Seo\Policies\MetasPolicy;
use Arcanesoft\Seo\Policies\PagesPolicy;
use Arcanesoft\Seo\Policies\RedirectsPolicy;

/**
 * Class AuthorizationServiceProvider
 *
 * @package  Arcanesoft\Seo\Providers
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class AuthorizationServiceProvider extends ServiceProvider
{
    /* -----------------------------------------------------------------
     |  Main Methods
     | -----------------------------------------------------------------
     */

    /**
     * Register any application authentication / authorization services.
     */
    public function boot()
    {
        parent::registerPolicies();

        $this->defineMany(DashboardPolicy::class, DashboardPolicy::policies());
        $this->defineMany(PagesPolicy::class,     PagesPolicy::policies());
        $this->defineMany(FootersPolicy::class,   FootersPolicy::policies());
        $this->defineMany(MetasPolicy::class,     MetasPolicy::policies());
        $this->defineMany(RedirectsPolicy::class, RedirectsPolicy::policies());
    }
}


text:
Model::Model()
{

}

Model::Model(std::vector<ISceneObject*> polygons):polygons_(polygons)
{

}

std::vector<ISceneObject*>&Model::GetGeometry()
{
    return polygons_;
}

void Model::ApplyTransformation(Transform A)
{
    for(auto obj: this->GetGeometry())
    {
        obj->ApplyTransform(A);
    }
}

void Model::ApplyMaterial(IMaterial* material)
{
    for(auto obj: polygons_)
    {
        obj->ApplyOutterMaterial(material);
        obj->ApplyInnerMaterial(material);
    }
}


text:
module MomentsHelper
end


text:
<?php
	$jadwalid 			= $jadwal->jadwalid;
if($this->input->post('is_submitted')){
	$nama			= $this->input->post('nama');
	$tanggal	    = $this->input->post('tanggal');
	$hari 		    = $this->input->post('hari');
	$jam			= $this->input->post('jam');
} else {
	$nama			= $jadwal->namaj;
	$tanggal		= $jadwal->tanggalj;
	$hari			= $jadwal->harij;
	$jam			= $jadwal->jamj;
}
?>
<!doctype html>

		<title>dosen Page | Edit jadwal</title>

		<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.css">

Edit Jadwal

<?= validation_errors() ?>

				<?= form_open_multipart('dosen/jadwal/update/' . $jadwalid, ['class'=>'form-horizontal']) ?>

						Nama

						  <input type="text" class="form-control" name="nama" placeholder="" value="<?= $nama ?>">

						tanggal

						   <input type="date" class="form-control" name="tanggal" placeholder="" value="<?= $tanggal ?>">

						hari

						  <input type="text" class="form-control" name="hari" placeholder="" value="<?= $hari ?>">

						jam

						  <input type="time" class="form-control" name="jam" placeholder="" value="<?= $jam ?>">

						  <input type="hidden" name="is_submitted" value="1" />
						  <button type="submit" class="btn btn-default">Save</button>

				<?= form_close() ?>


text:
If the current default version is broken

## Find the target version to rollback to

Check the [list of default versions](https://docs.google.com/spreadsheets/d/1MyxMZDSPedahmdh9EHGRxFdNMwVfBD4cB4jYGWuG6FM)
of the Perf Dashboard. The current version should be at the top of the
list, and the previous version should be directly under it.

You should rollback to the previous version. Please add that version to
the top of the list after you rollback to it.

## Rollback to the previous version

Find the target version in the [app engine versions
list](https://appengine.google.com/deployment?&app_id=s~chromeperf).
Select the checkbox next to it, and then hit the "Migrate Traffic" button.


text:
[![Netlify Status](https://api.netlify.com/api/v1/badges/0543ef53-e923-4a99-98f0-33f20b8641fa/deploy-status)](https://app.netlify.com/sites/boring-swanson-cc9834/deploys)

A super-fast ASCII title generator.

- Vue front-end app.
- Webpack for building.
- Hosted on Netlify.
- SSL Certificate provided by Let's Encrypt and generated via Netlify admin.
- Domain name registered at Name.com

Hude kudos to the original font creators and patorjk who wrote the [figlet.js](https://github.com/patorjk/figlet.js) font parser.


text:
require 'concurrent/utility/monotonic_time'
require 'concurrent/channel/tick'
require 'concurrent/channel/buffer/base'

module Concurrent
  class Channel
    module Buffer

      class Ticker < Base

        def initialize(interval)
          super()
          synchronize do
            @interval = interval.to_f
            @next_tick = Concurrent.monotonic_time + interval
          end
        end

        def size() 1; end

        def empty?() false; end

        def full?() true; end

        def put(item)
          false
        end

        def offer(item)
          false
        end

        def take
          loop do
            result, _ = do_poll
            if result.nil?
              return NO_VALUE
            elsif result != NO_VALUE
              return result
            end
          end
        end

        def next
          loop do
            result, _ = do_poll
            if result.nil?
              return NO_VALUE, false
            elsif result != NO_VALUE
              return result, true
            end
          end
        end

        def poll
          result, _ = do_poll
          if result.nil? || result == NO_VALUE
            NO_VALUE
          else
            result
          end
        end

        private

        def do_poll
          if ns_closed?
            return nil, false
          elsif (now = Concurrent.monotonic_time) > @next_tick
            tick = Concurrent::Channel::Tick.new(@next_tick)
            @next_tick = now + @interval
            return tick, true
          else
            return NO_VALUE, true
          end
        end
      end
    end
  end
end


text:
namespace gfx {

// gfx can't depend upon aura, otherwise we have circular dependencies. So,
// gfx::Screen is pluggable for aura and Desktop plugs in the real
// implementation.

// static
Screen* Screen::instance_ = NULL;

// static
void Screen::SetInstance(Screen* screen) {
  delete instance_;
  instance_ = screen;
}

// static
gfx::Point Screen::GetCursorScreenPoint() {
  return instance_->GetCursorScreenPointImpl();
}

// static
gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeWindow window) {
  return instance_->GetMonitorWorkAreaNearestWindowImpl(window);
}

// static
gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeWindow window) {
  return instance_->GetMonitorAreaNearestWindowImpl(window);
}

// static
gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) {
  return instance_->GetMonitorWorkAreaNearestPointImpl(point);
}

// static
gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) {
  return instance_->GetMonitorAreaNearestPointImpl(point);
}

// static
gfx::Rect Screen::GetPrimaryMonitorWorkArea() {
  return instance_->GetMonitorWorkAreaNearestPoint(gfx::Point());
}

// static
gfx::Rect Screen::GetPrimaryMonitorBounds() {
  return instance_->GetMonitorAreaNearestPoint(gfx::Point());
}

// static
gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) {
  return instance_->GetMonitorWorkAreaNearestPoint(gfx::Point());
}

// static
gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
  return instance_->GetWindowAtCursorScreenPointImpl();
}

// static
gfx::Size Screen::GetPrimaryMonitorSize() {
  return instance_->GetPrimaryMonitorSizeImpl();
}

// static
int Screen::GetNumMonitors() {
  return instance_->GetNumMonitorsImpl();
}

}  // namespace gfx


text:
package ru.histone.evaluator.nodes;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * Class representing undefined type in Histone
 */
public class UndefinedNode extends Node {

    protected UndefinedNode(NodeFactory nodeFactory) {
        super(nodeFactory);
    }

    @Override
    public Node oper_add(Node right) {
        if (right.isNumber() || right.isUndefined()) {
            return getNodeFactory().UNDEFINED;
        } else {
            return this.getAsString().oper_add(right.getAsString());
        }
    }

    @Override
    public Node oper_mul(Node right) {
        return getNodeFactory().UNDEFINED;
    }

    @Override
    public Node oper_div(Node right) {
        return getNodeFactory().UNDEFINED;
    }

    @Override
    public Node oper_mod(Node right) {
        return getNodeFactory().UNDEFINED;
    }

    @Override
    public Node oper_negate() {
        return getNodeFactory().UNDEFINED;
    }

    @Override
    public Node oper_sub(Node right) {
        return getNodeFactory().UNDEFINED;
    }

    @Override
    public Node oper_not() {
        return getNodeFactory().TRUE;
    }

    @Override
    public Node oper_equal(Node right) {
        return (this.getAsBoolean() == right.getAsBoolean()) ? getNodeFactory().TRUE : getNodeFactory().FALSE;
    }

    @Override
    public Node oper_greaterThan(Node right) {
        return this.getAsBoolean().oper_greaterThan(right.getAsBoolean());
    }

    @Override
    public Node oper_greaterOrEqual(Node right) {
        return this.getAsBoolean().oper_greaterOrEqual(right.getAsBoolean());
    }

    @Override
    public Node oper_lessThan(Node right) {
        return this.getAsBoolean().oper_lessThan(right.getAsBoolean());
    }

    @Override
    public Node oper_lessOrEqual(Node right) {
        return this.getAsBoolean().oper_lessOrEqual(right.getAsBoolean());
    }

    @Override
    public BooleanHistoneNode getAsBoolean() {
        return getNodeFactory().FALSE;
    }

    @Override
    public NumberHistoneNode getAsNumber() {
        return getNodeFactory().UNDEFINED_NUMBER;
    }

    @Override
    public StringHistoneNode getAsString() {
        return getNodeFactory().string("");
    }

    @Override
    public ObjectHistoneNode getAsObject() {
        throw new RuntimeException("Can't cast " + getClass() + " to object");
    }

    @Override
    public JsonNode getAsJsonNode() {
        return getNodeFactory().NULL.getAsJsonNode();
    }

    @Override
    public String toString() {
        return "undefined()";
    }

}


text:
import React from 'react';
import renderer from 'react-test-renderer';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { cardActions, cardReducer, CardBuilder } from 'cf-builder-card';
import { mount } from 'enzyme';

let store;

beforeEach(() => {
  store = createStore(
    combineReducers({
      cards: cardReducer
    })
  );
});

test('should render a card', () => {
  const component = renderer.create(
    <Provider store={store}>
      <CardBuilder
        cardName="test-card"
        title="This is a Card"
        description="This is the description of a card."
      />
    </Provider>
  );
  expect(component.toJSON()).toMatchSnapshot();
});

test('should render the description with markdown', () => {
  const wrapper = mount(
    <Provider store={store}>
      <CardBuilder
        cardName="test-card"
        title="This is a Card"
        description="This is the *description* of a card."
      />
    </Provider>
  );

  const description = wrapper
    .find('.cf-card__content')
    .find('.cf-dynamic-content')
    .render() // entering dynamic content
    .find('p')
    .html();

  expect(description).toBe('This is the description of a card.');
});

test('should render button', () => {
  const button = <button>Click Me</button>;
  const component = renderer.create(
    <Provider store={store}>
      <CardBuilder
        cardName="test-card"
        title="This is a Card"
        description="This is the *description* of a card."
        control={button}
      />
    </Provider>
  );
  expect(component.toJSON()).toMatchSnapshot();
});

test('should render a table', () => {
  const table =
;
  const component = renderer.create(
    <Provider store={store}>
      <CardBuilder
        cardName="test-card"
        title="This is a Card"
        description="This is the *description* of a card."
        table={table}
      />
    </Provider>
  );
  expect(component.toJSON()).toMatchSnapshot();
});

test('should render drawers', () => {
  const component = renderer.create(
    <Provider store={store}>
      <CardBuilder
        cardName="test-card"
        title="This is a Card"
        description="This is the *description* of a card."
        drawers={[
          {
            id: 'help',
            name: 'Help',
            content: 'Help Content'
          },
          {
            id: 'api',
            name: 'API',
            content: 'API Content'
          }
        ]}
      />
    </Provider>
  );
  expect(component.toJSON()).toMatchSnapshot();
});


text:
package apple.security.protocol;

import org.moe.natj.general.ann.Generated;
import org.moe.natj.general.ann.Library;
import org.moe.natj.general.ann.Runtime;
import org.moe.natj.objc.ObjCRuntime;
import org.moe.natj.objc.ann.ObjCProtocolName;

@Generated
@Library("Security")
@Runtime(ObjCRuntime.class)
@ObjCProtocolName("OS_sec_identity")
public interface OS_sec_identity {
}


text:
package org.restler.http.security.authorization;

import java.util.Base64;

public class BasicAuthorizationStrategy implements AuthorizationStrategy {

    private String loginValue;
    private String passwordValue;

    public BasicAuthorizationStrategy(String login, String password) {
        loginValue = login;
        passwordValue = password;
    }

    @Override
    public Object authorize() {
        String loginPassword = loginValue + ":" + passwordValue;

        return Base64.getMimeEncoder().encodeToString(loginPassword.getBytes());
    }
}


text:
\section{Tools used}
\label{sec:tools}

\verb|Python| was our language of choice to implement our tool to predict if a
tweet will be retweeted or not. Multiple libraries were used:
\begin{itemize}
	\item matplotlib: to plot 2D graphs of the ROC curve.
	\item nltk: for stemming and various language processing functions and some
		  classifiers.
	\item numpy: to use special array objects.
	\item scipy: for some numerical routines.
	\item scikit-learn: for some classifiers.
	\item twython: to extract tweets from Twitter using their API.
\end{itemize}

Due to some libraries being not compatible with \verb|Python 3|, we had to
stick with \verb|Python 2|.

We were able to develop a comprehensive tool that is easy to extend by taking
advantage of object oriented programming. Adding new features or new
classifiers can be done very easily thanks to the concept of inheritance.

The figure \ref{fig:architecture} shows the global architecture of the
interesting part of the code.

\begin{figure}[!h]
\centering
 \includegraphics[width=0.9\columnwidth]{img/architecture.pdf}
\caption{Source code architecture}
\label{fig:architecture}
\end{figure}

Our tool is able to perform various tasks such as classification using numerous
classifiers and features, algorithm tournament and cross-validation. It is also
able to plot ROC curve and Decision Trees, randomizing the dataset and taking
the advantage of multiprocessing for performing hard computing tasks.

In order to let the scientific community, or any people, use it, we released it
under the terms of the permissive BSD license. Being written in \verb|Python|,
it is available for any operating system on which \verb|Python| can run. We,
however, only tested it on Linux.


text:
Helm chart for Aleph

## Values

### Global configs

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| global.amazon | bool | `true` | Are we using AWS services like s3? |
| global.google | bool | `false` | Are we using GCE services like storage, vision api? |
| global.image.repository | string | `"alephdata/aleph"` | Aleph docker image repo |
| global.image.tag | string | `"3.11.1"` | Aleph docker image tag |
| global.image.tag | string | `"Always"` |  |
| global.namingPrefix | string | `"aleph"` | Prefix for the names of k8s resources |

### Environment variables

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| global.env.ALEPH_API_RATE_LIMIT | string | `"600"` | API rate limiting (req/min for anonymous users) |
| global.env.ALEPH_APP_NAME | string | `"aleph"` | App name |
| global.env.ALEPH_APP_TITLE | string | `"Aleph"` | App title |
| global.env.ALEPH_CACHE | string | `"true"` |  |
| global.env.ALEPH_DEBUG | string | `"false"` |  |
| global.env.ALEPH_ELASTICSEARCH_URI | string | `"http://aleph-index-master.default.svc.cluster.local:9200"` |  |
| global.env.ALEPH_FORCE_HTTPS | string | `"false"` |  |
| global.env.ALEPH_INDEX_PREFIX | string | `"aleph"` |  |
| global.env.ALEPH_INDEX_READ | string | `"v1:v2"` |  |
| global.env.ALEPH_INDEX_REPLICAS | string | `"2"` |  |
| global.env.ALEPH_INDEX_WRITE | string | `"v2"` |  |
| global.env.ALEPH_OAUTH | string | `"false"` | Enable OAuth login? |
| global.env.ALEPH_PAGES_PATH | string | `"/aleph/site/aleph.occrp.org/pages"` |  |
| global.env.ALEPH_PASSWORD_LOGIN | string | `"true"` | Enable password login? |
| global.env.ALEPH_UI_URL | string | `"http://localhost"` |  |
| global.env.ELASTICSEARCH_TIMEOUT | string | `"600"` | Default elasticsearch timeout |
| global.env.REDIS_URL | string | `"redis://aleph-redis-master.default.svc.cluster.local:6379/0"` | Redis url |

Checkout [https://docs.alephdata.org/developers/installation#configuration](https://docs.alephdata.org/developers/installation#configuration) for all available options.


text:
package com.github.e2point718.har2jmx;

import java.io.IOException;

import org.json.JSONException;

import junit.framework.TestCase;

public class HARBuildTest extends TestCase {

	public void testInvalidJSON(){
		try {
			HAR.build("test");
			fail("Invalid HAR JSON object should throw JSONException");
		} catch (JSONException e) {
		}
	}

	public void testInvalidJSONWithoutLog(){
		try {
			HAR.build("{}");
			fail("Invalid HAR JSON object should throw JSONException");
		} catch (JSONException e) {
		}
	}

	public void testJSONWithoutPagesOrEntries(){
		assertNotNull(HAR.build("{\"log\": {}}"));
	}

	public void test_har_simple_get() throws IOException{
		HAR har = HAR.build(HARBuildTest.class.getResource("/har_simple_get.json"));
		assertNotNull(har);
		assertEquals(1, har.pages.length);
		assertEquals(18, har.entries.length);
		assertNotNull(har.entries[0].pageref);
	}

	public void test_har_simple_post() throws IOException{
		HAR har = HAR.build(HARBuildTest.class.getResource("/har_simple_post.json"));
		assertNotNull(har);
		assertEquals(1, har.pages.length);
		assertEquals(27, har.entries.length);
		assertNotNull(har.entries[0].pageref);
	}
}


text:
using System;
using System.Collections.Generic;
using Bedrock.Modularity;

namespace Bedrock.Tests.Mocks
{
    public class MockModuleTypeLoader : IModuleTypeLoader
    {
        public List<ModuleInfo> LoadedModules = new List<ModuleInfo>();
        public bool canLoadModuleTypeReturnValue = true;
        public Exception LoadCompletedError;

        public bool CanLoadModuleType(ModuleInfo moduleInfo)
        {
            return canLoadModuleTypeReturnValue;
        }

        public void LoadModuleType(ModuleInfo moduleInfo)
        {
            this.LoadedModules.Add(moduleInfo);
            this.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, this.LoadCompletedError));
        }

        public event EventHandler<ModuleDownloadProgressChangedEventArgs> ModuleDownloadProgressChanged;

        public void RaiseLoadModuleProgressChanged(ModuleDownloadProgressChangedEventArgs e)
        {
            if (this.ModuleDownloadProgressChanged != null)
            {
                this.ModuleDownloadProgressChanged(this, e);
            }
        }

        public event EventHandler<LoadModuleCompletedEventArgs> LoadModuleCompleted;

        public void RaiseLoadModuleCompleted(ModuleInfo moduleInfo, Exception error)
        {
            this.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, error));
        }

        public void RaiseLoadModuleCompleted(LoadModuleCompletedEventArgs e)
        {
            if (this.LoadModuleCompleted != null)
            {
                this.LoadModuleCompleted(this, e);
            }
        }
    }
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Statistics of xcomp in UD_Sanskrit-UFAL</title>
    <link rel="root" href=""/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/jquery-ui-redmond.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style-vis.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/hint.css"/>

          home

          edit page
          issue tracker

          <gcse:search></gcse:search>

        This page pertains to UD version 2.

            It appears that you have Javascript disabled.
            Please consider enabling Javascript for this page to see the visualizations.

Treebank Statistics: UD_Sanskrit-UFAL: Relations: xcomp

This relation is universal.

17 nodes (1%) are attached to their parents as xcomp.

13 instances of xcomp (76%) are right-to-left (child precedes parent).
Average distance between parent and child is 1.82352941176471.

The following 3 pairs of parts of speech are connected with xcomp: <tt>VERB</tt>-<tt>NOUN</tt> (10; 59% instances), <tt>VERB</tt>-<tt>ADJ</tt> (5; 29% instances), <tt>VERB</tt>-<tt>VERB</tt> (2; 12% instances).

# visual-style 5	bgColor:blue
# visual-style 5	fgColor:white
# visual-style 6	bgColor:blue
# visual-style 6	fgColor:white
# visual-style 6 5 xcomp	color:blue
1	तत्	तद्	PRON	_	Case=Nom|Gender=Neut|Number=Sing|PronType=Dem	6	obl	_	Translit=tat|LTranslit=tad|Gloss=thus
2	एषा	एतद्	DET	_	Case=Nom|Gender=Fem|Number=Sing|PronType=Dem	6	nsubj	_	Translit=eṣā|LTranslit=etad|Gloss=this
3	कदाचिद्	कदा-चिद्	ADV	_	PronType=Int	6	advmod	_	Translit=kadācid|LTranslit=kadā-cid|Gloss=perhaps
4	वैश्या	वैश्या	NOUN	_	Compound=Yes	5	nmod:poss	_	Translit=vaiśyā|LTranslit=vaiśyā|Gloss=worker
5	सुता	सुत	NOUN	_	Case=Nom|Gender=Fem|Number=Sing	6	xcomp	_	Translit=sutā|LTranslit=suta|Gloss=daughter
6	भविष्यति	भू	VERB	_	Mood=Ind|Number=Sing|Person=3|Tense=Fut|VerbForm=Fin|Voice=Act	0	root	_	SpaceAfter=No|Translit=bhaviṣyati|LTranslit=bhū|Gloss=will-be
7	।	।	PUNCT	_	_	6	punct	_	Translit=.|LTranslit=.|Gloss=.

# visual-style 4	bgColor:blue
# visual-style 4	fgColor:white
# visual-style 5	bgColor:blue
# visual-style 5	fgColor:white
# visual-style 5 4 xcomp	color:blue
1	अथ	अथ	ADV	_	_	10	advmod	_	Translit=atha|LTranslit=atha|Gloss=thus
2	तान्	तद्	PRON	_	Case=Acc|Gender=Masc|Number=Plur|PronType=Dem	3	det	_	Translit=tān|LTranslit=tad|Gloss=those
3	शास्त्र	शास्त्र	NOUN	_	Compound=Yes	4	obl	_	Translit=śāstra|LTranslit=śāstra|Gloss=education
4	विमुखान्	विमुख	ADJ	_	Case=Acc|Gender=Masc|Number=Plur	5	xcomp	_	Translit=vimukhān|LTranslit=vimukha|Gloss=disinclined
5	समालोक्य	समालोक्	VERB	_	VerbForm=Conv	10	advcl	_	Translit=samālokya|LTranslit=samālok|Gloss=after-having-perceived
6	स	तद्	DET	_	Case=Nom|Gender=Masc|Number=Sing|PronType=Dem	7	det	_	Translit=sa|LTranslit=tad|Gloss=that
7	राजा	राजन्	NOUN	_	Case=Nom|Gender=Masc|Number=Sing	10	nsubj	_	Translit=rājā|LTranslit=rājan|Gloss=king
8	सचिवान्	सचिव	NOUN	_	Case=Acc|Gender=Masc|Number=Plur	9	obj	_	Translit=sacivān|LTranslit=saciva|Gloss=ministers
9	आहूय	आहूय	VERB	_	VerbForm=Conv	10	advcl	_	Translit=āhūya|LTranslit=āhūya|Gloss=after-having-summoned
10	प्रोवाच	प्रो-वच्	VERB	_	Aspect=Perf|Mood=Ind|Number=Sing|Person=3|Tense=Past|VerbForm=Fin|Voice=Act	0	root	_	Translit=provāca|LTranslit=pro-vac|Gloss=said
11	।	।	PUNCT	_	_	10	punct	_	Translit=.|LTranslit=.|Gloss=.

# visual-style 7	bgColor:blue
# visual-style 7	fgColor:white
# visual-style 8	bgColor:blue
# visual-style 8	fgColor:white
# visual-style 8 7 xcomp	color:blue
1	एतस्मिन्	एतद्	DET	_	Case=Loc|Gender=Neut|Number=Sing|PronType=Dem	2	det	_	Translit=etasmin|LTranslit=etad|Gloss=this
2	अन्तरे	अन्तर	NOUN	_	Case=Loc|Gender=Neut|Number=Sing	8	obl	_	Translit=antare|LTranslit=antara|Gloss=place
3	ते	तद्	DET	_	Case=Nom|Gender=Masc|Number=Plur|PronType=Dem	4	det	_	Translit=te|LTranslit=tad|Gloss=the
4	वानराः	वानर	NOUN	_	Case=Nom|Gender=Masc|Number=Plur	8	nsubj	_	Translit=vānarāḥ|LTranslit=vānara|Gloss=monkeys
5	यथा	यथा	ADV	_	PronType=Rel	8	advmod	_	Translit=yathā|LTranslit=yathā|Gloss=so
6	इच्छया	इच्छा	NOUN	_	Case=Ins|Gender=Fem|Number=Sing	8	obl	_	Translit=icchayā|LTranslit=icchā|Gloss=by-desire
7	क्रीडितुम्	क्रीड्	VERB	_	VerbForm=Inf	8	xcomp	_	Translit=krīḍitum|LTranslit=krīḍ|Gloss=play
8	आरब्धम्	आ-रब्ध्	VERB	_	Case=Nom|Gender=Neut|Number=Sing|Tense=Past|VerbForm=Part|Voice=Pass	0	root	_	SpaceAfter=No|Translit=ārabdham|LTranslit=ā-rabdh|Gloss=took
9	।	।	PUNCT	_	_	8	punct	_	Translit=.|LTranslit=.|Gloss=.

© 2014–2021
            Universal Dependencies contributors.
            Site powered by Annodoc and brat
.


text:
#include <common.h>
#include <mpc8xx.h>

/*-----------------------------------------------------------------------
 * Process Hardware Information Block:
 *
 * If we boot on a system fresh from factory, check if the Hardware
 * Information Block exists and save the information it contains.
 *
 * The TQM8xxL / TQM82xx Hardware Information Block is defined as
 * follows:
 * - located in first flash bank
 * - starts at offset 0x0003FFC0
 * - size 0x00000040
 *
 * Internal structure:
 * - sequence of ASCII character strings
 * - fields separated by a single space character (0x20)
 * - last field terminated by NUL character (0x00)
 * - remaining space filled with NUL characters (0x00)
 *
 * Fields in Hardware Information Block:
 * 1) Module Type
 * 2) Serial Number
 * 3) First MAC Address
 * 4) Number of additional MAC addresses
 */

void load_sernum_ethaddr (void)
{
	unsigned char *hwi;
	unsigned char  serial [CONFIG_SYS_HWINFO_SIZE];
	unsigned char  ethaddr[CONFIG_SYS_HWINFO_SIZE];
	unsigned short ih, is, ie, part;

	hwi = (unsigned char *)(CONFIG_SYS_FLASH_BASE + CONFIG_SYS_HWINFO_OFFSET);
	ih = is = ie = 0;

	if (*((unsigned long *)hwi) != (unsigned long)CONFIG_SYS_HWINFO_MAGIC) {
		return;
	}

	part = 1;

	/* copy serial # / MAC address */
	while ((hwi[ih] != '\0') && (ih < CONFIG_SYS_HWINFO_SIZE)) {
		if (hwi[ih] < ' ' || hwi[ih] > '~') { /* ASCII strings! */
			return;
		}
		switch (part) {
		default:		/* Copy serial # */
			if (hwi[ih] == ' ') {
				++part;
			}
			serial[is++] = hwi[ih];
			break;
		case 3:			/* Copy MAC address */
			if (hwi[ih] == ' ') {
				++part;
				break;
			}
			ethaddr[ie++] = hwi[ih];
			if ((ie % 3) == 2)
				ethaddr[ie++] = ':';
			break;
		}
		++ih;
	}
	serial[is]  = '\0';
	if (ie && ethaddr[ie-1] == ':')
		--ie;
	ethaddr[ie] = '\0';

	/* set serial# and ethaddr if not yet defined */
	if (getenv("serial#") == NULL) {
		setenv ((char *)"serial#", (char *)serial);
	}

	if (getenv("ethaddr") == NULL) {
		setenv ((char *)"ethaddr", (char *)ethaddr);
	}
}