github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/logbuffer/claim.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 ) 24 25 type Claim struct { 26 buffer atomic.Buffer 27 } 28 29 func (c *Claim) Wrap(buf *atomic.Buffer, offset, length int32) { 30 atomic.BoundsCheck(offset, length, buf.Capacity()) 31 ptr := unsafe.Pointer(uintptr(buf.Ptr()) + uintptr(offset)) 32 c.buffer.Wrap(ptr, length) 33 } 34 35 // The referenced buffer to be used. 36 // 37 // @return the referenced buffer to be used.. 38 func (claim *Claim) Buffer() *atomic.Buffer { 39 // TODO Should this be a pointer or a copy? 40 return &claim.buffer 41 } 42 43 // offset in the buffer at which the claimed range begins. 44 // 45 // @return offset in the buffer at which the range begins. 46 func (claim *Claim) Offset() int32 { 47 return DataFrameHeader.Length 48 } 49 50 // The length of the claimed range in the buffer. 51 // 52 // @return length of the range in the buffer. 53 func (claim *Claim) Length() int32 { 54 return claim.buffer.Capacity() - DataFrameHeader.Length 55 } 56 57 // Get the value stored in the reserve space at the end of a data frame header. 58 // 59 // Note: The value is in {@link ByteOrder#LITTLE_ENDIAN} format. 60 // 61 // @return the value stored in the reserve space at the end of a data frame header. 62 // @see DataHeaderFlyweight 63 func (claim *Claim) ReservedValue() int64 { 64 return claim.buffer.GetInt64(DataFrameHeader.ReservedValueFieldOffset) 65 } 66 67 // Write the provided value into the reserved space at the end of the data frame header. 68 // 69 // Note: The value will be written in {@link ByteOrder#LITTLE_ENDIAN} format. 70 // 71 // @param value to be stored in the reserve space at the end of a data frame header. 72 // @return this for fluent API semantics. 73 // @see DataHeaderFlyweight 74 func (claim *Claim) SetReservedValue(value int64) *Claim { 75 claim.buffer.PutInt64(DataFrameHeader.ReservedValueFieldOffset, value) 76 return claim 77 } 78 79 // Commit the message to the log buffer so that is it available to subscribers. 80 func (claim *Claim) Commit() { 81 frameLength := claim.buffer.Capacity() 82 83 claim.buffer.PutInt32Ordered(DataFrameHeader.FrameLengthFieldOffset, frameLength) 84 } 85 86 // Abort a claim of the message space to the log buffer so that the log can progress by ignoring this claim. 87 func (claim *Claim) Abort() { 88 frameLength := claim.buffer.Capacity() 89 90 claim.buffer.PutUInt16(DataFrameHeader.TypeFieldOffset, DataFrameHeader.TypePad) 91 claim.buffer.PutInt32Ordered(DataFrameHeader.FrameLengthFieldOffset, frameLength) 92 }