Skip to content
Permalink
Browse files

Merge pull request #88 from jfontan/fix-metadata-dir

siva: fix placement of metadata files with bucket
  • Loading branch information...
jfontan committed Sep 9, 2019
2 parents d4f7dd5 + cc1aa7a commit 688eaa875ae552af2f87701f5e1eebb3aeb41adc
Showing with 40 additions and 13 deletions.
  1. +11 −4 siva/library.go
  2. +2 −1 siva/location.go
  3. +4 −5 siva/metadata.go
  4. +23 −3 siva/metadata_test.go
@@ -2,7 +2,6 @@ package siva

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
@@ -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)
@@ -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.
@@ -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
@@ -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,
}
}

@@ -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
@@ -3,6 +3,7 @@ package siva
import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/ghodss/yaml"
@@ -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")
@@ -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")
@@ -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) {

0 comments on commit 688eaa8

Please sign in to comment.
You can’t perform that action at this time.