Skip to content
Snippets Groups Projects
Unverified Commit c51f9208 authored by Ashwin Ramesh's avatar Ashwin Ramesh
Browse files

Ensure DestUids is sorted

parent a61cafcf
No related branches found
No related tags found
No related merge requests found
language: go
go:
- 1.7
- 1.8
dist: trusty
sudo: required
notifications:
......
......@@ -27,6 +27,28 @@ type WriteIterator struct {
}
// AsList implements sort.Interface by for block lists
type AsList struct{ l *task.List }
func (s AsList) Len() int { return ListLen(s.l) }
func (s AsList) Swap(i, j int) {
p, q := ridx(s.l, i)
m, n := ridx(s.l, j)
s.l.Blocks[p].List[q], s.l.Blocks[m].List[n] = s.l.Blocks[m].List[n], s.l.Blocks[p].List[q]
}
func (s AsList) Less(i, j int) bool {
p, q := ridx(s.l, i)
m, n := ridx(s.l, j)
return s.l.Blocks[p].List[q] < s.l.Blocks[m].List[n]
}
func Sort(ul *task.List) {
sort.Sort(AsList{ul})
for _, it := range ul.Blocks {
it.MaxInt = it.List[len(it.List)-1]
}
}
func NewWriteIterator(l *task.List, whence int) WriteIterator {
blen := len(l.Blocks)
var cur *task.Block
......
......@@ -27,6 +27,30 @@ func newList(data []uint64) *task.List {
return SortedListToBlock(data)
}
func TestSort1(t *testing.T) {
input := newList([]uint64{55})
Sort(input)
require.Equal(t, BlockToList(input), []uint64{55})
}
func TestSort2(t *testing.T) {
input := newList([]uint64{})
Sort(input)
require.Equal(t, BlockToList(input), []uint64{})
}
func TestSort3(t *testing.T) {
input := newList([]uint64{55, 392, 1, 123})
Sort(input)
require.Equal(t, BlockToList(input), []uint64{1, 55, 123, 392})
}
func TestSort4(t *testing.T) {
input := newList([]uint64{55, 1, 100000})
Sort(input)
require.Equal(t, BlockToList(input), []uint64{1, 55, 100000})
}
func TestMergeSorted1(t *testing.T) {
input := []*task.List{
newList([]uint64{55}),
......
......@@ -282,7 +282,6 @@ func (sg *SubGraph) preTraverse(uid uint64, dst, parent outputNode) error {
continue
}
uc := dst.New(fieldName)
if rerr := pc.preTraverse(childUID, uc, dst); rerr != nil {
if rerr.Error() == "_INV_" {
invalidUids[childUID] = true
......@@ -638,13 +637,18 @@ func newGraph(ctx context.Context, gq *gql.GraphQuery) (*SubGraph, error) {
}
if len(gq.UID) > 0 {
o := new(task.List)
it := algo.NewWriteIterator(o, 0)
sg.SrcUIDs = new(task.List)
ito := algo.NewWriteIterator(o, 0)
itsg := algo.NewWriteIterator(sg.SrcUIDs, 0)
for _, uid := range gq.UID {
it.Append(uid)
ito.Append(uid)
itsg.Append(uid)
}
it.End()
sg.SrcUIDs = o
ito.End()
itsg.End()
sg.uidMatrix = []*task.List{o}
// User specified list may not be sorted.
algo.Sort(sg.SrcUIDs)
}
sg.values = createNilValuesList(1)
// Copy roots filter.
......@@ -903,7 +907,17 @@ func (sg *SubGraph) fillVars(mp map[string]*task.List) {
func ProcessGraph(ctx context.Context, sg, parent *SubGraph, rch chan error) {
var err error
if len(sg.Params.NeedsVar) != 0 && len(sg.SrcFunc) == 0 {
// Retain the actual order in uidMatrix. But sort the destUids.
sg.uidMatrix = []*task.List{sg.DestUIDs}
it := algo.NewListIterator(sg.DestUIDs)
var o task.List
wit := algo.NewWriteIterator(&o, 0)
for ; it.Valid(); it.Next() {
wit.Append(it.Val())
}
wit.End()
algo.Sort(&o)
sg.DestUIDs = &o
} else if len(sg.Attr) == 0 {
// If we have a filter SubGraph which only contains an operator,
// it won't have any attribute to work on.
......
......@@ -470,6 +470,24 @@ func TestShortestPath(t *testing.T) {
js)
}
func TestShortestPathRev(t *testing.T) {
populateGraph(t)
query := `
{
A as shortest(from:23, to:1) {
friend
}
me(var: A) {
name
}
}`
js := processToFastJSON(t, query)
require.JSONEq(t,
`{"me":[{"name":"Rick Grimes"},{"name":"Michonne"}]}`,
js)
}
func TestShortestPathWeightsMultiFacet_Error(t *testing.T) {
populateGraph(t)
query := `
......@@ -2816,6 +2834,17 @@ func TestRootList1(t *testing.T) {
require.JSONEq(t, `{"me":[{"name":"Michonne"},{"name":"Rick Grimes"},{"name":"Glenn Rhee"},{"name":"Alice"}]}`, js)
}
func TestRootList2(t *testing.T) {
populateGraph(t)
query := `{
me(id:[0x01, 23, a.bc, 24]) {
name
}
}`
js := processToFastJSON(t, query)
require.JSONEq(t, `{"me":[{"name":"Michonne"},{"name":"Rick Grimes"},{"name":"Alice"},{"name":"Glenn Rhee"}]}`, js)
}
func TestGeneratorMultiRootFilter1(t *testing.T) {
populateGraph(t)
query := `
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment