-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathBatchCompletionState.h
More file actions
188 lines (164 loc) · 4.13 KB
/
BatchCompletionState.h
File metadata and controls
188 lines (164 loc) · 4.13 KB
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
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alexander Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2017 Alexander Peshkov <peshkoff@mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ________________________________
*/
#include "firebird.h"
#include "../common/classes/auto.h"
#include "../common/classes/array.h"
#include "../common/utils_proto.h"
#define DEB_BATCH(x)
namespace Firebird {
class Transliterate
{
public:
virtual void transliterate(IStatus* status) = 0;
virtual ~Transliterate() = default;
};
class BatchCompletionState final :
public DisposeIface<IBatchCompletionStateImpl<BatchCompletionState, CheckStatusWrapper> >
{
public:
BatchCompletionState(bool storeCounts, ULONG lim)
: rare(getPool()),
reccount(0u),
detailedLimit(lim)
{
if (storeCounts)
array = FB_NEW_POOL(getPool()) DenseArray(getPool());
}
~BatchCompletionState()
{
for (unsigned i = 0; i < rare.getCount() && rare[i].second; ++i)
rare[i].second->dispose();
}
void regError(IStatus* errStatus, Transliterate* transliterate)
{
IStatus* newVector = nullptr;
if (rare.getCount() < detailedLimit)
{
newVector = errStatus->clone();
if (transliterate)
transliterate->transliterate(newVector);
}
rare.add(StatusPair(reccount, newVector));
regUpdate(IBatchCompletionState::EXECUTE_FAILED);
}
void regErrorAt(ULONG at, IStatus* errStatus)
{
IStatus* newVector = nullptr;
if ((rare.getCount() < detailedLimit) && errStatus)
newVector = errStatus->clone();
rare.add(StatusPair(at, newVector));
}
void regUpdate(SLONG count)
{
if (array)
array->push(count);
++reccount;
}
void regSize(ULONG total)
{
reccount = total;
}
// IBatchCompletionState implementation
unsigned getSize(CheckStatusWrapper*)
{
return reccount;
}
int getState(CheckStatusWrapper* status, unsigned pos)
{
try
{
checkRange(pos);
if (array)
return (*array)[pos];
ULONG index = find(pos);
return (index >= rare.getCount() || rare[index].first != pos) ?
SUCCESS_NO_INFO : EXECUTE_FAILED;
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
return 0;
}
unsigned findError(CheckStatusWrapper* status, unsigned pos)
{
try
{
ULONG index = find(pos);
if (index < rare.getCount())
return rare[index].first;
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
return NO_MORE_ERRORS;
}
void getStatus(CheckStatusWrapper* status, IStatus* to, unsigned pos)
{
try
{
checkRange(pos);
ULONG index = find(pos);
if (index < rare.getCount() && rare[index].first == pos)
{
if (rare[index].second)
{
CheckStatusWrapper w(to);
fb_utils::copyStatus(&w, rare[index].second);
return;
}
(Arg::Gds(isc_batch_compl_detail) << Arg::Num(pos)).raise();
}
}
catch (const Exception& ex)
{
ex.stuffException(status);
}
}
private:
typedef Pair<NonPooled<ULONG, IStatus*> > StatusPair;
typedef Array<StatusPair> RarefiedArray;
RarefiedArray rare;
typedef Array<SLONG> DenseArray;
AutoPtr<DenseArray> array;
ULONG reccount, detailedLimit;
ULONG find(ULONG recno) const
{
ULONG high = rare.getCount(), low = 0;
while (high > low)
{
ULONG med = (high + low) / 2;
if (recno > rare[med].first)
low = med + 1;
else
high = med;
}
return low;
}
void checkRange(unsigned pos)
{
if (pos >= reccount)
(Arg::Gds(isc_batch_compl_range) << Arg::Num(pos) << Arg::Num(reccount)).raise();
}
};
}