Skip to content

Commit

Permalink
util/size: define some constants for size (#37468)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Aug 30, 2022
1 parent e8daf1a commit d5c96ce
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ go_library(
"//util/rowcodec",
"//util/sem",
"//util/set",
"//util/size",
"//util/sqlexec",
"//util/stmtsummary",
"//util/stringutil",
Expand Down
17 changes: 6 additions & 11 deletions executor/inspection_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/size"
"github.com/pingcap/tidb/util/sqlexec"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -425,23 +426,17 @@ func (c configInspection) checkTiKVBlockCacheSizeConfig(ctx context.Context, sct
}

func (configInspection) convertReadableSizeToByteSize(sizeStr string) (uint64, error) {
const KB = uint64(1024)
const MB = KB * 1024
const GB = MB * 1024
const TB = GB * 1024
const PB = TB * 1024

rate := uint64(1)
if strings.HasSuffix(sizeStr, "KiB") {
rate = KB
rate = size.KB
} else if strings.HasSuffix(sizeStr, "MiB") {
rate = MB
rate = size.MB
} else if strings.HasSuffix(sizeStr, "GiB") {
rate = GB
rate = size.GB
} else if strings.HasSuffix(sizeStr, "TiB") {
rate = TB
rate = size.TB
} else if strings.HasSuffix(sizeStr, "PiB") {
rate = PB
rate = size.PB
}
if rate != 1 && len(sizeStr) > 3 {
sizeStr = sizeStr[:len(sizeStr)-3]
Expand Down
2 changes: 2 additions & 0 deletions planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ go_library(
"//table/tables",
"//table/temptable",
"//tablecodec",
"//telemetry",
"//types",
"//types/parser_driver",
"//util",
Expand Down Expand Up @@ -242,6 +243,7 @@ go_test(
"//util/chunk",
"//util/collate",
"//util/dbterror",
"//util/hack",
"//util/hint",
"//util/kvcache",
"//util/logutil",
Expand Down
3 changes: 1 addition & 2 deletions telemetry/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ go_test(
flaky = True,
deps = [
"//config",
"//ddl",
"//domain",
"//domain/infosync",
"//kv",
"//parser/model",
"//session",
"//sessionctx",
"//sessionctx/variable",
Expand Down
5 changes: 4 additions & 1 deletion util/set/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ go_library(
],
importpath = "github.com/pingcap/tidb/util/set",
visibility = ["//visibility:public"],
deps = ["//util/hack"],
deps = [
"//util/hack",
"//util/memory",
],
)

go_test(
Expand Down
8 changes: 8 additions & 0 deletions util/size/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "size",
srcs = ["size.go"],
importpath = "github.com/pingcap/tidb/util/size",
visibility = ["//visibility:public"],
)
28 changes: 28 additions & 0 deletions util/size/size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed 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 size

const (
// KB is the kilobytes.
KB = uint64(1024)
// MB is the megabytes.
MB = KB * 1024
// GB is the gigabytes.
GB = MB * 1024
// TB is the terabytes.
TB = GB * 1024
// PB is the petabytes.
PB = TB * 1024
)

0 comments on commit d5c96ce

Please sign in to comment.