Skip to content

Commit

Permalink
codebase ported.
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed Feb 7, 2017
0 parents commit 1e83472
Show file tree
Hide file tree
Showing 35 changed files with 8,734 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
debug
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "specifications"]
path = specifications
url = git@github.com:mongodb/specifications.git
144 changes: 144 additions & 0 deletions core/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package core

import (
"sort"
"strings"
)

// NewCluster creates a new Cluster.
func NewCluster(monitor *ClusterMonitor) Cluster {
return &clusterImpl{
monitor: monitor,
}
}

// Cluster represents a connection to a cluster.
type Cluster interface {
// Desc gets a description of the cluster.
Desc() *ClusterDesc
// SelectServer selects a server given a selector.
SelectServer(ServerSelector) Server
}

// ClusterDesc is a description of a cluster.
type ClusterDesc struct {
clusterType ClusterType
servers []*ServerDesc
}

// Server returns the ServerDesc with the specified endpoint.
func (d *ClusterDesc) Server(endpoint Endpoint) (*ServerDesc, bool) {
for _, server := range d.servers {
if server.endpoint == endpoint {
return server, true
}
}
return nil, false
}

// Servers are the known servers that are part of the cluster.
func (d *ClusterDesc) Servers() []*ServerDesc {
new := make([]*ServerDesc, len(d.servers))
copy(new, d.servers)
return new
}

// Type is the type of the cluster.
func (d *ClusterDesc) Type() ClusterType {
return d.clusterType
}

// ClusterType represents a type of the cluster.
type ClusterType uint32

// ServerType constants.
const (
UnknownClusterType ClusterType = 0
Single ClusterType = 1
ReplicaSet ClusterType = 2
ReplicaSetNoPrimary ClusterType = 4 + ReplicaSet
ReplicaSetWithPrimary ClusterType = 8 + ReplicaSet
Sharded ClusterType = 256
)

// ServerSelector is a function that selects a server.
type ServerSelector func(*ClusterDesc, []*ServerDesc) []*ServerDesc

func diffClusterDesc(old, new *ClusterDesc) clusterDescDiff {
var diff clusterDescDiff
oldServers := serverDescSorter(old.Servers())
newServers := serverDescSorter(new.Servers())

sort.Sort(oldServers)
sort.Sort(newServers)

i := 0
j := 0
for {
if i < len(oldServers) && j < len(newServers) {
comp := strings.Compare(string(oldServers[i].endpoint), string(newServers[j].endpoint))
switch comp {
case 1:
//left is bigger than
diff.AddedServers = append(diff.AddedServers, newServers[j])
j++
case -1:
// right is bigger
diff.RemovedServers = append(diff.RemovedServers, oldServers[i])
i++
case 0:
i++
j++
}
} else if i < len(oldServers) {
diff.RemovedServers = append(diff.RemovedServers, oldServers[i])
i++
} else if j < len(newServers) {
diff.AddedServers = append(diff.AddedServers, newServers[j])
j++
} else {
break
}
}

return diff
}

type clusterDescDiff struct {
AddedServers []*ServerDesc
RemovedServers []*ServerDesc
}

type serverDescSorter []*ServerDesc

func (x serverDescSorter) Len() int { return len(x) }
func (x serverDescSorter) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x serverDescSorter) Less(i, j int) bool {
return strings.Compare(string(x[i].endpoint), string(x[j].endpoint)) < 0
}

type clusterImpl struct {
monitor *ClusterMonitor
}

func (c *clusterImpl) Desc() *ClusterDesc {
return c.monitor.Desc()
}

func (c *clusterImpl) SelectServer(selector ServerSelector) Server {
clusterDesc := c.Desc()
selected := selector(clusterDesc, clusterDesc.Servers())
return &serverImpl{
cluster: c,
connectionOpts: c.monitor.serverOptionsFactory(selected[0].endpoint).ConnectionOptions,
}
}

type serverImpl struct {
cluster *clusterImpl
connectionOpts ConnectionOptions
}

func (s *serverImpl) Connection() (Connection, error) {
return DialConnection(s.connectionOpts)
}
Loading

0 comments on commit 1e83472

Please sign in to comment.