forked from apmckinlay/csuneido
/
load.cpp
195 lines (178 loc) · 4.82 KB
/
load.cpp
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This file is part of Suneido - The Integrated Application Platform
* see: http://www.suneido.com for more information.
*
* Copyright (c) 2000 Suneido Software Corp.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation - version 2.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License in the file COPYING
* for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "load.h"
#include "istreamfile.h"
#include "database.h"
#include "thedb.h"
#include "record.h"
#include "query.h"
#include "port.h"
#ifdef _WIN32
#include <io.h> // for access
#else
#include <unistd.h> // for access
#endif
#include "alert.h"
#include "errlog.h"
#include "exceptimp.h"
static char* loadbuf = 0;
static int loadbuf_size = 0;
static int load1(Istream& fin, gcstring tblspec);
static int load_data(Istream& fin, const gcstring& table);
static void load_data_record(Istream& fin, const gcstring& table, int tran, int n);
static bool alerts = false;
struct Loading
{
Loading()
{
theDB()->loading = true;
loadbuf_size = 100000;
loadbuf = (char*) mem_committed(loadbuf_size);
verify(loadbuf);
}
~Loading()
{
theDB()->loading = false;
mem_release(loadbuf);
loadbuf = 0;
loadbuf_size = 0;
}
};
extern bool thedb_create;
void load(const gcstring& table)
{
thedb_create = true;
if (table != "") // load a single table
{
load_table(table);
}
else // load entire database
{
const size_t bufsize = 8000;
char buf[bufsize];
IstreamFile fin("database.su", "rb");
if (! fin)
except("can't open database.su");
fin.getline(buf, bufsize);
if (! has_prefix(buf, "Suneido dump 1"))
except("invalid file");
if (_access("suneido.db", 0) == 0)
{
remove("suneido.bak");
verify(0 == rename("suneido.db", "suneido.bak"));
}
Loading loading;
while (fin.getline(buf, bufsize))
{
if (has_prefix(buf, "======"))
{
memcpy(buf, "create", 6);
load1(fin, buf);
}
else
except("bad file format");
}
verify(!alerts);
}
}
int load_table(const gcstring& table)
{
const size_t bufsize = 8000;
char buf[bufsize];
IstreamFile fin((table + ".su").str(), "rb");
if (!fin)
except("can't open " << table << ".su");
fin.getline(buf, bufsize);
if (!has_prefix(buf, "Suneido dump 1"))
except("invalid file");
char* buf2 = buf + table.size() + 1;
fin.getline(buf2, bufsize);
verify(0 == memcmp(buf2, "======", 6));
memcpy(buf, "create ", 7);
memcpy(buf + 7, table.ptr(), table.size());
Loading loading;
int n = load1(fin, buf);
verify(!alerts);
return n;
}
static int load1(Istream& fin, gcstring tblspec)
{
int n = tblspec.find(' ', 7);
gcstring table = tblspec.substr(7, n - 7);
if (table != "views")
{
if (theDB()->istable(table))
theDB()->remove_table(table);
database_admin(tblspec.str());
}
return load_data(fin, table);
}
const int recsPerTran = 50;
static int load_data(Istream& fin, const gcstring& table)
{
int nrecs = 0;
int tran = theDB()->transaction(READWRITE);
for (;; ++nrecs)
{
int n;
fin.read((char*) &n, sizeof n);
if (fin.gcount() != sizeof n)
except("unexpected eof");
if (n == 0)
break ;
load_data_record(fin, table, tran, n);
if (nrecs % recsPerTran == recsPerTran - 1)
{
verify(theDB()->commit(tran));
tran = theDB()->transaction(READWRITE);
}
}
verify(theDB()->commit(tran));
return nrecs;
}
static void load_data_record(Istream& fin, const gcstring& table, int tran, int n)
{
try
{
if (n > loadbuf_size)
{
loadbuf_size = max(n, 2 * loadbuf_size);
mem_release(loadbuf);
loadbuf = (char*) mem_committed(loadbuf_size);
verify(loadbuf);
}
fin.read(loadbuf, n);
Record rec(loadbuf);
if (rec.cursize() != n)
except_err(table << ": rec size " << rec.cursize() << " not what was read " << n);
if (table == "views")
theDB()->add_any_record(tran, table, rec);
else
theDB()->add_record(tran, table, rec);
}
catch (const Except& e)
{
errlog("load: skipping corrupted record in: ", table.str(), e.str());
alert("skipping corrupted record in: " << table << ": " << e);
alerts = true;
}
}