Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing key handling in sort queries #140

Merged
merged 1 commit into from
Mar 14, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ func (s *Store) runQuerySort(source BucketSource, dataType interface{}, query *Q
}
}

tp := query.dataType

var keyType reflect.Type
var keyField string

for i := 0; i < tp.NumField(); i++ {
if strings.Contains(string(tp.Field(i).Tag), BoltholdKeyTag) {
keyType = tp.Field(i).Type
keyField = tp.Field(i).Name
break
}
}

// Run query without sort, skip or limit
// apply sort, skip and limit to entire dataset
qCopy := *query
Expand All @@ -154,6 +167,24 @@ func (s *Store) runQuerySort(source BucketSource, dataType interface{}, query *Q
var records []*record
err := s.runQuery(source, dataType, &qCopy, nil, 0,
func(r *record) error {
if keyType != nil {
var rowValue reflect.Value

// FIXME:
if tp.Kind() == reflect.Ptr {
rowValue = r.value
} else {
rowValue = r.value.Elem()
}
rowKey := rowValue
for rowKey.Kind() == reflect.Ptr {
rowKey = rowKey.Elem()
}
err := s.decode(r.key, rowKey.FieldByName(keyField).Addr().Interface())
if err != nil {
return err
}
}
records = append(records, r)

return nil
Expand Down
52 changes: 52 additions & 0 deletions sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ var sortTests = []test{
query: bolthold.Where("Category").Eq("animal").SortBy("Name").Skip(10),
result: []int{},
},
{
name: "Sort By Key",
query: bolthold.Where("Category").Eq("animal").SortBy("Key"),
result: []int{2, 5, 8, 9, 13, 14, 16},
},
{
name: "Sort By Key Reversed",
query: bolthold.Where("Category").Eq("animal").SortBy("Key").Reverse(),
result: []int{16, 14, 13, 9, 8, 5, 2},
},
}

func TestSortedFind(t *testing.T) {
Expand Down Expand Up @@ -259,3 +269,45 @@ func TestSortedFindWithNonSlicePtr(t *testing.T) {
_ = store.Find(result, bolthold.Where("Name").Eq("blah").SortBy("Name"))
})
}

func TestIssue139SortOnSequenceKey(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {

type Row struct {
ID uint64 `boltholdKey:"ID"`
}

ok(t, store.Insert(bolthold.NextSequence(), &Row{}))
ok(t, store.Insert(bolthold.NextSequence(), &Row{}))
ok(t, store.Insert(bolthold.NextSequence(), &Row{}))
ok(t, store.Insert(bolthold.NextSequence(), &Row{}))

var rows []*Row
ok(t, store.Find(&rows, (&bolthold.Query{}).SortBy("ID").Reverse()))
equals(t, uint64(4), rows[0].ID)
equals(t, uint64(3), rows[1].ID)
equals(t, uint64(2), rows[2].ID)
equals(t, uint64(1), rows[3].ID)

})

testWrap(t, func(store *bolthold.Store, t *testing.T) {

type Row struct {
ID *uint64 `boltholdKey:"ID"`
}

ok(t, store.Insert(bolthold.NextSequence(), &Row{}))
ok(t, store.Insert(bolthold.NextSequence(), &Row{}))
ok(t, store.Insert(bolthold.NextSequence(), &Row{}))
ok(t, store.Insert(bolthold.NextSequence(), &Row{}))

var rows []*Row
ok(t, store.Find(&rows, (&bolthold.Query{}).SortBy("ID").Reverse()))
equals(t, uint64(4), *rows[0].ID)
equals(t, uint64(3), *rows[1].ID)
equals(t, uint64(2), *rows[2].ID)
equals(t, uint64(1), *rows[3].ID)

})
}