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

Method WriteAt

internal/caches/writer_partial_file.go:111–165  ·  view source on GitHub ↗

WriteAt 在指定位置写入数据

(offset int64, data []byte)

Source from the content-addressed store, hash-verified

109
110// WriteAt 在指定位置写入数据
111func (this *PartialFileWriter) WriteAt(offset int64, data []byte) error {
112 var c = int64(len(data))
113 if c == 0 {
114 return nil
115 }
116 var end = offset + c - 1
117
118 // 是否已包含在内
119 if this.ranges.Contains(offset, end) {
120 return nil
121 }
122
123 // prevent extending too much space in a single writing
124 var maxOffset = this.ranges.Max()
125 if offset-maxOffset > 16<<20 {
126 var extendSizePerStep int64 = 1 << 20
127 var maxExtendSize int64 = 32 << 20
128 if fsutils.DiskIsExtremelyFast() {
129 maxExtendSize = 128 << 20
130 extendSizePerStep = 4 << 20
131 } else if fsutils.DiskIsFast() {
132 maxExtendSize = 64 << 20
133 extendSizePerStep = 2 << 20
134 }
135 if offset-maxOffset > maxExtendSize {
136 stat, err := this.rawWriter.Stat()
137 if err != nil {
138 return nil
139 }
140
141 // extend min size to prepare for file tail
142 if stat.Size()+extendSizePerStep <= this.bodyOffset+offset+int64(len(data)) {
143 _ = this.rawWriter.Truncate(stat.Size() + extendSizePerStep)
144 return nil
145 }
146 }
147 }
148
149 if this.bodyOffset == 0 {
150 var keyLength = 0
151 if this.ranges.Version == 0 { // 以往的版本包含有Key
152 keyLength = len(this.key)
153 }
154 this.bodyOffset = SizeMeta + int64(keyLength) + this.headerSize
155 }
156
157 _, err := this.rawWriter.WriteAt(data, this.bodyOffset+offset)
158 if err != nil {
159 return err
160 }
161
162 this.ranges.Add(offset, end)
163
164 return nil
165}
166
167// SetBodyLength 设置内容总长度
168func (this *PartialFileWriter) SetBodyLength(bodyLength int64) {

Callers

nothing calls this directly

Calls 7

MaxMethod · 0.80
StatMethod · 0.65
WriteAtMethod · 0.65
AddMethod · 0.65
ContainsMethod · 0.45
SizeMethod · 0.45
TruncateMethod · 0.45

Tested by

no test coverage detected