github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/object_stats.go (about)

     1  // Copyright 2023 Matrix Origin
     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 objectio
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  )
    24  
    25  type ObjectDescriber interface {
    26  	DescribeObject() ([]ObjectStats, error)
    27  }
    28  
    29  const (
    30  	rowCntLen = 4
    31  	blkCntLen = 4
    32  
    33  	objectNameOffset       = 0
    34  	extentOffset           = objectNameOffset + ObjectNameLen
    35  	rowCntOffset           = extentOffset + ExtentLen
    36  	blkCntOffset           = rowCntOffset + rowCntLen
    37  	zoneMapOffset          = blkCntOffset + blkCntLen
    38  	objectSizeOffset       = zoneMapOffset + ZoneMapSize
    39  	objectSizeLen          = 4
    40  	objectOriginSizeOffset = objectSizeOffset + objectSizeLen
    41  	objectOriginSizeLen    = 4
    42  	ObjectStatsLen         = objectOriginSizeOffset + objectOriginSizeLen
    43  )
    44  
    45  var ZeroObjectStats ObjectStats
    46  
    47  // ObjectStats has format:
    48  // +------------------------------------------------------------------------------------------------+
    49  // |object_name(60B)|extent(13B)|row_cnt(4B)|block_cnt(4B)|zone_map(64B)|objectSize|objectOriginSize|
    50  // +------------------------------------------------------------------------------------------------+
    51  type ObjectStats [ObjectStatsLen]byte
    52  
    53  func NewObjectStats() *ObjectStats {
    54  	return new(ObjectStats)
    55  }
    56  
    57  func (des *ObjectStats) Marshal() []byte {
    58  	return des[:]
    59  }
    60  
    61  func (des *ObjectStats) UnMarshal(data []byte) {
    62  	copy(des[:], data)
    63  }
    64  
    65  // Clone deep copies the stats and returns its pointer
    66  func (des *ObjectStats) Clone() *ObjectStats {
    67  	copied := NewObjectStats()
    68  	copy(copied[:], des[:])
    69  	return copied
    70  }
    71  
    72  func (des *ObjectStats) IsZero() bool {
    73  	return bytes.Equal(des[:], ZeroObjectStats[:])
    74  }
    75  
    76  func (des *ObjectStats) ZMIsEmpty() bool {
    77  	return bytes.Equal(des[zoneMapOffset:zoneMapOffset+zoneMapLen],
    78  		ZeroObjectStats[zoneMapOffset:zoneMapOffset+zoneMapLen])
    79  }
    80  
    81  func (des *ObjectStats) ObjectShortName() *ObjectNameShort {
    82  	return des.ObjectName().Short()
    83  }
    84  
    85  func (des *ObjectStats) ObjectLocation() Location {
    86  	return BuildLocation(des.ObjectName(), des.Extent(), 0, 0)
    87  }
    88  
    89  func (des *ObjectStats) ObjectName() ObjectName {
    90  	return ObjectName(des[objectNameOffset : objectNameOffset+ObjectNameLen])
    91  }
    92  
    93  func (des *ObjectStats) Size() uint32 {
    94  	return types.DecodeUint32(des[objectSizeOffset : objectSizeOffset+objectSizeLen])
    95  }
    96  
    97  func (des *ObjectStats) OriginSize() uint32 {
    98  	return types.DecodeUint32(des[objectOriginSizeOffset : objectOriginSizeOffset+objectOriginSizeLen])
    99  }
   100  
   101  func (des *ObjectStats) BlkCnt() uint32 {
   102  	return types.DecodeUint32(des[blkCntOffset : blkCntOffset+blkCntLen])
   103  }
   104  
   105  func (des *ObjectStats) SortKeyZoneMap() ZoneMap {
   106  	return ZoneMap(des[zoneMapOffset : zoneMapOffset+zoneMapLen])
   107  }
   108  
   109  func (des *ObjectStats) Extent() Extent {
   110  	return des[extentOffset : extentOffset+ExtentLen]
   111  }
   112  
   113  func (des *ObjectStats) Rows() uint32 {
   114  	return types.DecodeUint32(des[rowCntOffset : rowCntOffset+rowCntLen])
   115  }
   116  
   117  func (des *ObjectStats) String() string {
   118  	return fmt.Sprintf("[object stats]: objName: %s; extent: %v; "+
   119  		"rowCnt: %d; blkCnt: %d; sortKey zoneMap: %v; size: %d; originSize: %d",
   120  		des.ObjectName().String(), des.Extent().String(),
   121  		des.Rows(), des.BlkCnt(), des.SortKeyZoneMap(),
   122  		des.Size(), des.OriginSize())
   123  }
   124  
   125  func setHelper(stats *ObjectStats, offset int, data []byte) error {
   126  	if stats == nil {
   127  		return moerr.NewInternalErrorNoCtx("invalid object stats")
   128  	}
   129  
   130  	if data == nil {
   131  		return moerr.NewInternalErrorNoCtx("invalid input data")
   132  	}
   133  	copy(stats[offset:], data)
   134  	return nil
   135  }
   136  
   137  func SetObjectStatsRowCnt(stats *ObjectStats, cnt uint32) error {
   138  	return setHelper(stats, rowCntOffset, types.EncodeUint32(&cnt))
   139  }
   140  
   141  func SetObjectStatsBlkCnt(stats *ObjectStats, cnt uint32) error {
   142  	return setHelper(stats, blkCntOffset, types.EncodeUint32(&cnt))
   143  }
   144  
   145  func SetObjectStatsObjectName(stats *ObjectStats, name ObjectName) error {
   146  	return setHelper(stats, objectNameOffset, name)
   147  }
   148  
   149  func SetObjectStatsShortName(stats *ObjectStats, name *ObjectNameShort) error {
   150  	return setHelper(stats, objectNameOffset, name[:])
   151  }
   152  
   153  func SetObjectStatsExtent(stats *ObjectStats, extent Extent) error {
   154  	return setHelper(stats, extentOffset, extent)
   155  }
   156  
   157  func SetObjectStatsSortKeyZoneMap(stats *ObjectStats, zoneMap ZoneMap) error {
   158  	return setHelper(stats, zoneMapOffset, zoneMap)
   159  }
   160  
   161  func SetObjectStatsLocation(stats *ObjectStats, location Location) error {
   162  	return setHelper(stats, objectNameOffset, location[:ObjectNameLen+ExtentLen])
   163  }
   164  
   165  func SetObjectStatsSize(stats *ObjectStats, size uint32) error {
   166  	return setHelper(stats, objectSizeOffset, types.EncodeUint32(&size))
   167  }
   168  
   169  func SetObjectStatsOriginSize(stats *ObjectStats, size uint32) error {
   170  	return setHelper(stats, objectOriginSizeOffset, types.EncodeUint32(&size))
   171  }
   172  
   173  // ForeachObjectStats executes onStats on each object stats until onStats returns false
   174  // or all object stats have been visited
   175  func ForeachObjectStats(onStats func(stats *ObjectStats) bool, statsList ...ObjectStats) {
   176  	statsLen := len(statsList)
   177  	for idx := 0; idx < statsLen; idx++ {
   178  		if !onStats(&statsList[idx]) {
   179  			return
   180  		}
   181  	}
   182  }