• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

blevesearch / bleve / 20431931185

22 Dec 2025 12:28PM UTC coverage: 53.702% (-0.02%) from 53.722%
20431931185

Pull #2268

github

CascadingRadium
use prealloc
Pull Request #2268: Geo-spatial queries performance optimization

127 of 149 new or added lines in 10 files covered. (85.23%)

21 existing lines in 6 files now uncovered.

19104 of 35574 relevant lines covered (53.7%)

0.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/numeric/prefix_coded.go
1
//  Copyright (c) 2014 Couchbase, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//                 http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package numeric
16

17
import "fmt"
18

19
const ShiftStartInt64 byte = 0x20
20

21
// PrefixCoded is a byte array encoding of
22
// 64-bit numeric values shifted by 0-63 bits
23
type PrefixCoded []byte
24

25
func NewPrefixCodedInt64(in int64, shift uint) (PrefixCoded, error) {
1✔
26
        rv, _, err := NewPrefixCodedInt64Prealloc(in, shift, nil)
1✔
27
        return rv, err
1✔
28
}
1✔
29

30
func NewPrefixCodedInt64Prealloc(in int64, shift uint, prealloc []byte) (
31
        rv PrefixCoded, preallocRest []byte, err error) {
1✔
32
        if shift > 63 {
1✔
33
                return nil, prealloc, fmt.Errorf("cannot shift %d, must be between 0 and 63", shift)
×
34
        }
×
35

36
        nChars := ((63 - shift) / 7) + 1
1✔
37

1✔
38
        size := int(nChars + 1)
1✔
39
        if len(prealloc) >= size {
1✔
40
                rv = PrefixCoded(prealloc[0:size])
×
41
                preallocRest = prealloc[size:]
×
42
        } else {
1✔
43
                rv = make(PrefixCoded, size)
1✔
44
        }
1✔
45

46
        rv[0] = ShiftStartInt64 + byte(shift)
1✔
47

1✔
48
        sortableBits := int64(uint64(in) ^ 0x8000000000000000)
1✔
49
        sortableBits = int64(uint64(sortableBits) >> shift)
1✔
50
        for nChars > 0 {
2✔
51
                // Store 7 bits per byte for compatibility
1✔
52
                // with UTF-8 encoding of terms
1✔
53
                rv[nChars] = byte(sortableBits & 0x7f)
1✔
54
                nChars--
1✔
55
                sortableBits = int64(uint64(sortableBits) >> 7)
1✔
56
        }
1✔
57

58
        return rv, preallocRest, nil
1✔
59
}
60

61
func MustNewPrefixCodedInt64(in int64, shift uint) PrefixCoded {
×
62
        rv, err := NewPrefixCodedInt64(in, shift)
×
63
        if err != nil {
×
64
                panic(err)
×
65
        }
66
        return rv
×
67
}
68

NEW
69
func MustNewPrefixCodedInt64Prealloc(in int64, shift uint, prealloc []byte) PrefixCoded {
×
NEW
70
        rv, _, err := NewPrefixCodedInt64Prealloc(in, shift, prealloc)
×
NEW
71
        if err != nil {
×
NEW
72
                panic(err)
×
73
        }
NEW
74
        return rv
×
75
}
76

77
// Shift returns the number of bits shifted
78
// returns 0 if in uninitialized state
79
func (p PrefixCoded) Shift() (uint, error) {
1✔
80
        if len(p) > 0 {
2✔
81
                shift := p[0] - ShiftStartInt64
1✔
82
                if shift < 0 || shift < 63 {
2✔
83
                        return uint(shift), nil
1✔
84
                }
1✔
85
        }
86
        return 0, fmt.Errorf("invalid prefix coded value")
×
87
}
88

89
func (p PrefixCoded) Int64() (int64, error) {
1✔
90
        shift, err := p.Shift()
1✔
91
        if err != nil {
1✔
92
                return 0, err
×
93
        }
×
94
        var sortableBits int64
1✔
95
        for _, inbyte := range p[1:] {
2✔
96
                sortableBits <<= 7
1✔
97
                sortableBits |= int64(inbyte)
1✔
98
        }
1✔
99
        return int64(uint64((sortableBits << shift)) ^ 0x8000000000000000), nil
1✔
100
}
101

102
func ValidPrefixCodedTerm(p string) (bool, int) {
1✔
103
        return ValidPrefixCodedTermBytes([]byte(p))
1✔
104
}
1✔
105

106
func ValidPrefixCodedTermBytes(p []byte) (bool, int) {
1✔
107
        if len(p) > 0 {
2✔
108
                if p[0] < ShiftStartInt64 || p[0] > ShiftStartInt64+63 {
2✔
109
                        return false, 0
1✔
110
                }
1✔
111
                shift := p[0] - ShiftStartInt64
1✔
112
                nChars := ((63 - int(shift)) / 7) + 1
1✔
113
                if len(p) != nChars+1 {
2✔
114
                        return false, 0
1✔
115
                }
1✔
116
                return true, int(shift)
1✔
117
        }
118
        return false, 0
×
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc