text:
#define _POSIX_C_SOURCE 200809L
#define _BSD_SOURCE 1
#define _GNU_SOURCE 1

#include <assert.h>
#include <errno.h>
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <math.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

#include "lmdb.h"
#include "xxhash.h"

#include "page_db.h"
#include "util.h"
#include "scheduler.h"
#include "bf_scheduler.h"
#include "scorer.h"
#include "page_rank_scorer.h"
#include "hits_scorer.h"

static void
bf_scheduler_set_error(BFScheduler *sch, int code, const char *message) {
     error_set(sch->error, code, message);
}

static void
bf_scheduler_add_error(BFScheduler *sch, const char *message) {
     error_add(sch->error, message);
}

BFSchedulerError
bf_scheduler_new(BFScheduler **sch, PageDB *db, const char *path) {
     BFScheduler *p = *sch = calloc(1, sizeof(*p));
     if (!p ||
         !(p->error         = error_new()) ||
         !(p->scorer        = calloc(1, sizeof(*p->scorer))) ||
         !(p->update_thread = calloc(1, sizeof(*p->update_thread)))) {

          free(p->update_thread);
          free(p->scorer);
          free(p->error);
          free(p);

          return bf_scheduler_error_memory;
     }
     char *error = 0;
     int rc;
     if ((rc = pthread_mutex_init(&p->update_thread->state_mutex, 0)) != 0)
          error = "initializing update thread mutex";
     else if ((rc = pthread_mutex_init(&p->update_thread->wait_mutex, 0)) != 0)
          error = "initializing n_pages mutex";
     else if ((rc = pthread_cond_init(&p->update_thread->wait_cond, 0)) != 0)
          error = "initializing n_pages cond";

     if (error != 0) {
          bf_scheduler_set_error(p, bf_scheduler_error_thread, __func__);
          bf_scheduler_add_error(p, error);
          bf_scheduler_add_error(p, strerror(rc));
          return 0;

     }
     p->update_thread->state = update_thread_none;
     p->update_thread->n_pages_old = 0.0;
     p->update_thread->n_pages_new = 0.0;

     p->page_db = db;
     p->persist = BF_SCHEDULER_DEFAULT_PERSIST;
     p->max_soft_domain_crawl_rate = -1.0;
     p->max_hard_domain_crawl_rate = -1.0;
     p->max_crawl_depth = 0;

     p->path = path? path: concat(db->path, "bfs", '_');
     if (!p->path)
          error = "building scheduler path";
     else
          error = make_dir(p->path);

     if (error != 0) {
          bf_scheduler_set_error(p, bf_scheduler_error_invalid_path, __func__);
          bf_scheduler_add_error(p, error);
          return p->error->code;
     }

     if (txn_manager_new(&p->txn_manager, 0) != 0) {
          bf_scheduler_set_error(p, bf_scheduler_error_internal, __func__);
          bf_scheduler_add_error(p, p->txn_manager?
                                 p->txn_manager->error->message:
                                 "NULL");
          return p->error->code;
     }

     // initialize LMDB on the directory
     if ((rc = mdb_env_create(&p->txn_manager->env) != 0))
          error = "creating environment";
     else if ((rc = mdb_env_set_mapsize(p->txn_manager->env,
                                        BF_SCHEDULER_DEFAULT_SIZE)) != 0)
          error = "setting map size";
     else if ((rc = mdb_env_set_maxdbs(p->txn_manager->env, 1)) != 0)
          error = "setting number of databases";
     else if ((rc = mdb_env_open(
                    p->txn_manager->env,
                    p->path,
                    MDB_NOTLS | MDB_NOSYNC, 0664) != 0))
          error = "opening environment";

     if (error != 0) {
          bf_scheduler_set_error(p, bf_scheduler_error_internal, __func__);
          bf_scheduler_add_error(p, error);
          bf_scheduler_add_error(p, mdb_strerror(rc));
          return p->error->code;
     }

     return 0;
}

static int
bf_scheduler_open_cursor(MDB_txn *txn, MDB_cursor **cursor) {
     MDB_dbi dbi;
     int mdb_rc =
          mdb_dbi_open(txn, "schedule", MDB_CREATE, &dbi) ||
          mdb_set_compare(txn, dbi, schedule_entry_mdb_cmp_desc) ||
          mdb_cursor_open(txn, dbi, cursor);

     if (mdb_rc != 0)
          *cursor = 0;

     return mdb_rc;
}

static BFSchedulerError
bf_scheduler_expand(BFScheduler *sch) {
     if (txn_manager_expand(sch->txn_manager, 0) != 0) {
          bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
          bf_scheduler_add_error(sch, sch->txn_manager->error->message);
     }
     return sch->error->code;
}

BFSchedulerError
bf_scheduler_add(BFScheduler *sch, const CrawledPage *page) {
     if (bf_scheduler_expand(sch) != 0)
          return sch->error->code;

     char *error1 = 0;
     char *error2 = 0;

     MDB_txn *txn = 0;
     MDB_cursor *cur = 0;

     PageInfoList *pil;
     if (page_db_add(sch->page_db, page, &pil) != 0) {
          error1 = "adding crawled page";
          error2 = sch->page_db->error->message;
          goto on_error;
     }

     int rc = 0;
     if ((rc = pthread_mutex_lock(&sch->update_thread->wait_mutex)) != 0)
          error1 = "locking n_pages mutex";
     else {
          sch->update_thread->n_pages_new += 1.0;
          if ((rc = pthread_cond_broadcast(&sch->update_thread->wait_cond)) != 0)
               error1 = "broadcasting n_pages signal";
          else if ((rc = pthread_mutex_unlock(&sch->update_thread->wait_mutex)) != 0)
               error1 = "unlocking n_pages mutex";
     }
     if (error1 != 0) {
          error2 = strerror(rc);
          goto on_error;
     }

     if (txn_manager_begin(sch->txn_manager, 0, &txn) != 0) {
          error1 = "starting transaction";
          error2 = sch->txn_manager->error->message;
          goto on_error;
     }
     int mdb_rc = bf_scheduler_open_cursor(txn, &cur);
     if (mdb_rc != 0) {
          error1 = "opening cursor";
          error2 = mdb_strerror(mdb_rc);
          goto on_error;
     }

     for (PageInfoList *node = pil; node != 0; node=node->next) {
          PageInfo *pi = node->page_info;
          if ((pi->n_crawls == 0) &&
              ((sch->max_crawl_depth == 0) ||
               (pi->depth <= sch->max_crawl_depth))) {
               ScheduleKey se = {
                    .score = 0.0,
                    .hash = node->hash
               };
               if (sch->scorer->state)
                    sch->scorer->add(sch->scorer->state, pi, &se.score);
               else
                    se.score = pi->score;
               MDB_val key = {
                    .mv_size = sizeof(se),
                    .mv_data = &se
               };
               MDB_val val = {
                    .mv_size = 0,
                    .mv_data = 0
               };
               if ((mdb_rc = mdb_cursor_put(cur, &key, &val, 0)) != 0) {
                    error1 = "adding page to schedule";
                    error2 = mdb_strerror(mdb_rc);
                    goto on_error;
               }
          }
     }
     if (txn_manager_commit(sch->txn_manager, txn) != 0) {
          error1 = "commiting schedule transaction";
          error2 = sch->txn_manager->error->message;
          goto on_error;
     }
     page_info_list_delete(pil);
     return 0;

on_error:
     if (txn != 0)
          txn_manager_abort(sch->txn_manager, txn);
     page_info_list_delete(pil);

     bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
     bf_scheduler_add_error(sch, error1);
     bf_scheduler_add_error(sch, error2);
     return sch->error->code;
}

static BFSchedulerError
bf_scheduler_change_score(BFScheduler *sch,
                          MDB_cursor *cur,
                          uint64_t hash,
                          float score_old,
                          float score_new) {
     char *error1 = 0;
     char *error2 = 0;
     // Delete old key
     ScheduleKey se = { .score = score_old, .hash = hash };
     MDB_val key = {.mv_size = sizeof(se), .mv_data = &se};
     MDB_val val = {0, 0};
     int mdb_rc;
     switch (mdb_rc = mdb_cursor_get(cur, &key, &val, MDB_SET)) {
     case 0:
          // Delete old key
          if ((mdb_rc = mdb_cursor_del(cur, 0)) != 0) {
               error1 = "deleting Hash/Idx item";
               error2 = mdb_strerror(mdb_rc);
               goto on_error;
          }
          // Add new key
          se.score = score_new;
          if ((mdb_rc = mdb_cursor_put(cur, &key, &val, 0)) != 0) {
               error1 = "adding updated Hash/Index item";
               error2 = mdb_strerror(mdb_rc);
               goto on_error;
          }
          break;
     case MDB_NOTFOUND:
          // OK, do nothing
          break;
     default:
          error1 = "trying to retrieve Hash/Index item";
          error2 = mdb_strerror(mdb_rc);
          goto on_error;
          break;
     }
     return 0;

on_error:
     bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
     bf_scheduler_add_error(sch, error1);
     bf_scheduler_add_error(sch, error2);
     return sch->error->code;
}

static BFSchedulerError
bf_scheduler_update_batch(BFScheduler *sch) {
     assert(sch->scorer->state != 0);

     if (bf_scheduler_expand(sch) != 0)
          return sch->error->code;

     char *error1 = 0;
     char *error2 = 0;
     MDB_txn *txn = 0;
     MDB_cursor *cur = 0;

     // if no Hash/Idx stream active create a new one. It will be deleted if:
     //     1. We reach stream_state_end or
     //     2. An error is produced
     if (!sch->update_thread->stream &&
         (hashidx_stream_new(&sch->update_thread->stream, sch->page_db) != 0)) {
               error1 = "creating Hash/Index stream";
               error2 = sch->page_db->error->message;
               goto on_error;
     }

     // create new read/write transaction and cursor inside the schedule
     if (txn_manager_begin(sch->txn_manager, 0, &txn) != 0) {
          error1 = "starting transaction";
          error2 = sch->txn_manager->error->message;
          goto on_error;
     }
     int mdb_rc = bf_scheduler_open_cursor(txn, &cur);
     if (mdb_rc != 0) {
          error1 = "opening cursor";
          error2 = mdb_strerror(mdb_rc);
          goto on_error;
     }

     // we make the update in batches because there can be only one simultaneous
     // write transaction, and we need another write transaction to get requests, which
     // actually has higher priority
     for (size_t i=0; i<BF_SCHEDULER_UPDATE_BATCH_SIZE && sch->update_thread->stream; ++i) {

          uint64_t hash;
          size_t idx;
          float score_old;
          float score_new;
          switch (hashidx_stream_next(sch->update_thread->stream, &hash, &idx)) {
          case stream_state_next:
               sch->scorer->get(sch->scorer->state, idx, &score_old, &score_new);
               // to gain some performance we don't bother to change the schedule unless
               // there is some significant score change
               if (fabs(score_old - score_new) >= 0.1*fabs(score_old) &&
                   (bf_scheduler_change_score(sch, cur, hash, score_old, score_new) != 0)) {
                    hashidx_stream_delete(sch->update_thread->stream);
                    txn_manager_abort(sch->txn_manager, txn);
                    return sch->error->code;
               }
               break;
          case stream_state_end:
               hashidx_stream_delete(sch->update_thread->stream);
               sch->update_thread->stream = 0;
               break;
          case stream_state_init: // not possible really
          case stream_state_error:
               error1 = "processing the Hash/Idx stream";
               break;
          }
     }
     cur = 0;

     if (txn_manager_commit(sch->txn_manager, txn) != 0) {
          error1 = "commiting schedule transaction";
          error2 = sch->txn_manager->error->message;
          goto on_error;
     }
     txn = 0;

     return 0;
on_error:
     if (sch->update_thread->stream)
          hashidx_stream_delete(sch->update_thread->stream);
     if (txn)
          txn_manager_abort(sch->txn_manager, txn);

     bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
     bf_scheduler_add_error(sch, error1);
     bf_scheduler_add_error(sch, error2);
     return sch->error->code;
}

static BFSchedulerError
bf_scheduler_update_step(BFScheduler *sch) {
     if (sch->scorer->update(sch->scorer->state) != 0) {
          bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
          bf_scheduler_add_error(sch, "updating scorer");
          return sch->error->code;
     }
     do {
          if (bf_scheduler_update_batch(sch) != 0)
               return sch->error->code;
     } while (sch->update_thread->stream);
     return 0;
}

static BFSchedulerError
bf_scheduler_mutex_lock(BFScheduler *sch) {
     int rc = pthread_mutex_lock(&sch->update_thread->state_mutex);
     if (rc != 0) {
          bf_scheduler_set_error(sch, bf_scheduler_error_thread, __func__);
          bf_scheduler_add_error(sch, "locking update thread mutex");
          bf_scheduler_add_error(sch, strerror(rc));
     }
     return sch->error->code;
}

static BFSchedulerError
bf_scheduler_mutex_unlock(BFScheduler *sch) {
     int rc = pthread_mutex_unlock(&sch->update_thread->state_mutex);
     if (rc != 0) {
          bf_scheduler_set_error(sch, bf_scheduler_error_thread, __func__);
          bf_scheduler_add_error(sch, "unlocking update thread mutex");
          bf_scheduler_add_error(sch, strerror(rc));
     }
     return sch->error->code;
}

static BFSchedulerError
bf_scheduler_update_finished(BFScheduler *sch, int *stop) {
     if (bf_scheduler_mutex_lock(sch) != 0)
          return sch->error->code;

     if (sch->update_thread->state == update_thread_stopped)
          sch->update_thread->state = update_thread_finished;
     *stop = (sch->update_thread->state == update_thread_finished);

     return bf_scheduler_mutex_unlock(sch);
}

static int
bf_scheduler_update_is_blocked(BFScheduler *sch) {
     const int block_1 = sch->update_thread->n_pages_new <
	  (sch->update_thread->n_pages_old + BF_SCHEDULER_UPDATE_NUM_PAGES);
     const int block_2 = sch->update_thread->n_pages_new <
	  sch->update_thread->n_pages_old*(1.0 + BF_SCHEDULER_UPDATE_PER_PAGES);
     return block_1 || block_2;
}

static void
bf_scheduler_update_set_block(BFScheduler *sch) {
     sch->update_thread->n_pages_old = sch->update_thread->n_pages_new;
}

static void*
bf_scheduler_update_thread(void *arg) {
     BFScheduler *sch = arg;
     int stop = 0;
     int rc = 0;
     do {
	  if (sch->update_thread->rest_time == 0) {
	       if ((rc = pthread_mutex_lock(&sch->update_thread->wait_mutex)) != 0)
		    goto error_thread;
	       while (bf_scheduler_update_is_blocked(sch)) {
		    if ((rc = pthread_cond_wait(&sch->update_thread->wait_cond,
						&sch->update_thread->wait_mutex)) != 0)
			 goto error_thread;

		    if (bf_scheduler_update_finished(sch, &stop) != 0)
			 return sch;
		    if (stop)
			 break;
	       }
	       bf_scheduler_update_set_block(sch);
	       if ((rc = pthread_mutex_unlock(&sch->update_thread->wait_mutex)) != 0)
		    goto error_thread;
	  } else {
	       struct timespec req = {
		    .tv_sec = sch->update_thread->rest_time,
		    .tv_nsec = 0
	       };
	       struct timespec rem;
	       int continue_sleeping;
	       do {
		    if (nanosleep(&req, &rem) == 0)
			 continue_sleeping = 0;
		    else switch (rc = errno) {
			 case EINTR:
			      req = rem;
			      continue_sleeping = 1;
			      break;
			 default:
			      goto error_thread;
			 }
	       } while (continue_sleeping);
	  }
          if (bf_scheduler_update_finished(sch, &stop) != 0)
               return sch;

          if (!stop && bf_scheduler_update_step(sch) != 0)
               break;

          if (bf_scheduler_update_finished(sch, &stop) != 0)
               return sch;
     } while (!stop);

     return sch;

error_thread:
     bf_scheduler_set_error(sch, bf_scheduler_error_thread, __func__);
     bf_scheduler_add_error(sch, strerror(rc));
     return sch;
}

BFSchedulerError
bf_scheduler_update_start(BFScheduler *sch) {
     BFSchedulerError rc;
     if (sch->scorer->state) {
          if ((rc = bf_scheduler_mutex_lock(sch)) != 0)
               return rc;

          switch (sch->update_thread->state) {
          case update_thread_finished:
               // the update thread acknowledges it has to stop. It will
               // take very little time to stop, just join with it.
               if ((rc = pthread_join(sch->update_thread->thread, 0)) != 0) {
                    bf_scheduler_set_error(sch, bf_scheduler_error_thread, __func__);
                    bf_scheduler_add_error(sch, "joining with update thread");
                    bf_scheduler_add_error(sch, strerror(rc));

               }
          case update_thread_none:
               // create a new thread and put to work
               if ((rc = pthread_create(
                         &sch->update_thread->thread,
                         0,
                         bf_scheduler_update_thread,
                         (void*)sch)) != 0) {
                    bf_scheduler_set_error(sch, bf_scheduler_error_thread, __func__);
                    bf_scheduler_add_error(sch, "creating thread");
                    bf_scheduler_add_error(sch, strerror(rc));
                    return sch->error->code;
               }
          case update_thread_stopped:
               sch->update_thread->state = update_thread_working;
               break;
          case update_thread_working:
               // do nothing
               break;
          }
          if ((rc = bf_scheduler_mutex_unlock(sch) != 0))
               return rc;
     }
     return 0;
}

BFSchedulerError
bf_scheduler_update_stop(BFScheduler *sch) {
     if (sch->scorer->state)
          if (bf_scheduler_mutex_lock(sch) == 0) {
               switch (sch->update_thread->state) {
               case update_thread_none:
                    bf_scheduler_set_error(sch, bf_scheduler_error_thread, __func__);
                    bf_scheduler_add_error(sch, "attempted to stop non-existing update thread");
                    break;
               case update_thread_working:
                    sch->update_thread->state = update_thread_stopped;

                    pthread_mutex_lock(&sch->update_thread->wait_mutex);
                    pthread_cond_broadcast(&sch->update_thread->wait_cond);
                    pthread_mutex_unlock(&sch->update_thread->wait_mutex);

                    break;
               case update_thread_stopped:
               case update_thread_finished:
                    // do nothing
                    break;
               }
               bf_scheduler_mutex_unlock(sch);
          }
     return sch->error->code;
}
static BFSchedulerError
bf_scheduler_add_requests(BFScheduler *sch,
                          MDB_cursor *cur,
                          PageRequest *req,
                          size_t max_request,
                          float crawl_limit) {

     char *error1 = 0;
     char *error2 = 0;

     MDB_cursor_op cur_op = MDB_FIRST;
     while (req->n_urls < max_request) {
          int mdb_rc;
          MDB_val key;
          MDB_val val;
          ScheduleKey *se;
          PageInfo *pi;

          switch (mdb_rc = mdb_cursor_get(cur, &key, &val, cur_op)) {
          case 0:
               se = key.mv_data;
               if (page_db_get_info(sch->page_db, se->hash, &pi) != 0) {
                    error1 = "retrieving PageInfo from PageDB";
                    error2 = sch->page_db->error->message;
                    goto on_error;
               }
               break;

          case MDB_NOTFOUND: // no more pages left
               return 0;
          default:
               error1 = "getting head of schedule";
               error2 = mdb_strerror(mdb_rc);
               goto on_error;
          }
          int delete = 0;
          if (pi) {
               if (pi->n_crawls == 0) {
                   if ((crawl_limit < 0) ||
                       page_db_get_domain_crawl_rate(
                            sch->page_db,
                            page_db_hash_get_domain(se->hash)) <= crawl_limit) {

                        // if requested --> delete
                        delete = 1;
                        if (page_request_add_url(req, pi->url) != 0) {
                             error1 = "adding url to request";
                             goto on_error;
                        }
                   }
               } else { // if already crawled --> delete
                    delete = 1;
               }
               page_info_delete(pi);
          }
          if (delete && (mdb_rc = mdb_cursor_del(cur, 0)) != 0) {
               error1 = "deleting head of schedule";
               error2 = mdb_strerror(mdb_rc);
                    goto on_error;
          }
          // If we delete an item, what's the state of the cursor? It appears you can
          // use either MDB_NEXT or MDB_GET_CURRENT to get the next element. MDB_NEXT
          // works both if we delete or not. See:
          //     http://www.openldap.org/lists/openldap-devel/201502/msg00028.html
          cur_op = MDB_NEXT;
     }
     return 0;
on_error:
     bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
     bf_scheduler_add_error(sch, error1);
     bf_scheduler_add_error(sch, error2);

     return sch->error->code;
}

BFSchedulerError
bf_scheduler_request(BFScheduler *sch, size_t n_pages, PageRequest **request) {
     char *error1 = 0;
     char *error2 = 0;

     MDB_txn *txn = 0;
     MDB_cursor *cur = 0;

     if (txn_manager_begin(sch->txn_manager, 0, &txn) != 0) {
          error1 = "starting transaction";
          error2 = sch->txn_manager->error->message;
          goto on_error;
     }

     int mdb_rc = bf_scheduler_open_cursor(txn, &cur);
     if (mdb_rc != 0) {
          error1 = "opening cursor";
          error2 = mdb_strerror(mdb_rc);
          goto on_error;
     }

     PageRequest *req = *request = page_request_new(n_pages);
     if (!req) {
          error1 = "allocating memory";
          goto on_error;
     }

#define ADD_REQS(limit) bf_scheduler_add_requests(sch, cur, req, n_pages, limit)
     if (ADD_REQS(sch->max_soft_domain_crawl_rate) != 0)
          return sch->error->code;
     if (req->n_urls < n_pages) {
          // If a hard limit has been defined try to increment crawl rate until
          // getting all the required URLs or hitting the hard limit
          if (sch->max_hard_domain_crawl_rate >
              sch->max_soft_domain_crawl_rate) {
               const float k =
                    log(sch->max_hard_domain_crawl_rate/sch->max_soft_domain_crawl_rate)/
                    (BF_SCHEDULER_CRAWL_RATE_STEPS - 1.0);
               for (int step = 1;
                    (step < BF_SCHEDULER_CRAWL_RATE_STEPS) &&
                         (req->n_urls < n_pages);
                    ++step) {
                    if (ADD_REQS(sch->max_soft_domain_crawl_rate*exp(k*step)) != 0)
                         return sch->error->code;
               }
          // otherwise just fill the requests
          } else if (ADD_REQS(sch->max_hard_domain_crawl_rate) != 0) {
               return sch->error->code;
          }
     }

     if (txn_manager_commit(sch->txn_manager, txn) != 0) {
          error1 = "commiting schedule transaction";
          error2 = sch->txn_manager->error->message;
          goto on_error;
     }
     return 0;

on_error:
     bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
     bf_scheduler_add_error(sch, error1);
     bf_scheduler_add_error(sch, error2);

     return sch->error->code;
}

void
bf_scheduler_set_persist(BFScheduler *sch, int value) {
     sch->persist = value;
}

void
bf_scheduler_set_max_crawl_depth(BFScheduler *sch, uint64_t value) {
     sch->max_crawl_depth = value;
}

BFSchedulerError
bf_scheduler_set_max_domain_crawl_rate(BFScheduler *sch,
                                       float max_soft_crawl_rate,
                                       float max_hard_crawl_rate) {
     // The window T must be a period of time long enough for the crawl rate of a
     // domain to be stable. Let's say at least 10 measures are expected inside
     // this time window
     float window = 10.0/max_hard_crawl_rate;

     /* If W is the window size, then the minimum number of domains we need
        if we want to track domains at a rate R is W/R. Let's assume
        we want to track at least 0.1*max_domain_temp then the number N of domains
        is:

         W / R * 10 = N
     */
     size_t n_domains = 100;
     if (page_db_set_domain_temp(sch->page_db, n_domains, window) != 0) {
          bf_scheduler_set_error(sch, bf_scheduler_error_internal, __func__);
          bf_scheduler_add_error(sch, sch->page_db->error->message);
          return sch->error->code;
     }
     sch->max_soft_domain_crawl_rate = max_soft_crawl_rate;
     sch->max_hard_domain_crawl_rate = max_hard_crawl_rate;

     return 0;
}

/** Set @ref BFScheduler::update_interval option for scheduler */
void
bf_scheduler_set_update_interval(BFScheduler *sch, time_t value) {
     sch->update_thread->rest_time = value;
}

void
bf_scheduler_delete(BFScheduler *sch) {
     if (sch->update_thread->state != update_thread_none) {
          (void)bf_scheduler_update_stop(sch);
          (void)pthread_join(sch->update_thread->thread, 0);
     }
     (void)pthread_mutex_destroy(&sch->update_thread->wait_mutex);
     (void)pthread_cond_destroy(&sch->update_thread->wait_cond);
     (void)pthread_mutex_destroy(&sch->update_thread->state_mutex);

     mdb_env_close(sch->txn_manager->env);
     (void)txn_manager_delete(sch->txn_manager);
     if (!sch->persist) {
          char *data = build_path(sch->path, "data.mdb");
          char *lock = build_path(sch->path, "lock.mdb");
          remove(data);
          remove(lock);
          free(data);
          free(lock);

          remove(sch->path);
     }
     free(sch->update_thread);
     free(sch->scorer);
     free(sch->path);
     error_delete(sch->error);
     free(sch);
}

#if (defined TEST) && TEST
#include "test_bf_scheduler.c"
#endif // TEST


text:
//
//  YXDCustomIndexView.h
//  CustomIndexViewDemo
//
//  Created by dd on 15/1/8.
//  Copyright (c) 2015年 YangXudong. All rights reserved.
//

#import <UIKit/UIKit.h>

@class YXDCustomIndexView;

@protocol YXDCustomIndexViewDelegate <NSObject>

/**
 *  选择了某个索引
 */
- (void) customIndexView:(YXDCustomIndexView *)customView selectedSectionIndex:(int)index;

@end

@interface YXDCustomIndexView : UIView

@property (nonatomic, weak) id<YXDCustomIndexViewDelegate> delegate;

@property (nonatomic, strong) NSArray *sectionTitleArray;

+ (YXDCustomIndexView *) customIndexViewWithWidth:(float)width
                                           center:(CGPoint)center
                                sectionTitleArray:(NSArray *)sectionTitleArray
                                         delegate:(id)delegate
                                        superView:(UIView *)superView;

@end

redpajama_set_name: RedPajamaGithub


text:
import { serverurl } from '../config'

export const availableThemes = [
  { name: 'Light', value: 'default' },
  { name: 'One Dark (Default)', value: 'one-dark' },
  { name: 'Monokai', value: 'monokai' },
  { name: 'Solarized Dark', value: 'solarized dark' },
  { name: 'Solarized Light', value: 'solarized light' },
  { name: 'Dracula', value: 'dracula' },
  { name: 'Material', value: 'material' },
  { name: 'Nord', value: 'nord' },
  { name: 'Panda', value: 'panda-syntax' },
  { name: 'Ayu Dark', value: 'ayu-dark' },
  { name: 'Ayu Mirage', value: 'ayu-mirage' },
  { name: 'Tomorror Night Bright', value: 'tomorrow-night-bright' },
  { name: 'Tomorror Night Eighties', value: 'tomorrow-night-eighties' }
]

export const emojifyImageDir = window.USE_CDN ? 'https://cdn.jsdelivr.net/npm/@hackmd/emojify.js@2.1.0/dist/images/basic' : `${serverurl}/build/emojify.js/dist/images/basic`


text:
namespace app_runtime = extensions::core_api::app_runtime;

using content::RenderFrameHost;
using content::WebContents;
using extensions::ExtensionHost;

namespace extensions {

namespace {

struct ResponseInfo {
  scoped_refptr<const Extension> guest_extension;
  base::WeakPtr<AppViewGuest> app_view_guest;
  GuestViewBase::WebContentsCreatedCallback callback;

  ResponseInfo(const Extension* guest_extension,
               const base::WeakPtr<AppViewGuest>& app_view_guest,
               const GuestViewBase::WebContentsCreatedCallback& callback)
      : guest_extension(guest_extension),
        app_view_guest(app_view_guest),
        callback(callback) {}

  ~ResponseInfo() {}
};

typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap;
static base::LazyInstance<PendingResponseMap> pending_response_map =
    LAZY_INSTANCE_INITIALIZER;

}  // namespace

// static.
const char AppViewGuest::Type[] = "appview";

// static.
bool AppViewGuest::CompletePendingRequest(
    content::BrowserContext* browser_context,
    const GURL& url,
    int guest_instance_id,
    const std::string& guest_extension_id) {
  PendingResponseMap* response_map = pending_response_map.Pointer();
  PendingResponseMap::iterator it = response_map->find(guest_instance_id);
  if (it == response_map->end()) {
    // TODO(fsamuel): An app is sending invalid responses. We should probably
    // kill it.
    return false;
  }

  linked_ptr<ResponseInfo> response_info = it->second;
  if (!response_info->app_view_guest ||
      (response_info->guest_extension->id() != guest_extension_id)) {
    // TODO(fsamuel): An app is trying to respond to an <appview> that didn't
    // initiate communication with it. We should kill the app here.
    return false;
  }

  response_info->app_view_guest->
      CompleteCreateWebContents(url,
                                response_info->guest_extension,
                                response_info->callback);

  response_map->erase(guest_instance_id);
  return true;
}

// static
GuestViewBase* AppViewGuest::Create(content::BrowserContext* browser_context,
                                    int guest_instance_id) {
  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kEnableAppView)) {
    return NULL;
  }
  return new AppViewGuest(browser_context, guest_instance_id);
}

AppViewGuest::AppViewGuest(content::BrowserContext* browser_context,
                           int guest_instance_id)
    : GuestView<AppViewGuest>(browser_context, guest_instance_id),
      weak_ptr_factory_(this) {
}

AppViewGuest::~AppViewGuest() {
}

WindowController* AppViewGuest::GetExtensionWindowController() const {
  return NULL;
}

content::WebContents* AppViewGuest::GetAssociatedWebContents() const {
  return guest_web_contents();
}

bool AppViewGuest::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message)
    IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams& params) {
  ContextMenuDelegate* menu_delegate =
      ContextMenuDelegate::FromWebContents(guest_web_contents());
  DCHECK(menu_delegate);

  scoped_ptr<RenderViewContextMenu> menu =
      menu_delegate->BuildMenu(guest_web_contents(), params);
  menu_delegate->ShowMenu(menu.Pass());
  return true;
}

const char* AppViewGuest::GetAPINamespace() {
  return appview::kEmbedderAPINamespace;
}

void AppViewGuest::CreateWebContents(
    const std::string& embedder_extension_id,
    int embedder_render_process_id,
    const base::DictionaryValue& create_params,
    const WebContentsCreatedCallback& callback) {
  std::string app_id;
  if (!create_params.GetString(appview::kAppID, &app_id)) {
    callback.Run(NULL);
    return;
  }

  const base::DictionaryValue* data = NULL;
  if (!create_params.GetDictionary(appview::kData, &data)) {
    callback.Run(NULL);
    return;
  }

  ExtensionService* service =
      ExtensionSystem::Get(browser_context())->extension_service();
  const Extension* guest_extension = service->GetExtensionById(app_id, false);
  const Extension* embedder_extension =
      service->GetExtensionById(embedder_extension_id, false);

  if (!guest_extension || !guest_extension->is_platform_app() ||
      !embedder_extension | !embedder_extension->is_platform_app()) {
    callback.Run(NULL);
    return;
  }

  pending_response_map.Get().insert(
      std::make_pair(GetGuestInstanceID(),
                     make_linked_ptr(new ResponseInfo(
                                        guest_extension,
                                        weak_ptr_factory_.GetWeakPtr(),
                                        callback))));

  LazyBackgroundTaskQueue* queue =
      ExtensionSystem::Get(browser_context())->lazy_background_task_queue();
  if (queue->ShouldEnqueueTask(browser_context(), guest_extension)) {
    queue->AddPendingTask(browser_context(),
                          guest_extension->id(),
                          base::Bind(
                              &AppViewGuest::LaunchAppAndFireEvent,
                              weak_ptr_factory_.GetWeakPtr(),
                              base::Passed(make_scoped_ptr(data->DeepCopy())),
                              callback));
    return;
  }

  ProcessManager* process_manager =
      ExtensionSystem::Get(browser_context())->process_manager();
  ExtensionHost* host =
      process_manager->GetBackgroundHostForExtension(guest_extension->id());
  DCHECK(host);
  LaunchAppAndFireEvent(make_scoped_ptr(data->DeepCopy()), callback, host);
}

void AppViewGuest::DidAttachToEmbedder() {
  // This is called after the guest process has been attached to a host
  // element. This means that the host element knows how to route input
  // events to the guest, and the guest knows how to get frames to the
  // embedder.
  guest_web_contents()->GetController().LoadURL(
      url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
}

void AppViewGuest::DidInitialize() {
  extension_function_dispatcher_.reset(
      new ExtensionFunctionDispatcher(browser_context(), this));
}

void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) {
  extension_function_dispatcher_->Dispatch(
      params, guest_web_contents()->GetRenderViewHost());
}

void AppViewGuest::CompleteCreateWebContents(
    const GURL& url,
    const Extension* guest_extension,
    const WebContentsCreatedCallback& callback) {
  if (!url.is_valid()) {
    callback.Run(NULL);
    return;
  }
  url_ = url;
  guest_extension_id_ = guest_extension->id();

  WebContents::CreateParams params(
      browser_context(),
      content::SiteInstance::CreateForURL(browser_context(),
                                          guest_extension->url()));
  params.guest_delegate = this;
  callback.Run(WebContents::Create(params));
}

void AppViewGuest::LaunchAppAndFireEvent(
    scoped_ptr<base::DictionaryValue> data,
    const WebContentsCreatedCallback& callback,
    ExtensionHost* extension_host) {
  ExtensionSystem* system = ExtensionSystem::Get(browser_context());
  bool has_event_listener = system->event_router()->ExtensionHasEventListener(
      extension_host->extension()->id(),
      app_runtime::OnEmbedRequested::kEventName);
  if (!has_event_listener) {
    callback.Run(NULL);
    return;
  }

  scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue());
  embed_request->SetInteger(appview::kGuestInstanceID, GetGuestInstanceID());
  embed_request->SetString(appview::kEmbedderID, embedder_extension_id());
  embed_request->Set(appview::kData, data.release());
  AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
      browser_context(), embed_request.Pass(), extension_host->extension());
}

}  // namespace extensions


text:
using namespace std;

class cashierq : public util::app_owned {
  public:
    explicit cashierq() :
      app_owned("CASHIERQ"),
      randomizer(100, 200) {}

  protected:
    virtual void do_run(raii::shmem<util::shared_data> &shmem) override {
      log.debug("Constructing semaphores");
      raii::sem cashier_sem(shmem->sem_cashier, NAMES_SEM_CASHIER_CASHIER);
      raii::sem child_sem(shmem->sem_cashier, NAMES_SEM_CASHIER_CHILD);
      raii::sem money_sem(shmem->sem_cashier, NAMES_SEM_CASHIER_MONEY);
      raii::sem change_sem(shmem->sem_cashier, NAMES_SEM_CASHIER_CHANGE);

      log.debug("Opening cashierq fifo for reading");
      raii::fifo_reader<unsigned long> cashier_fifo(NAMES_CASHIERQ_FIFO);
      log.debug("Fifo opened");

      log.debug("Opening carrouselq fifo for writing");
      raii::fifo_writer<unsigned long> carrousel_fifo(NAMES_CARROUSELQ_FIFO);
      log.debug("Fifo opened. Starting arrival loop");

      while (!is_halted()) {
        try {
          log.debug("Reading a child id from the cashier queue");
          auto id = cashier_fifo.read();
          log.debug("Read child id $ from cashier queue", id);

          log.info("Child $ is waiting for the cashier to be available", id);
          cashier_sem.wait();

          log.debug("The cashier is mine. Writting child id.");
          shmem->cashier_child_id = id;

          log.debug("Child id provided, making it available");
          child_sem.signal();

          log.debug("Writing money to shared memory");
          shmem->cashier_child_money = randomizer.next();

          log.info("The child $ gives $ money to the cashier", id, shmem->cashier_child_money);
          money_sem.signal();

          log.info("The child $ is waiting for the change", id);
          change_sem.wait();
          log.info("The child $ received change $", id, shmem->cashier_child_change);

          log.info("Child $ is done with the cashier, going to the carrousel", id);
          cashier_sem.signal();

          log.debug("Writing child id $ to carrousel queue", id);
          carrousel_fifo.write(id);
        } catch (syscalls::interrupt &e) {
          log.debug("An interrupt occurred while blocked on system call: $", e.what());
        }
      }

      log.debug("Halt was set, terminating");
    }

  private:
    util::randomizer randomizer;
};

DEFINE_MAIN(cashierq);


text:
export interface IReusable<T extends object> {
	// constructor
	new(): T;
	// static
	reset?(obj: T): void;
}

export interface IReusablePool<T extends object> {    // name it ReusableFood
	getInstance(): IReusablePool<T>;   // static
	acquireReusable(): T;
	releaseReusable(reusable: T): void;
	setMaxPoolSize(size: number): void;
}

/**
 * @description Reusable pool
 */
export class ReusablePool<T extends object> implements IReusablePool<T> {

	private instanceTime: Date; // remove this. just for debuggin to see that there is only one instance of objPool

	private pool: T[];
	private reusable: IReusable<T>;

	private constructor() {
		this.instanceTime = new Date();
	}

	public getInstance(): IReusablePool<T> {
		return null;
	}

	/**
	 * removes a reusable obj from the pool and returns it
	 * if there are no reusable objs in the pool, creates a new one.
	 * if it cannot create a reusable obj, then wait until one is released.
	 */
	public acquireReusable(): T {
		if (this.pool.length) {
			return this.pool.splice(0, 1)[0];
		}

		return new this.reusable();
	}

	public releaseReusable(reusable: T): void {
		if (this.reusable.reset) {
			this.reusable.reset(reusable);
		}

		this.pool.push(reusable);
	}

	public setMaxPoolSize(size: number): void {
		// will do
	}
}


text:
/**
 * @author Anton Avtamonov
 * @version $Revision$
 */
package javax.swing.plaf.basic;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.MouseWheelEvent;
import javax.swing.BasicSwingTestCase;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import org.apache.harmony.x.swing.Utilities;

public class BasicTableHeaderUITest extends BasicSwingTestCase {

    private BasicTableHeaderUI ui;

    private MouseWheelEvent mouseWheelEvent =
            new MouseWheelEvent(new Label(),
                    0, 0, 0, 0, 0, 0, false, 0,
                    MouseWheelEvent.WHEEL_UNIT_SCROLL, 0);

    public BasicTableHeaderUITest(final String name) {
        super(name);
    }

    @Override
    protected void setUp() throws Exception {
        ui = new BasicTableHeaderUI();
    }

    @Override
    protected void tearDown() throws Exception {
        ui = null;
    }

    public void testCreateMouseInputListener() throws Exception {
        assertSame(BasicTableHeaderUI.MouseInputHandler.class, ui.createMouseInputListener()
                .getClass());
        assertNotSame(ui.createMouseInputListener(), ui.createMouseInputListener());
    }

    public void testCreate() throws Exception {
        assertSame(BasicTableHeaderUI.class, BasicTableHeaderUI.createUI(null).getClass());
        assertNotSame(BasicTableHeaderUI.createUI(null), BasicTableHeaderUI.createUI(null));
        assertNull(((BasicTableHeaderUI) BasicTableHeaderUI.createUI(new JTableHeader())).header);
    }

    public void testInstallUninstallUI() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.uninstallUI(new JTable());
            }
        });
        assertNull(ui.header);
        assertNull(ui.rendererPane);
        JTableHeader header = new JTableHeader();
        ui.installUI(header);
        assertSame(header, ui.header);
        assertNotNull(ui.rendererPane);
        assertTrue(ui.header.isOpaque());
        ui.uninstallUI(null);
        assertNull(ui.header);
        assertNull(ui.rendererPane);
    }

    public void testInstallUninstallDefaults() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.installDefaults();
            }
        });

        // Regression test for HARMONY-2613
        // Make sure it throws no exceptions
        ui.uninstallDefaults();

        ui.header = new JTableHeader();
        ui.header.setForeground(new ColorUIResource(Color.RED));
        ui.header.setBackground(new ColorUIResource(Color.BLUE));
        ui.header.setFont(new FontUIResource(new Font("any", Font.BOLD, 20)));
        ui.installDefaults();
        assertEquals(UIManager.getColor("TableHeader.background"), ui.header.getBackground());
        assertEquals(UIManager.getColor("TableHeader.foreground"), ui.header.getForeground());
        assertEquals(UIManager.getFont("TableHeader.font"), ui.header.getFont());
        ui.uninstallDefaults();
        if (isHarmony()) {
            assertNull(ui.header.getForeground());
            assertNull(ui.header.getBackground());
            assertNull(ui.header.getFont());
        }
    }

    public void testInstallUninstallListeners() throws Exception {
        assertNull(ui.mouseInputListener);

        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.installListeners();
            }
        });
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.uninstallListeners();
            }
        });
        ui.header = new JTableHeader();
        int mouseListenersCount = ui.header.getMouseListeners().length;
        int mouseMotionListenersCount = ui.header.getMouseMotionListeners().length;
        ui.installListeners();
        assertEquals(mouseListenersCount + 1, ui.header.getMouseListeners().length);
        assertEquals(mouseMotionListenersCount + 1, ui.header.getMouseMotionListeners().length);
        assertNotNull(ui.mouseInputListener);
        ui.uninstallListeners();
        assertEquals(mouseListenersCount, ui.header.getMouseListeners().length);
        assertEquals(mouseMotionListenersCount, ui.header.getMouseMotionListeners().length);
        assertNull(ui.mouseInputListener);
    }

    public void testInstallUninstallKeyboardActions() throws Exception {
        ui.header = new JTableHeader();
        ui.installKeyboardActions();
        assertTrue(Utilities.isEmptyArray(ui.header.getActionMap().allKeys()));
    }

    public void testInstallKeyboardActions() throws Exception {
        // Regression test for HARMONY-2613
        // Make sure it throws no exceptions
        ui.installKeyboardActions();
    }

    public void testUninstallKeyboardActions() throws Exception {
        // Regression test for HARMONY-2613
        // Make sure it throws no exceptions
        ui.uninstallKeyboardActions();
    }

    public void testPaint() throws Exception {
        final Graphics g = createTestGraphics();

        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.paint(g, new JTable());
            }
        });
        ui.installUI(new JTableHeader());
        ui.header.getColumnModel().addColumn(new TableColumn());
        g.setClip(0, 0, 100, 100);
        ui.paint(g, null);
    }

    public void testGetMinimumMaximumPreferredSize() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.getMinimumSize(new JTable());
            }
        });
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.getMaximumSize(new JTable());
            }
        });
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.getPreferredSize(new JTable());
            }
        });
        ui.header = new JTableHeader();
        assertEquals(new Dimension(), ui.getMinimumSize(null));
        assertEquals(new Dimension(), ui.getMaximumSize(null));
        assertEquals(new Dimension(), ui.getPreferredSize(null));
        TableColumn column1 = new TableColumn(0, 30);
        column1.setMinWidth(20);
        column1.setMaxWidth(60);
        ui.header.getColumnModel().addColumn(column1);
        assertEquals(new Dimension(20, 0), ui.getMinimumSize(null));
        assertEquals(new Dimension(60, 0), ui.getMaximumSize(null));
        assertEquals(new Dimension(30, 0), ui.getPreferredSize(null));
        TableColumn column2 = new TableColumn(0, 40);
        column2.setMinWidth(30);
        column2.setMaxWidth(100);
        ui.header.getColumnModel().addColumn(column2);
        assertEquals(new Dimension(50, 0), ui.getMinimumSize(null));
        assertEquals(new Dimension(160, 0), ui.getMaximumSize(null));
        assertEquals(new Dimension(70, 0), ui.getPreferredSize(null));
        column1.setHeaderValue("any");
        Component renderingComponent = ui.header.getDefaultRenderer()
                .getTableCellRendererComponent(null, "any", false, false, 0, 0);
        assertEquals(new Dimension(50, renderingComponent.getMinimumSize().height), ui
                .getMinimumSize(null));
        assertEquals(new Dimension(160, renderingComponent.getMaximumSize().height), ui
                .getMaximumSize(null));
        assertEquals(new Dimension(70, renderingComponent.getPreferredSize().height), ui
                .getPreferredSize(null));
    }

    public void testMouseInputHandlerMouseEnteredNull() throws Exception {
        // Regression test for HARMONY-1777
        new BasicTableHeaderUI().new MouseInputHandler().mouseEntered(null);
    }

    public void testMouseInputHandlerMouseClicked() throws Exception {
        // Regression test for HARMONY-2613
        // Make sure it throws no exceptions
        ui.new MouseInputHandler().mouseClicked(mouseWheelEvent);
    }

    public void testMouseInputHandlerMouseEntered() throws Exception {
        // Regression test for HARMONY-2613
        // Make sure it throws no exceptions
        ui.new MouseInputHandler().mouseEntered(mouseWheelEvent);
    }

    public void testMouseInputHandlerMouseExited() throws Exception {
        // Regression test for HARMONY-2613
        // Make sure it throws no exceptions
        ui.new MouseInputHandler().mouseExited(mouseWheelEvent);
    }

    public void testMouseInputHandlerMousePressed() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.new MouseInputHandler().mousePressed(mouseWheelEvent);
            }
        });
    }

    public void testMouseInputHandlerMouseReleased() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.new MouseInputHandler().mouseReleased(mouseWheelEvent);
            }
        });
    }

    public void testMouseInputHandlerMouseDragged() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.new MouseInputHandler().mouseDragged(mouseWheelEvent);
            }
        });
    }

    public void testMouseInputHandlerMouseMoved() throws Exception {
        testExceptionalCase(new NullPointerCase() {
            // Regression test for HARMONY-2613
            @Override
            public void exceptionalAction() throws Exception {
                ui.new MouseInputHandler().mouseMoved(mouseWheelEvent);
            }
        });
    }
}


text:
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://www.mendix.com/package/1.0/">
	<clientModule name="Taggd" version="1.0.0" xmlns="http://www.mendix.com/clientModule/1.0/">
		<widgetFiles>
			<widgetFile path="Taggd/Taggd.xml"/>
		</widgetFiles>
		<files>
			<file path="Taggd/widget/"/>
		</files>
	</clientModule>
</package>


text:
package altsrc

import (
	"flag"

	"github.com/urfave/cli"
)

// WARNING: This file is generated!

// BoolFlag is the flag type that wraps cli.BoolFlag to allow
// for other values to be specified
type BoolFlag struct {
	cli.BoolFlag
	set *flag.FlagSet
}

// NewBoolFlag creates a new BoolFlag
func NewBoolFlag(fl cli.BoolFlag) *BoolFlag {
	return &BoolFlag{BoolFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped BoolFlag.Apply
func (f *BoolFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.BoolFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped BoolFlag.ApplyWithError
func (f *BoolFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.BoolFlag.ApplyWithError(set)
}

// BoolTFlag is the flag type that wraps cli.BoolTFlag to allow
// for other values to be specified
type BoolTFlag struct {
	cli.BoolTFlag
	set *flag.FlagSet
}

// NewBoolTFlag creates a new BoolTFlag
func NewBoolTFlag(fl cli.BoolTFlag) *BoolTFlag {
	return &BoolTFlag{BoolTFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped BoolTFlag.Apply
func (f *BoolTFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.BoolTFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped BoolTFlag.ApplyWithError
func (f *BoolTFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.BoolTFlag.ApplyWithError(set)
}

// DurationFlag is the flag type that wraps cli.DurationFlag to allow
// for other values to be specified
type DurationFlag struct {
	cli.DurationFlag
	set *flag.FlagSet
}

// NewDurationFlag creates a new DurationFlag
func NewDurationFlag(fl cli.DurationFlag) *DurationFlag {
	return &DurationFlag{DurationFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped DurationFlag.Apply
func (f *DurationFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.DurationFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped DurationFlag.ApplyWithError
func (f *DurationFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.DurationFlag.ApplyWithError(set)
}

// Float64Flag is the flag type that wraps cli.Float64Flag to allow
// for other values to be specified
type Float64Flag struct {
	cli.Float64Flag
	set *flag.FlagSet
}

// NewFloat64Flag creates a new Float64Flag
func NewFloat64Flag(fl cli.Float64Flag) *Float64Flag {
	return &Float64Flag{Float64Flag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped Float64Flag.Apply
func (f *Float64Flag) Apply(set *flag.FlagSet) {
	f.set = set
	f.Float64Flag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped Float64Flag.ApplyWithError
func (f *Float64Flag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.Float64Flag.ApplyWithError(set)
}

// GenericFlag is the flag type that wraps cli.GenericFlag to allow
// for other values to be specified
type GenericFlag struct {
	cli.GenericFlag
	set *flag.FlagSet
}

// NewGenericFlag creates a new GenericFlag
func NewGenericFlag(fl cli.GenericFlag) *GenericFlag {
	return &GenericFlag{GenericFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped GenericFlag.Apply
func (f *GenericFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.GenericFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped GenericFlag.ApplyWithError
func (f *GenericFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.GenericFlag.ApplyWithError(set)
}

// Int64Flag is the flag type that wraps cli.Int64Flag to allow
// for other values to be specified
type Int64Flag struct {
	cli.Int64Flag
	set *flag.FlagSet
}

// NewInt64Flag creates a new Int64Flag
func NewInt64Flag(fl cli.Int64Flag) *Int64Flag {
	return &Int64Flag{Int64Flag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped Int64Flag.Apply
func (f *Int64Flag) Apply(set *flag.FlagSet) {
	f.set = set
	f.Int64Flag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped Int64Flag.ApplyWithError
func (f *Int64Flag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.Int64Flag.ApplyWithError(set)
}

// IntFlag is the flag type that wraps cli.IntFlag to allow
// for other values to be specified
type IntFlag struct {
	cli.IntFlag
	set *flag.FlagSet
}

// NewIntFlag creates a new IntFlag
func NewIntFlag(fl cli.IntFlag) *IntFlag {
	return &IntFlag{IntFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped IntFlag.Apply
func (f *IntFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.IntFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped IntFlag.ApplyWithError
func (f *IntFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.IntFlag.ApplyWithError(set)
}

// IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow
// for other values to be specified
type IntSliceFlag struct {
	cli.IntSliceFlag
	set *flag.FlagSet
}

// NewIntSliceFlag creates a new IntSliceFlag
func NewIntSliceFlag(fl cli.IntSliceFlag) *IntSliceFlag {
	return &IntSliceFlag{IntSliceFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped IntSliceFlag.Apply
func (f *IntSliceFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.IntSliceFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped IntSliceFlag.ApplyWithError
func (f *IntSliceFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.IntSliceFlag.ApplyWithError(set)
}

// Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow
// for other values to be specified
type Int64SliceFlag struct {
	cli.Int64SliceFlag
	set *flag.FlagSet
}

// NewInt64SliceFlag creates a new Int64SliceFlag
func NewInt64SliceFlag(fl cli.Int64SliceFlag) *Int64SliceFlag {
	return &Int64SliceFlag{Int64SliceFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped Int64SliceFlag.Apply
func (f *Int64SliceFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.Int64SliceFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped Int64SliceFlag.ApplyWithError
func (f *Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.Int64SliceFlag.ApplyWithError(set)
}

// StringFlag is the flag type that wraps cli.StringFlag to allow
// for other values to be specified
type StringFlag struct {
	cli.StringFlag
	set *flag.FlagSet
}

// NewStringFlag creates a new StringFlag
func NewStringFlag(fl cli.StringFlag) *StringFlag {
	return &StringFlag{StringFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped StringFlag.Apply
func (f *StringFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.StringFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped StringFlag.ApplyWithError
func (f *StringFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.StringFlag.ApplyWithError(set)
}

// StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow
// for other values to be specified
type StringSliceFlag struct {
	cli.StringSliceFlag
	set *flag.FlagSet
}

// NewStringSliceFlag creates a new StringSliceFlag
func NewStringSliceFlag(fl cli.StringSliceFlag) *StringSliceFlag {
	return &StringSliceFlag{StringSliceFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped StringSliceFlag.Apply
func (f *StringSliceFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.StringSliceFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped StringSliceFlag.ApplyWithError
func (f *StringSliceFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.StringSliceFlag.ApplyWithError(set)
}

// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow
// for other values to be specified
type Uint64Flag struct {
	cli.Uint64Flag
	set *flag.FlagSet
}

// NewUint64Flag creates a new Uint64Flag
func NewUint64Flag(fl cli.Uint64Flag) *Uint64Flag {
	return &Uint64Flag{Uint64Flag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped Uint64Flag.Apply
func (f *Uint64Flag) Apply(set *flag.FlagSet) {
	f.set = set
	f.Uint64Flag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped Uint64Flag.ApplyWithError
func (f *Uint64Flag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.Uint64Flag.ApplyWithError(set)
}

// UintFlag is the flag type that wraps cli.UintFlag to allow
// for other values to be specified
type UintFlag struct {
	cli.UintFlag
	set *flag.FlagSet
}

// NewUintFlag creates a new UintFlag
func NewUintFlag(fl cli.UintFlag) *UintFlag {
	return &UintFlag{UintFlag: fl, set: nil}
}

// Apply saves the flagSet for later usage calls, then calls the
// wrapped UintFlag.Apply
func (f *UintFlag) Apply(set *flag.FlagSet) {
	f.set = set
	f.UintFlag.Apply(set)
}

// ApplyWithError saves the flagSet for later usage calls, then calls the
// wrapped UintFlag.ApplyWithError
func (f *UintFlag) ApplyWithError(set *flag.FlagSet) error {
	f.set = set
	return f.UintFlag.ApplyWithError(set)
}


text:
require 'spec_helper'

describe Bosh::AwsCloud::Cloud do
  describe 'has_disk?' do
    context 'when disk is found' do
      let(:disk) {instance_double('AWS::EC2::Volume', id: 'v-foo', exists?: true)}

      it 'returns true' do
        cloud = mock_cloud do |ec2|
          allow(ec2.volumes).to receive(:[]).with("v-foo").and_return(disk)
        end

        expect(cloud.has_disk?('v-foo')).to be(true)
      end
    end

    context 'when disk is not found' do
      let(:disk) {instance_double('AWS::EC2::Volume', id: 'non-existing-disk-uuid', exists?: false)}

      it 'returns false' do
        cloud = mock_cloud do |ec2|
          allow(ec2.volumes).to receive(:[]).with('non-existing-disk-uuid').and_return(disk)
        end

        expect(cloud.has_disk?('non-existing-disk-uuid')).to be(false)
      end
    end
  end
end


text:
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}

import           Data.Aeson
import           Data.HashMap.Strict
import           Database.Cayley.Client as C
import           Database.Cayley.Types  as CT
import           Test.Hspec
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key       as K
import qualified Data.Aeson.KeyMap    as KM
#endif

main :: IO ()
main = hspec $ do

    describe "write" $
        it "writes a quad to Cayley server" $ do
            c <- connectCayley defaultCayleyConfig
#if MIN_VERSION_aeson(2,0,0)
            write c Quad { subject = "<danny>", predicate = "<follows>", C.object = "<sandy>", label = Nothing } `shouldReturn` Just (Object (KM.fromList [("result",String "Successfully wrote 1 quads.")]))
#else
            write c Quad { subject = "<danny>", predicate = "<follows>", C.object = "<sandy>", label = Nothing } `shouldReturn` Just (Object (fromList [("result",String "Successfully wrote 1 quads.")]))
#endif

    describe "query" $
        it "query Cayley server" $ do
            c <- connectCayley defaultCayleyConfig
            Right a <- query c "graph.Vertex('<sandy>').In('<follows>').All()"
            encode a `shouldBe` "[{\"id\":\"<danny>\"}]"

    describe "shape" $
        it "returns the description of the last query executed" $ do
            c <- connectCayley defaultCayleyConfig
            queryShape c "graph.Vertex('<sandy>').In('<follows>').All()" `shouldReturn` Right (Shape {nodes = [Node {CT.id = 4, tags = Nothing, values = Just ["<sandy>"], isLinkNode = False, isFixed = True},Node {CT.id = 8, tags = Nothing, values = Just ["<follows>"], isLinkNode = False, isFixed = True},Node {CT.id = 2, tags = Nothing, values = Nothing, isLinkNode = True, isFixed = False},Node {CT.id = 1, tags = Just ["id"], values = Nothing, isLinkNode = False, isFixed = False}], links = [Link {source = 1, target = 4, linkNode = 2}]})

    describe "writeNQuadFile" $
        it "writes quads from a file to Cayley server" $ do
            c <- connectCayley defaultCayleyConfig
            (writeNQuadFile c "./tests/testdata.nq" >>= results) `shouldReturn` Right 15


text:
#pragma once

#include "fakeit/DomainObjects.hpp"
#include "fakeit/MockImpl.hpp"
#include "fakeit/ActualInvocation.hpp"
#include "fakeit/Prototype.hpp"

namespace fakeit {
    namespace internal {
    }
    using namespace fakeit;
    using namespace fakeit::internal;

    template<typename C, typename ... baseclasses>
    class Mock : public ActualInvocationsSource {
        MockImpl<C, baseclasses...> impl;
    public:
        virtual ~Mock() = default;

        static_assert(std::is_polymorphic<C>::value, "Can only mock a polymorphic type");

        Mock() : impl(Fakeit) {
        }

        explicit Mock(C &obj) : impl(Fakeit, obj) {
        }

        virtual C &get() {
            return impl.get();
        }

        C &operator()() {
            return get();
        }

        void Reset() {
            impl.reset();
        }

        template<class DATA_TYPE, typename ... arglist,
                class = typename std::enable_if<std::is_member_object_pointer<DATA_TYPE C::*>::value>::type>
        DataMemberStubbingRoot<C, DATA_TYPE> Stub(DATA_TYPE C::* member, const arglist &... ctorargs) {
            return impl.stubDataMember(member, ctorargs...);
        }

        template<int id, typename R, typename T, typename ... arglist, class = typename std::enable_if<
                !std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<R, arglist...> stub(R (T::*vMethod)(arglist...) const) {
            auto methodWithoutConstVolatile = reinterpret_cast<R (T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                !std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<R, arglist...> stub(R(T::*vMethod)(arglist...) volatile) {
            auto methodWithoutConstVolatile = reinterpret_cast<R(T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                !std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<R, arglist...> stub(R(T::*vMethod)(arglist...) const volatile) {
            auto methodWithoutConstVolatile = reinterpret_cast<R(T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                !std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<R, arglist...> stub(R(T::*vMethod)(arglist...)) {
            return impl.template stubMethod<id>(vMethod);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<void, arglist...> stub(R(T::*vMethod)(arglist...) const) {
            auto methodWithoutConstVolatile = reinterpret_cast<void (T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<void, arglist...> stub(R(T::*vMethod)(arglist...) volatile) {
            auto methodWithoutConstVolatile = reinterpret_cast<void (T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<void, arglist...> stub(R(T::*vMethod)(arglist...) const volatile) {
            auto methodWithoutConstVolatile = reinterpret_cast<void (T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
                std::is_void<R>::value && std::is_base_of<T, C>::value>::type>
        MockingContext<void, arglist...> stub(R(T::*vMethod)(arglist...)) {
            auto methodWithoutConstVolatile = reinterpret_cast<void (T::*)(arglist...)>(vMethod);
            return impl.template stubMethod<id>(methodWithoutConstVolatile);
        }

        DtorMockingContext dtor() {
            return impl.stubDtor();
        }

        void getActualInvocations(std::unordered_set<Invocation *> &into) const override {
            impl.getActualInvocations(into);
        }

    };

}


text:
__author__ = "Simone Campagna"

__all__ = [
           'ArgDict',
           'InputArgDict',
           'OutputArgDict',
           'ResultArgDict',
          ]

import re

from collections import OrderedDict

from .storage import Storage

class ArgDict(Storage, OrderedDict):
    __formatter__ = "o{ordinal}"
    def __init__(self, factory, formatter=None, default=None, separator='='):
        Storage.__init__(self, factory)
        OrderedDict.__init__(self)
        self._re_split = re.compile("^([a-zA-Z]+\w*){separator}(.*)".format(separator=re.escape(separator)))
        self._default = default
        if formatter is None:
            formatter = self.__formatter__
        self._formatter = formatter
        self._automatic_labels = {}
        self._ordinals = {}

    def add(self, value):
        label, value = self.label_split(value)
        value = self.factory(value)
        ordinal = len(self)
        if label is None:
            self._default = value
            label = self._formatter.format(ordinal=ordinal)
            self._automatic_labels[ordinal] = label
        self[label] = value
        self._ordinals[label] = ordinal
        return label, value

    def get_ordinal(self, label):
        return self._ordinals.get(label, None)

    def get(self, label, ordinal=None):
        if label in self:
            return self[label]
        else:
            automatic_label = self._automatic_labels.get(ordinal, None)
            if automatic_label in self:
                return self[automatic_label]
        return self._default

    def label_split(self, value):
        m = self._re_split.match(value)
        if m:
            label, value = m.groups()
        else:
            label = None
        return label, value

class InputArgDict(ArgDict):
    __formatter__ = 'i{ordinal}'

class OutputArgDict(ArgDict):
    __formatter__ = 'o{ordinal}'

class ResultArgDict(ArgDict):
    __formatter__ = 'r{ordinal}'


text:
using Azure.Core.TestFramework;

namespace Azure.Management.Network.Tests
{
    public class NetworkManagementTestEnvironment : TestEnvironment
    {
        public NetworkManagementTestEnvironment() : base("networkmgmt")
        {
        }
    }
}


text:
<?php

final class DifferentialTabReplacementTestCase
  extends PhabricatorTestCase {

  public function testTabReplacement() {
    $tab1 = " ";
    $tab2 = "  ";

    $cat = "\xF0\x9F\x90\xB1";

    $cases = array(
      '' => '',
      'x' => 'x',

      // Tabs inside HTML tags should not be replaced.
      "<\t>x" => "<\t>x",

      // Normal tabs should be replaced. These are all aligned to the tab
      // width, so they'll be replaced inline.
      "\tx" => "{$tab2}x",
      "  \tx" => "  {$tab2}x",
      "\t x" => "{$tab2} x",
      "aa\tx" => "aa{$tab2}x",
      "aa  \tx" => "aa  {$tab2}x",
      "aa\t x" => "aa{$tab2} x",

      // This tab is not tabstop-aligned, so it is replaced with fewer
      // spaces to bring us to the next tabstop.
      " \tx" => " {$tab1}x",

      // Text inside HTML tags should not count when aligning tabs with
      // tabstops.
      "<tag> </tag>\tx" => "<tag> </tag>{$tab1}x",
      "<tag2> </tag>\tx" => "<tag2> </tag>{$tab1}x",

      // The code has to take a slow path when inputs contain unicode, but
      // should produce the right results and align tabs to tabstops while
      // respecting UTF8 display character widths, not byte widths.
      "{$cat}\tx" => "{$cat}{$tab1}x",
      "{$cat}{$cat}\tx" => "{$cat}{$cat}{$tab2}x",
    );

    foreach ($cases as $input => $expect) {
      $actual = DifferentialChangesetParser::replaceTabsWithSpaces(
        $input,
        2);

      $this->assertEqual(
        $expect,
        $actual,
        pht('Tabs to Spaces: %s', $input));
    }
  }

}


text:
use std::collections::HashSet;
use std::error::FromError;
use std::io::fs::PathExtensions;
use std::io::{self, File, fs};

use core::{Package,Manifest,SourceId};
use util::{self, CargoResult, human, Config, ChainError};
use util::important_paths::find_project_manifest_exact;
use util::toml::{Layout, project_layout};

pub fn read_manifest(contents: &[u8], layout: Layout, source_id: &SourceId,
                     config: &Config)
                     -> CargoResult<(Manifest, Vec<Path>)> {
    let root = layout.root.clone();
    util::toml::to_manifest(contents, source_id, layout, config).chain_error(|| {
        human(format!("failed to parse manifest at `{}`",
                      root.join("Cargo.toml").display()))
    })
}

pub fn read_package(path: &Path, source_id: &SourceId, config: &Config)
                    -> CargoResult<(Package, Vec<Path>)> {
    log!(5, "read_package; path={}; source-id={}", path.display(), source_id);
    let mut file = try!(File::open(path));
    let data = try!(file.read_to_end());

    let layout = project_layout(&path.dir_path());
    let (manifest, nested) =
        try!(read_manifest(data.as_slice(), layout, source_id, config));

    Ok((Package::new(manifest, path, source_id), nested))
}

pub fn read_packages(path: &Path, source_id: &SourceId, config: &Config)
                     -> CargoResult<Vec<Package>> {
    let mut all_packages = HashSet::new();
    let mut visited = HashSet::<Path>::new();

    log!(5, "looking for root package: {}, source_id={}", path.display(), source_id);

    try!(walk(path, |dir| {
        log!(5, "looking for child package: {}", dir.display());

        // Don't recurse into git databases
        if dir.filename_str() == Some(".git") { return Ok(false); }

        // Don't automatically discover packages across git submodules
        if dir != path && dir.join(".git").exists() { return Ok(false); }

        // Don't ever look at target directories
        if dir.filename_str() == Some("target") && has_manifest(&dir.dir_path()) {
            return Ok(false)
        }

        if has_manifest(dir) {
            try!(read_nested_packages(dir, &mut all_packages, source_id, config,
                                      &mut visited));
        }
        Ok(true)
    }));

    if all_packages.is_empty() {
        Err(human(format!("Could not find Cargo.toml in `{}`", path.display())))
    } else {
        log!(5, "all packages: {:?}", all_packages);
        Ok(all_packages.into_iter().collect())
    }
}

fn walk<F>(path: &Path, mut callback: F) -> CargoResult<()>
    where F: FnMut(&Path) -> CargoResult<bool>
{
    walk_inner(path, &mut callback)
}

fn walk_inner<F>(path: &Path, callback: &mut F) -> CargoResult<()>
    where F: FnMut(&Path) -> CargoResult<bool>
{
    if path.is_dir() {
        let continues = try!(callback(path));
        if !continues {
            log!(5, "not processing {}", path.display());
            return Ok(());
        }

        // Ignore any permission denied errors because temporary directories
        // can often have some weird permissions on them.
        let dirs = match fs::readdir(path) {
            Ok(dirs) => dirs,
            Err(ref e) if e.kind == io::PermissionDenied => return Ok(()),
            Err(e) => return Err(FromError::from_error(e)),
        };
        for dir in dirs.iter() {
            try!(walk_inner(dir, callback));
        }
    }

    Ok(())
}

fn has_manifest(path: &Path) -> bool {
    find_project_manifest_exact(path, "Cargo.toml").is_ok()
}

fn read_nested_packages(path: &Path,
                        all_packages: &mut HashSet<Package>,
                        source_id: &SourceId,
                        config: &Config,
                        visited: &mut HashSet<Path>) -> CargoResult<()> {
    if !visited.insert(path.clone()) { return Ok(()) }

    let manifest = try!(find_project_manifest_exact(path, "Cargo.toml"));

    let (pkg, nested) = try!(read_package(&manifest, source_id, config));
    all_packages.insert(pkg);

    // Registry sources are not allowed to have `path=` dependencies because
    // they're all translated to actual registry dependencies.
    if !source_id.is_registry() {
        for p in nested.iter() {
            try!(read_nested_packages(&path.join(p), all_packages, source_id,
                                      config, visited));
        }
    }

    Ok(())
}


text:
module ApplicationHelper

	def devise_flash
		if controller.devise_controller? && resource.errors.any?
			messages = resource.errors.full_messages
			flash.now[:error] = messages.uniq!
			flash.now[:error] = messages.join(", ")
		end
	end

end


text:
namespace JustEngine
{
	class DLL_EXPORT Quaternion
	{
	public:
		union
		{
			// rotate align (x, y, z) theta
			struct { float a, b, c, d; };// = (x, y, z) * sin(theta/2), cos(theta/2)
			struct { Vector3 u; float v; };
		};

		Quaternion();

		Quaternion(float x, float y, float z, float w);
		Quaternion(double x, double y, double z, double w);
		Quaternion(Vector3 u, float v);

		Vector3 operator*(const Vector3 other);

		Quaternion GetConjudget() const;

		Quaternion operator* (const Quaternion other);

		bool operator==(const Quaternion& other);
		bool operator!=(const Quaternion& other);
		Quaternion& operator=(const Quaternion& other);
	};

}


text:
Plugin.define "Kentico-CMS" do
author "Brendan Coles <bcoles@gmail.com>" # 2011-03-04
version "0.1"
description "Kentico CMS for ASP.NET - flexible, all-in-one solution for web developers"
website "http://www.kentico.com/"

# ShodanHQ results as at 2013-02-06 #
# 384 for CMSPreferredCulture

# Matches #
matches [

# Version Detection # Meta Generator
{ :version=>/<\/title><meta name="generator" content="Kentico CMS [^\(^>]{1,5} \(build ([\d\.]{1,10})\) [^"^>]*" \/>/ },

# CMSPreferredCulture # Cookie
{ :search=>"headers[set-cookie]", :regexp=>/CMSPreferredCulture=/ },

]

end