Skip to content

Commit

Permalink
Merge pull request #14 from Redth/feature-core-package
Browse files Browse the repository at this point in the history
Switch to unix time for storage in index file
  • Loading branch information
jamesmontemagno committed Dec 20, 2017
2 parents 439c485 + c96d473 commit 9347788
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/MonkeyCache.FileStore/Barrel.cs
Expand Up @@ -210,23 +210,23 @@ public bool IsExpired(string key)

Dictionary<string, Tuple<string, DateTime>> index;

const string INDEX_FILENAME = "index.dat";
const string INDEX_FILENAME = "idx.dat";

string indexFile;

void WriteIndex()
{
if (string.IsNullOrEmpty(indexFile))
indexFile = Path.Combine(baseDirectory.Value, INDEX_FILENAME);

if (!Directory.Exists(baseDirectory.Value))
Directory.CreateDirectory(baseDirectory.Value);

using (var f = File.Open(indexFile, FileMode.Create))
using (var sw = new StreamWriter(f))
{
foreach (var kvp in index)
sw.WriteLine($"{kvp.Key}\t{kvp.Value.Item1}\t{kvp.Value.Item2.ToString("o")}");
using (var sw = new StreamWriter(f)) {
foreach (var kvp in index) {
var dtEpoch = DateTimeToEpochSeconds(kvp.Value.Item2);
sw.WriteLine($"{kvp.Key}\t{kvp.Value.Item1}\t{dtEpoch.ToString()}");
}
}
}

Expand All @@ -253,8 +253,9 @@ void LoadIndex()
var etag = parts[1];
var dt = parts[2];

if (!string.IsNullOrEmpty(key) && DateTime.TryParse(dt, out var date) && !index.ContainsKey(key))
index.Add(key, new Tuple<string, DateTime>(etag, date));
int secondsSinceEpoch;
if (!string.IsNullOrEmpty(key) && int.TryParse(dt, out secondsSinceEpoch) && !index.ContainsKey(key))
index.Add(key, new Tuple<string, DateTime>(etag, EpochSecondsToDateTime(secondsSinceEpoch)));
}
}
}
Expand All @@ -266,5 +267,19 @@ static string Hash(string input)
var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
return BitConverter.ToString(data);
}


static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

static int DateTimeToEpochSeconds (DateTime date)
{
var diff = date - epoch;
return (int)diff.TotalSeconds;
}

static DateTime EpochSecondsToDateTime (int seconds)
{
return epoch + TimeSpan.FromSeconds(seconds);
}
}
}
2 changes: 1 addition & 1 deletion src/MonkeyCache.TestsShared/BarrelTests.cs
Expand Up @@ -414,7 +414,7 @@ void PerformanceTestRunner (int threads, bool allowDuplicateKeys, int keysPerThr
stopwatch.Stop();
Debug.WriteLine($"Empty ({tId}) took {stopwatch.ElapsedMilliseconds} ms");
Assert.IsTrue(stopwatch.ElapsedMilliseconds > 0);
Assert.IsTrue(stopwatch.ElapsedMilliseconds >= 0);
});

task.ContinueWith(t => {
Expand Down

0 comments on commit 9347788

Please sign in to comment.