本文整理汇总了Golang中github.com/cockroachdb/cockroach/client.DB类的典型用法代码示例。如果您正苦于以下问题:Golang DB类的具体用法?Golang DB怎么用?Golang DB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DB类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: compareBiogoNode
// compareBiogoNode compares a biogo node and a range tree node to determine if both
// contain the same values in the same order. It recursively calls itself on
// both children if they exist.
func compareBiogoNode(db *client.DB, biogoNode *llrb.Node, key *proto.Key) error {
// Retrieve the node form the range tree.
rtNode := &proto.RangeTreeNode{}
if err := db.GetProto(keys.RangeTreeNodeKey(*key), rtNode); err != nil {
return err
}
bNode := &proto.RangeTreeNode{
Key: proto.Key(biogoNode.Elem.(Key)),
ParentKey: proto.KeyMin,
Black: bool(biogoNode.Color),
}
if biogoNode.Left != nil {
leftKey := proto.Key(biogoNode.Left.Elem.(Key))
bNode.LeftKey = &leftKey
}
if biogoNode.Right != nil {
rightKey := proto.Key(biogoNode.Right.Elem.(Key))
bNode.RightKey = &rightKey
}
if err := nodesEqual(*key, *bNode, *rtNode); err != nil {
return err
}
if rtNode.LeftKey != nil {
if err := compareBiogoNode(db, biogoNode.Left, rtNode.LeftKey); err != nil {
return err
}
}
if rtNode.RightKey != nil {
if err := compareBiogoNode(db, biogoNode.Right, rtNode.RightKey); err != nil {
return err
}
}
return nil
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:38,代码来源:client_range_tree_test.go
示例2: getConfig
// getConfig retrieves the configuration for the specified key. If the
// key is empty, all configurations are returned. Otherwise, the
// leading "/" path delimiter is stripped and the configuration
// matching the remainder is retrieved. Note that this will retrieve
// the default config if "key" is equal to "/", and will list all
// configs if "key" is equal to "". The body result contains a listing
// of keys and retrieval of a config. The output format is determined
// by the request header.
func getConfig(db *client.DB, configPrefix proto.Key, config gogoproto.Message,
path string, r *http.Request) (body []byte, contentType string, err error) {
// Scan all configs if the key is empty.
if len(path) == 0 {
var rows []client.KeyValue
if rows, err = db.Scan(configPrefix, configPrefix.PrefixEnd(), maxGetResults); err != nil {
return
}
if len(rows) == maxGetResults {
log.Warningf("retrieved maximum number of results (%d); some may be missing", maxGetResults)
}
var prefixes []string
for _, row := range rows {
trimmed := bytes.TrimPrefix(row.Key, configPrefix)
prefixes = append(prefixes, url.QueryEscape(string(trimmed)))
}
// Encode the response.
body, contentType, err = util.MarshalResponse(r, prefixes, util.AllEncodings)
} else {
configkey := keys.MakeKey(configPrefix, proto.Key(path[1:]))
if err = db.GetProto(configkey, config); err != nil {
return
}
body, contentType, err = util.MarshalResponse(r, config, util.AllEncodings)
}
return
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:36,代码来源:config.go
示例3: compareBiogoTree
// compareBiogoTree walks both a biogo tree and the range tree to determine if both
// contain the same values in the same order.
func compareBiogoTree(db *client.DB, biogoTree *llrb.Tree) error {
rt := &proto.RangeTree{}
if err := db.GetProto(keys.RangeTreeRoot, rt); err != nil {
return err
}
return compareBiogoNode(db, biogoTree.Root, &rt.RootKey)
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:9,代码来源:client_range_tree_test.go
示例4: countRangeReplicas
func countRangeReplicas(db *client.DB) (int, error) {
desc := &roachpb.RangeDescriptor{}
if err := db.GetProto(keys.RangeDescriptorKey(roachpb.KeyMin), desc); err != nil {
return 0, err
}
return len(desc.Replicas), nil
}
开发者ID:rohanahata,项目名称:cockroach,代码行数:7,代码来源:replication_test.go
示例5: allocateNodeID
// allocateNodeID increments the node id generator key to allocate
// a new, unique node id.
func allocateNodeID(db *client.DB) (roachpb.NodeID, error) {
r, err := db.Inc(keys.NodeIDGenerator, 1)
if err != nil {
return 0, util.Errorf("unable to allocate node ID: %s", err)
}
return roachpb.NodeID(r.ValueInt()), nil
}
开发者ID:gechong,项目名称:cockroach,代码行数:9,代码来源:node.go
示例6: releaseLeases
func (p *planner) releaseLeases(db client.DB) {
if p.leases != nil {
for _, lease := range p.leases {
if err := p.leaseMgr.Release(lease); err != nil {
log.Warning(err)
}
}
p.leases = nil
}
// TODO(pmattis): This is a hack. Remove when schema change operations work
// properly.
if p.modifiedSchemas != nil {
for _, d := range p.modifiedSchemas {
var lease *LeaseState
err := db.Txn(func(txn *client.Txn) error {
var err error
lease, err = p.leaseMgr.Acquire(txn, d.id, d.version)
return err
})
if err != nil {
log.Warning(err)
continue
}
if err := p.leaseMgr.Release(lease); err != nil {
log.Warning(err)
}
}
p.modifiedSchemas = nil
}
}
开发者ID:BobbWu,项目名称:cockroach,代码行数:31,代码来源:plan.go
示例7: allocateStoreIDs
// allocateStoreIDs increments the store id generator key for the
// specified node to allocate "inc" new, unique store ids. The
// first ID in a contiguous range is returned on success.
func allocateStoreIDs(nodeID roachpb.NodeID, inc int64, db *client.DB) (roachpb.StoreID, error) {
r, err := db.Inc(keys.StoreIDGenerator, inc)
if err != nil {
return 0, util.Errorf("unable to allocate %d store IDs for node %d: %s", inc, nodeID, err)
}
return roachpb.StoreID(r.ValueInt() - inc + 1), nil
}
开发者ID:gechong,项目名称:cockroach,代码行数:10,代码来源:node.go
示例8: getPermConfig
// getPermConfig fetches the permissions config for 'prefix'.
func getPermConfig(db *client.DB, prefix string) (*config.PermConfig, error) {
config := &config.PermConfig{}
if err := db.GetProto(keys.MakeKey(keys.ConfigPermissionPrefix, proto.Key(prefix)), config); err != nil {
return nil, err
}
return config, nil
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:9,代码来源:util.go
示例9: split
func split(db *client.DB) {
for i := 1; i < 10; i++ {
fmt.Printf("split key: %v\n", i)
err := db.AdminSplit([]byte(fmt.Sprintf("%d", i)))
if err != nil {
panic(fmt.Sprintf("split fail. key: %v, err: %v\n", i, err))
}
}
}
开发者ID:YuleiXiao,项目名称:ccclient,代码行数:9,代码来源:main.go
示例10: deleteConfig
// deleteConfig removes the config specified by key.
func deleteConfig(db *client.DB, configPrefix proto.Key, path string, r *http.Request) error {
if len(path) == 0 {
return util.Errorf("no path specified for config Delete")
}
if path == "/" {
return util.Errorf("the default configuration cannot be deleted")
}
configKey := keys.MakeKey(configPrefix, proto.Key(path[1:]))
return db.Del(configKey)
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:11,代码来源:config.go
示例11: loadTree
// loadTree loads the tree root and all of its nodes. It puts all of the nodes
// into a map.
func loadTree(t *testing.T, db *client.DB) (*roachpb.RangeTree, map[string]roachpb.RangeTreeNode) {
tree := new(roachpb.RangeTree)
if err := db.GetProto(keys.RangeTreeRoot, tree); err != nil {
t.Fatal(err)
}
nodes := make(map[string]roachpb.RangeTreeNode)
if tree.RootKey != nil {
loadNodes(t, db, tree.RootKey, nodes)
}
return tree, nodes
}
开发者ID:billhongs,项目名称:cockroach,代码行数:13,代码来源:client_range_tree_test.go
示例12: loadTree
// loadTree loads the tree root and all of its nodes. It puts all of the nodes
// into a map.
func loadTree(t *testing.T, db *client.DB) (storage.RangeTree, map[string]storage.RangeTreeNode) {
var tree storage.RangeTree
if err := db.GetProto(keys.RangeTreeRoot, &tree); err != nil {
t.Fatal(err)
}
nodes := make(map[string]storage.RangeTreeNode)
if tree.RootKey != nil {
loadNodes(t, db, tree.RootKey, nodes)
}
return tree, nodes
}
开发者ID:GitGoldie,项目名称:cockroach,代码行数:13,代码来源:client_range_tree_test.go
示例13: getRangeKeys
// getRangeKeys returns the end keys of all ranges.
func getRangeKeys(db *client.DB) ([]roachpb.Key, error) {
rows, err := db.Scan(keys.Meta2Prefix, keys.MetaMax, 0)
if err != nil {
return nil, err
}
ret := make([]roachpb.Key, len(rows), len(rows))
for i := 0; i < len(rows); i++ {
ret[i] = bytes.TrimPrefix(rows[i].Key, keys.Meta2Prefix)
}
return ret, nil
}
开发者ID:haint504,项目名称:cockroach,代码行数:12,代码来源:split_test.go
示例14: treesEqual
// treesEqual compares the expectedTree and expectedNodes to the actual range
// tree stored in the db.
func treesEqual(db *client.DB, expected testRangeTree) error {
// Compare the tree roots.
actualTree := &proto.RangeTree{}
if err := db.GetProto(keys.RangeTreeRoot, actualTree); err != nil {
return err
}
if !reflect.DeepEqual(&expected.Tree, actualTree) {
return util.Errorf("Range tree root is not as expected - expected:%+v - actual:%+v", expected.Tree, actualTree)
}
return treeNodesEqual(db, expected, expected.Tree.RootKey)
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:14,代码来源:client_range_tree_test.go
示例15: insert
func insert(r *rand.Rand, db *client.DB, wg *sync.WaitGroup) {
wg.Add(1)
for i := 0; i < 1000; i++ {
key := getKey(r)
value := getValue(1024 * 10)
err := db.Put(key, value)
if err != nil {
fmt.Printf("put fail. err: %v, key: %v\n", err, key)
}
}
wg.Done()
}
开发者ID:YuleiXiao,项目名称:ccclient,代码行数:12,代码来源:main.go
示例16: loadNodes
// loadNodes fetches a node and recursively all of its children.
func loadNodes(t *testing.T, db *client.DB, key roachpb.RKey, nodes map[string]roachpb.RangeTreeNode) {
node := new(roachpb.RangeTreeNode)
if err := db.GetProto(keys.RangeTreeNodeKey(key), node); err != nil {
t.Fatal(err)
}
nodes[node.Key.String()] = *node
if node.LeftKey != nil {
loadNodes(t, db, node.LeftKey, nodes)
}
if node.RightKey != nil {
loadNodes(t, db, node.RightKey, nodes)
}
}
开发者ID:billhongs,项目名称:cockroach,代码行数:14,代码来源:client_range_tree_test.go
示例17: setDefaultRangeMaxBytes
// setDefaultRangeMaxBytes sets the range-max-bytes value for the default zone.
func setDefaultRangeMaxBytes(t *testing.T, db *client.DB, maxBytes int64) {
zone := &proto.ZoneConfig{}
if err := db.GetProto(keys.ConfigZonePrefix, zone); err != nil {
t.Fatal(err)
}
if zone.RangeMaxBytes == maxBytes {
return
}
zone.RangeMaxBytes = maxBytes
if err := db.Put(keys.ConfigZonePrefix, zone); err != nil {
t.Fatal(err)
}
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:14,代码来源:util.go
示例18: waitForInitialSplits
func waitForInitialSplits(db *client.DB) error {
expectedRanges := ExpectedInitialRangeCount()
return util.RetryForDuration(initialSplitsTimeout, func() error {
// Scan all keys in the Meta2Prefix; we only need a count.
rows, err := db.Scan(keys.Meta2Prefix, keys.MetaMax, 0)
if err != nil {
return err
}
if a, e := len(rows), expectedRanges; a != e {
return util.Errorf("had %d ranges at startup, expected %d", a, e)
}
return nil
})
}
开发者ID:JKhawaja,项目名称:cockroach,代码行数:14,代码来源:testserver.go
示例19: purgeOldLeases
// purgeOldLeases refreshes the leases on a table. Unused leases older than
// minVersion will be released.
// If deleted is set, minVersion is ignored; no lease is acquired and all
// existing unused leases are released. The table is further marked for
// deletion, which will cause existing in-use leases to be eagerly released once
// they're not in use any more.
// If t has no active leases, nothing is done.
func (t *tableState) purgeOldLeases(
db *client.DB, deleted bool, minVersion sqlbase.DescriptorVersion, store LeaseStore,
) error {
t.mu.Lock()
empty := len(t.active.data) == 0
t.mu.Unlock()
if empty {
// We don't currently have a lease on this table, so no need to refresh
// anything.
return nil
}
// Acquire and release a lease on the table at a version >= minVersion.
var lease *LeaseState
err := db.Txn(func(txn *client.Txn) error {
var err error
if !deleted {
lease, err = t.acquire(txn, minVersion, store)
if err == errTableDeleted {
deleted = true
}
}
if err == nil || deleted {
t.mu.Lock()
defer t.mu.Unlock()
var toRelease []*LeaseState
if deleted {
t.deleted = true
// If the table has been deleted, all leases are stale.
toRelease = append([]*LeaseState(nil), t.active.data...)
} else {
// Otherwise, all but the lease we just took are stale.
toRelease = append([]*LeaseState(nil), t.active.data[:len(t.active.data)-1]...)
}
if err := t.releaseLeasesIfNotActive(toRelease, store); err != nil {
return err
}
return nil
}
return err
})
if err != nil {
return err
}
if lease == nil {
return nil
}
return t.release(lease, store)
}
开发者ID:JKhawaja,项目名称:cockroach,代码行数:56,代码来源:lease.go
示例20: allocateNodeID
// allocateNodeID increments the node id generator key to allocate
// a new, unique node id. It will retry indefinitely on retryable
// errors.
func allocateNodeID(db *client.DB) (proto.NodeID, error) {
var id proto.NodeID
err := retry.WithBackoff(allocRetryOptions, func() (retry.Status, error) {
r, err := db.Inc(keys.NodeIDGenerator, 1)
if err != nil {
status := retry.Break
if _, ok := err.(util.Retryable); ok {
status = retry.Continue
}
return status, util.Errorf("unable to allocate node ID: %s", err)
}
id = proto.NodeID(r.ValueInt())
return retry.Break, nil
})
return id, err
}
开发者ID:huaxling,项目名称:cockroach,代码行数:19,代码来源:node.go
注:本文中的github.com/cockroachdb/cockroach/client.DB类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论