github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/counters/readable_counter.go (about) 1 // Copyright 2022 Steven Stern 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package counters 16 17 import ( 18 "fmt" 19 ) 20 21 // Aeron.NullValue, but using it directly creates a circular dependency 22 const nullValue = -1 23 24 type ReadableCounter struct { 25 Reader *Reader 26 RegistrationId int64 27 CounterId int32 28 metaDataOffset int32 29 valueOffset int32 30 isClosed bool 31 } 32 33 func NewReadableRegisteredCounter(reader *Reader, registrationId int64, counterId int32) (*ReadableCounter, error) { 34 if counterId < 0 || counterId >= int32(reader.maxCounterID) { 35 return nil, fmt.Errorf("counterId=%d maxCounterId=%d", counterId, reader.maxCounterID) 36 } 37 metaDataOffset := counterId * MetadataLength 38 valueOffset := counterId * CounterLength 39 return &ReadableCounter{reader, registrationId, counterId, metaDataOffset, valueOffset, false}, nil 40 } 41 42 func NewReadableCounter(reader *Reader, counterId int32) (*ReadableCounter, error) { 43 return NewReadableRegisteredCounter(reader, nullValue, counterId) 44 } 45 46 func (rc *ReadableCounter) State() int32 { 47 return rc.Reader.metaData.GetInt32Volatile(rc.metaDataOffset) 48 } 49 50 func (rc *ReadableCounter) Label() string { 51 return rc.Reader.labelValue(rc.metaDataOffset) 52 } 53 54 func (rc *ReadableCounter) Get() int64 { 55 return rc.Reader.values.GetInt64Volatile(rc.valueOffset) 56 } 57 58 func (rc *ReadableCounter) GetWeak() int64 { 59 return rc.Reader.values.GetInt64(rc.valueOffset) 60 } 61 62 func (rc *ReadableCounter) Close() { 63 rc.isClosed = true 64 } 65 66 func (rc *ReadableCounter) IsClosed() bool { 67 return rc.isClosed 68 }