MCPcopy
hub / github.com/dgraph-io/dgraph / sortWithoutIndex

Function sortWithoutIndex

worker/sort.go:139–187  ·  view source on GitHub ↗
(ctx context.Context, ts *pb.SortMessage)

Source from the content-addressed store, hash-verified

137}
138
139func sortWithoutIndex(ctx context.Context, ts *pb.SortMessage) *sortresult {
140 span := trace.SpanFromContext(ctx)
141 span.SetAttributes(attribute.String("sortWithoutIndex", "true"))
142
143 n := len(ts.UidMatrix)
144 r := new(pb.SortResult)
145 multiSortVals := make([][]types.Val, n)
146 var multiSortOffsets []int32
147 // Sort and paginate directly as it'd be expensive to iterate over the index which
148 // might have millions of keys just for retrieving some values.
149 sType, err := schema.State().TypeOf(ts.Order[0].Attr)
150 if err != nil || !sType.IsScalar() {
151 return resultWithError(errors.Errorf("Cannot sort attribute %s of type object.",
152 ts.Order[0].Attr))
153 }
154
155 for i := range n {
156 select {
157 case <-ctx.Done():
158 return resultWithError(ctx.Err())
159 default:
160 // Copy, otherwise it'd affect the destUids and hence the srcUids of Next level.
161 tempList := &pb.List{Uids: ts.UidMatrix[i].Uids}
162 var vals []types.Val
163 if vals, err = sortByValue(ctx, ts, tempList, sType); err != nil {
164 return resultWithError(err)
165 }
166 start, end, err := paginate(ts, tempList, vals)
167 if err != nil {
168 return resultWithError(err)
169 }
170 if len(ts.Order) > 1 {
171 var offset int32
172 // Usually start would equal ts.Offset unless the values around the offset index
173 // (at offset-1, offset-2 index and so on) are equal. In that case we keep those
174 // values and apply the remaining offset later.
175 if int32(start) < ts.Offset {
176 offset = ts.Offset - int32(start)
177 }
178 multiSortOffsets = append(multiSortOffsets, offset)
179 }
180 tempList.Uids = tempList.Uids[start:end]
181 vals = vals[start:end]
182 r.UidMatrix = append(r.UidMatrix, tempList)
183 multiSortVals[i] = vals
184 }
185 }
186 return &sortresult{r, multiSortOffsets, multiSortVals, nil}
187}
188
189func sortWithIndex(ctx context.Context, ts *pb.SortMessage) *sortresult {
190 if ctx.Err() != nil {

Callers 1

processSortFunction · 0.85

Calls 9

StateFunction · 0.92
resultWithErrorFunction · 0.85
sortByValueFunction · 0.85
paginateFunction · 0.85
TypeOfMethod · 0.80
IsScalarMethod · 0.80
StringMethod · 0.45
ErrorfMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected