/
Database.cs
133 lines (103 loc) · 4.15 KB
/
Database.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace randombg_dotnet{
using Entity = Dictionary<string, IDbRecord>;
public class Database{
private enum DbExistsOnFileSystem{
Unknown=-1,
False,
True
}
private DbExistsOnFileSystem _dbExistsOnFs =
DbExistsOnFileSystem.Unknown;
private string _dbFullName;
private Entity _images = new Entity();
private bool _isDbLoaded = false;
public string DbName { get; } = "randombg.db";
public Database(){
_dbFullName = Path.Combine(Globals.DbDir, DbName);
_dbExistsOnFs = File.Exists(Path.Combine(_dbFullName)) ?
DbExistsOnFileSystem.True :
DbExistsOnFileSystem.False;
}
public void Create(){
if (File.Exists(_dbFullName))
throw new InvalidOperationException(
$"Cannot create {_dbFullName} because it already exists.");
if (!Directory.Exists(Globals.DbDir))
Directory.CreateDirectory(Globals.DbDir);
using(var temp = File.Create(_dbFullName)){}
_dbExistsOnFs = DbExistsOnFileSystem.True;
Console.WriteLine($"Created datatabase: {_dbFullName}");
}
public List<ImageRecord> GetUnusedImages(){
if (!_isDbLoaded) Load();
return _images.Select(kvp => kvp.Value)
.Where(i => Boolean.Parse(i["HasBeenUsed"]) == false)
.Cast<ImageRecord>()
.ToList();
}
public void InsertNewRecords(string table, IEnumerable<IDbRecord> recs){
if (!_isDbLoaded) Load();
bool append = _images.Count > 0;
var distincts = recs.Distinct();
long inserted = 0;
foreach(var item in distincts)
if (!append || !_images.ContainsKey(item.Key)){
_images.Add(item.Key, item);
inserted++;
}
Console.WriteLine($"{inserted} rows inserted");
}
public void Load(){
if (_isDbLoaded) return;
if (_dbExistsOnFs == DbExistsOnFileSystem.False)
Create();
using (StreamReader sr = new StreamReader(_dbFullName)){
string line, table = String.Empty;
while ((line = sr.ReadLine()) != null){
if (IsIgnoreableLine(line))
continue;
if (IsTableHeader(line)){
table = GetTableName(line);
}else{
switch(table.ToUpper()){
case "IMAGES":
var record = new ImageRecord(line);
_images.Add(record.Key, record);
break;
default:
throw new ArgumentException(
$"Unhandled database table {table}");
}
}
}
}
_isDbLoaded = true;
}
private bool IsIgnoreableLine(string line){
return String.IsNullOrWhiteSpace(line) || line.StartsWith("#");
}
private bool IsTableHeader(string line){
return line.StartsWith("[") && line.EndsWith("]");
}
private string GetTableName(string line){
// Sample: [images]
return line.Substring(1, line.Length-2);
}
public void MarkImageDownloaded(ImageRecord img){
_images[img.Key] = img.ToggleHasBeenUsed();
}
public void Save(){
if (_dbExistsOnFs == DbExistsOnFileSystem.True && !_isDbLoaded) return;
using (StreamWriter sw = new StreamWriter(_dbFullName, false)){
sw.WriteLine($"[images]");
foreach(var kvp in _images)
sw.WriteLine(kvp.Value.ToDbString());
}
}
}
}