67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
package replicator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type (
|
|
Driver interface {
|
|
KeysSince(context.Context, time.Time) (chan KeyVersion, *error)
|
|
Get(context.Context, Key) (ValueVersion, error)
|
|
Set(context.Context, Key, Value, Version) error
|
|
Del(context.Context, Key, Version) error
|
|
}
|
|
|
|
Key struct {
|
|
Namespace string
|
|
Key string
|
|
}
|
|
|
|
Value []byte
|
|
|
|
Version []byte
|
|
|
|
KeyVersion struct {
|
|
Key Key
|
|
Version []byte
|
|
}
|
|
|
|
ValueVersion struct {
|
|
Value []byte
|
|
Version Version
|
|
}
|
|
)
|
|
|
|
func NewDriver(ctx context.Context, driver url.URL) (Driver, error) {
|
|
switch driver.Scheme {
|
|
case "filetree":
|
|
p := driver.Path
|
|
if driver.Host != "" {
|
|
p = path.Join(driver.Host, p)
|
|
}
|
|
return NewFileTree(p)
|
|
case "map":
|
|
return NewMap(), nil
|
|
default:
|
|
return nil, fmt.Errorf("unknown driver spec %s", driver.String())
|
|
}
|
|
}
|
|
|
|
func (version Version) AsTime() (time.Time, error) {
|
|
if len(version) == 0 {
|
|
return time.Time{}, nil
|
|
}
|
|
n, err := strconv.ParseInt(string(version), 10, 64)
|
|
return time.Unix(0, n), err
|
|
}
|
|
|
|
func TimeAsVersion(t time.Time) Version {
|
|
n := t.UnixNano()
|
|
return []byte(strconv.FormatInt(n, 10))
|
|
}
|