Permalink
Browse files

More reusable suffix finding.

  • Loading branch information...
dustin committed Apr 10, 2012
1 parent fc31f9a commit 1b5e92fe87421d96fb88dded5f2a76c2da49cabf
Showing with 43 additions and 37 deletions.
  1. +4 −18 pools.go
  2. +5 −5 pools_test.go
  3. +5 −14 populate/populate.go
  4. +23 −0 util.go
  5. +6 −0 util_test.go
View
@@ -88,25 +88,11 @@ func (b Bucket) NodeAddresses() []string {
// Get the longest common suffix of all host:port strings in the node list.
func (b Bucket) CommonAddressSuffix() string {
- rv := ""
- if len(b.VBucketServerMap.ServerList) == 0 {
- return ""
- }
- from := b.VBucketServerMap.ServerList
- for i := len(from[0]); i > 0; i-- {
- common := true
- suffix := from[0][i:]
- for _, s := range from {
- if !strings.HasSuffix(s, suffix) {
- common = false
- break
- }
- }
- if common {
- rv = suffix
- }
+ input := []string{}
+ for _, n := range b.Nodes {
+ input = append(input, n.Hostname)
}
- return rv
+ return FindCommonSuffix(input)
}
// The couchbase client gives access to all the things.
View
@@ -300,11 +300,11 @@ func TestCommonAddressSuffixUncommon(t *testing.T) {
func TestCommonAddressSuffixCommon(t *testing.T) {
b := Bucket{}
- b.VBucketServerMap.ServerList = []string{
- "server1.example.com:11210",
- "server2.example.com:11210",
- "server3.example.com:11210",
- "server4.example.com:11210",
+ b.Nodes = []Node{
+ Node{Hostname: "server1.example.com:11210"},
+ Node{Hostname: "server2.example.com:11210"},
+ Node{Hostname: "server3.example.com:11210"},
+ Node{Hostname: "server4.example.com:11210"},
}
assert(t, "useful suffix", ".example.com:11210",
b.CommonAddressSuffix())
View
@@ -53,7 +53,7 @@ type Record struct {
Score int `json:"score"`
}
-func report(c couchbase.Client, b couchbase.Bucket) {
+func report(c couchbase.Client, b *couchbase.Bucket) {
fmt.Printf("-----------------------------------------------------\n")
fmt.Printf("Got %d success messages, %d not-my-vbucket\n",
c.Statuses[0], c.Statuses[7])
@@ -80,7 +80,7 @@ func report(c couchbase.Client, b couchbase.Bucket) {
}
}
-func harass(c couchbase.Client, b couchbase.Bucket) {
+func harass(c couchbase.Client, b *couchbase.Bucket) {
fmt.Printf("Doing stuff\n")
go func() {
@@ -109,20 +109,11 @@ func harass(c couchbase.Client, b couchbase.Bucket) {
func main() {
flag.Parse()
- c, err := couchbase.Connect(flag.Arg(0))
- if err != nil {
- log.Fatalf("Error connecting: %v", err)
- }
-
- pool, err := c.GetPool(*poolName)
- if err != nil {
- log.Fatalf("Error getting pool: %v", err)
- }
-
- bucket, err := pool.GetBucket(*bucketName)
+ bucket, err := couchbase.GetBucket(flag.Arg(0), "default", "default")
if err != nil {
log.Fatalf("Error getting bucket: %v", err)
}
+ defer bucket.Close()
- harass(c, bucket)
+ harass(bucket.Pool, bucket)
}
View
23 util.go
@@ -9,3 +9,26 @@ func CleanupHost(h, commonSuffix string) string {
}
return h
}
+
+// Find the longest common suffix from the given strings.
+func FindCommonSuffix(input []string) string {
+ rv := ""
+ if len(input) < 2 {
+ return ""
+ }
+ from := input
+ for i := len(input[0]); i > 0; i-- {
+ common := true
+ suffix := input[0][i:]
+ for _, s := range from {
+ if !strings.HasSuffix(s, suffix) {
+ common = false
+ break
+ }
+ }
+ if common {
+ rv = suffix
+ }
+ }
+ return rv
+}
View
@@ -12,3 +12,9 @@ func TestCleanupHostEmpty(t *testing.T) {
CleanupHost("server1.example.com:11210", ".example.com:11210"),
"server1")
}
+
+func TestFindCommonSuffix(t *testing.T) {
+ assert(t, "empty", FindCommonSuffix([]string{}), "")
+ assert(t, "one", FindCommonSuffix([]string{"blah"}), "")
+ assert(t, "two", FindCommonSuffix([]string{"blah.com", "foo.com"}), ".com")
+}

0 comments on commit 1b5e92f

Please sign in to comment.