private bool WillReplicateByScript(string script, JsonDocument document) { var scriptedPatchRequest = new ScriptedPatchRequest { Script = script }; var patcher = new ReplicationScriptedJsonPatcher(database, scriptedPatchRequest); using (var scope = new DefaultScriptedJsonPatcherOperationScope(database)) { try { patcher.Apply(scope, document.ToJson(), scriptedPatchRequest, document.SerializedSizeOnDisk); //null means that we should skip this document return(scope.ActualPatchResult != JsValue.Null); } catch (Exception e) { //this document will not be replicated return(false); } } }
public IEnumerable <JsonDocument> Handle(IEnumerable <JsonDocument> docs) { if (strategy.SpecifiedCollections == null || strategy.SpecifiedCollections.Count == 0) { return(docs); } return(docs.Select(doc => { var collection = doc.Metadata.Value <string>(Constants.RavenEntityName); string script; if (string.IsNullOrEmpty(collection) || strategy.SpecifiedCollections.TryGetValue(collection, out script) == false) { if (Log.IsDebugEnabled) { Log.Debug(string.Format("Will not replicate document '{0}' to '{1}' because the replication of specified collection is turned on while the document does not belong to any of them", doc.Key, destinationId)); } return null; } if (script == null) { return doc; } var scriptedPatchRequest = new ScriptedPatchRequest { Script = script }; var patcher = new ReplicationScriptedJsonPatcher(database, scriptedPatchRequest); using (var scope = new DefaultScriptedJsonPatcherOperationScope(database)) { try { var transformedDoc = patcher.Apply(scope, doc.ToJson(), scriptedPatchRequest, doc.SerializedSizeOnDisk); if (scope.ActualPatchResult == JsValue.Null) // null means that document should be skip { if (Log.IsDebugEnabled) { Log.Debug(string.Format("Will not replicate document '{0}' to '{1}' because a collection specific script filtered it out", doc.Key, destinationId)); } return null; } doc.Metadata = (RavenJObject)transformedDoc[Constants.Metadata]; transformedDoc.Remove(Constants.Metadata); doc.DataAsJson = transformedDoc; return doc; } catch (ParseException e) { Log.WarnException(string.Format("Could not parse replication transformation script of '{0}' collection on document {1}", collection, doc.Key), e); throw; } catch (Exception e) { Log.WarnException(string.Format("Could not apply replication transformation script of '{0}' collection on document {1}", collection, doc.Key), e); throw; } } }) .Where(x => x != null)); }