Esempio n. 1
0
        public static void MapReduce()
        {
            var databaseName = new DatabaseNamespace(DbConnection.Db.DatabaseNamespace.DatabaseName);

            var code = (BsonJavaScript) @"
                function () {
                    var docs = db.Entry.find({isShown: true}, {'publishDate':1});
                    var length = docs.count();
                    var merged = { };
                    for (var i = 0; i < length; i++)
                    {
                        var value = docs[i].publishDate;
                        var date = (new Date(value)).getUTCFullYear().toString() + '/' + ('0' + ((new Date(value)).getUTCMonth() + 1)).slice(-2);
                        if (date in merged) {
                            merged[date] += 1;
                        } else {
                            merged[date] = 1;
                        }
                    }
                    db.DateSummary.remove({});
                    db.DateSummary.insert({_id:'dateGrouping', value:merged});
                }";

            var messageEncodingSettings = new MessageEncoderSettings();
            var operation    = new EvalOperation(databaseName, code, messageEncodingSettings);
            var session      = new CoreSessionHandle(NoCoreSession.Instance);
            var writeBinding = new WritableServerBinding(DbConnection.Db.Client.Cluster, session);

            operation.Execute(writeBinding, CancellationToken.None);
        }
Esempio n. 2
0
        public static void MapReduce()
        {
            var databaseName = new DatabaseNamespace(DbConnection.Db.DatabaseNamespace.DatabaseName);

            var code = (BsonJavaScript) @"
                function () {
                    var docs = db.Entry.find({isShown: true}, {'tag':1});
                    var length = docs.count();
                    var tags = [];
                    for (var i = 0; i < length; i++)
                    {
                        var doc = docs[i];
                        tags = tags.concat(doc.tag);
                    }
                    var merged = { };
                    for (var i = 0; i < tags.length; i++)
                    {
                        var tag = tags[i].replace('.', '\uff0e');
                        if (tag in merged) {
                            merged[tag] += 1;
                        } else {
                            merged[tag] = 1;
                        }
                    }
                    db.TagSummary.remove({});
                    db.TagSummary.insert({_id:'tag', value:merged});
                }";

            var messageEncodingSettings = new MessageEncoderSettings();
            var operation    = new EvalOperation(databaseName, code, messageEncodingSettings);
            var session      = new CoreSessionHandle(NoCoreSession.Instance);
            var writeBinding = new WritableServerBinding(DbConnection.Db.Client.Cluster, session);

            operation.Execute(writeBinding, CancellationToken.None);
        }
Esempio n. 3
0
        public IClientSessionHandle StartImplicitSession(CancellationToken cancellationToken)
        {
            var options           = new ClientSessionOptions();
            var coreServerSession = new CoreServerSession();
            var coreSession       = new CoreSession(coreServerSession, options.ToCore(isImplicit: true));
            var coreSessionHandle = new CoreSessionHandle(coreSession);

            return(new ClientSessionHandle(_client, options, coreSessionHandle));
        }
Esempio n. 4
0
        // private methods
        private IClientSessionHandle CreateClientSession()
        {
            var client            = new Mock <IMongoClient>().Object;
            var options           = new ClientSessionOptions();
            var coreServerSession = new CoreServerSession();
            var coreSession       = new CoreSession(coreServerSession, options.ToCore());
            var coreSessionHandle = new CoreSessionHandle(coreSession);

            return(new ClientSessionHandle(client, options, coreSessionHandle));
        }
        /// <summary>
        /// Evaluates the specified javascript within a MongoDb database.
        /// </summary>
        /// <param name="database">MongoDb Database to execute the javascript.</param>
        /// <param name="javascript">Javascript to execute.</param>
        /// <param name="args">Optional arguments that can be provided to the javascript function to execute.</param>
        /// <returns>A <see cref="BsonValue" /> result.</returns>
        public static BsonValue Eval(this IMongoDatabase database, string javascript, IEnumerable <BsonValue> args = null)
        {
            var op = GetEvalOperation(database, javascript, args);

            using (var sessionHandle = new CoreSessionHandle(NoCoreSession.Instance))
            {
                using (var writeBinding = new WritableServerBinding(database.Client.Cluster, sessionHandle))
                {
                    return(op.Execute(writeBinding, CancellationToken.None));
                }
            }
        }
Esempio n. 6
0
        public IClientSessionHandle StartImplicitSession(CancellationToken cancellationToken)
        {
            var cluster           = Mock.Of <ICluster>();
            var options           = new ClientSessionOptions();
            var coreServerSession = new CoreServerSession();

#pragma warning disable CS0618 // Type or member is obsolete
            var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore(isImplicit: true));
#pragma warning restore CS0618 // Type or member is obsolete
            var coreSessionHandle = new CoreSessionHandle(coreSession);
            return(new ClientSessionHandle(_client, options, coreSessionHandle));
        }
        // private methods
        private IClientSessionHandle CreateClientSession()
        {
            var client            = new Mock <IMongoClient>().Object;
            var options           = new ClientSessionOptions();
            var cluster           = Mock.Of <ICluster>();
            var coreServerSession = new CoreServerSession();

#pragma warning disable CS0618 // Type or member is obsolete
            var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
#pragma warning restore CS0618 // Type or member is obsolete
            var coreSessionHandle = new CoreSessionHandle(coreSession);
            return(new ClientSessionHandle(client, options, coreSessionHandle));
        }
Esempio n. 8
0
 // private methods
 private IClientSessionHandle CreateSession(bool usingSession)
 {
     if (usingSession)
     {
         var client            = new Mock <IMongoClient>().Object;
         var cluster           = Mock.Of <ICluster>();
         var options           = new ClientSessionOptions();
         var coreServerSession = new CoreServerSession();
         var coreSession       = new CoreSession(cluster, coreServerSession, options.ToCore());
         var coreSessionHandle = new CoreSessionHandle(coreSession);
         return(new ClientSessionHandle(client, options, coreSessionHandle));
     }
     else
     {
         return(null);
     }
 }
Esempio n. 9
0
 // private methods
 private IClientSessionHandle CreateSession(bool usingSession)
 {
     if (usingSession)
     {
         var client            = Mock.Of <IMongoClient>();
         var options           = new ClientSessionOptions();
         var coreSession       = Mock.Of <ICoreSession>();
         var coreServerSession = Mock.Of <ICoreServerSession>();
         Mock.Get(coreSession).SetupGet(m => m.ServerSession).Returns(coreServerSession);
         var coreSessionHandle = new CoreSessionHandle(coreSession);
         return(new ClientSessionHandle(client, options, coreSessionHandle));
     }
     else
     {
         return(null);
     }
 }
Esempio n. 10
0
        public void Fork_should_return_expected_result()
        {
            var coreServerSession = new CoreServerSession();
            var options           = new ClientSessionOptions();
            var coreSession       = new CoreSession(coreServerSession, options.ToCore());
            var coreSessionHandle = new CoreSessionHandle(coreSession);
            var subject           = CreateSubject(coreSession: coreSessionHandle);

            coreSessionHandle.ReferenceCount().Should().Be(1);

            var result = subject.Fork();

            result.Client.Should().BeSameAs(subject.Client);
            result.Options.Should().BeSameAs(subject.Options);
            result.WrappedCoreSession.Should().NotBeSameAs(subject.WrappedCoreSession);
            var coreSessionHandle1 = (CoreSessionHandle)subject.WrappedCoreSession;
            var coreSessionHandle2 = (CoreSessionHandle)result.WrappedCoreSession;

            coreSessionHandle2.Wrapped.Should().BeSameAs(coreSessionHandle1.Wrapped);
            coreSessionHandle.ReferenceCount().Should().Be(2);
        }
Esempio n. 11
0
        public void Fork_should_return_expected_result()
        {
            var cluster           = Mock.Of <ICluster>();
            var coreServerSession = new CoreServerSession();
            var options           = new ClientSessionOptions();

#pragma warning disable CS0618 // Type or member is obsolete
            var coreSession = new CoreSession(cluster, coreServerSession, options.ToCore());
#pragma warning restore CS0618 // Type or member is obsolete
            var coreSessionHandle = new CoreSessionHandle(coreSession);
            var subject           = CreateSubject(coreSession: coreSessionHandle);
            coreSessionHandle.ReferenceCount().Should().Be(1);

            var result = subject.Fork();

            result.Client.Should().BeSameAs(subject.Client);
            result.Options.Should().BeSameAs(subject.Options);
            result.WrappedCoreSession.Should().NotBeSameAs(subject.WrappedCoreSession);
            var coreSessionHandle1 = (CoreSessionHandle)subject.WrappedCoreSession;
            var coreSessionHandle2 = (CoreSessionHandle)result.WrappedCoreSession;
            coreSessionHandle2.Wrapped.Should().BeSameAs(coreSessionHandle1.Wrapped);
            coreSessionHandle.ReferenceCount().Should().Be(2);
        }
Esempio n. 12
0
        private static ReferenceCountedCoreSession _wrapped(this CoreSessionHandle obj)
        {
            var fieldInfo = typeof(CoreSessionHandle).GetField("_wrapped", BindingFlags.NonPublic | BindingFlags.Instance);

            return((ReferenceCountedCoreSession)fieldInfo.GetValue(obj));
        }