Skip to content

Commit

Permalink
update bytes/string convert
Browse files Browse the repository at this point in the history
  • Loading branch information
hailin0 committed Aug 9, 2023
1 parent a14fcca commit 62d5949
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
38 changes: 38 additions & 0 deletions pkg/convert/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package convert

import (
"reflect"
"unsafe"
)

// StringToBytes converts string to bytes.
func StringToBytes(s string) (b []byte) {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}

// BytesToString converts bytes to string.
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
5 changes: 3 additions & 2 deletions pkg/wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pkg/errors"
"go.uber.org/multierr"

"github.com/apache/skywalking-banyandb/pkg/convert"
"github.com/apache/skywalking-banyandb/pkg/encoding"
"github.com/apache/skywalking-banyandb/pkg/logger"
"github.com/apache/skywalking-banyandb/pkg/run"
Expand Down Expand Up @@ -175,15 +176,15 @@ type logSeriesID struct {
}

func newLogSeriesID(b []byte) logSeriesID {
return logSeriesID{key: string(b), byteLen: len(b)}
return logSeriesID{key: convert.BytesToString(b), byteLen: len(b)}
}

func (s logSeriesID) string() string {
return s.key
}

func (s logSeriesID) bytes() []byte {
return []byte(s.key)
return convert.StringToBytes(s.key)
}

func (s logSeriesID) len() int {
Expand Down

0 comments on commit 62d5949

Please sign in to comment.