Skip to content

Commit

Permalink
siva: fix placement of metadata files with bucket
Browse files Browse the repository at this point in the history
Location metadata files were placed in the library root path instead in
the siva directory.

    cf/cf2e799463e1a00dbd1addd2003b0c7db31dbfe2.siva
    cf2e799463e1a00dbd1addd2003b0c7db31dbfe2.yaml

instead of:

    cf/cf2e799463e1a00dbd1addd2003b0c7db31dbfe2.siva
    cf/cf2e799463e1a00dbd1addd2003b0c7db31dbfe2.yaml

Signed-off-by: Javi Fontan <jfontan@gmail.com>
  • Loading branch information
jfontan committed Sep 9, 2019
1 parent d4f7dd5 commit cc1aa7a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
15 changes: 11 additions & 4 deletions siva/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package siva

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -271,9 +270,17 @@ func (l *Library) location(id borges.LocationID, create bool) (borges.Location,
}

func buildSivaPath(id borges.LocationID, bucket int) string {
siva := fmt.Sprintf("%s.siva", id)
return buildPath(id, bucket, ".siva")
}

func buildSivaMetadataPath(id borges.LocationID, bucket int) string {
return buildPath(id, bucket, locMetadataFileExt)
}

func buildPath(id borges.LocationID, bucket int, suffix string) string {
name := string(id) + suffix
if bucket == 0 {
return siva
return name
}

r := []rune(id)
Expand All @@ -284,7 +291,7 @@ func buildSivaPath(id borges.LocationID, bucket int) string {
bucketDir = string(r[:bucket])
}

return filepath.Join(bucketDir, siva)
return filepath.Join(bucketDir, name)
}

// Locations implements borges.Library interface.
Expand Down
3 changes: 2 additions & 1 deletion siva/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func newLocation(
var metadata *locationMetadata
if lib.metadata != nil {
var err error
metadata, err = loadOrCreateLocationMetadata(lib.fs, string(id))
mPath := buildSivaMetadataPath(id, lib.options.Bucket)
metadata, err = loadOrCreateLocationMetadata(lib.fs, mPath)
if err != nil {
// TODO: skip metadata if corrupted? log a warning?
return nil, err
Expand Down
9 changes: 4 additions & 5 deletions siva/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ type locationMetadata struct {
const locMetadataFileExt = ".yaml"

func newLocationMetadata(
id string,
fs billy.Filesystem,
path string,
) *locationMetadata {
return &locationMetadata{
Versions: make(map[int]*Version),
fs: fs,
path: id + locMetadataFileExt,
path: path,
}
}

Expand All @@ -241,12 +241,11 @@ func parseLocationMetadata(d []byte) (*locationMetadata, error) {

func loadOrCreateLocationMetadata(
fs billy.Filesystem,
id string,
path string,
) (*locationMetadata, error) {
path := id + locMetadataFileExt
m, err := loadLocationMetadata(fs, path)
if os.IsNotExist(err) {
return newLocationMetadata(id, fs), nil
return newLocationMetadata(fs, path), nil
}

return m, err
Expand Down
26 changes: 23 additions & 3 deletions siva/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package siva
import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -239,10 +240,21 @@ func TestMetadataLibraryWrite(t *testing.T) {
}

func TestMetadataLocationWrite(t *testing.T) {
for _, bucket := range []int{0, 2} {
t.Run(fmt.Sprintf("bucket %v", bucket), func(s *testing.T) {
testMetadataLocationWrite(s, bucket)
})
}
}

func testMetadataLocationWrite(t *testing.T, bucket int) {
t.Helper()
require := require.New(t)
fs, _ := setupFS(t, "../_testdata/rooted", true, 0)
fs, _ := setupFS(t, "../_testdata/rooted", true, bucket)

lib, err := NewLibrary("test", fs, &LibraryOptions{})
lib, err := NewLibrary("test", fs, &LibraryOptions{
Bucket: bucket,
})
require.NoError(err)

loc, err := lib.Location("cf2e799463e1a00dbd1addd2003b0c7db31dbfe2")
Expand Down Expand Up @@ -303,7 +315,9 @@ func TestMetadataLocationWrite(t *testing.T) {

// Reopen library and check versions

lib, err = NewLibrary("test", fs, &LibraryOptions{})
lib, err = NewLibrary("test", fs, &LibraryOptions{
Bucket: bucket,
})
require.NoError(err)

loc, err = lib.Location("cf2e799463e1a00dbd1addd2003b0c7db31dbfe2")
Expand Down Expand Up @@ -336,6 +350,12 @@ func TestMetadataLocationWrite(t *testing.T) {
"gitserver.com/a",
"gitserver.com/b",
}, repos)

// Check that the metadata file is in the correct place

metadata := strings.TrimSuffix(l.path, ".siva") + ".yaml"
_, err = fs.Stat(metadata)
require.NoError(err, "metadata does not exist in the correct path")
}

func TestMetadataVersionOnCommit(t *testing.T) {
Expand Down

0 comments on commit cc1aa7a

Please sign in to comment.