github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/structure/string.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package structure 15 16 import ( 17 "context" 18 "strconv" 19 20 "github.com/whtcorpsinc/errors" 21 "github.com/whtcorpsinc/milevadb/ekv" 22 ) 23 24 // Set sets the string value of the key. 25 func (t *TxStructure) Set(key []byte, value []byte) error { 26 if t.readWriter == nil { 27 return ErrWriteOnSnapshot 28 } 29 ek := t.encodeStringDataKey(key) 30 return t.readWriter.Set(ek, value) 31 } 32 33 // Get gets the string value of a key. 34 func (t *TxStructure) Get(key []byte) ([]byte, error) { 35 ek := t.encodeStringDataKey(key) 36 value, err := t.reader.Get(context.TODO(), ek) 37 if ekv.ErrNotExist.Equal(err) { 38 err = nil 39 } 40 return value, errors.Trace(err) 41 } 42 43 // GetInt64 gets the int64 value of a key. 44 func (t *TxStructure) GetInt64(key []byte) (int64, error) { 45 v, err := t.Get(key) 46 if err != nil || v == nil { 47 return 0, errors.Trace(err) 48 } 49 50 n, err := strconv.ParseInt(string(v), 10, 64) 51 return n, errors.Trace(err) 52 } 53 54 // Inc increments the integer value of a key by step, returns 55 // the value after the increment. 56 func (t *TxStructure) Inc(key []byte, step int64) (int64, error) { 57 if t.readWriter == nil { 58 return 0, ErrWriteOnSnapshot 59 } 60 ek := t.encodeStringDataKey(key) 61 // txn Inc will dagger this key, so we don't dagger it here. 62 n, err := ekv.IncInt64(t.readWriter, ek, step) 63 if ekv.ErrNotExist.Equal(err) { 64 err = nil 65 } 66 return n, errors.Trace(err) 67 } 68 69 // Clear removes the string value of the key. 70 func (t *TxStructure) Clear(key []byte) error { 71 if t.readWriter == nil { 72 return ErrWriteOnSnapshot 73 } 74 ek := t.encodeStringDataKey(key) 75 err := t.readWriter.Delete(ek) 76 if ekv.ErrNotExist.Equal(err) { 77 err = nil 78 } 79 return errors.Trace(err) 80 }