MCPcopy Create free account
hub / github.com/ethstorage/es-node / WriteWith

Method WriteWith

ethstorage/data_shard.go:360–381  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

358
359// Write a value of the KV to the store using a customized encoder.
360func (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.
384func (ds *DataShard) Write(kvIdx uint64, b []byte, commit common.Hash) error {

Callers 2

WriteMethod · 0.95
TryWriteEncodedMethod · 0.80

Calls 3

ContainsMethod · 0.95
writeChunkMethod · 0.95
WriteMetaMethod · 0.95

Tested by

no test coverage detected