github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/dataio/blockio/info.go (about)

     1  // Copyright 2021 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 blockio
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/objectio"
    24  )
    25  
    26  type Meta struct {
    27  	key        string
    28  	loc        objectio.Extent
    29  	rows       uint32
    30  	objectSize uint32
    31  }
    32  
    33  func (m *Meta) GetKey() string          { return m.key }
    34  func (m *Meta) GetLoc() objectio.Extent { return m.loc }
    35  func (m *Meta) GetRows() uint32         { return m.rows }
    36  func (m *Meta) GetObjectSize() uint32   { return m.objectSize }
    37  
    38  type Delta struct {
    39  	key        string
    40  	loc        objectio.Extent
    41  	objectSize uint32
    42  }
    43  
    44  func (d *Delta) GetKey() string          { return d.key }
    45  func (d *Delta) GetLoc() objectio.Extent { return d.loc }
    46  func (d *Delta) GetObjectSize() uint32   { return d.objectSize }
    47  
    48  func DecodeMetaLocToMeta(metaLoc string) (*Meta, error) {
    49  	info := strings.Split(metaLoc, ":")
    50  	name := info[0]
    51  	location := strings.Split(info[1], "_")
    52  	offset, err := strconv.ParseUint(location[0], 10, 32)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	size, err := strconv.ParseUint(location[1], 10, 32)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	osize, err := strconv.ParseUint(location[2], 10, 32)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	id, err := strconv.ParseUint(location[3], 10, 32)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	rows, err := strconv.ParseUint(info[2], 10, 32)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	objectSize, err := strconv.ParseUint(info[3], 10, 32)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	extent := objectio.NewExtent(uint32(id), uint32(offset), uint32(size), uint32(osize))
    77  	meta := &Meta{
    78  		key:        name,
    79  		loc:        extent,
    80  		rows:       uint32(rows),
    81  		objectSize: uint32(objectSize),
    82  	}
    83  	return meta, nil
    84  }
    85  
    86  func EncodeMetalocFromMetas(name string, blks []objectio.BlockObject) string {
    87  	offset := blks[0].GetExtent().Offset()
    88  	length := blks[0].GetExtent().Length()
    89  	var buf bytes.Buffer
    90  	_, _ = buf.WriteString(name)
    91  	for _, blk := range blks {
    92  		_, _ = buf.WriteString(fmt.Sprintf(":%d_%d_%d_%d",
    93  			offset,
    94  			length,
    95  			length,
    96  			blk.GetExtent().Id()))
    97  	}
    98  	return buf.String()
    99  }
   100  
   101  func DecodeMetaLocToMetas(metaLoc string) (string, []objectio.Extent, error) {
   102  	info := strings.Split(metaLoc, ":")
   103  	name := info[0]
   104  	extents := make([]objectio.Extent, 0)
   105  	for i := 1; i < len(info); i++ {
   106  		location := strings.Split(info[i], "_")
   107  		offset, err := strconv.ParseUint(location[0], 10, 32)
   108  		if err != nil {
   109  			return "", nil, err
   110  		}
   111  		size, err := strconv.ParseUint(location[1], 10, 32)
   112  		if err != nil {
   113  			return "", nil, err
   114  		}
   115  		osize, err := strconv.ParseUint(location[2], 10, 32)
   116  		if err != nil {
   117  			return "", nil, err
   118  		}
   119  		id, err := strconv.ParseUint(location[3], 10, 32)
   120  		if err != nil {
   121  			return "", nil, err
   122  		}
   123  		extent := objectio.NewExtent(uint32(id), uint32(offset), uint32(size), uint32(osize))
   124  		extents = append(extents, extent)
   125  	}
   126  	return name, extents, nil
   127  }
   128  
   129  func DecodeDeltaLocToDelta(metaLoc string) (*Delta, error) {
   130  	info := strings.Split(metaLoc, ":")
   131  	name := info[0]
   132  	location := strings.Split(info[1], "_")
   133  	offset, err := strconv.ParseUint(location[0], 10, 32)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	size, err := strconv.ParseUint(location[1], 10, 32)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	osize, err := strconv.ParseUint(location[2], 10, 32)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	objectSize, err := strconv.ParseUint(info[3], 10, 32)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	extent := objectio.NewExtent(0, uint32(offset), uint32(size), uint32(osize))
   150  	delta := &Delta{
   151  		key:        name,
   152  		loc:        extent,
   153  		objectSize: uint32(objectSize),
   154  	}
   155  	return delta, nil
   156  }