Skip to content

Commit

Permalink
*: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Nov 19, 2015
1 parent 549dc3b commit bdf08bf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.
42 changes: 42 additions & 0 deletions store/hbase/hbase_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2015 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 hbasekv

import (
"strings"

. "github.com/pingcap/check"
)

var _ = Suite(&testHBaseSuite{})

type testHBaseSuite struct {
}

func (t *testHBaseSuite) TestParseDSN(c *C) {
zks, oracle, table, err := parseDSN("zk1.com,zk2,192.168.0.1|localhost:1234/tidb")
c.Assert(strings.Join(zks, " "), Equals, "zk1.com zk2 192.168.0.1")
c.Assert(oracle, Equals, "localhost:1234")
c.Assert(table, Equals, "tidb")
c.Assert(err, IsNil)

zks, oracle, table, err = parseDSN("zk1,zk2/tidb")
c.Assert(strings.Join(zks, " "), Equals, "zk1 zk2")
c.Assert(oracle, Equals, "")
c.Assert(table, Equals, "tidb")
c.Assert(err, IsNil)

_, _, _, err = parseDSN("zk1,zk2|localhost:1234")
c.Assert(err, NotNil)
}
52 changes: 28 additions & 24 deletions store/hbase/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ var (
)

var (
// ErrDsnInvalid is returned when store dsn is invalid.
ErrDsnInvalid = errors.New("dsn invalid")
// ErrInvalidDSN is returned when store dsn is invalid.
ErrInvalidDSN = errors.New("invalid dsn")
)

type storeCache struct {
Expand Down Expand Up @@ -141,30 +141,16 @@ func (d Driver) Open(dsn string) (kv.Storage, error) {
mc.mu.Lock()
defer mc.mu.Unlock()

if len(dsn) == 0 {
return nil, errors.Trace(ErrDsnInvalid)
}
pos := strings.LastIndex(dsn, "/")
if pos == -1 {
return nil, errors.Trace(ErrDsnInvalid)
}
tableName := dsn[pos+1:]
addrs := dsn[:pos]

var tsoAddr string
pos = strings.LastIndex(addrs, "|")
if pos != -1 {
tsoAddr = addrs[pos+1:]
addrs = addrs[:pos]
}

zks := strings.Split(addrs, ",")

if store, ok := mc.cache[dsn]; ok {
// TODO: check the cache store has the same engine with this Driver.
return store, nil
}

zks, oracleAddr, tableName, err := parseDSN(dsn)
if err != nil {
return nil, errors.Trace(err)
}

// create buffered HBase connections, HBaseClient is goroutine-safe, so
// it's OK to redistribute to transactions.
conns := make([]hbase.HBaseClient, 0, hbaseConnPoolSize)
Expand All @@ -190,10 +176,10 @@ func (d Driver) Open(dsn string) (kv.Storage, error) {
}

var ora oracle.Oracle
if tsoAddr == "" {
ora = &oracles.LocalOracle{}
if oracleAddr == "" {
ora = oracles.NewLocalOracle()
} else {
ora = oracles.NewTsoOracle(tsoAddr)
ora = oracles.NewRemoteOracle(oracleAddr)
}

s := &hbaseStore{
Expand All @@ -205,3 +191,21 @@ func (d Driver) Open(dsn string) (kv.Storage, error) {
mc.cache[dsn] = s
return s, nil
}

func parseDSN(dsn string) (zks []string, oracleAddr, tableName string, err error) {
pos := strings.LastIndex(dsn, "/")
if pos == -1 {
err = errors.Trace(ErrInvalidDSN)
return
}
tableName = dsn[pos+1:]
addrs := dsn[:pos]

pos = strings.LastIndex(addrs, "|")
if pos != -1 {
oracleAddr = addrs[pos+1:]
addrs = addrs[:pos]
}
zks = strings.Split(addrs, ",")
return
}

0 comments on commit bdf08bf

Please sign in to comment.