Esempio n. 1
0
// QueryDivisionByDivisionID querys Division by taxid
func QueryDivisionByDivisionID(db *bolt.DB, bucket string, ids []string) ([]nodes.Division, error) {
	for _, id := range ids {
		if !reDigitals.MatchString(id) {
			return []nodes.Division{}, fmt.Errorf("non-digital division given: %s", id)
		}
	}
	divisions := make([]nodes.Division, len(ids))
	if len(ids) == 0 {
		return divisions, 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, id := range ids {
			s := string(b.Get([]byte(id)))
			if s == "" {
				divisions[i] = nodes.Division{}
				continue
			}
			division, err := nodes.DivisionFromJSON(s)
			if err != nil {
				return errors.New("failed to parse division record from database")
			}
			divisions[i] = division
		}
		return nil
	})
	return divisions, err
}
Esempio n. 2
0
// LoadAllDivisions loads all divisions into memory
func LoadAllDivisions(db *bolt.DB, bucket string) (map[string]nodes.Division, error) {
	divisions := make(map[string]nodes.Division)

	ch := make(chan string, runtime.NumCPU())
	chDone := make(chan int)
	go func() {
		for s := range ch {
			division, err := nodes.DivisionFromJSON(s)
			checkError(err)
			divisions[division.DivisionID] = division
		}
		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 divisions, err
}