Write a value of the KV to the store using a customized encoder.
(kvIdx uint64, b []byte, commit common.Hash, encoder func([]byte, uint64) []byte)
| 358 | |
| 359 | // Write a value of the KV to the store using a customized encoder. |
| 360 | func (ds *DataShard) WriteWith(kvIdx uint64, b []byte, commit common.Hash, encoder func([]byte, uint64) []byte) error { |
| 361 | if !ds.Contains(kvIdx) { |
| 362 | return fmt.Errorf("kv not found") |
| 363 | } |
| 364 | |
| 365 | if uint64(len(b)) > ds.kvSize { |
| 366 | return fmt.Errorf("write data too large") |
| 367 | } |
| 368 | cb := make([]byte, ds.kvSize) |
| 369 | copy(cb, b) |
| 370 | for i := uint64(0); i < ds.chunksPerKv; i++ { |
| 371 | chunkIdx := kvIdx*ds.chunksPerKv + i |
| 372 | encodedChunk := encoder(cb[int(i*ds.chunkSize):int((i+1)*ds.chunkSize)], chunkIdx) |
| 373 | err := ds.writeChunk(chunkIdx, encodedChunk) |
| 374 | |
| 375 | if err != nil { |
| 376 | return err |
| 377 | } |
| 378 | } |
| 379 | // This is not atomic, but we should get error since we already pre-allocate the space |
| 380 | return ds.WriteMeta(kvIdx, commit[:]) |
| 381 | } |
| 382 | |
| 383 | // Write a value of the KV to the store. The value will be encoded with kvIdx and SP address. |
| 384 | func (ds *DataShard) Write(kvIdx uint64, b []byte, commit common.Hash) error { |
no test coverage detected