forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1e83472
Showing
35 changed files
with
8,734 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.