Exemplo n.º 1
0
func main() {
	rbt := rbtree.New()

	rbt.Insert(rbtree.String("Hello"))
	rbt.Insert(rbtree.String("World"))

	rbt.Ascend(rbt.Min(), Print)
}
Exemplo n.º 2
0
// func NewRpcMux generates a new RPC mux
//
// The mux initially has no services registered
func NewRpcMux(transport RpcTransport) *RpcMux {
	mux := &RpcMux{
		address:    transport.GetAddress(),
		nextId:     0,
		generation: randUint64(),
		transport:  transport,
		active:     rbtree.New(),
		handlers:   make(map[string]RpcEndpoint),
		filters:    make(map[int]*rpcPacketFilterList),
		sessions:   make(map[string]*rpcSession),
	}
	mux.RegisterEndpoint("_ping", &rpcPingEndpoint{})
	mux.filters[RpcDirectionRx] = NewRpcPacketFilterList()
	mux.filters[RpcDirectionTx] = NewRpcPacketFilterList()
	return mux
}
Exemplo n.º 3
0
func main() {
	rbt := rbtree.New()

	m := 0
	n := 10

	for m < n {
		rbt.Insert(rbtree.Int(m))
		m++
	}

	m = 0
	for m < n {
		if m%2 == 0 {
			rbt.Delete(rbtree.Int(m))
		}
		m++
	}

	rbt.Ascend(rbt.Min(), Print)
}
Exemplo n.º 4
0
func main() {
	rbt := rbtree.New()

	var1 := Var{
		Expiry: time.Now().Add(time.Second * 10),
		ID:     "var1",
	}
	var2 := Var{
		Expiry: time.Now().Add(time.Second * 20),
		ID:     "var2",
	}
	var3 := Var{
		Expiry: var2.Expiry,
		ID:     "var2-dup",
	}
	var4 := Var{
		Expiry: time.Now().Add(time.Second * 40),
		ID:     "var4",
	}
	var5 := Var{
		Expiry: time.Now().Add(time.Second * 50),
		ID:     "var5",
	}

	rbt.Insert(var1)
	rbt.Insert(var2)
	rbt.Insert(var3)
	rbt.Insert(var4)
	rbt.Insert(var5)

	tmp := Var{
		Expiry: var4.Expiry,
		ID:     "This field is not the key factor",
	}

	// var4 and var5 were expected
	rbt.Ascend(rbt.Get(tmp), Print)
}
Exemplo n.º 5
0
// func Clear clears all entries
func (l *rpcPacketFilterList) Clear() {
	l.Lock()
	defer l.Unlock()
	l.tree = rbtree.New()
}
Exemplo n.º 6
0
// func Handle handles an inbound packet
func (mux *RpcMux) handleRequest(request *RpcPacket) {
	// check whether we are already processing, and if so return cached version; if null ignore
	// drop all replies of other generations
	// drop all replies older than the last unacked
	mux.smutex.Lock()
	src := request.Source.String()
	session, ok := mux.sessions[src]
	if ok && session.generation != request.Generation {
		ok = false
	}
	if !ok {
		session = &rpcSession{generation: request.Generation, replies: rbtree.New()}
		// if there was a session from a previous generation the reply will simply not be cached
		mux.sessions[src] = session
	}
	mux.smutex.Unlock()

	session.Lock()
	// drop old reply caches
	for {
		item := session.replies.Min()
		if item == nil {
			break
		}
		comm := item.(*rpcCommunication)
		// stop if we are now into unacked packets
		// stop if we are still constructing the reply (should not happen as that should be unacked) or this is us
		if comm == nil || comm.id >= request.MinUnackedId || comm.reply == nil || comm.id == request.Id {
			break
		}
		session.replies.Delete(item)
	}

	searchcomm := request.getRequestCommunication()

	var reply *RpcPacket = nil

	// TODO: optimise this into a single 'Remove' call
	if item := session.replies.Get(searchcomm); item != nil {
		if comm := item.(*rpcCommunication); comm != nil &&
			comm.source == searchcomm.source &&
			comm.dest == searchcomm.dest &&
			comm.id == searchcomm.id &&
			comm.service == searchcomm.service {
			// OK so we've found a dupe. There are two possibilities:
			// 1. we are still processing the packet, in which case we silently discard this or
			// 2. we have already processed the packet, in which case we resend the cached reply
			reply = comm.reply
			session.Unlock()
			if reply != nil {
				// already cached so no need to recache
				if err := mux.sendWithFilter(reply); err != nil {
					log.Printf("[WARN] Could not send reply: %v", err)
				}
			}
			return
		}
	}

	// cache it, with a nil packet
	session.replies.Insert(searchcomm)

	session.Unlock()

	mux.hmutex.RLock()
	endpoint, ok := mux.handlers[request.Service]
	mux.hmutex.RUnlock()
	if ok {
		reply = endpoint.Handle(mux, request)
	} else {
		reply = request.MakeError(mux.address, RpcErrBadService)
	}

	// cache the reply
	session.Lock()
	searchcomm.reply = reply
	session.Unlock()

	if err := mux.sendWithFilter(reply); err != nil {
		log.Printf("[WARN] Could not send reply: %v", err)
	}
}
Exemplo n.º 7
0
func newSortableRowSet(start uint, limit uint, cols []*ColumnDefinition) *SortableRowSet {
	return &SortableRowSet{Limit: limit, Start: start, cols: cols, Rows: rbtree.New()}
}