Skip to content

Commit 9e99020

Browse files
committed
gitbase: use only one cache for all reposiories
Also fix a bug getting the cache size from cli. Signed-off-by: Javi Fontan <[email protected]>
1 parent 42ec285 commit 9e99020

12 files changed

+72
-30
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

99
- git libraries bare or non bare format is automatically detected ([#897](https://github.com/src-d/gitbase/pull/897))
10+
- Fix bug that created multiple object cache with incorrect size ([#898](https://github.com/src-d/gitbase/pull/898))
1011

1112
## [0.22.0-beta1] - 2019-06-20
1213

cmd/gitbase/command/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ func (c *Server) buildDatabase() error {
225225
)
226226
}
227227

228-
c.rootLibrary = libraries.New(libraries.Options{})
229-
c.pool = gitbase.NewRepositoryPool(c.CacheSize*cache.MiByte, c.rootLibrary)
228+
c.sharedCache = cache.NewObjectLRU(c.CacheSize * cache.MiByte)
230229

231-
c.sharedCache = cache.NewObjectLRU(512 * cache.MiByte)
230+
c.rootLibrary = libraries.New(libraries.Options{})
231+
c.pool = gitbase.NewRepositoryPool(c.sharedCache, c.rootLibrary)
232232

233233
if err := c.addDirectories(); err != nil {
234234
return err

cmd/gitbase/command/server_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
fixtures "github.com/src-d/go-git-fixtures"
1111
"github.com/stretchr/testify/require"
12+
"gopkg.in/src-d/go-git.v4/plumbing"
1213
)
1314

1415
func TestDirectories(t *testing.T) {
@@ -273,3 +274,48 @@ func bareTestName(d directory, err bool) string {
273274

274275
return fmt.Sprintf("%s_%s", d.Path, bare)
275276
}
277+
278+
func TestCache(t *testing.T) {
279+
require := require.New(t)
280+
281+
tmpDir, err := ioutil.TempDir("", "gitbase")
282+
require.NoError(err)
283+
func() {
284+
require.NoError(os.RemoveAll(tmpDir))
285+
}()
286+
287+
server := &Server{
288+
CacheSize: 512,
289+
Format: "siva",
290+
Bucket: 0,
291+
LogLevel: "debug",
292+
Directories: []string{"../../../_testdata"},
293+
IndexDir: tmpDir,
294+
}
295+
296+
err = server.buildDatabase()
297+
require.NoError(err)
298+
299+
cache := server.sharedCache
300+
pool := server.pool
301+
hash := plumbing.NewHash("dbfab055c70379219cbcf422f05316fdf4e1aed3")
302+
303+
_, ok := cache.Get(hash)
304+
require.False(ok)
305+
306+
repo, err := pool.GetRepo("015da2f4-6d89-7ec8-5ac9-a38329ea875b")
307+
require.NoError(err)
308+
309+
_, ok = repo.Cache().Get(hash)
310+
require.False(ok)
311+
require.Equal(cache, repo.Cache())
312+
313+
_, err = repo.CommitObject(hash)
314+
require.NoError(err)
315+
316+
_, ok = cache.Get(hash)
317+
require.True(ok)
318+
319+
_, ok = repo.Cache().Get(hash)
320+
require.True(ok)
321+
}

common_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func buildSession(
4949
lib, err := newMultiLibrary()
5050
require.NoError(err)
5151

52-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
52+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
5353
for _, fixture := range repos {
5454
path := fixture.Worktree().Root()
5555
ok, err := IsGitRepo(path)
@@ -173,7 +173,7 @@ func setupSivaCloseRepos(t *testing.T, dir string) (*sql.Context, *closedLibrary
173173
require.NoError(err)
174174

175175
closedLib := &closedLibrary{multiLibrary: lib}
176-
pool := NewRepositoryPool(cache.DefaultMaxSize, closedLib)
176+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), closedLib)
177177

178178
cwd, err := os.Getwd()
179179
require.NoError(err)
@@ -298,7 +298,7 @@ func newMultiPool() (*multiLibrary, *RepositoryPool, error) {
298298
return nil, nil, err
299299
}
300300

301-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
301+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
302302

303303
return lib, pool, err
304304
}

database_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestDatabase_Tables(t *testing.T) {
1515
require := require.New(t)
1616

1717
lib := libraries.New(libraries.Options{})
18-
db := getDB(t, testDBName, NewRepositoryPool(0, lib))
18+
db := getDB(t, testDBName, NewRepositoryPool(nil, lib))
1919

2020
tables := db.Tables()
2121
var tableNames []string
@@ -46,7 +46,7 @@ func TestDatabase_Name(t *testing.T) {
4646
require := require.New(t)
4747

4848
lib := libraries.New(libraries.Options{})
49-
db := getDB(t, testDBName, NewRepositoryPool(0, lib))
49+
db := getDB(t, testDBName, NewRepositoryPool(nil, lib))
5050
require.Equal(testDBName, db.Name())
5151
}
5252

integration_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestIntegration(t *testing.T) {
5151
require.NoError(t, err)
5252
lib.AddLocation(loc)
5353

54-
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
54+
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
5555
engine := newBaseEngine(pool)
5656

5757
testCases := []struct {
@@ -639,7 +639,7 @@ func TestMissingHeadRefs(t *testing.T) {
639639
})
640640
require.NoError(err)
641641

642-
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
642+
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
643643
engine := newBaseEngine(pool)
644644

645645
session := gitbase.NewSession(pool)
@@ -1013,7 +1013,7 @@ func setup(t testing.TB) (*sqle.Engine, *gitbase.RepositoryPool, func()) {
10131013
require.NoError(t, err)
10141014
lib.AddLocation(loc)
10151015

1016-
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
1016+
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
10171017

10181018
cleanup := func() {
10191019
require.NoError(t, fixtures.Clean())

internal/function/commit_stats_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func setupPool(t *testing.T) (*gitbase.RepositoryPool, func()) {
108108
require.NoError(t, err)
109109
lib.AddLocation(loc)
110110

111-
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
111+
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
112112

113113
return pool, cleanup
114114
}

internal/function/uast_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,6 @@ func bblfshFixtures(
423423
func setup(t *testing.T) (*sql.Context, func()) {
424424
t.Helper()
425425

426-
// pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
427-
// for _, f := range fixtures.ByTag("worktree") {
428-
// pool.AddGit(f.Worktree().Root())
429-
// }
430-
431426
// create library directory and move repo inside
432427

433428
path := fixtures.ByTag("worktree").One().Worktree().Root()
@@ -445,7 +440,7 @@ func setup(t *testing.T) (*sql.Context, func()) {
445440
require.NoError(t, err)
446441
lib.AddLocation(loc)
447442

448-
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
443+
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
449444

450445
session := gitbase.NewSession(pool)
451446
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))

internal/rule/squashjoins_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestAnalyzeSquashJoinsExchange(t *testing.T) {
2222

2323
catalog := sql.NewCatalog()
2424
catalog.AddDatabase(
25-
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(0, lib)),
25+
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(nil, lib)),
2626
)
2727
a := analyzer.NewBuilder(catalog).
2828
WithParallelism(2).
@@ -61,7 +61,7 @@ func TestAnalyzeSquashNaturalJoins(t *testing.T) {
6161

6262
catalog := sql.NewCatalog()
6363
catalog.AddDatabase(
64-
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(0, lib)),
64+
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(nil, lib)),
6565
)
6666
a := analyzer.NewBuilder(catalog).
6767
WithParallelism(2).
@@ -2289,7 +2289,7 @@ func TestIsJoinLeafSquashable(t *testing.T) {
22892289

22902290
func TestOrderedTableNames(t *testing.T) {
22912291
lib := libraries.New(libraries.Options{})
2292-
tables := gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(0, lib)).Tables()
2292+
tables := gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(nil, lib)).Tables()
22932293

22942294
input := []sql.Table{
22952295
tables[gitbase.BlobsTableName],
@@ -2831,5 +2831,5 @@ func (l dummyLookup) Indexes() []string {
28312831

28322832
func gitbaseTables() map[string]sql.Table {
28332833
lib := libraries.New(libraries.Options{})
2834-
return gitbase.NewDatabase("", gitbase.NewRepositoryPool(0, lib)).Tables()
2834+
return gitbase.NewDatabase("", gitbase.NewRepositoryPool(nil, lib)).Tables()
28352835
}

repositories_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRepositoriesTable(t *testing.T) {
2222
lib, err := newMultiLibrary()
2323
require.NoError(err)
2424

25-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
25+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
2626

2727
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
2828
for _, id := range repoIDs {

repository_pool.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ type RepositoryPool struct {
7474
library borges.Library
7575
}
7676

77-
// NewRepositoryPool initializes a new RepositoryPool with LRU cache.
77+
// NewRepositoryPool holds a repository library and a shared object cache.
7878
func NewRepositoryPool(
79-
maxCacheSize cache.FileSize,
79+
c cache.Object,
8080
lib borges.Library,
8181
) *RepositoryPool {
8282
return &RepositoryPool{
83-
cache: cache.NewObjectLRU(maxCacheSize),
83+
cache: c,
8484
library: lib,
8585
}
8686
}

repository_pool_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestRepositoryPoolBasic(t *testing.T) {
2020
lib, err := newMultiLibrary()
2121
require.NoError(err)
2222

23-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
23+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
2424

2525
iter, err := pool.RepoIter()
2626
require.NoError(err)
@@ -66,7 +66,7 @@ func TestRepositoryPoolGit(t *testing.T) {
6666
lib, err := newMultiLibrary()
6767
require.NoError(err)
6868

69-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
69+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
7070

7171
require.NoError(lib.AddPlain(path, path, nil))
7272

@@ -107,7 +107,7 @@ func TestRepositoryPoolIterator(t *testing.T) {
107107
lib, err := newMultiLibrary()
108108
require.NoError(err)
109109

110-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
110+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
111111
err = lib.AddPlain("0", path, nil)
112112
require.NoError(err)
113113
err = lib.AddPlain("1", path, nil)
@@ -145,7 +145,7 @@ func TestRepositoryPoolSiva(t *testing.T) {
145145
cwd, err := os.Getwd()
146146
require.NoError(err)
147147

148-
pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
148+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
149149
path := filepath.Join(cwd, "_testdata")
150150

151151
require.NoError(

0 commit comments

Comments
 (0)