Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Add support for DESC clustering order in the mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxWaterfall committed Jul 27, 2022
1 parent a99361d commit c3fd40d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ func (t gocqlTypeInfo) Custom() string {
}

type keyPart struct {
Key string
Value interface{}
Key string
Value interface{}
ClusteringOrder ColumnDirection
}

func (k *keyPart) Bytes() []byte {
Expand All @@ -271,6 +272,9 @@ func (k key) Less(other key) bool {
if cmp == 0 {
continue
}
if k[i].ClusteringOrder { // desc
return cmp > 0
}
return cmp < 0
}

Expand All @@ -294,7 +298,7 @@ func (k key) ToSuperColumn() *superColumn {
func (k key) Append(column string, value interface{}) key {
newKey := make([]keyPart, len(k)+1)
copy(newKey, k)
newKey[len(k)] = keyPart{column, value}
newKey[len(k)] = keyPart{Key: column, Value: value}
return newKey
}

Expand Down Expand Up @@ -364,6 +368,16 @@ func (t *MockTable) getOrCreateColumnGroup(rowKey, superColumnKey key) map[strin
row := t.getOrCreateRow(rowKey)
scol := superColumnKey.ToSuperColumn()

// Retrieve the clustering order from the table options
// and assign to the key parts to set the sort ordering on each column
keyOrder := make(map[string]ColumnDirection, 0)
for _, v := range t.options.ClusteringOrder {
keyOrder[v.Column] = v.Direction
}
for i, kp := range scol.Key {
scol.Key[i].ClusteringOrder = keyOrder[kp.Key]
}

if row.Has(scol) {
return row.Get(scol).(*superColumn).Columns
}
Expand Down

0 comments on commit c3fd40d

Please sign in to comment.