PRCYCoin  2.0.0.7rc1
P2P Digital Currency
db.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "wallet/db.h"
7 
8 #include "addrman.h"
9 #include "hash.h"
10 #include "protocol.h"
11 #include "util.h"
12 #include "utilstrencodings.h"
13 
14 #include <stdint.h>
15 
16 #ifndef WIN32
17 #include <sys/stat.h>
18 #endif
19 
20 #include <boost/thread.hpp>
21 #include <boost/version.hpp>
22 
23 
24 
25 //
26 // CDB
27 //
28 
30 
32 {
33  if (!fDbEnvInit)
34  return;
35 
36  fDbEnvInit = false;
37  int ret = dbenv->close(0);
38  if (ret != 0)
39  LogPrintf("CDBEnv::EnvShutdown : Error %d shutting down database environment: %s\n", ret, DbEnv::strerror(ret));
40  if (!fMockDb)
41  DbEnv((u_int32_t)0).remove(strPath.c_str(), 0);
42 }
43 
45 {
46  delete dbenv;
47  dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
48  fDbEnvInit = false;
49  fMockDb = false;
50 }
51 
52 CDBEnv::CDBEnv() : dbenv(NULL)
53 {
54  Reset();
55 }
56 
58 {
59  EnvShutdown();
60  delete dbenv;
61  dbenv = NULL;
62 }
63 
65 {
66  EnvShutdown();
67 }
68 
69 bool CDBEnv::Open(const fs::path& pathIn)
70 {
71  if (fDbEnvInit)
72  return true;
73 
74  boost::this_thread::interruption_point();
75 
76  strPath = pathIn.string();
77  fs::path pathLogDir = pathIn / "database";
78  TryCreateDirectory(pathLogDir);
79  fs::path pathErrorFile = pathIn / "db.log";
80  LogPrintf("CDBEnv::Open: LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
81 
82  unsigned int nEnvFlags = 0;
83  if (GetBoolArg("-privdb", true))
84  nEnvFlags |= DB_PRIVATE;
85 
86  dbenv->set_lg_dir(pathLogDir.string().c_str());
87  dbenv->set_cachesize(1, 0x100000, 1); // 1 MiB should be enough for just the wallet, Increased by 1 GB
88  dbenv->set_lg_bsize(0x10000);
89  dbenv->set_lg_max(1048576);
90  dbenv->set_lk_max_locks(40000);
91  dbenv->set_lk_max_objects(40000);
92  dbenv->set_errfile(fsbridge::fopen(pathErrorFile, "a"));
93  dbenv->set_flags(DB_AUTO_COMMIT, 1);
94  dbenv->set_flags(DB_TXN_WRITE_NOSYNC, 1);
95  dbenv->log_set_config(DB_LOG_AUTO_REMOVE, 1);
96  int ret = dbenv->open(strPath.c_str(),
97  DB_CREATE |
98  DB_INIT_LOCK |
99  DB_INIT_LOG |
100  DB_INIT_MPOOL |
101  DB_INIT_TXN |
102  DB_THREAD |
103  DB_RECOVER |
104  nEnvFlags,
105  S_IRUSR | S_IWUSR);
106  if (ret != 0) {
107  dbenv->close(0);
108  return error("CDBEnv::Open : Error %d opening database environment: %s\n", ret, DbEnv::strerror(ret));
109  }
110 
111  fDbEnvInit = true;
112  fMockDb = false;
113  return true;
114 }
115 
117 {
118  if (fDbEnvInit)
119  throw std::runtime_error("CDBEnv::MakeMock : Already initialized");
120 
121  boost::this_thread::interruption_point();
122 
123  LogPrint(BCLog::DB, "CDBEnv::MakeMock\n");
124 
125  dbenv->set_cachesize(1, 0, 1);
126  dbenv->set_lg_bsize(10485760 * 4);
127  dbenv->set_lg_max(10485760);
128  dbenv->set_lk_max_locks(10000);
129  dbenv->set_lk_max_objects(10000);
130  dbenv->set_flags(DB_AUTO_COMMIT, 1);
131  dbenv->log_set_config(DB_LOG_IN_MEMORY, 1);
132  int ret = dbenv->open(NULL,
133  DB_CREATE |
134  DB_INIT_LOCK |
135  DB_INIT_LOG |
136  DB_INIT_MPOOL |
137  DB_INIT_TXN |
138  DB_THREAD |
139  DB_PRIVATE,
140  S_IRUSR | S_IWUSR);
141  if (ret > 0)
142  throw std::runtime_error(strprintf("CDBEnv::MakeMock : Error %d opening database environment.", ret));
143 
144  fDbEnvInit = true;
145  fMockDb = true;
146 }
147 
148 CDBEnv::VerifyResult CDBEnv::Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile))
149 {
150  LOCK(cs_db);
151  assert(mapFileUseCount.count(strFile) == 0);
152 
153  Db db(dbenv, 0);
154  int result = db.verify(strFile.c_str(), NULL, NULL, 0);
155  if (result == 0)
156  return VERIFY_OK;
157  else if (recoverFunc == NULL)
158  return RECOVER_FAIL;
159 
160  // Try to recover:
161  bool fRecovered = (*recoverFunc)(*this, strFile);
162  return (fRecovered ? RECOVER_OK : RECOVER_FAIL);
163 }
164 
165 bool CDBEnv::Compact(const std::string& strFile)
166 {
167  LOCK(cs_db);
168 
169  DB_COMPACT dbcompact;
170  dbcompact.compact_fillpercent = 80;
171  dbcompact.compact_pages = DB_MAX_PAGES;
172  dbcompact.compact_timeout = 0;
173 
174  DB_COMPACT *pdbcompact;
175  pdbcompact = &dbcompact;
176 
177  int result = 1;
178  if (mapDb[strFile] != NULL) {
179  Db* pdb = mapDb[strFile];
180  result = pdb->compact(NULL, NULL, NULL, pdbcompact, DB_FREE_SPACE, NULL);
181  delete pdb;
182  mapDb[strFile] = NULL;
183 
184  switch (result)
185  {
186  case DB_LOCK_DEADLOCK:
187  LogPrint(BCLog::DB,"Deadlock %i\n", result);
188  break;
189  case DB_LOCK_NOTGRANTED:
190  LogPrint(BCLog::DB,"Lock Not Granted %i\n", result);
191  break;
192  case DB_REP_HANDLE_DEAD:
193  LogPrint(BCLog::DB,"Handle Dead %i\n", result);
194  break;
195  case DB_REP_LOCKOUT:
196  LogPrint(BCLog::DB,"Rep Lockout %i\n", result);
197  break;
198  case EACCES:
199  LogPrint(BCLog::DB,"Eacces %i\n", result);
200  break;
201  case EINVAL:
202  LogPrint(BCLog::DB,"Error Invalid %i\n", result);
203  break;
204  case 0:
205  LogPrint(BCLog::DB,"Wallet Compact Sucessful\n");
206  break;
207  default:
208  LogPrint(BCLog::DB,"Compact result int %i\n", result);
209  }
210  }
211  return (result == 0);
212 }
213 
214 bool CDBEnv::Salvage(std::string strFile, bool fAggressive, std::vector<CDBEnv::KeyValPair>& vResult)
215 {
216  LOCK(cs_db);
217  assert(mapFileUseCount.count(strFile) == 0);
218 
219  u_int32_t flags = DB_SALVAGE;
220  if (fAggressive)
221  flags |= DB_AGGRESSIVE;
222 
223  std::stringstream strDump;
224 
225  Db db(dbenv, 0);
226  int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
227  if (result == DB_VERIFY_BAD) {
228  LogPrintf("CDBEnv::Salvage : Database salvage found errors, all data may not be recoverable.\n");
229  if (!fAggressive) {
230  LogPrintf("CDBEnv::Salvage : Rerun with aggressive mode to ignore errors and continue.\n");
231  return false;
232  }
233  }
234  if (result != 0 && result != DB_VERIFY_BAD) {
235  LogPrintf("CDBEnv::Salvage : Database salvage failed with result %d.\n", result);
236  return false;
237  }
238 
239  // Format of bdb dump is ascii lines:
240  // header lines...
241  // HEADER=END
242  // hexadecimal key
243  // hexadecimal value
244  // ... repeated
245  // DATA=END
246 
247  std::string strLine;
248  while (!strDump.eof() && strLine != "HEADER=END")
249  getline(strDump, strLine); // Skip past header
250 
251  std::string keyHex, valueHex;
252  while (!strDump.eof() && keyHex != "DATA=END") {
253  getline(strDump, keyHex);
254  if (keyHex != "DATA=END") {
255  getline(strDump, valueHex);
256  vResult.push_back(std::make_pair(ParseHex(keyHex), ParseHex(valueHex)));
257  }
258  }
259 
260  return (result == 0);
261 }
262 
263 
264 void CDBEnv::CheckpointLSN(const std::string& strFile)
265 {
266  dbenv->txn_checkpoint(0, 0, 0);
267  if (fMockDb)
268  return;
269  dbenv->lsn_reset(strFile.c_str(), 0);
270 }
271 
272 
273 CDB::CDB(const std::string& strFilename, const char* pszMode, bool fFlushOnCloseIn) : pdb(NULL), activeTxn(NULL)
274 {
275  int ret;
276  fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
277  fFlushOnClose = fFlushOnCloseIn;
278  if (strFilename.empty())
279  return;
280 
281  bool fCreate = strchr(pszMode, 'c') != NULL;
282  unsigned int nFlags = DB_THREAD;
283  if (fCreate)
284  nFlags |= DB_CREATE;
285 
286  {
287  LOCK(bitdb.cs_db);
288  if (!bitdb.Open(GetDataDir()))
289  throw std::runtime_error("CDB : Failed to open database environment.");
290 
291  strFile = strFilename;
293  pdb = bitdb.mapDb[strFile];
294  if (pdb == NULL) {
295  pdb = new Db(bitdb.dbenv, 0);
296 
297  bool fMockDb = bitdb.IsMock();
298  if (fMockDb) {
299  DbMpoolFile* mpf = pdb->get_mpf();
300  ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
301  if (ret != 0)
302  throw std::runtime_error(strprintf("CDB : Failed to configure for no temp file backing for database %s", strFile));
303  }
304 
305  ret = pdb->open(NULL, // Txn pointer
306  fMockDb ? NULL : strFile.c_str(), // Filename
307  fMockDb ? strFile.c_str() : "main", // Logical db name
308  DB_BTREE, // Database type
309  nFlags, // Flags
310  0);
311 
312  if (ret != 0) {
313  delete pdb;
314  pdb = NULL;
316  std::string tempCopy(strFile);
317  strFile = "";
318  throw std::runtime_error(strprintf("CDB : Error %d, can't open database %s", ret, tempCopy));
319  }
320 
321  if (fCreate && !Exists(std::string("version"))) {
322  bool fTmp = fReadOnly;
323  fReadOnly = false;
324  WriteVersion(CLIENT_VERSION);
325  fReadOnly = fTmp;
326  }
327 
328  bitdb.mapDb[strFile] = pdb;
329  }
330  }
331 }
332 
334 {
335  if (activeTxn)
336  return;
337 
338  // Flush database activity from memory pool to disk log
339  unsigned int nMinutes = 0;
340  if (fReadOnly)
341  nMinutes = 1;
342 
343  bitdb.dbenv->txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
344 }
345 
347 {
348  if (!pdb)
349  return;
350  if (activeTxn)
351  activeTxn->abort();
352  activeTxn = NULL;
353  pdb = NULL;
354 
355  if (fFlushOnClose)
356  Flush();
357 
358  {
359  LOCK(bitdb.cs_db);
361  }
362 }
363 
364 void CDBEnv::CloseDb(const std::string& strFile)
365 {
366  {
367  LOCK(cs_db);
368  if (mapDb[strFile] != NULL) {
369  // Close the database handle
370  Db* pdb = mapDb[strFile];
371  pdb->close(0);
372  delete pdb;
373  mapDb[strFile] = NULL;
374  }
375  }
376 }
377 
378 bool CDBEnv::RemoveDb(const std::string& strFile)
379 {
380  this->CloseDb(strFile);
381 
382  LOCK(cs_db);
383  int rc = dbenv->dbremove(NULL, strFile.c_str(), NULL, DB_AUTO_COMMIT);
384  return (rc == 0);
385 }
386 
387 bool CDB::Rewrite(const std::string& strFile, const char* pszSkip)
388 {
389  while (true) {
390  {
391  LOCK(bitdb.cs_db);
392  if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0) {
393  // Flush log data to the dat file
397 
398  bool fSuccess = true;
399  LogPrintf("CDB::Rewrite : Rewriting %s...\n", strFile);
400  std::string strFileRes = strFile + ".rewrite";
401  { // surround usage of db with extra {}
402  CDB db(strFile.c_str(), "r");
403  Db* pdbCopy = new Db(bitdb.dbenv, 0);
404 
405  int ret = pdbCopy->open(NULL, // Txn pointer
406  strFileRes.c_str(), // Filename
407  "main", // Logical db name
408  DB_BTREE, // Database type
409  DB_CREATE, // Flags
410  0);
411  if (ret > 0) {
412  LogPrintf("CDB::Rewrite : Can't create database file %s\n", strFileRes);
413  fSuccess = false;
414  }
415 
416  Dbc* pcursor = db.GetCursor();
417  if (pcursor)
418  while (fSuccess) {
419  CDataStream ssKey(SER_DISK, CLIENT_VERSION);
420  CDataStream ssValue(SER_DISK, CLIENT_VERSION);
421  int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
422  if (ret == DB_NOTFOUND) {
423  pcursor->close();
424  break;
425  } else if (ret != 0) {
426  pcursor->close();
427  fSuccess = false;
428  break;
429  }
430  if (pszSkip &&
431  strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
432  continue;
433  if (strncmp(&ssKey[0], "\x07version", 8) == 0) {
434  // Update version:
435  ssValue.clear();
436  ssValue << CLIENT_VERSION;
437  }
438  Dbt datKey(&ssKey[0], ssKey.size());
439  Dbt datValue(&ssValue[0], ssValue.size());
440  int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
441  if (ret2 > 0)
442  fSuccess = false;
443  }
444  if (fSuccess) {
445  db.Close();
447  if (pdbCopy->close(0))
448  fSuccess = false;
449  } else {
450  pdbCopy->close(0);
451  }
452  delete pdbCopy;
453  }
454  if (fSuccess) {
455  Db dbA(bitdb.dbenv, 0);
456  if (dbA.remove(strFile.c_str(), NULL, 0))
457  fSuccess = false;
458  Db dbB(bitdb.dbenv, 0);
459  if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
460  fSuccess = false;
461  }
462  if (!fSuccess)
463  LogPrintf("CDB::Rewrite : Failed to rewrite database file %s\n", strFileRes);
464  return fSuccess;
465  }
466  }
467  MilliSleep(100);
468  }
469  return false;
470 }
471 
472 
473 void CDBEnv::Flush(bool fShutdown)
474 {
475  int64_t nStart = GetTimeMillis();
476  // Flush log data to the actual data file on all files that are not in use
477  LogPrint(BCLog::DB, "CDBEnv::Flush : Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started");
478  if (!fDbEnvInit)
479  return;
480  {
481  LOCK(cs_db);
482  std::map<std::string, int>::iterator mi = mapFileUseCount.begin();
483  while (mi != mapFileUseCount.end()) {
484  std::string strFile = (*mi).first;
485  int nRefCount = (*mi).second;
486  LogPrint(BCLog::DB, "CDBEnv::Flush : Flushing %s (refcount = %d)...\n", strFile, nRefCount);
487  if (nRefCount == 0) {
488  // Move log data to the dat file
489  CloseDb(strFile);
490  LogPrint(BCLog::DB, "CDBEnv::Flush: %s checkpoint\n", strFile);
491  dbenv->txn_checkpoint(0, 0, 0);
492  LogPrint(BCLog::DB, "CDBEnv::Flush: %s detach\n", strFile);
493  if (!fMockDb)
494  dbenv->lsn_reset(strFile.c_str(), 0);
495  LogPrint(BCLog::DB, "CDBEnv::Flush: %s closed\n", strFile);
496  mapFileUseCount.erase(mi++);
497  } else
498  mi++;
499  }
500  LogPrint(BCLog::DB, "CDBEnv::Flush : Flush(%s)%s took %15dms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " database not started", GetTimeMillis() - nStart);
501  if (fShutdown) {
502  char** listp;
503  if (mapFileUseCount.empty()) {
504  dbenv->log_archive(&listp, DB_ARCH_REMOVE);
505  Close();
506  if (!fMockDb)
507  fs::remove_all(fs::path(strPath) / "database");
508  }
509  }
510  }
511 }
CDBEnv::VERIFY_OK
@ VERIFY_OK
Definition: db.h:58
CDBEnv::VerifyResult
VerifyResult
Verify that database file strFile is OK.
Definition: db.h:58
CDBEnv::cs_db
RecursiveMutex cs_db
Definition: db.h:40
CDBEnv::RemoveDb
bool RemoveDb(const std::string &strFile)
Definition: db.cpp:378
CDB::Exists
bool Exists(const K &key)
Definition: db.h:203
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:13
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: utilstrencodings.cpp:77
CDB::Flush
void Flush()
Definition: db.cpp:333
CDBEnv::Salvage
bool Salvage(std::string strFile, bool fAggressive, std::vector< KeyValPair > &vResult)
Definition: db.cpp:214
flags
int flags
Definition: prcycoin-tx.cpp:297
CDBEnv::Close
void Close()
Definition: db.cpp:64
CDBEnv::Verify
VerifyResult Verify(std::string strFile, bool(*recoverFunc)(CDBEnv &dbenv, std::string strFile))
Definition: db.cpp:148
CDBEnv::Open
bool Open(const fs::path &path)
Definition: db.cpp:69
CDBEnv
Definition: db.h:28
CDBEnv::fMockDb
bool fMockDb
Definition: db.h:32
CDBEnv::CDBEnv
CDBEnv()
Definition: db.cpp:52
CDB::activeTxn
DbTxn * activeTxn
Definition: db.h:100
CDBEnv::CloseDb
void CloseDb(const std::string &strFile)
Definition: db.cpp:364
CDB::Rewrite
static bool Rewrite(const std::string &strFile, const char *pszSkip=NULL)
Definition: db.cpp:387
db.h
CDB::WriteVersion
bool WriteVersion(int nVersion)
Definition: db.h:320
CDB::fReadOnly
bool fReadOnly
Definition: db.h:101
CDBEnv::CheckpointLSN
void CheckpointLSN(const std::string &strFile)
Definition: db.cpp:264
CDB
RAII class that provides access to a Berkeley database.
Definition: db.h:95
CDB::pdb
Db * pdb
Definition: db.h:98
CDBEnv::Reset
void Reset()
Definition: db.cpp:44
GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:255
CDBEnv::RECOVER_OK
@ RECOVER_OK
Definition: db.h:59
LogPrintf
#define LogPrintf(...)
Definition: logging.h:147
CDB::fFlushOnClose
bool fFlushOnClose
Definition: db.h:102
CDBEnv::dbenv
DbEnv * dbenv
Definition: db.h:41
CDB::ReadAtCursor
int ReadAtCursor(Dbc *pcursor, CDataStream &ssKey, CDataStream &ssValue, unsigned int fFlags=DB_NEXT)
Definition: db.h:233
SER_DISK
@ SER_DISK
Definition: serialize.h:160
CDBEnv::Flush
void Flush(bool fShutdown)
Definition: db.cpp:473
BCLog::DB
@ DB
Definition: logging.h:46
LogPrint
#define LogPrint(category,...)
Definition: logging.h:162
CDBEnv::EnvShutdown
void EnvShutdown()
Definition: db.cpp:31
CDataStream::size
size_type size() const
Definition: streams.h:120
GetTimeMillis
int64_t GetTimeMillis()
Definition: utiltime.cpp:31
strprintf
#define strprintf
Definition: tinyformat.h:1056
CDBEnv::mapFileUseCount
std::map< std::string, int > mapFileUseCount
Definition: db.h:42
CDB::GetCursor
Dbc * GetCursor()
Definition: db.h:222
CDataStream::clear
void clear()
Definition: streams.h:126
LOCK
#define LOCK(cs)
Definition: sync.h:182
CDB::Close
void Close()
Definition: db.cpp:346
CDBEnv::Compact
bool Compact(const std::string &strFile)
Salvage data from a file that Verify says is bad.
Definition: db.cpp:165
GetDataDir
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:349
CDBEnv::strPath
std::string strPath
Definition: db.h:35
MilliSleep
void MilliSleep(int64_t n)
Definition: utiltime.cpp:45
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:34
addrman.h
utilstrencodings.h
CDBEnv::MakeMock
void MakeMock()
Definition: db.cpp:116
CDBEnv::fDbEnvInit
bool fDbEnvInit
Definition: db.h:31
GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:241
bitdb
CDBEnv bitdb
Definition: db.cpp:29
CDBEnv::mapDb
std::map< std::string, Db * > mapDb
Definition: db.h:43
CDBEnv::~CDBEnv
~CDBEnv()
Definition: db.cpp:57
CDBEnv::RECOVER_FAIL
@ RECOVER_FAIL
Definition: db.h:60
CDBEnv::IsMock
bool IsMock()
Definition: db.h:50
error
bool error(const char *fmt, const Args &... args)
Definition: util.h:61
TryCreateDirectory
bool TryCreateDirectory(const fs::path &p)
Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
Definition: util.cpp:464
CDB::CDB
CDB(const std::string &strFilename, const char *pszMode="r+", bool fFlushOnCloseIn=true)
Definition: db.cpp:273
CDB::strFile
std::string strFile
Definition: db.h:99