// CutIndexKeyNew cuts encoded index key into colIDs to bytes slices.
// The returned value b is the remaining bytes of the key which would be empty if it is unique index or handle data
// if it is non-unique index.
169859
func CutIndexKeyNew(key kv.Key, length int) (values [][]byte, b []byte, err error) {
	b = key[prefixLen+idLen:]
	values = make([][]byte, 0, length)
	for i := 0; i < length; i++ {
		var val []byte
		val, b, err = codec.CutOne(b)
		if err != nil {
			return nil, nil, errors.Trace(err)
		}
		values = append(values, val)
	}
	return
}
52176
func createPurgeIndexByHeightRangeEndKey(blockHeight uint64) []byte {
	var endKey []byte
	endKey = append(endKey, purgeIndexByHeightPrefix)
	endKey = append(endKey, compositeKeySep)
	endKey = append(endKey, util.EncodeOrderPreservingVarUint64(blockHeight)...)
	endKey = append(endKey, byte(0xff))
	return endKey
}
843871
func (c *Client) DeleteIndex(indices ...string) *IndicesDeleteService {
	return NewIndicesDeleteService(c).Index(indices)
}
338057
func (r *View) indexDestructive(needle []byte) bool {
	tbuf := make([]byte, 2*len(needle))
	idx := int64(0)
	for {
		data := r.chunkBytes()
		if len(data) == 0 {
			return false
		}

		// Scan the current chunk for needle. Note that if the current chunk is too
		// small to hold needle, this is a no-op.
		if idx = int64(bytes.Index(data, needle)); idx >= 0 {
			r.Skip(idx)
			return true
		}
		if len(data) > len(needle) {
			// The needle is definitely not in this space.
			r.Skip(int64(len(data) - len(needle)))
		}

		// needle isn't in the current chunk; however, it may begin at the end of
		// the current chunk and complete in future chunks.
		//
		// We will scan a space twice the size of the needle, as otherwise, this
		// would end up scanning for one possibility, incrementing by one, and
		// repeating via 'for' loop iterations.
		//
		// Afterwards, we advance only the size of the needle, as we don't want to
		// preclude the needle starting after our last scan range.
		//
		// For example, to find needle "NDL":
		//
		// AAAAND|L|AAAA
		//     |------|^- [NDLAAA], 0
		//
		// AAAAN|D|NDL|AAAA
		//    |------| [ANDNDL], 3
		//
		// AAAA|A|A|NDL
		//   |-------| [AAAAND], -1, consume 3 => A|NDL|
		//
		//
		// Note that we perform the read with a cloned View so we don't
		// actually consume this data.
		pr := r.Clone()
		amt, _ := pr.Read(tbuf)
		if amt < len(needle) {
			// All remaining buffers cannot hold the needle.
			return false
		}

		if idx = int64(bytes.Index(tbuf[:amt], needle)); idx >= 0 {
			r.Skip(idx)
			return true
		}
		r.Skip(int64(len(needle)))
	}
}
311458
func (c *Client) DeleteIndex(name string) *DeleteIndexService {
	builder := NewDeleteIndexService(c)
	builder.Index(name)
	return builder
}
1137054
func (e *Entry) GetIndex(key string) int {
	for i := range e.Values {
		if e.Values[i].Key == key {
			return i
		}
	}
	return -1
}
339596
func (c *threadSafeMap) IndexKeys(indexName, indexKey string) ([]string, error) {
	c.lock.RLock()
	defer c.lock.RUnlock()

	indexFunc := c.indexers[indexName]
	if indexFunc == nil {
		return nil, fmt.Errorf("Index with name %s does not exist", indexName)
	}

	index := c.indices[indexName]

	set := index[indexKey]
	return set.List(), nil
}
321208
func cutNewLines(s string) string {
	r := strings.SplitN(s, "\r", 2)
	r = strings.SplitN(r[0], "\n", 2)
	return r[0]
}
983542
func NewKeyValuePair(pos position, key, ws string, value *string, comment *Comment) *KeyValuePair {
	return &KeyValuePair{
		pos: pos,

		key:     key,
		ws:      ws,
		value:   value,
		comment: comment,
	}
}
625280
func (c *Client) NewIndexRequest(bucket, index, key, start, end string) *RpbIndexReq {
	opts := &RpbIndexReq{
		Bucket: []byte(bucket),
		Index:  []byte(index),
	}

	var qType RpbIndexReq_IndexQueryType
	if key != "" {
		qType = 0
		opts.Qtype = &qType
		opts.Key = []byte(key)
	} else {
		qType = 1
		opts.Qtype = &qType
		opts.RangeMin = []byte(start)
		opts.RangeMax = []byte(end)
	}

	return opts
}
883098
func NewIndex(a []string) Index {
	index := make(map[string]int)
	for i, v := range a {
		index[v] = i
	}
	return index
}
463279
func CutColumnID(b []byte) (remain []byte, n int64, err error) {
	if len(b) < 1 {
		return nil, 0, errors.New("invalid encoded key")
	}
	// skip the flag
	b = b[1:]
	return DecodeVarint(b)
}
1085854
func BuildIndexTable(sortedKeywords []string) (IndexTable, error) {
	idx := IndexTable{Dup: map[int32]int32{}}
	if !sort.StringsAreSorted(sortedKeywords) {
		return idx, fmt.Errorf("unsorted keywords")
	}
	keys := make([]string, 0, len(sortedKeywords))
	ids := make([]int, 0, len(sortedKeywords))
	prev := struct {
		no   int
		word string
	}{}
	for i, key := range sortedKeywords {
		if key == prev.word {
			idx.Dup[int32(prev.no)]++
			continue
		}
		prev.no = i
		prev.word = key
		keys = append(keys, key)
		ids = append(ids, i)
	}
	d, err := da.BuildWithIDs(keys, ids)
	if err != nil {
		return idx, fmt.Errorf("build error, %v", err)
	}
	idx.Da = d
	return idx, nil
}
185622
func (ms *MapperStruct) NewKey() sophie.Sophier {
	if ms.NewKeyF != nil {
		return ms.NewKeyF()
	}
	return new(sophie.RawByteSlice)
}
1231300
func (sc SigChain) GetExpectedNextHighSkip(mctx MetaContext, uid keybase1.UID) (HighSkip, error) {
	if sc.localChainNextHighSkipOverride != nil {
		return *sc.localChainNextHighSkipOverride, nil
	}
	if len(sc.chainLinks) == 0 {
		return NewInitialHighSkip(), nil
	}
	return sc.GetLastLink().ExpectedNextHighSkip(mctx, uid)
}
1065968
func (a *segment) searchIndex(key []byte) (int, int) {
	if a.index != nil {
		// Check the in-memory index for a more accurate window.
		return a.index.lookup(key)
	}

	return 0, a.Len()
}
527148
func (s *segmentKeysIndex) lookup(key []byte) (leftPos int, rightPos int) {
	i, j := 0, s.numKeys

	if i == j || s.numKeys < 2 {
		// The index either wasn't used or isn't of any use.
		rightPos = s.srcKeyCount
		return
	}

	// If key smaller than the first key, return early.
	keyStart := s.offsets[0]
	keyEnd := s.offsets[1]
	cmp := bytes.Compare(key, s.data[keyStart:keyEnd])
	if cmp < 0 {
		return
	}

	indexOfLastKey := s.numKeys - 1

	// If key larger than last key, return early.
	keyStart = s.offsets[indexOfLastKey]
	keyEnd = uint32(s.numKeyBytes)
	cmp = bytes.Compare(s.data[keyStart:keyEnd], key)
	if cmp < 0 {
		leftPos = (indexOfLastKey) * s.hop
		rightPos = s.srcKeyCount
		return
	}

	for i < j {
		h := i + (j-i)/2

		keyStart = s.offsets[h]
		if h < indexOfLastKey {
			keyEnd = s.offsets[h+1]
		} else {
			keyEnd = uint32(s.numKeyBytes)
		}

		cmp = bytes.Compare(s.data[keyStart:keyEnd], key)
		if cmp == 0 {
			leftPos = h * s.hop
			rightPos = leftPos + 1
			return // Direct hit.
		} else if cmp < 0 {
			if i == h {
				break
			}
			i = h
		} else {
			j = h
		}
	}

	leftPos = i * s.hop
	rightPos = j * s.hop

	return
}