Put write a key-value pair to db, and the key must be not empty
(key []byte, value []byte)
| 145 | |
| 146 | // Put write a key-value pair to db, and the key must be not empty |
| 147 | func (db *DB) Put(key []byte, value []byte) error { |
| 148 | zap.L().Info("put", zap.ByteString("key", key), zap.ByteString("value", value)) |
| 149 | // check key |
| 150 | if len(key) == 0 { |
| 151 | return _const.ErrKeyIsEmpty |
| 152 | } |
| 153 | |
| 154 | // check LogRecord |
| 155 | logRecord := &data2.LogRecord{ |
| 156 | Key: encodeLogRecordKeyWithSeq(key, nonTransactionSeqNo), |
| 157 | Value: value, |
| 158 | Type: data2.LogRecordNormal, |
| 159 | } |
| 160 | |
| 161 | // append log record |
| 162 | pos, err := db.appendLogRecordWithLock(logRecord) |
| 163 | if err != nil { |
| 164 | return err |
| 165 | } |
| 166 | |
| 167 | // update index |
| 168 | if ok := db.index.Put(key, pos); !ok { |
| 169 | return _const.ErrIndexUpdateFailed |
| 170 | } |
| 171 | |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | // appendLogRecord ethod added lock logic split, |
| 176 | // to avoid batch write resulting in deadlock problems |