Browse Source

Merge #9353: Add data() method to CDataStream (and use it)

5113474 wallet: Use CDataStream.data() (Wladimir J. van der Laan)
e2300ff bench: Use CDataStream.data() (Wladimir J. van der Laan)
adff950 dbwrapper: Use new .data() method of CDataStream (Wladimir J. van der Laan)
a2141e4 streams: Remove special cases for ancient MSVC (Wladimir J. van der Laan)
af4c44c streams: Add data() method to CDataStream (Wladimir J. van der Laan)
0.14
Pieter Wuille 8 years ago
parent
commit
07fd147b9f
No known key found for this signature in database
GPG Key ID: DBA1A67379A1A931
  1. 2
      src/bench/verify_script.cpp
  2. 12
      src/dbwrapper.h
  3. 6
      src/streams.h
  4. 8
      src/wallet/db.cpp
  5. 12
      src/wallet/db.h

2
src/bench/verify_script.cpp

@ -93,7 +93,7 @@ static void VerifyScriptBench(benchmark::State& state)
txCredit.vout[0].scriptPubKey.data(), txCredit.vout[0].scriptPubKey.data(),
txCredit.vout[0].scriptPubKey.size(), txCredit.vout[0].scriptPubKey.size(),
txCredit.vout[0].nValue, txCredit.vout[0].nValue,
(const unsigned char*)&stream[0], stream.size(), 0, flags, nullptr); (const unsigned char*)stream.data(), stream.size(), 0, flags, nullptr);
assert(csuccess == 1); assert(csuccess == 1);
#endif #endif
} }

12
src/dbwrapper.h

@ -67,12 +67,12 @@ public:
{ {
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(ssKey.data(), ssKey.size());
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value; ssValue << value;
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue(&ssValue[0], ssValue.size()); leveldb::Slice slValue(ssValue.data(), ssValue.size());
batch.Put(slKey, slValue); batch.Put(slKey, slValue);
ssKey.clear(); ssKey.clear();
@ -84,7 +84,7 @@ public:
{ {
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(ssKey.data(), ssKey.size());
batch.Delete(slKey); batch.Delete(slKey);
ssKey.clear(); ssKey.clear();
@ -115,7 +115,7 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(ssKey.data(), ssKey.size());
piter->Seek(slKey); piter->Seek(slKey);
} }
@ -208,7 +208,7 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(ssKey.data(), ssKey.size());
std::string strValue; std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@ -242,7 +242,7 @@ public:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(ssKey.data(), ssKey.size());
std::string strValue; std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);

6
src/streams.h

@ -174,12 +174,10 @@ public:
Init(nTypeIn, nVersionIn); Init(nTypeIn, nVersionIn);
} }
#if !defined(_MSC_VER) || _MSC_VER >= 1300
CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend) CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
{ {
Init(nTypeIn, nVersionIn); Init(nTypeIn, nVersionIn);
} }
#endif
CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end()) CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
{ {
@ -245,6 +243,8 @@ public:
void clear() { vch.clear(); nReadPos = 0; } void clear() { vch.clear(); nReadPos = 0; }
iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); } iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); } void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
value_type* data() { return vch.data() + nReadPos; }
const value_type* data() const { return vch.data() + nReadPos; }
void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last) void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
{ {
@ -259,7 +259,6 @@ public:
vch.insert(it, first, last); vch.insert(it, first, last);
} }
#if !defined(_MSC_VER) || _MSC_VER >= 1300
void insert(iterator it, const char* first, const char* last) void insert(iterator it, const char* first, const char* last)
{ {
assert(last - first >= 0); assert(last - first >= 0);
@ -272,7 +271,6 @@ public:
else else
vch.insert(it, first, last); vch.insert(it, first, last);
} }
#endif
iterator erase(iterator it) iterator erase(iterator it)
{ {

8
src/wallet/db.cpp

@ -397,15 +397,15 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
break; break;
} }
if (pszSkip && if (pszSkip &&
strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
continue; continue;
if (strncmp(&ssKey[0], "\x07version", 8) == 0) { if (strncmp(ssKey.data(), "\x07version", 8) == 0) {
// Update version: // Update version:
ssValue.clear(); ssValue.clear();
ssValue << CLIENT_VERSION; ssValue << CLIENT_VERSION;
} }
Dbt datKey(&ssKey[0], ssKey.size()); Dbt datKey(ssKey.data(), ssKey.size());
Dbt datValue(&ssValue[0], ssValue.size()); Dbt datValue(ssValue.data(), ssValue.size());
int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE); int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
if (ret2 > 0) if (ret2 > 0)
fSuccess = false; fSuccess = false;

12
src/wallet/db.h

@ -122,7 +122,7 @@ protected:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000); ssKey.reserve(1000);
ssKey << key; ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size()); Dbt datKey(ssKey.data(), ssKey.size());
// Read // Read
Dbt datValue; Dbt datValue;
@ -158,13 +158,13 @@ protected:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000); ssKey.reserve(1000);
ssKey << key; ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size()); Dbt datKey(ssKey.data(), ssKey.size());
// Value // Value
CDataStream ssValue(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(10000); ssValue.reserve(10000);
ssValue << value; ssValue << value;
Dbt datValue(&ssValue[0], ssValue.size()); Dbt datValue(ssValue.data(), ssValue.size());
// Write // Write
int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE)); int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
@ -187,7 +187,7 @@ protected:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000); ssKey.reserve(1000);
ssKey << key; ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size()); Dbt datKey(ssKey.data(), ssKey.size());
// Erase // Erase
int ret = pdb->del(activeTxn, &datKey, 0); int ret = pdb->del(activeTxn, &datKey, 0);
@ -207,7 +207,7 @@ protected:
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(1000); ssKey.reserve(1000);
ssKey << key; ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size()); Dbt datKey(ssKey.data(), ssKey.size());
// Exists // Exists
int ret = pdb->exists(activeTxn, &datKey, 0); int ret = pdb->exists(activeTxn, &datKey, 0);
@ -234,7 +234,7 @@ protected:
Dbt datKey; Dbt datKey;
unsigned int fFlags = DB_NEXT; unsigned int fFlags = DB_NEXT;
if (setRange) { if (setRange) {
datKey.set_data(&ssKey[0]); datKey.set_data(ssKey.data());
datKey.set_size(ssKey.size()); datKey.set_size(ssKey.size());
fFlags = DB_SET_RANGE; fFlags = DB_SET_RANGE;
} }

Loading…
Cancel
Save