Exemple #1
0
// QueryGenCodeByGenCodeID querys GenCode by taxid
func QueryGenCodeByGenCodeID(db *bolt.DB, bucket string, ids []string) ([]nodes.GenCode, error) {
	for _, id := range ids {
		if !reDigitals.MatchString(id) {
			return []nodes.GenCode{}, fmt.Errorf("non-digital gencode given: %s", id)
		}
	}
	gencodes := make([]nodes.GenCode, len(ids))
	if len(ids) == 0 {
		return gencodes, 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 == "" {
				gencodes[i] = nodes.GenCode{}
				continue
			}
			gencode, err := nodes.GenCodeFromJSON(s)
			if err != nil {
				return errors.New("failed to parse gencode record from database")
			}
			gencodes[i] = gencode
		}
		return nil
	})
	return gencodes, err
}
Exemple #2
0
// LoadAllGenCodes loads all gencodes into memory
func LoadAllGenCodes(db *bolt.DB, bucket string) (map[string]nodes.GenCode, error) {
	gencodes := make(map[string]nodes.GenCode)

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