Skip to content

Commit

Permalink
rowutil.ToVersionedRows: Inner slices are chronological, outer one ca…
Browse files Browse the repository at this point in the history
…n be either (sorted with given rowLess func)
  • Loading branch information
elimisteve committed Jan 8, 2017
1 parent b020738 commit fe29018
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions rowutil/version.go
Expand Up @@ -18,8 +18,8 @@ func ToVersionedRows(origRows types.Rows, rowLess func(r1, r2 *types.Row) bool)
rows := make(types.Rows, len(origRows))
copy(rows, origRows)

// Sort ~original
rows.Sort(rowLess)
// Every inner slice will remain in chronological order (ascending)
rows.Sort(ByTagPrefix("created:", true))

// 2D return value
var rrows []types.Rows
Expand All @@ -46,21 +46,23 @@ func ToVersionedRows(origRows types.Rows, rowLess func(r1, r2 *types.Row) bool)

// Each version slice is now individually ordered, but we need to
// return them in a [][]*Row. What should the ordering of this
// slice of slices be? Answer: ordered by the first member of each
// version slice.
// slice of slices be? Answer: ordered by the last member of each
// version slice (in either ascending or descending order, as
// determined by rowLess).

firsts := make(types.Rows, 0, len(mRows))
firstToRows := make(map[*types.Row]types.Rows, len(firsts))
lasts := make(types.Rows, 0, len(mRows))
lastToRows := make(map[*types.Row]types.Rows, len(lasts))

for _, rowGroup := range mRows {
firsts = append(firsts, rowGroup[0])
// Map the first element of each slice to its containing slice
firstToRows[rowGroup[0]] = rowGroup
last := rowGroup[len(rowGroup)-1]
lasts = append(lasts, last)
// Map the last element of each slice to its containing slice
lastToRows[last] = rowGroup
}
firsts.Sort(rowLess)
lasts.Sort(rowLess)

for _, fr := range firsts {
rrows = append(rrows, firstToRows[fr])
for _, fr := range lasts {
rrows = append(rrows, lastToRows[fr])
}

return rrows
Expand Down
6 changes: 3 additions & 3 deletions rowutil/version_test.go
Expand Up @@ -30,8 +30,8 @@ func TestToVersionedRows(t *testing.T) {

orig0 := types.Rows{r0, r1, r2, r3, r4, r5, r6, r7}
want0 := []types.Rows{
types.Rows{r0, r1, r3, r4},
types.Rows{r2},
types.Rows{r0, r1, r3, r4},
types.Rows{r5},
types.Rows{r6, r7},
}
Expand All @@ -53,9 +53,9 @@ func TestToVersionedRows(t *testing.T) {
// Descending

want0 = []types.Rows{
types.Rows{r7, r6},
types.Rows{r6, r7},
types.Rows{r5},
types.Rows{r4, r3, r1, r0},
types.Rows{r0, r1, r3, r4},
types.Rows{r2},
}
want1 = want0
Expand Down

0 comments on commit fe29018

Please sign in to comment.