Ejemplo n.º 1
0
func (s marketStore) record(m emdn.Transaction) {
	k := m.Item
	// Demand
	tree, ok := s.itemDemand[k]
	if !ok {
		tree = llrb.New()
		s.itemDemand[k] = tree
	}
	tree.ReplaceOrInsert(demtrans(m))
	for tree.Len() > maxItems {
		tree.DeleteMin()
	}
	stationPriceUpdate(s.stationDemand, m.Station, k, m.SellPrice)

	// Supply
	tree, ok = s.itemSupply[k]
	if !ok {
		tree = llrb.New()
		s.itemSupply[k] = tree
	}
	if m.BuyPrice == 0 {
		m.BuyPrice = math.MaxInt64
	}
	tree.ReplaceOrInsert(suptrans(m))
	for tree.Len() > maxItems {
		tree.DeleteMax()
	}
	stationPriceUpdate(s.stationSupply, m.Station, k, m.BuyPrice)
}
Ejemplo n.º 2
0
func InitCache() *MyError.MyError {
	once.Do(func() {
		DomainRRCache = &DomainRRTree{
			LLRB:    llrb.New(),
			RWMutex: &sync.RWMutex{},
		}
		DomainSOACache = &DomainSOATree{
			LLRB:    llrb.New(),
			RWMutex: &sync.RWMutex{},
		}
	})
	return nil
}
Ejemplo n.º 3
0
func newShardedCache(n int, de time.Duration) *shardedCache {
	max := big.NewInt(0).SetUint64(uint64(math.MaxUint32))
	rnd, err := rand.Int(rand.Reader, max)
	var seed uint32
	if err != nil {
		os.Stderr.Write([]byte("WARNING: go-cache's newShardedCache failed to read from the system CSPRNG (/dev/urandom or equivalent.) Your system's security may be compromised. Continuing with an insecure seed.\n"))
		seed = insecurerand.Uint32()
	} else {
		seed = uint32(rnd.Uint64())
	}
	sc := &shardedCache{
		seed: seed,
		m:    uint32(n),
		cs:   make([]*cache, n),
	}
	for i := 0; i < n; i++ {
		c := &cache{
			defaultExpiration: de,
			items:             map[string]Item{},
			sortedItems:       llrb.New(),
		}
		sc.cs[i] = c
	}
	return sc
}
Ejemplo n.º 4
0
// NewEnvironment constructs an Environment with the given parent to provide a
// fallback for finding variables. The parent may be nil.
func NewEnvironment(parent Environment) Environment {
	e := new(environment)
	e.writable = true
	e.vars = llrb.New()
	e.parent = parent
	return e
}
Ejemplo n.º 5
0
// Returns a new task queue.
// The user could submit new task through channel ch.
func NewTaskQueue(ch <-chan Task) *TaskQueue {
	ret := new(TaskQueue)
	ret.tree = llrb.New(taskBefore)
	ret.ch = ch
	ret.waitTime = maxTime
	return ret
}
Ejemplo n.º 6
0
// Add a term that can be searched on (append or create to an existing term)
func (rfs *RootFileSystem) SearchAddTerm(area string, term string, path string, version string) error {
	searchIndex := rfs.ChangeCache.GetSearchIndex()
	treeNode, ok := searchIndex.Terms[area]
	var searchTree *SearchTree = &SearchTree{}
	var err error
	if !ok {
		// Create a node for this tree
		treeNode = rfs.BlockHandler.GetFreeBlockNode(SEARCHTREE)
		searchIndex.Terms[area] = treeNode
		searchTree.Tree = llrb.New()
		searchTree.Node = treeNode
		// Need to save the searchTree back
		rfs.ChangeCache.SaveSearchIndex(searchIndex)
	} else {
		searchTree, err = rfs.ChangeCache.GetSearchTree(treeNode)
		if err != nil {
			return err
		}
	}
	// Now we have a search tree, add the term
	var searchItem = SearchEntry{term, nil}
	existingValue := searchTree.Tree.Get(searchItem)
	var pointValue SearchEntry
	if existingValue != nil {
		pointValue = existingValue.(SearchEntry)
	} else {
		pointValue = SearchEntry{term, make([]Entry, 0)}
	}
	entry := Entry{path, version}
	pointValue.Matches = append(pointValue.Matches, entry)
	searchTree.Tree.ReplaceOrInsert(pointValue)
	// And save this searchTree back
	rfs.ChangeCache.SaveSearchTree(searchTree)
	return nil
}
Ejemplo n.º 7
0
// rebuildIndex does the work of regenerating the index
// with the given keys.
func rebuild(less llrb.LessFunc, keys <-chan string) *llrb.Tree {
	tree := llrb.New(less)
	for key := range keys {
		tree.ReplaceOrInsert(key)
	}
	return tree
}
Ejemplo n.º 8
0
func (tdi *TripleDirectionIndex) GetOrCreate(d graph.Direction, id int64) *llrb.LLRB {
	directionIndex := tdi.GetForDir(d)
	if _, ok := directionIndex[id]; !ok {
		directionIndex[id] = llrb.New()
	}
	return directionIndex[id]
}
Ejemplo n.º 9
0
func (this *Trellis) insert(t int, s *Bigram, sa *Bigram, kb int, p float64) {
	i, ok := this.trl[t].Get(s)
	if !ok {
		TRACE(4, "    Inserting. Is a new element, init list.", MOD_HMM)
		m := llrb.New()
		m.InsertNoReplace(NewElement(sa, kb, p))
		this.trl[t].Insert(s, m)
	} else {
		TRACE(4, "    Inserting. Not a new element, add. List size="+strconv.Itoa(i.(*llrb.LLRB).Len())+"/"+strconv.Itoa(this.kbest), MOD_HMM)
		j := i.(*llrb.LLRB).Min()
		if i.(*llrb.LLRB).Len() == this.kbest && p < j.(*Element).prob {
			TRACE(4, "    Not worth inserting", MOD_HMM)
			return
		}

		i.(*llrb.LLRB).InsertNoReplace(NewElement(sa, kb, p))

		if i.(*llrb.LLRB).Len() > this.kbest {
			i.(*llrb.LLRB).Delete(i.(*llrb.LLRB).Min())
			TRACE(4, "    list too long. Last erased", MOD_HMM)
		}
	}
	i, ok = this.trl[t].Get(s)
	if ok && i.(*llrb.LLRB).Len() > 0 {
		TRACE(4, "MAX:"+strconv.FormatFloat(i.(*llrb.LLRB).Max().(*Element).prob, 'f', -1, 64)+" MIN:"+strconv.FormatFloat(i.(*llrb.LLRB).Min().(*Element).prob, 'f', -1, 64), MOD_HMM)
	}
}
Ejemplo n.º 10
0
// NewRegionCache creates a RegionCache.
func NewRegionCache(pdClient pd.Client) *RegionCache {
	return &RegionCache{
		pdClient: pdClient,
		regions:  make(map[RegionVerID]*Region),
		sorted:   llrb.New(),
	}
}
Ejemplo n.º 11
0
// rebuildIndex does the work of regenerating the index
// with the given keys.
func rebuild(less LessFunction, keys <-chan string) *llrb.LLRB {
	tree := llrb.New()
	for key := range keys {
		tree.ReplaceOrInsert(llrbString{s: key, l: less})
	}
	return tree
}
Ejemplo n.º 12
0
func NewQTask() *QTask {
	return &QTask{
		tree:     llrb.New(),
		qchan:    make(chan Task, 30),
		waitTime: maxTime,
		mTask:    make(map[string]Task),
	}
}
Ejemplo n.º 13
0
// NewRegionCache creates a RegionCache.
func NewRegionCache(pdClient pd.Client) *RegionCache {
	c := &RegionCache{
		pdClient: pdClient,
	}
	c.mu.regions = make(map[RegionVerID]*Region)
	c.mu.sorted = llrb.New()
	return c
}
func BenchmarkSortedInsert_InsertNoReplace(b *testing.B) {
	for i := 0; i < b.N; i++ {
		tree := llrb.New()
		for i := 0; i < len(fixture.SortedTestData); i++ {
			tree.InsertNoReplace(llrbItem(fixture.SortedTestData[i]))
		}
	}
}
func BenchmarkInsert(b *testing.B) {
	for i := 0; i < b.N; i++ {
		tree := llrb.New()
		for i := 0; i < len(fixture.TestData); i++ {
			tree.ReplaceOrInsert(llrbItem(fixture.TestData[i]))
		}
	}
}
Ejemplo n.º 16
0
func newTreeBasedConnMap(maxNrConn, maxNrUsers, maxNrConnsPerUser int) connMap {
	ret := new(treeBasedConnMap)
	ret.tree = llrb.New()
	ret.maxNrConn = maxNrConn
	ret.maxNrUsers = maxNrUsers
	ret.maxNrConnsPerUser = maxNrConnsPerUser
	return ret
}
Ejemplo n.º 17
0
func main() {
	tree := llrb.New()
	tree.ReplaceOrInsert(llrb.Int(1))
	tree.ReplaceOrInsert(llrb.Int(2))
	tree.ReplaceOrInsert(llrb.Int(3))
	tree.ReplaceOrInsert(llrb.Int(4))
	tree.DeleteMin()
	tree.Delete(llrb.Int(4))
	tree.AscendGreaterOrEqual(tree.Min(), Print)
}
Ejemplo n.º 18
0
func (this *Set) Insert(item interface{}) {
	bin := this.bins[this.hasher(item)]
	if bin == nil {
		bin = llrb.New(llrb.LessFunc(this.lesser))
		this.bins[this.hasher(item)] = bin
	}
	if bin.ReplaceOrInsert(item) == nil {
		this.count++
	}
}
Ejemplo n.º 19
0
// NewRestrictedEnvironment constructs an Environment with the given set of
// mappings. The environment cannot be modified.
func NewRestrictedEnvironment(parent Environment, mapping map[Symbol]interface{}) Environment {
	e := new(environment)
	e.writable = false
	e.vars = llrb.New()
	for sym, val := range mapping {
		item := newEnvItem(sym, val)
		e.vars.ReplaceOrInsert(item)
	}
	e.parent = parent
	return e
}
Ejemplo n.º 20
0
// There's no way back!
func (self *treeBasedConnMap) CloseAll() {
	self.lock.Lock()
	var nilcs *connSet
	self.tree.AscendGreaterOrEqual(nilcs, func(i llrb.Item) bool {
		if cs, ok := i.(*connSet); ok {
			cs.CloseAll()
		}
		return true
	})
	self.tree = llrb.New()
}
Ejemplo n.º 21
0
// newMemoryTable constructs a MemoryTable instance.
func newMemoryTable(tableID int64, tableName string, cols []*table.Column, alloc autoid.Allocator) *MemoryTable {
	name := model.NewCIStr(tableName)
	t := &MemoryTable{
		ID:           tableID,
		Name:         name,
		alloc:        alloc,
		Columns:      cols,
		recordPrefix: tablecodec.GenTableRecordPrefix(tableID),
		tree:         llrb.New(),
	}
	return t
}
Ejemplo n.º 22
0
func NewManager(helper MHelper) *IManager {
	m := &IManager{}
	m.sortedItems = llrb.New()
	m.itemsByTid = make(map[string]*Item)
	m.itemsById = make(map[int64]*Item)
	m.itemsByTitle = make(map[string]*Item)
	m.mutex = &sync.RWMutex{}
	m.max_items = 120
	m.max_show = 100
	m.max_on_top = 5
	m.max_comments = 20
	m.h = helper
	return m
}
Ejemplo n.º 23
0
func newCache(de time.Duration, m map[string]Item) *cache {
	if de == 0 {
		de = -1
	}
	c := &cache{
		defaultExpiration: de,
		items:             m,
	}
	c.sortedItems = llrb.New()
	for k, item := range m {
		c.sortedItems.InsertNoReplace(Node{Key: k, Expiration: item.Expiration})
	}
	return c
}
Ejemplo n.º 24
0
// rebuildIndex does the work of regenerating the index
// according to the comparison function in the store.
func (s *OrderedStore) rebuildIndex() {
	s.index = llrb.New(s.lf)
	keyChan := s.Keys()
	count, began := 0, time.Now()
	for {
		key, ok := <-keyChan
		if !ok {
			break // closed
		}
		s.index.ReplaceOrInsert(key)
		count = count + 1
	}
	if count > 0 {
		log.Printf("index rebuilt (%d keys) in %s", count, time.Since(began))
	}
}
func BenchmarkIterate(b *testing.B) {
	tree := llrb.New()
	for i := 0; i < len(fixture.TestData); i++ {
		tree.ReplaceOrInsert(llrbItem(fixture.TestData[i]))
	}
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		min := tree.Min()
		tree.AscendGreaterOrEqual(min, func(i llrb.Item) bool {
			_ = i.(llrbItem).Key
			_ = i.(llrbItem).Value
			return true
		})
	}
}
Ejemplo n.º 26
0
func main() {
	tree := llrb.New(lessInt)
	tree.ReplaceOrInsert(1)
	tree.ReplaceOrInsert(2)
	tree.ReplaceOrInsert(3)
	tree.ReplaceOrInsert(4)
	tree.DeleteMin()
	tree.Delete(4)
	c := tree.IterAscend()
	for {
		u := <-c
		if u == nil {
			break
		}
		fmt.Printf("%d\n", int(u.(int)))
	}
}
Ejemplo n.º 27
0
func TestQueryGets(t *testing.T) {
	for i, test := range veraxDevices {
		var err error

		var vresults *llrb.Tree
		if vresults, err = ReadVeraxResults(test.path); err != nil {
			t.Errorf("#%d, %s: ReadVeraxResults error: %s", i, test.path, err)
		}

		var counter int
		var uri, oids string
		gresults := llrb.New(LessOID)
		ch := vresults.IterAscend()
		for {
			r := <-ch
			if r == nil {
				break
			}
			counter += 1
			if counter <= 3 { // TODO random number of oids, not hardcoded n
				oid := r.(QueryResult)
				oids += "," + oid.Oid
			} else {
				uri = `snmp://[email protected]:` + strconv.Itoa(test.port) + "//(" + oids[1:] + ")"
				counter = 0 // reset
				oids = ""   // reset

				params := &QueryParams{
					Uri:     uri,
					Version: GNET_SNMP_V2C,
					Timeout: 200,
					Retries: 5,
					Tree:    gresults,
				}
				if _, err := Query(params); err != nil {
					t.Errorf("Query error: %s. Uri: %s", err, uri)
				}
			}
		}
		CompareVerax(t, gresults, vresults)
	}
}
Ejemplo n.º 28
0
func main() {
	tree := llrb.New()
	tree.ReplaceOrInsert(llrb.Int(1))
	tree.InsertNoReplace(llrb.Int(1))
	tree.ReplaceOrInsert(llrb.Int(2))
	tree.ReplaceOrInsert(llrb.Int(3))
	tree.InsertNoReplace(llrb.Int(3))
	tree.InsertNoReplace(llrb.Int(2))
	tree.ReplaceOrInsert(llrb.Int(4))
	tree.DeleteMin()
	tree.Delete(llrb.Int(4))
	tree.AscendGreaterOrEqual(tree.Min(), func(item llrb.Item) bool {
		i, ok := item.(llrb.Int)
		if !ok {
			return false
		}
		fmt.Println(int(i))
		return true
	})
}
Ejemplo n.º 29
0
func main() {
	flag.Parse()
	vals := rand.Perm(*size)
	var t, v interface{}
	v = vals
	var stats runtime.MemStats
	for i := 0; i < 10; i++ {
		runtime.GC()
	}
	fmt.Println("-------- BEFORE ----------")
	runtime.ReadMemStats(&stats)
	fmt.Printf("%+v\n", stats)
	start := time.Now()
	if *gollrb {
		tr := llrb.New()
		for _, v := range vals {
			tr.ReplaceOrInsert(llrb.Int(v))
		}
		t = tr // keep it around
	} else {
		tr := btree.New(*degree)
		for _, v := range vals {
			tr.ReplaceOrInsert(btree.Int(v))
		}
		t = tr // keep it around
	}
	fmt.Printf("%v inserts in %v\n", *size, time.Since(start))
	fmt.Println("-------- AFTER ----------")
	runtime.ReadMemStats(&stats)
	fmt.Printf("%+v\n", stats)
	for i := 0; i < 10; i++ {
		runtime.GC()
	}
	fmt.Println("-------- AFTER GC ----------")
	runtime.ReadMemStats(&stats)
	fmt.Printf("%+v\n", stats)
	if t == v {
		fmt.Println("to make sure vals and tree aren't GC'd")
	}
}
Ejemplo n.º 30
0
func IoRoutine(request <-chan *IoArgs, responce chan<- interface{}) {
	log.Println("start IoRoutine")

	sortPieces := llrb.New(ioLessFun)

	for arg := range request {
		//todo: sort by offset and batch process io
		cnt := len(request)

		//batch get request, then sort by offset
		sortPieces.InsertNoReplace(arg)
		for i := 0; i < cnt; i++ {
			a := <-request
			sortPieces.InsertNoReplace(a)
			if a.ioMode == MODE_WRITE {
				break
			}
		}

		//batch process
		if cnt > cap(request)/2 {
			log.Println("io is busy, batch io count", cnt)
		}

		for {
			min := sortPieces.DeleteMin()
			if min == nil {
				break
			}

			//log.Println("io offset", min.(*IoArgs).offset)

			HandleIo(min.(*IoArgs))
			responce <- min.(*IoArgs).context
		}
	}

	log.Println("exit IoRoutine")
}