-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyValue.cs
377 lines (339 loc) · 9.04 KB
/
KeyValue.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System; //System.Console, System.Exception.
using System.Collections; //Hashtable
using System.Collections.Generic; //List, HashSet
using System.Linq; //Keys.Cast<string>.ToList()
namespace Storage
{
public class KeyValue
{
public static bool UseSQLite3 = false; //or "true" to use
public static void Main_(string[] args){
Test(args); //run tests
}
public static void Test(string[] args){
try{
KeyValue hashtable = new KeyValue(args); //create new hashtable
hashtable.Reset(true);
hashtable.Count(true);
hashtable.Add("key1", "value1", false, true);
hashtable.Add("key2", "value2", false, true);
hashtable.Add("key3", "value'", false, true);
hashtable.Count(true);
hashtable.Keys(true);
hashtable.GetValue("key1", true);
hashtable.GetValue("key2", true);
hashtable.GetValue("key3", true);
hashtable.Remove("key1", true);
hashtable.Add("key1", "value5", true);
hashtable.GetValue("key1", true);
hashtable.GetValue("key2", true);
hashtable.GetValue("key3", true);
hashtable.Contains("key100", true);
hashtable.ContainsKey("key1", true);
hashtable.Search("lue", true);
hashtable.Reset(true);
hashtable.Count(true);
hashtable.Keys(true);
}
catch(Exception ex){
Console.WriteLine(ex);
}
}
/*
In memory "key-value"-storage.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable?view=net-6.0
.NET Framework only:
For very large Hashtable objects,
you can increase the maximum capacity
to 2 billion elements
on a 64-bit system
by setting the enabled attribute of the <gcAllowVeryLargeObjects> configuration element to true
in the run-time environment.
*/
//constructor
public KeyValue(string[] args){
if(args.Length == 1){ //if filename only
UseSQLite3 = false;
new KeyValue(args[0]);
}
else{ //if HashTableName, KeyName, ValueName too
UseSQLite3 = true;
new KeyValue(args[0], args[1], args[2], args[3]); //and load hashtable from storage
}
}
//constructor
public KeyValue(
string DbFileName = null
, string KeyValueTableName = null
, string KeyName = null
, string ValueName = null
){
Console.WriteLine("KeyValue: "+DbFileName+", "+KeyValueTableName+", "+KeyName+", "+ValueName);
if(String.IsNullOrEmpty(KeyValueTableName)){
UseSQLite3 = false;
}
else{
UseSQLite3 = true;
}
Console.WriteLine("UseSQLite3 = "+UseSQLite3);
//on initialize object, just initialize this
hashtable = Load(DbFileName, KeyValueTableName, KeyName, ValueName); //and load hashtable from storage
}
private const int CacheLimit = 100000; // how many records to keep in memory (reduce DB read operations)
public Hashtable hashtable = new Hashtable(); //cache
/*
Methods:
// Count( show=false )
// Keys( show=false )
// ContainsKey( key , show=false )
// Contains( key , show=false )
// GetValue( key , show=false )
// Search( searchstring, caseSensetive=false, show=false )
// Add( key, value, replace=false, show=false )
// Remove( key, show=false )
// Reset( show=false )
// Load()
// Save()
*/
// ShowAndReturn( value, show=false )
// Show object, and return this as is
public object ShowAndReturn(object value, bool show = false){
if(show){
if(value == null){
Console.WriteLine("null");
}
else{
Console.WriteLine((string)value.ToString());
}
}
return value;
}
// Count( show=false )
// Get number of keys in hashtable storage
public int Count(
bool show = false
)
{
if(UseSQLite3 == true){
int count = SQLite3.Count();
if(show == true){
Console.WriteLine("count: "+count);
}
return count;
}
else{
return (int)ShowAndReturn(hashtable.Count, show);
}
}
// Keys( show=false )
// Get list of keys in hashtable storage
public List<string> Keys(
bool show = false
){
List<string> result = new List<string>();
if(UseSQLite3 == true){
result = SQLite3.Keys();
}
else{
result = hashtable.Keys.Cast<string>().ToList();
}
if(show){
foreach(string key in result){
Console.WriteLine("key: "+key);
}
}
return result;
}
// ContainsKey( key , show=false )
// Check is key contains or not
public bool ContainsKey(
string key
, bool show = false
){
if(UseSQLite3 == true){
return (bool)ShowAndReturn(SQLite3.ContainsKey(key), show);
}
else{
return (bool)ShowAndReturn(hashtable.ContainsKey(key), show);
}
}
// Contains( key , show=false )
// The same, check is key contains or not
public bool Contains(
string key
, bool show = false
){
return ContainsKey(key, show);
}
//Add value to cache
public void CacheValue(string key, string value){
//reset cache, if need
if(hashtable.Count >= CacheLimit){
hashtable = new Hashtable();
}
//cashing
if(!hashtable.ContainsKey(key)){
hashtable[key] = value;
}
}
// GetValue( key , show=false )
// Get value by key from hashtable
public string GetValue(
string key
, bool show = false
){
if(hashtable.ContainsKey(key)){
return (string)ShowAndReturn((string)hashtable[key], show);
}
else{
string value = SQLite3.GetValue(key);
CacheValue(key, value);
return (string)ShowAndReturn((string)value, show);
}
}
// Search( searchstring, caseSensetive=false, show=false )
// Get list of keys, where part of value is found
public List<string> Search(
string searchstring
, bool caseSensetive = false
, bool show = false
){
//Search in cache:
List<string> FoundKeys = new List<string>();
if(caseSensetive == true){
searchstring = searchstring.ToLower();
}
foreach(DictionaryEntry record in hashtable){
string value = (string)record.Value;
if(caseSensetive == true){
value = value.ToLower();
}
if(value.Contains(searchstring)){
FoundKeys.Add((string)record.Key);
}
}
if(show == true){
foreach(string key in FoundKeys){
Console.WriteLine("key: "+key);
}
}
if(UseSQLite3 == true)
{
//SQLite search
List<string> keys = SQLite3.SearchString(searchstring, caseSensetive);
foreach (string key in keys){
if(show == true){
Console.WriteLine("key: "+key);
FoundKeys.Add(key);
}
}
}
return FoundKeys;
}
// Add( key, value, replace=false, show=false )
// Add key-value in hashtable
public bool Add(
string key
, string value
, bool replace = false
, bool show = false
){
try{
if(
replace == false
&& hashtable.ContainsKey(key)
){
return (bool)ShowAndReturn(false, show);
}
if(UseSQLite3 == true){
int result = SQLite3.Add(key, value, replace);
if(result == -1){//if not add - false
return (bool)ShowAndReturn(false, show);
}
CacheValue(key, value);
if(show == true){
Console.WriteLine("Add result: "+result);
}
return (bool)ShowAndReturn(true, show);
}
else{
hashtable[key] = value;
Save();
return (bool)ShowAndReturn(true, show);
}
}
catch{
return (bool)ShowAndReturn(false, show);
}
}
// Remove( key, show=false )
// Remove from hashtable the key-value pair, by key
public bool Remove(
string key
, bool show = false
){
try{
if(UseSQLite3 == true){
SQLite3.Remove(key);
if(hashtable.ContainsKey(key)){
hashtable.Remove(key);
}
}
else{
hashtable.Remove(key);
Save();
}
return (bool)ShowAndReturn(true, show);
}
catch{
return (bool)ShowAndReturn(false, show);
}
}
// Reset( show=false )
// Clear hashtable
public bool Reset(
bool show = false
){
try{
hashtable = new Hashtable();
if(UseSQLite3 == true){
SQLite3.Reset();
}
return (bool)ShowAndReturn(true, show);
}
catch{
return (bool)ShowAndReturn(false, show);
}
}
//Load hashtable from storage
public Hashtable Load(
string DbFileName = null
, string KeyValueTableName = null
, string KeyName = null
, string ValueName = null
){
if(UseSQLite3 == true){
SQLite3.UseSQLite3 = true;
SQLite3.KeyValueTableName = KeyValueTableName;
SQLite3.KeyName = KeyName;
SQLite3.ValueName = ValueName;
SQLite3.openSQLite3Db(DbFileName);
hashtable = new Hashtable();
return hashtable; //return empty hashtable, to use direct SQL-requests for DB.
}
else{
return Storage.Load(DbFileName);
}
}
//Save hashtable in storage
public void Save(){
if(UseSQLite3 == true){
// SQLite3.Save(hashtable);
//Each change are already saved
}
else{
Storage.Save(hashtable);
}
}
}
}