github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/exchangers/importers/importer.go (about) 1 // Copyright 2018-2021 CERN 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 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package importers 20 21 import ( 22 "errors" 23 "fmt" 24 "strings" 25 "sync" 26 27 "github.com/cs3org/reva/v2/pkg/mentix/connectors" 28 "github.com/cs3org/reva/v2/pkg/mentix/exchangers" 29 "github.com/cs3org/reva/v2/pkg/mentix/meshdata" 30 ) 31 32 // Importer is the interface that all importers must implement. 33 type Importer interface { 34 exchangers.Exchanger 35 36 // Process is called periodically to perform the actual import; if data has been imported, true is returned. 37 Process(*connectors.Collection) (bool, error) 38 } 39 40 // BaseImporter implements basic importer functionality common to all importers. 41 type BaseImporter struct { 42 exchangers.BaseExchanger 43 44 meshDataUpdates meshdata.Vector 45 46 updatesLocker sync.RWMutex 47 } 48 49 // Process is called periodically to perform the actual import; if data has been imported, true is returned. 50 func (importer *BaseImporter) Process(connectors *connectors.Collection) (bool, error) { 51 if importer.meshDataUpdates == nil { // No data present for updating, so nothing to process 52 return false, nil 53 } 54 55 var processErrs []string 56 57 // Data is read, so lock it for writing during the loop 58 importer.updatesLocker.RLock() 59 for _, connector := range connectors.Connectors { 60 if !importer.IsConnectorEnabled(connector.GetID()) { 61 continue 62 } 63 64 if err := importer.processMeshDataUpdates(connector); err != nil { 65 processErrs = append(processErrs, fmt.Sprintf("unable to process imported mesh data for connector '%v': %v", connector.GetName(), err)) 66 } 67 } 68 importer.updatesLocker.RUnlock() 69 70 importer.setMeshDataUpdates(nil) 71 72 var err error 73 if len(processErrs) != 0 { 74 err = errors.New(strings.Join(processErrs, "; ")) 75 } 76 return true, err 77 } 78 79 func (importer *BaseImporter) processMeshDataUpdates(connector connectors.Connector) error { 80 for _, meshData := range importer.meshDataUpdates { 81 if err := connector.UpdateMeshData(meshData); err != nil { 82 return fmt.Errorf("error while updating mesh data: %v", err) 83 } 84 } 85 86 return nil 87 } 88 89 func (importer *BaseImporter) setMeshDataUpdates(meshDataUpdates meshdata.Vector) { 90 importer.updatesLocker.Lock() 91 defer importer.updatesLocker.Unlock() 92 93 importer.meshDataUpdates = meshDataUpdates 94 }