Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table namespace classifier #761

Merged
merged 1 commit into from Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions server/core/tablecodec.go
@@ -0,0 +1,59 @@
// Copyright 2016 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package core

import (
"bytes"
"encoding/binary"
"github.com/juju/errors"
)

var tablePrefix = []byte{'t'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seem we must support index prefix later too.


const signMask uint64 = 0x8000000000000000

// Key represents high-level Key type.
type Key []byte

// HasPrefix tests whether the Key begins with prefix.
func (k Key) HasPrefix(prefix Key) bool {
return bytes.HasPrefix(k, prefix)
}

// DecodeTableID decodes the table ID of the key, if the key is not table key, returns 0.
func DecodeTableID(key Key) int64 {
if !key.HasPrefix(tablePrefix) {
return 0
}
key = key[len(tablePrefix):]
_, tableID, _ := DecodeInt(key)
return tableID
}

// DecodeInt decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeInt(b []byte) ([]byte, int64, error) {
if len(b) < 8 {
return nil, 0, errors.New("insufficient bytes to decode value")
}

u := binary.BigEndian.Uint64(b[:8])
v := decodeCmpUintToInt(u)
b = b[8:]
return b, v, nil
}

func decodeCmpUintToInt(u uint64) int64 {
return int64(u ^ signMask)
}
46 changes: 46 additions & 0 deletions server/tableNamespaceClassifier.go
@@ -0,0 +1,46 @@
package server
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use table_namespace_classifier.go , we don't support CamelCase for the file name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add license header.


import (
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
)

type tableNamespaceClassifier struct {
nsInfo *namespacesInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is namespaceInfo defined? Forget to submit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in another PR #747

Saving/Loading namespace and classifier are separated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is supposed to merge into dantin/namespace

}

func newTableNamespaceClassifier(nsInfo *namespacesInfo) tableNamespaceClassifier {
return tableNamespaceClassifier{
nsInfo,
}
}

func (classifier tableNamespaceClassifier) GetAllNamespaces() []string {
nsList := make([]string, len(classifier.nsInfo.namespaces))
for name := range classifier.nsInfo.namespaces {
nsList = append(nsList, name)
}
return nsList
}

func (classifier tableNamespaceClassifier) GetStoreNamespace(storeInfo *core.StoreInfo) string {
for name, ns := range classifier.nsInfo.namespaces {
if storeInfo.Id == ns.ID {
return name
}
}
return namespace.DefaultNamespace
}

func (classifier tableNamespaceClassifier) GetRegionNamespace(regionInfo *core.RegionInfo) string {
for name, ns := range classifier.nsInfo.namespaces {
startTable := core.DecodeTableID(regionInfo.StartKey)
endTable := core.DecodeTableID(regionInfo.EndKey)
for _, tableID := range ns.TableIDs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have many tables in one namespace, seem this range check will hurt the performance.
Maybe is it better to use a hash map for TableIDs ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok. Because the number of regions may be a lot, now we always try the best to avoid traversing regions.

if tableID == startTable && tableID == endTable {
return name
}
}
}
return namespace.DefaultNamespace
}