github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/logbuffer/header.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 logbuffer 18 19 import ( 20 "unsafe" 21 22 "github.com/lirm/aeron-go/aeron/atomic" 23 "github.com/lirm/aeron-go/aeron/util" 24 ) 25 26 type Header struct { 27 buffer atomic.Buffer 28 offset int32 29 initialTermID int32 30 positionBitsToShift int32 31 } 32 33 //go:norace 34 func (hdr *Header) Wrap(ptr unsafe.Pointer, length int32) *Header { 35 hdr.buffer.Wrap(ptr, length) 36 return hdr 37 } 38 39 // Position calculates the current position to which the image has advanced on 40 // reading this message. 41 func (hdr *Header) Position() int64 { 42 resultingOffset := util.AlignInt32(hdr.Offset()+hdr.FrameLength(), FrameAlignment) 43 return computePosition(hdr.TermId(), resultingOffset, hdr.positionBitsToShift, hdr.InitialTermId()) 44 } 45 46 func (hdr *Header) Offset() int32 { 47 return hdr.offset 48 } 49 50 func (hdr *Header) Flags() uint8 { 51 return GetFlags(&hdr.buffer, hdr.offset) 52 } 53 54 func (hdr *Header) FrameLength() int32 { 55 return GetFrameLength(&hdr.buffer, hdr.offset) 56 } 57 58 func (hdr *Header) TermId() int32 { 59 return GetTermId(&hdr.buffer, hdr.offset) 60 } 61 62 func (hdr *Header) SessionId() int32 { 63 return GetSessionId(&hdr.buffer, hdr.offset) 64 } 65 66 func (hdr *Header) StreamId() int32 { 67 return GetStreamId(&hdr.buffer, hdr.offset) 68 } 69 70 func (hdr *Header) GetReservedValue() int64 { 71 return GetReservedValue(&hdr.buffer, hdr.offset) 72 } 73 74 func (hdr *Header) SetOffset(offset int32) *Header { 75 hdr.offset = offset 76 return hdr 77 } 78 79 func (hdr *Header) InitialTermId() int32 { 80 return hdr.initialTermID 81 } 82 83 func (hdr *Header) SetInitialTermID(initialTermID int32) *Header { 84 hdr.initialTermID = initialTermID 85 return hdr 86 } 87 88 func (hdr *Header) SetPositionBitsToShift(positionBitsToShift int32) *Header { 89 hdr.positionBitsToShift = positionBitsToShift 90 return hdr 91 } 92 93 func (hdr *Header) SetReservedValue(reservedValue int64) *Header { 94 hdr.buffer.PutInt64(reservedValueOffset(hdr.offset), reservedValue) 95 return hdr 96 } 97 98 func (hdr *Header) SetSessionId(value int32) *Header { 99 hdr.buffer.PutInt32(sessionIdOffset(hdr.offset), value) 100 return hdr 101 } 102 103 // computePosition computes the current position in absolute number of bytes. 104 func computePosition(activeTermId int32, termOffset int32, positionBitsToShift int32, initialTermId int32) int64 { 105 termCount := activeTermId - initialTermId // copes with negative activeTermId on rollover 106 return (int64(termCount) << uint32(positionBitsToShift)) + int64(termOffset) 107 }