MCPcopy
hub / github.com/aceld/zinx / Write

Method Write

zutils/witer.go:92–128  ·  view source on GitHub ↗
(p []byte)

Source from the content-addressed store, hash-verified

90}
91
92func (w *Writer) Write(p []byte) (n int, err error) {
93 w.mu.Lock()
94 defer w.mu.Unlock()
95 if w.cons {
96 os.Stderr.Write(p)
97 }
98 if w.file == nil {
99 if err := w.rotate(); err != nil {
100 os.Stderr.Write(p)
101 return 0, err
102 }
103 }
104
105 t := time.Now()
106 b := t.AppendFormat(nil, time.RFC3339)
107
108 // 按天切割
109 if !bytes.Equal(w.creates[:10], b[:10]) { //2023-04-05
110 go w.delete() // 每天检测一次旧文件
111 if err := w.rotate(); err != nil {
112 return 0, err
113 }
114 }
115 // 按大小切割,需要考虑bw缓冲区中未写入的数据
116 if w.size+int64(len(p))+int64(w.bw.Buffered()) >= w.maxSize {
117 if err := w.rotate(); err != nil {
118 return 0, err
119 }
120 }
121 // n, err = w.file.Write(p)
122 n, err = w.bw.Write(p)
123 w.size += int64(n)
124 if err != nil {
125 return n, err
126 }
127 return
128}
129
130// rotate 切割文件
131func (w *Writer) rotate() error {

Callers 15

PackMethod · 0.80
TestDataPackFunction · 0.80
PackMethod · 0.80
OutPutMethod · 0.80
PackMethod · 0.80
SendMsgMethod · 0.80
TestFnv32Function · 0.80
ClinetFunction · 0.80
ClientJoinFunction · 0.80
ClientTestFunction · 0.80
SendMethod · 0.80

Calls 2

rotateMethod · 0.95
deleteMethod · 0.95

Tested by 6

TestDataPackFunction · 0.64
TestFnv32Function · 0.64
ClinetFunction · 0.64
ClientJoinFunction · 0.64
ClientTestFunction · 0.64