-
Notifications
You must be signed in to change notification settings - Fork 0
/
astorage.pas
121 lines (100 loc) · 2.73 KB
/
astorage.pas
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
unit AStorage;
uses Timers;
uses System;
uses System.IO;
uses System.Linq;
var storage: string;
storage_path: string := 'database.dmp';
hashset_storage: string;
hashset_storage_path: string := 'database.hashsets.dmp';
t: Timer;
procedure save();
begin
System.IO.File.WriteAllText(storage_path, storage);
System.IO.File.WriteAllText(hashset_storage_path, hashset_storage);
WriteLn('Stored ', length(storage) + length(hashset_storage), ' bytes to storages');
end;
function hsetexists(set_name: string): boolean;
begin
result := hashset_storage.split(',').Contains(set_name);
end;
function hcreateset(set_name: string): string;
begin
if(not hsetexists(set_name)) then begin
if(length(hashset_storage) = 0) then
hashset_storage := set_name
else
hashset_storage := hashset_storage + ',' + set_name;
result := '::> OK';
end else begin
result := '::> ALREADY_EXISTS';
end;
end;
function hset(set_name, key, value: string): string;
begin
if(not (hsetexists(set_name)) and (set_name <> '')) then begin
result := '::> Set doesn''t exist';
exit();
end;
if(length(storage) = 0) then
storage := set_name + ':' + key + ':' + value
else
storage := storage + ',' + set_name + ':' + key + ':' + value;
result := '::> OK';
end;
function hget(set_name, key: string): string;
begin
if(not (hsetexists(set_name)) and (set_name <> '')) then begin
result := '::> Set doesn''t exist';
exit();
end;
var found := false;
var answer := '';
foreach var row in storage.split(',') do begin
var columns := row.split(':');
if (length(columns) <> 3) then
continue;
if ((columns[0] = set_name) and (columns[1] = key)) then begin
answer := columns[2];
found := true;
end;
end;
if(not found) then
result := '::> Not found'
else
result := answer;
end;
function hgetall(set_name: string): string;
begin
if(not hsetexists(set_name)) then begin
result := '::> Set doesn''t exist';
exit();
end;
var answer := '';
foreach var row in storage.split(',') do begin
var columns := row.split(':');
if (length(columns) <> 3) then
continue;
if (columns[0] = set_name) then begin
answer := answer + columns[1] + '=' + columns[2] + chr(10);
end;
end;
result := answer;
end;
procedure flushall();
begin
hashset_storage := '';
storage := '';
end;
begin
if (System.IO.File.Exists(storage_path)) then begin
storage := System.IO.File.ReadAllText(storage_path);
WriteLn('Restored ', length(storage), ' bytes from storage');
end;
if (System.IO.File.Exists(hashset_storage_path)) then begin
hashset_storage := System.IO.File.ReadAllText(hashset_storage_path);
WriteLn('Restored ', length(hashset_storage), ' bytes from hashset storage');
end;
t := new Timer(15000, save);
t.Start();
end.