Skip to content

Commit f47608f

Browse files
author
SirJosh3917
committed
added stream support
1 parent 171170b commit f47608f

File tree

4 files changed

+100
-61
lines changed

4 files changed

+100
-61
lines changed

StringDB.Tester/Program.cs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,14 @@
11
using Newtonsoft.Json;
2-
2+
using StringDB.Reader;
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67

78
namespace StringDB.Tester {
89

910
internal class Program {
1011
private static void Main(string[] args) {
11-
using (var db = Database.FromFile("string.db")) {
12-
foreach (var i in db) // loop over every item and say the index
13-
Console.WriteLine(i.Index);
14-
15-
Console.WriteLine(GetSizeOfObject(db));
16-
17-
var fs = System.IO.File.OpenRead("file-to-insert.txt");
18-
19-
db.Insert("very-cool", fs);
20-
21-
db.Insert("test_key", "test_value"); // insert a single item named "test_key"
22-
23-
db.InsertRange(new KeyValuePair<string, string>[] { // insert multiple items
24-
new KeyValuePair<string, string>("test1", "value1"),
25-
new KeyValuePair<string, string>("test2", "value2"),
26-
new KeyValuePair<string, string>("test3", "value3"),
27-
new KeyValuePair<string, string>("test4", "value4"),
28-
new KeyValuePair<string, string>("test5", "value5"),
29-
});
30-
31-
foreach (var i in db) // loop over every item in the DB again and say the index
32-
Console.WriteLine(i.ToString());
33-
34-
var testKey = db.GetByIndex("test_key"); // get test_key
35-
36-
Console.WriteLine(testKey.Value); // say the value of test_key
37-
38-
db.OverwriteValue(testKey, "new_value"); // change the value
39-
40-
Console.WriteLine(testKey.Value); // say the new value
41-
42-
db.OverwriteValue(testKey, "newest_value"); // change the value again
43-
44-
// insert another test_key
45-
46-
db.Insert("test_key", "i'm hidden behind the other test_key!");
47-
48-
// foreach loop
49-
50-
foreach (var i in db)
51-
Console.WriteLine(i.Index);
52-
53-
// will print out the first test_key and the other test key
54-
55-
foreach (var i in db.GetMultipleByIndex("test_key")) //let's get every single test_key
56-
Console.WriteLine(i.Value); //write out the value
57-
58-
// now by doing so many tiny inserts we can save a little space if we clean it
59-
60-
using (var cleaneddb = Database.FromFile("cleaned-string.db")) {
61-
cleaneddb.CleanFrom(db);
62-
}
63-
}
64-
65-
// let's see hwo big the normal database is compared to the cleaned one
66-
67-
Console.WriteLine("unclean: " + new System.IO.FileInfo("string.db").Length + " bytes");
68-
Console.WriteLine("clean: " + new System.IO.FileInfo("cleaned-string.db").Length + " bytes");
69-
70-
Console.ReadLine();
7112
}
7213
}
7314

StringDB/Reader/Enumerability/ReaderPair.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ internal ReaderPair(PartDataPair dp, IRawReader rawReader) {
2525
/// <summary>Get the value as a byte array instead.</summary>
2626
public byte[] ByteArrayValue => this._byteValueCache ?? (this._byteValueCache = (this._dp.ReadData(this._rawReader) ?? new byte[0] { }));
2727

28+
/// <summary>Get a stream of the value</summary>
29+
public System.IO.Stream StreamValue => this._rawReader.GetStreamOfDataAt(this._dp.DataPosition);
30+
2831
/// <summary>Whatever the index is.</summary>
2932
public string Index => this._strIndexCache ?? (this._strIndexCache = this._dp.Index.GetString());
3033

StringDB/Reader/RawReader.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ internal interface IRawReader {
1111

1212
IPart ReadOn(IPart previous);
1313

14+
Stream GetStreamOfDataAt(long p);
15+
1416
byte[] ReadDataValueAt(long p);
1517

1618
long ReadDataValueLengthAt(long p);
@@ -108,6 +110,30 @@ public long ReadDataValueLengthAt(long p) {
108110
#endif
109111
}
110112

113+
public Stream GetStreamOfDataAt(long p) {
114+
#if THREAD_SAFE
115+
lock(_lock) {
116+
#endif
117+
var len = ReadDataValueLengthAt(p);
118+
119+
var newp = p + 1;
120+
121+
if (len < byte.MaxValue)
122+
newp += sizeof(byte);
123+
else if (len < ushort.MaxValue)
124+
newp += sizeof(ushort);
125+
else if (len < uint.MaxValue)
126+
newp += sizeof(uint);
127+
else if ((ulong)len < ulong.MaxValue)
128+
newp += sizeof(ulong);
129+
130+
131+
return new StreamFragment(this._stream, newp, len);
132+
#if THREAD_SAFE
133+
}
134+
#endif
135+
}
136+
111137
public byte[] ReadDataValueAt(long p) {
112138
#if THREAD_SAFE
113139
lock(_lock) {

StringDB/Reader/StreamFragment.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace StringDB.Reader
7+
{
8+
public class StreamFragment : Stream {
9+
public StreamFragment(Stream main, long pos, long lenAfterPos) {
10+
this.Length = lenAfterPos;
11+
this._originalPos = pos;
12+
this._pos = pos;
13+
this._s = main;
14+
}
15+
16+
private Stream _s;
17+
18+
public override bool CanRead => true;
19+
public override bool CanSeek => true;
20+
public override bool CanWrite => false;
21+
22+
public override long Length { get; }
23+
24+
private long _originalPos { get; }
25+
private long _pos;
26+
public override long Position {
27+
get {
28+
return this._pos - this._originalPos;
29+
}
30+
set {
31+
var lP = this._pos;
32+
this._pos = this._originalPos + value;
33+
//TODO: >=
34+
if (this._pos - this._originalPos < 0 || this._pos - this._originalPos > this.Length)
35+
this._pos = lP;
36+
}
37+
}
38+
39+
public override void Flush() { }
40+
41+
public override int Read(byte[] buffer, int offset, int count) {
42+
if (this._pos - this._originalPos < 0)
43+
return -1;
44+
45+
var c = count;
46+
if (this._pos - this._originalPos + c > this.Length)
47+
c += (int)( this.Length - ((this._pos - this._originalPos) + c) );
48+
49+
this._s.Seek(this._pos, SeekOrigin.Begin);
50+
this._pos += c;
51+
return this._s.Read(buffer, offset, c);
52+
}
53+
54+
public override long Seek(long offset, SeekOrigin origin) {
55+
if(origin == SeekOrigin.Begin) {
56+
this.Position = offset;
57+
} else if (origin == SeekOrigin.Current) {
58+
this.Position += offset;
59+
} else if (origin == SeekOrigin.End) {
60+
this.Position = this.Length + offset;
61+
}
62+
63+
return this.Position;
64+
}
65+
66+
public override void SetLength(long value) => throw new NotImplementedException();
67+
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
68+
}
69+
}

0 commit comments

Comments
 (0)