fsledger_provider.go 2.32 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

17
package fsledger
manish's avatar
manish committed
18
19

import (
20
21
	"github.com/hyperledger/fabric/common/ledger/blkstorage"
	"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
manish's avatar
manish committed
22
23
)

24
25
// Provider impements interface OrdererLedgerProvider
type Provider struct {
manish's avatar
manish committed
26
27
28
	blkStoreProvider blkstorage.BlockStoreProvider
}

29
30
// NewProvider construct a new filesystem based orderer ledger provider. Only one instance should be created
func NewProvider(conf *fsblkstorage.Conf) *Provider {
manish's avatar
manish committed
31
32
33
34
35
	attrsToIndex := []blkstorage.IndexableAttr{
		blkstorage.IndexableAttrBlockNum,
	}
	indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
	fsBlkStoreProvider := fsblkstorage.NewProvider(conf, indexConfig)
36
	return &Provider{fsBlkStoreProvider}
manish's avatar
manish committed
37
38
39
}

// Create implements corresponding method in the interface ledger.OrdererLedgerProvider
40
func (p *Provider) Create(ledgerID string) (OrdererLedger, error) {
manish's avatar
manish committed
41
42
43
44
	blkStore, err := p.blkStoreProvider.CreateBlockStore(ledgerID)
	if err != nil {
		return nil, err
	}
45
	return &fsLedger{blkStore}, nil
manish's avatar
manish committed
46
47
48
}

// Open implements corresponding method in the interface ledger.OrdererLedgerProvider
49
func (p *Provider) Open(ledgerID string) (OrdererLedger, error) {
manish's avatar
manish committed
50
51
52
53
	blkStore, err := p.blkStoreProvider.OpenBlockStore(ledgerID)
	if err != nil {
		return nil, err
	}
54
	return &fsLedger{blkStore}, nil
manish's avatar
manish committed
55
56
57
}

// Exists implements corresponding method in the interface ledger.OrdererLedgerProvider
58
func (p *Provider) Exists(ledgerID string) (bool, error) {
manish's avatar
manish committed
59
60
61
62
	return p.blkStoreProvider.Exists(ledgerID)
}

// List implements corresponding method in the interface ledger.OrdererLedgerProvider
63
func (p *Provider) List() ([]string, error) {
manish's avatar
manish committed
64
65
66
67
	return p.blkStoreProvider.List()
}

// Close implements corresponding method in the interface ledger.OrdererLedgerProvider
68
func (p *Provider) Close() {
manish's avatar
manish committed
69
70
	p.blkStoreProvider.Close()
}