forked from patorjk/JavaScript-Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxn.cpp
More file actions
210 lines (194 loc) · 6.68 KB
/
txn.cpp
File metadata and controls
210 lines (194 loc) · 6.68 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "lmdb-js.h"
using namespace Napi;
TxnTracked::TxnTracked(MDB_txn *txn, unsigned int flags) {
this->txn = txn;
this->flags = flags;
parent = nullptr;
}
TxnTracked::~TxnTracked() {
this->txn = nullptr;
}
TxnWrap::TxnWrap(const Napi::CallbackInfo& info) : ObjectWrap<TxnWrap>(info) {
EnvWrap *ew;
napi_unwrap(info.Env(), info[0], (void**)&ew);
if (ew->env == nullptr) throwError(info.Env(), "Attempt to start a transaction on a database environment that is closed");
int flags = 0;
TxnWrap *parentTw;
if (info[1].IsBoolean() && ew->writeWorker) { // this is from a transaction callback
txn = ew->writeWorker->AcquireTxn(&flags);
parentTw = nullptr;
} else {
if (info[1].IsObject()) {
Object options = info[1].As<Object>();
// Get flags from options
setFlagFromValue(&flags, MDB_RDONLY, "readOnly", false, options);
} else if (info[1].IsNumber()) {
flags = info[1].As<Number>();
}
MDB_txn *parentTxn;
if (info[2].IsObject()) {
napi_unwrap(info.Env(), info[2], (void**) &parentTw);
parentTxn = parentTw->txn;
} else {
parentTxn = nullptr;
parentTw = nullptr;
// Check existence of current write transaction
if (0 == (flags & MDB_RDONLY)) {
if (ew->currentWriteTxn != nullptr) {
throwError(info.Env(), "You have already opened a write transaction in the current process, can't open a second one.");
return;
}
//fprintf(stderr, "begin sync txn");
auto writeWorker = ew->writeWorker;
if (writeWorker) {
parentTxn = writeWorker->AcquireTxn(&flags); // see if we have a paused transaction
// else we create a child transaction from the current batch transaction. TODO: Except in WRITEMAP mode, where we need to indicate that the transaction should not be committed
}
}
}
//fprintf(stderr, "txn_begin from txn.cpp %u %p\n", flags, parentTxn);
if ((flags & MDB_RDONLY) && parentTxn) {
// if a txn is passed in, we check to see if it is up-to-date and can be reused
MDB_envinfo stat;
mdb_env_info(ew->env, &stat);
if (mdb_txn_id(parentTxn) == stat.me_last_txnid) {
txn = nullptr;
info.This().As<Object>().Set("address", Number::New(info.Env(), 0));
return;
}
parentTxn = nullptr;
}
int rc = mdb_txn_begin(ew->env, parentTxn, flags, &txn);
if (rc == MDB_READERS_FULL) { // try again after reader check, in case a dead process frees a slot
int dead;
mdb_reader_check(ew->env, &dead);
ew->consolidateTxns();
rc = mdb_txn_begin(ew->env, parentTxn, flags, &txn);
}
if (rc != 0) {
txn = nullptr;
throwLmdbError(info.Env(), rc);
return;
}
}
// Set the current write transaction
if (0 == (flags & MDB_RDONLY)) {
ew->currentWriteTxn = this;
}
else {
ew->readTxns.push_back(this);
ew->currentReadTxn = txn;
}
this->parentTw = parentTw;
this->flags = flags;
this->ew = ew;
this->env = ew->env;
info.This().As<Object>().Set("address", Number::New(info.Env(), (size_t) this));
}
TxnWrap::~TxnWrap() {
// Close if not closed already
if (this->txn) {
mdb_txn_abort(txn);
this->removeFromEnvWrap();
}
}
void TxnWrap::removeFromEnvWrap() {
if (this->ew) {
if (this->ew->currentWriteTxn == this) {
this->ew->currentWriteTxn = this->parentTw;
}
else {
auto it = std::find(ew->readTxns.begin(), ew->readTxns.end(), this);
if (it != ew->readTxns.end()) {
ew->readTxns.erase(it);
}
}
this->ew = nullptr;
}
this->txn = nullptr;
}
Value TxnWrap::commit(const Napi::CallbackInfo& info) {
// this should only be used for committing read-only txns
if (!this->txn) {
return throwError(info.Env(), "The transaction is already closed.");
}
int rc = mdb_txn_commit(this->txn);
this->removeFromEnvWrap();
if (rc != 0) {
return throwLmdbError(info.Env(), rc);
}
return info.Env().Undefined();
}
Value TxnWrap::abort(const Napi::CallbackInfo& info) {
if (!this->txn) {
return throwError(info.Env(), "The transaction is already closed.");
}
mdb_txn_abort(this->txn);
this->removeFromEnvWrap();
return info.Env().Undefined();
}
NAPI_FUNCTION(resetTxn) {
ARGS(1)
GET_INT64_ARG(0);
TxnWrap* tw = (TxnWrap*) i64;
if (!tw->txn || !tw->getEnv()) {
THROW_ERROR("The transaction is already closed.");
} else {
tw->reset();
RETURN_UNDEFINED;
}
}
void resetTxnFFI(double twPointer) {
TxnWrap* tw = (TxnWrap*) (size_t) twPointer;
if (tw->txn && tw->getEnv()) tw->reset();
}
void TxnWrap::reset() {
ew->readTxnRenewed = false;
mdb_txn_reset(txn);
}
Value TxnWrap::renew(const Napi::CallbackInfo& info) {
if (!this->txn || !this->ew->env) {
return throwError(info.Env(), "The transaction is already closed.");
}
int rc = mdb_txn_renew(this->txn);
if (rc != 0) {
return throwLmdbError(info.Env(), rc);
}
return info.Env().Undefined();
}
MDB_env* TxnWrap::getEnv() {
return this->ew->env;
}
void TxnWrap::setupExports(Napi::Env env, Object exports) {
// TxnWrap: Prepare constructor template
Function TxnClass = DefineClass(env, "Txn", {
// TxnWrap: Add functions to the prototype
TxnWrap::InstanceMethod("commit", &TxnWrap::commit),
TxnWrap::InstanceMethod("abort", &TxnWrap::abort),
TxnWrap::InstanceMethod("renew", &TxnWrap::renew),
});
exports.Set("Txn", TxnClass);
EXPORT_NAPI_FUNCTION("resetTxn", resetTxn);
EXPORT_FUNCTION_ADDRESS("resetTxnPtr", resetTxnFFI);
//txnTpl->InstanceTemplate()->SetInternalFieldCount(1);
}
// This file contains code from the node-lmdb project
// Copyright (c) 2013-2017 Timur Kristóf
// Copyright (c) 2021 Kristopher Tate
// Licensed to you under the terms of the MIT license
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.