/* AppendMessages checks to see whether the segment is full, and if not it appends the messages to it. */ func (seg *Segment) AppendMessages(msgs model.Messages) (appendIndex, lastIndex int64, segmentFull bool, err error) { seg.lock.Lock() defer seg.lock.Unlock() seg.statsAppendMessageCalls++ seg.lastAccessTime = time.Now() seg.lastModifiedTime = seg.lastAccessTime if seg.fileSize >= int64(seg.target_max_segment_size)*1024*1024 { seg.node_log("Segment %v at or beyond target size of %v MB\n", seg.filename, seg.target_max_segment_size) return 0, seg.firstIndex + int64(seg.msgCount) - 1, true, nil } if !seg.segmentOpen { err := seg.openWhileHoldingLock(false) if err != nil { return 0, 0, false, err } } // Go to the end of the file if seg.filePosition != seg.fileSize { seg.statsSeekCount++ seg.fileSize, err = seg.file.Seek(0, 2) seg.filePosition = seg.fileSize if err != nil { seg.node_log("Error seeking to end of file in segment file %v: %v\n", seg.filename, err) return 0, 0, false, err } } else { seg.statsNoSeekAppends++ } appendIndex = seg.firstIndex + int64(seg.msgCount) //writer := bufio.NewWriter(seg.file) seg.writesPendingSync = true // Try and write out the messages written, err := msgs.Write(seg.file) if err != nil { seg.node_log("Error writing to segment %v: %v\n", seg.filename, err) return appendIndex, seg.firstIndex + int64(seg.msgCount) - 1, false, err } // Populate the offset index list msgsOffsets, err := msgs.Offsets() if err != nil { seg.node_log("Error getting offsets for messages written: %v\n", err) return appendIndex, seg.firstIndex + int64(seg.msgCount) - 1, false, err } for msgIndex, msgOffset := range msgsOffsets { seg.appendOffsetToIndexHoldingLock(appendIndex+int64(msgIndex), seg.filePosition+int64(msgOffset)) } seg.msgCount += msgs.GetCount() seg.fileSize += int64(written) seg.filePosition = seg.fileSize return appendIndex, seg.firstIndex + int64(seg.msgCount) - 1, false, nil }
func (mlog *MemoryLogStorage) AppendFirstMessages(msgs model.Messages, leaderFirstIndex int64) (lastIndex int64, err error) { if len(mlog.offsets) != 0 { return 0, errors.New("Attempt to add first messages, but segment already has content!") } mlog.firstIndex = leaderFirstIndex msgs.Write(mlog) // Populate the offsets // Populate the offsets msgsOffsets, err := msgs.Offsets() if err != nil { return 0, err } for _, msgOffset := range msgsOffsets { mlog.offsets = append(mlog.offsets, msgOffset) } return int64(len(mlog.offsets)) + mlog.firstIndex - 1, nil }
func (mlog *MemoryLogStorage) AppendMessages(msgs model.Messages) (lastIndex int64, err error) { if mlog.firstIndex == 0 { mlog.firstIndex = 1 } firstOffset := len(mlog.data) msgs.Write(mlog) // Populate the offsets msgsOffsets, err := msgs.Offsets() if err != nil { return 0, err } for _, msgOffset := range msgsOffsets { mlog.offsets = append(mlog.offsets, firstOffset+msgOffset) } return int64(len(mlog.offsets)) + mlog.firstIndex - 1, nil }