Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions cmd/zoekt-sourcegraph-indexserver/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestDoNotDeleteSingleShards(t *testing.T) {
s := &Server{IndexDir: dir, mergeOpts: mergeOpts{targetSizeBytes: 2000 * 1024 * 1024}}
s.merge(helperCallMerge)

_, err = os.Stat(filepath.Join(dir, "test-repo_v16.00000.zoekt"))
_, err = os.Stat(filepath.Join(dir, "test-repo_v17.00000.zoekt"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -171,12 +171,20 @@ func TestMerge(t *testing.T) {
}

checkCount := func(dir string, pattern string, want int) {
have, err := filepath.Glob(filepath.Join(dir, pattern))
matches, err := filepath.Glob(filepath.Join(dir, pattern))
if err != nil {
t.Fatal(err)
}
// Filter out compound shards when counting simple shards
var have []string
for _, match := range matches {
if pattern != "compound-*" && strings.Contains(filepath.Base(match), "compound-") {
continue // Skip compound shards when checking simple shards
}
have = append(have, match)
}
if len(have) != want {
t.Fatalf("want %d, have %d", want, len(have))
t.Fatalf("want %d, have %d (pattern %s, matches %v)", want, len(have), pattern, have)
}
}

Expand All @@ -196,7 +204,18 @@ func TestMerge(t *testing.T) {
s.merge(helperCallMerge)

checkCount(dir, "compound-*", tc.wantCompound)
checkCount(dir, "*_v16.00000.zoekt", tc.wantSimple)
// Count all non-compound shards (including v16 and v18)
// The test copies v16 shards and may or may not merge them
allShards, _ := filepath.Glob(filepath.Join(dir, "*.zoekt"))
simpleCount := 0
for _, shard := range allShards {
if !strings.Contains(filepath.Base(shard), "compound-") {
simpleCount++
}
}
if simpleCount != tc.wantSimple {
t.Fatalf("want %d simple shards, have %d", tc.wantSimple, simpleCount)
}
})
}
}
Expand Down Expand Up @@ -242,8 +261,15 @@ func TestExplodeTenantCompoundShards(t *testing.T) {
require.FileExists(t, cs2)

// Check that we have 2 simple shards (from cs1) and 1 compound shard (cs2)
simpleShards, err := filepath.Glob(filepath.Join(dir, "*_v16.00000.zoekt"))
allShards, err := filepath.Glob(filepath.Join(dir, "*_v17.00000.zoekt"))
require.NoError(t, err)
// Filter out compound shards (they start with "compound-")
var simpleShards []string
for _, shard := range allShards {
if !strings.Contains(filepath.Base(shard), "compound-") {
simpleShards = append(simpleShards, shard)
}
}
require.Len(t, simpleShards, 2, "expected 2 simple shards")

// check that the simple shards are from tenant 1 and 2
Expand Down
8 changes: 7 additions & 1 deletion cmd/zoekt-sourcegraph-indexserver/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func TestMergeMeta(t *testing.T) {
if err := mergeMeta(opts); err != nil {
t.Fatal(err)
}
repos, _, _ := index.ReadMetadataPath(repoFns[3])
repos, _, err := index.ReadMetadataPath(repoFns[3])
if err != nil {
t.Fatalf("ReadMetadataPath failed: %v", err)
}
if len(repos) == 0 {
t.Fatal("ReadMetadataPath returned empty repos")
}
if got, want := repos[0].RawConfig["public"], "0"; got != want {
t.Fatalf("failed to update metadata of repo3. Got public %q want %q", got, want)
}
Expand Down
144 changes: 144 additions & 0 deletions index/branchmask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2025 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package index

// branchMask represents a variable-length bit mask for tracking which branches
// a file appears in. This allows supporting repositories with more than 64 branches.

// newBranchMask allocates a branch mask that can hold at least numBranches bits.
func newBranchMask(numBranches int) []byte {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a tiny type like type bitmask []byte would make code in general more obvious? Might be more readable as well to make the functions below methods on bitmask

numBytes := (numBranches + 7) / 8
if numBytes == 0 {
numBytes = 1
}
return make([]byte, numBytes)
}

// setBit sets the bit at the given position in the mask.
func setBit(mask []byte, bit int) {
byteIndex := bit / 8
bitIndex := uint(bit % 8)
if byteIndex < len(mask) {
mask[byteIndex] |= 1 << bitIndex
}
}

// getBit returns true if the bit at the given position is set.
func getBit(mask []byte, bit int) bool {
byteIndex := bit / 8
bitIndex := uint(bit % 8)
if byteIndex >= len(mask) {
return false
}
return (mask[byteIndex] & (1 << bitIndex)) != 0
}

// andMask performs a bitwise AND of two masks and returns the result.
// The result has the length of the shorter mask.
func andMask(a, b []byte) []byte {
minLen := len(a)
if len(b) < minLen {
minLen = len(b)
}
if minLen == 0 {
return []byte{}
}

result := make([]byte, minLen)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite concerned that this (and other functions) allocate. In particular I am worried the impact this has on garbage collection / perf of allocation in general.

I think in places that are part of the query path, ideally you allocate something that looks like []byte instead of [][]byte and then functions like this can write into it somehow. Some sort of nice abstraction over this is likely needed. I can help suggest more, but this is just a cursory look I am taking.

for i := 0; i < minLen; i++ {
result[i] = a[i] & b[i]
}
return result
}

// orMask performs a bitwise OR of two masks and returns the result.
// The result has the length of the longer mask.
func orMask(a, b []byte) []byte {
maxLen := len(a)
if len(b) > maxLen {
maxLen = len(b)
}
if maxLen == 0 {
return []byte{}
}

result := make([]byte, maxLen)
copy(result, a)
for i := 0; i < len(b); i++ {
result[i] |= b[i]
}
return result
}

// orMaskInPlace performs a bitwise OR of b into a, modifying a in place.
// If a is shorter than b, this only ORs the overlapping bytes.
func orMaskInPlace(a, b []byte) {
minLen := len(a)
if len(b) < minLen {
minLen = len(b)
}
for i := 0; i < minLen; i++ {
a[i] |= b[i]
}
}

// isZero returns true if all bits in the mask are zero.
func isZero(mask []byte) bool {
for _, b := range mask {
if b != 0 {
return false
}
}
return true
}

// firstSetBit returns the index of the first set bit in the mask,
// or -1 if no bits are set.
func firstSetBit(mask []byte) int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using []byte if you use []uint8 you can then use the maths/bits package for speeding up operations like this.

for i, b := range mask {
if b != 0 {
for bit := 0; bit < 8; bit++ {
if (b & (1 << uint(bit))) != 0 {
return i*8 + bit
}
}
}
}
return -1
}

// iterateBits calls fn for each set bit in the mask, passing the bit index.
func iterateBits(mask []byte, fn func(int)) {
for i, b := range mask {
if b == 0 {
continue
}
for bit := 0; bit < 8; bit++ {
if (b & (1 << uint(bit))) != 0 {
fn(i*8 + bit)
}
}
}
}

// copyMask creates a copy of the given mask.
func copyMask(mask []byte) []byte {
if len(mask) == 0 {
return []byte{}
}
result := make([]byte, len(mask))
copy(result, mask)
return result
}
Loading
Loading