github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/logbuffer/term/reader.go (about) 1 /* 2 Copyright 2016 Stanislav Liberman 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package term 18 19 import ( 20 "github.com/lirm/aeron-go/aeron/atomic" 21 "github.com/lirm/aeron-go/aeron/logbuffer" 22 "github.com/lirm/aeron-go/aeron/util" 23 ) 24 25 // Read will attempt to read the next frame from the term and invoke the callback if successful. 26 // Method will return a tuple of new term offset and number of fragments read 27 // 28 //go:norace 29 func Read(termBuffer *atomic.Buffer, termOffset int32, handler FragmentHandler, fragmentLimit int, 30 header *logbuffer.Header) (int32, int) { 31 32 capacity := termBuffer.Capacity() 33 34 var fragmentsRead int 35 for fragmentsRead < fragmentLimit && termOffset < capacity { 36 frameLength := logbuffer.GetFrameLength(termBuffer, termOffset) 37 if frameLength <= 0 { 38 break 39 } 40 41 fragmentOffset := termOffset 42 termOffset += util.AlignInt32(frameLength, logbuffer.FrameAlignment) 43 44 if !logbuffer.IsPaddingFrame(termBuffer, fragmentOffset) { 45 header.Wrap(termBuffer.Ptr(), termBuffer.Capacity()) 46 header.SetOffset(fragmentOffset) 47 handler(termBuffer, fragmentOffset+logbuffer.DataFrameHeader.Length, 48 frameLength-logbuffer.DataFrameHeader.Length, header) 49 50 fragmentsRead++ 51 } 52 } 53 54 return termOffset, fragmentsRead 55 } 56 57 // BoundedRead will attempt to read frames from the term up to the specified offsetLimit. 58 // Method will return a tuple of new term offset and number of fragments read 59 func BoundedRead(termBuffer *atomic.Buffer, termOffset int32, offsetLimit int32, handler FragmentHandler, 60 fragmentLimit int, header *logbuffer.Header) (int32, int) { 61 62 var fragmentsRead int 63 for fragmentsRead < fragmentLimit && termOffset < offsetLimit { 64 frameLength := logbuffer.GetFrameLength(termBuffer, termOffset) 65 if frameLength <= 0 { 66 break 67 } 68 69 fragmentOffset := termOffset 70 termOffset += util.AlignInt32(frameLength, logbuffer.FrameAlignment) 71 72 if !logbuffer.IsPaddingFrame(termBuffer, fragmentOffset) { 73 header.Wrap(termBuffer.Ptr(), termBuffer.Capacity()) 74 header.SetOffset(fragmentOffset) 75 handler(termBuffer, fragmentOffset+logbuffer.DataFrameHeader.Length, 76 frameLength-logbuffer.DataFrameHeader.Length, header) 77 78 fragmentsRead++ 79 } 80 } 81 82 return termOffset, fragmentsRead 83 }