MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / InitAutoDiscard

Method InitAutoDiscard

internal/caches/reader_file.go:40–115  ·  view source on GitHub ↗
(autoDiscard bool)

Source from the content-addressed store, hash-verified

38}
39
40func (this *FileReader) InitAutoDiscard(autoDiscard bool) error {
41 if this.openFile != nil {
42 this.meta = this.openFile.meta
43 this.header = this.openFile.header
44 }
45
46 var isOk = false
47
48 if autoDiscard {
49 defer func() {
50 if !isOk {
51 _ = this.discard()
52 }
53 }()
54 }
55
56 var buf = this.meta
57 if len(buf) == 0 {
58 buf = make([]byte, SizeMeta)
59 ok, err := this.readToBuff(this.fp, buf)
60 if err != nil {
61 return err
62 }
63 if !ok {
64 return ErrNotFound
65 }
66 this.meta = buf
67 }
68
69 this.expiresAt = int64(binary.BigEndian.Uint32(buf[:SizeExpiresAt]))
70
71 var status = types.Int(string(buf[OffsetStatus : OffsetStatus+SizeStatus]))
72 if status < 100 || status > 999 {
73 return errors.New("invalid status")
74 }
75 this.status = status
76
77 // URL
78 var urlLength = binary.BigEndian.Uint32(buf[OffsetURLLength : OffsetURLLength+SizeURLLength])
79
80 // header
81 var headerSize = int(binary.BigEndian.Uint32(buf[OffsetHeaderLength : OffsetHeaderLength+SizeHeaderLength]))
82 if headerSize == 0 {
83 return nil
84 }
85 this.headerSize = headerSize
86 this.headerOffset = int64(SizeMeta) + int64(urlLength)
87
88 // body
89 this.bodyOffset = this.headerOffset + int64(headerSize)
90 var bodySize = int(binary.BigEndian.Uint64(buf[OffsetBodyLength : OffsetBodyLength+SizeBodyLength]))
91 if bodySize == 0 {
92 isOk = true
93 return nil
94 }
95 this.bodySize = int64(bodySize)
96
97 // read header

Callers 1

InitMethod · 0.95

Calls 3

discardMethod · 0.95
readToBuffMethod · 0.95
SeekMethod · 0.80

Tested by

no test coverage detected