github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/server/storage_provider.go (about)

     1  // Copyright 2018 Google Inc. All Rights Reserved.
     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 server
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"sync"
    21  
    22  	"github.com/google/trillian/monitoring"
    23  	"github.com/google/trillian/storage"
    24  )
    25  
    26  // NewStorageProviderFunc is the signature of a function which can be registered
    27  // to provide instances of storage providers.
    28  type NewStorageProviderFunc func(monitoring.MetricFactory) (StorageProvider, error)
    29  
    30  var (
    31  	storageSystem = flag.String("storage_system", "mysql", fmt.Sprintf("Storage system to use. One of: %v", storageProviders()))
    32  
    33  	spMu     sync.RWMutex
    34  	spOnce   sync.Once
    35  	spByName map[string]NewStorageProviderFunc
    36  )
    37  
    38  // RegisterStorageProvider registers the provided StorageProvider.
    39  func RegisterStorageProvider(name string, sp NewStorageProviderFunc) error {
    40  	spMu.Lock()
    41  	defer spMu.Unlock()
    42  
    43  	spOnce.Do(func() {
    44  		spByName = make(map[string]NewStorageProviderFunc)
    45  	})
    46  
    47  	_, exists := spByName[name]
    48  	if exists {
    49  		return fmt.Errorf("storage provider %v already registered", name)
    50  	}
    51  	spByName[name] = sp
    52  	return nil
    53  }
    54  
    55  // NewStorageProviderFromFlags returns a new StorageProvider instance of the type
    56  // specified by flag.
    57  func NewStorageProviderFromFlags(mf monitoring.MetricFactory) (StorageProvider, error) {
    58  	return NewStorageProvider(*storageSystem, mf)
    59  }
    60  
    61  // NewStorageProvider returns a new StorageProvider instance of the type
    62  // specified by name.
    63  func NewStorageProvider(name string, mf monitoring.MetricFactory) (StorageProvider, error) {
    64  	spMu.RLock()
    65  	defer spMu.RUnlock()
    66  
    67  	sp := spByName[name]
    68  	if sp == nil {
    69  		return nil, fmt.Errorf("no such storage provider %v", name)
    70  	}
    71  
    72  	return sp(mf)
    73  }
    74  
    75  // storageProviders returns a slice of all registered storage provider names.
    76  func storageProviders() []string {
    77  	spMu.RLock()
    78  	defer spMu.RUnlock()
    79  
    80  	r := []string{}
    81  	for k := range spByName {
    82  		r = append(r, k)
    83  	}
    84  
    85  	return r
    86  }
    87  
    88  // StorageProvider is an interface which allows trillian binaries to use
    89  // different storage implementations.
    90  type StorageProvider interface {
    91  	// LogStorage creates and returns a LogStorage implementation.
    92  	LogStorage() storage.LogStorage
    93  	// MapStorage creates and returns a MapStorage implementation.
    94  	MapStorage() storage.MapStorage
    95  	// AdminStorage creates and returns a AdminStorage implementation.
    96  	AdminStorage() storage.AdminStorage
    97  
    98  	// Close closes the underlying storage.
    99  	Close() error
   100  }