github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/ledger/ram/factory.go (about) 1 /* 2 Copyright IBM Corp. 2017 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 ramledger 18 19 import ( 20 "sync" 21 22 "github.com/hyperledger/fabric/orderer/ledger" 23 cb "github.com/hyperledger/fabric/protos/common" 24 ) 25 26 type ramLedgerFactory struct { 27 maxSize int 28 ledgers map[string]ledger.ReadWriter 29 mutex sync.Mutex 30 } 31 32 // GetOrCreate gets an existing ledger (if it exists) or creates it if it does not 33 func (rlf *ramLedgerFactory) GetOrCreate(chainID string) (ledger.ReadWriter, error) { 34 rlf.mutex.Lock() 35 defer rlf.mutex.Unlock() 36 37 key := chainID 38 39 l, ok := rlf.ledgers[key] 40 if ok { 41 return l, nil 42 } 43 44 ch := newChain(rlf.maxSize) 45 rlf.ledgers[key] = ch 46 return ch, nil 47 } 48 49 // newChain creates a new chain backed by a RAM ledger 50 func newChain(maxSize int) ledger.ReadWriter { 51 preGenesis := &cb.Block{ 52 Header: &cb.BlockHeader{ 53 Number: ^uint64(0), 54 }, 55 } 56 57 rl := &ramLedger{ 58 maxSize: maxSize, 59 size: 1, 60 oldest: &simpleList{ 61 signal: make(chan struct{}), 62 block: preGenesis, 63 }, 64 } 65 rl.newest = rl.oldest 66 return rl 67 } 68 69 // ChainIDs returns the chain IDs the factory is aware of 70 func (rlf *ramLedgerFactory) ChainIDs() []string { 71 rlf.mutex.Lock() 72 defer rlf.mutex.Unlock() 73 ids := make([]string, len(rlf.ledgers)) 74 75 i := 0 76 for key := range rlf.ledgers { 77 ids[i] = key 78 i++ 79 } 80 81 return ids 82 } 83 84 // Close is a no-op for the RAM ledger 85 func (rlf *ramLedgerFactory) Close() { 86 return // nothing to do 87 } 88 89 // New creates a new ledger factory 90 func New(maxSize int) ledger.Factory { 91 rlf := &ramLedgerFactory{ 92 maxSize: maxSize, 93 ledgers: make(map[string]ledger.ReadWriter), 94 } 95 96 return rlf 97 }