mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2024-12-03 02:15:41 +00:00
Implement "locate" action.
This commit is contained in:
parent
cc5bb9bc34
commit
45e5b7bc8d
9 changed files with 278 additions and 2 deletions
|
@ -53,6 +53,7 @@ add_subdirectory(src/main)
|
||||||
add_subdirectory(src/common)
|
add_subdirectory(src/common)
|
||||||
add_subdirectory(src/delete)
|
add_subdirectory(src/delete)
|
||||||
add_subdirectory(src/query)
|
add_subdirectory(src/query)
|
||||||
|
add_subdirectory(src/locate)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
INTERFACE ${PostgreSQL_LIBRARIES}
|
INTERFACE ${PostgreSQL_LIBRARIES}
|
||||||
|
|
|
@ -34,4 +34,3 @@ int main (int parArgc, char* parArgv[]) {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
#include <boost/range/adaptor/map.hpp>
|
#include <boost/range/adaptor/map.hpp>
|
||||||
#include <boost/range/algorithm/copy.hpp>
|
#include <boost/range/algorithm/copy.hpp>
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace din {
|
namespace din {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
21
src/locate/CMakeLists.txt
Normal file
21
src/locate/CMakeLists.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
project(${bare_name}-locate CXX)
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME}
|
||||||
|
main.cpp
|
||||||
|
commandline.cpp
|
||||||
|
postgre_locate.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(${PROJECT_NAME}
|
||||||
|
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME}
|
||||||
|
PRIVATE ${bare_name}-if
|
||||||
|
PRIVATE ${bare_name}-common
|
||||||
|
)
|
||||||
|
|
||||||
|
string(REPLACE "${bare_name}-" "" ACTION_NAME "${PROJECT_NAME}")
|
||||||
|
target_compile_definitions(${PROJECT_NAME}
|
||||||
|
PRIVATE ACTION_NAME="${ACTION_NAME}"
|
||||||
|
)
|
59
src/locate/commandline.cpp
Normal file
59
src/locate/commandline.cpp
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "dindexer".
|
||||||
|
*
|
||||||
|
* "dindexer" 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.
|
||||||
|
*
|
||||||
|
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "commandline.hpp"
|
||||||
|
#include "dindexer-common/commandline.hpp"
|
||||||
|
#include <boost/program_options.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace po = boost::program_options;
|
||||||
|
|
||||||
|
namespace din {
|
||||||
|
bool parse_commandline (int parArgc, char* parArgv[], po::variables_map& parVarMap) {
|
||||||
|
po::options_description set_options(ACTION_NAME " options");
|
||||||
|
set_options.add_options()
|
||||||
|
("case-insensitive,i", "Disable case sensitivity during search")
|
||||||
|
//("option,o", po::value<std::string>()->default_value("default_value"), "Help message")
|
||||||
|
//("option2", po::value<int>(), "Help message")
|
||||||
|
;
|
||||||
|
|
||||||
|
po::options_description positional_options("Positional options");
|
||||||
|
positional_options.add_options()
|
||||||
|
("substring", po::value<std::string>(), "Substring to look for")
|
||||||
|
;
|
||||||
|
|
||||||
|
const auto desc = dinlib::get_default_commandline();
|
||||||
|
po::options_description all("Available options");
|
||||||
|
po::positional_options_description pd;
|
||||||
|
all.add(desc).add(positional_options).add(set_options);
|
||||||
|
pd.add("substring", 1);//.add("pos_option2", 1);
|
||||||
|
try {
|
||||||
|
po::store(po::command_line_parser(parArgc, parArgv).options(all).positional(pd).run(), parVarMap);
|
||||||
|
}
|
||||||
|
catch (const po::validation_error& err) {
|
||||||
|
throw dinlib::ValidationError(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
po::notify(parVarMap);
|
||||||
|
|
||||||
|
if (dinlib::manage_common_commandline(std::cout, ACTION_NAME, "[options...] <file name, path or substring>", parVarMap, {std::cref(desc), std::cref(set_options)})) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} //namespace din
|
29
src/locate/commandline.hpp
Normal file
29
src/locate/commandline.hpp
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "dindexer".
|
||||||
|
*
|
||||||
|
* "dindexer" 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.
|
||||||
|
*
|
||||||
|
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef id1B7A42F6E46547A6AB0F914E2A91399F
|
||||||
|
#define id1B7A42F6E46547A6AB0F914E2A91399F
|
||||||
|
|
||||||
|
#include "dindexer-common/validationerror.hpp"
|
||||||
|
#include "dindexer-common/mediatypes.hpp"
|
||||||
|
#include <boost/program_options/variables_map.hpp>
|
||||||
|
|
||||||
|
namespace din {
|
||||||
|
bool parse_commandline ( int parArgc, char* parArgv[], boost::program_options::variables_map& parVarMap );
|
||||||
|
} //namespace din
|
||||||
|
|
||||||
|
#endif
|
65
src/locate/main.cpp
Normal file
65
src/locate/main.cpp
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "dindexer".
|
||||||
|
*
|
||||||
|
* "dindexer" 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.
|
||||||
|
*
|
||||||
|
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "commandline.hpp"
|
||||||
|
#include "postgre_locate.hpp"
|
||||||
|
#include "dindexer-common/settings.hpp"
|
||||||
|
#include "dindexerConfig.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <ciso646>
|
||||||
|
#include <iterator>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace din {
|
||||||
|
std::ostream& operator<< (std::ostream& parStream, const LocatedItem& parItem) {
|
||||||
|
parStream << parItem.group_id << '\t' << parItem.id << '\t' << parItem.path;
|
||||||
|
return parStream;
|
||||||
|
}
|
||||||
|
} //namespace din
|
||||||
|
|
||||||
|
int main (int parArgc, char* parArgv[]) {
|
||||||
|
using boost::program_options::variables_map;
|
||||||
|
|
||||||
|
variables_map vm;
|
||||||
|
try {
|
||||||
|
if (din::parse_commandline(parArgc, parArgv, vm)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::invalid_argument& err) {
|
||||||
|
std::cerr << err.what() << "\nUse --help for help" << std::endl;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (not vm.count("substring")) {
|
||||||
|
std::cerr << "Missing search parameter, please use --help for usage instructions.\n";
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
dinlib::Settings settings;
|
||||||
|
{
|
||||||
|
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings);
|
||||||
|
if (not loaded) {
|
||||||
|
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto results = din::locate_in_db(settings.db, vm["substring"].as<std::string>(), not not vm.count("case-insensitive"));
|
||||||
|
std::copy(results.begin(), results.end(), std::ostream_iterator<din::LocatedItem>(std::cout, "\n"));
|
||||||
|
return 0;
|
||||||
|
}
|
67
src/locate/postgre_locate.cpp
Normal file
67
src/locate/postgre_locate.cpp
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "dindexer".
|
||||||
|
*
|
||||||
|
* "dindexer" 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.
|
||||||
|
*
|
||||||
|
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "postgre_locate.hpp"
|
||||||
|
#include "pq/connection.hpp"
|
||||||
|
#include <utility>
|
||||||
|
#include <sstream>
|
||||||
|
#include <boost/utility/string_ref.hpp>
|
||||||
|
#include <boost/algorithm/string.hpp>
|
||||||
|
#include <boost/lexical_cast.hpp>
|
||||||
|
|
||||||
|
namespace din {
|
||||||
|
namespace {
|
||||||
|
const int g_max_results = 200;
|
||||||
|
} //unnamed namespace
|
||||||
|
|
||||||
|
std::vector<LocatedItem> locate_in_db (const dinlib::SettingsDB& parDB, const std::string& parSearch, bool parCaseInsensitive) {
|
||||||
|
using boost::lexical_cast;
|
||||||
|
using boost::string_ref;
|
||||||
|
namespace ba = boost::algorithm;
|
||||||
|
|
||||||
|
pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port);
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
const auto clean_string_with_quotes = conn.escaped_literal(parSearch);
|
||||||
|
const auto clean_string = string_ref(clean_string_with_quotes).substr(1, clean_string_with_quotes.size() - 2);
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "SELECT \"path\",\"id\",\"group_id\" FROM \"files\" WHERE ";
|
||||||
|
if (parCaseInsensitive) {
|
||||||
|
std::string lower(clean_string);
|
||||||
|
ba::to_lower(lower);
|
||||||
|
oss << "LOWER(\"path\") LIKE '%" << lower << "%' ";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
oss << "\"path\" LIKE '%" << clean_string << "%' ";
|
||||||
|
}
|
||||||
|
oss << "LIMIT " << g_max_results << ';';
|
||||||
|
|
||||||
|
auto result = conn.query(oss.str());
|
||||||
|
std::vector<LocatedItem> retval;
|
||||||
|
retval.reserve(result.size());
|
||||||
|
for (const auto& record : result) {
|
||||||
|
retval.push_back(LocatedItem{
|
||||||
|
record["path"],
|
||||||
|
std::stoull(record["id"]),
|
||||||
|
lexical_cast<uint32_t>(record["group_id"])
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::move(retval);
|
||||||
|
}
|
||||||
|
} //namespace din
|
36
src/locate/postgre_locate.hpp
Normal file
36
src/locate/postgre_locate.hpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/* Copyright 2015, Michele Santullo
|
||||||
|
* This file is part of "dindexer".
|
||||||
|
*
|
||||||
|
* "dindexer" 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.
|
||||||
|
*
|
||||||
|
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef id1AE05A59AE0E4A4490040FD85D9AF665
|
||||||
|
#define id1AE05A59AE0E4A4490040FD85D9AF665
|
||||||
|
|
||||||
|
#include "dindexer-common/settings.hpp"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace din {
|
||||||
|
struct LocatedItem {
|
||||||
|
std::string path;
|
||||||
|
uint64_t id;
|
||||||
|
uint32_t group_id;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<LocatedItem> locate_in_db ( const dinlib::SettingsDB& parDB, const std::string& parSearch, bool parCaseInsensitive );
|
||||||
|
} //namespace din
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue