You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package replication
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  7. "github.com/chrislusf/seaweedfs/weed/replication/sink/filersink"
  8. "github.com/chrislusf/seaweedfs/weed/replication/source"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. type Replicator struct {
  12. sink sink.ReplicationSink
  13. source *source.FilerSource
  14. }
  15. func NewReplicator(sourceConfig, sinkConfig util.Configuration) *Replicator {
  16. sink := &filersink.FilerSink{}
  17. sink.Initialize(sinkConfig)
  18. source := &source.FilerSource{}
  19. source.Initialize(sourceConfig)
  20. sink.SetSourceFiler(source)
  21. return &Replicator{
  22. sink: sink,
  23. source: source,
  24. }
  25. }
  26. func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error {
  27. if !strings.HasPrefix(key, r.source.Dir) {
  28. return nil
  29. }
  30. key = filepath.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
  31. if message.OldEntry != nil && message.NewEntry == nil {
  32. return r.sink.DeleteEntry(key, message.OldEntry, message.DeleteChunks)
  33. }
  34. if message.OldEntry == nil && message.NewEntry != nil {
  35. return r.sink.CreateEntry(key, message.NewEntry)
  36. }
  37. return r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, message.DeleteChunks)
  38. }