src: storage refactoring

* renamed Storage to PersistentStorage
* added Storage as baseclass that implements data injection
* cleaned ParserClient interface
* added default constructors for storage types
* cleaned SqlteStorage by implementing the private getAll methods as template specializations
* added CXX flags to CMakeLists (this is needed for sqlite with visual studio)
* removed TestStorage.h/.cpp since these files were not used anymore.
This commit is contained in:
malte_langkabel
2016-05-11 13:23:31 +02:00
parent 2a73c42a18
commit 6a576bccc9
29 changed files with 2072 additions and 1820 deletions
+1
View File
@@ -59,6 +59,7 @@ endif ()
# enable fts4 module for sqlite
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_PARENTHESIS")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_PARENTHESIS")
# Clang ------------------------------------------------------------------------
+3 -3
View File
@@ -5,14 +5,14 @@
#include "clang/Tooling/JSONCompilationDatabase.h"
#include "data/parser/cxx/CxxParser.h"
#include "data/Storage.h"
#include "data/PersistentStorage.h"
#include "utility/file/FileRegister.h"
#include "utility/messaging/type/MessageFinishedParsing.h"
#include "utility/messaging/type/MessageStatus.h"
#include "utility/utility.h"
TaskParseCxx::TaskParseCxx(
Storage* storage,
PersistentStorage* storage,
const FileManager* fileManager,
const Parser::Arguments& arguments,
const std::vector<FilePath>& files
@@ -126,7 +126,7 @@ Task::TaskState TaskParseCxx::update()
m_parserClient->finishParsingFile(sourcePath);
m_parserClient->resetStorage();
m_storage->injectData(intermediateStorage);
m_storage->inject(intermediateStorage.get());
if (isSource)
{
+2
View File
@@ -161,6 +161,8 @@ add_files(
data/HierarchyCache.h
data/IntermediateStorage.cpp
data/IntermediateStorage.h
data/PersistentStorage.cpp
data/PersistentStorage.h
data/SqliteIndex.cpp
data/SqliteIndex.h
data/SqliteStorage.cpp
+2 -2
View File
@@ -10,7 +10,7 @@
#include "data/access/StorageAccessProxy.h"
#include "data/graph/Token.h"
#include "data/parser/cxx/TaskParseCxx.h"
#include "data/Storage.h"
#include "data/PersistentStorage.h"
#include "data/TaskCleanStorage.h"
#include "settings/ApplicationSettings.h"
#include "settings/ProjectSettings.h"
@@ -219,7 +219,7 @@ void Project::loadStorage(const FilePath& path)
FilePath dbPath = FilePath(path).replaceExtension("coatidb");
if (!m_storage || path != m_projectSettingsFilepath || !dbPath.exists())
{
m_storage = std::make_shared<Storage>(dbPath);
m_storage = std::make_shared<PersistentStorage>(dbPath);
}
}
+2 -2
View File
@@ -7,7 +7,7 @@
#include "data/parser/Parser.h"
class Storage;
class PersistentStorage;
class StorageAccessProxy;
class Project
@@ -57,7 +57,7 @@ private:
FilePath m_projectSettingsFilepath;
FileManager m_fileManager;
std::shared_ptr<Storage> m_storage;
std::shared_ptr<PersistentStorage> m_storage;
};
#endif // PROJECT_H
+50 -242
View File
@@ -12,32 +12,36 @@ IntermediateStorage::~IntermediateStorage()
{
}
Id IntermediateStorage::addEdge(int type, Id sourceId, Id targetId)
Id IntermediateStorage::addFile(const std::string& name, const std::string& filePath, const std::string& modificationTime)
{
std::shared_ptr<StorageEdge> edge = std::make_shared<StorageEdge>(0, type, sourceId, targetId);
std::shared_ptr<StorageFile> file = std::make_shared<StorageFile>(0, name, filePath, modificationTime);
std::string serialized = serialize(*(edge.get()));
std::unordered_map<std::string, Id>::const_iterator it = m_edgeNamesToIds.find(serialized);
if (it != m_edgeNamesToIds.end())
std::string serialized = serialize(*(file.get()));
std::unordered_map<std::string, Id>::const_iterator it = m_fileNamesToIds.find(serialized);
if (it != m_fileNamesToIds.end())
{
return it->second;
Id id = it->second;
if (m_fileIdsToData[id]->filePath.size() == 0) // stored information is incomplete.
{
m_fileIdsToData[id]->filePath = filePath; // so we replace it.
}
if (m_fileIdsToData[id]->modificationTime.size() == 0) // stored information is incomplete.
{
m_fileIdsToData[id]->modificationTime = modificationTime; // so we replace it.
}
return id;
}
Id id = m_nextId++;
m_edgeNamesToIds[serialized] = id;
m_edgeIdsToData[id] = edge;
if (type == Edge::EDGE_MEMBER)
{
m_nodeIdsToMemberEdgeIds[targetId] = id;
}
m_fileNamesToIds[serialized] = id;
m_fileIdsToData[id] = file;
return id;
}
Id IntermediateStorage::addNode(int type, const NameHierarchy& nameHierarchy, int definitionType)
Id IntermediateStorage::addNode(int type, const std::string& serializedName, int definitionType)
{
std::shared_ptr<StorageNode> node = std::make_shared<StorageNode>(0, type, NameHierarchy::serialize(nameHierarchy), definitionType);
std::shared_ptr<StorageNode> node = std::make_shared<StorageNode>(0, type, serializedName, definitionType);
std::string serialized = serialize(*(node.get()));
std::unordered_map<std::string, Id>::const_iterator it = m_nodeNamesToIds.find(serialized);
@@ -67,43 +71,20 @@ Id IntermediateStorage::addNode(int type, const NameHierarchy& nameHierarchy, in
return id;
}
Id IntermediateStorage::addFile(const std::string& name, const std::string& filePath, const std::string& modificationTime)
Id IntermediateStorage::addEdge(int type, Id sourceId, Id targetId)
{
std::shared_ptr<StorageFile> file = std::make_shared<StorageFile>(0, name, filePath, modificationTime);
std::shared_ptr<StorageEdge> edge = std::make_shared<StorageEdge>(0, type, sourceId, targetId);
std::string serialized = serialize(*(file.get()));
std::unordered_map<std::string, Id>::const_iterator it = m_fileNamesToIds.find(serialized);
if (it != m_fileNamesToIds.end())
{
Id id = it->second;
if (m_fileIdsToData[id]->filePath.size() == 0) // stored information is incomplete.
{
m_fileIdsToData[id] = file; // so we replace it.
}
return id;
}
Id id = m_nextId++;
m_fileNamesToIds[serialized] = id;
m_fileIdsToData[id] = file;
return id;
}
Id IntermediateStorage::addFile(const std::string& filePath)
{
std::shared_ptr<StorageFile> file = std::make_shared<StorageFile>(0, "", filePath, "");
std::string serialized = serialize(*(file.get()));
std::unordered_map<std::string, Id>::const_iterator it = m_fileNamesToIds.find(serialized);
if (it != m_fileNamesToIds.end())
std::string serialized = serialize(*(edge.get()));
std::unordered_map<std::string, Id>::const_iterator it = m_edgeNamesToIds.find(serialized);
if (it != m_edgeNamesToIds.end())
{
return it->second;
}
Id id = m_nextId++;
m_fileNamesToIds[serialized] = id;
m_fileIdsToData[id] = file;
m_edgeNamesToIds[serialized] = id;
m_edgeIdsToData[id] = edge;
return id;
}
@@ -126,229 +107,48 @@ Id IntermediateStorage::addLocalSymbol(const std::string& name)
return id;
}
void IntermediateStorage::addSourceLocation(Id elementId, const ParseLocation& location, int type)
void IntermediateStorage::addSourceLocation(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type)
{
Id fileNodeId = addFile(location.filePath.str());
m_sourceLocations.push_back(StorageSourceLocation(
0,
elementId,
fileNodeId,
location.startLineNumber,
location.startColumnNumber,
location.endLineNumber,
location.endColumnNumber,
startLine,
startCol,
endLine,
endCol,
type
));
}
void IntermediateStorage::addComponentAccess(Id nodeId, int type)
void IntermediateStorage::addComponentAccess(Id edgeId, int type)
{
std::unordered_map<Id, Id>::const_iterator it = m_nodeIdsToMemberEdgeIds.find(nodeId);
if (it != m_nodeIdsToMemberEdgeIds.end())
{
m_componentAccesses.push_back(StorageComponentAccess(it->second, type));
}
else
{
LOG_ERROR_STREAM(<< "Cannot assign access" << type << " to node id " << nodeId << " because it's not a child node.");
}
m_componentAccesses.push_back(StorageComponentAccess(edgeId, type));
}
void IntermediateStorage::addCommentLocation(const ParseLocation& location)
void IntermediateStorage::addCommentLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol)
{
Id fileNodeId = addFile(location.filePath.str());
m_commentLocations.push_back(StorageCommentLocation(
0,
fileNodeId,
location.startLineNumber,
location.startColumnNumber,
location.endLineNumber,
location.endColumnNumber
startLine,
startCol,
endLine,
endCol
));
}
void IntermediateStorage::addError(const std::string& message, bool fatal, const ParseLocation& location)
void IntermediateStorage::addError(const std::string& message, bool fatal, const std::string& filePath, uint startLine, uint startCol)
{
m_errors.push_back(StorageError(
message,
fatal,
location.filePath.str(),
location.startLineNumber,
location.startColumnNumber
filePath,
startLine,
startCol
));
}
void IntermediateStorage::transferToStorage(SqliteStorage& storage)
{
storage.beginTransaction();
std::unordered_map<Id, Id> clientIdToStorageId;
for (std::unordered_map<Id, std::shared_ptr<StorageFile>>::const_iterator it = m_fileIdsToData.begin(); it != m_fileIdsToData.end(); it++)
{
if (it->second->name.size() > 0)
{
Id fileNodeId = storage.getFileByPath(it->second->filePath).id;
if (fileNodeId == 0)
{
NameHierarchy nameHierarchy;
nameHierarchy.push(std::make_shared<NameElement>(it->second->name));
fileNodeId = storage.addFile(
NameHierarchy::serialize(nameHierarchy),
it->second->filePath,
it->second->modificationTime
);
}
clientIdToStorageId[it->first] = fileNodeId;
}
}
for (std::map<Id, std::shared_ptr<StorageNode>>::const_iterator it = m_nodeIdsToData.begin(); it != m_nodeIdsToData.end(); it++)
{
StorageNode clientNode = *(it->second.get());
StorageNode storageNode = storage.getNodeBySerializedName(clientNode.serializedName);
Id storageNodeId = storageNode.id;
if (storageNodeId)
{
if (clientNode.definitionType > 0)
{
if (storageNode.definitionType == 0)
{
storage.setNodeDefinitionType(clientNode.definitionType, storageNode.id);
if(storageNode.type < clientNode.type)
{
storage.setNodeType(clientNode.type, storageNode.id);
}
}
}
}
else
{
storageNodeId = storage.addNode(clientNode.type, clientNode.serializedName, clientNode.definitionType);
}
clientIdToStorageId[it->first] = storageNodeId;
}
for (std::map<Id, std::shared_ptr<StorageEdge>>::const_iterator it = m_edgeIdsToData.begin(); it != m_edgeIdsToData.end(); it++)
{
std::unordered_map<Id, Id>::const_iterator it2;
it2 = clientIdToStorageId.find(it->second->sourceNodeId);
if (it2 == clientIdToStorageId.end())
{
continue;
}
Id storageSourceId = it2->second;
it2 = clientIdToStorageId.find(it->second->targetNodeId);
if (it2 == clientIdToStorageId.end())
{
continue;
}
Id storageTargetId = it2->second;
Id edgeId = storage.getEdgeBySourceTargetType(storageSourceId, storageTargetId, it->second->type).id;
if (!edgeId)
{
edgeId = storage.addEdge(it->second->type, storageSourceId, storageTargetId);
}
clientIdToStorageId[it->first] = edgeId;
}
for (std::map<Id, std::shared_ptr<StorageLocalSymbol>>::const_iterator it = m_localSymbolIdsToData.begin(); it != m_localSymbolIdsToData.end(); it++)
{
StorageLocalSymbol clientLocalSymbol = *(it->second.get());
StorageLocalSymbol storageLocalSymbol = storage.getLocalSymbolByName(clientLocalSymbol.name);
Id storageLocalSymbolId = storageLocalSymbol.id;
if (storageLocalSymbolId == 0)
{
storageLocalSymbolId = storage.addLocalSymbol(clientLocalSymbol.name);
}
clientIdToStorageId[it->first] = storageLocalSymbolId;
}
for (size_t i = 0; i < m_sourceLocations.size(); i++)
{
StorageSourceLocation sourceLocation = m_sourceLocations[i];
std::unordered_map<Id, Id>::const_iterator it;
it = clientIdToStorageId.find(sourceLocation.elementId);
if (it == clientIdToStorageId.end())
{
continue;
}
Id storageElementId = it->second;
it = clientIdToStorageId.find(sourceLocation.fileNodeId);
if (it == clientIdToStorageId.end())
{
continue;
}
Id storageFileNodeId = it->second;
storage.addSourceLocation(
storageElementId,
storageFileNodeId,
sourceLocation.startLine,
sourceLocation.startCol,
sourceLocation.endLine,
sourceLocation.endCol,
sourceLocation.type
);
}
for (size_t i = 0; i < m_componentAccesses.size(); i++)
{
StorageComponentAccess componentAccess = m_componentAccesses[i];
std::unordered_map<Id, Id>::const_iterator it;
it = clientIdToStorageId.find(componentAccess.memberEdgeId);
if (it == clientIdToStorageId.end())
{
continue;
}
Id storageMemberEdgeId = it->second;
storage.addComponentAccess(storageMemberEdgeId, componentAccess.type);
}
for (size_t i = 0; i < m_commentLocations.size(); i++)
{
StorageCommentLocation commentLocation = m_commentLocations[i];
std::unordered_map<Id, Id>::const_iterator it;
it = clientIdToStorageId.find(commentLocation.fileNodeId);
if (it == clientIdToStorageId.end())
{
continue;
}
Id storageFileNodeId = it->second;
storage.addCommentLocation(
storageFileNodeId,
commentLocation.startLine,
commentLocation.startCol,
commentLocation.endLine,
commentLocation.endCol
);
}
for (size_t i = 0; i < m_errors.size(); i++)
{
StorageError error = m_errors[i];
storage.addError(
error.message,
error.fatal,
error.filePath,
error.lineNumber,
error.columnNumber
);
}
storage.commitTransaction();
}
void IntermediateStorage::forEachFile(std::function<void(const Id /*id*/, const StorageFile& /*data*/)> callback) const
{
for (std::unordered_map<Id, std::shared_ptr<StorageFile>>::const_iterator it = m_fileIdsToData.begin(); it != m_fileIdsToData.end(); it++)
@@ -373,6 +173,14 @@ void IntermediateStorage::forEachEdge(std::function<void(const Id /*id*/, const
}
}
void IntermediateStorage::forEachLocalSymbol(std::function<void(const Id /*id*/, const StorageLocalSymbol& /*data*/)> callback) const
{
for (std::map<Id, std::shared_ptr<StorageLocalSymbol>>::const_iterator it = m_localSymbolIdsToData.begin(); it != m_localSymbolIdsToData.end(); it++)
{
callback(it->first, *(it->second.get()));
}
}
void IntermediateStorage::forEachSourceLocation(std::function<void(const StorageSourceLocation& /*data*/)> callback) const
{
for (std::vector<StorageSourceLocation>::const_iterator it = m_sourceLocations.begin(); it != m_sourceLocations.end(); it++)
+20 -25
View File
@@ -2,40 +2,35 @@
#define INTERMEDIATE_STORAGE_H
#include <memory>
#include <string>
#include <map>
#include <unordered_map>
#include "utility/types.h"
#include "data/name/NameHierarchy.h"
#include "data/parser/ParseLocation.h"
#include "data/SqliteStorage.h"
#include "data/StorageTypes.h"
#include "data/Storage.h"
class IntermediateStorage
class IntermediateStorage: public Storage
{
public:
IntermediateStorage();
~IntermediateStorage();
Id addEdge(int type, Id sourceId, Id targetId);
Id addNode(int type, const NameHierarchy& nameHierarchy, int definitionType);
Id addFile(const std::string& name, const std::string& filePath, const std::string& modificationTime);
Id addFile(const std::string& filePath);
Id addLocalSymbol(const std::string& name);
void addSourceLocation(Id elementId, const ParseLocation& location, int type);
void addComponentAccess(Id nodeId , int type);
void addCommentLocation(const ParseLocation& location);
void addError(const std::string& message, bool fatal, const ParseLocation& location);
virtual ~IntermediateStorage();
void transferToStorage(SqliteStorage& storage); // TODO: remove this and use foreach-callbacks instead
virtual Id addFile(const std::string& name, const std::string& filePath, const std::string& modificationTime);
virtual Id addNode(int type, const std::string& serializedName, int definitionType);
virtual Id addEdge(int type, Id sourceId, Id targetId);
virtual Id addLocalSymbol(const std::string& name);
virtual void addSourceLocation(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type);
virtual void addComponentAccess(Id edgeId , int type);
virtual void addCommentLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol);
virtual void addError(const std::string& message, bool fatal, const std::string& filePath, uint startLine, uint startCol);
void forEachFile(std::function<void(const Id /*id*/, const StorageFile& /*data*/)> callback) const;
void forEachNode(std::function<void(const Id /*id*/, const StorageNode& /*data*/)> callback) const;
void forEachEdge(std::function<void(const Id /*id*/, const StorageEdge& /*data*/)> callback) const;
void forEachSourceLocation(std::function<void(const StorageSourceLocation& /*data*/)> callback) const;
void forEachComponentAccess(std::function<void(const StorageComponentAccess& /*data*/)> callback) const;
void forEachCommentLocation(std::function<void(const StorageCommentLocation& /*data*/)> callback) const;
void forEachError(std::function<void(const StorageError& /*data*/)> callback) const;
virtual void forEachFile(std::function<void(const Id /*id*/, const StorageFile& /*data*/)> callback) const;
virtual void forEachNode(std::function<void(const Id /*id*/, const StorageNode& /*data*/)> callback) const;
virtual void forEachEdge(std::function<void(const Id /*id*/, const StorageEdge& /*data*/)> callback) const;
virtual void forEachLocalSymbol(std::function<void(const Id /*id*/, const StorageLocalSymbol& /*data*/)> callback) const;
virtual void forEachSourceLocation(std::function<void(const StorageSourceLocation& /*data*/)> callback) const;
virtual void forEachComponentAccess(std::function<void(const StorageComponentAccess& /*data*/)> callback) const;
virtual void forEachCommentLocation(std::function<void(const StorageCommentLocation& /*data*/)> callback) const;
virtual void forEachError(std::function<void(const StorageError& /*data*/)> callback) const;
private:
std::string serialize(const StorageEdge& edge);
File diff suppressed because it is too large Load Diff
+153
View File
@@ -0,0 +1,153 @@
#ifndef PERSISTENT_STORAGE_H
#define PERSISTENT_STORAGE_H
#include <memory>
#include <vector>
#include "utility/file/FilePath.h"
#include "data/access/StorageAccess.h"
#include "data/graph/token_component/TokenComponentAccess.h"
#include "data/location/TokenLocationCollection.h"
#include "data/parser/ParserClient.h"
#include "data/parser/ParseLocation.h"
#include "data/search/SearchIndex.h"
#include "data/HierarchyCache.h"
#include "data/SqliteStorage.h"
#include "data/Storage.h"
#include "data/parser/ParserClientImpl.h"
class PersistentStorage
: public Storage
, public StorageAccess
{
public:
PersistentStorage(const FilePath& dbPath);
virtual ~PersistentStorage();
virtual Id addFile(const std::string& name, const std::string& filePath, const std::string& modificationTime);
virtual Id addNode(int type, const std::string& serializedName, int definitionType);
virtual Id addEdge(int type, Id sourceId, Id targetId);
virtual Id addLocalSymbol(const std::string& name);
virtual void addSourceLocation(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type);
virtual void addComponentAccess(Id edgeId , int type);
virtual void addCommentLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol);
virtual void addError(const std::string& message, bool fatal, const std::string& filePath, uint startLine, uint startCol);
virtual void forEachFile(std::function<void(const Id /*id*/, const StorageFile& /*data*/)> callback) const;
virtual void forEachNode(std::function<void(const Id /*id*/, const StorageNode& /*data*/)> callback) const;
virtual void forEachEdge(std::function<void(const Id /*id*/, const StorageEdge& /*data*/)> callback) const;
virtual void forEachLocalSymbol(std::function<void(const Id /*id*/, const StorageLocalSymbol& /*data*/)> callback) const;
virtual void forEachSourceLocation(std::function<void(const StorageSourceLocation& /*data*/)> callback) const;
virtual void forEachComponentAccess(std::function<void(const StorageComponentAccess& /*data*/)> callback) const;
virtual void forEachCommentLocation(std::function<void(const StorageCommentLocation& /*data*/)> callback) const;
virtual void forEachError(std::function<void(const StorageError& /*data*/)> callback) const;
virtual void startInjection();
virtual void finishInjection();
FilePath getDbFilePath() const;
Version getVersion() const;
void init();
void clear();
void clearCaches();
std::set<FilePath> getDependingFilePaths(const std::set<FilePath>& filePaths);
std::set<FilePath> getDependingFilePaths(const FilePath& filePath);
void clearFileElements(const std::vector<FilePath>& filePaths);
void removeUnusedNames();
std::vector<FileInfo> getInfoOnAllFiles() const;
void logStats() const;
void startParsing();
void finishParsing();
// StorageAccess implementation
virtual Id getIdForNodeWithNameHierarchy(const NameHierarchy& nameHierarchy) const;
virtual Id getIdForEdge(
Edge::EdgeType type, const NameHierarchy& fromNameHierarchy, const NameHierarchy& toNameHierarchy) const;
virtual NameHierarchy getNameHierarchyForNodeWithId(Id nodeId) const;
virtual Node::NodeType getNodeTypeForNodeWithId(Id nodeId) const;
virtual std::shared_ptr<TokenLocationCollection> getFullTextSearchLocations(const std::string& searchTerm) const;
virtual std::vector<SearchMatch> getAutocompletionMatches(const std::string& query) const;
virtual std::vector<SearchMatch> getSearchMatchesForTokenIds(const std::vector<Id>& elementIds) const;
virtual std::shared_ptr<Graph> getGraphForAll() const;
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId, Id* declarationId) const;
virtual std::vector<Id> getNodeIdsForLocationIds(const std::vector<Id>& locationIds) const;
virtual std::vector<Id> getLocalSymbolIdsForLocationIds(const std::vector<Id>& locationIds) const;
virtual std::vector<Id> getTokenIdsForMatches(const std::vector<SearchMatch>& matches) const;
virtual Id getTokenIdForFileNode(const FilePath& filePath) const;
virtual std::vector<Id> getTokenIdsForAggregationEdge(Id sourceId, Id targetId) const;
virtual std::shared_ptr<TokenLocationCollection> getTokenLocationsForTokenIds(
const std::vector<Id>& tokenIds
) const;
virtual std::shared_ptr<TokenLocationCollection> getTokenLocationsForLocationIds(
const std::vector<Id>& locationIds
) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForFile(const std::string& filePath) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
) const;
virtual TokenLocationCollection getErrorTokenLocations(std::vector<ErrorInfo>* errors) const;
virtual std::shared_ptr<TokenLocationFile> getCommentLocationsInFile(const FilePath& filePath) const;
virtual std::shared_ptr<TextAccess> getFileContent(const FilePath& filePath) const;
virtual FileInfo getFileInfoForFilePath(const FilePath& filePath) const;
virtual std::vector<FileInfo> getFileInfosForFilePaths(const std::vector<FilePath>& filePaths) const;
virtual ErrorCountInfo getErrorCount() const;
virtual StorageStats getStorageStats() const;
private:
Id getFileNodeId(const FilePath& filePath) const;
FilePath getFileNodePath(Id fileId) const;
Id getLastVisibleParentNodeId(const Id nodeId) const;
std::vector<Id> getAllChildNodeIds(const Id nodeId) const;
void addNodesToGraph(const std::vector<Id>& nodeIds, Graph* graph) const;
void addEdgesToGraph(const std::vector<Id>& edgeIds, Graph* graph) const;
void addNodesWithChildrenAndEdgesToGraph(
const std::vector<Id>& nodeIds,
const std::vector<Id>& edgeIds, Graph* graph
) const;
void addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const;
void addComponentAccessToGraph(Graph* graph) const;
void buildSearchIndex();
void buildHierarchyCache();
void optimizeFTSTable();
void log(std::string type, std::string str, const ParseLocation& location) const;
int m_preInjectionErrorCount;
SearchIndex m_commandIndex;
SearchIndex m_elementIndex;
SqliteStorage m_sqliteStorage;
mutable std::map <FilePath, Id> m_fileNodeIds;
HierarchyCache m_hierarchyCache;
};
#endif // PERSISTENT_STORAGE_H
+213 -224
View File
@@ -217,9 +217,9 @@ Id SqliteStorage::addError(const std::string& message, bool fatal, const std::st
void SqliteStorage::removeElement(Id id)
{
m_database.execDML((
"DELETE FROM element WHERE id == " + std::to_string(id) + ";"
).c_str());
std::vector<Id> ids;
ids.push_back(id);
removeElements(ids);
}
void SqliteStorage::removeElements(const std::vector<Id>& ids)
@@ -264,22 +264,6 @@ void SqliteStorage::removeErrorsInFiles(const std::vector<FilePath>& filePaths)
).c_str());
}
StorageNode SqliteStorage::getFirstNode() const
{
std::vector<StorageNode> nodes = getAllNodes("LIMIT 1");
if (nodes.size())
{
return nodes[0];
}
return StorageNode();
}
std::vector<StorageNode> SqliteStorage::getAllNodes() const
{
return getAllNodes("");
}
bool SqliteStorage::isEdge(Id elementId) const
{
int count = m_database.execScalar(("SELECT count(*) FROM edge WHERE id = " + std::to_string(elementId) + ";").c_str());
@@ -321,101 +305,56 @@ StorageEdge SqliteStorage::getEdgeById(Id edgeId) const
StorageEdge SqliteStorage::getEdgeBySourceTargetType(Id sourceId, Id targetId, int type) const
{
StorageEdge edge(
getFirstResult<Id>(
"SELECT id FROM edge WHERE "
"source_node_id == " + std::to_string(sourceId) + " AND "
"target_node_id == " + std::to_string(targetId) + " AND "
"type == " + std::to_string(type) + ";"
),
type, sourceId, targetId
return getFirst<StorageEdge>("WHERE "
"source_node_id == " + std::to_string(sourceId) + " AND "
"target_node_id == " + std::to_string(targetId) + " AND "
"type == " + std::to_string(type)
);
return edge;
}
std::vector<StorageEdge> SqliteStorage::getEdgesByIds(const std::vector<Id>& edgeIds) const
{
return getAllEdges("WHERE id IN (" + utility::join(utility::toStrings(edgeIds), ',') + ")");
return getAll<StorageEdge>("WHERE id IN (" + utility::join(utility::toStrings(edgeIds), ',') + ")");
}
std::vector<StorageEdge> SqliteStorage::getEdgesBySourceId(Id sourceId) const
{
return getAllEdges("WHERE source_node_id == " + std::to_string(sourceId));
return getAll<StorageEdge>("WHERE source_node_id == " + std::to_string(sourceId));
}
std::vector<StorageEdge> SqliteStorage::getEdgesBySourceIds(const std::vector<Id>& sourceIds) const
{
return getAllEdges("WHERE source_node_id IN (" + utility::join(utility::toStrings(sourceIds), ',') + ")");
return getAll<StorageEdge>("WHERE source_node_id IN (" + utility::join(utility::toStrings(sourceIds), ',') + ")");
}
std::vector<StorageEdge> SqliteStorage::getEdgesByTargetId(Id targetId) const
{
return getAllEdges("WHERE target_node_id == " + std::to_string(targetId));
return getAll<StorageEdge>("WHERE target_node_id == " + std::to_string(targetId));
}
std::vector<StorageEdge> SqliteStorage::getEdgesByTargetIds(const std::vector<Id>& targetIds) const
{
return getAllEdges("WHERE target_node_id IN (" + utility::join(utility::toStrings(targetIds), ',') + ")");
return getAll<StorageEdge>("WHERE target_node_id IN (" + utility::join(utility::toStrings(targetIds), ',') + ")");
}
std::vector<StorageEdge> SqliteStorage::getEdgesBySourceOrTargetId(Id id) const
{
return getAllEdges("WHERE source_node_id == " + std::to_string(id) + " OR target_node_id == " + std::to_string(id));
return getAll<StorageEdge>("WHERE source_node_id == " + std::to_string(id) + " OR target_node_id == " + std::to_string(id));
}
std::vector<StorageEdge> SqliteStorage::getEdgesByType(int type) const
{
return getAllEdges("WHERE type == " + std::to_string(type));
return getAll<StorageEdge>("WHERE type == " + std::to_string(type));
}
std::vector<StorageEdge> SqliteStorage::getEdgesBySourceType(Id sourceId, int type) const
{
std::vector<StorageEdge> edges;
CppSQLite3Query q = m_database.execQuery((
"SELECT id, target_node_id FROM edge WHERE "
"source_node_id == " + std::to_string(sourceId) + " AND "
"type == " + std::to_string(type) + ";"
).c_str());
while (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id targetId = q.getIntField(1, 0);
if (id != 0 && targetId != 0)
{
edges.push_back(StorageEdge(id, type, sourceId, targetId));
}
q.nextRow();
}
return edges;
return getAll<StorageEdge>("WHERE source_node_id == " + std::to_string(sourceId) + " AND type == " + std::to_string(type));
}
std::vector<StorageEdge> SqliteStorage::getEdgesByTargetType(Id targetId, int type) const
{
std::vector<StorageEdge> edges;
CppSQLite3Query q = m_database.execQuery((
"SELECT id, source_node_id FROM edge WHERE "
"target_node_id == " + std::to_string(targetId) + " AND "
"type == " + std::to_string(type) + ";"
).c_str());
while (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id sourceId = q.getIntField(1, 0);
if (id != 0 && sourceId != 0)
{
edges.push_back(StorageEdge(id, type, sourceId, targetId));
}
q.nextRow();
}
return edges;
return getAll<StorageEdge>("WHERE target_node_id == " + std::to_string(targetId) + " AND type == " + std::to_string(type));
}
void SqliteStorage::optimizeFTSTable() const
@@ -508,59 +447,39 @@ StorageNode SqliteStorage::getNodeById(Id id) const
{
if (id != 0)
{
return getFirstNode("WHERE id == " + std::to_string(id));
return getFirst<StorageNode>("WHERE id == " + std::to_string(id));
}
return StorageNode();
}
StorageNode SqliteStorage::getNodeBySerializedName(const std::string& serializedName) const
{
return getFirstNode("WHERE serialized_name == '" + serializedName + "'");
return getFirst<StorageNode>("WHERE serialized_name == '" + serializedName + "'");
}
std::vector<StorageNode> SqliteStorage::getNodesByIds(const std::vector<Id>& nodeIds) const
{
return getAllNodes("WHERE id IN (" + utility::join(utility::toStrings(nodeIds), ',') + ")");
return getAll<StorageNode>("WHERE id IN (" + utility::join(utility::toStrings(nodeIds), ',') + ")");
}
StorageLocalSymbol SqliteStorage::getLocalSymbolByName(const std::string& name) const
{
StorageLocalSymbol localSymbol(
getFirstResult<Id>(
"SELECT id FROM local_symbol WHERE "
"name == '" + name + "';"
),
name
);
return localSymbol;
return getFirst<StorageLocalSymbol>("WHERE name == '" + name + "'");
}
StorageFile SqliteStorage::getFileById(const Id id) const
{
return getFirstFile(
"SELECT node.id, node.serialized_name, file.path, file.modification_time FROM node INNER JOIN file ON node.id = file.id "
"WHERE node.id == " + std::to_string(id) + ";"
);
return getFirst<StorageFile>("WHERE node.id == " + std::to_string(id));
}
StorageFile SqliteStorage::getFileByPath(const FilePath& filePath) const
{
StorageFile storageFile = getFirstFile(
"SELECT node.id, node.serialized_name, file.path, file.modification_time FROM node INNER JOIN file ON node.id = file.id "
"WHERE file.path == '" + filePath.str() + "';"
);
return storageFile;
return getFirst<StorageFile>("WHERE file.path == '" + filePath.str() + "'");
}
std::vector<StorageFile> SqliteStorage::getFilesByPaths(const std::vector<FilePath>& filePaths) const
{
return getAllFiles("WHERE file.path IN ('" + utility::join(utility::toStrings(filePaths), "', '") + "')");
}
std::vector<StorageFile> SqliteStorage::getAllFiles() const
{
return getAllFiles("");
return getAll<StorageFile>("WHERE file.path IN ('" + utility::join(utility::toStrings(filePaths), "', '") + "')");
}
std::vector<Id> SqliteStorage::getAllFileIds() const
@@ -610,8 +529,8 @@ void SqliteStorage::setNodeDefinitionType(int definitionType, Id nodeId)
StorageSourceLocation SqliteStorage::getSourceLocationById(const Id id) const
{
return getFirstSourceLocation(
"SELECT id, element_id, file_node_id, start_line, start_column, end_line, end_column, type FROM source_location WHERE id == " + std::to_string(id) + ";"
return getFirst<StorageSourceLocation>(
"WHERE id == " + std::to_string(id) + ";"
);
}
@@ -689,9 +608,14 @@ std::vector<StorageSourceLocation> SqliteStorage::getTokenLocationsForElementIds
Id SqliteStorage::getElementIdByLocationId(Id locationId) const
{
return getFirstResult<Id>(
"SELECT element_id FROM source_location WHERE id == " + std::to_string(locationId) + ";"
);
CppSQLite3Query q = m_database.execQuery((
"SELECT element_id FROM source_location WHERE id == " + std::to_string(locationId) + " LIMIT 1;"
).c_str());
if (!q.eof())
{
return q.getIntField(0, 0);
}
return 0;
}
StorageComponentAccess SqliteStorage::getComponentAccessByMemberEdgeId(Id memberEdgeId) const
@@ -733,77 +657,52 @@ std::vector<StorageComponentAccess> SqliteStorage::getComponentAccessByMemberEdg
std::vector<StorageCommentLocation> SqliteStorage::getCommentLocationsInFile(const FilePath& filePath) const
{
Id fileNodeId = getFileByPath(filePath.str()).id;
CppSQLite3Query q = m_database.execQuery((
"SELECT id, file_node_id, start_line, start_column, end_line, end_column FROM comment_location "
"WHERE file_node_id == " + std::to_string(fileNodeId) + ";"
).c_str());
std::vector<StorageCommentLocation> commentLocations;
while (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id fileNodeId = q.getIntField(1, 0);
const int startLineNumber = q.getIntField(2, -1);
const int startColNumber = q.getIntField(3, -1);
const int endLineNumber = q.getIntField(4, -1);
const int endColNumber = q.getIntField(5, -1);
if (id != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && endColNumber != -1)
{
commentLocations.push_back(StorageCommentLocation(
id, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber
));
}
q.nextRow();
}
return commentLocations;
}
std::vector<StorageError> SqliteStorage::getAllErrors() const
{
CppSQLite3Query q = m_database.execQuery(
"SELECT message, fatal, file_path, line_number, column_number FROM error;"
);
std::vector<StorageError> errors;
while (!q.eof())
{
const std::string message = q.getStringField(0, "");
const bool fatal = q.getIntField(1, 0);
const std::string filePath = q.getStringField(2, "");
const uint lineNumber = q.getIntField(3, 0);
const uint columnNumber = q.getIntField(4, 0);
errors.push_back(StorageError(message, fatal, filePath, lineNumber, columnNumber));
q.nextRow();
}
return errors;
return getAll<StorageCommentLocation>("WHERE file_node_id == " + std::to_string(fileNodeId));
}
std::vector<StorageError> SqliteStorage::getFatalErrors() const
{
CppSQLite3Query q = m_database.execQuery(
"SELECT message, fatal, file_path, line_number, column_number FROM error WHERE fatal == 1;"
);
return getAll<StorageError>("WHERE fatal == 1");
}
std::vector<StorageError> errors;
while (!q.eof())
{
const std::string message = q.getStringField(0, "");
const bool fatal = q.getIntField(1, 0);
const std::string filePath = q.getStringField(2, "");
const uint lineNumber = q.getIntField(3, 0);
const uint columnNumber = q.getIntField(4, 0);
std::vector<StorageFile> SqliteStorage::getAllFiles() const
{
return getAll<StorageFile>("");
}
errors.push_back(StorageError(message, fatal, filePath, lineNumber, columnNumber));
std::vector<StorageNode> SqliteStorage::getAllNodes() const
{
return getAll<StorageNode>("");
}
q.nextRow();
}
std::vector<StorageEdge> SqliteStorage::getAllEdges() const
{
return getAll<StorageEdge>("");
}
return errors;
std::vector<StorageLocalSymbol> SqliteStorage::getAllLocalSymbols() const
{
return getAll<StorageLocalSymbol>("");
}
std::vector<StorageSourceLocation> SqliteStorage::getAllSourceLocations() const
{
return getAll<StorageSourceLocation>("");
}
std::vector<StorageComponentAccess> SqliteStorage::getAllComponentAccesses() const
{
return getAll<StorageComponentAccess>("");
}
std::vector<StorageCommentLocation> SqliteStorage::getAllCommentLocations() const
{
return getAll<StorageCommentLocation>("");
}
std::vector<StorageError> SqliteStorage::getAllErrors() const
{
return getAll<StorageError>("");
}
int SqliteStorage::getNodeCount() const
@@ -895,7 +794,6 @@ void SqliteStorage::setupTables()
{
m_database.execDML(
"CREATE VIRTUAL TABLE IF NOT EXISTS file USING fts4("
//"CREATE TABLE IF NOT EXISTS file ("
"id INTEGER NOT NULL, "
"path TEXT, "
"modification_time TEXT, "
@@ -907,7 +805,7 @@ void SqliteStorage::setupTables()
}
catch (CppSQLite3Exception& e)
{
std::cerr << e.errorCode() << ":" << e.errorMessage() << std::endl;
LOG_ERROR(std::to_string(e.errorCode()) + ": " + e.errorMessage());
}
m_database.execDML(
@@ -1010,26 +908,15 @@ void SqliteStorage::insertOrUpdateMetaValue(const std::string& key, const std::s
).c_str());
}
StorageFile SqliteStorage::getFirstFile(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery(query.c_str());
if (!q.eof())
{
const Id id = q.getIntField(0, 0);
const std::string serializedName = q.getStringField(1, "");
const std::string filePath = q.getStringField(2, "");
const std::string modificationTime = q.getStringField(3, "");
if (id != 0)
{
return StorageFile(id, serializedName, filePath, modificationTime);
}
}
return StorageFile(0, "", "", "");
}
std::vector<StorageFile> SqliteStorage::getAllFiles(const std::string& query) const
template <>
std::vector<StorageFile> SqliteStorage::getAll<StorageFile>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT file.id, node.serialized_name, file.path, file.modification_time FROM file "
@@ -1054,33 +941,8 @@ std::vector<StorageFile> SqliteStorage::getAllFiles(const std::string& query) co
return files;
}
StorageSourceLocation SqliteStorage::getFirstSourceLocation(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery(query.c_str());
if (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id elementId = q.getIntField(1, 0);
const Id fileNodeId = q.getIntField(2, 0);
const int startLineNumber = q.getIntField(3, -1);
const int startColNumber = q.getIntField(4, -1);
const int endLineNumber = q.getIntField(5, -1);
const int endColNumber = q.getIntField(6, -1);
const int type = q.getIntField(7, -1);
if (id != 0 && elementId != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && endColNumber != -1 && type != -1)
{
return StorageSourceLocation(
id, elementId, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber, type
);
}
}
return StorageSourceLocation(0, 0, 0, -1, -1, -1, -1, -1);
}
std::vector<StorageEdge> SqliteStorage::getAllEdges(const std::string& query) const
template <>
std::vector<StorageEdge> SqliteStorage::getAll<StorageEdge>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT id, type, source_node_id, target_node_id FROM edge " + query + ";"
@@ -1104,7 +966,8 @@ std::vector<StorageEdge> SqliteStorage::getAllEdges(const std::string& query) co
return edges;
}
std::vector<StorageNode> SqliteStorage::getAllNodes(const std::string& query) const
template <>
std::vector<StorageNode> SqliteStorage::getAll<StorageNode>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT id, type, serialized_name, definition_type FROM node " + query + ";"
@@ -1128,12 +991,138 @@ std::vector<StorageNode> SqliteStorage::getAllNodes(const std::string& query) co
return nodes;
}
StorageNode SqliteStorage::getFirstNode(const std::string& query) const
template <>
std::vector<StorageLocalSymbol> SqliteStorage::getAll<StorageLocalSymbol>(const std::string& query) const
{
std::vector<StorageNode> nodes = getAllNodes(query + " LIMIT 1");
if (nodes.size() > 0)
CppSQLite3Query q = m_database.execQuery((
"SELECT id, name FROM local_symbol " + query + ";"
).c_str());
std::vector<StorageLocalSymbol> localSymbols;
while (!q.eof())
{
return nodes[0];
const Id id = q.getIntField(0, 0);
const std::string name = q.getStringField(1, "");
if (id != 0)
{
localSymbols.push_back(StorageLocalSymbol(id, name));
}
q.nextRow();
}
return StorageNode();
return localSymbols;
}
template <>
std::vector<StorageSourceLocation> SqliteStorage::getAll<StorageSourceLocation>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT id, element_id, file_node_id, start_line, start_column, end_line, end_column, type FROM source_location " + query + ";"
).c_str());
std::vector<StorageSourceLocation> sourceLocations;
while (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id elementId = q.getIntField(1, 0);
const Id fileNodeId = q.getIntField(2, 0);
const int startLineNumber = q.getIntField(3, -1);
const int startColNumber = q.getIntField(4, -1);
const int endLineNumber = q.getIntField(5, -1);
const int endColNumber = q.getIntField(6, -1);
const int type = q.getIntField(7, -1);
if (id != 0 && elementId != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && endColNumber != -1 && type != -1)
{
sourceLocations.push_back(StorageSourceLocation(id, elementId, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber, type));
}
q.nextRow();
}
return sourceLocations;
}
template <>
std::vector<StorageComponentAccess> SqliteStorage::getAll<StorageComponentAccess>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT id, edge_id, type FROM component_access " + query + ";"
).c_str());
std::vector<StorageComponentAccess> componentAccesses;
while (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id edgeId = q.getIntField(1, 0);
const int type = q.getIntField(2, -1);
if (id != 0 && edgeId != 0 && type != -1)
{
componentAccesses.push_back(StorageComponentAccess(edgeId, type));
}
q.nextRow();
}
return componentAccesses;
}
template <>
std::vector<StorageCommentLocation> SqliteStorage::getAll<StorageCommentLocation>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT id, file_node_id, start_line, start_column, end_line, end_column FROM comment_location " + query + ";"
).c_str());
std::vector<StorageCommentLocation> commentLocations;
while (!q.eof())
{
const Id id = q.getIntField(0, 0);
const Id fileNodeId = q.getIntField(1, 0);
const int startLineNumber = q.getIntField(2, -1);
const int startColNumber = q.getIntField(3, -1);
const int endLineNumber = q.getIntField(4, -1);
const int endColNumber = q.getIntField(5, -1);
if (id != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && endColNumber != -1)
{
commentLocations.push_back(StorageCommentLocation(
id, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber
));
}
q.nextRow();
}
return commentLocations;
}
template <>
std::vector<StorageError> SqliteStorage::getAll<StorageError>(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT message, fatal, file_path, line_number, column_number FROM error " + query + ";"
).c_str());
std::vector<StorageError> errors;
while (!q.eof())
{
const std::string message = q.getStringField(0, "");
const bool fatal = q.getIntField(1, 0);
const std::string filePath = q.getStringField(2, "");
const uint lineNumber = q.getIntField(3, -1);
const uint columnNumber = q.getIntField(4, -1);
if (lineNumber != -1 && columnNumber != -1)
{
errors.push_back(StorageError(message, fatal, filePath, lineNumber, columnNumber));
}
q.nextRow();
}
return errors;
}
+36 -20
View File
@@ -42,9 +42,7 @@ public:
Id addFile(const std::string& serializedName, const std::string& filePath, const std::string& modificationTime);
Id addLocalSymbol(const std::string& name);
Id addSourceLocation(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type);
Id addComponentAccess(Id memberEdgeId, int type);
Id addCommentLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol);
Id addError(const std::string& message, bool fatal, const std::string& filePath, uint lineNumber, uint columnNumber);
@@ -54,9 +52,6 @@ public:
void removeErrorsInFiles(const std::vector<FilePath>& filePaths);
StorageNode getFirstNode() const;
std::vector<StorageNode> getAllNodes() const;
bool isEdge(Id elementId) const;
bool isNode(Id elementId) const;
bool isFile(Id elementId) const;
@@ -86,7 +81,6 @@ public:
std::vector<Id> getAllFileIds() const;
std::vector<StorageFile> getFilesByPaths(const std::vector<FilePath>& filePaths) const;
std::vector<StorageFile> getAllFiles() const;
std::shared_ptr<TextAccess> getFileContentByPath(const std::string& filePath) const;
void setNodeType(int type, Id nodeId);
@@ -106,9 +100,17 @@ public:
void optimizeFTSTable() const;
std::vector<StorageCommentLocation> getCommentLocationsInFile(const FilePath& filePath) const;
std::vector<StorageError> getAllErrors() const;
std::vector<StorageError> getFatalErrors() const;
std::vector<StorageFile> getAllFiles() const;
std::vector<StorageNode> getAllNodes() const;
std::vector<StorageEdge> getAllEdges() const;
std::vector<StorageLocalSymbol> getAllLocalSymbols() const;
std::vector<StorageSourceLocation> getAllSourceLocations() const;
std::vector<StorageComponentAccess> getAllComponentAccesses() const;
std::vector<StorageCommentLocation> getAllCommentLocations() const;
std::vector<StorageError> getAllErrors() const;
int getNodeCount() const;
int getEdgeCount() const;
int getFileCount() const;
@@ -124,26 +126,40 @@ private:
std::string getMetaValue(const std::string& key) const;
void insertOrUpdateMetaValue(const std::string& key, const std::string& value);
StorageFile getFirstFile(const std::string& query) const;
std::vector<StorageFile> getAllFiles(const std::string& query) const;
StorageSourceLocation getFirstSourceLocation(const std::string& query) const;
template <typename ResultType>
std::vector<ResultType> getAll(const std::string& query) const;
std::vector<StorageEdge> getAllEdges(const std::string& query) const;
std::vector<StorageNode> getAllNodes(const std::string& query) const;
StorageNode getFirstNode(const std::string& query) const;
template <>
std::vector<StorageFile> getAll<StorageFile>(const std::string& query) const;
template <>
std::vector<StorageEdge> getAll<StorageEdge>(const std::string& query) const;
template <>
std::vector<StorageNode> getAll<StorageNode>(const std::string& query) const;
template <>
std::vector<StorageLocalSymbol> getAll<StorageLocalSymbol>(const std::string& query) const;
template <>
std::vector<StorageSourceLocation> getAll<StorageSourceLocation>(const std::string& query) const;
template <>
std::vector<StorageComponentAccess> getAll<StorageComponentAccess>(const std::string& query) const;
template <>
std::vector<StorageCommentLocation> getAll<StorageCommentLocation>(const std::string& query) const;
template <>
std::vector<StorageError> getAll<StorageError>(const std::string& query) const;
template <typename ResultType>
ResultType getFirstResult(const std::string& query) const;
ResultType getFirst(const std::string& query) const
{
std::vector<ResultType> results = getAll<ResultType>(query + " LIMIT 1");
if (results.size() > 0)
{
return results[0];
}
return ResultType();
}
mutable CppSQLite3DB m_database;
FilePath m_dbFilePath;
};
template <typename ResultType>
ResultType SqliteStorage::getFirstResult(const std::string& query) const
{
CppSQLite3Query q = m_database.execQuery(query.c_str());
return q.getIntField(0, 0);
}
#endif // SQLITE_STORAGE_H
+146 -1111
View File
File diff suppressed because it is too large Load Diff
+26 -114
View File
@@ -1,128 +1,40 @@
#ifndef STORAGE_H
#define STORAGE_H
#include <memory>
#include <vector>
#include <functional>
#include <string>
#include "utility/file/FilePath.h"
#include "data/name/NameHierarchy.h"
#include "data/StorageTypes.h"
#include "utility/types.h"
#include "data/access/StorageAccess.h"
#include "data/graph/token_component/TokenComponentAccess.h"
#include "data/location/TokenLocationCollection.h"
#include "data/parser/ParserClient.h"
#include "data/parser/ParseLocation.h"
#include "data/search/SearchIndex.h"
#include "data/HierarchyCache.h"
#include "data/SqliteStorage.h"
#include "data/parser/ParserClientImpl.h"
class Storage: public StorageAccess
class Storage
{
public:
Storage(const FilePath& dbPath);
Storage();
virtual ~Storage();
FilePath getDbFilePath() const;
Version getVersion() const;
virtual Id addFile(const std::string& name, const std::string& filePath, const std::string& modificationTime) = 0;
virtual Id addNode(int type, const std::string& serializedName, int definitionType) = 0;
virtual Id addEdge(int type, Id sourceId, Id targetId) = 0;
virtual Id addLocalSymbol(const std::string& name) = 0;
virtual void addSourceLocation(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type) = 0;
virtual void addComponentAccess(Id edgeId , int type) = 0;
virtual void addCommentLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol) = 0;
virtual void addError(const std::string& message, bool fatal, const std::string& filePath, uint startLine, uint startCol) = 0;
void init();
void clear();
void clearCaches();
virtual void forEachFile(std::function<void(const Id /*id*/, const StorageFile& /*data*/)> callback) const = 0;
virtual void forEachNode(std::function<void(const Id /*id*/, const StorageNode& /*data*/)> callback) const = 0;
virtual void forEachEdge(std::function<void(const Id /*id*/, const StorageEdge& /*data*/)> callback) const = 0;
virtual void forEachLocalSymbol(std::function<void(const Id /*id*/, const StorageLocalSymbol& /*data*/)> callback) const = 0;
virtual void forEachSourceLocation(std::function<void(const StorageSourceLocation& /*data*/)> callback) const = 0;
virtual void forEachComponentAccess(std::function<void(const StorageComponentAccess& /*data*/)> callback) const = 0;
virtual void forEachCommentLocation(std::function<void(const StorageCommentLocation& /*data*/)> callback) const = 0;
virtual void forEachError(std::function<void(const StorageError& /*data*/)> callback) const = 0;
std::set<FilePath> getDependingFilePaths(const std::set<FilePath>& filePaths);
std::set<FilePath> getDependingFilePaths(const FilePath& filePath);
void clearFileElements(const std::vector<FilePath>& filePaths);
void removeUnusedNames();
std::vector<FileInfo> getInfoOnAllFiles() const;
void logStats() const;
void startParsing();
void finishParsing();
void injectData(std::shared_ptr<IntermediateStorage> injectedStorage);
// StorageAccess implementation
virtual Id getIdForNodeWithNameHierarchy(const NameHierarchy& nameHierarchy) const;
virtual Id getIdForEdge(
Edge::EdgeType type, const NameHierarchy& fromNameHierarchy, const NameHierarchy& toNameHierarchy) const;
virtual Id getIdForFirstNode() const;
virtual NameHierarchy getNameHierarchyForNodeWithId(Id nodeId) const;
virtual Node::NodeType getNodeTypeForNodeWithId(Id nodeId) const;
virtual std::shared_ptr<TokenLocationCollection> getFullTextSearchLocations(const std::string& searchTerm) const;
virtual std::vector<SearchMatch> getAutocompletionMatches(const std::string& query) const;
virtual std::vector<SearchMatch> getSearchMatchesForTokenIds(const std::vector<Id>& elementIds) const;
virtual std::shared_ptr<Graph> getGraphForAll() const;
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId, Id* declarationId) const;
virtual std::vector<Id> getNodeIdsForLocationIds(const std::vector<Id>& locationIds) const;
virtual std::vector<Id> getLocalSymbolIdsForLocationIds(const std::vector<Id>& locationIds) const;
virtual std::vector<Id> getTokenIdsForMatches(const std::vector<SearchMatch>& matches) const;
virtual Id getTokenIdForFileNode(const FilePath& filePath) const;
virtual std::vector<Id> getTokenIdsForAggregationEdge(Id sourceId, Id targetId) const;
virtual std::shared_ptr<TokenLocationCollection> getTokenLocationsForTokenIds(
const std::vector<Id>& tokenIds
) const;
virtual std::shared_ptr<TokenLocationCollection> getTokenLocationsForLocationIds(
const std::vector<Id>& locationIds
) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForFile(const std::string& filePath) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationsForLinesInFile(
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
) const;
virtual TokenLocationCollection getErrorTokenLocations(std::vector<ErrorInfo>* errors) const;
virtual std::shared_ptr<TokenLocationFile> getCommentLocationsInFile(const FilePath& filePath) const;
virtual std::shared_ptr<TextAccess> getFileContent(const FilePath& filePath) const;
virtual FileInfo getFileInfoForFilePath(const FilePath& filePath) const;
virtual std::vector<FileInfo> getFileInfosForFilePaths(const std::vector<FilePath>& filePaths) const;
virtual ErrorCountInfo getErrorCount() const;
virtual StorageStats getStorageStats() const;
private:
Id getFileNodeId(const FilePath& filePath) const;
FilePath getFileNodePath(Id fileId) const;
Id getLastVisibleParentNodeId(const Id nodeId) const;
std::vector<Id> getAllChildNodeIds(const Id nodeId) const;
void addNodesToGraph(const std::vector<Id>& nodeIds, Graph* graph) const;
void addEdgesToGraph(const std::vector<Id>& edgeIds, Graph* graph) const;
void addNodesWithChildrenAndEdgesToGraph(
const std::vector<Id>& nodeIds,
const std::vector<Id>& edgeIds, Graph* graph
) const;
void addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const;
void addComponentAccessToGraph(Graph* graph) const;
void buildSearchIndex();
void buildHierarchyCache();
void optimizeFTSTable();
void log(std::string type, std::string str, const ParseLocation& location) const;
SearchIndex m_commandIndex;
SearchIndex m_elementIndex;
SqliteStorage m_sqliteStorage;
mutable std::map <FilePath, Id> m_fileNodeIds;
HierarchyCache m_hierarchyCache;
virtual void startInjection();
virtual void finishInjection();
void inject(Storage* injected);
};
#endif // STORAGE_H
+52
View File
@@ -9,6 +9,13 @@
struct StorageEdge
{
StorageEdge()
: id(0)
, type(0)
, sourceNodeId(0)
, targetNodeId(0)
{}
StorageEdge(Id id, int type, Id sourceNodeId, Id targetNodeId)
: id(id)
, type(type)
@@ -45,6 +52,13 @@ struct StorageNode
struct StorageFile
{
StorageFile()
: id(0)
, name("")
, filePath("")
, modificationTime("")
{}
StorageFile(Id id, const std::string& name, const std::string& filePath, const std::string& modificationTime)
: id(id)
, name(name)
@@ -60,6 +74,11 @@ struct StorageFile
struct StorageLocalSymbol
{
StorageLocalSymbol()
: id(0)
, name("")
{}
StorageLocalSymbol(Id id, const std::string& name)
: id(id)
, name(name)
@@ -71,6 +90,17 @@ struct StorageLocalSymbol
struct StorageSourceLocation
{
StorageSourceLocation()
: id(0)
, elementId(0)
, fileNodeId(0)
, startLine(-1)
, startCol(-1)
, endLine(-1)
, endCol(-1)
, type(0)
{}
StorageSourceLocation(Id id, Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type)
: id(id)
, elementId(elementId)
@@ -94,6 +124,11 @@ struct StorageSourceLocation
struct StorageComponentAccess
{
StorageComponentAccess()
: memberEdgeId(0)
, type(0)
{}
StorageComponentAccess(Id memberEdgeId, int type)
: memberEdgeId(memberEdgeId)
, type(type)
@@ -105,6 +140,15 @@ struct StorageComponentAccess
struct StorageCommentLocation
{
StorageCommentLocation()
: id(0)
, fileNodeId(0)
, startLine(-1)
, startCol(-1)
, endLine(-1)
, endCol(-1)
{}
StorageCommentLocation(Id id, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol)
: id(id)
, fileNodeId(fileNodeId)
@@ -124,6 +168,14 @@ struct StorageCommentLocation
struct StorageError
{
StorageError()
: message("")
, fatal(0)
, filePath("")
, lineNumber(-1)
, columnNumber(-1)
{}
StorageError(const std::string& message, bool fatal, const std::string& filePath, uint lineNumber, uint columnNumber)
: message(message)
, fatal(fatal)
+2 -2
View File
@@ -1,10 +1,10 @@
#include "data/TaskCleanStorage.h"
#include "data/Storage.h"
#include "data/PersistentStorage.h"
#include "utility/messaging/type/MessageStatus.h"
#include "utility/utility.h"
TaskCleanStorage::TaskCleanStorage(Storage* storage, const std::vector<FilePath>& filePaths)
TaskCleanStorage::TaskCleanStorage(PersistentStorage* storage, const std::vector<FilePath>& filePaths)
: m_storage(storage)
, m_filePaths(filePaths)
, m_fileCount(filePaths.size())
+3 -3
View File
@@ -7,14 +7,14 @@
#include "utility/scheduling/Task.h"
#include "utility/TimePoint.h"
class Storage;
class PersistentStorage;
class TaskCleanStorage
: public Task
{
public:
TaskCleanStorage(
Storage* storage,
PersistentStorage* storage,
const std::vector<FilePath>& filePaths
);
@@ -26,7 +26,7 @@ public:
virtual void revert();
private:
Storage* m_storage;
PersistentStorage* m_storage;
std::vector<FilePath> m_filePaths;
const size_t m_fileCount;
-2
View File
@@ -32,8 +32,6 @@ public:
virtual Id getIdForEdge(
Edge::EdgeType type, const NameHierarchy& fromNameHierarchy, const NameHierarchy& toNameHierarchy) const = 0;
virtual Id getIdForFirstNode() const = 0;
virtual NameHierarchy getNameHierarchyForNodeWithId(Id id) const = 0;
virtual Node::NodeType getNodeTypeForNodeWithId(Id id) const = 0;
@@ -54,16 +54,6 @@ Id StorageAccessProxy::getIdForEdge(
return 0;
}
Id StorageAccessProxy::getIdForFirstNode() const
{
if (hasSubject())
{
return m_subject->getIdForFirstNode();
}
return 0;
}
Node::NodeType StorageAccessProxy::getNodeTypeForNodeWithId(Id id) const
{
if (hasSubject())
-2
View File
@@ -17,8 +17,6 @@ public:
virtual Id getIdForEdge(
Edge::EdgeType type, const NameHierarchy& fromNameHierarchy, const NameHierarchy& toNameHierarchy) const;
virtual Id getIdForFirstNode() const;
virtual NameHierarchy getNameHierarchyForNodeWithId(Id id) const;
virtual Node::NodeType getNodeTypeForNodeWithId(Id id) const;
@@ -9,7 +9,7 @@ class TokenComponentAccess
: public TokenComponent
{
public:
enum AccessType : int
enum AccessType : int // todo: use normal numbers here. not 2^x
{
ACCESS_PUBLIC = 0x1,
ACCESS_PROTECTED = 0x2,
-3
View File
@@ -39,9 +39,6 @@ public:
ParserClient();
virtual ~ParserClient();
virtual void startParsing() = 0;
virtual void finishParsing() = 0;
virtual void startParsingFile(const FilePath& filePath) = 0;
virtual void finishParsingFile(const FilePath& filePath) = 0;
+37 -15
View File
@@ -25,16 +25,9 @@ void ParserClientImpl::resetStorage()
m_storage.reset();
}
void ParserClientImpl::startParsing()
{
}
void ParserClientImpl::finishParsing()
{
}
void ParserClientImpl::startParsingFile(const FilePath& filePath)
{
m_nodeIdsToMemberEdgeIds.clear();
}
void ParserClientImpl::finishParsingFile(const FilePath& filePath)
@@ -473,7 +466,7 @@ Id ParserClientImpl::addFile(const std::string& filePath)
return 0;
}
return m_storage->addFile(filePath);
return m_storage->addFile("", filePath, "");
}
Id ParserClientImpl::addNode(Node::NodeType nodeType, NameHierarchy nameHierarchy, DefinitionType definitionType)
@@ -483,7 +476,7 @@ Id ParserClientImpl::addNode(Node::NodeType nodeType, NameHierarchy nameHierarch
return 0;
}
return m_storage->addNode(Node::typeToInt(nodeType), nameHierarchy, definitionTypeToInt(definitionType));
return m_storage->addNode(Node::typeToInt(nodeType), NameHierarchy::serialize(nameHierarchy), definitionTypeToInt(definitionType));
}
Id ParserClientImpl::addEdge(int type, Id sourceId, Id targetId)
@@ -498,7 +491,14 @@ Id ParserClientImpl::addEdge(int type, Id sourceId, Id targetId)
return 0;
}
return m_storage->addEdge(type, sourceId, targetId);
Id edgeId = m_storage->addEdge(type, sourceId, targetId);
if (type == Edge::EDGE_MEMBER)
{
m_nodeIdsToMemberEdgeIds[targetId] = edgeId;
}
return edgeId;
}
Id ParserClientImpl::addLocalSymbol(const std::string& name)
@@ -529,7 +529,15 @@ void ParserClientImpl::addSourceLocation(Id elementId, const ParseLocation& loca
return;
}
m_storage->addSourceLocation(elementId, location, type);
m_storage->addSourceLocation(
elementId,
addFile(location.filePath.str()),
location.startLineNumber,
location.startColumnNumber,
location.endLineNumber,
location.endColumnNumber,
type
);
}
void ParserClientImpl::addComponentAccess(Id nodeId , int type)
@@ -539,7 +547,15 @@ void ParserClientImpl::addComponentAccess(Id nodeId , int type)
return;
}
m_storage->addComponentAccess(nodeId, type);
std::unordered_map<Id, Id>::const_iterator it = m_nodeIdsToMemberEdgeIds.find(nodeId);
if (it != m_nodeIdsToMemberEdgeIds.end())
{
m_storage->addComponentAccess(it->second, type);
}
else
{
LOG_ERROR_STREAM(<< "Cannot assign access" << type << " to node id " << nodeId << " because it's not a child node.");
}
}
void ParserClientImpl::addCommentLocation(const ParseLocation& location)
@@ -549,7 +565,13 @@ void ParserClientImpl::addCommentLocation(const ParseLocation& location)
return;
}
m_storage->addCommentLocation(location);
m_storage->addCommentLocation(
addFile(location.filePath.str()),
location.startLineNumber,
location.startColumnNumber,
location.endLineNumber,
location.endColumnNumber
);
}
void ParserClientImpl::addError(const std::string& message, bool fatal, const ParseLocation& location)
@@ -559,7 +581,7 @@ void ParserClientImpl::addError(const std::string& message, bool fatal, const Pa
return;
}
m_storage->addError(message, fatal, location);
m_storage->addError(message, fatal, location.filePath.str(), location.startLineNumber, location.startColumnNumber);
}
void ParserClientImpl::log(std::string type, std::string str, const ParseLocation& location) const
+1 -3
View File
@@ -18,9 +18,6 @@ public:
void setStorage(std::shared_ptr<IntermediateStorage> storage);
void resetStorage();
virtual void startParsing();
virtual void finishParsing();
virtual void startParsingFile(const FilePath& filePath);
virtual void finishParsingFile(const FilePath& filePath);
@@ -105,6 +102,7 @@ private:
void log(std::string type, std::string str, const ParseLocation& location) const;
std::shared_ptr<IntermediateStorage> m_storage;
std::unordered_map<Id, Id> m_nodeIdsToMemberEdgeIds;
};
#endif // PARSER_CLIENT_IMPL_H
+3 -3
View File
@@ -9,7 +9,7 @@
#include "utility/scheduling/Task.h"
#include "utility/TimePoint.h"
class Storage;
class PersistentStorage;
class FileManager;
class CxxParser;
@@ -26,7 +26,7 @@ class TaskParseCxx
{
public:
TaskParseCxx(
Storage* storage,
PersistentStorage* storage,
const FileManager* fileManager,
const Parser::Arguments& arguments,
const std::vector<FilePath>& files
@@ -42,7 +42,7 @@ public:
virtual void revert();
private:
Storage* m_storage;
PersistentStorage* m_storage;
std::shared_ptr<CxxParser> m_parser;
std::shared_ptr<ParserClientImpl> m_parserClient;
const Parser::Arguments m_arguments;
-2
View File
@@ -3,8 +3,6 @@ add_files(
helper/TestFileManager.cpp
helper/TestFileManager.h
helper/TestStorage.cpp
helper/TestStorage.h
TestSuiteFixture.cpp
TestSuiteFixture.h
+9 -9
View File
@@ -7,7 +7,7 @@
#include "data/graph/token_component/TokenComponentStatic.h"
#include "data/location/TokenLocation.h"
#include "data/parser/ParseLocation.h"
#include "data/Storage.h"
#include "data/PersistentStorage.h"
#include "data/type/DataType.h"
#include "data/type/NamedDataType.h"
@@ -26,7 +26,7 @@ public:
std::shared_ptr<IntermediateStorage> intermetiateStorage = std::make_shared<IntermediateStorage>();
Id id = intermetiateStorage->addFile("test.h", "path/to/test.h", "someTime");
storage.injectData(intermetiateStorage);
storage.inject(intermetiateStorage.get());
TS_ASSERT_EQUALS(storage.getNameHierarchyForNodeWithId(id).getQualifiedNameWithSignature(), "test.h");
TS_ASSERT_EQUALS(storage.getNodeTypeForNodeWithId(id), Node::NODE_FILE);
@@ -39,9 +39,9 @@ public:
TestStorage storage;
std::shared_ptr<IntermediateStorage> intermetiateStorage = std::make_shared<IntermediateStorage>();
Id id = intermetiateStorage->addNode(Node::typeToInt(Node::NODE_TYPEDEF), a, true);
Id id = intermetiateStorage->addNode(Node::typeToInt(Node::NODE_TYPEDEF), NameHierarchy::serialize(a), true);
storage.injectData(intermetiateStorage);
storage.inject(intermetiateStorage.get());
Id storedId = storage.getIdForNodeWithNameHierarchy(a);
@@ -58,11 +58,11 @@ public:
TestStorage storage;
std::shared_ptr<IntermediateStorage> intermetiateStorage = std::make_shared<IntermediateStorage>();
Id aId = intermetiateStorage->addNode(Node::typeToInt(Node::NODE_STRUCT), a, true);
Id bId = intermetiateStorage->addNode(Node::typeToInt(Node::NODE_FIELD), b, true);
Id aId = intermetiateStorage->addNode(Node::typeToInt(Node::NODE_STRUCT), NameHierarchy::serialize(a), true);
Id bId = intermetiateStorage->addNode(Node::typeToInt(Node::NODE_FIELD), NameHierarchy::serialize(b), true);
intermetiateStorage->addEdge(Edge::typeToInt(Edge::EDGE_MEMBER), aId, bId);
storage.injectData(intermetiateStorage);
storage.inject(intermetiateStorage.get());
TS_ASSERT(storage.getIdForEdge(Edge::EDGE_MEMBER, a, b) != 0);
}
@@ -224,11 +224,11 @@ public:
private:
class TestStorage
: public Storage
: public PersistentStorage
{
public:
TestStorage()
: Storage("data/test.sqlite")
: PersistentStorage("data/test.sqlite")
{
clear();
}
-6
View File
@@ -1,6 +0,0 @@
#include "TestStorage.h"
TestStorage::TestStorage()
: Storage("data/test.sqlite")
{
}
-14
View File
@@ -1,14 +0,0 @@
#ifndef TEST_STORAGE_H
#define TEST_STORAGE_H
#include "data/Storage.h"
class TestStorage
: public Storage
{
public:
TestStorage();
void parseCxxCode(std::string code);
};
#endif // TEST_STORAGE_H
+2 -2
View File
@@ -1,10 +1,10 @@
#include "data/parser/cxx/TaskParseCxx.h"
#include "data/Storage.h"
#include "data/PersistentStorage.h"
#include "utility/messaging/type/MessageFinishedParsing.h"
TaskParseCxx::TaskParseCxx(
Storage* storage,
PersistentStorage* storage,
const FileManager* fileManager,
const Parser::Arguments& arguments,
const std::vector<FilePath>& files