/
iterator.go
63 lines (53 loc) · 1.24 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package dstore
// Iterator is an iterator for Document's.
type Iterator interface {
// Next document, or nil.
Next() (*Document, error)
// Release resources associated with the iterator.
Release()
}
// NewIterator returns an iterator for a Document slice.
func NewIterator(docs ...*Document) Iterator {
return &docsIterator{docs: docs}
}
type docsIterator struct {
docs []*Document
index int
}
func (i *docsIterator) Next() (*Document, error) {
if i.index >= len(i.docs) {
return nil, nil
}
d := i.docs[i.index]
i.index++
return d, nil
}
func (i *docsIterator) Release() {
i.docs = nil
}
// CollectionIterator is an iterator for Collection's.
type CollectionIterator interface {
// Next collection, or nil.
Next() (*Collection, error)
// Release resources associated with the iterator.
Release()
}
// NewCollectionIterator returns an iterator for a Collection slice.
func NewCollectionIterator(cols []*Collection) CollectionIterator {
return &colsIterator{cols: cols}
}
type colsIterator struct {
cols []*Collection
index int
}
func (i *colsIterator) Next() (*Collection, error) {
if i.index >= len(i.cols) {
return nil, nil
}
c := i.cols[i.index]
i.index++
return c, nil
}
func (i *colsIterator) Release() {
i.cols = nil
}