当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Batch方法代码示例

本文整理汇总了Golang中github.com/boltdb/bolt.DB.Batch方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Batch方法的具体用法?Golang DB.Batch怎么用?Golang DB.Batch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/boltdb/bolt.DB的用法示例。


在下文中一共展示了DB.Batch方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: generateID

func generateID(db *bolt.DB) ([]byte, error) {
	var id []byte
	err := db.Batch(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("idgen"))
		if b == nil {
			log.Fatal(fmt.Errorf("Empty idgen"))
		}
		cid := b.Get([]byte("id"))
		if cid == nil {
			// If no ID, create ID starting at 1
			err := b.Put([]byte("id"), []byte("1"))
			if err != nil {
				return err
			}
			cid = b.Get([]byte("id"))
		} else {
			// Increment ID by 1 if it already exists
			cidInt, err := strconv.Atoi(string(cid))
			if err != nil {
				return err
			}
			cidInt = cidInt + 1
			cid = []byte(strconv.Itoa(cidInt))
			b.Put([]byte("id"), cid)
		}
		id = cid
		return nil
	})
	if err != nil {
		return nil, err
	}
	return id, nil
}
开发者ID:jmptrader,项目名称:todocli,代码行数:33,代码来源:todocli.go

示例2: sync

func sync(db *bolt.DB) {
	tc := transmission.New("http://127.0.0.1:9091", "", "")
	if _, err := tc.GetTorrents(); err != nil {
		fmt.Println("Could not connect to Transmission RPC API: " + err.Error())
		return
	}

	var (
		shows   []*Show
		results []*search.Result
	)

	waiting := 0
	fin := make(chan bool)
	rec := make(chan *search.Result)

	db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("Shows"))
		c := b.Cursor()

		for k, v := c.First(); k != nil; k, v = c.Next() {
			show, err := ShowFromBytes(v)
			if err == nil {
				shows = append(shows, show)
			}
		}

		return nil
	})

	for _, show := range shows {
		if show.Paused {
			continue
		}

		waiting += 1
		go syncShow(show, rec, fin)
	}

	for waiting > 0 {
		select {
		case result := <-rec:
			results = append(results, result)

		case <-fin:
			waiting -= 1
		}
	}

	for _, result := range results {
		fmt.Printf("Found torrent: '%s'\n", result.Name)

		if _, err := tc.AddTorrentByFilename(result.MagnetURL, ""); err != nil {
			fmt.Println("Error adding torrent: " + err.Error())
			fmt.Println("Stopping...")
			return
		}
	}

	fmt.Printf("Found %d torrent(s)\n", len(results))

	err := db.Batch(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("Shows"))

		for _, show := range shows {
			if err := b.Put([]byte(show.Name), show.Bytes()); err != nil {
				return err
			}
		}

		return nil
	})

	if err != nil {
		fmt.Printf("Error occurred while saving shows: %s", err.Error())
	}
}
开发者ID:Ragnis,项目名称:autousts,代码行数:77,代码来源:main.go

示例3: scanCities

func scanCities(
	db *bolt.DB, filename string, minPopulation int,
) (int, error) {
	file, err := os.Open(filename)
	if err != nil {
		return 0, err
	}
	defer file.Close()

	reader := bufio.NewReader(file)
	scanner := bufio.NewScanner(reader)

	citiesCount := 0

	err = db.Batch(func(tx *bolt.Tx) error {
		citiesBucket := tx.Bucket(ds.CitiesBucketName)
		cityNamesBucket := tx.Bucket(ds.CityNamesBucketName)

		for scanner.Scan() {
			cityData := strings.Split(scanner.Text(), "\t")
			cityBytes, err := prepareCityBytes(cityData)
			if err != nil {
				return err
			}

			population, _ := strconv.ParseInt(cityData[14], 0, 64)
			if population > int64(minPopulation) {
				citiesBucket.Put([]byte(cityData[0]), cityBytes)

				addCityToIndex(
					cityNamesBucket, cityData[0], cityData[1], "", uint32(population),
				)

				citiesCount++
			}
		}

		return err
	})

	return citiesCount, err
}
开发者ID:lebedev-yury,项目名称:cities-api,代码行数:42,代码来源:scan_cities.go

示例4: scanCountries

func scanCountries(db *bolt.DB, filename string) (int, error) {
	file, err := os.Open(filename)
	if err != nil {
		return 0, err
	}
	defer file.Close()

	reader := bufio.NewReader(file)
	scanner := bufio.NewScanner(reader)

	countriesCount := 0

	err = db.Batch(func(tx *bolt.Tx) error {
		countriesBucket := tx.Bucket(ds.CountriesBucketName)

		for scanner.Scan() {
			countryString := scanner.Text()
			if strings.HasPrefix(countryString, "#") {
				continue
			}

			countryData := strings.Split(countryString, "\t")
			countryBytes, err := prepareCountryBytes(countryData)
			if err != nil {
				return err
			}

			if id := countryData[16]; id != "" {
				countriesBucket.Put([]byte(id), countryBytes)
				countriesCount++
			}
		}

		return err
	})

	return countriesCount, err
}
开发者ID:lebedev-yury,项目名称:cities-api,代码行数:38,代码来源:scan_countries.go


注:本文中的github.com/boltdb/bolt.DB.Batch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。