public override void Dispose()
            {
                var patcher = new ScriptedIndexResultsJsonPatcher(database, forEntityNames);

                if (string.IsNullOrEmpty(scriptedIndexResults.DeleteScript) == false)
                {
                    foreach (var removeKey in removed)
                    {
                        patcher.Apply(new RavenJObject(), new ScriptedPatchRequest
                        {
                            Script = scriptedIndexResults.DeleteScript,
                            Values =
                            {
                                { "key", removeKey }
                            }
                        });
                    }
                }

                if (string.IsNullOrEmpty(scriptedIndexResults.IndexScript) == false)
                {
                    foreach (var kvp in created)
                    {
                        try
                        {
                            patcher.Apply(kvp.Value, new ScriptedPatchRequest
                            {
                                Script = scriptedIndexResults.IndexScript,
                                Values =
                                {
                                    { "key", kvp.Key }
                                }
                            });
                        }
                        catch (Exception e)
                        {
                            log.Warn("Could not apply index script " + scriptedIndexResults.Id + " to index result with key: " + kvp.Key, e);
                        }
                    }
                }

                database.TransactionalStorage.Batch(accessor =>
                {
                    if (patcher.CreatedDocs != null)
                    {
                        foreach (var jsonDocument in patcher.CreatedDocs)
                        {
                            patcher.DocumentsToDelete.Remove(jsonDocument.Key);
                            database.Put(jsonDocument.Key, jsonDocument.Etag, jsonDocument.DataAsJson, jsonDocument.Metadata, null);
                        }
                    }

                    foreach (var doc in patcher.DocumentsToDelete)
                    {
                        database.Delete(doc, null, null);
                    }
                });
            }
            public override void Dispose()
            {
                var patcher = new ScriptedIndexResultsJsonPatcher(database, forEntityNames);

                if (string.IsNullOrEmpty(scriptedIndexResults.DeleteScript) == false)
                {
                    foreach (var removeKey in removed)
                    {
                        patcher.Apply(new RavenJObject(), new ScriptedPatchRequest
                        {
                            Script = scriptedIndexResults.DeleteScript,
                            Values =
							{
								{"key", removeKey}
							}
                        });
                    }
                }

                if (string.IsNullOrEmpty(scriptedIndexResults.IndexScript) == false)
                {
                    foreach (var kvp in created)
                    {
                        try
                        {
                            patcher.Apply(kvp.Value, new ScriptedPatchRequest
                            {
                                Script = scriptedIndexResults.IndexScript,
                                Values =
                                {
                                    {"key", kvp.Key}
                                }
                            });
                        }
                        catch (Exception e)
                        {
                            log.Warn("Could not apply index script " + scriptedIndexResults.Id + " to index result with key: " + kvp.Key, e);
                        }
                    }
                }

                database.TransactionalStorage.Batch(accessor =>
                {
                    if (patcher.CreatedDocs != null)
                    {
                        foreach (var jsonDocument in patcher.CreatedDocs)
                        {
                            patcher.DocumentsToDelete.Remove(jsonDocument.Key);
                            database.Put(jsonDocument.Key, jsonDocument.Etag, jsonDocument.DataAsJson, jsonDocument.Metadata, null);
                        }
                    }

                    foreach (var doc in patcher.DocumentsToDelete)
                    {
                        database.Delete(doc, null, null);
                    }
                });
            }
            public override void Dispose()
            {
                var patcher = new ScriptedIndexResultsJsonPatcher(database, forEntityNames);

                if (string.IsNullOrEmpty(scriptedIndexResults.DeleteScript) == false)
                {
                    foreach (var kvp in removed)
                    {
                        foreach (var entry in kvp.Value)
                        {
                            patcher.Apply(entry, new ScriptedPatchRequest
                            {
                                Script = scriptedIndexResults.DeleteScript,
                                Values =
                                {
                                    {"key", kvp.Key}
                                }
                            });

                            if (log.IsDebugEnabled && patcher.Debug.Count > 0)
                            {
                                log.Debug("Debug output for doc: {0} for index {1} (delete):\r\n.{2}", kvp.Key,
                                          scriptedIndexResults.Id, string.Join("\r\n", patcher.Debug));

                                patcher.Debug.Clear();
                            }
                        }

                    }
                }

                if (string.IsNullOrEmpty(scriptedIndexResults.IndexScript) == false)
                {
                    foreach (var kvp in created)
                    {
                        try
                        {
                            foreach (var entry in kvp.Value)
                            {
                                patcher.Apply(entry, new ScriptedPatchRequest
                                {
                                    Script = scriptedIndexResults.IndexScript,
                                    Values =
                                {
                                    {"key", kvp.Key}
                                }
                                });
                            }
                           
                        }
                        catch (Exception e)
                        {
                            log.Warn(
                                "Could not apply index script " + scriptedIndexResults.Id +
                                " to index result with key: " + kvp.Key, e);
                        }
                        finally
                        {
                            if (log.IsDebugEnabled && patcher.Debug.Count > 0)
                            {
                                log.Debug("Debug output for doc: {0} for index {1} (index):\r\n.{2}", kvp.Key, scriptedIndexResults.Id, string.Join("\r\n", patcher.Debug));

                                patcher.Debug.Clear();
                            }
                        }
                    }
                }

                database.TransactionalStorage.Batch(accessor =>
                {
                    foreach (var operation in patcher.GetOperations())
                    {
	                    switch (operation.Type)
	                    {
							case ScriptedJsonPatcher.OperationType.Put:
			                    database.Documents.Put(operation.Document.Key, operation.Document.Etag, operation.Document.DataAsJson,
			                                 operation.Document.Metadata, null);
								break;
							case ScriptedJsonPatcher.OperationType.Delete:
								database.Documents.Delete(operation.DocumentKey, null, null);
								break;
							default:
								throw new ArgumentOutOfRangeException("operation.Type");
	                    }    
                    }
                });
            }