github.com/turingchain2020/turingchain@v1.1.21/blockchain/localdb.go (about) 1 package blockchain 2 3 import ( 4 "github.com/turingchain2020/turingchain/common" 5 "github.com/turingchain2020/turingchain/common/db" 6 "github.com/turingchain2020/turingchain/queue" 7 "github.com/turingchain2020/turingchain/types" 8 ) 9 10 func (chain *BlockChain) procLocalDB(msgtype int64, msg *queue.Message, reqnum chan struct{}) bool { 11 switch msgtype { 12 case types.EventLocalGet: 13 go chain.processMsg(msg, reqnum, chain.localGet) 14 case types.EventLocalSet: 15 go chain.processMsg(msg, reqnum, chain.localSet) 16 case types.EventLocalBegin: 17 go chain.processMsg(msg, reqnum, chain.localBegin) 18 case types.EventLocalCommit: 19 go chain.processMsg(msg, reqnum, chain.localCommit) 20 case types.EventLocalRollback: 21 go chain.processMsg(msg, reqnum, chain.localRollback) 22 case types.EventLocalList: 23 go chain.processMsg(msg, reqnum, chain.localList) 24 case types.EventLocalPrefixCount: 25 go chain.processMsg(msg, reqnum, chain.localPrefixCount) 26 case types.EventLocalNew: 27 go chain.processMsg(msg, reqnum, chain.localNew) 28 case types.EventLocalClose: 29 go chain.processMsg(msg, reqnum, chain.localClose) 30 default: 31 return false 32 } 33 return true 34 } 35 36 func (chain *BlockChain) localGet(msg *queue.Message) { 37 keys := (msg.Data).(*types.LocalDBGet) 38 if keys.Txid == 0 { 39 values := chain.blockStore.Get(keys) 40 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, values)) 41 return 42 } 43 tx, err := common.GetPointer(keys.Txid) 44 if err != nil { 45 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, err)) 46 return 47 } 48 var reply types.LocalReplyValue 49 for i := 0; i < len(keys.Keys); i++ { 50 key := keys.Keys[i] 51 value, err := tx.(db.KVDB).Get(key) 52 if err != nil { 53 chainlog.Debug("localGet", "i", i, "key", string(key), "err", err) 54 } 55 reply.Values = append(reply.Values, value) 56 } 57 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, &reply)) 58 } 59 60 //只允许设置 通过 transaction 来 set 信息 61 func (chain *BlockChain) localSet(msg *queue.Message) { 62 kvs := (msg.Data).(*types.LocalDBSet) 63 if kvs.Txid == 0 { 64 msg.Reply(chain.client.NewMessage("", types.EventLocalSet, types.ErrNotSetInTransaction)) 65 return 66 } 67 txp, err := common.GetPointer(kvs.Txid) 68 if err != nil { 69 msg.Reply(chain.client.NewMessage("", types.EventLocalSet, err)) 70 return 71 } 72 tx := txp.(db.KVDB) 73 for i := 0; i < len(kvs.KV); i++ { 74 err := tx.Set(kvs.KV[i].Key, kvs.KV[i].Value) 75 if err != nil { 76 chainlog.Error("localSet", "i", i, "key", string(kvs.KV[i].Key), "err", err) 77 } 78 } 79 msg.Reply(chain.client.NewMessage("", types.EventLocalSet, nil)) 80 } 81 82 //创建 localdb transaction 83 func (chain *BlockChain) localNew(msg *queue.Message) { 84 tx := db.NewLocalDB(chain.blockStore.db, msg.GetData().(bool)) 85 id := common.StorePointer(tx) 86 msg.Reply(chain.client.NewMessage("", types.EventLocalNew, &types.Int64{Data: id})) 87 } 88 89 //关闭 localdb transaction 90 func (chain *BlockChain) localClose(msg *queue.Message) { 91 id := (msg.Data).(*types.Int64).Data 92 _, err := common.GetPointer(id) 93 common.RemovePointer(id) 94 msg.Reply(chain.client.NewMessage("", types.EventLocalClose, err)) 95 } 96 97 func (chain *BlockChain) localBegin(msg *queue.Message) { 98 id := (msg.Data).(*types.Int64).Data 99 tx, err := common.GetPointer(id) 100 if err != nil { 101 msg.Reply(chain.client.NewMessage("", types.EventLocalBegin, err)) 102 return 103 } 104 tx.(db.KVDB).Begin() 105 msg.Reply(chain.client.NewMessage("", types.EventLocalBegin, nil)) 106 } 107 108 func (chain *BlockChain) localCommit(msg *queue.Message) { 109 id := (msg.Data).(*types.Int64).Data 110 tx, err := common.GetPointer(id) 111 if err != nil { 112 msg.Reply(chain.client.NewMessage("", types.EventLocalCommit, err)) 113 return 114 } 115 err = tx.(db.KVDB).Commit() 116 msg.Reply(chain.client.NewMessage("", types.EventLocalCommit, err)) 117 } 118 119 func (chain *BlockChain) localRollback(msg *queue.Message) { 120 id := (msg.Data).(*types.Int64).Data 121 tx, err := common.GetPointer(id) 122 if err != nil { 123 msg.Reply(chain.client.NewMessage("", types.EventLocalRollback, err)) 124 return 125 } 126 tx.(db.KVDB).Rollback() 127 msg.Reply(chain.client.NewMessage("", types.EventLocalRollback, nil)) 128 } 129 130 func (chain *BlockChain) localList(msg *queue.Message) { 131 q := (msg.Data).(*types.LocalDBList) 132 var values [][]byte 133 if q.Txid > 0 { 134 tx, err := common.GetPointer(q.Txid) 135 if err != nil { 136 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, err)) 137 return 138 } 139 values, err = tx.(db.KVDB).List(q.Prefix, q.Key, q.Count, q.Direction) 140 if err != nil { 141 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, err)) 142 return 143 } 144 } else { 145 values = db.NewListHelper(chain.blockStore.db).List(q.Prefix, q.Key, q.Count, q.Direction) 146 } 147 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, &types.LocalReplyValue{Values: values})) 148 } 149 150 //获取指定前缀key的数量 151 func (chain *BlockChain) localPrefixCount(msg *queue.Message) { 152 Prefix := (msg.Data).(*types.ReqKey) 153 counts := db.NewListHelper(chain.blockStore.db).PrefixCount(Prefix.Key) 154 msg.Reply(chain.client.NewMessage("", types.EventLocalReplyValue, &types.Int64{Data: counts})) 155 }