text:
package archive

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"sort"
	"sync/atomic"

	"go.chromium.org/luci/common/data/recordio"
	"go.chromium.org/luci/common/errors"
	"go.chromium.org/luci/common/gcloud/gs"
	"go.chromium.org/luci/common/iotools"
	log "go.chromium.org/luci/common/logging"
	"go.chromium.org/luci/logdog/api/logpb"
	"go.chromium.org/luci/logdog/common/storage"
	"go.chromium.org/luci/logdog/common/types"

	cloudStorage "cloud.google.com/go/storage"
	"github.com/golang/protobuf/proto"
)

const (
	// maxStreamRecordSize is the maximum record size we're willing to read from
	// our archived log stream. This will help prevent out-of-memory errors if the
	// arhived log stream is malicious or corrupt.
	//
	// Make this twice as large as the maximum log entry size
	maxStreamRecordSize = 2 * types.MaxLogEntryDataSize
)

// Options is the set of configuration options for this Storage instance.
//
// Unlike other Storage instances, this is bound to a single archived stream.
// Project and Path parameters in requests will be ignored in favor of the
// Google Storage URLs.
type Options struct {
	// Index is the Google Storage URL for the stream's index.
	Index gs.Path
	// Stream is the Google Storage URL for the stream's entries.
	Stream gs.Path

	// Client is the HTTP client to use for authentication.
	//
	// Closing this Storage instance does not close the underlying Client.
	Client gs.Client

	// Cache, if not nil, will be used to cache data.
	Cache storage.Cache
}

type storageImpl struct {
	*Options

	index atomic.Value
}

// New instantiates a new Storage instance, bound to the supplied Options.
func New(o Options) (storage.Storage, error) {
	s := storageImpl{
		Options: &o,
	}

	if !s.Stream.IsFullPath() {
		return nil, fmt.Errorf("invalid stream URL: %q", s.Stream)
	}
	if s.Index != "" && !s.Index.IsFullPath() {
		return nil, fmt.Errorf("invalid index URL: %v", s.Index)
	}

	return &s, nil
}

func (s *storageImpl) Close() {}

func (s *storageImpl) Put(context.Context, storage.PutRequest) error { return storage.ErrReadOnly }

func (s *storageImpl) Expunge(context.Context, storage.ExpungeRequest) error {
	return storage.ErrReadOnly
}

func (s *storageImpl) Get(c context.Context, req storage.GetRequest, cb storage.GetCallback) error {
	idx, err := s.getIndex(c)
	if err != nil {
		return err
	}

	// Identify the byte offsets that we want to fetch from the entries stream.
	st := buildGetStrategy(&req, idx)
	if st == nil {
		// No more records to read.
		return nil
	}

	switch err := s.getLogEntriesIter(c, st, cb); errors.Unwrap(err) {
	case nil, io.EOF:
		// We hit the end of our log stream.
		return nil

	case cloudStorage.ErrObjectNotExist, cloudStorage.ErrBucketNotExist:
		return storage.ErrDoesNotExist

	default:
		return errors.Annotate(err, "failed to read log stream").Err()
	}
}

// getLogEntriesImpl retrieves log entries from archive until complete.
func (s *storageImpl) getLogEntriesIter(c context.Context, st *getStrategy, cb storage.GetCallback) error {
	// Get our maximum byte limit. If we are externally constrained via MaxBytes,
	// apply that limit too.
	// Get an archive reader.
	var (
		offset = st.startOffset
		length = st.length()
	)

	log.Fields{
		"offset": offset,
		"length": length,
		"path":   s.Stream,
	}.Debugf(c, "Creating stream reader for range.")
	storageReader, err := s.Client.NewReader(s.Stream, int64(offset), length)
	if err != nil {
		log.WithError(err).Errorf(c, "Failed to create stream Reader.")
		return errors.Annotate(err, "failed to create stream Reader").Err()
	}
	defer func() {
		if tmpErr := storageReader.Close(); tmpErr != nil {
			// (Non-fatal)
			log.WithError(tmpErr).Warningf(c, "Error closing stream Reader.")
		}
	}()

	// Count how many bytes we've read.
	cr := iotools.CountingReader{Reader: storageReader}

	// Iteratively update our strategy's start offset each time we read a complete
	// frame.
	var (
		rio       = recordio.NewReader(&cr, maxStreamRecordSize)
		buf       bytes.Buffer
		remaining = st.count
	)
	for {
		// Reset the count so we know how much we read for this frame.
		cr.Count = 0

		sz, r, err := rio.ReadFrame()
		if err != nil {
			return errors.Annotate(err, "failed to read frame").Err()
		}

		buf.Reset()
		buf.Grow(int(sz))

		switch amt, err := buf.ReadFrom(r); {
		case err != nil:
			log.Fields{
				log.ErrorKey:  err,
				"frameOffset": offset,
				"frameSize":   sz,
			}.Errorf(c, "Failed to read frame data.")
			return errors.Annotate(err, "failed to read frame data").Err()

		case amt != sz:
			// If we didn't buffer the complete frame, we hit a premature EOF.
			return errors.Annotate(io.EOF, "incomplete frame read").Err()
		}

		// If we read from offset 0, the first frame will be the log stream's
		// descriptor, which we can discard.
		discardFrame := (offset == 0)
		offset += uint64(cr.Count)
		if discardFrame {
			continue
		}

		// Punt this log entry to our callback, if appropriate.
		entry := storage.MakeEntry(buf.Bytes(), -1)
		switch idx, err := entry.GetStreamIndex(); {
		case err != nil:
			log.Fields{
				log.ErrorKey:  err,
				"frameOffset": offset,
				"frameSize":   sz,
			}.Errorf(c, "Failed to get log entry index.")
			return errors.Annotate(err, "failed to get log entry index").Err()

		case idx < st.startIndex:
			// Skip this entry, as it's before the first requested entry.
			continue
		}

		// We want to punt this entry, but we also want to re-use our Buffer. Clone
		// its data so it is independent.
		entry.D = make([]byte, len(entry.D))
		copy(entry.D, buf.Bytes())
		if !cb(entry) {
			return nil
		}

		// Enforce our limit, if one is supplied.
		if remaining > 0 {
			remaining--
			if remaining == 0 {
				return nil
			}
		}
	}
}

func (s *storageImpl) Tail(c context.Context, project string, path types.StreamPath) (*storage.Entry, error) {
	idx, err := s.getIndex(c)
	if err != nil {
		return nil, err
	}

	// Get the offset that is as close to our tail record as possible. If we know
	// what that index is (from "idx"), we can request it directly. Otherwise, we
	// will get as close as possible and read forwards from there.
	req := storage.GetRequest{}
	switch {
	case idx.LastStreamIndex > 0:
		req.Index = types.MessageIndex(idx.LastStreamIndex)
		req.Limit = 1

	case len(idx.Entries) > 0:
		req.Index = types.MessageIndex(idx.Entries[len(idx.Entries)-1].StreamIndex)
	}

	// Build a Get strategy for our closest-to-Tail index.
	st := buildGetStrategy(&req, idx)
	if st == nil {
		return nil, storage.ErrDoesNotExist
	}

	// Read forwards to EOF. Retain the last entry that we read.
	var lastEntry *storage.Entry
	err = s.Get(c, req, func(e *storage.Entry) bool {
		lastEntry = e

		// We can stop if we have the last stream index and this is that index.
		if idx.LastStreamIndex > 0 {
			// Get the index for this entry.
			//
			// We can ignore this error, since "Get" will have already resolved the
			// index successfully.
			if sidx, _ := e.GetStreamIndex(); sidx == types.MessageIndex(idx.LastStreamIndex) {
				return false
			}
		}
		return true
	})
	switch {
	case err != nil:
		return nil, err

	case lastEntry == nil:
		return nil, storage.ErrDoesNotExist

	default:
		return lastEntry, nil
	}
}

// getIndex returns the cached log stream index, fetching it if necessary.
func (s *storageImpl) getIndex(c context.Context) (*logpb.LogIndex, error) {
	idx := s.index.Load()
	if idx != nil {
		return idx.(*logpb.LogIndex), nil
	}

	index, err := loadIndex(c, s.Client, s.Index, s.Cache)
	switch errors.Unwrap(err) {
	case nil:
		break

	case cloudStorage.ErrBucketNotExist, cloudStorage.ErrObjectNotExist:
		// Treat a missing index the same as an empty index.
		log.WithError(err).Warningf(c, "Index is invalid, using empty index.")
		index = &logpb.LogIndex{}

	default:
		return nil, err
	}

	s.index.Store(index)
	return index, nil
}

func loadIndex(c context.Context, client gs.Client, path gs.Path, cache storage.Cache) (*logpb.LogIndex, error) {
	// If there is no path, then return an empty index.
	if path == "" {
		log.Infof(c, "No index path, using empty index.")
		return &logpb.LogIndex{}, nil
	}

	// If we have a cache, see if the index is cached.
	var (
		indexData []byte
		cached    bool
	)
	if cache != nil {
		var ok bool
		indexData, ok = getCachedLogIndexData(c, cache, path)
		if ok {
			cached = true
		}
	}

	if indexData == nil {
		// No cache, or no cached entry. Load from storage.
		r, err := client.NewReader(path, 0, -1)
		if err != nil {
			log.WithError(err).Errorf(c, "Failed to create index Reader.")
			return nil, errors.Annotate(err, "failed to create index Reader").Err()
		}
		defer func() {
			if err := r.Close(); err != nil {
				log.WithError(err).Warningf(c, "Error closing index Reader.")
			}
		}()

		if indexData, err = io.ReadAll(r); err != nil {
			log.WithError(err).Errorf(c, "Failed to read index.")
			return nil, errors.Annotate(err, "failed to read index").Err()
		}
	}

	index := logpb.LogIndex{}
	if err := proto.Unmarshal(indexData, &index); err != nil {
		log.WithError(err).Errorf(c, "Failed to unmarshal index.")
		return nil, errors.Annotate(err, "failed to unmarshal index").Err()
	}

	// If the index is valid, but wasn't cached previously, then cache it.
	if cache != nil && !cached {
		putCachedLogIndexData(c, cache, path, indexData)
	}

	return &index, nil
}

type getStrategy struct {
	// startIndex is desired initial log entry index.
	startIndex types.MessageIndex

	// startOffset is the beginning byte offset of the log entry stream. This may
	// be lower than the offset of the starting record if the index is sparse.
	startOffset uint64
	// endOffset is the ending byte offset of the log entry stream. This will be
	// 0 if an end offset is not known.
	endOffset uint64

	// count is the number of log entries that will be fetched. If 0, no upper
	// bound was calculated.
	count uint64
}

func (gs *getStrategy) length() int64 {
	if gs.startOffset < gs.endOffset {
		return int64(gs.endOffset - gs.startOffset)
	}
	return -1
}

// setCount sets the `count` field. If called multiple times, the smallest
// assigned value will be retained.
func (gs *getStrategy) setCount(v uint64) {
	if gs.count == 0 || gs.count > v {
		gs.count = v
	}
}

func buildGetStrategy(req *storage.GetRequest, idx *logpb.LogIndex) *getStrategy {
	st := getStrategy{
		startIndex: req.Index,
	}

	// If the user has requested an index past the end of the stream, return no
	// entries (count == 0). This only works if the last stream index is known.
	if idx.LastStreamIndex > 0 && req.Index > types.MessageIndex(idx.LastStreamIndex) {
		return nil
	}

	// Identify the closest index entry to the requested log.
	//
	// If the requested log starts before the first index entry, we must read from
	// record #0.
	startIndexEntry := indexEntryFor(idx.Entries, req.Index)
	if startIndexEntry >= 0 {
		st.startOffset = idx.Entries[startIndexEntry].Offset
	}

	// Determine an upper bound based on our limits.
	//
	// If we have a count limit, identify the maximum entry that can be loaded,
	// find the index entry closest to it, and use that to determine our upper
	// bound.
	if req.Limit > 0 {
		st.setCount(uint64(req.Limit))

		// Find the index entry for the stream entry AFTER the last one we are going
		// to return.
		entryAfterGetBlock := req.Index + types.MessageIndex(req.Limit)
		endIndexEntry := indexEntryFor(idx.Entries, entryAfterGetBlock)
		switch {
		case endIndexEntry < 0:
			// The last possible request log entry is before the first index entry.
			// Read up to the first index entry.
			endIndexEntry = 0

		case endIndexEntry <= startIndexEntry:
			// The last possible request log entry is closest to the start index
			// entry. Use the index entry immediately after it.
			endIndexEntry = startIndexEntry + 1

		default:
			// We have the index entry <= the stream entry after the last one that we
			// will return.
			//
			// If we're sparse, this could be the index at or before our last entry.
			// If this is the case, use the next index entry, which will be after
			// "entryAfterGetBlock" (EAGB).
			//
			// START ------ LIMIT     (LIMIT+1)
			//   |          [IDX]         |          [IDX]
			// index          |   entryAfterGetBlock   |
			//           endIndexEntry         (endIndexEntry+1)
			if types.MessageIndex(idx.Entries[endIndexEntry].StreamIndex) < entryAfterGetBlock {
				endIndexEntry++
			}
		}

		// If we're pointing to a valid index entry, set our upper bound.
		if endIndexEntry < len(idx.Entries) {
			st.endOffset = idx.Entries[endIndexEntry].Offset
		}
	}

	return &st
}

// indexEntryFor identifies the log index entry closest (<=) to the specified
// index.
//
// If the first index entry is greater than our search index, -1 will be
// returned. This should never happen in practice, though, since our index
// construction always indexes log entry #0.
//
// It does this by performing a binary search over the index entries.
func indexEntryFor(entries []*logpb.LogIndex_Entry, i types.MessageIndex) int {
	ui := uint64(i)
	s := sort.Search(len(entries), func(i int) bool {
		return entries[i].StreamIndex > ui
	})

	// The returned index is the one immediately after the index that we want. If
	// our search returned 0, the first index entry is > our search entry, and we
	// will return nil.
	return s - 1
}


text:
#import <Foundation/Foundation.h>

@interface NSInvocation (MumbleKitAdditions)
+ (NSInvocation *) invocationWithTarget:(id)target selector:(SEL)selector;
+ (void **) nilPointerLocation;
- (void) invokeOnMainThread;
@end

redpajama_set_name: RedPajamaGithub


text:
'use strict';

var chalk = require('chalk');
var request = require('request');

function convertBTC() {
  var currency = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'USD';
  var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;

  var url = 'https://apiv2.bitcoinaverage.com/convert/global?from=BTC&to=' + currency + '&amount=' + amount;

  request(url, function (error, response, body) {
    var apiResponse = void 0;
    try {
      apiResponse = JSON.parse(body);
    } catch (parseError) {
      console.log(chalk.red('Something went wrong in the API. Try in a few minutes.'));
      return parseError;
    }

    console.log(chalk.red(amount) + ' BTC to ' + chalk.cyan(currency) + ' = ' + chalk.yellow(apiResponse.price));
  });
}

module.exports = convertBTC;


text:
#include <ArrayInfo.hpp>
#include <Array.hpp>
#include <select.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <kernel/select.hpp>

using af::dim4;

namespace cpu
{

template<typename T>
void select(Array<T> &out, const Array<char> &cond, const Array<T> &a, const Array<T> &b)
{
    out.eval();
    cond.eval();
    a.eval();
    b.eval();
    getQueue().enqueue(kernel::select<T>, out, cond, a, b);
}

template<typename T, bool flip>
void select_scalar(Array<T> &out, const Array<char> &cond, const Array<T> &a, const double &b)
{
    out.eval();
    cond.eval();
    a.eval();
    getQueue().enqueue(kernel::select_scalar<T, flip>, out, cond, a, b);
}

#define INSTANTIATE(T)                                              \
    template void select<T>(Array<T> &out, const Array<char> &cond, \
                            const Array<T> &a, const Array<T> &b);  \
    template void select_scalar<T, true >(Array<T> &out,            \
                                          const Array<char> &cond,  \
                                          const Array<T> &a,        \
                                          const double &b);         \
    template void select_scalar<T, false>(Array<T> &out, const      \
                                          Array<char> &cond,        \
                                          const Array<T> &a,        \
                                          const double &b);         \

INSTANTIATE(float  )
INSTANTIATE(double )
INSTANTIATE(cfloat )
INSTANTIATE(cdouble)
INSTANTIATE(int    )
INSTANTIATE(uint   )
INSTANTIATE(intl   )
INSTANTIATE(uintl  )
INSTANTIATE(char   )
INSTANTIATE(uchar  )
INSTANTIATE(short  )
INSTANTIATE(ushort )

}


text:
mkdir -p src build
cp ../../images/*.png *.html *.js *.json src
${CHROME_BIN} --pack-extension=src --pack-extension-key=unofficial_key.pem
cp src.crx build/rndphrase.crx

# ZIP file
cd src
zip ../build/rndphrase.zip *
cd ..


text:
namespace {

const char kApplicationName[] = "chrome-sync";

}  // namespace

namespace syncer {

SyncInvalidationListener::Delegate::~Delegate() {}

SyncInvalidationListener::SyncInvalidationListener(
    scoped_ptr<notifier::PushClient> push_client)
    : push_client_channel_(push_client.Pass()),
      sync_system_resources_(&push_client_channel_, this),
      delegate_(NULL),
      ticl_state_(DEFAULT_INVALIDATION_ERROR),
      push_client_state_(DEFAULT_INVALIDATION_ERROR),
      weak_ptr_factory_(this) {
  DCHECK(CalledOnValidThread());
  push_client_channel_.AddObserver(this);
}

SyncInvalidationListener::~SyncInvalidationListener() {
  DCHECK(CalledOnValidThread());
  push_client_channel_.RemoveObserver(this);
  Stop();
  DCHECK(!delegate_);
}

void SyncInvalidationListener::Start(
    const CreateInvalidationClientCallback&
        create_invalidation_client_callback,
    const std::string& client_id, const std::string& client_info,
    const std::string& invalidation_bootstrap_data,
    const UnackedInvalidationsMap& initial_unacked_invalidations,
    const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker,
    Delegate* delegate) {
  DCHECK(CalledOnValidThread());
  Stop();

  sync_system_resources_.set_platform(client_info);
  sync_system_resources_.Start();

  // The Storage resource is implemented as a write-through cache.  We populate
  // it with the initial state on startup, so subsequent writes go to disk and
  // update the in-memory cache, while reads just return the cached state.
  sync_system_resources_.storage()->SetInitialState(
      invalidation_bootstrap_data);

  unacked_invalidations_map_ = initial_unacked_invalidations;
  invalidation_state_tracker_ = invalidation_state_tracker;
  DCHECK(invalidation_state_tracker_.IsInitialized());

  DCHECK(!delegate_);
  DCHECK(delegate);
  delegate_ = delegate;

#if defined(OS_IOS)
  int client_type = ipc::invalidation::ClientType::CHROME_SYNC_IOS;
#else
  int client_type = ipc::invalidation::ClientType::CHROME_SYNC;
#endif
  invalidation_client_.reset(
      create_invalidation_client_callback.Run(
          &sync_system_resources_, client_type, client_id,
          kApplicationName, this));
  invalidation_client_->Start();

  registration_manager_.reset(
      new RegistrationManager(invalidation_client_.get()));
}

void SyncInvalidationListener::UpdateCredentials(
    const std::string& email, const std::string& token) {
  DCHECK(CalledOnValidThread());
  push_client_channel_.UpdateCredentials(email, token);
}

void SyncInvalidationListener::UpdateRegisteredIds(const ObjectIdSet& ids) {
  DCHECK(CalledOnValidThread());
  registered_ids_ = ids;
  // |ticl_state_| can go to INVALIDATIONS_ENABLED even without a
  // working XMPP connection (as observed by us), so check it instead
  // of GetState() (see http://crbug.com/139424).
  if (ticl_state_ == INVALIDATIONS_ENABLED && registration_manager_) {
    DoRegistrationUpdate();
  }
}

void SyncInvalidationListener::Ready(
    invalidation::InvalidationClient* client) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  ticl_state_ = INVALIDATIONS_ENABLED;
  EmitStateChange();
  DoRegistrationUpdate();
}

void SyncInvalidationListener::Invalidate(
    invalidation::InvalidationClient* client,
    const invalidation::Invalidation& invalidation,
    const invalidation::AckHandle& ack_handle) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  client->Acknowledge(ack_handle);

  const invalidation::ObjectId& id = invalidation.object_id();

  std::string payload;
  // payload() CHECK()'s has_payload(), so we must check it ourselves first.
  if (invalidation.has_payload())
    payload = invalidation.payload();

  DVLOG(2) << "Received invalidation with version " << invalidation.version()
           << " for " << ObjectIdToString(id);

  ObjectIdInvalidationMap invalidations;
  Invalidation inv = Invalidation::Init(id, invalidation.version(), payload);
  inv.set_ack_handler(GetThisAsAckHandler());
  invalidations.Insert(inv);

  DispatchInvalidations(invalidations);
}

void SyncInvalidationListener::InvalidateUnknownVersion(
    invalidation::InvalidationClient* client,
    const invalidation::ObjectId& object_id,
    const invalidation::AckHandle& ack_handle) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  DVLOG(1) << "InvalidateUnknownVersion";
  client->Acknowledge(ack_handle);

  ObjectIdInvalidationMap invalidations;
  Invalidation unknown_version = Invalidation::InitUnknownVersion(object_id);
  unknown_version.set_ack_handler(GetThisAsAckHandler());
  invalidations.Insert(unknown_version);

  DispatchInvalidations(invalidations);
}

// This should behave as if we got an invalidation with version
// UNKNOWN_OBJECT_VERSION for all known data types.
void SyncInvalidationListener::InvalidateAll(
    invalidation::InvalidationClient* client,
    const invalidation::AckHandle& ack_handle) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  DVLOG(1) << "InvalidateAll";
  client->Acknowledge(ack_handle);

  ObjectIdInvalidationMap invalidations;
  for (ObjectIdSet::iterator it = registered_ids_.begin();
       it != registered_ids_.end(); ++it) {
    Invalidation unknown_version = Invalidation::InitUnknownVersion(*it);
    unknown_version.set_ack_handler(GetThisAsAckHandler());
    invalidations.Insert(unknown_version);
  }

  DispatchInvalidations(invalidations);
}

// If a handler is registered, emit right away.  Otherwise, save it for later.
void SyncInvalidationListener::DispatchInvalidations(
    const ObjectIdInvalidationMap& invalidations) {
  DCHECK(CalledOnValidThread());

  ObjectIdInvalidationMap to_save = invalidations;
  ObjectIdInvalidationMap to_emit =
      invalidations.GetSubsetWithObjectIds(registered_ids_);

  SaveInvalidations(to_save);
  EmitSavedInvalidations(to_emit);
}

void SyncInvalidationListener::SaveInvalidations(
    const ObjectIdInvalidationMap& to_save) {
  ObjectIdSet objects_to_save = to_save.GetObjectIds();
  for (ObjectIdSet::const_iterator it = objects_to_save.begin();
       it != objects_to_save.end(); ++it) {
    UnackedInvalidationsMap::iterator lookup =
        unacked_invalidations_map_.find(*it);
    if (lookup == unacked_invalidations_map_.end()) {
      lookup = unacked_invalidations_map_.insert(
          std::make_pair(*it, UnackedInvalidationSet(*it))).first;
    }
    lookup->second.AddSet(to_save.ForObject(*it));
  }

  invalidation_state_tracker_.Call(
      FROM_HERE,
      &InvalidationStateTracker::SetSavedInvalidations,
      unacked_invalidations_map_);
}

void SyncInvalidationListener::EmitSavedInvalidations(
    const ObjectIdInvalidationMap& to_emit) {
  DVLOG(2) << "Emitting invalidations: " << to_emit.ToString();
  delegate_->OnInvalidate(to_emit);
}

void SyncInvalidationListener::InformRegistrationStatus(
      invalidation::InvalidationClient* client,
      const invalidation::ObjectId& object_id,
      InvalidationListener::RegistrationState new_state) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  DVLOG(1) << "InformRegistrationStatus: "
           << ObjectIdToString(object_id) << " " << new_state;

  if (new_state != InvalidationListener::REGISTERED) {
    // Let |registration_manager_| handle the registration backoff policy.
    registration_manager_->MarkRegistrationLost(object_id);
  }
}

void SyncInvalidationListener::InformRegistrationFailure(
    invalidation::InvalidationClient* client,
    const invalidation::ObjectId& object_id,
    bool is_transient,
    const std::string& error_message) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  DVLOG(1) << "InformRegistrationFailure: "
           << ObjectIdToString(object_id)
           << "is_transient=" << is_transient
           << ", message=" << error_message;

  if (is_transient) {
    // We don't care about |unknown_hint|; we let
    // |registration_manager_| handle the registration backoff policy.
    registration_manager_->MarkRegistrationLost(object_id);
  } else {
    // Non-transient failures require an action to resolve. This could happen
    // because:
    // - the server doesn't yet recognize the data type, which could happen for
    //   brand-new data types.
    // - the user has changed his password and hasn't updated it yet locally.
    // Either way, block future registration attempts for |object_id|. However,
    // we don't forget any saved invalidation state since we may use it once the
    // error is addressed.
    registration_manager_->DisableId(object_id);
  }
}

void SyncInvalidationListener::ReissueRegistrations(
    invalidation::InvalidationClient* client,
    const std::string& prefix,
    int prefix_length) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  DVLOG(1) << "AllRegistrationsLost";
  registration_manager_->MarkAllRegistrationsLost();
}

void SyncInvalidationListener::InformError(
    invalidation::InvalidationClient* client,
    const invalidation::ErrorInfo& error_info) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(client, invalidation_client_.get());
  LOG(ERROR) << "Ticl error " << error_info.error_reason() << ": "
             << error_info.error_message()
             << " (transient = " << error_info.is_transient() << ")";
  if (error_info.error_reason() == invalidation::ErrorReason::AUTH_FAILURE) {
    ticl_state_ = INVALIDATION_CREDENTIALS_REJECTED;
  } else {
    ticl_state_ = TRANSIENT_INVALIDATION_ERROR;
  }
  EmitStateChange();
}

void SyncInvalidationListener::Acknowledge(
  const invalidation::ObjectId& id,
  const syncer::AckHandle& handle) {
  UnackedInvalidationsMap::iterator lookup =
      unacked_invalidations_map_.find(id);
  if (lookup == unacked_invalidations_map_.end()) {
    DLOG(WARNING) << "Received acknowledgement for untracked object ID";
    return;
  }
  lookup->second.Acknowledge(handle);
  invalidation_state_tracker_.Call(
      FROM_HERE,
      &InvalidationStateTracker::SetSavedInvalidations,
      unacked_invalidations_map_);
}

void SyncInvalidationListener::Drop(
    const invalidation::ObjectId& id,
    const syncer::AckHandle& handle) {
  UnackedInvalidationsMap::iterator lookup =
      unacked_invalidations_map_.find(id);
  if (lookup == unacked_invalidations_map_.end()) {
    DLOG(WARNING) << "Received drop for untracked object ID";
    return;
  }
  lookup->second.Drop(handle);
  invalidation_state_tracker_.Call(
      FROM_HERE,
      &InvalidationStateTracker::SetSavedInvalidations,
      unacked_invalidations_map_);
}

void SyncInvalidationListener::WriteState(const std::string& state) {
  DCHECK(CalledOnValidThread());
  DVLOG(1) << "WriteState";
  invalidation_state_tracker_.Call(
      FROM_HERE, &InvalidationStateTracker::SetBootstrapData, state);
}

void SyncInvalidationListener::DoRegistrationUpdate() {
  DCHECK(CalledOnValidThread());
  const ObjectIdSet& unregistered_ids =
      registration_manager_->UpdateRegisteredIds(registered_ids_);
  for (ObjectIdSet::iterator it = unregistered_ids.begin();
       it != unregistered_ids.end(); ++it) {
    unacked_invalidations_map_.erase(*it);
  }
  invalidation_state_tracker_.Call(
      FROM_HERE,
      &InvalidationStateTracker::SetSavedInvalidations,
      unacked_invalidations_map_);

  ObjectIdInvalidationMap object_id_invalidation_map;
  for (UnackedInvalidationsMap::iterator map_it =
       unacked_invalidations_map_.begin();
       map_it != unacked_invalidations_map_.end(); ++map_it) {
    if (registered_ids_.find(map_it->first) == registered_ids_.end()) {
      continue;
    }
    map_it->second.ExportInvalidations(
        GetThisAsAckHandler(),
        &object_id_invalidation_map);
  }

  // There's no need to run these through DispatchInvalidations(); they've
  // already been saved to storage (that's where we found them) so all we need
  // to do now is emit them.
  EmitSavedInvalidations(object_id_invalidation_map);
}

void SyncInvalidationListener::StopForTest() {
  DCHECK(CalledOnValidThread());
  Stop();
}

void SyncInvalidationListener::Stop() {
  DCHECK(CalledOnValidThread());
  if (!invalidation_client_) {
    return;
  }

  registration_manager_.reset();
  sync_system_resources_.Stop();
  invalidation_client_->Stop();

  invalidation_client_.reset();
  delegate_ = NULL;

  ticl_state_ = DEFAULT_INVALIDATION_ERROR;
  push_client_state_ = DEFAULT_INVALIDATION_ERROR;
}

InvalidatorState SyncInvalidationListener::GetState() const {
  DCHECK(CalledOnValidThread());
  if (ticl_state_ == INVALIDATION_CREDENTIALS_REJECTED ||
      push_client_state_ == INVALIDATION_CREDENTIALS_REJECTED) {
    // If either the ticl or the push client rejected our credentials,
    // return INVALIDATION_CREDENTIALS_REJECTED.
    return INVALIDATION_CREDENTIALS_REJECTED;
  }
  if (ticl_state_ == INVALIDATIONS_ENABLED &&
      push_client_state_ == INVALIDATIONS_ENABLED) {
    // If the ticl is ready and the push client notifications are
    // enabled, return INVALIDATIONS_ENABLED.
    return INVALIDATIONS_ENABLED;
  }
  // Otherwise, we have a transient error.
  return TRANSIENT_INVALIDATION_ERROR;
}

void SyncInvalidationListener::EmitStateChange() {
  DCHECK(CalledOnValidThread());
  delegate_->OnInvalidatorStateChange(GetState());
}

WeakHandle<AckHandler> SyncInvalidationListener::GetThisAsAckHandler() {
  DCHECK(CalledOnValidThread());
  return WeakHandle<AckHandler>(weak_ptr_factory_.GetWeakPtr());
}

void SyncInvalidationListener::OnNetworkChannelStateChanged(
    InvalidatorState invalidator_state) {
  DCHECK(CalledOnValidThread());
  push_client_state_ = invalidator_state;
  EmitStateChange();
}

}  // namespace syncer


text:
'use strict';

angular
  .module('gtrApp', [
    'ui.router',
    'angular-uri',
    'gtrApp.config'
  ]).config(function ($stateProvider, $urlRouterProvider, config) {
    $stateProvider

      .state('auth', {
        url: '/auth',
        controller: 'AuthCtrl',
        reloadOnSearch: false
      })

      .state('main', {
        url: '/:team',
        templateUrl: 'views/main.html',
        controller: 'MainCtrl',
        resolve: {team: function($q, $stateParams, config) {
          var defer = $q.defer();
          if (config.teams[$stateParams.team]) {
            defer.resolve($stateParams.team);
          } else {
            defer.reject('Team does not exist');
          }

          return defer.promise;
        }}
      });

      $urlRouterProvider.otherwise('/' + Object.keys(config.teams)[0]);
  });


text:
 **Magic Circle against Law**

**School** abjuration [chaotic]; **Level** cleric 3, sorcerer/wizard 3

This spell functions like _ [magic circle against evil](magicCircleAgainstEvil#_magic-circle-against-evil), _except that it is similar to _ [protection from law](protectionFromLaw#_protection-from-law) _instead of _ [protection from evil](protectionFromEvil#_protection-from-evil)_, and it can imprison a nonchaotic called creature.


text:
export default func => (...args) => args.reduce(
    (func, arg) => func(arg),
    func
);


text:
title: Populating with Data Programmatically
page_title: Populating with Data Programmatically - RadGanttView
description: You can manually populate RadGanttView with the summary items, task items and milestone items.
slug: winforms/ganttview-/working-with-data/populating-with-data-programmatically
tags: populating,with,data,programmatically
published: True
position: 2
previous_url: ganttview-working-with-data-populating-with-data-programmatically
---

# Populating with Data Programmatically

__RadGanttview__ supports unbound mode as well, which means that you can manually populate it with the summary items, task items and milestone items (if needed). Then just set up the links between tasks and you have your gantt setup. The following example starts by setting the desired start and end range of the graphical element and then we are adding a few task items with sub items. At the end we are adding the links between the items.

{{source=..\SamplesCS\GanttView\WorkingWithData\PopulatingWithDataProgrammatically.cs region=PopulateData}}
{{source=..\SamplesVB\GanttView\WorkingWithData\PopulatingWithDataProgrammatically.vb region=PopulateData}}

````C#

this.radGanttView1.GanttViewElement.GraphicalViewElement.TimelineStart = new DateTime(2010, 10, 9);
this.radGanttView1.GanttViewElement.GraphicalViewElement.TimelineEnd = new DateTime(2010, 12, 10);

//setup data items
GanttViewDataItem item1 = new GanttViewDataItem();
item1.Start = new DateTime(2010, 10, 10);
item1.End = new DateTime(2010, 10, 15);
item1.Progress = 30m;
item1.Title = "Summary task.1. title";

GanttViewDataItem subitem11 = new GanttViewDataItem();
subitem11.Start = new DateTime(2010, 10, 10);
subitem11.End = new DateTime(2010, 10, 12);
subitem11.Progress = 10m;
subitem11.Title = "Sub-task.1.1 title";

GanttViewDataItem subitem12 = new GanttViewDataItem();
subitem12.Start = new DateTime(2010, 10, 12);
subitem12.End = new DateTime(2010, 10, 15);
subitem12.Progress = 20m;
subitem12.Title = "Sub-task.1.2 title";

//add subitems
item1.Items.Add(subitem11);
item1.Items.Add(subitem12);

this.radGanttView1.Items.Add(item1);

GanttViewDataItem item2 = new GanttViewDataItem();
item2.Start = new DateTime(2010, 10, 12);
item2.End = new DateTime(2010, 10, 18);
item2.Progress = 40m;
item2.Title = "Summary task.2. title";

GanttViewDataItem subitem21 = new GanttViewDataItem();
subitem21.Start = new DateTime(2010, 10, 12);
subitem21.End = new DateTime(2010, 10, 13);
subitem21.Progress = 10m;
subitem21.Title = "Sub-task.2.1 title";

GanttViewDataItem subitem22 = new GanttViewDataItem();
subitem22.Start = new DateTime(2010, 10, 13);
subitem22.End = new DateTime(2010, 10, 18);
subitem22.Progress = 30m;
subitem22.Title = "Sub-task.2.2 title";

GanttViewDataItem subitem23 = new GanttViewDataItem();
subitem23.Start = new DateTime(2010, 10, 18);
subitem23.End = new DateTime(2010, 10, 18);
subitem23.Title = "Sub-task.2.3 title";

//add subitems
item2.Items.Add(subitem21);
item2.Items.Add(subitem22);
item2.Items.Add(subitem23);

this.radGanttView1.Items.Add(item2);

//add links between items
GanttViewLinkDataItem link1 = new GanttViewLinkDataItem();
link1.StartItem = subitem11;
link1.EndItem = subitem12;
link1.LinkType = TasksLinkType.FinishToStart;
this.radGanttView1.Links.Add(link1);

GanttViewLinkDataItem link2 = new GanttViewLinkDataItem();
link2.StartItem = subitem21;
link2.EndItem = subitem22;
link2.LinkType = TasksLinkType.StartToStart;
this.radGanttView1.Links.Add(link2);

GanttViewLinkDataItem link3 = new GanttViewLinkDataItem();
link3.StartItem = subitem22;
link3.EndItem = subitem23;
link3.LinkType = TasksLinkType.FinishToStart;
this.radGanttView1.Links.Add(link3);

````
````VB.NET
Me.RadGanttView1.GanttViewElement.GraphicalViewElement.TimelineStart = New DateTime(2010, 10, 9)
Me.RadGanttView1.GanttViewElement.GraphicalViewElement.TimelineEnd = New DateTime(2010, 12, 10)
'setup data items
Dim item1 As New GanttViewDataItem()
item1.Start = New DateTime(2010, 10, 10)
item1.[End] = New DateTime(2010, 10, 15)
item1.Progress = 30D
item1.Title = "Summary task.1. title"
Dim subitem11 As New GanttViewDataItem()
subitem11.Start = New DateTime(2010, 10, 10)
subitem11.[End] = New DateTime(2010, 10, 12)
subitem11.Progress = 10D
subitem11.Title = "Sub-task.1.1 title"
Dim subitem12 As New GanttViewDataItem()
subitem12.Start = New DateTime(2010, 10, 12)
subitem12.[End] = New DateTime(2010, 10, 15)
subitem12.Progress = 20D
subitem12.Title = "Sub-task.1.2 title"
'add subitems
item1.Items.Add(subitem11)
item1.Items.Add(subitem12)
Me.RadGanttView1.Items.Add(item1)
Dim item2 As New GanttViewDataItem()
item2.Start = New DateTime(2010, 10, 12)
item2.[End] = New DateTime(2010, 10, 18)
item2.Progress = 40D
item2.Title = "Summary task.2. title"
Dim subitem21 As New GanttViewDataItem()
subitem21.Start = New DateTime(2010, 10, 12)
subitem21.[End] = New DateTime(2010, 10, 13)
subitem21.Progress = 10D
subitem21.Title = "Sub-task.2.1 title"
Dim subitem22 As New GanttViewDataItem()
subitem22.Start = New DateTime(2010, 10, 13)
subitem22.[End] = New DateTime(2010, 10, 18)
subitem22.Progress = 30D
subitem22.Title = "Sub-task.2.2 title"
Dim subitem23 As New GanttViewDataItem()
subitem23.Start = New DateTime(2010, 10, 18)
subitem23.[End] = New DateTime(2010, 10, 18)
subitem23.Title = "Sub-task.2.3 title"
'add subitems
item2.Items.Add(subitem21)
item2.Items.Add(subitem22)
item2.Items.Add(subitem23)
Me.RadGanttView1.Items.Add(item2)
'add links between items
Dim link1 As New GanttViewLinkDataItem()
link1.StartItem = subitem11
link1.EndItem = subitem12
link1.LinkType = TasksLinkType.FinishToStart
Me.RadGanttView1.Links.Add(link1)
Dim link2 As New GanttViewLinkDataItem()
link2.StartItem = subitem21
link2.EndItem = subitem22
link2.LinkType = TasksLinkType.StartToStart
Me.RadGanttView1.Links.Add(link2)
Dim link3 As New GanttViewLinkDataItem()
link3.StartItem = subitem22
link3.EndItem = subitem23
link3.LinkType = TasksLinkType.FinishToStart
Me.RadGanttView1.Links.Add(link3)

````

{{endregion}}

![ganttview-working-with-data-populating-with-data-programmatically 001](images/ganttview-working-with-data-populating-with-data-programmatically001.png)

Now we can just add the desired columns to be displayed in __GanttViewTextViewElement__. During the column initialization we will pass a string to specify the __FieldName__ so the column will know which fields of the tasks to display. In addition this string will also be used as header text.

{{source=..\SamplesCS\GanttView\WorkingWithData\PopulatingWithDataProgrammatically.cs region=AddColumns}}
{{source=..\SamplesVB\GanttView\WorkingWithData\PopulatingWithDataProgrammatically.vb region=AddColumns}}

````C#

GanttViewTextViewColumn titleColumn = new GanttViewTextViewColumn("Title");
GanttViewTextViewColumn startColumn = new GanttViewTextViewColumn("Start");
GanttViewTextViewColumn endColumn = new GanttViewTextViewColumn("End");

this.radGanttView1.GanttViewElement.Columns.Add(titleColumn);
this.radGanttView1.GanttViewElement.Columns.Add(startColumn);
this.radGanttView1.GanttViewElement.Columns.Add(endColumn);

````
````VB.NET
Dim titleColumn As New GanttViewTextViewColumn("Title")
Dim startColumn As New GanttViewTextViewColumn("Start")
Dim endColumn As New GanttViewTextViewColumn("End")
Me.RadGanttView1.GanttViewElement.Columns.Add(titleColumn)
Me.RadGanttView1.GanttViewElement.Columns.Add(startColumn)
Me.RadGanttView1.GanttViewElement.Columns.Add(endColumn)

````

{{endregion}}

![ganttview-working-with-data-populating-with-data-programmatically 002](images/ganttview-working-with-data-populating-with-data-programmatically002.png)

# See Also

* [Binding to Database]({%slug winforms/ganttview-/working-with-data/binding-to-database%})
* [Data Binding Basics]({%slug winforms/ganttview-/working-with-data/data-binding-basics%})
* [Importing XML from MS Project]({%slug winforms/ganttview-/working-with-data/importing-xml-from-ms-project%})
* [Link Type Converter]({%slug winforms/ganttview-/working-with-data/link-type-converter%})
* [Adding new items]({%slug winforms/ganttview-/working-with-data/adding-new-items%})


text:
typedef struct ALLEGRO_SYSTEM_IPHONE {
    ALLEGRO_SYSTEM system;

    ALLEGRO_MUTEX *mutex;
    ALLEGRO_COND *cond;

   bool has_shutdown, wants_shutdown;
   int visuals_count;
   ALLEGRO_EXTRA_DISPLAY_SETTINGS **visuals;
} ALLEGRO_SYSTEM_IPHONE;

typedef struct ALLEGRO_DISPLAY_IPHONE {
    ALLEGRO_DISPLAY display;
} ALLEGRO_DISPLAY_IPHONE;

void _al_iphone_init_path(void);
void _al_iphone_add_view(ALLEGRO_DISPLAY *d);
void _al_iphone_make_view_current(void);
void _al_iphone_flip_view(void);
void _al_iphone_reset_framebuffer(void);
ALLEGRO_SYSTEM_INTERFACE *_al_get_iphone_system_interface(void);
ALLEGRO_DISPLAY_INTERFACE *_al_get_iphone_display_interface(void);
ALLEGRO_PATH *_al_iphone_get_path(int id);
ALLEGRO_KEYBOARD_DRIVER *_al_get_iphone_keyboard_driver(void);
ALLEGRO_MOUSE_DRIVER *_al_get_iphone_mouse_driver(void);
ALLEGRO_JOYSTICK_DRIVER *_al_get_iphone_joystick_driver(void);
void _al_iphone_setup_opengl_view(ALLEGRO_DISPLAY *d);
void _al_iphone_generate_mouse_event(unsigned int type,
   int x, int y, unsigned int button, ALLEGRO_DISPLAY *d);
void _al_iphone_update_visuals(void);
void _al_iphone_accelerometer_control(int frequency);
void _al_iphone_generate_joystick_event(float x, float y, float z);
void _al_iphone_await_termination(void);
void _al_iphone_get_screen_size(int *w, int *h);
void _al_iphone_translate_from_screen(ALLEGRO_DISPLAY *d, int *x, int *y);
void _al_iphone_translate_to_screen(ALLEGRO_DISPLAY *d, int *x, int *y);
void _al_iphone_clip(ALLEGRO_BITMAP const *bitmap, int x_1, int y_1, int x_2, int y_2);
float _al_iphone_get_screen_scale(void);


text:
ForkStalkSharp
==============

Just some stalking project.

It takes a repository owner and name and returns information about updated forks.

Useful for tracking forks which have contributions and have not been upstreamed yet
(merged or had a pull request opened).

Notes:
======
 * It does a lot of queries. Currently, forks count isn't limited on the API side,
so if the repository has many forks, it will end up receiving data for all of them.
 * The queries are done on the non-authenticated rate limit or the user's own limit.


text:
namespace rocksdb {

static std::string IKey(const std::string& user_key,
                        uint64_t seq,
                        ValueType vt) {
  std::string encoded;
  AppendInternalKey(&encoded, ParsedInternalKey(user_key, seq, vt));
  return encoded;
}

static std::string Shorten(const std::string& s, const std::string& l) {
  std::string result = s;
  InternalKeyComparator(BytewiseComparator()).FindShortestSeparator(&result, l);
  return result;
}

static std::string ShortSuccessor(const std::string& s) {
  std::string result = s;
  InternalKeyComparator(BytewiseComparator()).FindShortSuccessor(&result);
  return result;
}

static void TestKey(const std::string& key,
                    uint64_t seq,
                    ValueType vt) {
  std::string encoded = IKey(key, seq, vt);

  Slice in(encoded);
  ParsedInternalKey decoded("", 0, kTypeValue);

  ASSERT_TRUE(ParseInternalKey(in, &decoded));
  ASSERT_EQ(key, decoded.user_key.ToString());
  ASSERT_EQ(seq, decoded.sequence);
  ASSERT_EQ(vt, decoded.type);

  ASSERT_TRUE(!ParseInternalKey(Slice("bar"), &decoded));
}

class FormatTest : public testing::Test {};

TEST_F(FormatTest, InternalKey_EncodeDecode) {
  const char* keys[] = { "", "k", "hello", "longggggggggggggggggggggg" };
  const uint64_t seq[] = {
    1, 2, 3,
    (1ull << 8) - 1, 1ull << 8, (1ull << 8) + 1,
    (1ull << 16) - 1, 1ull << 16, (1ull << 16) + 1,
    (1ull << 32) - 1, 1ull << 32, (1ull << 32) + 1
  };
  for (unsigned int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
    for (unsigned int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {
      TestKey(keys[k], seq[s], kTypeValue);
      TestKey("hello", 1, kTypeDeletion);
    }
  }
}

TEST_F(FormatTest, InternalKeyShortSeparator) {
  // When user keys are same
  ASSERT_EQ(IKey("foo", 100, kTypeValue),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("foo", 99, kTypeValue)));
  ASSERT_EQ(IKey("foo", 100, kTypeValue),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("foo", 101, kTypeValue)));
  ASSERT_EQ(IKey("foo", 100, kTypeValue),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("foo", 100, kTypeValue)));
  ASSERT_EQ(IKey("foo", 100, kTypeValue),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("foo", 100, kTypeDeletion)));

  // When user keys are misordered
  ASSERT_EQ(IKey("foo", 100, kTypeValue),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("bar", 99, kTypeValue)));

  // When user keys are different, but correctly ordered
  ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("hello", 200, kTypeValue)));

  ASSERT_EQ(IKey("ABC2", kMaxSequenceNumber, kValueTypeForSeek),
            Shorten(IKey("ABC1AAAAA", 100, kTypeValue),
                    IKey("ABC2ABB", 200, kTypeValue)));

  ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
            Shorten(IKey("AAA1AAA", 100, kTypeValue),
                    IKey("AAA2AA", 200, kTypeValue)));

  ASSERT_EQ(
      IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
      Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA4", 200, kTypeValue)));

  ASSERT_EQ(
      IKey("AAA1B", kMaxSequenceNumber, kValueTypeForSeek),
      Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));

  ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
            Shorten(IKey("AAA1AAA", 100, kTypeValue),
                    IKey("AAA2A", 200, kTypeValue)));

  ASSERT_EQ(
      IKey("AAA1", 100, kTypeValue),
      Shorten(IKey("AAA1", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));

  // When start user key is prefix of limit user key
  ASSERT_EQ(IKey("foo", 100, kTypeValue),
            Shorten(IKey("foo", 100, kTypeValue),
                    IKey("foobar", 200, kTypeValue)));

  // When limit user key is prefix of start user key
  ASSERT_EQ(IKey("foobar", 100, kTypeValue),
            Shorten(IKey("foobar", 100, kTypeValue),
                    IKey("foo", 200, kTypeValue)));
}

TEST_F(FormatTest, InternalKeyShortestSuccessor) {
  ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
            ShortSuccessor(IKey("foo", 100, kTypeValue)));
  ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),
            ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));
}

TEST_F(FormatTest, IterKeyOperation) {
  IterKey k;
  const char p[] = "abcdefghijklmnopqrstuvwxyz";
  const char q[] = "0123456789";

  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string(""));

  k.TrimAppend(0, p, 3);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("abc"));

  k.TrimAppend(1, p, 3);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("aabc"));

  k.TrimAppend(0, p, 26);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("abcdefghijklmnopqrstuvwxyz"));

  k.TrimAppend(26, q, 10);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("abcdefghijklmnopqrstuvwxyz0123456789"));

  k.TrimAppend(36, q, 1);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("abcdefghijklmnopqrstuvwxyz01234567890"));

  k.TrimAppend(26, q, 1);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("abcdefghijklmnopqrstuvwxyz0"));

  // Size going up, memory allocation is triggered
  k.TrimAppend(27, p, 26);
  ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
            std::string("abcdefghijklmnopqrstuvwxyz0"
                        "abcdefghijklmnopqrstuvwxyz"));
}

TEST_F(FormatTest, UpdateInternalKey) {
  std::string user_key("abcdefghijklmnopqrstuvwxyz");
  uint64_t new_seq = 0x123456;
  ValueType new_val_type = kTypeDeletion;

  std::string ikey;
  AppendInternalKey(&ikey, ParsedInternalKey(user_key, 100U, kTypeValue));
  size_t ikey_size = ikey.size();
  UpdateInternalKey(&ikey, new_seq, new_val_type);
  ASSERT_EQ(ikey_size, ikey.size());

  Slice in(ikey);
  ParsedInternalKey decoded;
  ASSERT_TRUE(ParseInternalKey(in, &decoded));
  ASSERT_EQ(user_key, decoded.user_key.ToString());
  ASSERT_EQ(new_seq, decoded.sequence);
  ASSERT_EQ(new_val_type, decoded.type);
}

}  // namespace rocksdb

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}


text:
<?php

/**
 * @license see LICENSE
 */

namespace UForm\Form\Element\Primary\Structural;

use UForm\Form\Element\Primary\Structural;

class Divider extends Structural
{

    public function __construct()
    {
        parent::__construct();
        $this->addSemanticType('divider');
    }
}


text:
execfile('scripts/aux.py')
project,record,parameters = sumatra_record(__file__)
vars().update(parameters)

# MAIN SCRIPT
import numpy as np
import pylab as pl

np.random.seed(seed)
distr = getattr(np.random, distr)
data = distr(size=n)

fig,ax = pl.subplots(1)
ax.hist(data)
# END OF MAIN SCRIPT

if record:
    np.savetxt('Data/%s.dat' %record.label, data)
    pl.savefig('Data/%s.png' %record.label)
    sumatra_save(project,record)

pl.show()


text:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ---
// Author: Sainbayar Sukhbaatar
//         Dai Mikurube
//

#include "deep-heap-profile.h"

#ifdef DEEP_HEAP_PROFILE
#include <algorithm>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>  // for getpagesize and getpid
#endif  // HAVE_UNISTD_H

#include "base/cycleclock.h"
#include "base/sysinfo.h"
#include "internal_logging.h"  // for ASSERT, etc

static const int kProfilerBufferSize = 1 << 20;
static const int kHashTableSize = 179999;  // The same as heap-profile-table.cc.

static const int PAGEMAP_BYTES = 8;
static const uint64 MAX_ADDRESS = kuint64max;

// Header strings of the dumped heap profile.
static const char kProfileHeader[] = "heap profile: ";
static const char kProfileVersion[] = "DUMP_DEEP_5";
static const char kGlobalStatsHeader[] = "GLOBAL_STATS:\n";
static const char kStacktraceHeader[] = "STACKTRACES:\n";
static const char kProcSelfMapsHeader[] = "\nMAPPED_LIBRARIES:\n";

static const char kVirtualLabel[] = "virtual";
static const char kCommittedLabel[] = "committed";

DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile,
                                 const char* prefix)
    : pagemap_fd_(-1),
      most_recent_pid_(-1),
      stats_(),
      dump_count_(0),
      filename_prefix_(NULL),
      profiler_buffer_(NULL),
      bucket_id_(0),
      heap_profile_(heap_profile) {
  const int deep_table_bytes = kHashTableSize * sizeof(*deep_table_);
  deep_table_ = reinterpret_cast<DeepBucket**>(
      heap_profile->alloc_(deep_table_bytes));
  memset(deep_table_, 0, deep_table_bytes);

  // Copy filename prefix.
  const int prefix_length = strlen(prefix);
  filename_prefix_ =
      reinterpret_cast<char*>(heap_profile_->alloc_(prefix_length + 1));
  memcpy(filename_prefix_, prefix, prefix_length);
  filename_prefix_[prefix_length] = '\0';

  profiler_buffer_ =
      reinterpret_cast<char*>(heap_profile_->alloc_(kProfilerBufferSize));
}

DeepHeapProfile::~DeepHeapProfile() {
  heap_profile_->dealloc_(profiler_buffer_);
  heap_profile_->dealloc_(filename_prefix_);
  DeallocateDeepBucketTable(deep_table_, heap_profile_);
  deep_table_ = NULL;
}

int DeepHeapProfile::FillOrderedProfile(char buffer[], int buffer_size) {
#ifndef NDEBUG
  int64 starting_cycles = CycleClock::Now();
#endif
  ++dump_count_;

  // Re-open files in /proc/pid/ if the process is newly forked one.
  if (most_recent_pid_ != getpid()) {
    most_recent_pid_ = getpid();
    pagemap_fd_ = OpenProcPagemap();

    for (int i = 0; i < kHashTableSize; i++) {
      for (DeepBucket* deep_bucket = deep_table_[i];
           deep_bucket != NULL;
           deep_bucket = deep_bucket->next) {
        deep_bucket->is_logged = false;
      }
    }

    // Write maps into "|filename_prefix|.<pid>.maps" using global buffer.
    WriteMapsToFile(filename_prefix_, 0,
                    kProfilerBufferSize, profiler_buffer_);
  }
  // Write maps into "|filename_prefix|.<pid>.|count|.maps" using global buffer.
  WriteMapsToFile(filename_prefix_, dump_count_,
                  kProfilerBufferSize, profiler_buffer_);

  // Reset committed sizes of buckets.
  ResetCommittedSize(deep_table_);

  // Allocate a list for mmap'ed regions.
  num_mmap_allocations_ = 0;
  heap_profile_->mmap_address_map_->Iterate(CountMMap, this);
  mmap_list_length_ = 0;
  mmap_list_ = reinterpret_cast<MMapListEntry*>(heap_profile_->alloc_(
      sizeof(MMapListEntry) * num_mmap_allocations_));

  // Touch all the allocated pages.  Touching is required to avoid new page
  // commitment while filling the list in SnapshotGlobalStatsWithoutMalloc.
  for (int i = 0;
       i < num_mmap_allocations_;
       i += getpagesize() / 2 / sizeof(MMapListEntry))
    mmap_list_[i].first_address = 0;
  mmap_list_[num_mmap_allocations_ - 1].last_address = 0;

  SnapshotGlobalStatsWithoutMalloc(pagemap_fd_, &stats_, NULL, 0);
  size_t anonymous_committed = stats_.all[ANONYMOUS].committed_bytes();

  // Note: Try to minimize the number of calls to malloc in the following
  // region, up until we call WriteBucketsToBucketFile(), near the end of this
  // function.  Calling malloc in the region may make a gap between the
  // observed size and actual memory allocation.  The gap is less than or equal
  // to the size of allocated memory in the region.  Calls to malloc won't
  // break anything, but can add some noise to the recorded information.
  // TODO(dmikurube): Eliminate dynamic memory allocation caused by snprintf.
  // glibc's snprintf internally allocates memory by alloca normally, but it
  // allocates memory by malloc if large memory is required.

  // Record committed sizes.
  SnapshotAllAllocsWithoutMalloc();

  // Check if committed bytes changed during SnapshotAllAllocsWithoutMalloc.
  SnapshotGlobalStatsWithoutMalloc(pagemap_fd_, &stats_,
                                   mmap_list_, mmap_list_length_);
#ifndef NDEBUG
  size_t committed_difference =
      stats_.all[ANONYMOUS].committed_bytes() - anonymous_committed;
  if (committed_difference != 0) {
    RAW_LOG(0, "Difference in committed size: %ld", committed_difference);
  }
#endif

  // Start filling buffer with the ordered profile.
  int printed = snprintf(buffer, buffer_size,
                         "%s%s\n", kProfileHeader, kProfileVersion);
  if (IsPrintedStringValid(printed, buffer_size, 0)) {
    return 0;
  }
  int used_in_buffer = printed;

  // Fill buffer with the global stats.
  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                     kGlobalStatsHeader);
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  used_in_buffer = UnparseGlobalStats(used_in_buffer, buffer_size, buffer);

  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                     kStacktraceHeader);
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                     "%10s %10s\n", kVirtualLabel, kCommittedLabel);
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  // Fill buffer.
  used_in_buffer = SnapshotBucketTableWithoutMalloc(deep_table_,
                                                    used_in_buffer,
                                                    buffer_size,
                                                    buffer);

  RAW_DCHECK(used_in_buffer < buffer_size, "");

  // Note: Memory snapshots are complete, and malloc may again be used freely.

  heap_profile_->dealloc_(mmap_list_);
  mmap_list_ = NULL;

  // Write the bucket listing into a .bucket file.
  WriteBucketsToBucketFile();

#ifndef NDEBUG
  int64 elapsed_cycles = CycleClock::Now() - starting_cycles;
  double elapsed_seconds = elapsed_cycles / CyclesPerSecond();
  RAW_LOG(0, "Time spent on DeepProfiler: %.3f sec\n", elapsed_seconds);
#endif

  return used_in_buffer;
}

void DeepHeapProfile::RegionStats::Initialize() {
  virtual_bytes_ = 0;
  committed_bytes_ = 0;
}

void DeepHeapProfile::RegionStats::Record(
    int pagemap_fd, uint64 first_address, uint64 last_address) {
  virtual_bytes_ += static_cast<size_t>(last_address - first_address + 1);
  committed_bytes_ += GetCommittedSize(pagemap_fd, first_address, last_address);
}

// static
void DeepHeapProfile::DeallocateDeepBucketTable(
    DeepBucket** deep_table, HeapProfileTable* heap_profile) {
  ASSERT(deep_table != NULL);
  for (int db = 0; db < kHashTableSize; db++) {
    for (DeepBucket* x = deep_table[db]; x != 0; /**/) {
      DeepBucket* db = x;
      x = x->next;
      heap_profile->dealloc_(db);
    }
  }
  heap_profile->dealloc_(deep_table);
}

// static
bool DeepHeapProfile::IsPrintedStringValid(int printed,
                                           int buffer_size,
                                           int used_in_buffer) {
  return printed < 0 || printed >= buffer_size - used_in_buffer;
}

// static
int DeepHeapProfile::OpenProcPagemap() {
  char filename[100];
  snprintf(filename, sizeof(filename), "/proc/%d/pagemap",
           static_cast<int>(getpid()));
  int pagemap_fd = open(filename, O_RDONLY);
  RAW_DCHECK(pagemap_fd != -1, "Failed to open /proc/self/pagemap");
  return pagemap_fd;
}

// static
bool DeepHeapProfile::SeekProcPagemap(int pagemap_fd, uint64 address) {
  int64 index = (address / getpagesize()) * PAGEMAP_BYTES;
  int64 offset = lseek64(pagemap_fd, index, SEEK_SET);
  RAW_DCHECK(offset == index, "Failed in seeking.");
  return offset >= 0;
}

// static
bool DeepHeapProfile::ReadProcPagemap(int pagemap_fd, PageState* state) {
  static const uint64 U64_1 = 1;
  static const uint64 PFN_FILTER = (U64_1 << 55) - U64_1;
  static const uint64 PAGE_PRESENT = U64_1 << 63;
  static const uint64 PAGE_SWAP = U64_1 << 62;
  static const uint64 PAGE_RESERVED = U64_1 << 61;
  static const uint64 FLAG_NOPAGE = U64_1 << 20;
  static const uint64 FLAG_KSM = U64_1 << 21;
  static const uint64 FLAG_MMAP = U64_1 << 11;

  uint64 pagemap_value;
  int result = read(pagemap_fd, &pagemap_value, PAGEMAP_BYTES);
  if (result != PAGEMAP_BYTES) {
    return false;
  }

  // Check if the page is committed.
  state->is_committed = (pagemap_value & (PAGE_PRESENT | PAGE_SWAP));

  state->is_present = (pagemap_value & PAGE_PRESENT);
  state->is_swapped = (pagemap_value & PAGE_SWAP);
  state->is_shared = false;

  return true;
}

// static
size_t DeepHeapProfile::GetCommittedSize(
    int pagemap_fd, uint64 first_address, uint64 last_address) {
  int page_size = getpagesize();
  uint64 page_address = (first_address / page_size) * page_size;
  size_t committed_size = 0;

  SeekProcPagemap(pagemap_fd, first_address);

  // Check every page on which the allocation resides.
  while (page_address <= last_address) {
    // Read corresponding physical page.
    PageState state;
    // TODO(dmikurube): Read pagemap in bulk for speed.
    if (ReadProcPagemap(pagemap_fd, &state) == false) {
      // We can't read the last region (e.g vsyscall).
#ifndef NDEBUG
      RAW_LOG(0, "pagemap read failed @ %#llx %"PRId64" bytes",
              first_address, last_address - first_address + 1);
#endif
      return 0;
    }

    if (state.is_committed) {
      // Calculate the size of the allocation part in this page.
      size_t bytes = page_size;

      // If looking at the last page in a given region.
      if (last_address <= page_address - 1 + page_size) {
        bytes = last_address - page_address + 1;
      }

      // If looking at the first page in a given region.
      if (page_address < first_address) {
        bytes -= first_address - page_address;
      }

      committed_size += bytes;
    }
    if (page_address > MAX_ADDRESS - page_size) {
      break;
    }
    page_address += page_size;
  }

  return committed_size;
}

// static
void DeepHeapProfile::WriteMapsToFile(const char* filename_prefix,
                                      unsigned count,
                                      int buffer_size,
                                      char buffer[]) {
  char filename[100];
  if (count > 0) {
    snprintf(filename, sizeof(filename),
             "%s.%05d.%04d.maps", filename_prefix, static_cast<int>(getpid()),
             count);
  } else {
    snprintf(filename, sizeof(filename),
             "%s.%05d.maps", filename_prefix, static_cast<int>(getpid()));
  }

  RawFD maps_fd = RawOpenForWriting(filename);
  RAW_DCHECK(maps_fd != kIllegalRawFD, "");

  int map_length;
  bool wrote_all;
  map_length = tcmalloc::FillProcSelfMaps(buffer, buffer_size, &wrote_all);
  RAW_DCHECK(wrote_all, "");
  RAW_DCHECK(map_length <= buffer_size, "");
  RawWrite(maps_fd, buffer, map_length);
  RawClose(maps_fd);
}

// TODO(dmikurube): Eliminate dynamic memory allocation caused by snprintf.
// ProcMapsIterator uses snprintf internally in construction.
// static
void DeepHeapProfile::SnapshotGlobalStatsWithoutMalloc(int pagemap_fd,
                                                       GlobalStats* stats,
                                                       MMapListEntry* mmap_list,
                                                       int mmap_list_length) {
  ProcMapsIterator::Buffer iterator_buffer;
  ProcMapsIterator iterator(0, &iterator_buffer);
  uint64 first_address, last_address, offset;
  int64 unused_inode;
  char* flags;
  char* filename;
  int mmap_list_index = 0;
  enum MapsRegionType type;

  for (int i = 0; i < NUMBER_OF_MAPS_REGION_TYPES; ++i) {
    stats->all[i].Initialize();
    stats->nonprofiled[i].Initialize();
  }

  while (iterator.Next(&first_address, &last_address,
                       &flags, &offset, &unused_inode, &filename)) {
    // 'last_address' should be the last inclusive address of the region.
    last_address -= 1;
    if (strcmp("[vsyscall]", filename) == 0) {
      continue;  // Reading pagemap will fail in [vsyscall].
    }

    type = ABSENT;
    if (filename[0] == '/') {
      if (flags[2] == 'x')
        type = FILE_EXEC;
      else
        type = FILE_NONEXEC;
    } else if (filename[0] == '\0' || filename[0] == '\n') {
      type = ANONYMOUS;
    } else if (strcmp(filename, "[stack]") == 0) {
      type = STACK;
    } else {
      type = OTHER;
    }
    stats->all[type].Record(pagemap_fd, first_address, last_address);

    // TODO(dmikurube): Avoid double-counting of pagemap.
    // Counts nonprofiled memory regions in /proc/<pid>/maps.
    if (mmap_list != NULL) {
      // It assumes that every mmap'ed region is included in one maps line.
      uint64 cursor = first_address;
      bool first = true;

      do {
        if (!first) {
          mmap_list[mmap_list_index].type = type;
          cursor = mmap_list[mmap_list_index].last_address + 1;
          ++mmap_list_index;
        }
        first = false;

        uint64 last_address_of_nonprofiled;
        // If the next mmap entry is away from the current maps line.
        if (mmap_list_index >= mmap_list_length ||
            mmap_list[mmap_list_index].first_address > last_address) {
          last_address_of_nonprofiled = last_address;
        } else {
          last_address_of_nonprofiled =
              mmap_list[mmap_list_index].first_address - 1;
        }

        if (last_address_of_nonprofiled + 1 > cursor) {
          stats->nonprofiled[type].Record(
              pagemap_fd, cursor, last_address_of_nonprofiled);
          cursor = last_address_of_nonprofiled + 1;
        }
      } while (mmap_list_index < mmap_list_length &&
               mmap_list[mmap_list_index].last_address <= last_address);
    }
  }
}

// This hash function is from heap-profile-table:GetBucket.
// static
void DeepHeapProfile::AddIntegerToHashValue(
    uintptr_t add, uintptr_t* hash_value) {
  *hash_value += add;
  *hash_value += *hash_value << 10;
  *hash_value ^= *hash_value >> 6;
}

// GetDeepBucket is implemented as almost copy of heap-profile-table:GetBucket.
// It's to avoid modifying heap-profile-table.  Performance issues can be
// ignored in usual Chromium runs.  Another hash function can be tried in an
// easy way in future.
DeepHeapProfile::DeepBucket* DeepHeapProfile::GetDeepBucket(
    Bucket* bucket, bool is_mmap,
#if defined(TYPE_PROFILING)
    const std::type_info* type,
#endif
    DeepBucket **table) {
  // Make hash-value
  uintptr_t h = 0;

  AddIntegerToHashValue(reinterpret_cast<uintptr_t>(bucket), &h);
  if (is_mmap) {
    AddIntegerToHashValue(1, &h);
  } else {
    AddIntegerToHashValue(0, &h);
  }

#if defined(TYPE_PROFILING)
  if (type == NULL) {
    AddIntegerToHashValue(0, &h);
  } else {
    AddIntegerToHashValue(reinterpret_cast<uintptr_t>(type->name()), &h);
  }
#endif

  h += h << 3;
  h ^= h >> 11;

  // Lookup stack trace in table
  unsigned int buck = ((unsigned int) h) % kHashTableSize;
  for (DeepBucket* db = table[buck]; db != 0; db = db->next) {
    if (db->bucket == bucket) {
      return db;
    }
  }

  // Create new bucket
  DeepBucket* db =
      reinterpret_cast<DeepBucket*>(heap_profile_->alloc_(sizeof(DeepBucket)));
  memset(db, 0, sizeof(*db));
  db->bucket         = bucket;
#if defined(TYPE_PROFILING)
  db->type           = type;
#endif
  db->committed_size = 0;
  db->is_mmap        = is_mmap;
  db->id             = (bucket_id_++);
  db->is_logged      = false;
  db->next           = table[buck];
  table[buck] = db;
  return db;
}

void DeepHeapProfile::ResetCommittedSize(DeepBucket** deep_table) {
  for (int i = 0; i < kHashTableSize; i++) {
    for (DeepBucket* deep_bucket = deep_table[i];
         deep_bucket != NULL;
         deep_bucket = deep_bucket->next) {
      deep_bucket->committed_size = 0;
    }
  }
}

// TODO(dmikurube): Eliminate dynamic memory allocation caused by snprintf.
int DeepHeapProfile::SnapshotBucketTableWithoutMalloc(DeepBucket** deep_table,
                                                      int used_in_buffer,
                                                      int buffer_size,
                                                      char buffer[]) {
  for (int i = 0; i < kHashTableSize; i++) {
    for (DeepBucket* deep_bucket = deep_table[i];
         deep_bucket != NULL;
         deep_bucket = deep_bucket->next) {
      Bucket* bucket = deep_bucket->bucket;
      if (bucket->alloc_size - bucket->free_size == 0) {
        continue;  // Skip empty buckets.
      }
      used_in_buffer = UnparseBucket(
          *deep_bucket, "", used_in_buffer, buffer_size, buffer, NULL);
    }
  }
  return used_in_buffer;
}

// static
bool DeepHeapProfile::ByFirstAddress(const MMapListEntry& a,
                                     const MMapListEntry& b) {
  return a.first_address < b.first_address;
}

// static
void DeepHeapProfile::CountMMap(const void* pointer,
                                AllocValue* alloc_value,
                                DeepHeapProfile* deep_profile) {
  ++deep_profile->num_mmap_allocations_;
}

void DeepHeapProfile::RecordAlloc(const void* pointer,
                                  AllocValue* alloc_value,
                                  DeepHeapProfile* deep_profile) {
  uint64 address = reinterpret_cast<uintptr_t>(pointer);
  size_t committed = GetCommittedSize(deep_profile->pagemap_fd_,
      address, address + alloc_value->bytes - 1);

  DeepBucket* deep_bucket = deep_profile->GetDeepBucket(
      alloc_value->bucket(), /* is_mmap */ false,
#if defined(TYPE_PROFILING)
      LookupType(pointer),
#endif
      deep_profile->deep_table_);
  deep_bucket->committed_size += committed;
  deep_profile->stats_.profiled_malloc.AddToVirtualBytes(alloc_value->bytes);
  deep_profile->stats_.profiled_malloc.AddToCommittedBytes(committed);
}

void DeepHeapProfile::RecordMMap(const void* pointer,
                                 AllocValue* alloc_value,
                                 DeepHeapProfile* deep_profile) {
  uint64 address = reinterpret_cast<uintptr_t>(pointer);
  size_t committed = GetCommittedSize(deep_profile->pagemap_fd_,
      address, address + alloc_value->bytes - 1);

  DeepBucket* deep_bucket = deep_profile->GetDeepBucket(
      alloc_value->bucket(), /* is_mmap */ true,
#if defined(TYPE_PROFILING)
      NULL,
#endif
      deep_profile->deep_table_);
  deep_bucket->committed_size += committed;
  deep_profile->stats_.profiled_mmap.AddToVirtualBytes(alloc_value->bytes);
  deep_profile->stats_.profiled_mmap.AddToCommittedBytes(committed);

  if (deep_profile->mmap_list_length_ < deep_profile->num_mmap_allocations_) {
    deep_profile->mmap_list_[deep_profile->mmap_list_length_].first_address =
        address;
    deep_profile->mmap_list_[deep_profile->mmap_list_length_].last_address =
        address - 1 + alloc_value->bytes;
    deep_profile->mmap_list_[deep_profile->mmap_list_length_].type = ABSENT;
    ++deep_profile->mmap_list_length_;
  } else {
    RAW_LOG(0, "Unexpected number of mmap entries: %d/%d",
            deep_profile->mmap_list_length_,
            deep_profile->num_mmap_allocations_);
  }
}

void DeepHeapProfile::SnapshotAllAllocsWithoutMalloc() {
  stats_.profiled_mmap.Initialize();
  stats_.profiled_malloc.Initialize();

  // malloc allocations.
  heap_profile_->alloc_address_map_->Iterate(RecordAlloc, this);

  // mmap allocations.
  heap_profile_->mmap_address_map_->Iterate(RecordMMap, this);
  std::sort(mmap_list_, mmap_list_ + mmap_list_length_, ByFirstAddress);
}

int DeepHeapProfile::FillBucketForBucketFile(const DeepBucket* deep_bucket,
                                             int buffer_size,
                                             char buffer[]) {
  const Bucket* bucket = deep_bucket->bucket;
  int printed = snprintf(buffer, buffer_size, "%05d", deep_bucket->id);
  if (IsPrintedStringValid(printed, buffer_size, 0)) {
    return 0;
  }
  int used_in_buffer = printed;

  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                     " %s", deep_bucket->is_mmap ? "mmap" : "malloc");
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

#if defined(TYPE_PROFILING)
  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                     " t0x%" PRIxPTR,
                     reinterpret_cast<uintptr_t>(deep_bucket->type));
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  if (deep_bucket->type == NULL) {
    printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                       " nno_typeinfo");
  } else {
    printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                       " n%s", deep_bucket->type->name());
  }
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;
#endif

  for (int depth = 0; depth < bucket->depth; depth++) {
    printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                       " 0x%08" PRIxPTR,
                       reinterpret_cast<uintptr_t>(bucket->stack[depth]));
    if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
      return used_in_buffer;
    }
    used_in_buffer += printed;
  }
  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                     "\n");
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  return used_in_buffer;
}

void DeepHeapProfile::WriteBucketsTableToBucketFile(DeepBucket** deep_table,
                                                    RawFD bucket_fd) {
  // We will use the global buffer here.
  char* buffer = profiler_buffer_;
  int buffer_size = kProfilerBufferSize;
  int used_in_buffer = 0;

  for (int i = 0; i < kHashTableSize; i++) {
    for (DeepBucket* deep_bucket = deep_table[i];
         deep_bucket != NULL;
         deep_bucket = deep_bucket->next) {
      Bucket* bucket = deep_bucket->bucket;
      if (deep_bucket->is_logged) {
        continue;  // Skip the bucket if it is already logged.
      }
      if (bucket->alloc_size - bucket->free_size <= 64) {
        continue;  // Skip small buckets.
      }

      used_in_buffer += FillBucketForBucketFile(
          deep_bucket, buffer_size - used_in_buffer, buffer + used_in_buffer);
      deep_bucket->is_logged = true;

      // Write to file if buffer 80% full.
      if (used_in_buffer > buffer_size * 0.8) {
        RawWrite(bucket_fd, buffer, used_in_buffer);
        used_in_buffer = 0;
      }
    }
  }

  RawWrite(bucket_fd, buffer, used_in_buffer);
}

void DeepHeapProfile::WriteBucketsToBucketFile() {
  char filename[100];
  snprintf(filename, sizeof(filename),
           "%s.%05d.%04d.buckets", filename_prefix_, getpid(), dump_count_);
  RawFD bucket_fd = RawOpenForWriting(filename);
  RAW_DCHECK(bucket_fd != kIllegalRawFD, "");

  WriteBucketsTableToBucketFile(deep_table_, bucket_fd);

  RawClose(bucket_fd);
}

int DeepHeapProfile::UnparseBucket(const DeepBucket& deep_bucket,
                                   const char* extra,
                                   int used_in_buffer,
                                   int buffer_size,
                                   char* buffer,
                                   Stats* profile_stats) {
  const Bucket& bucket = *deep_bucket.bucket;
  if (profile_stats != NULL) {
    profile_stats->allocs += bucket.allocs;
    profile_stats->alloc_size += bucket.alloc_size;
    profile_stats->frees += bucket.frees;
    profile_stats->free_size += bucket.free_size;
  }

  int printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                         "%10"PRId64" %10"PRId64" %6d %6d @%s %d\n",
                         bucket.alloc_size - bucket.free_size,
                         deep_bucket.committed_size,
                         bucket.allocs, bucket.frees, extra, deep_bucket.id);
  // If it looks like the snprintf failed, ignore the fact we printed anything.
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  return used_in_buffer;
}

int DeepHeapProfile::UnparseRegionStats(const RegionStats* stats,
                                        const char* name,
                                        int used_in_buffer,
                                        int buffer_size,
                                        char* buffer) {
  int printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                         "%25s %12ld %12ld\n",
                         name, stats->virtual_bytes(),
                         stats->committed_bytes());
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  return used_in_buffer;
}

int DeepHeapProfile::UnparseGlobalStats(int used_in_buffer,
                                        int buffer_size,
                                        char* buffer) {
  RegionStats all_total;
  RegionStats nonprofiled_total;
  for (int i = 0; i < NUMBER_OF_MAPS_REGION_TYPES; ++i) {
    all_total.AddAnotherRegionStat(stats_.all[i]);
    nonprofiled_total.AddAnotherRegionStat(stats_.nonprofiled[i]);
  }
  int printed = snprintf(
      buffer + used_in_buffer, buffer_size - used_in_buffer,
      "# total (%lu) %c= profiled-mmap (%lu) + nonprofiled-* (%lu)\n",
      all_total.committed_bytes(),
      all_total.committed_bytes() ==
          stats_.profiled_mmap.committed_bytes() +
          nonprofiled_total.committed_bytes() ? '=' : '!',
      stats_.profiled_mmap.committed_bytes(),
      nonprofiled_total.committed_bytes());
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
                         "%25s %12s %12s\n", "",
                         kVirtualLabel, kCommittedLabel);
  if (IsPrintedStringValid(printed, buffer_size, used_in_buffer)) {
    return used_in_buffer;
  }
  used_in_buffer += printed;

  used_in_buffer = UnparseRegionStats(&(all_total),
      "total", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.all[FILE_EXEC]),
      "file-exec", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.all[FILE_NONEXEC]),
      "file-nonexec", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.all[ANONYMOUS]),
      "anonymous", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.all[STACK]),
      "stack", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.all[OTHER]),
      "other", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(nonprofiled_total),
      "nonprofiled-total", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.nonprofiled[ABSENT]),
      "nonprofiled-absent", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.nonprofiled[ANONYMOUS]),
      "nonprofiled-anonymous", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.nonprofiled[FILE_EXEC]),
      "nonprofiled-file-exec", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.nonprofiled[FILE_NONEXEC]),
      "nonprofiled-file-nonexec", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.nonprofiled[STACK]),
      "nonprofiled-stack", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.nonprofiled[OTHER]),
      "nonprofiled-other", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.profiled_mmap),
      "profiled-mmap", used_in_buffer, buffer_size, buffer);
  used_in_buffer = UnparseRegionStats(&(stats_.profiled_malloc),
      "profiled-malloc", used_in_buffer, buffer_size, buffer);
  return used_in_buffer;
}
#else  // DEEP_HEAP_PROFILE

DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile,
                                 const char* prefix)
    : heap_profile_(heap_profile) {
}

DeepHeapProfile::~DeepHeapProfile() {
}

int DeepHeapProfile::FillOrderedProfile(char buffer[], int buffer_size) {
  return heap_profile_->FillOrderedProfile(buffer, buffer_size);
}

#endif  // DEEP_HEAP_PROFILE


text:
#include "OgreStableHeaders.h"

#include "OgreArchive.h"
#include "OgreException.h"

namespace Ogre {
    //---------------------------------------------------------------------
    DataStreamPtr Archive::create(const String&)
    {
        OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
                    "This archive does not support creation of files.",
                    "Archive::create");
    }
    //---------------------------------------------------------------------
    void Archive::remove(const String&)
    {
        OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
                    "This archive does not support removal of files.",
                    "Archive::remove");
    }
    //---------------------------------------------------------------------
}


text:
namespace ModelGraphSTD
{
    public enum LineStyle : byte
    {
        PointToPoint = 0,
        SimpleSpline = 1,
        DoubleSpline = 2,
    }
}


text:
<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: google/cloud/aiplatform/v1/entity_type.proto

namespace GPBMetadata\Google\Cloud\Aiplatform\V1;

class EntityType
{
    public static $is_initialized = false;

    public static function initOnce() {
        $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();

        if (static::$is_initialized == true) {
          return;
        }
        \GPBMetadata\Google\Api\FieldBehavior::initOnce();
        \GPBMetadata\Google\Api\Resource::initOnce();
        \GPBMetadata\Google\Cloud\Aiplatform\V1\FeaturestoreMonitoring::initOnce();
        \GPBMetadata\Google\Protobuf\Timestamp::initOnce();
        $pool->internalAddGeneratedFile(
            '
¸
,google/cloud/aiplatform/v1/entity_type.protogoogle.cloud.aiplatform.v1google/api/resource.proto8google/cloud/aiplatform/v1/featurestore_monitoring.protogoogle/protobuf/timestamp.proto"—

EntityType
name (	BàA
description (	BàA4
create_time (2.google.protobuf.TimestampBàA4
update_time (2.google.protobuf.TimestampBàAG
labels (22.google.cloud.aiplatform.v1.EntityType.LabelsEntryBàA
etag (	BàAX
monitoring_config (28.google.cloud.aiplatform.v1.FeaturestoreMonitoringConfigBàA-
LabelsEntry
key (
value (	:8:ŠêA†
$aiplatform.googleapis.com/EntityType^projects/{project}/locations/{location}/featurestores/{featurestore}/entityTypes/{entity_type}BÓ
com.google.cloud.aiplatform.v1BEntityTypeProtoPZDgoogle.golang.org/genproto/googleapis/cloud/aiplatform/v1;aiplatformªGoogle.Cloud.AIPlatform.V1ÊGoogle\\Cloud\\AIPlatform\\V1êGoogle::Cloud::AIPlatform::V1bproto3'
        , true);

        static::$is_initialized = true;
    }
}


text:
Matrix is an open network and anyone can participate in it. As a result, a
very wide range of content exists, and it is important to empower users to be
able to select which content they wish to see, and which they wish to block. By
extension, room moderators and server admins should also be able to select
which content they do not wish to host in their rooms and servers.

The protocol's position in this solution should be one of neutrality: it
should not be deciding what content is undesirable for any particular entity and
should instead be empowering those entities to make their own decisions. This
proposal introduces "moderation policy rooms" as a basic mechanism to help users
manage this process, by providing a way of modelling sets of servers, rooms and users
which can then be used to make filtering decisions. This proposal makes no
attempt at interpreting the model and actually making those decisions however.

To reaffirm: where this proposal says that some content is undesirable it does not intend to
bias the reader into what that could entail. Undesirability is purely in the hands of the
entity perceiving the content. For example, someone who believes birthday cake is undesirable
is perfectly valid in taking that position and is encouraged by this proposal to set up or
use a policy room which prevents birthday cake from coming across their field of view.

## Proposal

Moderation policy lists, also known as ban lists in this proposal, are stored as room state events,
allowing for structures and concepts to be reused without defining a new room version. This
proposal does not make any restrictions on how the rooms are configured, just that the state
events described here are represented in a room. For example, a room which is invite only is
just as valid as a room that is not: the important details are specific state events and not
the accessibility, retention, or other aspects of the room.

Ban lists are stored as `m.policy.rule.<kind>` state events, with state keys being arbitrary IDs
assigned by the sender. The `<kind>` is currently one of `user`, `room`, and `server`. Three
fields are defined in `content`:

* `entity` (`string`) - **Required.** The entity/entities the recommendation applies to. Simple globs are supported
  for defining entities (`*` and `?` as wildcards, just like `m.room.server_acl`).
* `recommendation` (`enum`) - **Required.** The action subscribed entities should take against
  the described entity. Currently only `m.ban` is defined (see below).
* `reason` (`string`) - **Required.** The human-readable description for the recommendation.

Invalid or missing fields are to be treated as though the rule doesn't exist. This is to
allow for rules to be deleted while state events cannot be deleted in Matrix.

An example set of minimal state events for banning `@alice:example.org`, `!matrix:example.org`,
`evil.example.org`, and `*.evil.example.org` would be:

```json
[
    {
        "type": "m.policy.rule.user",
        "state_key": "rule_1",
        "content": {
            "entity": "@alice:example.org",
            "recommendation": "m.ban",
            "reason": "undesirable behaviour"
        }
    },
    {
        "type": "m.policy.rule.room",
        "state_key": "rule_2",
        "content": {
            "entity": "!matrix:example.org",
            "recommendation": "m.ban",
            "reason": "undesirable content"
        }
    },
    {
        "type": "m.policy.rule.server",
        "state_key": "rule_3",
        "content": {
            "entity": "evil.example.org",
            "recommendation": "m.ban",
            "reason": "undesirable engagement"
        }
    },
    {
        "type": "m.policy.rule.server",
        "state_key": "rule_4",
        "content": {
            "entity": "*.evil.example.org",
            "recommendation": "m.ban",
            "reason": "undesirable engagement"
        }
    }
]
```

When the entity is a room, it can be a room alias or ID - the subscriber is responsible for
resolving it to a room ID (if it wants to).

Non-standard recommendations are permitted using the Java package naming convention for
namespacing. A `recommendation` is just a recommendation: how implementations apply the rules
is left as a concern for those implementations. The only standard `recommendation` currently
defined is `m.ban`: The `entity` should be banned from participation where possible.

The enforcement mechanism of `m.ban` is deliberately left as an implementation detail to avoid the
protocol imposing its opinion on how the lists are interpreted. However, a suggestion for
a simple implementation is as follows:

* Is a user...
  * Applied to a user: The user should be added to the subscriber's ignore list.
  * Applied to a room: The user should be banned from the room (either on sight or immediately).
  * Applied to a server: The user should not be allowed to send invites to users on the server.
* Is a room...
  * Applied to a user: The user should leave the room and not join it
    ([MSC2270](https://github.com/matrix-org/matrix-doc/pull/2270)-style ignore).
  * Applied to a room: No-op because a room cannot ban itself.
  * Applied to a server: The server should prevent users from joining the room and from receiving
    invites to it (similar to the `shutdown_room` API in Synapse).
* Is a server...
  * Applied to a user: The user should not receive events or invites from the server.
  * Applied to a room: The server is added as a denied server in the ACLs.
  * Applied to a server: The subscriber should avoid federating with the server as much as
    possible by blocking invites from the server and not sending traffic unless strictly
    required (no outbound invites).

Other entities could be represented by this recommendation as well, however as per the
introduction to this proposal they are strictly out of scope. An example would be an integration
manager which doesn't want to offer integrations to banned entities - this is an implementation
detail for the integration manager to solve.

A new event type is introduced here instead of reusing existing events (membership, ACLs, etc)
because the implication of a recommendation/rule is less clear when using the more narrow-scoped
events. For example, a philosophical question arises over what a `membership` of `ban` means to a server
subscribed to the list. More questions get raised when the `membership` of a user isn't `ban`,
but is `join` or similar instead - if the subscriber was mirroring events, it would be inclined
to try and sync membership lists, which this proposal attempts to avoid by using a more generic
and neutral event type.

How subscriptions to ban lists are handled is also left as an implementation
detail (to avoid unnecessarily blocking progress on this MSC). The subscriber
could be anything that speaks Matrix, therefore this proposal makes no attempt
to describe how this should work for everything. Some ideas for how this could
be implemented include joining the ban list room to watch for updates and
applying them automatically, however there is no requirement that the
subscriber needs to join the room: they could peek or poll at an interval
instead, which is just as valid.

To ease sharing of these ban list rooms, a system very similar to [MSC1951's sharable URLs](
https://github.com/matrix-org/matrix-doc/pull/1951/files#diff-4ee6ed0ee1f2df73efac5fa9a9835642R50-R70)
is defined. There are two ways to share the ban list: a link to the room as one would when
sharing any reference to any other room ("please add `#bans:example.org` to your block list"),
or by using plain `http` or `https` URLs. Just like in MSC1951, the URL when approached with
a `Accept: application/json` header or has `.json` appended to the end of the URL should return
a `room_uri` which is a reference to the ban list room. Currently, the reference would be a
`matrix.to` URI, however in the future this could be a `mx://` or similar URL. When not approached
with the intent of JSON, the service could return a user-friendly page describing what is included
in the ban list.

## Future considerations

This proposal notably does not define specific behaviour for AI or machine learning applications.
Implementations are currently able to apply AI/ML to their systems if they see fit (for example,
spam detection or undesirable content being uploaded), however no specification is proposed
here to make the interaction standardized.

This proposal additionally does not describe how a server could subscribe to a ban list: this
is left for the server to figure out (possibly by using a utility user account?) potentially
with the support of other proposals, such as [MSC1777](https://github.com/matrix-org/matrix-doc/pull/1777).

Further work on reputation systems could enhance ban lists by adding additional metrics to
assert their validity. This proposal assumes social trust ("don't use it if you
don't trust the creator") over verifiable/provable trust - a future proposal can easily add
such systems to these ban lists.

This proposal intentionally does not handle how a server could assist the user in preventing
undesirable content or subscribing to ban lists. Users already have some tools at their disposal,
such as being able to ignore other users, and are encouraged to make use of those first. Other
proposals are encouraged to specify what additional tools might look like to better handle
ban lists.

Media (uploaded content) is not handled by this proposal either. This is a concern left for
other proposals like [MSC2278](https://github.com/matrix-org/matrix-doc/pull/2278) and
[MSC701](https://github.com/matrix-org/matrix-doc/issues/701).

## Security considerations

Using this solution one can build a social system of shared blacklists, which
may create a divide within established communities if not carefully deployed.
This may well not be a suitable answer for all communities.

Depending on how the implementations handle subscriptions, user IDs may be linked to ban
lists and therefore expose the views of that user. Using the example from the introduction,
if a user who does not like birthday cake were to join the ban list room for blocking
birthday cake, that user's preference would be exposed to any other observers of that ban
list. Proposals like [MSC1228](https://github.com/matrix-org/matrix-doc/issues/1228) and
[MSC1777](https://github.com/matrix-org/matrix-doc/pull/1777) could help solve this.

## Implementation notes

This proposal is partially implemented by [mjolnir](https://github.com/matrix-org/mjolnir)
using the `org.matrix.mjolnir.*` namespace until this becomes stable. This results in
the following mappings:

* `m.policy.rule.user` => `org.matrix.mjolnir.rule.user`
* `m.policy.rule.room` => `org.matrix.mjolnir.rule.room`
* `m.policy.rule.server` => `org.matrix.mjolnir.rule.server`
* `m.ban` => `org.matrix.mjolnir.ban`


text:
START_DELAY=5
OPENNMS_DATA_DIR=/opennms-data
OPENNMS_HOME=/opt/opennms
OPENNMS_DATASOURCES_TPL=/tmp/opennms-datasources.xml.tpl
OPENNMS_DATASOURCES_CFG=${OPENNMS_HOME}/etc/opennms-datasources.xml

# Error codes
E_ILLEGAL_ARGS=126

# Help function used in error messages and -h option
usage() {
  echo ""
  echo "Docker entry script for OpenNMS service container"
  echo ""
  echo "-f: Start OpenNMS in foreground with existing configuration."
  echo "-h: Show this help."
  echo "-i: Initialize Java environment, database and pristine OpenNMS configuration files, do not start OpenNMS."
  echo "-s: Initialize environment like -i and start OpenNMS in foreground."
  echo ""
}

initdb() {
  if [ ! -d ${OPENNMS_HOME} ]; then
    echo "OpenNMS home directory doesn't exist in ${OPENNMS_HOME}."
    exit ${E_ILLEGAL_ARGS}
  fi

  if [ ! -f ${OPENNMS_HOME}/etc/configured ]; then
    envsubst < ${OPENNMS_DATASOURCES_TPL} > ${OPENNMS_DATASOURCES_CFG}

    # Allow connection to Karaf console into Docker container
    sed -i "s,sshHost=127.0.0.1,sshHost=0.0.0.0," ${OPENNMS_HOME}/etc/org.apache.karaf.shell.cfg
    cd ${OPENNMS_HOME}/bin
    ./runjava -s
    sleep ${START_DELAY}
    ./install -dis
  else
    echo "OpenNMS is already configured skip initdb."
  fi
}

initConfig() {
  if [ ! "$(ls --ignore .git --ignore .gitignore --ignore ${OPENNMS_DATASOURCES_CFG} -A ${OPENNMS_HOME}/etc)"  ]; then
    cp -r ${OPENNMS_HOME}/share/etc-pristine/* ${OPENNMS_HOME}/etc/
  else
    echo "OpenNMS configuration already initialized."
  fi
}

initData() {
  # Create OpenNMS data directories
  mkdir -p ${OPENNMS_DATA_DIR}/logs \
           ${OPENNMS_DATA_DIR}/rrd/response \
           ${OPENNMS_DATA_DIR}/rrd/snmp \
           ${OPENNMS_DATA_DIR}/reports

  # Remove symlinks and pristine empty data directories
  rm -rf $OPENNMS_HOME/logs
  rm -rf ${OPENNMS_HOME}/share/rrd
  rm -rf ${OPENNMS_HOME}/share/reports

  # Create links to directories which can be mounted into a data container
  ln -s ${OPENNMS_DATA_DIR}/logs ${OPENNMS_HOME}/logs
  ln -s ${OPENNMS_DATA_DIR}/rrd ${OPENNMS_HOME}/share/rrd
  ln -s ${OPENNMS_DATA_DIR}/reports ${OPENNMS_HOME}/share/reports
}

start() {
  cd ${OPENNMS_HOME}/bin
  sleep ${START_DELAY}
  exec ./opennms -f start
}

# Evaluate arguments for build script.
if [[ "${#}" == 0 ]]; then
  usage
  exit ${E_ILLEGAL_ARGS}
fi

# Evaluate arguments for build script.
while getopts fhis flag; do
  case ${flag} in
    f)
      start
      exit
      ;;
    h)
      usage
      exit
      ;;
    i)
      initConfig
      initdb
      initData
      exit
      ;;
    s)
      initConfig
      initdb
      initData
      start
      exit
      ;;
    *)
      usage
      exit ${E_ILLEGAL_ARGS}
      ;;
  esac
done

# Strip of all remaining arguments
shift $((OPTIND - 1));

# Check if there are remaining arguments
if [[ "${#}" > 0 ]]; then
  echo "Error: To many arguments: ${*}."
  usage
  exit ${E_ILLEGAL_ARGS}
fi