From c4400c0afcdc64e014ebe108904adc1c1bc86bbd Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Thu, 7 Sep 2017 12:01:46 +0200 Subject: [PATCH] logic: reorder Java child nodes to source location order --- src/lib/data/storage/PersistentStorage.cpp | 98 +++++++++++++++++++++- src/lib/data/storage/PersistentStorage.h | 6 +- 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src/lib/data/storage/PersistentStorage.cpp b/src/lib/data/storage/PersistentStorage.cpp index 8c661ddc..07cb043f 100644 --- a/src/lib/data/storage/PersistentStorage.cpp +++ b/src/lib/data/storage/PersistentStorage.cpp @@ -402,6 +402,7 @@ void PersistentStorage::buildCaches() buildFilePathMaps(); buildSearchIndex(); + buildMemberEdgeIdOrderMap(); buildHierarchyCache(); } @@ -2195,7 +2196,18 @@ void PersistentStorage::addEdgesToGraph(const std::vector& newEdgeIds, Graph if (sourceNode && targetNode) { - graph->createEdge(storageEdge.id, Edge::intToType(storageEdge.type), sourceNode, targetNode); + Edge::EdgeType type = Edge::intToType(storageEdge.type); + Id edgeId = storageEdge.id; + if (type & Edge::EDGE_MEMBER && m_memberEdgeIdOrderMap.size()) + { + auto it = m_memberEdgeIdOrderMap.find(edgeId); + if (it != m_memberEdgeIdOrderMap.end()) + { + edgeId = it->second; + } + } + + graph->createEdge(edgeId, type, sourceNode, targetNode); } else { @@ -2443,9 +2455,16 @@ void PersistentStorage::buildFilePathMaps() for (StorageFile& file: m_sqliteIndexStorage.getAll()) { - m_fileNodeIds.emplace(FilePath(file.filePath), file.id); - m_fileNodePaths.emplace(file.id, FilePath(file.filePath)); + FilePath path = FilePath(file.filePath); + + m_fileNodeIds.emplace(path, file.id); + m_fileNodePaths.emplace(file.id, path); m_fileNodeComplete.emplace(file.id, file.complete); + + if (!m_hasJavaFiles && path.extension() == ".java") + { + m_hasJavaFiles = true; + } } for (StorageSymbol& symbol : m_sqliteIndexStorage.getAll()) @@ -2514,6 +2533,79 @@ void PersistentStorage::buildFullTextSearchIndex() const } } +void PersistentStorage::buildMemberEdgeIdOrderMap() +{ + TRACE(); + + if (!m_hasJavaFiles) + { + return; + } + + std::vector childNodeIds; + std::unordered_map childIdToMemberEdgeIdMap; + + for (const StorageEdge& edge : m_sqliteIndexStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_MEMBER))) + { + childNodeIds.push_back(edge.targetNodeId); + childIdToMemberEdgeIdMap.emplace(edge.targetNodeId, edge.id); + } + + std::vector locationIds; + std::unordered_map locationIdToElementIdMap; + for (const StorageOccurrence& occurrence: m_sqliteIndexStorage.getOccurrencesForElementIds(childNodeIds)) + { + locationIds.push_back(occurrence.sourceLocationId); + locationIdToElementIdMap.emplace(occurrence.sourceLocationId, occurrence.elementId); + } + + SourceLocationCollection collection; + for (const StorageSourceLocation& location: m_sqliteIndexStorage.getAllByIds(locationIds)) + { + LocationType locType = intToLocationType(location.type); + if (locType != LOCATION_TOKEN) + { + continue; + } + + FilePath path(m_fileNodePaths[location.fileNodeId]); + if (path.extension() == ".java") + { + collection.addSourceLocation( + intToLocationType(location.type), + location.id, + std::vector(), + FilePath(std::to_string(location.fileNodeId)), + location.startLine, + location.startCol, + location.endLine, + location.endCol + ); + } + } + + // Set first 3 bits to 1 to avoid collisions + Id baseId = ~(~Id(0) >> 3) + 1; + + collection.forEachSourceLocation( + [&](SourceLocation* location) + { + auto it = locationIdToElementIdMap.find(location->getLocationId()); + if (it != locationIdToElementIdMap.end()) + { + auto it2 = childIdToMemberEdgeIdMap.find(it->second); + if (it2 != childIdToMemberEdgeIdMap.end()) + { + if (m_memberEdgeIdOrderMap.emplace(it2->second, baseId).second) + { + baseId++; + } + } + } + } + ); +} + void PersistentStorage::buildHierarchyCache() { TRACE(); diff --git a/src/lib/data/storage/PersistentStorage.h b/src/lib/data/storage/PersistentStorage.h index 7a7e42e1..41e92fd6 100644 --- a/src/lib/data/storage/PersistentStorage.h +++ b/src/lib/data/storage/PersistentStorage.h @@ -176,9 +176,10 @@ private: void buildFilePathMaps(); void buildSearchIndex(); void buildFullTextSearchIndex() const; + void buildMemberEdgeIdOrderMap(); void buildHierarchyCache(); - size_t m_preInjectionErrorCount; + size_t m_preInjectionErrorCount = 0; SearchIndex m_commandIndex; SearchIndex m_symbolIndex; @@ -194,8 +195,11 @@ private: std::map m_fileNodeComplete; std::map m_symbolDefinitionKinds; + std::map m_memberEdgeIdOrderMap; HierarchyCache m_hierarchyCache; + + bool m_hasJavaFiles = false; }; #endif // PERSISTENT_STORAGE_H