2013-05-25 14:43:14 -04:00
|
|
|
// Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
//
|
|
|
|
// This file is part of YouCompleteMe.
|
|
|
|
//
|
|
|
|
// YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "IdentifierDatabase.h"
|
|
|
|
#include "standard.h"
|
|
|
|
|
|
|
|
#include "Candidate.h"
|
|
|
|
#include "CandidateRepository.h"
|
|
|
|
#include "IdentifierUtils.h"
|
|
|
|
#include "Result.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
#include <boost/thread/locks.hpp>
|
|
|
|
#include <boost/unordered_set.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/cxx11/any_of.hpp>
|
|
|
|
|
|
|
|
using boost::algorithm::any_of;
|
|
|
|
using boost::algorithm::is_upper;
|
|
|
|
|
|
|
|
|
|
|
|
namespace YouCompleteMe {
|
|
|
|
|
|
|
|
IdentifierDatabase::IdentifierDatabase()
|
|
|
|
: candidate_repository_( CandidateRepository::Instance() ) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-26 16:28:00 -04:00
|
|
|
void IdentifierDatabase::AddIdentifiers(
|
2013-05-30 01:23:19 -04:00
|
|
|
const FiletypeIdentifierMap &filetype_identifier_map ) {
|
2013-05-26 16:28:00 -04:00
|
|
|
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
|
|
|
|
|
|
|
|
foreach ( const FiletypeIdentifierMap::value_type & filetype_and_map,
|
|
|
|
filetype_identifier_map ) {
|
|
|
|
foreach( const FilepathToIdentifiers::value_type & filepath_and_identifiers,
|
|
|
|
filetype_and_map.second ) {
|
|
|
|
AddIdentifiersNoLock( filepath_and_identifiers.second,
|
|
|
|
filetype_and_map.first,
|
|
|
|
filepath_and_identifiers.first );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IdentifierDatabase::AddIdentifiers(
|
2013-05-25 14:43:14 -04:00
|
|
|
const std::vector< std::string > &new_candidates,
|
|
|
|
const std::string &filetype,
|
|
|
|
const std::string &filepath ) {
|
2013-05-26 16:28:00 -04:00
|
|
|
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
|
|
|
|
AddIdentifiersNoLock( new_candidates, filetype, filepath );
|
2013-05-25 14:43:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IdentifierDatabase::ClearCandidatesStoredForFile(
|
|
|
|
const std::string &filetype,
|
|
|
|
const std::string &filepath ) {
|
2013-05-26 16:28:00 -04:00
|
|
|
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
|
|
|
|
GetCandidateSet( filetype, filepath ).clear();
|
2013-05-25 14:43:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IdentifierDatabase::ResultsForQueryAndType(
|
|
|
|
const std::string &query,
|
|
|
|
const std::string &filetype,
|
|
|
|
std::vector< Result > &results ) const {
|
2013-05-26 16:28:00 -04:00
|
|
|
FiletypeCandidateMap::const_iterator it;
|
2013-05-25 14:43:14 -04:00
|
|
|
{
|
2013-05-26 16:28:00 -04:00
|
|
|
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
|
|
|
|
it = filetype_candidate_map_.find( filetype );
|
2013-05-25 14:43:14 -04:00
|
|
|
|
2013-05-26 16:28:00 -04:00
|
|
|
if ( it == filetype_candidate_map_.end() || query.empty() )
|
2013-05-25 14:43:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Bitset query_bitset = LetterBitsetFromString( query );
|
|
|
|
bool query_has_uppercase_letters = any_of( query, is_upper() );
|
|
|
|
|
|
|
|
boost::unordered_set< const Candidate * > seen_candidates;
|
|
|
|
seen_candidates.reserve( candidate_repository_.NumStoredCandidates() );
|
|
|
|
|
|
|
|
{
|
2013-05-26 16:28:00 -04:00
|
|
|
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
|
2013-05-25 14:43:14 -04:00
|
|
|
foreach ( const FilepathToCandidates::value_type & path_and_candidates,
|
|
|
|
*it->second ) {
|
|
|
|
foreach ( const Candidate * candidate, *path_and_candidates.second ) {
|
|
|
|
if ( ContainsKey( seen_candidates, candidate ) )
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
seen_candidates.insert( candidate );
|
|
|
|
|
|
|
|
if ( !candidate->MatchesQueryBitset( query_bitset ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Result result = candidate->QueryMatchResult(
|
|
|
|
query, query_has_uppercase_letters );
|
|
|
|
|
|
|
|
if ( result.IsSubsequence() )
|
|
|
|
results.push_back( result );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort( results.begin(), results.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-26 16:28:00 -04:00
|
|
|
// WARNING: You need to hold the filetype_candidate_map_mutex_ before calling
|
|
|
|
// this function and while using the returned set.
|
|
|
|
std::set< const Candidate * > &IdentifierDatabase::GetCandidateSet(
|
2013-05-25 14:43:14 -04:00
|
|
|
const std::string &filetype,
|
|
|
|
const std::string &filepath ) {
|
|
|
|
boost::shared_ptr< FilepathToCandidates > &path_to_candidates =
|
2013-05-26 16:28:00 -04:00
|
|
|
filetype_candidate_map_[ filetype ];
|
2013-05-25 14:43:14 -04:00
|
|
|
|
|
|
|
if ( !path_to_candidates )
|
|
|
|
path_to_candidates.reset( new FilepathToCandidates() );
|
|
|
|
|
2013-05-26 16:28:00 -04:00
|
|
|
boost::shared_ptr< std::set< const Candidate * > > &candidates =
|
2013-05-25 14:43:14 -04:00
|
|
|
( *path_to_candidates )[ filepath ];
|
|
|
|
|
|
|
|
if ( !candidates )
|
2013-05-26 16:28:00 -04:00
|
|
|
candidates.reset( new std::set< const Candidate * >() );
|
2013-05-25 14:43:14 -04:00
|
|
|
|
|
|
|
return *candidates;
|
|
|
|
}
|
|
|
|
|
2013-05-26 16:28:00 -04:00
|
|
|
// WARNING: You need to hold the filetype_candidate_map_mutex_ before calling
|
|
|
|
// this function and while using the returned set.
|
|
|
|
void IdentifierDatabase::AddIdentifiersNoLock(
|
|
|
|
const std::vector< std::string > &new_candidates,
|
|
|
|
const std::string &filetype,
|
|
|
|
const std::string &filepath ) {
|
|
|
|
std::set< const Candidate *> &candidates =
|
|
|
|
GetCandidateSet( filetype, filepath );
|
|
|
|
|
|
|
|
std::vector< const Candidate * > repository_candidates =
|
|
|
|
candidate_repository_.GetCandidatesForStrings( new_candidates );
|
|
|
|
|
|
|
|
candidates.insert( repository_candidates.begin(),
|
|
|
|
repository_candidates.end() );
|
|
|
|
}
|
|
|
|
|
2013-05-25 14:43:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace YouCompleteMe
|