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

Method open

value.go:907–944  ·  view source on GitHub ↗
(path string, flags uint32)

Source from the content-addressed store, hash-verified

905}
906
907func (lf *logFile) open(path string, flags uint32) error {
908 var err error
909 if lf.fd, err = y.OpenExistingFile(path, flags); err != nil {
910 return y.Wrapf(err, "Error while opening file in logfile %s", path)
911 }
912
913 fi, err := lf.fd.Stat()
914 if err != nil {
915 return errFile(err, lf.path, "Unable to run file.Stat")
916 }
917 sz := fi.Size()
918 y.AssertTruef(
919 sz <= math.MaxUint32,
920 "file size: %d greater than %d",
921 uint32(sz), uint32(math.MaxUint32),
922 )
923 lf.size = uint32(sz)
924 if sz < vlogHeaderSize {
925 // Every vlog file should have at least vlogHeaderSize. If it is less than vlogHeaderSize
926 // then it must have been corrupted. But no need to handle here. log replayer will truncate
927 // and bootstrap the logfile. So ignoring here.
928 return nil
929 }
930 buf := make([]byte, vlogHeaderSize)
931 if _, err = lf.fd.Read(buf); err != nil {
932 return y.Wrapf(err, "Error while reading vlog file %d", lf.fid)
933 }
934 keyID := binary.BigEndian.Uint64(buf[:8])
935 var dk *pb.DataKey
936 // retrieve datakey.
937 if dk, err = lf.registry.dataKey(keyID); err != nil {
938 return y.Wrapf(err, "While opening vlog file %d", lf.fid)
939 }
940 lf.dataKey = dk
941 lf.baseIV = buf[8:]
942 y.AssertTrue(len(lf.baseIV) == 12)
943 return nil
944}
945
946// bootstrap will initialize the log file with key id and baseIV.
947// The below figure shows the layout of log file.

Callers 3

openMethod · 0.45
OpenFunction · 0.45

Calls 8

OpenExistingFileFunction · 0.92
WrapfFunction · 0.92
AssertTruefFunction · 0.92
AssertTrueFunction · 0.92
errFileFunction · 0.85
dataKeyMethod · 0.80
SizeMethod · 0.45
ReadMethod · 0.45

Tested by 1