github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/internal/structure/string.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     2  //
     3  // Copyright 2015 PingCAP, Inc.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package structure
    18  
    19  import (
    20  	"context"
    21  	"strconv"
    22  
    23  	"github.com/pingcap/errors"
    24  	"github.com/vescale/zgraph/storage/kv"
    25  )
    26  
    27  // Set sets the string value of the key.
    28  func (t *TxStructure) Set(key []byte, value []byte) error {
    29  	if t.readWriter == nil {
    30  		return ErrWriteOnSnapshot
    31  	}
    32  	ek := t.encodeStringDataKey(key)
    33  	return t.readWriter.Set(ek, value)
    34  }
    35  
    36  // Get gets the string value of a key.
    37  func (t *TxStructure) Get(key []byte) ([]byte, error) {
    38  	ek := t.encodeStringDataKey(key)
    39  	value, err := t.reader.Get(context.TODO(), ek)
    40  	if errors.Cause(err) == kv.ErrNotExist {
    41  		err = nil
    42  	}
    43  	return value, errors.Trace(err)
    44  }
    45  
    46  // GetInt64 gets the int64 value of a key.
    47  func (t *TxStructure) GetInt64(key []byte) (int64, error) {
    48  	v, err := t.Get(key)
    49  	if err != nil || v == nil {
    50  		return 0, errors.Trace(err)
    51  	}
    52  
    53  	n, err := strconv.ParseInt(string(v), 10, 64)
    54  	return n, errors.Trace(err)
    55  }
    56  
    57  // Inc increments the integer value of a key by step, returns
    58  // the value after the increment.
    59  func (t *TxStructure) Inc(key []byte, step int64) (int64, error) {
    60  	if t.readWriter == nil {
    61  		return 0, ErrWriteOnSnapshot
    62  	}
    63  	ek := t.encodeStringDataKey(key)
    64  	// txn Inc will lock this key, so we don't lock it here.
    65  	n, err := IncInt64(t.readWriter, ek, step)
    66  	if errors.Cause(err) == kv.ErrNotExist {
    67  		err = nil
    68  	}
    69  	return n, errors.Trace(err)
    70  }
    71  
    72  // Clear removes the string value of the key.
    73  func (t *TxStructure) Clear(key []byte) error {
    74  	if t.readWriter == nil {
    75  		return ErrWriteOnSnapshot
    76  	}
    77  	ek := t.encodeStringDataKey(key)
    78  	err := t.readWriter.Delete(ek)
    79  	if errors.Cause(err) == kv.ErrNotExist {
    80  		err = nil
    81  	}
    82  	return errors.Trace(err)
    83  }
    84  
    85  // IncInt64 increases the value for key k in kv store by step.
    86  func IncInt64(rm kv.RetrieverMutator, k kv.Key, step int64) (int64, error) {
    87  	val, err := rm.Get(context.TODO(), k)
    88  	if errors.Cause(err) == kv.ErrNotExist {
    89  		err = rm.Set(k, []byte(strconv.FormatInt(step, 10)))
    90  		if err != nil {
    91  			return 0, err
    92  		}
    93  		return step, nil
    94  	}
    95  	if err != nil {
    96  		return 0, err
    97  	}
    98  
    99  	intVal, err := strconv.ParseInt(string(val), 10, 64)
   100  	if err != nil {
   101  		return 0, errors.Trace(err)
   102  	}
   103  
   104  	intVal += step
   105  	err = rm.Set(k, []byte(strconv.FormatInt(intVal, 10)))
   106  	if err != nil {
   107  		return 0, err
   108  	}
   109  	return intVal, nil
   110  }