github.com/coreos/mantle@v0.13.0/storage/index/sync.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package index
    16  
    17  import (
    18  	"golang.org/x/net/context"
    19  
    20  	gs "google.golang.org/api/storage/v1"
    21  
    22  	"github.com/coreos/mantle/storage"
    23  )
    24  
    25  type SyncIndexJob struct {
    26  	storage.SyncJob
    27  	IndexJob
    28  
    29  	srcIndexes IndexSet
    30  	dstIndexes IndexSet
    31  }
    32  
    33  func NewSyncIndexJob(src, dst *storage.Bucket) *SyncIndexJob {
    34  	si := &SyncIndexJob{
    35  		SyncJob: storage.SyncJob{
    36  			Source:      src,
    37  			Destination: dst,
    38  		},
    39  		IndexJob: IndexJob{
    40  			Bucket: dst,
    41  		},
    42  		srcIndexes: NewIndexSet(src),
    43  		dstIndexes: NewIndexSet(dst),
    44  	}
    45  	si.SyncJob.SourceFilter(si.srcIndexes.NotIndex)
    46  	si.SyncJob.DeleteFilter(si.dstIndexes.NotIndex)
    47  	return si
    48  }
    49  
    50  // DestinationPrefix overrides the Destination bucket's default prefix.
    51  func (si *SyncIndexJob) DestinationPrefix(p string) {
    52  	si.SyncJob.DestinationPrefix(p)
    53  	si.IndexJob.Prefix(p)
    54  }
    55  
    56  // Prefix is an alias for DestinationPrefix()
    57  func (si *SyncIndexJob) Prefix(p string) {
    58  	si.DestinationPrefix(p)
    59  }
    60  
    61  // SourceFilter selects which objects to copy from Source.
    62  func (si *SyncIndexJob) SourceFilter(f storage.Filter) {
    63  	si.SyncJob.SourceFilter(func(obj *gs.Object) bool {
    64  		return f(obj) && si.srcIndexes.NotIndex(obj)
    65  	})
    66  }
    67  
    68  // DeleteFilter selects which objects may be pruned from Destination.
    69  func (si *SyncIndexJob) DeleteFilter(f storage.Filter) {
    70  	si.SyncJob.DeleteFilter(func(obj *gs.Object) bool {
    71  		return f(obj) && si.dstIndexes.NotIndex(obj)
    72  	})
    73  }
    74  
    75  // Delete enables deletion of extra objects and indexes from Destination.
    76  func (si *SyncIndexJob) Delete(enable bool) {
    77  	si.SyncJob.Delete(enable)
    78  	si.IndexJob.Delete(enable)
    79  }
    80  
    81  // Recursive toggles copying/indexing subdirectories (the default).
    82  func (si *SyncIndexJob) Recursive(enable bool) {
    83  	si.SyncJob.Recursive(enable)
    84  	si.IndexJob.Recursive(enable)
    85  }
    86  
    87  func (sj *SyncIndexJob) Do(ctx context.Context) error {
    88  	if err := sj.SyncJob.Do(ctx); err != nil {
    89  		return err
    90  	}
    91  	return sj.IndexJob.Do(ctx)
    92  }