Skip to content

Commit f5a621a

Browse files
committed
Switch sliceIterator for radixIterator
1 parent 5ec478c commit f5a621a

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

txn.go

+17-28
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,6 @@ func (txn *Txn) DeleteAll(table, index string, args ...interface{}) (int, error)
258258
return 0, fmt.Errorf("cannot delete in read-only transaction")
259259
}
260260

261-
// TODO: Currently we use Get to just every object and then
262-
// iterate and delete them all. This works because sliceIterator
263-
// has the full result set, but we may need to handle the iteraction
264-
// between the iterator and delete in the future.
265-
266261
// Get all the objects
267262
iter, err := txn.Get(table, index, args...)
268263
if err != nil {
@@ -383,19 +378,15 @@ func (txn *Txn) Get(table, index string, args ...interface{}) (ResultIterator, e
383378
indexTxn := txn.readableIndex(table, indexSchema.Name)
384379
indexRoot := indexTxn.Root()
385380

386-
// Collect all the objects by walking the prefix. This should obviously
387-
// be optimized by using an iterator over the radix tree, but that is
388-
// a lot more work so its a TODO for now.
389-
var results []interface{}
390-
indexRoot.WalkPrefix(val, func(key []byte, val interface{}) bool {
391-
results = append(results, val)
392-
return false
393-
})
381+
// Get an interator over the index
382+
indexIter := indexRoot.Iterator()
383+
384+
// Seek the iterator to the appropriate sub-set
385+
indexIter.SeekPrefix(val)
394386

395-
// Create a crappy iterator
396-
iter := &sliceIterator{
397-
nextIndex: 0,
398-
results: results,
387+
// Create an iterator
388+
iter := &radixIterator{
389+
iter: indexIter,
399390
}
400391
return iter, nil
401392
}
@@ -408,19 +399,17 @@ func (txn *Txn) Defer(fn func()) {
408399
txn.after = append(txn.after, fn)
409400
}
410401

411-
// Slice iterator is used to iterate over a slice of results.
412-
// This is not very efficient as it means the results have already
413-
// been materialized under the iterator.
414-
type sliceIterator struct {
415-
nextIndex int
416-
results []interface{}
402+
// radixIterator is used to wrap an underlying iradix iterator.
403+
// This is much mroe efficient than a sliceIterator as we are not
404+
// materializing the entire view.
405+
type radixIterator struct {
406+
iter *iradix.Iterator
417407
}
418408

419-
func (s *sliceIterator) Next() interface{} {
420-
if s.nextIndex >= len(s.results) {
409+
func (r *radixIterator) Next() interface{} {
410+
_, value, ok := r.iter.Next()
411+
if !ok {
421412
return nil
422413
}
423-
result := s.results[s.nextIndex]
424-
s.nextIndex++
425-
return result
414+
return value
426415
}

0 commit comments

Comments
 (0)