mirror of
https://github.com/safing/portbase
synced 2025-09-09 13:55:47 +00:00
Complete first alpha version
This commit is contained in:
parent
c399d7c748
commit
1ef3ceb274
22 changed files with 299 additions and 159 deletions
|
@ -1,6 +1,10 @@
|
|||
package iterator
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/tevino/abool"
|
||||
|
||||
"github.com/Safing/portbase/database/record"
|
||||
)
|
||||
|
||||
|
@ -8,19 +12,43 @@ import (
|
|||
type Iterator struct {
|
||||
Next chan record.Record
|
||||
Done chan struct{}
|
||||
Err error
|
||||
|
||||
errLock sync.Mutex
|
||||
err error
|
||||
doneClosed *abool.AtomicBool
|
||||
}
|
||||
|
||||
// New creates a new Iterator.
|
||||
func New() *Iterator {
|
||||
return &Iterator{
|
||||
Next: make(chan record.Record, 10),
|
||||
Done: make(chan struct{}),
|
||||
Next: make(chan record.Record, 10),
|
||||
Done: make(chan struct{}),
|
||||
doneClosed: abool.NewBool(false),
|
||||
}
|
||||
}
|
||||
|
||||
// Finish is called be the storage to signal the end of the query results.
|
||||
func (it *Iterator) Finish(err error) {
|
||||
close(it.Next)
|
||||
close(it.Done)
|
||||
it.Err = err
|
||||
if it.doneClosed.SetToIf(false, true) {
|
||||
close(it.Done)
|
||||
}
|
||||
|
||||
it.errLock.Lock()
|
||||
defer it.errLock.Unlock()
|
||||
it.err = err
|
||||
}
|
||||
|
||||
// Cancel is called by the iteration consumer to cancel the running query.
|
||||
func (it *Iterator) Cancel() {
|
||||
if it.doneClosed.SetToIf(false, true) {
|
||||
close(it.Done)
|
||||
}
|
||||
}
|
||||
|
||||
// Err returns the iterator error, if exists.
|
||||
func (it *Iterator) Err() error {
|
||||
it.errLock.Lock()
|
||||
defer it.errLock.Unlock()
|
||||
return it.err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue