Permalink
Browse files

Remove old documents from the cache wen they are updated/deleted

  • Loading branch information...
1 parent f4993da commit d4c0bbe293ffce88c52a22a77e7f7fc7bf6a18b1 @ayende ayende committed Jun 22, 2011
@@ -1,96 +1,96 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <PropertyGroup>
- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>8.0.30703</ProductVersion>
- <SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{AA580048-17C7-4B1E-AA3A-321157CF3511}</ProjectGuid>
- <OutputType>Library</OutputType>
- <AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>Raven.Client.MvcIntegration</RootNamespace>
- <AssemblyName>Raven.Client.MvcIntegration</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
- <FileAlignment>512</FileAlignment>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
- <DebugSymbols>true</DebugSymbols>
- <DebugType>full</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\Debug\</OutputPath>
- <DefineConstants>DEBUG;TRACE</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
- <DebugType>pdbonly</DebugType>
- <Optimize>true</Optimize>
- <OutputPath>bin\Release\</OutputPath>
- <DefineConstants>TRACE</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- </PropertyGroup>
- <ItemGroup>
- <Reference Include="Newtonsoft.Json">
- <HintPath>..\SharedLibs\Newtonsoft.Json.dll</HintPath>
- </Reference>
- <Reference Include="System" />
- <Reference Include="System.Core" />
- <Reference Include="System.Web" />
- <Reference Include="System.Web.Abstractions" />
- <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll</HintPath>
- </Reference>
- <Reference Include="System.Web.Routing" />
- <Reference Include="System.Xml.Linq" />
- <Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
- <Reference Include="System.Data" />
- <Reference Include="System.Xml" />
- </ItemGroup>
- <ItemGroup>
- <Compile Include="JsonFormatterAndFieldsFilterer.cs" />
- <Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="RavenProfiler.cs" />
- <Compile Include="RavenProfilingHandler.cs" />
- <Compile Include="RecordCurrentControllerContextFilter.cs" />
- </ItemGroup>
- <ItemGroup>
- <ProjectReference Include="..\Modules\Json\Raven.Json\Raven.Json.csproj">
- <Project>{B9DD0239-3476-48CB-A541-1B3EC6679BB6}</Project>
- <Name>Raven.Json</Name>
- </ProjectReference>
- <ProjectReference Include="..\Raven.Abstractions\Raven.Abstractions.csproj">
- <Project>{41AC479E-1EB2-4D23-AAF2-E4C8DF1BC2BA}</Project>
- <Name>Raven.Abstractions</Name>
- </ProjectReference>
- <ProjectReference Include="..\Raven.Client.Lightweight\Raven.Client.Lightweight.csproj">
- <Project>{4E087ECB-E7CA-4891-AC3C-3C76702715B6}</Project>
- <Name>Raven.Client.Lightweight</Name>
- </ProjectReference>
- </ItemGroup>
- <ItemGroup>
- <EmbeddedResource Include="ravendb-profiler-scripts.js" />
- </ItemGroup>
- <ItemGroup>
- <EmbeddedResource Include="Templates\session-template.tmpl.html" />
- </ItemGroup>
- <ItemGroup>
- <EmbeddedResource Include="Templates\ravendb-profiler.tmpl.html" />
- </ItemGroup>
- <ItemGroup>
- <EmbeddedResource Include="styles.css" />
- </ItemGroup>
- <ItemGroup>
- <EmbeddedResource Include="Templates\request-details.tmpl.html" />
- </ItemGroup>
- <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{AA580048-17C7-4B1E-AA3A-321157CF3511}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Raven.Client.MvcIntegration</RootNamespace>
+ <AssemblyName>Raven.Client.MvcIntegration</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\SharedLibs\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Web" />
+ <Reference Include="System.Web.Abstractions" />
+ <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Routing" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="JsonFormatterAndFieldsFilterer.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RavenProfiler.cs" />
+ <Compile Include="RavenProfilingHandler.cs" />
+ <Compile Include="RecordCurrentControllerContextFilter.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Modules\Json\Raven.Json\Raven.Json.csproj">
+ <Project>{B9DD0239-3476-48CB-A541-1B3EC6679BB6}</Project>
+ <Name>Raven.Json</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\Raven.Abstractions\Raven.Abstractions.csproj">
+ <Project>{41AC479E-1EB2-4D23-AAF2-E4C8DF1BC2BA}</Project>
+ <Name>Raven.Abstractions</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\Raven.Client.Lightweight\Raven.Client.Lightweight.csproj">
+ <Project>{4E087ECB-E7CA-4891-AC3C-3C76702715B6}</Project>
+ <Name>Raven.Client.Lightweight</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="ravendb-profiler-scripts.js" />
+ </ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="Templates\session-template.tmpl.html" />
+ </ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="Templates\ravendb-profiler.tmpl.html" />
+ </ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="styles.css" />
+ </ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="Templates\request-details.tmpl.html" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
- -->
+ -->
</Project>
@@ -33,6 +33,11 @@ public void SetCachedDocument(string key, Guid etag, RavenJObject doc, RavenJObj
};
}
+ public void RemoveCachedDocument(string key, Guid etag)
+ {
+ cachedSerializedDocuments.Remove("Doc/" + key + "/" + etag);
+ }
+
public void Dispose()
{
cachedSerializedDocuments.Dispose();
@@ -6,6 +6,7 @@ namespace Raven.Database.Impl
public interface IDocumentCacher : IDisposable
{
CachedDocument GetCachedDocument(string key, Guid etag);
- void SetCachedDocument(string key, Guid etag, RavenJObject doc, RavenJObject metadata);
+ void SetCachedDocument(string key, Guid etag, RavenJObject doc, RavenJObject metadata);
+ void RemoveCachedDocument(string key, Guid etag);
}
}
@@ -303,16 +303,19 @@ public Guid AddDocument(string key, Guid? etag, RavenJObject data, RavenJObject
if (key != null && Encoding.Unicode.GetByteCount(key) >= 255)
throw new ArgumentException("The key must be a maximum of 255 bytes in unicode, 127 characters, key is: " + key, "key");
+ Guid existingEtag;
Api.JetSetCurrentIndex(session, Documents, "by_key");
Api.MakeKey(session, Documents, key, Encoding.Unicode, MakeKeyGrbit.NewKey);
var isUpdate = Api.TrySeek(session, Documents, SeekGrbit.SeekEQ);
if (isUpdate)
{
EnsureNotLockedByTransaction(key, null);
- EnsureDocumentEtagMatch(key, etag, "PUT");
+ existingEtag = EnsureDocumentEtagMatch(key, etag, "PUT");
+
}
else
{
+ existingEtag = Guid.Empty;
EnsureDocumentIsNotCreatedInAnotherTransaction(key, Guid.NewGuid());
if (Api.TryMoveFirst(session, Details))
Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["document_count"], 1);
@@ -335,6 +338,7 @@ public Guid AddDocument(string key, Guid? etag, RavenJObject data, RavenJObject
logger.DebugFormat("Inserted a new document with key '{0}', update: {1}, ",
key, isUpdate);
+ cacher.RemoveCachedDocument(key, newEtag);
return newEtag;
}
@@ -400,13 +404,16 @@ public bool DeleteDocument(string key, Guid? etag, out RavenJObject metadata)
if (Api.TryMoveFirst(session, Details))
Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["document_count"], -1);
- EnsureDocumentEtagMatch(key, etag, "DELETE");
+ var existingEtag = EnsureDocumentEtagMatch(key, etag, "DELETE");
EnsureNotLockedByTransaction(key, null);
metadata = Api.RetrieveColumn(session, Documents, tableColumnsCache.DocumentsColumns["metadata"]).ToJObject();
Api.JetDelete(session, Documents);
logger.DebugFormat("Document with key '{0}' was deleted", key);
+
+ cacher.RemoveCachedDocument(key, existingEtag);
+
return true;
}
@@ -19,7 +19,7 @@ namespace Raven.Storage.Esent.StorageActions
public partial class DocumentStorageActions
{
- private void EnsureDocumentEtagMatch(string key, Guid? etag, string method)
+ private Guid EnsureDocumentEtagMatch(string key, Guid? etag, string method)
{
var existingEtag = Api.RetrieveColumn(session, Documents, tableColumnsCache.DocumentsColumns["etag"]).TransfromToGuidWithProperSorting();
if (existingEtag != etag && etag != null)
@@ -31,6 +31,7 @@ private void EnsureDocumentEtagMatch(string key, Guid? etag, string method)
ExpectedETag = existingEtag
};
}
+ return existingEtag;
}
private void EnsureDocumentEtagMatchInTransaction(string key, Guid? etag)
@@ -220,7 +220,7 @@ private RavenJObject ReadDocument(Tuple<MemoryStream, RavenJObject> stream, Json
public bool DeleteDocument(string key, Guid? etag, out RavenJObject metadata)
{
- AssertValidEtag(key, etag, "DELETE", null);
+ var existingEtag = AssertValidEtag(key, etag, "DELETE", null);
metadata = null;
var readResult = storage.Documents.Read(new RavenJObject { { "key", key } });
@@ -231,14 +231,16 @@ public bool DeleteDocument(string key, Guid? etag, out RavenJObject metadata)
storage.Documents.Remove(new RavenJObject { { "key", key } });
+ documentCacher.RemoveCachedDocument(key, existingEtag);
+
return true;
}
public Guid AddDocument(string key, Guid? etag, RavenJObject data, RavenJObject metadata)
{
- AssertValidEtag(key, etag, "PUT", null);
+ var existingEtag = AssertValidEtag(key, etag, "PUT", null);
- var ms = new MemoryStream();
+ var ms = new MemoryStream();
metadata.WriteTo(ms);
@@ -256,6 +258,8 @@ public Guid AddDocument(string key, Guid? etag, RavenJObject data, RavenJObject
{"entityName", metadata.Value<string>(Constants.RavenEntityName)}
}, ms.ToArray());
+ documentCacher.RemoveCachedDocument(key, existingEtag);
+
return newEtag;
}
@@ -273,17 +277,17 @@ private int GetNextDocumentId()
return id;
}
- private void AssertValidEtag(string key, Guid? etag, string op, TransactionInformation transactionInformation)
+ private Guid AssertValidEtag(string key, Guid? etag, string op, TransactionInformation transactionInformation)
{
var readResult = storage.Documents.Read(new RavenJObject { { "key", key } });
if (readResult != null)
{
StorageHelper.AssertNotModifiedByAnotherTransaction(storage, transactionStorageActions, key, readResult, transactionInformation);
+ var existingEtag = new Guid(readResult.Key.Value<byte[]>("etag"));
if (etag != null)
{
- var existingEtag = new Guid(readResult.Key.Value<byte[]>("etag"));
if (existingEtag != etag)
{
throw new ConcurrencyException(op + " attempted on document '" + key +
@@ -294,15 +298,16 @@ private void AssertValidEtag(string key, Guid? etag, string op, TransactionInfor
};
}
}
+ return existingEtag;
}
- else
- {
- readResult =
- storage.DocumentsModifiedByTransactions.Read(
- new RavenJObject { { "key", key } });
- StorageHelper.AssertNotModifiedByAnotherTransaction(storage, transactionStorageActions, key, readResult, transactionInformation);
- }
+ readResult = storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } });
+ StorageHelper.AssertNotModifiedByAnotherTransaction(storage, transactionStorageActions, key, readResult, transactionInformation);
+
+ if(readResult == null)
+ return Guid.Empty;
+
+ return new Guid(readResult.Key.Value<byte[]>("etag"));
}
}

0 comments on commit d4c0bbe

Please sign in to comment.