github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/fs_blockstore_provider.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fsblkstorage
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    21  	"github.com/hyperledger/fabric/common/ledger/util"
    22  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    23  )
    24  
    25  // FsBlockstoreProvider provides handle to block storage - this is not thread-safe
    26  type FsBlockstoreProvider struct {
    27  	conf            *Conf
    28  	indexConfig     *blkstorage.IndexConfig
    29  	leveldbProvider *leveldbhelper.Provider
    30  }
    31  
    32  // NewProvider constructs a filesystem based block store provider
    33  func NewProvider(conf *Conf, indexConfig *blkstorage.IndexConfig) blkstorage.BlockStoreProvider {
    34  	p := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: conf.getIndexDir()})
    35  	return &FsBlockstoreProvider{conf, indexConfig, p}
    36  }
    37  
    38  // CreateBlockStore simply calls OpenBlockStore
    39  func (p *FsBlockstoreProvider) CreateBlockStore(ledgerid string) (blkstorage.BlockStore, error) {
    40  	return p.OpenBlockStore(ledgerid)
    41  }
    42  
    43  // OpenBlockStore opens a block store for given ledgerid.
    44  // If a blockstore is not existing, this method creates one
    45  // This method should be invoked only once for a particular ledgerid
    46  func (p *FsBlockstoreProvider) OpenBlockStore(ledgerid string) (blkstorage.BlockStore, error) {
    47  	indexStoreHandle := p.leveldbProvider.GetDBHandle(ledgerid)
    48  	return newFsBlockStore(ledgerid, p.conf, p.indexConfig, indexStoreHandle), nil
    49  }
    50  
    51  // Exists tells whether the BlockStore with given id exists
    52  func (p *FsBlockstoreProvider) Exists(ledgerid string) (bool, error) {
    53  	exists, _, err := util.FileExists(p.conf.getLedgerBlockDir(ledgerid))
    54  	return exists, err
    55  }
    56  
    57  // List lists the ids of the existing ledgers
    58  func (p *FsBlockstoreProvider) List() ([]string, error) {
    59  	return util.ListSubdirs(p.conf.getChainsDir())
    60  }
    61  
    62  // Close closes the FsBlockstoreProvider
    63  func (p *FsBlockstoreProvider) Close() {
    64  	p.leveldbProvider.Close()
    65  }