it is a naiive thing
parent
6d6df1da80
commit
1a586de656
|
|
@ -2,21 +2,88 @@ package replicator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Replicator struct {
|
type Replicator struct {
|
||||||
Src Driver
|
Src Driver
|
||||||
Dest Driver
|
Dest Driver
|
||||||
|
Interval time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReplicator(src, dest Driver) Replicator {
|
func NewReplicator(src, dest Driver) Replicator {
|
||||||
return Replicator{
|
return Replicator{
|
||||||
Src: src,
|
Src: src,
|
||||||
Dest: dest,
|
Dest: dest,
|
||||||
|
Interval: time.Second,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Replicator) Stream(ctx context.Context) error {
|
func (r Replicator) Stream(ctx context.Context) error {
|
||||||
return io.EOF
|
last, err := r.getContinuation(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if last == nil {
|
||||||
|
if err := r.fullStream(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.deltaStream(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Replicator) getContinuation(ctx context.Context) (Version, error) {
|
||||||
|
return nil, nil // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Replicator) setContinuation(ctx context.Context, continuation Version) error {
|
||||||
|
return nil // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Replicator) deltaStream(ctx context.Context) error {
|
||||||
|
continuation, err := r.getContinuation(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for ctx.Err() == nil {
|
||||||
|
if err := r._deltaStream(ctx, continuation); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case <-time.After(r.Interval):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Replicator) _deltaStream(ctx context.Context, continuation Version) error {
|
||||||
|
last, err := continuation.AsTime()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
keys, after := r.Src.KeysSince(ctx, last)
|
||||||
|
for keyVersion := range keys {
|
||||||
|
if t, err := keyVersion.Version.AsTime(); err != nil {
|
||||||
|
return err
|
||||||
|
} else if t.After(last) {
|
||||||
|
last = t
|
||||||
|
}
|
||||||
|
|
||||||
|
valueVersion, err := r.Src.Get(ctx, keyVersion.Key)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.Dest.Set(ctx, keyVersion.Key, valueVersion.Value, valueVersion.Version); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r.setContinuation(ctx, TimeAsVersion(last))
|
||||||
|
}
|
||||||
|
return *after
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Replicator) fullStream(ctx context.Context) error {
|
||||||
|
return r._deltaStream(ctx, nil)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,9 @@ func TestReplicatorStream(t *testing.T) {
|
||||||
before: func(t *testing.T, r Replicator) {
|
before: func(t *testing.T, r Replicator) {
|
||||||
r.Src.Set(nil, key, value, version)
|
r.Src.Set(nil, key, value, version)
|
||||||
},
|
},
|
||||||
|
during: func(t *testing.T, r Replicator) {
|
||||||
|
time.Sleep(time.Millisecond * 200)
|
||||||
|
},
|
||||||
after: func(t *testing.T, r Replicator) {
|
after: func(t *testing.T, r Replicator) {
|
||||||
if got, _ := r.Dest.Get(nil, key); !got.Version.Equal(version) || !got.Value.Equal(value) {
|
if got, _ := r.Dest.Get(nil, key); !got.Version.Equal(version) || !got.Value.Equal(value) {
|
||||||
t.Error(got)
|
t.Error(got)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue