Skip to content

Commit

Permalink
Make paths match tiles spec
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Jul 8, 2024
1 parent ce6baca commit ee81580
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
45 changes: 23 additions & 22 deletions api/layout/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,46 @@ package layout

import (
"fmt"
"path/filepath"
)

const (
// CheckpointPath is the location of the file containing the log checkpoint.
CheckpointPath = "checkpoint"
)

// EntriesPathForLogIndex() builds the local path at which the leaf with the given index lives in.
// EntriesPathForLogIndex builds the local path at which the leaf with the given index lives in.
// Note that this will be an entry bundle containing up to 256 entries and thus multiple
// indices can map to the same output path.
//
// TODO(mhutchinson): revisit to consider how partial tile suffixes should be added.
func EntriesPathForLogIndex(seq uint64) string {
seq = seq / 256
return EntriesPath(seq)
return EntriesPath(seq / 256)
}

func EntriesPath(bundleIndex uint64) string {
frag := []string{
"tile",
"entries",
fmt.Sprintf("x%03x", (bundleIndex>>16)&0xff),
fmt.Sprintf("x%03x", (bundleIndex>>8)&0xff),
fmt.Sprintf("%03x", bundleIndex&0xff),
}
return filepath.Join(frag...)
// EntriesPath returns the local path for the Nth entry bundle.
func EntriesPath(N uint64) string {
return fmt.Sprintf("tile/entries%s", fmtN(N))
}

// TilePath builds the path to the subtree tile with the given level and index in tile space.
//
// Note that NodeCoordsToTileAddress can be used to convert from node- to tile-space.
func TilePath(tileLevel, tileIndex uint64) string {
frag := []string{
"tile",
fmt.Sprintf("%d", tileLevel),
fmt.Sprintf("x%03x", (tileIndex>>16)&0xff),
fmt.Sprintf("x%03x", (tileIndex>>8)&0xff),
fmt.Sprintf("%03x", tileIndex&0xff),
return fmt.Sprintf("tile/%d%s", tileLevel, fmtN(tileIndex))
}

// fmtN returns the "N" part of a Tiles-spec path.
//
// N is grouped into chunks of 3 decimal digits, starting with the most significant digit, and
// padding with zeroes as necessary.
// Digit groups are prefixed with "x", except for the least-significant group which has no prefix,
// and separated with slashes.
//
// See https://github.com/C2SP/C2SP/blob/main/tlog-tiles.md#:~:text=index%201234067%20will%20be%20encoded%20as%20x001/x234/067
func fmtN(N uint64) string {
n := fmt.Sprintf("/%03d", N%1000)
N /= 1000
for N > 0 {
n = fmt.Sprintf("/x%03d%s", N%1000, n)
N /= 1000
}
return filepath.Join(frag...)
return n
}
42 changes: 23 additions & 19 deletions api/layout/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ func TestEntriesPathForLogIndex(t *testing.T) {
}{
{
seq: 0,
wantPath: "tile/entries/x000/x000/000",
wantPath: "tile/entries/000",
}, {
seq: 255,
wantPath: "tile/entries/x000/x000/000",
wantPath: "tile/entries/000",
}, {
seq: 256,
wantPath: "tile/entries/x000/x000/001",
wantPath: "tile/entries/001",
}, {
seq: 0xffeeddccbb,
wantPath: "tile/entries/x0ee/x0dd/0cc",
seq: 123456789 * 256,
wantPath: "tile/entries/x123/x456/789",
},
} {
desc := fmt.Sprintf("seq %d", test.seq)
Expand All @@ -50,26 +50,26 @@ func TestEntriesPathForLogIndex(t *testing.T) {

func TestEntriesPath(t *testing.T) {
for _, test := range []struct {
seq uint64
N uint64
wantPath string
}{
{
seq: 0,
wantPath: "tile/entries/x000/x000/000",
N: 0,
wantPath: "tile/entries/000",
}, {
seq: 255,
wantPath: "tile/entries/x000/x000/0ff",
N: 255,
wantPath: "tile/entries/255",
}, {
seq: 256,
wantPath: "tile/entries/x000/x001/000",
N: 256,
wantPath: "tile/entries/256",
}, {
seq: 0xdccbb,
wantPath: "tile/entries/x00d/x0cc/0bb",
N: 123456789000,
wantPath: "tile/entries/x123/x456/x789/000",
},
} {
desc := fmt.Sprintf("seq %d", test.seq)
desc := fmt.Sprintf("N %d", test.N)
t.Run(desc, func(t *testing.T) {
gotPath := EntriesPath(test.seq)
gotPath := EntriesPath(test.N)
if gotPath != test.wantPath {
t.Errorf("got file %q want %q", gotPath, test.wantPath)
}
Expand All @@ -86,11 +86,15 @@ func TestTilePath(t *testing.T) {
{
level: 0,
index: 0,
wantPath: "tile/0/x000/x000/000",
wantPath: "tile/0/000",
}, {
level: 15,
index: 455667,
wantPath: "tile/15/x455/667",
}, {
level: 15,
index: 0x455667,
wantPath: "tile/15/x045/x056/067",
index: 123456789,
wantPath: "tile/15/x123/x456/789",
},
} {
desc := fmt.Sprintf("level %x index %x", test.level, test.index)
Expand Down

0 comments on commit ee81580

Please sign in to comment.