MCPcopy
hub / github.com/dgraph-io/dgraph / Write

Method Write

x/log_writer.go:82–124  ·  view source on GitHub ↗
(p []byte)

Source from the content-addressed store, hash-verified

80}
81
82func (l *LogWriter) Write(p []byte) (int, error) {
83 if l == nil {
84 return 0, nil
85 }
86
87 l.mu.Lock()
88 defer l.mu.Unlock()
89
90 if l.size+int64(len(p)) >= l.MaxSize*1024*1024 {
91 if err := l.rotate(); err != nil {
92 return 0, err
93 }
94 }
95
96 // if encryption is enabled store the data in encrypted way
97 // encrypted writes will be preceded by the following header
98 // #################################################################
99 // ##### [16]byte iv + [4]byte uint32(len(p)) + [:]byte p #####
100 // #################################################################
101 if l.EncryptionKey != nil {
102 iv := make([]byte, 16)
103 if _, err := rand.Read(iv); err != nil {
104 return 0, err
105 }
106
107 lengthHeader := make([]byte, 4)
108 binary.BigEndian.PutUint32(lengthHeader, uint32(len(p)))
109
110 cipherText, err := encrypt(l.EncryptionKey, iv, p)
111 if err != nil {
112 return 0, err
113 }
114
115 allocation := append(append(iv, lengthHeader...), cipherText...)
116 n, err := l.writer.Write(allocation)
117 l.size = l.size + int64(n)
118 return n, err
119 }
120
121 n, err := l.writer.Write(p)
122 l.size = l.size + int64(n)
123 return n, err
124}
125
126func (l *LogWriter) Close() error {
127 if l == nil {

Callers 1

Calls 6

rotateMethod · 0.95
encryptFunction · 0.85
ReadMethod · 0.65
WriteMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45

Tested by 1