<commit_before>// Copyright Microsoft Corp.
// All rights reserved.

package ipam

import (
	"fmt"
	"net"
	"strings"
)

const (
	// Default address space IDs.
	localDefaultAddressSpaceId  = "LocalDefaultAddressSpace"
	globalDefaultAddressSpaceId = "GlobalDefaultAddressSpace"

	// Address space scopes.
	localScope  = "local"
	globalScope = "global"
)

// Represents the key to an address pool.
type addressPoolId struct {
	asId        string
	subnet      string
	childSubnet string
}

// Represents a set of non-overlapping address pools.
type addressSpace struct {
	Id    string
	Scope string
	Pools map[string]*addressPool
	epoch int
}

// Represents a subnet and the set of addresses in it.
type addressPool struct {
	as        *addressSpace
	Id        string
	IfName    string
	Subnet    net.IPNet
	Addresses map[string]*addressRecord
	IsIPv6    bool
	Priority  int
	InUse     bool
	epoch     int
}

// Represents an IP address in a pool.
type addressRecord struct {
	Addr  net.IP
	InUse bool
	epoch int
}

//
// AddressPoolId
//

// Creates a new address pool ID object.
func newAddressPoolId(asId string, subnet string, childSubnet string) *addressPoolId {
	return &addressPoolId{
		asId:        asId,
		subnet:      subnet,
		childSubnet: childSubnet,
	}
}

// Creates a new pool ID from a string representation.
func newAddressPoolIdFromString(s string) (*addressPoolId, error) {
	var pid addressPoolId

	p := strings.Split(s, "|")
	if len(p) > 3 {
		return nil, errInvalidPoolId
	}

	pid.asId = p[0]
	if len(p) >= 2 {
		pid.subnet = p[1]
	}
	if len(p) == 3 {
		pid.childSubnet = p[2]
	}

	return &pid, nil
}

// Returns the string representation of a pool ID.
func (pid *addressPoolId) String() string {
	s := fmt.Sprintf("%s|%s", pid.asId, pid.subnet)
	if pid.childSubnet != "" {
		s = fmt.Sprintf("%s|%s", s, pid.childSubnet)
	}
	return s
}

//
// AddressSpace
//

// Creates a new addressSpace object.
func (am *addressManager) newAddressSpace(id string, scope string) (*addressSpace, error) {
	if scope != localScope && scope != globalScope {
		return nil, errInvalidScope
	}

	return &addressSpace{
		Id:    id,
		Scope: scope,
		Pools: make(map[string]*addressPool),
	}, nil
}

// Returns the address space with the given ID.
func (am *addressManager) getAddressSpace(id string) (*addressSpace, error) {
	as := am.AddrSpaces[id]
	if as == nil {
		return nil, errInvalidAddressSpace
	}

	return as, nil
}

// Sets a new or updates an existing address space.
func (am *addressManager) setAddressSpace(as *addressSpace) error {
	as1, ok := am.AddrSpaces[as.Id]
	if !ok {
		am.AddrSpaces[as.Id] = as
	} else {
		as1.merge(as)
	}

	// Notify NetPlugin of external interfaces.
	for _, ap := range as.Pools {
		am.netApi.AddExternalInterface(ap.IfName, ap.Subnet.String())
	}

	am.save()

	return nil
}

// Merges a new address space to an existing one.
func (as *addressSpace) merge(newas *addressSpace) {
	// The new epoch after the merge.
	as.epoch++

	// Add new pools and addresses.
	for pk, pv := range newas.Pools {
		ap := as.Pools[pk]

		if ap == nil {
			// This is a new address pool.
			// Merge it to the existing address space.
			as.Pools[pk] = pv
			delete(newas.Pools, pk)
			pv.epoch = as.epoch
		} else {
			// This pool already exists.
			// Compare address records one by one.
			for ak, av := range pv.Addresses {
				ar := ap.Addresses[ak]

				if ar == nil {
					// This is a new address record.
					// Merge it to the existing address pool.
					ap.Addresses[ak] = av
					delete(ap.Addresses, ak)
					av.epoch = as.epoch
				} else {
					// This address record already exists.
					ar.epoch = as.epoch
				}
			}

			ap.epoch = as.epoch
		}
	}

	// Cleanup stale pools and addresses from the old epoch.
	// Those currently in use will be deleted after they are released.
	for pk, pv := range as.Pools {
		if pv.epoch < as.epoch {
			for ak, av := range pv.Addresses {
				if !av.InUse {
					delete(pv.Addresses, ak)
				}
			}

			if !pv.InUse {
				delete(as.Pools, pk)
			}
		}
	}

	return
}

// Creates a new addressPool object.
func (as *addressSpace) newAddressPool(ifName string, priority int, subnet *net.IPNet) (*addressPool, error) {
	id := subnet.String()

	pool, ok := as.Pools[id]
	if ok {
		return pool, errAddressPoolExists
	}

	v6 := (len(subnet.IP) > net.IPv4len)

	pool = &addressPool{
		as:        as,
		Id:        id,
		IfName:    ifName,
		Subnet:    *subnet,
		Addresses: make(map[string]*addressRecord),
		IsIPv6:    v6,
		Priority:  priority,
		epoch:     as.epoch,
	}

	as.Pools[id] = pool

	return pool, nil
}

// Returns the address pool with the given pool ID.
func (as *addressSpace) getAddressPool(poolId string) (*addressPool, error) {
	ap := as.Pools[poolId]
	if ap == nil {
		return nil, errInvalidPoolId
	}

	return ap, nil
}

// Requests a new address pool from the address space.
func (as *addressSpace) requestPool(poolId string, subPoolId string, options map[string]string, v6 bool) (*addressPool, error) {
	var ap *addressPool

	if poolId != "" {
		// Return the specific address pool requested.
		ap = as.Pools[poolId]
		if ap == nil {
			return nil, errAddressPoolNotFound
		}

		// Fail if requested pool is already in use.
		if ap.InUse {
			return nil, errAddressPoolInUse
		}
	} else {
		// Return any available address pool.
		highestPriority := -1

		for _, pool := range as.Pools {
			// Skip if pool is already in use.
			if pool.InUse {
				continue
			}

			// Pick a pool from the same address family.
			if pool.IsIPv6 != v6 {
				continue
			}

			// Pick the pool with the highest priority.
			if pool.Priority > highestPriority {
				highestPriority = pool.Priority
				ap = pool
			}
		}

		if ap == nil {
			return nil, errNoAvailableAddressPools
		}
	}

	ap.InUse = true

	return ap, nil
}

// Releases a previously requested address pool back to its address space.
func (as *addressSpace) releasePool(poolId string) error {
	ap, ok := as.Pools[poolId]
	if !ok {
		return errAddressPoolNotFound
	}

	if !ap.InUse {
		return errAddressPoolNotInUse
	}

	ap.InUse = false

	// Delete address pool if it is no longer available.
	if ap.epoch < as.epoch {
		delete(as.Pools, poolId)
	}

	return nil
}

//
// AddressPool
//

// Creates a new addressRecord object.
func (ap *addressPool) newAddressRecord(addr *net.IP) (*addressRecord, error) {
	id := addr.String()

	if !ap.Subnet.Contains(*addr) {
		return nil, errInvalidAddress
	}

	ar, ok := ap.Addresses[id]
	if ok {
		return ar, errAddressExists
	}

	ar = &addressRecord{
		Addr:  *addr,
		epoch: ap.epoch,
	}

	ap.Addresses[id] = ar

	return ar, nil
}

// Requests a new address from the address pool.
func (ap *addressPool) requestAddress(address string, options map[string]string) (string, error) {
	var ar *addressRecord

	if address != "" {
		// Return the specific address requested.
		ar = ap.Addresses[address]
		if ar == nil {
			return "", errAddressNotFound
		}
		if ar.InUse {
			return "", errAddressInUse
		}
	} else {
		// Return any available address.
		for _, ar = range ap.Addresses {
			if !ar.InUse {
				break
			}
		}

		if ar == nil {
			return "", errNoAvailableAddresses
		}
	}

	ar.InUse = true

	// Return address in CIDR notation.
	addr := net.IPNet{
		IP:   ar.Addr,
		Mask: ap.Subnet.Mask,
	}

	return addr.String(), nil
}

// Releases a previously requested address back to its address pool.
func (ap *addressPool) releaseAddress(address string) error {
	ar := ap.Addresses[address]
	if ar == nil {
		return errAddressNotFound
	}
	if !ar.InUse {
		return errAddressNotInUse
	}

	ar.InUse = false

	// Delete address record if it is no longer available.
	if ar.epoch < ap.as.epoch {
		delete(ap.Addresses, address)
	}

	return nil
}
<commit_msg>Fixed IP address family check<commit_after>// Copyright Microsoft Corp.
// All rights reserved.

package ipam

import (
	"fmt"
	"net"
	"strings"
)

const (
	// Default address space IDs.
	localDefaultAddressSpaceId  = "LocalDefaultAddressSpace"
	globalDefaultAddressSpaceId = "GlobalDefaultAddressSpace"

	// Address space scopes.
	localScope  = "local"
	globalScope = "global"
)

// Represents the key to an address pool.
type addressPoolId struct {
	asId        string
	subnet      string
	childSubnet string
}

// Represents a set of non-overlapping address pools.
type addressSpace struct {
	Id    string
	Scope string
	Pools map[string]*addressPool
	epoch int
}

// Represents a subnet and the set of addresses in it.
type addressPool struct {
	as        *addressSpace
	Id        string
	IfName    string
	Subnet    net.IPNet
	Addresses map[string]*addressRecord
	IsIPv6    bool
	Priority  int
	InUse     bool
	epoch     int
}

// Represents an IP address in a pool.
type addressRecord struct {
	Addr  net.IP
	InUse bool
	epoch int
}

//
// AddressPoolId
//

// Creates a new address pool ID object.
func newAddressPoolId(asId string, subnet string, childSubnet string) *addressPoolId {
	return &addressPoolId{
		asId:        asId,
		subnet:      subnet,
		childSubnet: childSubnet,
	}
}

// Creates a new pool ID from a string representation.
func newAddressPoolIdFromString(s string) (*addressPoolId, error) {
	var pid addressPoolId

	p := strings.Split(s, "|")
	if len(p) > 3 {
		return nil, errInvalidPoolId
	}

	pid.asId = p[0]
	if len(p) >= 2 {
		pid.subnet = p[1]
	}
	if len(p) == 3 {
		pid.childSubnet = p[2]
	}

	return &pid, nil
}

// Returns the string representation of a pool ID.
func (pid *addressPoolId) String() string {
	s := fmt.Sprintf("%s|%s", pid.asId, pid.subnet)
	if pid.childSubnet != "" {
		s = fmt.Sprintf("%s|%s", s, pid.childSubnet)
	}
	return s
}

//
// AddressSpace
//

// Creates a new addressSpace object.
func (am *addressManager) newAddressSpace(id string, scope string) (*addressSpace, error) {
	if scope != localScope && scope != globalScope {
		return nil, errInvalidScope
	}

	return &addressSpace{
		Id:    id,
		Scope: scope,
		Pools: make(map[string]*addressPool),
	}, nil
}

// Returns the address space with the given ID.
func (am *addressManager) getAddressSpace(id string) (*addressSpace, error) {
	as := am.AddrSpaces[id]
	if as == nil {
		return nil, errInvalidAddressSpace
	}

	return as, nil
}

// Sets a new or updates an existing address space.
func (am *addressManager) setAddressSpace(as *addressSpace) error {
	as1, ok := am.AddrSpaces[as.Id]
	if !ok {
		am.AddrSpaces[as.Id] = as
	} else {
		as1.merge(as)
	}

	// Notify NetPlugin of external interfaces.
	if am.netApi != nil {
		for _, ap := range as.Pools {
			am.netApi.AddExternalInterface(ap.IfName, ap.Subnet.String())
		}
	}

	am.save()

	return nil
}

// Merges a new address space to an existing one.
func (as *addressSpace) merge(newas *addressSpace) {
	// The new epoch after the merge.
	as.epoch++

	// Add new pools and addresses.
	for pk, pv := range newas.Pools {
		ap := as.Pools[pk]

		if ap == nil {
			// This is a new address pool.
			// Merge it to the existing address space.
			as.Pools[pk] = pv
			delete(newas.Pools, pk)
			pv.epoch = as.epoch
		} else {
			// This pool already exists.
			// Compare address records one by one.
			for ak, av := range pv.Addresses {
				ar := ap.Addresses[ak]

				if ar == nil {
					// This is a new address record.
					// Merge it to the existing address pool.
					ap.Addresses[ak] = av
					delete(ap.Addresses, ak)
					av.epoch = as.epoch
				} else {
					// This address record already exists.
					ar.epoch = as.epoch
				}
			}

			ap.epoch = as.epoch
		}
	}

	// Cleanup stale pools and addresses from the old epoch.
	// Those currently in use will be deleted after they are released.
	for pk, pv := range as.Pools {
		if pv.epoch < as.epoch {
			for ak, av := range pv.Addresses {
				if !av.InUse {
					delete(pv.Addresses, ak)
				}
			}

			if !pv.InUse {
				delete(as.Pools, pk)
			}
		}
	}

	return
}

// Creates a new addressPool object.
func (as *addressSpace) newAddressPool(ifName string, priority int, subnet *net.IPNet) (*addressPool, error) {
	id := subnet.String()

	pool, ok := as.Pools[id]
	if ok {
		return pool, errAddressPoolExists
	}

	v6 := (subnet.IP.To4() == nil)

	pool = &addressPool{
		as:        as,
		Id:        id,
		IfName:    ifName,
		Subnet:    *subnet,
		Addresses: make(map[string]*addressRecord),
		IsIPv6:    v6,
		Priority:  priority,
		epoch:     as.epoch,
	}

	as.Pools[id] = pool

	return pool, nil
}

// Returns the address pool with the given pool ID.
func (as *addressSpace) getAddressPool(poolId string) (*addressPool, error) {
	ap := as.Pools[poolId]
	if ap == nil {
		return nil, errInvalidPoolId
	}

	return ap, nil
}

// Requests a new address pool from the address space.
func (as *addressSpace) requestPool(poolId string, subPoolId string, options map[string]string, v6 bool) (*addressPool, error) {
	var ap *addressPool

	if poolId != "" {
		// Return the specific address pool requested.
		ap = as.Pools[poolId]
		if ap == nil {
			return nil, errAddressPoolNotFound
		}

		// Fail if requested pool is already in use.
		if ap.InUse {
			return nil, errAddressPoolInUse
		}
	} else {
		// Return any available address pool.
		highestPriority := -1

		for _, pool := range as.Pools {
			// Skip if pool is already in use.
			if pool.InUse {
				continue
			}

			// Pick a pool from the same address family.
			if pool.IsIPv6 != v6 {
				continue
			}

			// Pick the pool with the highest priority.
			if pool.Priority > highestPriority {
				highestPriority = pool.Priority
				ap = pool
			}
		}

		if ap == nil {
			return nil, errNoAvailableAddressPools
		}
	}

	ap.InUse = true

	return ap, nil
}

// Releases a previously requested address pool back to its address space.
func (as *addressSpace) releasePool(poolId string) error {
	ap, ok := as.Pools[poolId]
	if !ok {
		return errAddressPoolNotFound
	}

	if !ap.InUse {
		return errAddressPoolNotInUse
	}

	ap.InUse = false

	// Delete address pool if it is no longer available.
	if ap.epoch < as.epoch {
		delete(as.Pools, poolId)
	}

	return nil
}

//
// AddressPool
//

// Creates a new addressRecord object.
func (ap *addressPool) newAddressRecord(addr *net.IP) (*addressRecord, error) {
	id := addr.String()

	if !ap.Subnet.Contains(*addr) {
		return nil, errInvalidAddress
	}

	ar, ok := ap.Addresses[id]
	if ok {
		return ar, errAddressExists
	}

	ar = &addressRecord{
		Addr:  *addr,
		epoch: ap.epoch,
	}

	ap.Addresses[id] = ar

	return ar, nil
}

// Requests a new address from the address pool.
func (ap *addressPool) requestAddress(address string, options map[string]string) (string, error) {
	var ar *addressRecord

	if address != "" {
		// Return the specific address requested.
		ar = ap.Addresses[address]
		if ar == nil {
			return "", errAddressNotFound
		}
		if ar.InUse {
			return "", errAddressInUse
		}
	} else {
		// Return any available address.
		for _, ar = range ap.Addresses {
			if !ar.InUse {
				break
			}
		}

		if ar == nil {
			return "", errNoAvailableAddresses
		}
	}

	ar.InUse = true

	// Return address in CIDR notation.
	addr := net.IPNet{
		IP:   ar.Addr,
		Mask: ap.Subnet.Mask,
	}

	return addr.String(), nil
}

// Releases a previously requested address back to its address pool.
func (ap *addressPool) releaseAddress(address string) error {
	ar := ap.Addresses[address]
	if ar == nil {
		return errAddressNotFound
	}
	if !ar.InUse {
		return errAddressNotInUse
	}

	ar.InUse = false

	// Delete address record if it is no longer available.
	if ar.epoch < ap.as.epoch {
		delete(ap.Addresses, address)
	}

	return nil
}
<|endoftext|>


<commit_before>package blockring

import (
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/hexablock/blockring/store"
	"github.com/hexablock/blockring/structs"
	"github.com/hexablock/blockring/utils"
	chord "github.com/ipkg/go-chord"
	"google.golang.org/grpc"
)

var errNoPeersFound = errors.New("no peers found")

// ChordRing contains
type ChordRing struct {
	conf      *chord.Config
	trans     *chord.GRPCTransport
	ring      *chord.Ring
	peerStore store.PeerStore
}

// NewChordRing instantiates new ChordRing struct joining or creating a ring based on the configuration.
func NewChordRing(conf *Config, peerStore store.PeerStore, gserver *grpc.Server) (*ChordRing, error) {

	var (
		trans = chord.NewGRPCTransport(gserver, conf.Timeouts.RPC, conf.Timeouts.Idle)
		ring  *ChordRing
		err   error
	)

	if conf.RetryJoin {
		ring, err = retryJoinRing(conf, peerStore, trans)
	} else {
		ring, err = joinOrBootstrap(conf, peerStore, trans)
	}

	if err == nil {
		if dlg, ok := conf.Chord.Delegate.(*ChordDelegate); ok {
			dlg.RegisterRing(ring)
		} else {
			err = errors.New("unsupported chord delegate")
		}
	}

	return ring, err
}

// Vnodes returns all vnodes for a given host.  If host is empty string, local vnodes are returned.
func (cr *ChordRing) Vnodes(host string) ([]*chord.Vnode, error) {
	if host == "" {
		return cr.trans.ListVnodes(cr.conf.Hostname)
	}
	return cr.trans.ListVnodes(host)
}

// LookupKey returns the key hash, pred. vnode, n succesor vnodes
func (cr *ChordRing) LookupKey(key []byte, n int) ([]byte, *chord.Vnode, []*chord.Vnode, error) {
	return cr.ring.Lookup(n, key)
}

// LookupHash returns the pred. vnode, n succesor vnodes
func (cr *ChordRing) LookupHash(hash []byte, n int) (*chord.Vnode, []*chord.Vnode, error) {
	return cr.ring.LookupHash(n, hash)
}

// LocateReplicatedKey returns vnodes where a key and replicas are located.
func (cr *ChordRing) LocateReplicatedKey(key []byte, n int) ([]*structs.Location, error) {
	hashes := utils.ReplicatedKeyHashes(key, n)
	out := make([]*structs.Location, n)

	for i, h := range hashes {
		_, vs, err := cr.ring.LookupHash(1, h)
		if err != nil {
			return nil, err
		}
		out[i] = &structs.Location{Id: h, Vnode: vs[0]}
	}
	return out, nil
}

// LocateReplicatedHash returns vnodes where a key and replicas are located.
func (cr *ChordRing) LocateReplicatedHash(hash []byte, n int) ([]*structs.Location, error) {
	hashes := utils.ReplicaHashes(hash, n)
	out := make([]*structs.Location, n)

	for i, h := range hashes {
		_, vs, err := cr.ring.LookupHash(1, h)
		if err != nil {
			return nil, err
		}
		out[i] = &structs.Location{Id: h, Vnode: vs[0]}
	}
	return out, nil
}

// Hostname returns the hostname of the node per the config.
func (cr *ChordRing) Hostname() string {
	return cr.conf.Hostname
}

// NumSuccessors returns the num of succesors per the config.
func (cr *ChordRing) NumSuccessors() int {
	return cr.conf.NumSuccessors
}

func joinOrBootstrap(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (*ChordRing, error) {
	peers := peerStore.Peers()
	if len(peers) > 0 {
		// retry join
		return retryJoinRing(conf, peerStore, trans)
	}

	if len(conf.Peers) > 0 {

		if conf.RetryJoin {
			return retryJoinRing(conf, peerStore, trans)
		}

		// join
		_, ring, err := joinRing(conf, peerStore, trans)
		if err == nil {
			return ring, nil
		}
	}

	// create
	log.Printf("Starting mode=bootstrap hostname=%s", conf.Chord.Hostname)
	cring, err := chord.Create(conf.Chord, trans)
	if err == nil {
		return &ChordRing{ring: cring, conf: conf.Chord, trans: trans, peerStore: peerStore}, nil
	}
	return nil, err
}

// try joining each peer one by one returning the peer and ring on the first successful join
func joinRing(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (string, *ChordRing, error) {

	peers := append(peerStore.Peers(), conf.Peers...)
	peers = dedup(peers)

	for _, peer := range peers {
		log.Printf("Trying peer=%s", peer)
		ring, err := chord.Join(conf.Chord, trans, peer)
		if err == nil {
			cr := &ChordRing{ring: ring, conf: conf.Chord, trans: trans, peerStore: peerStore}
			cr.peerStore.AddPeer(peer)
			return peer, cr, nil
		}

		log.Printf("Failed to connect peer=%s msg='%v'", peer, err)
		peerStore.RemovePeer(peer)

		<-time.After(1250 * time.Millisecond)
	}

	return "", nil, fmt.Errorf("all peers exhausted")
}

// RetryJoinRing implements exponential backoff rejoin.
func retryJoinRing(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (*ChordRing, error) {

	retryInSec := 2
	tries := 0

	for {
		tries++
		if tries == 3 {
			tries = 0
			retryInSec *= retryInSec
		}
		// try each set of peers
		_, ring, err := joinRing(conf, peerStore, trans)
		if err == nil {
			return ring, nil
		}
		log.Printf("Failed to connect msg='%v'", err)
		log.Printf("Trying in %d secs ...", retryInSec)
		<-time.After(time.Duration(retryInSec) * time.Second)

	}

}

// JoinRingOrBootstrap joins or bootstraps based on config.
/*func joinRingOrBootstrap(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (*ChordRing, error) {
	//if len(conf.Peers) > 0 {
	_, ring, err := joinRing(conf, peerStore, trans)
	if err == nil {
		return ring, nil
	}

	//}

	// TODO: remove failed peers from peer store

	log.Printf("Starting mode=bootstrap hostname=%s", conf.Chord.Hostname)
	cring, err := chord.Create(conf.Chord, trans)
	if err == nil {
		return &ChordRing{ring: cring, conf: conf.Chord, trans: trans, peerStore: peerStore}, nil
	}
	return nil, err
}*/

func dedup(list []string) []string {
	m := map[string]int{}
	for i, l := range list {
		m[l] = i
	}

	o := make([]string, len(m))
	i := 0
	for k := range m {
		o[i] = k
		i++
	}
	return o
}
<commit_msg>Re-logic node startup<commit_after>package blockring

import (
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/hexablock/blockring/store"
	"github.com/hexablock/blockring/structs"
	"github.com/hexablock/blockring/utils"
	chord "github.com/ipkg/go-chord"
	"google.golang.org/grpc"
)

var errNoPeersFound = errors.New("no peers found")

// ChordRing contains
type ChordRing struct {
	conf      *chord.Config
	trans     *chord.GRPCTransport
	ring      *chord.Ring
	peerStore store.PeerStore
}

// NewChordRing instantiates new ChordRing struct joining or creating a ring based on the configuration.
func NewChordRing(conf *Config, peerStore store.PeerStore, gserver *grpc.Server) (*ChordRing, error) {

	var (
		trans = chord.NewGRPCTransport(gserver, conf.Timeouts.RPC, conf.Timeouts.Idle)
		ring  *ChordRing
		err   error
	)

	if conf.RetryJoin {
		ring, err = retryJoinRing(conf, peerStore, trans)
	} else {
		ring, err = joinOrBootstrap(conf, peerStore, trans)
	}

	if err == nil {
		if dlg, ok := conf.Chord.Delegate.(*ChordDelegate); ok {
			dlg.RegisterRing(ring)
		} else {
			err = errors.New("unsupported chord delegate")
		}
	}

	return ring, err
}

// Vnodes returns all vnodes for a given host.  If host is empty string, local vnodes are returned.
func (cr *ChordRing) Vnodes(host string) ([]*chord.Vnode, error) {
	if host == "" {
		return cr.trans.ListVnodes(cr.conf.Hostname)
	}
	return cr.trans.ListVnodes(host)
}

// LookupKey returns the key hash, pred. vnode, n succesor vnodes
func (cr *ChordRing) LookupKey(key []byte, n int) ([]byte, *chord.Vnode, []*chord.Vnode, error) {
	return cr.ring.Lookup(n, key)
}

// LookupHash returns the pred. vnode, n succesor vnodes
func (cr *ChordRing) LookupHash(hash []byte, n int) (*chord.Vnode, []*chord.Vnode, error) {
	return cr.ring.LookupHash(n, hash)
}

// LocateReplicatedKey returns vnodes where a key and replicas are located.
func (cr *ChordRing) LocateReplicatedKey(key []byte, n int) ([]*structs.Location, error) {
	hashes := utils.ReplicatedKeyHashes(key, n)
	out := make([]*structs.Location, n)

	for i, h := range hashes {
		_, vs, err := cr.ring.LookupHash(1, h)
		if err != nil {
			return nil, err
		}
		out[i] = &structs.Location{Id: h, Vnode: vs[0]}
	}
	return out, nil
}

// LocateReplicatedHash returns vnodes where a key and replicas are located.
func (cr *ChordRing) LocateReplicatedHash(hash []byte, n int) ([]*structs.Location, error) {
	hashes := utils.ReplicaHashes(hash, n)
	out := make([]*structs.Location, n)

	for i, h := range hashes {
		_, vs, err := cr.ring.LookupHash(1, h)
		if err != nil {
			return nil, err
		}
		out[i] = &structs.Location{Id: h, Vnode: vs[0]}
	}
	return out, nil
}

// Hostname returns the hostname of the node per the config.
func (cr *ChordRing) Hostname() string {
	return cr.conf.Hostname
}

// NumSuccessors returns the num of succesors per the config.
func (cr *ChordRing) NumSuccessors() int {
	return cr.conf.NumSuccessors
}

// join ring if peers provided.
func joinOrBootstrap(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (*ChordRing, error) {

	if len(conf.Peers) > 0 {

		if conf.RetryJoin {
			return retryJoinRing(conf, peerStore, trans)
		}

		// join
		_, ring, err := joinRing(conf, peerStore, trans)
		if err == nil {
			return ring, nil
		}
	}

	// if we have known peers and we have not left the ring then simply start the ring as other members
	// will assimilate.  not doing so at times causes a panic.
	if len(peerStore.Peers()) > 0 {
		log.Printf("Starting hostname=%s", conf.Chord.Hostname)
	} else {
		log.Printf("Starting mode=bootstrap hostname=%s", conf.Chord.Hostname)
	}

	// create
	cring, err := chord.Create(conf.Chord, trans)
	if err == nil {
		return &ChordRing{ring: cring, conf: conf.Chord, trans: trans, peerStore: peerStore}, nil
	}
	return nil, err
}

// try joining each peer one by one returning the peer and ring on the first successful join
func joinRing(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (string, *ChordRing, error) {
	knownPeers := peerStore.Peers()
	peers := dedup(append(conf.Peers, knownPeers...))

	for _, peer := range peers {
		log.Printf("Trying peer=%s", peer)
		ring, err := chord.Join(conf.Chord, trans, peer)
		if err == nil {
			cr := &ChordRing{ring: ring, conf: conf.Chord, trans: trans, peerStore: peerStore}
			cr.peerStore.AddPeer(peer)
			return peer, cr, nil
		}

		log.Printf("Failed to connect peer=%s msg='%v'", peer, err)
		peerStore.RemovePeer(peer)

		<-time.After(1250 * time.Millisecond)
	}

	return "", nil, fmt.Errorf("all peers exhausted")
}

// RetryJoinRing implements exponential backoff rejoin.
func retryJoinRing(conf *Config, peerStore store.PeerStore, trans *chord.GRPCTransport) (*ChordRing, error) {

	retryInSec := 2
	tries := 0

	for {
		tries++
		if tries == 3 {
			tries = 0
			retryInSec *= retryInSec
		}
		// try each set of peers
		_, ring, err := joinRing(conf, peerStore, trans)
		if err == nil {
			return ring, nil
		}
		log.Printf("Failed to connect msg='%v'", err)
		log.Printf("Trying in %d secs ...", retryInSec)
		<-time.After(time.Duration(retryInSec) * time.Second)

	}

}

func dedup(list []string) []string {
	m := map[string]int{}
	for i, l := range list {
		m[l] = i
	}

	o := make([]string, len(m))
	i := 0
	for k := range m {
		o[i] = k
		i++
	}
	return o
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"html/template"
	"net/http"
	"os"
	"roosevelt"
	"sir"
	"winston"
)

type tv struct {
	Location string
	GramsLen int
}

type stv struct {
	Result roosevelt.QueryResult
	Score  int
}

func AddHandler(w http.ResponseWriter, r *http.Request) {
	go roosevelt.Add(r.FormValue("website"))
	http.Redirect(w, r, "/", http.StatusFound)
}

func parseTemplate(filenames ...string) *template.Template {
	t := template.New("layout")
	t.Delims("//", "//")

	t, err := t.ParseFiles(filenames...)
	sir.CheckError(err)

	return t
}

func SearchHandler(w http.ResponseWriter, r *http.Request) {
	stvs := make([]stv, 0)

	for index, location := range roosevelt.Query(r.FormValue("query")) {
		stvs = append(stvs, stv{location, index})
	}

	templatePool["search"].Execute(w, stvs)
}

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	tvs := make([]tv, 0)
	tvs = append(tvs, tv{"Index", roosevelt.IndexDataLen()})

	for i := 0; i < len(winston.TheDocuments); i++ {
		tvs = append(tvs, tv{winston.TheDocuments[i].Location, len(winston.TheDocuments[i].Grams)})
	}

	templatePool["index"].Execute(w, tvs)
}

type Response map[string]interface{}

func (r Response) String() (s string) {
	b, err := json.Marshal(r)

	if err != nil {
		s = ""
		return
	}

	s = string(b)
	return
}

func TestHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprint(w, Response{"success": true, "message": "Hello!"})
	return
}

type templateCache map[string]*template.Template

var templatePool templateCache

func initTemplatePool() {
	templatePool = make(templateCache)

	templatePool["index"] = parseTemplate("templates/layout.html", "templates/index.html")
	templatePool["search"] = parseTemplate("templates/layout.html", "templates/search.html")
}

func main() {
	initTemplatePool()

	wd, err := os.Getwd()
	sir.CheckError(err)

	http.HandleFunc("/", IndexHandler)
	http.HandleFunc("/add", AddHandler)
	http.HandleFunc("/test", TestHandler)
	http.HandleFunc("/search", SearchHandler)

	http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(wd+`/public`))))

	err = http.ListenAndServe(":9090", nil)
	sir.CheckError(err)
}
<commit_msg>adding nimitz<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"nimitz"
	"os"
	"roosevelt"
	"sir"
	"winston"
)

type tv struct {
	Location string
	GramsLen int
}

type stv struct {
	Result roosevelt.QueryResult
	Score  int
}

func AddHandler(w http.ResponseWriter, r *http.Request) {
	go roosevelt.Add(r.FormValue("website"))
	http.Redirect(w, r, "/", http.StatusFound)
}

func SearchHandler(w http.ResponseWriter, r *http.Request) {
	stvs := make([]stv, 0)

	for index, location := range roosevelt.Query(r.FormValue("query")) {
		stvs = append(stvs, stv{location, index})
	}

	pool.Pools["search"].Execute(w, stvs)
}

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	tvs := make([]tv, 0)
	tvs = append(tvs, tv{"Index", roosevelt.IndexDataLen()})

	for i := 0; i < len(winston.TheDocuments); i++ {
		tvs = append(tvs, tv{winston.TheDocuments[i].Location, len(winston.TheDocuments[i].Grams)})
	}

	pool.Pools["index"].Execute(w, tvs)
}

type Response map[string]interface{}

func (r Response) String() (s string) {
	b, err := json.Marshal(r)

	if err != nil {
		s = ""
		return
	}

	s = string(b)
	return
}

func TestHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprint(w, Response{"success": true, "message": "Hello!"})
	return
}

var pool nimitz.Pool

func init() {
	pool.Fill("index", "templates/layout.html", "templates/index.html")
	pool.Fill("search", "templates/layout.html", "templates/search.html")
}

func main() {
	wd, err := os.Getwd()
	sir.CheckError(err)

	http.HandleFunc("/", IndexHandler)
	http.HandleFunc("/add", AddHandler)
	http.HandleFunc("/test", TestHandler)
	http.HandleFunc("/search", SearchHandler)

	http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(wd+`/public`))))

	err = http.ListenAndServe(":9090", nil)
	sir.CheckError(err)
}
<|endoftext|>


<commit_before>// This file was auto-generated by the veyron vdl tool.
// Source: config.vdl

// Package vdl describes the configuration for the vdl tool.
package vdl

import (
	// The non-user imports are prefixed with "__" to prevent collisions.
	__vdl "veyron.io/veyron/veyron2/vdl"
)

// Config specifies the configuration for the vdl tool.  This is typically
// represented in optional "vdl.config" files in each vdl source package.  Each
// "vdl.config" file implicitly imports this package.  E.g. you may refer to
// vdl.Go in the "vdl.config" file without explicitly importing "vdl".
type Config struct {
	// GenLanguages restricts the set of code generation languages.  If the set is
	// empty, all supported languages are allowed to be generated.
	GenLanguages map[GenLanguage]struct{}
	// Language-specific configurations.
	Go         GoConfig
	Java       JavaConfig
	Javascript JavascriptConfig
}

func (Config) __VDLReflect(struct {
	Name string "veyron.io/veyron/veyron2/vdl/vdlroot/src/vdl.Config"
}) {
}

// GenLanguage enumerates the known code generation languages.
type GenLanguage int

const (
	GenLanguageGo GenLanguage = iota
	GenLanguageJava
	GenLanguageJavascript
)

// GenLanguageAll holds all labels for GenLanguage.
var GenLanguageAll = []GenLanguage{GenLanguageGo, GenLanguageJava, GenLanguageJavascript}

// GenLanguageFromString creates a GenLanguage from a string label.
// Returns true iff the label is valid.
func GenLanguageFromString(label string) (x GenLanguage, ok bool) {
	ok = x.Assign(label)
	return
}

// Assign assigns label to x.
// Returns true iff the label is valid.
func (x *GenLanguage) Assign(label string) bool {
	switch label {
	case "Go":
		*x = GenLanguageGo
		return true
	case "Java":
		*x = GenLanguageJava
		return true
	case "Javascript":
		*x = GenLanguageJavascript
		return true
	}
	*x = -1
	return false
}

// String returns the string label of x.
func (x GenLanguage) String() string {
	switch x {
	case GenLanguageGo:
		return "Go"
	case GenLanguageJava:
		return "Java"
	case GenLanguageJavascript:
		return "Javascript"
	}
	return ""
}

func (GenLanguage) __VDLReflect(struct {
	Name string "veyron.io/veyron/veyron2/vdl/vdlroot/src/vdl.GenLanguage"
	Enum struct{ Go, Java, Javascript string }
}) {
}

// GoConfig specifies go specific configuration.
type GoConfig struct {
	// NoFmt disables gofmt formatting on the generated source.
	NoFmt bool
}

func (GoConfig) __VDLReflect(struct {
	Name string "veyron.io/veyron/veyron2/vdl/vdlroot/src/vdl.GoConfig"
}) {
}

// JavaConfig specifies java specific configuration.
type JavaConfig struct {
}

func (JavaConfig) __VDLReflect(struct {
	Name string "veyron.io/veyron/veyron2/vdl/vdlroot/src/vdl.JavaConfig"
}) {
}

// JavascriptConfig specifies javascript specific configuration.
type JavascriptConfig struct {
}

func (JavascriptConfig) __VDLReflect(struct {
	Name string "veyron.io/veyron/veyron2/vdl/vdlroot/src/vdl.JavascriptConfig"
}) {
}

func init() {
	__vdl.Register(Config{})
	__vdl.Register(GenLanguageGo)
	__vdl.Register(GoConfig{})
	__vdl.Register(JavaConfig{})
	__vdl.Register(JavascriptConfig{})
}
<commit_msg>veyron2/vdl: Re-generate vdlroot dirs.<commit_after>// This file was auto-generated by the veyron vdl tool.
// Source: config.vdl

// Package vdl describes the configuration for the vdl tool.
package vdl

import (
	// The non-user imports are prefixed with "__" to prevent collisions.
	__vdl "veyron.io/veyron/veyron2/vdl"
)

// Config specifies the configuration for the vdl tool.  This is typically
// represented in optional "vdl.config" files in each vdl source package.  Each
// "vdl.config" file implicitly imports this package.  E.g. you may refer to
// vdl.Go in the "vdl.config" file without explicitly importing "vdl".
type Config struct {
	// GenLanguages restricts the set of code generation languages.  If the set is
	// empty, all supported languages are allowed to be generated.
	GenLanguages map[GenLanguage]struct{}
	// Language-specific configurations.
	Go         GoConfig
	Java       JavaConfig
	Javascript JavascriptConfig
}

func (Config) __VDLReflect(struct {
	Name string "vdl.Config"
}) {
}

// GenLanguage enumerates the known code generation languages.
type GenLanguage int

const (
	GenLanguageGo GenLanguage = iota
	GenLanguageJava
	GenLanguageJavascript
)

// GenLanguageAll holds all labels for GenLanguage.
var GenLanguageAll = []GenLanguage{GenLanguageGo, GenLanguageJava, GenLanguageJavascript}

// GenLanguageFromString creates a GenLanguage from a string label.
// Returns true iff the label is valid.
func GenLanguageFromString(label string) (x GenLanguage, ok bool) {
	ok = x.Assign(label)
	return
}

// Assign assigns label to x.
// Returns true iff the label is valid.
func (x *GenLanguage) Assign(label string) bool {
	switch label {
	case "Go":
		*x = GenLanguageGo
		return true
	case "Java":
		*x = GenLanguageJava
		return true
	case "Javascript":
		*x = GenLanguageJavascript
		return true
	}
	*x = -1
	return false
}

// String returns the string label of x.
func (x GenLanguage) String() string {
	switch x {
	case GenLanguageGo:
		return "Go"
	case GenLanguageJava:
		return "Java"
	case GenLanguageJavascript:
		return "Javascript"
	}
	return ""
}

func (GenLanguage) __VDLReflect(struct {
	Name string "vdl.GenLanguage"
	Enum struct{ Go, Java, Javascript string }
}) {
}

// GoConfig specifies go specific configuration.
type GoConfig struct {
	// NoFmt disables gofmt formatting on the generated source.
	NoFmt bool
}

func (GoConfig) __VDLReflect(struct {
	Name string "vdl.GoConfig"
}) {
}

// JavaConfig specifies java specific configuration.
type JavaConfig struct {
}

func (JavaConfig) __VDLReflect(struct {
	Name string "vdl.JavaConfig"
}) {
}

// JavascriptConfig specifies javascript specific configuration.
type JavascriptConfig struct {
}

func (JavascriptConfig) __VDLReflect(struct {
	Name string "vdl.JavascriptConfig"
}) {
}

func init() {
	__vdl.Register(Config{})
	__vdl.Register(GenLanguageGo)
	__vdl.Register(GoConfig{})
	__vdl.Register(JavaConfig{})
	__vdl.Register(JavascriptConfig{})
}
<|endoftext|>


<commit_before>package video

import "github.com/32bitkid/mpeg/util"

type PictureCodingExtension struct {
	f_code                     [2][2]uint32
	intra_dc_precision         uint32 // 2 uimsbf
	picture_structure          uint32 // 2 uimsbf
	top_field_first            bool   // 1 uimsbf
	frame_pred_frame_dct       uint32 // 1 uimsbf
	concealment_motion_vectors bool   // 1 uimsbf
	q_scale_type               bool   // 1 uimsbf
	intra_vlc_format           bool   // 1 uimsbf
	alternate_scan             bool   // 1 uimsbf
	repeat_first_field         bool   // 1 uimsbf
	chroma_420_type            bool   // 1 uimsbf
	progressive_frame          bool   // 1 uimsbf
	composite_display_flag     bool   // 1 uimsbf

	v_axis            bool   // 1 uimsbf
	field_sequence    uint32 // 3 uimsbf
	sub_carrier       bool   // 1 uimsbf
	burst_amplitude   uint32 // 7 uimsbf
	sub_carrier_phase uint32 // 8 uimsbf
}

func picture_coding_extension(br util.BitReader32) (*PictureCodingExtension, error) {

	err := start_code_check(br, ExtensionStartCode)
	if err != nil {
		return nil, err
	}

	err = extension_code_check(br, PictureCodingExtensionID)
	if err != nil {
		return nil, err
	}

	pce := PictureCodingExtension{}

	pce.f_code[0][0], err = br.Read32(4)
	if err != nil {
		return nil, err
	}
	pce.f_code[0][1], err = br.Read32(4)
	if err != nil {
		return nil, err
	}
	pce.f_code[1][0], err = br.Read32(4)
	if err != nil {
		return nil, err
	}
	pce.f_code[1][1], err = br.Read32(4)
	if err != nil {
		return nil, err
	}

	pce.intra_dc_precision, err = br.Read32(2)
	if err != nil {
		return nil, err
	}

	pce.picture_structure, err = br.Read32(2)
	if err != nil {
		return nil, err
	}

	pce.top_field_first, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.frame_pred_frame_dct, err = br.Read32(1)
	if err != nil {
		return nil, err
	}

	pce.concealment_motion_vectors, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.q_scale_type, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.intra_vlc_format, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.alternate_scan, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.repeat_first_field, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.chroma_420_type, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.progressive_frame, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.composite_display_flag, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	if pce.composite_display_flag {
		pce.v_axis, err = br.ReadBit()
		if err != nil {
			return nil, err
		}
		pce.field_sequence, err = br.Read32(3)
		if err != nil {
			return nil, err
		}
		pce.sub_carrier, err = br.ReadBit()
		if err != nil {
			return nil, err
		}
		pce.burst_amplitude, err = br.Read32(7)
		if err != nil {
			return nil, err
		}
		pce.sub_carrier_phase, err = br.Read32(8)
		if err != nil {
			return nil, err
		}
	}

	return &pce, next_start_code(br)
}
<commit_msg>PictureStructure enum<commit_after>package video

import "github.com/32bitkid/mpeg/util"

const (
	_ uint32 = iota
	PictureStructure_TopField
	PictureStructure_BottomField
	PictureStructure_FramePicture
)

type PictureCodingExtension struct {
	f_code                     [2][2]uint32
	intra_dc_precision         uint32 // 2 uimsbf
	picture_structure          uint32 // 2 uimsbf
	top_field_first            bool   // 1 uimsbf
	frame_pred_frame_dct       uint32 // 1 uimsbf
	concealment_motion_vectors bool   // 1 uimsbf
	q_scale_type               bool   // 1 uimsbf
	intra_vlc_format           bool   // 1 uimsbf
	alternate_scan             bool   // 1 uimsbf
	repeat_first_field         bool   // 1 uimsbf
	chroma_420_type            bool   // 1 uimsbf
	progressive_frame          bool   // 1 uimsbf
	composite_display_flag     bool   // 1 uimsbf

	v_axis            bool   // 1 uimsbf
	field_sequence    uint32 // 3 uimsbf
	sub_carrier       bool   // 1 uimsbf
	burst_amplitude   uint32 // 7 uimsbf
	sub_carrier_phase uint32 // 8 uimsbf
}

func picture_coding_extension(br util.BitReader32) (*PictureCodingExtension, error) {

	err := start_code_check(br, ExtensionStartCode)
	if err != nil {
		return nil, err
	}

	err = extension_code_check(br, PictureCodingExtensionID)
	if err != nil {
		return nil, err
	}

	pce := PictureCodingExtension{}

	pce.f_code[0][0], err = br.Read32(4)
	if err != nil {
		return nil, err
	}
	pce.f_code[0][1], err = br.Read32(4)
	if err != nil {
		return nil, err
	}
	pce.f_code[1][0], err = br.Read32(4)
	if err != nil {
		return nil, err
	}
	pce.f_code[1][1], err = br.Read32(4)
	if err != nil {
		return nil, err
	}

	pce.intra_dc_precision, err = br.Read32(2)
	if err != nil {
		return nil, err
	}

	pce.picture_structure, err = br.Read32(2)
	if err != nil {
		return nil, err
	}

	pce.top_field_first, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.frame_pred_frame_dct, err = br.Read32(1)
	if err != nil {
		return nil, err
	}

	pce.concealment_motion_vectors, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.q_scale_type, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.intra_vlc_format, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.alternate_scan, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.repeat_first_field, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.chroma_420_type, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.progressive_frame, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	pce.composite_display_flag, err = br.ReadBit()
	if err != nil {
		return nil, err
	}

	if pce.composite_display_flag {
		pce.v_axis, err = br.ReadBit()
		if err != nil {
			return nil, err
		}
		pce.field_sequence, err = br.Read32(3)
		if err != nil {
			return nil, err
		}
		pce.sub_carrier, err = br.ReadBit()
		if err != nil {
			return nil, err
		}
		pce.burst_amplitude, err = br.Read32(7)
		if err != nil {
			return nil, err
		}
		pce.sub_carrier_phase, err = br.Read32(8)
		if err != nil {
			return nil, err
		}
	}

	return &pce, next_start_code(br)
}
<|endoftext|>


<commit_before>// Package varint encodes and decodes SQLite4 variable length integers
// which is an encoding of 64-bit unsigned integers into 1-9 bytes
// (varuint).
// See: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki
package varint

// Uint64 decodes the received varint encoded byte slice; returning
// the value and the amount of bytes used.
//
// From: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki
//
// Decode:
//
//    If A0 is between 0 and 240 inclusive, then the result is the value of A0.
//    If A0 is between 241 and 248 inclusive, then the result is 240+256*(A0-241)+A1.
//    If A0 is 249 then the result is 2288+256*A1+A2.
//    If A0 is 250 then the result is A1..A3 as a 3-byte big-endian integer.
//    If A0 is 251 then the result is A1..A4 as a 4-byte big-endian integer.
//    If A0 is 252 then the result is A1..A5 as a 5-byte big-endian integer.
//    If A0 is 253 then the result is A1..A6 as a 6-byte big-endian integer.
//    If A0 is 254 then the result is A1..A7 as a 7-byte big-endian integer.
//    If A0 is 255 then the result is A1..A8 as a 8-byte big-endian integer.
func Uint64(buf []byte) (uint64, int) {
	// check the first byte
	b := buf[0]
	if b <= 0xF0 {
		return uint64(buf[0]), 1
	}
	if b <= 0xF8 {
		return 240 + 256*(uint64(buf[0])-241) + uint64(buf[1]), 2
	}
	if b == 0xF9 {
		return 2288 + 256*uint64(buf[1]) + uint64(buf[2]), 3
	}
	if b == 0xFA {
		return uint64(buf[1])<<16 | uint64(buf[2])<<8 | uint64(buf[3]), 4
	}
	if b == 0xFB {
		return uint64(buf[1])<<24 | uint64(buf[2])<<16 | uint64(buf[3])<<8 | uint64(buf[4]), 5
	}
	if b == 0xFC {
		return uint64(buf[1])<<32 | uint64(buf[2])<<24 | uint64(buf[3])<<16 | uint64(buf[4])<<8 | uint64(buf[5]), 6
	}
	if b == 0xFD {
		return uint64(buf[1])<<40 | uint64(buf[2])<<32 | uint64(buf[3])<<24 | uint64(buf[4])<<16 | uint64(buf[5])<<8 | uint64(buf[6]), 7
	}
	if b == 0xFE {
		return uint64(buf[1])<<48 | uint64(buf[2])<<40 | uint64(buf[3])<<32 | uint64(buf[4])<<24 | uint64(buf[5])<<16 | uint64(buf[6])<<8 | uint64(buf[7]), 8
	}
	return uint64(buf[1])<<56 | uint64(buf[2])<<48 | uint64(buf[3])<<40 | uint64(buf[4])<<32 | uint64(buf[5])<<24 | uint64(buf[6])<<16 | uint64(buf[7])<<8 | uint64(buf[8]), 9
}

// PutUint64 encodes the received uint64 into varint using the minimum
// necessary bytes.  The number of bytes written is returned.
//
// From: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki
//
// Encode:
//
//    Let the input value be V.
//
//    If V<=240 then output a single by A0 equal to V.
//    If V<=2287 then output A0 as (V-240)/256 + 241 and A1 as (V-240)%256.
//    If V<=67823 then output A0 as 249, A1 as (V-2288)/256, and A2 as (V-2288)%256.
//    If V<=16777215 then output A0 as 250 and A1 through A3 as a big-endian 3-byte integer.
//    If V<=4294967295 then output A0 as 251 and A1..A4 as a big-endian 4-byte integer.
//    If V<=1099511627775 then output A0 as 252 and A1..A5 as a big-endian 5-byte integer.
//    If V<=281474976710655 then output A0 as 253 and A1..A6 as a big-endian 6-byte integer.
//    If V<=72057594037927935 then output A0 as 254 and A1..A7 as a big-endian 7-byte integer.
//    Otherwise then output A0 as 255 and A1..A8 as a big-endian 8-byte integer.
func PutUint64(buf []byte, x uint64) int {
	if x < 241 {
		buf[0] = byte(x)
		return 1
	}
	if x < 2288 {
		buf[0] = byte((x-240)/256 + 241)
		buf[1] = byte((x - 240) % 256)
		return 2
	}
	if x < 67824 {
		buf[0] = 0xF9
		buf[1] = byte((x - 2288) / 256)
		buf[2] = byte((x - 2288) % 256)
		return 3
	}
	if x < 1<<24 {
		buf[0] = 0xFA
		buf[1] = byte(x >> 16)
		buf[2] = byte(x >> 8)
		buf[3] = byte(x)
		return 4
	}
	if x < 1<<32 {
		buf[0] = 0xFB
		buf[1] = byte(x >> 24)
		buf[2] = byte(x >> 16)
		buf[3] = byte(x >> 8)
		buf[4] = byte(x)
		return 5
	}
	if x < 1<<40 {
		buf[0] = 0xFC
		buf[1] = byte(x >> 32)
		buf[2] = byte(x >> 24)
		buf[3] = byte(x >> 16)
		buf[4] = byte(x >> 8)
		buf[5] = byte(x)
		return 6
	}
	if x < 1<<48 {
		buf[0] = 0xFD
		buf[1] = byte(x >> 40)
		buf[2] = byte(x >> 32)
		buf[3] = byte(x >> 24)
		buf[4] = byte(x >> 16)
		buf[5] = byte(x >> 8)
		buf[6] = byte(x)
		return 7
	}
	if x < 1<<56 {
		buf[0] = 0xFE
		buf[1] = byte(x >> 48)
		buf[2] = byte(x >> 40)
		buf[3] = byte(x >> 32)
		buf[4] = byte(x >> 24)
		buf[5] = byte(x >> 16)
		buf[6] = byte(x >> 8)
		buf[7] = byte(x)
		return 8
	}
	buf[0] = 0xFF
	buf[1] = byte(x >> 56)
	buf[2] = byte(x >> 48)
	buf[3] = byte(x >> 40)
	buf[4] = byte(x >> 32)
	buf[5] = byte(x >> 24)
	buf[6] = byte(x >> 16)
	buf[7] = byte(x >> 8)
	buf[8] = byte(x)
	return 9
}
<commit_msg>access first byte directly instead of storing in a var<commit_after>// Package varint encodes and decodes SQLite4 variable length integers
// which is an encoding of 64-bit unsigned integers into 1-9 bytes
// (varuint).
// See: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki
package varint

// Uint64 decodes the received varint encoded byte slice; returning
// the value and the amount of bytes used.
//
// From: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki
//
// Decode:
//
//    If A0 is between 0 and 240 inclusive, then the result is the value of A0.
//    If A0 is between 241 and 248 inclusive, then the result is 240+256*(A0-241)+A1.
//    If A0 is 249 then the result is 2288+256*A1+A2.
//    If A0 is 250 then the result is A1..A3 as a 3-byte big-endian integer.
//    If A0 is 251 then the result is A1..A4 as a 4-byte big-endian integer.
//    If A0 is 252 then the result is A1..A5 as a 5-byte big-endian integer.
//    If A0 is 253 then the result is A1..A6 as a 6-byte big-endian integer.
//    If A0 is 254 then the result is A1..A7 as a 7-byte big-endian integer.
//    If A0 is 255 then the result is A1..A8 as a 8-byte big-endian integer.
func Uint64(buf []byte) (uint64, int) {
	// check the first byte
	if buf[0] <= 0xF0 {
		return uint64(buf[0]), 1
	}
	if buf[0] <= 0xF8 {
		return 240 + 256*(uint64(buf[0])-241) + uint64(buf[1]), 2
	}
	if buf[0] == 0xF9 {
		return 2288 + 256*uint64(buf[1]) + uint64(buf[2]), 3
	}
	if buf[0] == 0xFA {
		return uint64(buf[1])<<16 | uint64(buf[2])<<8 | uint64(buf[3]), 4
	}
	if buf[0] == 0xFB {
		return uint64(buf[1])<<24 | uint64(buf[2])<<16 | uint64(buf[3])<<8 | uint64(buf[4]), 5
	}
	if buf[0] == 0xFC {
		return uint64(buf[1])<<32 | uint64(buf[2])<<24 | uint64(buf[3])<<16 | uint64(buf[4])<<8 | uint64(buf[5]), 6
	}
	if buf[0] == 0xFD {
		return uint64(buf[1])<<40 | uint64(buf[2])<<32 | uint64(buf[3])<<24 | uint64(buf[4])<<16 | uint64(buf[5])<<8 | uint64(buf[6]), 7
	}
	if buf[0] == 0xFE {
		return uint64(buf[1])<<48 | uint64(buf[2])<<40 | uint64(buf[3])<<32 | uint64(buf[4])<<24 | uint64(buf[5])<<16 | uint64(buf[6])<<8 | uint64(buf[7]), 8
	}
	return uint64(buf[1])<<56 | uint64(buf[2])<<48 | uint64(buf[3])<<40 | uint64(buf[4])<<32 | uint64(buf[5])<<24 | uint64(buf[6])<<16 | uint64(buf[7])<<8 | uint64(buf[8]), 9
}

// PutUint64 encodes the received uint64 into varint using the minimum
// necessary bytes.  The number of bytes written is returned.
//
// From: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki
//
// Encode:
//
//    Let the input value be V.
//
//    If V<=240 then output a single by A0 equal to V.
//    If V<=2287 then output A0 as (V-240)/256 + 241 and A1 as (V-240)%256.
//    If V<=67823 then output A0 as 249, A1 as (V-2288)/256, and A2 as (V-2288)%256.
//    If V<=16777215 then output A0 as 250 and A1 through A3 as a big-endian 3-byte integer.
//    If V<=4294967295 then output A0 as 251 and A1..A4 as a big-endian 4-byte integer.
//    If V<=1099511627775 then output A0 as 252 and A1..A5 as a big-endian 5-byte integer.
//    If V<=281474976710655 then output A0 as 253 and A1..A6 as a big-endian 6-byte integer.
//    If V<=72057594037927935 then output A0 as 254 and A1..A7 as a big-endian 7-byte integer.
//    Otherwise then output A0 as 255 and A1..A8 as a big-endian 8-byte integer.
func PutUint64(buf []byte, x uint64) int {
	if x < 241 {
		buf[0] = byte(x)
		return 1
	}
	if x < 2288 {
		buf[0] = byte((x-240)/256 + 241)
		buf[1] = byte((x - 240) % 256)
		return 2
	}
	if x < 67824 {
		buf[0] = 0xF9
		buf[1] = byte((x - 2288) / 256)
		buf[2] = byte((x - 2288) % 256)
		return 3
	}
	if x < 1<<24 {
		buf[0] = 0xFA
		buf[1] = byte(x >> 16)
		buf[2] = byte(x >> 8)
		buf[3] = byte(x)
		return 4
	}
	if x < 1<<32 {
		buf[0] = 0xFB
		buf[1] = byte(x >> 24)
		buf[2] = byte(x >> 16)
		buf[3] = byte(x >> 8)
		buf[4] = byte(x)
		return 5
	}
	if x < 1<<40 {
		buf[0] = 0xFC
		buf[1] = byte(x >> 32)
		buf[2] = byte(x >> 24)
		buf[3] = byte(x >> 16)
		buf[4] = byte(x >> 8)
		buf[5] = byte(x)
		return 6
	}
	if x < 1<<48 {
		buf[0] = 0xFD
		buf[1] = byte(x >> 40)
		buf[2] = byte(x >> 32)
		buf[3] = byte(x >> 24)
		buf[4] = byte(x >> 16)
		buf[5] = byte(x >> 8)
		buf[6] = byte(x)
		return 7
	}
	if x < 1<<56 {
		buf[0] = 0xFE
		buf[1] = byte(x >> 48)
		buf[2] = byte(x >> 40)
		buf[3] = byte(x >> 32)
		buf[4] = byte(x >> 24)
		buf[5] = byte(x >> 16)
		buf[6] = byte(x >> 8)
		buf[7] = byte(x)
		return 8
	}
	buf[0] = 0xFF
	buf[1] = byte(x >> 56)
	buf[2] = byte(x >> 48)
	buf[3] = byte(x >> 40)
	buf[4] = byte(x >> 32)
	buf[5] = byte(x >> 24)
	buf[6] = byte(x >> 16)
	buf[7] = byte(x >> 8)
	buf[8] = byte(x)
	return 9
}
<|endoftext|>


<commit_before>package consul

import (
	"fmt"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/hashicorp/errwrap"
	"github.com/hashicorp/terraform/helper/schema"
)

const (
	queryOptNodesAttr = "nodes"

	nodeID              = "id"
	nodeAddress         = "address"
	nodeMetaAttr        = "meta"
	nodeName            = "name"
	nodeTaggedAddresses = "tagged_addresses"

	queryOpts = "query_opts"

	apiTaggedLAN    = "lan"
	apiTaggedWAN    = "wan"
	schemaTaggedLAN = "lan"
	schemaTaggedWAN = "wan"
)

func dataSourceConsulCatalogNodes() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceConsulCatalogNodesRead,
		Schema: map[string]*schema.Schema{
			queryOpts: schemaQueryOpts,
			queryOptNodesAttr: &schema.Schema{
				Computed: true,
				Type:     schema.TypeList,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						nodeID: &schema.Schema{
							Type:         schema.TypeString,
							Computed:     true,
							ValidateFunc: makeValidationFunc(nodeID, []interface{}{validateRegexp(`^[\S]+$`)}),
						},
						nodeName: &schema.Schema{
							Type:         schema.TypeString,
							Computed:     true,
							ValidateFunc: makeValidationFunc(nodeName, []interface{}{validateRegexp(`^[\S]+$`)}),
						},
						nodeAddress: &schema.Schema{
							Type:     schema.TypeString,
							Computed: true,
						},
						nodeMetaAttr: &schema.Schema{
							Type:     schema.TypeMap,
							Computed: true,
						},
						nodeTaggedAddresses: &schema.Schema{
							Type:     schema.TypeMap,
							Computed: true,
							Elem: &schema.Resource{
								Schema: map[string]*schema.Schema{
									schemaTaggedLAN: &schema.Schema{
										Type:     schema.TypeString,
										Computed: true,
									},
									schemaTaggedWAN: &schema.Schema{
										Type:     schema.TypeString,
										Computed: true,
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

func dataSourceConsulCatalogNodesRead(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*consulapi.Client)

	// Parse out data source filters to populate Consul's query options
	queryOpts, err := getQueryOpts(d, client)
	if err != nil {
		return errwrap.Wrapf("unable to get query options for fetching catalog nodes: {{err}}", err)
	}

	nodes, meta, err := client.Catalog().Nodes(queryOpts)
	if err != nil {
		return err
	}

	l := make([]interface{}, 0, len(nodes))

	for _, node := range nodes {
		const defaultNodeAttrs = 4
		m := make(map[string]interface{}, defaultNodeAttrs)
		id := node.ID
		if id == "" {
			id = node.Node
		}

		m[nodeID] = id
		m[nodeName] = node.Node
		m[nodeAddress] = node.Address

		{
			const initNumTaggedAddrs = 2
			taggedAddrs := make(map[string]interface{}, initNumTaggedAddrs)
			if addr, found := node.TaggedAddresses[apiTaggedLAN]; found {
				taggedAddrs[schemaTaggedLAN] = addr
			}
			if addr, found := node.TaggedAddresses[apiTaggedWAN]; found {
				taggedAddrs[schemaTaggedWAN] = addr
			}
			m[nodeTaggedAddresses] = taggedAddrs
		}

		{
			const initNumMetaAddrs = 4
			metaVals := make(map[string]interface{}, initNumMetaAddrs)
			for s, t := range node.Meta {
				metaVals[s] = t
			}
			m[nodeMetaAttr] = metaVals
		}

		l = append(l, m)
	}

	const idKeyFmt = "catalog-nodes-%s"
	d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))

	d.Set("datacenter", queryOpts.Datacenter)
	if err := d.Set(queryOptNodesAttr, l); err != nil {
		return errwrap.Wrapf("Unable to store nodes: {{err}}", err)
	}

	return nil
}
<commit_msg>Commit miss: `s/query_opts/query_options/`<commit_after>package consul

import (
	"fmt"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/hashicorp/errwrap"
	"github.com/hashicorp/terraform/helper/schema"
)

const (
	queryOptNodesAttr = "nodes"

	nodeID              = "id"
	nodeAddress         = "address"
	nodeMetaAttr        = "meta"
	nodeName            = "name"
	nodeTaggedAddresses = "tagged_addresses"

	queryOpts = "query_options"

	apiTaggedLAN    = "lan"
	apiTaggedWAN    = "wan"
	schemaTaggedLAN = "lan"
	schemaTaggedWAN = "wan"
)

func dataSourceConsulCatalogNodes() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceConsulCatalogNodesRead,
		Schema: map[string]*schema.Schema{
			queryOpts: schemaQueryOpts,
			queryOptNodesAttr: &schema.Schema{
				Computed: true,
				Type:     schema.TypeList,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						nodeID: &schema.Schema{
							Type:         schema.TypeString,
							Computed:     true,
							ValidateFunc: makeValidationFunc(nodeID, []interface{}{validateRegexp(`^[\S]+$`)}),
						},
						nodeName: &schema.Schema{
							Type:         schema.TypeString,
							Computed:     true,
							ValidateFunc: makeValidationFunc(nodeName, []interface{}{validateRegexp(`^[\S]+$`)}),
						},
						nodeAddress: &schema.Schema{
							Type:     schema.TypeString,
							Computed: true,
						},
						nodeMetaAttr: &schema.Schema{
							Type:     schema.TypeMap,
							Computed: true,
						},
						nodeTaggedAddresses: &schema.Schema{
							Type:     schema.TypeMap,
							Computed: true,
							Elem: &schema.Resource{
								Schema: map[string]*schema.Schema{
									schemaTaggedLAN: &schema.Schema{
										Type:     schema.TypeString,
										Computed: true,
									},
									schemaTaggedWAN: &schema.Schema{
										Type:     schema.TypeString,
										Computed: true,
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

func dataSourceConsulCatalogNodesRead(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*consulapi.Client)

	// Parse out data source filters to populate Consul's query options
	queryOpts, err := getQueryOpts(d, client)
	if err != nil {
		return errwrap.Wrapf("unable to get query options for fetching catalog nodes: {{err}}", err)
	}

	nodes, meta, err := client.Catalog().Nodes(queryOpts)
	if err != nil {
		return err
	}

	l := make([]interface{}, 0, len(nodes))

	for _, node := range nodes {
		const defaultNodeAttrs = 4
		m := make(map[string]interface{}, defaultNodeAttrs)
		id := node.ID
		if id == "" {
			id = node.Node
		}

		m[nodeID] = id
		m[nodeName] = node.Node
		m[nodeAddress] = node.Address

		{
			const initNumTaggedAddrs = 2
			taggedAddrs := make(map[string]interface{}, initNumTaggedAddrs)
			if addr, found := node.TaggedAddresses[apiTaggedLAN]; found {
				taggedAddrs[schemaTaggedLAN] = addr
			}
			if addr, found := node.TaggedAddresses[apiTaggedWAN]; found {
				taggedAddrs[schemaTaggedWAN] = addr
			}
			m[nodeTaggedAddresses] = taggedAddrs
		}

		{
			const initNumMetaAddrs = 4
			metaVals := make(map[string]interface{}, initNumMetaAddrs)
			for s, t := range node.Meta {
				metaVals[s] = t
			}
			m[nodeMetaAttr] = metaVals
		}

		l = append(l, m)
	}

	const idKeyFmt = "catalog-nodes-%s"
	d.SetId(fmt.Sprintf(idKeyFmt, queryOpts.Datacenter))

	d.Set("datacenter", queryOpts.Datacenter)
	if err := d.Set(queryOptNodesAttr, l); err != nil {
		return errwrap.Wrapf("Unable to store nodes: {{err}}", err)
	}

	return nil
}
<|endoftext|>


<commit_before>package jman

import (
	"bytes"
	"encoding/json"
	"github.com/bmizerany/assert"
	"strconv"
	"testing"
)

func TestSimplejson(t *testing.T) {
	var ok bool
	var err error

	js, err := New([]byte(`{
		"test": {
			"string_array": ["asdf", "ghjk", "zxcv"],
			"string_array_null": ["abc", null, "efg"],
			"array": [1, "2", 3],
			"arraywithsubs": [{"subkeyone": 1},
			{"subkeytwo": 2, "subkeythree": 3}],
			"int": 10,
			"float": 5.150,
			"string": "simplejson",
			"bool": true,
			"sub_obj": {"a": 1}
		}
	}`))

	assert.NotEqual(t, nil, js)
	assert.Equal(t, nil, err)

	_, ok = js.CheckGet("test")
	assert.Equal(t, true, ok)

	_, ok = js.CheckGet("missing_key")
	assert.Equal(t, false, ok)

	aws := js.Get("test").Get("arraywithsubs")
	assert.NotEqual(t, nil, aws)
	var awsval int
	awsval, _ = aws.Get(0).Get("subkeyone").CheckInt()
	assert.Equal(t, 1, awsval)
	awsval, _ = aws.Get(1).Get("subkeytwo").CheckInt()
	assert.Equal(t, 2, awsval)
	awsval, _ = aws.Get(1).Get("subkeythree").CheckInt()
	assert.Equal(t, 3, awsval)

	i, _ := js.Get("test").Get("int").CheckInt()
	assert.Equal(t, 10, i)

	f, _ := js.Get("test").Get("float").CheckFloat64()
	assert.Equal(t, 5.150, f)

	s, _ := js.Get("test").Get("string").CheckString()
	assert.Equal(t, "simplejson", s)

	b, _ := js.Get("test").Get("bool").CheckBool()
	assert.Equal(t, true, b)

	mi := js.Get("test").Get("int").Int()
	assert.Equal(t, 10, mi)

	mi2 := js.Get("test").Get("missing_int").Int(5150)
	assert.Equal(t, 5150, mi2)

	ms := js.Get("test").Get("string").String()
	assert.Equal(t, "simplejson", ms)

	ms2 := js.Get("test").Get("missing_string").String("fyea")
	assert.Equal(t, "fyea", ms2)

	ma2 := js.Get("test").Get("missing_array").Slice([]interface{}{"1", 2, "3"})
	assert.Equal(t, ma2, []interface{}{"1", 2, "3"})

	mm2 := js.Get("test").Get("missing_map").Map(map[string]interface{}{"found": false})
	assert.Equal(t, mm2, map[string]interface{}{"found": false})

	gp, _ := js.Get("test", "string").CheckString()
	assert.Equal(t, "simplejson", gp)

	gp2, _ := js.Get("test", "int").CheckInt()
	assert.Equal(t, 10, gp2)

	gpa, _ := js.Get("test", "string_array", 0).CheckString()
	assert.Equal(t, "asdf", gpa)

	gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").CheckInt()
	assert.Equal(t, 3, gpa2)

	jm, ok := js.Get("test").CheckNodeMap()
	assert.Equal(t, ok, true)
	jmbool, _ := jm["bool"].CheckBool()
	assert.Equal(t, true, jmbool)

	ja, ok := js.Get("test", "string_array").CheckNodeList()
	assert.Equal(t, ok, true)
	jastr, _ := ja[0].CheckString()
	assert.Equal(t, "asdf", jastr)

	assert.Equal(t, js.Get("test").Get("bool").Bool(), true)

	js.Set("float2", 300.0)
	assert.Equal(t, js.Get("float2").Float64(), 300.0)

	js.Set("test2", "setTest")
	assert.Equal(t, "setTest", js.Get("test2").String())

	js.Del("test2")
	assert.NotEqual(t, "setTest", js.Get("test2").String())

	js.Get("test").Get("sub_obj").Set("a", 2)
	assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").Int())

	js.Get("test", "sub_obj").Set("a", 3)
	assert.Equal(t, 3, js.Get("test", "sub_obj", "a").Int())

	jmm := js.Get("missing_map").NodeMap(NodeMap{"js1": js})
	assert.Equal(t, js, jmm["js1"])

	jma := js.Get("missing_array").NodeList(NodeList{js})
	assert.Equal(t, js, jma[0])
}

func TestStdlibInterfaces(t *testing.T) {
	val := new(struct {
		Name   string `json:"name"`
		Params *Node  `json:"params"`
	})
	val2 := new(struct {
		Name   string `json:"name"`
		Params *Node  `json:"params"`
	})

	raw := `{"name":"myobject","params":{"string":"simplejson"}}`

	assert.Equal(t, nil, json.Unmarshal([]byte(raw), val))

	assert.Equal(t, "myobject", val.Name)
	assert.NotEqual(t, nil, val.Params.data)
	s, _ := val.Params.Get("string").CheckString()
	assert.Equal(t, "simplejson", s)

	p, err := json.Marshal(val)
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, json.Unmarshal(p, val2))
	assert.Equal(t, val, val2) // stable
}

func TestSet(t *testing.T) {
	js, err := New([]byte(`{}`))
	assert.Equal(t, nil, err)

	js.Set("baz", "bing")

	s, ok := js.Get("baz").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bing", s)
}

func TestReplace(t *testing.T) {
	js, err := New([]byte(`{}`))
	assert.Equal(t, nil, err)

	err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`))
	assert.Equal(t, nil, err)

	s, ok := js.Get("baz").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bing", s)
}

func TestSetPath(t *testing.T) {
	js, err := New([]byte(`{}`))
	assert.Equal(t, nil, err)

	js.SetPath([]string{"foo", "bar"}, "baz")

	s, ok := js.Get("foo", "bar").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "baz", s)
}

func TestSetPathNoPath(t *testing.T) {
	js, err := New([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`))
	assert.Equal(t, nil, err)

	f := js.Get("some_number").Float64(99.0)
	assert.Equal(t, f, 1.0)

	js.SetPath([]string{}, map[string]interface{}{"foo": "bar"})

	s, ok := js.Get("foo").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bar", s)

	f = js.Get("some_number").Float64(99.0)
	assert.Equal(t, f, 99.0)
}

func TestPathWillAugmentExisting(t *testing.T) {
	js, err := New([]byte(`{"this":{"a":"aa","b":"bb","c":"cc"}}`))
	assert.Equal(t, nil, err)

	js.SetPath([]string{"this", "d"}, "dd")

	cases := []struct {
		path    []interface{}
		outcome string
	}{
		{
			path:    []interface{}{"this", "a"},
			outcome: "aa",
		},
		{
			path:    []interface{}{"this", "b"},
			outcome: "bb",
		},
		{
			path:    []interface{}{"this", "c"},
			outcome: "cc",
		},
		{
			path:    []interface{}{"this", "d"},
			outcome: "dd",
		},
	}

	for _, tc := range cases {
		s, ok := js.Get(tc.path...).CheckString()
		assert.Equal(t, true, ok)
		assert.Equal(t, tc.outcome, s)
	}
}

func TestPathWillOverwriteExisting(t *testing.T) {
	// notice how "a" is 0.1 - but then we'll try to set at path a, foo
	js, err := New([]byte(`{"this":{"a":0.1,"b":"bb","c":"cc"}}`))
	assert.Equal(t, nil, err)

	js.SetPath([]string{"this", "a", "foo"}, "bar")

	s, ok := js.Get("this", "a", "foo").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bar", s)
}

func TestNewFromReader(t *testing.T) {
	buf := bytes.NewBuffer([]byte(`{
		"test": {
			"array": [1, "2", 3],
			"arraywithsubs": [
				{"subkeyone": 1},
				{"subkeytwo": 2, "subkeythree": 3}
			],
			"bignum": 8000000000
		}
	}`))
	js, err := NewFromReader(buf)

	//Standard Test Case
	assert.NotEqual(t, nil, js)
	assert.Equal(t, nil, err)

	arr, _ := js.Get("test").Get("array").CheckSlice()
	assert.NotEqual(t, nil, arr)
	for i, v := range arr {
		var iv int
		switch v.(type) {
		case float64:
			iv = int(v.(float64))
		case string:
			iv, _ = strconv.Atoi(v.(string))
		}
		assert.Equal(t, i+1, iv)
	}

	ma := js.Get("test").Get("array").Slice()
	assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)})

	mm := js.Get("test").Get("arraywithsubs").Get(0).Map()
	assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)})

	assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000))
}

func TestSimplejson2(t *testing.T) {
	js, err := New([]byte(`{
		"test": {
			"array": [1, "2", 3],
			"arraywithsubs": [
				{"subkeyone": 1},
				{"subkeytwo": 2, "subkeythree": 3}
			],
			"bignum": 8000000000
		}
	}`))

	assert.NotEqual(t, nil, js)
	assert.Equal(t, nil, err)

	arr, _ := js.Get("test").Get("array").CheckSlice()
	assert.NotEqual(t, nil, arr)
	for i, v := range arr {
		var iv int
		switch v.(type) {
		case float64:
			iv = int(v.(float64))
		case string:
			iv, _ = strconv.Atoi(v.(string))
		}
		assert.Equal(t, i+1, iv)
	}

	ma := js.Get("test").Get("array").Slice()
	assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)})

	mm := js.Get("test").Get("arraywithsubs").Get(0).Map()
	assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)})

	assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000))
}
<commit_msg>Updated the tests<commit_after>package jman

import (
	"bytes"
	"encoding/json"
	"github.com/bmizerany/assert"
	"strconv"
	"testing"
)

func TestSimplejson(t *testing.T) {
	var ok bool
	var err error

	js, err := New([]byte(`{
		"test": {
			"string_array": ["asdf", "ghjk", "zxcv"],
			"string_array_null": ["abc", null, "efg"],
			"array": [1, "2", 3],
			"arraywithsubs": [{"subkeyone": 1},
			{"subkeytwo": 2, "subkeythree": 3}],
			"int": 10,
			"float": 5.150,
			"string": "simplejson",
			"bool": true,
			"sub_obj": {"a": 1}
		}
	}`))

	assert.NotEqual(t, nil, js)
	assert.Equal(t, nil, err)

	_, ok = js.CheckGet("test")
	assert.Equal(t, true, ok)

	_, ok = js.CheckGet("missing_key")
	assert.Equal(t, false, ok)

	aws := js.Get("test").Get("arraywithsubs")
	assert.NotEqual(t, nil, aws)
	var awsval int
	awsval, _ = aws.Get(0).Get("subkeyone").CheckInt()
	assert.Equal(t, 1, awsval)
	awsval, _ = aws.Get(1).Get("subkeytwo").CheckInt()
	assert.Equal(t, 2, awsval)
	awsval, _ = aws.Get(1).Get("subkeythree").CheckInt()
	assert.Equal(t, 3, awsval)

	i, _ := js.Get("test").Get("int").CheckInt()
	assert.Equal(t, 10, i)

	f, _ := js.Get("test").Get("float").CheckFloat64()
	assert.Equal(t, 5.150, f)

	s, _ := js.Get("test").Get("string").CheckString()
	assert.Equal(t, "simplejson", s)

	b, _ := js.Get("test").Get("bool").CheckBool()
	assert.Equal(t, true, b)

	mi := js.Get("test").Get("int").Int()
	assert.Equal(t, 10, mi)

	mi2 := js.Get("test").Get("missing_int").Int(5150)
	assert.Equal(t, 5150, mi2)

	ms := js.Get("test").Get("string").String()
	assert.Equal(t, "simplejson", ms)

	ms2 := js.Get("test").Get("missing_string").String("fyea")
	assert.Equal(t, "fyea", ms2)

	ma2 := js.Get("test").Get("missing_array").Slice([]interface{}{"1", 2, "3"})
	assert.Equal(t, ma2, []interface{}{"1", 2, "3"})

	mm2 := js.Get("test").Get("missing_map").Map(map[string]interface{}{"found": false})
	assert.Equal(t, mm2, map[string]interface{}{"found": false})

	gp, _ := js.Get("test", "string").CheckString()
	assert.Equal(t, "simplejson", gp)

	gp2, _ := js.Get("test", "int").CheckInt()
	assert.Equal(t, 10, gp2)

	gpa, _ := js.Get("test", "string_array", 0).CheckString()
	assert.Equal(t, "asdf", gpa)

	gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").CheckInt()
	assert.Equal(t, 3, gpa2)

	jm, ok := js.Get("test").CheckNodeMap()
	assert.Equal(t, ok, true)
	jmbool, _ := jm["bool"].CheckBool()
	assert.Equal(t, true, jmbool)

	ja, ok := js.Get("test", "string_array").CheckNodeList()
	assert.Equal(t, ok, true)
	jastr, _ := ja[0].CheckString()
	assert.Equal(t, "asdf", jastr)

	assert.Equal(t, js.Get("test").Get("bool").Bool(), true)

	js.Set("float2", 300.0)
	assert.Equal(t, js.Get("float2").Float64(), 300.0)

	js.Set("test2", "setTest")
	assert.Equal(t, "setTest", js.Get("test2").String())

	js.Del("test2")
	assert.NotEqual(t, "setTest", js.Get("test2").String())

	js.Get("test").Get("sub_obj").Set("a", 2)
	assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").Int())

	js.Get("test", "sub_obj").Set("a", 3)
	assert.Equal(t, 3, js.Get("test", "sub_obj", "a").Int())

	jmm := js.Get("missing_map").NodeMap(NodeMap{"js1": js})
	assert.Equal(t, js, jmm["js1"])

	jma := js.Get("missing_array").NodeList(NodeList{js})
	assert.Equal(t, js, jma[0])
}

func TestStdlibInterfaces(t *testing.T) {
	val := new(struct {
		Name   string `json:"name"`
		Params *Node  `json:"params"`
	})
	val2 := new(struct {
		Name   string `json:"name"`
		Params *Node  `json:"params"`
	})

	raw := `{"name":"myobject","params":{"string":"simplejson"}}`

	assert.Equal(t, nil, json.Unmarshal([]byte(raw), val))

	assert.Equal(t, "myobject", val.Name)
	assert.NotEqual(t, nil, val.Params.data)
	s, _ := val.Params.Get("string").CheckString()
	assert.Equal(t, "simplejson", s)

	p, err := json.Marshal(val)
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, json.Unmarshal(p, val2))
	assert.Equal(t, val, val2) // stable
}

func TestSet(t *testing.T) {
	js, err := New([]byte(`{}`))
	assert.Equal(t, nil, err)

	js.Set("baz", "bing")

	s, ok := js.Get("baz").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bing", s)
}

func TestReplace(t *testing.T) {
	js, err := New([]byte(`{}`))
	assert.Equal(t, nil, err)

	err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`))
	assert.Equal(t, nil, err)

	s, ok := js.Get("baz").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bing", s)
}

func TestSetBranch(t *testing.T) {
	js, err := New([]byte(`{}`))
	assert.Equal(t, nil, err)

	js.SetBranch([]string{"foo", "bar"}, "baz")

	s, ok := js.Get("foo", "bar").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "baz", s)
}

func TestSetBranchNoPath(t *testing.T) {
	js, err := New([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`))
	assert.Equal(t, nil, err)

	f := js.Get("some_number").Float64(99.0)
	assert.Equal(t, f, 1.0)

	js.SetBranch([]string{}, map[string]interface{}{"foo": "bar"})

	s, ok := js.Get("foo").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bar", s)

	f = js.Get("some_number").Float64(99.0)
	assert.Equal(t, f, 99.0)
}

func TestPathWillAugmentExisting(t *testing.T) {
	js, err := New([]byte(`{"this":{"a":"aa","b":"bb","c":"cc"}}`))
	assert.Equal(t, nil, err)

	js.SetBranch([]string{"this", "d"}, "dd")

	cases := []struct {
		path    []interface{}
		outcome string
	}{
		{
			path:    []interface{}{"this", "a"},
			outcome: "aa",
		},
		{
			path:    []interface{}{"this", "b"},
			outcome: "bb",
		},
		{
			path:    []interface{}{"this", "c"},
			outcome: "cc",
		},
		{
			path:    []interface{}{"this", "d"},
			outcome: "dd",
		},
	}

	for _, tc := range cases {
		s, ok := js.Get(tc.path...).CheckString()
		assert.Equal(t, true, ok)
		assert.Equal(t, tc.outcome, s)
	}
}

func TestPathWillOverwriteExisting(t *testing.T) {
	// notice how "a" is 0.1 - but then we'll try to set at path a, foo
	js, err := New([]byte(`{"this":{"a":0.1,"b":"bb","c":"cc"}}`))
	assert.Equal(t, nil, err)

	js.SetBranch([]string{"this", "a", "foo"}, "bar")

	s, ok := js.Get("this", "a", "foo").CheckString()
	assert.Equal(t, true, ok)
	assert.Equal(t, "bar", s)
}

func TestNewFromReader(t *testing.T) {
	buf := bytes.NewBuffer([]byte(`{
		"test": {
			"array": [1, "2", 3],
			"arraywithsubs": [
				{"subkeyone": 1},
				{"subkeytwo": 2, "subkeythree": 3}
			],
			"bignum": 8000000000
		}
	}`))
	js, err := NewFromReader(buf)

	//Standard Test Case
	assert.NotEqual(t, nil, js)
	assert.Equal(t, nil, err)

	arr, _ := js.Get("test").Get("array").CheckSlice()
	assert.NotEqual(t, nil, arr)
	for i, v := range arr {
		var iv int
		switch v.(type) {
		case float64:
			iv = int(v.(float64))
		case string:
			iv, _ = strconv.Atoi(v.(string))
		}
		assert.Equal(t, i+1, iv)
	}

	ma := js.Get("test").Get("array").Slice()
	assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)})

	mm := js.Get("test").Get("arraywithsubs").Get(0).Map()
	assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)})

	assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000))
}

func TestSimplejson2(t *testing.T) {
	js, err := New([]byte(`{
		"test": {
			"array": [1, "2", 3],
			"arraywithsubs": [
				{"subkeyone": 1},
				{"subkeytwo": 2, "subkeythree": 3}
			],
			"bignum": 8000000000
		}
	}`))

	assert.NotEqual(t, nil, js)
	assert.Equal(t, nil, err)

	arr, _ := js.Get("test").Get("array").CheckSlice()
	assert.NotEqual(t, nil, arr)
	for i, v := range arr {
		var iv int
		switch v.(type) {
		case float64:
			iv = int(v.(float64))
		case string:
			iv, _ = strconv.Atoi(v.(string))
		}
		assert.Equal(t, i+1, iv)
	}

	ma := js.Get("test").Get("array").Slice()
	assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)})

	mm := js.Get("test").Get("arraywithsubs").Get(0).Map()
	assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)})

	assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000))
}
<|endoftext|>


<commit_before>package ace

import (
	"testing"
)

func BenchmarkCombineHandlers(b *testing.B) {
	testHandler := func(c *C) {}
	r := Router{}

	r.Use(testHandler,
		testHandler,
		testHandler,
		testHandler,
		testHandler,
		testHandler,
	)

	handlers := []HandlerFunc{
		testHandler,
		testHandler,
		testHandler,
		testHandler,
		testHandler,
		testHandler,
	}

	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		r.combineHandlers(handlers)
	}
}
<commit_msg>remove benchmark file<commit_after><|endoftext|>


<commit_before>// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xml_test

import (
	"encoding/xml"
	"fmt"
	"os"
)

func ExampleMarshalIndent() {
	type Address struct {
		City, State string
	}
	type Person struct {
		XMLName   xml.Name `xml:"person"`
		Id        int      `xml:"id,attr"`
		FirstName string   `xml:"name>first"`
		LastName  string   `xml:"name>last"`
		Age       int      `xml:"age"`
		Height    float32  `xml:"height,omitempty"`
		Married   bool
		Address
		Comment string `xml:",comment"`
	}

	v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
	v.Comment = " Need more details. "
	v.Address = Address{"Hanga Roa", "Easter Island"}

	output, err := xml.MarshalIndent(v, "  ", "    ")
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}

	os.Stdout.Write(output)
	// Output:
	//   <person id="13">
	//       <name>
	//           <first>John</first>
	//           <last>Doe</last>
	//       </name>
	//       <age>42</age>
	//       <Married>false</Married>
	//       <City>Hanga Roa</City>
	//       <State>Easter Island</State>
	//       <!-- Need more details. -->
	//   </person>
}

// This example demonstrates unmarshaling an XML excerpt into a value with
// some preset fields. Note that the Phone field isn't modified and that
// the XML <address> element is ignored. Also, the Groups field is assigned
// considering the element path provided in its tag.
func ExampleUnmarshal() {
	type Email struct {
		Where string `xml:"where,attr"`
		Addr  string
	}
	type Address struct {
		City, State string
	}
	type Result struct {
		XMLName xml.Name `xml:"Person"`
		Name    string   `xml:"FullName"`
		Phone   string
		Email   []Email
		Groups  []string `xml:"Group>Value"`
		Address
	}
	v := Result{Name: "none", Phone: "none"}
	v.Address = Address{"Hanga Roa", "Easter Island"}

	data := `
		<Person>
			<FullName>Grace R. Emlin</FullName>
			<Email where="home">
				<Addr>gre@example.com</Addr>
			</Email>
			<Email where='work'>
				<Addr>gre@work.com</Addr>
			</Email>
			<Group>
				<Value>Friends</Value>
				<Value>Squash</Value>
			</Group>
			<Address>123 Main Street</Address>
		</Person>
	`
	err := xml.Unmarshal([]byte(data), &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	fmt.Printf("XMLName: %#v\n", v.XMLName)
	fmt.Printf("Name: %q\n", v.Name)
	fmt.Printf("Phone: %q\n", v.Phone)
	fmt.Printf("Email: %v\n", v.Email)
	fmt.Printf("Groups: %v\n", v.Groups)
	fmt.Printf("Address: %v\n", v.Address)
	// Output:
	// XMLName: xml.Name{Space:"", Local:"Person"}
	// Name: "Grace R. Emlin"
	// Phone: "none"
	// Email: [{home gre@example.com} {work gre@work.com}]
	// Groups: [Friends Squash]
	// Address: {Hanga Roa Easter Island}
}
<commit_msg>encoding/xml: fix anonymous field Unmarshal example<commit_after>// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xml_test

import (
	"encoding/xml"
	"fmt"
	"os"
)

func ExampleMarshalIndent() {
	type Address struct {
		City, State string
	}
	type Person struct {
		XMLName   xml.Name `xml:"person"`
		Id        int      `xml:"id,attr"`
		FirstName string   `xml:"name>first"`
		LastName  string   `xml:"name>last"`
		Age       int      `xml:"age"`
		Height    float32  `xml:"height,omitempty"`
		Married   bool
		Address
		Comment string `xml:",comment"`
	}

	v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
	v.Comment = " Need more details. "
	v.Address = Address{"Hanga Roa", "Easter Island"}

	output, err := xml.MarshalIndent(v, "  ", "    ")
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}

	os.Stdout.Write(output)
	// Output:
	//   <person id="13">
	//       <name>
	//           <first>John</first>
	//           <last>Doe</last>
	//       </name>
	//       <age>42</age>
	//       <Married>false</Married>
	//       <City>Hanga Roa</City>
	//       <State>Easter Island</State>
	//       <!-- Need more details. -->
	//   </person>
}

// This example demonstrates unmarshaling an XML excerpt into a value with
// some preset fields. Note that the Phone field isn't modified and that
// the XML <Company> element is ignored. Also, the Groups field is assigned
// considering the element path provided in its tag.
func ExampleUnmarshal() {
	type Email struct {
		Where string `xml:"where,attr"`
		Addr  string
	}
	type Address struct {
		City, State string
	}
	type Result struct {
		XMLName xml.Name `xml:"Person"`
		Name    string   `xml:"FullName"`
		Phone   string
		Email   []Email
		Groups  []string `xml:"Group>Value"`
		Address
	}
	v := Result{Name: "none", Phone: "none"}

	data := `
		<Person>
			<FullName>Grace R. Emlin</FullName>
			<Company>Example Inc.</Company>
			<Email where="home">
				<Addr>gre@example.com</Addr>
			</Email>
			<Email where='work'>
				<Addr>gre@work.com</Addr>
			</Email>
			<Group>
				<Value>Friends</Value>
				<Value>Squash</Value>
			</Group>
			<City>Hanga Roa</City>
			<State>Easter Island</State>
		</Person>
	`
	err := xml.Unmarshal([]byte(data), &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	fmt.Printf("XMLName: %#v\n", v.XMLName)
	fmt.Printf("Name: %q\n", v.Name)
	fmt.Printf("Phone: %q\n", v.Phone)
	fmt.Printf("Email: %v\n", v.Email)
	fmt.Printf("Groups: %v\n", v.Groups)
	fmt.Printf("Address: %v\n", v.Address)
	// Output:
	// XMLName: xml.Name{Space:"", Local:"Person"}
	// Name: "Grace R. Emlin"
	// Phone: "none"
	// Email: [{home gre@example.com} {work gre@work.com}]
	// Groups: [Friends Squash]
	// Address: {Hanga Roa Easter Island}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"
	"strings"

	pb "github.com/demisto/pb-go"
)

var (
	appId, userKey, name, out, file, input, cmd *string
	debug                                       *bool
)

func init() {
	appId = flag.String("appId", "", "Application ID as received from pandoranbots.")
	userKey = flag.String("userKey", "", "User key as received from pandoranbots.")
	name = flag.String("name", "", "The bot name to use.")
	out = flag.String("out", "", "Output file. If not specified will write to standard output.")
	file = flag.String("file", "", "Input file for uploads or file name for downloads.")
	input = flag.String("input", "", "Input to talk.")
	cmd = flag.String("cmd", "", "The command to execute. Can be one of the following: list/createBot/deleteBot/listFiles/download/upload/downloadBot/deleteFile/verify/talk")
	debug = flag.Bool("debug", false, "Debug output")
}

func main() {
	flag.Parse()
	c, err := pb.New(pb.SetErrorLog(log.New(os.Stderr, "", log.Lshortfile)), pb.SetCredentials(*appId, *userKey))
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}
	if *debug {
		pb.SetTraceLog(log.New(os.Stderr, "TRACE: ", log.Lshortfile))(c)
	}
	if strings.ToLower(*cmd) != "list" && *name == "" {
		fmt.Println("You must specify the bot name")
		os.Exit(1)
	}
	switch strings.ToLower(*cmd) {
	case "list":
		res, err := c.List()
		if err != nil {
			fmt.Printf("%v\n", err)
			os.Exit(2)
		}
		for _, s := range res {
			fmt.Println(s)
		}
	case "createbot":
		err = c.CreateBot(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("Bot successfully created.")
		}
	case "deletebot":
		err = c.DeleteBot(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("Bot successfully deleted.")
		}
	case "upload":
		if *file == "" {
			fmt.Println("You must specify the file name to upload")
			os.Exit(1)
		}
		err = c.UploadFileFromPath(*name, *file)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("File successfully uploaded.")
		}
	case "download":
		if *out == "" {
			if *file == "" {
				fmt.Println("You must specify the file name to download")
				os.Exit(1)
			}
			err = c.GetFile(*name, *file, os.Stdout)
			if err != nil {
				fmt.Printf("%v\n", err)
			}
		} else {
			err = c.GetFileToPath(*name, *out)
			if err != nil {
				fmt.Printf("%v\n", err)
			} else {
				fmt.Println("File successfully downloaded.")
			}
		}
	case "deletefile":
		if *file == "" {
			fmt.Println("You must specify the file name to delete")
			os.Exit(1)
		}
		err = c.DeleteFile(*name, *file)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("File successfully deleted.")
		}
	case "listfiles":
		res, err := c.ListFiles(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Printf("%v\n", res)
		}
	case "downloadbot":
		if *out == "" {
			err = c.DownloadFiles(*name, os.Stdout)
			if err != nil {
				fmt.Printf("%v\n", err)
			}
		} else {
			err = c.DownloadFilesToPath(*name, *out)
			if err != nil {
				fmt.Printf("%v\n", err)
			} else {
				fmt.Println("Bot files successfully downloaded.")
			}
		}
	case "verify":
		err = c.Verify(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("Bot verified.")
		}
	case "talk":
		// While we are not quiting, let's talk
		if *input == "" {
			var sessionId int
			r := bufio.NewReader(os.Stdin)
			for line, err := r.ReadString('\n'); strings.ToLower(line) != "exit" && err == nil; line, err = r.ReadString('\n') {
				res, err := c.Talk(*name, line, "", sessionId, false)
				if err != nil {
					fmt.Printf("%v\n", err)
					os.Exit(2)
				}
				sessionId = res.SessionId
				for _, s := range res.Responses {
					fmt.Println(s)
				}
			}
		} else {
			res, err := c.Talk(*name, *input, "", 0, false)
			if err != nil {
				fmt.Printf("%v\n", err)
			} else {
				fmt.Printf("%v", res)
			}
		}
	default:
		fmt.Printf("Command [%s] was not recognized\n", *cmd)
	}
}
<commit_msg>Fixed the 'exit' for talk<commit_after>package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"
	"strings"

	pb "github.com/demisto/pb-go"
)

var (
	appId, userKey, name, out, file, input, cmd *string
	debug                                       *bool
)

func init() {
	appId = flag.String("appId", "", "Application ID as received from pandoranbots.")
	userKey = flag.String("userKey", "", "User key as received from pandoranbots.")
	name = flag.String("name", "", "The bot name to use.")
	out = flag.String("out", "", "Output file. If not specified will write to standard output.")
	file = flag.String("file", "", "Input file for uploads or file name for downloads.")
	input = flag.String("input", "", "Input to talk.")
	cmd = flag.String("cmd", "", "The command to execute. Can be one of the following: list/createBot/deleteBot/listFiles/download/upload/downloadBot/deleteFile/verify/talk")
	debug = flag.Bool("debug", false, "Debug output")
}

func main() {
	flag.Parse()
	c, err := pb.New(pb.SetErrorLog(log.New(os.Stderr, "", log.Lshortfile)), pb.SetCredentials(*appId, *userKey))
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}
	if *debug {
		pb.SetTraceLog(log.New(os.Stderr, "TRACE: ", log.Lshortfile))(c)
	}
	if strings.ToLower(*cmd) != "list" && *name == "" {
		fmt.Println("You must specify the bot name")
		os.Exit(1)
	}
	switch strings.ToLower(*cmd) {
	case "list":
		res, err := c.List()
		if err != nil {
			fmt.Printf("%v\n", err)
			os.Exit(2)
		}
		for _, s := range res {
			fmt.Println(s)
		}
	case "createbot":
		err = c.CreateBot(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("Bot successfully created.")
		}
	case "deletebot":
		err = c.DeleteBot(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("Bot successfully deleted.")
		}
	case "upload":
		if *file == "" {
			fmt.Println("You must specify the file name to upload")
			os.Exit(1)
		}
		err = c.UploadFileFromPath(*name, *file)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("File successfully uploaded.")
		}
	case "download":
		if *out == "" {
			if *file == "" {
				fmt.Println("You must specify the file name to download")
				os.Exit(1)
			}
			err = c.GetFile(*name, *file, os.Stdout)
			if err != nil {
				fmt.Printf("%v\n", err)
			}
		} else {
			err = c.GetFileToPath(*name, *out)
			if err != nil {
				fmt.Printf("%v\n", err)
			} else {
				fmt.Println("File successfully downloaded.")
			}
		}
	case "deletefile":
		if *file == "" {
			fmt.Println("You must specify the file name to delete")
			os.Exit(1)
		}
		err = c.DeleteFile(*name, *file)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("File successfully deleted.")
		}
	case "listfiles":
		res, err := c.ListFiles(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Printf("%v\n", res)
		}
	case "downloadbot":
		if *out == "" {
			err = c.DownloadFiles(*name, os.Stdout)
			if err != nil {
				fmt.Printf("%v\n", err)
			}
		} else {
			err = c.DownloadFilesToPath(*name, *out)
			if err != nil {
				fmt.Printf("%v\n", err)
			} else {
				fmt.Println("Bot files successfully downloaded.")
			}
		}
	case "verify":
		err = c.Verify(*name)
		if err != nil {
			fmt.Printf("%v\n", err)
		} else {
			fmt.Println("Bot verified.")
		}
	case "talk":
		// While we are not quiting, let's talk
		if *input == "" {
			var sessionId int
			r := bufio.NewReader(os.Stdin)
			for line, err := r.ReadString('\n'); strings.ToLower(line) != "exit\n" && err == nil; line, err = r.ReadString('\n') {
				res, err := c.Talk(*name, line, "", sessionId, false)
				if err != nil {
					fmt.Printf("%v\n", err)
					os.Exit(2)
				}
				sessionId = res.SessionId
				for _, s := range res.Responses {
					fmt.Println(s)
				}
			}
		} else {
			res, err := c.Talk(*name, *input, "", 0, false)
			if err != nil {
				fmt.Printf("%v\n", err)
			} else {
				fmt.Printf("%v", res)
			}
		}
	default:
		fmt.Printf("Command [%s] was not recognized\n", *cmd)
	}
}
<|endoftext|>


<commit_before>package cache

import (
	"container/list"
    "sync"
  	"time"
)

type Service struct {
	//RecordType string
	Value      string
	TTL        int
	Aliases    string
	Time       time.time

}
type Record struct {
	size int
	list *list.List
}
type RecordCache struct {
	mu sync.Mutex

	list *list.List
	table map[string]*Record
}
type LRUCache struct {
	mu sync.Mutex

	// list & table of *entry objects
	list  *list.List
	table map[string]*RecordCache

	// Our current size, in bytes. Obviously a gross simplification and low-grade
	// approximation.
	size uint64

	// How many bytes we are limiting the cache to.
	capacity uint64
}

//name to type
func NewLRUCache(capacity uint64) *LRUCache {
	return &LRUCache{
		list:     list.New(),
		table:    make(map[string]*RecordCache),
		capacity: capacity,
	}
}

//type to record
func NewRecordCache() *RecordCache{
	return &RecordCache{
		list:	list.New(),
		table:  make(map[string]*Record),
	}

}

<commit_msg>update<commit_after>package cache

import (
	"container/list"
    "sync"
  	"time"
)

type Service struct {
	//RecordType string
	Value      string
	TTL        int
	Aliases    string
	Time       time.Time
}
type Record struct {
	size int
	list *list.List
}
type RecordCache struct {
	mu sync.Mutex

	list *list.List
	table map[string]*Record
}
type LRUCache struct {
	mu sync.Mutex

	// list & table of *entry objects
	list  *list.List
	table map[string]*RecordCache

	// Our current size, in bytes. Obviously a gross simplification and low-grade
	// approximation.
	size uint64

	// How many bytes we are limiting the cache to.
	capacity uint64
}

//name to type
func NewLRUCache(capacity uint64) *LRUCache {
	return &LRUCache{
		list:     list.New(),
		table:    make(map[string]*RecordCache),
		capacity: capacity,
	}
}

//type to record
func NewRecordCache() *RecordCache{
	return &RecordCache{
		list:	list.New(),
		table:  make(map[string]*Record),
	}

}

<|endoftext|>