private BsonValue ExecuteOperation(EvalOperation operation, IWriteBinding binding, bool async) { if (async) { return operation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult(); } else { return operation.Execute(binding, CancellationToken.None); } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { if (this.checkpointTrigger != null) { this.checkpointTrigger.Dispose(); this.checkpointTrigger = null; } if (this.learnBlock != null) { // complete beginning of the pipeline this.deserializeBlock.Complete(); // wait at the end of the pipeline this.checkpointBlock.Completion.Wait(TimeSpan.FromMinutes(1)); } if (this.evalOperation != null) { this.evalOperation.Dispose(); this.evalOperation = null; } if (this.latencyOperation != null) { this.latencyOperation.Dispose(); this.latencyOperation = null; } }
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); }
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); }
public BsonValue RunScript(string command, CancellationToken cancellationToken) { var script = new BsonJavaScript(command); var operation = new EvalOperation(_database.DatabaseNamespace, script, null); var writeBinding = new WritableServerBinding(_client.Cluster, NoCoreSession.NewHandle()); return(operation.Execute(writeBinding, CancellationToken.None)); }
static EvalOperation GetEvalOperation(IMongoDatabase mongoDB, string javascript, IEnumerable <BsonValue> args) { var function = new BsonJavaScript(javascript); var op = new EvalOperation(mongoDB.DatabaseNamespace, function, null); op.Args = args; return(op); }
public Task <BsonValue> RunScriptAsync(string command, CancellationToken cancellationToken) { var script = new BsonJavaScript(command); var operation = new EvalOperation(_database.DatabaseNamespace, script, null); var writeBinding = new WritableServerBinding(_client.Cluster); return(operation.ExecuteAsync(writeBinding, CancellationToken.None)); }
protected async Task <BsonValue> EvalAsync(string javascript) { var function = new BsonJavaScript(javascript); var op = new EvalOperation(this._database.DatabaseNamespace, function, null); using (var writeBinding = new WritableServerBinding(this._client.Cluster, new CoreSessionHandle(NoCoreSession.Instance))) { return(await op.ExecuteAsync(writeBinding, CancellationToken.None)); } }
public void Args_should_work() { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); var args = new BsonValue[] { 1, 2, 3 }; subject.Args = args; subject.Args.Should().Equal(args); }
public async Task <BsonValue> ExecuteScriptAsync(string javascript) { var function = new BsonJavaScript(javascript); EvalOperation op = new EvalOperation(database.DatabaseNamespace, function, new MessageEncoderSettings { }); using (var writeBinding = new WritableServerBinding(database.Client.Cluster, new CoreSessionHandle(NoCoreSession.Instance))) { var result = await op.ExecuteAsync(writeBinding, CancellationToken.None); return(result); } }
public void constructor_should_initialize_subject() { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); subject.Args.Should().BeNull(); subject.DatabaseNamespace.Should().Be(_adminDatabaseNamespace); subject.Function.Should().Be(function); subject.MaxTime.Should().NotHaveValue(); // subject.MessageEncoderSettings.Should().Be(_messageEncoderSettings); Assert.Equal(_messageEncoderSettings, subject.MessageEncoderSettings); subject.NoLock.Should().NotHaveValue(); }
/// <summary> /// /// </summary> /// <param name="text"></param> /// <returns></returns> public async Task <BsonValue> EvalAsync(string text) { var client = _database as MongoClient; if (client == null) { throw new ArgumentException("Client is not a MongoClient"); } var function = new BsonJavaScript(text); var op = new EvalOperation(_database.DatabaseNamespace, function, null); using (var writeBinding = new WritableServerBinding(client.Cluster, new CoreSessionHandle(NoCoreSession.Instance))) { return(await op.ExecuteAsync(writeBinding, CancellationToken.None)); } }
public IActionResult RunScript(string query) { if (string.IsNullOrEmpty(query)) return Json(new { Result = false, Message = "Empty query!" }); try { var bscript = new BsonJavaScript(query); var operation = new EvalOperation(_mongoDBContext.Database().DatabaseNamespace, bscript, null); var writeBinding = new WritableServerBinding(_mongoDBContext.Database().Client.Cluster, NoCoreSession.NewHandle()); var result = operation.Execute(writeBinding, CancellationToken.None); var xx = result["_ns"]; return Json(new { Result = true, Message = result.ToString() }); } catch (Exception ex) { return Json(new { Result = false, Message = ex.Message }); } }
private BsonValue Eval(IClientSessionHandle session, EvalArgs args) { if (args == null) { throw new ArgumentNullException("args"); } if (args.Code == null) { throw new ArgumentException("Code is null.", "args"); } var operation = new EvalOperation(_namespace, args.Code, GetMessageEncoderSettings()) { Args = args.Args, MaxTime = args.MaxTime, NoLock = args.Lock.HasValue ? !args.Lock : null }; return(ExecuteWriteOperation(session, operation)); }
/// <summary> /// Evaluates JavaScript code at the server. /// </summary> /// <param name="args">The args.</param> /// <returns>The result of evaluating the code.</returns> public virtual BsonValue Eval(EvalArgs args) { if (args == null) { throw new ArgumentNullException("args"); } if (args.Code == null) { throw new ArgumentException("Code is null.", "args"); } var operation = new EvalOperation(_namespace, args.Code, GetMessageEncoderSettings()) { Args = args.Args, MaxTime = args.MaxTime, NoLock = args.Lock.HasValue ? !args.Lock : null }; using (var binding = _server.GetWriteBinding()) { return(operation.Execute(binding)); } }
public void Execute_should_return_expected_result_when_args_are_provided( [Values(false, true)] bool async) { RequireServer.Check().Authentication(false); var function = "function(x) { return x; }"; var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); subject.Args = new BsonValue[] { 1 }; var result = ExecuteOperation(subject, async); result.Should().Be(1.0); }
public void CreateCommand_should_return_expected_result() { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); var expectedResult = new BsonDocument { { "$eval", function } }; var result = subject.CreateCommand(); result.Should().Be(expectedResult); }
public void ExecuteAsync_should_throw_when_binding_isNull() { var function = "return 1"; var subject = new EvalOperation(_databaseNamespace, function, _messageEncoderSettings); Action action = () => subject.ExecuteAsync(null, CancellationToken.None).GetAwaiter().GetResult(); action.ShouldThrow<ArgumentNullException>(); }
internal TrainEventProcessorFactory(OnlineTrainerSettingsInternal settings, Learner trainer, PerformanceCounters performanceCounters) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (trainer == null) { throw new ArgumentNullException(nameof(trainer)); } if (performanceCounters == null) { throw new ArgumentNullException(nameof(performanceCounters)); } this.trainer = trainer; this.performanceCounters = performanceCounters; this.telemetry = new TelemetryClient(); this.telemetry.Context.Component.Version = GetType().Assembly.GetName().Version.ToString(); this.evalOperation = new EvalOperation(settings, performanceCounters); this.latencyOperation = new LatencyOperation(); this.deserializeBlock = new TransformManyBlock <PipelineData, PipelineData>( (Func <PipelineData, IEnumerable <PipelineData> >) this.Stage1_Deserialize, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4, // Math.Max(2, Environment.ProcessorCount - 1), BoundedCapacity = 1024 }); this.deserializeBlock.Completion.Trace(this.telemetry, "Stage 1 - Deserialization"); this.learnBlock = new TransformManyBlock <object, object>( (Func <object, IEnumerable <object> >) this.Stage2_ProcessEvent, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1, BoundedCapacity = 1024 }); this.learnBlock.Completion.Trace(this.telemetry, "Stage 2 - Learning"); // trigger checkpoint checking every second this.checkpointTrigger = Observable.Interval(TimeSpan.FromSeconds(1)) .Select(_ => new CheckpointEvaluateTriggerEvent()) .Subscribe(this.learnBlock.AsObserver()); this.checkpointBlock = new ActionBlock <object>( this.trainer.Checkpoint, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1, BoundedCapacity = 4 }); this.learnBlock.Completion.Trace(this.telemetry, "Stage 3 - CheckPointing"); // setup pipeline this.deserializeBlock.LinkTo( this.learnBlock, new DataflowLinkOptions { PropagateCompletion = true }); this.learnBlock.LinkTo( this.evalOperation.TargetBlock, new DataflowLinkOptions { PropagateCompletion = true }, obj => obj is TrainerResult); this.learnBlock.LinkTo( this.checkpointBlock, new DataflowLinkOptions { PropagateCompletion = true }, obj => obj is CheckpointData); // consume all unmatched this.learnBlock.LinkTo(DataflowBlock.NullTarget <object>()); }
public void Execute_should_return_expected_result( [Values(false, true)] bool async) { var function = "return 1"; var subject = new EvalOperation(_databaseNamespace, function, _messageEncoderSettings); var result = ExecuteOperation(subject, async); result.Should().Be(1.0); }
// private methods private BsonValue ExecuteOperation(EvalOperation operation, bool async) { using (var binding = CoreTestConfiguration.GetReadWriteBinding()) { return ExecuteOperation(operation, binding, async); } }
public void NoLock_should_work( [Values(null, false, true)] bool? value) { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); subject.NoLock = value; subject.NoLock.Should().Be(value); }
public void MaxTime_should_work( [Values(null, 0, 1)] int? value) { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); var maxTime = value.HasValue ? (TimeSpan?)TimeSpan.FromSeconds(value.Value) : null; subject.MaxTime = maxTime; subject.MaxTime.Should().Be(maxTime); }
public void Execute_should_throw_when_binding_isNull( [Values(false, true)] bool async) { var function = "return 1"; var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); Action action = () => ExecuteOperation(subject, null, async); action.ShouldThrow<ArgumentNullException>(); }
public void Execute_should_return_expected_result_when_noLock_is_provided( [Values(false, true)] bool async) { RequireServer.Check().Authentication(false); var function = "return 1"; var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); subject.NoLock = true; var result = ExecuteOperation(subject, async); result.Should().Be(1.0); }
public void Execute_should_return_expected_result( [Values(false, true)] bool async) { RequireServer.Where(authentication: AuthenticationRequirement.Off); var function = "return 1"; var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); var result = ExecuteOperation(subject, async); result.Should().Be(1.0); }
public void CreateCommand_should_return_expected_result_when_noLock_is_provided() { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); subject.NoLock = true; var expectedResult = new BsonDocument { { "$eval", function }, { "nolock", true } }; var result = subject.CreateCommand(); result.Should().Be(expectedResult); }
public void CreateCommand_should_return_expected_result_when_maxTime_is_provided() { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); subject.MaxTime = TimeSpan.FromSeconds(1); var expectedResult = new BsonDocument { { "$eval", function }, { "maxTimeMS", 1000.0 } }; var result = subject.CreateCommand(); result.Should().Be(expectedResult); }
public void CreateCommand_should_return_expected_result_when_args_are_provided() { var function = new BsonJavaScript("return 1"); var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings); var args = new BsonValue[] { 1, 2, 3 }; subject.Args = args; var expectedResult = new BsonDocument { { "$eval", function }, { "args", new BsonArray(args) } }; var result = subject.CreateCommand(); result.Should().Be(expectedResult); }
public async Task ExecuteAsync_should_return_expected_result_when_noLock_is_provided() { var function = "return 1"; var subject = new EvalOperation(_databaseNamespace, function, _messageEncoderSettings); subject.NoLock = true; BsonValue result; using (var binding = CoreTestConfiguration.GetReadWriteBinding()) { result = await subject.ExecuteAsync(binding, CancellationToken.None); } result.Should().Be(1); }