Skip to content

Commit

Permalink
gitbase: use only one cache for all reposiories
Browse files Browse the repository at this point in the history
Also fix a bug getting the cache size from cli.

Signed-off-by: Javi Fontan <jfontan@gmail.com>
  • Loading branch information
jfontan committed Jun 21, 2019
1 parent 35d6bc7 commit 7c26f69
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 30 deletions.
6 changes: 3 additions & 3 deletions cmd/gitbase/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ func (c *Server) buildDatabase() error {
)
}

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

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

if err := c.addDirectories(); err != nil {
return err
Expand Down
49 changes: 49 additions & 0 deletions cmd/gitbase/command/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package command
import (
"testing"

"io/ioutil"
"os"

"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-git.v4/plumbing"
)

func TestDirectories(t *testing.T) {
Expand Down Expand Up @@ -147,3 +151,48 @@ func TestDirectories(t *testing.T) {
})
}
}

func TestCache(t *testing.T) {
require := require.New(t)

tmpDir, err := ioutil.TempDir("", "gitbase")
require.NoError(err)
func() {
require.NoError(os.RemoveAll(tmpDir))
}()

server := &Server{
CacheSize: 512,
Format: "siva",
Bucket: 0,
LogLevel: "debug",
Directories: []string{"../../../_testdata"},
IndexDir: tmpDir,
}

err = server.buildDatabase()
require.NoError(err)

cache := server.sharedCache
pool := server.pool
hash := plumbing.NewHash("dbfab055c70379219cbcf422f05316fdf4e1aed3")

_, ok := cache.Get(hash)
require.False(ok)

repo, err := pool.GetRepo("015da2f4-6d89-7ec8-5ac9-a38329ea875b")
require.NoError(err)

_, ok = repo.Cache().Get(hash)
require.False(ok)
require.Equal(cache, repo.Cache())

_, err = repo.CommitObject(hash)
require.NoError(err)

_, ok = cache.Get(hash)
require.True(ok)

_, ok = repo.Cache().Get(hash)
require.True(ok)
}
6 changes: 3 additions & 3 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func buildSession(
lib, err := newMultiLibrary()
require.NoError(err)

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

closedLib := &closedLibrary{multiLibrary: lib}
pool := NewRepositoryPool(cache.DefaultMaxSize, closedLib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), closedLib)

cwd, err := os.Getwd()
require.NoError(err)
Expand Down Expand Up @@ -298,7 +298,7 @@ func newMultiPool() (*multiLibrary, *RepositoryPool, error) {
return nil, nil, err
}

pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

return lib, pool, err
}
Expand Down
4 changes: 2 additions & 2 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDatabase_Tables(t *testing.T) {
require := require.New(t)

lib := libraries.New(libraries.Options{})
db := getDB(t, testDBName, NewRepositoryPool(0, lib))
db := getDB(t, testDBName, NewRepositoryPool(nil, lib))

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

lib := libraries.New(libraries.Options{})
db := getDB(t, testDBName, NewRepositoryPool(0, lib))
db := getDB(t, testDBName, NewRepositoryPool(nil, lib))
require.Equal(testDBName, db.Name())
}

Expand Down
6 changes: 3 additions & 3 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestIntegration(t *testing.T) {
require.NoError(t, err)
lib.AddLocation(loc)

pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
engine := newBaseEngine(pool)

testCases := []struct {
Expand Down Expand Up @@ -639,7 +639,7 @@ func TestMissingHeadRefs(t *testing.T) {
})
require.NoError(err)

pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
engine := newBaseEngine(pool)

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

pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

cleanup := func() {
require.NoError(t, fixtures.Clean())
Expand Down
2 changes: 1 addition & 1 deletion internal/function/commit_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func setupPool(t *testing.T) (*gitbase.RepositoryPool, func()) {
require.NoError(t, err)
lib.AddLocation(loc)

pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

return pool, cleanup
}
7 changes: 1 addition & 6 deletions internal/function/uast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,6 @@ func bblfshFixtures(
func setup(t *testing.T) (*sql.Context, func()) {
t.Helper()

// pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
// for _, f := range fixtures.ByTag("worktree") {
// pool.AddGit(f.Worktree().Root())
// }

// create library directory and move repo inside

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

pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := gitbase.NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

session := gitbase.NewSession(pool)
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
Expand Down
8 changes: 4 additions & 4 deletions internal/rule/squashjoins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAnalyzeSquashJoinsExchange(t *testing.T) {

catalog := sql.NewCatalog()
catalog.AddDatabase(
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(0, lib)),
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(nil, lib)),
)
a := analyzer.NewBuilder(catalog).
WithParallelism(2).
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestAnalyzeSquashNaturalJoins(t *testing.T) {

catalog := sql.NewCatalog()
catalog.AddDatabase(
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(0, lib)),
gitbase.NewDatabase("foo", gitbase.NewRepositoryPool(nil, lib)),
)
a := analyzer.NewBuilder(catalog).
WithParallelism(2).
Expand Down Expand Up @@ -2289,7 +2289,7 @@ func TestIsJoinLeafSquashable(t *testing.T) {

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

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

func gitbaseTables() map[string]sql.Table {
lib := libraries.New(libraries.Options{})
return gitbase.NewDatabase("", gitbase.NewRepositoryPool(0, lib)).Tables()
return gitbase.NewDatabase("", gitbase.NewRepositoryPool(nil, lib)).Tables()
}
2 changes: 1 addition & 1 deletion repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestRepositoriesTable(t *testing.T) {
lib, err := newMultiLibrary()
require.NoError(err)

pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
for _, id := range repoIDs {
Expand Down
6 changes: 3 additions & 3 deletions repository_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ type RepositoryPool struct {
library borges.Library
}

// NewRepositoryPool initializes a new RepositoryPool with LRU cache.
// NewRepositoryPool holds a repository library and a shared object cache.
func NewRepositoryPool(
maxCacheSize cache.FileSize,
c cache.Object,
lib borges.Library,
) *RepositoryPool {
return &RepositoryPool{
cache: cache.NewObjectLRU(maxCacheSize),
cache: c,
library: lib,
}
}
Expand Down
8 changes: 4 additions & 4 deletions repository_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRepositoryPoolBasic(t *testing.T) {
lib, err := newMultiLibrary()
require.NoError(err)

pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

iter, err := pool.RepoIter()
require.NoError(err)
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestRepositoryPoolGit(t *testing.T) {
lib, err := newMultiLibrary()
require.NoError(err)

pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)

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

Expand Down Expand Up @@ -107,7 +107,7 @@ func TestRepositoryPoolIterator(t *testing.T) {
lib, err := newMultiLibrary()
require.NoError(err)

pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
err = lib.AddPlain("0", path, nil)
require.NoError(err)
err = lib.AddPlain("1", path, nil)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestRepositoryPoolSiva(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(err)

pool := NewRepositoryPool(cache.DefaultMaxSize, lib)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
path := filepath.Join(cwd, "_testdata")

require.NoError(
Expand Down

0 comments on commit 7c26f69

Please sign in to comment.