2017-08-21 12:00:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "symbolstorageinterface.h"
|
|
|
|
|
|
2018-10-11 18:09:55 +02:00
|
|
|
#include <filepathcachingfwd.h>
|
2017-08-17 12:44:52 +02:00
|
|
|
#include <sqliteexception.h>
|
2017-08-21 12:00:27 +02:00
|
|
|
#include <sqlitetransaction.h>
|
2018-10-11 18:09:55 +02:00
|
|
|
#include <sqlitetable.h>
|
2018-12-17 12:06:57 +01:00
|
|
|
#include <includesearchpath.h>
|
2017-08-17 12:44:52 +02:00
|
|
|
|
2019-01-23 14:10:58 +01:00
|
|
|
#include <utils/cpplanguage_details.h>
|
|
|
|
|
|
2018-01-22 14:21:01 +01:00
|
|
|
#include <QJsonArray>
|
2018-02-06 19:03:14 +01:00
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
2018-01-22 14:21:01 +01:00
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2019-03-21 17:55:24 +01:00
|
|
|
template<typename DatabaseType = Sqlite::Database>
|
2018-01-22 14:21:01 +01:00
|
|
|
class SymbolStorage final : public SymbolStorageInterface
|
2017-08-21 12:00:27 +02:00
|
|
|
{
|
2018-10-11 18:09:55 +02:00
|
|
|
using Database = DatabaseType;
|
|
|
|
|
using ReadStatement = typename Database::ReadStatement;
|
|
|
|
|
using WriteStatement = typename Database::WriteStatement;
|
2017-08-21 12:00:27 +02:00
|
|
|
|
|
|
|
|
public:
|
2018-10-11 18:09:55 +02:00
|
|
|
SymbolStorage(Database &database)
|
2019-03-13 15:09:30 +01:00
|
|
|
: transaction(database)
|
|
|
|
|
, database(database)
|
2017-08-21 12:00:27 +02:00
|
|
|
{
|
2019-03-13 15:09:30 +01:00
|
|
|
transaction.commit();
|
2017-08-21 12:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addSymbolsAndSourceLocations(const SymbolEntries &symbolEntries,
|
|
|
|
|
const SourceLocationEntries &sourceLocations) override
|
|
|
|
|
{
|
|
|
|
|
fillTemporarySymbolsTable(symbolEntries);
|
|
|
|
|
fillTemporaryLocationsTable(sourceLocations);
|
|
|
|
|
addNewSymbolsToSymbols();
|
|
|
|
|
syncNewSymbolsFromSymbols();
|
|
|
|
|
syncSymbolsIntoNewLocations();
|
|
|
|
|
deleteAllLocationsFromUpdatedFiles();
|
|
|
|
|
insertNewLocationsInLocations();
|
|
|
|
|
deleteNewSymbolsTable();
|
|
|
|
|
deleteNewLocationsTable();
|
2018-01-22 14:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:51:43 +02:00
|
|
|
void insertOrUpdateIndexingTimeStamps(const FilePathIds &filePathIds, TimeStamp indexingTimeStamp) override
|
2019-03-21 17:55:24 +01:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
Sqlite::ImmediateTransaction transaction{database};
|
|
|
|
|
|
|
|
|
|
for (FilePathId filePathId : filePathIds) {
|
|
|
|
|
inserOrUpdateIndexingTimesStampStatement.write(filePathId.filePathId,
|
|
|
|
|
indexingTimeStamp.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
} catch (const Sqlite::StatementIsBusy &) {
|
|
|
|
|
insertOrUpdateIndexingTimeStamps(filePathIds, indexingTimeStamp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insertOrUpdateIndexingTimeStamps(const FileStatuses &fileStatuses) override
|
|
|
|
|
{
|
|
|
|
|
for (FileStatus fileStatus : fileStatuses) {
|
|
|
|
|
inserOrUpdateIndexingTimesStampStatement.write(fileStatus.filePathId.filePathId,
|
|
|
|
|
fileStatus.lastModified);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:51:43 +02:00
|
|
|
SourceTimeStamps fetchIndexingTimeStamps() const override
|
2019-03-21 17:55:24 +01:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
Sqlite::DeferredTransaction transaction{database};
|
|
|
|
|
|
|
|
|
|
auto timeStamps = fetchIndexingTimeStampsStatement.template values<SourceTimeStamp, 2>(
|
|
|
|
|
1024);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
return timeStamps;
|
|
|
|
|
} catch (const Sqlite::StatementIsBusy &) {
|
|
|
|
|
return fetchIndexingTimeStamps();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:51:43 +02:00
|
|
|
SourceTimeStamps fetchIncludedIndexingTimeStamps(FilePathId sourcePathId) const override
|
2019-03-21 17:55:24 +01:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
Sqlite::DeferredTransaction transaction{database};
|
|
|
|
|
|
|
|
|
|
auto timeStamps = fetchIncludedIndexingTimeStampsStatement
|
|
|
|
|
.template values<SourceTimeStamp, 2>(1024, sourcePathId.filePathId);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
return timeStamps;
|
|
|
|
|
} catch (const Sqlite::StatementIsBusy &) {
|
|
|
|
|
return fetchIncludedIndexingTimeStamps(sourcePathId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePathIds fetchDependentSourceIds(const FilePathIds &sourcePathIds) const override
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
FilePathIds dependentSourceIds;
|
|
|
|
|
|
|
|
|
|
Sqlite::DeferredTransaction transaction{database};
|
|
|
|
|
|
|
|
|
|
for (FilePathId sourcePathId : sourcePathIds) {
|
|
|
|
|
FilePathIds newDependentSourceIds;
|
|
|
|
|
newDependentSourceIds.reserve(dependentSourceIds.size() + 1024);
|
|
|
|
|
|
|
|
|
|
auto newIds = fetchDependentSourceIdsStatement
|
|
|
|
|
.template values<FilePathId>(1024, sourcePathId.filePathId);
|
|
|
|
|
|
|
|
|
|
std::set_union(dependentSourceIds.begin(),
|
|
|
|
|
dependentSourceIds.end(),
|
|
|
|
|
newIds.begin(),
|
|
|
|
|
newIds.end(),
|
|
|
|
|
std::back_inserter(newDependentSourceIds));
|
|
|
|
|
|
|
|
|
|
dependentSourceIds = std::move(newDependentSourceIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
return dependentSourceIds;
|
|
|
|
|
} catch (const Sqlite::StatementIsBusy &) {
|
|
|
|
|
return fetchDependentSourceIds(sourcePathIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
void fillTemporarySymbolsTable(const SymbolEntries &symbolEntries)
|
|
|
|
|
{
|
2019-03-13 15:09:30 +01:00
|
|
|
WriteStatement &statement = insertSymbolsToNewSymbolsStatement;
|
2017-08-21 12:00:27 +02:00
|
|
|
|
|
|
|
|
for (const auto &symbolEntry : symbolEntries) {
|
|
|
|
|
statement.write(symbolEntry.first,
|
|
|
|
|
symbolEntry.second.usr,
|
2018-04-05 10:58:33 +02:00
|
|
|
symbolEntry.second.symbolName,
|
|
|
|
|
static_cast<uint>(symbolEntry.second.symbolKind));
|
2017-08-21 12:00:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fillTemporaryLocationsTable(const SourceLocationEntries &sourceLocations)
|
|
|
|
|
{
|
2019-03-13 15:09:30 +01:00
|
|
|
WriteStatement &statement = insertLocationsToNewLocationsStatement;
|
2017-08-21 12:00:27 +02:00
|
|
|
|
2018-04-09 13:30:30 +02:00
|
|
|
for (const auto &locationEntry : sourceLocations) {
|
|
|
|
|
statement.write(locationEntry.symbolId,
|
|
|
|
|
locationEntry.lineColumn.line,
|
|
|
|
|
locationEntry.lineColumn.column,
|
|
|
|
|
locationEntry.filePathId.filePathId,
|
|
|
|
|
int(locationEntry.kind));
|
2017-08-21 12:00:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
void addNewSymbolsToSymbols() { addNewSymbolsToSymbolsStatement.execute(); }
|
2017-08-21 12:00:27 +02:00
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
void syncNewSymbolsFromSymbols() { syncNewSymbolsFromSymbolsStatement.execute(); }
|
2017-08-21 12:00:27 +02:00
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
void syncSymbolsIntoNewLocations() { syncSymbolsIntoNewLocationsStatement.execute(); }
|
2017-08-21 12:00:27 +02:00
|
|
|
|
|
|
|
|
void deleteAllLocationsFromUpdatedFiles()
|
|
|
|
|
{
|
2019-03-13 15:09:30 +01:00
|
|
|
deleteAllLocationsFromUpdatedFilesStatement.execute();
|
2017-08-21 12:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
void insertNewLocationsInLocations() { insertNewLocationsInLocationsStatement.execute(); }
|
2017-08-21 12:00:27 +02:00
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
void deleteNewSymbolsTable() { deleteNewSymbolsTableStatement.execute(); }
|
2017-08-21 12:00:27 +02:00
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
void deleteNewLocationsTable() { deleteNewLocationsTableStatement.execute(); }
|
2017-08-21 12:00:27 +02:00
|
|
|
|
|
|
|
|
SourceLocationEntries sourceLocations() const
|
|
|
|
|
{
|
|
|
|
|
return SourceLocationEntries();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 18:09:55 +02:00
|
|
|
Sqlite::Table createNewSymbolsTable() const
|
|
|
|
|
{
|
|
|
|
|
Sqlite::Table table;
|
|
|
|
|
table.setName("newSymbols");
|
|
|
|
|
table.setUseTemporaryTable(true);
|
|
|
|
|
table.addColumn("temporarySymbolId", Sqlite::ColumnType::Integer, Sqlite::Contraint::PrimaryKey);
|
|
|
|
|
const Sqlite::Column &symbolIdColumn = table.addColumn("symbolId", Sqlite::ColumnType::Integer);
|
|
|
|
|
const Sqlite::Column &usrColumn = table.addColumn("usr", Sqlite::ColumnType::Text);
|
|
|
|
|
const Sqlite::Column &symbolNameColumn = table.addColumn("symbolName", Sqlite::ColumnType::Text);
|
|
|
|
|
table.addColumn("symbolKind", Sqlite::ColumnType::Integer);
|
|
|
|
|
table.addIndex({usrColumn, symbolNameColumn});
|
|
|
|
|
table.addIndex({symbolIdColumn});
|
|
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
table.initialize(database);
|
2018-10-11 18:09:55 +02:00
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sqlite::Table createNewLocationsTable() const
|
|
|
|
|
{
|
|
|
|
|
Sqlite::Table table;
|
|
|
|
|
table.setName("newLocations");
|
|
|
|
|
table.setUseTemporaryTable(true);
|
|
|
|
|
table.addColumn("temporarySymbolId", Sqlite::ColumnType::Integer);
|
|
|
|
|
table.addColumn("symbolId", Sqlite::ColumnType::Integer);
|
|
|
|
|
const Sqlite::Column &sourceIdColumn = table.addColumn("sourceId", Sqlite::ColumnType::Integer);
|
|
|
|
|
const Sqlite::Column &lineColumn = table.addColumn("line", Sqlite::ColumnType::Integer);
|
|
|
|
|
const Sqlite::Column &columnColumn = table.addColumn("column", Sqlite::ColumnType::Integer);
|
|
|
|
|
table.addColumn("locationKind", Sqlite::ColumnType::Integer);
|
|
|
|
|
table.addUniqueIndex({sourceIdColumn, lineColumn, columnColumn});
|
|
|
|
|
|
2019-03-13 15:09:30 +01:00
|
|
|
table.initialize(database);
|
2018-10-11 18:09:55 +02:00
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2019-03-13 15:09:30 +01:00
|
|
|
Sqlite::ImmediateNonThrowingDestructorTransaction transaction;
|
|
|
|
|
Database &database;
|
2018-10-11 18:09:55 +02:00
|
|
|
Sqlite::Table newSymbolsTablet{createNewSymbolsTable()};
|
|
|
|
|
Sqlite::Table newLocationsTable{createNewLocationsTable()};
|
2019-03-13 15:09:30 +01:00
|
|
|
WriteStatement insertSymbolsToNewSymbolsStatement{
|
2018-10-11 18:09:55 +02:00
|
|
|
"INSERT INTO newSymbols(temporarySymbolId, usr, symbolName, symbolKind) VALUES(?,?,?,?)",
|
2019-03-13 15:09:30 +01:00
|
|
|
database};
|
|
|
|
|
WriteStatement insertLocationsToNewLocationsStatement{
|
|
|
|
|
"INSERT OR IGNORE INTO newLocations(temporarySymbolId, line, column, sourceId, "
|
|
|
|
|
"locationKind) VALUES(?,?,?,?,?)",
|
|
|
|
|
database};
|
|
|
|
|
ReadStatement selectNewSourceIdsStatement{
|
|
|
|
|
"SELECT DISTINCT sourceId FROM newLocations WHERE NOT EXISTS (SELECT sourceId FROM sources "
|
|
|
|
|
"WHERE newLocations.sourceId == sources.sourceId)",
|
|
|
|
|
database};
|
|
|
|
|
WriteStatement addNewSymbolsToSymbolsStatement{
|
2018-10-11 18:09:55 +02:00
|
|
|
"INSERT INTO symbols(usr, symbolName, symbolKind) "
|
|
|
|
|
"SELECT usr, symbolName, symbolKind FROM newSymbols WHERE NOT EXISTS "
|
|
|
|
|
"(SELECT usr FROM symbols WHERE symbols.usr == newSymbols.usr)",
|
2019-03-13 15:09:30 +01:00
|
|
|
database};
|
|
|
|
|
WriteStatement syncNewSymbolsFromSymbolsStatement{
|
|
|
|
|
"UPDATE newSymbols SET symbolId = (SELECT symbolId FROM symbols WHERE newSymbols.usr = "
|
|
|
|
|
"symbols.usr)",
|
|
|
|
|
database};
|
|
|
|
|
WriteStatement syncSymbolsIntoNewLocationsStatement{
|
|
|
|
|
"UPDATE newLocations SET symbolId = (SELECT symbolId FROM newSymbols WHERE "
|
|
|
|
|
"newSymbols.temporarySymbolId = newLocations.temporarySymbolId)",
|
|
|
|
|
database};
|
|
|
|
|
WriteStatement deleteAllLocationsFromUpdatedFilesStatement{
|
2018-10-11 18:09:55 +02:00
|
|
|
"DELETE FROM locations WHERE sourceId IN (SELECT DISTINCT sourceId FROM newLocations)",
|
2019-03-13 15:09:30 +01:00
|
|
|
database};
|
|
|
|
|
WriteStatement insertNewLocationsInLocationsStatement{
|
|
|
|
|
"INSERT INTO locations(symbolId, line, column, sourceId, locationKind) SELECT symbolId, "
|
|
|
|
|
"line, column, sourceId, locationKind FROM newLocations",
|
|
|
|
|
database};
|
|
|
|
|
WriteStatement deleteNewSymbolsTableStatement{"DELETE FROM newSymbols", database};
|
|
|
|
|
WriteStatement deleteNewLocationsTableStatement{"DELETE FROM newLocations", database};
|
2019-03-21 17:55:24 +01:00
|
|
|
WriteStatement inserOrUpdateIndexingTimesStampStatement{
|
|
|
|
|
"INSERT INTO fileStatuses(sourceId, indexingTimeStamp) VALUES (?001, ?002) ON "
|
|
|
|
|
"CONFLICT(sourceId) DO UPDATE SET indexingTimeStamp = ?002",
|
|
|
|
|
database};
|
|
|
|
|
mutable ReadStatement fetchIncludedIndexingTimeStampsStatement{
|
|
|
|
|
"WITH RECURSIVE collectedDependencies(sourceId) AS (VALUES(?) UNION SELECT "
|
|
|
|
|
"dependencySourceId FROM sourceDependencies, collectedDependencies WHERE "
|
|
|
|
|
"sourceDependencies.sourceId == collectedDependencies.sourceId) SELECT DISTINCT sourceId, "
|
|
|
|
|
"indexingTimeStamp FROM collectedDependencies NATURAL JOIN fileStatuses ORDER BY sourceId",
|
|
|
|
|
database};
|
|
|
|
|
mutable ReadStatement fetchIndexingTimeStampsStatement{
|
|
|
|
|
"SELECT sourceId, indexingTimeStamp FROM fileStatuses", database};
|
|
|
|
|
mutable ReadStatement fetchDependentSourceIdsStatement{
|
|
|
|
|
"WITH RECURSIVE collectedDependencies(sourceId) AS (VALUES(?) UNION SELECT "
|
|
|
|
|
"sourceDependencies.sourceId FROM sourceDependencies, collectedDependencies WHERE "
|
|
|
|
|
"sourceDependencies.dependencySourceId == collectedDependencies.sourceId) SELECT sourceId "
|
|
|
|
|
"FROM collectedDependencies WHERE sourceId NOT IN (SELECT dependencySourceId FROM "
|
|
|
|
|
"sourceDependencies) ORDER BY sourceId",
|
|
|
|
|
database};
|
2017-08-21 12:00:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|