text:
package server

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/nats-io/gnatsd/server/pse"
	"github.com/nats-io/jwt"
)

const (
	connectEventSubj         = "$SYS.ACCOUNT.%s.CONNECT"
	disconnectEventSubj      = "$SYS.ACCOUNT.%s.DISCONNECT"
	accConnsReqSubj          = "$SYS.REQ.ACCOUNT.%s.CONNS"
	accUpdateEventSubj       = "$SYS.ACCOUNT.%s.CLAIMS.UPDATE"
	connsRespSubj            = "$SYS._INBOX_.%s"
	accConnsEventSubj        = "$SYS.SERVER.ACCOUNT.%s.CONNS"
	shutdownEventSubj        = "$SYS.SERVER.%s.SHUTDOWN"
	authErrorEventSubj       = "$SYS.SERVER.%s.CLIENT.AUTH.ERR"
	serverStatsSubj          = "$SYS.SERVER.%s.STATSZ"
	serverStatsReqSubj       = "$SYS.REQ.SERVER.%s.STATSZ"
	serverStatsPingReqSubj   = "$SYS.REQ.SERVER.PING"
	leafNodeConnectEventSubj = "$SYS.ACCOUNT.%s.LEAFNODE.CONNECT"

	shutdownEventTokens = 4
	serverSubjectIndex  = 2
	accUpdateTokens     = 5
	accUpdateAccIndex   = 2
)

// Used to send and receive messages from inside the server.
type internal struct {
	account *Account
	client  *client
	seq     uint64
	sid     uint64
	servers map[string]*serverUpdate
	sweeper *time.Timer
	stmr    *time.Timer
	subs    map[string]msgHandler
	sendq   chan *pubMsg
	wg      sync.WaitGroup
	orphMax time.Duration
	chkOrph time.Duration
	statsz  time.Duration
}

// ServerStatsMsg is sent periodically with stats updates.
type ServerStatsMsg struct {
	Server ServerInfo  `json:"server"`
	Stats  ServerStats `json:"statsz"`
}

// ConnectEventMsg is sent when a new connection is made that is part of an account.
type ConnectEventMsg struct {
	Server ServerInfo `json:"server"`
	Client ClientInfo `json:"client"`
}

// DisconnectEventMsg is sent when a new connection previously defined from a
// ConnectEventMsg is closed.
type DisconnectEventMsg struct {
	Server   ServerInfo `json:"server"`
	Client   ClientInfo `json:"client"`
	Sent     DataStats  `json:"sent"`
	Received DataStats  `json:"received"`
	Reason   string     `json:"reason"`
}

// AccountNumConns is an event that will be sent from a server that is tracking
// a given account when the number of connections changes. It will also HB
// updates in the absence of any changes.
type AccountNumConns struct {
	Server     ServerInfo `json:"server"`
	Account    string     `json:"acc"`
	Conns      int        `json:"conns"`
	LeafNodes  int        `json:"leafnodes"`
	TotalConns int        `json:"total_conns"`
}

// accNumConnsReq is sent when we are starting to track an account for the first
// time. We will request others send info to us about their local state.
type accNumConnsReq struct {
	Server  ServerInfo `json:"server"`
	Account string     `json:"acc"`
}

type ServerInfo struct {
	Host    string    `json:"host"`
	ID      string    `json:"id"`
	Cluster string    `json:"cluster,omitempty"`
	Version string    `json:"ver"`
	Seq     uint64    `json:"seq"`
	Time    time.Time `json:"time"`
}

// ClientInfo is detailed information about the client forming a connection.
type ClientInfo struct {
	Start   time.Time  `json:"start,omitempty"`
	Host    string     `json:"host,omitempty"`
	ID      uint64     `json:"id"`
	Account string     `json:"acc"`
	User    string     `json:"user,omitempty"`
	Name    string     `json:"name,omitempty"`
	Lang    string     `json:"lang,omitempty"`
	Version string     `json:"ver,omitempty"`
	RTT     string     `json:"rtt,omitempty"`
	Stop    *time.Time `json:"stop,omitempty"`
}

// Various statistics we will periodically send out.
type ServerStats struct {
	Start            time.Time      `json:"start"`
	Mem              int64          `json:"mem"`
	Cores            int            `json:"cores"`
	CPU              float64        `json:"cpu"`
	Connections      int            `json:"connections"`
	TotalConnections uint64         `json:"total_connections"`
	ActiveAccounts   int            `json:"active_accounts"`
	NumSubs          uint32         `json:"subscriptions"`
	Sent             DataStats      `json:"sent"`
	Received         DataStats      `json:"received"`
	SlowConsumers    int64          `json:"slow_consumers"`
	Routes           []*RouteStat   `json:"routes,omitempty"`
	Gateways         []*GatewayStat `json:"gateways,omitempty"`
}

type RouteStat struct {
	ID       uint64    `json:"rid"`
	Sent     DataStats `json:"sent"`
	Received DataStats `json:"received"`
	Pending  int       `json:"pending"`
}

type GatewayStat struct {
	ID         uint64    `json:"gwid"`
	Name       string    `json:"name"`
	Sent       DataStats `json:"sent"`
	Received   DataStats `json:"received"`
	NumInbound int       `json:"inbound_connections"`
}

// DataStats reports how may msg and bytes. Applicable for both sent and received.
type DataStats struct {
	Msgs  int64 `json:"msgs"`
	Bytes int64 `json:"bytes"`
}

// Used for internally queueing up messages that the server wants to send.
type pubMsg struct {
	sub  string
	rply string
	si   *ServerInfo
	msg  interface{}
	last bool
}

// Used to track server updates.
type serverUpdate struct {
	seq   uint64
	ltime time.Time
}

// internalSendLoop will be responsible for serializing all messages that
// a server wants to send.
func (s *Server) internalSendLoop(wg *sync.WaitGroup) {
	defer wg.Done()

	s.mu.Lock()
	if s.sys == nil || s.sys.sendq == nil {
		s.mu.Unlock()
		return
	}
	c := s.sys.client
	sendq := s.sys.sendq
	id := s.info.ID
	host := s.info.Host
	seqp := &s.sys.seq
	var cluster string
	if s.gateway.enabled {
		cluster = s.getGatewayName()
	}
	s.mu.Unlock()

	for s.eventsRunning() {
		// Setup information for next message
		seq := atomic.AddUint64(seqp, 1)
		select {
		case pm := <-sendq:
			if pm.si != nil {
				pm.si.Host = host
				pm.si.Cluster = cluster
				pm.si.ID = id
				pm.si.Seq = seq
				pm.si.Version = VERSION
				pm.si.Time = time.Now()
			}
			var b []byte
			if pm.msg != nil {
				b, _ = json.MarshalIndent(pm.msg, _EMPTY_, "  ")
			}
			// Prep internal structures needed to send message.
			c.pa.subject = []byte(pm.sub)
			c.pa.size = len(b)
			c.pa.szb = []byte(strconv.FormatInt(int64(len(b)), 10))
			c.pa.reply = []byte(pm.rply)
			// Add in NL
			b = append(b, _CRLF_...)
			c.processInboundClientMsg(b)
			c.flushClients(0) // Never spend time in place.
			// See if we are doing graceful shutdown.
			if pm.last {
				return
			}
		case <-s.quitCh:
			return
		}
	}
}

// Will send a shutdown message.
func (s *Server) sendShutdownEvent() {
	s.mu.Lock()
	if s.sys == nil || s.sys.sendq == nil {
		s.mu.Unlock()
		return
	}
	subj := fmt.Sprintf(shutdownEventSubj, s.info.ID)
	sendq := s.sys.sendq
	// Stop any more messages from queueing up.
	s.sys.sendq = nil
	// Unhook all msgHandlers. Normal client cleanup will deal with subs, etc.
	s.sys.subs = nil
	s.mu.Unlock()
	// Send to the internal queue and mark as last.
	sendq <- &pubMsg{subj, _EMPTY_, nil, nil, true}
}

// This will queue up a message to be sent.
// Assumes lock is held on entry.
func (s *Server) sendInternalMsg(sub, rply string, si *ServerInfo, msg interface{}) {
	if s.sys == nil || s.sys.sendq == nil {
		return
	}
	sendq := s.sys.sendq
	// Don't hold lock while placing on the channel.
	s.mu.Unlock()
	sendq <- &pubMsg{sub, rply, si, msg, false}
	s.mu.Lock()
}

// Locked version of checking if events system running. Also checks server.
func (s *Server) eventsRunning() bool {
	s.mu.Lock()
	er := s.running && s.eventsEnabled()
	s.mu.Unlock()
	return er
}

// EventsEnabled will report if the server has internal events enabled via
// a defined system account.
func (s *Server) EventsEnabled() bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.eventsEnabled()
}

// eventsEnabled will report if events are enabled.
// Lock should be held.
func (s *Server) eventsEnabled() bool {
	return s.sys != nil && s.sys.client != nil && s.sys.account != nil
}

// Check for orphan servers who may have gone away without notification.
// This should be wrapChk() to setup common locking.
func (s *Server) checkRemoteServers() {
	now := time.Now()
	for sid, su := range s.sys.servers {
		if now.Sub(su.ltime) > s.sys.orphMax {
			s.Debugf("Detected orphan remote server: %q", sid)
			// Simulate it going away.
			s.processRemoteServerShutdown(sid)
			delete(s.sys.servers, sid)
		}
	}
	if s.sys.sweeper != nil {
		s.sys.sweeper.Reset(s.sys.chkOrph)
	}
}

// Grab RSS and PCPU
func updateServerUsage(v *ServerStats) {
	var rss, vss int64
	var pcpu float64
	pse.ProcUsage(&pcpu, &rss, &vss)
	v.Mem = rss
	v.CPU = pcpu
	v.Cores = numCores
}

// Generate a route stat for our statz update.
func routeStat(r *client) *RouteStat {
	if r == nil {
		return nil
	}
	r.mu.Lock()
	rs := &RouteStat{
		ID: r.cid,
		Sent: DataStats{
			Msgs:  atomic.LoadInt64(&r.outMsgs),
			Bytes: atomic.LoadInt64(&r.outBytes),
		},
		Received: DataStats{
			Msgs:  atomic.LoadInt64(&r.inMsgs),
			Bytes: atomic.LoadInt64(&r.inBytes),
		},
		Pending: int(r.out.pb),
	}
	r.mu.Unlock()
	return rs
}

// Actual send method for statz updates.
// Lock should be held.
func (s *Server) sendStatsz(subj string) {
	m := ServerStatsMsg{}
	updateServerUsage(&m.Stats)
	m.Stats.Start = s.start
	m.Stats.Connections = len(s.clients)
	m.Stats.TotalConnections = s.totalClients
	m.Stats.ActiveAccounts = int(atomic.LoadInt32(&s.activeAccounts))
	m.Stats.Received.Msgs = atomic.LoadInt64(&s.inMsgs)
	m.Stats.Received.Bytes = atomic.LoadInt64(&s.inBytes)
	m.Stats.Sent.Msgs = atomic.LoadInt64(&s.outMsgs)
	m.Stats.Sent.Bytes = atomic.LoadInt64(&s.outBytes)
	m.Stats.SlowConsumers = atomic.LoadInt64(&s.slowConsumers)
	m.Stats.NumSubs = s.numSubscriptions()

	for _, r := range s.routes {
		m.Stats.Routes = append(m.Stats.Routes, routeStat(r))
	}
	if s.gateway.enabled {
		gw := s.gateway
		gw.RLock()
		for name, c := range gw.out {
			gs := &GatewayStat{Name: name}
			c.mu.Lock()
			gs.ID = c.cid
			gs.Sent = DataStats{
				Msgs:  atomic.LoadInt64(&c.outMsgs),
				Bytes: atomic.LoadInt64(&c.outBytes),
			}
			c.mu.Unlock()
			// Gather matching inbound connections
			gs.Received = DataStats{}
			for _, c := range gw.in {
				c.mu.Lock()
				if c.gw.name == name {
					gs.Received.Msgs += atomic.LoadInt64(&c.inMsgs)
					gs.Received.Bytes += atomic.LoadInt64(&c.inBytes)
					gs.NumInbound++
				}
				c.mu.Unlock()
			}
			m.Stats.Gateways = append(m.Stats.Gateways, gs)
		}
		gw.RUnlock()
	}
	s.sendInternalMsg(subj, _EMPTY_, &m.Server, &m)
}

// Send out our statz update.
// This should be wrapChk() to setup common locking.
func (s *Server) heartbeatStatsz() {
	if s.sys.stmr != nil {
		s.sys.stmr.Reset(s.sys.statsz)
	}
	s.sendStatsz(fmt.Sprintf(serverStatsSubj, s.info.ID))
}

// This should be wrapChk() to setup common locking.
func (s *Server) startStatszTimer() {
	s.sys.stmr = time.AfterFunc(s.sys.statsz, s.wrapChk(s.heartbeatStatsz))
}

// Start a ticker that will fire periodically and check for orphaned servers.
// This should be wrapChk() to setup common locking.
func (s *Server) startRemoteServerSweepTimer() {
	s.sys.sweeper = time.AfterFunc(s.sys.chkOrph, s.wrapChk(s.checkRemoteServers))
}

// This will setup our system wide tracking subs.
// For now we will setup one wildcard subscription to
// monitor all accounts for changes in number of connections.
// We can make this on a per account tracking basis if needed.
// Tradeoff is subscription and interest graph events vs connect and
// disconnect events, etc.
func (s *Server) initEventTracking() {
	if !s.eventsEnabled() {
		return
	}
	subject := fmt.Sprintf(accConnsEventSubj, "*")
	if _, err := s.sysSubscribe(subject, s.remoteConnsUpdate); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// This will be for responses for account info that we send out.
	subject = fmt.Sprintf(connsRespSubj, s.info.ID)
	if _, err := s.sysSubscribe(subject, s.remoteConnsUpdate); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// Listen for broad requests to respond with account info.
	subject = fmt.Sprintf(accConnsReqSubj, "*")
	if _, err := s.sysSubscribe(subject, s.connsRequest); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// Listen for all server shutdowns.
	subject = fmt.Sprintf(shutdownEventSubj, "*")
	if _, err := s.sysSubscribe(subject, s.remoteServerShutdown); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// Listen for account claims updates.
	subject = fmt.Sprintf(accUpdateEventSubj, "*")
	if _, err := s.sysSubscribe(subject, s.accountClaimUpdate); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// Listen for requests for our statsz.
	subject = fmt.Sprintf(serverStatsReqSubj, s.info.ID)
	if _, err := s.sysSubscribe(subject, s.statszReq); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// Listen for ping messages that will be sent to all servers for statsz.
	if _, err := s.sysSubscribe(serverStatsPingReqSubj, s.statszReq); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
	// Listen for updates when leaf nodes connect for a given account. This will
	// force any gateway connections to move to `modeInterestOnly`
	subject = fmt.Sprintf(leafNodeConnectEventSubj, "*")
	if _, err := s.sysSubscribe(subject, s.leafNodeConnected); err != nil {
		s.Errorf("Error setting up internal tracking: %v", err)
	}
}

// accountClaimUpdate will receive claim updates for accounts.
func (s *Server) accountClaimUpdate(sub *subscription, subject, reply string, msg []byte) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if !s.eventsEnabled() {
		return
	}
	toks := strings.Split(subject, tsep)
	if len(toks) < accUpdateTokens {
		s.Debugf("Received account claims update on bad subject %q", subject)
		return
	}
	if v, ok := s.accounts.Load(toks[accUpdateAccIndex]); ok {
		s.updateAccountWithClaimJWT(v.(*Account), string(msg))
	}
}

// processRemoteServerShutdown will update any affected accounts.
// Will update the remote count for clients.
// Lock assume held.
func (s *Server) processRemoteServerShutdown(sid string) {
	s.accounts.Range(func(k, v interface{}) bool {
		a := v.(*Account)
		a.mu.Lock()
		prev := a.strack[sid]
		delete(a.strack, sid)
		a.nrclients -= prev.conns
		a.nrleafs -= prev.leafs
		a.mu.Unlock()
		return true
	})
}

// remoteServerShutdownEvent is called when we get an event from another server shutting down.
func (s *Server) remoteServerShutdown(sub *subscription, subject, reply string, msg []byte) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if !s.eventsEnabled() {
		return
	}
	toks := strings.Split(subject, tsep)
	if len(toks) < shutdownEventTokens {
		s.Debugf("Received remote server shutdown on bad subject %q", subject)
		return
	}
	sid := toks[serverSubjectIndex]
	su := s.sys.servers[sid]
	if su != nil {
		s.processRemoteServerShutdown(sid)
	}
}

// updateRemoteServer is called when we have an update from a remote server.
// This allows us to track remote servers, respond to shutdown messages properly,
// make sure that messages are ordered, and allow us to prune dead servers.
// Lock should be held upon entry.
func (s *Server) updateRemoteServer(ms *ServerInfo) {
	su := s.sys.servers[ms.ID]
	if su == nil {
		s.sys.servers[ms.ID] = &serverUpdate{ms.Seq, time.Now()}
	} else {
		// Should alwqys be going up.
		if ms.Seq <= su.seq {
			s.Errorf("Received out of order remote server update from: %q", ms.ID)
			return
		}
		su.seq = ms.Seq
		su.ltime = time.Now()
	}
}

// shutdownEventing will clean up all eventing state.
func (s *Server) shutdownEventing() {
	if !s.eventsRunning() {
		return
	}

	s.mu.Lock()
	clearTimer(&s.sys.sweeper)
	clearTimer(&s.sys.stmr)
	s.mu.Unlock()

	// We will queue up a shutdown event and wait for the
	// internal send loop to exit.
	s.sendShutdownEvent()
	s.sys.wg.Wait()

	s.mu.Lock()
	defer s.mu.Unlock()

	// Whip through all accounts.
	s.accounts.Range(func(k, v interface{}) bool {
		a := v.(*Account)
		a.mu.Lock()
		a.nrclients = 0
		// Now clear state
		clearTimer(&a.etmr)
		clearTimer(&a.ctmr)
		a.clients = nil
		a.strack = nil
		a.mu.Unlock()
		return true
	})
	// Turn everything off here.
	s.sys = nil
}

// Request for our local connection count.
func (s *Server) connsRequest(sub *subscription, subject, reply string, msg []byte) {
	if !s.eventsRunning() {
		return
	}
	m := accNumConnsReq{}
	if err := json.Unmarshal(msg, &m); err != nil {
		s.sys.client.Errorf("Error unmarshalling account connections request message: %v", err)
		return
	}
	acc, _ := s.lookupAccount(m.Account)
	if acc == nil {
		return
	}
	if nlc := acc.NumLocalConnections(); nlc > 0 {
		s.mu.Lock()
		s.sendAccConnsUpdate(acc, reply)
		s.mu.Unlock()
	}
}

// leafNodeConnected is an event we will receive when a leaf node for a given account
// connects.
func (s *Server) leafNodeConnected(sub *subscription, subject, reply string, msg []byte) {
	m := accNumConnsReq{}
	if err := json.Unmarshal(msg, &m); err != nil {
		s.sys.client.Errorf("Error unmarshalling account connections request message: %v", err)
		return
	}

	s.mu.Lock()
	na := m.Account == "" || !s.eventsEnabled() || !s.gateway.enabled
	s.mu.Unlock()

	if na {
		return
	}

	if acc, _ := s.lookupAccount(m.Account); acc != nil {
		s.switchAccountToInterestMode(acc.Name)
	}
}

// statszReq is a request for us to respond with current statz.
func (s *Server) statszReq(sub *subscription, subject, reply string, msg []byte) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if !s.eventsEnabled() || reply == _EMPTY_ {
		return
	}
	s.sendStatsz(reply)
}

// remoteConnsUpdate gets called when we receive a remote update from another server.
func (s *Server) remoteConnsUpdate(sub *subscription, subject, reply string, msg []byte) {
	if !s.eventsRunning() {
		return
	}
	m := AccountNumConns{}
	if err := json.Unmarshal(msg, &m); err != nil {
		s.sys.client.Errorf("Error unmarshalling account connection event message: %v", err)
		return
	}

	// See if we have the account registered, if not drop it.
	acc, _ := s.lookupAccount(m.Account)

	s.mu.Lock()
	defer s.mu.Unlock()

	// check again here if we have been shutdown.
	if !s.running || !s.eventsEnabled() {
		return
	}

	// Double check that this is not us, should never happen, so error if it does.
	if m.Server.ID == s.info.ID {
		s.sys.client.Errorf("Processing our own account connection event message: ignored")
		return
	}
	if acc == nil {
		s.sys.client.Debugf("Received account connection event for unknown account: %s", m.Account)
		return
	}
	// If we are here we have interest in tracking this account. Update our accounting.
	acc.mu.Lock()
	if acc.strack == nil {
		acc.strack = make(map[string]sconns)
	}
	// This does not depend on receiving all updates since each one is idempotent.
	prev := acc.strack[m.Server.ID]
	acc.strack[m.Server.ID] = sconns{conns: int32(m.Conns), leafs: int32(m.LeafNodes)}
	acc.nrclients += int32(m.Conns) - prev.conns
	acc.nrleafs += int32(m.LeafNodes) - prev.leafs
	acc.mu.Unlock()

	s.updateRemoteServer(&m.Server)
}

// Setup tracking for this account. This allows us to track globally
// account activity.
// Lock should be held on entry.
func (s *Server) enableAccountTracking(a *Account) {
	if a == nil || !s.eventsEnabled() {
		return
	}

	// TODO(ik): Generate payload although message may not be sent.
	// May need to ensure we do so only if there is a known interest.
	// This can get complicated with gateways.

	subj := fmt.Sprintf(accConnsReqSubj, a.Name)
	reply := fmt.Sprintf(connsRespSubj, s.info.ID)
	m := accNumConnsReq{Account: a.Name}
	s.sendInternalMsg(subj, reply, &m.Server, &m)
}

// Event on leaf node connect.
// Lock should NOT be held on entry.
func (s *Server) sendLeafNodeConnect(a *Account) {
	s.mu.Lock()
	// If we are not in operator mode, or do not have any gateways defined, this should also be a no-op.
	if a == nil || !s.eventsEnabled() || !s.gateway.enabled {
		s.mu.Unlock()
		return
	}
	subj := fmt.Sprintf(leafNodeConnectEventSubj, a.Name)
	m := accNumConnsReq{Account: a.Name}
	s.sendInternalMsg(subj, "", &m.Server, &m)
	s.mu.Unlock()

	s.switchAccountToInterestMode(a.Name)
}

// FIXME(dlc) - make configurable.
const eventsHBInterval = 30 * time.Second

// sendAccConnsUpdate is called to send out our information on the
// account's local connections.
// Lock should be held on entry.
func (s *Server) sendAccConnsUpdate(a *Account, subj string) {
	if !s.eventsEnabled() || a == nil || a == s.gacc {
		return
	}
	a.mu.RLock()

	// If no limits set, don't update, no need to.
	if a.mconns == jwt.NoLimit && a.mleafs == jwt.NoLimit {
		a.mu.RUnlock()
		return
	}

	// Build event with account name and number of local clients and leafnodes.
	m := AccountNumConns{
		Account:    a.Name,
		Conns:      a.numLocalConnections(),
		LeafNodes:  a.numLocalLeafNodes(),
		TotalConns: a.numLocalConnections() + a.numLocalLeafNodes(),
	}
	a.mu.RUnlock()

	s.sendInternalMsg(subj, _EMPTY_, &m.Server, &m)

	// Set timer to fire again unless we are at zero.
	a.mu.Lock()
	if a.numLocalConnections() == 0 {
		clearTimer(&a.etmr)
	} else {
		// Check to see if we have an HB running and update.
		if a.ctmr == nil {
			a.etmr = time.AfterFunc(eventsHBInterval, func() { s.accConnsUpdate(a) })
		} else {
			a.etmr.Reset(eventsHBInterval)
		}
	}
	a.mu.Unlock()
}

// accConnsUpdate is called whenever there is a change to the account's
// number of active connections, or during a heartbeat.
func (s *Server) accConnsUpdate(a *Account) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if !s.eventsEnabled() || a == nil {
		return
	}
	subj := fmt.Sprintf(accConnsEventSubj, a.Name)
	s.sendAccConnsUpdate(a, subj)
}

// accountConnectEvent will send an account client connect event if there is interest.
// This is a billing event.
func (s *Server) accountConnectEvent(c *client) {
	s.mu.Lock()
	if !s.eventsEnabled() {
		s.mu.Unlock()
		return
	}
	s.mu.Unlock()

	subj := fmt.Sprintf(connectEventSubj, c.acc.Name)

	c.mu.Lock()
	m := ConnectEventMsg{
		Client: ClientInfo{
			Start:   c.start,
			Host:    c.host,
			ID:      c.cid,
			Account: accForClient(c),
			User:    nameForClient(c),
			Name:    c.opts.Name,
			Lang:    c.opts.Lang,
			Version: c.opts.Version,
		},
	}
	c.mu.Unlock()

	s.mu.Lock()
	s.sendInternalMsg(subj, _EMPTY_, &m.Server, &m)
	s.mu.Unlock()
}

// accountDisconnectEvent will send an account client disconnect event if there is interest.
// This is a billing event.
func (s *Server) accountDisconnectEvent(c *client, now time.Time, reason string) {
	s.mu.Lock()
	gacc := s.gacc
	if !s.eventsEnabled() {
		s.mu.Unlock()
		return
	}
	s.mu.Unlock()

	c.mu.Lock()

	// Ignore global account activity
	if c.acc == nil || c.acc == gacc {
		c.mu.Unlock()
		return
	}

	m := DisconnectEventMsg{
		Client: ClientInfo{
			Start:   c.start,
			Stop:    &now,
			Host:    c.host,
			ID:      c.cid,
			Account: accForClient(c),
			User:    nameForClient(c),
			Name:    c.opts.Name,
			Lang:    c.opts.Lang,
			Version: c.opts.Version,
			RTT:     c.getRTT(),
		},
		Sent: DataStats{
			Msgs:  c.inMsgs,
			Bytes: c.inBytes,
		},
		Received: DataStats{
			Msgs:  c.outMsgs,
			Bytes: c.outBytes,
		},
		Reason: reason,
	}
	c.mu.Unlock()

	subj := fmt.Sprintf(disconnectEventSubj, c.acc.Name)

	s.mu.Lock()
	s.sendInternalMsg(subj, _EMPTY_, &m.Server, &m)
	s.mu.Unlock()
}

func (s *Server) sendAuthErrorEvent(c *client) {
	s.mu.Lock()
	if !s.eventsEnabled() {
		s.mu.Unlock()
		return
	}
	s.mu.Unlock()
	now := time.Now()
	c.mu.Lock()
	m := DisconnectEventMsg{
		Client: ClientInfo{
			Start:   c.start,
			Stop:    &now,
			Host:    c.host,
			ID:      c.cid,
			Account: accForClient(c),
			User:    nameForClient(c),
			Name:    c.opts.Name,
			Lang:    c.opts.Lang,
			Version: c.opts.Version,
			RTT:     c.getRTT(),
		},
		Sent: DataStats{
			Msgs:  c.inMsgs,
			Bytes: c.inBytes,
		},
		Received: DataStats{
			Msgs:  c.outMsgs,
			Bytes: c.outBytes,
		},
		Reason: AuthenticationViolation.String(),
	}
	c.mu.Unlock()

	s.mu.Lock()
	subj := fmt.Sprintf(authErrorEventSubj, s.info.ID)
	s.sendInternalMsg(subj, _EMPTY_, &m.Server, &m)
	s.mu.Unlock()

}

// Internal message callback. If the msg is needed past the callback it is
// required to be copied.
type msgHandler func(sub *subscription, subject, reply string, msg []byte)

func (s *Server) deliverInternalMsg(sub *subscription, subject, reply, msg []byte) {
	s.mu.Lock()
	if !s.eventsEnabled() || s.sys.subs == nil {
		s.mu.Unlock()
		return
	}
	cb := s.sys.subs[string(sub.sid)]
	s.mu.Unlock()
	if cb != nil {
		cb(sub, string(subject), string(reply), msg)
	}
}

// Create an internal subscription. No support for queue groups atm.
func (s *Server) sysSubscribe(subject string, cb msgHandler) (*subscription, error) {
	if !s.eventsEnabled() {
		return nil, ErrNoSysAccount
	}
	if cb == nil {
		return nil, fmt.Errorf("undefined message handler")
	}
	s.mu.Lock()
	sid := strconv.FormatInt(int64(s.sys.sid), 10)
	s.sys.subs[sid] = cb
	s.sys.sid++
	c := s.sys.client
	s.mu.Unlock()

	// Now create the subscription
	if err := c.processSub([]byte(subject + " " + sid)); err != nil {
		return nil, err
	}
	c.mu.Lock()
	sub := c.subs[sid]
	c.mu.Unlock()
	return sub, nil
}

func (s *Server) sysUnsubscribe(sub *subscription) {
	if sub == nil || !s.eventsEnabled() {
		return
	}
	s.mu.Lock()
	acc := s.sys.account
	c := s.sys.client
	delete(s.sys.subs, string(sub.sid))
	s.mu.Unlock()
	c.unsubscribe(acc, sub, true)
}

// Helper to grab name for a client.
func nameForClient(c *client) string {
	if c.user != nil {
		return c.user.Nkey
	}
	return "N/A"
}

// Helper to grab account name for a client.
func accForClient(c *client) string {
	if c.acc != nil {
		return c.acc.Name
	}
	return "N/A"
}

// Helper to clear timers.
func clearTimer(tp **time.Timer) {
	if t := *tp; t != nil {
		t.Stop()
		*tp = nil
	}
}

// Helper function to wrap functions with common test
// to lock server and return if events not enabled.
func (s *Server) wrapChk(f func()) func() {
	return func() {
		s.mu.Lock()
		defer s.mu.Unlock()
		if !s.eventsEnabled() {
			return
		}
		f()
	}
}


text:
function lfcc(filesPath)
  addpath('~/ASem7/Tools/voicebox/')
  tic;
  display(['Extracting Features from Audio Files..']);
  files = dir([filesPath, '*.wav']);

  for i = 1:length(files)
    fileName = files(i).name;      % bus_01.wav
    fileNameParts = regexp(fileName, '[a-z0-9_]+', 'match'); %'bus' 'wav'
    className = fileNameParts{1};
    display(['Reading ... FileNo : ', '[', num2str(i), '/', ...
    num2str(length(files)), '],  FileName : ', fileName]);
    [audio, sr] = audioread([filesPath fileName]);
    audio = mean(audio,2);
    features = melcepst(audio,sr,'0dD',19,40,1764);
    disp(size(features))
    save(['../../../saved/features/2016/mfcc2/features/', className, '.txt'], 'features', '-ascii');
  end
  toc;
end

redpajama_set_name: RedPajamaGithub


text:
<?php

# Author Bermi Ferrer - MIT LICENSE

class Page extends ActiveRecord
{
    public $_Behavior;

    public $has_many = array(
    'parts'=>array(
    'dependent' => 'destroy',
    'class_name' => 'PagePart',
    'handler_name' => 'part'
    ));

    public $belongs_to = array(
    'layout' => array('class_name' => 'ContentLayout', 'primary_key_name' => 'layout_id'),
    'author' => array('class_name' => 'User', 'foreign_key' => 'created_by'),
    'editor' => array('class_name' => 'User', 'foreign_key' => 'updated_by')
    );

    public $acts_as = array('nested_set' => array('order' => 'virtual DESC, title ASC','scope' => "locale = ?"));

    public function getLocale()
    {
        $default_lang = Ak::lang();
        $lang = !isset($this->_original_locale) || empty($this->locale) ? $default_lang : $this->locale;
        $locale = in_array($lang, Ak::langs()) ? $lang : $default_lang;
        $this->_original_locale = empty($this->_original_locale) ? $locale : $this->_original_locale;
        return $locale;
    }

    public function forceSetLocale($locale)
    {
        $this->_forced_locale = $locale;
    }

    public function isHomepage()
    {
        return !$this->isNewRecord() && $this->nested_set->isRoot();
    }

    public function saveWithParts($parts = array(), $position = null)
    {
        $this->transactionStart();
        $this->parts = array();
        $this->part->load();
        foreach ($parts as $part){
            $this->part->create($part);
        }
        //$this->set('parent_id', null);
        $success = $this->save() && !$this->transactionHasFailed();

        if($success && !$this->isHomepage()){
            $this->nested_set->moveToChildOf($position);
        }

        if(!$success){
            $this->transactionFail();
        }
        $this->transactionComplete();
        return $success;
    }

    public function updateWithParts($parts = array(), $position = null)
    {
        $this->part->load();
        //$this->layout->load();
        $this->transactionStart();

        foreach (array_keys($this->parts) as $k){
            if(!isset($parts[$k+1])){
                $this->parts[$k]->destroy();
            }
        }
        $success = true;
        foreach ($parts as $k=>$part){
            if(!$success){
                break;
            }
            if(empty($this->parts[$k-1])){
                if(!$this->part->create($part)){
                    $success = false;
                    $this->addErrorToBase($this->t('Unexpected errors on page part'));
                }
            }else{
                $this->parts[$k-1]->setAttributes($part);
            }
        }
        $success = $this->save() && !$this->transactionHasFailed();

        if(!empty($position) && $success && $this->parent_id != $position){
            $this->nested_set->moveToChildOf($position);
        }

        if(!$success){
            $this->transactionFail();
        }
        $this->transactionComplete();
        return $success;
    }

    public function beforeSave()
    {
        if(!empty($this->_controller)){
            $this->initiateBehavior($this->_controller);
        }else{
            $this->loadBehavior();
        }

        $this->_updatePublishedAt();
        $this->_updateVirtual();
        return true;
    }

    public function beforeSaveOnCreate()
    {
        $this->_updateLocale();
        return true;
    }

    public function validate()
    {
        $this->validatesPresenceOf(array('title','slug','breadcrumb','status','created_by'));

        $this->_validateSlug();

        $this->validatesInclusionOf('behavior',array_keys($this->getAvailableBehaviors()), 'inclusion', true);
        $this->validatesInclusionOf('status',array_keys($this->getAvailableStatuses()));

        $this->validatesInclusionOf('locale', Ak::langs());

        if(isset($this->_original_locale) && $this->_original_locale != $this->get('locale')){
            $this->addError('locale', $this->t('Page locale can\'t be changed once it has been set.'));
        }
    }

    public function _validateSlug($parent_id = null)
    {
        $original_parent_id = $this->parent_id;
        $this->parent_id = !empty($parent_id) ? $parent_id : $this->parent_id;

        if(!$this->getErrorsOn('slug')){
            $this->validatesFormatOf('slug','/^([\w\d]+|[\w\d](?!.*(\-|\.){2,}.*)[\w\d\-_\.]*[\w\d]|\/)$/');
            if(!empty($this->parent_id)){
                $this->validatesUniquenessOf('slug', array('scope' => array('locale','parent_id'), 'message' => $this->t('Slug is already used in a page that shares the parent with this one')));
            }
        }
        $this->parent_id = $original_parent_id;
    }

    public function validateOnCreate()
    {
        $this->validatesAssociated('parts');
    }

    public function isPublished()
    {
        return !empty($this->status) && $this->status == 'published';
    }

    public function &getPart($name)
    {
        if(empty($this->parts)){
            $this->part->load();
        }
        $IndexedPart = $this->_getPartIndexedAs($name);
        return $IndexedPart;
    }

    public function &getInheritedPart($name)
    {
        if(!$Part = $this->getPart($name)){
            if(!$ParentPages = $this->getParentPages()){
                $false = false;
                return $false;
            }
            foreach (array_reverse(array_keys($ParentPages)) as $k){
                if($Part = $ParentPages[$k]->getPart($name)){
                    $this->__indexed_parts[$name] = $Part;
                    break;
                }
            }
        }

        return $Part;
    }

    public function &getParentPages()
    {
        if(!isset($this->ParentPages)){
            $this->ParentPages = $this->nested_set->getAncestors();
        }
        return $this->ParentPages;
    }

    public function getFilteredPart($name, $use_inherited_if_unavailable = false)
    {
        $method = $use_inherited_if_unavailable ? 'getInheritedPart' : 'getPart';
        $Part = $this->$method($name);
        return is_object($Part) ? EditamFilter::getFilteredContent($Part) : false;
    }

    public function &_findInheritedPart($name)
    {
        $Part = $this->part->find('first', array('default'=>false, 'conditions'=>array('name = ?', $name)));
    }

    public function &_getPartIndexedAs($name)
    {
        empty($this->__indexed_parts[$name]) ? $this->_loadIndexedParts() : null;
        if(!isset($this->__indexed_parts[$name])){
            $false = false;
            return $false;
        }
        return $this->__indexed_parts[$name];
    }

    public function _loadIndexedParts()
    {
        foreach (array_keys($this->parts) as $k){
            $this->__indexed_parts[$this->parts[$k]->get('name')] = $this->parts[$k];
        }
    }

    public function &getLayoutInstance()
    {
        if($this->layout->getType() != 'ContentLayout'){
            $this->layout->load(true);
            if($this->layout->getType() != 'ContentLayout'){
                if($this->nested_set->isRoot()){
                    $false = false;
                    return $false;
                }
                $Parent = $this->nested_set->getParent();
                $Layout = $Parent->getLayoutInstance();
                return $Layout;
            }
        }
        return $this->layout;
    }

    public function getLayout()
    {
        $Layout = $this->getLayoutInstance();
        return isset($Layout->content) ? $Layout->get('content') : false;
    }

    public function getLayoutName()
    {
        if(empty($this->layout->name)){
            $this->layout->load();
        }
        if(empty($this->layout->name) && $parent = $this->nested_set->getParent()){
            return $parent->getLayoutName();
        }else{
            return false;
        }
        return $this->layout->name;
    }

    public function &findByUrl($url, $show_unpublished = false)
    {
        $Page = $this->_Behavior->findPageByUrl($url, $show_unpublished);
        return $Page;
    }

    public function &_findByUrl($url_parts, $show_unpublished = false, $missing_page_mode = false)
    {
        $sql = $this->_getSqlForUrlFinder($url_parts, $show_unpublished, $missing_page_mode);
        $this->_db->addLimitAndOffset($sql, array('limit' => 1, 'offset' => null));
        if($result = $this->findBySql($sql, array('default'=>false))){
            return $result[0];
        }
        $false = false;
        return $false; // Trick for avoiding pass by reference notices on PHP4
    }

    public function _getSqlForUrlFinder($url_parts, $show_unpublished = false, $missing_page_mode = false)
    {
        $url_parts = is_array($url_parts) ? $url_parts : explode('/',$url_parts.'/');
        $url_parts = array_diff($url_parts,array(''));
        if(empty($url_parts[0]) || $url_parts[0] != '/'){
            array_unshift($url_parts, '/');
        }
        $total_url_parts = count($url_parts);
        $url_parts = array_reverse($url_parts);
        $sql = '';
        $locale = Ak::lang();

        $conditions = '';
        $joins = '';
        $table_alias = false;
        foreach ($url_parts as $position => $url_part){
            $previous_table_alias = $table_alias;
            $table_alias = 'pages'.$position;
            $position = $position +1;
            $condition = $missing_page_mode && $position ==  1 ? ' '.$table_alias.'.behavior = "page_missing" ' :
            $table_alias.'.slug = '.$this->castAttributeForDatabase('slug',$url_part);
            $condition .= ' AND '.$table_alias.'.locale = '.$this->castAttributeForDatabase('locale',$locale).' ';
            if($position == 1){
                $select = 'SELECT '.$table_alias.'.* FROM pages AS '.$table_alias;
                $conditions .= ' WHERE '.$condition.($show_unpublished?'':' AND '.$table_alias.'.status = '.$this->castAttributeForDatabase('status','published'));
            }else{
                $joins .= ' INNER JOIN pages '.$table_alias.' ON '.$table_alias.'.id = '.$previous_table_alias.'.parent_id ';
                $conditions .= $condition.($show_unpublished ? '' : " AND $table_alias.status = 'published'");
            }
            if($position < $total_url_parts){
                $conditions .= ' AND ';
            }
        }
        return $select.$joins.$conditions;
    }

    public function &findMissingPageForUrl($url)
    {
        $Page = $this->_Behavior->findMissingPageForUrl($url);
        return $Page;
    }

    /**
     * Callbacks
     */
    public function _updatePublishedAt()
    {
        if(empty($this->published_at) && $this->status == 'published'){
            $this->published_at = Ak::getDate();
        }
    }
    public function _updateVirtual()
    {
        $this->set('is_virtual', $this->isVirtual());
    }

    public function _updateLocale()
    {
        empty($this->locale) ? $this->set('locale',Ak::lang()) : null;
    }

    /**
     * Page behaviors
     */

    public function loadBehavior($behavior = null)
    {
        $behavior = empty($behavior) ? $this->behavior : $behavior;
        $this->behavior = !empty($behavior) && in_array($behavior, array_keys($this->getAvailableBehaviors())) ? $behavior : '';

        $this->_Behavior = $this->getBehaviorInstance();
    }

    public function initiateBehavior(&$controller)
    {
        $this->loadBehavior($this->get('behavior'));
        $this->_Behavior->init($controller);
    }

    public function &getBehaviorInstance()
    {
        $behavior_class = empty($this->behavior) ? 'BaseBehavior' : AkInflector::camelize($this->behavior).'Behavior';
        $Instance = new $behavior_class();
        return $Instance;
    }

    public function getUrl()
    {
        return $this->_Behavior->getPageUrl();
    }

    public function getInheritedSlug($force = false, $include_locale = EDITAM_IS_MULTILINGUAL)
    {
        if($force || empty($this->_inherited_slug)){
            if(!empty($this->nested_set) && !empty($this->lft) && !empty($this->rgt)){
                $this->_inherited_slug = str_replace('//','/',
                ($include_locale?'/'.$this->locale:'').'/'.trim(join('/',array_keys($this->collect($this->nested_set->getSelfAndAncestors(),'slug','title'))),'/').'/');
            }
        }
        return $this->_inherited_slug;
    }

    public function hasCache()
    {
        return $this->_Behavior->hasPageCache();
    }

    public function canUsePageCache()
    {
        return $this->_Behavior->canUsePageCache();
    }

    public function render()
    {
        return $this->_Behavior->renderPage();
    }

    public function isVirtual()
    {
        return $this->_Behavior->isPageVirtual();
    }

    public function process()
    {
        return $this->_Behavior->process();
    }

    public function getChildUrl()
    {
        return $this->_Behavior->getChildUrl();
    }

    /**
     * Page elements discovery functions
     */
    public function getAvailableBehaviors()
    {
        static $behaviors = array();
        if(empty($behaviors)){
            if(defined('EDITAM_AVAILABLE_BEHAVIORS')){
                foreach (Ak::toArray(EDITAM_AVAILABLE_BEHAVIORS) as $behavior){
                    $behaviors[$behavior] = $this->t($behavior);
                }
            }else{
                foreach (AkFileSystem::dir(AK_APP_DIR.DS.'behaviors', array('dirs'=>false)) as $file){
                    if(substr($file,-14) == '_behavior.php'){
                        $behavior = substr($file,0,-14);
                        if($behavior != 'base'){
                            $behaviors[$behavior] = $this->t($behavior);
                        }
                    }
                }
            }
            $behaviors = array_map(array('AkInflector','humanize'), $behaviors);
        }
        return $behaviors;
    }

    public function getAvailableStatuses()
    {
        return array(
        'published' => $this->t('Published'),
        'draft' => $this->t('Draft'),
        'reviewed' => $this->t('Reviewed'),
        'hidden' => $this->t('Hidden')
        );
    }

    public function moveBeside(&$Page, $position = 'left')
    {
        $method = $position == 'left' ? 'moveToRightOf' : 'moveToLeftOf';
        $this->_validateSlug($Page->parent_id);
        if(!$this->hasErrors()){
            $this->nested_set->$method($Page);
            return true;
        }
        return false;
    }

    public function clearCachedPages()
    {
        $Cache = Ak::cache();
        $Cache->init(EDITAM_CACHE_LIFE);
        $Cache->clean(AK_HOST);

    }

    public function clearOldPagesFromCache()
    {
        $Cache = Ak::cache();
        $Cache->init(EDITAM_CACHE_LIFE);
        $Cache->clean(AK_HOST, 'old');
    }
}


text:
namespace Fixtures.MirrorRecursiveTypes
{
    using Microsoft.Rest;
    using Models;
    using Newtonsoft.Json;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Some cool documentation.
    /// </summary>
    public partial interface IRecursiveTypesAPI : System.IDisposable
    {
        /// <summary>
        /// The base URI of the service.
        /// </summary>
        System.Uri BaseUri { get; set; }

        /// <summary>
        /// Gets or sets json serialization settings.
        /// </summary>
        JsonSerializerSettings SerializationSettings { get; }

        /// <summary>
        /// Gets or sets json deserialization settings.
        /// </summary>
        JsonSerializerSettings DeserializationSettings { get; }

        /// <summary>
        /// Products
        /// </summary>
        /// <remarks>
        /// The Products endpoint returns information about the Uber products
        /// offered at a given location. The response includes the display name
        /// and other details about each product, and lists the products in the
        /// proper display order.
        /// </remarks>
        /// <param name='subscriptionId'>
        /// Subscription Id.
        /// </param>
        /// <param name='resourceGroupName'>
        /// Resource Group Id.
        /// </param>
        /// <param name='apiVersion'>
        /// API Id.
        /// </param>
        /// <param name='body'>
        /// API body mody.
        /// </param>
        /// <param name='customHeaders'>
        /// The headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        Task<HttpOperationResponse<Product>> PostWithHttpMessagesAsync(string subscriptionId, string resourceGroupName, string apiVersion, Product body = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

    }
}


text:
set -e

. ./common.sh

s0_prefix="`pwd`/stage0_prefix"
outdir="`pwd`/stage1_packages"
buildparentdir="`pwd`/stage1_build"

export ARCH_BUILD="`gcc -dumpmachine`"
export PATH="$s0_prefix/bin:$toolsdir:$PATH"

# prepare directories
mkdir -p $outdir $buildparentdir

failed() {
    echo "---------------------------------------------------------------------"
    echo "$1 $build_dir failed"
    head -n 2000 "$build_dir/build.log"
    echo "---------------------------------------------------------------------"
    tail -n 2000 "$build_dir/build.log"
    echo "---------------------------------------------------------------------"
    exit 1
}

build_s1_pkg() {
    pkg="$1"
    build_dir="${buildparentdir}/build-${pkg}"
    root_dir="${build_dir}/root"

    # figure out version
    versions="`cd ${packages}/${pkg}/ && echo *-\~\~stage1`"
    ver="`pkgvercmp -M -- $versions`"
    phys_path="${packages}/${pkg}/${ver}"
    control="$phys_path/control"

    # make sure it's not completed yet
    if [ -d "${outdir}/${pkg}/${ver}" ] ; then
        echo "stage1: Skipping $pkg"
        return
    fi

    echo "stage1: Building $pkg $ver"

    # prepare build directory
    rm -rf "$build_dir"
    cp -Lr "$phys_path/" "$build_dir"
    mkdir -p "$root_dir"

    pkgbuild -w "$build_dir" -o root -d "$distfiles" -V '-~~stage1' "$control" \
      >"$build_dir/build.log" 2>&1 || failed build

    for binpkg in `srccontrol -p "$control"`; do
        cp -ar "${build_dir}/root/packages/${binpkg}" "$outdir/"
    done
    cd "${buildparentdir}"
}

build_sdtools_pkg() {
    pkg="staticdistro-tools"
    build_dir="${buildparentdir}/build-${pkg}"
    root_dir="${build_dir}/root"

    # make sure it's not completed yet
    if [ -d "${outdir}/${pkg}" ] ; then
        echo "stage1: Skipping $pkg"
        return
    fi

    # figure out version
    phys_path="`cd ${toolsdir} && pwd -P`"
    control="$phys_path/control"
    ver="0.0.`date +'%Y%m%d%H%M'`-~~stage1"

    echo "stage1: Building $pkg $ver"

    # prepare build directory
    rm -rf "$build_dir"
    cp -r "$phys_path" "$build_dir"
    mkdir -p "$root_dir"

    pkgbuild -w "$build_dir" -o root -d "$distfiles" -v "$ver" \
      "$control" >"$build_dir/build.log" 2>&1 || failed build

    cp -r "${build_dir}/root/packages/${pkg}" "$outdir/"
    cd "${buildparentdir}"
}

stage1_packages="binutils gcc musl sbase mksh make sed grep awk"
for p in $stage1_packages ; do
    build_s1_pkg $p
done
build_sdtools_pkg


text:
TOXNET

U.S. National Library of Medicine

TOXNET - TOXICOLOGY DATA NETWORK

		FAQs
		TOXNET Fact Sheet
		Training Manual & Schedule

	<app-init></app-init>
	<app-modal-outer></app-modal-outer>
	<app-top-menu></app-top-menu>
	<router-outlet></router-outlet>

ChemIDplus Main Site

Copyright  |  Privacy  |  Contact Us

		About
		Contact Us
		SIS Home
		Copyright
		Privacy
		Accessibility
		Viewers and Players

ChemIDplus Main Site

U.S. National Library of Medicine 8600 Rockville Pike, Bethesda, MD 20894

National Institutes of Health, Health & Human Services


text:
This is the PIMF portion of a [benchmarking test suite](../) comparing a variety of web development platforms.

### JSON Encoding Test
Uses the PHP standard [JSON encoder](http://www.php.net/manual/en/function.json-encode.php).

* [JSON test controller](/app/Vanilla/Controller/Hello.php)

### Data-Store/Database Mapping Test
Uses the PIMF PDO Entity-Manager.

* [DB test controller](/app/Vanilla/Controller/Hello.php)

### Template Test
Uses PIMF plain vanilla PHTML rendering

* [Template test controller](/app/Vanilla/Controller/Hello.php)

## Infrastructure Software Versions
The tests were run with:

* [PIMF Version 1.8.6](http://pimf-framework.de/)
* [PHP Version 5.5.17](http://www.php.net/) with FPM and APC
* [nginx 1.4.0](http://nginx.org/)
* [MySQL 5.5.29](https://dev.mysql.com/)

## Test URLs
### JSON Encoding Test

http://localhost/json

### Data-Store/Database Mapping Test

http://localhost/db

### Variable Query Test

http://localhost/queries?queries=10

### Templating Test

http://localhost/fortunes

### Database Updates Test

http://localhost/updates?queries=10

### Plaintext Test

http://localhost/plaintext


text:
package it.bellotti.android.swipetabs.transforms;

import android.view.View;

/**
 * Created with IntelliJ IDEA.
 * User: SimoneBellotti
 * Date: 24/11/2014
 * Time: 13.12
 */
public class ZoomInPageTransformer extends BaseTransformer {

    @Override
    protected void onTransform(View view, float position) {
        final float scale = position < 0 ? position + 1f : Math.abs(1f - position);
        view.setScaleX(scale);
        view.setScaleY(scale);
        view.setPivotX(view.getWidth() * 0.5f);
        view.setPivotY(view.getHeight() * 0.5f);
        view.setAlpha(position < -1f || position > 1f ? 0f : 1f - (scale - 1f));
    }
}


text:
<?php

namespace Google\Service\Contentwarehouse;

class AppsPeopleOzExternalMergedpeopleapiProductMetadata extends \Google\Model
{
  /**
   * @var string
   */
  public $productSource;

  /**
   * @param string
   */
  public function setProductSource($productSource)
  {
    $this->productSource = $productSource;
  }
  /**
   * @return string
   */
  public function getProductSource()
  {
    return $this->productSource;
  }
}

// Adding a class alias for backwards compatibility with the previous class name.
class_alias(AppsPeopleOzExternalMergedpeopleapiProductMetadata::class, 'Google_Service_Contentwarehouse_AppsPeopleOzExternalMergedpeopleapiProductMetadata');


text: export { Gift16 as default } from "../../";


text:
{% with pag_neib=data.paginator.pag_neib|stringformat:"i" %}
{% with pag_neibn="-"|add:pag_neib %}
{% if data.paginator.num_pages != 1 %}

    {% if data.number > 1 %}

    {% else %}

    {% endif %}
    {% if data.number < data.paginator.num_pages %}

    {% else %}

    {% endif %}

    {% if data.number > pag_neib|add:1 %}

        1
          {% if data.number > pag_neib|add:2 %}

          {% endif %}

    {% endif %}
    {% for i in data.paginator.page_range %}
      {% if i >= data.number|add:pag_neibn %}
        {% if i <= data.number|add:pag_neib %}
          {% if data.number == i %}

{{ i }}(current)

          {% else %}

{{ i }}

          {% endif %}
        {% endif %}
      {% endif %}
    {% endfor %}
    {% if data.number < data.paginator.num_pages|add:pag_neibn %}

          {% if data.number < data.paginator.num_pages|add:pag_neibn|add:"-1" %}

          {% endif %}
        {{ data.paginator.num_pages }}

    {% endif %}

{% endif %}
{% endwith %}
{% endwith %}


text:
.. _usage:

Usage
=====

Djangocassandra is designed to be used with the **stock** version of Django 1.7 (specifically tested with Django 1.7.7). Many features of the Django ORM are supported however there are a few exceptions that should be taken into consideration when using djangocassandra as your database backend.

.. _autofields:

AutoFields
----------

Currently Django only supports integer auto fields. This isn't a huge issue as Cassandra works just fine if you use integer primary keys however when using Cassandra you can use any column type as your primary keys. Here's a few solutions to this problem that you may find useful:

Use The Included AutoFieldUUID Model Field
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I have included in the djangocassandra project an AutoFieldUUID model field that correctly handles the generation of uuid4() UUID's upon creation. You must specifically define the autofield on all of your models as shown::

  class AutoIdModel(Model):
        id = AutoFieldUUID(primary_key=True)
        data = CharField(
            max_length=64
        )

Use The Knotis Fork Of Django
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Instead of installing stock Django you can install the `Knotis custom-autofield branch <https://github.com/Knotis/django/tree/custom-autofield>`_ (https://github.com/Knotis/django/tree/custom-autofield) of Django which provides you a variable in settings.py to provide your own AutoField like so::

  CUSTOM_AUTOFIELD_CLASS='djangocassandra.db.fields.AutoFieldUUID'

Doing this will make Django use the auto field class you defined whenever it would normally add the stock integer based auto field.

Installing the Knotis fork of Django is as simple as running:

``pip install git+https://github.com/Knotis/django@custom-autofield``


text:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema addictionRemoval
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema addictionRemoval
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `addictionRemoval` DEFAULT CHARACTER SET utf8 ;
USE `addictionRemoval` ;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Member`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Member` (
  `firstname` VARCHAR(45) NOT NULL,
  `lastname` VARCHAR(45) NOT NULL,
  `email` VARCHAR(45) NOT NULL,
  `rating` DOUBLE NULL,
  `accountCreatedOn` DATE NOT NULL,
  `phoneNumber` VARCHAR(15) NULL,
  `city` VARCHAR(45) NOT NULL,
  `dob` DATE NOT NULL,
  `isBanned` TINYINT NULL,
  `isDoc` TINYINT NOT NULL,
  PRIMARY KEY (`email`),
  UNIQUE INDEX `Email_UNIQUE` (`email` ASC))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Report`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Report` (
  `reportid` INT NOT NULL,
  `type` VARCHAR(45) NOT NULL,
  `email` VARCHAR(45) NOT NULL,
  `Member_email` VARCHAR(45) NOT NULL,
  `Description` VARCHAR(45) NULL,
  PRIMARY KEY (`reportid`, `email`),
  UNIQUE INDEX `Report_ID_UNIQUE` (`reportid` ASC),
  UNIQUE INDEX `Type_UNIQUE` (`type` ASC),
  INDEX `fk_Report_Member_idx` (`Member_email` ASC),
  CONSTRAINT `fk_Report_Member`
    FOREIGN KEY (`Member_email`)
    REFERENCES `addictionRemoval`.`Member` (`email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`User` (
  `Member_email` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`Member_email`),
  INDEX `fk_User_Member1_idx` (`Member_email` ASC),
  CONSTRAINT `fk_User_Member1`
    FOREIGN KEY (`Member_email`)
    REFERENCES `addictionRemoval`.`Member` (`email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Addictions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Addictions` (
  `addictionName` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`addictionName`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Case`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Case` (
  `isCompleted` TINYINT NULL,
  `caseid` INT NOT NULL AUTO_INCREMENT,
  `counsellor_email` VARCHAR(45) NOT NULL,
  `patient_email` VARCHAR(45) NOT NULL,
  `startDate` TIMESTAMP(6) NOT NULL,
  `endDate` TIMESTAMP(6) NOT NULL,
  `Addictions_addictionName` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`caseid`),
  UNIQUE INDEX `Caseid_UNIQUE` (`caseid` ASC),
  INDEX `fk_Case_Member1_idx` (`counsellor_email` ASC),
  INDEX `fk_Case_User1_idx` (`patient_email` ASC),
  INDEX `fk_Case_Addictions1_idx` (`Addictions_addictionName` ASC),
  CONSTRAINT `fk_Case_Member1`
    FOREIGN KEY (`counsellor_email`)
    REFERENCES `addictionRemoval`.`Member` (`email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Case_User1`
    FOREIGN KEY (`patient_email`)
    REFERENCES `addictionRemoval`.`User` (`Member_email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Case_Addictions1`
    FOREIGN KEY (`Addictions_addictionName`)
    REFERENCES `addictionRemoval`.`Addictions` (`addictionName`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Chats`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Chats` (
  `message` LONGTEXT NOT NULL,
  `sentByCounsellor` TINYINT NOT NULL,
  `timestamp` TIMESTAMP(6) NOT NULL,
  `Case_caseid` INT NOT NULL,
  PRIMARY KEY (`Case_caseid`),
  CONSTRAINT `fk_Chats_Case1`
    FOREIGN KEY (`Case_caseid`)
    REFERENCES `addictionRemoval`.`Case` (`caseid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Doctor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Doctor` (
  `Specialization` VARCHAR(45) NOT NULL,
  `Member_email` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`Member_email`),
  CONSTRAINT `fk_Doctor_Member1`
    FOREIGN KEY (`Member_email`)
    REFERENCES `addictionRemoval`.`Member` (`email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Blog`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Blog` (
  `blogid` INT NOT NULL,
  `message` LONGTEXT NOT NULL,
  `title` VARCHAR(45) NOT NULL,
  `timestamp` TIMESTAMP(6) NULL,
  `Doctor_Member_email` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`blogid`),
  UNIQUE INDEX `blogid_UNIQUE` (`blogid` ASC),
  INDEX `fk_Blog_Doctor1_idx` (`Doctor_Member_email` ASC),
  CONSTRAINT `fk_Blog_Doctor1`
    FOREIGN KEY (`Doctor_Member_email`)
    REFERENCES `addictionRemoval`.`Doctor` (`Member_email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `addictionRemoval`.`Member_had_Addictions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `addictionRemoval`.`Member_had_Addictions` (
  `Member_email` VARCHAR(45) NOT NULL,
  `Addictions_addictionName` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`Member_email`, `Addictions_addictionName`),
  INDEX `fk_Member_has_Addictions_Addictions1_idx` (`Addictions_addictionName` ASC),
  INDEX `fk_Member_has_Addictions_Member1_idx` (`Member_email` ASC),
  CONSTRAINT `fk_Member_has_Addictions_Member1`
    FOREIGN KEY (`Member_email`)
    REFERENCES `addictionRemoval`.`Member` (`email`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Member_has_Addictions_Addictions1`
    FOREIGN KEY (`Addictions_addictionName`)
    REFERENCES `addictionRemoval`.`Addictions` (`addictionName`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Member`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Member` (`firstname`, `lastname`, `email`, `rating`, `accountCreatedOn`, `phoneNumber`, `city`, `dob`, `isBanned`, `isDoc`) VALUES ('Doc1', 'doc1', 'doc1@gmail.com', 3.5, '2017-10-28', '9565485622', 'Delhi', '1987-12-25', 0, 1);
INSERT INTO `addictionRemoval`.`Member` (`firstname`, `lastname`, `email`, `rating`, `accountCreatedOn`, `phoneNumber`, `city`, `dob`, `isBanned`, `isDoc`) VALUES ('Doc2', 'doc2', 'doc2@gmail.com', 2.5, '2017-10-28', '9565484622', 'Bombay', '1987-08-15', 0, 1);
INSERT INTO `addictionRemoval`.`Member` (`firstname`, `lastname`, `email`, `rating`, `accountCreatedOn`, `phoneNumber`, `city`, `dob`, `isBanned`, `isDoc`) VALUES ('User1', 'user1', 'user1@gmail.com', 2, '2017-10-28', '9545785233', 'Chandigarh', '1985-10-10', 0, 0);
INSERT INTO `addictionRemoval`.`Member` (`firstname`, `lastname`, `email`, `rating`, `accountCreatedOn`, `phoneNumber`, `city`, `dob`, `isBanned`, `isDoc`) VALUES ('User2', 'user2', 'user2@gmail.com', 3, '2017-10-28', '9546765433', 'Kolkata', '1985-09-09', 0, 0);

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Report`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Report` (`reportid`, `type`, `email`, `Member_email`, `Description`) VALUES (1, 'communication abuse', 'doc1@gmail.com', 'user2@gmail.com', 'He was using abusive language');

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`User`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`User` (`Member_email`) VALUES ('user1@gmail.com');
INSERT INTO `addictionRemoval`.`User` (`Member_email`) VALUES ('user2@gmail.com');

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Addictions`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Addictions` (`addictionName`) VALUES ('Drinking');
INSERT INTO `addictionRemoval`.`Addictions` (`addictionName`) VALUES ('Drugs');
INSERT INTO `addictionRemoval`.`Addictions` (`addictionName`) VALUES ('Smoking');

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Case`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Case` (`isCompleted`, `caseid`, `counsellor_email`, `patient_email`, `startDate`, `endDate`, `Addictions_addictionName`) VALUES (0, 1, 'doc1@gmail.com', 'user2@gmail.com', '2017-10-30 13:15:16.000000', '2000-01-01 00:00:01', 'smoking');
INSERT INTO `addictionRemoval`.`Case` (`isCompleted`, `caseid`, `counsellor_email`, `patient_email`, `startDate`, `endDate`, `Addictions_addictionName`) VALUES (0, 2, 'doc2@gmail.com', 'user1@gmail.com', '2017-10-30 13:14:16.000000', '2000-01-01 00:00:01', 'drinking');

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Doctor`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Doctor` (`Specialization`, `Member_email`) VALUES ('Psychologist', 'doc1@gmail.com');
INSERT INTO `addictionRemoval`.`Doctor` (`Specialization`, `Member_email`) VALUES ('Rehabilitation Counselor', 'doc2@gmail.com');

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Blog`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Blog` (`blogid`, `message`, `title`, `timestamp`, `Doctor_Member_email`) VALUES (1, 'Smoking is bad!', 'This is a blog on smoking', '2017-10-25 13:15:16.000000', 'doc1@gmail.com');
INSERT INTO `addictionRemoval`.`Blog` (`blogid`, `message`, `title`, `timestamp`, `Doctor_Member_email`) VALUES (2, 'Drinking is really bad and harmful.', 'Blog on Drinking', '2017-10-25 13:15:16.000000', 'doc2@gmail.com');

COMMIT;

-- -----------------------------------------------------
-- Data for table `addictionRemoval`.`Member_had_Addictions`
-- -----------------------------------------------------
START TRANSACTION;
USE `addictionRemoval`;
INSERT INTO `addictionRemoval`.`Member_had_Addictions` (`Member_email`, `Addictions_addictionName`) VALUES ('doc2@gmail.com', 'Drinking');
INSERT INTO `addictionRemoval`.`Member_had_Addictions` (`Member_email`, `Addictions_addictionName`) VALUES ('user2@gmail.com', 'Drinking');
INSERT INTO `addictionRemoval`.`Member_had_Addictions` (`Member_email`, `Addictions_addictionName`) VALUES ('doc1@gmail.com', 'Drugs');
INSERT INTO `addictionRemoval`.`Member_had_Addictions` (`Member_email`, `Addictions_addictionName`) VALUES ('doc2@gmail.com', 'Drugs');
INSERT INTO `addictionRemoval`.`Member_had_Addictions` (`Member_email`, `Addictions_addictionName`) VALUES ('doc1@gmail.com', 'Smoking');
INSERT INTO `addictionRemoval`.`Member_had_Addictions` (`Member_email`, `Addictions_addictionName`) VALUES ('user1@gmail.com', 'Smoking');

COMMIT;


text:
package com.github.dannil.httpdownloader.service;

import com.github.dannil.httpdownloader.model.User;

/**
 * Interface for RegisterService.
 *
 * @author Daniel Nilsson (daniel.nilsson94 @ outlook.com)
 * @version 1.0.1-SNAPSHOT
 * @since 0.0.1-SNAPSHOT
 */
public interface IRegisterService {

    // Others, defined in UserRepository
    User save(User user);

    User findByEmail(String email);

}


text:
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace GroupDocs_Annotation_SharePoint_WebPart.AnnotationResults
{
    [DataContract]
    public class DeleteAnnotationResult : Result
    {
        [XmlElement("id")]
        [DataMember(Name = "id")]
        public long Id { get; set; }

        [XmlElement("guid")]
        [DataMember(Name = "guid")]
        public string Guid { get; set; }
    }
}


text:
'use strict'

var split = require('./split')

/**
 * Select some elements from a gamut
 *
 * @name gamut.select
 * @function
 * @param {String|Array} numbers - a __1-based__ index of the elements
 * @param {String|Array} gamut - the notes or intervals
 * @return {Array} the selected elements
 *
 * @example
 * var select = require('tonal.gamut/select')
 * select('1 3 5', 'C D E F G A B') // => ['C', 'E', 'G']
 */
module.exports = function s (nums, gamut) {
  if (arguments.length === 1) return function (g) { return s(nums, g) }
  var g = split(gamut)
  return split(nums).map(function (n) {
    return g[n - 1]
  })
}


text:
package TPIS_Trab1.Services.Exception;

public class CouldNotReadFileException extends Exception {

    public CouldNotReadFileException(String message) {
        super(message);
    }

    public CouldNotReadFileException(String message, Exception e) {
        super(message, e);
    }

}


text:
share_examples_for 'A Relation' do

  before :all do
    # The two needed instance variables need to be set in a
    # before :all callback.
    #   @relation is the relation being tested here.
    #   @expected is an array of the elements that are expected to be in
    #     the relation.
    %w[ @relation @expected ].each do |ivar|
      raise "#{ivar} needs to be defined" unless instance_variable_get(ivar)
    end

    # There needs to be enough items to be able to run all the tests
    raise "@expected needs to have at least 6 items" unless @expected.length >= 6
  end

  before :each do
    @expected = @expected.dup
  end

  describe "#each" do
    it "iterates over the rows in any order" do
      @relation.should have_rows(@expected)
    end
  end

  describe "#where" do
    before :all do
      @expected = @expected.sort_by { |r| r[@relation[:age]] }
      @pivot = @expected[@expected.length / 2]
    end

    it "finds rows with an equal to predicate" do
      expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] }
      @relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected)
    end

    it "finds rows with an equal to complement predicate" do
      expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] }
      @relation.where(@relation[:age].eq(@pivot[@relation[:age]]).complement).should have_rows(expected)
    end

    it "finds rows with a not eq predicate" do
      expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] }
      @relation.where(@relation[:age].not_eq(@pivot[@relation[:age]])).should have_rows(expected)
    end

    it "finds rows with an not eq complement predicate" do
      expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] }
      @relation.where(@relation[:age].not_eq(@pivot[@relation[:age]]).complement).should have_rows(expected)
    end

    it "finds rows with a less than predicate" do
      expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] }
      @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected)
    end

    it "finds rows with a less than complement predicate" do
      expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] }
      @relation.where(@relation[:age].lt(@pivot[@relation[:age]]).complement).should have_rows(expected)
    end

    it "finds rows with a less than or equal to predicate" do
      expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] }
      @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected)
    end

    it "finds rows with a less than or equal to complement predicate" do
      expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] }
      @relation.where(@relation[:age].lteq(@pivot[@relation[:age]]).complement).should have_rows(expected)
    end

    it "finds rows with a greater than predicate" do
      expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] }
      @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected)
    end

    it "finds rows with a greater than complement predicate" do
      expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] }
      @relation.where(@relation[:age].gt(@pivot[@relation[:age]]).complement).should have_rows(expected)
    end

    it "finds rows with a greater than or equal to predicate" do
      expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] }
      @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected)
    end

    it "finds rows with a greater than or equal to complement predicate" do
      expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] }
      @relation.where(@relation[:age].gteq(@pivot[@relation[:age]]).complement).should have_rows(expected)
    end

    it "finds rows with a matches predicate" do
      expected = @expected.select { |r| r[@relation[:name]] =~ /#{@pivot[@relation[:name]]}/ }
      @relation.where(@relation[:name].matches(/#{@pivot[@relation[:name]]}/)).should have_rows(expected)
    end

    it "finds rows with a matches complement predicate" do
      expected = @expected.select { |r| r[@relation[:name]] !~ /#{@pivot[@relation[:name]]}/ }
      @relation.where(@relation[:name].matches(/#{@pivot[@relation[:name]]}/).complement).should have_rows(expected)
    end

    it "finds rows with a not matches predicate" do
      expected = @expected.select { |r| r[@relation[:name]] !~ /#{@pivot[@relation[:name]]}/ }
      @relation.where(@relation[:name].not_matches(/#{@pivot[@relation[:name]]}/)).should have_rows(expected)
    end

    it "finds rows with a not matches complement predicate" do
      expected = @expected.select { |r| r[@relation[:name]] =~ /#{@pivot[@relation[:name]]}/ }
      @relation.where(@relation[:name].not_matches(/#{@pivot[@relation[:name]]}/).complement).should have_rows(expected)
    end

    it "finds rows with an in predicate" do
      expected = @expected.select {|r| r[@relation[:age]] >=3 && r[@relation[:age]] <= 20}
      @relation.where(@relation[:age].in(3..20)).should have_rows(expected)
    end

    it "finds rows with an in complement predicate" do
      expected = @expected.select {|r| !(r[@relation[:age]] >=3 && r[@relation[:age]] <= 20)}
      @relation.where(@relation[:age].in(3..20).complement).should have_rows(expected)
    end

    it "finds rows with a not in predicate" do
      expected = @expected.select {|r| !(r[@relation[:age]] >=3 && r[@relation[:age]] <= 20)}
      @relation.where(@relation[:age].not_in(3..20)).should have_rows(expected)
    end

    it "finds rows with a not in complement predicate" do
      expected = @expected.select {|r| r[@relation[:age]] >=3 && r[@relation[:age]] <= 20}
      @relation.where(@relation[:age].not_in(3..20).complement).should have_rows(expected)
    end

    it "finds rows with a polyadic predicate of class Any" do
      expected = @expected.select {|r| [2,4,8,16].include?(r[@relation[:age]])}
      @relation.where(@relation[:age].in_any([2,4], [8, 16])).should have_rows(expected)
    end

    it "finds rows with a polyadic predicate of class Any complement" do
      expected = @expected.select {|r| ![2,4,8,16].include?(r[@relation[:age]])}
      @relation.where(@relation[:age].in_any([2,4], [8, 16]).complement).should have_rows(expected)
    end

    it "finds rows with a polyadic predicate of class All" do
      expected = @expected.select {|r| r[@relation[:name]] =~ /Name/ && r[@relation[:name]] =~ /1/}
      @relation.where(@relation[:name].matches_all(/Name/, /1/)).should have_rows(expected)
    end

    it "finds rows with a polyadic predicate of class All complement" do
      expected = @expected.select {|r| !(r[@relation[:name]] =~ /Name/ && r[@relation[:name]] =~ /1/)}
      @relation.where(@relation[:name].matches_all(/Name/, /1/).complement).should have_rows(expected)
    end
  end

  describe "#order" do
    describe "by one attribute" do
      before :all do
        @expected.sort! { |a, b| a[@relation[:age]] <=> b[@relation[:age]]}.map! {|e| e[@relation[:id]]}
      end

      it "can be specified as ascending order" do
        actual = []
        @relation.order(@relation[:age].asc).each { |r| actual << r[@relation[:id]] }
        actual.should == @expected
      end

      it "can be specified as descending order" do
        actual = []
        @relation.order(@relation[:age].desc).each { |r| actual << r[@relation[:id]] }
        actual.should == @expected.reverse
      end
    end

    describe "by two attributes in two separate calls to #order" do
      before :all do
        @expected = @expected.sort_by { |e| [e[@relation[:name]], e[@relation[:age]]]}.map {|e| e[@relation[:id]]}
      end

      it "can be specified as ascending order" do
        actual = []
        @relation.order(@relation[:age].asc).order(@relation[:name].asc).each { |r| actual << r[@relation[:id]] }
        actual.should == @expected
      end

      it "can be specified as descending order" do
        actual = []
        @relation.order(@relation[:age].desc).order(@relation[:name].desc).each { |r| actual << r[@relation[:id]] }
        actual.should == @expected.reverse
      end
    end

    describe "by two attributes in one call to #order" do
      before :all do
        @expected = @expected.sort_by { |e| [e[@relation[:name]], e[@relation[:age]]]}.map {|e| e[@relation[:id]]}
      end

      it "can be specified as ascending order in one call to #order" do
        actual = []
        @relation.order(@relation[:name].asc, @relation[:age].asc).each { |r| actual << r[@relation[:id]] }
        actual.should == @expected
      end

      it "can be specified as descending order in one call to #order" do
        actual = []
        @relation.order(@relation[:name].desc, @relation[:age].desc).each { |r| actual << r[@relation[:id]] }
        actual.should == @expected.reverse
      end
    end
  end

  describe "#take" do
    it "returns a relation" do
      @relation.take(3).should be_a(Arel::Relation)
    end

    it "returns X items from the collection" do
      length = @expected.length

      @relation.take(3).each do |resource|
        @expected.delete_if { |r| r.tuple == resource.tuple }
      end

      @expected.length.should == length - 3
    end

    it "works with ordering" do
      expected = @expected.sort_by { |r| [r[@relation[:age]], r[@relation[:id]]] }.map { |r| r[@relation[:id]] }
      actual   = @relation.order(@relation[:age].asc, @relation[:id].asc).take(3).map { |r| r[@relation[:id]] }

      actual.should == expected[0,3]
    end
  end

  describe "#skip" do
    it "returns a relation" do
      @relation.skip(3).should be_a(Arel::Relation)
    end

    it "skips X items from the collection" do
      length = @expected.length

      @relation.skip(3).each do |resource|
        @expected.delete_if { |r| r.tuple == resource.tuple }
      end

      @expected.length.should == 3
    end

    it "works with ordering" do
      expected = @expected.sort_by { |r| [r[@relation[:age]], r[@relation[:id]]] }.map { |r| r[@relation[:id]] }
      actual   = @relation.order(@relation[:age].asc, @relation[:id].asc).skip(3).map { |r| r[@relation[:id]] }

      actual.should == expected[3..-1]
    end
  end
end


text:
<?php

if (!defined('PGV_PHPGEDVIEW')) {
	header('HTTP/1.0 403 Forbidden');
	exit;
}

// -- Define a fact array to map GEDCOM tags with their brasilian portugese values
$factarray["ABBR"]="Abreviação";
$factarray["ADDR"]="Endereço";
$factarray["ADR1"]="Endereço 1";
$factarray["ADR2"]="Endereço 2";
$factarray["ADOP"]="Adoção";
$factarray["AGE"]="Idade";
$factarray["AGNC"]="Agência";
$factarray["ALIA"]="Apelido";
$factarray["ANCE"]="Ancestrais";
$factarray["ANUL"]="Anulação";
$factarray["ASSO"]="Associado";
$factarray["AUTH"]="Autor";
$factarray["BAPL"]="Batismo LDS";
$factarray["BAPM"]="Batismo";
$factarray["BARM"]="Bar Mitzvah";
$factarray["BASM"]="Bas Mitzvah";
$factarray["BIRT"]="Nascimento";
$factarray["BLES"]="Bênção";
$factarray["BLOB"]="Objeto Binário";
$factarray["BURI"]="Sepultamento";
$factarray["CALN"]="Número de Chamada";
$factarray["CAST"]="Casta/Status Social";
$factarray["CAUS"]="Causa do Falecimento";
$factarray["CEME"]="Cemitério";
$factarray["CENS"]="Censo";
$factarray["CHAN"]="Atualizado em";
$factarray["CHAR"]="Conjunto de caracteres";
$factarray["CHIL"]="Filho";
$factarray["CHR"]="Batizado";
$factarray["CITY"]="Cidade";
$factarray["CONF"]="Confirmação";
$factarray["CONL"]="Confirmação LDS";
$factarray["COPR"]="Direitos Autorais";
$factarray["CORP"]="Instituição/Empresa";
$factarray["CREM"]="Cremação";
$factarray["CTRY"]="País";
$factarray["DATA"]="Dados";
$factarray["DATE"]="Data";
$factarray["DEAT"]="Falecimento";
$factarray["DESC"]="Descendentes";
$factarray["DEST"]="Destino";
$factarray["DIV"]="Divórcio";
$factarray["DIVF"]="Pedido de Divórcio";
$factarray["DSCR"]="Descrição";
$factarray["EDUC"]="Educação";
$factarray["EMIG"]="Emigração";
$factarray["ENGA"]="Noivado";
$factarray["EVEN"]="Evento";
$factarray["FAM"]="Família";
$factarray["FAMC"]="Família como Filho";
$factarray["FAMF"]="Arquivo de Família";
$factarray["FAMS"]="Familia como Cônjuge";
$factarray["FCOM"]="Primeira Comunhão";
$factarray["FILE"]="Arquivo Externo";
$factarray["FORM"]="Formato";
$factarray["GIVN"]="Nomes";
$factarray["GRAD"]="Graduação";
$factarray["HUSB"]="Marido";
$factarray["IDNO"]="Número de Identificação";
$factarray["IMMI"]="Imigração";
$factarray["LEGA"]="Herdeiro";
$factarray["MARC"]="Contrato Matrimonial";
$factarray["MARL"]="Licença de Casamento";
$factarray["MARR"]="Casamento";
$factarray["MARS"]="Acôrdo Matrimonial";
$factarray["MEDI"]="Tipo de Mídia";
$factarray["NAME"]="Nome";
$factarray["NATI"]="Nacionalidade";
$factarray["NATU"]="Naturalização";
$factarray["NCHI"]="Número de Filhos";
$factarray["NICK"]="Apelido";
$factarray["NMR"]="Número de Casamentos";
$factarray["NOTE"]="Nota";
$factarray["NPFX"]="Prefixo";
$factarray["NSFX"]="Sufixo";
$factarray["OBJE"]="Mídia";
$factarray["OCCU"]="Ocupação";
$factarray["ORDI"]="Mandato";
$factarray["ORDN"]="Ordenação";
$factarray["PAGE"]="Detalhes da Citação";
$factarray["PEDI"]="Árvore Genealógica";
$factarray["PLAC"]="Local";
$factarray["PHON"]="Telefone";
$factarray["POST"]="CEP";
$factarray["PROB"]="Comprovação de Legitimidade";
$factarray["PROP"]="Propriedade";
$factarray["PUBL"]="Publicação";
$factarray["QUAY"]="Credibilidade dos Dados";
$factarray["REPO"]="Repositório ";
$factarray["REFN"]="Número de Referência";
$factarray["RELA"]="Relacionamento";
$factarray["RELI"]="Religião";
$factarray["RESI"]="Residência";
$factarray["RESN"]="Restrição";
$factarray["RETI"]="Aposentadoria";
$factarray["RFN"]="Número do Registro";
$factarray["RIN"]="Número de Identificação do Registro";
$factarray["ROLE"]="Cargo";
$factarray["SEX"]="Sexo";
$factarray["SOUR"]="Fonte";
$factarray["SPFX"]="Prefixo do Sobrenome";
$factarray["SSN"]="Número do Seguro Social";
$factarray["STAE"]="Estado";
$factarray["STAT"]="Situação";
$factarray["SURN"]="Sobrenome";
$factarray["TEMP"]="Templo";
$factarray["TEXT"]="Texto";
$factarray["TIME"]="HH:MM:SS";
$factarray["TITL"]="Título";
$factarray["TYPE"]="Tipo";
$factarray["WIFE"]="Esposa";
$factarray["WILL"]="Testamento";
$factarray["_EMAIL"]="Endereço de Email";
$factarray["EMAIL"]="Endereço de Email";
$factarray["_TODO"]="A Fazer";
$factarray["_UID"]="Identificador Universal";
$factarray["_PRIM"]="Imagem de destaque";
$factarray["_MDCL"]	= "Prontuário";
$factarray["_DEG"]="Graduação";
$factarray["_MILT"]="Serviço Militar";
$factarray["_SEPR"]="Separado";
$factarray["_DETS"]="Falecimento de Cônjuge";
$factarray["CITN"]="Cidadania";
$factarray["_FA1"]="Fato 1";
$factarray["_FA2"]="Fato 2";
$factarray["_FA3"]="Fato 3";
$factarray["_FA4"]="Fato 4";
$factarray["_FA5"]="Fato 5";
$factarray["_FA6"]="Fato 6";
$factarray["_FA7"]="Fato 7";
$factarray["_FA8"]="Fato 8";
$factarray["_FA9"]="Fato 9";
$factarray["_FA10"]="Fato 10";
$factarray["_FA11"]="Fato 11";
$factarray["_FA12"]="Fato 12";
$factarray["_FA13"]="Fato 13";
$factarray["_MEND"]	= "Situação Final Do Casamento";
$factarray["_MSTAT"]	= "Situação Inicial Do Casamento";
$factarray["_FREL"]	= "Relação Paterna";
$factarray["_MREL"]	= "Relação Materna";
$factarray["FAX"]="FAX";
$factarray["FACT"]="Fato";
$factarray["WWW"]="Página Pessoal na Internet";
$factarray["MAP"]="Mapa";
$factarray["LATI"]="Latitude";
$factarray["LONG"]="Longitude";
$factarray["_HEB"] = "Hebreu";
$factarray["FONE"]="Fonético";
$factarray["ROMN"]="Romano";
$factarray["_NAME"]="Nome para Correspondência";
$factarray["URL"]="URL na Internet";
$factarray["_SCBK"]="Livro de Fotos";
$factarray["_TYPE"]="Tipo de Mídia";
$factarray["_SSHOW"]="Show de Slide";
$factarray["_SUBQ"]="Versão Reduzida";
$factarray["_BIBL"]="Bibliografia";
$factarray["EMAL"]="Endereço de Email";
$factarray["_ADPF"]="Adoção Paterna";
$factarray["_ADPM"]="Adoção Materna";
$factarray["_AKAN"]="Também conhecido por";
$factarray["_AKA"]="Também conhecido por";
$factarray["_EYEC"]="Cor dos olhos";
$factarray["_MARI"]	= "Intenção Matrimonial";
$factarray["_FNRL"]="Funeral";
$factarray["_HAIR"]="Cor do cabelo";
$factarray["_HEIG"]="Altura";
$factarray["_HOL"]="Holocausto";
$factarray["_INTE"]="Sepultado";
$factarray["_MBON"]="Ligação Familiar";
$factarray["_MEDC"]="Quadro Clínico";
$factarray["_MILI"]="Militar";
$factarray["_NMR"]="Solteiro";
$factarray["_NLIV"]="Não Vivos";
$factarray["_NMAR"]="Nunca foi casado";
$factarray["_PRMN"]="Número permanente";
$factarray["_WEIG"]="Peso";
$factarray["_YART"]="Yartzeit";
$factarray["_MARNM"]="Nome de casada";
$factarray["_STAT"]="Estado Civil";
$factarray["COMM"]="Comentário";
$factarray["MARR_CIVIL"]="Casamento Civil";
$factarray["MARR_RELIGIOUS"]="Casamento Religioso";
$factarray["MARR_PARTNERS"]="Regime de Bens";
$factarray["MARR_UNKNOWN"]="Casamento Desconhecido";
$factarray["_HNM"]="Nome Hebráico";
$factarray["_DEAT_SPOU"]="Falecimento do Cônjuge";
$factarray["_BIRT_CHIL"]="Nascimento de Filho";
$factarray["_MARR_CHIL"]="Casamento de Filho";
$factarray["_DEAT_CHIL"]="Falecimento de Filho";
$factarray["_BIRT_GCHI"]="Nascimento de Neto";
$factarray["_MARR_GCHI"]="Casamento de Neto";
$factarray["_DEAT_GCHI"]="Falecimento de Neto";
$factarray["_MARR_FATH"]="Casamento do Pai";
$factarray["_DEAT_FATH"]="Falecimento do Pai";
$factarray["_MARR_MOTH"]="Casamento da Mãe";
$factarray["_DEAT_MOTH"]="Falecimento da Mãe";
$factarray["_BIRT_SIBL"]="Nascimento de Parente";
$factarray["_MARR_SIBL"]="Casamento de Parente";
$factarray["_DEAT_SIBL"]="Falecimento de Parente";
$factarray["_BIRT_HSIB"]="Nascimento de Aparentado";
$factarray["_MARR_HSIB"]="Casamento de Aparentado";
$factarray["_DEAT_HSIB"]="Falecimento de Aparentado";
$factarray["_DEAT_GPAR"]="Falecimento de Avô";
$factarray["_BIRT_FSIB"]="Nascimento de Parente Paterno";
$factarray["_MARR_FSIB"]="Casamento de Parente Paterno";
$factarray["_DEAT_FSIB"]="Falecimento de Parente Paterno";
$factarray["_BIRT_MSIB"]="Nascimento de Parente Materno";
$factarray["_MARR_MSIB"]="Casamento de Parente Materno";
$factarray["_DEAT_MSIB"]="Falecimento de Parente Materno";
$factarray["_BIRT_COUS"]="Nascimento de Primo";
$factarray["_MARR_COUS"]="Casamento de Primo";
$factarray["_DEAT_COUS"]="Falecimento de Primo";
$factarray["_THUM"]="Usar esta imagem como Miniatura?";
$factarray["_PGVU"]="Última Alteração por";
$factarray["SERV"]="Servidor Remoto";
$factarray["_GEDF"]="Arquivo GEDCOM";
?>


text:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="20dp"
    android:height="20dp"
    android:viewportWidth="20"
    android:viewportHeight="20"
    android:tint="?attr/colorControlNormal">
  <path
      android:fillColor="@android:color/white"
      android:pathData="M5.5,18.75V2.958H8.042V1.25H11.958V2.958H14.5V18.75Z"/>
</vector>


text:
Focus meter - Innovative Project
==========

|     #1           | Focus meter           |
| ---------------- | ------------- |
| Project goals    | Mobile application for measuring meeting participants focus and engagement. |
| Scope definition | Mobile application and web server that would allow to monitor participants mood. Based on the survey meeting agenda could be changed.|
| Requirements     | Windows Phone/Android      |
| Idea author      | Mateusz Jaworski     |

### Prototype

![](https://github.com/nsn-wroclaw/focusmeter/raw/master/doc/mobile.png)


text:
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace MP.FilterReader.COMTypeDef
{
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000109-0000-0000-C000-000000000046")]
    public interface IPersistStream
    {
        void GetClassID(out Guid pClassID);
        [PreserveSig]
        int IsDirty();
        void Load([In] IStream pStm);
        void Save([In] IStream pStm, [In, MarshalAs(UnmanagedType.Bool)] bool fClearDirty);
        void GetSizeMax(out long pcbSize);
    }
}


text:
import { UserContext } from '@savantly/sprout-api';
export interface UserContextService {
    getUserContext: () => UserContext;
}

let singletonInstance: UserContextService;

/**
 * Used during startup by Sprout to set the service so it is available
 * via the {@link getUserContextService} to the rest of the application.
 *
 * @internal
 */
export function setUserContextService(instance: UserContextService) {
  singletonInstance = instance;
}

/**
 * Used to retrieve the {@link UserContextService}
 *
 * @public
 */
export function getUserContextService(): UserContextService {
  return singletonInstance;
}


text:
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Debugger.Editor {
    public class BraceHighlighter : IBackgroundRenderer {
        TextEditor editor;

        public BraceHighlighter(TextEditor editor) {
            this.editor = editor;
        }

        public void Draw(TextView textView, System.Windows.Media.DrawingContext drawingContext) {

        }

        public KnownLayer Layer {
            get { return KnownLayer.Background; }
        }
    }
}


text:
class UpdateNuclearPlantCostsInputs < ActiveRecord::Migration[7.0]
  include ETEngine::ScenarioMigration

  UPDATES = {
    investment_costs_nuclear_nuclear_plant: [4_950_000_000.0, 6_992_000_000.0],
    om_costs_nuclear_nuclear_plant: [6_288.0, 4_000.0]
  }

  def up
    UPDATES.each do |key, (before, after)|
      say_new_default(key, before, after)
    end

    migrate_scenarios do |scenario|
      UPDATES.each do |key, (before, after)|
        run(scenario, key, before, after)
      end
    end
  end

  def down
    UPDATES.each do |key, (after, before)|
      say_new_default(key, before, after)
    end

    migrate_scenarios do |scenario|
      UPDATES.each do |key, (after, before)|
        run(scenario, key, before, after, true)
      end
    end
  end

  private

  def say_new_default(key, before, after)
    say "New default for #{key}: #{default_value(before, after)}"
  end

  def default_value(before, after)
    ((before / after) - 1.0) * 100
  end

  def run(scenario, key, before, after, rollback = true)
    original_value = scenario.user_values[key]

    new_percentage =
      if original_value
        # Update existing value.
        factor = 1 + (original_value.to_f / 100)
        real_value = before * factor
        new_factor = real_value / after

        (new_factor - 1) * 100
      else
        # Set new default to match the old costs.
        default_value(before, after)
      end

    # When rolling back, if the value is 0% it's likely the original scenario had no value.
    # Remove it.
    if rollback && new_percentage.between?(-0.0001, 0.0001)
      scenario.user_values.delete(key)
    else
      scenario.user_values[key] = new_percentage.clamp(-100.0, 300.0)
    end
  end
end


text:
classdef prtPreProcPls < prtPreProc
    % prtPreProcPls   Partial least squares
    %
    %   PLS = prtPreProcPls creates a partial least-squares pre-processing
    %   object.
    %
    %   PLS = prtPreProcPls('nComponents',N) constructs a
    %   prtPreProcPLS object PLS with nComponents set to the value N.
    %
    %   A prtPreProcPls object has the following properites:
    %
    %    nComponents    - The number of principle componenets
    %
    %   A prtPreProcPls object also inherits all properties and functions from
    %   the prtAction class
    %
    %   Example:
    %
    %   dataSet = prtDataGenFeatureSelection;  % Load a data set
    %   pls = prtPreProcPls;                   % Create a prtPreProcPls Object
    %
    %   pls = pls.train(dataSet);              % Train
    %   dataSetNew = pls.run(dataSet);         % Run
    %
    %   % Plot
    %   plot(dataSetNew);
    %   title('PLS Projected Data');
    %
    %   See Also: prtPreProc, prtPreProcPca, prtPreProcPls,
    %   prtPreProcHistEq, prtPreProcZeroMeanColumns, prtPreProcLda,
    %   prtPreProcZeroMeanRows, prtPreProcLogDisc, prtPreProcZmuv,
    %   prtPreProcMinMaxRows

% Copyright (c) 2014 CoVar Applied Technologies
%
% Permission is hereby granted, free of charge, to any person obtaining a
% copy of this software and associated documentation files (the
% "Software"), to deal in the Software without restriction, including
% without limitation the rights to use, copy, modify, merge, publish,
% distribute, sublicense, and/or sell copies of the Software, and to permit
% persons to whom the Software is furnished to do so, subject to the
% following conditions:
%
% The above copyright notice and this permission notice shall be included
% in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
% OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
% NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
% OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
% USE OR OTHER DEALINGS IN THE SOFTWARE.

    properties (SetAccess=private)
        name = 'Partial Least Squares' % Partial Least Squares
        nameAbbreviation = 'PLS' % PLS
    end

    properties
        nComponents = 2;   % The number of Pls components
    end
    properties (SetAccess=private)

        projectionMatrix = [];  % Projection Matrix
        xMeanVector = [];  % X means
        yMeanVector = [];  % Y means
    end

    methods

          % Allow for string, value pairs
        function Obj = prtPreProcPls(varargin)
            Obj = prtUtilAssignStringValuePairs(Obj,varargin{:});
        end
    end

    methods
        function Obj = set.nComponents(Obj,nComp)
            if ~prtUtilIsPositiveScalarInteger(nComp);
                error('prt:prtPreProcPls','nComponents must be a positive scalar integer.');
            end
            Obj.nComponents = nComp;
        end
	end

	methods (Hidden = true)
        function featureNameModificationFunction = getFeatureNameModificationFunction(obj) %#ok<MANU>
            featureNameModificationFunction = prtUtilFeatureNameModificationFunctionHandleCreator('PLS Score #index#');
        end
	end

    methods (Access=protected,Hidden=true)

        function Obj = trainAction(Obj,DataSet)
            DataSet = DataSet.retainLabeled;

            X = DataSet.getObservations;
%             if DataSet.nClasses > 2
%                 Y = DataSet.getTargetsAsBinaryMatrix;
%             else
%                 Y = DataSet.getTargetsAsBinaryMatrix;
%                 Y = Y(:,2); %0's and 1's for H1
%             end
            %

            Y = DataSet.getY;
            maxComps = min(size(X));
            if Obj.nComponents > maxComps;
                warning('prt:prtPreProcPls','nComponents (%d) > maximum components for the data set (%d); setting nComponents = %d',Obj.nComponents,maxComps,maxComps);
                Obj.nComponents = maxComps;
            end

            Obj.xMeanVector = mean(X,1);
            Obj.yMeanVector = mean(Y,1);
            X = bsxfun(@minus, X, Obj.xMeanVector);
            Y = bsxfun(@minus, Y, Obj.yMeanVector);

            [garbage,garbage, P] = prtUtilSimpls(X,Y,Obj.nComponents);

            Obj.projectionMatrix = pinv(P');
            Obj.projectionMatrix = bsxfun(@rdivide,Obj.projectionMatrix,sqrt(sum(Obj.projectionMatrix.^2,1)));
        end

        function DataSet = runAction(Obj,DataSet)
            X = DataSet.getObservations;
            X = bsxfun(@minus,X,Obj.xMeanVector);
            DataSet = DataSet.setObservations(X*Obj.projectionMatrix);
        end
    end
end


text:
namespace IPC {

template <class Element>
struct ParamTraits<google::protobuf::RepeatedPtrField<Element>> {
  typedef google::protobuf::RepeatedPtrField<Element> param_type;

  static void Write(base::Pickle* m, const param_type& p) {
    WriteParam(m, p.size());
    for (const auto& element : p)
      WriteParam(m, element);
  }

  static bool Read(const base::Pickle* m,
                   base::PickleIterator* iter,
                   param_type* p) {
    int size;
    if (!iter->ReadLength(&size) || size < 0)
      return false;
    if (INT_MAX / static_cast<int>(sizeof(void*)) <= size)
      return false;
    p->Clear();
    p->Reserve(size);
    while (size--) {
      if (!ReadParam(m, iter, p->Add()))
        return false;
    }
    return true;
  }

  static void Log(const param_type& p, std::string* l) {
    bool logged_first = false;
    for (const auto& element : p) {
      if (logged_first)
        l->push_back(' ');
      else
        logged_first = true;
      LogParam(element, l);
    }
  }
};

}  // namespace IPC

#endif  // CHROME_COMMON_SAFE_BROWSING_PROTOBUF_MESSAGE_PARAM_TRAITS_H_