示例#1
0
func newTree(ary uint64) *ptree {
	ptree := &ptree{
		root:    newNode(true, newKeys(), newNodes()),
		ary:     ary,
		pending: &pending{},
		waiter:  queue.New(10),
	}
	ptree.initPending()
	go ptree.listen()
	return ptree
}
示例#2
0
func (ptree *ptree) runAdds(addOperations map[*node]Keys) {
	if len(addOperations) == 0 {
		return
	}

	q := queue.New(int64(len(addOperations)))

	for n := range addOperations {
		q.Put(n)
	}

	nextLayer := make(map[*node][]*recursiveBuild)
	dummyRoot := &node{
		keys:  newKeys(),
		nodes: newNodes(),
	} // constructed in case we need it
	var needRoot uint64
	queue.ExecuteInParallel(q, func(ifc interface{}) {
		n := ifc.(*node)
		keys := addOperations[n]

		if len(keys) == 0 {
			return
		}

		parent := n.parent
		if parent == nil {
			parent = dummyRoot
			atomic.AddUint64(&needRoot, 1)
		}

		for _, key := range keys {
			oldKey := n.keys.insert(key)
			if oldKey == nil {
				atomic.AddUint64(&ptree.number, 1)
			}
		}

		if n.needsSplit(ptree.ary) {
			keys := make(Keys, 0, n.keys.len())
			nodes := make([]*node, 0, n.nodes.len())
			ptree.recursiveSplit(n, parent, nil, &nodes, &keys)
			ptree.write.Lock()
			nextLayer[parent] = append(
				nextLayer[parent], &recursiveBuild{keys: keys, nodes: nodes, parent: parent},
			)
			ptree.write.Unlock()
		}
	})

	setRoot := needRoot > 0

	ptree.recursiveAdd(nextLayer, setRoot)
}
示例#3
0
文件: messenger.go 项目: sevein/qubot
// add outgoing message to the corresponding channel queue, returns a pointer
// to the queue, a boolean where true means that the queue had to be created and
// false otherwise and an error if the message could not be added to the queue.
func (chq *chqueue) add(msg *slack.OutgoingMessage) (*queue.Queue, bool, error) {
	q := chq.get(msg.Channel)

	created := false
	if q == nil {
		q = queue.New(10)
		created = true

		chq.mux.Lock()
		chq.q[msg.Channel] = q
		chq.mux.Unlock()
	}

	err := q.Put(msg)

	return q, created, err
}
示例#4
0
func (ptree *ptree) recursiveAdd(layer map[*node][]*recursiveBuild, setRoot bool) {
	if len(layer) == 0 {
		return
	}

	if setRoot && len(layer) > 1 {
		panic(`SHOULD ONLY HAVE ONE ROOT`)
	}

	q := queue.New(int64(len(layer)))
	for _, rbs := range layer {
		q.Put(rbs)
	}

	layer = make(map[*node][]*recursiveBuild, len(layer))
	dummyRoot := &node{
		keys:  newKeys(),
		nodes: newNodes(),
	}
	queue.ExecuteInParallel(q, func(ifc interface{}) {
		rbs := ifc.([]*recursiveBuild)

		if len(rbs) == 0 {
			return
		}

		n := rbs[0].parent
		if setRoot {
			ptree.root = n
		}

		parent := n.parent
		if parent == nil {
			parent = dummyRoot
			setRoot = true
		}

		for _, rb := range rbs {
			for i, k := range rb.keys {
				if n.keys.len() == 0 {
					n.keys.insert(k)
					n.nodes.push(rb.nodes[i*2])
					n.nodes.push(rb.nodes[i*2+1])
					continue
				}

				n.keys.insert(k)
				index := n.search(k)
				n.nodes.replaceAt(index, rb.nodes[i*2])
				n.nodes.insertAt(index+1, rb.nodes[i*2+1])
			}
		}

		if n.needsSplit(ptree.ary) {
			keys := make(Keys, 0, n.keys.len())
			nodes := make([]*node, 0, n.nodes.len())
			ptree.recursiveSplit(n, parent, nil, &nodes, &keys)
			ptree.write.Lock()
			layer[parent] = append(
				layer[parent], &recursiveBuild{keys: keys, nodes: nodes, parent: parent},
			)
			ptree.write.Unlock()
		}
	})

	ptree.recursiveAdd(layer, setRoot)
}
示例#5
0
func init() {
	songQueue = queue.New(100)
}
示例#6
0
	"github.com/Workiva/go-datastructures/queue"
)

type Song struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Thumbnail   string `json:"thumbnail"`
	Id          string `json:"id"`
}

type JSONQueue struct {
	Queue []Song `json:"queue"`
}

// This is the queue that will hold all the songs in memory
var songQueue = queue.New(100)

func main() {
	// Setting the log flags
	log.SetFlags(log.Ltime | log.Lshortfile)

	port := os.Getenv("PORT")
	if port == "" {
		port = "3000"
	}

	// Returning the static files
	http.Handle("/", http.FileServer(http.Dir("./public")))

	// Setting up handlers for individual routes
	http.HandleFunc("/get_queue", get_queue)