fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

index: decide between tenant and non-tenant shard name in one place (#953)

We have two places with duplicated logic around how it decides the layout of
shards on disk. This now moves that decision into one place.

Additionally we can now unexport index.ShardName. It was only used in one
place outside the package, and that was easy to replace with a hardcoded
string since it is just a test.

Test Plan: Just CI. This has no actual change in functionality, just
refactoring.

+21 -13
+1 -1
cmd/zoekt-sourcegraph-indexserver/cleanup_test.go
··· 22 22 return shard{ 23 23 RepoID: fakeID(name), 24 24 RepoName: name, 25 - Path: index.ShardName("", name, 15, n), 25 + Path: fmt.Sprintf("%s_v%d.%05d.zoekt", name, 15, n), 26 26 ModTime: mtime, 27 27 RepoTombstone: false, 28 28 }
+1 -1
index/builder.go
··· 349 349 prefix = o.RepositoryDescription.Name 350 350 } 351 351 352 - return ShardName(o.IndexDir, prefix, version, n) 352 + return shardName(o.IndexDir, prefix, version, n) 353 353 } 354 354 355 355 type IndexState string
+9 -7
index/merge.go
··· 11 11 "sort" 12 12 13 13 "github.com/sourcegraph/zoekt" 14 - "github.com/sourcegraph/zoekt/internal/tenant" 15 14 ) 16 15 17 16 // Merge files into a compound shard in dstDir. Merge returns tmpName and a ··· 209 208 ibFunc(ib) 210 209 } 211 210 212 - prefix := "" 213 - if tenant.EnforceTenant() { 214 - prefix = tenant.SrcPrefix(ib.repoList[0].TenantID, ib.repoList[0].ID) 215 - } else { 216 - prefix = ib.repoList[0].Name 211 + opts := Options{ 212 + IndexDir: dstDir, 213 + RepositoryDescription: ib.repoList[0], 214 + 215 + // TODO we should remove these fields from Options and just rely on them 216 + // being set by RepositoryDescription. 217 + TenantID: ib.repoList[0].TenantID, 218 + RepoID: ib.repoList[0].ID, 217 219 } 218 220 219 - shardName := ShardName(dstDir, prefix, ib.indexFormatVersion, 0) 221 + shardName := opts.shardNameVersion(ib.indexFormatVersion, 0) 220 222 shardNameTmp := shardName + ".tmp" 221 223 shardNames[shardNameTmp] = shardName 222 224 return builderWriteAll(shardNameTmp, ib)
+7 -1
index/read_test.go
··· 368 368 t.Fatal(err) 369 369 } 370 370 371 - outName := ShardName("testdata/backcompat", "new", IndexFormatVersion, 0) 371 + opts := Options{ 372 + IndexDir: "testdata/backcompat", 373 + RepositoryDescription: zoekt.Repository{ 374 + Name: "new", 375 + }, 376 + } 377 + outName := opts.shardName(0) 372 378 t.Log("writing new file", outName) 373 379 374 380 err = os.WriteFile(outName, buf.Bytes(), 0o644)
+2 -2
index/shard_builder.go
··· 596 596 } 597 597 } 598 598 599 - // ShardName returns the name of the shard for the given prefix, version, and 599 + // shardName returns the name of the shard for the given prefix, version, and 600 600 // shard number. 601 - func ShardName(indexDir string, prefix string, version, n int) string { 601 + func shardName(indexDir string, prefix string, version, n int) string { 602 602 prefix = url.QueryEscape(prefix) 603 603 if len(prefix) > 200 { 604 604 prefix = prefix[:200] + hashString(prefix)[:8]
+1 -1
index/shard_builder_test.go
··· 40 40 41 41 for _, test := range tests { 42 42 t.Run(test.name, func(t *testing.T) { 43 - actual := ShardName(test.indexDir, test.prefix, test.version, test.shardNum) 43 + actual := shardName(test.indexDir, test.prefix, test.version, test.shardNum) 44 44 if actual != test.expected { 45 45 t.Errorf("expected %q, got %q", test.expected, actual) 46 46 }