Beispiel #1
0
// LoadAllNames loads all names into memory
func LoadAllNames(db *bolt.DB, bucket string) (map[string]nodes.Name, error) {
	names := make(map[string]nodes.Name)

	ch := make(chan string, runtime.NumCPU())
	chDone := make(chan int)
	go func() {
		for s := range ch {
			name, err := nodes.NameFromJSON(s)
			checkError(err)
			names[name.TaxID] = name
		}
		chDone <- 1
	}()

	err := db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(bucket))
		if b == nil {
			return fmt.Errorf("database not exists: %s", bucket)
		}

		b.ForEach(func(k, v []byte) error {
			ch <- string(v)
			return nil
		})

		return nil
	})
	close(ch)
	<-chDone
	return names, err
}
Beispiel #2
0
// QueryNameByTaxID querys Name by taxid
func QueryNameByTaxID(db *bolt.DB, bucket string, taxids []string) ([]nodes.Name, error) {
	names := make([]nodes.Name, len(taxids))
	if len(taxids) == 0 {
		return names, nil
	}
	err := db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(bucket))
		if b == nil {
			return fmt.Errorf("database not exists: %s", bucket)
		}
		for i, taxid := range taxids {
			s := string(b.Get([]byte(taxid)))
			if s == "" {
				names[i] = nodes.Name{}
				continue
			}
			name, err := nodes.NameFromJSON(s)
			checkError(err)
			names[i] = name
		}
		return nil
	})
	return names, err
}