// Find the specific shard with the given key
182399
func (m *SyncMap) locate(key string) *syncMap {
	return m.shards[bkdrHash(key)&uint32((m.shardCount-1))]
}
1057089
func (c *ShardedClient) Get(key string) GetResponse {
	shard, conn, err := c.manager.GetShard(key)
	if shard == -1 {
		return NewGetErrorResponse(key, c.unmappedError(key))
	}
	if err != nil {
		return NewGetErrorResponse(key, c.connectionError(shard, err))
	}
	if conn == nil {
		// NOTE: zero is an invalid version id.
		return NewGetResponse(key, StatusKeyNotFound, 0, nil, 0)
	}

	client := c.builder(shard, conn)
	defer c.release(client, conn)

	result := client.Get(key)
	if client.IsValidState() {
		getOkByAddr.Add(conn.Key().Address, 1)
	} else {
		getErrByAddr.Add(conn.Key().Address, 1)
	}
	return result
}
624723
func intersect(si *topo.ShardInfo, allShards []*topo.ShardInfo) bool {
	for _, shard := range allShards {
		if key.KeyRangesIntersect(si.KeyRange, shard.KeyRange) {
			return true
		}
	}
	return false
}
1125866
func (l *ShardedNoTS) Get(tenantID, key string) (interface{}, error) {
	cache, ok := l.cache[tenantID]
	if !ok {
		return nil, ErrNotFound
	}

	return cache.Get(key)
}
499837
func (ss *shardedSearcher) Close() {
	ss.lock(context.Background())
	defer ss.unlock()
	for _, s := range ss.shards {
		s.Close()
	}
}
357146
func (s *rowSegment) Intersect(other *rowSegment) *rowSegment {
	data := s.data.Intersect(&other.data)

	return &rowSegment{
		data:  *data,
		shard: s.shard,
		n:     data.Count(),
	}
}
899341
func (m *SyncMap) Get(key string) (value interface{}, ok bool) {
	shard := m.locate(key)
	shard.RLock()
	value, ok = shard.items[key]
	shard.RUnlock()
	return
}
1160996
func (ts *Server) FindAllShardsInKeyspace(ctx context.Context, keyspace string) (map[string]*ShardInfo, error) {
	shards, err := ts.GetShardNames(ctx, keyspace)
	if err != nil {
		return nil, vterrors.Wrapf(err, "failed to get list of shards for keyspace '%v'", keyspace)
	}

	result := make(map[string]*ShardInfo, len(shards))
	wg := sync.WaitGroup{}
	mu := sync.Mutex{}
	rec := concurrency.FirstErrorRecorder{}
	for _, shard := range shards {
		wg.Add(1)
		go func(shard string) {
			defer wg.Done()
			si, err := ts.GetShard(ctx, keyspace, shard)
			if err != nil {
				if IsErrType(err, NoNode) {
					log.Warningf("GetShard(%v, %v) returned ErrNoNode, consider checking the topology.", keyspace, shard)
				} else {
					rec.RecordError(vterrors.Wrapf(err, "GetShard(%v, %v) failed", keyspace, shard))
				}
				return
			}
			mu.Lock()
			result[shard] = si
			mu.Unlock()
		}(shard)
	}
	wg.Wait()
	if rec.HasErrors() {
		return nil, rec.Error()
	}
	return result, nil
}
810671
func (s *xorKeySpace) Key(id []byte) Key {
	hash := sha256.Sum256(id)
	key := hash[:]
	return Key{
		Space:    s,
		Original: id,
		Bytes:    key,
	}
}
351891
func HashKey(key []byte) int64 {
	h := int64(xxhash.Sum64(key))
	if h == 0 {
		h = 1
	} else if h < 0 {
		h = 0 - h
	}
	return h
}
21429
func (m *SyncMap) Pop() (string, interface{}) {
	if m.Size() == 0 {
		panic("syncmap: map is empty")
	}

	var (
		key   string
		value interface{}
		found = false
		n     = int(m.shardCount)
	)

	for !found {
		idx := rand.Intn(n)
		shard := m.shards[idx]
		shard.Lock()
		if len(shard.items) > 0 {
			found = true
			for key, value = range shard.items {
				break
			}
			delete(shard.items, key)
		}
		shard.Unlock()
	}

	return key, value
}
254882
func (t *SecureTrie) hashKey(key []byte) []byte {
	h := newHasher(nil)
	h.sha.Reset()
	h.sha.Write(key)
	buf := h.sha.Sum(t.hashKeyBuf[:0])
	returnHasherToPool(h)
	return buf
}
1273797
func (s *shard) Add(key uint32, el interface{}) {
	l := s.Len()
	if l+1 > s.size {
		s.Evict()
	}

	s.Lock()
	s.items[key] = el
	s.Unlock()
}
775047
func (c *ShardedClient) Delete(key string) MutateResponse {
	return c.mutate(
		key,
		func(shardClient Client) MutateResponse {
			return shardClient.Delete(key)
		})
}
809389
func (c *Checkpoint) Get(streamName, shardID string) (string, error) {
	namespace := fmt.Sprintf("%s-%s", c.appName, streamName)

	params := &dynamodb.GetItemInput{
		TableName:      aws.String(c.tableName),
		ConsistentRead: aws.Bool(true),
		Key: map[string]*dynamodb.AttributeValue{
			"namespace": &dynamodb.AttributeValue{
				S: aws.String(namespace),
			},
			"shard_id": &dynamodb.AttributeValue{
				S: aws.String(shardID),
			},
		},
	}

	resp, err := c.client.GetItem(params)
	if err != nil {
		if c.retryer.ShouldRetry(err) {
			return c.Get(streamName, shardID)
		}
		return "", err
	}

	var i item
	dynamodbattribute.UnmarshalMap(resp.Item, &i)
	return i.SequenceNumber, nil
}
981972
func (r *RepoKeyResource) Find(owner, slug string, id int) (*Key, error) {
	key := Key{}
	path := fmt.Sprintf("/repositories/%s/%s/deploy-keys/%v", owner, slug, id)
	if err := r.client.do("GET", path, nil, nil, &key); err != nil {
		return nil, err
	}

	return &key, nil
}
1067554
func (c *Conn) GetCatShards() (shards CatShards) {
	shards = make(CatShards, 0)
	//force it to only respond with the columns we know about and in a forced order
	args := map[string]interface{}{"bytes": "b", "h": "index,shard,prirep,state,docs,store,ip,node"}
	s, err := c.DoCommand("GET", "/_cat/shards", args, nil)
	if err == nil {
		catShardLines := strings.Split(string(s[:]), "\n")
		for _, shardLine := range catShardLines {
			shard, _ := NewCatShardInfo(shardLine)
			if nil != shard {
				shards = append(shards, *shard)
			}
		}
	}
	return shards
}