Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/cas/mount/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ import (
"path/filepath"

"github.com/Skoowshot/qrix-runtime/internal/cas"
"github.com/Skoowshot/qrix-runtime/internal/cas/disk"
)

const standardDirPerms os.FileMode = 0755
const putTemporaryFilePattern = "put-*"

var _ cas.Mount = (*Disk)(nil)

// Disk is a physical disk implementation of cas.Mount, laying out files by given Layout at the given TargetPath
// Disk is a physical disk implementation of cas.Mount, laying out files by given DiskLayout at the given TargetPath
type Disk struct {
targetPath string
layout disk.Layout
layout DiskLayout
}

func NewDisk(
targetPath string,
layout disk.Layout,
layout DiskLayout,
) *Disk {
return &Disk{
targetPath: targetPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package disk
package mount

import (
"path/filepath"
Expand All @@ -8,18 +8,18 @@ import (

const shardingLevelSize = 2

// Layout defines how the contents are aligned inside of disk.Mount.
type Layout func(hash cas.Hash) string
// DiskLayout defines how the contents are aligned inside of disk.Mount.
type DiskLayout func(hash cas.Hash) string

// FlatLayout aligns the contents flat, all files in the same directory.
func FlatLayout() Layout {
// FlatDiskLayout aligns the contents flat, all files in the same directory.
func FlatDiskLayout() DiskLayout {
return func(hash cas.Hash) string {
return hash.HexString()
}
}

// ShardedLayout shards the contents by defined levels, files split into multiple directories.
func ShardedLayout(shardingLevels int) Layout {
// ShardedDiskLayout shards the contents by defined levels, files split into multiple directories.
func ShardedDiskLayout(shardingLevels int) DiskLayout {
return func(hash cas.Hash) string {
hexHash := hash.HexString()
if shardingLevels <= 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package disk
package mount

import (
"path/filepath"
Expand All @@ -13,13 +13,13 @@ func TestFlatLayout(t *testing.T) {

tests := []struct {
name string
layout Layout
layout DiskLayout
hash cas.Hash
want string
}{
{
name: "FlatLayout returns raw hex",
layout: FlatLayout(),
layout: FlatDiskLayout(),
hash: sampleHash,
want: hexStr,
},
Expand All @@ -41,31 +41,31 @@ func TestShardedLayout(t *testing.T) {

tests := []struct {
name string
layout Layout
layout DiskLayout
hash cas.Hash
want string
}{
{
name: "ShardedLayout 2 levels",
layout: ShardedLayout(2),
layout: ShardedDiskLayout(2),
hash: sampleHash,
want: filepath.Join("01", "02", hexStr),
},
{
name: "Zero hash keeps leading zeros",
layout: ShardedLayout(2),
layout: ShardedDiskLayout(2),
hash: cas.Hash([16]byte{}),
want: filepath.Join("00", "00", "00000000000000000000000000000000"),
},
{
name: "Zero or negative levels fallback to flat",
layout: ShardedLayout(0),
layout: ShardedDiskLayout(0),
hash: sampleHash,
want: hexStr,
},
{
name: "Prefix longer than hash length falls back to flat",
layout: ShardedLayout(20), // 40 > 32
layout: ShardedDiskLayout(20), // 40 > 32
hash: sampleHash,
want: hexStr,
},
Expand Down
7 changes: 3 additions & 4 deletions internal/cas/mount/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/Skoowshot/qrix-runtime/internal/cas"
"github.com/Skoowshot/qrix-runtime/internal/cas/disk"
)

func TestDisk_Get(t *testing.T) {
Expand Down Expand Up @@ -37,7 +36,7 @@ func TestDisk_Get(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempPath := t.TempDir()
mount := NewDisk(tempPath, disk.FlatLayout())
mount := NewDisk(tempPath, FlatDiskLayout())
if tt.setup != nil {
tt.setup(t, mount)
}
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestDisk_Has(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempPath := t.TempDir()
mount := NewDisk(tempPath, disk.FlatLayout())
mount := NewDisk(tempPath, FlatDiskLayout())
if tt.setup != nil {
tt.setup(t, mount)
}
Expand Down Expand Up @@ -134,7 +133,7 @@ func TestDisk_Put(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempPath := t.TempDir()
mount := NewDisk(tempPath, disk.FlatLayout())
mount := NewDisk(tempPath, FlatDiskLayout())
if tt.setup != nil {
tt.setup(t, mount)
}
Expand Down
Loading