appendLogRecord Append data to a file
(logRecord *data2.LogRecord)
| 182 | |
| 183 | // appendLogRecord Append data to a file |
| 184 | func (db *DB) appendLogRecord(logRecord *data2.LogRecord) (*data2.LogRecordPst, error) { |
| 185 | // Check whether the active data file exists |
| 186 | // Initializes the data file if empty |
| 187 | if db.activeFile == nil { |
| 188 | if err := db.setActiveDataFile(); err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Write data coding |
| 194 | encRecord, size := data2.EncodeLogRecord(logRecord) |
| 195 | if db.activeFile.WriteOff+size > db.options.DataFileSize { |
| 196 | // Persisting data files to ensure that existing data is persisted to disk |
| 197 | if err := db.activeFile.Sync(); err != nil { |
| 198 | return nil, err |
| 199 | } |
| 200 | |
| 201 | // Converts the current active file to the old data file |
| 202 | db.olderFiles[db.activeFile.FileID] = db.activeFile |
| 203 | |
| 204 | // Open a new active file |
| 205 | if err := db.setActiveDataFile(); err != nil { |
| 206 | return nil, err |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | writeOff := db.activeFile.WriteOff |
| 211 | if err := db.activeFile.Write(encRecord); err != nil { |
| 212 | return nil, err |
| 213 | } |
| 214 | |
| 215 | // Determines whether to initialize based on user configuration |
| 216 | if db.options.SyncWrite { |
| 217 | if err := db.activeFile.Sync(); err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Build in-memory index information |
| 223 | pst := &data2.LogRecordPst{ |
| 224 | Fid: db.activeFile.FileID, |
| 225 | Offset: writeOff, |
| 226 | } |
| 227 | return pst, nil |
| 228 | |
| 229 | } |
| 230 | |
| 231 | // Set the current active file |
| 232 | // Hold a mutex before accessing this method |
no test coverage detected