Skip to content

Commit

Permalink
Remove uses of stringstream
Browse files Browse the repository at this point in the history
Summary:
- stringstream is slower so don't use it
- Make sure all create table has a space after the name of the table. This makes it easier to copy the name when running `.schema` in sqlite

Reviewed By: markw65

Differential Revision: D13226007

fbshipit-source-id: 1d9aeb68e52eb4454786514906896dfe8eae0547
  • Loading branch information
WizKid authored and hhvm-bot committed Dec 6, 2018
1 parent fc4bbd8 commit 56901f1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 83 deletions.
35 changes: 17 additions & 18 deletions hphp/runtime/vm/func-emitter.cpp
Expand Up @@ -633,13 +633,12 @@ FuncRepoProxy::~FuncRepoProxy() {
} }


void FuncRepoProxy::createSchema(int repoId, RepoTxn& txn) { void FuncRepoProxy::createSchema(int repoId, RepoTxn& txn) {
std::stringstream ssCreate; auto createQuery = folly::sformat(
ssCreate << "CREATE TABLE " << m_repo.table(repoId, "Func") "CREATE TABLE {} "
<< "(unitSn INTEGER, funcSn INTEGER, preClassId INTEGER," "(unitSn INTEGER, funcSn INTEGER, preClassId INTEGER, name TEXT, "
" name TEXT, top INTEGER," " top INTEGER, extraData BLOB, PRIMARY KEY (unitSn, funcSn));",
" extraData BLOB," m_repo.table(repoId, "Func"));
" PRIMARY KEY (unitSn, funcSn));"; txn.exec(createQuery);
txn.exec(ssCreate.str());
} }


void FuncRepoProxy::InsertFuncStmt void FuncRepoProxy::InsertFuncStmt
Expand All @@ -648,11 +647,11 @@ void FuncRepoProxy::InsertFuncStmt
Id preClassId, const StringData* name, Id preClassId, const StringData* name,
bool top) { bool top) {
if (!prepared()) { if (!prepared()) {
std::stringstream ssInsert; auto insertQuery = folly::sformat(
ssInsert << "INSERT INTO " << m_repo.table(m_repoId, "Func") "INSERT INTO {} "
<< " VALUES(@unitSn, @funcSn, @preClassId, @name, " "VALUES(@unitSn, @funcSn, @preClassId, @name, @top, @extraData);",
" @top, @extraData);"; m_repo.table(m_repoId, "Func"));
txn.prepare(*this, ssInsert.str()); txn.prepare(*this, insertQuery);
} }


BlobEncoder extraBlob; BlobEncoder extraBlob;
Expand All @@ -671,12 +670,12 @@ void FuncRepoProxy::GetFuncsStmt
::get(UnitEmitter& ue) { ::get(UnitEmitter& ue) {
RepoTxn txn(m_repo); RepoTxn txn(m_repo);
if (!prepared()) { if (!prepared()) {
std::stringstream ssSelect; auto selectQuery = folly::sformat(
ssSelect << "SELECT funcSn,preClassId,name,top,extraData " "SELECT funcSn, preClassId, name, top, extraData "
"FROM " "FROM {} "
<< m_repo.table(m_repoId, "Func") "WHERE unitSn == @unitSn ORDER BY funcSn ASC;",
<< " WHERE unitSn == @unitSn ORDER BY funcSn ASC;"; m_repo.table(m_repoId, "Func"));
txn.prepare(*this, ssSelect.str()); txn.prepare(*this, selectQuery);
} }
RepoTxnQuery query(txn, *this); RepoTxnQuery query(txn, *this);
query.bindInt64("@unitSn", ue.m_sn); query.bindInt64("@unitSn", ue.m_sn);
Expand Down
33 changes: 17 additions & 16 deletions hphp/runtime/vm/preclass-emitter.cpp
Expand Up @@ -383,12 +383,12 @@ PreClassRepoProxy::~PreClassRepoProxy() {


void PreClassRepoProxy::createSchema(int repoId, RepoTxn& txn) { void PreClassRepoProxy::createSchema(int repoId, RepoTxn& txn) {
{ {
std::stringstream ssCreate; auto createQuery = folly::sformat(
ssCreate << "CREATE TABLE " << m_repo.table(repoId, "PreClass") "CREATE TABLE {} "
<< "(unitSn INTEGER, preClassId INTEGER, name TEXT," "(unitSn INTEGER, preClassId INTEGER, name TEXT, hoistable INTEGER, "
" hoistable INTEGER, extraData BLOB," " extraData BLOB, PRIMARY KEY (unitSn, preClassId));",
" PRIMARY KEY (unitSn, preClassId));"; m_repo.table(repoId, "PreClass"));
txn.exec(ssCreate.str()); txn.exec(createQuery);
} }
} }


Expand All @@ -398,11 +398,11 @@ void PreClassRepoProxy::InsertPreClassStmt
const StringData* name, const StringData* name,
PreClass::Hoistable hoistable) { PreClass::Hoistable hoistable) {
if (!prepared()) { if (!prepared()) {
std::stringstream ssInsert; auto insertQuery = folly::sformat(
ssInsert << "INSERT INTO " << m_repo.table(m_repoId, "PreClass") "INSERT INTO {} "
<< " VALUES(@unitSn, @preClassId, @name, @hoistable, " "VALUES(@unitSn, @preClassId, @name, @hoistable, @extraData);",
"@extraData);"; m_repo.table(m_repoId, "PreClass"));
txn.prepare(*this, ssInsert.str()); txn.prepare(*this, insertQuery);
} }


auto n = name->slice(); auto n = name->slice();
Expand All @@ -425,11 +425,12 @@ void PreClassRepoProxy::GetPreClassesStmt
::get(UnitEmitter& ue) { ::get(UnitEmitter& ue) {
RepoTxn txn(m_repo); RepoTxn txn(m_repo);
if (!prepared()) { if (!prepared()) {
std::stringstream ssSelect; auto selectQuery = folly::sformat(
ssSelect << "SELECT preClassId,name,hoistable,extraData FROM " "SELECT preClassId, name, hoistable, extraData "
<< m_repo.table(m_repoId, "PreClass") "FROM {} "
<< " WHERE unitSn == @unitSn ORDER BY preClassId ASC;"; "WHERE unitSn == @unitSn ORDER BY preClassId ASC;",
txn.prepare(*this, ssSelect.str()); m_repo.table(m_repoId, "PreClass"));
txn.prepare(*this, selectQuery);
} }
RepoTxnQuery query(txn, *this); RepoTxnQuery query(txn, *this);
query.bindInt64("@unitSn", ue.m_sn); query.bindInt64("@unitSn", ue.m_sn);
Expand Down
92 changes: 43 additions & 49 deletions hphp/runtime/vm/repo.cpp
Expand Up @@ -362,10 +362,10 @@ Repo::enumerateUnits(int repoId, bool preloadOnly, bool warn) {
void Repo::InsertFileHashStmt::insert(RepoTxn& txn, const StringData* path, void Repo::InsertFileHashStmt::insert(RepoTxn& txn, const StringData* path,
const MD5& md5) { const MD5& md5) {
if (!prepared()) { if (!prepared()) {
std::stringstream ssInsert; auto insertQuery = folly::sformat(
ssInsert << "INSERT INTO " << m_repo.table(m_repoId, "FileMd5") "INSERT INTO {} VALUES(@path, @md5);",
<< " VALUES(@path, @md5);"; m_repo.table(m_repoId, "FileMd5"));
txn.prepare(*this, ssInsert.str()); txn.prepare(*this, insertQuery);
} }
RepoTxnQuery query(txn, *this); RepoTxnQuery query(txn, *this);
query.bindStaticString("@path", path); query.bindStaticString("@path", path);
Expand All @@ -377,13 +377,14 @@ RepoStatus Repo::GetFileHashStmt::get(const char *path, MD5& md5) {
try { try {
RepoTxn txn(m_repo); RepoTxn txn(m_repo);
if (!prepared()) { if (!prepared()) {
std::stringstream ssSelect; auto selectQuery = folly::sformat(
ssSelect << "SELECT f.md5 FROM " "SELECT f.md5 "
<< m_repo.table(m_repoId, "FileMd5") "FROM {} AS f, {} AS u "
<< " AS f, " << m_repo.table(m_repoId, "Unit") "WHERE path == @path AND f.md5 == u.md5 "
<< " AS u WHERE path == @path AND f.md5 == u.md5" "ORDER BY unitSn DESC LIMIT 1;",
<< " ORDER BY unitSn DESC LIMIT 1;"; m_repo.table(m_repoId, "FileMd5"),
txn.prepare(*this, ssSelect.str()); m_repo.table(m_repoId, "Unit"));
txn.prepare(*this, selectQuery);
} }
RepoTxnQuery query(txn, *this); RepoTxnQuery query(txn, *this);
query.bindText("@path", path, strlen(path)); query.bindText("@path", path, strlen(path));
Expand Down Expand Up @@ -459,9 +460,8 @@ void Repo::commitMd5(UnitOrigin unitOrigin, UnitEmitter* ue) {
} }


std::string Repo::table(int repoId, const char* tablePrefix) { std::string Repo::table(int repoId, const char* tablePrefix) {
std::stringstream ss; return folly::sformat(
ss << dbName(repoId) << "." << tablePrefix << "_" << repoSchemaId(); "{}.{}_{}", dbName(repoId), tablePrefix, repoSchemaId());
return ss.str();
} }


void Repo::exec(const std::string& sQuery) { void Repo::exec(const std::string& sQuery) {
Expand Down Expand Up @@ -932,10 +932,9 @@ void Repo::attachLocal(const char* path, bool isWritable) {
} }
} }
try { try {
std::stringstream ssAttach; auto attachQuery = folly::sformat(
ssAttach << "ATTACH DATABASE '" << repoPath << "' as " "ATTACH DATABASE '{}' as {};", repoPath, dbName(RepoIdLocal));
<< dbName(RepoIdLocal) << ";"; exec(attachQuery);
exec(ssAttach.str());
pragmas(RepoIdLocal); pragmas(RepoIdLocal);
} catch (RepoExc& re) { } catch (RepoExc& re) {
// Failed to run pragmas on local DB - ignored // Failed to run pragmas on local DB - ignored
Expand Down Expand Up @@ -964,10 +963,9 @@ void Repo::pragmas(int repoId) {
} }


void Repo::getIntPragma(int repoId, const char* name, int& val) { void Repo::getIntPragma(int repoId, const char* name, int& val) {
std::stringstream ssPragma; auto pragmaQuery = folly::sformat("PRAGMA {}.{};", dbName(repoId), name);
ssPragma << "PRAGMA " << dbName(repoId) << "." << name << ";";
RepoStmt stmt(*this); RepoStmt stmt(*this);
stmt.prepare(ssPragma.str()); stmt.prepare(pragmaQuery);
RepoQuery query(stmt); RepoQuery query(stmt);
query.step(); query.step();
query.getInt(0, val); query.getInt(0, val);
Expand All @@ -981,9 +979,9 @@ void Repo::setIntPragma(int repoId, const char* name, int val) {


// Pragma writes must be executed outside transactions, since they may change // Pragma writes must be executed outside transactions, since they may change
// transaction behavior. // transaction behavior.
std::stringstream ssPragma; auto pragmaQuery = folly::sformat(
ssPragma << "PRAGMA " << dbName(repoId) << "." << name << " = " << val << ";"; "PRAGMA {}.{} = {};", dbName(repoId), name, val);
exec(ssPragma.str()); exec(pragmaQuery);
if (debug) { if (debug) {
// Verify that the pragma had the desired effect. // Verify that the pragma had the desired effect.
int newval = -1; int newval = -1;
Expand All @@ -996,10 +994,9 @@ void Repo::setIntPragma(int repoId, const char* name, int val) {
} }


void Repo::getTextPragma(int repoId, const char* name, std::string& val) { void Repo::getTextPragma(int repoId, const char* name, std::string& val) {
std::stringstream ssPragma; auto pragmaQuery = folly::sformat("PRAGMA {}.{};", dbName(repoId), name);
ssPragma << "PRAGMA " << dbName(repoId) << "." << name << ";";
RepoStmt stmt(*this); RepoStmt stmt(*this);
stmt.prepare(ssPragma.str()); stmt.prepare(pragmaQuery);
RepoQuery query(stmt); RepoQuery query(stmt);
const char* s; const char* s;
query.step(); query.step();
Expand All @@ -1015,10 +1012,9 @@ void Repo::setTextPragma(int repoId, const char* name, const char* val) {


// Pragma writes must be executed outside transactions, since they may change // Pragma writes must be executed outside transactions, since they may change
// transaction behavior. // transaction behavior.
std::stringstream ssPragma; auto pragmaQuery = folly::sformat(
ssPragma << "PRAGMA {}.{} = {};", dbName(repoId), name, val);
"PRAGMA " << dbName(repoId) << "." << name << " = '" << val << "';"; exec(pragmaQuery);
exec(ssPragma.str());
if (debug) { if (debug) {
// Verify that the pragma had the desired effect. // Verify that the pragma had the desired effect.
std::string newval = "?"; std::string newval = "?";
Expand Down Expand Up @@ -1054,12 +1050,12 @@ RepoStatus Repo::initSchema(int repoId, bool& isWritable,
bool Repo::schemaExists(int repoId) { bool Repo::schemaExists(int repoId) {
try { try {
RepoTxn txn(*this); RepoTxn txn(*this);
std::stringstream ssSelect; auto selectQuery = folly::sformat(
ssSelect << "SELECT product FROM " << table(repoId, "magic") << ";"; "SELECT product FROM {};", table(repoId, "magic"));
RepoStmt stmt(*this); RepoStmt stmt(*this);
// If the DB is 'new' and hasn't been initialized yet then we expect this // If the DB is 'new' and hasn't been initialized yet then we expect this
// prepare() to fail. // prepare() to fail.
stmt.prepare(ssSelect.str()); stmt.prepare(selectQuery);
// This SHOULDN'T fail - we create the table under a transaction - so if it // This SHOULDN'T fail - we create the table under a transaction - so if it
// exists then it should have our magic value. // exists then it should have our magic value.
RepoTxnQuery query(txn, stmt); RepoTxnQuery query(txn, stmt);
Expand All @@ -1079,24 +1075,22 @@ RepoStatus Repo::createSchema(int repoId, std::string& errorMsg) {
try { try {
RepoTxn txn(*this); RepoTxn txn(*this);
{ {
std::stringstream ssCreate; auto createQuery = folly::sformat(
ssCreate << "CREATE TABLE " << table(repoId, "magic") "CREATE TABLE {} (product TEXT);", table(repoId, "magic"));
<< "(product TEXT);"; txn.exec(createQuery);
txn.exec(ssCreate.str());

auto insertQuery = folly::sformat(
std::stringstream ssInsert; "INSERT INTO {} VALUES('{}');", table(repoId, "magic"), kMagicProduct);
ssInsert << "INSERT INTO " << table(repoId, "magic") txn.exec(insertQuery);
<< " VALUES('" << kMagicProduct << "');";
txn.exec(ssInsert.str());
} }
{ {
std::stringstream ssCreate; auto createQuery = folly::sformat(
ssCreate << "CREATE TABLE " << table(repoId, "FileMd5") "CREATE TABLE {} (path TEXT, md5 BLOB, UNIQUE(path, md5));",
<< "(path TEXT, md5 BLOB, UNIQUE(path, md5));"; table(repoId, "FileMd5"));
txn.exec(ssCreate.str()); txn.exec(createQuery);
} }
txn.exec(folly::format("CREATE TABLE {} (data BLOB);", txn.exec(folly::sformat("CREATE TABLE {} (data BLOB);",
table(repoId, "GlobalData")).str()); table(repoId, "GlobalData")));
m_urp.createSchema(repoId, txn); m_urp.createSchema(repoId, txn);
m_pcrp.createSchema(repoId, txn); m_pcrp.createSchema(repoId, txn);
m_frp.createSchema(repoId, txn); m_frp.createSchema(repoId, txn);
Expand Down

0 comments on commit 56901f1

Please sign in to comment.