-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
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,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[]>()); | ||
} | ||
} | ||
} |
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,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> |
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,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> |
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,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 | ||
} | ||
} | ||
} |
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