Skip to content

Commit

Permalink
StoneVault added
Browse files Browse the repository at this point in the history
added StoneVault for a long term storage solution, so that multiple StringDB versions can co-mingle with eachother and data is moveable inbetween versions - as well as moveable inbetween platforms ( with such a simple format )
  • Loading branch information
SirJosh3917 committed Jul 26, 2018
1 parent 81c4c14 commit 4955724
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
39 changes: 39 additions & 0 deletions StoneVault.Tester/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using StringDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace StoneVault.Tester
{
class Program
{
static void Main(string[] args)
{
using (var vault = new MemoryStream())
using (var db = Database.FromStream(new MemoryStream(), true)) {
db.Fill("EXAMPLE KEY", " ~ v a l u e ~ ", 25);

Vault.Store(ReadOut(db), vault);

vault.Seek(0, SeekOrigin.Begin);

int items = 0;

foreach(var i in Vault.Read(vault)) {
Console.WriteLine($"[{Encoding.UTF8.GetString(i.Key)}, {Encoding.UTF8.GetString(i.Value)}]");
items++;
}

Console.WriteLine($"Items: {items}");
}

Console.ReadLine();
}

static IEnumerable<KeyValuePair<byte[], byte[]>> ReadOut(IDatabase db) {
foreach (var i in db)
yield return new KeyValuePair<byte[], byte[]>(i.Index.GetAs<byte[]>(), i.Value.GetAs<byte[]>());
}
}
}
13 changes: 13 additions & 0 deletions StoneVault.Tester/StoneVault.Tester.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\StoneVault\StoneVault.csproj" />
<ProjectReference Include="..\StringDB\StringDB.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions StoneVault/StoneVault.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.4;netstandard1.5;netstandard1.6;netstandard2.0;netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
</PropertyGroup>

</Project>
68 changes: 68 additions & 0 deletions StoneVault/Vault.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace StoneVault {

/// <summary>Vault - Store your data in a stone, unchangeable format for transferring to and from new StringDB versions.</summary>
public static class Vault {

private const byte DATA_GOOD = 0x00;
private const byte DATA_END = 0xFF;

/// <param name="encoding">Default ( null ) is UTF8</param>
public static void Store(IEnumerable<KeyValuePair<byte[], byte[]>> vaultItems, Stream writeTo, Encoding encoding = null) {
using (var bw = new BinaryWriter(writeTo, encoding ?? Encoding.UTF8, true)) {
foreach (var i in vaultItems) {
WriteByteArray(bw, i.Key); // write the key and value
WriteByteArray(bw, i.Value);
}
bw.Write(DATA_END); // data has ended

bw.Flush(); // flush
}
}

/// <param name="encoding">Default ( null ) is UTF8</param>
public static IEnumerable<KeyValuePair<byte[], byte[]>> Read(Stream readFrom, Encoding encoding = null) {
bool toggle = false;
byte[] last = new byte[0];
foreach(var i in ReadOut(readFrom, encoding)) {
if(toggle)
yield return new KeyValuePair<byte[], byte[]>(last, i);
else last = i;

toggle = !toggle;
}
}

public static void WriteIn(IEnumerable<byte[]> vaultItems, Stream writeTo, Encoding encoding = null) {
using (var bw = new BinaryWriter(writeTo, encoding ?? Encoding.UTF8, true)) {
foreach (var i in vaultItems)
WriteByteArray(bw, i);
bw.Write(DATA_END);

bw.Flush();
}
}

/// <summary>StoneVault's format is based on individual byte arrays, which is just meshed together for Store & Write</summary>
public static IEnumerable<byte[]> ReadOut(Stream readFrom, Encoding encoding = null) {
using (var br = new BinaryReader(readFrom, encoding ?? Encoding.UTF8, true))
while (br.ReadByte() != DATA_END) {
var len = br.ReadInt64();

if (len > int.MaxValue) throw new ArgumentOutOfRangeException($"Unable to read out as a byte array - the length is over the maximum int value {int.MaxValue}");
yield return br.ReadBytes((int)len);
}
}

private static void WriteByteArray(BinaryWriter bw, byte[] item) {
bw.Write(DATA_GOOD); // the byte array is good for reading signal

bw.Write((long)item.Length); // write the length of it ( as a long, for future Stream implementations )
bw.Write(item); // write the data
}
}
}
14 changes: 13 additions & 1 deletion StringDB.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StringDB", "StringDB\String
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StringDB.Tester", "StringDB.Tester\StringDB.Tester.csproj", "{98DE3535-FA0B-465B-ADA4-1FCF2D5CF1F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringDB.Benchmarks", "StringDB.Benchmarks\StringDB.Benchmarks.csproj", "{D1567AE6-7059-48FE-A098-42273AF219B4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StringDB.Benchmarks", "StringDB.Benchmarks\StringDB.Benchmarks.csproj", "{D1567AE6-7059-48FE-A098-42273AF219B4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StoneVault", "StoneVault\StoneVault.csproj", "{FE10F1D7-06A8-4354-B0A5-18A74310A43B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StoneVault.Tester", "StoneVault.Tester\StoneVault.Tester.csproj", "{C3670717-A7BE-4BA7-8F68-D0A915DB5B51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,6 +31,14 @@ Global
{D1567AE6-7059-48FE-A098-42273AF219B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1567AE6-7059-48FE-A098-42273AF219B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1567AE6-7059-48FE-A098-42273AF219B4}.Release|Any CPU.Build.0 = Release|Any CPU
{FE10F1D7-06A8-4354-B0A5-18A74310A43B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE10F1D7-06A8-4354-B0A5-18A74310A43B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE10F1D7-06A8-4354-B0A5-18A74310A43B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE10F1D7-06A8-4354-B0A5-18A74310A43B}.Release|Any CPU.Build.0 = Release|Any CPU
{C3670717-A7BE-4BA7-8F68-D0A915DB5B51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3670717-A7BE-4BA7-8F68-D0A915DB5B51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3670717-A7BE-4BA7-8F68-D0A915DB5B51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3670717-A7BE-4BA7-8F68-D0A915DB5B51}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 4955724

Please sign in to comment.