Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

changing FirkinDictionary to lazy load values on enumeration

  • Loading branch information...
commit f9e4cdb8509199e2c53905241ee0bfe0a1ac44c0 1 parent ea4f173
@sdether authored
View
2  Firkin.Reactive.Test/Firkin.Reactive.Test.csproj
@@ -43,7 +43,7 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
- <Reference Include="System.Reactive, Version=1.0.10605.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+ <Reference Include="System.Reactive, Version=1.0.10621.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
View
2  Firkin.Reactive/Firkin.Reactive.csproj
@@ -35,7 +35,7 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
- <Reference Include="System.Reactive, Version=1.0.10605.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+ <Reference Include="System.Reactive, Version=1.0.10621.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
View
2  Firkin.Test.Perf/Firkin.Test.Perf.csproj
@@ -48,7 +48,7 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
- <Reference Include="System.Reactive, Version=1.0.10605.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+ <Reference Include="System.Reactive, Version=1.0.10621.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
View
70 Firkin/Data/LazyFirkinCollection.cs
@@ -0,0 +1,70 @@
+/*
+ * Firkin
+ * Copyright (C) 2010 Arne F. Claassen
+ * http://www.claassen.net/geek/blog geekblog [at] claassen [dot] net
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Droog.Firkin.Data {
+ public class LazyFirkinCollection<TKey, TValue> : ICollection<TValue> {
+ private readonly ICollection<TKey> _keys;
+ private readonly Func<TKey, FirkinStream> _getStream;
+ private readonly Func<FirkinStream, TValue> _getValue;
+
+ public LazyFirkinCollection(ICollection<TKey> keys, Func<TKey, FirkinStream> getStream, Func<FirkinStream, TValue> getValue) {
+ _keys = keys;
+ _getStream = getStream;
+ _getValue = getValue;
+ }
+
+ public IEnumerator<TValue> GetEnumerator() {
+ foreach(var key in _keys) {
+ var s = _getStream(key);
+ if(s == null) {
+ continue;
+ }
+ yield return _getValue(s);
+ }
+ }
+
+ IEnumerator IEnumerable.GetEnumerator() {
+ return GetEnumerator();
+ }
+
+ public void Add(TValue item) { }
+
+ public void Clear() { }
+
+ public bool Contains(TValue item) {
+ return false;
+ }
+
+ public void CopyTo(TValue[] array, int arrayIndex) {
+ var source = this.Take(array.Length - arrayIndex).ToArray();
+ Array.Copy(source, 0, array, arrayIndex, source.Length);
+ }
+
+ public bool Remove(TValue item) {
+ return false;
+ }
+
+ public int Count { get { return _keys.Count; } }
+
+ public bool IsReadOnly { get { return true; } }
+ }
+}
View
1  Firkin/Firkin.csproj
@@ -52,6 +52,7 @@
<Compile Include="Data\HintRecord.cs" />
<Compile Include="FirkinHashChange.cs" />
<Compile Include="FirkinHashChangeAction.cs" />
+ <Compile Include="Data\LazyFirkinCollection.cs" />
<Compile Include="Serialization\BinaryFormatterGenerator.cs" />
<Compile Include="Serialization\ISerializerGenerator.cs" />
<Compile Include="Serialization\IStreamSerializer.cs" />
View
14 Firkin/FirkinDictionary.cs
@@ -19,6 +19,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
+using Droog.Firkin.Data;
using Droog.Firkin.Serialization;
using System.Linq;
@@ -41,7 +42,13 @@ public class FirkinDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDispos
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
- return _hash.Select(pair => new KeyValuePair<TKey, TValue>(pair.Key, _valueSerializer.Deserialize(pair.Value))).GetEnumerator();
+ var keys = Keys;
+ foreach(var key in keys) {
+ var v = _hash.Get(key);
+ if(v != null) {
+ yield return new KeyValuePair<TKey, TValue>(key, _valueSerializer.Deserialize(v));
+ }
+ }
}
IEnumerator IEnumerable.GetEnumerator() {
@@ -129,8 +136,11 @@ public class FirkinDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDispos
get { return _hash.Keys.ToList(); }
}
+
public ICollection<TValue> Values {
- get { return this.Select(x => x.Value).ToList(); }
+ get {
+ return new LazyFirkinCollection<TKey, TValue>(Keys, key => _hash.Get(key), stream => _valueSerializer.Deserialize(stream));
+ }
}
public void Merge() {
Please sign in to comment.
Something went wrong with that request. Please try again.