Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the beginnings of View caching to minimize on disk reads
- Loading branch information
RobertTheGrey
committed
Aug 1, 2010
1 parent
0e84a97
commit 2b9d71f
Showing
20 changed files
with
382 additions
and
148 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework; | ||
using SparkSense.Parsing; | ||
using System.IO; | ||
|
||
namespace SparkSense.Tests.Parsing | ||
{ | ||
[TestFixture] | ||
public class CachingViewFolderTests | ||
{ | ||
private const string ROOT_VIEW_PATH = "SparkSense.Tests.Views"; | ||
|
||
[Test] | ||
public void ShouldLoadFromDiskIfPathNotInCache() | ||
{ | ||
string contents = String.Empty; | ||
var cache = new CachingViewFolder(ROOT_VIEW_PATH); | ||
var content = cache.GetViewSource("Shared\\Application.spark"); | ||
|
||
using (TextReader reader = new StreamReader(content.OpenViewStream())) | ||
contents = reader.ReadToEnd(); | ||
|
||
Assert.That(contents.Contains("no header by default")); | ||
} | ||
|
||
[Test] | ||
public void ShouldLoadFromDiskIfPathInCacheWithNullData() | ||
{ | ||
string path = "Shared\\Application.spark"; | ||
string contents = String.Empty; | ||
var cache = new CachingViewFolder(ROOT_VIEW_PATH); | ||
cache.Add(path); | ||
var content = cache.GetViewSource(path); | ||
|
||
using (TextReader reader = new StreamReader(content.OpenViewStream())) | ||
contents = reader.ReadToEnd(); | ||
|
||
Assert.That(contents.Contains("no header by default")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Spark.FileSystem; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace SparkSense.Parsing | ||
{ | ||
public class CachingViewFolder : IViewFolder | ||
{ | ||
private FileSystemViewFolder _disk; | ||
private InMemoryViewFolder _cache; | ||
|
||
// TODO: Rob G evict cache entry when file on disk changes | ||
|
||
public CachingViewFolder(string basePath) | ||
{ | ||
_cache = new InMemoryViewFolder(); | ||
_disk = new FileSystemViewFolder(basePath); | ||
} | ||
public IViewFile GetViewSource(string path) | ||
{ | ||
if (!_cache.HasView(path) || _cache[path].Length == 0) | ||
{ | ||
LoadFromDisk(path); | ||
} | ||
return _cache.GetViewSource(path); | ||
} | ||
|
||
public IList<string> ListViews(string path) | ||
{ | ||
return _cache.ListViews(path); | ||
} | ||
|
||
public bool HasView(string path) | ||
{ | ||
return _cache.HasView(path) || _disk.HasView(path); | ||
} | ||
|
||
public void Add(string path) | ||
{ | ||
_cache.Add(path, null); | ||
} | ||
|
||
private void LoadFromDisk(string path) | ||
{ | ||
var fileContents = _disk.GetViewSource(path); | ||
string contents; | ||
using (TextReader reader = new StreamReader(fileContents.OpenViewStream())) | ||
contents = reader.ReadToEnd(); | ||
_cache.Set(path, contents); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
using System.Collections.Generic; | ||
using Spark.FileSystem; | ||
|
||
namespace SparkSense.Parsing | ||
{ | ||
public interface IProjectExplorer | ||
{ | ||
List<string> ViewMap { get; } | ||
bool TryGetActiveDocumentPath(out string activeDocumentPath); | ||
bool ViewFolderExists(); | ||
IViewFolder GetViewFolder(); | ||
string GetCurrentView(); | ||
bool IsCurrentDocumentASparkFile(); | ||
string GetCurrentViewPath(); | ||
} | ||
} |
Oops, something went wrong.