MCPcopy
hub / github.com/dgraph-io/badger / bootstrap

Method bootstrap

value.go:951–984  ·  view source on GitHub ↗

bootstrap will initialize the log file with key id and baseIV. The below figure shows the layout of log file. +----------------+------------------+------------------+ | keyID(8 bytes) | baseIV(12 bytes)| entry... | +----------------+------------------+------------------+

()

Source from the content-addressed store, hash-verified

949// | keyID(8 bytes) | baseIV(12 bytes)| entry... |
950// +----------------+------------------+------------------+
951func (lf *logFile) bootstrap() error {
952 var err error
953 // delete all the data. because bootstrap is been called while creating vlog and as well
954 // as replaying log. While replaying log, there may be any data left. So we need to truncate
955 // everything.
956 if err = lf.fd.Truncate(0); err != nil {
957 return y.Wrapf(err, "Error while bootstraping.")
958 }
959
960 if _, err = lf.fd.Seek(0, io.SeekStart); err != nil {
961 return y.Wrapf(err, "Error while SeekStart for the logfile %d in logFile.bootstarp", lf.fid)
962 }
963 // generate data key for the log file.
964 var dk *pb.DataKey
965 if dk, err = lf.registry.latestDataKey(); err != nil {
966 return y.Wrapf(err, "Error while retrieving datakey in logFile.bootstarp")
967 }
968 lf.dataKey = dk
969 // We'll always preserve vlogHeaderSize for key id and baseIV.
970 buf := make([]byte, vlogHeaderSize)
971 // write key id to the buf.
972 // key id will be zero if the logfile is in plain text.
973 binary.BigEndian.PutUint64(buf[:8], lf.keyID())
974 // generate base IV. It'll be used with offset of the vptr to encrypt the entry.
975 if _, err := cryptorand.Read(buf[8:]); err != nil {
976 return y.Wrapf(err, "Error while creating base IV, while creating logfile")
977 }
978 // Initialize base IV.
979 lf.baseIV = buf[8:]
980 y.AssertTrue(len(lf.baseIV) == 12)
981 // write the key id and base IV to the file.
982 _, err = lf.fd.Write(buf)
983 return err
984}
985
986func (vlog *valueLog) createVlogFile(fid uint32) (*logFile, error) {
987 path := vlog.fpath(fid)

Callers 2

createVlogFileMethod · 0.95
replayLogMethod · 0.80

Calls 8

keyIDMethod · 0.95
WrapfFunction · 0.92
AssertTrueFunction · 0.92
TruncateMethod · 0.80
latestDataKeyMethod · 0.80
SeekMethod · 0.65
ReadMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected