コード例 #1
0
        internal static bool PinChannelSourceAndChannelIfRequired(
            IChannelSourceHandle channelSource,
            IChannelHandle channel,
            ICoreSessionHandle session,
            out IChannelSourceHandle pinnedChannelSource,
            out IChannelHandle pinnedChannel)
        {
            if (IsInLoadBalancedMode(channel.ConnectionDescription))
            {
                var server = channelSource.Server;

                pinnedChannelSource = new ChannelSourceHandle(
                    new ChannelChannelSource(
                        server,
                        channel.Fork(),
                        session.Fork()));

                if (session.IsInTransaction && !IsChannelPinned(session.CurrentTransaction))
                {
                    session.CurrentTransaction.PinChannel(channel.Fork());
                    session.CurrentTransaction.PinnedServer = server;
                }

                pinnedChannel = channel.Fork();

                return(true);
            }

            pinnedChannelSource = null;
            pinnedChannel       = null;
            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Checks whether the server is alive (throws an exception if not).
        /// </summary>
        public void Ping()
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new PingOperation(messageEncoderSettings);

            var server = GetServer();

            using (var channelSource = new ChannelSourceHandle(new ServerChannelSource(server)))
                using (var channelSourceBinding = new ChannelSourceReadWriteBinding(channelSource, ReadPreference.PrimaryPreferred))
                {
                    operation.Execute(channelSourceBinding, CancellationToken.None);
                }
        }
コード例 #3
0
        private IReadOnlyList <IAsyncCursor <TDocument> > CreateCursors(IChannelSourceHandle channelSource, BsonDocument command, BsonDocument result)
        {
            var cursors = new List <AsyncCursor <TDocument> >();

            using (var getMoreChannelSource = new ChannelSourceHandle(new ServerChannelSource(channelSource.Server, channelSource.Session.Fork())))
            {
                foreach (var cursorDocument in result["cursors"].AsBsonArray.Select(v => v["cursor"].AsBsonDocument))
                {
                    var cursorId   = cursorDocument["id"].ToInt64();
                    var firstBatch = cursorDocument["firstBatch"].AsBsonArray.Select(v =>
                    {
                        var bsonDocument = (BsonDocument)v;
                        using (var reader = new BsonDocumentReader(bsonDocument))
                        {
                            var context  = BsonDeserializationContext.CreateRoot(reader);
                            var document = _serializer.Deserialize(context);
                            return(document);
                        }
                    })
                                     .ToList();

                    // it's not affected by loadbalancing logic since it was deprecated in server version 4.1.
                    var cursor = new AsyncCursor <TDocument>(
                        getMoreChannelSource.Fork(),
                        _collectionNamespace,
                        command,
                        firstBatch,
                        cursorId,
                        _batchSize ?? 0,
                        0, // limit
                        _serializer,
                        _messageEncoderSettings);

                    cursors.Add(cursor);
                }
            }

            return(cursors);
        }