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.

46 lines
1.2 KiB

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