@@ -15,19 +15,33 @@ export const MIN_SECONDARY_WRITE_WIRE_VERSION = 13;
1515export type ServerSelector = (
1616 topologyDescription : TopologyDescription ,
1717 servers : ServerDescription [ ] ,
18- deprioritized : ServerDescription [ ]
18+ deprioritized : DeprioritizedServers
1919) => ServerDescription [ ] ;
2020
21- function sdEquals ( a : ServerDescription , b : ServerDescription ) {
22- return a . address === b . address && a . equals ( b ) ;
21+ /** @internal */
22+ export class DeprioritizedServers {
23+ private deprioritized : Set < string > = new Set ( ) ;
24+
25+ constructor ( descriptions ?: Iterable < ServerDescription > ) {
26+ for ( const description of descriptions ?? [ ] ) {
27+ this . add ( description ) ;
28+ }
29+ }
30+
31+ add ( { address } : ServerDescription ) {
32+ this . deprioritized . add ( address ) ;
33+ }
34+
35+ has ( { address } : ServerDescription ) : boolean {
36+ return this . deprioritized . has ( address ) ;
37+ }
2338}
39+
2440function filterDeprioritized (
2541 candidates : ServerDescription [ ] ,
26- deprioritized : ServerDescription [ ]
42+ deprioritized : DeprioritizedServers
2743) : ServerDescription [ ] {
28- const filtered = candidates . filter (
29- candidate => ! deprioritized . some ( serverDescription => sdEquals ( serverDescription , candidate ) )
30- ) ;
44+ const filtered = candidates . filter ( candidate => ! deprioritized . has ( candidate ) ) ;
3145
3246 return filtered . length ? filtered : candidates ;
3347}
@@ -39,7 +53,7 @@ export function writableServerSelector(): ServerSelector {
3953 return function writableServer (
4054 topologyDescription : TopologyDescription ,
4155 servers : ServerDescription [ ] ,
42- deprioritized : ServerDescription [ ]
56+ deprioritized : DeprioritizedServers
4357 ) : ServerDescription [ ] {
4458 const eligibleServers = filterDeprioritized (
4559 servers . filter ( ( { isWritable } ) => isWritable ) ,
@@ -58,7 +72,7 @@ export function sameServerSelector(description?: ServerDescription): ServerSelec
5872 return function sameServerSelector (
5973 _topologyDescription : TopologyDescription ,
6074 servers : ServerDescription [ ] ,
61- _deprioritized : ServerDescription [ ]
75+ _deprioritized : DeprioritizedServers
6276 ) : ServerDescription [ ] {
6377 if ( ! description ) return [ ] ;
6478 // Filter the servers to match the provided description only if
@@ -261,15 +275,11 @@ function loadBalancerFilter(server: ServerDescription): boolean {
261275}
262276
263277function isDeprioritizedFactory (
264- deprioritized : ServerDescription [ ]
278+ deprioritized : DeprioritizedServers
265279) : ( server : ServerDescription ) => boolean {
266280 return server =>
267281 // if any deprioritized servers equal the server, here we are.
268- ! deprioritized . some ( deprioritizedServer => {
269- const result = sdEquals ( deprioritizedServer , server ) ;
270- // console.error(result);
271- return result ;
272- } ) ;
282+ ! deprioritized . has ( server ) ;
273283}
274284
275285/**
@@ -285,7 +295,7 @@ export function readPreferenceServerSelector(readPreference: ReadPreference): Se
285295 return function readPreferenceServers (
286296 topologyDescription : TopologyDescription ,
287297 servers : ServerDescription [ ] ,
288- deprioritized : ServerDescription [ ]
298+ deprioritized : DeprioritizedServers
289299 ) : ServerDescription [ ] {
290300 if ( topologyDescription . type === TopologyType . LoadBalanced ) {
291301 return servers . filter ( loadBalancerFilter ) ;
0 commit comments