Commit 083aed66 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 9003ac50
......@@ -101,7 +101,7 @@ type FileStorage struct {
txnhMax TxnHeader
// driver client <- watcher: data file updates.
watchq chan interface{} // XXX
watchq chan watchEvent
}
// IStorageDriver
......@@ -450,14 +450,14 @@ func (fs *FileStorage) watcher() {
_ = err
}
// watch watches updates to fs.file and notifies subscriber about new transaction via fs.watchq.
// watch watches updates to .file and notifies Watch about new transactions.
//
// watch is the only place that mutates index and txnh{Min,Max}.
// XXX ^^^ will change after commit is implemented.
func (fs *FileStorage) watch() (err error) {
f := fs.file
idx := fs.index
defer xerr.Contextf(&err, "%s: watch", f.Name())
defer xerr.Contextf(&err, "%s: watcher", f.Name())
// XXX race -> start watching before FileStorage is returned to user
......@@ -477,15 +477,10 @@ func (fs *FileStorage) watch() (err error) {
//
// besides relying on notify we also check file periodically to avoid
// stalls due to e.g. OS notification errors.
var it *Iter
itReset := func() { // reset it to (re)start from toppos with empty buffer
it = Iterate(seqReadAt(f), idx.TopPos, IterForward)
}
itReset()
tick := time.NewTicker(1*time.Second)
defer tick.Stop()
var t0partial time.Time
mainloop:
for {
// XXX select here
......@@ -503,7 +498,10 @@ func (fs *FileStorage) watch() (err error) {
return fmt.Errorf("file truncated (%d -> %d)", idx.TopPos, fsize)
}
// there is some data after toppos - try to read it.
// there is some data after toppos - try to advance as much as we can.
// start iterating afresh with empty buffer.
it := Iterate(seqReadAt(f), idx.TopPos, IterForward)
for {
err = it.NextTxn(LoadNoStrings)
if err != nil {
// transaction header could not be fully read.
......@@ -523,20 +521,17 @@ func (fs *FileStorage) watch() (err error) {
}
} else {
// not ok - e.g. IO error
// only EOF is ok - it is e.g. when transaction was aborted
// only EOF is ok - it can happen when transaction was aborted
if err != io.EOF {
return err
}
// EOF - reset t₀(partial)
t0partial = time.Time{}
}
// after any error (EOF, partial read) we have to retry with
// reset bufferring.
itReset()
continue
// after any error (EOF, partial read) we have to resync
continue mainloop
}
// read ok - reset t₀(partial)
......@@ -547,10 +542,9 @@ func (fs *FileStorage) watch() (err error) {
// we could successfully read the transaction header. Try to see now,
// whether it is finished transaction or not.
if it.Txnh.Status == zodb.TxnInprogress {
// not yet. we have to reset because transaction finish writes
// not yet. we have to resync because transaction finish writes
// to what we have already buffered.
itReset()
continue
continue mainloop
}
// it is fully-committed transaction. scan its data records to update
......@@ -583,10 +577,12 @@ func (fs *FileStorage) watch() (err error) {
}
fs.mu.Unlock()
// XXX -> watchq
// XXX cancel on close
fs.watchq <- watchEvent{it.Txnh.Tid, oidv}
}
select {
// XXX handle quit
// XXX handle close
case err := <-w.Errors:
if err == fsnotify.ErrEventOverflow {
......@@ -609,9 +605,23 @@ func (fs *FileStorage) watch() (err error) {
}
}
func (fs *FileStorage) Watch(ctx context.Context) (zodb.Tid, []zodb.Oid, error) {
panic("TODO")
// <-fs.watchq
// watchEvent is one event from watch to Watch
type watchEvent struct {
tid zodb.Tid
oidv []zodb.Oid
}
func (fs *FileStorage) Watch(ctx context.Context) (_ zodb.Tid, _ []zodb.Oid, err error) {
defer xerr.Contextf(&err, "%s: watch", fs.file.Name())
// XXX handle close
select {
case <-ctx.Done():
return zodb.InvalidTid, nil, ctx.Err()
case w := <-fs.watchq:
return w.tid, w.oidv, nil
}
}
// --- open + rebuild index ---
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment