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


Golang bolt.DB类代码示例

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


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

示例1: AtomicDelete

// AtomicDelete deletes a value at "key" if the key
// has not been modified in the meantime, throws an
// error if this is the case
func (b *BoltDB) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
	var (
		val []byte
		db  *bolt.DB
		err error
	)

	if previous == nil {
		return false, store.ErrPreviousNotSpecified
	}
	if db, err = b.getDBhandle(); err != nil {
		return false, err
	}
	defer b.releaseDBhandle()

	err = db.Update(func(tx *bolt.Tx) error {
		bucket := tx.Bucket(b.boltBucket)
		if bucket == nil {
			return ErrBoltBucketNotFound
		}

		val = bucket.Get([]byte(key))
		dbIndex := binary.LittleEndian.Uint64(val[:libkvmetadatalen])
		if dbIndex != previous.LastIndex {
			return store.ErrKeyModified
		}
		err := bucket.Delete([]byte(key))
		return err
	})
	if err != nil {
		return false, err
	}
	return true, err
}
开发者ID:thieman,项目名称:libnetwork,代码行数:37,代码来源:boltdb.go

示例2: ShowStatus

func ShowStatus(db *bolt.DB, u string) error {
	ur, err := url.Parse(u)
	if err != nil {
		return err
	}

	fmt.Printf("Host %s status:\n", ur.Host)
	db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(ur.Host))
		c := b.Cursor()
		for k, v := c.First(); k != nil; k, v = c.Next() {
			var sr StatRecord
			err := json.Unmarshal(v, &sr)
			if err != nil {
				fmt.Println(err)
				c.Next()
			}

			fmt.Printf("%s => %s\n", k, sr)
		}
		fmt.Printf("\n\n")
		return nil
	})
	return nil
}
开发者ID:SmilingNavern,项目名称:zennag,代码行数:25,代码来源:db.go

示例3: benchReadSequential

func benchReadSequential(db *bolt.DB, options *BenchOptions, results *BenchResults) error {
	return db.View(func(tx *bolt.Tx) error {
		var t = time.Now()

		for {
			c := tx.Bucket(benchBucketName).Cursor()
			var count int
			for k, v := c.First(); k != nil; k, v = c.Next() {
				if v == nil {
					return errors.New("invalid value")
				}
				count++
			}

			if options.WriteMode == "seq" && count != options.Iterations {
				return fmt.Errorf("read seq: iter mismatch: expected %d, got %d", options.Iterations, count)
			}

			results.ReadOps += count

			// Make sure we do this for at least a second.
			if time.Since(t) >= time.Second {
				break
			}
		}

		return nil
	})
}
开发者ID:jebjerg,项目名称:bolt,代码行数:29,代码来源:bench.go

示例4: init

// init initializes the database for this layer setup.
func (l *Layered) init(db *bolt.DB) ([]*layerVertex, error) {
	layerVertices := make([]*layerVertex, len(l.Layers))
	for i, layer := range l.Layers {
		var parent *Layer
		if i > 0 {
			parent = l.Layers[i-1]
		}

		layerVertex, err := l.initLayer(db, layer, parent)
		if err != nil {
			return nil, err
		}

		layerVertices[i] = layerVertex
		if parent != nil {
			// We have a prior layer, so setup the edge pointer
			err = db.Update(func(tx *bolt.Tx) error {
				bucket := tx.Bucket(boltEdgesBucket)
				return bucket.Put(
					[]byte(layer.ID),
					[]byte(parent.ID))
			})
			if err != nil {
				return nil, err
			}
		}
	}

	return layerVertices, nil
}
开发者ID:rowhit,项目名称:otto,代码行数:31,代码来源:layered.go

示例5: deleteLayer

func (l *Layered) deleteLayer(db *bolt.DB, layer *Layer, path string) error {
	if err := os.RemoveAll(path); err != nil {
		return err
	}

	return db.Update(func(tx *bolt.Tx) error {
		// Delete the layer itself
		bucket := tx.Bucket(boltLayersBucket)
		key := []byte(layer.ID)
		if err := bucket.Delete(key); err != nil {
			return err
		}

		// Delete all the edges
		bucket = tx.Bucket(boltEdgesBucket)
		if err := bucket.Delete(key); err != nil {
			return err
		}

		// Find any values
		return bucket.ForEach(func(k, data []byte) error {
			if string(data) == layer.ID {
				return bucket.Delete(k)
			}

			return nil
		})
	})
}
开发者ID:rowhit,项目名称:otto,代码行数:29,代码来源:layered.go

示例6: benchWriteNestedWithSource

func benchWriteNestedWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {
	results.WriteOps = options.Iterations

	for i := 0; i < options.Iterations; i += options.BatchSize {
		err := db.Update(func(tx *bolt.Tx) error {
			top, _ := tx.CreateBucketIfNotExists(benchBucketName)

			var name = make([]byte, options.KeySize)
			binary.BigEndian.PutUint32(name, keySource())
			b, _ := top.CreateBucketIfNotExists(name)

			for j := 0; j < options.BatchSize; j++ {
				var key = make([]byte, options.KeySize)
				var value = make([]byte, options.ValueSize)
				binary.BigEndian.PutUint32(key, keySource())
				if err := b.Put(key, value); err != nil {
					return err
				}
			}

			return nil
		})
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:ronggenliu,项目名称:bolt,代码行数:28,代码来源:bench.go

示例7: addUser

func addUser(db *bolt.DB, email string, password string) bool {
	check := []byte("")
	hashedPass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		fmt.Println(err)
	}
	db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("UsersBucket"))
		check = b.Get([]byte(email))
		return nil
	})
	if check == nil {
		db.Update(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte("UsersBucket"))
			err := b.Put([]byte(email), []byte(hashedPass))
			return err
		})
		if err != nil {
			fmt.Println(err)
		}
		return true
	} else {
		return false
	}
}
开发者ID:GetGoBlog,项目名称:GoBlog,代码行数:25,代码来源:main.go

示例8: QueryNodeByTaxID

// QueryNodeByTaxID querys Node by taxid
func QueryNodeByTaxID(db *bolt.DB, bucket string, taxids []string) ([]nodes.Node, error) {
	for _, taxid := range taxids {
		if !reDigitals.MatchString(taxid) {
			return []nodes.Node{}, fmt.Errorf("non-digital taxid given: %s", taxid)
		}
	}
	nods := make([]nodes.Node, len(taxids))
	if len(taxids) == 0 {
		return nods, 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 == "" {
				nods[i] = nodes.Node{}
				continue
			}
			node, err := nodes.NodeFromJSON(s)
			if err != nil {
				return errors.New("failed to parse node record from database")
			}
			nods[i] = node
		}
		return nil
	})
	return nods, err
}
开发者ID:shenwei356,项目名称:gtaxon,代码行数:32,代码来源:nodes.go

示例9: dump

func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
	for k := range changes {
		// marshal
		var rs *RankSet
		s.lock_read(func() {
			rs = s.ranks[k]
		})
		if rs == nil {
			log.Warning("empty rankset:", k)
			continue
		}

		// serialization and save
		bin, err := rs.Marshal()
		if err != nil {
			log.Critical("cannot marshal:", err)
			os.Exit(-1)
		}

		db.Update(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte(BOLTDB_BUCKET))
			err := b.Put([]byte(fmt.Sprint(k)), bin)
			return err
		})
	}
}
开发者ID:anders007,项目名称:rank,代码行数:26,代码来源:service.go

示例10: probe

// *whirrrrrrrrrrr*
func probe(db *bolt.DB, api *tbotapi.TelegramBotAPI, closed chan struct{}, wg *sync.WaitGroup) {
	dbBucketName := "alphazero"
	dbBucketStats := fmt.Sprintf("%s.stats", dbBucketName)
	for {
		select {
		case <-closed:
			wg.Done()
			return
		case val := <-api.Updates:
			typ := val.Message.Type()
			if typ != model.TextType {
				fmt.Fprintf(os.Stderr, "Not handling type: %s\n", typ)
				continue
			}

			response, md := process(db, val)
			db.Update(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte(dbBucketStats))
				id := []byte("messages.count.received")
				var val []byte
				if val = b.Get(id); val == nil {
					val = []byte("0")
				}
				c, _ := strconv.Atoi(string(val))
				c++
				return b.Put(id, []byte(strconv.Itoa(c)))
			})

			// unsafeval := blackfriday.MarkdownCommon([]byte(*val.Message.Text))
			// unsafe := blackfriday.MarkdownCommon([]byte(response))
			// html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)

			sender := val.Message.Chat
			recipient := model.NewRecipientFromChat(val.Message.Chat)

			om := model.NewOutgoingMessage(
				model.NewChatRecipient(*recipient.ChatID),
				response).SetMarkdown(md)

			var msg *model.MessageResponse
			var err error

			msg, err = api.SendMessageExtended(om)

			if err != nil {
				msg, err = api.SendMessageExtended(om.SetMarkdown(false))
				if err != nil {
					fmt.Fprintf(os.Stderr, "Err: %s\n", err)
					continue
				}
			}

			fmt.Printf(
				"MessageID: %04d, ChatID: %s, Text: %s, IsGroupChat:%t\n",
				msg.Message.ID, sender.String(), response, msg.Message.Chat.IsGroupChat())
		case val := <-api.Errors:
			fmt.Printf("Err: %s\n", val)
		}
	}
}
开发者ID:zeroed,项目名称:alphazerobot,代码行数:61,代码来源:main.go

示例11: LoadAllNodes

// LoadAllNodes loads all nodes into memory
func LoadAllNodes(db *bolt.DB, bucket string) (map[string]nodes.Node, error) {
	nods := make(map[string]nodes.Node)

	ch := make(chan string, runtime.NumCPU())
	chDone := make(chan int)
	go func() {
		for s := range ch {
			node, err := nodes.NodeFromJSON(s)
			checkError(err)
			nods[node.TaxID] = node
		}
		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 nods, err
}
开发者ID:shenwei356,项目名称:gtaxon,代码行数:32,代码来源:nodes.go

示例12: printDBDebug

func printDBDebug(db *bolt.DB) {
	dbBucketName := "alphazero"
	dbBucketLogs := fmt.Sprintf("%s.logs", dbBucketName)
	db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(dbBucketLogs))
		c := b.Cursor()
		i := 10
		for k, v := c.Last(); k != nil; k, v = c.Prev() {
			log := LogEntry{}
			json.Unmarshal(v, &log)
			fmt.Printf("- %s\n", log.String())
			i--
			if i == 0 {
				break
			}
		}
		return nil
	})

	dbBucketStats := fmt.Sprintf("%s.stats", dbBucketName)
	db.View(func(tx *bolt.Tx) error {
		c := tx.Bucket([]byte(dbBucketStats)).Cursor()
		prefix := []byte("stats.")
		for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
			fmt.Printf("key=%s, value=%s\n", k, v)
		}
		return nil
	})
}
开发者ID:zeroed,项目名称:alphazerobot,代码行数:29,代码来源:main.go

示例13: logAction

func logAction(db *bolt.DB, action string) {
	dbBucketName := "alphazero"
	dbBucketLogs := fmt.Sprintf("%s.logs", dbBucketName)
	db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(dbBucketLogs))

		id, _ := b.NextSequence()
		l := LogEntry{
			ID:     id,
			When:   strconv.FormatInt(time.Now().Unix(), 10),
			Action: action,
			UUID:   uuid.NewV4().String(),
		}
		buf, err := json.Marshal(l)
		if err != nil {
			return err
		}

		err = b.Put(itob(l.ID), buf)
		if err != nil {
			return err
		}
		return nil
	})
}
开发者ID:zeroed,项目名称:alphazerobot,代码行数:25,代码来源:main.go

示例14: SerializeIndex

func (c *Context) SerializeIndex(index *Index) (err error) {
	var data []byte
	var db *bolt.DB

	if data, err = json.Marshal(index); err != nil {
		return
	}

	db, err = c.OpenDB()
	if err != nil {
		return err
	}
	defer db.Close()

	return db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists(byteify(IndicesKey))
		if err != nil {
			return err
		}
		if bucket == nil {
			return ErrNoSuchDbBucket
		}
		return bucket.Put(byteify(index.FileId), data)
	})
}
开发者ID:hatchling,项目名称:drive,代码行数:25,代码来源:config.go

示例15: newBroadcastsStore

// newBroadcastsStore creates a new BroadcastAddresses object after doing the
// necessary initialization.
func newBroadcastsStore(db *bolt.DB, username string) (*BroadcastAddresses, error) {

	b := &BroadcastAddresses{
		db:       db,
		username: []byte(username),
		addrs:    make([]bmutil.Address, 0),
	}

	err := db.Update(func(tx *bolt.Tx) error {
		userbucket, err := tx.CreateBucketIfNotExists([]byte(username))
		if err != nil {
			return err
		}

		bucket, err := userbucket.CreateBucketIfNotExists(broadcastAddressesBucket)
		if err != nil {
			return err
		}

		return bucket.ForEach(func(k, _ []byte) error {
			addr, err := bmutil.DecodeAddress(string(k))
			if err != nil {
				return err
			}

			b.addrs = append(b.addrs, *addr)
			return nil
		})
	})
	if err != nil {
		return nil, err
	}

	return b, nil
}
开发者ID:DanielKrawisz,项目名称:bmagent,代码行数:37,代码来源:broadcasts.go


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