diff --git a/.clang-format b/.clang-format index 130ce61da..e7fa4fb1f 100644 --- a/.clang-format +++ b/.clang-format @@ -99,7 +99,7 @@ CompactNamespaces: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true -DerivePointerAlignment: true +DerivePointerAlignment: false DisableFormat: false EmptyLineAfterAccessModifier: Never EmptyLineBeforeAccessModifier: LogicalBlock @@ -136,7 +136,7 @@ IndentPPDirectives: None IndentRequiresClause: true IndentWidth: 4 IndentWrappedFunctionNames: false -InsertBraces: false +InsertBraces: true InsertNewlineAtEOF: false InsertTrailingCommas: None IntegerLiteralSeparator: @@ -175,12 +175,12 @@ PenaltyIndentedWhitespace: 0 PenaltyReturnTypeOnItsOwnLine: 200 PointerAlignment: Left PPIndentWidth: -1 -QualifierAlignment: Leave +QualifierAlignment: Left ReferenceAlignment: Pointer ReflowComments: true RemoveBracesLLVM: false RemoveParentheses: Leave -RemoveSemicolon: false +RemoveSemicolon: true RequiresClausePosition: OwnLine RequiresExpressionIndentation: OuterScope SeparateDefinitionBlocks: Leave diff --git a/clang-tidy/.clang-tidy b/clang-tidy/.clang-tidy deleted file mode 100644 index 063df74f1..000000000 --- a/clang-tidy/.clang-tidy +++ /dev/null @@ -1,31 +0,0 @@ -Checks: 'clang-diagnostic-*, - clang-analyzer-*, - performance-*, - bugprone-*, - -bugprone-exception-escape, - -bugprone-branch-clone, - -bugprone-easily-swappable-parameters, - -bugprone-macro-parentheses, - -bugprone-signed-char-misuse, - -bugprone-narrowing-conversions, - -bugprone-reserved-identifier, - -bugprone-implicit-widening-of-multiplication-result, - -bugprone-assignment-in-if-condition, - -bugprone-parent-virtual-call, - -bugprone-integer-division, - -bugprone-unhandled-self-assignment, - -bugprone-inc-dec-in-conditions, - -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, - -performance-no-int-to-ptr, - -performance-enum-size, - -performance-avoid-endl' -# clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling - too many unnecessary warning in vendored code -# performance-no-int-to-ptr - consider how to fix this -# bugprone-macro-parentheses - consider fixing -WarningsAsErrors: '*' -HeaderFilterRegex: '.*(?= 4.0.0 are given under - # the top level key 'Diagnostics' in the output yaml files - mergekey = "Diagnostics" - merged=[] - for replacefile in glob.iglob(os.path.join(tmpdir, '*.yaml')): - content = yaml.safe_load(open(replacefile, 'r')) - if not content: - continue # Skip empty files. - merged.extend(content.get(mergekey, [])) - - if merged: - # MainSourceFile: The key is required by the definition inside - # include/clang/Tooling/ReplacementsYaml.h, but the value - # is actually never used inside clang-apply-replacements, - # so we set it to '' here. - output = {'MainSourceFile': '', mergekey: merged} - with open(mergefile, 'w') as out: - yaml.safe_dump(output, out) - else: - # Empty the file: - open(mergefile, 'w').close() - - -def find_binary(arg, name, build_path): - """Get the path for a binary or exit""" - if arg: - if shutil.which(arg): - return arg - else: - raise SystemExit( - "error: passed binary '{}' was not found or is not executable" - .format(arg)) - - built_path = os.path.join(build_path, "bin", name) - binary = shutil.which(name) or shutil.which(built_path) - if binary: - return binary - else: - raise SystemExit( - "error: failed to find {} in $PATH or at {}" - .format(name, built_path)) - - -def apply_fixes(args, clang_apply_replacements_binary, tmpdir): - """Calls clang-apply-fixes on a given directory.""" - invocation = [clang_apply_replacements_binary] - invocation.append('-ignore-insert-conflict') - if args.format: - invocation.append('-format') - if args.style: - invocation.append('-style=' + args.style) - invocation.append(tmpdir) - subprocess.call(invocation) - - -def run_tidy(args, clang_tidy_binary, tmpdir, build_path, queue, lock, - failed_files): - """Takes filenames out of queue and runs clang-tidy on them.""" - while True: - name = queue.get() - invocation = get_tidy_invocation(name, clang_tidy_binary, args.checks, - tmpdir, build_path, args.header_filter, - args.allow_enabling_alpha_checkers, - args.extra_arg, args.extra_arg_before, - args.quiet, args.config_file, args.config, - args.line_filter, args.use_color, - args.plugins) - - proc = subprocess.Popen(invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output, err = proc.communicate() - if proc.returncode != 0: - if proc.returncode < 0: - msg = "%s: terminated by signal %d\n" % (name, -proc.returncode) - err += msg.encode('utf-8') - failed_files.append(name) - with lock: - sys.stdout.write(' '.join(invocation) + '\n' + output.decode('utf-8')) - if len(err) > 0: - sys.stdout.flush() - sys.stderr.write(err.decode('utf-8')) - queue.task_done() - - -def main(): - parser = argparse.ArgumentParser(description='Runs clang-tidy over all files ' - 'in a compilation database. Requires ' - 'clang-tidy and clang-apply-replacements in ' - '$PATH or in your build directory.') - parser.add_argument('-allow-enabling-alpha-checkers', - action='store_true', help='allow alpha checkers from ' - 'clang-analyzer.') - parser.add_argument('-clang-tidy-binary', metavar='PATH', - default='clang-tidy-18', - help='path to clang-tidy binary') - parser.add_argument('-clang-apply-replacements-binary', metavar='PATH', - default='clang-apply-replacements-18', - help='path to clang-apply-replacements binary') - parser.add_argument('-checks', default=None, - help='checks filter, when not specified, use clang-tidy ' - 'default') - config_group = parser.add_mutually_exclusive_group() - config_group.add_argument('-config', default=None, - help='Specifies a configuration in YAML/JSON format: ' - ' -config="{Checks: \'*\', ' - ' CheckOptions: {x: y}}" ' - 'When the value is empty, clang-tidy will ' - 'attempt to find a file named .clang-tidy for ' - 'each source file in its parent directories.') - config_group.add_argument('-config-file', default=None, - help='Specify the path of .clang-tidy or custom config ' - 'file: e.g. -config-file=/some/path/myTidyConfigFile. ' - 'This option internally works exactly the same way as ' - '-config option after reading specified config file. ' - 'Use either -config-file or -config, not both.') - parser.add_argument('-header-filter', default=None, - help='regular expression matching the names of the ' - 'headers to output diagnostics from. Diagnostics from ' - 'the main file of each translation unit are always ' - 'displayed.') - parser.add_argument('-line-filter', default=None, - help='List of files with line ranges to filter the' - 'warnings.') - if yaml: - parser.add_argument('-export-fixes', metavar='filename', dest='export_fixes', - help='Create a yaml file to store suggested fixes in, ' - 'which can be applied with clang-apply-replacements.') - parser.add_argument('-j', type=int, default=0, - help='number of tidy instances to be run in parallel.') - parser.add_argument('files', nargs='*', default=['.*'], - help='files to be processed (regex on path)') - parser.add_argument('-fix', action='store_true', help='apply fix-its') - parser.add_argument('-format', action='store_true', help='Reformat code ' - 'after applying fixes') - parser.add_argument('-style', default='file', help='The style of reformat ' - 'code after applying fixes') - parser.add_argument('-use-color', type=strtobool, nargs='?', const=True, - help='Use colors in diagnostics, overriding clang-tidy\'s' - ' default behavior. This option overrides the \'UseColor' - '\' option in .clang-tidy file, if any.') - parser.add_argument('-p', dest='build_path', - help='Path used to read a compile command database.') - parser.add_argument('-extra-arg', dest='extra_arg', - action='append', default=[], - help='Additional argument to append to the compiler ' - 'command line.') - parser.add_argument('-extra-arg-before', dest='extra_arg_before', - action='append', default=[], - help='Additional argument to prepend to the compiler ' - 'command line.') - parser.add_argument('-ignore', default=DEFAULT_CLANG_TIDY_IGNORE, - help='File path to clang-tidy-ignore') - parser.add_argument('-quiet', action='store_true', - help='Run clang-tidy in quiet mode') - parser.add_argument('-load', dest='plugins', - action='append', default=[], - help='Load the specified plugin in clang-tidy.') - args = parser.parse_args() - - db_path = 'compile_commands.json' - - if args.build_path is not None: - build_path = args.build_path - else: - # Find our database - build_path = find_compilation_database(db_path) - - clang_tidy_binary = find_binary(args.clang_tidy_binary, "clang-tidy", - build_path) - - tmpdir = None - if args.fix or (yaml and args.export_fixes): - clang_apply_replacements_binary = find_binary( - args.clang_apply_replacements_binary, "clang-apply-replacements", - build_path) - tmpdir = tempfile.mkdtemp() - - try: - invocation = get_tidy_invocation("", clang_tidy_binary, args.checks, - None, build_path, args.header_filter, - args.allow_enabling_alpha_checkers, - args.extra_arg, args.extra_arg_before, - args.quiet, args.config_file, args.config, - args.line_filter, args.use_color, - args.plugins) - invocation.append('-list-checks') - invocation.append('-') - if args.quiet: - # Even with -quiet we still want to check if we can call clang-tidy. - with open(os.devnull, 'w') as dev_null: - subprocess.check_call(invocation, stdout=dev_null) - else: - subprocess.check_call(invocation) - except: - print("Unable to run clang-tidy.", file=sys.stderr) - sys.exit(1) - - # Load the database and extract all files. - database = json.load(open(os.path.join(build_path, db_path))) - files = set([make_absolute(entry['file'], entry['directory']) - for entry in database]) - files, excluded = filter_files(args.ignore, files) - if excluded: - print("Excluding the following files:\n" + "\n".join(excluded) + "\n") - - max_task = args.j - if max_task == 0: - max_task = multiprocessing.cpu_count() - - # Build up a big regexy filter from all command line arguments. - file_name_re = re.compile('|'.join(args.files)) - - return_code = 0 - try: - # Spin up a bunch of tidy-launching threads. - task_queue = queue.Queue(max_task) - # List of files with a non-zero return code. - failed_files = [] - lock = threading.Lock() - for _ in range(max_task): - t = threading.Thread(target=run_tidy, - args=(args, clang_tidy_binary, tmpdir, build_path, - task_queue, lock, failed_files)) - t.daemon = True - t.start() - - # Fill the queue with files. - for name in files: - if file_name_re.search(name): - task_queue.put(name) - - # Wait for all threads to be done. - task_queue.join() - if len(failed_files): - return_code = 1 - - except KeyboardInterrupt: - # This is a sad hack. Unfortunately subprocess goes - # bonkers with ctrl-c and we start forking merrily. - print('\nCtrl-C detected, goodbye.') - if tmpdir: - shutil.rmtree(tmpdir) - os.kill(0, 9) - - if yaml and args.export_fixes: - print('Writing fixes to ' + args.export_fixes + ' ...') - try: - merge_replacement_files(tmpdir, args.export_fixes) - except: - print('Error exporting fixes.\n', file=sys.stderr) - traceback.print_exc() - return_code=1 - - if args.fix: - print('Applying fixes ...') - try: - apply_fixes(args, clang_apply_replacements_binary, tmpdir) - except: - print('Error applying fixes.\n', file=sys.stderr) - traceback.print_exc() - return_code = 1 - - if tmpdir: - shutil.rmtree(tmpdir) - sys.exit(return_code) - - -if __name__ == '__main__': - main() diff --git a/cpp_src/CMakeLists.txt b/cpp_src/CMakeLists.txt index f0ae5f63d..264bce334 100644 --- a/cpp_src/CMakeLists.txt +++ b/cpp_src/CMakeLists.txt @@ -86,8 +86,8 @@ if(NOT MSVC AND NOT APPLE) endif() if(MSVC) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -wd4244 -wd4267 -wd4996 -wd4717 -MP") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4244 -wd4267 -wd4996 -wd4717 -wd4800 -wd4396 -wd4503 -MP") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -wd4244 -wd4267 -wd4996 -wd4717 -MP -MD") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4244 -wd4267 -wd4996 -wd4717 -wd4800 -wd4396 -wd4503 -MP -MD") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -SAFESEH:NO") else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -Wswitch-enum") @@ -567,9 +567,10 @@ endif() include_directories(${PROJECT_BINARY_DIR}) -string ( REGEX REPLACE "(.*)([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)" "\\2.\\3.\\4" REINDEXER_VERSION ${REINDEXER_VERSION_FULL}) +string ( REGEX REPLACE "v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)" "\\1.\\2.\\3" REINDEXER_VERSION ${REINDEXER_VERSION_FULL}) string ( REGEX REPLACE ".*-([0-9]+)-(.*)" "\\1.\\2" REINDEXER_RELEASE ${REINDEXER_VERSION_FULL}) set(REINDEXER_VERSION_REDUCED ${REINDEXER_VERSION}) + if(CMAKE_MATCH_1) set(REINDEXER_VERSION ${REINDEXER_VERSION}.${REINDEXER_RELEASE}) endif() diff --git a/cpp_src/client/coroqueryresults.cc b/cpp_src/client/coroqueryresults.cc index 6e858b108..8eab07c21 100644 --- a/cpp_src/client/coroqueryresults.cc +++ b/cpp_src/client/coroqueryresults.cc @@ -12,7 +12,7 @@ using namespace reindexer::net; CoroQueryResults::CoroQueryResults(int fetchFlags) : conn_(nullptr), fetchOffset_(0), fetchFlags_(fetchFlags), fetchAmount_(0), requestTimeout_(0) {} -CoroQueryResults::CoroQueryResults(net::cproto::CoroClientConnection *conn, NsArray &&nsArray, int fetchFlags, int fetchAmount, +CoroQueryResults::CoroQueryResults(net::cproto::CoroClientConnection* conn, NsArray&& nsArray, int fetchFlags, int fetchAmount, seconds timeout) : conn_(conn), nsArray_(std::move(nsArray)), @@ -21,7 +21,7 @@ CoroQueryResults::CoroQueryResults(net::cproto::CoroClientConnection *conn, NsAr fetchAmount_(fetchAmount), requestTimeout_(timeout) {} -CoroQueryResults::CoroQueryResults(net::cproto::CoroClientConnection *conn, NsArray &&nsArray, std::string_view rawResult, RPCQrId id, +CoroQueryResults::CoroQueryResults(net::cproto::CoroClientConnection* conn, NsArray&& nsArray, std::string_view rawResult, RPCQrId id, int fetchFlags, int fetchAmount, seconds timeout) : CoroQueryResults(conn, std::move(nsArray), fetchFlags, fetchAmount, timeout) { Bind(rawResult, id); @@ -61,7 +61,7 @@ void CoroQueryResults::Bind(std::string_view rawResult, RPCQrId id) { RawResBufT::max_size(), rawResLen, fetchAmount_); } rawResult_.assign(copyStart, rawResult.end()); - } catch (const Error &err) { + } catch (const Error& err) { status_ = err; } } @@ -98,7 +98,9 @@ void CoroQueryResults::fetchNextResults() { h_vector CoroQueryResults::GetNamespaces() const { h_vector ret; ret.reserve(nsArray_.size()); - for (auto &ns : nsArray_) ret.push_back(ns->name_); + for (auto& ns : nsArray_) { + ret.push_back(ns->name_); + } return ret; } @@ -107,14 +109,14 @@ TagsMatcher CoroQueryResults::getTagsMatcher(int nsid) const { return nsArray_[n class AdditionalRank : public IAdditionalDatasource { public: AdditionalRank(double r) noexcept : rank_(r) {} - void PutAdditionalFields(JsonBuilder &builder) const override final { builder.Put("rank()", rank_); } - IEncoderDatasourceWithJoins *GetJoinsDatasource() noexcept override final { return nullptr; } + void PutAdditionalFields(JsonBuilder& builder) const override final { builder.Put("rank()", rank_); } + IEncoderDatasourceWithJoins* GetJoinsDatasource() noexcept override final { return nullptr; } private: double rank_; }; -void CoroQueryResults::Iterator::getJSONFromCJSON(std::string_view cjson, WrSerializer &wrser, bool withHdrLen) { +void CoroQueryResults::Iterator::getJSONFromCJSON(std::string_view cjson, WrSerializer& wrser, bool withHdrLen) { auto tm = qr_->getTagsMatcher(itemParams_.nsid); JsonEncoder enc(&tm); JsonBuilder builder(wrser, ObjType::TypePlain); @@ -136,7 +138,7 @@ void CoroQueryResults::Iterator::getJSONFromCJSON(std::string_view cjson, WrSeri } } -Error CoroQueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLen) { +Error CoroQueryResults::Iterator::GetMsgPack(WrSerializer& wrser, bool withHdrLen) { readNext(); int type = qr_->queryParams_.flags & kResultsFormatMask; if (type == kResultsMsgPack) { @@ -151,7 +153,7 @@ Error CoroQueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLe return errOK; } -Error CoroQueryResults::Iterator::GetJSON(WrSerializer &wrser, bool withHdrLen) { +Error CoroQueryResults::Iterator::GetJSON(WrSerializer& wrser, bool withHdrLen) { readNext(); try { switch (qr_->queryParams_.flags & kResultsFormatMask) { @@ -169,13 +171,13 @@ Error CoroQueryResults::Iterator::GetJSON(WrSerializer &wrser, bool withHdrLen) default: return Error(errParseBin, "Server returned data in unknown format %d", qr_->queryParams_.flags & kResultsFormatMask); } - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; } -Error CoroQueryResults::Iterator::GetCJSON(WrSerializer &wrser, bool withHdrLen) { +Error CoroQueryResults::Iterator::GetCJSON(WrSerializer& wrser, bool withHdrLen) { readNext(); try { switch (qr_->queryParams_.flags & kResultsFormatMask) { @@ -193,7 +195,7 @@ Error CoroQueryResults::Iterator::GetCJSON(WrSerializer &wrser, bool withHdrLen) default: return Error(errParseBin, "Server returned data in unknown format %d", qr_->queryParams_.flags & kResultsFormatMask); } - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; @@ -215,7 +217,7 @@ Item CoroQueryResults::Iterator::GetItem() { break; } case kResultsJson: { - char *endp = nullptr; + char* endp = nullptr; err = item.FromJSON(itemParams_.data, &endp); break; } @@ -226,7 +228,7 @@ Item CoroQueryResults::Iterator::GetItem() { return item; } return Item(); - } catch (const Error &) { + } catch (const Error&) { return Item(); } } @@ -248,7 +250,9 @@ std::string_view CoroQueryResults::Iterator::GetRaw() { } void CoroQueryResults::Iterator::readNext() { - if (nextPos_ != 0) return; + if (nextPos_ != 0) { + return; + } std::string_view rawResult(qr_->rawResult_.data(), qr_->rawResult_.size()); @@ -261,12 +265,12 @@ void CoroQueryResults::Iterator::readNext() { (void)joinedCnt; } nextPos_ = pos_ + ser.Pos(); - } catch (const Error &err) { - const_cast(qr_)->status_ = err; + } catch (const Error& err) { + const_cast(qr_)->status_ = err; } } -CoroQueryResults::Iterator &CoroQueryResults::Iterator::operator++() { +CoroQueryResults::Iterator& CoroQueryResults::Iterator::operator++() { try { readNext(); idx_++; @@ -274,11 +278,11 @@ CoroQueryResults::Iterator &CoroQueryResults::Iterator::operator++() { nextPos_ = 0; if (idx_ != qr_->queryParams_.qcount && idx_ == qr_->queryParams_.count + qr_->fetchOffset_) { - const_cast(qr_)->fetchNextResults(); + const_cast(qr_)->fetchNextResults(); pos_ = 0; } - } catch (const Error &err) { - const_cast(qr_)->status_ = err; + } catch (const Error& err) { + const_cast(qr_)->status_ = err; } return *this; diff --git a/cpp_src/client/coroqueryresults.h b/cpp_src/client/coroqueryresults.h index d791ae426..4a5204ca4 100644 --- a/cpp_src/client/coroqueryresults.h +++ b/cpp_src/client/coroqueryresults.h @@ -40,7 +40,7 @@ class CoroQueryResults { Iterator& operator++(); Error Status() const noexcept { return qr_->status_; } bool operator!=(const Iterator& other) const noexcept { return idx_ != other.idx_; } - bool operator==(const Iterator& other) const noexcept { return idx_ == other.idx_; }; + bool operator==(const Iterator& other) const noexcept { return idx_ == other.idx_; } Iterator& operator*() { return *this; } void readNext(); void getJSONFromCJSON(std::string_view cjson, WrSerializer& wrser, bool withHdrLen = true); diff --git a/cpp_src/client/cororeindexer.cc b/cpp_src/client/cororeindexer.cc index 66b6c9653..cf36c8c3f 100644 --- a/cpp_src/client/cororeindexer.cc +++ b/cpp_src/client/cororeindexer.cc @@ -55,12 +55,8 @@ Error CoroReindexer::GetMeta(std::string_view nsName, const std::string& key, st Error CoroReindexer::PutMeta(std::string_view nsName, const std::string& key, std::string_view data) { return impl_->PutMeta(nsName, key, data, ctx_); } -Error CoroReindexer::EnumMeta(std::string_view nsName, std::vector& keys) { - return impl_->EnumMeta(nsName, keys, ctx_); -} -Error CoroReindexer::DeleteMeta(std::string_view nsName, const std::string& key) { - return impl_->DeleteMeta(nsName, key, ctx_); -} +Error CoroReindexer::EnumMeta(std::string_view nsName, std::vector& keys) { return impl_->EnumMeta(nsName, keys, ctx_); } +Error CoroReindexer::DeleteMeta(std::string_view nsName, const std::string& key) { return impl_->DeleteMeta(nsName, key, ctx_); } Error CoroReindexer::Delete(const Query& q, CoroQueryResults& result) { return impl_->Delete(q, result, ctx_); } Error CoroReindexer::Select(std::string_view query, CoroQueryResults& result) { return impl_->Select(query, result, ctx_); } Error CoroReindexer::Select(const Query& q, CoroQueryResults& result) { return impl_->Select(q, result, ctx_); } diff --git a/cpp_src/client/cororeindexer.h b/cpp_src/client/cororeindexer.h index 9ad7dc646..a7d849d25 100644 --- a/cpp_src/client/cororeindexer.h +++ b/cpp_src/client/cororeindexer.h @@ -29,23 +29,23 @@ class CoroRPCClient; class CoroReindexer { public: /// Completion routine - typedef std::function Completion; + typedef std::function Completion; /// Create Reindexer database object - CoroReindexer(const ReindexerConfig & = ReindexerConfig()); + CoroReindexer(const ReindexerConfig& = ReindexerConfig()); /// Destroy Reindexer database object ~CoroReindexer(); - CoroReindexer(const CoroReindexer &) = delete; - CoroReindexer(CoroReindexer &&) noexcept; - CoroReindexer &operator=(const CoroReindexer &) = delete; - CoroReindexer &operator=(CoroReindexer &&) noexcept; + CoroReindexer(const CoroReindexer&) = delete; + CoroReindexer(CoroReindexer&&) noexcept; + CoroReindexer& operator=(const CoroReindexer&) = delete; + CoroReindexer& operator=(CoroReindexer&&) noexcept; /// Connect - connect to reindexer server /// @param dsn - uri of server and database, like: `cproto://user@password:127.0.0.1:6534/dbname` or /// `ucproto://user@password:/tmp/reindexer.sock:/dbname` /// @param loop - event loop for connections and coroutines handling /// @param opts - Connect options. May contain any of
- Error Connect(const std::string &dsn, dynamic_loop &loop, const client::ConnectOpts &opts = client::ConnectOpts()); + Error Connect(const std::string& dsn, dynamic_loop& loop, const client::ConnectOpts& opts = client::ConnectOpts()); /// Stop - shutdown connector void Stop(); /// Open or create namespace @@ -54,10 +54,10 @@ class CoroReindexer { /// StorageOpts::Enabled() - Enable storage. If storage is disabled, then namespace will be completely in-memory
/// StorageOpts::CreateIfMissing () - Storage will be created, if missing /// @return errOK - On success - Error OpenNamespace(std::string_view nsName, const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); + Error OpenNamespace(std::string_view nsName, const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); /// Create new namespace. Will fail, if namespace already exists /// @param nsDef - NamespaceDef with namespace initial parameters - Error AddNamespace(const NamespaceDef &nsDef); + Error AddNamespace(const NamespaceDef& nsDef); /// Close namespace. Will free all memory resources, associated with namespace. Forces sync changes to disk /// @param nsName - Name of namespace Error CloseNamespace(std::string_view nsName); @@ -70,19 +70,19 @@ class CoroReindexer { /// Rename namespace. If namespace with dstNsName exists, then it is replaced. /// @param srcNsName - Name of namespace /// @param dstNsName - desired name of namespace - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName); /// Add index to namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error AddIndex(std::string_view nsName, const IndexDef &index); + Error AddIndex(std::string_view nsName, const IndexDef& index); /// Update index in namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error UpdateIndex(std::string_view nsName, const IndexDef &index); + Error UpdateIndex(std::string_view nsName, const IndexDef& index); /// Drop index from namespace /// @param nsName - Name of namespace /// @param index - index name - Error DropIndex(std::string_view nsName, const IndexDef &index); + Error DropIndex(std::string_view nsName, const IndexDef& index); /// Set fields schema for namespace /// @param nsName - Name of namespace /// @param schema - JSON in JsonSchema format @@ -90,50 +90,50 @@ class CoroReindexer { /// Get list of all available namespaces /// @param defs - std::vector of NamespaceDef of available namespaces /// @param opts - Enumeration options - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts); /// Gets a list of available databases for a certain server. /// @param dbList - list of DB names - Error EnumDatabases(std::vector &dbList); + Error EnumDatabases(std::vector& dbList); /// Insert new Item to namespace. If item with same PK is already exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Insert(std::string_view nsName, Item &item); + Error Insert(std::string_view nsName, Item& item); /// Update Item in namespace. If item with same PK is not exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Update(std::string_view nsName, Item &item); + Error Update(std::string_view nsName, Item& item); /// Update or Insert Item in namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Upsert(std::string_view nsName, Item &item); + Error Upsert(std::string_view nsName, Item& item); /// Updates all items in namespace, that satisfy provided query. /// @param query - Query to define items set for update. /// @param result - QueryResults with IDs of deleted items. - Error Update(const Query &query, CoroQueryResults &result); + Error Update(const Query& query, CoroQueryResults& result); /// Delete Item from namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Delete(std::string_view nsName, Item &item); + Error Delete(std::string_view nsName, Item& item); /// Delete all items from namespace, which matches provided Query /// @param query - Query with conditions /// @param result - QueryResults with IDs of deleted items - Error Delete(const Query &query, CoroQueryResults &result); + Error Delete(const Query& query, CoroQueryResults& result); /// Execute SQL Query and return results /// May be used with completion /// @param query - SQL query. Only "SELECT" semantic is supported /// @param result - QueryResults with found items - Error Select(std::string_view query, CoroQueryResults &result); + Error Select(std::string_view query, CoroQueryResults& result); /// Execute Query and return results /// May be used with completion /// @param query - Query object with query attributes /// @param result - QueryResults with found items - Error Select(const Query &query, CoroQueryResults &result); + Error Select(const Query& query, CoroQueryResults& result); /// Flush changes to storage /// @param nsName - Name of namespace Error Commit(std::string_view nsName); @@ -145,34 +145,34 @@ class CoroReindexer { /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - output string with metadata - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data); /// Put metadata to storage by key /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - string with metadata - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data); /// Get list of all metadata keys /// @param nsName - Name of namespace /// @param keys - std::vector filled with meta keys - Error EnumMeta(std::string_view nsName, std::vector &keys); + Error EnumMeta(std::string_view nsName, std::vector& keys); /// Delete metadata from storage by key /// @param nsName - Name of namespace /// @param key - string with meta key - Error DeleteMeta(std::string_view nsName, const std::string &key); + Error DeleteMeta(std::string_view nsName, const std::string& key); /// Subscribe to updates of database /// @param observer - Observer interface, which will receive updates /// @param filters - Subscription filters set /// @param opts - Subscription options (allows to either add new filters or reset them) - Error SubscribeUpdates(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts = SubscriptionOpts()); + Error SubscribeUpdates(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts = SubscriptionOpts()); /// Unsubscribe from updates of database /// Cancellation context doesn't affect this call /// @param observer - Observer interface, which will be unsubscribed updates - Error UnsubscribeUpdates(IUpdatesObserver *observer); + Error UnsubscribeUpdates(IUpdatesObserver* observer); /// Get possible suggestions for token (set by 'pos') in Sql query. /// @param sqlQuery - sql query. /// @param pos - position in sql query for suggestions. /// @param suggestions - all the suggestions for 'pos' position in query. - Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector &suggestions); + Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector& suggestions); /// Get current connection status Error Status(); /// Allocate new transaction for namespace @@ -180,14 +180,14 @@ class CoroReindexer { CoroTransaction NewTransaction(std::string_view nsName); /// Commit transaction - transaction will be deleted after commit /// @param tr - transaction to commit - Error CommitTransaction(CoroTransaction &tr); + Error CommitTransaction(CoroTransaction& tr); /// RollBack transaction - transaction will be deleted after rollback /// @param tr - transaction to rollback - Error RollBackTransaction(CoroTransaction &tr); + Error RollBackTransaction(CoroTransaction& tr); /// Add cancelable context /// @param cancelCtx - context pointer - CoroReindexer WithContext(const IRdxCancelContext *cancelCtx) { return CoroReindexer(impl_, ctx_.WithCancelContext(cancelCtx)); } + CoroReindexer WithContext(const IRdxCancelContext* cancelCtx) { return CoroReindexer(impl_, ctx_.WithCancelContext(cancelCtx)); } /// Add execution timeout to the next query /// @param timeout - Optional server-side execution timeout for each subquery @@ -198,8 +198,8 @@ class CoroReindexer { typedef ReindexerConfig ConfigT; private: - CoroReindexer(CoroRPCClient *impl, InternalRdxContext &&ctx) : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} - CoroRPCClient *impl_; + CoroReindexer(CoroRPCClient* impl, InternalRdxContext&& ctx) : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} + CoroRPCClient* impl_; bool owner_; InternalRdxContext ctx_; }; diff --git a/cpp_src/client/cororpcclient.cc b/cpp_src/client/cororpcclient.cc index 1776d0d8b..43b41257c 100644 --- a/cpp_src/client/cororpcclient.cc +++ b/cpp_src/client/cororpcclient.cc @@ -66,7 +66,9 @@ Error CoroRPCClient::AddNamespace(const NamespaceDef& nsDef, const InternalRdxCo nsDef.GetJSON(ser); auto status = conn_.Call(mkCommand(cproto::kCmdOpenNamespace, &ctx), ser.Slice()).Status(); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } namespaces_.emplace(nsDef.name, Namespace::Ptr(new Namespace(nsDef.name))); return errOK; @@ -92,7 +94,9 @@ Error CoroRPCClient::TruncateNamespace(std::string_view nsName, const InternalRd Error CoroRPCClient::RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx) { auto status = conn_.Call(mkCommand(cproto::kCmdRenameNamespace, &ctx), srcNsName, dstNsName).Status(); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } if (srcNsName != dstNsName) { auto namespacePtr = namespaces_.find(srcNsName); @@ -152,7 +156,9 @@ Error CoroRPCClient::modifyItem(std::string_view nsName, Item& item, int mode, s return err; } } else { - if (ret.Status().code() != errStateInvalidated || tryCount > 2) return ret.Status(); + if (ret.Status().code() != errStateInvalidated || tryCount > 2) { + return ret.Status(); + } if (withNetTimeout) { netTimeout = netDeadline - conn_.Now(); } @@ -168,7 +174,9 @@ Error CoroRPCClient::modifyItem(std::string_view nsName, Item& item, int mode, s auto newItem = NewItem(nsName); char* endp = nullptr; Error err = newItem.FromJSON(item.impl_->GetJSON(), &endp); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } item = std::move(newItem); } @@ -288,7 +296,9 @@ void vec2pack(const h_vector& vec, WrSerializer& ser) { // Get array of payload Type Versions ser.PutVarUint(vec.size()); - for (auto v : vec) ser.PutVarUint(v); + for (auto v : vec) { + ser.PutVarUint(v); + } return; } @@ -436,7 +446,9 @@ Error CoroRPCClient::GetSqlSuggestions(std::string_view query, int pos, std::vec suggests.clear(); suggests.reserve(rargs.size()); - for (auto& rarg : rargs) suggests.push_back(rarg.As()); + for (auto& rarg : rargs) { + suggests.push_back(rarg.As()); + } } return ret.Status(); } catch (const Error& err) { @@ -488,7 +500,9 @@ void CoroRPCClient::onUpdates(const cproto::CoroRPCAnswer& ans) { std::string_view nsName(args[1]); std::string_view pwalRec(args[2]); lsn_t originLSN; - if (args.size() >= 4) originLSN = lsn_t(args[3].As()); + if (args.size() >= 4) { + originLSN = lsn_t(args[3].As()); + } WALRecord wrec(pwalRec); if (wrec.type == WalItemModify) { @@ -508,7 +522,9 @@ void CoroRPCClient::onUpdates(const cproto::CoroRPCAnswer& ans) { InternalRdxContext ctx(nullptr); CoroQueryResults qr; auto err = Select(Query(std::string(nsName)).Limit(0), qr, ctx); - if (!err.ok()) return; + if (!err.ok()) { + return; + } } else { // We have bundled tagsMatcher if (bundledTagsMatcher) { diff --git a/cpp_src/client/cororpcclient.h b/cpp_src/client/cororpcclient.h index 65de044d1..696732960 100644 --- a/cpp_src/client/cororpcclient.h +++ b/cpp_src/client/cororpcclient.h @@ -25,72 +25,72 @@ using std::chrono::seconds; using namespace net; class CoroRPCClient { public: - typedef std::function Completion; - CoroRPCClient(const ReindexerConfig &config); - CoroRPCClient(const CoroRPCClient &) = delete; - CoroRPCClient(CoroRPCClient &&) = delete; - CoroRPCClient &operator=(const CoroRPCClient &) = delete; - CoroRPCClient &operator=(CoroRPCClient &&) = delete; + typedef std::function Completion; + CoroRPCClient(const ReindexerConfig& config); + CoroRPCClient(const CoroRPCClient&) = delete; + CoroRPCClient(CoroRPCClient&&) = delete; + CoroRPCClient& operator=(const CoroRPCClient&) = delete; + CoroRPCClient& operator=(CoroRPCClient&&) = delete; ~CoroRPCClient(); - Error Connect(const std::string &dsn, ev::dynamic_loop &loop, const client::ConnectOpts &opts); + Error Connect(const std::string& dsn, ev::dynamic_loop& loop, const client::ConnectOpts& opts); void Stop(); - Error OpenNamespace(std::string_view nsName, const InternalRdxContext &ctx, - const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); - Error AddNamespace(const NamespaceDef &nsDef, const InternalRdxContext &ctx); - Error CloseNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error DropNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error TruncateNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName, const InternalRdxContext &ctx); - Error AddIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error UpdateIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error DropIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext &ctx); - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts, const InternalRdxContext &ctx); - Error EnumDatabases(std::vector &dbList, const InternalRdxContext &ctx); - Error Insert(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Update(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Upsert(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Delete(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Delete(const Query &query, CoroQueryResults &result, const InternalRdxContext &ctx); - Error Update(const Query &query, CoroQueryResults &result, const InternalRdxContext &ctx); - Error Select(std::string_view query, CoroQueryResults &result, const InternalRdxContext &ctx) { + Error OpenNamespace(std::string_view nsName, const InternalRdxContext& ctx, + const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); + Error AddNamespace(const NamespaceDef& nsDef, const InternalRdxContext& ctx); + Error CloseNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error DropNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error TruncateNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx); + Error AddIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error UpdateIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error DropIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext& ctx); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts, const InternalRdxContext& ctx); + Error EnumDatabases(std::vector& dbList, const InternalRdxContext& ctx); + Error Insert(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Update(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Upsert(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Delete(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Delete(const Query& query, CoroQueryResults& result, const InternalRdxContext& ctx); + Error Update(const Query& query, CoroQueryResults& result, const InternalRdxContext& ctx); + Error Select(std::string_view query, CoroQueryResults& result, const InternalRdxContext& ctx) { return selectImpl(query, result, config_.RequestTimeout, ctx); } - Error Select(const Query &query, CoroQueryResults &result, const InternalRdxContext &ctx) { + Error Select(const Query& query, CoroQueryResults& result, const InternalRdxContext& ctx) { return selectImpl(query, result, config_.RequestTimeout, ctx); } Error Commit(std::string_view nsName); Item NewItem(std::string_view nsName); - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data, const InternalRdxContext &ctx); - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data, const InternalRdxContext &ctx); - Error EnumMeta(std::string_view nsName, std::vector &keys, const InternalRdxContext &ctx); - Error DeleteMeta(std::string_view nsName, const std::string &key, const InternalRdxContext &ctx); - Error SubscribeUpdates(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts = SubscriptionOpts()); - Error UnsubscribeUpdates(IUpdatesObserver *observer); - Error GetSqlSuggestions(std::string_view query, int pos, std::vector &suggests); - Error Status(const InternalRdxContext &ctx); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data, const InternalRdxContext& ctx); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data, const InternalRdxContext& ctx); + Error EnumMeta(std::string_view nsName, std::vector& keys, const InternalRdxContext& ctx); + Error DeleteMeta(std::string_view nsName, const std::string& key, const InternalRdxContext& ctx); + Error SubscribeUpdates(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts = SubscriptionOpts()); + Error UnsubscribeUpdates(IUpdatesObserver* observer); + Error GetSqlSuggestions(std::string_view query, int pos, std::vector& suggests); + Error Status(const InternalRdxContext& ctx); - CoroTransaction NewTransaction(std::string_view nsName, const InternalRdxContext &ctx); - Error CommitTransaction(CoroTransaction &tr, const InternalRdxContext &ctx); - Error RollBackTransaction(CoroTransaction &tr, const InternalRdxContext &ctx); + CoroTransaction NewTransaction(std::string_view nsName, const InternalRdxContext& ctx); + Error CommitTransaction(CoroTransaction& tr, const InternalRdxContext& ctx); + Error RollBackTransaction(CoroTransaction& tr, const InternalRdxContext& ctx); protected: - Error selectImpl(std::string_view query, CoroQueryResults &result, seconds netTimeout, const InternalRdxContext &ctx); - Error selectImpl(const Query &query, CoroQueryResults &result, seconds netTimeout, const InternalRdxContext &ctx); - Error modifyItem(std::string_view nsName, Item &item, int mode, seconds netTimeout, const InternalRdxContext &ctx); + Error selectImpl(std::string_view query, CoroQueryResults& result, seconds netTimeout, const InternalRdxContext& ctx); + Error selectImpl(const Query& query, CoroQueryResults& result, seconds netTimeout, const InternalRdxContext& ctx); + Error modifyItem(std::string_view nsName, Item& item, int mode, seconds netTimeout, const InternalRdxContext& ctx); Error subscribeImpl(bool subscribe); - Namespace *getNamespace(std::string_view nsName); - Error addConnectEntry(const std::string &dsn, const client::ConnectOpts &opts, size_t idx); - void onUpdates(const net::cproto::CoroRPCAnswer &ans); + Namespace* getNamespace(std::string_view nsName); + Error addConnectEntry(const std::string& dsn, const client::ConnectOpts& opts, size_t idx); + void onUpdates(const net::cproto::CoroRPCAnswer& ans); void startResubRoutine(); void resubRoutine(); - void onConnFatalError(const Error &) noexcept { subscribed_ = false; } + void onConnFatalError(const Error&) noexcept { subscribed_ = false; } - cproto::CommandParams mkCommand(cproto::CmdCode cmd, const InternalRdxContext *ctx = nullptr) const noexcept; - static cproto::CommandParams mkCommand(cproto::CmdCode cmd, seconds reqTimeout, const InternalRdxContext *ctx) noexcept; + cproto::CommandParams mkCommand(cproto::CmdCode cmd, const InternalRdxContext* ctx = nullptr) const noexcept; + static cproto::CommandParams mkCommand(cproto::CmdCode cmd, seconds reqTimeout, const InternalRdxContext* ctx) noexcept; fast_hash_map namespaces_; @@ -100,10 +100,10 @@ class CoroRPCClient { bool subscribed_ = false; bool terminate_ = false; coroutine::wait_group resubWg_; - ev::dynamic_loop *loop_ = nullptr; + ev::dynamic_loop* loop_ = nullptr; }; -void vec2pack(const h_vector &vec, WrSerializer &ser); +void vec2pack(const h_vector& vec, WrSerializer& ser); } // namespace client } // namespace reindexer diff --git a/cpp_src/client/corotransaction.cc b/cpp_src/client/corotransaction.cc index f2a25e1b0..3ca1b9bd6 100644 --- a/cpp_src/client/corotransaction.cc +++ b/cpp_src/client/corotransaction.cc @@ -25,18 +25,24 @@ Error CoroTransaction::addTxItem(Item&& item, ItemModifyMode mode) { conn_->Call({net::cproto::kCmdAddTxItem, RequestTimeout_, execTimeout_, nullptr}, FormatJson, itemData, mode, "", 0, txId_); if (!ret.Status().ok()) { - if (ret.Status().code() != errStateInvalidated || tryCount > 2) return ret.Status(); + if (ret.Status().code() != errStateInvalidated || tryCount > 2) { + return ret.Status(); + } CoroQueryResults qr; InternalRdxContext ctx; ctx = ctx.WithTimeout(execTimeout_); auto err = rpcClient_->Select(Query(nsName_).Limit(0), qr, ctx); - if (!err.ok()) return Error(errLogic, "Can't update TagsMatcher"); + if (!err.ok()) { + return Error(errLogic, "Can't update TagsMatcher"); + } auto newItem = NewItem(); char* endp = nullptr; err = newItem.FromJSON(item.impl_->GetJSON(), &endp); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } item = std::move(newItem); } else { break; @@ -48,7 +54,9 @@ Error CoroTransaction::addTxItem(Item&& item, ItemModifyMode mode) { } Item CoroTransaction::NewItem() { - if (!rpcClient_) return Item(Error(errLogic, "rpcClient not set for client transaction")); + if (!rpcClient_) { + return Item(Error(errLogic, "rpcClient not set for client transaction")); + } return rpcClient_->NewItem(nsName_); } } // namespace client diff --git a/cpp_src/client/internalrdxcontext.h b/cpp_src/client/internalrdxcontext.h index f4395ad6d..6fd813ad8 100644 --- a/cpp_src/client/internalrdxcontext.h +++ b/cpp_src/client/internalrdxcontext.h @@ -21,7 +21,9 @@ class InternalRdxContext { InternalRdxContext WithCancelContext(const IRdxCancelContext* cancelCtx) noexcept { return InternalRdxContext(cancelCtx, cmpl_, execTimeout_); } - InternalRdxContext WithCompletion(Completion cmpl, InternalRdxContext&) noexcept { return InternalRdxContext(std::move(cmpl), execTimeout_); } + InternalRdxContext WithCompletion(Completion cmpl, InternalRdxContext&) noexcept { + return InternalRdxContext(std::move(cmpl), execTimeout_); + } InternalRdxContext WithCompletion(Completion cmpl) const noexcept { return InternalRdxContext(std::move(cmpl), execTimeout_); } InternalRdxContext WithTimeout(milliseconds execTimeout) const noexcept { return InternalRdxContext(cmpl_, execTimeout); } diff --git a/cpp_src/client/item.cc b/cpp_src/client/item.cc index 81160da29..490d5d49e 100644 --- a/cpp_src/client/item.cc +++ b/cpp_src/client/item.cc @@ -8,12 +8,12 @@ namespace client { Item::Item() : status_(errNotValid) {} -Item::Item(Item &&other) noexcept : impl_(std::move(other.impl_)), status_(std::move(other.status_)), id_(other.id_) {} +Item::Item(Item&& other) noexcept : impl_(std::move(other.impl_)), status_(std::move(other.status_)), id_(other.id_) {} -Item::Item(ItemImpl *impl) : impl_(impl) {} -Item::Item(const Error &err) : impl_(nullptr), status_(err) {} +Item::Item(ItemImpl* impl) : impl_(impl) {} +Item::Item(const Error& err) : impl_(nullptr), status_(err) {} -Item &Item::operator=(Item &&other) noexcept { +Item& Item::operator=(Item&& other) noexcept { if (&other != this) { impl_ = std::move(other.impl_); status_ = std::move(other.status_); @@ -26,8 +26,8 @@ Item::~Item() {} Item::operator bool() const { return impl_ != nullptr; } -Error Item::FromJSON(std::string_view slice, char **endp, bool pkOnly) { return impl_->FromJSON(slice, endp, pkOnly); } -Error Item::FromCJSON(std::string_view slice) &noexcept { +Error Item::FromJSON(std::string_view slice, char** endp, bool pkOnly) { return impl_->FromJSON(slice, endp, pkOnly); } +Error Item::FromCJSON(std::string_view slice) & noexcept { try { impl_->FromCJSON(slice); } @@ -35,15 +35,15 @@ Error Item::FromCJSON(std::string_view slice) &noexcept { return {}; } void Item::FromCJSONImpl(std::string_view slice) & { impl_->FromCJSON(slice); } -Error Item::FromMsgPack(std::string_view slice, size_t &offset) { return impl_->FromMsgPack(slice, offset); } +Error Item::FromMsgPack(std::string_view slice, size_t& offset) { return impl_->FromMsgPack(slice, offset); } std::string_view Item::GetCJSON() { return impl_->GetCJSON(); } std::string_view Item::GetJSON() { return impl_->GetJSON(); } std::string_view Item::GetMsgPack() { return impl_->GetMsgPack(); } -void Item::SetPrecepts(const std::vector &precepts) { impl_->SetPrecepts(precepts); } +void Item::SetPrecepts(const std::vector& precepts) { impl_->SetPrecepts(precepts); } bool Item::IsTagsUpdated() { return impl_->tagsMatcher().isUpdated(); } int Item::GetStateToken() { return impl_->tagsMatcher().stateToken(); } -Item &Item::Unsafe(bool enable) { +Item& Item::Unsafe(bool enable) { impl_->Unsafe(enable); return *this; } diff --git a/cpp_src/client/item.h b/cpp_src/client/item.h index 8d7efe5dc..3a7ebee00 100644 --- a/cpp_src/client/item.h +++ b/cpp_src/client/item.h @@ -23,22 +23,22 @@ class Item { Item(); /// Destroy Item ~Item(); - Item(const Item &) = delete; - Item(Item &&) noexcept; - Item &operator=(const Item &) = delete; - Item &operator=(Item &&) noexcept; + Item(const Item&) = delete; + Item(Item&&) noexcept; + Item& operator=(const Item&) = delete; + Item& operator=(Item&&) noexcept; /// Build item from JSON
/// If Item is in *Unsafe Mode*, then Item will not store slice, but just keep pointer to data in slice, /// application *MUST* hold slice until end of life of Item /// @param slice - data slice with Json. /// @param endp - pounter to end of parsed part of slice - Error FromJSON(std::string_view slice, char **endp = nullptr, bool = false); + Error FromJSON(std::string_view slice, char** endp = nullptr, bool = false); /// Build item from JSON
/// If Item is in *Unsafe Mode*, then Item will not store slice, but just keep pointer to data in slice, /// application *MUST* hold slice until end of life of Item /// @param slice - data slice with CJson - Error FromCJSON(std::string_view slice) &noexcept; + Error FromCJSON(std::string_view slice) & noexcept; void FromCJSONImpl(std::string_view slice) &; /// Serialize item to CJSON.
/// If Item is in *Unfafe Mode*, then returned slice is allocated in temporary buffer, and can be invalidated by any next operation with @@ -56,7 +56,7 @@ class Item { /// Builds item from msgpack::object. /// @param slice - msgpack encoded data buffer. /// @param offset - position to start from. - Error FromMsgPack(std::string_view slice, size_t &offset); + Error FromMsgPack(std::string_view slice, size_t& offset); /// Get status of item /// @return data slice with JSON. Returned slice is allocated in temporary Item's buffer, and can be invalidated by any next operation /// with Item @@ -69,7 +69,7 @@ class Item { int NumFields() const noexcept; /// Set additional percepts for modify operation /// @param precepts - strings in format "fieldName=Func()" - void SetPrecepts(const std::vector &precepts); + void SetPrecepts(const std::vector& precepts); /// Check was names tags updated while modify operation /// @return true: tags was updated. bool IsTagsUpdated(); @@ -83,11 +83,11 @@ class Item { /// The advantage of unsafe mode is speed. It does not call extra memory allocation from heap and copying data.
/// The disadvantage of unsafe mode is potentially danger code. Most of C++ stl containters in many cases invalidates references - /// and in unsafe mode caller is responsibe to guarantee, that all resources passed to Item will keep valid - Item &Unsafe(bool enable = true); + Item& Unsafe(bool enable = true); private: - explicit Item(ItemImpl *impl); - explicit Item(const Error &err); + explicit Item(ItemImpl* impl); + explicit Item(const Error& err); void setID(int id) { id_ = id; } std::unique_ptr impl_; diff --git a/cpp_src/client/itemimpl.cc b/cpp_src/client/itemimpl.cc index 39217dabd..c9be720c0 100644 --- a/cpp_src/client/itemimpl.cc +++ b/cpp_src/client/itemimpl.cc @@ -8,7 +8,7 @@ namespace reindexer { namespace client { -ItemImpl &ItemImpl::operator=(ItemImpl &&other) noexcept { +ItemImpl& ItemImpl::operator=(ItemImpl&& other) noexcept { if (&other != this) { payloadType_ = std::move(other.payloadType_); payloadValue_ = std::move(other.payloadValue_); @@ -57,7 +57,7 @@ void ItemImpl::FromCJSON(std::string_view slice) { pl.Set(0, Variant(p_string(&tupleData_), Variant::no_hold_t{})); } -Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool /*pkOnly*/) { +Error ItemImpl::FromJSON(std::string_view slice, char** endp, bool /*pkOnly*/) { std::string_view data = slice; if (!unsafe_ && endp == nullptr) { holder_.emplace_back(slice); @@ -71,11 +71,13 @@ Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool /*pkOnly*/) { gason::JsonParser parser(&largeJSONStrings_); try { node = parser.Parse(giftStr(data), &len); - if (node.value.getTag() != gason::JSON_OBJECT) return Error(errParseJson, "Expected json object"); + if (node.value.getTag() != gason::JSON_OBJECT) { + return Error(errParseJson, "Expected json object"); + } if (unsafe_ && endp) { - *endp = const_cast(data.data()) + len; + *endp = const_cast(data.data()) + len; } - } catch (gason::Exception &e) { + } catch (gason::Exception& e) { return Error(errParseJson, "Error parsing json: '%s', pos: %d", e.what(), len); } @@ -94,7 +96,7 @@ Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool /*pkOnly*/) { return err; } -Error ItemImpl::FromMsgPack(std::string_view buf, size_t &offset) { +Error ItemImpl::FromMsgPack(std::string_view buf, size_t& offset) { Payload pl = GetPayload(); MsgPackDecoder decoder(tagsMatcher_); @@ -107,7 +109,7 @@ Error ItemImpl::FromMsgPack(std::string_view buf, size_t &offset) { return err; } -void ItemImpl::FromCJSON(ItemImpl *other) { +void ItemImpl::FromCJSON(ItemImpl* other) { auto cjson = other->GetCJSON(); FromCJSON(cjson); } @@ -117,7 +119,7 @@ std::string_view ItemImpl::GetMsgPack() { ConstPayload pl = GetConstPayload(); MsgPackEncoder msgpackEncoder(&tagsMatcher_); - const TagsLengths &tagsLengths = msgpackEncoder.GetTagsMeasures(pl); + const TagsLengths& tagsLengths = msgpackEncoder.GetTagsMeasures(pl); ser_.Reset(); MsgPackBuilder msgpackBuilder(ser_, &tagsLengths, &startTag, ObjType::TypePlain, &tagsMatcher_); diff --git a/cpp_src/client/itemimpl.h b/cpp_src/client/itemimpl.h index fba2bfa1e..09e441b71 100644 --- a/cpp_src/client/itemimpl.h +++ b/cpp_src/client/itemimpl.h @@ -12,45 +12,45 @@ namespace client { class ItemImpl { public: // Construct empty item - ItemImpl(PayloadType type, const TagsMatcher &tagsMatcher) + ItemImpl(PayloadType type, const TagsMatcher& tagsMatcher) : payloadType_(std::move(type)), payloadValue_(payloadType_.TotalSize(), 0, payloadType_.TotalSize() + 0x100), tagsMatcher_(tagsMatcher) { tagsMatcher_.clearUpdated(); } - ItemImpl(PayloadType type, PayloadValue v, const TagsMatcher &tagsMatcher) + ItemImpl(PayloadType type, PayloadValue v, const TagsMatcher& tagsMatcher) : payloadType_(std::move(type)), payloadValue_(std::move(v)), tagsMatcher_(tagsMatcher) { tagsMatcher_.clearUpdated(); } - ItemImpl(const ItemImpl &) = delete; - ItemImpl(ItemImpl &&o) = default; - ItemImpl &operator=(const ItemImpl &) = delete; - ItemImpl &operator=(ItemImpl &&) noexcept; + ItemImpl(const ItemImpl&) = delete; + ItemImpl(ItemImpl&& o) = default; + ItemImpl& operator=(const ItemImpl&) = delete; + ItemImpl& operator=(ItemImpl&&) noexcept; - void SetField(int field, const VariantArray &krs); + void SetField(int field, const VariantArray& krs); Variant GetField(int field); std::string_view GetJSON(); - Error FromJSON(std::string_view slice, char **endp = nullptr, bool pkOnly = false); - void FromCJSON(ItemImpl *other); + Error FromJSON(std::string_view slice, char** endp = nullptr, bool pkOnly = false); + void FromCJSON(ItemImpl* other); std::string_view GetCJSON(); void FromCJSON(std::string_view slice); std::string_view GetMsgPack(); - Error FromMsgPack(std::string_view slice, size_t &offset); + Error FromMsgPack(std::string_view slice, size_t& offset); PayloadType Type() const noexcept { return payloadType_; } - PayloadValue &Value() noexcept { return payloadValue_; } + PayloadValue& Value() noexcept { return payloadValue_; } Payload GetPayload() noexcept { return Payload(payloadType_, payloadValue_); } ConstPayload GetConstPayload() const noexcept { return ConstPayload(payloadType_, payloadValue_); } - TagsMatcher &tagsMatcher() { return tagsMatcher_; } + TagsMatcher& tagsMatcher() { return tagsMatcher_; } - void SetPrecepts(const std::vector &precepts) { precepts_ = precepts; } - const std::vector &GetPrecepts() const noexcept { return precepts_; } + void SetPrecepts(const std::vector& precepts) { precepts_ = precepts; } + const std::vector& GetPrecepts() const noexcept { return precepts_; } void Unsafe(bool enable) noexcept { unsafe_ = enable; } protected: diff --git a/cpp_src/client/queryresults.cc b/cpp_src/client/queryresults.cc index 406a647cd..e2bf53833 100644 --- a/cpp_src/client/queryresults.cc +++ b/cpp_src/client/queryresults.cc @@ -12,7 +12,7 @@ using namespace reindexer::net; QueryResults::QueryResults(int fetchFlags) : conn_(nullptr), queryID_(0), fetchOffset_(0), fetchFlags_(fetchFlags), fetchAmount_(0), requestTimeout_(0) {} -QueryResults::QueryResults(QueryResults &&obj) noexcept +QueryResults::QueryResults(QueryResults&& obj) noexcept : conn_(obj.conn_), nsArray_(std::move(obj.nsArray_)), rawResult_(std::move(obj.rawResult_)), @@ -25,7 +25,7 @@ QueryResults::QueryResults(QueryResults &&obj) noexcept status_(std::move(obj.status_)), cmpl_(std::move(obj.cmpl_)) {} -QueryResults &QueryResults::operator=(QueryResults &&obj) noexcept { +QueryResults& QueryResults::operator=(QueryResults&& obj) noexcept { if (this != &obj) { rawResult_ = std::move(obj.rawResult_); conn_ = obj.conn_; @@ -42,7 +42,7 @@ QueryResults &QueryResults::operator=(QueryResults &&obj) noexcept { return *this; } -QueryResults::QueryResults(net::cproto::ClientConnection *conn, NsArray &&nsArray, Completion cmpl, int fetchFlags, int fetchAmount, +QueryResults::QueryResults(net::cproto::ClientConnection* conn, NsArray&& nsArray, Completion cmpl, int fetchFlags, int fetchAmount, seconds timeout) : conn_(conn), nsArray_(std::move(nsArray)), @@ -53,7 +53,7 @@ QueryResults::QueryResults(net::cproto::ClientConnection *conn, NsArray &&nsArra requestTimeout_(timeout), cmpl_(std::move(cmpl)) {} -QueryResults::QueryResults(net::cproto::ClientConnection *conn, NsArray &&nsArray, Completion cmpl, std::string_view rawResult, int queryID, +QueryResults::QueryResults(net::cproto::ClientConnection* conn, NsArray&& nsArray, Completion cmpl, std::string_view rawResult, int queryID, int fetchFlags, int fetchAmount, seconds timeout) : QueryResults(conn, std::move(nsArray), std::move(cmpl), fetchFlags, fetchAmount, timeout) { Bind(rawResult, queryID); @@ -85,7 +85,7 @@ void QueryResults::Bind(std::string_view rawResult, int queryID) { PayloadType("tmp").clone()->deserialize(ser); }, ResultSerializer::AggsFlag::ClearAggregations); - } catch (const Error &err) { + } catch (const Error& err) { status_ = err; } @@ -116,7 +116,9 @@ void QueryResults::fetchNextResults() { h_vector QueryResults::GetNamespaces() const { h_vector ret; ret.reserve(nsArray_.size()); - for (auto &ns : nsArray_) ret.push_back(ns->name_); + for (auto& ns : nsArray_) { + ret.push_back(ns->name_); + } return ret; } @@ -128,14 +130,14 @@ TagsMatcher QueryResults::getTagsMatcher(int nsid) const { class AdditionalRank : public IAdditionalDatasource { public: AdditionalRank(double r) noexcept : rank_(r) {} - void PutAdditionalFields(JsonBuilder &builder) const override final { builder.Put("rank()", rank_); } - IEncoderDatasourceWithJoins *GetJoinsDatasource() noexcept override final { return nullptr; } + void PutAdditionalFields(JsonBuilder& builder) const override final { builder.Put("rank()", rank_); } + IEncoderDatasourceWithJoins* GetJoinsDatasource() noexcept override final { return nullptr; } private: double rank_; }; -void QueryResults::Iterator::getJSONFromCJSON(std::string_view cjson, WrSerializer &wrser, bool withHdrLen) { +void QueryResults::Iterator::getJSONFromCJSON(std::string_view cjson, WrSerializer& wrser, bool withHdrLen) { auto tm = qr_->getTagsMatcher(itemParams_.nsid); JsonEncoder enc(&tm); JsonBuilder builder(wrser, ObjType::TypePlain); @@ -157,7 +159,7 @@ void QueryResults::Iterator::getJSONFromCJSON(std::string_view cjson, WrSerializ } } -Error QueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLen) { +Error QueryResults::Iterator::GetMsgPack(WrSerializer& wrser, bool withHdrLen) { readNext(); int type = qr_->queryParams_.flags & kResultsFormatMask; if (type == kResultsMsgPack) { @@ -172,7 +174,7 @@ Error QueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLen) { return errOK; } -Error QueryResults::Iterator::GetJSON(WrSerializer &wrser, bool withHdrLen) { +Error QueryResults::Iterator::GetJSON(WrSerializer& wrser, bool withHdrLen) { readNext(); try { switch (qr_->queryParams_.flags & kResultsFormatMask) { @@ -190,13 +192,13 @@ Error QueryResults::Iterator::GetJSON(WrSerializer &wrser, bool withHdrLen) { default: return Error(errParseBin, "Server returned data in unknown format %d", qr_->queryParams_.flags & kResultsFormatMask); } - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; } -Error QueryResults::Iterator::GetCJSON(WrSerializer &wrser, bool withHdrLen) { +Error QueryResults::Iterator::GetCJSON(WrSerializer& wrser, bool withHdrLen) { readNext(); try { switch (qr_->queryParams_.flags & kResultsFormatMask) { @@ -213,7 +215,7 @@ Error QueryResults::Iterator::GetCJSON(WrSerializer &wrser, bool withHdrLen) { default: return Error(errParseBin, "Server returned data in unknown format %d", qr_->queryParams_.flags & kResultsFormatMask); } - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; @@ -239,7 +241,7 @@ Item QueryResults::Iterator::GetItem() { break; } case kResultsJson: { - char *endp = nullptr; + char* endp = nullptr; err = item.FromJSON(itemParams_.data, &endp); break; } @@ -250,7 +252,7 @@ Item QueryResults::Iterator::GetItem() { return item; } return Item(); - } catch (const Error &) { + } catch (const Error&) { return Item(); } } @@ -272,7 +274,9 @@ std::string_view QueryResults::Iterator::GetRaw() { } void QueryResults::Iterator::readNext() { - if (nextPos_ != 0) return; + if (nextPos_ != 0) { + return; + } std::string_view rawResult(qr_->rawResult_.data(), qr_->rawResult_.size()); @@ -285,12 +289,12 @@ void QueryResults::Iterator::readNext() { (void)joinedCnt; } nextPos_ = pos_ + ser.Pos(); - } catch (const Error &err) { - const_cast(qr_)->status_ = err; + } catch (const Error& err) { + const_cast(qr_)->status_ = err; } } -QueryResults::Iterator &QueryResults::Iterator::operator++() { +QueryResults::Iterator& QueryResults::Iterator::operator++() { try { readNext(); idx_++; @@ -298,11 +302,11 @@ QueryResults::Iterator &QueryResults::Iterator::operator++() { nextPos_ = 0; if (idx_ != qr_->queryParams_.qcount && idx_ == qr_->queryParams_.count + qr_->fetchOffset_) { - const_cast(qr_)->fetchNextResults(); + const_cast(qr_)->fetchNextResults(); pos_ = 0; } - } catch (const Error &err) { - const_cast(qr_)->status_ = err; + } catch (const Error& err) { + const_cast(qr_)->status_ = err; } return *this; diff --git a/cpp_src/client/reindexer.cc b/cpp_src/client/reindexer.cc index a23dc0d8f..f69a35959 100644 --- a/cpp_src/client/reindexer.cc +++ b/cpp_src/client/reindexer.cc @@ -49,12 +49,8 @@ Error Reindexer::GetMeta(std::string_view nsName, const std::string& key, std::s Error Reindexer::PutMeta(std::string_view nsName, const std::string& key, std::string_view data) { return impl_->PutMeta(nsName, key, data, ctx_); } -Error Reindexer::EnumMeta(std::string_view nsName, std::vector& keys) { - return impl_->EnumMeta(nsName, keys, ctx_); -} -Error Reindexer::DeleteMeta(std::string_view nsName, const std::string& key) { - return impl_->DeleteMeta(nsName, key, ctx_); -} +Error Reindexer::EnumMeta(std::string_view nsName, std::vector& keys) { return impl_->EnumMeta(nsName, keys, ctx_); } +Error Reindexer::DeleteMeta(std::string_view nsName, const std::string& key) { return impl_->DeleteMeta(nsName, key, ctx_); } Error Reindexer::Delete(const Query& q, QueryResults& result) { return impl_->Delete(q, result, ctx_); } Error Reindexer::Select(std::string_view query, QueryResults& result) { return impl_->Select(query, result, ctx_, nullptr); } Error Reindexer::Select(const Query& q, QueryResults& result) { return impl_->Select(q, result, ctx_, nullptr); } diff --git a/cpp_src/client/reindexer.h b/cpp_src/client/reindexer.h index 7ec39e8e0..088f9db25 100644 --- a/cpp_src/client/reindexer.h +++ b/cpp_src/client/reindexer.h @@ -29,25 +29,25 @@ class RPCClient; class Reindexer { public: /// Completion routine - typedef std::function Completion; + typedef std::function Completion; /// Create Reindexer database object - Reindexer(const ReindexerConfig & = ReindexerConfig()); + Reindexer(const ReindexerConfig& = ReindexerConfig()); /// Destroy Reindexer database object ~Reindexer(); - Reindexer(const Reindexer &) = delete; - Reindexer(Reindexer &&) noexcept; - Reindexer &operator=(const Reindexer &) = delete; - Reindexer &operator=(Reindexer &&) noexcept; + Reindexer(const Reindexer&) = delete; + Reindexer(Reindexer&&) noexcept; + Reindexer& operator=(const Reindexer&) = delete; + Reindexer& operator=(Reindexer&&) noexcept; /// Connect - connect to reindexer server /// @param dsn - uri of server and database, like: `cproto://user@password:127.0.0.1:6534/dbname` or /// `ucproto://user@password:/tmp/reindexer.sock:/dbname` /// @param opts - Connect options. May contain any of
- Error Connect(const std::string &dsn, const client::ConnectOpts &opts = client::ConnectOpts()); + Error Connect(const std::string& dsn, const client::ConnectOpts& opts = client::ConnectOpts()); /// Connect - connect to reindexer server /// @param connectData - list of server dsn + it's ConnectOpts - Error Connect(const std::vector> &connectData); + Error Connect(const std::vector>& connectData); /// Stop - shutdown connector void Stop(); /// Open or create namespace @@ -56,10 +56,10 @@ class Reindexer { /// StorageOpts::Enabled() - Enable storage. If storage is disabled, then namespace will be completely in-memory
/// StorageOpts::CreateIfMissing () - Storage will be created, if missing /// @return errOK - On success - Error OpenNamespace(std::string_view nsName, const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); + Error OpenNamespace(std::string_view nsName, const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); /// Create new namespace. Will fail, if namespace already exists /// @param nsDef - NamespaceDef with namespace initial parameters - Error AddNamespace(const NamespaceDef &nsDef); + Error AddNamespace(const NamespaceDef& nsDef); /// Close namespace. Will free all memory resources, associated with namespace. Forces sync changes to disk /// @param nsName - Name of namespace Error CloseNamespace(std::string_view nsName); @@ -72,19 +72,19 @@ class Reindexer { /// Rename namespace. If namespace with dstNsName exists, then it is replaced. /// @param srcNsName - Name of namespace /// @param dstNsName - desired name of namespace - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName); /// Add index to namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error AddIndex(std::string_view nsName, const IndexDef &index); + Error AddIndex(std::string_view nsName, const IndexDef& index); /// Update index in namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error UpdateIndex(std::string_view nsName, const IndexDef &index); + Error UpdateIndex(std::string_view nsName, const IndexDef& index); /// Drop index from namespace /// @param nsName - Name of namespace /// @param index - index name - Error DropIndex(std::string_view nsName, const IndexDef &index); + Error DropIndex(std::string_view nsName, const IndexDef& index); /// Set fields schema for namespace /// @param nsName - Name of namespace /// @param schema - JSON in JsonSchema format @@ -92,50 +92,50 @@ class Reindexer { /// Get list of all available namespaces /// @param defs - std::vector of NamespaceDef of available namespaces /// @param opts - Enumeration options - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts); /// Gets a list of available databases for a certain server. /// @param dbList - list of DB names - Error EnumDatabases(std::vector &dbList); + Error EnumDatabases(std::vector& dbList); /// Insert new Item to namespace. If item with same PK is already exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Insert(std::string_view nsName, Item &item); + Error Insert(std::string_view nsName, Item& item); /// Update Item in namespace. If item with same PK is not exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Update(std::string_view nsName, Item &item); + Error Update(std::string_view nsName, Item& item); /// Update or Insert Item in namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Upsert(std::string_view nsName, Item &item); + Error Upsert(std::string_view nsName, Item& item); /// Updates all items in namespace, that satisfy provided query. /// @param query - Query to define items set for update. /// @param result - QueryResults with IDs of deleted items. - Error Update(const Query &query, QueryResults &result); + Error Update(const Query& query, QueryResults& result); /// Delete Item from namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Delete(std::string_view nsName, Item &item); + Error Delete(std::string_view nsName, Item& item); /// Delete all items from namespace, which matches provided Query /// @param query - Query with conditions /// @param result - QueryResults with IDs of deleted items - Error Delete(const Query &query, QueryResults &result); + Error Delete(const Query& query, QueryResults& result); /// Execute SQL Query and return results /// May be used with completion /// @param query - SQL query. Only "SELECT" semantic is supported /// @param result - QueryResults with found items - Error Select(std::string_view query, QueryResults &result); + Error Select(std::string_view query, QueryResults& result); /// Execute Query and return results /// May be used with completion /// @param query - Query object with query attributes /// @param result - QueryResults with found items - Error Select(const Query &query, QueryResults &result); + Error Select(const Query& query, QueryResults& result); /// Flush changes to storage /// @param nsName - Name of namespace Error Commit(std::string_view nsName); @@ -147,34 +147,34 @@ class Reindexer { /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - output string with metadata - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data); /// Put metadata to storage by key /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - string with metadata - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data); /// Get list of all metadata keys /// @param nsName - Name of namespace /// @param keys - std::vector filled with meta keys - Error EnumMeta(std::string_view nsName, std::vector &keys); + Error EnumMeta(std::string_view nsName, std::vector& keys); /// Delete metadata from storage by key /// @param nsName - Name of namespace /// @param key - string with meta key - Error DeleteMeta(std::string_view nsName, const std::string &key); + Error DeleteMeta(std::string_view nsName, const std::string& key); /// Subscribe to updates of database /// @param observer - Observer interface, which will receive updates /// @param filters - Subscription filters set /// @param opts - Subscription options (allows to either add new filters or reset them) - Error SubscribeUpdates(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts = SubscriptionOpts()); + Error SubscribeUpdates(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts = SubscriptionOpts()); /// Unsubscribe from updates of database /// Cancellation context doesn't affect this call /// @param observer - Observer interface, which will be unsubscribed updates - Error UnsubscribeUpdates(IUpdatesObserver *observer); + Error UnsubscribeUpdates(IUpdatesObserver* observer); /// Get possible suggestions for token (set by 'pos') in Sql query. /// @param sqlQuery - sql query. /// @param pos - position in sql query for suggestions. /// @param suggestions - all the suggestions for 'pos' position in query. - Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector &suggestions); + Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector& suggestions); /// Get current connection status Error Status(); /// Allocate new transaction for namespace @@ -182,14 +182,14 @@ class Reindexer { Transaction NewTransaction(std::string_view nsName); /// Commit transaction - transaction will be deleted after commit /// @param tr - transaction to commit - Error CommitTransaction(Transaction &tr); + Error CommitTransaction(Transaction& tr); /// RollBack transaction - transaction will be deleted after rollback /// @param tr - transaction to rollback - Error RollBackTransaction(Transaction &tr); + Error RollBackTransaction(Transaction& tr); /// Add cancelable context /// @param cancelCtx - context pointer - Reindexer WithContext(const IRdxCancelContext *cancelCtx) { return Reindexer(impl_, ctx_.WithCancelContext(cancelCtx)); } + Reindexer WithContext(const IRdxCancelContext* cancelCtx) { return Reindexer(impl_, ctx_.WithCancelContext(cancelCtx)); } /// Add execution timeout to the next query /// @param timeout - Optional server-side execution timeout for each subquery @@ -202,8 +202,8 @@ class Reindexer { typedef Item ItemT; private: - Reindexer(RPCClient *impl, InternalRdxContext &&ctx) : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} - RPCClient *impl_; + Reindexer(RPCClient* impl, InternalRdxContext&& ctx) : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} + RPCClient* impl_; bool owner_; InternalRdxContext ctx_; }; diff --git a/cpp_src/client/resultserializer.cc b/cpp_src/client/resultserializer.cc index 110ae75ea..9671281de 100644 --- a/cpp_src/client/resultserializer.cc +++ b/cpp_src/client/resultserializer.cc @@ -30,7 +30,9 @@ void ResultSerializer::GetRawQueryParams(ResultSerializer::QueryParams& ret, con bool firstAgg = true; for (;;) { int tag = GetVarUint(); - if (tag == QueryResultEnd) break; + if (tag == QueryResultEnd) { + break; + } if ((clearAggs == AggsFlag::DontClearAggregations) && firstAgg) { firstAgg = false; ret.aggResults.clear(); diff --git a/cpp_src/client/rpcclient.cc b/cpp_src/client/rpcclient.cc index 92fe71f7d..d7747af15 100644 --- a/cpp_src/client/rpcclient.cc +++ b/cpp_src/client/rpcclient.cc @@ -30,7 +30,9 @@ Error RPCClient::startWorkers() { connections_.resize(config_.ConnPoolSize); for (size_t i = 0; i < workers_.size(); i++) { workers_[i].thread_ = std::thread([this](size_t id) { this->run(id); }, i); - while (!workers_[i].running) std::this_thread::sleep_for(std::chrono::milliseconds(10)); + while (!workers_[i].running) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } } return errOK; } @@ -57,7 +59,9 @@ Error RPCClient::Connect(const std::string& dsn, const client::ConnectOpts& opts std::vector tmpConnectData(1); connectData_.entries.swap(tmpConnectData); Error err = addConnectEntry(dsn, opts, 0); - if (err.ok()) return startWorkers(); + if (err.ok()) { + return startWorkers(); + } return err; } @@ -72,13 +76,17 @@ Error RPCClient::Connect(const std::vectorCall(mkCommand(cproto::kCmdOpenNamespace, &ctx), ser.Slice()).Status(); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } std::unique_lock lock(nsMutex_); namespaces_.emplace(nsDef.name, Namespace::Ptr(new Namespace(nsDef.name))); @@ -166,7 +178,9 @@ Error RPCClient::TruncateNamespace(std::string_view nsName, const InternalRdxCon Error RPCClient::RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx) { auto status = getConn()->Call(mkCommand(cproto::kCmdRenameNamespace, &ctx), srcNsName, dstNsName).Status(); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } if (srcNsName != dstNsName) { std::unique_lock lock(nsMutex_); @@ -223,7 +237,9 @@ Error RPCClient::modifyItem(std::string_view nsName, Item& item, int mode, secon auto ret = conn->Call(mkCommand(cproto::kCmdModifyItem, netTimeout, &ctx), nsName, int(FormatCJson), item.GetCJSON(), mode, ser.Slice(), item.GetStateToken(), 0); if (!ret.Status().ok()) { - if (ret.Status().code() != errStateInvalidated || tryCount > 2) return ret.Status(); + if (ret.Status().code() != errStateInvalidated || tryCount > 2) { + return ret.Status(); + } if (withNetTimeout) { netTimeout = netDeadline - conn->Now(); } @@ -239,7 +255,9 @@ Error RPCClient::modifyItem(std::string_view nsName, Item& item, int mode, secon auto newItem = NewItem(nsName); char* endp = nullptr; Error err = newItem.FromJSON(item.impl_->GetJSON(), &endp); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } item = std::move(newItem); continue; @@ -265,14 +283,18 @@ Error RPCClient::modifyItemAsync(std::string_view nsName, Item* item, int mode, ser.PutVString(p); } } - if (!conn) conn = getConn(); + if (!conn) { + conn = getConn(); + } std::string ns(nsName); auto deadline = netTimeout.count() ? conn->Now() + netTimeout : seconds(0); conn->Call( [this, ns, mode, item, deadline, ctx](const net::cproto::RPCAnswer& ret, cproto::ClientConnection* conn) -> void { if (!ret.Status().ok()) { - if (ret.Status().code() != errStateInvalidated) return ctx.cmpl()(ret.Status()); + if (ret.Status().code() != errStateInvalidated) { + return ctx.cmpl()(ret.Status()); + } seconds netTimeout(0); if (deadline.count()) { netTimeout = deadline - conn->Now(); @@ -281,7 +303,9 @@ Error RPCClient::modifyItemAsync(std::string_view nsName, Item* item, int mode, QueryResults* qr = new QueryResults; InternalRdxContext ctxCmpl = ctx.WithCompletion([=](const Error& ret) { delete qr; - if (!ret.ok()) return ctx.cmpl()(ret); + if (!ret.ok()) { + return ctx.cmpl()(ret); + } seconds timeout(0); if (deadline.count()) { @@ -291,15 +315,21 @@ Error RPCClient::modifyItemAsync(std::string_view nsName, Item* item, int mode, // Rebuild item with new state auto newItem = NewItem(ns); Error err = newItem.FromJSON(item->impl_->GetJSON()); - if (err.ok()) return ctx.cmpl()(err); + if (err.ok()) { + return ctx.cmpl()(err); + } newItem.SetPrecepts(item->impl_->GetPrecepts()); *item = std::move(newItem); err = modifyItemAsync(ns, item, mode, conn, timeout, ctx); - if (err.ok()) return ctx.cmpl()(err); + if (err.ok()) { + return ctx.cmpl()(err); + } }); auto err = selectImpl(Query(ns).Limit(0), *qr, conn, netTimeout, ctxCmpl); - if (err.ok()) return ctx.cmpl()(err); - } else + if (err.ok()) { + return ctx.cmpl()(err); + } + } else { try { auto args = ret.GetArgs(2); ctx.cmpl()(QueryResults(conn, {getNamespace(ns)}, nullptr, p_string(args[0]), int(args[1]), 0, config_.FetchAmount, @@ -308,6 +338,7 @@ Error RPCClient::modifyItemAsync(std::string_view nsName, Item* item, int mode, } catch (const Error& err) { ctx.cmpl()(err); } + } }, mkCommand(cproto::kCmdModifyItem, netTimeout, &ctx), ns, int(FormatCJson), item->GetCJSON(), mode, ser.Slice(), item->GetStateToken(), 0); @@ -447,7 +478,9 @@ Error RPCClient::selectImpl(std::string_view query, QueryResults& result, cproto h_vector vers; vec2pack(vers, pser); - if (!conn) conn = getConn(); + if (!conn) { + conn = getConn(); + } result = QueryResults(conn, {}, ctx.cmpl(), result.fetchFlags_, config_.FetchAmount, config_.RequestTimeout); @@ -501,7 +534,9 @@ Error RPCClient::selectImpl(const Query& query, QueryResults& result, cproto::Cl } vec2pack(vers, pser); - if (!conn) conn = getConn(); + if (!conn) { + conn = getConn(); + } result = QueryResults(conn, std::move(nsArray), ctx.cmpl(), result.fetchFlags_, config_.FetchAmount, config_.RequestTimeout); @@ -610,7 +645,9 @@ Error RPCClient::GetSqlSuggestions(std::string_view query, int pos, std::vector< suggests.clear(); suggests.reserve(rargs.size()); - for (auto& rarg : rargs) suggests.push_back(rarg.As()); + for (auto& rarg : rargs) { + suggests.push_back(rarg.As()); + } } return ret.Status(); } catch (const Error& err) { @@ -709,7 +746,9 @@ void RPCClient::onUpdates(net::cproto::RPCAnswer& ans, cproto::ClientConnection* std::string_view nsName(args[1]); std::string_view pwalRec(args[2]); lsn_t originLSN; - if (args.size() >= 4) originLSN = lsn_t(args[3].As()); + if (args.size() >= 4) { + originLSN = lsn_t(args[3].As()); + } WALRecord wrec(pwalRec); if (wrec.type == WalItemModify) { @@ -759,7 +798,9 @@ void RPCClient::onUpdates(net::cproto::RPCAnswer& ans, cproto::ClientConnection* serialDelays_ = 0; observers_.OnUpdatesLost(nsName); } else { - for (auto& a1 : uq) onUpdates(a1, conn); + for (auto& a1 : uq) { + onUpdates(a1, conn); + } } }), conn); @@ -799,8 +840,12 @@ void RPCClient::onUpdates(net::cproto::RPCAnswer& ans, cproto::ClientConnection* } bool RPCClient::onConnectionFail(int failedDsnIndex) { - if (!connectData_.ThereAreReconnectOptions()) return false; - if (!connectData_.CurrDsnFailed(failedDsnIndex)) return false; + if (!connectData_.ThereAreReconnectOptions()) { + return false; + } + if (!connectData_.CurrDsnFailed(failedDsnIndex)) { + return false; + } connectData_.lastFailedEntryIdx = failedDsnIndex; connectData_.validEntryIdx.store(connectData_.GetNextDsnIndex(), std::memory_order_release); diff --git a/cpp_src/client/rpcclient.h b/cpp_src/client/rpcclient.h index 349cba590..748a2c2bc 100644 --- a/cpp_src/client/rpcclient.h +++ b/cpp_src/client/rpcclient.h @@ -29,57 +29,57 @@ using std::chrono::seconds; using namespace net; class RPCClient { public: - typedef std::function Completion; - RPCClient(const ReindexerConfig &config); - RPCClient(const RPCClient &) = delete; - RPCClient(RPCClient &&) = delete; - RPCClient &operator=(const RPCClient &) = delete; - RPCClient &operator=(RPCClient &&) = delete; + typedef std::function Completion; + RPCClient(const ReindexerConfig& config); + RPCClient(const RPCClient&) = delete; + RPCClient(RPCClient&&) = delete; + RPCClient& operator=(const RPCClient&) = delete; + RPCClient& operator=(RPCClient&&) = delete; ~RPCClient(); - Error Connect(const std::string &dsn, const client::ConnectOpts &opts); - Error Connect(const std::vector> &connectData); + Error Connect(const std::string& dsn, const client::ConnectOpts& opts); + Error Connect(const std::vector>& connectData); void Stop(); - Error OpenNamespace(std::string_view nsName, const InternalRdxContext &ctx, - const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); - Error AddNamespace(const NamespaceDef &nsDef, const InternalRdxContext &ctx); - Error CloseNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error DropNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error TruncateNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName, const InternalRdxContext &ctx); - Error AddIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error UpdateIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error DropIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext &ctx); - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts, const InternalRdxContext &ctx); - Error EnumDatabases(std::vector &dbList, const InternalRdxContext &ctx); - Error Insert(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Update(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Upsert(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Delete(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx); - Error Delete(const Query &query, QueryResults &result, const InternalRdxContext &ctx); - Error Update(const Query &query, QueryResults &result, const InternalRdxContext &ctx); - Error Select(std::string_view query, QueryResults &result, const InternalRdxContext &ctx, cproto::ClientConnection *conn = nullptr) { + Error OpenNamespace(std::string_view nsName, const InternalRdxContext& ctx, + const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); + Error AddNamespace(const NamespaceDef& nsDef, const InternalRdxContext& ctx); + Error CloseNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error DropNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error TruncateNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx); + Error AddIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error UpdateIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error DropIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext& ctx); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts, const InternalRdxContext& ctx); + Error EnumDatabases(std::vector& dbList, const InternalRdxContext& ctx); + Error Insert(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Update(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Upsert(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Delete(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx); + Error Delete(const Query& query, QueryResults& result, const InternalRdxContext& ctx); + Error Update(const Query& query, QueryResults& result, const InternalRdxContext& ctx); + Error Select(std::string_view query, QueryResults& result, const InternalRdxContext& ctx, cproto::ClientConnection* conn = nullptr) { return selectImpl(query, result, conn, config_.RequestTimeout, ctx); } - Error Select(const Query &query, QueryResults &result, const InternalRdxContext &ctx, cproto::ClientConnection *conn = nullptr) { + Error Select(const Query& query, QueryResults& result, const InternalRdxContext& ctx, cproto::ClientConnection* conn = nullptr) { return selectImpl(query, result, conn, config_.RequestTimeout, ctx); } Error Commit(std::string_view nsName); Item NewItem(std::string_view nsName); - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data, const InternalRdxContext &ctx); - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data, const InternalRdxContext &ctx); - Error EnumMeta(std::string_view nsName, std::vector &keys, const InternalRdxContext &ctx); - Error DeleteMeta(std::string_view nsName, const std::string &key, const InternalRdxContext &ctx); - Error SubscribeUpdates(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts = SubscriptionOpts()); - Error UnsubscribeUpdates(IUpdatesObserver *observer); - Error GetSqlSuggestions(std::string_view query, int pos, std::vector &suggests); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data, const InternalRdxContext& ctx); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data, const InternalRdxContext& ctx); + Error EnumMeta(std::string_view nsName, std::vector& keys, const InternalRdxContext& ctx); + Error DeleteMeta(std::string_view nsName, const std::string& key, const InternalRdxContext& ctx); + Error SubscribeUpdates(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts = SubscriptionOpts()); + Error UnsubscribeUpdates(IUpdatesObserver* observer); + Error GetSqlSuggestions(std::string_view query, int pos, std::vector& suggests); Error Status(); - Transaction NewTransaction(std::string_view nsName, const InternalRdxContext &ctx); - Error CommitTransaction(Transaction &tr, const InternalRdxContext &ctx); - Error RollBackTransaction(Transaction &tr, const InternalRdxContext &ctx); + Transaction NewTransaction(std::string_view nsName, const InternalRdxContext& ctx); + Error CommitTransaction(Transaction& tr, const InternalRdxContext& ctx); + Error RollBackTransaction(Transaction& tr, const InternalRdxContext& ctx); protected: struct worker { @@ -90,26 +90,26 @@ class RPCClient { std::atomic_bool running; }; - Error selectImpl(std::string_view query, QueryResults &result, cproto::ClientConnection *, seconds netTimeout, - const InternalRdxContext &ctx); - Error selectImpl(const Query &query, QueryResults &result, cproto::ClientConnection *, seconds netTimeout, - const InternalRdxContext &ctx); - Error modifyItem(std::string_view nsName, Item &item, int mode, seconds netTimeout, const InternalRdxContext &ctx); - Error modifyItemAsync(std::string_view nsName, Item *item, int mode, cproto::ClientConnection *, seconds netTimeout, - const InternalRdxContext &ctx); + Error selectImpl(std::string_view query, QueryResults& result, cproto::ClientConnection*, seconds netTimeout, + const InternalRdxContext& ctx); + Error selectImpl(const Query& query, QueryResults& result, cproto::ClientConnection*, seconds netTimeout, + const InternalRdxContext& ctx); + Error modifyItem(std::string_view nsName, Item& item, int mode, seconds netTimeout, const InternalRdxContext& ctx); + Error modifyItemAsync(std::string_view nsName, Item* item, int mode, cproto::ClientConnection*, seconds netTimeout, + const InternalRdxContext& ctx); Error subscribeImpl(bool subscribe); - Namespace *getNamespace(std::string_view nsName); + Namespace* getNamespace(std::string_view nsName); Error startWorkers(); - Error addConnectEntry(const std::string &dsn, const client::ConnectOpts &opts, size_t idx); + Error addConnectEntry(const std::string& dsn, const client::ConnectOpts& opts, size_t idx); void run(size_t thIdx); - void onUpdates(net::cproto::RPCAnswer &ans, cproto::ClientConnection *conn); + void onUpdates(net::cproto::RPCAnswer& ans, cproto::ClientConnection* conn); bool onConnectionFail(int failedDsnIndex); void checkSubscribes(); - net::cproto::ClientConnection *getConn(); - cproto::CommandParams mkCommand(cproto::CmdCode cmd, const InternalRdxContext *ctx = nullptr) const noexcept; - static cproto::CommandParams mkCommand(cproto::CmdCode cmd, seconds reqTimeout, const InternalRdxContext *ctx) noexcept; + net::cproto::ClientConnection* getConn(); + cproto::CommandParams mkCommand(cproto::CmdCode cmd, const InternalRdxContext* ctx = nullptr) const noexcept; + static cproto::CommandParams mkCommand(cproto::CmdCode cmd, seconds reqTimeout, const InternalRdxContext* ctx) noexcept; std::vector> connections_; @@ -120,13 +120,13 @@ class RPCClient { std::atomic curConnIdx_; ReindexerConfig config_; UpdatesObservers observers_; - std::atomic updatesConn_; + std::atomic updatesConn_; std::vector delayedUpdates_; uint64_t serialDelays_ = 0; cproto::ClientConnection::ConnectData connectData_; }; -void vec2pack(const h_vector &vec, WrSerializer &ser); +void vec2pack(const h_vector& vec, WrSerializer& ser); } // namespace client } // namespace reindexer diff --git a/cpp_src/client/rpcclientmock.cc b/cpp_src/client/rpcclientmock.cc index 275414551..7b6bd9e58 100644 --- a/cpp_src/client/rpcclientmock.cc +++ b/cpp_src/client/rpcclientmock.cc @@ -54,7 +54,9 @@ Error RPCClientMock::Delete(const Query& query, QueryResults& result, const Inte }; int flags = kResultsWithItemID; - if (outputFormat == FormatMsgPack) flags |= kResultsMsgPack; + if (outputFormat == FormatMsgPack) { + flags |= kResultsMsgPack; + } auto ret = conn->Call(mkCommand(cproto::kCmdDeleteQuery, &ctx), ser.Slice(), flags); icompl(ret, conn); return ret.Status(); @@ -132,7 +134,9 @@ Error RPCClientMock::modifyItem(std::string_view nsName, Item& item, int mode, s auto ret = conn->Call(mkCommand(cproto::kCmdModifyItem, netTimeout, &ctx), nsName, format, data, mode, ser.Slice(), item.GetStateToken(), 0); if (!ret.Status().ok()) { - if (ret.Status().code() != errStateInvalidated || tryCount > 2) return ret.Status(); + if (ret.Status().code() != errStateInvalidated || tryCount > 2) { + return ret.Status(); + } if (withNetTimeout) { netTimeout = netDeadline - conn->Now(); } @@ -148,7 +152,9 @@ Error RPCClientMock::modifyItem(std::string_view nsName, Item& item, int mode, s auto newItem = NewItem(nsName); char* endp = nullptr; Error err = newItem.FromJSON(item.impl_->GetJSON(), &endp); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } item = std::move(newItem); continue; @@ -174,7 +180,9 @@ Error RPCClientMock::modifyItemAsync(std::string_view nsName, Item* item, int mo ser.PutVString(p); } } - if (!conn) conn = getConn(); + if (!conn) { + conn = getConn(); + } std::string_view data; switch (format) { @@ -196,7 +204,9 @@ Error RPCClientMock::modifyItemAsync(std::string_view nsName, Item* item, int mo conn->Call( [this, ns, mode, item, deadline, ctx, format](const net::cproto::RPCAnswer& ret, cproto::ClientConnection* conn) -> void { if (!ret.Status().ok()) { - if (ret.Status().code() != errStateInvalidated) return ctx.cmpl()(ret.Status()); + if (ret.Status().code() != errStateInvalidated) { + return ctx.cmpl()(ret.Status()); + } seconds netTimeout(0); if (deadline.count()) { netTimeout = deadline - conn->Now(); @@ -205,7 +215,9 @@ Error RPCClientMock::modifyItemAsync(std::string_view nsName, Item* item, int mo QueryResults* qr = new QueryResults; InternalRdxContext ctxCmpl = ctx.WithCompletion([=](const Error& ret) { delete qr; - if (!ret.ok()) return ctx.cmpl()(ret); + if (!ret.ok()) { + return ctx.cmpl()(ret); + } seconds timeout(0); if (deadline.count()) { @@ -215,15 +227,21 @@ Error RPCClientMock::modifyItemAsync(std::string_view nsName, Item* item, int mo // Rebuild item with new state auto newItem = NewItem(ns); Error err = newItem.FromJSON(item->impl_->GetJSON()); - if (!err.ok()) return ctx.cmpl()(ret); + if (!err.ok()) { + return ctx.cmpl()(ret); + } newItem.SetPrecepts(item->impl_->GetPrecepts()); *item = std::move(newItem); err = modifyItemAsync(ns, item, mode, conn, timeout, ctx, format); - if (!err.ok()) return ctx.cmpl()(ret); + if (!err.ok()) { + return ctx.cmpl()(ret); + } }); auto err = selectImpl(Query(ns).Limit(0), *qr, conn, netTimeout, ctxCmpl, format); - if (err.ok()) return ctx.cmpl()(err); - } else + if (err.ok()) { + return ctx.cmpl()(err); + } + } else { try { auto args = ret.GetArgs(2); ctx.cmpl()(QueryResults(conn, {getNamespace(ns)}, nullptr, p_string(args[0]), int(args[1]), 0, config_.FetchAmount, @@ -232,6 +250,7 @@ Error RPCClientMock::modifyItemAsync(std::string_view nsName, Item* item, int mo } catch (const Error& err) { ctx.cmpl()(err); } + } }, mkCommand(cproto::kCmdModifyItem, netTimeout, &ctx), ns, format, data, mode, ser.Slice(), item->GetStateToken(), 0); return errOK; @@ -250,7 +269,9 @@ Error RPCClientMock::selectImpl(std::string_view query, QueryResults& result, cp h_vector vers; vec2pack(vers, pser); - if (!conn) conn = getConn(); + if (!conn) { + conn = getConn(); + } result = QueryResults(conn, {}, ctx.cmpl(), result.fetchFlags_, config_.FetchAmount, config_.RequestTimeout); @@ -316,7 +337,9 @@ Error RPCClientMock::selectImpl(const Query& query, QueryResults& result, cproto } vec2pack(vers, pser); - if (!conn) conn = getConn(); + if (!conn) { + conn = getConn(); + } result = QueryResults(conn, std::move(nsArray), ctx.cmpl(), result.fetchFlags_, config_.FetchAmount, config_.RequestTimeout); diff --git a/cpp_src/client/rpcclientmock.h b/cpp_src/client/rpcclientmock.h index b3c72f192..1a11cc485 100644 --- a/cpp_src/client/rpcclientmock.h +++ b/cpp_src/client/rpcclientmock.h @@ -8,30 +8,30 @@ namespace client { class RPCClientMock : public RPCClient { public: - RPCClientMock(const ReindexerConfig &config = ReindexerConfig()); - Error Insert(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx, int outputFormat = FormatCJson); - Error Update(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx, int outputFormat = FormatCJson); - Error Upsert(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx, int outputFormat = FormatCJson); - Error Delete(std::string_view nsName, client::Item &item, const InternalRdxContext &ctx, int outputFormat = FormatCJson); - Error Delete(const Query &query, QueryResults &result, const InternalRdxContext &ctx, int outputFormat = FormatCJson); - Error Update(const Query &query, QueryResults &result, const InternalRdxContext &ctx, int outputFormat = FormatCJson); - Error Select(std::string_view query, QueryResults &result, const InternalRdxContext &ctx, cproto::ClientConnection *conn = nullptr, + RPCClientMock(const ReindexerConfig& config = ReindexerConfig()); + Error Insert(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx, int outputFormat = FormatCJson); + Error Update(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx, int outputFormat = FormatCJson); + Error Upsert(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx, int outputFormat = FormatCJson); + Error Delete(std::string_view nsName, client::Item& item, const InternalRdxContext& ctx, int outputFormat = FormatCJson); + Error Delete(const Query& query, QueryResults& result, const InternalRdxContext& ctx, int outputFormat = FormatCJson); + Error Update(const Query& query, QueryResults& result, const InternalRdxContext& ctx, int outputFormat = FormatCJson); + Error Select(std::string_view query, QueryResults& result, const InternalRdxContext& ctx, cproto::ClientConnection* conn = nullptr, int outputFormat = FormatCJson) { return selectImpl(query, result, conn, config_.RequestTimeout, ctx, outputFormat); } - Error Select(const Query &query, QueryResults &result, const InternalRdxContext &ctx, cproto::ClientConnection *conn = nullptr, + Error Select(const Query& query, QueryResults& result, const InternalRdxContext& ctx, cproto::ClientConnection* conn = nullptr, int outputFormat = FormatCJson) { return selectImpl(query, result, conn, config_.RequestTimeout, ctx, outputFormat); } private: - Error selectImpl(std::string_view query, QueryResults &result, cproto::ClientConnection *, seconds netTimeout, - const InternalRdxContext &ctx, int outputFormat); - Error selectImpl(const Query &query, QueryResults &result, cproto::ClientConnection *, seconds netTimeout, - const InternalRdxContext &ctx, int outputFormat); - Error modifyItem(std::string_view nsName, Item &item, int mode, seconds netTimeout, const InternalRdxContext &ctx, int format); - Error modifyItemAsync(std::string_view nsName, Item *item, int mode, cproto::ClientConnection *, seconds netTimeout, - const InternalRdxContext &ctx, int format); + Error selectImpl(std::string_view query, QueryResults& result, cproto::ClientConnection*, seconds netTimeout, + const InternalRdxContext& ctx, int outputFormat); + Error selectImpl(const Query& query, QueryResults& result, cproto::ClientConnection*, seconds netTimeout, const InternalRdxContext& ctx, + int outputFormat); + Error modifyItem(std::string_view nsName, Item& item, int mode, seconds netTimeout, const InternalRdxContext& ctx, int format); + Error modifyItemAsync(std::string_view nsName, Item* item, int mode, cproto::ClientConnection*, seconds netTimeout, + const InternalRdxContext& ctx, int format); }; } // namespace client diff --git a/cpp_src/client/synccoroqueryresults.cc b/cpp_src/client/synccoroqueryresults.cc index 14507bccc..8f573b562 100644 --- a/cpp_src/client/synccoroqueryresults.cc +++ b/cpp_src/client/synccoroqueryresults.cc @@ -8,7 +8,7 @@ namespace client { using namespace reindexer::net; -SyncCoroQueryResults::SyncCoroQueryResults(SyncCoroReindexer *rx, int fetchFlags) : results_(fetchFlags), rx_(rx) {} +SyncCoroQueryResults::SyncCoroQueryResults(SyncCoroReindexer* rx, int fetchFlags) : results_(fetchFlags), rx_(rx) {} void SyncCoroQueryResults::Bind(std::string_view rawResult, RPCQrId id) { results_.Bind(rawResult, id); } diff --git a/cpp_src/client/synccororeindexer.cc b/cpp_src/client/synccororeindexer.cc index 14f0c4343..630d49ade 100644 --- a/cpp_src/client/synccororeindexer.cc +++ b/cpp_src/client/synccororeindexer.cc @@ -47,12 +47,8 @@ Error SyncCoroReindexer::GetMeta(std::string_view nsName, const std::string& key Error SyncCoroReindexer::PutMeta(std::string_view nsName, const std::string& key, std::string_view data) { return impl_->PutMeta(nsName, key, data, ctx_); } -Error SyncCoroReindexer::EnumMeta(std::string_view nsName, std::vector& keys) { - return impl_->EnumMeta(nsName, keys, ctx_); -} -Error SyncCoroReindexer::DeleteMeta(std::string_view nsName, const std::string& key) { - return impl_->DeleteMeta(nsName, key, ctx_); -} +Error SyncCoroReindexer::EnumMeta(std::string_view nsName, std::vector& keys) { return impl_->EnumMeta(nsName, keys, ctx_); } +Error SyncCoroReindexer::DeleteMeta(std::string_view nsName, const std::string& key) { return impl_->DeleteMeta(nsName, key, ctx_); } Error SyncCoroReindexer::Delete(const Query& q, SyncCoroQueryResults& result) { return impl_->Delete(q, result, ctx_); } Error SyncCoroReindexer::Select(std::string_view query, SyncCoroQueryResults& result) { return impl_->Select(query, result, ctx_); } Error SyncCoroReindexer::Select(const Query& q, SyncCoroQueryResults& result) { return impl_->Select(q, result, ctx_); } diff --git a/cpp_src/client/synccororeindexer.h b/cpp_src/client/synccororeindexer.h index 7638dbb19..20c989c83 100644 --- a/cpp_src/client/synccororeindexer.h +++ b/cpp_src/client/synccororeindexer.h @@ -15,19 +15,19 @@ class SyncCoroReindexerImpl; class SyncCoroReindexer { public: /// Create Reindexer database object - SyncCoroReindexer(const ReindexerConfig & = ReindexerConfig()); - SyncCoroReindexer(SyncCoroReindexer &&rdx) noexcept; + SyncCoroReindexer(const ReindexerConfig& = ReindexerConfig()); + SyncCoroReindexer(SyncCoroReindexer&& rdx) noexcept; /// Destroy Reindexer database object ~SyncCoroReindexer(); - SyncCoroReindexer(const SyncCoroReindexer &) = delete; - SyncCoroReindexer &operator=(const SyncCoroReindexer &) = delete; - SyncCoroReindexer &operator=(SyncCoroReindexer &&rdx) noexcept; + SyncCoroReindexer(const SyncCoroReindexer&) = delete; + SyncCoroReindexer& operator=(const SyncCoroReindexer&) = delete; + SyncCoroReindexer& operator=(SyncCoroReindexer&& rdx) noexcept; /// Connect - connect to reindexer server /// @param dsn - uri of server and database, like: `cproto://user@password:127.0.0.1:6534/dbname` or /// `ucproto://user@password:/tmp/reindexer.sock:/dbname` /// @param opts - Connect options. May contain any of
- Error Connect(const std::string &dsn, const client::ConnectOpts &opts = client::ConnectOpts()); + Error Connect(const std::string& dsn, const client::ConnectOpts& opts = client::ConnectOpts()); /// Stop - shutdown connector Error Stop(); /// Open or create namespace @@ -36,10 +36,10 @@ class SyncCoroReindexer { /// StorageOpts::Enabled() - Enable storage. If storage is disabled, then namespace will be completely in-memory
/// StorageOpts::CreateIfMissing () - Storage will be created, if missing /// @return errOK - On success - Error OpenNamespace(std::string_view nsName, const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); + Error OpenNamespace(std::string_view nsName, const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); /// Create new namespace. Will fail, if namespace already exists /// @param nsDef - NamespaceDef with namespace initial parameters - Error AddNamespace(const NamespaceDef &nsDef); + Error AddNamespace(const NamespaceDef& nsDef); /// Close namespace. Will free all memory resources, associated with namespace. Forces sync changes to disk /// @param nsName - Name of namespace Error CloseNamespace(std::string_view nsName); @@ -52,19 +52,19 @@ class SyncCoroReindexer { /// Rename namespace. If namespace with dstNsName exists, then it is replaced. /// @param srcNsName - Name of namespace /// @param dstNsName - desired name of namespace - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName); /// Add index to namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error AddIndex(std::string_view nsName, const IndexDef &index); + Error AddIndex(std::string_view nsName, const IndexDef& index); /// Update index in namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error UpdateIndex(std::string_view nsName, const IndexDef &index); + Error UpdateIndex(std::string_view nsName, const IndexDef& index); /// Drop index from namespace /// @param nsName - Name of namespace /// @param index - index name - Error DropIndex(std::string_view nsName, const IndexDef &index); + Error DropIndex(std::string_view nsName, const IndexDef& index); /// Set fields schema for namespace /// @param nsName - Name of namespace /// @param schema - JSON in JsonSchema format @@ -72,50 +72,50 @@ class SyncCoroReindexer { /// Get list of all available namespaces /// @param defs - std::vector of NamespaceDef of available namespaces /// @param opts - Enumeration options - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts); /// Gets a list of available databases for a certain server. /// @param dbList - list of DB names - Error EnumDatabases(std::vector &dbList); + Error EnumDatabases(std::vector& dbList); /// Insert new Item to namespace. If item with same PK is already exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Insert(std::string_view nsName, Item &item); + Error Insert(std::string_view nsName, Item& item); /// Update Item in namespace. If item with same PK is not exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Update(std::string_view nsName, Item &item); + Error Update(std::string_view nsName, Item& item); /// Update or Insert Item in namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Upsert(std::string_view nsName, Item &item); + Error Upsert(std::string_view nsName, Item& item); /// Updates all items in namespace, that satisfy provided query. /// @param query - Query to define items set for update. /// @param result - QueryResults with IDs of deleted items. - Error Update(const Query &query, SyncCoroQueryResults &result); + Error Update(const Query& query, SyncCoroQueryResults& result); /// Delete Item from namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Delete(std::string_view nsName, Item &item); + Error Delete(std::string_view nsName, Item& item); /// Delete all items from namespace, which matches provided Query /// @param query - Query with conditions /// @param result - QueryResults with IDs of deleted items - Error Delete(const Query &query, SyncCoroQueryResults &result); + Error Delete(const Query& query, SyncCoroQueryResults& result); /// Execute SQL Query and return results /// May be used with completion /// @param query - SQL query. Only "SELECT" semantic is supported /// @param result - QueryResults with found items - Error Select(std::string_view query, SyncCoroQueryResults &result); + Error Select(std::string_view query, SyncCoroQueryResults& result); /// Execute SQL Query and return results /// May be used with completion /// @param query - Query object with query attributes /// @param result - QueryResults with found items - Error Select(const Query &query, SyncCoroQueryResults &result); + Error Select(const Query& query, SyncCoroQueryResults& result); /// Flush changes to storage /// @param nsName - Name of namespace @@ -128,25 +128,25 @@ class SyncCoroReindexer { /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - output string with metadata - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data); /// Put metadata to storage by key /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - string with metadata - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data); /// Get list of all metadata keys /// @param nsName - Name of namespace /// @param keys - std::vector filled with meta keys - Error EnumMeta(std::string_view nsName, std::vector &keys); + Error EnumMeta(std::string_view nsName, std::vector& keys); /// Delete metadata from storage by key /// @param nsName - Name of namespace /// @param key - string with meta key - Error DeleteMeta(std::string_view nsName, const std::string &key); + Error DeleteMeta(std::string_view nsName, const std::string& key); /// Get possible suggestions for token (set by 'pos') in Sql query. /// @param sqlQuery - sql query. /// @param pos - position in sql query for suggestions. /// @param suggestions - all the suggestions for 'pos' position in query. - Error GetSqlSuggestions(const std::string_view sqlQuery, int pos, std::vector &suggestions); + Error GetSqlSuggestions(const std::string_view sqlQuery, int pos, std::vector& suggestions); /// Get current connection status Error Status(); /// Allocate new transaction for namespace @@ -154,14 +154,14 @@ class SyncCoroReindexer { SyncCoroTransaction NewTransaction(std::string_view nsName); /// Commit transaction - transaction will be deleted after commit /// @param tr - transaction to commit - Error CommitTransaction(SyncCoroTransaction &tr); + Error CommitTransaction(SyncCoroTransaction& tr); /// RollBack transaction - transaction will be deleted after rollback /// @param tr - transaction to rollback - Error RollBackTransaction(SyncCoroTransaction &tr); + Error RollBackTransaction(SyncCoroTransaction& tr); /// Add cancelable context /// @param cancelCtx - context pointer - SyncCoroReindexer WithContext(const IRdxCancelContext *cancelCtx) { + SyncCoroReindexer WithContext(const IRdxCancelContext* cancelCtx) { return SyncCoroReindexer(impl_, ctx_.WithCancelContext(cancelCtx)); } @@ -174,8 +174,8 @@ class SyncCoroReindexer { private: friend SyncCoroQueryResults; - SyncCoroReindexer(SyncCoroReindexerImpl *impl, InternalRdxContext &&ctx) : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} - SyncCoroReindexerImpl *impl_; + SyncCoroReindexer(SyncCoroReindexerImpl* impl, InternalRdxContext&& ctx) : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} + SyncCoroReindexerImpl* impl_; bool owner_; InternalRdxContext ctx_; }; diff --git a/cpp_src/client/synccororeindexerimpl.cc b/cpp_src/client/synccororeindexerimpl.cc index 1aadb43d7..2d44e27cd 100644 --- a/cpp_src/client/synccororeindexerimpl.cc +++ b/cpp_src/client/synccororeindexerimpl.cc @@ -6,7 +6,7 @@ namespace client { using std::chrono::milliseconds; -SyncCoroReindexerImpl::SyncCoroReindexerImpl(const ReindexerConfig &conf) : conf_(conf) {} +SyncCoroReindexerImpl::SyncCoroReindexerImpl(const ReindexerConfig& conf) : conf_(conf) {} SyncCoroReindexerImpl::~SyncCoroReindexerImpl() { if (loopThread_->joinable()) { @@ -15,9 +15,11 @@ SyncCoroReindexerImpl::~SyncCoroReindexerImpl() { } } -Error SyncCoroReindexerImpl::Connect(const std::string &dsn, const client::ConnectOpts &opts) { +Error SyncCoroReindexerImpl::Connect(const std::string& dsn, const client::ConnectOpts& opts) { std::unique_lock lock(loopThreadMtx_); - if (loopThread_) return Error(errLogic, "Client is already started"); + if (loopThread_) { + return Error(errLogic, "Client is already started"); + } std::promise isRunningPromise; auto isRunningFuture = isRunningPromise.get_future(); @@ -36,64 +38,64 @@ Error SyncCoroReindexerImpl::Stop() { } return errOK; } -Error SyncCoroReindexerImpl::OpenNamespace(std::string_view nsName, const InternalRdxContext &ctx, const StorageOpts &opts) { +Error SyncCoroReindexerImpl::OpenNamespace(std::string_view nsName, const InternalRdxContext& ctx, const StorageOpts& opts) { return sendCommand(DbCmdOpenNamespace, std::forward(nsName), ctx, opts); } -Error SyncCoroReindexerImpl::AddNamespace(const NamespaceDef &nsDef, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::AddNamespace(const NamespaceDef& nsDef, const InternalRdxContext& ctx) { return sendCommand(DbCmdAddNamespace, nsDef, ctx); } -Error SyncCoroReindexerImpl::CloseNamespace(std::string_view nsName, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::CloseNamespace(std::string_view nsName, const InternalRdxContext& ctx) { return sendCommand(DbCmdCloseNamespace, std::forward(nsName), ctx); } -Error SyncCoroReindexerImpl::DropNamespace(std::string_view nsName, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::DropNamespace(std::string_view nsName, const InternalRdxContext& ctx) { return sendCommand(DbCmdDropNamespace, std::forward(nsName), ctx); } -Error SyncCoroReindexerImpl::TruncateNamespace(std::string_view nsName, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::TruncateNamespace(std::string_view nsName, const InternalRdxContext& ctx) { return sendCommand(DbCmdTruncateNamespace, std::forward(nsName), ctx); } -Error SyncCoroReindexerImpl::RenameNamespace(std::string_view srcNsName, const std::string &dstNsName, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx) { return sendCommand(DbCmdRenameNamespace, std::forward(srcNsName), dstNsName, ctx); } -Error SyncCoroReindexerImpl::AddIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::AddIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx) { return sendCommand(DbCmdAddIndex, std::forward(nsName), index, ctx); } -Error SyncCoroReindexerImpl::UpdateIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::UpdateIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx) { return sendCommand(DbCmdUpdateIndex, std::forward(nsName), index, ctx); } -Error SyncCoroReindexerImpl::DropIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::DropIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx) { return sendCommand(DbCmdDropIndex, std::forward(nsName), index, ctx); } -Error SyncCoroReindexerImpl::SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext& ctx) { return sendCommand(DbCmdSetSchema, std::forward(nsName), std::forward(schema), ctx); } -Error SyncCoroReindexerImpl::EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts, const InternalRdxContext& ctx) { return sendCommand(DbCmdEnumNamespaces, defs, std::forward(opts), ctx); } -Error SyncCoroReindexerImpl::EnumDatabases(std::vector &dbList, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::EnumDatabases(std::vector& dbList, const InternalRdxContext& ctx) { return sendCommand(DbCmdEnumDatabases, dbList, ctx); } -Error SyncCoroReindexerImpl::Insert(std::string_view nsName, Item &item, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Insert(std::string_view nsName, Item& item, const InternalRdxContext& ctx) { return sendCommand(DbCmdInsert, std::forward(nsName), item, ctx); } -Error SyncCoroReindexerImpl::Update(std::string_view nsName, Item &item, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Update(std::string_view nsName, Item& item, const InternalRdxContext& ctx) { return sendCommand(DbCmdUpdate, std::forward(nsName), item, ctx); } -Error SyncCoroReindexerImpl::Upsert(std::string_view nsName, Item &item, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Upsert(std::string_view nsName, Item& item, const InternalRdxContext& ctx) { return sendCommand(DbCmdUpsert, std::forward(nsName), item, ctx); } -Error SyncCoroReindexerImpl::Update(const Query &query, SyncCoroQueryResults &result, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Update(const Query& query, SyncCoroQueryResults& result, const InternalRdxContext& ctx) { return sendCommand(DbCmdUpdateQ, query, result.results_, ctx); } -Error SyncCoroReindexerImpl::Delete(std::string_view nsName, Item &item, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Delete(std::string_view nsName, Item& item, const InternalRdxContext& ctx) { return sendCommand(DbCmdDelete, std::forward(nsName), item, ctx); } -Error SyncCoroReindexerImpl::Delete(const Query &query, SyncCoroQueryResults &result, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Delete(const Query& query, SyncCoroQueryResults& result, const InternalRdxContext& ctx) { return sendCommand(DbCmdDeleteQ, query, result.results_, ctx); } -Error SyncCoroReindexerImpl::Select(std::string_view query, SyncCoroQueryResults &result, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Select(std::string_view query, SyncCoroQueryResults& result, const InternalRdxContext& ctx) { return sendCommand(DbCmdSelectS, std::forward(query), result, ctx); } -Error SyncCoroReindexerImpl::Select(const Query &query, SyncCoroQueryResults &result, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::Select(const Query& query, SyncCoroQueryResults& result, const InternalRdxContext& ctx) { return sendCommand(DbCmdSelectQ, query, result, ctx); } Error SyncCoroReindexerImpl::Commit(std::string_view nsName) { @@ -103,64 +105,64 @@ Item SyncCoroReindexerImpl::NewItem(std::string_view nsName) { return sendCommand(DbCmdNewItem, std::forward(nsName)); } -Error SyncCoroReindexerImpl::GetMeta(std::string_view nsName, const std::string &key, std::string &data, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::GetMeta(std::string_view nsName, const std::string& key, std::string& data, const InternalRdxContext& ctx) { return sendCommand(DbCmdGetMeta, std::forward(nsName), key, data, ctx); } -Error SyncCoroReindexerImpl::PutMeta(std::string_view nsName, const std::string &key, std::string_view data, - const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::PutMeta(std::string_view nsName, const std::string& key, std::string_view data, + const InternalRdxContext& ctx) { return sendCommand(DbCmdPutMeta, std::forward(nsName), key, std::forward(data), ctx); } -Error SyncCoroReindexerImpl::EnumMeta(std::string_view nsName, std::vector &keys, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::EnumMeta(std::string_view nsName, std::vector& keys, const InternalRdxContext& ctx) { return sendCommand(DbCmdEnumMeta, std::forward(nsName), keys, ctx); } -Error SyncCoroReindexerImpl::DeleteMeta(std::string_view nsName, const std::string &key, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::DeleteMeta(std::string_view nsName, const std::string& key, const InternalRdxContext& ctx) { return sendCommand(DbCmdDeleteMeta, std::forward(nsName), key, ctx); } -Error SyncCoroReindexerImpl::GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector &suggestions) { +Error SyncCoroReindexerImpl::GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector& suggestions) { return sendCommand(DbCmdGetSqlSuggestions, std::forward(sqlQuery), std::forward(pos), suggestions); } -Error SyncCoroReindexerImpl::Status(const InternalRdxContext &ctx) { return sendCommand(DbCmdStatus, ctx); } +Error SyncCoroReindexerImpl::Status(const InternalRdxContext& ctx) { return sendCommand(DbCmdStatus, ctx); } -SyncCoroTransaction SyncCoroReindexerImpl::NewTransaction(std::string_view nsName, const InternalRdxContext &ctx) { +SyncCoroTransaction SyncCoroReindexerImpl::NewTransaction(std::string_view nsName, const InternalRdxContext& ctx) { CoroTransaction tx = sendCommand(DbCmdNewTransaction, std::forward(nsName), ctx); if (tx.Status().ok()) { return SyncCoroTransaction(std::move(tx), this); } return SyncCoroTransaction(tx.Status(), this); } -Error SyncCoroReindexerImpl::CommitTransaction(SyncCoroTransaction &tr, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::CommitTransaction(SyncCoroTransaction& tr, const InternalRdxContext& ctx) { Error err = sendCommand(DbCmdCommitTransaction, tr.tr_, ctx); tr = SyncCoroTransaction(errOK, this); return err; } -Error SyncCoroReindexerImpl::RollBackTransaction(SyncCoroTransaction &tr, const InternalRdxContext &ctx) { +Error SyncCoroReindexerImpl::RollBackTransaction(SyncCoroTransaction& tr, const InternalRdxContext& ctx) { Error err = sendCommand(DbCmdRollBackTransaction, tr, ctx); tr = SyncCoroTransaction(errOK, this); return err; } -Error SyncCoroReindexerImpl::fetchResults(int flags, SyncCoroQueryResults &result) { +Error SyncCoroReindexerImpl::fetchResults(int flags, SyncCoroQueryResults& result) { return sendCommand(DbCmdFetchResults, std::forward(flags), result); } -Error SyncCoroReindexerImpl::addTxItem(SyncCoroTransaction &tr, Item &&item, ItemModifyMode mode) { +Error SyncCoroReindexerImpl::addTxItem(SyncCoroTransaction& tr, Item&& item, ItemModifyMode mode) { return sendCommand(DbCmdAddTxItem, tr.tr_, std::move(item), std::forward(mode)); } -Error SyncCoroReindexerImpl::modifyTx(SyncCoroTransaction &tr, Query &&q) { +Error SyncCoroReindexerImpl::modifyTx(SyncCoroTransaction& tr, Query&& q) { return sendCommand(DbCmdModifyTx, tr.tr_, std::move(q)); } -Item SyncCoroReindexerImpl::newItemTx(CoroTransaction &tr) { return sendCommand(DbCmdNewItemTx, tr); } +Item SyncCoroReindexerImpl::newItemTx(CoroTransaction& tr) { return sendCommand(DbCmdNewItemTx, tr); } -void SyncCoroReindexerImpl::threadLoopFun(std::promise &&isRunning, const std::string &dsn, const client::ConnectOpts &opts) { - coroutine::channel chCommand; +void SyncCoroReindexerImpl::threadLoopFun(std::promise&& isRunning, const std::string& dsn, const client::ConnectOpts& opts) { + coroutine::channel chCommand; commandAsync_.set(loop_); - commandAsync_.set([this, &chCommand](net::ev::async &) { + commandAsync_.set([this, &chCommand](net::ev::async&) { loop_.spawn([this, &chCommand]() { - std::vector q; + std::vector q; commandsQueue_.Get(q); for (auto c : q) { chCommand.push(c); @@ -170,7 +172,7 @@ void SyncCoroReindexerImpl::threadLoopFun(std::promise &&isRunning, const commandAsync_.start(); closeAsync_.set(loop_); - closeAsync_.set([this, &chCommand](net::ev::async &) { loop_.spawn([&chCommand]() { chCommand.close(); }); }); + closeAsync_.set([this, &chCommand](net::ev::async&) { loop_.spawn([&chCommand]() { chCommand.close(); }); }); closeAsync_.start(); auto coro = [this, &chCommand, dsn, opts, &isRunning]() { @@ -192,124 +194,124 @@ void SyncCoroReindexerImpl::threadLoopFun(std::promise &&isRunning, const closeAsync_.stop(); } -void SyncCoroReindexerImpl::coroInterpreter(reindexer::client::CoroRPCClient &rx, coroutine::channel &chCommand, - coroutine::wait_group &wg) { +void SyncCoroReindexerImpl::coroInterpreter(reindexer::client::CoroRPCClient& rx, coroutine::channel& chCommand, + coroutine::wait_group& wg) { using namespace std::placeholders; coroutine::wait_group_guard wgg(wg); - for (std::pair v = chCommand.pop(); v.second == true; v = chCommand.pop()) { + for (std::pair v = chCommand.pop(); v.second == true; v = chCommand.pop()) { switch (v.first->id_) { case DbCmdOpenNamespace: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::OpenNamespace, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdAddNamespace: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::AddNamespace, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdCloseNamespace: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::CloseNamespace, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdDropNamespace: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::DropNamespace, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdTruncateNamespace: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::TruncateNamespace, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdRenameNamespace: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::RenameNamespace, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdAddIndex: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::AddIndex, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdUpdateIndex: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::UpdateIndex, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdDropIndex: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::DropIndex, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdSetSchema: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::SetSchema, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdEnumNamespaces: { - std::function &, EnumNamespacesOpts, const InternalRdxContext &)> f = + std::function&, EnumNamespacesOpts, const InternalRdxContext&)> f = std::bind(&client::CoroRPCClient::EnumNamespaces, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdEnumDatabases: { - std::function &, const InternalRdxContext &)> f = + std::function&, const InternalRdxContext&)> f = std::bind(&client::CoroRPCClient::EnumDatabases, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdInsert: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::Insert, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdUpdate: { - std::function f = - std::bind(static_cast( + std::function f = + std::bind(static_cast( &client::CoroRPCClient::Update), &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdUpsert: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::Upsert, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdUpdateQ: { - std::function f = - std::bind(static_cast( + std::function f = + std::bind(static_cast( &client::CoroRPCClient::Update), &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdDelete: { - std::function f = - std::bind(static_cast( + std::function f = + std::bind(static_cast( &client::CoroRPCClient::Delete), &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdDeleteQ: { - std::function f = - std::bind(static_cast( + std::function f = + std::bind(static_cast( &client::CoroRPCClient::Delete), &rx, _1, _2, _3); execCommand(v.first, f); @@ -323,15 +325,14 @@ void SyncCoroReindexerImpl::coroInterpreter(reindexer::client::CoroRPCClient &rx } case DbCmdSelectS: { auto cd = - dynamic_cast *>(v.first); + dynamic_cast*>(v.first); assertrx(cd); Error err = rx.Select(std::get<0>(cd->arguments), std::get<1>(cd->arguments).results_, std::get<2>(cd->arguments)); cd->ret.set_value(std::move(err)); break; } case DbCmdSelectQ: { - auto cd = - dynamic_cast *>(v.first); + auto cd = dynamic_cast*>(v.first); assertrx(cd); Error err = rx.Select(std::get<0>(cd->arguments), std::get<1>(cd->arguments).results_, std::get<2>(cd->arguments)); cd->ret.set_value(std::move(err)); @@ -343,64 +344,64 @@ void SyncCoroReindexerImpl::coroInterpreter(reindexer::client::CoroRPCClient &rx break; } case DbCmdGetMeta: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::GetMeta, &rx, _1, _2, _3, _4); execCommand(v.first, f); break; } case DbCmdPutMeta: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::PutMeta, &rx, _1, _2, _3, _4); execCommand(v.first, f); break; } case DbCmdEnumMeta: { - std::function &, const InternalRdxContext &)> f = + std::function&, const InternalRdxContext&)> f = std::bind(&client::CoroRPCClient::EnumMeta, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdDeleteMeta: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::DeleteMeta, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdGetSqlSuggestions: { - std::function &)> f = + std::function&)> f = std::bind(&client::CoroRPCClient::GetSqlSuggestions, &rx, _1, _2, _3); execCommand(v.first, f); break; } case DbCmdStatus: { - std::function f = std::bind(&client::CoroRPCClient::Status, &rx, _1); + std::function f = std::bind(&client::CoroRPCClient::Status, &rx, _1); execCommand(v.first, f); break; } case DbCmdNewTransaction: { - auto *cd = dynamic_cast *>(v.first); + auto* cd = dynamic_cast*>(v.first); assertrx(cd); CoroTransaction coroTrans = rx.NewTransaction(std::get<0>(cd->arguments), std::get<1>(cd->arguments)); cd->ret.set_value(std::move(coroTrans)); break; } case DbCmdCommitTransaction: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::CommitTransaction, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdRollBackTransaction: { - std::function f = + std::function f = std::bind(&client::CoroRPCClient::RollBackTransaction, &rx, _1, _2); execCommand(v.first, f); break; } case DbCmdFetchResults: { - auto cd = dynamic_cast *>(v.first); + auto cd = dynamic_cast*>(v.first); assertrx(cd); - CoroQueryResults &coroResults = std::get<1>(cd->arguments).results_; + CoroQueryResults& coroResults = std::get<1>(cd->arguments).results_; auto ret = coroResults.conn_->Call( {reindexer::net::cproto::kCmdFetchResults, coroResults.requestTimeout_, milliseconds(0), nullptr}, coroResults.queryID_.main, std::get<0>(cd->arguments), coroResults.queryParams_.count + coroResults.fetchOffset_, @@ -427,21 +428,21 @@ void SyncCoroReindexerImpl::coroInterpreter(reindexer::client::CoroRPCClient &rx break; } case DbCmdNewItemTx: { - auto cd = dynamic_cast *>(v.first); + auto cd = dynamic_cast*>(v.first); assertrx(cd); Item item = execNewItemTx(std::get<0>(cd->arguments)); cd->ret.set_value(std::move(item)); break; } case DbCmdAddTxItem: { - auto cd = dynamic_cast *>(v.first); + auto cd = dynamic_cast*>(v.first); assertrx(cd); Error err = std::get<0>(cd->arguments).addTxItem(std::move(std::get<1>(cd->arguments)), std::get<2>(cd->arguments)); cd->ret.set_value(err); break; } case DbCmdModifyTx: { - auto cd = dynamic_cast *>(v.first); + auto cd = dynamic_cast*>(v.first); assertrx(cd); CoroTransaction tr = std::get<0>(cd->arguments); Error err(errLogic, "Connection pointer in transaction is nullptr."); @@ -460,19 +461,21 @@ void SyncCoroReindexerImpl::coroInterpreter(reindexer::client::CoroRPCClient &rx } } -Item SyncCoroReindexerImpl::execNewItemTx(CoroTransaction &tr) { - if (!tr.rpcClient_) return Item(Error(errLogic, "rpcClient not set for client transaction")); +Item SyncCoroReindexerImpl::execNewItemTx(CoroTransaction& tr) { + if (!tr.rpcClient_) { + return Item(Error(errLogic, "rpcClient not set for client transaction")); + } return tr.rpcClient_->NewItem(tr.nsName_); } -void SyncCoroReindexerImpl::CommandsQueue::Push(net::ev::async &ev, DatabaseCommandBase *cmd) { +void SyncCoroReindexerImpl::CommandsQueue::Push(net::ev::async& ev, DatabaseCommandBase* cmd) { { std::unique_lock lock(mtx_); queue_.emplace_back(cmd); } ev.send(); } -void SyncCoroReindexerImpl::CommandsQueue::Get(std::vector &cmds) { +void SyncCoroReindexerImpl::CommandsQueue::Get(std::vector& cmds) { std::unique_lock lock(mtx_); cmds.swap(queue_); } diff --git a/cpp_src/client/synccororeindexerimpl.h b/cpp_src/client/synccororeindexerimpl.h index 960f77722..f7ea0442b 100644 --- a/cpp_src/client/synccororeindexerimpl.h +++ b/cpp_src/client/synccororeindexerimpl.h @@ -15,57 +15,57 @@ class SyncCoroTransaction; class SyncCoroReindexerImpl { public: /// Create Reindexer database object - SyncCoroReindexerImpl(const ReindexerConfig & = ReindexerConfig()); + SyncCoroReindexerImpl(const ReindexerConfig& = ReindexerConfig()); /// Destrory Reindexer database object ~SyncCoroReindexerImpl(); - SyncCoroReindexerImpl(const SyncCoroReindexerImpl &) = delete; - SyncCoroReindexerImpl &operator=(const SyncCoroReindexerImpl &) = delete; + SyncCoroReindexerImpl(const SyncCoroReindexerImpl&) = delete; + SyncCoroReindexerImpl& operator=(const SyncCoroReindexerImpl&) = delete; - Error Connect(const std::string &dsn, const client::ConnectOpts &opts = client::ConnectOpts()); + Error Connect(const std::string& dsn, const client::ConnectOpts& opts = client::ConnectOpts()); Error Stop(); - Error OpenNamespace(std::string_view nsName, const InternalRdxContext &ctx, - const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); - Error AddNamespace(const NamespaceDef &nsDef, const InternalRdxContext &ctx); - Error CloseNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error DropNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error TruncateNamespace(std::string_view nsName, const InternalRdxContext &ctx); - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName, const InternalRdxContext &ctx); - Error AddIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error UpdateIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error DropIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx); - Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext &ctx); - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts, const InternalRdxContext &ctx); - Error EnumDatabases(std::vector &dbList, const InternalRdxContext &ctx); - Error Insert(std::string_view nsName, Item &item, const InternalRdxContext &ctx); - Error Update(std::string_view nsName, Item &item, const InternalRdxContext &ctx); - Error Upsert(std::string_view nsName, Item &item, const InternalRdxContext &ctx); - Error Update(const Query &query, SyncCoroQueryResults &result, const InternalRdxContext &ctx); - Error Delete(std::string_view nsName, Item &item, const InternalRdxContext &ctx); - Error Delete(const Query &query, SyncCoroQueryResults &result, const InternalRdxContext &ctx); - Error Select(std::string_view query, SyncCoroQueryResults &result, const InternalRdxContext &ctx); - Error Select(const Query &query, SyncCoroQueryResults &result, const InternalRdxContext &ctx); + Error OpenNamespace(std::string_view nsName, const InternalRdxContext& ctx, + const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); + Error AddNamespace(const NamespaceDef& nsDef, const InternalRdxContext& ctx); + Error CloseNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error DropNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error TruncateNamespace(std::string_view nsName, const InternalRdxContext& ctx); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx); + Error AddIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error UpdateIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error DropIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx); + Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext& ctx); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts, const InternalRdxContext& ctx); + Error EnumDatabases(std::vector& dbList, const InternalRdxContext& ctx); + Error Insert(std::string_view nsName, Item& item, const InternalRdxContext& ctx); + Error Update(std::string_view nsName, Item& item, const InternalRdxContext& ctx); + Error Upsert(std::string_view nsName, Item& item, const InternalRdxContext& ctx); + Error Update(const Query& query, SyncCoroQueryResults& result, const InternalRdxContext& ctx); + Error Delete(std::string_view nsName, Item& item, const InternalRdxContext& ctx); + Error Delete(const Query& query, SyncCoroQueryResults& result, const InternalRdxContext& ctx); + Error Select(std::string_view query, SyncCoroQueryResults& result, const InternalRdxContext& ctx); + Error Select(const Query& query, SyncCoroQueryResults& result, const InternalRdxContext& ctx); Error Commit(std::string_view nsName); Item NewItem(std::string_view nsName); - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data, const InternalRdxContext &ctx); - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data, const InternalRdxContext &ctx); - Error EnumMeta(std::string_view nsName, std::vector &keys, const InternalRdxContext &ctx); - Error DeleteMeta(std::string_view nsName, const std::string &key, const InternalRdxContext &ctx); - Error GetSqlSuggestions(const std::string_view sqlQuery, int pos, std::vector &suggestions); - Error Status(const InternalRdxContext &ctx); - SyncCoroTransaction NewTransaction(std::string_view nsName, const InternalRdxContext &ctx); - Error CommitTransaction(SyncCoroTransaction &tr, const InternalRdxContext &ctx); - Error RollBackTransaction(SyncCoroTransaction &tr, const InternalRdxContext &ctx); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data, const InternalRdxContext& ctx); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data, const InternalRdxContext& ctx); + Error EnumMeta(std::string_view nsName, std::vector& keys, const InternalRdxContext& ctx); + Error DeleteMeta(std::string_view nsName, const std::string& key, const InternalRdxContext& ctx); + Error GetSqlSuggestions(const std::string_view sqlQuery, int pos, std::vector& suggestions); + Error Status(const InternalRdxContext& ctx); + SyncCoroTransaction NewTransaction(std::string_view nsName, const InternalRdxContext& ctx); + Error CommitTransaction(SyncCoroTransaction& tr, const InternalRdxContext& ctx); + Error RollBackTransaction(SyncCoroTransaction& tr, const InternalRdxContext& ctx); private: friend class SyncCoroQueryResults; friend class SyncCoroTransaction; - Error fetchResults(int flags, SyncCoroQueryResults &result); - Error addTxItem(SyncCoroTransaction &tr, Item &&item, ItemModifyMode mode); - Error modifyTx(SyncCoroTransaction &tr, Query &&q); - Item execNewItemTx(CoroTransaction &tr); - Item newItemTx(CoroTransaction &tr); - Error execAddTxItem(CoroTransaction &tr, Item &item, ItemModifyMode mode); - void threadLoopFun(std::promise &&isRunning, const std::string &dsn, const client::ConnectOpts &opts); + Error fetchResults(int flags, SyncCoroQueryResults& result); + Error addTxItem(SyncCoroTransaction& tr, Item&& item, ItemModifyMode mode); + Error modifyTx(SyncCoroTransaction& tr, Query&& q); + Item execNewItemTx(CoroTransaction& tr); + Item newItemTx(CoroTransaction& tr); + Error execAddTxItem(CoroTransaction& tr, Item& item, ItemModifyMode mode); + void threadLoopFun(std::promise&& isRunning, const std::string& dsn, const client::ConnectOpts& opts); bool exit_ = false; enum CmdName { @@ -117,12 +117,12 @@ class SyncCoroReindexerImpl { struct DatabaseCommand : public DatabaseCommandBase { std::promise ret; std::tuple arguments; - DatabaseCommand(CmdName id, std::promise r, P &&...p) + DatabaseCommand(CmdName id, std::promise r, P&&... p) : DatabaseCommandBase(id), ret(std::move(r)), arguments(std::forward

(p)...) {} }; template - R sendCommand(CmdName c, Args &&...args) { + R sendCommand(CmdName c, Args&&... args) { std::promise promise; std::future future = promise.get_future(); DatabaseCommand cmd(c, std::move(promise), std::forward(args)...); @@ -132,8 +132,8 @@ class SyncCoroReindexerImpl { } template - void execCommand(DatabaseCommandBase *cmd, const std::function &fun) { - auto cd = dynamic_cast *>(cmd); + void execCommand(DatabaseCommandBase* cmd, const std::function& fun) { + auto cd = dynamic_cast*>(cmd); if (cd) { R r = std::apply(fun, cd->arguments); cd->ret.set_value(std::move(r)); @@ -145,11 +145,11 @@ class SyncCoroReindexerImpl { class CommandsQueue { public: CommandsQueue() {} - void Push(net::ev::async &ev, DatabaseCommandBase *cmd); - void Get(std::vector &cmds); + void Push(net::ev::async& ev, DatabaseCommandBase* cmd); + void Get(std::vector& cmds); private: - std::vector queue_; + std::vector queue_; std::mutex mtx_; }; @@ -160,8 +160,8 @@ class SyncCoroReindexerImpl { net::ev::async commandAsync_; net::ev::async closeAsync_; const ReindexerConfig conf_; - void coroInterpreter(reindexer::client::CoroRPCClient &rx, coroutine::channel &chCommand, - coroutine::wait_group &wg); + void coroInterpreter(reindexer::client::CoroRPCClient& rx, coroutine::channel& chCommand, + coroutine::wait_group& wg); }; } // namespace client diff --git a/cpp_src/client/transaction.cc b/cpp_src/client/transaction.cc index 19b5adcaf..e7b107bd4 100644 --- a/cpp_src/client/transaction.cc +++ b/cpp_src/client/transaction.cc @@ -12,7 +12,9 @@ void Transaction::Modify(Query&& query) { WrSerializer ser; query.Serialize(ser); auto ret = conn_->Call({cproto::kCmdUpdateQueryTx, RequestTimeout_, execTimeout_, nullptr}, ser.Slice(), txId_).Status(); - if (!ret.ok()) throw ret; + if (!ret.ok()) { + throw ret; + } } throw Error(errLogic, "Connection pointer in transaction is nullptr."); } @@ -26,29 +28,38 @@ void Transaction::addTxItem(Item&& item, ItemModifyMode mode) { conn_->Call({net::cproto::kCmdAddTxItem, RequestTimeout_, execTimeout_, nullptr}, FormatJson, itemData, mode, "", 0, txId_); if (!ret.Status().ok()) { - if (ret.Status().code() != errStateInvalidated || tryCount > 2) throw ret.Status(); + if (ret.Status().code() != errStateInvalidated || tryCount > 2) { + throw ret.Status(); + } QueryResults qr; InternalRdxContext ctx; ctx = ctx.WithTimeout(execTimeout_); auto err = rpcClient_->Select(Query(nsName_).Limit(0), qr, ctx, conn_); - if (!err.ok()) throw Error(errLogic, "Can't update TagsMatcher"); + if (!err.ok()) { + throw Error(errLogic, "Can't update TagsMatcher"); + } auto newItem = NewItem(); char* endp = nullptr; err = newItem.FromJSON(item.impl_->GetJSON(), &endp); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } item = std::move(newItem); } else { break; } } - } else + } else { throw Error(errLogic, "Connection pointer in transaction is nullptr."); + } } Item Transaction::NewItem() { - if (!rpcClient_) throw Error(errLogic, "rpcClient not set for client transaction"); + if (!rpcClient_) { + throw Error(errLogic, "rpcClient not set for client transaction"); + } return rpcClient_->NewItem(nsName_); } } // namespace client diff --git a/cpp_src/cmd/reindexer_server/contrib/Dockerfile b/cpp_src/cmd/reindexer_server/contrib/Dockerfile index db8c9a379..5e9da49cd 100644 --- a/cpp_src/cmd/reindexer_server/contrib/Dockerfile +++ b/cpp_src/cmd/reindexer_server/contrib/Dockerfile @@ -1,11 +1,11 @@ -FROM alpine:3.19 AS build +FROM alpine:3.20 AS build RUN cd /tmp && apk update && \ apk add git curl autoconf automake libtool linux-headers g++ make libunwind-dev grpc-dev protobuf-dev c-ares-dev patch && \ git clone https://github.com/gperftools/gperftools.git && \ cd gperftools && git checkout gperftools-2.15 && \ sed -i s/_sigev_un\._tid/sigev_notify_thread_id/ src/profile-handler.cc && \ - ./autogen.sh && ./configure --disable-dependency-tracking && make -j8 && make install + ./autogen.sh && ./configure --disable-dependency-tracking && make -j6 && make install ADD . /src @@ -15,7 +15,7 @@ RUN ./dependencies.sh && \ mkdir build && \ cd build && \ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. -DENABLE_GRPC=On -DGRPC_PACKAGE_PROVIDER="" && \ - make -j8 reindexer_server reindexer_tool && \ + make -j6 reindexer_server reindexer_tool && \ make install -C cpp_src/cmd/reindexer_server && \ make install -C cpp_src/cmd/reindexer_tool && \ make install -C cpp_src/server/grpc && \ @@ -23,18 +23,18 @@ RUN ./dependencies.sh && \ rm -rf /usr/local/lib/*.a /usr/local/include /usr/local/lib/libtcmalloc_debug* /usr/local/lib/libtcmalloc_minimal* \ /usr/local/lib/libprofiler* /usr/local/lib/libtcmalloc.* /usr/local/share/doc /usr/local/share/man /usr/local/share/perl5 /usr/local/bin/pprof* -FROM alpine:3.19 +FROM alpine:3.20 COPY --from=build /usr/local /usr/local COPY --from=build /entrypoint.sh /entrypoint.sh RUN apk update && apk add libstdc++ libunwind snappy leveldb c-ares libprotobuf xz-libs grpc-cpp && rm -rf /var/cache/apk/* -ENV RX_DATABASE /db -ENV RX_CORELOG stdout -ENV RX_HTTPLOG stdout -ENV RX_RPCLOG stdout -ENV RX_SERVERLOG stdout -ENV RX_LOGLEVEL info +ENV RX_DATABASE=/db +ENV RX_CORELOG=stdout +ENV RX_HTTPLOG=stdout +ENV RX_RPCLOG=stdout +ENV RX_SERVERLOG=stdout +ENV RX_LOGLEVEL=info RUN chmod +x /entrypoint.sh diff --git a/cpp_src/cmd/reindexer_server/contrib/Dockerfile.deb b/cpp_src/cmd/reindexer_server/contrib/Dockerfile.deb index d64e65dc2..3efe1de23 100644 --- a/cpp_src/cmd/reindexer_server/contrib/Dockerfile.deb +++ b/cpp_src/cmd/reindexer_server/contrib/Dockerfile.deb @@ -21,12 +21,12 @@ COPY --from=build /usr/local /usr/local COPY --from=build /entrypoint.sh /entrypoint.sh RUN apt update -y && apt install -y libleveldb1d libunwind8 libjemalloc2 libgrpc++1.51 && rm -rf /var/lib/apt -ENV RX_DATABASE /db -ENV RX_CORELOG stdout -ENV RX_HTTPLOG stdout -ENV RX_RPCLOG stdout -ENV RX_SERVERLOG stdout -ENV RX_LOGLEVEL info +ENV RX_DATABASE=/db +ENV RX_CORELOG=stdout +ENV RX_HTTPLOG=stdout +ENV RX_RPCLOG=stdout +ENV RX_SERVERLOG=stdout +ENV RX_LOGLEVEL=info RUN chmod +x /entrypoint.sh diff --git a/cpp_src/cmd/reindexer_server/test/test_storage_compatibility.sh b/cpp_src/cmd/reindexer_server/test/test_storage_compatibility.sh new file mode 100755 index 000000000..d189d3841 --- /dev/null +++ b/cpp_src/cmd/reindexer_server/test/test_storage_compatibility.sh @@ -0,0 +1,195 @@ +#!/bin/bash +# Task: https://github.com/restream/reindexer/-/issues/1188 +set -e + +function KillAndRemoveServer { + local pid=$1 + kill $pid + wait $pid + yum remove -y 'reindexer*' > /dev/null +} + +function WaitForDB { + # wait until DB is loaded + set +e # disable "exit on error" so the script won't stop when DB's not loaded yet + is_connected=$(reindexer_tool --dsn $ADDRESS --command '\databases list'); + while [[ $is_connected != "test" ]] + do + sleep 2 + is_connected=$(reindexer_tool --dsn $ADDRESS --command '\databases list'); + done + set -e +} + +function CompareNamespacesLists { + local ns_list_actual=$1 + local ns_list_expected=$2 + local pid=$3 + + diff=$(echo ${ns_list_actual[@]} ${ns_list_expected[@]} | tr ' ' '\n' | sort | uniq -u) # compare in any order + if [ "$diff" == "" ]; then + echo "## PASS: namespaces list not changed" + else + echo "##### FAIL: namespaces list was changed" + echo "expected: $ns_list_expected" + echo "actual: $ns_list_actual" + KillAndRemoveServer $pid; + exit 1 + fi +} + +function CompareMemstats { + local actual=$1 + local expected=$2 + local pid=$3 + diff=$(echo ${actual[@]} ${expected[@]} | tr ' ' '\n' | sed 's/\(.*\),$/\1/' | sort | uniq -u) # compare in any order + if [ "$diff" == "" ]; then + echo "## PASS: memstats not changed" + else + echo "##### FAIL: memstats was changed" + echo "expected: $expected" + echo "actual: $actual" + KillAndRemoveServer $pid; + exit 1 + fi +} + + +RX_SERVER_CURRENT_VERSION_RPM="$(basename build/reindexer-*server*.rpm)" +VERSION_FROM_RPM=$(echo "$RX_SERVER_CURRENT_VERSION_RPM" | grep -o '.*server-..') +VERSION=$(echo ${VERSION_FROM_RPM: -2:1}) # one-digit version + +echo "## choose latest release rpm file" +if [ $VERSION == 3 ]; then + LATEST_RELEASE=$(python3 cpp_src/cmd/reindexer_server/test/get_last_rx_version.py -v 3) + namespaces_list_expected=$'purchase_options_ext_dict\nchild_account_recommendations\n#config\n#activitystats\nradio_channels\ncollections\n#namespaces\nwp_imports_tasks\nepg_genres\nrecom_media_items_personal\nrecom_epg_archive_default\n#perfstats\nrecom_epg_live_default\nmedia_view_templates\nasset_video_servers\nwp_tasks_schedule\nadmin_roles\n#clientsstats\nrecom_epg_archive_personal\nrecom_media_items_similars\nmenu_items\naccount_recommendations\nkaraoke_items\nmedia_items\nbanners\n#queriesperfstats\nrecom_media_items_default\nrecom_epg_live_personal\nservices\n#memstats\nchannels\nmedia_item_recommendations\nwp_tasks_tasks\nepg' +elif [ $VERSION == 4 ]; then + LATEST_RELEASE=$(python3 cpp_src/cmd/reindexer_server/test/get_last_rx_version.py -v 4) + # replicationstats ns added for v4 + namespaces_list_expected=$'purchase_options_ext_dict\nchild_account_recommendations\n#config\n#activitystats\n#replicationstats\nradio_channels\ncollections\n#namespaces\nwp_imports_tasks\nepg_genres\nrecom_media_items_personal\nrecom_epg_archive_default\n#perfstats\nrecom_epg_live_default\nmedia_view_templates\nasset_video_servers\nwp_tasks_schedule\nadmin_roles\n#clientsstats\nrecom_epg_archive_personal\nrecom_media_items_similars\nmenu_items\naccount_recommendations\nkaraoke_items\nmedia_items\nbanners\n#queriesperfstats\nrecom_media_items_default\nrecom_epg_live_personal\nservices\n#memstats\nchannels\nmedia_item_recommendations\nwp_tasks_tasks\nepg' +else + echo "Unknown version" + exit 1 +fi + +echo "## downloading latest release rpm file: $LATEST_RELEASE" +curl "http://repo.itv.restr.im/itv-api-ng/7/x86_64/$LATEST_RELEASE" --output $LATEST_RELEASE; +echo "## downloading example DB" +curl "https://git.restream.ru/MaksimKravchuk/reindexer_testdata/-/raw/master/big.zip" --output big.zip; +unzip -o big.zip # unzips into mydb_big.rxdump; + +ADDRESS="cproto://127.0.0.1:6534/" +DB_NAME="test" + +memstats_expected=$'[ +{"replication":{"data_hash":24651210926,"data_count":3}}, +{"replication":{"data_hash":6252344969,"data_count":1}}, +{"replication":{"data_hash":37734732881,"data_count":28}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":1024095024522,"data_count":1145}}, +{"replication":{"data_hash":8373644068,"data_count":1315}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":7404222244,"data_count":97}}, +{"replication":{"data_hash":94132837196,"data_count":4}}, +{"replication":{"data_hash":1896088071,"data_count":2}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":-672103903,"data_count":33538}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":6833710705,"data_count":1}}, +{"replication":{"data_hash":5858155773472,"data_count":4500}}, +{"replication":{"data_hash":-473221280268823592,"data_count":65448}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":8288213744,"data_count":3}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":0,"data_count":0}}, +{"replication":{"data_hash":354171024786967,"data_count":3941}}, +{"replication":{"data_hash":-6520334670,"data_count":35886}}, +{"replication":{"data_hash":112772074632,"data_count":281}}, +{"replication":{"data_hash":-12679568198538,"data_count":1623116}} +] +Returned 27 rows' + +echo "##### Forward compatibility test #####" + +DB_PATH=$(pwd)"/rx_db" + +echo "Database: "$DB_PATH + +echo "## installing latest release: $LATEST_RELEASE" +yum install -y $LATEST_RELEASE > /dev/null; +# run RX server with disabled logging +reindexer_server -l warning --httplog=none --rpclog=none --db $DB_PATH & +server_pid=$! +sleep 2; + +reindexer_tool --dsn $ADDRESS$DB_NAME -f mydb_big.rxdump --createdb; +sleep 1; + +namespaces_1=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command '\namespaces list'); +echo $namespaces_1; +CompareNamespacesLists "${namespaces_1[@]}" "${namespaces_list_expected[@]}" $server_pid; + +memstats_1=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command 'select replication.data_hash, replication.data_count from #memstats'); +CompareMemstats "${memstats_1[@]}" "${memstats_expected[@]}" $server_pid; + +KillAndRemoveServer $server_pid; + +echo "## installing current version: $RX_SERVER_CURRENT_VERSION_RPM" +yum install -y build/*.rpm > /dev/null; +reindexer_server -l0 --corelog=none --httplog=none --rpclog=none --db $DB_PATH & +server_pid=$! +sleep 2; + +WaitForDB + +namespaces_2=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command '\namespaces list'); +echo $namespaces_2; +CompareNamespacesLists "${namespaces_2[@]}" "${namespaces_1[@]}" $server_pid; + +memstats_2=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command 'select replication.data_hash, replication.data_count from #memstats'); +CompareMemstats "${memstats_2[@]}" "${memstats_1[@]}" $server_pid; + +KillAndRemoveServer $server_pid; +rm -rf $DB_PATH; +sleep 1; + +echo "##### Backward compatibility test #####" + +echo "## installing current version: $RX_SERVER_CURRENT_VERSION_RPM" +yum install -y build/*.rpm > /dev/null; +reindexer_server -l warning --httplog=none --rpclog=none --db $DB_PATH & +server_pid=$! +sleep 2; + +reindexer_tool --dsn $ADDRESS$DB_NAME -f mydb_big.rxdump --createdb; +sleep 1; + +namespaces_3=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command '\namespaces list'); +echo $namespaces_3; +CompareNamespacesLists "${namespaces_3[@]}" "${namespaces_list_expected[@]}" $server_pid; + +memstats_3=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command 'select replication.data_hash, replication.data_count from #memstats'); +CompareMemstats "${memstats_3[@]}" "${memstats_expected[@]}" $server_pid; + +KillAndRemoveServer $server_pid; + +echo "## installing latest release: $LATEST_RELEASE" +yum install -y $LATEST_RELEASE > /dev/null; +reindexer_server -l warning --httplog=none --rpclog=none --db $DB_PATH & +server_pid=$! +sleep 2; + +WaitForDB + +namespaces_4=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command '\namespaces list'); +echo $namespaces_4; +CompareNamespacesLists "${namespaces_4[@]}" "${namespaces_3[@]}" $server_pid; + +memstats_4=$(reindexer_tool --dsn $ADDRESS$DB_NAME --command 'select replication.data_hash, replication.data_count from #memstats'); +CompareMemstats "${memstats_4[@]}" "${memstats_3[@]}" $server_pid; + +KillAndRemoveServer $server_pid; +rm -rf $DB_PATH; diff --git a/cpp_src/cmd/reindexer_tool/commandsexecutor.cc b/cpp_src/cmd/reindexer_tool/commandsexecutor.cc index e5e4ff453..0802cf656 100644 --- a/cpp_src/cmd/reindexer_tool/commandsexecutor.cc +++ b/cpp_src/cmd/reindexer_tool/commandsexecutor.cc @@ -214,7 +214,9 @@ Error CommandsExecutor::runImpl(const std::string& dsn, Args&&... a if (err.ok() && !uri_.parse(dsn)) { err = Error(errNotValid, "Cannot connect to DB: Not a valid uri"); } - if (err.ok()) err = db().Connect(dsn, std::forward(args)...); + if (err.ok()) { + err = db().Connect(dsn, std::forward(args)...); + } if (err.ok()) { loop_.spawn( [this] { @@ -303,7 +305,9 @@ std::string CommandsExecutor::getCurrentDsn(bool withPath) const { template Error CommandsExecutor::queryResultsToJson(std::ostream& o, const typename DBInterface::QueryResultsT& r, bool isWALQuery, bool fstream) { - if (cancelCtx_.IsCancelled()) return errOK; + if (cancelCtx_.IsCancelled()) { + return errOK; + } WrSerializer ser; size_t i = 0; bool scrollable = !fstream && !reindexer::isStdoutRedirected(); @@ -314,8 +318,12 @@ Error CommandsExecutor::queryResultsToJson(std::ostream& o, const t } bool prettyPrint = variables_[kVariableOutput] == kOutputModePretty; for (auto it : r) { - if (auto err = it.Status(); !err.ok()) return err; - if (cancelCtx_.IsCancelled()) break; + if (auto err = it.Status(); !err.ok()) { + return err; + } + if (cancelCtx_.IsCancelled()) { + break; + } if (isWALQuery) { ser << '#'; { @@ -337,19 +345,27 @@ Error CommandsExecutor::queryResultsToJson(std::ostream& o, const t return std::move(err); } } else { - if (isWALQuery) ser << "WalItemUpdate "; + if (isWALQuery) { + ser << "WalItemUpdate "; + } if (prettyPrint) { WrSerializer json; Error err = it.GetJSON(json, false); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } prettyPrintJSON(reindexer::giftStr(json.Slice()), ser); } else { Error err = it.GetJSON(ser, false); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } } - if ((++i != r.Count()) && !isWALQuery) ser << ','; + if ((++i != r.Count()) && !isWALQuery) { + ser << ','; + } ser << '\n'; if ((ser.Len() > 0x100000) || prettyPrint || scrollable) { if (scrollable && (i % (terminalSize.height - 1) == 0)) { @@ -376,7 +392,7 @@ Error CommandsExecutor::getAvailableDatabases(std::vector< } template -void CommandsExecutor::addCommandsSuggestions(std::string const& cmd, std::vector& suggestions) { +void CommandsExecutor::addCommandsSuggestions(const std::string& cmd, std::vector& suggestions) { LineParser parser(cmd); std::string_view token = parser.NextToken(); @@ -440,7 +456,9 @@ template void CommandsExecutor::checkForNsNameMatch(std::string_view str, std::vector& suggestions) { std::vector allNsDefs; Error err = db().EnumNamespaces(allNsDefs, reindexer::EnumNamespacesOpts().WithClosed()); - if (!err.ok()) return; + if (!err.ok()) { + return; + } for (auto& ns : allNsDefs) { if (str.empty() || reindexer::isBlank(str) || ((str.length() < ns.name.length()) && reindexer::checkIfStartsWith(str, ns.name))) { suggestions.emplace_back(ns.name); @@ -463,7 +481,9 @@ Error CommandsExecutor::processImpl(const std::string& command) noe LineParser parser(command); auto token = parser.NextToken(); - if (!token.length() || token.substr(0, 2) == "--") return errOK; + if (!token.length() || token.substr(0, 2) == "--") { + return errOK; + } Error ret; for (auto& c : cmds_) { @@ -561,7 +581,9 @@ Error CommandsExecutor::commandSelect(const std::string& command) n } if (!cancelCtx_.IsCancelled()) { output_() << "Returned " << results.Count() << " rows"; - if (results.TotalCount()) output_() << ", total count " << results.TotalCount(); + if (results.TotalCount()) { + output_() << ", total count " << results.TotalCount(); + } output_() << std::endl; } @@ -589,14 +611,18 @@ Error CommandsExecutor::commandSelect(const std::string& command) n rowWidth += mW; } for (size_t i = 0; i < agg.fields.size(); ++i) { - if (i != 0) output_() << "| "; + if (i != 0) { + output_() << "| "; + } output_() << std::left << std::setw(maxW.at(i)) << agg.fields[i]; } output_() << "| count" << std::endl; output_() << std::left << std::setw(rowWidth) << std::setfill('-') << "" << std::endl << std::setfill(' '); for (auto& row : agg.facets) { for (size_t i = 0; i < row.values.size(); ++i) { - if (i != 0) output_() << "| "; + if (i != 0) { + output_() << "| "; + } output_() << std::left << std::setw(maxW.at(i)) << row.values[i]; } output_() << "| " << row.count << std::endl; @@ -683,10 +709,14 @@ Error CommandsExecutor::commandDelete(const std::string& command) { auto nsName = reindexer::unescapeString(parser.NextToken()); auto item = db().NewItem(nsName); - if (!item.Status().ok()) return item.Status(); + if (!item.Status().ok()) { + return item.Status(); + } auto err = item.Unsafe().FromJSON(parser.CurPtr()); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } return db().Delete(nsName, item); } @@ -714,7 +744,9 @@ Error CommandsExecutor::commandDump(const std::string& command) { std::vector allNsDefs, doNsDefs; auto err = db().WithContext(&cancelCtx_).EnumNamespaces(allNsDefs, reindexer::EnumNamespacesOpts()); - if (err) return err; + if (err) { + return err; + } if (!parser.End()) { // build list of namespaces for dumped @@ -739,7 +771,9 @@ Error CommandsExecutor::commandDump(const std::string& command) { for (auto& nsDef : doNsDefs) { // skip system namespaces, except #config - if (reindexer::isSystemNamespaceNameFast(nsDef.name) && nsDef.name != "#config") continue; + if (reindexer::isSystemNamespaceNameFast(nsDef.name) && nsDef.name != "#config") { + continue; + } wrser << "-- Dumping namespace '" << nsDef.name << "' ..." << '\n'; @@ -767,16 +801,22 @@ Error CommandsExecutor::commandDump(const std::string& command) { typename DBInterface::QueryResultsT itemResults; err = db().WithContext(&cancelCtx_).Select(Query(nsDef.name), itemResults); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } for (auto it : itemResults) { - if (auto err = it.Status(); !err.ok()) return err; + if (auto err = it.Status(); !err.ok()) { + return err; + } if (cancelCtx_.IsCancelled()) { return Error(errCanceled, "Canceled"); } wrser << "\\UPSERT " << reindexer::escapeString(nsDef.name) << ' '; err = it.GetJSON(wrser, false); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } wrser << '\n'; if (wrser.Len() > 0x100000) { output_() << wrser.Slice(); @@ -797,7 +837,7 @@ Error CommandsExecutor::commandNamespaces(const std::string& comman std::string_view subCommand = parser.NextToken(); if (iequals(subCommand, "add")) { - parser.NextToken(); // nsName + parser.NextToken(); // nsName NamespaceDef def(""); Error err = def.FromJSON(reindexer::giftStr(parser.CurPtr())); @@ -866,11 +906,15 @@ Error CommandsExecutor::commandMeta(const std::string& command) { auto nsName = reindexer::unescapeString(parser.NextToken()); std::vector allMeta; auto err = db().EnumMeta(nsName, allMeta); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } for (auto& metaKey : allMeta) { std::string metaData; err = db().GetMeta(nsName, metaKey, metaData); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } output_() << metaKey << " = " << metaData << std::endl; } return err; @@ -950,7 +994,9 @@ Error CommandsExecutor::commandBench(const std::string& command) { nsDef.AddIndex("id", "hash", "int", IndexOpts().PK()); err = db().AddNamespace(nsDef); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } output_() << "Seeding " << kBenchItemsCount << " documents to bench namespace..." << std::endl; err = seedBenchItems(); @@ -968,8 +1014,12 @@ Error CommandsExecutor::commandBench(const std::string& command) { auto worker = std::bind(getBenchWorkerFn(count, errCount), deadline); const auto numThreads = std::min(std::max(numThreads_, 1u), 65535u); auto threads = std::unique_ptr(new std::thread[numThreads]); - for (unsigned i = 0; i < numThreads; i++) threads[i] = std::thread(worker); - for (unsigned i = 0; i < numThreads; i++) threads[i].join(); + for (unsigned i = 0; i < numThreads; i++) { + threads[i] = std::thread(worker); + } + for (unsigned i = 0; i < numThreads; i++) { + threads[i].join(); + } output_() << "Done. Got " << count / benchTime << " QPS, " << errCount << " errors" << std::endl; return err; @@ -1035,15 +1085,23 @@ Error CommandsExecutor::commandProcessDatabase if (subCommand == "list"sv) { std::vector dbList; Error err = getAvailableDatabases(dbList); - if (!err.ok()) return err; - for (const std::string& dbName : dbList) output_() << dbName << std::endl; + if (!err.ok()) { + return err; + } + for (const std::string& dbName : dbList) { + output_() << dbName << std::endl; + } return Error(); } else if (subCommand == "use"sv) { std::string currentDsn = getCurrentDsn() + std::string(parser.NextToken()); stop(false); auto err = db().Connect(currentDsn, loop_); - if (err.ok()) err = db().Status(); - if (err.ok()) output_() << "Succesfully connected to " << currentDsn << std::endl; + if (err.ok()) { + err = db().Status(); + } + if (err.ok()) { + output_() << "Succesfully connected to " << currentDsn << std::endl; + } return err; } else if (subCommand == "create"sv) { auto dbName = parser.NextToken(); @@ -1081,10 +1139,14 @@ Error CommandsExecutor::seedBenchItems() { JsonBuilder(ser).Put("id", i).Put("data", i); auto err = item.Unsafe().FromJSON(ser.Slice()); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } err = db().Upsert(kBenchNamespace, item); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } return errOK; } @@ -1101,7 +1163,9 @@ Error CommandsExecutor::seedBenchItems() { JsonBuilder(ser).Put("id", i).Put("data", i); auto intErr = item.Unsafe().FromJSON(ser.Slice()); - if (intErr.ok()) intErr = db().Upsert(kBenchNamespace, item); + if (intErr.ok()) { + intErr = db().Upsert(kBenchNamespace, item); + } if (!intErr.ok()) { err = intErr; return; @@ -1144,7 +1208,9 @@ std::function CommandsExecutor CommandsExecutor::OnWALUpdate(reindexer::LSNPair LSNs, std::st template void CommandsExecutor::OnConnectionState(const Error& err) { - if (err.ok()) + if (err.ok()) { output_() << "[OnConnectionState] connected" << std::endl; - else + } else { output_() << "[OnConnectionState] closed, reason: " << err.what() << std::endl; + } } template diff --git a/cpp_src/cmd/reindexer_tool/commandsexecutor.h b/cpp_src/cmd/reindexer_tool/commandsexecutor.h index 2a6ff0af9..899940b86 100644 --- a/cpp_src/cmd/reindexer_tool/commandsexecutor.h +++ b/cpp_src/cmd/reindexer_tool/commandsexecutor.h @@ -73,7 +73,7 @@ class CommandsExecutor : public reindexer::IUpdatesObserver { Error queryResultsToJson(std::ostream& o, const typename DBInterface::QueryResultsT& r, bool isWALQuery, bool fstream); Error getAvailableDatabases(std::vector&); - void addCommandsSuggestions(std::string const& input, std::vector& suggestions); + void addCommandsSuggestions(const std::string& input, std::vector& suggestions); void checkForNsNameMatch(std::string_view str, std::vector& suggestions); void checkForCommandNameMatch(std::string_view str, std::initializer_list cmds, std::vector& suggestions); diff --git a/cpp_src/cmd/reindexer_tool/commandsprocessor.cc b/cpp_src/cmd/reindexer_tool/commandsprocessor.cc index 9f7cf47b1..fb435a0de 100644 --- a/cpp_src/cmd/reindexer_tool/commandsprocessor.cc +++ b/cpp_src/cmd/reindexer_tool/commandsprocessor.cc @@ -29,8 +29,8 @@ Error CommandsProcessor::process(const std::string& command) { #if REINDEX_WITH_REPLXX template template -void CommandsProcessor::setCompletionCallback(T& rx, void (T::*set_completion_callback)(new_v_callback_t const&)) { - (rx.*set_completion_callback)([this](std::string const& input, int) -> replxx::Replxx::completions_t { +void CommandsProcessor::setCompletionCallback(T& rx, void (T::*set_completion_callback)(const new_v_callback_t&)) { + (rx.*set_completion_callback)([this](const std::string& input, int) -> replxx::Replxx::completions_t { std::vector completions; const auto err = executor_.GetSuggestions(input, completions); replxx::Replxx::completions_t result; @@ -45,12 +45,14 @@ void CommandsProcessor::setCompletionCallback(T& rx, void (T::*set_ template template -void CommandsProcessor::setCompletionCallback(T& rx, void (T::*set_completion_callback)(old_v_callback_t const&, void*)) { +void CommandsProcessor::setCompletionCallback(T& rx, void (T::*set_completion_callback)(const old_v_callback_t&, void*)) { (rx.*set_completion_callback)( - [this](std::string const& input, int, void*) -> replxx::Replxx::completions_t { + [this](const std::string& input, int, void*) -> replxx::Replxx::completions_t { std::vector completions; const auto err = executor_.GetSuggestions(input, completions); - if (!err.ok()) return {}; + if (!err.ok()) { + return {}; + } return completions; }, nullptr); @@ -95,14 +97,18 @@ bool CommandsProcessor::interactive() { // main repl loop while (executor_.GetStatus().running) { - char const* input = nullptr; + const char* input = nullptr; do { input = rx.input(prompt); } while (!input && errno == EAGAIN); - if (input == nullptr) break; + if (input == nullptr) { + break; + } - if (!*input) continue; + if (!*input) { + continue; + } Error err = process(input); if (!err.ok()) { @@ -119,7 +125,9 @@ bool CommandsProcessor::interactive() { while (executor_.GetStatus().running) { std::string command; std::cout << prompt; - if (!std::getline(std::cin, command)) break; + if (!std::getline(std::cin, command)) { + break; + } Error err = process(command); if (!err.ok()) { std::cerr << "ERROR: " << err.what() << std::endl; diff --git a/cpp_src/cmd/reindexer_tool/commandsprocessor.h b/cpp_src/cmd/reindexer_tool/commandsprocessor.h index 85ed9df2d..46874628b 100644 --- a/cpp_src/cmd/reindexer_tool/commandsprocessor.h +++ b/cpp_src/cmd/reindexer_tool/commandsprocessor.h @@ -9,8 +9,8 @@ namespace reindexer_tool { #if REINDEX_WITH_REPLXX -typedef std::function old_v_callback_t; -typedef std::function new_v_callback_t; +typedef std::function old_v_callback_t; +typedef std::function new_v_callback_t; #endif // REINDEX_WITH_REPLXX template @@ -35,9 +35,9 @@ class CommandsProcessor { #if REINDEX_WITH_REPLXX template - void setCompletionCallback(T& rx, void (T::*set_completion_callback)(new_v_callback_t const&)); + void setCompletionCallback(T& rx, void (T::*set_completion_callback)(const new_v_callback_t&)); template - void setCompletionCallback(T& rx, void (T::*set_completion_callback)(old_v_callback_t const&, void*)); + void setCompletionCallback(T& rx, void (T::*set_completion_callback)(const old_v_callback_t&, void*)); #endif // REINDEX_WITH_REPLXX Error process(const std::string& command); diff --git a/cpp_src/cmd/reindexer_tool/iotools.h b/cpp_src/cmd/reindexer_tool/iotools.h index 2d0f04159..8f56648b0 100644 --- a/cpp_src/cmd/reindexer_tool/iotools.h +++ b/cpp_src/cmd/reindexer_tool/iotools.h @@ -17,7 +17,9 @@ class Output { } std::ostream& operator()() { - if (!isCout_ && !f_.is_open()) throw Error(errLogic, "%s", strerror(errState_)); + if (!isCout_ && !f_.is_open()) { + throw Error(errLogic, "%s", strerror(errState_)); + } return isCout_ ? std::cout : f_; } @@ -34,13 +36,19 @@ class LineParser { public: LineParser(const std::string& line) : line_(line), cur_(line.data()) {} std::string_view NextToken() { - while (*cur_ == ' ' || *cur_ == '\t') cur_++; + while (*cur_ == ' ' || *cur_ == '\t') { + cur_++; + } const char* next = cur_; - while (*next != ' ' && *next != '\t' && *next) next++; + while (*next != ' ' && *next != '\t' && *next) { + next++; + } std::string_view ret(cur_, next - cur_); cur_ = next; - while (*cur_ == ' ' || *cur_ == '\t') cur_++; + while (*cur_ == ' ' || *cur_ == '\t') { + cur_++; + } return ret; } bool End() { return *cur_ == 0; } diff --git a/cpp_src/cmd/reindexer_tool/reindexer_tool.cc b/cpp_src/cmd/reindexer_tool/reindexer_tool.cc index 2e2368a85..d3630ba08 100644 --- a/cpp_src/cmd/reindexer_tool/reindexer_tool.cc +++ b/cpp_src/cmd/reindexer_tool/reindexer_tool.cc @@ -161,11 +161,15 @@ int main(int argc, char* argv[]) { CommandsProcessor commandsProcessor(args::get(outFileName), args::get(fileName), args::get(connThreads), config); err = commandsProcessor.Connect(dsn, reindexer::client::ConnectOpts().CreateDBIfMissing(createDBF && args::get(createDBF))); - if (err.ok()) ok = commandsProcessor.Run(args::get(command)); + if (err.ok()) { + ok = commandsProcessor.Run(args::get(command)); + } } else if (checkIfStartsWithCS("builtin://"sv, dsn)) { CommandsProcessor commandsProcessor(args::get(outFileName), args::get(fileName), args::get(connThreads)); err = commandsProcessor.Connect(dsn, ConnectOpts().DisableReplication()); - if (err.ok()) ok = commandsProcessor.Run(args::get(command)); + if (err.ok()) { + ok = commandsProcessor.Run(args::get(command)); + } } else { #ifdef _WIN32 std::cerr << "Invalid DSN format: " << dsn << " Must begin from cproto:// or builtin://" << std::endl; diff --git a/cpp_src/cmd/reindexer_tool/tableviewscroller.cc b/cpp_src/cmd/reindexer_tool/tableviewscroller.cc index 75ebf609e..dacf7f6f0 100644 --- a/cpp_src/cmd/reindexer_tool/tableviewscroller.cc +++ b/cpp_src/cmd/reindexer_tool/tableviewscroller.cc @@ -62,7 +62,9 @@ TableViewScroller::TableViewScroller(const QueryResultsT& r, rein template void TableViewScroller::Scroll(Output& output, const std::function& isCanceled) { - if (isCanceled()) return; + if (isCanceled()) { + return; + } reindexer::TerminalSize terminalSize = reindexer::getTerminalSize(); reindexer::TableCalculator tableCalculator(r_, terminalSize.width); @@ -71,7 +73,9 @@ void TableViewScroller::Scroll(Output& output, const std::functio #ifndef WIN32 FILE* pfile = nullptr; - if (viaMoreCmd) pfile = popen("more", "w"); + if (viaMoreCmd) { + pfile = popen("more", "w"); + } #else viaMoreCmd = false; #endif @@ -96,7 +100,9 @@ void TableViewScroller::Scroll(Output& output, const std::functio } #ifndef WIN32 - if (pfile) pclose(pfile); + if (pfile) { + pclose(pfile); + } #endif } diff --git a/cpp_src/core/activity_context.cc b/cpp_src/core/activity_context.cc index af2c7d52c..3027cc1e2 100644 --- a/cpp_src/core/activity_context.cc +++ b/cpp_src/core/activity_context.cc @@ -28,7 +28,9 @@ void ActivityContainer::Unregister(const RdxActivityContext* context) { } void ActivityContainer::Reregister(const RdxActivityContext* oldCtx, const RdxActivityContext* newCtx) { - if (oldCtx == newCtx) return; + if (oldCtx == newCtx) { + return; + } std::unique_lock lck(mtx_); const auto eraseCount = cont_.erase(oldCtx); @@ -46,7 +48,9 @@ std::vector ActivityContainer::List() { { std::lock_guard lck(mtx_); ret.reserve(cont_.size()); - for (const RdxActivityContext* ctx : cont_) ret.emplace_back(*ctx); + for (const RdxActivityContext* ctx : cont_) { + ret.emplace_back(*ctx); + } } return ret; } @@ -86,7 +90,9 @@ void Activity::GetJSON(WrSerializer& ser) const { using namespace std::chrono; JsonBuilder builder(ser); builder.Put("client", activityTracer); - if (!user.empty()) builder.Put("user", user); + if (!user.empty()) { + builder.Put("user", user); + } builder.Put("query", query); builder.Put("query_id", id); std::time_t t = system_clock_w::to_time_t(startTime); @@ -96,7 +102,9 @@ void Activity::GetJSON(WrSerializer& ser) const { ss << buffer << '.' << std::setw(3) << std::setfill('0') << (duration_cast(startTime.time_since_epoch()).count() % 1000); builder.Put("query_start", ss.str()); builder.Put("state", DescribeState(state)); - if (state == WaitLock) builder.Put("lock_description", "Wait lock for " + std::string(description)); + if (state == WaitLock) { + builder.Put("lock_description", "Wait lock for " + std::string(description)); + } builder.End(); } @@ -105,18 +113,16 @@ RdxActivityContext::RdxActivityContext(std::string_view activityTracer, std::str : data_{nextId(), std::string(activityTracer), std::string(user), std::string(query), ipConnectionId, Activity::InProgress, system_clock_w::now(), ""sv}, state_(serializeState(clientState ? Activity::Sending : Activity::InProgress)), - parent_(&parent) -{ + parent_(&parent) { parent_->Register(this); } // NOLINTNEXTLINE (performance-noexcept-move-constructor) RdxActivityContext::RdxActivityContext(RdxActivityContext&& other) - : data_(other.data_), - state_(other.state_.load(std::memory_order_relaxed)), - parent_(other.parent_) -{ - if (parent_) parent_->Reregister(&other, this); + : data_(other.data_), state_(other.state_.load(std::memory_order_relaxed)), parent_(other.parent_) { + if (parent_) { + parent_->Reregister(&other, this); + } other.parent_ = nullptr; } diff --git a/cpp_src/core/activity_context.h b/cpp_src/core/activity_context.h index b6780f5b6..f8cef62df 100644 --- a/cpp_src/core/activity_context.h +++ b/cpp_src/core/activity_context.h @@ -94,7 +94,9 @@ class RdxActivityContext { int ipConnectionId, bool clientState = false); RdxActivityContext(RdxActivityContext&&); ~RdxActivityContext() { - if (parent_) parent_->Unregister(this); + if (parent_) { + parent_->Unregister(this); + } assertrx(refCount_.load(std::memory_order_relaxed) == 0u); } operator Activity() const; diff --git a/cpp_src/core/cbinding/reindexer_c.cc b/cpp_src/core/cbinding/reindexer_c.cc index a7f21ffbc..cb891ab4e 100644 --- a/cpp_src/core/cbinding/reindexer_c.cc +++ b/cpp_src/core/cbinding/reindexer_c.cc @@ -449,17 +449,25 @@ reindexer_error reindexer_connect(uintptr_t rx, reindexer_string dsn, ConnectOpt } Reindexer* db = reinterpret_cast(rx); - if (!db) return error2c(err_not_init); + if (!db) { + return error2c(err_not_init); + } Error err = db->Connect(str2c(dsn), opts); - if (err.ok() && db->NeedTraceActivity()) db->SetActivityTracer("builtin", ""); + if (err.ok() && db->NeedTraceActivity()) { + db->SetActivityTracer("builtin", ""); + } return error2c(err); } reindexer_error reindexer_init_system_namespaces(uintptr_t rx) { Reindexer* db = reinterpret_cast(rx); - if (!db) return error2c(err_not_init); + if (!db) { + return error2c(err_not_init); + } Error err = db->InitSystemNamespaces(); - if (err.ok() && db->NeedTraceActivity()) db->SetActivityTracer("builtin", ""); + if (err.ok() && db->NeedTraceActivity()) { + db->SetActivityTracer("builtin", ""); + } return error2c(err); } @@ -520,7 +528,9 @@ reindexer_ret reindexer_select_query(uintptr_t rx, struct reindexer_buffer in, i ActiveQueryScope scope(q, QuerySelect); err = rdxKeeper.db().Select(q, *result); - if (q.GetDebugLevel() >= LogError && err.code() != errOK) logPrintf(LogError, "Query error %s", err.what()); + if (q.GetDebugLevel() >= LogError && err.code() != errOK) { + logPrintf(LogError, "Query error %s", err.what()); + } if (err.ok()) { results2c(std::move(result), &out, as_json, pt_versions, pt_versions_count); } else { @@ -555,7 +565,9 @@ reindexer_ret reindexer_delete_query(uintptr_t rx, reindexer_buffer in, reindexe ActiveQueryScope scope(q, QueryDelete); res = rdxKeeper.db().Delete(q, *result); - if (q.GetDebugLevel() >= LogError && res.code() != errOK) logPrintf(LogError, "Query error %s", res.what()); + if (q.GetDebugLevel() >= LogError && res.code() != errOK) { + logPrintf(LogError, "Query error %s", res.what()); + } if (res.ok()) { results2c(std::move(result), &out); } @@ -584,7 +596,9 @@ reindexer_ret reindexer_update_query(uintptr_t rx, reindexer_buffer in, reindexe ActiveQueryScope scope(q, QueryUpdate); res = rdxKeeper.db().Update(q, *result); - if (q.GetDebugLevel() >= LogError && res.code() != errOK) logPrintf(LogError, "Query error %s", res.what()); + if (q.GetDebugLevel() >= LogError && res.code() != errOK) { + logPrintf(LogError, "Query error %s", res.what()); + } if (res.ok()) { int32_t ptVers = -1; results2c(std::move(result), &out, 0, &ptVers, 1); diff --git a/cpp_src/core/cbinding/reindexer_c.h b/cpp_src/core/cbinding/reindexer_c.h index 5b2129040..cc1fc55c6 100644 --- a/cpp_src/core/cbinding/reindexer_c.h +++ b/cpp_src/core/cbinding/reindexer_c.h @@ -5,8 +5,8 @@ extern "C" { #endif #include -#include "reindexer_ctypes.h" #include "core/type_consts.h" +#include "reindexer_ctypes.h" uintptr_t init_reindexer(); uintptr_t init_reindexer_with_config(reindexer_config config); @@ -41,10 +41,10 @@ reindexer_ret reindexer_commit_transaction(uintptr_t rx, uintptr_t tr, reindexer reindexer_error reindexer_rollback_transaction(uintptr_t rx, uintptr_t tr); reindexer_ret reindexer_modify_item_packed(uintptr_t rx, reindexer_buffer args, reindexer_buffer data, reindexer_ctx_info ctx_info); -reindexer_ret reindexer_select(uintptr_t rx, reindexer_string query, int as_json, int32_t *pt_versions, int pt_versions_count, +reindexer_ret reindexer_select(uintptr_t rx, reindexer_string query, int as_json, int32_t* pt_versions, int pt_versions_count, reindexer_ctx_info ctx_info); -reindexer_ret reindexer_select_query(uintptr_t rx, reindexer_buffer in, int as_json, int32_t *pt_versions, int pt_versions_count, +reindexer_ret reindexer_select_query(uintptr_t rx, reindexer_buffer in, int as_json, int32_t* pt_versions, int pt_versions_count, reindexer_ctx_info ctx_info); reindexer_ret reindexer_delete_query(uintptr_t rx, reindexer_buffer in, reindexer_ctx_info ctx_info); reindexer_ret reindexer_update_query(uintptr_t rx, reindexer_buffer in, reindexer_ctx_info ctx_info); @@ -53,7 +53,7 @@ reindexer_buffer reindexer_cptr2cjson(uintptr_t results_ptr, uintptr_t cptr, int void reindexer_free_cjson(reindexer_buffer b); reindexer_error reindexer_free_buffer(reindexer_resbuffer in); -reindexer_error reindexer_free_buffers(reindexer_resbuffer *in, int count); +reindexer_error reindexer_free_buffers(reindexer_resbuffer* in, int count); reindexer_error reindexer_commit(uintptr_t rx, reindexer_string nsName); @@ -65,7 +65,7 @@ reindexer_error reindexer_delete_meta(uintptr_t rx, reindexer_string ns, reindex reindexer_error reindexer_cancel_context(reindexer_ctx_info ctx_info, ctx_cancel_type how); -void reindexer_enable_logger(void (*logWriter)(int level, char *msg)); +void reindexer_enable_logger(void (*logWriter)(int level, char* msg)); void reindexer_disable_logger(); void reindexer_init_locale(); diff --git a/cpp_src/core/cbinding/reindexer_ctypes.h b/cpp_src/core/cbinding/reindexer_ctypes.h index 32e4188c7..07d6fcdee 100644 --- a/cpp_src/core/cbinding/reindexer_ctypes.h +++ b/cpp_src/core/cbinding/reindexer_ctypes.h @@ -12,7 +12,7 @@ typedef struct reindexer_config { } reindexer_config; typedef struct reindexer_buffer { - uint8_t *data; + uint8_t* data; int len; } reindexer_buffer; @@ -24,12 +24,12 @@ typedef struct reindexer_resbuffer { } reindexer_resbuffer; typedef struct reindexer_error { - const char *what; + const char* what; int code; } reindexer_error; typedef struct reindexer_string { - void *p; + void* p; int32_t n; int8_t reserved[4]; } reindexer_string; diff --git a/cpp_src/core/cbinding/resultserializer.cc b/cpp_src/core/cbinding/resultserializer.cc index a5085e52a..0c0041566 100644 --- a/cpp_src/core/cbinding/resultserializer.cc +++ b/cpp_src/core/cbinding/resultserializer.cc @@ -41,7 +41,9 @@ void WrResultSerializer::putQueryParams(const QueryResults* results) { for (int i = 0; i < totalCnt; i++) { const TagsMatcher& tm = results->getTagsMatcher(i); - if (int32_t(tm.version() ^ tm.stateToken()) != opts_.ptVersions[i]) cnt++; + if (int32_t(tm.version() ^ tm.stateToken()) != opts_.ptVersions[i]) { + cnt++; + } } PutVarUint(cnt); for (int i = 0; i < totalCnt; i++) { @@ -149,7 +151,9 @@ void WrResultSerializer::putItemParams(const QueryResults* result, int idx, bool default: throw Error(errParams, "Can't serialize query results: unknown format %d", int((opts_.flags & kResultsFormatMask))); } - if (!err.ok()) throw Error(errParseBin, "Internal error serializing query results: %s", err.what()); + if (!err.ok()) { + throw Error(errParseBin, "Internal error serializing query results: %s", err.what()); + } } void WrResultSerializer::putPayloadType(const QueryResults* results, int nsid) { @@ -180,14 +184,24 @@ bool WrResultSerializer::PutResults(const QueryResults* result) { } // Result has items from multiple namespaces, so pass nsid to each item - if (result->getMergedNSCount() > 1) opts_.flags |= kResultsWithNsID; + if (result->getMergedNSCount() > 1) { + opts_.flags |= kResultsWithNsID; + } // Result has joined items, so pass them to client within items from main NS - if (result->joined_.size() > 0) opts_.flags |= kResultsWithJoined; + if (result->joined_.size() > 0) { + opts_.flags |= kResultsWithJoined; + } - if (result->haveRank) opts_.flags |= kResultsWithRank; - if (result->needOutputRank) opts_.flags |= kResultsNeedOutputRank; + if (result->haveRank) { + opts_.flags |= kResultsWithRank; + } + if (result->needOutputRank) { + opts_.flags |= kResultsNeedOutputRank; + } // If data is not cacheable, just do not pass item's ID and LSN. Clients should not cache this data - if (result->nonCacheableData) opts_.flags &= ~kResultsWithItemID; + if (result->nonCacheableData) { + opts_.flags &= ~kResultsWithItemID; + } // MsgPack items contain fields names so there is no need to transfer payload types // and joined data, as well as for JSON (they both contain it already) if ((opts_.flags & kResultsFormatMask) == kResultsJson || (opts_.flags & kResultsFormatMask) == kResultsMsgPack) { @@ -212,15 +226,21 @@ bool WrResultSerializer::PutResults(const QueryResults* result) { } for (auto it = jIt.begin(), end = jIt.end(); it != end; ++it, ++joinedField) { PutVarUint(it.ItemsCount()); - if (it.ItemsCount() == 0) continue; + if (it.ItemsCount() == 0) { + continue; + } QueryResults qr = it.ToQueryResults(); qr.addNSContext(result->getPayloadType(joinedField), result->getTagsMatcher(joinedField), result->getFieldsFilter(joinedField), result->getSchema(joinedField)); - for (size_t idx = 0, qrCnt = qr.Count(); idx < qrCnt; ++idx) putItemParams(&qr, idx, false); + for (size_t idx = 0, qrCnt = qr.Count(); idx < qrCnt; ++idx) { + putItemParams(&qr, idx, false); + } } } } - if (i == 0) grow((opts_.fetchLimit - 1) * (len_ - saveLen)); + if (i == 0) { + grow((opts_.fetchLimit - 1) * (len_ - saveLen)); + } } return opts_.fetchOffset + opts_.fetchLimit >= result->Count(); } diff --git a/cpp_src/core/cjson/baseencoder.cc b/cpp_src/core/cjson/baseencoder.cc index 5b6dfe564..992a268a6 100644 --- a/cpp_src/core/cjson/baseencoder.cc +++ b/cpp_src/core/cjson/baseencoder.cc @@ -27,8 +27,7 @@ void BaseEncoder::Encode(std::string_view tuple, Builder& builder, IAdd [[maybe_unused]] const ctag begTag = rdser.GetCTag(); assertrx(begTag.Type() == TAG_OBJECT); Builder objNode = builder.Object(nullptr); - while (encode(nullptr, rdser, objNode, true)) - ; + while (encode(nullptr, rdser, objNode, true)); if (ds) { assertrx(!ds->GetJoinsDatasource()); ds->PutAdditionalFields(objNode); @@ -51,8 +50,7 @@ void BaseEncoder::Encode(ConstPayload& pl, Builder& builder, IAdditiona [[maybe_unused]] const ctag begTag = rdser.GetCTag(); assertrx(begTag.Type() == TAG_OBJECT); Builder objNode = builder.Object(nullptr); - while (encode(&pl, rdser, objNode, true)) - ; + while (encode(&pl, rdser, objNode, true)); if (ds) { if (const auto joinsDs = ds->GetJoinsDatasource()) { @@ -93,7 +91,9 @@ const TagsLengths& BaseEncoder::GetTagsMeasures(ConstPayload& pl, IEnco template void BaseEncoder::collectJoinedItemsTagsSizes(IEncoderDatasourceWithJoins* ds, size_t rowid) { const size_t itemsCount = ds->GetJoinedRowItemsCount(rowid); - if (!itemsCount) return; + if (!itemsCount) { + return; + } BaseEncoder subEnc(&ds->GetJoinedItemTagsMatcher(rowid), &ds->GetJoinedItemFieldsFilter(rowid)); for (size_t i = 0; i < itemsCount; ++i) { @@ -105,7 +105,9 @@ void BaseEncoder::collectJoinedItemsTagsSizes(IEncoderDatasourceWithJoi template void BaseEncoder::encodeJoinedItems(Builder& builder, IEncoderDatasourceWithJoins* ds, size_t rowid) { const size_t itemsCount = ds->GetJoinedRowItemsCount(rowid); - if (!itemsCount) return; + if (!itemsCount) { + return; + } std::string nsTagName("joined_" + ds->GetJoinedItemNamespace(rowid)); auto arrNode = builder.Array(nsTagName); @@ -144,7 +146,9 @@ bool BaseEncoder::encode(ConstPayload* pl, Serializer& rdser, Builder& // get field from indexed field if (tagField >= 0) { - if (!pl) throw Error(errParams, "Trying to encode index field %d without payload", tagField); + if (!pl) { + throw Error(errParams, "Trying to encode index field %d without payload", tagField); + } const auto& f = pl->Type().Field(tagField); if (!f.IsArray() && objectScalarIndexes_.test(tagField)) { throw Error(errParams, "Non-array field '%s' [%d] from '%s' can only be encoded once.", f.Name(), tagField, pl->Type().Name()); @@ -172,7 +176,9 @@ bool BaseEncoder::encode(ConstPayload* pl, Serializer& rdser, Builder& } case TAG_NULL: objectScalarIndexes_.set(tagField); - if (visible) builder.Null(tagName); + if (visible) { + builder.Null(tagName); + } break; case TAG_VARINT: case TAG_DOUBLE: @@ -182,7 +188,9 @@ bool BaseEncoder::encode(ConstPayload* pl, Serializer& rdser, Builder& case TAG_OBJECT: case TAG_UUID: objectScalarIndexes_.set(tagField); - if (visible) builder.Put(tagName, pl->Get(tagField, cnt), cnt); + if (visible) { + builder.Put(tagName, pl->Get(tagField, cnt), cnt); + } ++cnt; break; } @@ -210,19 +218,19 @@ bool BaseEncoder::encode(ConstPayload* pl, Serializer& rdser, Builder& builder.Array(tagName, rdser, atagType, atagCount); } else { const KeyValueType kvt{atagType}; - for (size_t i = 0; i < atagCount; ++i) rdser.SkipRawVariant(kvt); + for (size_t i = 0; i < atagCount; ++i) { + rdser.SkipRawVariant(kvt); + } } break; } case TAG_OBJECT: { if (visible) { auto objNode = builder.Object(tagName); - while (encode(pl, rdser, objNode, true)) - ; + while (encode(pl, rdser, objNode, true)); } else { thread_local static Builder objNode; - while (encode(pl, rdser, objNode, false)) - ; + while (encode(pl, rdser, objNode, false)); } break; } @@ -320,7 +328,9 @@ bool BaseEncoder::collectTagsSizes(ConstPayload& pl, Serializer& rdser) } } } - if (tagName && filter_) curTagsPath_.pop_back(); + if (tagName && filter_) { + curTagsPath_.pop_back(); + } return true; } diff --git a/cpp_src/core/cjson/baseencoder.h b/cpp_src/core/cjson/baseencoder.h index 1ad04b51c..9eafa4808 100644 --- a/cpp_src/core/cjson/baseencoder.h +++ b/cpp_src/core/cjson/baseencoder.h @@ -23,26 +23,26 @@ class IEncoderDatasourceWithJoins { virtual size_t GetJoinedRowsCount() const noexcept = 0; virtual size_t GetJoinedRowItemsCount(size_t rowId) const = 0; virtual ConstPayload GetJoinedItemPayload(size_t rowid, size_t plIndex) const = 0; - virtual const std::string &GetJoinedItemNamespace(size_t rowid) const noexcept = 0; - virtual const TagsMatcher &GetJoinedItemTagsMatcher(size_t rowid) noexcept = 0; - virtual const FieldsSet &GetJoinedItemFieldsFilter(size_t rowid) noexcept = 0; + virtual const std::string& GetJoinedItemNamespace(size_t rowid) const noexcept = 0; + virtual const TagsMatcher& GetJoinedItemTagsMatcher(size_t rowid) noexcept = 0; + virtual const FieldsSet& GetJoinedItemFieldsFilter(size_t rowid) noexcept = 0; }; template class IAdditionalDatasource { public: - virtual void PutAdditionalFields(Builder &) const = 0; - virtual IEncoderDatasourceWithJoins *GetJoinsDatasource() noexcept = 0; + virtual void PutAdditionalFields(Builder&) const = 0; + virtual IEncoderDatasourceWithJoins* GetJoinsDatasource() noexcept = 0; }; template class BaseEncoder { public: - BaseEncoder(const TagsMatcher *tagsMatcher, const FieldsSet *filter = nullptr); - void Encode(ConstPayload &pl, Builder &builder, IAdditionalDatasource * = nullptr); - void Encode(std::string_view tuple, Builder &wrSer, IAdditionalDatasource *); + BaseEncoder(const TagsMatcher* tagsMatcher, const FieldsSet* filter = nullptr); + void Encode(ConstPayload& pl, Builder& builder, IAdditionalDatasource* = nullptr); + void Encode(std::string_view tuple, Builder& wrSer, IAdditionalDatasource*); - const TagsLengths &GetTagsMeasures(ConstPayload &pl, IEncoderDatasourceWithJoins *ds = nullptr); + const TagsLengths& GetTagsMeasures(ConstPayload& pl, IEncoderDatasourceWithJoins* ds = nullptr); protected: using IndexedTagsPathInternalT = IndexedTagsPathImpl<16>; @@ -50,20 +50,20 @@ class BaseEncoder { constexpr static bool kWithFieldExtractor = std::is_same_v; struct DummyTagsPathScope { - DummyTagsPathScope(TagsPath & /*tagsPath*/, int16_t /*tagName*/) noexcept {} + DummyTagsPathScope(TagsPath& /*tagsPath*/, int16_t /*tagName*/) noexcept {} }; using PathScopeT = std::conditional_t, DummyTagsPathScope>; - bool encode(ConstPayload *pl, Serializer &rdser, Builder &builder, bool visible); - void encodeJoinedItems(Builder &builder, IEncoderDatasourceWithJoins *ds, size_t joinedIdx); - bool collectTagsSizes(ConstPayload &pl, Serializer &rdser); - void collectJoinedItemsTagsSizes(IEncoderDatasourceWithJoins *ds, size_t rowid); + bool encode(ConstPayload* pl, Serializer& rdser, Builder& builder, bool visible); + void encodeJoinedItems(Builder& builder, IEncoderDatasourceWithJoins* ds, size_t joinedIdx); + bool collectTagsSizes(ConstPayload& pl, Serializer& rdser); + void collectJoinedItemsTagsSizes(IEncoderDatasourceWithJoins* ds, size_t rowid); - std::string_view getPlTuple(ConstPayload &pl); + std::string_view getPlTuple(ConstPayload& pl); - const TagsMatcher *tagsMatcher_; + const TagsMatcher* tagsMatcher_; std::array fieldsoutcnt_{0}; - const FieldsSet *filter_; + const FieldsSet* filter_; WrSerializer tmpPlTuple_; TagsPath curTagsPath_; IndexedTagsPathInternalT indexedTagsPath_; diff --git a/cpp_src/core/cjson/cjsonbuilder.cc b/cpp_src/core/cjson/cjsonbuilder.cc index 9c32c5c4d..9273e5b11 100644 --- a/cpp_src/core/cjson/cjsonbuilder.cc +++ b/cpp_src/core/cjson/cjsonbuilder.cc @@ -2,7 +2,7 @@ namespace reindexer { -CJsonBuilder::CJsonBuilder(WrSerializer &ser, ObjType type, const TagsMatcher *tm, int tagName) : tm_(tm), ser_(&ser), type_(type) { +CJsonBuilder::CJsonBuilder(WrSerializer& ser, ObjType type, const TagsMatcher* tm, int tagName) : tm_(tm), ser_(&ser), type_(type) { switch (type_) { case ObjType::TypeArray: case ObjType::TypeObjectArray: @@ -39,7 +39,7 @@ void CJsonBuilder::Array(int tagName, span data, int /*offset*/) { } } -CJsonBuilder &CJsonBuilder::Put(int tagName, bool arg, int /*offset*/) { +CJsonBuilder& CJsonBuilder::Put(int tagName, bool arg, int /*offset*/) { if (type_ == ObjType::TypeArray) { itemType_ = TAG_BOOL; } else { @@ -50,7 +50,7 @@ CJsonBuilder &CJsonBuilder::Put(int tagName, bool arg, int /*offset*/) { return *this; } -CJsonBuilder &CJsonBuilder::Put(int tagName, int64_t arg, int /*offset*/) { +CJsonBuilder& CJsonBuilder::Put(int tagName, int64_t arg, int /*offset*/) { if (type_ == ObjType::TypeArray) { itemType_ = TAG_VARINT; } else { @@ -61,7 +61,7 @@ CJsonBuilder &CJsonBuilder::Put(int tagName, int64_t arg, int /*offset*/) { return *this; } -CJsonBuilder &CJsonBuilder::Put(int tagName, int arg, int /*offset*/) { +CJsonBuilder& CJsonBuilder::Put(int tagName, int arg, int /*offset*/) { if (type_ == ObjType::TypeArray) { itemType_ = TAG_VARINT; } else { @@ -72,7 +72,7 @@ CJsonBuilder &CJsonBuilder::Put(int tagName, int arg, int /*offset*/) { return *this; } -CJsonBuilder &CJsonBuilder::Put(int tagName, double arg, int /*offset*/) { +CJsonBuilder& CJsonBuilder::Put(int tagName, double arg, int /*offset*/) { if (type_ == ObjType::TypeArray) { itemType_ = TAG_DOUBLE; } else { @@ -83,7 +83,7 @@ CJsonBuilder &CJsonBuilder::Put(int tagName, double arg, int /*offset*/) { return *this; } -CJsonBuilder &CJsonBuilder::Put(int tagName, std::string_view arg, int /*offset*/) { +CJsonBuilder& CJsonBuilder::Put(int tagName, std::string_view arg, int /*offset*/) { if (type_ == ObjType::TypeArray) { itemType_ = TAG_STRING; } else { @@ -94,13 +94,13 @@ CJsonBuilder &CJsonBuilder::Put(int tagName, std::string_view arg, int /*offset* return *this; } -CJsonBuilder &CJsonBuilder::Put(int tagName, Uuid arg, int /*offset*/) { +CJsonBuilder& CJsonBuilder::Put(int tagName, Uuid arg, int /*offset*/) { ser_->PutCTag(ctag{TAG_UUID, tagName}); ser_->PutUuid(arg); return *this; } -CJsonBuilder &CJsonBuilder::Null(int tagName) { +CJsonBuilder& CJsonBuilder::Null(int tagName) { if (type_ == ObjType::TypeArray) { itemType_ = TAG_NULL; } else { @@ -110,37 +110,24 @@ CJsonBuilder &CJsonBuilder::Null(int tagName) { return *this; } -CJsonBuilder &CJsonBuilder::Ref(int tagName, const Variant &v, int field) { - v.Type().EvaluateOneOf( - [&](OneOf) { - ser_->PutCTag(ctag{TAG_VARINT, tagName, field}); - }, - [&](KeyValueType::Bool) { - ser_->PutCTag(ctag{TAG_BOOL, tagName, field}); - }, - [&](KeyValueType::Double) { - ser_->PutCTag(ctag{TAG_DOUBLE, tagName, field}); - }, - [&](KeyValueType::String) { - ser_->PutCTag(ctag{TAG_STRING, tagName, field}); - }, - [&](KeyValueType::Uuid) { - ser_->PutCTag(ctag{TAG_UUID, tagName, field}); - }, - [&](OneOf) { - ser_->PutCTag(ctag{TAG_NULL, tagName}); - }, - [](OneOf) noexcept { std::abort(); }); +CJsonBuilder& CJsonBuilder::Ref(int tagName, const Variant& v, int field) { + v.Type().EvaluateOneOf([&](OneOf) { ser_->PutCTag(ctag{TAG_VARINT, tagName, field}); }, + [&](KeyValueType::Bool) { ser_->PutCTag(ctag{TAG_BOOL, tagName, field}); }, + [&](KeyValueType::Double) { ser_->PutCTag(ctag{TAG_DOUBLE, tagName, field}); }, + [&](KeyValueType::String) { ser_->PutCTag(ctag{TAG_STRING, tagName, field}); }, + [&](KeyValueType::Uuid) { ser_->PutCTag(ctag{TAG_UUID, tagName, field}); }, + [&](OneOf) { ser_->PutCTag(ctag{TAG_NULL, tagName}); }, + [](OneOf) noexcept { std::abort(); }); return *this; } -CJsonBuilder &CJsonBuilder::ArrayRef(int tagName, int field, int count) { +CJsonBuilder& CJsonBuilder::ArrayRef(int tagName, int field, int count) { ser_->PutCTag(ctag{TAG_ARRAY, tagName, field}); ser_->PutVarUint(count); return *this; } -CJsonBuilder &CJsonBuilder::Put(int tagName, const Variant &kv, int offset) { +CJsonBuilder& CJsonBuilder::Put(int tagName, const Variant& kv, int offset) { kv.Type().EvaluateOneOf([&](KeyValueType::Int) { Put(tagName, int(kv), offset); }, [&](KeyValueType::Int64) { Put(tagName, int64_t(kv), offset); }, [&](KeyValueType::Double) { Put(tagName, double(kv), offset); }, @@ -148,7 +135,7 @@ CJsonBuilder &CJsonBuilder::Put(int tagName, const Variant &kv, int offset) { [&](KeyValueType::Null) { Null(tagName); }, [&](KeyValueType::Bool) { Put(tagName, bool(kv), offset); }, [&](KeyValueType::Tuple) { auto arrNode = Array(tagName); - for (auto &val : kv.getCompositeValues()) { + for (auto& val : kv.getCompositeValues()) { arrNode.Put(nullptr, val); } }, diff --git a/cpp_src/core/cjson/cjsonbuilder.h b/cpp_src/core/cjson/cjsonbuilder.h index ed3d79c8d..4ecd4a7bf 100644 --- a/cpp_src/core/cjson/cjsonbuilder.h +++ b/cpp_src/core/cjson/cjsonbuilder.h @@ -7,23 +7,23 @@ namespace reindexer { -void copyCJsonValue(TagType tagType, Serializer &rdser, WrSerializer &wrser); +void copyCJsonValue(TagType tagType, Serializer& rdser, WrSerializer& wrser); class CJsonBuilder { public: - CJsonBuilder(WrSerializer &ser, ObjType = ObjType::TypeObject, const TagsMatcher *tm = nullptr, int tagName = 0); + CJsonBuilder(WrSerializer& ser, ObjType = ObjType::TypeObject, const TagsMatcher* tm = nullptr, int tagName = 0); CJsonBuilder() noexcept : tm_(nullptr), ser_(nullptr), type_(ObjType::TypePlain) {} ~CJsonBuilder() { End(); } - CJsonBuilder(const CJsonBuilder &) = delete; - CJsonBuilder(CJsonBuilder &&other) noexcept + CJsonBuilder(const CJsonBuilder&) = delete; + CJsonBuilder(CJsonBuilder&& other) noexcept : tm_(other.tm_), ser_(other.ser_), type_(other.type_), savePos_(other.savePos_), count_(other.count_), itemType_(other.itemType_) { other.type_ = ObjType::TypePlain; } - CJsonBuilder &operator=(const CJsonBuilder &) = delete; - CJsonBuilder &operator=(CJsonBuilder &&) = delete; + CJsonBuilder& operator=(const CJsonBuilder&) = delete; + CJsonBuilder& operator=(CJsonBuilder&&) = delete; - void SetTagsMatcher(const TagsMatcher *tm) noexcept { tm_ = tm; } + void SetTagsMatcher(const TagsMatcher* tm) noexcept { tm_ = tm; } /// Start new object CJsonBuilder Object(int tagName); @@ -37,62 +37,74 @@ class CJsonBuilder { void Array(int tagName, span data, int /*offset*/ = 0) { ser_->PutCTag(ctag{TAG_ARRAY, tagName}); ser_->PutCArrayTag(carraytag(data.size(), TAG_STRING)); - for (auto d : data) ser_->PutVString(d); + for (auto d : data) { + ser_->PutVString(d); + } } void Array(int tagName, span data, int offset = 0); void Array(int tagName, span data, int /*offset*/ = 0) { ser_->PutCTag(ctag{TAG_ARRAY, tagName}); ser_->PutCArrayTag(carraytag(data.size(), TAG_VARINT)); - for (auto d : data) ser_->PutVarint(d); + for (auto d : data) { + ser_->PutVarint(d); + } } void Array(int tagName, span data, int /*offset*/ = 0) { ser_->PutCTag(ctag{TAG_ARRAY, tagName}); ser_->PutCArrayTag(carraytag(data.size(), TAG_VARINT)); - for (auto d : data) ser_->PutVarint(d); + for (auto d : data) { + ser_->PutVarint(d); + } } void Array(int tagName, span data, int /*offset*/ = 0) { ser_->PutCTag(ctag{TAG_ARRAY, tagName}); ser_->PutCArrayTag(carraytag(data.size(), TAG_BOOL)); - for (auto d : data) ser_->PutBool(d); + for (auto d : data) { + ser_->PutBool(d); + } } void Array(int tagName, span data, int /*offset*/ = 0) { ser_->PutCTag(ctag{TAG_ARRAY, tagName}); ser_->PutCArrayTag(carraytag(data.size(), TAG_DOUBLE)); - for (auto d : data) ser_->PutDouble(d); + for (auto d : data) { + ser_->PutDouble(d); + } } - void Array(int tagName, Serializer &ser, TagType tagType, int count) { + void Array(int tagName, Serializer& ser, TagType tagType, int count) { ser_->PutCTag(ctag{TAG_ARRAY, tagName}); ser_->PutCArrayTag(carraytag(count, tagType)); - while (count--) copyCJsonValue(tagType, ser, *ser_); + while (count--) { + copyCJsonValue(tagType, ser, *ser_); + } } template - CJsonBuilder &Put(std::nullptr_t, const T &arg, int offset = 0) { + CJsonBuilder& Put(std::nullptr_t, const T& arg, int offset = 0) { return Put(0, arg, offset); } void Write(std::string_view data) { ser_->Write(data); } - CJsonBuilder &Null(std::nullptr_t) { return Null(0); } - - CJsonBuilder &Put(int tagName, bool arg, int offset = 0); - CJsonBuilder &Put(int tagName, int arg, int offset = 0); - CJsonBuilder &Put(int tagName, int64_t arg, int offset = 0); - CJsonBuilder &Put(int tagName, double arg, int offset = 0); - CJsonBuilder &Put(int tagName, std::string_view arg, int offset = 0); - CJsonBuilder &Put(int tagName, Uuid arg, int offset = 0); - CJsonBuilder &Ref(int tagName, const Variant &v, int field); - CJsonBuilder &ArrayRef(int tagName, int field, int count); - CJsonBuilder &Null(int tagName); - CJsonBuilder &Put(int tagName, const Variant &kv, int offset = 0); - CJsonBuilder &Put(int tagName, const char *arg, int offset = 0) { return Put(tagName, std::string_view(arg), offset); } - CJsonBuilder &End() { + CJsonBuilder& Null(std::nullptr_t) { return Null(0); } + + CJsonBuilder& Put(int tagName, bool arg, int offset = 0); + CJsonBuilder& Put(int tagName, int arg, int offset = 0); + CJsonBuilder& Put(int tagName, int64_t arg, int offset = 0); + CJsonBuilder& Put(int tagName, double arg, int offset = 0); + CJsonBuilder& Put(int tagName, std::string_view arg, int offset = 0); + CJsonBuilder& Put(int tagName, Uuid arg, int offset = 0); + CJsonBuilder& Ref(int tagName, const Variant& v, int field); + CJsonBuilder& ArrayRef(int tagName, int field, int count); + CJsonBuilder& Null(int tagName); + CJsonBuilder& Put(int tagName, const Variant& kv, int offset = 0); + CJsonBuilder& Put(int tagName, const char* arg, int offset = 0) { return Put(tagName, std::string_view(arg), offset); } + CJsonBuilder& End() { switch (type_) { case ObjType::TypeArray: - *(reinterpret_cast(ser_->Buf() + savePos_)) = carraytag(count_, itemType_); + *(reinterpret_cast(ser_->Buf() + savePos_)) = carraytag(count_, itemType_); break; case ObjType::TypeObjectArray: - *(reinterpret_cast(ser_->Buf() + savePos_)) = carraytag(count_, TAG_OBJECT); + *(reinterpret_cast(ser_->Buf() + savePos_)) = carraytag(count_, TAG_OBJECT); break; case ObjType::TypeObject: ser_->PutCTag(kCTagEnd); @@ -109,8 +121,8 @@ class CJsonBuilder { private: inline void putTag(int tagName, TagType tagType) { ser_->PutCTag(ctag{tagType, tagName}); } - const TagsMatcher *tm_; - WrSerializer *ser_; + const TagsMatcher* tm_; + WrSerializer* ser_; ObjType type_; int savePos_ = 0; int count_ = 0; diff --git a/cpp_src/core/cjson/cjsondecoder.cc b/cpp_src/core/cjson/cjsondecoder.cc index 10bd1a7f8..26c7d8549 100644 --- a/cpp_src/core/cjson/cjsondecoder.cc +++ b/cpp_src/core/cjson/cjsondecoder.cc @@ -7,7 +7,7 @@ namespace reindexer { template -bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrser, FilterT filter, RecoderT recoder, TagOptT) { +bool CJsonDecoder::decodeCJson(Payload& pl, Serializer& rdser, WrSerializer& wrser, FilterT filter, RecoderT recoder, TagOptT) { const ctag tag = rdser.GetCTag(); TagType tagType = tag.Type(); if (tagType == TAG_END) { @@ -38,7 +38,7 @@ bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrs } else if (recoder.Recode(rdser, pl, tagName, wrser)) { // No more actions needed after recoding } else { - const auto &fieldRef{pl.Type().Field(field)}; + const auto& fieldRef{pl.Type().Field(field)}; const KeyValueType fieldType{fieldRef.Type()}; if (tagType == TAG_ARRAY) { if rx_unlikely (!fieldRef.IsArray()) { @@ -65,9 +65,7 @@ bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrs objectScalarIndexes_.set(field); pl.Set(field, cjsonValueToVariant(tagType, rdser, fieldType), true); fieldType.EvaluateOneOf( - [&](OneOf) { - wrser.PutCTag(ctag{TAG_VARINT, tagName, field}); - }, + [&](OneOf) { wrser.PutCTag(ctag{TAG_VARINT, tagName, field}); }, [&](OneOf) { wrser.PutCTag(ctag{fieldType.ToTagType(), tagName, field}); }, @@ -84,8 +82,7 @@ bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrs tagType = recoder.RegisterTagType(tagType, tagsPath_); wrser.PutCTag(ctag{tagType, tagName, field}); if (tagType == TAG_OBJECT) { - while (decodeCJson(pl, rdser, wrser, filter.MakeCleanCopy(), recoder.MakeCleanCopy(), NamedTagOpt{})) - ; + while (decodeCJson(pl, rdser, wrser, filter.MakeCleanCopy(), recoder.MakeCleanCopy(), NamedTagOpt{})); } else if (recoder.Recode(rdser, wrser)) { // No more actions needed after recoding } else if (tagType == TAG_ARRAY) { @@ -112,8 +109,7 @@ bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrs } else { // !match wrser.PutCTag(ctag{tagType, tagName, field}); - while (decodeCJson(pl, rdser, wrser, filter.MakeSkipFilter(), recoder.MakeCleanCopy(), NamedTagOpt{})) - ; + while (decodeCJson(pl, rdser, wrser, filter.MakeSkipFilter(), recoder.MakeCleanCopy(), NamedTagOpt{})); } } @@ -124,7 +120,7 @@ bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrs return true; } -[[nodiscard]] Variant CJsonDecoder::cjsonValueToVariant(TagType tagType, Serializer &rdser, KeyValueType fieldType) { +[[nodiscard]] Variant CJsonDecoder::cjsonValueToVariant(TagType tagType, Serializer& rdser, KeyValueType fieldType) { if (fieldType.Is() && tagType != TagType::TAG_STRING) { storage_.emplace_back(rdser.GetRawVariant(KeyValueType{tagType}).As()); return Variant(p_string(&storage_.back()), Variant::no_hold_t{}); @@ -133,22 +129,22 @@ bool CJsonDecoder::decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrs } } -RX_NO_INLINE void CJsonDecoder::throwTagReferenceError(ctag tag, const Payload &pl) { +RX_NO_INLINE void CJsonDecoder::throwTagReferenceError(ctag tag, const Payload& pl) { throw Error(errLogic, "Reference tag was found in transport CJSON for field %d[%s] in ns [%s]", tag.Field(), tagsMatcher_.tag2name(tag.Name()), pl.Type().Name()); } -RX_NO_INLINE void CJsonDecoder::throwUnexpectedArrayError(const PayloadFieldType &fieldRef) { +RX_NO_INLINE void CJsonDecoder::throwUnexpectedArrayError(const PayloadFieldType& fieldRef) { throw Error(errLogic, "Error parsing cjson field '%s' - got array, expected scalar %s", fieldRef.Name(), fieldRef.Type().Name()); } template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::DummyFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::DummyFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::DummyFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::DummyFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::RestrictingFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::RestrictingFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::RestrictingFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::RestrictingFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); } // namespace reindexer diff --git a/cpp_src/core/cjson/cjsondecoder.h b/cpp_src/core/cjson/cjsondecoder.h index 69221c7b6..443fea4c7 100644 --- a/cpp_src/core/cjson/cjsondecoder.h +++ b/cpp_src/core/cjson/cjsondecoder.h @@ -15,18 +15,18 @@ class WrSerializer; class Recoder { public: [[nodiscard]] virtual TagType Type(TagType oldTagType) = 0; - virtual void Recode(Serializer &, WrSerializer &) const = 0; - virtual void Recode(Serializer &, Payload &, int tagName, WrSerializer &) = 0; + virtual void Recode(Serializer&, WrSerializer&) const = 0; + virtual void Recode(Serializer&, Payload&, int tagName, WrSerializer&) = 0; [[nodiscard]] virtual bool Match(int field) noexcept = 0; - [[nodiscard]] virtual bool Match(TagType, const TagsPath &) = 0; - virtual void Serialize(WrSerializer &wrser) = 0; + [[nodiscard]] virtual bool Match(TagType, const TagsPath&) = 0; + virtual void Serialize(WrSerializer& wrser) = 0; virtual bool Reset() = 0; virtual ~Recoder() = default; }; class CJsonDecoder { public: - explicit CJsonDecoder(TagsMatcher &tagsMatcher, std::deque &storage) noexcept + explicit CJsonDecoder(TagsMatcher& tagsMatcher, std::deque& storage) noexcept : tagsMatcher_(tagsMatcher), storage_(storage) {} class SkipFilter { public: @@ -34,39 +34,39 @@ class CJsonDecoder { SkipFilter MakeSkipFilter() const noexcept { return SkipFilter(); } RX_ALWAYS_INLINE bool contains([[maybe_unused]] int field) const noexcept { return false; } - RX_ALWAYS_INLINE bool match(const TagsPath &) const noexcept { return false; } + RX_ALWAYS_INLINE bool match(const TagsPath&) const noexcept { return false; } }; class DummyFilter { public: DummyFilter MakeCleanCopy() const noexcept { return DummyFilter(); } SkipFilter MakeSkipFilter() const noexcept { return SkipFilter(); } - RX_ALWAYS_INLINE bool HasArraysFields(const PayloadTypeImpl &) const noexcept { return false; } + RX_ALWAYS_INLINE bool HasArraysFields(const PayloadTypeImpl&) const noexcept { return false; } RX_ALWAYS_INLINE bool contains([[maybe_unused]] int field) const noexcept { return true; } - RX_ALWAYS_INLINE bool match(const TagsPath &) const noexcept { return true; } + RX_ALWAYS_INLINE bool match(const TagsPath&) const noexcept { return true; } }; class IndexedSkipFilter { public: - IndexedSkipFilter(const FieldsSet &f) noexcept : f_(&f) {} + IndexedSkipFilter(const FieldsSet& f) noexcept : f_(&f) {} IndexedSkipFilter MakeCleanCopy() const noexcept { return IndexedSkipFilter(*f_); } IndexedSkipFilter MakeSkipFilter() const noexcept { return IndexedSkipFilter(*f_); } RX_ALWAYS_INLINE bool contains(int field) const noexcept { return f_->contains(field); } - RX_ALWAYS_INLINE bool match(const TagsPath &) const noexcept { return false; } + RX_ALWAYS_INLINE bool match(const TagsPath&) const noexcept { return false; } private: - const FieldsSet *f_; + const FieldsSet* f_; }; class RestrictingFilter { public: - RestrictingFilter(const FieldsSet &f) noexcept : f_(&f), match_(true) {} + RestrictingFilter(const FieldsSet& f) noexcept : f_(&f), match_(true) {} RestrictingFilter MakeCleanCopy() const noexcept { return RestrictingFilter(*f_); } IndexedSkipFilter MakeSkipFilter() const noexcept { return IndexedSkipFilter(*f_); } - RX_ALWAYS_INLINE bool HasArraysFields(const PayloadTypeImpl &pt) const noexcept { + RX_ALWAYS_INLINE bool HasArraysFields(const PayloadTypeImpl& pt) const noexcept { for (auto f : *f_) { if (f >= 0 && pt.Field(f).IsArray()) { return true; @@ -79,38 +79,38 @@ class CJsonDecoder { match_ = f_->contains(field); return match_; } - RX_ALWAYS_INLINE bool match(const TagsPath &tagsPath) noexcept { + RX_ALWAYS_INLINE bool match(const TagsPath& tagsPath) noexcept { match_ = match_ && f_->getTagsPathsLength() && f_->match(tagsPath); return match_; } private: - const FieldsSet *f_; + const FieldsSet* f_; bool match_; }; class DummyRecoder { public: RX_ALWAYS_INLINE DummyRecoder MakeCleanCopy() const noexcept { return DummyRecoder(); } - RX_ALWAYS_INLINE bool Recode(Serializer &, WrSerializer &) const noexcept { return false; } - RX_ALWAYS_INLINE bool Recode(Serializer &, Payload &, int, WrSerializer &) const noexcept { return false; } + RX_ALWAYS_INLINE bool Recode(Serializer&, WrSerializer&) const noexcept { return false; } + RX_ALWAYS_INLINE bool Recode(Serializer&, Payload&, int, WrSerializer&) const noexcept { return false; } RX_ALWAYS_INLINE TagType RegisterTagType(TagType tagType, int) const noexcept { return tagType; } - RX_ALWAYS_INLINE TagType RegisterTagType(TagType tagType, const TagsPath &) const noexcept { return tagType; } - RX_ALWAYS_INLINE void Serialize(WrSerializer &) const {} + RX_ALWAYS_INLINE TagType RegisterTagType(TagType tagType, const TagsPath&) const noexcept { return tagType; } + RX_ALWAYS_INLINE void Serialize(WrSerializer&) const {} }; class DefaultRecoder { public: - DefaultRecoder(Recoder &r) noexcept : r_(&r), needToRecode_(false) {} + DefaultRecoder(Recoder& r) noexcept : r_(&r), needToRecode_(false) {} RX_ALWAYS_INLINE DefaultRecoder MakeCleanCopy() const noexcept { return DefaultRecoder(*r_); } - RX_ALWAYS_INLINE bool Recode(Serializer &ser, WrSerializer &wser) const { + RX_ALWAYS_INLINE bool Recode(Serializer& ser, WrSerializer& wser) const { if (needToRecode_) { r_->Recode(ser, wser); } return needToRecode_; } - RX_ALWAYS_INLINE bool Recode(Serializer &ser, Payload &pl, int tagName, WrSerializer &wser) const { + RX_ALWAYS_INLINE bool Recode(Serializer& ser, Payload& pl, int tagName, WrSerializer& wser) const { if (needToRecode_) { r_->Recode(ser, pl, tagName, wser); } @@ -120,21 +120,21 @@ class CJsonDecoder { needToRecode_ = r_->Match(field); return needToRecode_ ? r_->Type(tagType) : tagType; } - RX_ALWAYS_INLINE TagType RegisterTagType(TagType tagType, const TagsPath &tagsPath) { + RX_ALWAYS_INLINE TagType RegisterTagType(TagType tagType, const TagsPath& tagsPath) { needToRecode_ = r_->Match(tagType, tagsPath); return needToRecode_ ? r_->Type(tagType) : tagType; } - RX_ALWAYS_INLINE void Serialize(WrSerializer &wser) const { r_->Serialize(wser); } + RX_ALWAYS_INLINE void Serialize(WrSerializer& wser) const { r_->Serialize(wser); } private: - Recoder *r_; + Recoder* r_; bool needToRecode_; }; struct NamedTagOpt {}; struct NamelessTagOpt {}; template - void Decode(Payload &pl, Serializer &rdSer, WrSerializer &wrSer, FilterT filter = FilterT(), RecoderT recoder = RecoderT()) { + void Decode(Payload& pl, Serializer& rdSer, WrSerializer& wrSer, FilterT filter = FilterT(), RecoderT recoder = RecoderT()) { static_assert(std::is_same_v || std::is_same_v, "Other filter types are not allowed for the public API"); static_assert(std::is_same_v || std::is_same_v, @@ -157,28 +157,28 @@ class CJsonDecoder { private: template - bool decodeCJson(Payload &pl, Serializer &rdser, WrSerializer &wrser, FilterT filter, RecoderT recoder, TagOptT); + bool decodeCJson(Payload& pl, Serializer& rdser, WrSerializer& wrser, FilterT filter, RecoderT recoder, TagOptT); bool isInArray() const noexcept { return arrayLevel_ > 0; } - [[noreturn]] void throwTagReferenceError(ctag, const Payload &); - [[noreturn]] void throwUnexpectedArrayError(const PayloadFieldType &); + [[noreturn]] void throwTagReferenceError(ctag, const Payload&); + [[noreturn]] void throwUnexpectedArrayError(const PayloadFieldType&); - [[nodiscard]] Variant cjsonValueToVariant(TagType tag, Serializer &rdser, KeyValueType dstType); + [[nodiscard]] Variant cjsonValueToVariant(TagType tag, Serializer& rdser, KeyValueType dstType); - TagsMatcher &tagsMatcher_; + TagsMatcher& tagsMatcher_; TagsPath tagsPath_; int32_t arrayLevel_ = 0; ScalarIndexesSetT objectScalarIndexes_; // storage for owning strings obtained from numbers - std::deque &storage_; + std::deque& storage_; }; extern template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::DummyFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::DummyFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); extern template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::DummyFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::DummyFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); extern template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::RestrictingFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::RestrictingFilter, CJsonDecoder::DummyRecoder, CJsonDecoder::NamelessTagOpt); extern template bool CJsonDecoder::decodeCJson( - Payload &, Serializer &, WrSerializer &, CJsonDecoder::RestrictingFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); + Payload&, Serializer&, WrSerializer&, CJsonDecoder::RestrictingFilter, CJsonDecoder::DefaultRecoder, CJsonDecoder::NamelessTagOpt); } // namespace reindexer diff --git a/cpp_src/core/cjson/cjsonmodifier.cc b/cpp_src/core/cjson/cjsonmodifier.cc index ab6506079..101cb4809 100644 --- a/cpp_src/core/cjson/cjsonmodifier.cc +++ b/cpp_src/core/cjson/cjsonmodifier.cc @@ -11,11 +11,11 @@ const std::string_view kWrongFieldsAmountMsg = "Number of fields for update shou class CJsonModifier::Context { public: - Context(const IndexedTagsPath &fieldPath, const VariantArray &v, WrSerializer &ser, std::string_view tuple, FieldModifyMode m, - const Payload *pl = nullptr) + Context(const IndexedTagsPath& fieldPath, const VariantArray& v, WrSerializer& ser, std::string_view tuple, FieldModifyMode m, + const Payload* pl = nullptr) : value(v), wrser(ser), rdser(tuple), mode(m), payload(pl) { jsonPath.reserve(fieldPath.size()); - for (const IndexedPathNode &node : fieldPath) { + for (const IndexedPathNode& node : fieldPath) { isForAllItems_ = isForAllItems_ || node.IsForAllItems(); jsonPath.emplace_back(node.NameTag()); } @@ -29,23 +29,23 @@ class CJsonModifier::Context { } [[nodiscard]] bool IsForAllItems() const noexcept { return isForAllItems_; } - const VariantArray &value; - WrSerializer &wrser; + const VariantArray& value; + WrSerializer& wrser; Serializer rdser; TagsPath jsonPath; IndexedTagsPath currObjPath; FieldModifyMode mode = FieldModeSet; bool fieldUpdated = false; bool updateArrayElements = false; - const Payload *payload = nullptr; + const Payload* payload = nullptr; std::array fieldsArrayOffsets; private: bool isForAllItems_ = false; }; -void CJsonModifier::SetFieldValue(std::string_view tuple, const IndexedTagsPath &fieldPath, const VariantArray &val, WrSerializer &ser, - const Payload &pl) { +void CJsonModifier::SetFieldValue(std::string_view tuple, const IndexedTagsPath& fieldPath, const VariantArray& val, WrSerializer& ser, + const Payload& pl) { auto ctx = initState(tuple, fieldPath, val, ser, &pl, FieldModifyMode::FieldModeSet); updateFieldInTuple(ctx); if (!ctx.fieldUpdated && !ctx.IsForAllItems()) { @@ -53,8 +53,8 @@ void CJsonModifier::SetFieldValue(std::string_view tuple, const IndexedTagsPath } } -void CJsonModifier::SetObject(std::string_view tuple, const IndexedTagsPath &fieldPath, const VariantArray &val, WrSerializer &ser, - const Payload &pl) { +void CJsonModifier::SetObject(std::string_view tuple, const IndexedTagsPath& fieldPath, const VariantArray& val, WrSerializer& ser, + const Payload& pl) { auto ctx = initState(tuple, fieldPath, val, ser, &pl, FieldModifyMode::FieldModeSetJson); buildCJSON(ctx); if (!ctx.fieldUpdated && !ctx.IsForAllItems()) { @@ -62,13 +62,13 @@ void CJsonModifier::SetObject(std::string_view tuple, const IndexedTagsPath &fie } } -void CJsonModifier::RemoveField(std::string_view tuple, const IndexedTagsPath &fieldPath, WrSerializer &wrser) { +void CJsonModifier::RemoveField(std::string_view tuple, const IndexedTagsPath& fieldPath, WrSerializer& wrser) { auto ctx = initState(tuple, fieldPath, {}, wrser, nullptr, FieldModeDrop); dropFieldInTuple(ctx); } -CJsonModifier::Context CJsonModifier::initState(std::string_view tuple, const IndexedTagsPath &fieldPath, const VariantArray &val, - WrSerializer &ser, const Payload *pl, FieldModifyMode mode) { +CJsonModifier::Context CJsonModifier::initState(std::string_view tuple, const IndexedTagsPath& fieldPath, const VariantArray& val, + WrSerializer& ser, const Payload* pl, FieldModifyMode mode) { if (fieldPath.empty()) { throw Error(errLogic, kWrongFieldsAmountMsg); } @@ -79,12 +79,12 @@ CJsonModifier::Context CJsonModifier::initState(std::string_view tuple, const In return ctx; } -void CJsonModifier::updateObject(Context &ctx, int tagName) const { +void CJsonModifier::updateObject(Context& ctx, int tagName) const { ctx.fieldUpdated = true; JsonDecoder jsonDecoder(tagsMatcher_); if (ctx.value.IsArrayValue()) { CJsonBuilder cjsonBuilder(ctx.wrser, ObjType::TypeArray, &tagsMatcher_, tagName); - for (const auto &item : ctx.value) { + for (const auto& item : ctx.value) { auto objBuilder = cjsonBuilder.Object(nullptr); jsonDecoder.Decode(std::string_view(item), objBuilder, ctx.jsonPath); } @@ -96,7 +96,7 @@ void CJsonModifier::updateObject(Context &ctx, int tagName) const { jsonDecoder.Decode(std::string_view(ctx.value.front()), cjsonBuilder, ctx.jsonPath); } -void CJsonModifier::insertField(Context &ctx) const { +void CJsonModifier::insertField(Context& ctx) const { ctx.fieldUpdated = true; assertrx(ctx.currObjPath.size() < fieldPath_.size()); @@ -127,10 +127,14 @@ void CJsonModifier::insertField(Context &ctx) const { ctx.currObjPath.clear(); } -bool CJsonModifier::needToInsertField(const Context &ctx) const { +bool CJsonModifier::needToInsertField(const Context& ctx) const { assertrx_throw(!fieldPath_.empty()); - if (ctx.fieldUpdated || fieldPath_.back().IsArrayNode()) return false; - if (ctx.currObjPath.size() >= fieldPath_.size()) return false; + if (ctx.fieldUpdated || fieldPath_.back().IsArrayNode()) { + return false; + } + if (ctx.currObjPath.size() >= fieldPath_.size()) { + return false; + } assertrx_throw(ctx.currObjPath.size() <= fieldPath_.size()); for (unsigned i = 0; i < ctx.currObjPath.size(); ++i) { if (fieldPath_[i] != ctx.currObjPath[i]) { @@ -148,11 +152,11 @@ bool CJsonModifier::needToInsertField(const Context &ctx) const { return true; } -TagType CJsonModifier::determineUpdateTagType(const Context &ctx, int field) const { +TagType CJsonModifier::determineUpdateTagType(const Context& ctx, int field) const { if (isIndexed(field)) { - const PayloadFieldType &fieldType = pt_.Field(field); + const PayloadFieldType& fieldType = pt_.Field(field); if (!fieldType.IsArray() || ctx.updateArrayElements || !ctx.value.IsNullValue()) { - for (auto &v : ctx.value) { + for (auto& v : ctx.value) { if (!fieldType.Type().IsSame(v.Type())) { throw Error(errParams, "Inserted field %s type [%s] doesn't match it's index type [%s]", fieldType.Name(), v.Type().Name(), fieldType.Type().Name()); @@ -169,10 +173,12 @@ TagType CJsonModifier::determineUpdateTagType(const Context &ctx, int field) con return arrayKvType2Tag(ctx.value); } -bool CJsonModifier::checkIfFoundTag(Context &ctx, bool isLastItem) const { - if (tagsPath_.empty() || !fieldPath_.Compare(tagsPath_)) return false; +bool CJsonModifier::checkIfFoundTag(Context& ctx, bool isLastItem) const { + if (tagsPath_.empty() || !fieldPath_.Compare(tagsPath_)) { + return false; + } - const auto &backFieldPath = fieldPath_.back(); + const auto& backFieldPath = fieldPath_.back(); if (!backFieldPath.IsArrayNode() || ((!backFieldPath.IsForAllItems() || isLastItem))) { ctx.fieldUpdated = true; } @@ -180,11 +186,11 @@ bool CJsonModifier::checkIfFoundTag(Context &ctx, bool isLastItem) const { return true; } -void CJsonModifier::setArray(Context &ctx) const { +void CJsonModifier::setArray(Context& ctx) const { auto type = arrayKvType2Tag(ctx.value); ctx.wrser.PutCArrayTag(carraytag{ctx.value.size(), type}); const bool isObjsArr = (type == TAG_OBJECT); - for (const auto &item : ctx.value) { + for (const auto& item : ctx.value) { if (isObjsArr) { type = item.Type().ToTagType(); ctx.wrser.PutCTag(ctag{type}); @@ -193,7 +199,7 @@ void CJsonModifier::setArray(Context &ctx) const { } } -void CJsonModifier::writeCTag(const ctag &tag, Context &ctx) { +void CJsonModifier::writeCTag(const ctag& tag, Context& ctx) { bool tagMatched = checkIfFoundTag(ctx); const TagType tagType = tag.Type(); const int field = tag.Field(); @@ -201,7 +207,7 @@ void CJsonModifier::writeCTag(const ctag &tag, Context &ctx) { if (tagType == TAG_ARRAY) { const auto count = ctx.rdser.GetVarUint(); if (!tagMatched || !ctx.fieldUpdated) { - auto &lastTag = tagsPath_.back(); + auto& lastTag = tagsPath_.back(); for (uint64_t i = 0; i < count; ++i) { lastTag.SetIndex(i); const bool isLastItem = (i + 1 == count); @@ -241,7 +247,7 @@ void CJsonModifier::writeCTag(const ctag &tag, Context &ctx) { } } -void CJsonModifier::updateArray(TagType atagType, uint32_t count, int tagName, Context &ctx) { +void CJsonModifier::updateArray(TagType atagType, uint32_t count, int tagName, Context& ctx) { assertrx_throw(!ctx.value.IsArrayValue()); // Unable to update array's element with array-value Variant value; @@ -293,7 +299,7 @@ void CJsonModifier::updateArray(TagType atagType, uint32_t count, int tagName, C assertrx_throw(ctx.fieldUpdated); } -void CJsonModifier::copyArray(int tagName, Context &ctx) { +void CJsonModifier::copyArray(int tagName, Context& ctx) { const carraytag atag = ctx.rdser.GetCArrayTag(); const TagType atagType = atag.Type(); const auto count = atag.Count(); @@ -360,7 +366,7 @@ void CJsonModifier::copyArray(int tagName, Context &ctx) { } } -bool CJsonModifier::updateFieldInTuple(Context &ctx) { +bool CJsonModifier::updateFieldInTuple(Context& ctx) { const ctag tag = ctx.rdser.GetCTag(); const TagType tagType = tag.Type(); if (tagType == TAG_END) { @@ -415,7 +421,7 @@ bool CJsonModifier::updateFieldInTuple(Context &ctx) { return true; } -bool CJsonModifier::dropFieldInTuple(Context &ctx) { +bool CJsonModifier::dropFieldInTuple(Context& ctx) { const ctag tag = ctx.rdser.GetCTag(); const TagType tagType = tag.Type(); if (tagType == TAG_END) { @@ -494,7 +500,7 @@ bool CJsonModifier::dropFieldInTuple(Context &ctx) { return true; } -void CJsonModifier::embedFieldValue(TagType type, int field, Context &ctx, size_t idx) const { +void CJsonModifier::embedFieldValue(TagType type, int field, Context& ctx, size_t idx) const { if (isIndexed(field)) { assertrx(ctx.payload); const Variant v = ctx.payload->Get(field, ctx.fieldsArrayOffsets[field] + idx); @@ -505,11 +511,13 @@ void CJsonModifier::embedFieldValue(TagType type, int field, Context &ctx, size_ copyCJsonValue(type, ctx.rdser, ctx.wrser); } -bool CJsonModifier::buildCJSON(Context &ctx) { +bool CJsonModifier::buildCJSON(Context& ctx) { const auto tag = ctx.rdser.GetCTag(); TagType tagType = tag.Type(); if (tagType == TAG_END) { - if (needToInsertField(ctx)) insertField(ctx); + if (needToInsertField(ctx)) { + insertField(ctx); + } ctx.wrser.PutCTag(kCTagEnd); return false; } diff --git a/cpp_src/core/cjson/cjsonmodifier.h b/cpp_src/core/cjson/cjsonmodifier.h index baa804d3e..c907b437c 100644 --- a/cpp_src/core/cjson/cjsonmodifier.h +++ b/cpp_src/core/cjson/cjsonmodifier.h @@ -10,34 +10,34 @@ class TagsMatcher; class CJsonModifier { public: - CJsonModifier(TagsMatcher &tagsMatcher, PayloadType pt) noexcept : pt_(std::move(pt)), tagsMatcher_(tagsMatcher) {} - void SetFieldValue(std::string_view tuple, const IndexedTagsPath &fieldPath, const VariantArray &val, WrSerializer &ser, - const Payload &pl); - void SetObject(std::string_view tuple, const IndexedTagsPath &fieldPath, const VariantArray &val, WrSerializer &ser, const Payload &pl); - void RemoveField(std::string_view tuple, const IndexedTagsPath &fieldPath, WrSerializer &wrser); + CJsonModifier(TagsMatcher& tagsMatcher, PayloadType pt) noexcept : pt_(std::move(pt)), tagsMatcher_(tagsMatcher) {} + void SetFieldValue(std::string_view tuple, const IndexedTagsPath& fieldPath, const VariantArray& val, WrSerializer& ser, + const Payload& pl); + void SetObject(std::string_view tuple, const IndexedTagsPath& fieldPath, const VariantArray& val, WrSerializer& ser, const Payload& pl); + void RemoveField(std::string_view tuple, const IndexedTagsPath& fieldPath, WrSerializer& wrser); private: class Context; - Context initState(std::string_view tuple, const IndexedTagsPath &fieldPath, const VariantArray &val, WrSerializer &ser, - const Payload *pl, FieldModifyMode mode); - bool updateFieldInTuple(Context &ctx); - bool dropFieldInTuple(Context &ctx); - bool buildCJSON(Context &ctx); - [[nodiscard]] bool needToInsertField(const Context &ctx) const; - void insertField(Context &ctx) const; - void embedFieldValue(TagType, int field, Context &ctx, size_t idx) const; - void updateObject(Context &ctx, int tagName) const; - void setArray(Context &ctx) const; - void writeCTag(const ctag &tag, Context &ctx); - void updateArray(TagType atagType, uint32_t count, int tagName, Context &ctx); - void copyArray(int TagName, Context &ctx); - [[nodiscard]] TagType determineUpdateTagType(const Context &ctx, int field) const; - [[nodiscard]] bool checkIfFoundTag(Context &ctx, bool isLastItem = false) const; + Context initState(std::string_view tuple, const IndexedTagsPath& fieldPath, const VariantArray& val, WrSerializer& ser, + const Payload* pl, FieldModifyMode mode); + bool updateFieldInTuple(Context& ctx); + bool dropFieldInTuple(Context& ctx); + bool buildCJSON(Context& ctx); + [[nodiscard]] bool needToInsertField(const Context& ctx) const; + void insertField(Context& ctx) const; + void embedFieldValue(TagType, int field, Context& ctx, size_t idx) const; + void updateObject(Context& ctx, int tagName) const; + void setArray(Context& ctx) const; + void writeCTag(const ctag& tag, Context& ctx); + void updateArray(TagType atagType, uint32_t count, int tagName, Context& ctx); + void copyArray(int TagName, Context& ctx); + [[nodiscard]] TagType determineUpdateTagType(const Context& ctx, int field) const; + [[nodiscard]] bool checkIfFoundTag(Context& ctx, bool isLastItem = false) const; [[nodiscard]] bool isIndexed(int field) const noexcept { return (field >= 0); } PayloadType pt_; IndexedTagsPath fieldPath_, tagsPath_; - TagsMatcher &tagsMatcher_; + TagsMatcher& tagsMatcher_; }; } // namespace reindexer diff --git a/cpp_src/core/cjson/cjsontools.cc b/cpp_src/core/cjson/cjsontools.cc index 4b95f3761..ecce2bdc4 100644 --- a/cpp_src/core/cjson/cjsontools.cc +++ b/cpp_src/core/cjson/cjsontools.cc @@ -1,10 +1,11 @@ #include "cjsontools.h" +#include #include "cjsonbuilder.h" #include "core/type_consts_helpers.h" namespace reindexer { -TagType arrayKvType2Tag(const VariantArray &values) { +TagType arrayKvType2Tag(const VariantArray& values) { if (values.empty()) { return TAG_NULL; } @@ -21,8 +22,10 @@ TagType arrayKvType2Tag(const VariantArray &values) { return type; } -void copyCJsonValue(TagType tagType, const Variant &value, WrSerializer &wrser) { - if (value.Type().Is()) return; +void copyCJsonValue(TagType tagType, const Variant& value, WrSerializer& wrser) { + if (value.Type().Is()) { + return; + } switch (tagType) { case TAG_DOUBLE: wrser.PutDouble(static_cast(value.convert(KeyValueType::Double{}))); @@ -55,7 +58,7 @@ void copyCJsonValue(TagType tagType, const Variant &value, WrSerializer &wrser) } } -void putCJsonRef(TagType tagType, int tagName, int tagField, const VariantArray &values, WrSerializer &wrser) { +void putCJsonRef(TagType tagType, int tagName, int tagField, const VariantArray& values, WrSerializer& wrser) { if (values.IsArrayValue()) { wrser.PutCTag(ctag{TAG_ARRAY, tagName, tagField}); wrser.PutVarUint(values.size()); @@ -64,19 +67,21 @@ void putCJsonRef(TagType tagType, int tagName, int tagField, const VariantArray } } -void putCJsonValue(TagType tagType, int tagName, const VariantArray &values, WrSerializer &wrser) { +void putCJsonValue(TagType tagType, int tagName, const VariantArray& values, WrSerializer& wrser) { if (values.IsArrayValue()) { const TagType elemType = arrayKvType2Tag(values); wrser.PutCTag(ctag{TAG_ARRAY, tagName}); wrser.PutCArrayTag(carraytag{values.size(), elemType}); if (elemType == TAG_OBJECT) { - for (const Variant &value : values) { + for (const Variant& value : values) { auto itemType = value.Type().ToTagType(); wrser.PutCTag(ctag{itemType}); copyCJsonValue(itemType, value, wrser); } } else { - for (const Variant &value : values) copyCJsonValue(elemType, value, wrser); + for (const Variant& value : values) { + copyCJsonValue(elemType, value, wrser); + } } } else if (values.size() == 1) { wrser.PutCTag(ctag{tagType, tagName}); @@ -86,7 +91,7 @@ void putCJsonValue(TagType tagType, int tagName, const VariantArray &values, WrS } } -void copyCJsonValue(TagType tagType, Serializer &rdser, WrSerializer &wrser) { +void copyCJsonValue(TagType tagType, Serializer& rdser, WrSerializer& wrser) { switch (tagType) { case TAG_DOUBLE: wrser.PutDouble(rdser.GetDouble()); @@ -115,7 +120,7 @@ void copyCJsonValue(TagType tagType, Serializer &rdser, WrSerializer &wrser) { } } -void skipCjsonTag(ctag tag, Serializer &rdser, std::array *fieldsArrayOffsets) { +void skipCjsonTag(ctag tag, Serializer& rdser, std::array* fieldsArrayOffsets) { switch (tag.Type()) { case TAG_ARRAY: { const auto field = tag.Field(); @@ -164,17 +169,17 @@ void skipCjsonTag(ctag tag, Serializer &rdser, std::array } } -Variant cjsonValueToVariant(TagType tagType, Serializer &rdser, KeyValueType dstType) { +Variant cjsonValueToVariant(TagType tagType, Serializer& rdser, KeyValueType dstType) { return rdser .GetRawVariant(dstType.Is() && tagType == TAG_VARINT ? KeyValueType{KeyValueType::Int{}} : KeyValueType{tagType}) .convert(dstType); } template -void buildPayloadTuple(const PayloadIface &pl, const TagsMatcher *tagsMatcher, WrSerializer &wrser) { +void buildPayloadTuple(const PayloadIface& pl, const TagsMatcher* tagsMatcher, WrSerializer& wrser) { CJsonBuilder builder(wrser, ObjType::TypeObject); for (int field = 1, numFields = pl.NumFields(); field < numFields; ++field) { - const PayloadFieldType &fieldType = pl.Type().Field(field); + const PayloadFieldType& fieldType = pl.Type().Field(field); if (fieldType.JsonPaths().size() < 1 || fieldType.JsonPaths()[0].empty()) { continue; } @@ -190,16 +195,192 @@ void buildPayloadTuple(const PayloadIface &pl, const TagsMatcher *tagsMatcher } } -template void buildPayloadTuple(const PayloadIface &, const TagsMatcher *, WrSerializer &); -template void buildPayloadTuple(const PayloadIface &, const TagsMatcher *, WrSerializer &); +template void buildPayloadTuple(const PayloadIface&, const TagsMatcher*, WrSerializer&); +template void buildPayloadTuple(const PayloadIface&, const TagsMatcher*, WrSerializer&); -void throwUnexpectedNestedArrayError(std::string_view parserName, const PayloadFieldType &f) { +void throwUnexpectedNestedArrayError(std::string_view parserName, const PayloadFieldType& f) { throw Error(errLogic, "Error parsing %s field '%s' - got value nested into the array, but expected scalar %s", parserName, f.Name(), f.Type().Name()); } -void throwScalarMultipleEncodesError(const Payload &pl, const PayloadFieldType &f, int field) { +void throwScalarMultipleEncodesError(const Payload& pl, const PayloadFieldType& f, int field) { throw Error(errLogic, "Non-array field '%s' [%d] from '%s' can only be encoded once.", f.Name(), field, pl.Type().Name()); } +static void dumpCjsonValue(TagType type, Serializer& cjson, std::ostream& dump) { + switch (type) { + case TAG_VARINT: + dump << cjson.GetVarint(); + break; + case TAG_DOUBLE: + dump << cjson.GetDouble(); + break; + case TAG_STRING: + dump << '"' << std::string_view{cjson.GetPVString()} << '"'; + break; + case TAG_BOOL: + dump << std::boolalpha << bool(cjson.GetVarUint()); + break; + case TAG_UUID: + dump << std::string{cjson.GetUuid()}; + break; + case TAG_NULL: + case TAG_OBJECT: + case TAG_ARRAY: + case TAG_END: + assertrx(0); + } +} + +template +static void dumpCjsonObject(Serializer& cjson, std::ostream& dump, const TagsMatcher* tm, const PL* pl, std::string_view tab, + unsigned indentLevel) { + const auto indent = [&dump, tab](unsigned indLvl) { + for (unsigned i = 0; i < indLvl; ++i) { + dump << tab; + } + }; + VariantArray buf; + while (!cjson.Eof()) { + const ctag tag = cjson.GetCTag(); + const TagType type = tag.Type(); + const auto name = tag.Name(); + const auto field = tag.Field(); + if (type == TAG_END) { + assertrx(indentLevel > 0); + --indentLevel; + } + indent(indentLevel); + dump << std::left << std::setw(10) << TagTypeToStr(type); + dump << std::right << std::setw(4) << name; + dump << std::right << std::setw(4) << field; + if (tm && name > 0) { + dump << " \"" << tm->tag2name(name) << '"'; + } + if (field >= 0) { + switch (type) { + case TAG_VARINT: + case TAG_DOUBLE: + case TAG_STRING: + case TAG_BOOL: + case TAG_UUID: + if (pl) { + buf.clear(); + pl->Get(field, buf); + assertrx(buf.size() == 1); + dump << " -> " << buf[0].As(); + } + break; + case TAG_ARRAY: { + dump << '\n'; + indent(indentLevel + 1); + const size_t count = cjson.GetVarUint(); + dump << "Count: " << count; + if (pl) { + dump << " -> ["; + buf.clear(); + pl->Get(field, buf); + assertrx(buf.size() == count); + for (size_t i = 0; i < count; ++i) { + if (i != 0) { + dump << ", "; + } + dump << buf[i].As(); + } + dump << ']'; + } + } break; + case TAG_NULL: + case TAG_OBJECT: + case TAG_END: + assertrx(0); + } + dump << '\n'; + } else { + dump << '\n'; + switch (type) { + case TAG_VARINT: + case TAG_DOUBLE: + case TAG_STRING: + case TAG_BOOL: + case TAG_UUID: + indent(indentLevel + 1); + dumpCjsonValue(type, cjson, dump); + dump << '\n'; + break; + case TAG_NULL: + break; + case TAG_ARRAY: { + const carraytag arr = cjson.GetCArrayTag(); + const size_t count = arr.Count(); + if (arr.Type() == TAG_OBJECT) { + indent(indentLevel + 1); + dump << " count: " << count << '\n'; + for (size_t i = 0; i < count; ++i) { + const ctag t = cjson.GetCTag(); + assertrx(t.Name() == 0); + assertrx(t.Field() < 0); + indent(indentLevel + 2); + dump << TagTypeToStr(t.Type()); + dump << ": "; + if (t.Type() == TAG_OBJECT) { + dump << "{\n"; + dumpCjsonObject(cjson, dump, tm, pl, tab, indentLevel + 3); + indent(indentLevel + 2); + dump << "}\n"; + } else { + dumpCjsonValue(t.Type(), cjson, dump); + dump << '\n'; + } + } + } else { + indent(indentLevel + 1); + dump << TagTypeToStr(arr.Type()) << " count: " << count << '\n'; + indent(indentLevel + 2); + dump << '['; + if (arr.Type() == TAG_OBJECT && count > 0) { + dump << '\n'; + for (size_t i = 0; i < count; ++i) { + indent(indentLevel + 3); + dump << TagTypeToStr(arr.Type()) << '\n'; + dumpCjsonObject(cjson, dump, tm, pl, tab, indentLevel + 4); + } + indent(indentLevel + 2); + } else { + for (size_t i = 0; i < count; ++i) { + if (i != 0) { + dump << ", "; + } + dumpCjsonValue(arr.Type(), cjson, dump); + } + } + dump << "]\n"; + } + } break; + case TAG_OBJECT: + dumpCjsonObject(cjson, dump, tm, pl, tab, indentLevel + 1); + break; + case TAG_END: + return; + } + } + } +} + +void DumpCjson(Serializer& cjson, std::ostream& dump, const Payload* pl, const TagsMatcher* tm, std::string_view tab) { + const auto osFlags = dump.flags(); + dump.exceptions(std::ostream::failbit | std::ostream::badbit); + dump << "TAG_TYPE NAME FIELD\n"; + dumpCjsonObject(cjson, dump, tm, pl, tab, 0); + dump.flags(osFlags); +} + +void DumpCjson(Serializer& cjson, std::ostream& dump, const ConstPayload* pl, const TagsMatcher* tm, std::string_view tab) { + const auto osFlags = dump.flags(); + dump.exceptions(std::ostream::failbit | std::ostream::badbit); + dump << "TAG_TYPE NAME FIELD\n"; + dumpCjsonObject(cjson, dump, tm, pl, tab, 0); + dump.flags(osFlags); +} + } // namespace reindexer diff --git a/cpp_src/core/cjson/cjsontools.h b/cpp_src/core/cjson/cjsontools.h index dddfbf2a5..93c618524 100644 --- a/cpp_src/core/cjson/cjsontools.h +++ b/cpp_src/core/cjson/cjsontools.h @@ -5,21 +5,21 @@ namespace reindexer { template -void buildPayloadTuple(const PayloadIface &pl, const TagsMatcher *tagsMatcher, WrSerializer &wrser); +void buildPayloadTuple(const PayloadIface& pl, const TagsMatcher* tagsMatcher, WrSerializer& wrser); -void copyCJsonValue(TagType tagType, Serializer &rdser, WrSerializer &wrser); -void copyCJsonValue(TagType tagType, const Variant &value, WrSerializer &wrser); -void putCJsonRef(TagType tagType, int tagName, int tagField, const VariantArray &values, WrSerializer &wrser); -void putCJsonValue(TagType tagType, int tagName, const VariantArray &values, WrSerializer &wrser); +void copyCJsonValue(TagType tagType, Serializer& rdser, WrSerializer& wrser); +void copyCJsonValue(TagType tagType, const Variant& value, WrSerializer& wrser); +void putCJsonRef(TagType tagType, int tagName, int tagField, const VariantArray& values, WrSerializer& wrser); +void putCJsonValue(TagType tagType, int tagName, const VariantArray& values, WrSerializer& wrser); -[[nodiscard]] TagType arrayKvType2Tag(const VariantArray &values); -void skipCjsonTag(ctag tag, Serializer &rdser, std::array *fieldsArrayOffsets = nullptr); -[[nodiscard]] Variant cjsonValueToVariant(TagType tag, Serializer &rdser, KeyValueType dstType); +[[nodiscard]] TagType arrayKvType2Tag(const VariantArray& values); +void skipCjsonTag(ctag tag, Serializer& rdser, std::array* fieldsArrayOffsets = nullptr); +[[nodiscard]] Variant cjsonValueToVariant(TagType tag, Serializer& rdser, KeyValueType dstType); -[[noreturn]] void throwUnexpectedNestedArrayError(std::string_view parserName, const PayloadFieldType &f); -[[noreturn]] void throwScalarMultipleEncodesError(const Payload &pl, const PayloadFieldType &f, int field); -RX_ALWAYS_INLINE void validateNonArrayFieldRestrictions(const ScalarIndexesSetT &scalarIndexes, const Payload &pl, - const PayloadFieldType &f, int field, bool isInArray, std::string_view parserName) { +[[noreturn]] void throwUnexpectedNestedArrayError(std::string_view parserName, const PayloadFieldType& f); +[[noreturn]] void throwScalarMultipleEncodesError(const Payload& pl, const PayloadFieldType& f, int field); +RX_ALWAYS_INLINE void validateNonArrayFieldRestrictions(const ScalarIndexesSetT& scalarIndexes, const Payload& pl, + const PayloadFieldType& f, int field, bool isInArray, std::string_view parserName) { if (!f.IsArray()) { if rx_unlikely (isInArray) { throwUnexpectedNestedArrayError(parserName, f); @@ -30,4 +30,23 @@ RX_ALWAYS_INLINE void validateNonArrayFieldRestrictions(const ScalarIndexesSetT } } +void DumpCjson(Serializer& cjson, std::ostream& dump, const ConstPayload*, const TagsMatcher* = nullptr, std::string_view tab = " "); +inline void DumpCjson(Serializer&& cjson, std::ostream& dump, const ConstPayload* pl, const TagsMatcher* tm = nullptr, + std::string_view tab = " ") { + DumpCjson(cjson, dump, pl, tm, tab); +} + +void DumpCjson(Serializer& cjson, std::ostream& dump, const Payload*, const TagsMatcher* = nullptr, std::string_view tab = " "); +inline void DumpCjson(Serializer&& cjson, std::ostream& dump, const Payload* pl, const TagsMatcher* tm = nullptr, + std::string_view tab = " ") { + DumpCjson(cjson, dump, pl, tm, tab); +} + +inline void DumpCjson(Serializer& cjson, std::ostream& dump, const TagsMatcher* tm = nullptr, std::string_view tab = " ") { + DumpCjson(cjson, dump, static_cast(nullptr), tm, tab); +} +inline void DumpCjson(Serializer&& cjson, std::ostream& dump, const TagsMatcher* tm = nullptr, std::string_view tab = " ") { + DumpCjson(cjson, dump, tm, tab); +} + } // namespace reindexer diff --git a/cpp_src/core/cjson/csvbuilder.cc b/cpp_src/core/cjson/csvbuilder.cc index 25b2eca49..a00a583c3 100644 --- a/cpp_src/core/cjson/csvbuilder.cc +++ b/cpp_src/core/cjson/csvbuilder.cc @@ -2,7 +2,7 @@ namespace reindexer { -CsvBuilder::CsvBuilder(ObjType type, const CsvBuilder &parent) +CsvBuilder::CsvBuilder(ObjType type, const CsvBuilder& parent) : ser_(parent.ser_), tm_(parent.tm_), type_(type), @@ -36,7 +36,7 @@ CsvBuilder::CsvBuilder(ObjType type, const CsvBuilder &parent) } } -CsvBuilder::CsvBuilder(WrSerializer &ser, CsvOrdering &ordering) +CsvBuilder::CsvBuilder(WrSerializer& ser, CsvOrdering& ordering) : ser_(&ser), level_(-1), ordering_(!ordering.ordering_.empty() ? &ordering.ordering_ : nullptr), @@ -46,7 +46,7 @@ CsvBuilder::~CsvBuilder() { End(); } std::string_view CsvBuilder::getNameByTag(int tagName) { return tagName ? tm_->tag2name(tagName) : std::string_view(); } -CsvBuilder &CsvBuilder::End() { +CsvBuilder& CsvBuilder::End() { if (!positions_.empty()) { postProcessing(); } @@ -89,9 +89,13 @@ void CsvBuilder::putName(std::string_view name) { tmProcessing(name); } - if (count_++) (*ser_) << ','; + if (count_++) { + (*ser_) << ','; + } - if (level_ < 1) return; + if (level_ < 1) { + return; + } if (name.data()) { (*ser_) << '"'; @@ -106,7 +110,7 @@ void CsvBuilder::tmProcessing(std::string_view name) { auto prevFinishPos = ser_->Len(); if (tag > 0) { - auto it = std::find_if(ordering_->begin(), ordering_->end(), [&tag](const auto &t) { return t == tag; }); + auto it = std::find_if(ordering_->begin(), ordering_->end(), [&tag](const auto& t) { return t == tag; }); if (it != ordering_->end()) { if (curTagPos_ > -1) { @@ -125,7 +129,9 @@ void CsvBuilder::tmProcessing(std::string_view name) { if (curTagPos_ > -1) { positions_[curTagPos_].second = prevFinishPos; } - if (count_) (*ser_) << ','; + if (count_) { + (*ser_) << ','; + } (*ser_) << "\"{"; type_ = ObjType::TypeObject; @@ -148,7 +154,7 @@ void CsvBuilder::postProcessing() { auto joinedData = std::string_view(ser_->Slice().data() + positions_[curTagPos_].second, ser_->Len() - positions_[curTagPos_].second); bool needDelim = false; - for (auto &[begin, end] : positions_) { + for (auto& [begin, end] : positions_) { if (needDelim) { *buf_ << ','; } else { @@ -162,7 +168,7 @@ void CsvBuilder::postProcessing() { *ser_ << buf_->Slice(); } -CsvBuilder &CsvBuilder::Put(std::string_view name, std::string_view arg, [[maybe_unused]] int offset) { +CsvBuilder& CsvBuilder::Put(std::string_view name, std::string_view arg, [[maybe_unused]] int offset) { putName(name); std::string_view optQuote = level_ > 0 ? "\"" : ""; @@ -173,32 +179,32 @@ CsvBuilder &CsvBuilder::Put(std::string_view name, std::string_view arg, [[maybe return *this; } -CsvBuilder &CsvBuilder::Put(std::string_view name, Uuid arg, [[maybe_unused]] int offset) { +CsvBuilder& CsvBuilder::Put(std::string_view name, Uuid arg, [[maybe_unused]] int offset) { putName(name); ser_->PrintJsonUuid(arg); return *this; } -CsvBuilder &CsvBuilder::Raw(std::string_view name, std::string_view arg) { +CsvBuilder& CsvBuilder::Raw(std::string_view name, std::string_view arg) { putName(name); (*ser_) << arg; return *this; } -CsvBuilder &CsvBuilder::Null(std::string_view name) { +CsvBuilder& CsvBuilder::Null(std::string_view name) { putName(name); (*ser_) << "null"; return *this; } -CsvBuilder &CsvBuilder::Put(std::string_view name, const Variant &kv, int offset) { +CsvBuilder& CsvBuilder::Put(std::string_view name, const Variant& kv, int offset) { kv.Type().EvaluateOneOf( [&](KeyValueType::Int) { Put(name, int(kv)); }, [&](KeyValueType::Int64) { Put(name, int64_t(kv), offset); }, [&](KeyValueType::Double) { Put(name, double(kv)); }, [&](KeyValueType::String) { Put(name, std::string_view(kv), offset); }, [&](KeyValueType::Null) { Null(name); }, [&](KeyValueType::Bool) { Put(name, bool(kv), offset); }, [&](KeyValueType::Tuple) { auto arrNode = Array(name); - for (auto &val : kv.getCompositeValues()) { + for (auto& val : kv.getCompositeValues()) { arrNode.Put({nullptr, 0}, val, offset); } }, diff --git a/cpp_src/core/cjson/csvbuilder.h b/cpp_src/core/cjson/csvbuilder.h index 7ab394f2b..b51bab67d 100644 --- a/cpp_src/core/cjson/csvbuilder.h +++ b/cpp_src/core/cjson/csvbuilder.h @@ -33,13 +33,13 @@ struct CsvOrdering { class CsvBuilder { public: CsvBuilder() = default; - CsvBuilder(WrSerializer &ser, CsvOrdering &ordering); + CsvBuilder(WrSerializer& ser, CsvOrdering& ordering); ~CsvBuilder(); - CsvBuilder &operator=(const CsvBuilder &) = delete; - CsvBuilder &operator=(CsvBuilder &&) = delete; + CsvBuilder& operator=(const CsvBuilder&) = delete; + CsvBuilder& operator=(CsvBuilder&&) = delete; - void SetTagsMatcher(const TagsMatcher *tm) { tm_ = tm; } + void SetTagsMatcher(const TagsMatcher* tm) { tm_ = tm; } /// Start new object CsvBuilder Object(std::string_view name = {}, int size = KUnknownFieldSize); @@ -52,70 +52,78 @@ class CsvBuilder { template void Array(int tagName, span data, int /*offset*/ = 0) { CsvBuilder node = Array(tagName); - for (const auto &d : data) node.Put({}, d); + for (const auto& d : data) { + node.Put({}, d); + } } template void Array(std::string_view n, span data, int /*offset*/ = 0) { CsvBuilder node = Array(n); - for (const auto &d : data) node.Put({}, d); + for (const auto& d : data) { + node.Put({}, d); + } } template void Array(std::string_view n, std::initializer_list data, int /*offset*/ = 0) { CsvBuilder node = Array(n); - for (const auto &d : data) node.Put({}, d); + for (const auto& d : data) { + node.Put({}, d); + } } - void Array(int tagName, Serializer &ser, TagType tagType, int count) { + void Array(int tagName, Serializer& ser, TagType tagType, int count) { CsvBuilder node = Array(tagName); - while (count--) node.Put({}, ser.GetRawVariant(KeyValueType{tagType})); + while (count--) { + node.Put({}, ser.GetRawVariant(KeyValueType{tagType})); + } } - CsvBuilder &Put(std::string_view name, const Variant &arg, int offset = 0); - CsvBuilder &Put(std::nullptr_t, const Variant &arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } - CsvBuilder &Put(std::string_view name, std::string_view arg, int offset = 0); - CsvBuilder &Put(std::string_view name, Uuid arg, int offset = 0); - CsvBuilder &Put(std::nullptr_t, std::string_view arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } - CsvBuilder &Put(std::string_view name, const char *arg, int offset = 0) { return Put(name, std::string_view(arg), offset); } - template ::value || std::is_floating_point::value>::type * = nullptr> - CsvBuilder &Put(std::string_view name, const T &arg, [[maybe_unused]] int offset = 0) { + CsvBuilder& Put(std::string_view name, const Variant& arg, int offset = 0); + CsvBuilder& Put(std::nullptr_t, const Variant& arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } + CsvBuilder& Put(std::string_view name, std::string_view arg, int offset = 0); + CsvBuilder& Put(std::string_view name, Uuid arg, int offset = 0); + CsvBuilder& Put(std::nullptr_t, std::string_view arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } + CsvBuilder& Put(std::string_view name, const char* arg, int offset = 0) { return Put(name, std::string_view(arg), offset); } + template ::value || std::is_floating_point::value>::type* = nullptr> + CsvBuilder& Put(std::string_view name, const T& arg, [[maybe_unused]] int offset = 0) { putName(name); (*ser_) << arg; return *this; } template - CsvBuilder &Put(int tagName, const T &arg, int offset = 0) { + CsvBuilder& Put(int tagName, const T& arg, int offset = 0) { return Put(getNameByTag(tagName), arg, offset); } - CsvBuilder &Raw(int tagName, std::string_view arg) { return Raw(getNameByTag(tagName), arg); } - CsvBuilder &Raw(std::string_view name, std::string_view arg); - CsvBuilder &Raw(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } - CsvBuilder &Json(std::string_view name, std::string_view arg) { return Raw(name, arg); } - CsvBuilder &Json(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } + CsvBuilder& Raw(int tagName, std::string_view arg) { return Raw(getNameByTag(tagName), arg); } + CsvBuilder& Raw(std::string_view name, std::string_view arg); + CsvBuilder& Raw(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } + CsvBuilder& Json(std::string_view name, std::string_view arg) { return Raw(name, arg); } + CsvBuilder& Json(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } - CsvBuilder &Null(int tagName) { return Null(getNameByTag(tagName)); } - CsvBuilder &Null(std::string_view name); + CsvBuilder& Null(int tagName) { return Null(getNameByTag(tagName)); } + CsvBuilder& Null(std::string_view name); - CsvBuilder &End(); + CsvBuilder& End(); protected: - CsvBuilder(ObjType type, const CsvBuilder &parent); + CsvBuilder(ObjType type, const CsvBuilder& parent); void putName(std::string_view name); std::string_view getNameByTag(int tagName); void tmProcessing(std::string_view name); void postProcessing(); - WrSerializer *ser_ = nullptr; - const TagsMatcher *tm_ = nullptr; + WrSerializer* ser_ = nullptr; + const TagsMatcher* tm_ = nullptr; ObjType type_ = ObjType::TypePlain; int count_ = 0; int level_ = 0; int startSerLen_ = 0; - const std::vector *ordering_ = nullptr; - WrSerializer *buf_ = nullptr; + const std::vector* ordering_ = nullptr; + WrSerializer* buf_ = nullptr; // idx - pos in ordering, {startTagPosInSer, endTagPosInSer(post culculated after received next tag)} std::vector> positions_; diff --git a/cpp_src/core/cjson/ctag.h b/cpp_src/core/cjson/ctag.h index 19567dd13..ffe6461ab 100644 --- a/cpp_src/core/cjson/ctag.h +++ b/cpp_src/core/cjson/ctag.h @@ -95,11 +95,13 @@ class carraytag { [[nodiscard]] RX_ALWAYS_INLINE constexpr bool operator!=(carraytag other) const noexcept { return !operator==(other); } private: - RX_ALWAYS_INLINE explicit constexpr carraytag(uint32_t atag) noexcept : carraytag{countImpl(atag), typeImpl(atag)} { assertrx_dbg(atag == atag_); } + RX_ALWAYS_INLINE explicit constexpr carraytag(uint32_t atag) noexcept : carraytag{countImpl(atag), typeImpl(atag)} { + assertrx_dbg(atag == atag_); + } [[nodiscard]] RX_ALWAYS_INLINE constexpr uint32_t asNumber() const noexcept { return atag_; } [[nodiscard]] RX_ALWAYS_INLINE static constexpr TagType typeImpl(uint32_t atag) noexcept { assertrx_dbg(((atag >> kCountBits) & kTypeMask) <= kMaxTagType); - return static_cast((atag >> kCountBits) & kTypeMask); // NOLINT(*EnumCastOutOfRange) + return static_cast((atag >> kCountBits) & kTypeMask); // NOLINT(*EnumCastOutOfRange) } [[nodiscard]] RX_ALWAYS_INLINE static constexpr uint32_t countImpl(uint32_t atag) noexcept { return atag & kCountMask; } diff --git a/cpp_src/core/cjson/defaultvaluecoder.cc b/cpp_src/core/cjson/defaultvaluecoder.cc index 6c6b5eb96..ef3aab6d5 100644 --- a/cpp_src/core/cjson/defaultvaluecoder.cc +++ b/cpp_src/core/cjson/defaultvaluecoder.cc @@ -2,7 +2,7 @@ namespace reindexer { -DefaultValueCoder::DefaultValueCoder(std::string_view ns, const PayloadFieldType &fld, std::vector &&tps, int16_t fieldIdx) +DefaultValueCoder::DefaultValueCoder(std::string_view ns, const PayloadFieldType& fld, std::vector&& tps, int16_t fieldIdx) : ns_(ns), field_(fld.Name()), tags_(std::move(tps)), @@ -19,7 +19,7 @@ bool DefaultValueCoder::Match(int field) noexcept { return false; // returned result is always same } -bool DefaultValueCoder::Match(TagType tt, const TagsPath &tp) { +bool DefaultValueCoder::Match(TagType tt, const TagsPath& tp) { static const bool result = false; // returned result is always same // nothing to look for (start tuple global object) @@ -73,7 +73,7 @@ bool DefaultValueCoder::Match(TagType tt, const TagsPath &tp) { return result; } -void DefaultValueCoder::Serialize(WrSerializer &wrser) { +void DefaultValueCoder::Serialize(WrSerializer& wrser) { if (blocked()) { return; // skip processing } @@ -101,10 +101,10 @@ bool DefaultValueCoder::Reset() noexcept { return (state_ == State::write); } -void DefaultValueCoder::match(const TagsPath &tp) { +void DefaultValueCoder::match(const TagsPath& tp) { ++nestingLevel_; - for (auto &path : tags_) { + for (auto& path : tags_) { if (path.front() != tp.front()) { continue; } @@ -131,7 +131,7 @@ void DefaultValueCoder::match(const TagsPath &tp) { } } -void DefaultValueCoder::write(WrSerializer &wrser) const { +void DefaultValueCoder::write(WrSerializer& wrser) const { int32_t nestedObjects = 0; for (size_t idx = copyPos_, sz = basePath_->size(); idx < sz; ++idx) { auto tagName = (*basePath_)[idx]; diff --git a/cpp_src/core/cjson/defaultvaluecoder.h b/cpp_src/core/cjson/defaultvaluecoder.h index 3cf9a5dac..2145b8599 100644 --- a/cpp_src/core/cjson/defaultvaluecoder.h +++ b/cpp_src/core/cjson/defaultvaluecoder.h @@ -6,18 +6,18 @@ namespace reindexer { class DefaultValueCoder : public Recoder { public: - DefaultValueCoder(std::string_view ns, const PayloadFieldType &fld, std::vector &&tps, int16_t fieldIdx); + DefaultValueCoder(std::string_view ns, const PayloadFieldType& fld, std::vector&& tps, int16_t fieldIdx); RX_ALWAYS_INLINE TagType Type(TagType tt) noexcept override final { return tt; } [[nodiscard]] bool Match(int f) noexcept override final; - [[nodiscard]] bool Match(TagType tt, const TagsPath &tp) override final; - RX_ALWAYS_INLINE void Recode(Serializer &, WrSerializer &) const noexcept override final { assertrx(false); } - RX_ALWAYS_INLINE void Recode(Serializer &, Payload &, int, WrSerializer &) noexcept override final { assertrx(false); } - void Serialize(WrSerializer &wrser) override final; + [[nodiscard]] bool Match(TagType tt, const TagsPath& tp) override final; + RX_ALWAYS_INLINE void Recode(Serializer&, WrSerializer&) const noexcept override final { assertrx(false); } + RX_ALWAYS_INLINE void Recode(Serializer&, Payload&, int, WrSerializer&) noexcept override final { assertrx(false); } + void Serialize(WrSerializer& wrser) override final; bool Reset() noexcept override final; private: - void match(const TagsPath &tp); - void write(WrSerializer &wrser) const; + void match(const TagsPath& tp); + void write(WrSerializer& wrser) const; [[nodiscard]] RX_ALWAYS_INLINE bool blocked() const noexcept { return ((state_ == State::found) || (state_ == State::write)); } [[nodiscard]] RX_ALWAYS_INLINE bool ready() const noexcept { return ((state_ == State::wait) || (state_ == State::match)); } @@ -29,7 +29,7 @@ class DefaultValueCoder : public Recoder { const TagType type_; const bool array_{false}; - const TagsPath *basePath_{nullptr}; + const TagsPath* basePath_{nullptr}; enum class State { wait, found, match, write } state_{State::wait}; uint32_t nestingLevel_{1}; uint32_t copyPos_{0}; diff --git a/cpp_src/core/cjson/fieldextractor.h b/cpp_src/core/cjson/fieldextractor.h index 79247070d..5112b3110 100644 --- a/cpp_src/core/cjson/fieldextractor.h +++ b/cpp_src/core/cjson/fieldextractor.h @@ -10,21 +10,21 @@ class FieldsExtractor { public: class FieldParams { public: - int &index; - int &length; + int& index; + int& length; int field; }; FieldsExtractor() = default; - FieldsExtractor(VariantArray *va, KeyValueType expectedType, int expectedPathDepth, const FieldsSet *filter, - FieldParams *params = nullptr) noexcept + FieldsExtractor(VariantArray* va, KeyValueType expectedType, int expectedPathDepth, const FieldsSet* filter, + FieldParams* params = nullptr) noexcept : values_(va), expectedType_(expectedType), expectedPathDepth_(expectedPathDepth), filter_(filter), params_(params) {} - FieldsExtractor(FieldsExtractor &&other) = default; - FieldsExtractor(const FieldsExtractor &) = delete; - FieldsExtractor &operator=(const FieldsExtractor &) = delete; - FieldsExtractor &operator=(FieldsExtractor &&) = delete; + FieldsExtractor(FieldsExtractor&& other) = default; + FieldsExtractor(const FieldsExtractor&) = delete; + FieldsExtractor& operator=(const FieldsExtractor&) = delete; + FieldsExtractor& operator=(FieldsExtractor&&) = delete; - void SetTagsMatcher(const TagsMatcher *) noexcept {} + void SetTagsMatcher(const TagsMatcher*) noexcept {} FieldsExtractor Object(int) noexcept { return FieldsExtractor(values_, expectedType_, expectedPathDepth_ - 1, filter_, params_); } FieldsExtractor Array(int) noexcept { @@ -41,7 +41,7 @@ class FieldsExtractor { template void Array(int, span data, int offset) { - const IndexedPathNode &pathNode = getArrayPathNode(); + const IndexedPathNode& pathNode = getArrayPathNode(); const PathType ptype = pathNotToType(pathNode); if (ptype == PathType::Other) { throw Error(errLogic, "Unable to extract array value without index value"); @@ -60,13 +60,13 @@ class FieldsExtractor { if (ptype == PathType::WithIndex) { int i = 0; - for (const auto &d : data) { + for (const auto& d : data) { if (i++ == pathNode.Index()) { put(0, Variant(d)); } } } else { - for (const auto &d : data) { + for (const auto& d : data) { put(0, Variant(d)); } } @@ -76,8 +76,8 @@ class FieldsExtractor { } } - void Array(int, Serializer &ser, TagType tagType, int count) { - const IndexedPathNode &pathNode = getArrayPathNode(); + void Array(int, Serializer& ser, TagType tagType, int count) { + const IndexedPathNode& pathNode = getArrayPathNode(); const PathType ptype = pathNotToType(pathNode); if (ptype == PathType::Other) { throw Error(errLogic, "Unable to extract array value without index value"); @@ -112,8 +112,10 @@ class FieldsExtractor { } } - FieldsExtractor &Put(int t, Variant arg, int offset) { - if (expectedPathDepth_ > 0) return *this; + FieldsExtractor& Put(int t, Variant arg, int offset) { + if (expectedPathDepth_ > 0) { + return *this; + } if (params_) { if (params_->index >= 0 && params_->length > 0 && offset == params_->index + params_->length) { // Concatenate fields from objects, nested in arrays @@ -127,11 +129,11 @@ class FieldsExtractor { } template - FieldsExtractor &Put(int tag, const T &arg, int offset) { + FieldsExtractor& Put(int tag, const T& arg, int offset) { return Put(tag, Variant{arg}, offset); } - FieldsExtractor &Null(int) noexcept { return *this; } + FieldsExtractor& Null(int) noexcept { return *this; } int TargetField() { return params_ ? params_->field : IndexValueType::NotSet; } bool IsHavingOffset() const noexcept { return params_ && (params_->length >= 0 || params_->index >= 0); } void OnScopeEnd(int offset) noexcept { @@ -144,41 +146,47 @@ class FieldsExtractor { private: enum class PathType { AllItems, WithIndex, Other }; - PathType pathNotToType(const IndexedPathNode &pathNode) noexcept { + PathType pathNotToType(const IndexedPathNode& pathNode) noexcept { return pathNode.IsForAllItems() ? PathType::AllItems : (pathNode.Index() == IndexValueType::NotSet) ? PathType::Other : PathType::WithIndex; } - FieldsExtractor &put(int, Variant arg) { - if (expectedPathDepth_ > 0) return *this; + FieldsExtractor& put(int, Variant arg) { + if (expectedPathDepth_ > 0) { + return *this; + } expectedType_.EvaluateOneOf( [&](OneOf) { arg.convert(expectedType_); }, [](OneOf) noexcept {}); assertrx_throw(values_); values_->emplace_back(std::move(arg)); - if (expectedPathDepth_ < 0) values_->MarkObject(); + if (expectedPathDepth_ < 0) { + values_->MarkObject(); + } return *this; } - const IndexedPathNode &getArrayPathNode() const { + const IndexedPathNode& getArrayPathNode() const { if (filter_ && filter_->getTagsPathsLength() > 0) { size_t lastItemIndex = filter_->getTagsPathsLength() - 1; if (filter_->isTagsPathIndexed(lastItemIndex)) { - const IndexedTagsPath &path = filter_->getIndexedTagsPath(lastItemIndex); + const IndexedTagsPath& path = filter_->getIndexedTagsPath(lastItemIndex); assertrx(path.size() > 0); - if (path.back().IsArrayNode()) return path.back(); + if (path.back().IsArrayNode()) { + return path.back(); + } } } static const IndexedPathNode commonNode{IndexedPathNode::AllItems}; return commonNode; } - VariantArray *values_ = nullptr; + VariantArray* values_ = nullptr; KeyValueType expectedType_{KeyValueType::Undefined{}}; int expectedPathDepth_ = 0; - const FieldsSet *filter_; - FieldParams *params_; + const FieldsSet* filter_; + FieldParams* params_; }; } // namespace reindexer diff --git a/cpp_src/core/cjson/jschemachecker.cc b/cpp_src/core/cjson/jschemachecker.cc index b36864211..0485cb734 100644 --- a/cpp_src/core/cjson/jschemachecker.cc +++ b/cpp_src/core/cjson/jschemachecker.cc @@ -11,12 +11,16 @@ namespace reindexer { JsonSchemaChecker::JsonSchemaChecker(const std::string& json, std::string rootTypeName) : rootTypeName_(std::move(rootTypeName)) { Error err = createTypeTable(json); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } isInit = true; } Error JsonSchemaChecker::Init(const std::string& json, std::string rootTypeName) { - if (isInit) return Error(errLogic, "JsonSchemaChecker already initialized."); + if (isInit) { + return Error(errLogic, "JsonSchemaChecker already initialized."); + } rootTypeName_ = std::move(rootTypeName); return createTypeTable(json); } @@ -29,7 +33,9 @@ bool JsonSchemaChecker::isSimpleType(std::string_view tp) { std::string JsonSchemaChecker::createType(const PrefixTree::PrefixTreeNode* node, const std::string& typeName) { using namespace std::string_view_literals; JsonSchemaChecker::TypeDescr typeDescr; - if (indexes_.find(typeName) != indexes_.end()) return typeName; + if (indexes_.find(typeName) != indexes_.end()) { + return typeName; + } if (node->children.empty() && node->props.allowAdditionalProps == true) { return std::string("any"); } @@ -54,7 +60,9 @@ std::string JsonSchemaChecker::createType(const PrefixTree::PrefixTreeNode* node throw Error(errLogic, "Incorrect schema type [%s]", chProps.type); } auto typeIndexIt = indexes_.find(subElement.typeName); - if (typeIndexIt == indexes_.end()) throw Error(errLogic, "Incorrect type %s", subElement.typeName); + if (typeIndexIt == indexes_.end()) { + throw Error(errLogic, "Incorrect type %s", subElement.typeName); + } subElement.typeIndex = typeIndexIt->second; typeDescr.subElementsTable.emplace_back(ch.first, std::move(subElement)); } @@ -74,7 +82,9 @@ void JsonSchemaChecker::addSimpleType(std::string tpName) { Error JsonSchemaChecker::createTypeTable(const std::string& json) { auto err = schema_.FromJSON(std::string_view(json)); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } auto root = schema_.GetRoot(); addSimpleType("any"); @@ -107,7 +117,9 @@ Error JsonSchemaChecker::Check(gason::JsonNode node) { } auto indxIt = indexes_.find(rootTypeName_); - if (indxIt == indexes_.end()) return Error(errParseJson, "Type '%s' not found.", rootTypeName_); + if (indxIt == indexes_.end()) { + return Error(errParseJson, "Type '%s' not found.", rootTypeName_); + } int nType = indxIt->second; std::string path; path.reserve(512); @@ -133,18 +145,23 @@ Error JsonSchemaChecker::checkScheme(const gason::JsonNode& node, int typeIndex, for (const auto& elem : node) { auto subElemIndex = descr.subElementsIndex.find(std::string_view(elem.key)); if (subElemIndex == descr.subElementsIndex.end()) { - if (!descr.allowAdditionalProps) + if (!descr.allowAdditionalProps) { return Error(errParseJson, "Key [%s] not allowed in [%s] object.", elem.key, path); - else + } else { continue; + } } err = checkExists(elem.key, &mmVals[subElemIndex->second], path); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } if (elem.value.getTag() == gason::JSON_OBJECT) { if (descr.subElementsTable[subElemIndex->second].second.typeName != "any") { err = checkScheme(elem, descr.subElementsTable[subElemIndex->second].second.typeIndex, path, descr.subElementsTable[subElemIndex->second].first); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } } else if (elem.value.getTag() == gason::JSON_ARRAY) { if (descr.subElementsTable[subElemIndex->second].second.typeName != "any") { @@ -155,7 +172,9 @@ Error JsonSchemaChecker::checkScheme(const gason::JsonNode& node, int typeIndex, if (entry.value.getTag() == gason::JSON_ARRAY || entry.value.getTag() == gason::JSON_OBJECT) { err = checkScheme(entry, descr.subElementsTable[subElemIndex->second].second.typeIndex, path, descr.subElementsTable[subElemIndex->second].first); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } } } diff --git a/cpp_src/core/cjson/jschemachecker.h b/cpp_src/core/cjson/jschemachecker.h index a869453ee..f0c1abc19 100644 --- a/cpp_src/core/cjson/jschemachecker.h +++ b/cpp_src/core/cjson/jschemachecker.h @@ -12,7 +12,7 @@ namespace reindexer { class JsonSchemaChecker { public: explicit JsonSchemaChecker(const std::string& json, std::string rootTypeName); - JsonSchemaChecker(){}; + JsonSchemaChecker() {}; Error Init(const std::string& json, std::string rootTypeName); Error Check(gason::JsonNode node); diff --git a/cpp_src/core/cjson/jsonbuilder.cc b/cpp_src/core/cjson/jsonbuilder.cc index b772dd0db..e93c05d05 100644 --- a/cpp_src/core/cjson/jsonbuilder.cc +++ b/cpp_src/core/cjson/jsonbuilder.cc @@ -2,7 +2,7 @@ namespace reindexer { -JsonBuilder::JsonBuilder(WrSerializer &ser, ObjType type, const TagsMatcher *tm, bool emitTrailingForFloat) +JsonBuilder::JsonBuilder(WrSerializer& ser, ObjType type, const TagsMatcher* tm, bool emitTrailingForFloat) : ser_(&ser), tm_(tm), type_(type), emitTrailingForFloat_(emitTrailingForFloat) { switch (type_) { case ObjType::TypeArray: @@ -19,7 +19,7 @@ JsonBuilder::JsonBuilder(WrSerializer &ser, ObjType type, const TagsMatcher *tm, std::string_view JsonBuilder::getNameByTag(int tagName) { return tagName ? tm_->tag2name(tagName) : std::string_view(); } -JsonBuilder &JsonBuilder::End() { +JsonBuilder& JsonBuilder::End() { switch (type_) { case ObjType::TypeArray: (*ser_) << ']'; @@ -47,38 +47,40 @@ JsonBuilder JsonBuilder::Array(std::string_view name, int /*size*/) { } void JsonBuilder::putName(std::string_view name) { - if (count_++) (*ser_) << ','; + if (count_++) { + (*ser_) << ','; + } if (name.data()) { // -V547 ser_->PrintJsonString(name); (*ser_) << ':'; } } -JsonBuilder &JsonBuilder::Put(std::string_view name, std::string_view arg, int /*offset*/) { +JsonBuilder& JsonBuilder::Put(std::string_view name, std::string_view arg, int /*offset*/) { putName(name); ser_->PrintJsonString(arg); return *this; } -JsonBuilder &JsonBuilder::Put(std::string_view name, Uuid arg, int /*offset*/) { +JsonBuilder& JsonBuilder::Put(std::string_view name, Uuid arg, int /*offset*/) { putName(name); ser_->PrintJsonUuid(arg); return *this; } -JsonBuilder &JsonBuilder::Raw(std::string_view name, std::string_view arg) { +JsonBuilder& JsonBuilder::Raw(std::string_view name, std::string_view arg) { putName(name); (*ser_) << arg; return *this; } -JsonBuilder &JsonBuilder::Null(std::string_view name) { +JsonBuilder& JsonBuilder::Null(std::string_view name) { putName(name); (*ser_) << "null"; return *this; } -JsonBuilder &JsonBuilder::Put(std::string_view name, const Variant &kv, int offset) { +JsonBuilder& JsonBuilder::Put(std::string_view name, const Variant& kv, int offset) { kv.Type().EvaluateOneOf( [&](KeyValueType::Int) { Put(name, int(kv), offset); }, [&](KeyValueType::Int64) { Put(name, int64_t(kv), offset); }, [&](KeyValueType::Double) { Put(name, double(kv), offset); }, @@ -86,7 +88,7 @@ JsonBuilder &JsonBuilder::Put(std::string_view name, const Variant &kv, int offs [&](KeyValueType::Bool) { Put(name, bool(kv), offset); }, [&](KeyValueType::Tuple) { auto arrNode = Array(name); - for (auto &val : kv.getCompositeValues()) { + for (auto& val : kv.getCompositeValues()) { arrNode.Put({nullptr, 0}, val, offset); } }, diff --git a/cpp_src/core/cjson/jsonbuilder.h b/cpp_src/core/cjson/jsonbuilder.h index 813a2fd42..015ec978a 100644 --- a/cpp_src/core/cjson/jsonbuilder.h +++ b/cpp_src/core/cjson/jsonbuilder.h @@ -9,17 +9,17 @@ namespace reindexer { class JsonBuilder { public: JsonBuilder() noexcept : ser_(nullptr), tm_(nullptr) {} - JsonBuilder(WrSerializer &ser, ObjType type = ObjType::TypeObject, const TagsMatcher *tm = nullptr, bool emitTrailingForFloat = true); + JsonBuilder(WrSerializer& ser, ObjType type = ObjType::TypeObject, const TagsMatcher* tm = nullptr, bool emitTrailingForFloat = true); ~JsonBuilder() { End(); } - JsonBuilder(const JsonBuilder &) = delete; - JsonBuilder(JsonBuilder &&other) noexcept + JsonBuilder(const JsonBuilder&) = delete; + JsonBuilder(JsonBuilder&& other) noexcept : ser_(other.ser_), tm_(other.tm_), type_(other.type_), count_(other.count_), emitTrailingForFloat_(other.emitTrailingForFloat_) { other.type_ = ObjType::TypePlain; } - JsonBuilder &operator=(const JsonBuilder &) = delete; - JsonBuilder &operator=(JsonBuilder &&) = delete; + JsonBuilder& operator=(const JsonBuilder&) = delete; + JsonBuilder& operator=(JsonBuilder&&) = delete; - void SetTagsMatcher(const TagsMatcher *tm) noexcept { tm_ = tm; } + void SetTagsMatcher(const TagsMatcher* tm) noexcept { tm_ = tm; } void EmitTrailingForFloat(bool val) noexcept { emitTrailingForFloat_ = val; } /// Start new object @@ -33,39 +33,47 @@ class JsonBuilder { template void Array(int tagName, span data, int /*offset*/ = 0) { JsonBuilder node = Array(tagName); - for (const auto &d : data) node.Put({}, d); + for (const auto& d : data) { + node.Put({}, d); + } } template void Array(std::string_view n, span data, int /*offset*/ = 0) { JsonBuilder node = Array(n); - for (const auto &d : data) node.Put({}, d); + for (const auto& d : data) { + node.Put({}, d); + } } template void Array(std::string_view n, std::initializer_list data, int /*offset*/ = 0) { JsonBuilder node = Array(n); - for (const auto &d : data) node.Put({}, d); + for (const auto& d : data) { + node.Put({}, d); + } } - void Array(int tagName, Serializer &ser, TagType tagType, int count) { + void Array(int tagName, Serializer& ser, TagType tagType, int count) { JsonBuilder node = Array(tagName); const KeyValueType kvt{tagType}; - while (count--) node.Put({}, ser.GetRawVariant(kvt)); + while (count--) { + node.Put({}, ser.GetRawVariant(kvt)); + } } - JsonBuilder &Put(std::string_view name, const Variant &arg, int offset = 0); - JsonBuilder &Put(std::nullptr_t, const Variant &arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } - JsonBuilder &Put(std::string_view name, std::string_view arg, int offset = 0); - JsonBuilder &Put(std::string_view name, Uuid arg, int offset = 0); - JsonBuilder &Put(std::nullptr_t, std::string_view arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } - JsonBuilder &Put(std::string_view name, const char *arg, int offset = 0) { return Put(name, std::string_view(arg), offset); } - template ::value>::type * = nullptr> - JsonBuilder &Put(std::string_view name, const T &arg, int /*offset*/ = 0) { + JsonBuilder& Put(std::string_view name, const Variant& arg, int offset = 0); + JsonBuilder& Put(std::nullptr_t, const Variant& arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } + JsonBuilder& Put(std::string_view name, std::string_view arg, int offset = 0); + JsonBuilder& Put(std::string_view name, Uuid arg, int offset = 0); + JsonBuilder& Put(std::nullptr_t, std::string_view arg, int offset = 0) { return Put(std::string_view{}, arg, offset); } + JsonBuilder& Put(std::string_view name, const char* arg, int offset = 0) { return Put(name, std::string_view(arg), offset); } + template ::value>::type* = nullptr> + JsonBuilder& Put(std::string_view name, const T& arg, int /*offset*/ = 0) { putName(name); (*ser_) << arg; return *this; } - template ::value>::type * = nullptr> - JsonBuilder &Put(std::string_view name, const T &arg, int /*offset*/ = 0) { + template ::value>::type* = nullptr> + JsonBuilder& Put(std::string_view name, const T& arg, int /*offset*/ = 0) { putName(name); if (emitTrailingForFloat_) { (*ser_) << arg; @@ -75,27 +83,27 @@ class JsonBuilder { return *this; } template - JsonBuilder &Put(int tagName, const T &arg, int offset = 0) { + JsonBuilder& Put(int tagName, const T& arg, int offset = 0) { return Put(getNameByTag(tagName), arg, offset); } - JsonBuilder &Raw(int tagName, std::string_view arg) { return Raw(getNameByTag(tagName), arg); } - JsonBuilder &Raw(std::string_view name, std::string_view arg); - JsonBuilder &Raw(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } - JsonBuilder &Json(std::string_view name, std::string_view arg) { return Raw(name, arg); } - JsonBuilder &Json(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } + JsonBuilder& Raw(int tagName, std::string_view arg) { return Raw(getNameByTag(tagName), arg); } + JsonBuilder& Raw(std::string_view name, std::string_view arg); + JsonBuilder& Raw(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } + JsonBuilder& Json(std::string_view name, std::string_view arg) { return Raw(name, arg); } + JsonBuilder& Json(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } - JsonBuilder &Null(int tagName) { return Null(getNameByTag(tagName)); } - JsonBuilder &Null(std::string_view name); + JsonBuilder& Null(int tagName) { return Null(getNameByTag(tagName)); } + JsonBuilder& Null(std::string_view name); - JsonBuilder &End(); + JsonBuilder& End(); protected: void putName(std::string_view name); std::string_view getNameByTag(int tagName); - WrSerializer *ser_; - const TagsMatcher *tm_; + WrSerializer* ser_; + const TagsMatcher* tm_; ObjType type_ = ObjType::TypePlain; int count_ = 0; bool emitTrailingForFloat_ = true; diff --git a/cpp_src/core/cjson/jsondecoder.cc b/cpp_src/core/cjson/jsondecoder.cc index aa153a828..85d3ac79d 100644 --- a/cpp_src/core/cjson/jsondecoder.cc +++ b/cpp_src/core/cjson/jsondecoder.cc @@ -9,7 +9,7 @@ namespace reindexer { -Error JsonDecoder::Decode(Payload &pl, WrSerializer &wrser, const gason::JsonValue &v) { +Error JsonDecoder::Decode(Payload& pl, WrSerializer& wrser, const gason::JsonValue& v) { try { objectScalarIndexes_.reset(); tagsPath_.clear(); @@ -17,42 +17,43 @@ Error JsonDecoder::Decode(Payload &pl, WrSerializer &wrser, const gason::JsonVal decodeJson(&pl, builder, v, 0, true); } - catch (const Error &err) { + catch (const Error& err) { return err; } return {}; } -void JsonDecoder::decodeJsonObject(Payload &pl, CJsonBuilder &builder, const gason::JsonValue &v, bool match) { - for (const auto &elem : v) { +void JsonDecoder::decodeJsonObject(Payload& pl, CJsonBuilder& builder, const gason::JsonValue& v, bool match) { + for (const auto& elem : v) { int tagName = tagsMatcher_.name2tag(elem.key, true); assertrx(tagName); tagsPath_.emplace_back(tagName); int field = tagsMatcher_.tags2field(tagsPath_.data(), tagsPath_.size()); if (filter_) { - if (field >= 0) + if (field >= 0) { match = filter_->contains(field); - else + } else { match = match && filter_->match(tagsPath_); + } } if (field < 0) { decodeJson(&pl, builder, elem.value, tagName, match); } else if (match) { // Indexed field. extract it - const auto &f = pl.Type().Field(field); + const auto& f = pl.Type().Field(field); switch (elem.value.getTag()) { case gason::JSON_ARRAY: { if rx_unlikely (!f.IsArray()) { throw Error(errLogic, "Error parsing json field '%s' - got array, expected scalar %s", f.Name(), f.Type().Name()); } int count = 0; - for (auto &subelem : elem.value) { + for (auto& subelem : elem.value) { (void)subelem; ++count; } int pos = pl.ResizeArray(field, count, true); - for (auto &subelem : elem.value) { + for (auto& subelem : elem.value) { pl.Set(field, pos++, jsonValue2Variant(subelem.value, f.Type(), f.Name())); } builder.ArrayRef(tagName, field, count); @@ -87,9 +88,11 @@ void JsonDecoder::decodeJsonObject(Payload &pl, CJsonBuilder &builder, const gas // Split original JSON into 2 parts: // 1. PayloadFields - fields from json found by 'jsonPath' tags // 2. stripped binary packed JSON without fields values found by 'jsonPath' tags -void JsonDecoder::decodeJson(Payload *pl, CJsonBuilder &builder, const gason::JsonValue &v, int tagName, bool match) { +void JsonDecoder::decodeJson(Payload* pl, CJsonBuilder& builder, const gason::JsonValue& v, int tagName, bool match) { auto jsonTag = v.getTag(); - if (!match && jsonTag != gason::JSON_OBJECT) return; + if (!match && jsonTag != gason::JSON_OBJECT) { + return; + } switch (jsonTag) { case gason::JSON_NUMBER: { int64_t value = v.toNumber(); @@ -120,7 +123,7 @@ void JsonDecoder::decodeJson(Payload *pl, CJsonBuilder &builder, const gason::Js type = ObjType::TypeObjectArray; } auto arrNode = builder.Array(tagName, type); - for (const auto &elem : v) { + for (const auto& elem : v) { decodeJson(pl, arrNode, elem.value, 0, match); } break; @@ -141,29 +144,29 @@ void JsonDecoder::decodeJson(Payload *pl, CJsonBuilder &builder, const gason::Js class TagsPathGuard { public: - TagsPathGuard(TagsPath &tagsPath, int tagName) noexcept : tagsPath_(tagsPath) { tagsPath_.emplace_back(tagName); } + TagsPathGuard(TagsPath& tagsPath, int tagName) noexcept : tagsPath_(tagsPath) { tagsPath_.emplace_back(tagName); } ~TagsPathGuard() { tagsPath_.pop_back(); } public: - TagsPath &tagsPath_; + TagsPath& tagsPath_; }; -void JsonDecoder::decodeJsonObject(const gason::JsonValue &root, CJsonBuilder &builder) { - for (const auto &elem : root) { +void JsonDecoder::decodeJsonObject(const gason::JsonValue& root, CJsonBuilder& builder) { + for (const auto& elem : root) { int tagName = tagsMatcher_.name2tag(elem.key, true); TagsPathGuard tagsPathGuard(tagsPath_, tagName); decodeJson(nullptr, builder, elem.value, tagName, true); } } -void JsonDecoder::Decode(std::string_view json, CJsonBuilder &builder, const TagsPath &fieldPath) { +void JsonDecoder::Decode(std::string_view json, CJsonBuilder& builder, const TagsPath& fieldPath) { try { objectScalarIndexes_.reset(); tagsPath_ = fieldPath; gason::JsonParser jsonParser; gason::JsonNode root = jsonParser.Parse(json); decodeJsonObject(root.value, builder); - } catch (gason::Exception &e) { + } catch (gason::Exception& e) { throw Error(errParseJson, "JSONDecoder: %s", e.what()); } } diff --git a/cpp_src/core/cjson/jsondecoder.h b/cpp_src/core/cjson/jsondecoder.h index c44a1432c..73dad50f2 100644 --- a/cpp_src/core/cjson/jsondecoder.h +++ b/cpp_src/core/cjson/jsondecoder.h @@ -8,20 +8,20 @@ namespace reindexer { class JsonDecoder { public: - explicit JsonDecoder(TagsMatcher &tagsMatcher) noexcept : tagsMatcher_(tagsMatcher), filter_(nullptr) {} - JsonDecoder(TagsMatcher &tagsMatcher, const FieldsSet *filter) noexcept : tagsMatcher_(tagsMatcher), filter_(filter) {} - Error Decode(Payload &pl, WrSerializer &wrSer, const gason::JsonValue &v); - void Decode(std::string_view json, CJsonBuilder &builder, const TagsPath &fieldPath); + explicit JsonDecoder(TagsMatcher& tagsMatcher) noexcept : tagsMatcher_(tagsMatcher), filter_(nullptr) {} + JsonDecoder(TagsMatcher& tagsMatcher, const FieldsSet* filter) noexcept : tagsMatcher_(tagsMatcher), filter_(filter) {} + Error Decode(Payload& pl, WrSerializer& wrSer, const gason::JsonValue& v); + void Decode(std::string_view json, CJsonBuilder& builder, const TagsPath& fieldPath); private: - void decodeJsonObject(const gason::JsonValue &root, CJsonBuilder &builder); - void decodeJsonObject(Payload &pl, CJsonBuilder &builder, const gason::JsonValue &v, bool match); - void decodeJson(Payload *pl, CJsonBuilder &builder, const gason::JsonValue &v, int tag, bool match); + void decodeJsonObject(const gason::JsonValue& root, CJsonBuilder& builder); + void decodeJsonObject(Payload& pl, CJsonBuilder& builder, const gason::JsonValue& v, bool match); + void decodeJson(Payload* pl, CJsonBuilder& builder, const gason::JsonValue& v, int tag, bool match); bool isInArray() const noexcept { return arrayLevel_ > 0; } - TagsMatcher &tagsMatcher_; + TagsMatcher& tagsMatcher_; TagsPath tagsPath_; - const FieldsSet *filter_; + const FieldsSet* filter_; int32_t arrayLevel_ = 0; ScalarIndexesSetT objectScalarIndexes_; }; diff --git a/cpp_src/core/cjson/msgpackbuilder.cc b/cpp_src/core/cjson/msgpackbuilder.cc index f50ba39eb..16158de01 100644 --- a/cpp_src/core/cjson/msgpackbuilder.cc +++ b/cpp_src/core/cjson/msgpackbuilder.cc @@ -5,46 +5,48 @@ namespace reindexer { -MsgPackBuilder::MsgPackBuilder(WrSerializer &wrser, ObjType type, size_t size) +MsgPackBuilder::MsgPackBuilder(WrSerializer& wrser, ObjType type, size_t size) : tm_(nullptr), tagsLengths_(nullptr), type_(type), tagIndex_(nullptr) { - msgpack_packer_init(&packer_, reinterpret_cast(&wrser), msgpack_wrserializer_write); + msgpack_packer_init(&packer_, reinterpret_cast(&wrser), msgpack_wrserializer_write); init(size); } -MsgPackBuilder::MsgPackBuilder(msgpack_packer &packer, ObjType type, size_t size) +MsgPackBuilder::MsgPackBuilder(msgpack_packer& packer, ObjType type, size_t size) : tm_(nullptr), packer_(packer), tagsLengths_(nullptr), type_(type), tagIndex_(nullptr) { init(size); } -MsgPackBuilder::MsgPackBuilder(WrSerializer &wrser, const TagsLengths *tagsLengths, int *startTag, ObjType type, const TagsMatcher *tm) +MsgPackBuilder::MsgPackBuilder(WrSerializer& wrser, const TagsLengths* tagsLengths, int* startTag, ObjType type, const TagsMatcher* tm) : tm_(tm), tagsLengths_(tagsLengths), type_(type), tagIndex_(startTag) { assertrx(startTag); - msgpack_packer_init(&packer_, reinterpret_cast(&wrser), msgpack_wrserializer_write); + msgpack_packer_init(&packer_, reinterpret_cast(&wrser), msgpack_wrserializer_write); init(KUnknownFieldSize); } -MsgPackBuilder::MsgPackBuilder(msgpack_packer &packer, const TagsLengths *tagsLengths, int *startTag, ObjType type, const TagsMatcher *tm) +MsgPackBuilder::MsgPackBuilder(msgpack_packer& packer, const TagsLengths* tagsLengths, int* startTag, ObjType type, const TagsMatcher* tm) : tm_(tm), packer_(packer), tagsLengths_(tagsLengths), type_(type), tagIndex_(startTag) { assertrx(startTag); init(KUnknownFieldSize); } -void MsgPackBuilder::Array(int tagName, Serializer &ser, TagType tagType, int count) { +void MsgPackBuilder::Array(int tagName, Serializer& ser, TagType tagType, int count) { checkIfCorrectArray(tagName); skipTag(); packKeyName(tagName); packArray(count); - while (count--) packCJsonValue(tagType, ser); + while (count--) { + packCJsonValue(tagType, ser); + } } -MsgPackBuilder &MsgPackBuilder::Json(std::string_view name, std::string_view arg) { +MsgPackBuilder& MsgPackBuilder::Json(std::string_view name, std::string_view arg) { gason::JsonParser parser; auto root = parser.Parse(arg); appendJsonObject(name, root); return *this; } -MsgPackBuilder &MsgPackBuilder::End() { +MsgPackBuilder& MsgPackBuilder::End() { switch (type_) { case ObjType::TypeObjectArray: case ObjType::TypeArray: @@ -77,7 +79,7 @@ void MsgPackBuilder::init(int size) { } } -void MsgPackBuilder::packCJsonValue(TagType tagType, Serializer &rdser) { +void MsgPackBuilder::packCJsonValue(TagType tagType, Serializer& rdser) { switch (tagType) { case TAG_DOUBLE: packValue(rdser.GetDouble()); @@ -104,7 +106,7 @@ void MsgPackBuilder::packCJsonValue(TagType tagType, Serializer &rdser) { } } -void MsgPackBuilder::appendJsonObject(std::string_view name, const gason::JsonNode &obj) { +void MsgPackBuilder::appendJsonObject(std::string_view name, const gason::JsonNode& obj) { auto type = obj.value.getTag(); switch (type) { case gason::JSON_STRING: { @@ -122,18 +124,18 @@ void MsgPackBuilder::appendJsonObject(std::string_view name, const gason::JsonNo case gason::JSON_OBJECT: case gason::JSON_ARRAY: { int size = 0; - for (const auto &node : obj) { + for (const auto& node : obj) { (void)node; ++size; } if (type == gason::JSON_OBJECT) { auto pack = Object(name, size); - for (const auto &node : obj) { + for (const auto& node : obj) { pack.appendJsonObject(std::string_view(node.key), node); } } else { auto pack = Array(name, size); - for (const auto &node : obj) { + for (const auto& node : obj) { pack.appendJsonObject(std::string_view(), node); } } diff --git a/cpp_src/core/cjson/msgpackbuilder.h b/cpp_src/core/cjson/msgpackbuilder.h index a6e6e5bb6..fa6244d1e 100644 --- a/cpp_src/core/cjson/msgpackbuilder.h +++ b/cpp_src/core/cjson/msgpackbuilder.h @@ -16,22 +16,22 @@ namespace reindexer { class MsgPackBuilder { public: - MsgPackBuilder(WrSerializer &wrser, ObjType type, size_t size); - MsgPackBuilder(msgpack_packer &packer, ObjType type, size_t size); - MsgPackBuilder(WrSerializer &wrser, const TagsLengths *tagsLengths, int *startTag, ObjType = ObjType::TypeObject, - const TagsMatcher *tm = nullptr); - MsgPackBuilder(msgpack_packer &packer, const TagsLengths *tagsLengths, int *startTag, ObjType = ObjType::TypeObject, - const TagsMatcher *tm = nullptr); + MsgPackBuilder(WrSerializer& wrser, ObjType type, size_t size); + MsgPackBuilder(msgpack_packer& packer, ObjType type, size_t size); + MsgPackBuilder(WrSerializer& wrser, const TagsLengths* tagsLengths, int* startTag, ObjType = ObjType::TypeObject, + const TagsMatcher* tm = nullptr); + MsgPackBuilder(msgpack_packer& packer, const TagsLengths* tagsLengths, int* startTag, ObjType = ObjType::TypeObject, + const TagsMatcher* tm = nullptr); MsgPackBuilder() noexcept : tm_(nullptr), packer_(), tagsLengths_(nullptr), type_(ObjType::TypePlain), tagIndex_(nullptr) {} ~MsgPackBuilder() { End(); } - MsgPackBuilder(MsgPackBuilder &&other) noexcept + MsgPackBuilder(MsgPackBuilder&& other) noexcept : tm_(other.tm_), packer_(other.packer_), tagsLengths_(other.tagsLengths_), type_(other.type_), tagIndex_(other.tagIndex_) {} - MsgPackBuilder(const MsgPackBuilder &) = delete; - MsgPackBuilder &operator=(const MsgPackBuilder &) = delete; - MsgPackBuilder &operator=(MsgPackBuilder &&) = delete; + MsgPackBuilder(const MsgPackBuilder&) = delete; + MsgPackBuilder& operator=(const MsgPackBuilder&) = delete; + MsgPackBuilder& operator=(MsgPackBuilder&&) = delete; - void SetTagsMatcher(const TagsMatcher *tm) noexcept { tm_ = tm; } + void SetTagsMatcher(const TagsMatcher* tm) noexcept { tm_ = tm; } MsgPackBuilder Raw(std::string_view, std::string_view) noexcept { return MsgPackBuilder(); } MsgPackBuilder Raw(std::nullptr_t, std::string_view arg) { return Raw(std::string_view{}, arg); } @@ -41,7 +41,9 @@ class MsgPackBuilder { skipTag(); packKeyName(tagName); packArray(data.size()); - for (const T &v : data) packValue(v); + for (const T& v : data) { + packValue(v); + } } template void Array(N tagName, span data, int /*offset*/ = 0) { @@ -49,7 +51,9 @@ class MsgPackBuilder { skipTag(); packKeyName(tagName); packArray(data.size()); - for (Uuid v : data) packValue(v); + for (Uuid v : data) { + packValue(v); + } } template @@ -58,7 +62,9 @@ class MsgPackBuilder { skipTag(); packKeyName(tagName); packArray(data.size()); - for (const p_string &v : data) packValue(std::string_view(v)); + for (const p_string& v : data) { + packValue(std::string_view(v)); + } } template @@ -72,12 +78,14 @@ class MsgPackBuilder { return MsgPackBuilder(packer_, ObjType::TypeObjectArray, size); } } - void Array(int tagName, Serializer &ser, TagType, int count); + void Array(int tagName, Serializer& ser, TagType, int count); template MsgPackBuilder Object(T tagName, int size = KUnknownFieldSize) { packKeyName(tagName); - if (isArray()) skipTag(); + if (isArray()) { + skipTag(); + } if (size == KUnknownFieldSize) { assertrx(tagsLengths_ && tagIndex_); return MsgPackBuilder(packer_, tagsLengths_, tagIndex_, ObjType::TypeObject, tm_); @@ -87,7 +95,7 @@ class MsgPackBuilder { } template - MsgPackBuilder &Null(T tagName) { + MsgPackBuilder& Null(T tagName) { skipTag(); packKeyName(tagName); packNil(); @@ -95,28 +103,38 @@ class MsgPackBuilder { } template - MsgPackBuilder &Put(N tagName, const T &arg, int /*offset*/ = 0) { - if (isArray()) skipTag(); + MsgPackBuilder& Put(N tagName, const T& arg, int /*offset*/ = 0) { + if (isArray()) { + skipTag(); + } skipTag(); packKeyName(tagName); packValue(arg); - if (isArray()) skipTag(); + if (isArray()) { + skipTag(); + } return *this; } template - MsgPackBuilder &Put(N tagName, Uuid arg) { - if (isArray()) skipTag(); + MsgPackBuilder& Put(N tagName, Uuid arg) { + if (isArray()) { + skipTag(); + } skipTag(); packKeyName(tagName); packValue(arg); - if (isArray()) skipTag(); + if (isArray()) { + skipTag(); + } return *this; } template - MsgPackBuilder &Put(T tagName, const Variant &kv, int offset = 0) { - if (isArray()) skipTag(); + MsgPackBuilder& Put(T tagName, const Variant& kv, int offset = 0) { + if (isArray()) { + skipTag(); + } skipTag(); packKeyName(tagName); kv.Type().EvaluateOneOf( @@ -125,22 +143,24 @@ class MsgPackBuilder { [&](KeyValueType::Null) { packNil(); }, [&](KeyValueType::Bool) { packValue(bool(kv)); }, [&](KeyValueType::Tuple) { auto arrNode = Array(tagName); - for (auto &val : kv.getCompositeValues()) { + for (auto& val : kv.getCompositeValues()) { arrNode.Put(0, val, offset); } }, [&](KeyValueType::Uuid) { packValue(Uuid{kv}); }, [](OneOf) noexcept {}); - if (isArray()) skipTag(); + if (isArray()) { + skipTag(); + } return *this; } - MsgPackBuilder &Json(std::string_view name, std::string_view arg); + MsgPackBuilder& Json(std::string_view name, std::string_view arg); - MsgPackBuilder &End(); + MsgPackBuilder& End(); private: void init(int size); - void packCJsonValue(TagType, Serializer &); + void packCJsonValue(TagType, Serializer&); void packNil() { msgpack_pack_nil(&packer_); } void packMap(size_t size) { msgpack_pack_map(&packer_, size); } @@ -171,15 +191,21 @@ class MsgPackBuilder { void checkIfCorrectArray(std::string_view) const {} void checkIfCorrectArray(int tagName) const { - if (tagName == 0) throw Error(errLogic, "Arrays of arrays are not supported in cjson"); + if (tagName == 0) { + throw Error(errLogic, "Arrays of arrays are not supported in cjson"); + } } void packKeyName(std::nullptr_t) {} void packKeyName(std::string_view name) { - if (!name.empty() && !isArray()) packValue(name); + if (!name.empty() && !isArray()) { + packValue(name); + } } void packKeyName(int tagName) { - if (tagName != 0 && !isArray()) packValue(tm_->tag2name(tagName)); + if (tagName != 0 && !isArray()) { + packValue(tm_->tag2name(tagName)); + } } int getTagSize() { @@ -190,7 +216,9 @@ class MsgPackBuilder { } void skipTag() { - if (tagsLengths_) ++(*tagIndex_); + if (tagsLengths_) { + ++(*tagIndex_); + } } void skipTagIfEqual(TagValues tagVal) { @@ -199,13 +227,13 @@ class MsgPackBuilder { } } - void appendJsonObject(std::string_view name, const gason::JsonNode &obj); + void appendJsonObject(std::string_view name, const gason::JsonNode& obj); - const TagsMatcher *tm_; + const TagsMatcher* tm_; msgpack_packer packer_; - const TagsLengths *tagsLengths_; + const TagsLengths* tagsLengths_; ObjType type_; - int *tagIndex_; + int* tagIndex_; }; } // namespace reindexer diff --git a/cpp_src/core/cjson/msgpackdecoder.cc b/cpp_src/core/cjson/msgpackdecoder.cc index 965fd2694..d75d8b606 100644 --- a/cpp_src/core/cjson/msgpackdecoder.cc +++ b/cpp_src/core/cjson/msgpackdecoder.cc @@ -51,7 +51,9 @@ int MsgPackDecoder::decodeKeyToTag(const msgpack_object_kv& obj) { } void MsgPackDecoder::decode(Payload& pl, CJsonBuilder& builder, const msgpack_object& obj, int tagName) { - if (tagName) tagsPath_.emplace_back(tagName); + if (tagName) { + tagsPath_.emplace_back(tagName); + } switch (obj.type) { case MSGPACK_OBJECT_NIL: builder.Null(tagName); @@ -118,7 +120,9 @@ void MsgPackDecoder::decode(Payload& pl, CJsonBuilder& builder, const msgpack_ob default: throw Error(errParams, "Unsupported MsgPack type: %s(%d)", ToString(obj.type), obj.type); } - if (tagName) tagsPath_.pop_back(); + if (tagName) { + tagsPath_.pop_back(); + } } Error MsgPackDecoder::Decode(std::string_view buf, Payload& pl, WrSerializer& wrser, size_t& offset) { @@ -127,7 +131,9 @@ Error MsgPackDecoder::Decode(std::string_view buf, Payload& pl, WrSerializer& wr tagsPath_.clear(); size_t baseOffset = offset; MsgPackValue data = parser_.Parse(buf, offset); - if rx_unlikely (!data.p) return Error(errLogic, "Error unpacking msgpack data"); + if rx_unlikely (!data.p) { + return Error(errLogic, "Error unpacking msgpack data"); + } if rx_unlikely (data.p->type != MSGPACK_OBJECT_MAP) { std::string_view slice = buf.substr(baseOffset, 16); return Error(errNotValid, "Unexpected MsgPack value. Expected %s, but got %s(%d) at %d(~>\"%s\"...)", diff --git a/cpp_src/core/cjson/protobufbuilder.h b/cpp_src/core/cjson/protobufbuilder.h index 22ac1e7c0..513d7bf1c 100644 --- a/cpp_src/core/cjson/protobufbuilder.h +++ b/cpp_src/core/cjson/protobufbuilder.h @@ -107,10 +107,14 @@ class ProtobufBuilder { void Array(int fieldIdx, Serializer& rdser, TagType tagType, int count) { if (tagType == TAG_VARINT || tagType == TAG_DOUBLE || tagType == TAG_BOOL) { auto array = ArrayPacked(fieldIdx); - while (count--) packItem(fieldIdx, tagType, rdser, array); + while (count--) { + packItem(fieldIdx, tagType, rdser, array); + } } else { auto array = ArrayNotPacked(fieldIdx); - while (count--) packItem(fieldIdx, tagType, rdser, array); + while (count--) { + packItem(fieldIdx, tagType, rdser, array); + } } } diff --git a/cpp_src/core/cjson/protobufschemabuilder.cc b/cpp_src/core/cjson/protobufschemabuilder.cc index 068f0db29..c6ca06c31 100644 --- a/cpp_src/core/cjson/protobufschemabuilder.cc +++ b/cpp_src/core/cjson/protobufschemabuilder.cc @@ -13,12 +13,20 @@ ProtobufSchemaBuilder::ProtobufSchemaBuilder(WrSerializer* ser, SchemaFieldsType : ser_(ser), fieldsTypes_(fieldsTypes), pt_(pt), tm_(tm), type_(type) { switch (type_) { case ObjType::TypePlain: - if (ser_) ser_->Write("syntax = \"proto3\";\n\n"); + if (ser_) { + ser_->Write("syntax = \"proto3\";\n\n"); + } break; case ObjType::TypeObject: - if (ser_) ser_->Write("message "); - if (ser_) ser_->Write(name); - if (ser_) ser_->Write(" {\n"); + if (ser_) { + ser_->Write("message "); + } + if (ser_) { + ser_->Write(name); + } + if (ser_) { + ser_->Write(" {\n"); + } break; case ObjType::TypeObjectArray: case ObjType::TypeArray: @@ -62,7 +70,9 @@ void ProtobufSchemaBuilder::End() { if (fieldsTypes_->tagsPath_.size() > 0) { fieldsTypes_->tagsPath_.pop_back(); } - if (ser_) ser_->Write("}\n"); + if (ser_) { + ser_->Write("}\n"); + } } type_ = ObjType::TypePlain; } @@ -75,11 +85,15 @@ void ProtobufSchemaBuilder::Field(std::string_view name, int tagName, const Fiel } if (props.isArray) { assertrx(type_ != ObjType::TypeArray && type_ != ObjType::TypeObjectArray); - if (ser_) ser_->Write("repeated "); + if (ser_) { + ser_->Write("repeated "); + } writeField(name, typeName, tagName); type.EvaluateOneOf( [&](OneOf) { - if (ser_) ser_->Write(" [packed=true]"); + if (ser_) { + ser_->Write(" [packed=true]"); + } }, [](OneOf) noexcept {}); @@ -87,7 +101,9 @@ void ProtobufSchemaBuilder::Field(std::string_view name, int tagName, const Fiel writeField(name, typeName, tagName); } fieldsTypes_->AddField(type, props.isArray); - if (ser_) ser_->Write(";\n"); + if (ser_) { + ser_->Write(";\n"); + } } ProtobufSchemaBuilder ProtobufSchemaBuilder::Object(int tagName, std::string_view name, bool buildTypesOnly, diff --git a/cpp_src/core/cjson/tagslengths.cc b/cpp_src/core/cjson/tagslengths.cc index 0e59459de..7ed567376 100644 --- a/cpp_src/core/cjson/tagslengths.cc +++ b/cpp_src/core/cjson/tagslengths.cc @@ -10,7 +10,9 @@ int computeObjectLength(TagsLengths& tagsLengths, std::size_t startTag, std::siz if (tagsLengths[i] == TagValues::StartObject) { std::size_t pos; tagsLengths[i] = computeObjectLength(tagsLengths, i + 1, pos); - if (!arrayItem) ++objectLength; + if (!arrayItem) { + ++objectLength; + } i = pos; } else if (tagsLengths[i] == TagValues::EndObject) { ++i; @@ -22,7 +24,9 @@ int computeObjectLength(TagsLengths& tagsLengths, std::size_t startTag, std::siz arrayItem = false; ++i; } else { - if (!arrayItem) ++objectLength; + if (!arrayItem) { + ++objectLength; + } ++i; } } diff --git a/cpp_src/core/cjson/tagsmatcher.h b/cpp_src/core/cjson/tagsmatcher.h index a69a31e71..2b7355206 100644 --- a/cpp_src/core/cjson/tagsmatcher.h +++ b/cpp_src/core/cjson/tagsmatcher.h @@ -16,7 +16,9 @@ class TagsMatcher { int name2tag(std::string_view name) const { return impl_->name2tag(name); } int name2tag(std::string_view name, bool canAdd) { - if (!name.data()) return 0; // -V547 + if (!name.data()) { + return 0; // -V547 + } int res = impl_->name2tag(name); return res ? res : impl_.clone()->name2tag(name, canAdd, updated_); } @@ -24,7 +26,9 @@ class TagsMatcher { const std::string& tag2name(int tag) const { return impl_->tag2name(tag); } TagsPath path2tag(std::string_view jsonPath) const { return impl_->path2tag(jsonPath); } TagsPath path2tag(std::string_view jsonPath, bool canAdd) { - if (jsonPath.empty()) return TagsPath(); + if (jsonPath.empty()) { + return TagsPath(); + } auto res = path2tag(jsonPath); return res.empty() && canAdd ? impl_.clone()->path2tag(jsonPath, canAdd, updated_) : res; } @@ -34,7 +38,9 @@ class TagsMatcher { return tagsPath; } IndexedTagsPath path2indexedtag(std::string_view jsonPath, bool canAdd) { - if (jsonPath.empty()) return IndexedTagsPath(); + if (jsonPath.empty()) { + return IndexedTagsPath(); + } auto res = impl_->path2indexedtag(jsonPath); return res.empty() && canAdd ? impl_.clone()->path2indexedtag(jsonPath, canAdd, updated_) : res; } diff --git a/cpp_src/core/cjson/tagsmatcherimpl.h b/cpp_src/core/cjson/tagsmatcherimpl.h index c23c757e3..31c875b29 100644 --- a/cpp_src/core/cjson/tagsmatcherimpl.h +++ b/cpp_src/core/cjson/tagsmatcherimpl.h @@ -18,7 +18,7 @@ namespace reindexer { class TagsMatcherImpl { public: TagsMatcherImpl() : version_(0), stateToken_(tools::RandomGenerator::gets32()) {} - TagsMatcherImpl(PayloadType &&payloadType) : version_(0), stateToken_(tools::RandomGenerator::gets32()) { + TagsMatcherImpl(PayloadType&& payloadType) : version_(0), stateToken_(tools::RandomGenerator::gets32()) { bool updated = false; updatePayloadType(std::move(payloadType), updated, false); (void)updated; // No update check required @@ -26,10 +26,10 @@ class TagsMatcherImpl { TagsPath path2tag(std::string_view jsonPath) const { bool updated = false; - return const_cast(this)->path2tag(jsonPath, false, updated); + return const_cast(this)->path2tag(jsonPath, false, updated); } - TagsPath path2tag(std::string_view jsonPath, bool canAdd, bool &updated) { + TagsPath path2tag(std::string_view jsonPath, bool canAdd, bool& updated) { TagsPath fieldTags; for (size_t pos = 0, lastPos = 0; pos != jsonPath.length(); lastPos = pos + 1) { pos = jsonPath.find('.', lastPos); @@ -51,10 +51,10 @@ class TagsMatcherImpl { IndexedTagsPath path2indexedtag(std::string_view jsonPath) const { bool updated = false; - return const_cast(this)->path2indexedtag(jsonPath, false, updated); + return const_cast(this)->path2indexedtag(jsonPath, false, updated); } - IndexedTagsPath path2indexedtag(std::string_view jsonPath, bool canAdd, bool &updated) { + IndexedTagsPath path2indexedtag(std::string_view jsonPath, bool canAdd, bool& updated) { using namespace std::string_view_literals; IndexedTagsPath fieldTags; for (size_t pos = 0, lastPos = 0; pos != jsonPath.length(); lastPos = pos + 1) { @@ -105,9 +105,11 @@ class TagsMatcherImpl { return (res == names2tags_.end()) ? 0 : res->second + 1; } - int name2tag(std::string_view n, bool canAdd, bool &updated) { + int name2tag(std::string_view n, bool canAdd, bool& updated) { int tag = name2tag(n); - if (tag || !canAdd) return tag; + if (tag || !canAdd) { + return tag; + } std::string name(n); validateTagSize(tags2names_.size() + 1); @@ -120,10 +122,12 @@ class TagsMatcherImpl { return res.first->second + 1; } - const std::string &tag2name(uint32_t tag) const { + const std::string& tag2name(uint32_t tag) const { static std::string emptystr; tag &= ctag::kNameMask; - if (tag == 0) return emptystr; + if (tag == 0) { + return emptystr; + } if (tag - 1 >= tags2names_.size()) { throw Error(errTagsMissmatch, "Unknown tag %d in cjson", tag); @@ -132,27 +136,33 @@ class TagsMatcherImpl { return tags2names_[tag - 1]; } - int tags2field(const int16_t *path, size_t pathLen) const noexcept { - if (!pathLen) return -1; + int tags2field(const int16_t* path, size_t pathLen) const noexcept { + if (!pathLen) { + return -1; + } return pathCache_.lookup(path, pathLen); } - void buildTagsCache(bool &updated) { - if (!payloadType_) return; + void buildTagsCache(bool& updated) { + if (!payloadType_) { + return; + } pathCache_.clear(); std::vector pathParts; std::vector pathIdx; for (int i = 1; i < payloadType_->NumFields(); i++) { - for (auto &jsonPath : payloadType_->Field(i).JsonPaths()) { - if (!jsonPath.length()) continue; + for (auto& jsonPath : payloadType_->Field(i).JsonPaths()) { + if (!jsonPath.length()) { + continue; + } pathIdx.clear(); - for (auto &name : split(jsonPath, ".", true, pathParts)) { + for (auto& name : split(jsonPath, ".", true, pathParts)) { pathIdx.emplace_back(name2tag(name, true, updated)); } pathCache_.set(pathIdx.data(), pathIdx.size(), i); } } } - void updatePayloadType(PayloadType payloadType, bool &updated, bool incVersion) { + void updatePayloadType(PayloadType payloadType, bool& updated, bool incVersion) { if (!payloadType && !payloadType_) { return; } @@ -162,8 +172,8 @@ class TagsMatcherImpl { newType = newType || bool(payloadType) != bool(payloadType_) || (payloadType_.NumFields() != payloadType.NumFields()); if (!newType) { for (int field = 1, fields = payloadType_.NumFields(); field < fields; ++field) { - auto &lf = payloadType_.Field(field); - auto &rf = payloadType.Field(field); + auto& lf = payloadType_.Field(field); + auto& rf = payloadType.Field(field); if (!lf.Type().IsSame(rf.Type()) || lf.IsArray() != rf.IsArray() || lf.JsonPaths() != rf.JsonPaths()) { newType = true; break; @@ -174,12 +184,14 @@ class TagsMatcherImpl { version_ += int(newType && incVersion); } - void serialize(WrSerializer &ser) const { + void serialize(WrSerializer& ser) const { ser.PutVarUint(tags2names_.size()); - for (size_t tag = 0; tag < tags2names_.size(); ++tag) ser.PutVString(tags2names_[tag]); + for (size_t tag = 0; tag < tags2names_.size(); ++tag) { + ser.PutVString(tags2names_[tag]); + } } - void deserialize(Serializer &ser) { + void deserialize(Serializer& ser) { clear(); size_t cnt = ser.GetVarUint(); validateTagSize(cnt); @@ -191,13 +203,13 @@ class TagsMatcherImpl { } // assert(ser.Eof()); } - void deserialize(Serializer &ser, int version, int stateToken) { + void deserialize(Serializer& ser, int version, int stateToken) { deserialize(ser); version_ = version; stateToken_ = stateToken; } - bool merge(const TagsMatcherImpl &tm, bool &updated) { + bool merge(const TagsMatcherImpl& tm, bool& updated) { if (tm.contains(*this)) { auto oldSz = size(); auto newSz = tm.names2tags_.size(); @@ -222,7 +234,7 @@ class TagsMatcherImpl { } return contains(tm); } - bool contains(const TagsMatcherImpl &tm) const noexcept { + bool contains(const TagsMatcherImpl& tm) const noexcept { return tags2names_.size() >= tm.tags2names_.size() && std::equal(tm.tags2names_.begin(), tm.tags2names_.end(), tags2names_.begin()); } @@ -260,7 +272,9 @@ class TagsMatcherImpl { int16_t path[256]; pathCache_.walk(path, 0, [&path, &res, this](int depth, int field) { for (int i = 0; i < depth; i++) { - if (i) res += "."; + if (i) { + res += "."; + } res += tag2name(path[i]) + "(" + std::to_string(path[i]) + ")"; } res += ":" + payloadType_->Field(field).Name() + "(" + std::to_string(field) + ") "; diff --git a/cpp_src/core/cjson/tagspath.h b/cpp_src/core/cjson/tagspath.h index 390c2d9db..116a423bf 100644 --- a/cpp_src/core/cjson/tagspath.h +++ b/cpp_src/core/cjson/tagspath.h @@ -22,15 +22,21 @@ class IndexedPathNode { IndexedPathNode(AllItemsType) noexcept : index_{ForAllItems} {} IndexedPathNode(int16_t _nameTag) noexcept : nameTag_(_nameTag) {} IndexedPathNode(int16_t _nameTag, int32_t _index) noexcept : nameTag_(_nameTag), index_(_index) {} - bool operator==(const IndexedPathNode &obj) const noexcept { - if (nameTag_ != obj.nameTag_) return false; - if (IsForAllItems() || obj.IsForAllItems()) return true; + bool operator==(const IndexedPathNode& obj) const noexcept { + if (nameTag_ != obj.nameTag_) { + return false; + } + if (IsForAllItems() || obj.IsForAllItems()) { + return true; + } if (index_ != IndexValueType::NotSet && obj.index_ != IndexValueType::NotSet) { - if (index_ != obj.index_) return false; + if (index_ != obj.index_) { + return false; + } } return true; } - bool operator!=(const IndexedPathNode &obj) const noexcept { return !(operator==(obj)); } + bool operator!=(const IndexedPathNode& obj) const noexcept { return !(operator==(obj)); } bool operator==(int16_t _nameTag) const noexcept { return _nameTag == nameTag_; } bool operator!=(int16_t _nameTag) const noexcept { return _nameTag != nameTag_; } explicit operator int() const noexcept { return nameTag_; } @@ -80,48 +86,64 @@ class IndexedTagsPathImpl : public h_vector { public: using Base = h_vector; using Base::Base; - explicit IndexedTagsPathImpl(const TagsPath &tp) { + explicit IndexedTagsPathImpl(const TagsPath& tp) { this->reserve(tp.size()); - for (auto t : tp) this->emplace_back(t); + for (auto t : tp) { + this->emplace_back(t); + } } template - bool Compare(const IndexedTagsPathImpl &obj) const noexcept { + bool Compare(const IndexedTagsPathImpl& obj) const noexcept { const size_t ourSize = this->size(); - if (obj.size() != ourSize) return false; - if (this->back().IsArrayNode() != obj.back().IsArrayNode()) return false; + if (obj.size() != ourSize) { + return false; + } + if (this->back().IsArrayNode() != obj.back().IsArrayNode()) { + return false; + } for (size_t i = 0; i < ourSize; ++i) { - const auto &ourNode = this->operator[](i); + const auto& ourNode = this->operator[](i); if (i == ourSize - 1) { if (ourNode.IsArrayNode()) { - if (ourNode.NameTag() != obj[i].NameTag()) return false; - if (ourNode.IsForAllItems() || obj[i].IsForAllItems()) break; + if (ourNode.NameTag() != obj[i].NameTag()) { + return false; + } + if (ourNode.IsForAllItems() || obj[i].IsForAllItems()) { + break; + } return (ourNode.Index() == obj[i].Index()); } else { return (ourNode.NameTag() == obj[i].NameTag()); } } else { - if (ourNode != obj[i]) return false; + if (ourNode != obj[i]) { + return false; + } } } return true; } - bool Compare(const TagsPath &obj) const noexcept { + bool Compare(const TagsPath& obj) const noexcept { const auto sz = this->size(); if (obj.size() != sz) { return false; } for (size_t i = 0; i < sz; ++i) { - if ((*this)[i].NameTag() != obj[i]) return false; + if ((*this)[i].NameTag() != obj[i]) { + return false; + } } return true; } - bool IsNestedOrEqualTo(const TagsPath &obj) const noexcept { + bool IsNestedOrEqualTo(const TagsPath& obj) const noexcept { const auto sz = this->size(); if (sz > obj.size()) { return false; } for (size_t i = 0; i < sz; ++i) { - if ((*this)[i].NameTag() != obj[i]) return false; + if ((*this)[i].NameTag() != obj[i]) { + return false; + } } return true; } @@ -131,20 +153,26 @@ using IndexedTagsPath = IndexedTagsPathImpl<6>; template class TagsPathScope { public: - TagsPathScope(TagsPath &tagsPath, int16_t tagName) : tagsPath_(tagsPath), tagName_(tagName) { - if (tagName_) tagsPath_.emplace_back(tagName); + TagsPathScope(TagsPath& tagsPath, int16_t tagName) : tagsPath_(tagsPath), tagName_(tagName) { + if (tagName_) { + tagsPath_.emplace_back(tagName); + } } - TagsPathScope(TagsPath &tagsPath, int16_t tagName, int32_t index) : tagsPath_(tagsPath), tagName_(tagName) { - if (tagName_) tagsPath_.emplace_back(tagName, index); + TagsPathScope(TagsPath& tagsPath, int16_t tagName, int32_t index) : tagsPath_(tagsPath), tagName_(tagName) { + if (tagName_) { + tagsPath_.emplace_back(tagName, index); + } } ~TagsPathScope() { - if (tagName_ && !tagsPath_.empty()) tagsPath_.pop_back(); + if (tagName_ && !tagsPath_.empty()) { + tagsPath_.pop_back(); + } } - TagsPathScope(const TagsPathScope &) = delete; - TagsPathScope &operator=(const TagsPathScope &) = delete; + TagsPathScope(const TagsPathScope&) = delete; + TagsPathScope& operator=(const TagsPathScope&) = delete; private: - TagsPath &tagsPath_; + TagsPath& tagsPath_; const int16_t tagName_; }; @@ -154,7 +182,7 @@ namespace std { template <> struct hash { public: - size_t operator()(const reindexer::TagsPath &v) const noexcept { + size_t operator()(const reindexer::TagsPath& v) const noexcept { return reindexer::_Hash_bytes(v.data(), v.size() * sizeof(typename reindexer::TagsPath::value_type)); } }; diff --git a/cpp_src/core/cjson/tagspathcache.h b/cpp_src/core/cjson/tagspathcache.h index dd94eef35..e759f962b 100644 --- a/cpp_src/core/cjson/tagspathcache.h +++ b/cpp_src/core/cjson/tagspathcache.h @@ -10,7 +10,7 @@ namespace reindexer { class TagsPathCache { public: - void set(const int16_t *tagsPath, size_t len, int field) { + void set(const int16_t* tagsPath, size_t len, int field) { assertrx(len); auto cache = this; for (;;) { @@ -30,7 +30,7 @@ class TagsPathCache { len--; } } - int lookup(const int16_t *tagsPath, size_t len) const noexcept { + int lookup(const int16_t* tagsPath, size_t len) const noexcept { assertrx(len); auto cache = this; for (;;) { @@ -50,11 +50,15 @@ class TagsPathCache { } } - void walk(int16_t *path, int depth, const std::function &visitor) const { - int16_t &i = path[depth]; + void walk(int16_t* path, int depth, const std::function& visitor) const { + int16_t& i = path[depth]; for (i = 0; i < int(entries_.size()); i++) { - if (entries_[i].field_ > 0) visitor(depth + 1, entries_[i].field_); - if (entries_[i].subCache_) entries_[i].subCache_->walk(path, depth + 1, visitor); + if (entries_[i].field_ > 0) { + visitor(depth + 1, entries_[i].field_); + } + if (entries_[i].subCache_) { + entries_[i].subCache_->walk(path, depth + 1, visitor); + } } } diff --git a/cpp_src/core/cjson/uuid_recoders.h b/cpp_src/core/cjson/uuid_recoders.h index c74af8e48..cdc264ac3 100644 --- a/cpp_src/core/cjson/uuid_recoders.h +++ b/cpp_src/core/cjson/uuid_recoders.h @@ -17,11 +17,11 @@ class RecoderUuidToString : public Recoder { return TAG_STRING; } } - void Recode(Serializer &, WrSerializer &) const override final; - void Recode(Serializer &, Payload &, int, WrSerializer &) override final { assertrx(false); } + void Recode(Serializer&, WrSerializer&) const override final; + void Recode(Serializer&, Payload&, int, WrSerializer&) override final { assertrx(false); } [[nodiscard]] bool Match(int) noexcept override final { return false; } - [[nodiscard]] bool Match(TagType, const TagsPath &tp) noexcept override final { return tagsPath_ == tp; } - void Serialize(WrSerializer &) override final {} + [[nodiscard]] bool Match(TagType, const TagsPath& tp) noexcept override final { return tagsPath_ == tp; } + void Serialize(WrSerializer&) override final {} bool Reset() override final { return false; } private: @@ -29,12 +29,12 @@ class RecoderUuidToString : public Recoder { }; template <> -inline void RecoderUuidToString::Recode(Serializer &rdser, WrSerializer &wrser) const { +inline void RecoderUuidToString::Recode(Serializer& rdser, WrSerializer& wrser) const { wrser.PutStrUuid(rdser.GetUuid()); } template <> -inline void RecoderUuidToString::Recode(Serializer &rdser, WrSerializer &wrser) const { +inline void RecoderUuidToString::Recode(Serializer& rdser, WrSerializer& wrser) const { const carraytag atag = rdser.GetCArrayTag(); const auto count = atag.Count(); assertrx(atag.Type() == TAG_UUID); @@ -55,9 +55,9 @@ class RecoderStringToUuidArray : public Recoder { return TAG_ARRAY; } [[nodiscard]] bool Match(int f) noexcept override final { return f == field_; } - [[nodiscard]] bool Match(TagType, const TagsPath &) noexcept override final { return false; } - void Recode(Serializer &, WrSerializer &) const override final { assertrx(false); } - void Recode(Serializer &rdser, Payload &pl, int tagName, WrSerializer &wrser) override final { + [[nodiscard]] bool Match(TagType, const TagsPath&) noexcept override final { return false; } + void Recode(Serializer&, WrSerializer&) const override final { assertrx(false); } + void Recode(Serializer& rdser, Payload& pl, int tagName, WrSerializer& wrser) override final { if (fromNotArrayField_) { pl.Set(field_, Variant{rdser.GetStrUuid()}, true); wrser.PutCTag(ctag{TAG_ARRAY, tagName, field_}); @@ -78,7 +78,7 @@ class RecoderStringToUuidArray : public Recoder { wrser.PutVarUint(count); } } - void Serialize(WrSerializer &) override final {} + void Serialize(WrSerializer&) override final {} bool Reset() override final { return false; } private: @@ -99,13 +99,13 @@ class RecoderStringToUuid : public Recoder { return TAG_UUID; } [[nodiscard]] bool Match(int f) noexcept override final { return f == field_; } - [[nodiscard]] bool Match(TagType, const TagsPath &) noexcept override final { return false; } - void Recode(Serializer &, WrSerializer &) const override final { assertrx(false); } - void Recode(Serializer &rdser, Payload &pl, int tagName, WrSerializer &wrser) override final { + [[nodiscard]] bool Match(TagType, const TagsPath&) noexcept override final { return false; } + void Recode(Serializer&, WrSerializer&) const override final { assertrx(false); } + void Recode(Serializer& rdser, Payload& pl, int tagName, WrSerializer& wrser) override final { pl.Set(field_, Variant{rdser.GetStrUuid()}, true); wrser.PutCTag(ctag{TAG_UUID, tagName, field_}); } - void Serialize(WrSerializer &) override final {} + void Serialize(WrSerializer&) override final {} bool Reset() override final { return false; } private: diff --git a/cpp_src/core/dbconfig.cc b/cpp_src/core/dbconfig.cc index 447412b78..38dbdad5d 100644 --- a/cpp_src/core/dbconfig.cc +++ b/cpp_src/core/dbconfig.cc @@ -12,18 +12,24 @@ namespace reindexer { static CacheMode str2cacheMode(std::string_view mode) { using namespace std::string_view_literals; - if (mode == "on"sv) return CacheModeOn; - if (mode == "off"sv || mode == ""sv) return CacheModeOff; - if (mode == "aggressive"sv) return CacheModeAggressive; + if (mode == "on"sv) { + return CacheModeOn; + } + if (mode == "off"sv || mode == ""sv) { + return CacheModeOff; + } + if (mode == "aggressive"sv) { + return CacheModeAggressive; + } throw Error(errParams, "Unknown cache mode %s", mode); } -Error DBConfigProvider::FromJSON(const gason::JsonNode &root) { +Error DBConfigProvider::FromJSON(const gason::JsonNode& root) { try { smart_lock lk(mtx_, true); - auto &profilingNode = root["profiling"]; + auto& profilingNode = root["profiling"]; if (!profilingNode.empty()) { profilingData_ = ProfilingConfigData{}; profilingData_.queriesPerfStats = profilingNode["queriesperfstats"].As(); @@ -45,13 +51,15 @@ Error DBConfigProvider::FromJSON(const gason::JsonNode &root) { transactionNode["avg_step_threshold_us"].As()}); } } - if (handlers_[ProfilingConf]) (handlers_[ProfilingConf])(); + if (handlers_[ProfilingConf]) { + (handlers_[ProfilingConf])(); + } } - auto &namespacesNode = root["namespaces"]; + auto& namespacesNode = root["namespaces"]; if (!namespacesNode.empty()) { namespacesData_.clear(); - for (auto &nsNode : namespacesNode) { + for (auto& nsNode : namespacesNode) { NamespaceConfigData data; data.lazyLoad = nsNode["lazyload"].As(); data.noQueryIdleThreshold = nsNode["unload_idle_threshold"].As(); @@ -96,20 +104,26 @@ Error DBConfigProvider::FromJSON(const gason::JsonNode &root) { namespacesData_.emplace(nsNode["namespace"].As(), std::move(data)); // NOLINT(performance-move-const-arg) } - if (handlers_[NamespaceDataConf]) (handlers_[NamespaceDataConf])(); + if (handlers_[NamespaceDataConf]) { + (handlers_[NamespaceDataConf])(); + } } - auto &replicationNode = root["replication"]; + auto& replicationNode = root["replication"]; if (!replicationNode.empty()) { auto err = replicationData_.FromJSON(replicationNode); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } - if (handlers_[ReplicationConf]) (handlers_[ReplicationConf])(); + if (handlers_[ReplicationConf]) { + (handlers_[ReplicationConf])(); + } } return errOK; - } catch (const Error &err) { + } catch (const Error& err) { return err; - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "DBConfigProvider: %s", ex.what()); } } @@ -124,7 +138,7 @@ ReplicationConfigData DBConfigProvider::GetReplicationConfig() { return replicationData_; } -bool DBConfigProvider::GetNamespaceConfig(std::string_view nsName, NamespaceConfigData &data) { +bool DBConfigProvider::GetNamespaceConfig(std::string_view nsName, NamespaceConfigData& data) { shared_lock lk(mtx_); auto it = namespacesData_.find(nsName); if (it == namespacesData_.end()) { @@ -138,7 +152,7 @@ bool DBConfigProvider::GetNamespaceConfig(std::string_view nsName, NamespaceConf return true; } -Error ReplicationConfigData::FromYML(const std::string &yaml) { +Error ReplicationConfigData::FromYML(const std::string& yaml) { try { YAML::Node root = YAML::Load(yaml); masterDSN = root["master_dsn"].as(masterDSN); @@ -156,18 +170,18 @@ Error ReplicationConfigData::FromYML(const std::string &yaml) { serverId = root["server_id"].as(serverId); auto node = root["namespaces"]; namespaces.clear(); - for (const auto &it : node) { + for (const auto& it : node) { namespaces.insert(it.as()); } return errOK; - } catch (const YAML::Exception &ex) { + } catch (const YAML::Exception& ex) { return Error(errParams, "yaml parsing error: '%s'", ex.what()); - } catch (const Error &err) { + } catch (const Error& err) { return err; } } -Error ReplicationConfigData::FromJSON(const gason::JsonNode &root) { +Error ReplicationConfigData::FromJSON(const gason::JsonNode& root) { try { masterDSN = root["master_dsn"].As(); appName = root["app_name"].As(std::move(appName)); @@ -183,21 +197,27 @@ Error ReplicationConfigData::FromJSON(const gason::JsonNode &root) { enableCompression = root["enable_compression"].As(enableCompression); serverId = root["server_id"].As(serverId); namespaces.clear(); - for (auto &objNode : root["namespaces"]) { + for (auto& objNode : root["namespaces"]) { namespaces.insert(objNode.As()); } - } catch (const Error &err) { + } catch (const Error& err) { return err; - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "ReplicationConfigData: %s", ex.what()); } return errOK; } -ReplicationRole ReplicationConfigData::str2role(const std::string &role) { - if (role == "master") return ReplicationMaster; - if (role == "slave") return ReplicationSlave; - if (role == "none") return ReplicationNone; +ReplicationRole ReplicationConfigData::str2role(const std::string& role) { + if (role == "master") { + return ReplicationMaster; + } + if (role == "slave") { + return ReplicationSlave; + } + if (role == "none") { + return ReplicationNone; + } throw Error(errParams, "Unknown replication role %s", role); } @@ -216,7 +236,7 @@ std::string ReplicationConfigData::role2str(ReplicationRole role) noexcept { } } -void ReplicationConfigData::GetJSON(JsonBuilder &jb) const { +void ReplicationConfigData::GetJSON(JsonBuilder& jb) const { jb.Put("role", role2str(role)); jb.Put("master_dsn", masterDSN); jb.Put("app_name", appName); @@ -230,14 +250,16 @@ void ReplicationConfigData::GetJSON(JsonBuilder &jb) const { jb.Put("server_id", serverId); { auto arrNode = jb.Array("namespaces"); - for (const auto &ns : namespaces) arrNode.Put(nullptr, ns); + for (const auto& ns : namespaces) { + arrNode.Put(nullptr, ns); + } } } -void ReplicationConfigData::GetYAML(WrSerializer &ser) const { +void ReplicationConfigData::GetYAML(WrSerializer& ser) const { YAML::Node nssYaml; nssYaml["namespaces"] = YAML::Node(YAML::NodeType::Sequence); - for (const auto &ns : namespaces) { + for (const auto& ns : namespaces) { nssYaml["namespaces"].push_back(ns); } // clang-format off diff --git a/cpp_src/core/dbconfig.h b/cpp_src/core/dbconfig.h index 643786014..19fb07830 100644 --- a/cpp_src/core/dbconfig.h +++ b/cpp_src/core/dbconfig.h @@ -48,7 +48,7 @@ class LongTxLoggingParams { class ProfilingConfigData { public: - ProfilingConfigData &operator=(const ProfilingConfigData &d) noexcept { + ProfilingConfigData& operator=(const ProfilingConfigData& d) noexcept { queriesThresholdUS.store(d.queriesThresholdUS, std::memory_order_relaxed); queriesPerfStats.store(d.queriesPerfStats, std::memory_order_relaxed); perfStats.store(d.perfStats, std::memory_order_relaxed); @@ -74,14 +74,14 @@ constexpr size_t kDefaultCacheSizeLimit = 1024 * 1024 * 128; constexpr uint32_t kDefaultHitCountToCache = 2; struct NamespaceCacheConfigData { - bool IsIndexesCacheEqual(const NamespaceCacheConfigData &o) noexcept { + bool IsIndexesCacheEqual(const NamespaceCacheConfigData& o) noexcept { return idxIdsetCacheSize == o.idxIdsetCacheSize && idxIdsetHitsToCache == o.idxIdsetHitsToCache && ftIdxCacheSize == o.ftIdxCacheSize && ftIdxHitsToCache == o.ftIdxHitsToCache; } - bool IsJoinCacheEqual(const NamespaceCacheConfigData &o) noexcept { + bool IsJoinCacheEqual(const NamespaceCacheConfigData& o) noexcept { return joinCacheSize == o.joinCacheSize && joinHitsToCache == o.joinHitsToCache; } - bool IsQueryCountCacheEqual(const NamespaceCacheConfigData &o) noexcept { + bool IsQueryCountCacheEqual(const NamespaceCacheConfigData& o) noexcept { return queryCountCacheSize == o.queryCountCacheSize && queryCountHitsToCache == o.queryCountHitsToCache; } @@ -119,10 +119,10 @@ struct NamespaceConfigData { enum ReplicationRole { ReplicationNone, ReplicationMaster, ReplicationSlave, ReplicationReadOnly }; struct ReplicationConfigData { - Error FromYML(const std::string &yml); - Error FromJSON(const gason::JsonNode &v); - void GetJSON(JsonBuilder &jb) const; - void GetYAML(WrSerializer &ser) const; + Error FromYML(const std::string& yml); + Error FromJSON(const gason::JsonNode& v); + void GetJSON(JsonBuilder& jb) const; + void GetYAML(WrSerializer& ser) const; ReplicationRole role = ReplicationNone; std::string masterDSN; @@ -139,7 +139,7 @@ struct ReplicationConfigData { bool enableCompression = true; int serverId = 0; - bool operator==(const ReplicationConfigData &rdata) const noexcept { + bool operator==(const ReplicationConfigData& rdata) const noexcept { return (role == rdata.role) && (connPoolSize == rdata.connPoolSize) && (workerThreads == rdata.workerThreads) && (clusterID == rdata.clusterID) && (forceSyncOnLogicError == rdata.forceSyncOnLogicError) && (forceSyncOnWrongDataHash == rdata.forceSyncOnWrongDataHash) && (masterDSN == rdata.masterDSN) && @@ -147,10 +147,10 @@ struct ReplicationConfigData { (timeoutSec == rdata.timeoutSec) && (namespaces == rdata.namespaces) && (enableCompression == rdata.enableCompression) && (serverId == rdata.serverId) && (appName == rdata.appName); } - bool operator!=(const ReplicationConfigData &rdata) const noexcept { return !operator==(rdata); } + bool operator!=(const ReplicationConfigData& rdata) const noexcept { return !operator==(rdata); } protected: - static ReplicationRole str2role(const std::string &); + static ReplicationRole str2role(const std::string&); static std::string role2str(ReplicationRole) noexcept; }; @@ -158,14 +158,14 @@ class DBConfigProvider { public: DBConfigProvider() = default; ~DBConfigProvider() = default; - DBConfigProvider(DBConfigProvider &obj) = delete; - DBConfigProvider &operator=(DBConfigProvider &obj) = delete; + DBConfigProvider(DBConfigProvider& obj) = delete; + DBConfigProvider& operator=(DBConfigProvider& obj) = delete; - Error FromJSON(const gason::JsonNode &root); + Error FromJSON(const gason::JsonNode& root); void setHandler(ConfigType cfgType, std::function handler); ReplicationConfigData GetReplicationConfig(); - bool GetNamespaceConfig(std::string_view nsName, NamespaceConfigData &data); + bool GetNamespaceConfig(std::string_view nsName, NamespaceConfigData& data); LongQueriesLoggingParams GetSelectLoggingParams() const noexcept { return profilingData_.longSelectLoggingParams.load(std::memory_order_relaxed); } diff --git a/cpp_src/core/expressiontree.h b/cpp_src/core/expressiontree.h index 8f578111c..5e3229a73 100644 --- a/cpp_src/core/expressiontree.h +++ b/cpp_src/core/expressiontree.h @@ -81,7 +81,7 @@ class ExpressionTree { } }; template - struct GetVisitor { + struct GetVisitor { RX_ALWAYS_INLINE const T& operator()(const T& v) const noexcept { return v; } template RX_ALWAYS_INLINE const T& operator()(const U&) const noexcept { @@ -560,20 +560,30 @@ class ExpressionTree { ExpressionTree& operator=(ExpressionTree&&) = default; ExpressionTree(const ExpressionTree& other) : activeBrackets_{other.activeBrackets_} { container_.reserve(other.container_.size()); - for (const Node& n : other.container_) container_.emplace_back(n.Copy()); + for (const Node& n : other.container_) { + container_.emplace_back(n.Copy()); + } } ExpressionTree& operator=(const ExpressionTree& other) { - if rx_unlikely (this == &other) return *this; + if rx_unlikely (this == &other) { + return *this; + } container_.clear(); container_.reserve(other.container_.size()); - for (const Node& n : other.container_) container_.emplace_back(n.Copy()); + for (const Node& n : other.container_) { + container_.emplace_back(n.Copy()); + } activeBrackets_ = other.activeBrackets_; return *this; } RX_ALWAYS_INLINE bool operator==(const ExpressionTree& other) const noexcept { - if (container_.size() != other.container_.size()) return false; + if (container_.size() != other.container_.size()) { + return false; + } for (size_t i = 0; i < container_.size(); ++i) { - if (container_[i] != other.container_[i]) return false; + if (container_[i] != other.container_[i]) { + return false; + } } return true; } @@ -589,10 +599,14 @@ class ExpressionTree { assertrx_dbg(pos < container_.size()); for (unsigned& b : activeBrackets_) { assertrx_dbg(b < container_.size()); - if (b >= pos) ++b; + if (b >= pos) { + ++b; + } } for (size_t i = 0; i < pos; ++i) { - if (container_[i].IsSubTree() && Next(i) > pos) container_[i].Append(); + if (container_[i].IsSubTree() && Next(i) > pos) { + container_[i].Append(); + } } container_.emplace(container_.begin() + pos, op, std::forward(v)); } @@ -605,10 +619,14 @@ class ExpressionTree { assertrx_throw(pos < container_.size()); for (unsigned& b : activeBrackets_) { assertrx_throw(b < container_.size()); - if (b >= pos) ++b; + if (b >= pos) { + ++b; + } } for (size_t i = 0; i < pos; ++i) { - if (container_[i].IsSubTree() && Next(i) > pos) container_[i].Append(); + if (container_[i].IsSubTree() && Next(i) > pos) { + container_[i].Append(); + } } container_.emplace(container_.begin() + pos, op, T(std::forward(args)...)); } @@ -618,10 +636,14 @@ class ExpressionTree { assertrx_dbg(pos < container_.size()); for (unsigned& b : activeBrackets_) { assertrx_dbg(b < container_.size()); - if (b > pos) ++b; + if (b > pos) { + ++b; + } } for (size_t i = 0; i < pos; ++i) { - if (container_[i].IsSubTree() && Next(i) > pos) container_[i].Append(); + if (container_[i].IsSubTree() && Next(i) > pos) { + container_[i].Append(); + } } container_.emplace(container_.begin() + pos + 1, op, std::forward(v)); } @@ -662,12 +684,16 @@ class ExpressionTree { /// Appends value as first child of the root template RX_ALWAYS_INLINE void AppendFront(OperationType op, T&& v) { - for (unsigned& i : activeBrackets_) ++i; + for (unsigned& i : activeBrackets_) { + ++i; + } container_.emplace(container_.begin(), op, std::forward(v)); } template RX_ALWAYS_INLINE void AppendFront(OperationType op, Args&&... args) { - for (unsigned& i : activeBrackets_) ++i; + for (unsigned& i : activeBrackets_) { + ++i; + } container_.emplace(container_.begin(), op, T{std::forward(args)...}); } void PopBack() { @@ -688,7 +714,9 @@ class ExpressionTree { assertrx_dbg(to <= container_.size()); for (unsigned& b : activeBrackets_) { assertrx_dbg(b < container_.size()); - if (b >= from) ++b; + if (b >= from) { + ++b; + } } for (size_t i = 0; i < from; ++i) { if (container_[i].IsSubTree()) { @@ -790,7 +818,9 @@ class ExpressionTree { std::remove_if(activeBrackets_.begin(), activeBrackets_.end(), [from, to](size_t b) { return b >= from && b < to; }), activeBrackets_.end()); for (auto& b : activeBrackets_) { - if (b >= to) b -= count; + if (b >= to) { + b -= count; + } } } template @@ -815,21 +845,29 @@ class ExpressionTree { } template RX_ALWAYS_INLINE void VisitForEach(const Visitor& visitor) const { - for (const Node& node : container_) node.visit(visitor); + for (const Node& node : container_) { + node.visit(visitor); + } } template RX_ALWAYS_INLINE void VisitForEach(const Visitor& visitor) { - for (Node& node : container_) node.visit(visitor); + for (Node& node : container_) { + node.visit(visitor); + } } template RX_ALWAYS_INLINE void VisitForEach(Fs&&... fs) const { overloaded visitor{std::forward(fs)...}; - for (const Node& node : container_) node.visit(visitor); + for (const Node& node : container_) { + node.visit(visitor); + } } template RX_ALWAYS_INLINE void VisitForEach(Fs&&... fs) { overloaded visitor{std::forward(fs)...}; - for (Node& node : container_) node.visit(visitor); + for (Node& node : container_) { + node.visit(visitor); + } } /// @class const_iterator @@ -906,16 +944,22 @@ class ExpressionTree { RX_ALWAYS_INLINE const_iterator cend() const noexcept { return {container_.end()}; } /// @return iterator to first entry of current bracket RX_ALWAYS_INLINE const_iterator begin_of_current_bracket() const noexcept { - if (activeBrackets_.empty()) return container_.cbegin(); + if (activeBrackets_.empty()) { + return container_.cbegin(); + } return container_.cbegin() + activeBrackets_.back() + 1; } RX_ALWAYS_INLINE const SubTree* LastOpenBracket() const { - if (activeBrackets_.empty()) return nullptr; + if (activeBrackets_.empty()) { + return nullptr; + } return &container_[activeBrackets_.back()].template Value(); } RX_ALWAYS_INLINE SubTree* LastOpenBracket() { - if (activeBrackets_.empty()) return nullptr; + if (activeBrackets_.empty()) { + return nullptr; + } return &container_[activeBrackets_.back()].template Value(); } /// @return the last appended leaf or last closed subtree or last openned subtree if it is empty @@ -924,9 +968,13 @@ class ExpressionTree { size_t start = 0; // start of last openned subtree; if (!activeBrackets_.empty()) { start = activeBrackets_.back() + 1; - if (start == container_.size()) return start - 1; // last oppened subtree is empty + if (start == container_.size()) { + return start - 1; // last oppened subtree is empty + } + } + while (Next(start) != container_.size()) { + start = Next(start); } - while (Next(start) != container_.size()) start = Next(start); return start; } diff --git a/cpp_src/core/ft/areaholder.h b/cpp_src/core/ft/areaholder.h index adbd06444..53bac35b1 100644 --- a/cpp_src/core/ft/areaholder.h +++ b/cpp_src/core/ft/areaholder.h @@ -16,10 +16,14 @@ struct Area { [[nodiscard]] bool inline IsIn(int pos) noexcept { return pos <= end && pos >= start; } - [[nodiscard]] bool inline Concat(const Area &rhs) noexcept { + [[nodiscard]] bool inline Concat(const Area& rhs) noexcept { if (IsIn(rhs.start) || IsIn(rhs.end) || (start > rhs.start && end < rhs.end)) { - if (start > rhs.start) start = rhs.start; - if (end < rhs.end) end = rhs.end; + if (start > rhs.start) { + start = rhs.start; + } + if (end < rhs.end) { + end = rhs.end; + } return true; } return false; @@ -39,7 +43,7 @@ class AreaBuffer { void Commit() { if (!data_.empty()) { boost::sort::pdqsort_branchless(data_.begin(), data_.end(), - [](const Area &rhs, const Area &lhs) noexcept { return rhs.start < lhs.start; }); + [](const Area& rhs, const Area& lhs) noexcept { return rhs.start < lhs.start; }); for (auto vit = data_.begin() + 1; vit != data_.end(); ++vit) { auto prev = vit - 1; if (vit->Concat(*prev)) { @@ -48,7 +52,7 @@ class AreaBuffer { } } } - [[nodiscard]] bool Insert(Area &&area, float termRank, int maxAreasInDoc, float maxTermRank) { + [[nodiscard]] bool Insert(Area&& area, float termRank, int maxAreasInDoc, float maxTermRank) { if (!data_.empty() && data_.back().Concat(area)) { return true; } else { @@ -67,8 +71,8 @@ class AreaBuffer { return false; } - [[nodiscard]] const RVector &GetData() const noexcept { return data_; } - void MoveAreas(AreaHolder &to, int field, int32_t rank, int maxAreasInDoc); + [[nodiscard]] const RVector& GetData() const noexcept { return data_; } + void MoveAreas(AreaHolder& to, int field, int32_t rank, int maxAreasInDoc); private: RVector data_; @@ -82,12 +86,12 @@ class AreaHolder { AreaHolder() = default; ~AreaHolder() = default; - AreaHolder(AreaHolder &&) = default; + AreaHolder(AreaHolder&&) = default; void Reserve(int size) { areas_.reserve(size); } void ReserveField(int size) { areas_.resize(size); } void Commit() { commited_ = true; - for (auto &area : areas_) { + for (auto& area : areas_) { area.Commit(); } } @@ -99,23 +103,27 @@ class AreaHolder { maxTermRank_ = rank; } } - [[nodiscard]] AreaBuffer *GetAreas(int field) { - if (!commited_) Commit(); + [[nodiscard]] AreaBuffer* GetAreas(int field) { + if (!commited_) { + Commit(); + } return (areas_.size() <= size_t(field)) ? nullptr : &areas_[field]; } - [[nodiscard]] AreaBuffer *GetAreasRaw(int field) noexcept { return (areas_.size() <= size_t(field)) ? nullptr : &areas_[field]; } + [[nodiscard]] AreaBuffer* GetAreasRaw(int field) noexcept { return (areas_.size() <= size_t(field)) ? nullptr : &areas_[field]; } [[nodiscard]] bool IsCommited() const noexcept { return commited_; } [[nodiscard]] size_t GetAreasCount() const noexcept { size_t size = 0; - for (const auto &aVec : areas_) { + for (const auto& aVec : areas_) { size += aVec.Size(); } return size; } - [[nodiscard]] bool InsertArea(Area &&area, int field, int32_t rank, int maxAreasInDoc) { + [[nodiscard]] bool InsertArea(Area&& area, int field, int32_t rank, int maxAreasInDoc) { commited_ = false; - if (areas_.size() <= size_t(field)) areas_.resize(field + 1); - auto &fieldAreas = areas_[field]; + if (areas_.size() <= size_t(field)) { + areas_.resize(field + 1); + } + auto& fieldAreas = areas_[field]; return fieldAreas.Insert(std::move(area), rank, maxAreasInDoc, maxTermRank_); } @@ -125,8 +133,8 @@ class AreaHolder { int32_t maxTermRank_ = 0; }; -inline void AreaBuffer::MoveAreas(AreaHolder &to, int field, int32_t rank, int maxAreasInDoc) { - for (auto &v : data_) { +inline void AreaBuffer::MoveAreas(AreaHolder& to, int field, int32_t rank, int maxAreasInDoc) { + for (auto& v : data_) { [[maybe_unused]] bool r = to.InsertArea(std::move(v), field, rank, maxAreasInDoc); } to.UpdateRank(rank); diff --git a/cpp_src/core/ft/bm25.h b/cpp_src/core/ft/bm25.h index b20e5521d..3cd0122b9 100644 --- a/cpp_src/core/ft/bm25.h +++ b/cpp_src/core/ft/bm25.h @@ -19,7 +19,9 @@ class Bm25Rx { static RX_ALWAYS_INLINE double IDF(double totalDocCount, double matchedDocCount) noexcept { double f = log((totalDocCount - matchedDocCount + 1) / matchedDocCount) / log(1 + totalDocCount); // saturate min to 0.2 - if (f < 0.2) f = 0.2; + if (f < 0.2) { + f = 0.2; + } return f; } static RX_ALWAYS_INLINE double TF(double termCountInDoc, double wordsInDoc) noexcept { diff --git a/cpp_src/core/ft/config/baseftconfig.cc b/cpp_src/core/ft/config/baseftconfig.cc index 1a1046b91..4a46c1522 100644 --- a/cpp_src/core/ft/config/baseftconfig.cc +++ b/cpp_src/core/ft/config/baseftconfig.cc @@ -7,11 +7,15 @@ namespace reindexer { BaseFTConfig::BaseFTConfig() { - for (const char **p = stop_words_en; *p != nullptr; p++) stopWords.insert({*p, StopWord::Type::Morpheme}); - for (const char **p = stop_words_ru; *p != nullptr; p++) stopWords.insert({*p, StopWord::Type::Morpheme}); + for (const char** p = stop_words_en; *p != nullptr; p++) { + stopWords.insert({*p, StopWord::Type::Morpheme}); + } + for (const char** p = stop_words_ru; *p != nullptr; p++) { + stopWords.insert({*p, StopWord::Type::Morpheme}); + } } -void BaseFTConfig::parseBase(const gason::JsonNode &root) { +void BaseFTConfig::parseBase(const gason::JsonNode& root) { enableTranslit = root["enable_translit"].As<>(enableTranslit); enableNumbersSearch = root["enable_numbers_search"].As<>(enableNumbersSearch); enableKbLayout = root["enable_kb_layout"].As<>(enableKbLayout); @@ -19,10 +23,10 @@ void BaseFTConfig::parseBase(const gason::JsonNode &root) { logLevel = root["log_level"].As<>(logLevel, 0, 5); extraWordSymbols = root["extra_word_symbols"].As<>(extraWordSymbols); - auto &stopWordsNode = root["stop_words"]; + auto& stopWordsNode = root["stop_words"]; if (!stopWordsNode.empty()) { stopWords.clear(); - for (auto &sw : stopWordsNode) { + for (auto& sw : stopWordsNode) { std::string word; StopWord::Type type = StopWord::Type::Stop; if (sw.value.getTag() == gason::JsonTag::JSON_STRING) { @@ -32,7 +36,7 @@ void BaseFTConfig::parseBase(const gason::JsonNode &root) { type = sw["is_morpheme"].As() ? StopWord::Type::Morpheme : StopWord::Type::Stop; } - if (std::find_if(word.begin(), word.end(), [](const auto &symbol) { return std::isspace(symbol); }) != word.end()) { + if (std::find_if(word.begin(), word.end(), [](const auto& symbol) { return std::isspace(symbol); }) != word.end()) { throw Error(errParams, "Stop words can't contain spaces: %s", word); } @@ -43,19 +47,25 @@ void BaseFTConfig::parseBase(const gason::JsonNode &root) { } } - auto &stemmersNode = root["stemmers"]; + auto& stemmersNode = root["stemmers"]; if (!stemmersNode.empty()) { stemmers.clear(); - for (auto &st : stemmersNode) stemmers.emplace_back(st.As()); + for (auto& st : stemmersNode) { + stemmers.emplace_back(st.As()); + } } synonyms.clear(); - for (auto &se : root["synonyms"]) { + for (auto& se : root["synonyms"]) { Synonym synonym; - for (auto &ae : se["alternatives"]) synonym.alternatives.emplace_back(ae.As()); - for (auto &te : se["tokens"]) synonym.tokens.emplace_back(te.As()); + for (auto& ae : se["alternatives"]) { + synonym.alternatives.emplace_back(ae.As()); + } + for (auto& te : se["tokens"]) { + synonym.tokens.emplace_back(te.As()); + } synonyms.emplace_back(std::move(synonym)); } - const auto &baseRankingConfigNode = root["base_ranking"]; + const auto& baseRankingConfigNode = root["base_ranking"]; if (!baseRankingConfigNode.empty()) { rankingConfig.fullMatch = baseRankingConfigNode["full_match_proc"].As<>(rankingConfig.fullMatch, 0, 500); rankingConfig.prefixMin = baseRankingConfigNode["prefix_min_proc"].As<>(rankingConfig.prefixMin, 0, 500); @@ -69,7 +79,7 @@ void BaseFTConfig::parseBase(const gason::JsonNode &root) { } } -void BaseFTConfig::getJson(JsonBuilder &jsonBuilder) const { +void BaseFTConfig::getJson(JsonBuilder& jsonBuilder) const { jsonBuilder.Put("enable_translit", enableTranslit); jsonBuilder.Put("enable_numbers_search", enableNumbersSearch); jsonBuilder.Put("enable_kb_layout", enableKbLayout); @@ -79,21 +89,25 @@ void BaseFTConfig::getJson(JsonBuilder &jsonBuilder) const { jsonBuilder.Array("stemmers", stemmers); { auto synonymsNode = jsonBuilder.Array("synonyms"); - for (const auto &synonym : synonyms) { + for (const auto& synonym : synonyms) { auto synonymObj = synonymsNode.Object(); { auto tokensNode = synonymObj.Array("tokens"); - for (const auto &token : synonym.tokens) tokensNode.Put(nullptr, token); + for (const auto& token : synonym.tokens) { + tokensNode.Put(nullptr, token); + } } { auto alternativesNode = synonymObj.Array("alternatives"); - for (const auto &token : synonym.alternatives) alternativesNode.Put(nullptr, token); + for (const auto& token : synonym.alternatives) { + alternativesNode.Put(nullptr, token); + } } } } { auto stopWordsNode = jsonBuilder.Array("stop_words"); - for (const auto &sw : stopWords) { + for (const auto& sw : stopWords) { auto wordNode = stopWordsNode.Object(nullptr); wordNode.Put("word", sw); wordNode.Put("is_morpheme", sw.type == StopWord::Type::Morpheme); diff --git a/cpp_src/core/ft/config/ftfastconfig.cc b/cpp_src/core/ft/config/ftfastconfig.cc index 46ecb1c11..e7a55de9e 100644 --- a/cpp_src/core/ft/config/ftfastconfig.cc +++ b/cpp_src/core/ft/config/ftfastconfig.cc @@ -12,7 +12,9 @@ namespace { template static bool isAllEqual(const C& c) { for (size_t i = 1, size = c.size(); i < size; ++i) { - if (!(c[0] == c[i])) return false; + if (!(c[0] == c[i])) { + return false; + } } return true; } diff --git a/cpp_src/core/ft/config/ftfuzzyconfig.cc b/cpp_src/core/ft/config/ftfuzzyconfig.cc index 15a2c2fec..29666f551 100644 --- a/cpp_src/core/ft/config/ftfuzzyconfig.cc +++ b/cpp_src/core/ft/config/ftfuzzyconfig.cc @@ -6,8 +6,10 @@ namespace reindexer { -void FtFuzzyConfig::parse(std::string_view json, const RHashMap &) { - if (json.empty()) return; +void FtFuzzyConfig::parse(std::string_view json, const RHashMap&) { + if (json.empty()) { + return; + } try { gason::JsonParser parser; @@ -26,12 +28,12 @@ void FtFuzzyConfig::parse(std::string_view json, const RHashMap &) const { +std::string FtFuzzyConfig::GetJson(const fast_hash_map&) const { WrSerializer wrser; JsonBuilder jsonBuilder(wrser); BaseFTConfig::getJson(jsonBuilder); diff --git a/cpp_src/core/ft/filters/synonyms.cc b/cpp_src/core/ft/filters/synonyms.cc index 64b2afe59..6c5413bb6 100644 --- a/cpp_src/core/ft/filters/synonyms.cc +++ b/cpp_src/core/ft/filters/synonyms.cc @@ -6,7 +6,9 @@ namespace reindexer { void Synonyms::GetVariants(const std::wstring& data, std::vector& result, int proc) { - if (one2one_.empty()) return; + if (one2one_.empty()) { + return; + } auto it = one2one_.find(data); if (it == one2one_.end()) { @@ -54,7 +56,9 @@ static void divOptsForAlternatives(FtDslOpts& opts, size_t size) { assertrx(size != 0); opts.boost /= size; opts.termLenBoost /= size; - for (auto& fOpts : opts.fieldsOpts) fOpts.boost /= size; + for (auto& fOpts : opts.fieldsOpts) { + fOpts.boost /= size; + } opts.qpos /= size; } diff --git a/cpp_src/core/ft/filters/translit.cc b/cpp_src/core/ft/filters/translit.cc index 114db5807..c8a5a0e38 100644 --- a/cpp_src/core/ft/filters/translit.cc +++ b/cpp_src/core/ft/filters/translit.cc @@ -10,7 +10,7 @@ Translit::Translit() { PrepareEnglish(); } -void Translit::GetVariants(const std::wstring &data, std::vector &result, int proc) { +void Translit::GetVariants(const std::wstring& data, std::vector& result, int proc) { std::wstring strings[maxTranslitVariants]; Context ctx; if (data.length()) { @@ -33,7 +33,7 @@ void Translit::GetVariants(const std::wstring &data, std::vector & for (int j = 0; j < maxTranslitVariants; ++j) { auto sym = GetEnglish(symbol, j, ctx); if (sym.second) { - auto &str = strings[j]; + auto& str = strings[j]; if (sym.first) { str.erase(str.end() - sym.first, str.end()); } @@ -51,7 +51,7 @@ void Translit::GetVariants(const std::wstring &data, std::vector & } int64_t lastResultIdx = -1; for (int i = 0; i < maxTranslitVariants; ++i) { - auto ¤t = strings[i]; + auto& current = strings[i]; for (int j = i + 1; j < maxTranslitVariants; ++j) { if (current == strings[j]) { current.clear(); @@ -65,16 +65,20 @@ void Translit::GetVariants(const std::wstring &data, std::vector & } } -std::pair Translit::GetEnglish(wchar_t symbol, size_t variant, Context &ctx) { +std::pair Translit::GetEnglish(wchar_t symbol, size_t variant, Context& ctx) { assertrx(symbol != 0 && symbol >= enLettersStartUTF16 && symbol - enLettersStartUTF16 < enAlfavitSize); if (variant == 1 && ctx.GetCount() > 0) { auto sym = en_d_buf_[ctx.GetLast()][symbol - enLettersStartUTF16]; - if (sym) return {1, sym}; + if (sym) { + return {1, sym}; + } } else if (variant == 2 && ctx.GetCount() > 1) { auto sym = en_t_buf_[ctx.GetPrevios()][ctx.GetLast()][symbol - enLettersStartUTF16]; ctx.Set(symbol - enLettersStartUTF16); - if (sym) return {2, sym}; + if (sym) { + return {2, sym}; + } } if (variant == 2) { diff --git a/cpp_src/core/ft/filters/translit.h b/cpp_src/core/ft/filters/translit.h index 4e05845bb..f9577a042 100644 --- a/cpp_src/core/ft/filters/translit.h +++ b/cpp_src/core/ft/filters/translit.h @@ -8,7 +8,7 @@ class Translit : public ITokenFilter { public: Translit(); - virtual void GetVariants(const std::wstring &data, std::vector &result, int proc) override final; + virtual void GetVariants(const std::wstring& data, std::vector& result, int proc) override final; private: void PrepareRussian(); @@ -29,7 +29,7 @@ class Translit : public ITokenFilter { size_t total_count_; unsigned short num_[2]; }; - std::pair GetEnglish(wchar_t, size_t, Context &ctx); + std::pair GetEnglish(wchar_t, size_t, Context& ctx); bool CheckIsEn(wchar_t symbol); static const int ruLettersStartUTF16 = 1072; diff --git a/cpp_src/core/ft/ft_fast/dataholder.cc b/cpp_src/core/ft/ft_fast/dataholder.cc index 607dd675d..4dc826ce9 100644 --- a/cpp_src/core/ft/ft_fast/dataholder.cc +++ b/cpp_src/core/ft/ft_fast/dataholder.cc @@ -50,7 +50,9 @@ std::string IDataHolder::Dump() { size_t counter = 0; for (auto& step : steps) { ss << "Step : " << std::to_string(counter); - if (!step.suffixes_.word_size()) ss << " - empty step"; + if (!step.suffixes_.word_size()) { + ss << " - empty step"; + } ss << std::endl; for (size_t i = 0; i < step.suffixes_.word_size(); i++) { ss << step.suffixes_.word_at(i) << std::endl; @@ -72,7 +74,9 @@ void IDataHolder::throwStepsOverflow() const { WordIdType IDataHolder::findWord(std::string_view word) { WordIdType id; id.SetEmpty(); - if (steps.size() <= 1) return id; + if (steps.size() <= 1) { + return id; + } for (auto step = steps.begin(); step != steps.end() - 1; ++step) { auto it = step->suffixes_.lower_bound(word); diff --git a/cpp_src/core/ft/ft_fast/dataholder.h b/cpp_src/core/ft/ft_fast/dataholder.h index e6aea4846..5926fc491 100644 --- a/cpp_src/core/ft/ft_fast/dataholder.h +++ b/cpp_src/core/ft/ft_fast/dataholder.h @@ -126,7 +126,9 @@ class IDataHolder { bool NeedRecomitLast() const noexcept { return steps.back().suffixes_.word_size() < size_t(cfg_->maxStepSize); } void SetWordsOffset(uint32_t word_offset) noexcept { assertrx(!steps.empty()); - if (status_ == CreateNew) steps.back().wordOffset_ = word_offset; + if (status_ == CreateNew) { + steps.back().wordOffset_ = word_offset; + } } bool NeedClear(bool complte_updated) const noexcept { return NeedRebuild(complte_updated) || !NeedRecomitLast(); } suffix_map& GetSuffix() noexcept { return steps.back().suffixes_; } diff --git a/cpp_src/core/ft/ft_fast/dataprocessor.cc b/cpp_src/core/ft/ft_fast/dataprocessor.cc index 8fab82b6e..fc0279362 100644 --- a/cpp_src/core/ft/ft_fast/dataprocessor.cc +++ b/cpp_src/core/ft/ft_fast/dataprocessor.cc @@ -22,14 +22,14 @@ void DataProcessor::Process(bool multithread) { const auto tm0 = system_clock_w::now(); size_t szCnt = buildWordsMap(words_um, multithread); const auto tm1 = system_clock_w::now(); - auto &words = holder_.GetWords(); + auto& words = holder_.GetWords(); const size_t wrdOffset = words.size(); holder_.SetWordsOffset(wrdOffset); const auto preprocWords = insertIntoSuffix(words_um, holder_); const auto tm2 = system_clock_w::now(); // Step 4: Commit suffixes array. It runs in parallel with next step - auto &suffixes = holder_.GetSuffix(); + auto& suffixes = holder_.GetSuffix(); auto tm3 = tm2, tm4 = tm2; std::thread sufBuildThread = runInThread(exwr, [&suffixes, &tm3] { suffixes.build(); @@ -71,9 +71,9 @@ void DataProcessor::Process(bool multithread) { } template -typename DataProcessor::WordsVector DataProcessor::insertIntoSuffix(words_map &words_um, DataHolder &holder) { - auto &words = holder.GetWords(); - auto &suffix = holder.GetSuffix(); +typename DataProcessor::WordsVector DataProcessor::insertIntoSuffix(words_map& words_um, DataHolder& holder) { + auto& words = holder.GetWords(); + auto& suffix = holder.GetSuffix(); suffix.reserve(words_um.size() * 20, words_um.size()); const bool enableNumbersSearch = holder.cfg_->enableNumbersSearch; @@ -81,7 +81,7 @@ typename DataProcessor::WordsVector DataProcessor::insertIntoSuf WordsVector found; found.reserve(words_um.size()); - for (auto &keyIt : words_um) { + for (auto& keyIt : words_um) { // if we still haven't whis word we add it to new suffix tree else we will only add info to current word auto id = words.size(); @@ -105,7 +105,7 @@ typename DataProcessor::WordsVector DataProcessor::insertIntoSuf } template -size_t DataProcessor::commitIdRelSets(const WordsVector &preprocWords, words_map &words_um, DataHolder &holder, +size_t DataProcessor::commitIdRelSets(const WordsVector& preprocWords, words_map& words_um, DataHolder& holder, size_t wrdOffset) { size_t idsetcnt = 0; auto wIt = holder.GetWords().begin() + wrdOffset; @@ -113,7 +113,7 @@ size_t DataProcessor::commitIdRelSets(const WordsVector &preprocWords, w auto preprocWordsSize = preprocWords.size(); for (auto keyIt = words_um.begin(), endIt = words_um.end(); keyIt != endIt; ++keyIt, ++i) { // Pack idrelset - PackedWordEntry *word = nullptr; + PackedWordEntry* word = nullptr; if (preprocWordsSize > i) { if (auto widPtr = std::get_if(&preprocWords[i]); widPtr) { assertrx_dbg(!widPtr->IsEmpty()); @@ -153,7 +153,7 @@ static uint32_t getMaxBuildWorkers(bool multithread) noexcept { } template -void makeDocsDistribution(ContextT *ctxs, size_t ctxsCount, size_t docs) { +void makeDocsDistribution(ContextT* ctxs, size_t ctxsCount, size_t docs) { if (ctxsCount == 1) { ctxs[0].from = 0; ctxs[0].to = docs; @@ -188,11 +188,11 @@ void makeDocsDistribution(ContextT *ctxs, size_t ctxsCount, size_t docs) { } template -size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithread) { +size_t DataProcessor::buildWordsMap(words_map& words_um, bool multithread) { ExceptionPtrWrapper exwr; uint32_t maxIndexWorkers = getMaxBuildWorkers(multithread); size_t szCnt = 0; - auto &vdocsTexts = holder_.vdocsTexts; + auto& vdocsTexts = holder_.vdocsTexts; struct context { words_map words_um; std::thread thread; @@ -232,8 +232,8 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea #endif // RX_WITH_STDLIB_DEBUG ThreadsContainer bgThreads; - auto &cfg = holder_.cfg_; - auto &vdocs = holder_.vdocs_; + auto& cfg = holder_.cfg_; + auto& vdocs = holder_.vdocs_; const int fieldscount = fieldSize_; size_t offset = holder_.vdocsOffset_; // build words map parallel in maxIndexWorkers threads @@ -249,11 +249,11 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea const word_hash h; for (VDocIdType j = start; j < fin; ++j) { const size_t vdocId = offset + j; - auto &vdoc = vdocs[vdocId]; + auto& vdoc = vdocs[vdocId]; vdoc.wordsCount.resize(fieldscount, 0.0); vdoc.mostFreqWordCount.resize(fieldscount, 0.0); - auto &vdocsText = vdocsTexts[j]; + auto& vdocsText = vdocsTexts[j]; for (size_t field = 0, sz = vdocsText.size(); field < sz; ++field) { split(vdocsText[field].first, str, wrds, extraWordSymbols); const int rfield = vdocsText[field].second; @@ -265,7 +265,9 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea for (auto word : wrds) { ++insertPos; const auto whash = h(word); - if (!word.length() || cfg->stopWords.find(word, whash) != cfg->stopWords.end()) continue; + if (!word.length() || cfg->stopWords.find(word, whash) != cfg->stopWords.end()) { + continue; + } auto [idxIt, emplaced] = ctx->words_um.try_emplace_prehashed(whash, word); (void)emplaced; @@ -290,13 +292,13 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea words_um = std::move(ctxs[0].words_um); // Merge results into single map for (uint32_t i = 1; i < maxIndexWorkers; ++i) { - auto &ctx = ctxs[i]; + auto& ctx = ctxs[i]; ctx.thread.join(); if (exwr.HasException()) { continue; } - for (auto &it : ctx.words_um) { + for (auto& it : ctx.words_um) { #if defined(RX_WITH_STDLIB_DEBUG) || defined(REINDEX_WITH_ASAN) const auto fBeforeMove = it.first; const auto sBeforeMove = it.second.MakeCopy(); @@ -311,8 +313,8 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea assertrx(it.second.vids.size() == sBeforeMove.vids.size()); assertrx(it.second.vids.capacity() == sCapacityBeforeMove); #endif // defined(RX_WITH_STDLIB_DEBUG) || defined(REINDEX_WITH_ASAN) - auto &resultVids = idxIt->second.vids; - auto &newVids = it.second.vids; + auto& resultVids = idxIt->second.vids; + auto& newVids = it.second.vids; resultVids.insert(resultVids.end(), std::make_move_iterator(newVids.begin()), std::make_move_iterator(newVids.end())); } } @@ -334,15 +336,17 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea if (vdocs.size()) { holder_.avgWordsCount_.resize(fieldscount, 0); for (int i = 0; i < fieldscount; i++) { - auto &avgRef = holder_.avgWordsCount_[i]; - for (auto &vdoc : vdocs) avgRef += vdoc.wordsCount[i]; + auto& avgRef = holder_.avgWordsCount_[i]; + for (auto& vdoc : vdocs) { + avgRef += vdoc.wordsCount[i]; + } avgRef /= vdocs.size(); } } // Check and print potential stop words if (holder_.cfg_->logLevel >= LogInfo) { WrSerializer out; - for (auto &w : words_um) { + for (auto& w : words_um) { if (w.second.vids.size() > vdocs.size() / 5 || int64_t(w.second.vids.size()) > holder_.cfg_->mergeLimit) { out << w.first << "(" << w.second.vids.size() << ") "; } @@ -354,11 +358,11 @@ size_t DataProcessor::buildWordsMap(words_map &words_um, bool multithrea } template -void DataProcessor::buildVirtualWord(std::string_view word, words_map &words_um, VDocIdType docType, int rfield, size_t insertPos, - std::vector &container) { - auto &vdoc(holder_.vdocs_[docType]); +void DataProcessor::buildVirtualWord(std::string_view word, words_map& words_um, VDocIdType docType, int rfield, size_t insertPos, + std::vector& container) { + auto& vdoc(holder_.vdocs_[docType]); NumToText::convert(word, container); - for (std::string &numberWord : container) { + for (std::string& numberWord : container) { WordEntry wentry; wentry.virtualWord = true; auto idxIt = words_um.emplace(std::move(numberWord), std::move(wentry)).first; @@ -372,7 +376,7 @@ void DataProcessor::buildVirtualWord(std::string_view word, words_map &w } template -void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector &preprocWords) { +void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector& preprocWords) { if (!holder_.cfg_->maxTypos) { return; } @@ -380,8 +384,8 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & return; } - auto &typosHalf = holder_.GetTyposHalf(); - auto &typosMax = holder_.GetTyposMax(); + auto& typosHalf = holder_.GetTyposHalf(); + auto& typosMax = holder_.GetTyposMax(); const auto wordsSize = preprocWords.size(); const auto maxTypoLen = holder_.cfg_->maxTypoLen; const auto maxTyposInWord = holder_.cfg_->MaxTyposInWord(); @@ -393,14 +397,14 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & typosHalf.reserve(multiplicator / 2, multiplicator * 5); auto wordPos = startPos; - for (auto &word : preprocWords) { + for (auto& word : preprocWords) { const auto wordString = std::get_if(&word); if (!wordString) { continue; } const auto wordId = holder_.BuildWordId(wordPos++); mktypos(tctx, *wordString, maxTyposInWord, maxTypoLen, - typos_context::CallBack{[&typosHalf, wordId](std::string_view typo, int, const typos_context::TyposVec &positions) { + typos_context::CallBack{[&typosHalf, wordId](std::string_view typo, int, const typos_context::TyposVec& positions) { typosHalf.emplace(typo, WordTypo{wordId, positions}); }}); } @@ -416,7 +420,7 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & auto wordPos = startPos; mult = wordsSize * (10 << (maxTyposInWord - 1)) - mult; typosMax.reserve(multiplicator / 2, multiplicator * 5); - for (auto &word : preprocWords) { + for (auto& word : preprocWords) { const auto wordString = std::get_if(&word); if (!wordString) { continue; @@ -424,7 +428,7 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & const auto wordId = holder_.BuildWordId(wordPos++); mktypos(tctx, *wordString, maxTyposInWord, maxTypoLen, typos_context::CallBack{[wordId, &typosMax, wordString](std::string_view typo, int level, - const typos_context::TyposVec &positions) { + const typos_context::TyposVec& positions) { if (level <= 1 && typo.size() != wordString->size()) { typosMax.emplace(typo, WordTypo{wordId, positions}); } @@ -438,7 +442,7 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & auto wordPos = startPos; typos_context tctx[kMaxTyposInWord]; typosHalf.reserve(multiplicator / 2, multiplicator * 5); - for (auto &word : preprocWords) { + for (auto& word : preprocWords) { const auto wordString = std::get_if(&word); if (!wordString) { continue; @@ -446,7 +450,7 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & const auto wordId = holder_.BuildWordId(wordPos++); mktypos(tctx, *wordString, maxTyposInWord, maxTypoLen, typos_context::CallBack{ - [wordId, &typosHalf, wordString](std::string_view typo, int level, const typos_context::TyposVec &positions) { + [wordId, &typosHalf, wordString](std::string_view typo, int level, const typos_context::TyposVec& positions) { if (level > 1 || typo.size() == wordString->size()) { typosHalf.emplace(typo, WordTypo{wordId, positions}); } @@ -463,9 +467,9 @@ void DataProcessor::buildTyposMap(uint32_t startPos, const WordsVector & template template -std::thread DataProcessor::runInThread(ExceptionPtrWrapper &ew, F &&f, Args &&...args) noexcept { +std::thread DataProcessor::runInThread(ExceptionPtrWrapper& ew, F&& f, Args&&... args) noexcept { return std::thread( - [fw = std::forward(f), &ew](auto &&...largs) noexcept { + [fw = std::forward(f), &ew](auto&&... largs) noexcept { try { fw(largs...); } catch (...) { diff --git a/cpp_src/core/ft/ft_fast/selecter.cc b/cpp_src/core/ft/ft_fast/selecter.cc index 56ba3787e..c02d82ed7 100644 --- a/cpp_src/core/ft/ft_fast/selecter.cc +++ b/cpp_src/core/ft/ft_fast/selecter.cc @@ -9,11 +9,21 @@ namespace { RX_ALWAYS_INLINE double pos2rank(int pos) { - if (pos <= 10) return 1.0 - (pos / 100.0); - if (pos <= 100) return 0.9 - (pos / 1000.0); - if (pos <= 1000) return 0.8 - (pos / 10000.0); - if (pos <= 10000) return 0.7 - (pos / 100000.0); - if (pos <= 100000) return 0.6 - (pos / 1000000.0); + if (pos <= 10) { + return 1.0 - (pos / 100.0); + } + if (pos <= 100) { + return 0.9 - (pos / 1000.0); + } + if (pos <= 1000) { + return 0.8 - (pos / 10000.0); + } + if (pos <= 10000) { + return 0.7 - (pos / 100000.0); + } + if (pos <= 100000) { + return 0.6 - (pos / 1000000.0); + } return 0.5; } } // namespace @@ -85,7 +95,9 @@ void Selecter::prepareVariants(std::vector& variants, RV FtDslOpts opts = term.opts; opts.pref = true; - if (&v != &variantsUtf16[0]) opts.suff = false; + if (&v != &variantsUtf16[0]) { + opts.suff = false; + } const auto charCount = getUTF8StringCharactersCount(stemstr); if (charCount >= kMinStemRellevantLen || !lowRelVariants) { @@ -115,7 +127,9 @@ RX_NO_INLINE MergeData Selecter::Process(FtDSLQuery&& dsl, bool inTransa // STEP 2: Search dsl terms for each variant std::vector synonymsDsl; holder_.synonyms_->PreProcess(dsl, synonymsDsl, holder_.cfg_->rankingConfig.synonyms); - if (!inTransaction) ThrowOnCancel(rdxCtx); + if (!inTransaction) { + ThrowOnCancel(rdxCtx); + } for (size_t i = 0; i < dsl.size(); ++i) { const auto irrVariantsCount = ctx.lowRelVariants.size(); @@ -149,11 +163,15 @@ RX_NO_INLINE MergeData Selecter::Process(FtDSLQuery&& dsl, bool inTransa std::vector results; size_t reserveSize = ctx.rawResults.size(); - for (const SynonymsDsl& synDsl : synonymsDsl) reserveSize += synDsl.dsl.size(); + for (const SynonymsDsl& synDsl : synonymsDsl) { + reserveSize += synDsl.dsl.size(); + } results.reserve(reserveSize); std::vector synonymsBounds; synonymsBounds.reserve(synonymsDsl.size()); - if (!inTransaction) ThrowOnCancel(rdxCtx); + if (!inTransaction) { + ThrowOnCancel(rdxCtx); + } for (SynonymsDsl& synDsl : synonymsDsl) { FtSelectContext synCtx; synCtx.rawResults.reserve(synDsl.dsl.size()); @@ -162,7 +180,9 @@ RX_NO_INLINE MergeData Selecter::Process(FtDSLQuery&& dsl, bool inTransa if rx_unlikely (holder_.cfg_->logLevel >= LogInfo) { WrSerializer wrSer; for (auto& variant : synCtx.variants) { - if (&variant != &*synCtx.variants.begin()) wrSer << ", "; + if (&variant != &*synCtx.variants.begin()) { + wrSer << ", "; + } wrSer << variant.pattern; } logPrintf(LogInfo, "Multiword synonyms variants: [%s]", wrSer.Slice()); @@ -245,7 +265,9 @@ void Selecter::processStepVariants(FtSelectContext& ctx, typename DataHo // Walk current variant in suffixes array and fill results do { - if (keyIt == suffixes.end()) break; + if (keyIt == suffixes.end()) { + break; + } if (vidsLimit <= vids) { if rx_unlikely (holder_.cfg_->logLevel >= LogInfo) { logPrintf(LogInfo, "Terminating suffix loop on limit (%d). Current variant is '%s%s%s'", initialLimit, @@ -279,8 +301,12 @@ void Selecter::processStepVariants(FtSelectContext& ctx, typename DataHo const ptrdiff_t suffixLen = keyIt->first - word; const int matchLen = tmpstr.length(); - if (!withSuffixes && suffixLen) continue; - if (!withPrefixes && wordLength != matchLen + suffixLen) break; + if (!withSuffixes && suffixLen) { + continue; + } + if (!withPrefixes && wordLength != matchLen + suffixLen) { + break; + } const int matchDif = std::abs(long(wordLength - matchLen + suffixLen)); const int proc = std::max(variant.proc - holder_.cfg_->partialMatchDecrease * matchDif / std::max(matchLen, 3), @@ -302,8 +328,9 @@ void Selecter::processStepVariants(FtSelectContext& ctx, typename DataHo ++matched; vids += vidsSize; } else { - if (ctx.rawResults[it->second.first][it->second.second].proc < proc) + if (ctx.rawResults[it->second.first][it->second.second].proc < proc) { ctx.rawResults[it->second.first][it->second.second].proc = proc; + } ++skipped; } } while ((keyIt++).lcp() >= int(tmpstr.length())); @@ -404,7 +431,9 @@ template RX_ALWAYS_INLINE void Selecter::debugMergeStep(const char* msg, int vid, float normBm25, float normDist, int finalRank, int prevRank) { #ifdef REINDEX_FT_EXTRA_DEBUG - if (holder_.cfg_->logLevel < LogTrace) return; + if (holder_.cfg_->logLevel < LogTrace) { + return; + } logPrintf(LogInfo, "%s - '%s' (vid %d), bm25 %f, dist %f, rank %d (prev rank %d)", msg, holder_.vdocs_[vid].keyDoc, vid, normBm25, normDist, finalRank, prevRank); @@ -633,7 +662,7 @@ void Selecter::addNewTerm(FtMergeStatuses::Statuses& mergeStatuses, Merg curExists[vid] = true; idoffsets[vid] = merged.size() - 1; } -}; +} template void Selecter::addAreas(MergeData& merged, int32_t areaIndex, const IdRelType& relid, int32_t termRank) { @@ -646,7 +675,7 @@ void Selecter::addAreas(MergeData& merged, int32_t areaIndex, const IdRe } area.UpdateRank(termRank); } -}; +} // idf=max(0.2, log((N-M+1)/M)/log(1+N)) // N - document count @@ -691,7 +720,9 @@ void Selecter::mergeIteration(TextSearchResults& rawRes, index_t rawResI // loop on subterm (word, translit, stemmmer,...) for (auto& r : rawRes) { - if (!inTransaction) ThrowOnCancel(rdxCtx); + if (!inTransaction) { + ThrowOnCancel(rdxCtx); + } Bm25Calculator bm25{double(totalDocsCount), double(r.vids->size()), holder_.cfg_->bm25Config.bm25k1, holder_.cfg_->bm25Config.bm25b}; static_assert(sizeof(bm25) <= 32, "Bm25Calculator size is greater than 32 bytes"); @@ -800,7 +831,9 @@ std::pair Selecter::calcTermRank(const TextSearchResults& r } } - if (!termRank) return std::make_pair(termRank, field); + if (!termRank) { + return std::make_pair(termRank, field); + } if (holder_.cfg_->summationRanksByFieldsRatio > 0) { boost::sort::pdqsort_branchless(ranksInFields.begin(), ranksInFields.end()); @@ -828,7 +861,9 @@ void Selecter::mergeIterationGroup(TextSearchResults& rawRes, index_t ra // loop on subterm (word, translit, stemmmer,...) for (auto& r : rawRes) { - if (!inTransaction) ThrowOnCancel(rdxCtx); + if (!inTransaction) { + ThrowOnCancel(rdxCtx); + } Bm25Calculator bm25(totalDocsCount, r.vids->size(), holder_.cfg_->bm25Config.bm25k1, holder_.cfg_->bm25Config.bm25b); static_assert(sizeof(bm25) <= 32, "Bm25Calculator size is greater than 32 bytes"); int vid = -1; @@ -846,11 +881,15 @@ void Selecter::mergeIterationGroup(TextSearchResults& rawRes, index_t ra continue; } // keyEntry can be assigned nullptr when removed - if (!vdocs[vid].keyEntry) continue; + if (!vdocs[vid].keyEntry) { + continue; + } // Find field with max rank auto [termRank, field] = calcTermRank(rawRes, bm25, relid, r.proc); - if (!termRank) continue; + if (!termRank) { + continue; + } if rx_unlikely (holder_.cfg_->logLevel >= LogTrace) { logPrintf(LogInfo, "Pattern %s, idf %f, termLenBoost %f", r.pattern, bm25.GetIDF(), rawRes.term.opts.termLenBoost); @@ -1065,7 +1104,9 @@ size_t Selecter::TyposHandler::Process(std::vector& r ++skiped; } } - if (dontUseMaxTyposForBoth_ && level == 1 && typo.size() != patternSize) return; + if (dontUseMaxTyposForBoth_ && level == 1 && typo.size() != patternSize) { + return; + } } }); if rx_unlikely (holder.cfg_->logLevel >= LogInfo) { @@ -1248,7 +1289,9 @@ MergeData Selecter::mergeResults(std::vector&& rawRes const auto& vdocs = holder_.vdocs_; MergeData merged; - if (!rawResults.size() || !vdocs.size()) return merged; + if (!rawResults.size() || !vdocs.size()) { + return merged; + } assertrx_throw(FtMergeStatuses::kExcluded > rawResults.size()); assertrx_throw(mergeStatuses.size() == vdocs.size()); @@ -1323,7 +1366,9 @@ MergeData Selecter::mergeResults(std::vector&& rawRes break; } } - if (matchSyn) continue; + if (matchSyn) { + continue; + } info.proc = 0; mergeStatuses[vid] = 0; } @@ -1354,12 +1399,16 @@ void Selecter::printVariants(const FtSelectContext& ctx, const TextSearc WrSerializer wrSer; wrSer << "variants: ["; for (auto& variant : ctx.variants) { - if (&variant != &*ctx.variants.begin()) wrSer << ", "; + if (&variant != &*ctx.variants.begin()) { + wrSer << ", "; + } wrSer << variant.pattern; } wrSer << "], variants_with_low_relevancy: ["; for (auto& variant : ctx.lowRelVariants) { - if (&variant != &*ctx.lowRelVariants.begin()) wrSer << ", "; + if (&variant != &*ctx.lowRelVariants.begin()) { + wrSer << ", "; + } wrSer << variant.pattern; } wrSer << "], typos: ["; diff --git a/cpp_src/core/ft/ft_fuzzy/advacedpackedvec.h b/cpp_src/core/ft/ft_fuzzy/advacedpackedvec.h index 696fd78af..5ff49b949 100644 --- a/cpp_src/core/ft/ft_fuzzy/advacedpackedvec.h +++ b/cpp_src/core/ft/ft_fuzzy/advacedpackedvec.h @@ -7,7 +7,7 @@ class IdRelType; class AdvacedPackedVec : public packed_vector { public: - AdvacedPackedVec(IdRelSet &&data); + AdvacedPackedVec(IdRelSet&& data); int max_id_; int min_id_; diff --git a/cpp_src/core/ft/ft_fuzzy/baseseacher.cc b/cpp_src/core/ft/ft_fuzzy/baseseacher.cc index 169367607..9b1789dd4 100644 --- a/cpp_src/core/ft/ft_fuzzy/baseseacher.cc +++ b/cpp_src/core/ft/ft_fuzzy/baseseacher.cc @@ -10,9 +10,9 @@ namespace search_engine { using namespace reindexer; -void BaseSearcher::AddSeacher(ITokenFilter::Ptr &&seacher) { searchers_.push_back(std::move(seacher)); } +void BaseSearcher::AddSeacher(ITokenFilter::Ptr&& seacher) { searchers_.push_back(std::move(seacher)); } -std::pair BaseSearcher::GetData(const BaseHolder::Ptr &holder, unsigned int i, wchar_t *buf, const wchar_t *src_data, +std::pair BaseSearcher::GetData(const BaseHolder::Ptr& holder, unsigned int i, wchar_t* buf, const wchar_t* src_data, size_t data_size) { size_t counter = 0; size_t final_counter = 0; @@ -43,8 +43,8 @@ std::pair BaseSearcher::GetData(const BaseHolder::Ptr &holder, uns return std::make_pair(cont, counter + final_counter); } -size_t BaseSearcher::ParseData(const BaseHolder::Ptr &holder, const std::wstring &src_data, int &max_id, int &min_id, - std::vector &rusults, const FtDslOpts &opts, double proc) { +size_t BaseSearcher::ParseData(const BaseHolder::Ptr& holder, const std::wstring& src_data, int& max_id, int& min_id, + std::vector& rusults, const FtDslOpts& opts, double proc) { wchar_t res_buf[maxFuzzyFTBufferSize]; size_t total_size = 0; size_t size = src_data.size(); @@ -56,8 +56,12 @@ size_t BaseSearcher::ParseData(const BaseHolder::Ptr &holder, const std::wstring auto it = holder->GetData(res_buf); if (it != holder->end()) { - if (it->second.max_id_ > max_id) max_id = it->second.max_id_; - if (it->second.min_id_ < min_id) min_id = it->second.min_id_; + if (it->second.max_id_ > max_id) { + max_id = it->second.max_id_; + } + if (it->second.min_id_ < min_id) { + min_id = it->second.min_id_; + } double final_proc = double(holder->cfg_.bufferSize * holder->cfg_.startDecreeseBoost - cont.second) / double(holder->cfg_.bufferSize * holder->cfg_.startDecreeseBoost); rusults.push_back(FirstResult{&it->second, &opts, static_cast(i), proc * final_proc}); @@ -67,8 +71,8 @@ size_t BaseSearcher::ParseData(const BaseHolder::Ptr &holder, const std::wstring return total_size; } -SearchResult BaseSearcher::Compare(const BaseHolder::Ptr &holder, const FtDSLQuery &dsl, bool inTransaction, - const reindexer::RdxContext &rdxCtx) { +SearchResult BaseSearcher::Compare(const BaseHolder::Ptr& holder, const FtDSLQuery& dsl, bool inTransaction, + const reindexer::RdxContext& rdxCtx) { size_t data_size = 0; std::vector data; @@ -79,8 +83,10 @@ SearchResult BaseSearcher::Compare(const BaseHolder::Ptr &holder, const FtDSLQue int max_id = 0; int min_id = INT32_MAX; - if (!inTransaction) ThrowOnCancel(rdxCtx); - for (auto &term : dsl) { + if (!inTransaction) { + ThrowOnCancel(rdxCtx); + } + for (auto& term : dsl) { data_size += ParseData(holder, term.pattern, max_id, min_id, results, term.opts, 1); if (holder->cfg_.enableTranslit) { @@ -117,12 +123,14 @@ SearchResult BaseSearcher::Compare(const BaseHolder::Ptr &holder, const FtDSLQue return res; } -void BaseSearcher::AddIndex(BaseHolder::Ptr &holder, std::string_view src_data, const IdType id, int field, - const std::string &extraWordSymbols) { +void BaseSearcher::AddIndex(BaseHolder::Ptr& holder, std::string_view src_data, const IdType id, int field, + const std::string& extraWordSymbols) { #ifdef FULL_LOG_FT words.push_back(std::make_pair(id, *src_data)); #endif - if (!src_data.length()) return; + if (!src_data.length()) { + return; + } std::pair pos; pos.first = 0; std::wstring utf16str; @@ -130,7 +138,7 @@ void BaseSearcher::AddIndex(BaseHolder::Ptr &holder, std::string_view src_data, split(src_data, utf16str, wrds, extraWordSymbols); wchar_t res_buf[maxFuzzyFTBufferSize]; size_t total_size = 0; - for (auto &term : wrds) { + for (auto& term : wrds) { unsigned int i = 0; std::pair cont; do { @@ -144,5 +152,5 @@ void BaseSearcher::AddIndex(BaseHolder::Ptr &holder, std::string_view src_data, holder->SetSize(total_size, id, field); } -void BaseSearcher::Commit(BaseHolder::Ptr &holder) { holder->Commit(); } +void BaseSearcher::Commit(BaseHolder::Ptr& holder) { holder->Commit(); } } // namespace search_engine diff --git a/cpp_src/core/ft/ft_fuzzy/baseseacher.h b/cpp_src/core/ft/ft_fuzzy/baseseacher.h index 6772a995a..eec4bc078 100644 --- a/cpp_src/core/ft/ft_fuzzy/baseseacher.h +++ b/cpp_src/core/ft/ft_fuzzy/baseseacher.h @@ -16,25 +16,24 @@ namespace search_engine { class BaseSearcher { public: - void AddSeacher(ITokenFilter::Ptr &&seacher); - void AddIndex(BaseHolder::Ptr &holder, std::string_view src_data, const IdType id, int field, const std::string &extraWordSymbols); - SearchResult Compare(const BaseHolder::Ptr &holder, const reindexer::FtDSLQuery &dsl, bool inTransaction, - const reindexer::RdxContext &); + void AddSeacher(ITokenFilter::Ptr&& seacher); + void AddIndex(BaseHolder::Ptr& holder, std::string_view src_data, const IdType id, int field, const std::string& extraWordSymbols); + SearchResult Compare(const BaseHolder::Ptr& holder, const reindexer::FtDSLQuery& dsl, bool inTransaction, const reindexer::RdxContext&); - void Commit(BaseHolder::Ptr &holder); + void Commit(BaseHolder::Ptr& holder); private: #ifdef FULL_LOG_FT std::vector> words; #endif - std::pair GetData(const BaseHolder::Ptr &holder, unsigned int i, wchar_t *buf, const wchar_t *src_data, size_t data_size); + std::pair GetData(const BaseHolder::Ptr& holder, unsigned int i, wchar_t* buf, const wchar_t* src_data, size_t data_size); - size_t ParseData(const BaseHolder::Ptr &holder, const std::wstring &src_data, int &max_id, int &min_id, - std::vector &rusults, const FtDslOpts &opts, double proc); + size_t ParseData(const BaseHolder::Ptr& holder, const std::wstring& src_data, int& max_id, int& min_id, + std::vector& rusults, const FtDslOpts& opts, double proc); - void AddIdToInfo(Info *info, const IdType id, std::pair pos, uint32_t total_size); - uint32_t FindHash(const std::wstring &data); + void AddIdToInfo(Info* info, const IdType id, std::pair pos, uint32_t total_size); + uint32_t FindHash(const std::wstring& data); std::vector> searchers_; }; } // namespace search_engine diff --git a/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.cc b/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.cc index d218341de..936f81c98 100644 --- a/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.cc +++ b/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.cc @@ -2,7 +2,7 @@ namespace search_engine { -DIt BaseHolder::GetData(const wchar_t *key) { +DIt BaseHolder::GetData(const wchar_t* key) { #ifndef DEBUG_FT return data_.find(std::wstring(key, cfg_.bufferSize)); #else @@ -10,7 +10,7 @@ DIt BaseHolder::GetData(const wchar_t *key) { #endif } void BaseHolder::SetSize(uint32_t size, VDocIdType id, int field) { words_[id][field] += size; } -void BaseHolder::AddDada(const wchar_t *key, VDocIdType id, int pos, int field) { +void BaseHolder::AddDada(const wchar_t* key, VDocIdType id, int pos, int field) { #ifndef DEBUG_FT std::wstring wkey(key, cfg_.bufferSize); auto it = tmp_data_.find(wkey); @@ -35,7 +35,7 @@ void BaseHolder::AddDada(const wchar_t *key, VDocIdType id, int pos, int field) void BaseHolder::Commit() { data_.reserve(tmp_data_.size()); data_.clear(); - for (auto &val : tmp_data_) { + for (auto& val : tmp_data_) { data_.insert(std::make_pair(val.first, AdvacedPackedVec(std::move(val.second)))); } diff --git a/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.h b/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.h index 5f04f4719..febe60736 100644 --- a/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.h +++ b/cpp_src/core/ft/ft_fuzzy/dataholder/basebuildedholder.h @@ -18,13 +18,13 @@ using namespace reindexer; #ifndef DEBUG_FT struct DataStructHash { - inline size_t operator()(const std::wstring &ent) const noexcept { return Hash(ent); } + inline size_t operator()(const std::wstring& ent) const noexcept { return Hash(ent); } }; struct DataStructEQ { - inline bool operator()(const std::wstring &ent, const std::wstring &ent1) const noexcept { return ent == ent1; } + inline bool operator()(const std::wstring& ent, const std::wstring& ent1) const noexcept { return ent == ent1; } }; struct DataStructLess { - inline bool operator()(const std::wstring &ent, const std::wstring &ent1) const noexcept { return ent < ent1; } + inline bool operator()(const std::wstring& ent, const std::wstring& ent1) const noexcept { return ent < ent1; } }; template using data_map = tsl::hopscotch_map; @@ -47,10 +47,10 @@ class BaseHolder { typedef shared_ptr Ptr; BaseHolder() {} - BaseHolder(BaseHolder &rhs) = delete; - BaseHolder(BaseHolder &&) noexcept = delete; - BaseHolder &operator=(const BaseHolder &) = delete; - BaseHolder &operator=(BaseHolder &&) noexcept = delete; + BaseHolder(BaseHolder& rhs) = delete; + BaseHolder(BaseHolder&&) noexcept = delete; + BaseHolder& operator=(const BaseHolder&) = delete; + BaseHolder& operator=(BaseHolder&&) noexcept = delete; void ClearTemp() { data_map tmp_data; @@ -62,10 +62,10 @@ class BaseHolder { ClearTemp(); data_.clear(); } - void SetConfig(const std::unique_ptr &cfg) { cfg_ = *cfg.get(); } - DIt GetData(const wchar_t *key); + void SetConfig(const std::unique_ptr& cfg) { cfg_ = *cfg.get(); } + DIt GetData(const wchar_t* key); void SetSize(uint32_t size, VDocIdType id, int filed); - void AddDada(const wchar_t *key, VDocIdType id, int pos, int field); + void AddDada(const wchar_t* key, VDocIdType id, int pos, int field); void Commit(); public: diff --git a/cpp_src/core/ft/ft_fuzzy/dataholder/smardeque.cc b/cpp_src/core/ft/ft_fuzzy/dataholder/smardeque.cc index d327a5e34..b309cf038 100644 --- a/cpp_src/core/ft/ft_fuzzy/dataholder/smardeque.cc +++ b/cpp_src/core/ft/ft_fuzzy/dataholder/smardeque.cc @@ -39,8 +39,12 @@ template typename SmartDeque::pointer SmartDeque::operator[](size_t num) const { size_t size = (num / block_size); size_t offset = (num % block_size); - if (!data_ || size >= size_ || !data_[size]) return nullptr; - if (data_[size][offset].proc_ == 0) return nullptr; + if (!data_ || size >= size_ || !data_[size]) { + return nullptr; + } + if (data_[size][offset].proc_ == 0) { + return nullptr; + } return &data_[size][offset]; } template @@ -163,7 +167,9 @@ SmartDeque& SmartDeque::operator=(SmartDeque&& rhs template SmartDeque::~SmartDeque() { - if (!data_) return; + if (!data_) { + return; + } for (size_t i = 0; i < size_; ++i) { delete[] data_[i]; } @@ -172,14 +178,18 @@ SmartDeque::~SmartDeque() { template void SmartDeque::allocSection(size_t num) { - if (!data_) return; + if (!data_) { + return; + } data_[num] = new T[block_size]; memset(data_[num], 0, block_size * sizeof(T)); } template typename SmartDeque::iterator& SmartDeque::iterator::operator++() { - if (parent_ == nullptr) return *this; + if (parent_ == nullptr) { + return *this; + } for (; size_ < parent_->size_; ++size_, offset_ = 0) { if (parent_->data_[size_]) { @@ -199,7 +209,7 @@ typename SmartDeque::iterator& SmartDeque::iterato template SmartDeque::iterator::iterator() : size_(0), offset_(0), parent_(nullptr), current_(nullptr) { - if constexpr(std::is_trivial::value) { + if constexpr (std::is_trivial::value) { memset(&default_data, 0, sizeof(default_data)); } else { static_assert(std::is_default_constructible::value, "Expecting default contractible type"); @@ -207,7 +217,7 @@ SmartDeque::iterator::iterator() : size_(0), offset_(0), parent_( } template SmartDeque::iterator::iterator(SmartDeque* parent) : size_(0), offset_(0), parent_(parent), current_(nullptr) { - if constexpr(std::is_trivial::value) { + if constexpr (std::is_trivial::value) { memset(&default_data, 0, sizeof(default_data)); } else { static_assert(std::is_default_constructible::value, "Expecting default contractible type"); diff --git a/cpp_src/core/ft/ft_fuzzy/dumper/fulltextdumper.cc b/cpp_src/core/ft/ft_fuzzy/dumper/fulltextdumper.cc index a80bff951..c7c74f949 100644 --- a/cpp_src/core/ft/ft_fuzzy/dumper/fulltextdumper.cc +++ b/cpp_src/core/ft/ft_fuzzy/dumper/fulltextdumper.cc @@ -13,7 +13,9 @@ FullTextDumper& FullTextDumper::Init() { } void FullTextDumper::LogFinalData(const reindexer::QueryResults& result) { - if (!std::getenv(env.c_str())) return; + if (!std::getenv(env.c_str())) { + return; + } startThread(); std::vector tmp_buffer; @@ -29,7 +31,9 @@ void FullTextDumper::LogFinalData(const reindexer::QueryResults& result) { new_info_ = true; } void FullTextDumper::Log(const std::string& data) { - if (!std::getenv(env.c_str())) return; + if (!std::getenv(env.c_str())) { + return; + } startThread(); std::lock_guard lk(cv_m); @@ -38,7 +42,9 @@ void FullTextDumper::Log(const std::string& data) { } void FullTextDumper::AddResultData(const std::string& reqest) { - if (!std::getenv(env.c_str())) return; + if (!std::getenv(env.c_str())) { + return; + } startThread(); std::vector tmp_buffer; @@ -95,7 +101,9 @@ void FullTextDumper::writeToFile() { } file.close(); } - if (stoped_ || !std::getenv(env.c_str())) return; + if (stoped_ || !std::getenv(env.c_str())) { + return; + } std::unique_lock lk(cv_m); if (cv.wait_for(lk, seconds(write_timeout_seconds), [this] { return stoped_.load(); })) { @@ -112,10 +120,14 @@ FullTextDumper::~FullTextDumper() { } // static class destructor - nothing conflicrts here - if (buffer_.empty()) return; + if (buffer_.empty()) { + return; + } std::ofstream file(file_path, std::ios::app); - if (!file.is_open()) return; + if (!file.is_open()) { + return; + } for (const auto& data : buffer_) { file << data << "\n"; } diff --git a/cpp_src/core/ft/ft_fuzzy/merger/basemerger.cc b/cpp_src/core/ft/ft_fuzzy/merger/basemerger.cc index d28bbfaab..1c6538c49 100644 --- a/cpp_src/core/ft/ft_fuzzy/merger/basemerger.cc +++ b/cpp_src/core/ft/ft_fuzzy/merger/basemerger.cc @@ -32,10 +32,11 @@ void MergedData::Add(const IDCtx& ctx) { if (!first_) { coof = ctx.pos - prev_.src_pos; - if (src_dst < 0) + if (src_dst < 0) { coof = ctx.cfg.posSourceDistMin; - else + } else { coof = (ctx.total_size * ctx.cfg.posSourceDistBoost - (coof - 1)) / double(ctx.total_size * ctx.cfg.posSourceDistBoost); + } } src_dst *= coof; src_dst = (src_dst * max_src_dist) / ctx.total_size; @@ -50,7 +51,9 @@ void MergedData::Add(const IDCtx& ctx) { dst = (dst * max_dst_dist) / size; double fboost = ctx.opts->fieldsOpts[ctx.data->at(i).field()].boost; - if (!fboost) fboost = 1; + if (!fboost) { + fboost = 1; + } src_dst = src_dst + dst; src_dst *= fboost; @@ -86,7 +89,9 @@ SearchResult BaseMerger::Merge(MergeCtx& ctx, bool inTransaction, const reindexe DataSet data_set(min_id_, max_id_); double max_proc = 0; for (auto& res : *ctx.results) { - if (!inTransaction) ThrowOnCancel(rdxCtx); + if (!inTransaction) { + ThrowOnCancel(rdxCtx); + } for (auto it = res.data->begin(); it != res.data->end(); ++it) { IDCtx id_ctx{&it->Pos(), res.pos, &max_proc, ctx.total_size, res.opts, *ctx.cfg, res.proc, ctx.sizes}; diff --git a/cpp_src/core/ft/ft_fuzzy/merger/basemerger.h b/cpp_src/core/ft/ft_fuzzy/merger/basemerger.h index cfc8f4cad..c86300b04 100644 --- a/cpp_src/core/ft/ft_fuzzy/merger/basemerger.h +++ b/cpp_src/core/ft/ft_fuzzy/merger/basemerger.h @@ -14,14 +14,14 @@ class RdxContext; namespace search_engine { struct IDCtx { - const RVector *data; + const RVector* data; int pos; - double *max_proc; + double* max_proc; size_t total_size; - const FtDslOpts *opts; - const FtFuzzyConfig &cfg; + const FtDslOpts* opts; + const FtFuzzyConfig& cfg; double proc; - word_size_map *sizes; + word_size_map* sizes; }; struct ResultMerger { int src_pos; @@ -32,7 +32,7 @@ struct ResultMerger { class MergedData { public: - MergedData(size_t id, const IDCtx &ctx) : id_(id) { + MergedData(size_t id, const IDCtx& ctx) : id_(id) { size_it_ = ctx.sizes->find(id_); if (size_it_ == ctx.sizes->end()) { abort(); @@ -40,7 +40,7 @@ class MergedData { Add(ctx); } - void Add(const IDCtx &ctx); + void Add(const IDCtx& ctx); double proc_ = 0; size_t id_; size_t count_ = 0; @@ -58,24 +58,24 @@ struct SearchResult { using namespace reindexer; struct FirstResult { - const AdvacedPackedVec *data; - const FtDslOpts *opts; + const AdvacedPackedVec* data; + const FtDslOpts* opts; int pos; double proc; }; struct MergeCtx { - std::vector *results; - const FtFuzzyConfig *cfg; + std::vector* results; + const FtFuzzyConfig* cfg; size_t total_size; - word_size_map *sizes; + word_size_map* sizes; }; class BaseMerger { public: BaseMerger(int max_id, int min_id); - SearchResult Merge(MergeCtx &ctx, bool inTransaction, const RdxContext &); + SearchResult Merge(MergeCtx& ctx, bool inTransaction, const RdxContext&); private: int max_id_; diff --git a/cpp_src/core/ft/ft_fuzzy/prefilter/prefilter.cc b/cpp_src/core/ft/ft_fuzzy/prefilter/prefilter.cc index feae2c86a..6c1f47321 100644 --- a/cpp_src/core/ft/ft_fuzzy/prefilter/prefilter.cc +++ b/cpp_src/core/ft/ft_fuzzy/prefilter/prefilter.cc @@ -50,7 +50,9 @@ std::vector Prefilter::findMin(const std::wstring& data) { for (size_t i = 0; i < data.size() - 1; ++i) { uint32_t hash[2]; MurmurHash3_x64_128(data.substr(i, 2).c_str(), data.size() * sizeof(wchar_t), seeds_[g], &hash); - if (hash[0] < min_hash) min_hash = hash[0]; + if (hash[0] < min_hash) { + min_hash = hash[0]; + } } sig[g] = min_hash; } @@ -64,7 +66,9 @@ double Prefilter::CalcMinDistance(const std::wstring& first, const std::wstring& double Prefilter::GetMinDistance(const std::vector& first, const std::vector& second) { size_t dist = 0; for (size_t i = 0; i < first.size(); ++i) { - if (first[i] == second[i]) dist++; + if (first[i] == second[i]) { + dist++; + } } return dist / static_cast(seeds_.size()); } diff --git a/cpp_src/core/ft/ft_fuzzy/searchengine.h b/cpp_src/core/ft/ft_fuzzy/searchengine.h index 2ec703572..5a0a4eca3 100644 --- a/cpp_src/core/ft/ft_fuzzy/searchengine.h +++ b/cpp_src/core/ft/ft_fuzzy/searchengine.h @@ -18,14 +18,14 @@ class SearchEngine { typedef std::shared_ptr Ptr; SearchEngine(); - void SetConfig(const std::unique_ptr &cfg); - SearchEngine(const SearchEngine &rhs) = delete; + void SetConfig(const std::unique_ptr& cfg); + SearchEngine(const SearchEngine& rhs) = delete; - SearchEngine &operator=(const SearchEngine &) = delete; + SearchEngine& operator=(const SearchEngine&) = delete; - SearchResult Search(const FtDSLQuery &dsl, bool inTransaction, const reindexer::RdxContext &); + SearchResult Search(const FtDSLQuery& dsl, bool inTransaction, const reindexer::RdxContext&); void Rebuild(); - void AddData(std::string_view src_data, const IdType id, int field, const std::string &extraWordSymbols); + void AddData(std::string_view src_data, const IdType id, int field, const std::string& extraWordSymbols); void Commit(); private: diff --git a/cpp_src/core/ft/ftdsl.cc b/cpp_src/core/ft/ftdsl.cc index c14246bcd..5fc5614ce 100644 --- a/cpp_src/core/ft/ftdsl.cc +++ b/cpp_src/core/ft/ftdsl.cc @@ -9,14 +9,14 @@ namespace reindexer { // Format: see fulltext.md -static bool is_term(int ch, const std::string &extraWordSymbols) noexcept { +static bool is_term(int ch, const std::string& extraWordSymbols) noexcept { return IsAlpha(ch) || IsDigit(ch) || extraWordSymbols.find(ch) != std::string::npos // wrong kb layout || ch == '[' || ch == ';' || ch == ',' || ch == '.'; } -static bool is_dslbegin(int ch, const std::string &extraWordSymbols) noexcept { +static bool is_dslbegin(int ch, const std::string& extraWordSymbols) noexcept { return is_term(ch, extraWordSymbols) || ch == '+' || ch == '-' || ch == '*' || ch == '\'' || ch == '\"' || ch == '@' || ch == '=' || ch == '\\'; } @@ -26,7 +26,7 @@ void FtDSLQuery::parse(std::string_view q) { utf8_to_utf16(q, utf16str); parse(utf16str); } -void FtDSLQuery::parse(std::wstring &utf16str) { +void FtDSLQuery::parse(std::wstring& utf16str) { int groupTermCounter = 0; bool inGroup = false; bool hasAnythingExceptNot = false; @@ -157,7 +157,9 @@ void FtDSLQuery::parse(std::wstring &utf16str) { maxPatternLen = (fte.pattern.length() > maxPatternLen) ? fte.pattern.length() : maxPatternLen; emplace_back(std::move(fte)); - if (inGroup) ++groupTermCounter; + if (inGroup) { + ++groupTermCounter; + } } } if (inGroup) { @@ -168,19 +170,25 @@ void FtDSLQuery::parse(std::wstring &utf16str) { } int cnt = 0; - for (auto &e : *this) { + for (auto& e : *this) { e.opts.termLenBoost = float(e.pattern.length()) / maxPatternLen; e.opts.qpos = cnt++; } } -void FtDSLQuery::parseFields(std::wstring &utf16str, std::wstring::iterator &it, h_vector &fieldsOpts) { +void FtDSLQuery::parseFields(std::wstring& utf16str, std::wstring::iterator& it, h_vector& fieldsOpts) { FtDslFieldOpts defFieldOpts{0.0, false}; - for (auto &fo : fieldsOpts) fo = defFieldOpts; + for (auto& fo : fieldsOpts) { + fo = defFieldOpts; + } while (it != utf16str.end()) { - while (it != utf16str.end() && !(IsAlpha(*it) || IsDigit(*it) || *it == '*' || *it == '_' || *it == '+')) ++it; - if (it == utf16str.end()) break; + while (it != utf16str.end() && !(IsAlpha(*it) || IsDigit(*it) || *it == '*' || *it == '_' || *it == '+')) { + ++it; + } + if (it == utf16str.end()) { + break; + } bool needSumRank = false; if (*it == '+') { @@ -190,7 +198,9 @@ void FtDSLQuery::parseFields(std::wstring &utf16str, std::wstring::iterator &it, } } auto begIt = it; - while (it != utf16str.end() && (IsAlpha(*it) || IsDigit(*it) || *it == '*' || *it == '_' || *it == '+' || *it == '.')) ++it; + while (it != utf16str.end() && (IsAlpha(*it) || IsDigit(*it) || *it == '*' || *it == '_' || *it == '+' || *it == '.')) { + ++it; + } auto endIt = it; float boost = 1.0; @@ -218,10 +228,15 @@ void FtDSLQuery::parseFields(std::wstring &utf16str, std::wstring::iterator &it, assertf(f->second < int(fieldsOpts.size()), "f=%d,fieldsOpts.size()=%d", f->second, fieldsOpts.size()); fieldsOpts[f->second] = {boost, needSumRank}; } - if (it == utf16str.end() || *it++ != ',') break; + if (it == utf16str.end() || *it++ != ',') { + break; + } + } + for (auto& fo : fieldsOpts) { + if (fo.boost == 0.0) { + fo = defFieldOpts; + } } - for (auto &fo : fieldsOpts) - if (fo.boost == 0.0) fo = defFieldOpts; } } // namespace reindexer diff --git a/cpp_src/core/ft/ftdsl.h b/cpp_src/core/ft/ftdsl.h index 3f39f3c65..3ef26922e 100644 --- a/cpp_src/core/ft/ftdsl.h +++ b/cpp_src/core/ft/ftdsl.h @@ -32,8 +32,8 @@ struct FtDslOpts { struct FtDSLEntry { FtDSLEntry() = default; - FtDSLEntry(std::wstring &&p, FtDslOpts &&o) : pattern{std::move(p)}, opts{std::move(o)} {} - FtDSLEntry(const std::wstring &p, const FtDslOpts &o) : pattern{p}, opts{o} {} + FtDSLEntry(std::wstring&& p, FtDslOpts&& o) : pattern{std::move(p)}, opts{std::move(o)} {} + FtDSLEntry(const std::wstring& p, const FtDslOpts& o) : pattern{p}, opts{o} {} std::wstring pattern; FtDslOpts opts; }; @@ -50,20 +50,20 @@ struct StopWord; class FtDSLQuery : public RVector { public: - FtDSLQuery(const RHashMap &fields, const StopWordsSetT &stopWords, const std::string &extraWordSymbols) noexcept + FtDSLQuery(const RHashMap& fields, const StopWordsSetT& stopWords, const std::string& extraWordSymbols) noexcept : fields_(fields), stopWords_(stopWords), extraWordSymbols_(extraWordSymbols) {} - void parse(std::wstring &utf16str); + void parse(std::wstring& utf16str); void parse(std::string_view q); FtDSLQuery CopyCtx() const noexcept { return {fields_, stopWords_, extraWordSymbols_}; } protected: - void parseFields(std::wstring &utf16str, std::wstring::iterator &it, h_vector &fieldsOpts); + void parseFields(std::wstring& utf16str, std::wstring::iterator& it, h_vector& fieldsOpts); - std::function resolver_; + std::function resolver_; - const RHashMap &fields_; - const StopWordsSetT &stopWords_; - const std::string &extraWordSymbols_; + const RHashMap& fields_; + const StopWordsSetT& stopWords_; + const std::string& extraWordSymbols_; }; } // namespace reindexer diff --git a/cpp_src/core/ft/idrelset.cc b/cpp_src/core/ft/idrelset.cc index 73aeb76eb..c1aeaf9e8 100644 --- a/cpp_src/core/ft/idrelset.cc +++ b/cpp_src/core/ft/idrelset.cc @@ -52,7 +52,9 @@ int IdRelType::Distance(const IdRelType& other, int max) const { int cur = sign ? i->fpos - j->fpos : j->fpos - i->fpos; if (cur < max && cur < (1 << PosType::posBits)) { max = cur; - if (max <= 1) break; + if (max <= 1) { + break; + } } (sign) ? j++ : i++; } @@ -67,7 +69,9 @@ int IdRelType::MinPositionInField(int field) const noexcept { assertrx(lower != pos_.cend() && lower->field() == field); int res = lower->pos(); while (++lower != pos_.cend() && lower->field() == field) { - if (lower->pos() < res) res = lower->pos(); + if (lower->pos() < res) { + res = lower->pos(); + } } return res; } diff --git a/cpp_src/core/ft/idrelset.h b/cpp_src/core/ft/idrelset.h index 3fa22d264..05ed10950 100644 --- a/cpp_src/core/ft/idrelset.h +++ b/cpp_src/core/ft/idrelset.h @@ -144,15 +144,21 @@ class IdRelType { class IdRelSet : public std::vector { public: int Add(VDocIdType id, int pos, int field) { - if (id > max_id_) max_id_ = id; - if (id < min_id_) min_id_ = id; + if (id > max_id_) { + max_id_ = id; + } + if (id < min_id_) { + min_id_ = id; + } auto& last = (empty() || back().Id() != id) ? emplace_back(id) : back(); last.Add(pos, field); return last.size(); } void SimpleCommit() noexcept { - for (auto& val : *this) val.SimpleCommit(); + for (auto& val : *this) { + val.SimpleCommit(); + } } VDocIdType max_id_ = 0; @@ -165,7 +171,9 @@ class IdRelVec : public std::vector { public: size_t heap_size() const noexcept { size_t res = capacity() * sizeof(IdRelType); - for (const auto& id : *this) res += id.HeapSize(); + for (const auto& id : *this) { + res += id.HeapSize(); + } return res; } void erase_back(size_t pos) noexcept { erase(begin() + pos, end()); } diff --git a/cpp_src/core/ft/stemmer.h b/cpp_src/core/ft/stemmer.h index dc2b61aa4..3f7344fb2 100644 --- a/cpp_src/core/ft/stemmer.h +++ b/cpp_src/core/ft/stemmer.h @@ -8,13 +8,13 @@ namespace reindexer { using std::mutex; class stemmer { public: - stemmer(const char *lang = "en") { stemmer_ = sb_stemmer_new(lang, "UTF_8"); } - stemmer(const stemmer &) = delete; - stemmer(stemmer &&other) noexcept { + stemmer(const char* lang = "en") { stemmer_ = sb_stemmer_new(lang, "UTF_8"); } + stemmer(const stemmer&) = delete; + stemmer(stemmer&& other) noexcept { stemmer_ = other.stemmer_; other.stemmer_ = nullptr; } - stemmer &operator=(stemmer &&other) noexcept { + stemmer& operator=(stemmer&& other) noexcept { if (this != &other) { stemmer_ = other.stemmer_; other.stemmer_ = nullptr; @@ -23,14 +23,14 @@ class stemmer { } ~stemmer() { sb_stemmer_delete(stemmer_); } - void stem(const std::string &src, std::string &dst) { + void stem(const std::string& src, std::string& dst) { std::lock_guard lock(lock_); - auto res = sb_stemmer_stem(stemmer_, reinterpret_cast(src.data()), src.length()); - dst.assign(reinterpret_cast(res)); + auto res = sb_stemmer_stem(stemmer_, reinterpret_cast(src.data()), src.length()); + dst.assign(reinterpret_cast(res)); } - sb_stemmer *stemmer_ = nullptr; + sb_stemmer* stemmer_ = nullptr; std::mutex lock_; }; diff --git a/cpp_src/core/ft/stopwords/stop.h b/cpp_src/core/ft/stopwords/stop.h index bda361eae..8860c32c4 100644 --- a/cpp_src/core/ft/stopwords/stop.h +++ b/cpp_src/core/ft/stopwords/stop.h @@ -1,6 +1,6 @@ #pragma once namespace reindexer { -extern const char *stop_words_ru[]; -extern const char *stop_words_en[]; +extern const char* stop_words_ru[]; +extern const char* stop_words_en[]; } // namespace reindexer \ No newline at end of file diff --git a/cpp_src/core/ft/stopwords/stop_en.cc b/cpp_src/core/ft/stopwords/stop_en.cc index 604f64701..8caf56a90 100644 --- a/cpp_src/core/ft/stopwords/stop_en.cc +++ b/cpp_src/core/ft/stopwords/stop_en.cc @@ -1,15 +1,15 @@ namespace reindexer { -const char *stop_words_en[] = { - "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", - "aren", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", - "by", "can", "cannot", "could", "couldn", "did", "didn", "do", "does", "doesn", "doing", "dont", - "down", "during", "each", "few", "for", "from", "further", "had", "hadnt", "has", "hasnt", "have", - "havent", "having", "he", "hed", "hell", "hes", "her", "here", "hers", "herself", "him", "himself", - "his", "how", "hows", "i", "id", "im", "if", "in", "into", "is", "it", "its", - "itself", "me", "more", "most", "must", "my", "myself", "no", "nor", "not", "of", "off", - "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", - "same", "she", "should", "so", "some", "such", "than", "that", "the", "their", "theirs", "them", - "themselves", "then", "there", "these", "they", "this", "those", "through", "to", "too", "under", "until", - "up", "very", "was", "we", "were", "what", "when", "where", "which", "while", "who", "whom", - "why", "with", "would", "you", "your", "yours", "yourself", "yourselves", nullptr}; +const char* stop_words_en[] = { + "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", + "aren", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", + "by", "can", "cannot", "could", "couldn", "did", "didn", "do", "does", "doesn", "doing", "dont", + "down", "during", "each", "few", "for", "from", "further", "had", "hadnt", "has", "hasnt", "have", + "havent", "having", "he", "hed", "hell", "hes", "her", "here", "hers", "herself", "him", "himself", + "his", "how", "hows", "i", "id", "im", "if", "in", "into", "is", "it", "its", + "itself", "me", "more", "most", "must", "my", "myself", "no", "nor", "not", "of", "off", + "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", + "same", "she", "should", "so", "some", "such", "than", "that", "the", "their", "theirs", "them", + "themselves", "then", "there", "these", "they", "this", "those", "through", "to", "too", "under", "until", + "up", "very", "was", "we", "were", "what", "when", "where", "which", "while", "who", "whom", + "why", "with", "would", "you", "your", "yours", "yourself", "yourselves", nullptr}; } diff --git a/cpp_src/core/ft/stopwords/stop_ru.cc b/cpp_src/core/ft/stopwords/stop_ru.cc index 5b1b26d98..659bfd905 100644 --- a/cpp_src/core/ft/stopwords/stop_ru.cc +++ b/cpp_src/core/ft/stopwords/stop_ru.cc @@ -1,5 +1,5 @@ namespace reindexer { -const char *stop_words_ru[] = { +const char* stop_words_ru[] = { // clang-format off "а", "е", diff --git a/cpp_src/core/ft/typos.cc b/cpp_src/core/ft/typos.cc index 75e412897..65593d860 100644 --- a/cpp_src/core/ft/typos.cc +++ b/cpp_src/core/ft/typos.cc @@ -5,8 +5,8 @@ namespace reindexer { template -static void mktyposInternal(typos_context *ctx, const std::wstring &word, int maxTyposLen, const typos_context::CallBack &callback, - typos_context::TyposVec &positions) { +static void mktyposInternal(typos_context* ctx, const std::wstring& word, int maxTyposLen, const typos_context::CallBack& callback, + typos_context::TyposVec& positions) { static_assert(level <= typos_context::TyposVec::capacity(), "Positions array must be able to store all the typos"); if constexpr (level == 0) { return; @@ -45,7 +45,7 @@ static void mktyposInternal(typos_context *ctx, const std::wstring &word, int ma } } -void mktypos(typos_context *ctx, const std::wstring &word, int level, int maxTyposLen, const typos_context::CallBack &callback) { +void mktypos(typos_context* ctx, const std::wstring& word, int level, int maxTyposLen, const typos_context::CallBack& callback) { utf16_to_utf8(word, ctx->typo); typos_context::TyposVec positions; callback(ctx->typo, level, positions); @@ -64,7 +64,7 @@ void mktypos(typos_context *ctx, const std::wstring &word, int level, int maxTyp } } -void mktypos(typos_context *ctx, std::string_view word, int level, int maxTyposLen, const typos_context::CallBack &callback) { +void mktypos(typos_context* ctx, std::string_view word, int level, int maxTyposLen, const typos_context::CallBack& callback) { ctx->typo.assign(word.begin(), word.end()); utf8_to_utf16(ctx->typo, ctx->utf16Word); typos_context::TyposVec positions; diff --git a/cpp_src/core/ft/typos.h b/cpp_src/core/ft/typos.h index 7f0059ac7..50f045566 100644 --- a/cpp_src/core/ft/typos.h +++ b/cpp_src/core/ft/typos.h @@ -13,13 +13,13 @@ struct typos_context { public: using value_type = int8_t; using size_type = uint8_t; - using const_iterator = const value_type *; + using const_iterator = const value_type*; static_assert(std::numeric_limits::max() >= kMaxTypoLenLimit, "'Positions' array must be able to store any available typos postions"); TyposVec() noexcept = default; - TyposVec(const TyposVec &o) noexcept = default; + TyposVec(const TyposVec& o) noexcept = default; void emplace_back(value_type v) { if (size_ >= kMaxTyposInWord) { @@ -38,19 +38,19 @@ struct typos_context { const_iterator begin() const noexcept { return arr_; } const_iterator end() const noexcept { return arr_ + size_; } value_type operator[](size_type pos) const { return arr_[pos]; } - TyposVec &operator=(const TyposVec &o) noexcept = default; + TyposVec& operator=(const TyposVec& o) noexcept = default; private: value_type arr_[kMaxTyposInWord]; size_type size_ = 0; }; - using CallBack = std::function; + using CallBack = std::function; std::wstring utf16Word, utf16Typo; std::string typo; }; -void mktypos(typos_context *ctx, const std::wstring &word, int level, int maxTyposLen, const typos_context::CallBack &callback); -void mktypos(typos_context *ctx, std::string_view word, int level, int maxTyposLen, const typos_context::CallBack &callback); +void mktypos(typos_context* ctx, const std::wstring& word, int level, int maxTyposLen, const typos_context::CallBack& callback); +void mktypos(typos_context* ctx, std::string_view word, int level, int maxTyposLen, const typos_context::CallBack& callback); } // namespace reindexer diff --git a/cpp_src/core/idset.cc b/cpp_src/core/idset.cc index 501c692f5..fd2c05ad4 100644 --- a/cpp_src/core/idset.cc +++ b/cpp_src/core/idset.cc @@ -5,7 +5,9 @@ namespace reindexer { std::string IdSetPlain::Dump() const { std::string buf = "["; - for (int i = 0; i < static_cast(size()); i++) buf += std::to_string((*this)[i]) + " "; + for (int i = 0; i < static_cast(size()); i++) { + buf += std::to_string((*this)[i]) + " "; + } buf += "]"; @@ -15,7 +17,9 @@ std::string IdSetPlain::Dump() const { std::ostream& operator<<(std::ostream& os, const IdSetPlain& idset) { os << '['; for (auto b = idset.begin(), it = b, e = idset.end(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << *it; } return os << ']'; diff --git a/cpp_src/core/idset.h b/cpp_src/core/idset.h index df16e24d3..af1b7d44b 100644 --- a/cpp_src/core/idset.h +++ b/cpp_src/core/idset.h @@ -69,15 +69,15 @@ class IdSetPlain : protected base_idset { bool IsEmpty() const noexcept { return empty(); } size_t Size() const noexcept { return size(); } size_t BTreeSize() const noexcept { return 0; } - const base_idsetset *BTree() const noexcept { return nullptr; } + const base_idsetset* BTree() const noexcept { return nullptr; } void ReserveForSorted(int sortedIdxCount) { reserve(size() * (sortedIdxCount + 1)); } std::string Dump() const; protected: - IdSetPlain(base_idset &&idset) noexcept : base_idset(std::move(idset)) {} + IdSetPlain(base_idset&& idset) noexcept : base_idset(std::move(idset)) {} }; -std::ostream &operator<<(std::ostream &, const IdSetPlain &); +std::ostream& operator<<(std::ostream&, const IdSetPlain&); // maxmimum size of idset without building btree const int kMaxPlainIdsetSize = 16; @@ -88,10 +88,10 @@ class IdSet : public IdSetPlain { public: using Ptr = intrusive_ptr>; IdSet() noexcept : usingBtree_(false) {} - IdSet(const IdSet &other) + IdSet(const IdSet& other) : IdSetPlain(other), set_(!other.set_ ? nullptr : new base_idsetset(*other.set_)), usingBtree_(other.usingBtree_.load()) {} - IdSet(IdSet &&other) noexcept : IdSetPlain(std::move(other)), set_(std::move(other.set_)), usingBtree_(other.usingBtree_.load()) {} - IdSet &operator=(IdSet &&other) noexcept { + IdSet(IdSet&& other) noexcept : IdSetPlain(std::move(other)), set_(std::move(other.set_)), usingBtree_(other.usingBtree_.load()) {} + IdSet& operator=(IdSet&& other) noexcept { if (&other != this) { IdSetPlain::operator=(std::move(other)); set_ = std::move(other.set_); @@ -99,7 +99,7 @@ class IdSet : public IdSetPlain { } return *this; } - IdSet &operator=(const IdSet &other) { + IdSet& operator=(const IdSet& other) { if (&other != this) { IdSetPlain::operator=(other); set_.reset(!other.set_ ? nullptr : new base_idsetset(*other.set_)); @@ -107,7 +107,7 @@ class IdSet : public IdSetPlain { } return *this; } - static Ptr BuildFromUnsorted(base_idset &&ids) { + static Ptr BuildFromUnsorted(base_idset&& ids) { boost::sort::pdqsort_branchless(ids.begin(), ids.end()); ids.erase(std::unique(ids.begin(), ids.end()), ids.end()); // TODO: It would be better to integrate unique into sort return make_intrusive>(std::move(ids)); @@ -166,11 +166,13 @@ class IdSet : public IdSetPlain { } template - void Append(InputIt first, InputIt last, const std::vector &mask, EditMode editMode = Auto) { + void Append(InputIt first, InputIt last, const std::vector& mask, EditMode editMode = Auto) { if (editMode == Unordered) { assertrx(!set_); for (; first != last; ++first) { - if (mask[*first]) push_back(*first); + if (mask[*first]) { + push_back(*first); + } } } else if (editMode == Auto) { if (!set_) { @@ -180,7 +182,9 @@ class IdSet : public IdSetPlain { } assertrx(!size()); for (; first != last; ++first) { - if (mask[*first]) set_->insert(*first); + if (mask[*first]) { + set_->insert(*first); + } } usingBtree_.store(true, std::memory_order_release); } else { @@ -202,7 +206,9 @@ class IdSet : public IdSetPlain { void Commit() { if (!size() && set_) { reserve(set_->size()); - for (auto id : *set_) push_back(id); + for (auto id : *set_) { + push_back(id); + } } usingBtree_.store(false, std::memory_order_release); @@ -211,7 +217,7 @@ class IdSet : public IdSetPlain { bool IsEmpty() const noexcept { return empty() && (!set_ || set_->empty()); } size_t Size() const noexcept { return usingBtree_.load(std::memory_order_acquire) ? set_->size() : size(); } size_t BTreeSize() const noexcept { return set_ ? sizeof(*set_.get()) + set_->size() * sizeof(int) : 0; } - const base_idsetset *BTree() const noexcept { return set_.get(); } + const base_idsetset* BTree() const noexcept { return set_.get(); } void ReserveForSorted(int sortedIdxCount) { reserve(((set_ ? set_->size() : size())) * (sortedIdxCount + 1)); } protected: @@ -220,7 +226,7 @@ class IdSet : public IdSetPlain { template friend class BtreeIndexReverseIteratorImpl; - IdSet(base_idset &&idset) noexcept : IdSetPlain(std::move(idset)), usingBtree_(false) {} + IdSet(base_idset&& idset) noexcept : IdSetPlain(std::move(idset)), usingBtree_(false) {} std::unique_ptr set_; std::atomic usingBtree_; diff --git a/cpp_src/core/idsetcache.h b/cpp_src/core/idsetcache.h index 6acdb92b5..d70d7f266 100644 --- a/cpp_src/core/idsetcache.h +++ b/cpp_src/core/idsetcache.h @@ -10,16 +10,16 @@ namespace reindexer { struct IdSetCacheKey { - IdSetCacheKey(const VariantArray &keys, CondType cond, SortType sort) noexcept : keys(&keys), cond(cond), sort(sort) {} - IdSetCacheKey(const IdSetCacheKey &other) : keys(&hkeys), cond(other.cond), sort(other.sort), hkeys(*other.keys) {} - IdSetCacheKey(IdSetCacheKey &&other) noexcept : keys(&hkeys), cond(other.cond), sort(other.sort) { + IdSetCacheKey(const VariantArray& keys, CondType cond, SortType sort) noexcept : keys(&keys), cond(cond), sort(sort) {} + IdSetCacheKey(const IdSetCacheKey& other) : keys(&hkeys), cond(other.cond), sort(other.sort), hkeys(*other.keys) {} + IdSetCacheKey(IdSetCacheKey&& other) noexcept : keys(&hkeys), cond(other.cond), sort(other.sort) { if (&other.hkeys == other.keys) { hkeys = std::move(other.hkeys); } else { hkeys = *other.keys; } } - IdSetCacheKey &operator=(const IdSetCacheKey &other) { + IdSetCacheKey& operator=(const IdSetCacheKey& other) { if (&other != this) { hkeys = *other.keys; keys = &hkeys; @@ -28,7 +28,7 @@ struct IdSetCacheKey { } return *this; } - IdSetCacheKey &operator=(IdSetCacheKey &&other) noexcept { + IdSetCacheKey& operator=(IdSetCacheKey&& other) noexcept { if (&other != this) { if (&other.hkeys == other.keys) { hkeys = std::move(other.hkeys); @@ -44,14 +44,14 @@ struct IdSetCacheKey { size_t Size() const noexcept { return sizeof(IdSetCacheKey) + keys->size() * sizeof(VariantArray::value_type); } - const VariantArray *keys; + const VariantArray* keys; CondType cond; SortType sort; VariantArray hkeys; }; template -T &operator<<(T &os, const IdSetCacheKey &k) { +T& operator<<(T& os, const IdSetCacheKey& k) { os << "{cond: " << CondTypeToStr(k.cond) << ", sort: " << k.sort << ", keys: "; k.hkeys.Dump(os); return os << '}'; @@ -59,14 +59,14 @@ T &operator<<(T &os, const IdSetCacheKey &k) { struct IdSetCacheVal { IdSetCacheVal() = default; - IdSetCacheVal(IdSet::Ptr &&i) noexcept : ids(std::move(i)) {} + IdSetCacheVal(IdSet::Ptr&& i) noexcept : ids(std::move(i)) {} size_t Size() const noexcept { return ids ? (sizeof(*ids.get()) + ids->heap_size()) : 0; } IdSet::Ptr ids; }; template -T &operator<<(T &os, const IdSetCacheVal &v) { +T& operator<<(T& os, const IdSetCacheVal& v) { if (v.ids) { return os << *v.ids; } else { @@ -75,12 +75,12 @@ T &operator<<(T &os, const IdSetCacheVal &v) { } struct equal_idset_cache_key { - bool operator()(const IdSetCacheKey &lhs, const IdSetCacheKey &rhs) const noexcept { + bool operator()(const IdSetCacheKey& lhs, const IdSetCacheKey& rhs) const noexcept { return lhs.cond == rhs.cond && lhs.sort == rhs.sort && *lhs.keys == *rhs.keys; } }; struct hash_idset_cache_key { - size_t operator()(const IdSetCacheKey &s) const noexcept { return (size_t(s.cond) << 8) ^ (size_t(s.sort) << 16) ^ s.keys->Hash(); } + size_t operator()(const IdSetCacheKey& s) const noexcept { return (size_t(s.cond) << 8) ^ (size_t(s.sort) << 16) ^ s.keys->Hash(); } }; using IdSetCacheBase = LRUCache; @@ -88,9 +88,9 @@ using IdSetCacheBase = LRUCache &s) { + void ClearSorted(const std::bitset& s) { if (s.any()) { - Clear([&s](const IdSetCacheKey &k) { return s.test(k.sort); }); + Clear([&s](const IdSetCacheKey& k) { return s.test(k.sort); }); } } }; diff --git a/cpp_src/core/index/index.cc b/cpp_src/core/index/index.cc index cd3ba6d3c..c7b8a6151 100644 --- a/cpp_src/core/index/index.cc +++ b/cpp_src/core/index/index.cc @@ -75,7 +75,9 @@ void Index::dump(S& os, std::string_view step, std::string_view offset) const { << newOffset << "selectKeyType: " << selectKeyType_.Name() << ",\n" << newOffset << "sortOrders: ["; for (size_t i = 0; i < sortOrders_.size(); ++i) { - if (i != 0) os << ", "; + if (i != 0) { + os << ", "; + } os << sortOrders_[i]; } os << "],\n" << newOffset << "sortId: " << sortId_ << ",\n" << newOffset << "opts: "; diff --git a/cpp_src/core/index/indexordered.cc b/cpp_src/core/index/indexordered.cc index fb416eba9..ba30c2c73 100644 --- a/cpp_src/core/index/indexordered.cc +++ b/cpp_src/core/index/indexordered.cc @@ -8,7 +8,7 @@ namespace reindexer { template -Variant IndexOrdered::Upsert(const Variant &key, IdType id, bool &clearCache) { +Variant IndexOrdered::Upsert(const Variant& key, IdType id, bool& clearCache) { if (key.Type().Is()) { if (this->empty_ids_.Unsorted().Add(id, IdSet::Auto, this->sortedIdxCount_)) { this->cache_.reset(); @@ -21,10 +21,11 @@ Variant IndexOrdered::Upsert(const Variant &key, IdType id, bool &clearCache) auto keyIt = this->idx_map.lower_bound(static_cast(key)); - if (keyIt == this->idx_map.end() || this->idx_map.key_comp()(static_cast(key), keyIt->first)) + if (keyIt == this->idx_map.end() || this->idx_map.key_comp()(static_cast(key), keyIt->first)) { keyIt = this->idx_map.insert(keyIt, {static_cast(key), typename T::mapped_type()}); - else + } else { this->delMemStat(keyIt); + } if (keyIt->second.Unsorted().Add(id, this->opts_.IsPK() ? IdSet::Ordered : IdSet::Auto, this->sortedIdxCount_)) { this->isBuilt_ = false; @@ -38,10 +39,12 @@ Variant IndexOrdered::Upsert(const Variant &key, IdType id, bool &clearCache) } template -SelectKeyResults IndexOrdered::SelectKey(const VariantArray &keys, CondType condition, SortType sortId, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &ctx, const RdxContext &rdxCtx) { +SelectKeyResults IndexOrdered::SelectKey(const VariantArray& keys, CondType condition, SortType sortId, Index::SelectOpts opts, + const BaseFunctionCtx::Ptr& ctx, const RdxContext& rdxCtx) { const auto indexWard(rdxCtx.BeforeIndexWork()); - if (opts.forceComparator) return IndexStore>::SelectKey(keys, condition, sortId, opts, ctx, rdxCtx); + if (opts.forceComparator) { + return IndexStore>::SelectKey(keys, condition, sortId, opts, ctx, rdxCtx); + } // Get set of keys or single key if (!IsOrderedCondition(condition)) { @@ -62,23 +65,31 @@ SelectKeyResults IndexOrdered::SelectKey(const VariantArray &keys, CondType c break; case CondLe: endIt = this->idx_map.lower_bound(static_cast(key1)); - if (endIt != this->idx_map.end() && !this->idx_map.key_comp()(static_cast(key1), endIt->first)) ++endIt; + if (endIt != this->idx_map.end() && !this->idx_map.key_comp()(static_cast(key1), endIt->first)) { + ++endIt; + } break; case CondGt: startIt = this->idx_map.upper_bound(static_cast(key1)); break; case CondGe: startIt = this->idx_map.find(static_cast(key1)); - if (startIt == this->idx_map.end()) startIt = this->idx_map.upper_bound(static_cast(key1)); + if (startIt == this->idx_map.end()) { + startIt = this->idx_map.upper_bound(static_cast(key1)); + } break; case CondRange: { - const auto &key2 = keys[1]; + const auto& key2 = keys[1]; startIt = this->idx_map.find(static_cast(key1)); - if (startIt == this->idx_map.end()) startIt = this->idx_map.upper_bound(static_cast(key1)); + if (startIt == this->idx_map.end()) { + startIt = this->idx_map.upper_bound(static_cast(key1)); + } endIt = this->idx_map.lower_bound(static_cast(key2)); - if (endIt != this->idx_map.end() && !this->idx_map.key_comp()(static_cast(key2), endIt->first)) ++endIt; + if (endIt != this->idx_map.end() && !this->idx_map.key_comp()(static_cast(key2), endIt->first)) { + ++endIt; + } if (endIt != this->idx_map.end() && this->idx_map.key_comp()(endIt->first, static_cast(key1))) { return SelectKeyResults(std::move(res)); @@ -123,12 +134,12 @@ SelectKeyResults IndexOrdered::SelectKey(const VariantArray &keys, CondType c // TODO: use count of items in ns to more clever select plan if (count < 50) { struct { - T *i_map; + T* i_map; SortType sortId; typename T::iterator startIt, endIt; } ctx = {&this->idx_map, sortId, startIt, endIt}; - auto selector = [&ctx, count](SelectKeyResult &res, size_t &idsCount) { + auto selector = [&ctx, count](SelectKeyResult& res, size_t& idsCount) { idsCount = 0; res.reserve(count); for (auto it = ctx.startIt; it != ctx.endIt; ++it) { @@ -158,17 +169,20 @@ SelectKeyResults IndexOrdered::SelectKey(const VariantArray &keys, CondType c } template -void IndexOrdered::MakeSortOrders(UpdateSortedContext &ctx) { +void IndexOrdered::MakeSortOrders(UpdateSortedContext& ctx) { logPrintf(LogTrace, "IndexOrdered::MakeSortOrders (%s)", this->name_); - auto &ids2Sorts = ctx.ids2Sorts(); + auto& ids2Sorts = ctx.ids2Sorts(); size_t totalIds = 0; - for (auto it : ids2Sorts) - if (it != SortIdUnexists) totalIds++; + for (auto it : ids2Sorts) { + if (it != SortIdUnexists) { + totalIds++; + } + } this->sortId_ = ctx.getCurSortId(); this->sortOrders_.resize(totalIds); size_t idx = 0; - for (auto &keyIt : this->idx_map) { + for (auto& keyIt : this->idx_map) { // assert (keyIt.second.size()); for (auto id : keyIt.second.Unsorted()) { if (id >= int(ids2Sorts.size()) || ids2Sorts[id] == SortIdUnexists) { @@ -202,8 +216,8 @@ IndexIterator::Ptr IndexOrdered::CreateIterator() const { } template -static std::unique_ptr IndexOrdered_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +static std::unique_ptr IndexOrdered_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { switch (idef.Type()) { case IndexIntBTree: return std::make_unique>>(idef, std::move(payloadType), std::move(fields), cacheCfg); @@ -239,8 +253,8 @@ static std::unique_ptr IndexOrdered_New(const IndexDef &idef, PayloadType } // NOLINTBEGIN(*cplusplus.NewDeleteLeaks) -std::unique_ptr IndexOrdered_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +std::unique_ptr IndexOrdered_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { return (idef.opts_.IsPK() || idef.opts_.IsDense()) ? IndexOrdered_New(idef, std::move(payloadType), std::move(fields), cacheCfg) : IndexOrdered_New(idef, std::move(payloadType), std::move(fields), cacheCfg); diff --git a/cpp_src/core/index/indexordered.h b/cpp_src/core/index/indexordered.h index a12928024..f0d0e25ad 100644 --- a/cpp_src/core/index/indexordered.h +++ b/cpp_src/core/index/indexordered.h @@ -10,19 +10,19 @@ class IndexOrdered : public IndexUnordered { using ref_type = typename IndexUnordered::ref_type; using key_type = typename IndexUnordered::key_type; - IndexOrdered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, const NamespaceCacheConfigData &cacheCfg) + IndexOrdered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, const NamespaceCacheConfigData& cacheCfg) : IndexUnordered(idef, std::move(payloadType), std::move(fields), cacheCfg) {} - SelectKeyResults SelectKey(const VariantArray &keys, CondType condition, SortType stype, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &ctx, const RdxContext &) override; - Variant Upsert(const Variant &key, IdType id, bool &clearCache) override; - void MakeSortOrders(UpdateSortedContext &ctx) override; + SelectKeyResults SelectKey(const VariantArray& keys, CondType condition, SortType stype, Index::SelectOpts opts, + const BaseFunctionCtx::Ptr& ctx, const RdxContext&) override; + Variant Upsert(const Variant& key, IdType id, bool& clearCache) override; + void MakeSortOrders(UpdateSortedContext& ctx) override; IndexIterator::Ptr CreateIterator() const override; std::unique_ptr Clone() const override { return std::make_unique>(*this); } bool IsOrdered() const noexcept override { return true; } }; -std::unique_ptr IndexOrdered_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg); +std::unique_ptr IndexOrdered_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg); } // namespace reindexer diff --git a/cpp_src/core/index/indexstore.cc b/cpp_src/core/index/indexstore.cc index 0d1ac79df..1fc21dcad 100644 --- a/cpp_src/core/index/indexstore.cc +++ b/cpp_src/core/index/indexstore.cc @@ -7,14 +7,14 @@ namespace reindexer { template <> -IndexStore::IndexStore(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields) +IndexStore::IndexStore(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields) : Index(idef, std::move(payloadType), std::move(fields)) { keyType_ = selectKeyType_ = KeyValueType::Double{}; opts_.Array(true); } template <> -void IndexStore::Delete(const Variant &key, IdType /*id*/, StringsHolder &strHolder, bool & /*clearCache*/) { +void IndexStore::Delete(const Variant& key, IdType /*id*/, StringsHolder& strHolder, bool& /*clearCache*/) { assertrx_dbg(!IsFulltext()); if (key.Type().Is()) { return; @@ -36,26 +36,28 @@ void IndexStore::Delete(const Variant &key, IdType /*id*/, StringsHo } } template -void IndexStore::Delete(const Variant & /*key*/, IdType /* id */, StringsHolder &, bool & /*clearCache*/) { +void IndexStore::Delete(const Variant& /*key*/, IdType /* id */, StringsHolder&, bool& /*clearCache*/) { assertrx_dbg(!IsFulltext()); } template -void IndexStore::Delete(const VariantArray &keys, IdType id, StringsHolder &strHolder, bool &clearCache) { +void IndexStore::Delete(const VariantArray& keys, IdType id, StringsHolder& strHolder, bool& clearCache) { if (keys.empty()) { Delete(Variant{}, id, strHolder, clearCache); } else { - for (const auto &key : keys) Delete(key, id, strHolder, clearCache); + for (const auto& key : keys) { + Delete(key, id, strHolder, clearCache); + } } } template <> -void IndexStore::Delete(const VariantArray & /*keys*/, IdType /*id*/, StringsHolder &, bool & /*clearCache*/) { +void IndexStore::Delete(const VariantArray& /*keys*/, IdType /*id*/, StringsHolder&, bool& /*clearCache*/) { assertrx(0); } template <> -Variant IndexStore::Upsert(const Variant &key, IdType id, bool & /*clearCache*/) { +Variant IndexStore::Upsert(const Variant& key, IdType id, bool& /*clearCache*/) { assertrx_dbg(!IsFulltext()); if (key.Type().Is()) { return Variant(); @@ -85,12 +87,12 @@ Variant IndexStore::Upsert(const Variant &key, IdType id, bool & /*c } template <> -Variant IndexStore::Upsert(const Variant &key, IdType /*id*/, bool & /*clearCache*/) { +Variant IndexStore::Upsert(const Variant& key, IdType /*id*/, bool& /*clearCache*/) { return key; } template -Variant IndexStore::Upsert(const Variant &key, IdType id, bool & /*clearCache*/) { +Variant IndexStore::Upsert(const Variant& key, IdType id, bool& /*clearCache*/) { if (!opts_.IsArray() && !opts_.IsDense() && !opts_.IsSparse() && !key.Type().Is()) { idx_data.resize(std::max(id + 1, IdType(idx_data.size()))); idx_data[id] = static_cast(key); @@ -99,17 +101,19 @@ Variant IndexStore::Upsert(const Variant &key, IdType id, bool & /*clearCache } template -void IndexStore::Upsert(VariantArray &result, const VariantArray &keys, IdType id, bool &clearCache) { +void IndexStore::Upsert(VariantArray& result, const VariantArray& keys, IdType id, bool& clearCache) { if (keys.empty()) { Upsert(Variant{}, id, clearCache); } else { result.reserve(keys.size()); - for (const auto &key : keys) result.emplace_back(Upsert(key, id, clearCache)); + for (const auto& key : keys) { + result.emplace_back(Upsert(key, id, clearCache)); + } } } template <> -void IndexStore::Upsert(VariantArray & /*result*/, const VariantArray & /*keys*/, IdType /*id*/, bool & /*clearCache*/) { +void IndexStore::Upsert(VariantArray& /*result*/, const VariantArray& /*keys*/, IdType /*id*/, bool& /*clearCache*/) { assertrx(0); } @@ -119,8 +123,8 @@ void IndexStore::Commit() { } template -SelectKeyResults IndexStore::SelectKey(const VariantArray &keys, CondType condition, SortType /*sortId*/, Index::SelectOpts sopts, - const BaseFunctionCtx::Ptr & /*ctx*/, const RdxContext &rdxCtx) { +SelectKeyResults IndexStore::SelectKey(const VariantArray& keys, CondType condition, SortType /*sortId*/, Index::SelectOpts sopts, + const BaseFunctionCtx::Ptr& /*ctx*/, const RdxContext& rdxCtx) { const auto indexWard(rdxCtx.BeforeIndexWork()); if (condition == CondEmpty && !this->opts_.IsArray() && !this->opts_.IsSparse()) { throw Error(errParams, "The 'is NULL' condition is suported only by 'sparse' or 'array' indexes"); @@ -136,7 +140,7 @@ SelectKeyResults IndexStore::SelectKey(const VariantArray &keys, CondType con } template -IndexMemStat IndexStore::GetMemStat(const RdxContext &) { +IndexMemStat IndexStore::GetMemStat(const RdxContext&) { IndexMemStat ret = memStat_; ret.name = name_; ret.uniqKeysCount = str_map.size(); @@ -146,26 +150,30 @@ IndexMemStat IndexStore::GetMemStat(const RdxContext &) { template template -void IndexStore::dump(S &os, std::string_view step, std::string_view offset) const { +void IndexStore::dump(S& os, std::string_view step, std::string_view offset) const { std::string newOffset{offset}; newOffset += step; os << "{\n" << newOffset << ": "; Index::Dump(os, step, newOffset); os << ",\n" << newOffset << "str_map: {"; for (auto b = str_map.begin(), it = b, e = str_map.end(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << '{' << (*it).first << ": " << (*it).second << '}'; } os << "},\n" << newOffset << "idx_data: ["; for (auto b = idx_data.cbegin(), it = b, e = idx_data.cend(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << *it; } os << "]\n" << offset << '}'; } template -void IndexStore::AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue &q) { +void IndexStore::AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue& q) { if constexpr (HasAddTask::value) { str_map.add_destroy_task(&q); } @@ -177,7 +185,7 @@ bool IndexStore::shouldHoldValueInStrMap() const noexcept { return this->opts_.GetCollateMode() != CollateNone || Type() == IndexStrStore; } -std::unique_ptr IndexStore_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields) { +std::unique_ptr IndexStore_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields) { switch (idef.Type()) { case IndexBool: return std::make_unique>(idef, std::move(payloadType), std::move(fields)); diff --git a/cpp_src/core/index/indexstore.h b/cpp_src/core/index/indexstore.h index 18e17f542..f9e26d82a 100644 --- a/cpp_src/core/index/indexstore.h +++ b/cpp_src/core/index/indexstore.h @@ -8,29 +8,29 @@ namespace reindexer { template class IndexStore : public Index { public: - IndexStore(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields) + IndexStore(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields) : Index(idef, std::move(payloadType), std::move(fields)) { static T a; keyType_ = selectKeyType_ = Variant(a).Type(); } - Variant Upsert(const Variant &key, IdType id, bool &clearCache) override; - void Upsert(VariantArray &result, const VariantArray &keys, IdType id, bool &clearCache) override; - void Delete(const Variant &key, IdType id, StringsHolder &, bool &clearCache) override; - void Delete(const VariantArray &keys, IdType id, StringsHolder &, bool &clearCache) override; - SelectKeyResults SelectKey(const VariantArray &keys, CondType condition, SortType stype, Index::SelectOpts res_type, - const BaseFunctionCtx::Ptr &ctx, const RdxContext &) override; + Variant Upsert(const Variant& key, IdType id, bool& clearCache) override; + void Upsert(VariantArray& result, const VariantArray& keys, IdType id, bool& clearCache) override; + void Delete(const Variant& key, IdType id, StringsHolder&, bool& clearCache) override; + void Delete(const VariantArray& keys, IdType id, StringsHolder&, bool& clearCache) override; + SelectKeyResults SelectKey(const VariantArray& keys, CondType condition, SortType stype, Index::SelectOpts res_type, + const BaseFunctionCtx::Ptr& ctx, const RdxContext&) override; void Commit() override; - void UpdateSortedIds(const UpdateSortedContext & /*ctx*/) override {} + void UpdateSortedIds(const UpdateSortedContext& /*ctx*/) override {} std::unique_ptr Clone() const override { return std::make_unique>(*this); } - IndexMemStat GetMemStat(const RdxContext &) override; + IndexMemStat GetMemStat(const RdxContext&) override; bool HoldsStrings() const noexcept override { return std::is_same_v || std::is_same_v; } - void Dump(std::ostream &os, std::string_view step = " ", std::string_view offset = "") const override { dump(os, step, offset); } - virtual void AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue &) override; + void Dump(std::ostream& os, std::string_view step = " ", std::string_view offset = "") const override { dump(os, step, offset); } + virtual void AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue&) override; virtual bool IsDestroyPartSupported() const noexcept override final { return true; } virtual bool IsUuid() const noexcept override final { return std::is_same_v; } - virtual void ReconfigureCache(const NamespaceCacheConfigData &) override {} - const void *ColumnData() const noexcept override final { return idx_data.size() ? idx_data.data() : nullptr; } + virtual void ReconfigureCache(const NamespaceCacheConfigData&) override {} + const void* ColumnData() const noexcept override final { return idx_data.size() ? idx_data.data() : nullptr; } template struct HasAddTask : std::false_type {}; @@ -49,12 +49,12 @@ class IndexStore : public Index { bool shouldHoldValueInStrMap() const noexcept; template - void dump(S &os, std::string_view step, std::string_view offset) const; + void dump(S& os, std::string_view step, std::string_view offset) const; }; template <> -IndexStore::IndexStore(const IndexDef &, PayloadType &&, FieldsSet &&); +IndexStore::IndexStore(const IndexDef&, PayloadType&&, FieldsSet&&); -std::unique_ptr IndexStore_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&); +std::unique_ptr IndexStore_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&&); } // namespace reindexer diff --git a/cpp_src/core/index/indextext/fastindextext.cc b/cpp_src/core/index/indextext/fastindextext.cc index c98c4ae40..7e56292de 100644 --- a/cpp_src/core/index/indextext/fastindextext.cc +++ b/cpp_src/core/index/indextext/fastindextext.cc @@ -10,7 +10,7 @@ namespace { // Available stemmers for languages -const char *stemLangs[] = {"en", "ru", "nl", "fin", "de", "da", "fr", "it", "hu", "no", "pt", "ro", "es", "sv", "tr", nullptr}; +const char* stemLangs[] = {"en", "ru", "nl", "fin", "de", "da", "fr", "it", "hu", "no", "pt", "ro", "es", "sv", "tr", nullptr}; } // namespace namespace reindexer { @@ -19,7 +19,7 @@ using std::chrono::duration_cast; using std::chrono::milliseconds; template -void FastIndexText::initHolder(FtFastConfig &cfg) { +void FastIndexText::initHolder(FtFastConfig& cfg) { switch (cfg.optimization) { case FtFastConfig::Optimization::Memory: holder_.reset(new DataHolder); @@ -34,14 +34,14 @@ void FastIndexText::initHolder(FtFastConfig &cfg) { holder_->translit_.reset(new Translit); holder_->kbLayout_.reset(new KbLayout); holder_->synonyms_.reset(new Synonyms); - for (const char **lang = stemLangs; *lang; ++lang) { + for (const char** lang = stemLangs; *lang; ++lang) { holder_->stemmers_.emplace(*lang, *lang); } holder_->SetConfig(&cfg); } template -Variant FastIndexText::Upsert(const Variant &key, IdType id, bool &clearCache) { +Variant FastIndexText::Upsert(const Variant& key, IdType id, bool& clearCache) { if rx_unlikely (key.Type().Is()) { if (this->empty_ids_.Unsorted().Add(id, IdSet::Auto, 0)) { this->isBuilt_ = false; @@ -59,7 +59,9 @@ Variant FastIndexText::Upsert(const Variant &key, IdType id, bool &clearCache } if (keyIt->second.Unsorted().Add(id, this->opts_.IsPK() ? IdSet::Ordered : IdSet::Auto, 0)) { this->isBuilt_ = false; - if (this->cache_ft_) this->cache_ft_->Clear(); + if (this->cache_ft_) { + this->cache_ft_->Clear(); + } clearCache = true; } this->addMemStat(keyIt); @@ -68,7 +70,7 @@ Variant FastIndexText::Upsert(const Variant &key, IdType id, bool &clearCache } template -void FastIndexText::Delete(const Variant &key, IdType id, StringsHolder &strHolder, bool &clearCache) { +void FastIndexText::Delete(const Variant& key, IdType id, StringsHolder& strHolder, bool& clearCache) { if rx_unlikely (key.Type().Is()) { this->empty_ids_.Unsorted().Erase(id); // ignore result this->isBuilt_ = false; @@ -76,7 +78,9 @@ void FastIndexText::Delete(const Variant &key, IdType id, StringsHolder &strH } auto keyIt = this->idx_map.find(static_cast(key)); - if (keyIt == this->idx_map.end()) return; + if (keyIt == this->idx_map.end()) { + return; + } this->isBuilt_ = false; this->delMemStat(keyIt); @@ -105,12 +109,14 @@ void FastIndexText::Delete(const Variant &key, IdType id, StringsHolder &strH if (this->KeyType().template Is() && this->opts_.GetCollateMode() != CollateNone) { IndexStore>::Delete(key, id, strHolder, clearCache); } - if (this->cache_ft_) this->cache_ft_->Clear(); + if (this->cache_ft_) { + this->cache_ft_->Clear(); + } clearCache = true; } template -IndexMemStat FastIndexText::GetMemStat(const RdxContext &ctx) { +IndexMemStat FastIndexText::GetMemStat(const RdxContext& ctx) { auto ret = IndexUnordered::GetMemStat(ctx); contexted_shared_lock lck(this->mtx_, ctx); @@ -120,15 +126,15 @@ IndexMemStat FastIndexText::GetMemStat(const RdxContext &ctx) { } template -IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTransaction, FtMergeStatuses &&statuses, - FtUseExternStatuses useExternSt, const RdxContext &rdxCtx) { +IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery&& dsl, bool inTransaction, FtMergeStatuses&& statuses, + FtUseExternStatuses useExternSt, const RdxContext& rdxCtx) { fctx->GetData()->extraWordSymbols_ = this->getConfig()->extraWordSymbols; fctx->GetData()->isWordPositions_ = true; MergeData mergeData; switch (holder_->cfg_->optimization) { case FtFastConfig::Optimization::Memory: { - DataHolder *d = dynamic_cast *>(holder_.get()); + DataHolder* d = dynamic_cast*>(holder_.get()); assertrx_throw(d); Selecter selecter{*d, this->Fields().size(), fctx->NeedArea(), holder_->cfg_->maxAreasInDoc}; if (useExternSt == FtUseExternStatuses::No) { @@ -139,7 +145,7 @@ IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTr break; } case FtFastConfig::Optimization::CPU: { - DataHolder *d = dynamic_cast *>(holder_.get()); + DataHolder* d = dynamic_cast*>(holder_.get()); assertrx_throw(d); Selecter selecter{*d, this->Fields().size(), fctx->NeedArea(), holder_->cfg_->maxAreasInDoc}; if (useExternSt == FtUseExternStatuses::No) { @@ -155,7 +161,7 @@ IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTr // convert vids(uniq documents id) to ids (real ids) IdSet::Ptr mergedIds = make_intrusive>(); - auto &holder = *this->holder_; + auto& holder = *this->holder_; if (mergeData.empty()) { return mergedIds; @@ -164,13 +170,15 @@ IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTr const double scalingFactor = mergeData.maxRank > 255 ? 255.0 / mergeData.maxRank : 1.0; const int minRelevancy = getConfig()->minRelevancy * 100 * scalingFactor; size_t releventDocs = 0; - for (auto &vid : mergeData) { - auto &vdoc = holder.vdocs_[vid.id]; + for (auto& vid : mergeData) { + auto& vdoc = holder.vdocs_[vid.id]; if (!vdoc.keyEntry) { continue; } vid.proc *= scalingFactor; - if (vid.proc <= minRelevancy) break; + if (vid.proc <= minRelevancy) { + break; + } assertrx_throw(!vdoc.keyEntry->Unsorted().empty()); cnt += vdoc.keyEntry->Sorted(0).size(); @@ -182,13 +190,13 @@ IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTr if (!fctx->NeedArea()) { if (useExternSt == FtUseExternStatuses::No) { appendMergedIds(mergeData, releventDocs, - [&fctx, &mergedIds](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo &vid) { + [&fctx, &mergedIds](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo& vid) { fctx->Add(ebegin, eend, vid.proc); mergedIds->Append(ebegin, eend, IdSet::Unordered); }); } else { appendMergedIds(mergeData, releventDocs, - [&fctx, &mergedIds, &statuses](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo &vid) { + [&fctx, &mergedIds, &statuses](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo& vid) { fctx->Add(ebegin, eend, vid.proc, statuses.rowIds); mergedIds->Append(ebegin, eend, statuses.rowIds, IdSet::Unordered); }); @@ -196,7 +204,7 @@ IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTr } else { if (useExternSt == FtUseExternStatuses::No) { appendMergedIds(mergeData, releventDocs, - [&fctx, &mergedIds, &mergeData](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo &vid) { + [&fctx, &mergedIds, &mergeData](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo& vid) { assertrx_throw(vid.areaIndex != std::numeric_limits::max()); fctx->Add(ebegin, eend, vid.proc, std::move(mergeData.vectorAreas[vid.areaIndex])); mergedIds->Append(ebegin, eend, IdSet::Unordered); @@ -204,7 +212,7 @@ IdSet::Ptr FastIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery &&dsl, bool inTr } else { appendMergedIds( mergeData, releventDocs, - [&fctx, &mergedIds, &mergeData, &statuses](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo &vid) { + [&fctx, &mergedIds, &mergeData, &statuses](IdSetCRef::iterator ebegin, IdSetCRef::iterator eend, const MergeInfo& vid) { assertrx_throw(vid.areaIndex != std::numeric_limits::max()); fctx->Add(ebegin, eend, vid.proc, statuses.rowIds, std::move(mergeData.vectorAreas[vid.areaIndex])); mergedIds->Append(ebegin, eend, statuses.rowIds, IdSet::Unordered); @@ -253,7 +261,7 @@ void FastIndexText::commitFulltextImpl() { this->holder_->rowId2Vdoc_.clear(); this->holder_->rowId2Vdoc_.reserve(this->holder_->vdocs_.size()); for (size_t i = 0, s = this->holder_->vdocs_.size(); i < s; ++i) { - const auto &vdoc = this->holder_->vdocs_[i]; + const auto& vdoc = this->holder_->vdocs_[i]; if (vdoc.keyEntry) { for (const auto id : vdoc.keyEntry->Unsorted()) { if (static_cast(id) >= this->holder_->rowId2Vdoc_.size()) { @@ -269,11 +277,11 @@ void FastIndexText::commitFulltextImpl() { duration_cast(tm2 - tm0).count(), duration_cast(tm1 - tm0).count(), duration_cast(tm2 - tm1).count()); } - } catch (Error &e) { + } catch (Error& e) { logPrintf(LogError, "FastIndexText::Commit exception: '%s'. Index will be rebuilt on the next query", e.what()); this->holder_->steps.clear(); throw; - } catch (std::exception &e) { + } catch (std::exception& e) { logPrintf(LogError, "FastIndexText::Commit exception: '%s'. Index will be rebuilt on the next query", e.what()); this->holder_->steps.clear(); throw; @@ -286,15 +294,15 @@ void FastIndexText::commitFulltextImpl() { template template -void FastIndexText::buildVdocs(Container &data) { +void FastIndexText::buildVdocs(Container& data) { // buffer strings, for printing non text fields - auto &bufStrs = this->holder_->bufStrs_; + auto& bufStrs = this->holder_->bufStrs_; // array with pointers to docs fields text // Prepare vdocs -> addresable array all docs in the index this->holder_->szCnt = 0; - auto &vdocs = this->holder_->vdocs_; - auto &vdocsTexts = this->holder_->vdocsTexts; + auto& vdocs = this->holder_->vdocs_; + auto& vdocsTexts = this->holder_->vdocsTexts; vdocs.reserve(vdocs.size() + data.size()); vdocsTexts.reserve(data.size()); @@ -329,7 +337,9 @@ void FastIndexText::buildVdocs(Container &data) { #endif if rx_unlikely (getConfig()->logLevel <= LogInfo) { - for (auto &f : vdocsTexts.back()) this->holder_->szCnt += f.first.length(); + for (auto& f : vdocsTexts.back()) { + this->holder_->szCnt += f.first.length(); + } } } if (status == FullRebuild) { @@ -339,11 +349,11 @@ void FastIndexText::buildVdocs(Container &data) { template template -RX_ALWAYS_INLINE void FastIndexText::appendMergedIds(MergeData &mergeData, size_t releventDocs, F &&appender) { - auto &holder = *this->holder_; +RX_ALWAYS_INLINE void FastIndexText::appendMergedIds(MergeData& mergeData, size_t releventDocs, F&& appender) { + auto& holder = *this->holder_; for (size_t i = 0; i < releventDocs; ++i) { - auto &vid = mergeData[i]; - auto &vdoc = holder.vdocs_[vid.id]; + auto& vid = mergeData[i]; + auto& vdoc = holder.vdocs_[vid.id]; if (vdoc.keyEntry) { appender(vdoc.keyEntry->Sorted(0).begin(), vdoc.keyEntry->Sorted(0).end(), vid); } @@ -351,7 +361,7 @@ RX_ALWAYS_INLINE void FastIndexText::appendMergedIds(MergeData &mergeData, si } template -void FastIndexText::initConfig(const FtFastConfig *cfg) { +void FastIndexText::initConfig(const FtFastConfig* cfg) { if (cfg) { this->cfg_.reset(new FtFastConfig(*cfg)); } else { @@ -363,15 +373,15 @@ void FastIndexText::initConfig(const FtFastConfig *cfg) { } template -bool eq_c(Container &c1, Container &c2) { +bool eq_c(Container& c1, Container& c2) { return c1.size() == c2.size() && std::equal(c1.begin(), c1.end(), c2.begin()); } template -void FastIndexText::SetOpts(const IndexOpts &opts) { +void FastIndexText::SetOpts(const IndexOpts& opts) { auto oldCfg = *getConfig(); IndexText::SetOpts(opts); - auto &newCfg = *getConfig(); + auto& newCfg = *getConfig(); if (!eq_c(oldCfg.stopWords, newCfg.stopWords) || oldCfg.stemmers != newCfg.stemmers || oldCfg.maxTypoLen != newCfg.maxTypoLen || oldCfg.enableNumbersSearch != newCfg.enableNumbersSearch || oldCfg.extraWordSymbols != newCfg.extraWordSymbols || @@ -384,24 +394,30 @@ void FastIndexText::SetOpts(const IndexOpts &opts) { this->holder_->Clear(); } this->holder_->status_ = FullRebuild; - if (this->cache_ft_) this->cache_ft_->Clear(); - for (auto &idx : this->idx_map) idx.second.SetVDocID(FtKeyEntryData::ndoc); + if (this->cache_ft_) { + this->cache_ft_->Clear(); + } + for (auto& idx : this->idx_map) { + idx.second.SetVDocID(FtKeyEntryData::ndoc); + } } else { logPrintf(LogInfo, "FulltextIndex config changed, cache cleared"); - if (this->cache_ft_) this->cache_ft_->Clear(); + if (this->cache_ft_) { + this->cache_ft_->Clear(); + } } this->holder_->synonyms_->SetConfig(&newCfg); } template -reindexer::FtPreselectT FastIndexText::FtPreselect(const RdxContext &rdxCtx) { +reindexer::FtPreselectT FastIndexText::FtPreselect(const RdxContext& rdxCtx) { this->build(rdxCtx); return FtMergeStatuses{FtMergeStatuses::Statuses(holder_->vdocs_.size(), FtMergeStatuses::kExcluded), std::vector(holder_->rowId2Vdoc_.size(), false), &holder_->rowId2Vdoc_}; } -std::unique_ptr FastIndexText_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +std::unique_ptr FastIndexText_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { switch (idef.Type()) { case IndexFastFT: return std::make_unique>>(idef, std::move(payloadType), std::move(fields), diff --git a/cpp_src/core/index/indextext/fastindextext.h b/cpp_src/core/index/indextext/fastindextext.h index ae6f74d5b..054afa650 100644 --- a/cpp_src/core/index/indextext/fastindextext.h +++ b/cpp_src/core/index/indextext/fastindextext.h @@ -18,7 +18,9 @@ class FastIndexText : public IndexText { FastIndexText(const FastIndexText& other) : Base(other) { initConfig(other.getConfig()); - for (auto& idx : this->idx_map) idx.second.SetVDocID(FtKeyEntryData::ndoc); + for (auto& idx : this->idx_map) { + idx.second.SetVDocID(FtKeyEntryData::ndoc); + } } FastIndexText(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, const NamespaceCacheConfigData& cacheCfg) diff --git a/cpp_src/core/index/indextext/fieldsgetter.h b/cpp_src/core/index/indextext/fieldsgetter.h index 7eccdc7da..300fe4cdc 100644 --- a/cpp_src/core/index/indextext/fieldsgetter.h +++ b/cpp_src/core/index/indextext/fieldsgetter.h @@ -7,10 +7,12 @@ namespace reindexer { class FieldsGetter { public: - FieldsGetter(const FieldsSet &fields, const PayloadType &plt, KeyValueType type) : fields_(fields), plt_(plt), type_(type) {} + FieldsGetter(const FieldsSet& fields, const PayloadType& plt, KeyValueType type) : fields_(fields), plt_(plt), type_(type) {} - RVector, 8> getDocFields(const key_string &doc, std::vector> &) { - if (!utf8::is_valid(doc->cbegin(), doc->cend())) throw Error(errParams, "Invalid UTF8 string in FullText index"); + RVector, 8> getDocFields(const key_string& doc, std::vector>&) { + if (!utf8::is_valid(doc->cbegin(), doc->cend())) { + throw Error(errParams, "Invalid UTF8 string in FullText index"); + } return {{std::string_view(*doc.get()), 0}}; } @@ -18,8 +20,8 @@ class FieldsGetter { VariantArray krefs; // Specific implemetation for composite index - RVector, 8> getDocFields(const PayloadValue &doc, - std::vector> &strsBuf) { + RVector, 8> getDocFields(const PayloadValue& doc, + std::vector>& strsBuf) { ConstPayload pl(plt_, doc); uint32_t fieldPos = 0; @@ -36,9 +38,9 @@ class FieldsGetter { } else { pl.Get(field, krefs); } - for (const Variant &kref : krefs) { + for (const Variant& kref : krefs) { if (!kref.Type().Is()) { - auto &str = strsBuf.emplace_back(std::make_unique(kref.As())); + auto& str = strsBuf.emplace_back(std::make_unique(kref.As())); ret.emplace_back(*str, fieldPos); } else { const std::string_view stringRef(kref); @@ -54,8 +56,8 @@ class FieldsGetter { } private: - const FieldsSet &fields_; - const PayloadType &plt_; + const FieldsSet& fields_; + const PayloadType& plt_; KeyValueType type_; }; diff --git a/cpp_src/core/index/indextext/fuzzyindextext.cc b/cpp_src/core/index/indextext/fuzzyindextext.cc index 8d875ba58..a3ea56d02 100644 --- a/cpp_src/core/index/indextext/fuzzyindextext.cc +++ b/cpp_src/core/index/indextext/fuzzyindextext.cc @@ -22,12 +22,16 @@ IdSet::Ptr FuzzyIndexText::Select(FtCtx::Ptr fctx, FtDSLQuery&& dsl, bool inT size_t counter = 0; for (auto it = result.data_->begin(); it != result.data_->end(); ++it, ++counter) { it->proc_ *= coof; - if (it->proc_ < getConfig()->minOkProc) continue; + if (it->proc_ < getConfig()->minOkProc) { + continue; + } assertrx(it->id_ < this->vdocs_.size()); const auto& id_set = this->vdocs_[it->id_].keyEntry->Sorted(0); fctx->Add(id_set.begin(), id_set.end(), it->proc_); mergedIds->Append(id_set.begin(), id_set.end(), IdSet::Unordered); - if ((counter & 0xFF) == 0 && !inTransaction) ThrowOnCancel(rdxCtx); + if ((counter & 0xFF) == 0 && !inTransaction) { + ThrowOnCancel(rdxCtx); + } } return mergedIds; diff --git a/cpp_src/core/index/indextext/indextext.cc b/cpp_src/core/index/indextext/indextext.cc index dbf99e8d1..6ba1425ed 100644 --- a/cpp_src/core/index/indextext/indextext.cc +++ b/cpp_src/core/index/indextext/indextext.cc @@ -8,7 +8,7 @@ namespace reindexer { template -IndexText::IndexText(const IndexText &other) +IndexText::IndexText(const IndexText& other) : IndexUnordered(other), cache_ft_(std::make_unique(other.cacheMaxSize_, other.hitsToCache_)), cacheMaxSize_(other.cacheMaxSize_), @@ -22,7 +22,7 @@ void IndexText::initSearchers() { size_t jsonPathIdx = 0; if (this->payloadType_) { - const auto &fields = this->Fields(); + const auto& fields = this->Fields(); for (unsigned i = 0, s = fields.size(); i < s; i++) { auto fieldIdx = fields[i]; if (fieldIdx == IndexValueType::SetByJsonPath) { @@ -43,7 +43,7 @@ void IndexText::initSearchers() { } template -void IndexText::SetOpts(const IndexOpts &opts) { +void IndexText::SetOpts(const IndexOpts& opts) { std::string oldCfg = this->opts_.config; this->opts_ = opts; @@ -60,7 +60,7 @@ void IndexText::SetOpts(const IndexOpts &opts) { } template -void IndexText::ReconfigureCache(const NamespaceCacheConfigData &cacheCfg) { +void IndexText::ReconfigureCache(const NamespaceCacheConfigData& cacheCfg) { if (cacheMaxSize_ != cacheCfg.ftIdxCacheSize || hitsToCache_ != cacheCfg.ftIdxHitsToCache) { cacheMaxSize_ = cacheCfg.ftIdxCacheSize; hitsToCache_ = cacheCfg.ftIdxHitsToCache; @@ -72,7 +72,7 @@ void IndexText::ReconfigureCache(const NamespaceCacheConfigData &cacheCfg) { } template -FtCtx::Ptr IndexText::prepareFtCtx(const BaseFunctionCtx::Ptr &ctx) { +FtCtx::Ptr IndexText::prepareFtCtx(const BaseFunctionCtx::Ptr& ctx) { FtCtx::Ptr ftctx = reindexer::static_ctx_pointer_cast(ctx); if rx_unlikely (!ftctx) { throw Error(errParams, "Full text index (%s) may not be used without context", Index::Name()); @@ -82,7 +82,7 @@ FtCtx::Ptr IndexText::prepareFtCtx(const BaseFunctionCtx::Ptr &ctx) { } template -void IndexText::build(const RdxContext &rdxCtx) { +void IndexText::build(const RdxContext& rdxCtx) { smart_lock lck(mtx_, rdxCtx); if (!this->isBuilt_) { // non atomic upgrade mutex to unique @@ -96,8 +96,8 @@ void IndexText::build(const RdxContext &rdxCtx) { // Generic implemetation for string index template -SelectKeyResults IndexText::SelectKey(const VariantArray &keys, CondType condition, SortType, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &ctx, const RdxContext &rdxCtx) { +SelectKeyResults IndexText::SelectKey(const VariantArray& keys, CondType condition, SortType, Index::SelectOpts opts, + const BaseFunctionCtx::Ptr& ctx, const RdxContext& rdxCtx) { const auto indexWard(rdxCtx.BeforeIndexWork()); if rx_unlikely (keys.size() < 1 || (condition != CondEq && condition != CondSet)) { throw Error(errParams, "Full text index (%s) support only EQ or SET condition with 1 or 2 parameter", Index::Name()); @@ -122,7 +122,7 @@ SelectKeyResults IndexText::SelectKey(const VariantArray &keys, CondType cond } template -SelectKeyResults IndexText::resultFromCache(const VariantArray &keys, FtIdSetCache::Iterator &&it, FtCtx::Ptr &ftctx) { +SelectKeyResults IndexText::resultFromCache(const VariantArray& keys, FtIdSetCache::Iterator&& it, FtCtx::Ptr& ftctx) { if rx_unlikely (cfg_->logLevel >= LogInfo) { logPrintf(LogInfo, "Get search results for '%s' in '%s' from cache", keys[0].As(), this->payloadType_ ? this->payloadType_->Name() : ""); @@ -133,9 +133,9 @@ SelectKeyResults IndexText::resultFromCache(const VariantArray &keys, FtIdSet } template -SelectKeyResults IndexText::doSelectKey(const VariantArray &keys, const std::optional &ckey, - FtMergeStatuses &&mergeStatuses, FtUseExternStatuses useExternSt, bool inTransaction, - FtCtx::Ptr ftctx, const RdxContext &rdxCtx) { +SelectKeyResults IndexText::doSelectKey(const VariantArray& keys, const std::optional& ckey, + FtMergeStatuses&& mergeStatuses, FtUseExternStatuses useExternSt, bool inTransaction, + FtCtx::Ptr ftctx, const RdxContext& rdxCtx) { if rx_unlikely (cfg_->logLevel >= LogInfo) { logPrintf(LogInfo, "Searching for '%s' in '%s' %s", keys[0].As(), this->payloadType_ ? this->payloadType_->Name() : "", ckey ? "(will cache)" : ""); @@ -150,12 +150,12 @@ SelectKeyResults IndexText::doSelectKey(const VariantArray &keys, const std:: if (mergedIds) { bool need_put = (useExternSt == FtUseExternStatuses::No) && ckey.has_value(); if (ftctx->NeedArea() && need_put && mergedIds->size()) { - auto config = dynamic_cast(cfg_.get()); + auto config = dynamic_cast(cfg_.get()); if (config && config->maxTotalAreasToCache >= 0) { - auto &d = *ftctx->GetData(); + auto& d = *ftctx->GetData(); size_t totalAreas = 0; assertrx_throw(d.holders_.has_value()); - for (auto &area : d.holders_.value()) { + for (auto& area : d.holders_.value()) { totalAreas += d.area_[area.second].GetAreasCount(); } if (totalAreas > unsigned(config->maxTotalAreasToCache)) { @@ -166,10 +166,10 @@ SelectKeyResults IndexText::doSelectKey(const VariantArray &keys, const std:: if (need_put && mergedIds->size()) { // This areas will be shared via cache, so lazy commit may race auto dPtr = ftctx->GetData(); - auto &d = *dPtr; + auto& d = *dPtr; if (d.holders_.has_value()) { - for (auto &area : d.holders_.value()) { - if (auto &aData = d.area_[area.second]; !aData.IsCommited()) { + for (auto& area : d.holders_.value()) { + if (auto& aData = d.area_[area.second]; !aData.IsCommited()) { aData.Commit(); } } @@ -183,8 +183,8 @@ SelectKeyResults IndexText::doSelectKey(const VariantArray &keys, const std:: } template -SelectKeyResults IndexText::SelectKey(const VariantArray &keys, CondType condition, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &ctx, FtPreselectT &&preselect, const RdxContext &rdxCtx) { +SelectKeyResults IndexText::SelectKey(const VariantArray& keys, CondType condition, Index::SelectOpts opts, + const BaseFunctionCtx::Ptr& ctx, FtPreselectT&& preselect, const RdxContext& rdxCtx) { const auto indexWard(rdxCtx.BeforeIndexWork()); if rx_unlikely (keys.size() < 1 || (condition != CondEq && condition != CondSet)) { throw Error(errParams, "Full text index (%s) support only EQ or SET condition with 1 or 2 parameter", Index::Name()); diff --git a/cpp_src/core/index/indextext/indextext.h b/cpp_src/core/index/indextext/indextext.h index 9580d50c3..65f54d7b0 100644 --- a/cpp_src/core/index/indextext/indextext.h +++ b/cpp_src/core/index/indextext/indextext.h @@ -52,7 +52,9 @@ class IndexText : public IndexUnordered { } void ClearCache() override { Base::ClearCache(); - if (cache_ft_) cache_ft_->Clear(); + if (cache_ft_) { + cache_ft_->Clear(); + } } void ClearCache(const std::bitset& s) override { Base::ClearCache(s); } void MarkBuilt() noexcept override { assertrx(0); } diff --git a/cpp_src/core/index/indexunordered.cc b/cpp_src/core/index/indexunordered.cc index bfd90c2dd..18af49bea 100644 --- a/cpp_src/core/index/indexunordered.cc +++ b/cpp_src/core/index/indexunordered.cc @@ -17,8 +17,8 @@ namespace reindexer { constexpr int kMaxIdsForDistinct = 500; template -IndexUnordered::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), @@ -27,81 +27,81 @@ IndexUnordered::IndexUnordered(const IndexDef &idef, PayloadType &&payloadTyp } template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(idef.opts_.collateOpts_), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(idef.opts_.collateOpts_), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(idef.opts_.collateOpts_), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(idef.opts_.collateOpts_), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(idef.opts_.collateOpts_), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(PayloadType{Base::GetPayloadType()}, FieldsSet{Base::Fields()}), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, - FieldsSet &&fields, const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, + FieldsSet&& fields, const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(PayloadType{Base::GetPayloadType()}, FieldsSet{Base::Fields()}), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, - FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, + FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(PayloadType{Base::GetPayloadType()}, FieldsSet{Base::Fields()}), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(PayloadType{Base::GetPayloadType()}, FieldsSet{Base::Fields()}), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), hitsToCache_(cacheCfg.idxIdsetHitsToCache) {} template <> -IndexUnordered>::IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) +IndexUnordered>::IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) : Base(idef, std::move(payloadType), std::move(fields)), idx_map(PayloadType{Base::GetPayloadType()}, FieldsSet{Base::Fields()}), cacheMaxSize_(cacheCfg.idxIdsetCacheSize), @@ -117,7 +117,7 @@ bool IndexUnordered::HoldsStrings() const noexcept { } template -IndexUnordered::IndexUnordered(const IndexUnordered &other) +IndexUnordered::IndexUnordered(const IndexUnordered& other) : Base(other), idx_map(other.idx_map), cache_(nullptr), @@ -127,29 +127,29 @@ IndexUnordered::IndexUnordered(const IndexUnordered &other) tracker_(other.tracker_) {} template -size_t heap_size(const key_type & /*kt*/) { +size_t heap_size(const key_type& /*kt*/) { return 0; } template <> -size_t heap_size(const key_string &kt) { +size_t heap_size(const key_string& kt) { return kt->heap_size() + sizeof(*kt.get()); } template <> -size_t heap_size(const key_string_with_hash &kt) { +size_t heap_size(const key_string_with_hash& kt) { return kt->heap_size() + sizeof(*kt.get()); } struct DeepClean { template - void operator()(T &v) const { + void operator()(T& v) const { free_node(v.first); free_node(v.second); } template - void free_node(T &v) const { + void free_node(T& v) const { if constexpr (!std::is_const_v) { v = T{}; } @@ -171,7 +171,7 @@ void IndexUnordered::delMemStat(typename T::iterator it) { } template -Variant IndexUnordered::Upsert(const Variant &key, IdType id, bool &clearCache) { +Variant IndexUnordered::Upsert(const Variant& key, IdType id, bool& clearCache) { // reset cache if (key.Type().Is()) { // TODO maybe error or default value if the index is not sparse if (this->empty_ids_.Unsorted().Add(id, IdSet::Auto, this->sortedIdxCount_)) { @@ -203,7 +203,7 @@ Variant IndexUnordered::Upsert(const Variant &key, IdType id, bool &clearCach } template -void IndexUnordered::Delete(const Variant &key, IdType id, StringsHolder &strHolder, bool &clearCache) { +void IndexUnordered::Delete(const Variant& key, IdType id, StringsHolder& strHolder, bool& clearCache) { if (key.Type().Is()) { this->empty_ids_.Unsorted().Erase(id); // ignore result this->isBuilt_ = false; @@ -251,8 +251,8 @@ void IndexUnordered::Delete(const Variant &key, IdType id, StringsHolder &str // WARNING: 'keys' is a key for LRUCache and in some cases (for ordered indexes, for example) can contain values, // which are not correspond to the initial values from queries conditions template -bool IndexUnordered::tryIdsetCache(const VariantArray &keys, CondType condition, SortType sortId, - const std::function &selector, SelectKeyResult &res) { +bool IndexUnordered::tryIdsetCache(const VariantArray& keys, CondType condition, SortType sortId, + const std::function& selector, SelectKeyResult& res) { size_t idsCount; if (!cache_ || IsComposite(this->Type())) { selector(res, idsCount); @@ -282,10 +282,12 @@ bool IndexUnordered::tryIdsetCache(const VariantArray &keys, CondType conditi } template -SelectKeyResults IndexUnordered::SelectKey(const VariantArray &keys, CondType condition, SortType sortId, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &funcCtx, const RdxContext &rdxCtx) { +SelectKeyResults IndexUnordered::SelectKey(const VariantArray& keys, CondType condition, SortType sortId, Index::SelectOpts opts, + const BaseFunctionCtx::Ptr& funcCtx, const RdxContext& rdxCtx) { const auto indexWard(rdxCtx.BeforeIndexWork()); - if (opts.forceComparator) return Base::SelectKey(keys, condition, sortId, opts, funcCtx, rdxCtx); + if (opts.forceComparator) { + return Base::SelectKey(keys, condition, sortId, opts, funcCtx, rdxCtx); + } SelectKeyResult res; @@ -300,15 +302,15 @@ SelectKeyResults IndexUnordered::SelectKey(const VariantArray &keys, CondType case CondEq: case CondSet: { struct { - T *i_map; - const VariantArray &keys; + T* i_map; + const VariantArray& keys; SortType sortId; Index::SelectOpts opts; bool isSparse; } ctx = {&this->idx_map, keys, sortId, opts, this->opts_.IsSparse()}; bool selectorWasSkipped = false; // should return true, if fallback to comparator required - auto selector = [&ctx, &selectorWasSkipped](SelectKeyResult &res, size_t &idsCount) -> bool { + auto selector = [&ctx, &selectorWasSkipped](SelectKeyResult& res, size_t& idsCount) -> bool { idsCount = 0; // Skip this index if there are some other indexes with potentially higher selectivity if (!ctx.opts.distinct && ctx.keys.size() > 1 && 8 * ctx.keys.size() > size_t(ctx.opts.maxIterations) && @@ -317,7 +319,7 @@ SelectKeyResults IndexUnordered::SelectKey(const VariantArray &keys, CondType return true; } res.reserve(ctx.keys.size()); - for (const auto &key : ctx.keys) { + for (const auto& key : ctx.keys) { auto keyIt = ctx.i_map->find(static_cast(key)); if (keyIt != ctx.i_map->end()) { res.emplace_back(keyIt->second, ctx.sortId); @@ -328,7 +330,9 @@ SelectKeyResults IndexUnordered::SelectKey(const VariantArray &keys, CondType res.deferedExplicitSort = SelectKeyResult::IsGenericSortRecommended(res.size(), idsCount, idsCount); // avoid comparator for sparse index - if (ctx.isSparse || !ctx.opts.itemsCountInNamespace) return false; + if (ctx.isSparse || !ctx.opts.itemsCountInNamespace) { + return false; + } // Check selectivity: // if ids count too much (more than maxSelectivityPercentForIdset() of namespace), // and index not optimized, or we have >4 other conditions @@ -371,7 +375,7 @@ SelectKeyResults IndexUnordered::SelectKey(const VariantArray &keys, CondType if (opts.distinct && this->idx_map.size() < kMaxIdsForDistinct) { // TODO change to more clever condition // Get set of any keys res.reserve(this->idx_map.size()); - for (auto &keyIt : this->idx_map) { + for (auto& keyIt : this->idx_map) { res.emplace_back(keyIt.second, sortId); } break; @@ -394,15 +398,19 @@ template void IndexUnordered::Commit() { this->empty_ids_.Unsorted().Commit(); - if (!cache_) cache_.reset(new IdSetCache(cacheMaxSize_, hitsToCache_)); + if (!cache_) { + cache_.reset(new IdSetCache(cacheMaxSize_, hitsToCache_)); + } - if (!tracker_.isUpdated()) return; + if (!tracker_.isUpdated()) { + return; + } logPrintf(LogTrace, "IndexUnordered::Commit (%s) %d uniq keys, %d empty, %s", this->name_, this->idx_map.size(), this->empty_ids_.Unsorted().size(), tracker_.isCompleteUpdated() ? "complete" : "partial"); if (tracker_.isCompleteUpdated()) { - for (auto &keyIt : this->idx_map) { + for (auto& keyIt : this->idx_map) { keyIt.second.Unsorted().Commit(); assertrx(keyIt.second.Unsorted().size()); } @@ -413,11 +421,11 @@ void IndexUnordered::Commit() { } template -void IndexUnordered::UpdateSortedIds(const UpdateSortedContext &ctx) { +void IndexUnordered::UpdateSortedIds(const UpdateSortedContext& ctx) { logPrintf(LogTrace, "IndexUnordered::UpdateSortedIds (%s) %d uniq keys, %d empty", this->name_, this->idx_map.size(), this->empty_ids_.Unsorted().size()); // For all keys in index - for (auto &keyIt : this->idx_map) { + for (auto& keyIt : this->idx_map) { keyIt.second.UpdateSortedIds(ctx); } @@ -428,15 +436,19 @@ template void IndexUnordered::SetSortedIdxCount(int sortedIdxCount) { if (this->sortedIdxCount_ != sortedIdxCount) { this->sortedIdxCount_ = sortedIdxCount; - for (auto &keyIt : idx_map) keyIt.second.Unsorted().ReserveForSorted(this->sortedIdxCount_); + for (auto& keyIt : idx_map) { + keyIt.second.Unsorted().ReserveForSorted(this->sortedIdxCount_); + } } } template -IndexMemStat IndexUnordered::GetMemStat(const RdxContext &ctx) { +IndexMemStat IndexUnordered::GetMemStat(const RdxContext& ctx) { IndexMemStat ret = Base::GetMemStat(ctx); ret.uniqKeysCount = idx_map.size(); - if (cache_) ret.idsetCache = cache_->GetMemStat(); + if (cache_) { + ret.idsetCache = cache_->GetMemStat(); + } ret.trackedUpdatesCount = tracker_.updatesSize(); ret.trackedUpdatesBuckets = tracker_.updatesBuckets(); ret.trackedUpdatesSize = tracker_.allocated(); @@ -446,7 +458,7 @@ IndexMemStat IndexUnordered::GetMemStat(const RdxContext &ctx) { template template -void IndexUnordered::dump(S &os, std::string_view step, std::string_view offset) const { +void IndexUnordered::dump(S& os, std::string_view step, std::string_view offset) const { std::string newOffset{offset}; newOffset += step; os << "{\n" << newOffset << ": "; @@ -456,7 +468,9 @@ void IndexUnordered::dump(S &os, std::string_view step, std::string_view offs std::string secondOffset{newOffset}; secondOffset += step; for (auto b = idx_map.begin(), it = b, e = idx_map.end(); it != e; ++it) { - if (it != b) os << ','; + if (it != b) { + os << ','; + } os << '\n' << secondOffset << '{' << it->first << ": "; it->second.Dump(os, step, secondOffset); os << '}'; @@ -475,7 +489,7 @@ void IndexUnordered::dump(S &os, std::string_view step, std::string_view offs } template -void IndexUnordered::AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue &q) { +void IndexUnordered::AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue& q) { if constexpr (Base::template HasAddTask::value) { idx_map.add_destroy_task(&q); } @@ -483,7 +497,7 @@ void IndexUnordered::AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue } template -void IndexUnordered::ReconfigureCache(const NamespaceCacheConfigData &cacheCfg) { +void IndexUnordered::ReconfigureCache(const NamespaceCacheConfigData& cacheCfg) { if (cacheMaxSize_ != cacheCfg.idxIdsetCacheSize || hitsToCache_ != cacheCfg.idxIdsetHitsToCache) { cacheMaxSize_ = cacheCfg.idxIdsetCacheSize; hitsToCache_ = cacheCfg.idxIdsetHitsToCache; @@ -494,8 +508,8 @@ void IndexUnordered::ReconfigureCache(const NamespaceCacheConfigData &cacheCf } template -static std::unique_ptr IndexUnordered_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +static std::unique_ptr IndexUnordered_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { switch (idef.Type()) { case IndexIntHash: return std::make_unique>>(idef, std::move(payloadType), std::move(fields), @@ -533,8 +547,8 @@ static std::unique_ptr IndexUnordered_New(const IndexDef &idef, PayloadTy } // NOLINTBEGIN(*cplusplus.NewDeleteLeaks) -std::unique_ptr IndexUnordered_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +std::unique_ptr IndexUnordered_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { return (idef.opts_.IsPK() || idef.opts_.IsDense()) ? IndexUnordered_New(idef, std::move(payloadType), std::move(fields), cacheCfg) : IndexUnordered_New(idef, std::move(payloadType), std::move(fields), cacheCfg); diff --git a/cpp_src/core/index/indexunordered.h b/cpp_src/core/index/indexunordered.h index 541fef791..418931c9e 100644 --- a/cpp_src/core/index/indexunordered.h +++ b/cpp_src/core/index/indexunordered.h @@ -26,36 +26,40 @@ class IndexUnordered : public IndexStore> { typename T::key_type>::type>::type>::type; using key_type = StoreIndexKeyType; - IndexUnordered(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, const NamespaceCacheConfigData &cacheCfg); - IndexUnordered(const IndexUnordered &other); + IndexUnordered(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, const NamespaceCacheConfigData& cacheCfg); + IndexUnordered(const IndexUnordered& other); - Variant Upsert(const Variant &key, IdType id, bool &chearCache) override; - void Delete(const Variant &key, IdType id, StringsHolder &, bool &chearCache) override; - SelectKeyResults SelectKey(const VariantArray &keys, CondType cond, SortType stype, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &ctx, const RdxContext &) override; + Variant Upsert(const Variant& key, IdType id, bool& chearCache) override; + void Delete(const Variant& key, IdType id, StringsHolder&, bool& chearCache) override; + SelectKeyResults SelectKey(const VariantArray& keys, CondType cond, SortType stype, Index::SelectOpts opts, + const BaseFunctionCtx::Ptr& ctx, const RdxContext&) override; void Commit() override; - void UpdateSortedIds(const UpdateSortedContext &) override; + void UpdateSortedIds(const UpdateSortedContext&) override; std::unique_ptr Clone() const override { return std::make_unique>(*this); } - IndexMemStat GetMemStat(const RdxContext &) override; + IndexMemStat GetMemStat(const RdxContext&) override; size_t Size() const noexcept override final { return idx_map.size(); } void SetSortedIdxCount(int sortedIdxCount) override; bool HoldsStrings() const noexcept override; void DestroyCache() override { cache_.reset(); } void ClearCache() override { - if (cache_) cache_->Clear(); + if (cache_) { + cache_->Clear(); + } } - void ClearCache(const std::bitset &s) override { - if (cache_) cache_->ClearSorted(s); + void ClearCache(const std::bitset& s) override { + if (cache_) { + cache_->ClearSorted(s); + } } - void Dump(std::ostream &os, std::string_view step = " ", std::string_view offset = "") const override { dump(os, step, offset); } + void Dump(std::ostream& os, std::string_view step = " ", std::string_view offset = "") const override { dump(os, step, offset); } void EnableUpdatesCountingMode(bool val) noexcept override { tracker_.enableCountingMode(val); } - void AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue &) override; - void ReconfigureCache(const NamespaceCacheConfigData &cacheCfg) override; + void AddDestroyTask(tsl::detail_sparse_hash::ThreadTaskQueue&) override; + void ReconfigureCache(const NamespaceCacheConfigData& cacheCfg) override; protected: - bool tryIdsetCache(const VariantArray &keys, CondType condition, SortType sortId, - const std::function &selector, SelectKeyResult &res); + bool tryIdsetCache(const VariantArray& keys, CondType condition, SortType sortId, + const std::function& selector, SelectKeyResult& res); void addMemStat(typename T::iterator it); void delMemStat(typename T::iterator it); @@ -72,12 +76,12 @@ class IndexUnordered : public IndexStore> { private: template - void dump(S &os, std::string_view step, std::string_view offset) const; + void dump(S& os, std::string_view step, std::string_view offset) const; }; constexpr unsigned maxSelectivityPercentForIdset() noexcept { return 30u; } -std::unique_ptr IndexUnordered_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg); +std::unique_ptr IndexUnordered_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg); } // namespace reindexer diff --git a/cpp_src/core/index/keyentry.h b/cpp_src/core/index/keyentry.h index 169e233c1..1e56711f4 100644 --- a/cpp_src/core/index/keyentry.h +++ b/cpp_src/core/index/keyentry.h @@ -65,11 +65,15 @@ class KeyEntry { if (ids_.size() != 0) { unsigned sortId = 0; while (ids_.capacity() >= ids_.size() * (sortId + 1)) { - if (sortId != 0) os << ", "; + if (sortId != 0) { + os << ", "; + } os << '['; const auto sorted = Sorted(sortId); for (auto b = sorted.begin(), it = b, e = sorted.end(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << *it; } os << ']'; @@ -77,7 +81,9 @@ class KeyEntry { } } os << ']'; - if (!newOffset.empty()) os << '\n' << offset; + if (!newOffset.empty()) { + os << '\n' << offset; + } os << '}'; } diff --git a/cpp_src/core/index/payload_map.h b/cpp_src/core/index/payload_map.h index 7a16a1cbb..d5a846524 100644 --- a/cpp_src/core/index/payload_map.h +++ b/cpp_src/core/index/payload_map.h @@ -12,13 +12,13 @@ namespace reindexer { class PayloadValueWithHash : public PayloadValue { public: PayloadValueWithHash() noexcept : PayloadValue() {} - PayloadValueWithHash(PayloadValue &&pv, const PayloadType &pt, const FieldsSet &fields) - : PayloadValue(std::move(pv)), hash_(ConstPayload(pt, *static_cast(this)).GetHash(fields)) {} - PayloadValueWithHash(const PayloadValueWithHash &o) noexcept : PayloadValue(o), hash_(o.hash_) {} - PayloadValueWithHash(PayloadValueWithHash &&o) noexcept : PayloadValue(std::move(o)), hash_(o.hash_) {} - PayloadValueWithHash &operator=(PayloadValueWithHash &&o) noexcept { + PayloadValueWithHash(PayloadValue&& pv, const PayloadType& pt, const FieldsSet& fields) + : PayloadValue(std::move(pv)), hash_(ConstPayload(pt, *static_cast(this)).GetHash(fields)) {} + PayloadValueWithHash(const PayloadValueWithHash& o) noexcept : PayloadValue(o), hash_(o.hash_) {} + PayloadValueWithHash(PayloadValueWithHash&& o) noexcept : PayloadValue(std::move(o)), hash_(o.hash_) {} + PayloadValueWithHash& operator=(PayloadValueWithHash&& o) noexcept { hash_ = o.hash_; - return static_cast(PayloadValue::operator=(std::move(o))); + return static_cast(PayloadValue::operator=(std::move(o))); } uint32_t GetHash() const noexcept { return hash_; } @@ -31,15 +31,15 @@ class equal_composite { using is_transparent = void; template - equal_composite(PT &&type, FS &&fields) : type_(std::forward(type)), fields_(std::forward(fields)) { + equal_composite(PT&& type, FS&& fields) : type_(std::forward(type)), fields_(std::forward(fields)) { assertrx_dbg(type_); } - bool operator()(const PayloadValue &lhs, const PayloadValue &rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } - bool operator()(const PayloadValueWithHash &lhs, const PayloadValueWithHash &rhs) const { + bool operator()(const PayloadValue& lhs, const PayloadValue& rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } + bool operator()(const PayloadValueWithHash& lhs, const PayloadValueWithHash& rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } - bool operator()(const PayloadValueWithHash &lhs, const PayloadValue &rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } - bool operator()(const PayloadValue &lhs, const PayloadValueWithHash &rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } + bool operator()(const PayloadValueWithHash& lhs, const PayloadValue& rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } + bool operator()(const PayloadValue& lhs, const PayloadValueWithHash& rhs) const { return ConstPayload(type_, lhs).IsEQ(rhs, fields_); } private: PayloadType type_; @@ -48,10 +48,10 @@ class equal_composite { class equal_composite_ref { public: - equal_composite_ref(const PayloadType &type, const FieldsSet &fields) noexcept : type_(type), fields_(fields) { + equal_composite_ref(const PayloadType& type, const FieldsSet& fields) noexcept : type_(type), fields_(fields) { assertrx_dbg(type_.get()); } - bool operator()(const PayloadValue &lhs, const PayloadValue &rhs) const { + bool operator()(const PayloadValue& lhs, const PayloadValue& rhs) const { assertrx_dbg(!lhs.IsFree()); assertrx_dbg(!rhs.IsFree()); return ConstPayload(type_, lhs).IsEQ(rhs, fields_); @@ -65,11 +65,11 @@ class equal_composite_ref { class hash_composite { public: template - hash_composite(PT &&type, FS &&fields) : type_(std::forward(type)), fields_(std::forward(fields)) { + hash_composite(PT&& type, FS&& fields) : type_(std::forward(type)), fields_(std::forward(fields)) { assertrx_dbg(type_); } - size_t operator()(const PayloadValueWithHash &s) const noexcept { return s.GetHash(); } - size_t operator()(const PayloadValue &s) const { return ConstPayload(type_, s).GetHash(fields_); } + size_t operator()(const PayloadValueWithHash& s) const noexcept { return s.GetHash(); } + size_t operator()(const PayloadValue& s) const { return ConstPayload(type_, s).GetHash(fields_); } private: PayloadType type_; @@ -78,10 +78,10 @@ class hash_composite { class hash_composite_ref { public: - hash_composite_ref(const PayloadType &type, const FieldsSet &fields) noexcept : type_(type), fields_(fields) { + hash_composite_ref(const PayloadType& type, const FieldsSet& fields) noexcept : type_(type), fields_(fields) { assertrx_dbg(type_.get()); } - size_t operator()(const PayloadValue &s) const { return ConstPayload(type_, s).GetHash(fields_); } + size_t operator()(const PayloadValue& s) const { return ConstPayload(type_, s).GetHash(fields_); } private: std::reference_wrapper type_; @@ -90,10 +90,10 @@ class hash_composite_ref { class less_composite { public: - less_composite(PayloadType &&type, FieldsSet &&fields) noexcept : type_(std::move(type)), fields_(std::move(fields)) { + less_composite(PayloadType&& type, FieldsSet&& fields) noexcept : type_(std::move(type)), fields_(std::move(fields)) { assertrx_dbg(type_); } - bool operator()(const PayloadValue &lhs, const PayloadValue &rhs) const { + bool operator()(const PayloadValue& lhs, const PayloadValue& rhs) const { assertrx_dbg(!lhs.IsFree()); assertrx_dbg(!rhs.IsFree()); return (ConstPayload(type_, lhs).Compare(rhs, fields_) == ComparationResult::Lt); @@ -106,10 +106,10 @@ class less_composite { class less_composite_ref { public: - less_composite_ref(const PayloadType &type, const FieldsSet &fields) noexcept : type_(type), fields_(fields) { + less_composite_ref(const PayloadType& type, const FieldsSet& fields) noexcept : type_(type), fields_(fields) { assertrx_dbg(type_.get()); } - bool operator()(const PayloadValue &lhs, const PayloadValue &rhs) const { + bool operator()(const PayloadValue& lhs, const PayloadValue& rhs) const { assertrx_dbg(!lhs.IsFree()); assertrx_dbg(!rhs.IsFree()); return (ConstPayload(type_, lhs).Compare(rhs, fields_) == ComparationResult::Lt); @@ -126,30 +126,38 @@ class payload_str_fields_helper; template <> class payload_str_fields_helper { protected: - payload_str_fields_helper(PayloadType &&payloadType, const FieldsSet &fields) : payload_type_(std::move(payloadType)) { + payload_str_fields_helper(PayloadType&& payloadType, const FieldsSet& fields) : payload_type_(std::move(payloadType)) { if (fields.getTagsPathsLength() || fields.getJsonPathsLength()) { str_fields_.push_back(0); } for (int f : payload_type_.StrFields()) { - if (fields.contains(f)) str_fields_.push_back(f); + if (fields.contains(f)) { + str_fields_.push_back(f); + } } } - payload_str_fields_helper(const payload_str_fields_helper &) = default; - payload_str_fields_helper(payload_str_fields_helper &&) = default; + payload_str_fields_helper(const payload_str_fields_helper&) = default; + payload_str_fields_helper(payload_str_fields_helper&&) = default; - inline void add_ref(PayloadValue &pv) const { + inline void add_ref(PayloadValue& pv) const { Payload pl(payload_type_, pv); - for (int f : str_fields_) pl.AddRefStrings(f); + for (int f : str_fields_) { + pl.AddRefStrings(f); + } } - inline void release(PayloadValue &pv) const { + inline void release(PayloadValue& pv) const { Payload pl(payload_type_, pv); - for (int f : str_fields_) pl.ReleaseStrings(f); + for (int f : str_fields_) { + pl.ReleaseStrings(f); + } } - inline void move_strings_to_holder(PayloadValue &pv, StringsHolder &strHolder) const { + inline void move_strings_to_holder(PayloadValue& pv, StringsHolder& strHolder) const { Payload pl(payload_type_, pv); - for (int f : str_fields_) pl.MoveStrings(f, strHolder); + for (int f : str_fields_) { + pl.MoveStrings(f, strHolder); + } } inline bool have_str_fields() const noexcept { return !str_fields_.empty(); } @@ -162,17 +170,17 @@ class payload_str_fields_helper { template <> class payload_str_fields_helper { protected: - payload_str_fields_helper(const PayloadType &, const FieldsSet &) noexcept {} + payload_str_fields_helper(const PayloadType&, const FieldsSet&) noexcept {} - inline void add_ref(PayloadValue &) const noexcept {} - inline void release(PayloadValue &) const noexcept {} - inline void move_strings_to_holder(PayloadValue &, StringsHolder &) const noexcept {} + inline void add_ref(PayloadValue&) const noexcept {} + inline void release(PayloadValue&) const noexcept {} + inline void move_strings_to_holder(PayloadValue&, StringsHolder&) const noexcept {} inline bool have_str_fields() const noexcept { return false; } }; struct no_deep_clean { template - void operator()(const T &) const noexcept {} + void operator()(const T&) const noexcept {} }; template @@ -201,70 +209,86 @@ class unordered_payload_map static_assert(std::is_nothrow_move_constructible>::value, "Nothrow movebale key and value required"); - unordered_payload_map(size_t size, PayloadType &&pt, FieldsSet &&f) + unordered_payload_map(size_t size, PayloadType&& pt, FieldsSet&& f) : base_hash_map(size, hash_composite(PayloadType{pt}, FieldsSet{f}), equal_composite(PayloadType{pt}, FieldsSet{f})), payload_str_fields_helper(PayloadType{pt}, f), payloadType_(std::move(pt)), fields_(std::move(f)) {} - unordered_payload_map(PayloadType &&pt, FieldsSet &&f) : unordered_payload_map(1000, std::move(pt), std::move(f)) {} + unordered_payload_map(PayloadType&& pt, FieldsSet&& f) : unordered_payload_map(1000, std::move(pt), std::move(f)) {} - unordered_payload_map(const unordered_payload_map &other) + unordered_payload_map(const unordered_payload_map& other) : base_hash_map(other), payload_str_fields_helper(other), payloadType_(other.payloadType_), fields_(other.fields_) { - for (auto &item : *this) this->add_ref(item.first); + for (auto& item : *this) { + this->add_ref(item.first); + } } - unordered_payload_map(unordered_payload_map &&) = default; - unordered_payload_map &operator=(unordered_payload_map &&other) { - for (auto &item : *this) this->release(item.first); + unordered_payload_map(unordered_payload_map&&) = default; + unordered_payload_map& operator=(unordered_payload_map&& other) { + for (auto& item : *this) { + this->release(item.first); + } base_hash_map::operator=(std::move(other)); return *this; } - unordered_payload_map &operator=(const unordered_payload_map &) = delete; + unordered_payload_map& operator=(const unordered_payload_map&) = delete; ~unordered_payload_map() { - for (auto &item : *this) this->release(item.first); + for (auto& item : *this) { + this->release(item.first); + } } - std::pair insert(const std::pair &v) { + std::pair insert(const std::pair& v) { PayloadValueWithHash key(v.first, payloadType_, fields_); auto res = base_hash_map::emplate(std::move(key), v.second); - if (res.second) add_ref(res.first->first); + if (res.second) { + add_ref(res.first->first); + } return res; } - std::pair insert(std::pair &&v) { + std::pair insert(std::pair&& v) { PayloadValueWithHash key(std::move(v.first), payloadType_, fields_); auto res = base_hash_map::emplace(std::move(key), std::move(v.second)); - if (res.second) this->add_ref(res.first->first); + if (res.second) { + this->add_ref(res.first->first); + } return res; } template - std::pair emplace(const PayloadValue &pl, V &&v) { + std::pair emplace(const PayloadValue& pl, V&& v) { PayloadValueWithHash key(PayloadValue{pl}, payloadType_, fields_); auto res = base_hash_map::emplace(std::move(key), std::forward(v)); - if (res.second) this->add_ref(res.first->first); + if (res.second) { + this->add_ref(res.first->first); + } return res; } template - std::pair emplace(PayloadValue &&pl, V &&v) { + std::pair emplace(PayloadValue&& pl, V&& v) { PayloadValueWithHash key(std::move(pl), payloadType_, fields_); auto res = base_hash_map::emplace(std::move(key), std::forward(v)); - if (res.second) this->add_ref(res.first->first); + if (res.second) { + this->add_ref(res.first->first); + } return res; } template - iterator erase(iterator pos, StringsHolder &strHolder) { + iterator erase(iterator pos, StringsHolder& strHolder) { static const deep_cleaner deep_clean; - if (pos != end()) this->move_strings_to_holder(pos->first, strHolder); + if (pos != end()) { + this->move_strings_to_holder(pos->first, strHolder); + } deep_clean(*pos); return base_hash_map::erase(pos); } - T1 &operator[](const PayloadValue &k) { + T1& operator[](const PayloadValue& k) { PayloadValueWithHash key(PayloadValue{k}, payloadType_, fields_); return base_hash_map::operator[](std::move(key)); } - T1 &operator[](PayloadValue &&k) { + T1& operator[](PayloadValue&& k) { PayloadValueWithHash key(std::move(k), payloadType_, fields_); return base_hash_map::operator[](std::move(key)); } @@ -300,29 +324,37 @@ class payload_map : private btree::btree_map, using base_tree_map::find; using payload_str_fields_helper::have_str_fields; - payload_map(PayloadType payloadType, const FieldsSet &fields) + payload_map(PayloadType payloadType, const FieldsSet& fields) : base_tree_map(less_composite(PayloadType{payloadType}, FieldsSet{fields})), payload_str_fields_helper(std::move(payloadType), fields) {} - payload_map(const payload_map &other) : base_tree_map(other), payload_str_fields_helper(other) { - for (auto &item : *this) this->add_ref(const_cast(item.first)); + payload_map(const payload_map& other) : base_tree_map(other), payload_str_fields_helper(other) { + for (auto& item : *this) { + this->add_ref(const_cast(item.first)); + } } - payload_map(payload_map &&) = default; + payload_map(payload_map&&) = default; ~payload_map() { - for (auto &item : *this) this->release(const_cast(item.first)); + for (auto& item : *this) { + this->release(const_cast(item.first)); + } } - std::pair insert(const value_type &v) { + std::pair insert(const value_type& v) { auto res = base_tree_map::insert(v); - if (res.second) this->add_ref(const_cast(res.first->first)); + if (res.second) { + this->add_ref(const_cast(res.first->first)); + } return res; } - iterator insert(iterator, const value_type &v) { return insert(v).first; } + iterator insert(iterator, const value_type& v) { return insert(v).first; } template - iterator erase(const iterator &pos, StringsHolder &strHolder) { + iterator erase(const iterator& pos, StringsHolder& strHolder) { static const deep_cleaner deep_clean; - if (pos != end()) this->move_strings_to_holder(const_cast(pos->first), strHolder); + if (pos != end()) { + this->move_strings_to_holder(const_cast(pos->first), strHolder); + } deep_clean(*pos); return base_tree_map::erase(pos); } diff --git a/cpp_src/core/index/rtree/greenesplitter.h b/cpp_src/core/index/rtree/greenesplitter.h index c8c6a7179..1ceb8760a 100644 --- a/cpp_src/core/index/rtree/greenesplitter.h +++ b/cpp_src/core/index/rtree/greenesplitter.h @@ -30,9 +30,15 @@ class GreeneSplitter : private SplitterappendingEntry_ : src[seeds.second]); @@ -76,7 +82,9 @@ class GreeneSplitter : private SplittermoveEntryTo(*firstNode, indexes[i]); } - if (MaxEntries % 2 == 0) ++i; + if (MaxEntries % 2 == 0) { + ++i; + } for (; i < MaxEntries - 1; ++i) { this->moveEntryTo(*secondNode, indexes[i]); } diff --git a/cpp_src/core/index/rtree/indexrtree.cc b/cpp_src/core/index/rtree/indexrtree.cc index c285a768d..fe6628dbf 100644 --- a/cpp_src/core/index/rtree/indexrtree.cc +++ b/cpp_src/core/index/rtree/indexrtree.cc @@ -10,10 +10,10 @@ namespace reindexer { template class Splitter, size_t MaxEntries, size_t MinEntries> -SelectKeyResults IndexRTree::SelectKey(const VariantArray &keys, CondType condition, +SelectKeyResults IndexRTree::SelectKey(const VariantArray& keys, CondType condition, SortType sortId, Index::SelectOpts opts, - const BaseFunctionCtx::Ptr &funcCtx, - const RdxContext &rdxCtx) { + const BaseFunctionCtx::Ptr& funcCtx, + const RdxContext& rdxCtx) { const auto indexWard(rdxCtx.BeforeIndexWork()); if (opts.forceComparator) { return IndexStore::SelectKey(keys, condition, sortId, opts, funcCtx, rdxCtx); @@ -21,8 +21,12 @@ SelectKeyResults IndexRTree::Select SelectKeyResult res; - if (condition != CondDWithin) throw Error(errQueryExec, "Only CondDWithin available for RTree index"); - if (keys.size() != 2) throw Error(errQueryExec, "CondDWithin expects two arguments"); + if (condition != CondDWithin) { + throw Error(errQueryExec, "Only CondDWithin available for RTree index"); + } + if (keys.size() != 2) { + throw Error(errQueryExec, "CondDWithin expects two arguments"); + } Point point; double distance; if (keys[0].Type().Is()) { @@ -34,9 +38,9 @@ SelectKeyResults IndexRTree::Select } class Visitor : public Map::Visitor { public: - Visitor(SortType sId, unsigned distinct, unsigned iCountInNs, SelectKeyResult &r) + Visitor(SortType sId, unsigned distinct, unsigned iCountInNs, SelectKeyResult& r) : sortId_{sId}, itemsCountInNs_{distinct ? 0u : iCountInNs}, res_{r} {} - bool operator()(const typename Map::value_type &v) override { + bool operator()(const typename Map::value_type& v) override { idsCount_ += v.second.Unsorted().size(); res_.emplace_back(v.second, sortId_); return ScanWin(); @@ -48,7 +52,7 @@ SelectKeyResults IndexRTree::Select private: SortType sortId_; unsigned itemsCountInNs_; - SelectKeyResult &res_; + SelectKeyResult& res_; size_t idsCount_ = 0; } visitor{sortId, opts.distinct, opts.itemsCountInNamespace, res}; this->idx_map.DWithin(point, distance, visitor); @@ -61,8 +65,8 @@ SelectKeyResults IndexRTree::Select template class Splitter, size_t MaxEntries, size_t MinEntries> -void IndexRTree::Upsert(VariantArray &result, const VariantArray &keys, IdType id, - bool &clearCache) { +void IndexRTree::Upsert(VariantArray& result, const VariantArray& keys, IdType id, + bool& clearCache) { if (keys.empty() || keys.IsNullValue()) { Upsert(Variant{}, id, clearCache); return; @@ -90,15 +94,17 @@ void IndexRTree::Upsert(VariantArra template class Splitter, size_t MaxEntries, size_t MinEntries> -void IndexRTree::Delete(const VariantArray &keys, IdType id, StringsHolder &strHolder, - bool &clearCache) { +void IndexRTree::Delete(const VariantArray& keys, IdType id, StringsHolder& strHolder, + bool& clearCache) { if (keys.empty() || keys.IsNullValue()) { return Delete(Variant{}, id, strHolder, clearCache); } int delcnt = 0; const Point point = static_cast(keys); typename Map::iterator keyIt = this->idx_map.find(point); - if (keyIt == this->idx_map.end()) return; + if (keyIt == this->idx_map.end()) { + return; + } this->cache_.reset(); clearCache = true; this->isBuilt_ = false; @@ -120,8 +126,8 @@ void IndexRTree::Delete(const Varia } } -std::unique_ptr IndexRTree_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +std::unique_ptr IndexRTree_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { switch (idef.opts_.RTreeType()) { case IndexOpts::Linear: if (idef.opts_.IsPK() || idef.opts_.IsDense()) { diff --git a/cpp_src/core/index/rtree/indexrtree.h b/cpp_src/core/index/rtree/indexrtree.h index 9d4b23986..86cb022ae 100644 --- a/cpp_src/core/index/rtree/indexrtree.h +++ b/cpp_src/core/index/rtree/indexrtree.h @@ -11,20 +11,20 @@ class IndexRTree : public IndexUnordered; public: - IndexRTree(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, const NamespaceCacheConfigData &cacheCfg) + IndexRTree(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, const NamespaceCacheConfigData& cacheCfg) : IndexUnordered{idef, std::move(payloadType), std::move(fields), cacheCfg} {} - SelectKeyResults SelectKey(const VariantArray &keys, CondType, SortType, Index::SelectOpts, const BaseFunctionCtx::Ptr &, - const RdxContext &) override; + SelectKeyResults SelectKey(const VariantArray& keys, CondType, SortType, Index::SelectOpts, const BaseFunctionCtx::Ptr&, + const RdxContext&) override; using IndexUnordered::Upsert; - void Upsert(VariantArray &result, const VariantArray &keys, IdType id, bool &clearCache) override; + void Upsert(VariantArray& result, const VariantArray& keys, IdType id, bool& clearCache) override; using IndexUnordered::Delete; - void Delete(const VariantArray &keys, IdType id, StringsHolder &, bool &clearCache) override; + void Delete(const VariantArray& keys, IdType id, StringsHolder&, bool& clearCache) override; std::unique_ptr Clone() const override { return std::make_unique(*this); } }; -std::unique_ptr IndexRTree_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg); +std::unique_ptr IndexRTree_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg); } // namespace reindexer diff --git a/cpp_src/core/index/rtree/linearsplitter.h b/cpp_src/core/index/rtree/linearsplitter.h index 1654c6eae..473111f54 100644 --- a/cpp_src/core/index/rtree/linearsplitter.h +++ b/cpp_src/core/index/rtree/linearsplitter.h @@ -53,7 +53,9 @@ class LinearSplitter : public GuttmanSplitter newMinX) { newMinX = currRect.Left(); @@ -86,7 +88,9 @@ class LinearSplitter : public GuttmanSplitter newMinY) { newMinY = currRect.Bottom(); diff --git a/cpp_src/core/index/rtree/quadraticsplitter.h b/cpp_src/core/index/rtree/quadraticsplitter.h index c00d71e1c..c8ac8b257 100644 --- a/cpp_src/core/index/rtree/quadraticsplitter.h +++ b/cpp_src/core/index/rtree/quadraticsplitter.h @@ -28,7 +28,9 @@ class QuadraticSplitter : public GuttmanSplittermoved_[i]) continue; + if (this->moved_[i]) { + continue; + } const auto rect = Base::getBoundRect((i == MaxEntries) ? this->appendingEntry_ : this->srcNode_.data_[i]); const auto currFirstAreaIncrease = Base::AreaIncrease(firstNode->BoundRect(), rect); const auto currSecondAreaIncrease = Base::AreaIncrease(secondNode->BoundRect(), rect); diff --git a/cpp_src/core/index/rtree/rstarsplitter.h b/cpp_src/core/index/rtree/rstarsplitter.h index 427a4add3..7b02c49b8 100644 --- a/cpp_src/core/index/rtree/rstarsplitter.h +++ b/cpp_src/core/index/rtree/rstarsplitter.h @@ -123,7 +123,9 @@ class RStarSplitter : private SplitterIsFull()) ++i; + while (i < data.size() && data[i]->IsFull()) { + ++i; + } } assertrx(i < data.size()); auto minOverlapIncrease = overlap(boundRect(data[i]->BoundRect(), insertingRect), i, data) - overlap(data[i]->BoundRect(), i, data); @@ -166,7 +168,9 @@ class RStarSplitter : private SplitterBoundRect(), dstBoundRect), result, except, data) - overlap(dstBoundRect, result, except, data); for (size_t i = result + 1; i < data.size(); ++i) { - if (i == except) continue; + if (i == except) { + continue; + } const auto currOverlapIncrease = overlap(boundRect(data[i]->BoundRect(), dstBoundRect), i, except, data) - overlap(dstBoundRect, i, except, data); if (approxEqual(minOverlapIncrease, currOverlapIncrease)) { @@ -204,7 +208,9 @@ class RStarSplitter : private Splitter().data_)& data) noexcept { - if (index1 > index2) std::swap(index1, index2); + if (index1 > index2) { + std::swap(index1, index2); + } assertrx(index2 < data.size()); double result = 0.0; size_t i = 0; @@ -222,10 +228,14 @@ class RStarSplitter : private Splitter= right) return 0.0; + if (left >= right) { + return 0.0; + } const auto bottom = std::max(r1.Bottom(), r2.Bottom()); const auto top = std::min(r1.Top(), r2.Top()); - if (bottom >= top) return 0.0; + if (bottom >= top) { + return 0.0; + } return (right - left) * (top - bottom); } }; diff --git a/cpp_src/core/index/rtree/rtree.h b/cpp_src/core/index/rtree/rtree.h index 9d494dcab..1e61ce68e 100644 --- a/cpp_src/core/index/rtree/rtree.h +++ b/cpp_src/core/index/rtree/rtree.h @@ -199,14 +199,18 @@ class RectangleTree { bool DWithin(Point p, double distance, RectangleTree::Visitor& visitor) const noexcept override { for (const auto& v : data_) { if (reindexer::DWithin(Traits::GetPoint(v), p, distance)) { - if (visitor(v)) return true; + if (visitor(v)) { + return true; + } } } return false; } bool ForEach(RectangleTree::Visitor& visitor) const noexcept override { for (const auto& v : data_) { - if (visitor(v)) return true; + if (visitor(v)) { + return true; + } } return false; } @@ -216,7 +220,9 @@ class RectangleTree { if (visitor(*it)) { data_.erase(it); if (data_.size() < MinEntries) { - if (data_.empty()) this->SetBoundRect({}); + if (data_.empty()) { + this->SetBoundRect({}); + } return {true, true}; } else { adjustBoundRect(); @@ -239,23 +245,35 @@ class RectangleTree { } double AreaIncrease(const reindexer::Rectangle& r) const noexcept override { - if (data_.empty()) return r.Area(); + if (data_.empty()) { + return r.Area(); + } return SplitterT::AreaIncrease(this->BoundRect(), r); } bool Check(const Node* parent) const noexcept override { - if (parent != this->Parent()) return false; - if (data_.size() > MaxEntries) return false; + if (parent != this->Parent()) { + return false; + } + if (data_.size() > MaxEntries) { + return false; + } if (data_.empty()) { - if (this->BoundRect() != reindexer::Rectangle{}) return false; + if (this->BoundRect() != reindexer::Rectangle{}) { + return false; + } } else { const reindexer::Rectangle thisBoundRect{this->BoundRect()}; reindexer::Rectangle boundRectOfAllChildren = boundRect(Traits::GetPoint(data_[0])); for (const auto& v : data_) { boundRectOfAllChildren = boundRect(boundRectOfAllChildren, Traits::GetPoint(v)); - if (!thisBoundRect.Contain(Traits::GetPoint(v))) return false; + if (!thisBoundRect.Contain(Traits::GetPoint(v))) { + return false; + } + } + if (thisBoundRect != boundRectOfAllChildren) { + return false; } - if (thisBoundRect != boundRectOfAllChildren) return false; } return true; } @@ -301,9 +319,13 @@ class RectangleTree { bool IsLeaf() const noexcept override { return false; } bool IsFull() const noexcept override { - if (data_.size() < MaxEntries) return false; + if (data_.size() < MaxEntries) { + return false; + } for (const auto& n : data_) { - if (!n->IsFull()) return false; + if (!n->IsFull()) { + return false; + } } return true; } @@ -316,7 +338,9 @@ class RectangleTree { } bool Empty() const noexcept override { for (const auto& n : data_) { - if (!n->Empty()) return false; + if (!n->Empty()) { + return false; + } } return true; } @@ -345,7 +369,9 @@ class RectangleTree { for (auto& n : data_) { if (n->BoundRect().Contain(p)) { const auto res = n->find(p); - if (res.second) return res; + if (res.second) { + return res; + } } } return {{nullptr, {}}, false}; @@ -354,7 +380,9 @@ class RectangleTree { for (auto& n : data_) { if (n->BoundRect().Contain(p)) { const auto res = n->find(p); - if (res.second) return res; + if (res.second) { + return res; + } } } return {{nullptr, {}}, false}; @@ -369,7 +397,9 @@ class RectangleTree { if (splittedChildren.first) { data_[nodeToInsert] = std::move(splittedChildren.first); auto splittedNodes = insert(std::move(splittedChildren.second)); - if (splittedNodes.first) return splittedNodes; + if (splittedNodes.first) { + return splittedNodes; + } data_[nodeToInsert]->SetParent(this); } if (data_.size() == 1) { @@ -383,16 +413,22 @@ class RectangleTree { bool DWithin(Point p, double distance, RectangleTree::Visitor& visitor) const noexcept override { for (const auto& n : data_) { if (reindexer::DWithin(n->BoundRect(), p, distance)) { - if (n->ForEach(visitor)) return true; + if (n->ForEach(visitor)) { + return true; + } } else if (intersect(n->BoundRect(), Circle{p, distance})) { - if (n->DWithin(p, distance, visitor)) return true; + if (n->DWithin(p, distance, visitor)) { + return true; + } } } return false; } bool ForEach(RectangleTree::Visitor& visitor) const noexcept override { for (const auto& n : data_) { - if (n->ForEach(visitor)) return true; + if (n->ForEach(visitor)) { + return true; + } } return false; } @@ -419,17 +455,29 @@ class RectangleTree { } bool Check(const Node* parent) const noexcept override { - if (parent != this->Parent()) return false; - if (data_.empty()) return false; - if (data_.size() > MaxEntries) return false; + if (parent != this->Parent()) { + return false; + } + if (data_.empty()) { + return false; + } + if (data_.size() > MaxEntries) { + return false; + } const reindexer::Rectangle thisBoundRect{this->BoundRect()}; reindexer::Rectangle boundRectOfAllChildren = data_[0]->BoundRect(); for (const auto& n : data_) { - if (!n->Check(this)) return false; + if (!n->Check(this)) { + return false; + } boundRectOfAllChildren = boundRect(boundRectOfAllChildren, n->BoundRect()); - if (!thisBoundRect.Contain(n->BoundRect())) return false; + if (!thisBoundRect.Contain(n->BoundRect())) { + return false; + } + } + if (this->BoundRect() != boundRectOfAllChildren) { + return false; } - if (this->BoundRect() != boundRectOfAllChildren) return false; return true; } @@ -557,12 +605,16 @@ class RectangleTree { iterator find(Point p) noexcept { const auto res = root_.find(p); - if (res.second) return res.first; + if (res.second) { + return res.first; + } return end(); } const_iterator find(Point p) const noexcept { const auto res = root_.find(p); - if (res.second) return res.first; + if (res.second) { + return res.first; + } return cend(); } void DWithin(Point p, double distance, RectangleTree::Visitor& visitor) const { root_.DWithin(p, distance, visitor); } diff --git a/cpp_src/core/index/rtree/splitter.h b/cpp_src/core/index/rtree/splitter.h index 51675c1f0..59eb756bb 100644 --- a/cpp_src/core/index/rtree/splitter.h +++ b/cpp_src/core/index/rtree/splitter.h @@ -12,7 +12,9 @@ class Splitter { ~Splitter() = default; static double AreaIncrease(const Rectangle& lhs, const Rectangle& rhs) noexcept { - if (lhs.Contain(rhs)) return 0.0; + if (lhs.Contain(rhs)) { + return 0.0; + } return reindexer::boundRect(lhs, rhs).Area() - lhs.Area(); } @@ -45,13 +47,17 @@ class Splitter { bool splitOfChildAvailable) { size_t i = 0; if (!splitOfChildAvailable) { - while (i < data.size() && data[i]->IsFull()) ++i; + while (i < data.size() && data[i]->IsFull()) { + ++i; + } } assertrx(i < data.size()); auto minAreaIncrease = data[i]->AreaIncrease(insertingRect); size_t result = i; for (++i; i < data.size(); ++i) { - if (!splitOfChildAvailable && data[i]->IsFull()) continue; + if (!splitOfChildAvailable && data[i]->IsFull()) { + continue; + } const auto currAreaIncrease = data[i]->AreaIncrease(insertingRect); if ((approxEqual(currAreaIncrease, minAreaIncrease) && data[i]->BoundRect().Area() < data[result]->BoundRect().Area()) || currAreaIncrease < minAreaIncrease) { @@ -70,7 +76,9 @@ class Splitter { } auto minAreaIncrease = dst.AreaIncrease(data[result]->BoundRect()); for (size_t i = result + 1; i < data.size(); ++i) { - if (i == except) continue; + if (i == except) { + continue; + } const auto currAreaIncrease = dst.AreaIncrease(data[i]->BoundRect()); if ((approxEqual(currAreaIncrease, minAreaIncrease) && data[i]->BoundRect().Area() < data[result]->BoundRect().Area()) || currAreaIncrease < minAreaIncrease) { diff --git a/cpp_src/core/index/string_map.h b/cpp_src/core/index/string_map.h index 67fcda0ad..2f7d0a616 100644 --- a/cpp_src/core/index/string_map.h +++ b/cpp_src/core/index/string_map.h @@ -245,7 +245,9 @@ struct StringMapEntryCleaner { template (), bool> = true> void free_node(const T& str) const { - if (needSaveExpiredStrings_) strHolder_.Add(str); + if (needSaveExpiredStrings_) { + strHolder_.Add(str); + } } template (), bool> = true> diff --git a/cpp_src/core/index/ttlindex.cc b/cpp_src/core/index/ttlindex.cc index 7fdbc1e5b..7a10c0b69 100644 --- a/cpp_src/core/index/ttlindex.cc +++ b/cpp_src/core/index/ttlindex.cc @@ -2,10 +2,10 @@ namespace reindexer { -void UpdateExpireAfter(Index *i, int64_t v) { - auto ttlIndexEntryPlain = dynamic_cast> *>(i); +void UpdateExpireAfter(Index* i, int64_t v) { + auto ttlIndexEntryPlain = dynamic_cast>*>(i); if (ttlIndexEntryPlain == nullptr) { - auto ttlIndex = dynamic_cast> *>(i); + auto ttlIndex = dynamic_cast>*>(i); if (ttlIndex == nullptr) { throw Error(errLogic, "Incorrect ttl index type"); } @@ -15,8 +15,8 @@ void UpdateExpireAfter(Index *i, int64_t v) { } } -std::unique_ptr TtlIndex_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +std::unique_ptr TtlIndex_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { if (idef.opts_.IsPK() || idef.opts_.IsDense()) { return std::make_unique>>(idef, std::move(payloadType), std::move(fields), cacheCfg); diff --git a/cpp_src/core/index/ttlindex.h b/cpp_src/core/index/ttlindex.h index b59b6947c..48899cafe 100644 --- a/cpp_src/core/index/ttlindex.h +++ b/cpp_src/core/index/ttlindex.h @@ -10,9 +10,9 @@ namespace reindexer { template class TtlIndex : public IndexOrdered { public: - TtlIndex(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, const NamespaceCacheConfigData &cacheCfg) + TtlIndex(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, const NamespaceCacheConfigData& cacheCfg) : IndexOrdered(idef, std::move(payloadType), std::move(fields), cacheCfg), expireAfter_(idef.expireAfter_) {} - TtlIndex(const TtlIndex &other) : IndexOrdered(other), expireAfter_(other.expireAfter_) {} + TtlIndex(const TtlIndex& other) : IndexOrdered(other), expireAfter_(other.expireAfter_) {} int64_t GetTTLValue() const noexcept override { return expireAfter_; } std::unique_ptr Clone() const override { return std::make_unique>(*this); } void UpdateExpireAfter(int64_t v) noexcept { expireAfter_ = v; } @@ -22,8 +22,8 @@ class TtlIndex : public IndexOrdered { int64_t expireAfter_ = 0; }; -std::unique_ptr TtlIndex_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg); -void UpdateExpireAfter(Index *i, int64_t v); +std::unique_ptr TtlIndex_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg); +void UpdateExpireAfter(Index* i, int64_t v); } // namespace reindexer diff --git a/cpp_src/core/index/updatetracker.h b/cpp_src/core/index/updatetracker.h index ee870dea4..78ff6ed86 100644 --- a/cpp_src/core/index/updatetracker.h +++ b/cpp_src/core/index/updatetracker.h @@ -18,17 +18,17 @@ class UpdateTracker { template struct hash_ptr { - size_t operator()(const T1 &obj) const noexcept { return std::hash()(reinterpret_cast(obj.get())); } + size_t operator()(const T1& obj) const noexcept { return std::hash()(reinterpret_cast(obj.get())); } }; template struct equal_ptr { - bool operator()(const T1 &lhs, const T1 &rhs) const noexcept { + bool operator()(const T1& lhs, const T1& rhs) const noexcept { return reinterpret_cast(lhs.get()) == reinterpret_cast(rhs.get()); } }; template struct less_ptr { - bool operator()(const T1 &lhs, const T1 &rhs) const noexcept { + bool operator()(const T1& lhs, const T1& rhs) const noexcept { return reinterpret_cast(lhs.get()) < reinterpret_cast(rhs.get()); } }; @@ -46,21 +46,25 @@ class UpdateTracker { typename std::conditional_t>; UpdateTracker() = default; - UpdateTracker(const UpdateTracker &other) + UpdateTracker(const UpdateTracker& other) : completeUpdate_(other.updated_.size() || other.completeUpdate_), simpleCounting_(other.simpleCounting_), updatesCounter_(other.updatesCounter_) { updatesBuckets_.store(updated_.bucket_count(), std::memory_order_relaxed); } - UpdateTracker &operator=(const UpdateTracker &other) = delete; + UpdateTracker& operator=(const UpdateTracker& other) = delete; - void markUpdated(T &idx_map, typename T::iterator &k, bool skipCommited = true) { - if (skipCommited && k->second.Unsorted().IsCommited()) return; + void markUpdated(T& idx_map, typename T::iterator& k, bool skipCommited = true) { + if (skipCommited && k->second.Unsorted().IsCommited()) { + return; + } if (simpleCounting_) { ++updatesCounter_; return; } - if (completeUpdate_) return; + if (completeUpdate_) { + return; + } if (updated_.size() > static_cast(idx_map.size() / 8) || updated_.size() > 10000000) { completeUpdate_ = true; clearUpdates(); @@ -69,8 +73,8 @@ class UpdateTracker { emplaceUpdate(k); } - void commitUpdated(T &idx_map) { - for (const auto &valIt : updated_) { + void commitUpdated(T& idx_map) { + for (const auto& valIt : updated_) { auto keyIt = idx_map.find(valIt); assertrx(keyIt != idx_map.end()); keyIt->second.Unsorted().Commit(); @@ -78,7 +82,7 @@ class UpdateTracker { } } - void markDeleted(typename T::iterator &k) { + void markDeleted(typename T::iterator& k) { if (simpleCounting_) { ++updatesCounter_; } else { @@ -92,8 +96,8 @@ class UpdateTracker { updatesCounter_ = 0; clearUpdates(); } - hash_map &updated() { return updated_; } - const hash_map &updated() const { return updated_; } + hash_map& updated() { return updated_; } + const hash_map& updated() const { return updated_; } uint32_t updatesSize() const noexcept { return updatesSize_.load(std::memory_order_relaxed); } uint32_t updatesBuckets() const noexcept { return updatesBuckets_.load(std::memory_order_relaxed); } uint32_t allocated() const noexcept { return allocatedMem_.load(std::memory_order_relaxed); } @@ -113,14 +117,14 @@ class UpdateTracker { } protected: - void eraseUpdate(typename T::iterator &k) { + void eraseUpdate(typename T::iterator& k) { updated_.erase(k->first); updatesSize_.store(updated_.size(), std::memory_order_relaxed); updatesBuckets_.store(updated_.bucket_count(), std::memory_order_relaxed); allocatedMem_.store(getMapAllocatedMemSize(), std::memory_order_relaxed); overflowSize_.store(getMapOverflowSize(), std::memory_order_relaxed); } - void emplaceUpdate(typename T::iterator &k) { + void emplaceUpdate(typename T::iterator& k) { updated_.emplace(k->first); updatesSize_.store(updated_.size(), std::memory_order_relaxed); updatesBuckets_.store(updated_.bucket_count(), std::memory_order_relaxed); diff --git a/cpp_src/core/index/uuid_index.cc b/cpp_src/core/index/uuid_index.cc index 7622020d3..dafac6a15 100644 --- a/cpp_src/core/index/uuid_index.cc +++ b/cpp_src/core/index/uuid_index.cc @@ -2,7 +2,7 @@ namespace reindexer { -void UuidIndex::Upsert(VariantArray &result, const VariantArray &keys, IdType id, bool &clearCache) { +void UuidIndex::Upsert(VariantArray& result, const VariantArray& keys, IdType id, bool& clearCache) { if (keys.empty() && !Opts().IsArray()) { result = {Upsert(Variant{Uuid{}}, id, clearCache)}; } else { @@ -10,8 +10,8 @@ void UuidIndex::Upsert(VariantArray &result, const VariantArray &keys, IdType id } } -std::unique_ptr IndexUuid_New(const IndexDef &idef, PayloadType &&payloadType, FieldsSet &&fields, - const NamespaceCacheConfigData &cacheCfg) { +std::unique_ptr IndexUuid_New(const IndexDef& idef, PayloadType&& payloadType, FieldsSet&& fields, + const NamespaceCacheConfigData& cacheCfg) { return std::make_unique(idef, std::move(payloadType), std::move(fields), cacheCfg); } diff --git a/cpp_src/core/indexdef.cc b/cpp_src/core/indexdef.cc index 74eab7234..ad79f4b96 100644 --- a/cpp_src/core/indexdef.cc +++ b/cpp_src/core/indexdef.cc @@ -11,22 +11,22 @@ namespace { using namespace std::string_view_literals; -static const std::vector &condsUsual() { +static const std::vector& condsUsual() { static const std::vector data{"SET"sv, "EQ"sv, "ANY"sv, "EMPTY"sv, "LT"sv, "LE"sv, "GT"sv, "GE"sv, "RANGE"sv}; return data; } -static const std::vector &condsText() { +static const std::vector& condsText() { static const std::vector data{"MATCH"sv}; return data; } -static const std::vector &condsBool() { +static const std::vector& condsBool() { static const std::vector data{"SET"sv, "EQ"sv, "ANY"sv, "EMPTY"sv}; return data; } -static const std::vector &condsGeom() { +static const std::vector& condsGeom() { static const std::vector data{"DWITHIN"sv}; return data; } @@ -35,11 +35,11 @@ enum Caps { CapComposite = 0x1, CapSortable = 0x2, CapFullText = 0x4 }; struct IndexInfo { const std::string_view fieldType, indexType; - const std::vector &conditions; + const std::vector& conditions; int caps; }; -static const std::unordered_map, std::equal_to> &availableIndexes() { +static const std::unordered_map, std::equal_to>& availableIndexes() { // clang-format off static const std::unordered_map, std::equal_to> data { {IndexIntHash, {"int"sv, "hash"sv, condsUsual(), CapSortable}}, @@ -112,7 +112,7 @@ IndexDef::IndexDef(std::string name, JsonPaths jsonPaths, IndexType type, IndexO this->FromType(type); } -bool IndexDef::IsEqual(const IndexDef &other, IndexComparison cmpType) const { +bool IndexDef::IsEqual(const IndexDef& other, IndexComparison cmpType) const { return name_ == other.name_ && jsonPaths_ == other.jsonPaths_ && Type() == other.Type() && fieldType_ == other.fieldType_ && opts_.IsEqual(other.opts_, cmpType) && expireAfter_ == other.expireAfter_; } @@ -130,20 +130,22 @@ IndexType IndexDef::Type() const { iType = "hash"; } } - for (const auto &it : availableIndexes()) { - if (fieldType_ == it.second.fieldType && iType == it.second.indexType) return it.first; + for (const auto& it : availableIndexes()) { + if (fieldType_ == it.second.fieldType && iType == it.second.indexType) { + return it.first; + } } throw Error(errParams, "Unsupported combination of field '%s' type '%s' and index type '%s'", name_, fieldType_, indexType_); } void IndexDef::FromType(IndexType type) { - const auto &it = availableIndexes().at(type); + const auto& it = availableIndexes().at(type); fieldType_ = it.fieldType; indexType_ = it.indexType; } -const std::vector &IndexDef::Conditions() const noexcept { +const std::vector& IndexDef::Conditions() const noexcept { const auto it{availableIndexes().find(Type())}; assertrx(it != availableIndexes().cend()); return it->second.conditions; @@ -159,18 +161,18 @@ bool isStore(IndexType type) noexcept { Error IndexDef::FromJSON(span json) { try { IndexDef::FromJSON(gason::JsonParser().Parse(json)); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "IndexDef: %s", ex.what()); - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; } -void IndexDef::FromJSON(const gason::JsonNode &root) { +void IndexDef::FromJSON(const gason::JsonNode& root) { name_ = root["name"].As(); jsonPaths_.clear(); - for (auto &subElem : root["json_paths"]) { + for (auto& subElem : root["json_paths"]) { jsonPaths_.push_back(subElem.As()); } fieldType_ = root["field_type"].As(); @@ -206,8 +208,10 @@ void IndexDef::FromJSON(const gason::JsonNode &root) { auto collateStr = root["collate_mode"].As(); if (!collateStr.empty()) { auto collateIt = find_if(begin(kAvailableCollates), end(kAvailableCollates), - [&collateStr](const std::pair &p) { return collateStr == p.second; }); - if (collateIt == end(kAvailableCollates)) throw Error(errParams, "Unknown collate mode %s", collateStr); + [&collateStr](const std::pair& p) { return collateStr == p.second; }); + if (collateIt == end(kAvailableCollates)) { + throw Error(errParams, "Unknown collate mode %s", collateStr); + } CollateMode collateValue = collateIt->first; opts_.SetCollateMode(collateValue); if (collateValue == CollateCustom) { @@ -216,7 +220,7 @@ void IndexDef::FromJSON(const gason::JsonNode &root) { } } -void IndexDef::GetJSON(WrSerializer &ser, int formatFlags) const { +void IndexDef::GetJSON(WrSerializer& ser, int formatFlags) const { JsonBuilder builder(ser); builder.Put("name", name_) @@ -256,13 +260,13 @@ void IndexDef::GetJSON(WrSerializer &ser, int formatFlags) const { builder.Put("is_sortable", isSortable(Type())); builder.Put("is_fulltext", IsFullText(Type())); auto arr = builder.Array("conditions"); - for (auto &cond : Conditions()) { + for (auto& cond : Conditions()) { arr.Put(nullptr, cond); } } auto arrNode = builder.Array("json_paths"); - for (auto &jsonPath : jsonPaths_) { + for (auto& jsonPath : jsonPaths_) { arrNode.Put(nullptr, jsonPath); } } diff --git a/cpp_src/core/indexdef.h b/cpp_src/core/indexdef.h index 91e8eb3e0..b621b6ee6 100644 --- a/cpp_src/core/indexdef.h +++ b/cpp_src/core/indexdef.h @@ -26,13 +26,13 @@ struct IndexDef { IndexDef(std::string name, JsonPaths jsonPaths, std::string indexType, std::string fieldType, IndexOpts opts, int64_t expireAfter); IndexDef(std::string name, std::string indexType, std::string fieldType, IndexOpts opts); IndexDef(std::string name, JsonPaths jsonPaths, IndexType type, IndexOpts opts); - bool IsEqual(const IndexDef &other, IndexComparison cmpType) const; + bool IsEqual(const IndexDef& other, IndexComparison cmpType) const; IndexType Type() const; - const std::vector &Conditions() const noexcept; + const std::vector& Conditions() const noexcept; void FromType(IndexType type); Error FromJSON(span json); - void FromJSON(const gason::JsonNode &jvalue); - void GetJSON(WrSerializer &ser, int formatFlags = 0) const; + void FromJSON(const gason::JsonNode& jvalue); + void GetJSON(WrSerializer& ser, int formatFlags = 0) const; public: std::string name_; diff --git a/cpp_src/core/indexopts.cc b/cpp_src/core/indexopts.cc index e41e9631a..b103a6b8c 100644 --- a/cpp_src/core/indexopts.cc +++ b/cpp_src/core/indexopts.cc @@ -68,21 +68,29 @@ void IndexOpts::Dump(T& os) const { needComma = true; } if (IsArray()) { - if (needComma) os << ", "; + if (needComma) { + os << ", "; + } os << "Array"; needComma = true; } if (IsDense()) { - if (needComma) os << ", "; + if (needComma) { + os << ", "; + } os << "Dense"; needComma = true; } if (IsSparse()) { - if (needComma) os << ", "; + if (needComma) { + os << ", "; + } os << "Sparse"; needComma = true; } - if (needComma) os << ", "; + if (needComma) { + os << ", "; + } os << RTreeType(); if (HasConfig()) { os << ", config: " << config; diff --git a/cpp_src/core/item.cc b/cpp_src/core/item.cc index 4454c7778..cee925eb2 100644 --- a/cpp_src/core/item.cc +++ b/cpp_src/core/item.cc @@ -7,7 +7,7 @@ namespace reindexer { -Item &Item::operator=(Item &&other) noexcept { +Item& Item::operator=(Item&& other) noexcept { if (&other != this) { if (impl_) { auto ns = impl_->GetNamespace(); @@ -45,10 +45,11 @@ Point Item::FieldRef::As() const { Item::FieldRef::operator Variant() const { VariantArray kr; - if (field_ >= 0) + if (field_ >= 0) { itemImpl_->GetPayload().Get(field_, kr); - else + } else { kr = itemImpl_->GetValueByJSONPath(jsonPath_); + } if (kr.size() != 1) { throw Error(errParams, "Invalid array access"); @@ -65,7 +66,7 @@ Item::FieldRef::operator VariantArray() const { return itemImpl_->GetValueByJSONPath(jsonPath_); } -Item::FieldRef &Item::FieldRef::operator=(Variant kr) { +Item::FieldRef& Item::FieldRef::operator=(Variant kr) { if (field_ >= 0) { itemImpl_->SetField(field_, VariantArray{std::move(kr)}); } else { @@ -75,10 +76,10 @@ Item::FieldRef &Item::FieldRef::operator=(Variant kr) { return *this; } -Item::FieldRef &Item::FieldRef::operator=(const char *str) { return operator=(p_string(str)); } -Item::FieldRef &Item::FieldRef::operator=(const std::string &str) { return operator=(p_string(&str)); } +Item::FieldRef& Item::FieldRef::operator=(const char* str) { return operator=(p_string(str)); } +Item::FieldRef& Item::FieldRef::operator=(const std::string& str) { return operator=(p_string(&str)); } -Item::FieldRef &Item::FieldRef::operator=(const VariantArray &krs) { +Item::FieldRef& Item::FieldRef::operator=(const VariantArray& krs) { if (field_ >= 0) { itemImpl_->SetField(field_, krs); } else { @@ -88,14 +89,14 @@ Item::FieldRef &Item::FieldRef::operator=(const VariantArray &krs) { } template -Item::FieldRef &Item::FieldRef::operator=(span arr) { +Item::FieldRef& Item::FieldRef::operator=(span arr) { constexpr static bool kIsStr = std::is_same_v || std::is_same_v || std::is_same_v || - std::is_same_v || std::is_same_v; + std::is_same_v || std::is_same_v; if (field_ < 0) { VariantArray krs; krs.MarkArray(); krs.reserve(arr.size()); - std::transform(arr.begin(), arr.end(), std::back_inserter(krs), [](const T &t) { return Variant(t); }); + std::transform(arr.begin(), arr.end(), std::back_inserter(krs), [](const T& t) { return Variant(t); }); itemImpl_->SetField(jsonPath_, krs); return *this; } @@ -105,12 +106,14 @@ Item::FieldRef &Item::FieldRef::operator=(span arr) { if constexpr (kIsStr) { if (itemImpl_->IsUnsafe() || itemImpl_->Type()->Field(field_).Type().Is()) { - for (auto &elem : arr) { + for (auto& elem : arr) { pl.Set(field_, pos++, Variant(elem)); } } else { - if (!itemImpl_->holder_) itemImpl_->holder_ = std::make_unique>(); - for (auto &elem : arr) { + if (!itemImpl_->holder_) { + itemImpl_->holder_ = std::make_unique>(); + } + for (auto& elem : arr) { if constexpr (std::is_same_v) { itemImpl_->holder_->push_back(elem.toString()); } else { @@ -120,7 +123,7 @@ Item::FieldRef &Item::FieldRef::operator=(span arr) { } } } else { - for (auto &elem : arr) { + for (auto& elem : arr) { pl.Set(field_, pos++, Variant(elem)); } } @@ -138,7 +141,7 @@ Item::~Item() { } } -Error Item::FromJSON(std::string_view slice, char **endp, bool pkOnly) & noexcept { +Error Item::FromJSON(std::string_view slice, char** endp, bool pkOnly) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->FromJSON(slice, endp, pkOnly)); } @@ -154,15 +157,15 @@ void Item::FromCJSONImpl(std::string_view slice, bool pkOnly) & { impl_->FromCJS std::string_view Item::GetCJSON() { return impl_->GetCJSON(); } std::string_view Item::GetJSON() { return impl_->GetJSON(); } -Error Item::FromMsgPack(std::string_view buf, size_t &offset) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->FromMsgPack(buf, offset)); } +Error Item::FromMsgPack(std::string_view buf, size_t& offset) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->FromMsgPack(buf, offset)); } Error Item::FromProtobuf(std::string_view sbuf) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->FromProtobuf(sbuf)); } -Error Item::GetMsgPack(WrSerializer &wrser) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->GetMsgPack(wrser)); } +Error Item::GetMsgPack(WrSerializer& wrser) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->GetMsgPack(wrser)); } std::string_view Item::GetMsgPack() & { return impl_->GetMsgPack(); } -Error Item::GetProtobuf(WrSerializer &wrser) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->GetProtobuf(wrser)); } +Error Item::GetProtobuf(WrSerializer& wrser) & noexcept { RETURN_RESULT_NOEXCEPT(impl_->GetProtobuf(wrser)); } int Item::NumFields() const { return impl_->Type().NumFields(); } @@ -173,18 +176,18 @@ Item::FieldRef Item::operator[](int field) const { return FieldRef(field, impl_); } -Item::FieldRef Item::FieldRefByName(std::string_view name, ItemImpl &impl) { +Item::FieldRef Item::FieldRefByName(std::string_view name, ItemImpl& impl) { int field = 0; return (impl.Type().FieldByName(name, field)) ? FieldRef(field, &impl) : FieldRef(name, &impl); } int Item::GetFieldTag(std::string_view name) const { return impl_->NameTag(name); } FieldsSet Item::PkFields() const { return impl_->PkFields(); } -void Item::SetPrecepts(const std::vector &precepts) & { impl_->SetPrecepts(precepts); } +void Item::SetPrecepts(const std::vector& precepts) & { impl_->SetPrecepts(precepts); } bool Item::IsTagsUpdated() const noexcept { return impl_->tagsMatcher().isUpdated(); } int Item::GetStateToken() const noexcept { return impl_->tagsMatcher().stateToken(); } -Item &Item::Unsafe(bool enable) & noexcept { +Item& Item::Unsafe(bool enable) & noexcept { impl_->Unsafe(enable); return *this; } @@ -192,10 +195,10 @@ Item &Item::Unsafe(bool enable) & noexcept { int64_t Item::GetLSN() { return impl_->Value().GetLSN(); } void Item::setLSN(int64_t lsn) { impl_->Value().SetLSN(lsn); } -template Item::FieldRef &Item::FieldRef::operator=(span arr); -template Item::FieldRef &Item::FieldRef::operator=(span arr); -template Item::FieldRef &Item::FieldRef::operator=(span arr); -template Item::FieldRef &Item::FieldRef::operator=(span); -template Item::FieldRef &Item::FieldRef::operator=(span); +template Item::FieldRef& Item::FieldRef::operator=(span arr); +template Item::FieldRef& Item::FieldRef::operator=(span arr); +template Item::FieldRef& Item::FieldRef::operator=(span arr); +template Item::FieldRef& Item::FieldRef::operator=(span); +template Item::FieldRef& Item::FieldRef::operator=(span); } // namespace reindexer diff --git a/cpp_src/core/item.h b/cpp_src/core/item.h index 6234a2f60..90f6fa42f 100644 --- a/cpp_src/core/item.h +++ b/cpp_src/core/item.h @@ -28,10 +28,10 @@ class Item { Item() noexcept : impl_(nullptr), status_(errNotValid) {} /// Destroy Item ~Item(); - Item(const Item &) = delete; - Item(Item &&other) noexcept : impl_(other.impl_), status_(std::move(other.status_)), id_(other.id_) { other.impl_ = nullptr; } - Item &operator=(const Item &) = delete; - Item &operator=(Item &&) noexcept; + Item(const Item&) = delete; + Item(Item&& other) noexcept : impl_(other.impl_), status_(std::move(other.status_)), id_(other.id_) { other.impl_ = nullptr; } + Item& operator=(const Item&) = delete; + Item& operator=(Item&&) noexcept; /// Reference to field. Interface for field data manipulation class [[nodiscard]] FieldRef { @@ -60,12 +60,12 @@ class Item { /// @tparam T - type. Must be one of: int, int64_t, double /// @param val - value, which will be setted to field template - FieldRef &operator=(const T &val) { + FieldRef& operator=(const T& val) { return operator=(Variant(val)); } /// Set single point type value /// @param p - point value, which will be setted to field - FieldRef &operator=(Point p) { + FieldRef& operator=(Point p) { double arr[]{p.X(), p.Y()}; return operator=(span(arr, 2)); } @@ -74,24 +74,24 @@ class Item { /// @tparam T - type. Must be one of: int, int64_t, double /// @param arr - std::vector of T values, which will be setted to field template - FieldRef &operator=(span arr); + FieldRef& operator=(span arr); /// Set array of values to field /// @tparam T - type. Must be one of: int, int64_t, double /// @param arr - std::vector of T values, which will be setted to field template - FieldRef &operator=(const std::vector &arr) { + FieldRef& operator=(const std::vector& arr) { return operator=(span>(arr)); } /// Set string value to field /// If Item is in Unsafe Mode, then Item will not store str, but just keep pointer to str, /// application *MUST* hold str until end of life of Item /// @param str - pointer to C null-terminated string, which will be setted to field - FieldRef &operator=(const char *str); + FieldRef& operator=(const char* str); /// Set string value
/// If Item is in Unsafe Mode, then Item will not store str, but just keep pointer to str, /// application *MUST* hold str until end of life of Item /// @param str - std::string, which will be setted to field - FieldRef &operator=(const std::string &str); + FieldRef& operator=(const std::string& str); /// Get field index name std::string_view Name() const; @@ -105,15 +105,15 @@ class Item { operator VariantArray() const; /// Set field value /// @param kr - key reference object, which will be set to field - FieldRef &operator=(Variant kr); + FieldRef& operator=(Variant kr); /// Set field value /// @param krs - key reference object, which will be set to field - FieldRef &operator=(const VariantArray &krs); + FieldRef& operator=(const VariantArray& krs); private: - FieldRef(int field, ItemImpl *itemImpl) noexcept : itemImpl_(itemImpl), field_(field) {} - FieldRef(std::string_view jsonPath, ItemImpl *itemImpl) noexcept : itemImpl_(itemImpl), jsonPath_(jsonPath), field_(-1) {} - ItemImpl *itemImpl_; + FieldRef(int field, ItemImpl* itemImpl) noexcept : itemImpl_(itemImpl), field_(field) {} + FieldRef(std::string_view jsonPath, ItemImpl* itemImpl) noexcept : itemImpl_(itemImpl), jsonPath_(jsonPath), field_(-1) {} + ItemImpl* itemImpl_; std::string_view jsonPath_; int field_; }; @@ -124,7 +124,7 @@ class Item { /// @param slice - data slice with Json. /// @param endp - pointer to end of parsed part of slice /// @param pkOnly - if TRUE, that mean a JSON string will be parse only primary key fields - Error FromJSON(std::string_view slice, char **endp = nullptr, bool pkOnly = false) & noexcept; + Error FromJSON(std::string_view slice, char** endp = nullptr, bool pkOnly = false) & noexcept; /// Build item from JSON
/// If Item is in *Unsafe Mode*, then Item will not store slice, but just keep pointer to data in slice, @@ -137,7 +137,7 @@ class Item { /// Builds item from msgpack::object. /// @param buf - msgpack encoded data buffer. /// @param offset - position to start from. - Error FromMsgPack(std::string_view buf, size_t &offset) & noexcept; + Error FromMsgPack(std::string_view buf, size_t& offset) & noexcept; /// Builds item from Protobuf /// @param sbuf - Protobuf encoded data @@ -145,14 +145,14 @@ class Item { /// Packs data in msgpack format /// @param wrser - buffer to serialize data to - Error GetMsgPack(WrSerializer &wrser) & noexcept; + Error GetMsgPack(WrSerializer& wrser) & noexcept; /// Packs data in msgpack format /// @return data slice with MsgPack [[nodiscard]] std::string_view GetMsgPack() &; /// Packs item data to Protobuf /// @param wrser - buffer to serialize data to - Error GetProtobuf(WrSerializer &wrser) & noexcept; + Error GetProtobuf(WrSerializer& wrser) & noexcept; /// Serialize item to CJSON.
/// If Item is in *Unsafe Mode*, then returned slice is allocated in temporary buffer, and can be invalidated by any next operation @@ -192,7 +192,7 @@ class Item { [[nodiscard]] FieldsSet PkFields() const; /// Set additional percepts for modify operation /// @param precepts - strings in format "fieldName=Func()" - void SetPrecepts(const std::vector &precepts) &; + void SetPrecepts(const std::vector& precepts) &; /// Check was names tags updated while modify operation /// @return true: tags was updated. [[nodiscard]] bool IsTagsUpdated() const noexcept; @@ -206,7 +206,7 @@ class Item { /// The advantage of unsafe mode is speed. It does not call extra memory allocation from heap and copying data.
/// The disadvantage of unsafe mode is potentially danger code. Most of C++ stl containters in many cases invalidates references - /// and in unsafe mode caller is responsibe to guarantee, that all resources passed to Item will keep valid - Item &Unsafe(bool enable = true) & noexcept; + Item& Unsafe(bool enable = true) & noexcept; /// Get index type by field id /// @return either index type or Undefined (if index with this number does not exist or PayloadType is not available) KeyValueType GetIndexType(int field) const noexcept; @@ -214,15 +214,15 @@ class Item { /// @param name - field name /// @param itemImpl - item /// @return field's ref - static FieldRef FieldRefByName(std::string_view name, ItemImpl &itemImpl); + static FieldRef FieldRefByName(std::string_view name, ItemImpl& itemImpl); private: - explicit Item(ItemImpl *impl) : impl_(impl) {} - explicit Item(const Error &err) : impl_(nullptr), status_(err) {} + explicit Item(ItemImpl* impl) : impl_(impl) {} + explicit Item(const Error& err) : impl_(nullptr), status_(err) {} void setID(int id) { id_ = id; } void setLSN(int64_t lsn); - ItemImpl *impl_; + ItemImpl* impl_; Error status_; int id_ = -1; friend class NamespaceImpl; diff --git a/cpp_src/core/itemimpl.cc b/cpp_src/core/itemimpl.cc index e96d2c75f..2755900bb 100644 --- a/cpp_src/core/itemimpl.cc +++ b/cpp_src/core/itemimpl.cc @@ -12,7 +12,7 @@ namespace reindexer { -void ItemImpl::SetField(int field, const VariantArray &krs) { +void ItemImpl::SetField(int field, const VariantArray& krs) { validateModifyArray(krs); cjson_ = std::string_view(); payloadValue_.Clone(); @@ -20,8 +20,10 @@ void ItemImpl::SetField(int field, const VariantArray &krs) { !payloadType_.Field(field).Type().Is()) { VariantArray krsCopy; krsCopy.reserve(krs.size()); - if (!holder_) holder_ = std::make_unique>(); - for (auto &kr : krs) { + if (!holder_) { + holder_ = std::make_unique>(); + } + for (auto& kr : krs) { holder_->push_back(kr.As()); krsCopy.emplace_back(p_string{&holder_->back()}); } @@ -31,11 +33,11 @@ void ItemImpl::SetField(int field, const VariantArray &krs) { } } -void ItemImpl::ModifyField(std::string_view jsonPath, const VariantArray &keys, FieldModifyMode mode) { +void ItemImpl::ModifyField(std::string_view jsonPath, const VariantArray& keys, FieldModifyMode mode) { ModifyField(tagsMatcher_.path2indexedtag(jsonPath, mode != FieldModeDrop), keys, mode); } -void ItemImpl::ModifyField(const IndexedTagsPath &tagsPath, const VariantArray &keys, FieldModifyMode mode) { +void ItemImpl::ModifyField(const IndexedTagsPath& tagsPath, const VariantArray& keys, FieldModifyMode mode) { validateModifyArray(keys); payloadValue_.Clone(); Payload pl = GetPayload(); @@ -66,22 +68,22 @@ void ItemImpl::ModifyField(const IndexedTagsPath &tagsPath, const VariantArray & case FieldModeArrayPushFront: throw Error(errLogic, "Update mode is not supported: %d", mode); } - } catch (const Error &e) { + } catch (const Error& e) { throw Error(e.code(), "Error modifying field value: '%s'", e.what()); - } catch (std::exception &e) { + } catch (std::exception& e) { throw Error(errLogic, "Error modifying field value: '%s'", e.what()); } tupleData_ = ser_.DetachLStr(); - pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); + pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); } -void ItemImpl::SetField(std::string_view jsonPath, const VariantArray &keys) { ModifyField(jsonPath, keys, FieldModeSet); } +void ItemImpl::SetField(std::string_view jsonPath, const VariantArray& keys) { ModifyField(jsonPath, keys, FieldModeSet); } void ItemImpl::DropField(std::string_view jsonPath) { ModifyField(jsonPath, {}, FieldModeDrop); } Variant ItemImpl::GetField(int field) { return GetPayload().Get(field, 0); } -void ItemImpl::GetField(int field, VariantArray &values) { GetPayload().Get(field, values); } +void ItemImpl::GetField(int field, VariantArray& values) { GetPayload().Get(field, values); } -Error ItemImpl::FromMsgPack(std::string_view buf, size_t &offset) { +Error ItemImpl::FromMsgPack(std::string_view buf, size_t& offset) { payloadValue_.Clone(); Payload pl = GetPayload(); if (!msgPackDecoder_) { @@ -94,7 +96,7 @@ Error ItemImpl::FromMsgPack(std::string_view buf, size_t &offset) { Error err = msgPackDecoder_->Decode(buf, pl, ser_, offset); if (err.ok()) { tupleData_ = ser_.DetachLStr(); - pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); + pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); } return err; } @@ -111,17 +113,17 @@ Error ItemImpl::FromProtobuf(std::string_view buf) { Error err = decoder.Decode(buf, pl, ser_); if (err.ok()) { tupleData_ = ser_.DetachLStr(); - pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); + pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); } return err; } -Error ItemImpl::GetMsgPack(WrSerializer &wrser) { +Error ItemImpl::GetMsgPack(WrSerializer& wrser) { int startTag = 0; ConstPayload pl = GetConstPayload(); MsgPackEncoder msgpackEncoder(&tagsMatcher_); - const TagsLengths &tagsLengths = msgpackEncoder.GetTagsMeasures(pl); + const TagsLengths& tagsLengths = msgpackEncoder.GetTagsMeasures(pl); MsgPackBuilder msgpackBuilder(wrser, &tagsLengths, &startTag, ObjType::TypePlain, &tagsMatcher_); msgpackEncoder.Encode(pl, msgpackBuilder); @@ -137,7 +139,7 @@ std::string_view ItemImpl::GetMsgPack() { return ser_.Slice(); } -Error ItemImpl::GetProtobuf(WrSerializer &wrser) { +Error ItemImpl::GetProtobuf(WrSerializer& wrser) { assertrx(ns_); ConstPayload pl = GetConstPayload(); ProtobufBuilder protobufBuilder(&wrser, ObjType::TypePlain, schema_.get(), &tagsMatcher_); @@ -147,7 +149,7 @@ Error ItemImpl::GetProtobuf(WrSerializer &wrser) { } // Construct item from compressed json -void ItemImpl::FromCJSON(std::string_view slice, bool pkOnly, Recoder *recoder) { +void ItemImpl::FromCJSON(std::string_view slice, bool pkOnly, Recoder* recoder) { payloadValue_.Clone(); std::string_view data = slice; if (!unsafe_) { @@ -170,7 +172,9 @@ void ItemImpl::FromCJSON(std::string_view slice, bool pkOnly, Recoder *recoder) Payload pl = GetPayload(); pl.Reset(); - if (!holder_) holder_ = std::make_unique>(); + if (!holder_) { + holder_ = std::make_unique>(); + } CJsonDecoder decoder(tagsMatcher_, *holder_); ser_.Reset(); @@ -188,13 +192,15 @@ void ItemImpl::FromCJSON(std::string_view slice, bool pkOnly, Recoder *recoder) } } - if (!rdser.Eof()) throw Error(errParseJson, "Internal error - left unparsed data %d", rdser.Pos()); + if (!rdser.Eof()) { + throw Error(errParseJson, "Internal error - left unparsed data %d", rdser.Pos()); + } tupleData_ = ser_.DetachLStr(); - pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); + pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); } -Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool pkOnly) { +Error ItemImpl::FromJSON(std::string_view slice, char** endp, bool pkOnly) { payloadValue_.Clone(); std::string_view data = slice; cjson_ = std::string_view(); @@ -205,11 +211,11 @@ Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool pkOnly) { try { gason::JsonParser parser(nullptr); parser.Parse(data, &len); - *endp = const_cast(data.data()) + len; + *endp = const_cast(data.data()) + len; sourceData_.reset(new char[len]); std::copy(data.begin(), data.begin() + len, sourceData_.get()); data = std::string_view(sourceData_.get(), len); - } catch (const gason::Exception &e) { + } catch (const gason::Exception& e) { return Error(errParseJson, "Error parsing json: '%s'", e.what()); } } else { @@ -224,11 +230,13 @@ Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool pkOnly) { gason::JsonParser parser(&largeJSONStrings_); try { node = parser.Parse(giftStr(data), &len); - if (node.value.getTag() != gason::JSON_OBJECT) return Error(errParseJson, "Expected json object"); + if (node.value.getTag() != gason::JSON_OBJECT) { + return Error(errParseJson, "Expected json object"); + } if (unsafe_ && endp) { - *endp = const_cast(data.data()) + len; + *endp = const_cast(data.data()) + len; } - } catch (gason::Exception &e) { + } catch (gason::Exception& e) { return Error(errParseJson, "Error parsing json: '%s', pos: %d", e.what(), len); } @@ -243,11 +251,11 @@ Error ItemImpl::FromJSON(std::string_view slice, char **endp, bool pkOnly) { // Put tuple to field[0] tupleData_ = ser_.DetachLStr(); - pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); + pl.Set(0, Variant(p_string(reinterpret_cast(tupleData_.get())), Variant::no_hold_t{})); return err; } -void ItemImpl::FromCJSON(ItemImpl &other, Recoder *recoder) { +void ItemImpl::FromCJSON(ItemImpl& other, Recoder* recoder) { FromCJSON(other.GetCJSON(), false, recoder); cjson_ = {}; } @@ -267,12 +275,14 @@ std::string_view ItemImpl::GetJSON() { std::string_view ItemImpl::GetCJSON(bool withTagsMatcher) { withTagsMatcher = withTagsMatcher && tagsMatcher_.isUpdated(); - if (cjson_.size() && !withTagsMatcher) return cjson_; + if (cjson_.size() && !withTagsMatcher) { + return cjson_; + } ser_.Reset(); return GetCJSON(ser_, withTagsMatcher); } -std::string_view ItemImpl::GetCJSON(WrSerializer &ser, bool withTagsMatcher) { +std::string_view ItemImpl::GetCJSON(WrSerializer& ser, bool withTagsMatcher) { withTagsMatcher = withTagsMatcher && tagsMatcher_.isUpdated(); if (cjson_.size() && !withTagsMatcher) { @@ -307,8 +317,8 @@ VariantArray ItemImpl::GetValueByJSONPath(std::string_view jsonPath) { return krefs; } -void ItemImpl::validateModifyArray(const VariantArray &values) { - for (const auto &v : values) { +void ItemImpl::validateModifyArray(const VariantArray& values) { + for (const auto& v : values) { v.Type().EvaluateOneOf([](OneOf) {}, [](KeyValueType::Tuple) { diff --git a/cpp_src/core/itemimpl.h b/cpp_src/core/itemimpl.h index 18fe391db..999602087 100644 --- a/cpp_src/core/itemimpl.h +++ b/cpp_src/core/itemimpl.h @@ -15,10 +15,10 @@ namespace reindexer { struct ItemImplRawData { ItemImplRawData() = default; explicit ItemImplRawData(PayloadValue v) : payloadValue_(std::move(v)) {} - ItemImplRawData(const ItemImplRawData &) = delete; - ItemImplRawData(ItemImplRawData &&) = default; - ItemImplRawData &operator=(const ItemImplRawData &) = delete; - ItemImplRawData &operator=(ItemImplRawData &&) = default; + ItemImplRawData(const ItemImplRawData&) = delete; + ItemImplRawData(ItemImplRawData&&) = default; + ItemImplRawData& operator=(const ItemImplRawData&) = delete; + ItemImplRawData& operator=(ItemImplRawData&&) = default; PayloadValue payloadValue_; std::unique_ptr tupleData_; @@ -38,7 +38,7 @@ class ItemImpl : public ItemImplRawData { ItemImpl() = default; // Construct empty item - ItemImpl(PayloadType type, const TagsMatcher &tagsMatcher, const FieldsSet &pkFields = {}, std::shared_ptr schema = {}) + ItemImpl(PayloadType type, const TagsMatcher& tagsMatcher, const FieldsSet& pkFields = {}, std::shared_ptr schema = {}) : ItemImplRawData(PayloadValue(type.TotalSize(), nullptr, type.TotalSize() + 0x100)), payloadType_(std::move(type)), tagsMatcher_(tagsMatcher), @@ -48,64 +48,64 @@ class ItemImpl : public ItemImplRawData { } // Construct empty item - ItemImpl(PayloadType type, const TagsMatcher &tagsMatcher, const FieldsSet &pkFields, std::shared_ptr schema, - ItemImplRawData &&rawData) + ItemImpl(PayloadType type, const TagsMatcher& tagsMatcher, const FieldsSet& pkFields, std::shared_ptr schema, + ItemImplRawData&& rawData) : ItemImplRawData(std::move(rawData)), payloadType_(std::move(type)), tagsMatcher_(tagsMatcher), pkFields_(pkFields), schema_(std::move(schema)) {} - ItemImpl(PayloadType type, PayloadValue v, const TagsMatcher &tagsMatcher, std::shared_ptr schema = {}) + ItemImpl(PayloadType type, PayloadValue v, const TagsMatcher& tagsMatcher, std::shared_ptr schema = {}) : ItemImplRawData(std::move(v)), payloadType_(std::move(type)), tagsMatcher_(tagsMatcher), schema_{std::move(schema)} { tagsMatcher_.clearUpdated(); } - ItemImpl(const ItemImpl &) = delete; - ItemImpl(ItemImpl &&) = default; - ItemImpl &operator=(ItemImpl &&) = default; - ItemImpl &operator=(const ItemImpl &) = delete; + ItemImpl(const ItemImpl&) = delete; + ItemImpl(ItemImpl&&) = default; + ItemImpl& operator=(ItemImpl&&) = default; + ItemImpl& operator=(const ItemImpl&) = delete; - void ModifyField(std::string_view jsonPath, const VariantArray &keys, FieldModifyMode mode); - void ModifyField(const IndexedTagsPath &tagsPath, const VariantArray &keys, FieldModifyMode mode); - void SetField(int field, const VariantArray &krs); - void SetField(std::string_view jsonPath, const VariantArray &keys); + void ModifyField(std::string_view jsonPath, const VariantArray& keys, FieldModifyMode mode); + void ModifyField(const IndexedTagsPath& tagsPath, const VariantArray& keys, FieldModifyMode mode); + void SetField(int field, const VariantArray& krs); + void SetField(std::string_view jsonPath, const VariantArray& keys); void DropField(std::string_view jsonPath); Variant GetField(int field); - void GetField(int field, VariantArray &); + void GetField(int field, VariantArray&); FieldsSet PkFields() const { return pkFields_; } int NameTag(std::string_view name) const { return tagsMatcher_.name2tag(name); } VariantArray GetValueByJSONPath(std::string_view jsonPath); std::string_view GetJSON(); - Error FromJSON(std::string_view slice, char **endp = nullptr, bool pkOnly = false); - void FromCJSON(ItemImpl &other, Recoder *); + Error FromJSON(std::string_view slice, char** endp = nullptr, bool pkOnly = false); + void FromCJSON(ItemImpl& other, Recoder*); std::string_view GetCJSON(bool withTagsMatcher = false); - std::string_view GetCJSON(WrSerializer &ser, bool withTagsMatcher = false); - void FromCJSON(std::string_view slice, bool pkOnly = false, Recoder * = nullptr); - Error FromMsgPack(std::string_view sbuf, size_t &offset); + std::string_view GetCJSON(WrSerializer& ser, bool withTagsMatcher = false); + void FromCJSON(std::string_view slice, bool pkOnly = false, Recoder* = nullptr); + Error FromMsgPack(std::string_view sbuf, size_t& offset); Error FromProtobuf(std::string_view sbuf); - Error GetMsgPack(WrSerializer &wrser); + Error GetMsgPack(WrSerializer& wrser); std::string_view GetMsgPack(); - Error GetProtobuf(WrSerializer &wrser); + Error GetProtobuf(WrSerializer& wrser); - const PayloadType &Type() const noexcept { return payloadType_; } - PayloadValue &Value() noexcept { return payloadValue_; } - PayloadValue &RealValue() noexcept { return realValue_; } + const PayloadType& Type() const noexcept { return payloadType_; } + PayloadValue& Value() noexcept { return payloadValue_; } + PayloadValue& RealValue() noexcept { return realValue_; } Payload GetPayload() noexcept { return Payload(payloadType_, payloadValue_); } ConstPayload GetConstPayload() const noexcept { return ConstPayload(payloadType_, payloadValue_); } std::shared_ptr GetSchema() const noexcept { return schema_; } - TagsMatcher &tagsMatcher() noexcept { return tagsMatcher_; } - std::shared_ptr &schema() noexcept { return schema_; } + TagsMatcher& tagsMatcher() noexcept { return tagsMatcher_; } + std::shared_ptr& schema() noexcept { return schema_; } - void SetPrecepts(const std::vector &precepts) { + void SetPrecepts(const std::vector& precepts) { precepts_ = precepts; cjson_ = std::string_view(); } - const std::vector &GetPrecepts() const noexcept { return precepts_; } + const std::vector& GetPrecepts() const noexcept { return precepts_; } void Unsafe(bool enable) noexcept { unsafe_ = enable; } bool IsUnsafe() const noexcept { return unsafe_; } void Clear() { @@ -128,7 +128,7 @@ class ItemImpl : public ItemImplRawData { } void SetNamespace(std::shared_ptr ns) noexcept { ns_ = std::move(ns); } std::shared_ptr GetNamespace() const noexcept { return ns_; } - static void validateModifyArray(const VariantArray &values); + static void validateModifyArray(const VariantArray& values); private: // Index fields payload data diff --git a/cpp_src/core/itemmodifier.cc b/cpp_src/core/itemmodifier.cc index 3676bff56..07cdf12ae 100644 --- a/cpp_src/core/itemmodifier.cc +++ b/cpp_src/core/itemmodifier.cc @@ -9,7 +9,7 @@ namespace reindexer { std::string_view ItemModifier::FieldData::Name() const noexcept { return entry_.Column(); } -void ItemModifier::FieldData::appendAffectedIndexes(const NamespaceImpl &ns, CompositeFlags &affectedComposites) const { +void ItemModifier::FieldData::appendAffectedIndexes(const NamespaceImpl& ns, CompositeFlags& affectedComposites) const { const auto firstCompositePos = ns.indexes_.firstCompositePos(); const auto firstSparsePos = ns.indexes_.firstSparsePos(); const auto totalIndexes = ns.indexes_.totalSize(); @@ -29,8 +29,8 @@ void ItemModifier::FieldData::appendAffectedIndexes(const NamespaceImpl &ns, Com affected.set(Index()); } else { for (int i = 0; i < firstSparsePos; ++i) { - const auto &ptField = ns.payloadType_.Field(i); - for (const auto &jpath : ptField.JsonPaths()) { + const auto& ptField = ns.payloadType_.Field(i); + for (const auto& jpath : ptField.JsonPaths()) { auto tp = ns.tagsMatcher_.path2tag(jpath); if (Tagspath().IsNestedOrEqualTo(tp)) { affected.set(i); @@ -41,11 +41,13 @@ void ItemModifier::FieldData::appendAffectedIndexes(const NamespaceImpl &ns, Com } for (int i = firstCompositePos; i < totalIndexes; ++i) { - const auto &fields = ns.indexes_[i]->Fields(); + const auto& fields = ns.indexes_[i]->Fields(); const auto idxId = i - firstCompositePos; for (const auto f : fields) { - if (f == IndexValueType::SetByJsonPath) continue; + if (f == IndexValueType::SetByJsonPath) { + continue; + } if (affected.test(f)) { affectedComposites[idxId] = true; break; @@ -69,16 +71,18 @@ void ItemModifier::FieldData::appendAffectedIndexes(const NamespaceImpl &ns, Com class ItemModifier::RollBack_ModifiedPayload final : private RollBackBase { public: - RollBack_ModifiedPayload(ItemModifier &modifier, IdType id) noexcept : modifier_{modifier}, itemId_{id} {} - RollBack_ModifiedPayload(RollBack_ModifiedPayload &&) noexcept = default; + RollBack_ModifiedPayload(ItemModifier& modifier, IdType id) noexcept : modifier_{modifier}, itemId_{id} {} + RollBack_ModifiedPayload(RollBack_ModifiedPayload&&) noexcept = default; ~RollBack_ModifiedPayload() override { RollBack(); } void RollBack() { - if (IsDisabled()) return; + if (IsDisabled()) { + return; + } auto indexesCacheCleaner{modifier_.ns_.GetIndexesCacheCleaner()}; - const std::vector &data = modifier_.rollBackIndexData_.IndexStatus(); - PayloadValue &plValue = modifier_.ns_.items_[itemId_]; - NamespaceImpl::IndexesStorage &indexes = modifier_.ns_.indexes_; + const std::vector& data = modifier_.rollBackIndexData_.IndexStatus(); + PayloadValue& plValue = modifier_.ns_.items_[itemId_]; + NamespaceImpl::IndexesStorage& indexes = modifier_.ns_.indexes_; Payload plSave(modifier_.ns_.payloadType_, modifier_.rollBackIndexData_.GetPayloadValueBackup()); @@ -106,7 +110,7 @@ class ItemModifier::RollBack_ModifiedPayload final : private RollBackBase { if (indexes[i]->Opts().IsSparse()) { try { cpl.GetByJsonPath(indexes[i]->Fields().getTagsPath(0), vals, indexes[i]->KeyType()); - } catch (const Error &) { + } catch (const Error&) { vals.resize(0); } modifier_.rollBackIndexData_.CjsonChanged(); @@ -122,7 +126,7 @@ class ItemModifier::RollBack_ModifiedPayload final : private RollBackBase { if (indexes[i]->Opts().IsSparse()) { try { plSave.GetByJsonPath(indexes[i]->Fields().getTagsPath(0), oldData, indexes[i]->KeyType()); - } catch (const Error &) { + } catch (const Error&) { oldData.resize(0); } } else { @@ -140,7 +144,7 @@ class ItemModifier::RollBack_ModifiedPayload final : private RollBackBase { } } if (modifier_.rollBackIndexData_.IsCjsonChanged()) { - const Variant &v = cjsonKref.front(); + const Variant& v = cjsonKref.front(); bool needClearCache{false}; indexes[0]->Delete(v, itemId_, *modifier_.ns_.strHolder(), needClearCache); VariantArray keys; @@ -161,20 +165,20 @@ class ItemModifier::RollBack_ModifiedPayload final : private RollBackBase { } using RollBackBase::Disable; - RollBack_ModifiedPayload(const RollBack_ModifiedPayload &) = delete; - RollBack_ModifiedPayload operator=(const RollBack_ModifiedPayload &) = delete; - RollBack_ModifiedPayload operator=(RollBack_ModifiedPayload &&) = delete; + RollBack_ModifiedPayload(const RollBack_ModifiedPayload&) = delete; + RollBack_ModifiedPayload operator=(const RollBack_ModifiedPayload&) = delete; + RollBack_ModifiedPayload operator=(RollBack_ModifiedPayload&&) = delete; private: - ItemModifier &modifier_; + ItemModifier& modifier_; IdType itemId_; }; -ItemModifier::FieldData::FieldData(const UpdateEntry &entry, NamespaceImpl &ns, CompositeFlags &affectedComposites) +ItemModifier::FieldData::FieldData(const UpdateEntry& entry, NamespaceImpl& ns, CompositeFlags& affectedComposites) : entry_(entry), tagsPathWithLastIndex_{std::nullopt}, arrayIndex_(IndexValueType::NotSet), isIndex_(false) { if (ns.tryGetIndexByName(entry_.Column(), fieldIndex_)) { isIndex_ = true; - const auto &idx = *ns.indexes_[fieldIndex_]; + const auto& idx = *ns.indexes_[fieldIndex_]; auto jsonPathsSize = (idx.Opts().IsSparse() || static_cast(fieldIndex_) >= ns.payloadType_.NumFields()) ? idx.Fields().size() : ns.payloadType_.Field(fieldIndex_).JsonPaths().size(); @@ -183,7 +187,7 @@ ItemModifier::FieldData::FieldData(const UpdateEntry &entry, NamespaceImpl &ns, throw Error(errParams, "Ambiguity when updating field with several json paths by index name: '%s'", entry_.Column()); } - const auto &fields{idx.Fields()}; + const auto& fields{idx.Fields()}; if (fields.size() != 1) { throw Error(errParams, "Cannot update composite index: '%s'", entry_.Column()); } @@ -215,7 +219,9 @@ ItemModifier::FieldData::FieldData(const UpdateEntry &entry, NamespaceImpl &ns, IndexedTagsPath tagsPath = ns.tagsMatcher_.path2indexedtag(entry_.Column(), true); std::string jsonPath; for (size_t i = 0; i < tagsPath.size(); ++i) { - if (i) jsonPath += '.'; + if (i) { + jsonPath += '.'; + } const auto tagName = tagsPath[i].NameTag(); tp.emplace_back(tagName); jsonPath += ns.tagsMatcher_.tag2name(tagName); @@ -232,7 +238,7 @@ ItemModifier::FieldData::FieldData(const UpdateEntry &entry, NamespaceImpl &ns, throw Error(errParams, "Cannot find field by json: '%s'", entry_.Column()); } if (isIndex_) { - auto &lastTag = tagsPath_.back(); + auto& lastTag = tagsPath_.back(); if (lastTag.IsWithIndex()) { tagsPathWithLastIndex_ = tagsPath_; arrayIndex_ = lastTag.Index(); @@ -243,13 +249,13 @@ ItemModifier::FieldData::FieldData(const UpdateEntry &entry, NamespaceImpl &ns, appendAffectedIndexes(ns, affectedComposites); } -ItemModifier::ItemModifier(const std::vector &updateEntries, NamespaceImpl &ns) +ItemModifier::ItemModifier(const std::vector& updateEntries, NamespaceImpl& ns) : ns_(ns), updateEntries_(updateEntries), rollBackIndexData_(ns_.indexes_.totalSize()), affectedComposites_(ns_.indexes_.totalSize() - ns_.indexes_.firstCompositePos(), false) { - for (const UpdateEntry &updateField : updateEntries_) { - for (const auto &v : updateField.Values()) { + for (const UpdateEntry& updateField : updateEntries_) { + for (const auto& v : updateField.Values()) { v.Type().EvaluateOneOf([](OneOf) {}, [](KeyValueType::Tuple) { @@ -268,8 +274,8 @@ ItemModifier::ItemModifier(const std::vector &updateEntries, Namesp } } -[[nodiscard]] bool ItemModifier::Modify(IdType itemId, const NsContext &ctx) { - PayloadValue &pv = ns_.items_[itemId]; +[[nodiscard]] bool ItemModifier::Modify(IdType itemId, const NsContext& ctx) { + PayloadValue& pv = ns_.items_[itemId]; Payload pl(ns_.payloadType_, pv); pv.Clone(pl.RealSize()); @@ -281,7 +287,7 @@ ItemModifier::ItemModifier(const std::vector &updateEntries, Namesp deleteItemFromComposite(itemId); try { VariantArray values; - for (FieldData &field : fieldsToModify_) { + for (FieldData& field : fieldsToModify_) { // values must be assigned a value in if else below if (field.Details().IsExpression()) { assertrx(field.Details().Values().size() > 0); @@ -315,14 +321,14 @@ ItemModifier::ItemModifier(const std::vector &updateEntries, Namesp return rollBackIndexData_.IsPkModified(); } -void ItemModifier::modifyCJSON(IdType id, FieldData &field, VariantArray &values) { - PayloadValue &plData = ns_.items_[id]; +void ItemModifier::modifyCJSON(IdType id, FieldData& field, VariantArray& values) { + PayloadValue& plData = ns_.items_[id]; Payload pl(*ns_.payloadType_.get(), plData); VariantArray cjsonKref; pl.Get(0, cjsonKref); cjsonCache_.Reset(); - const Variant &v = cjsonKref.front(); + const Variant& v = cjsonKref.front(); if (v.Type().Is()) { cjsonCache_.Assign(std::string_view(p_string(v))); } @@ -338,7 +344,7 @@ void ItemModifier::modifyCJSON(IdType id, FieldData &field, VariantArray &values } item.setID(id); - ItemImpl *impl = item.impl_; + ItemImpl* impl = item.impl_; ns_.setFieldsBasedOnPrecepts(impl); ns_.updateTagsMatcherFromItem(impl); @@ -353,7 +359,7 @@ void ItemModifier::modifyCJSON(IdType id, FieldData &field, VariantArray &values do { // update the indexes, and then tuple (1,2,...,0) fieldIdx %= ns_.indexes_.firstCompositePos(); - Index &index = *(ns_.indexes_[fieldIdx]); + Index& index = *(ns_.indexes_[fieldIdx]); bool isIndexSparse = index.Opts().IsSparse(); assertrx(!isIndexSparse || (isIndexSparse && index.Fields().getTagsPathsLength() > 0)); @@ -361,7 +367,7 @@ void ItemModifier::modifyCJSON(IdType id, FieldData &field, VariantArray &values assertrx(index.Fields().getTagsPathsLength() > 0); try { plNew.GetByJsonPath(index.Fields().getTagsPath(0), ns_.skrefs, index.KeyType()); - } catch (const Error &) { + } catch (const Error&) { ns_.skrefs.resize(0); } } else { @@ -369,19 +375,23 @@ void ItemModifier::modifyCJSON(IdType id, FieldData &field, VariantArray &values } if (index.Opts().GetCollateMode() == CollateUTF8) { - for (auto &key : ns_.skrefs) key.EnsureUTF8(); + for (auto& key : ns_.skrefs) { + key.EnsureUTF8(); + } } if ((fieldIdx == 0) && (cjsonCache_.Size() > 0)) { bool needClearCache{false}; rollBackIndexData_.CjsonChanged(); index.Delete(Variant(cjsonCache_.Get()), id, *strHolder, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } else { if (isIndexSparse) { try { pl.GetByJsonPath(index.Fields().getTagsPath(0), ns_.krefs, index.KeyType()); - } catch (const Error &) { + } catch (const Error&) { ns_.krefs.resize(0); } } else if (index.Opts().IsArray()) { @@ -389,18 +399,24 @@ void ItemModifier::modifyCJSON(IdType id, FieldData &field, VariantArray &values } else { pl.Get(fieldIdx, ns_.krefs); } - if (ns_.krefs == ns_.skrefs) continue; + if (ns_.krefs == ns_.skrefs) { + continue; + } bool needClearCache{false}; rollBackIndexData_.IndexChanged(fieldIdx, index.Opts().IsPK()); index.Delete(ns_.krefs, id, *strHolder, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } ns_.krefs.resize(0); bool needClearCache{false}; rollBackIndexData_.IndexChanged(fieldIdx, index.Opts().IsPK()); index.Upsert(ns_.krefs, ns_.skrefs, id, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } if (!isIndexSparse) { pl.Set(fieldIdx, ns_.krefs); @@ -418,7 +434,7 @@ void ItemModifier::deleteItemFromComposite(IdType itemId) { for (int i = firstCompositePos; i < totalIndexes; ++i) { if (affectedComposites_[i - firstCompositePos]) { bool needClearCache{false}; - const auto &compositeIdx = ns_.indexes_[i]; + const auto& compositeIdx = ns_.indexes_[i]; rollBackIndexData_.IndexAndCJsonChanged(i, compositeIdx->Opts().IsPK()); compositeIdx->Delete(Variant(ns_.items_[itemId]), itemId, *strHolder, needClearCache); if (needClearCache && compositeIdx->IsOrdered()) { @@ -434,7 +450,7 @@ void ItemModifier::insertItemIntoComposite(IdType itemId) { for (int i = firstCompositePos; i < totalIndexes; ++i) { if (affectedComposites_[i - firstCompositePos]) { bool needClearCache{false}; - auto &compositeIdx = ns_.indexes_[i]; + auto& compositeIdx = ns_.indexes_[i]; rollBackIndexData_.IndexChanged(i, compositeIdx->Opts().IsPK()); compositeIdx->Upsert(Variant(ns_.items_[itemId]), itemId, needClearCache); if (needClearCache && compositeIdx->IsOrdered()) { @@ -444,9 +460,9 @@ void ItemModifier::insertItemIntoComposite(IdType itemId) { } } -void ItemModifier::modifyField(IdType itemId, FieldData &field, Payload &pl, VariantArray &values) { +void ItemModifier::modifyField(IdType itemId, FieldData& field, Payload& pl, VariantArray& values) { assertrx_throw(field.IsIndex()); - Index &index = *(ns_.indexes_[field.Index()]); + Index& index = *(ns_.indexes_[field.Index()]); if (!index.Opts().IsSparse() && field.Details().Mode() == FieldModeDrop /*&& !(field.arrayIndex() != IndexValueType::NotSet || field.tagspath().back().IsArrayNode())*/) { // TODO #1218 allow to drop array fields throw Error(errLogic, "It's only possible to drop sparse or non-index fields via UPDATE statement!"); @@ -458,7 +474,9 @@ void ItemModifier::modifyField(IdType itemId, FieldData &field, Payload &pl, Var } if (index.Opts().GetCollateMode() == CollateUTF8) { - for (const Variant &key : values) key.EnsureUTF8(); + for (const Variant& key : values) { + key.EnsureUTF8(); + } } auto strHolder = ns_.strHolder(); @@ -473,7 +491,7 @@ void ItemModifier::modifyField(IdType itemId, FieldData &field, Payload &pl, Var Variant oldTupleValue = item.GetField(0); oldTupleValue.EnsureHold(); bool needClearCache{false}; - auto &tupleIdx = ns_.indexes_[0]; + auto& tupleIdx = ns_.indexes_[0]; tupleIdx->Delete(oldTupleValue, itemId, *strHolder, needClearCache); Variant tupleValue; std::exception_ptr exception; @@ -483,7 +501,9 @@ void ItemModifier::modifyField(IdType itemId, FieldData &field, Payload &pl, Var exception = std::current_exception(); } tupleValue = tupleIdx->Upsert(item.GetField(0), itemId, needClearCache); - if (needClearCache && tupleIdx->IsOrdered()) indexesCacheCleaner.Add(tupleIdx->SortId()); + if (needClearCache && tupleIdx->IsOrdered()) { + indexesCacheCleaner.Add(tupleIdx->SortId()); + } pl.Set(0, std::move(tupleValue)); if (exception) { std::rethrow_exception(exception); @@ -491,8 +511,8 @@ void ItemModifier::modifyField(IdType itemId, FieldData &field, Payload &pl, Var } } -void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, VariantArray &values, Payload &pl) { - Index &index = *(ns_.indexes_[field.Index()]); +void ItemModifier::modifyIndexValues(IdType itemId, const FieldData& field, VariantArray& values, Payload& pl) { + Index& index = *(ns_.indexes_[field.Index()]); if (values.IsNullValue() && !index.Opts().IsArray()) { throw Error(errParams, "Non-array index fields cannot be set to null!"); } @@ -500,7 +520,7 @@ void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, Vari auto indexesCacheCleaner{ns_.GetIndexesCacheCleaner()}; bool updateArrayPart = field.ArrayIndex() >= 0; bool isForAllItems = false; - for (const auto &tag : field.Tagspath()) { + for (const auto& tag : field.Tagspath()) { if (tag.IsArrayNode()) { updateArrayPart = true; } @@ -514,7 +534,7 @@ void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, Vari } ns_.krefs.resize(0); - for (Variant &key : values) { + for (Variant& key : values) { key.convert(index.KeyType()); } @@ -524,7 +544,7 @@ void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, Vari } int offset = -1, length = -1; isForAllItems = false; - for (const auto &tag : field.Tagspath()) { // TODO: Move to FieldEntry? + for (const auto& tag : field.Tagspath()) { // TODO: Move to FieldEntry? if (tag.IsForAllItems()) { isForAllItems = true; continue; @@ -536,9 +556,9 @@ void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, Vari ns_.skrefs = pl.GetIndexedArrayData(field.TagspathWithLastIndex(), field.Index(), offset, length); if (offset < 0 || length < 0) { - const auto &path = field.TagspathWithLastIndex(); + const auto& path = field.TagspathWithLastIndex(); std::string indexesStr; - for (auto &p : path) { + for (auto& p : path) { if (p.Index() >= 0) { if (indexesStr.size()) { indexesStr.append(","); @@ -556,13 +576,17 @@ void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, Vari bool needClearCache{false}; rollBackIndexData_.IndexChanged(field.Index(), index.Opts().IsPK()); index.Delete(ns_.skrefs.front(), itemId, *strHolder, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } bool needClearCache{false}; rollBackIndexData_.IndexChanged(field.Index(), index.Opts().IsPK()); index.Upsert(ns_.krefs, values, itemId, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } if (isForAllItems) { for (int i = offset, end = offset + length; i < end; ++i) { @@ -609,13 +633,17 @@ void ItemModifier::modifyIndexValues(IdType itemId, const FieldData &field, Vari bool needClearCache{false}; rollBackIndexData_.IndexChanged(field.Index(), index.Opts().IsPK()); index.Delete(ns_.skrefs, itemId, *strHolder, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } bool needClearCache{false}; rollBackIndexData_.IndexChanged(field.Index(), index.Opts().IsPK()); index.Upsert(ns_.krefs, kConcatIndexValues ? concatValues : values, itemId, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } if (!index.Opts().IsSparse()) { pl.Set(field.Index(), ns_.krefs); } diff --git a/cpp_src/core/itemmodifier.h b/cpp_src/core/itemmodifier.h index ea14afbd3..9cd09ab6d 100644 --- a/cpp_src/core/itemmodifier.h +++ b/cpp_src/core/itemmodifier.h @@ -12,23 +12,23 @@ class UpdateEntry; class ItemModifier { public: - ItemModifier(const std::vector &, NamespaceImpl &ns); - ItemModifier(const ItemModifier &) = delete; - ItemModifier &operator=(const ItemModifier &) = delete; - ItemModifier(ItemModifier &&) = delete; - ItemModifier &operator=(ItemModifier &&) = delete; + ItemModifier(const std::vector&, NamespaceImpl& ns); + ItemModifier(const ItemModifier&) = delete; + ItemModifier& operator=(const ItemModifier&) = delete; + ItemModifier(ItemModifier&&) = delete; + ItemModifier& operator=(ItemModifier&&) = delete; - [[nodiscard]] bool Modify(IdType itemId, const NsContext &ctx); - PayloadValue &GetPayloadValueBackup() { return rollBackIndexData_.GetPayloadValueBackup(); } + [[nodiscard]] bool Modify(IdType itemId, const NsContext& ctx); + PayloadValue& GetPayloadValueBackup() { return rollBackIndexData_.GetPayloadValueBackup(); } private: using CompositeFlags = h_vector; class FieldData { public: - FieldData(const UpdateEntry &entry, NamespaceImpl &ns, CompositeFlags &affectedComposites); - const UpdateEntry &Details() const noexcept { return entry_; } - const IndexedTagsPath &Tagspath() const noexcept { return tagsPath_; } - const IndexedTagsPath &TagspathWithLastIndex() const noexcept { + FieldData(const UpdateEntry& entry, NamespaceImpl& ns, CompositeFlags& affectedComposites); + const UpdateEntry& Details() const noexcept { return entry_; } + const IndexedTagsPath& Tagspath() const noexcept { return tagsPath_; } + const IndexedTagsPath& TagspathWithLastIndex() const noexcept { return tagsPathWithLastIndex_ ? *tagsPathWithLastIndex_ : tagsPath_; } int ArrayIndex() const noexcept { return arrayIndex_; } @@ -37,9 +37,9 @@ class ItemModifier { std::string_view Name() const noexcept; private: - void appendAffectedIndexes(const NamespaceImpl &ns, CompositeFlags &affectedComposites) const; + void appendAffectedIndexes(const NamespaceImpl& ns, CompositeFlags& affectedComposites) const; - const UpdateEntry &entry_; + const UpdateEntry& entry_; IndexedTagsPath tagsPath_; std::optional tagsPathWithLastIndex_; int fieldIndex_{IndexValueType::SetByJsonPath}; @@ -65,15 +65,15 @@ class ItemModifier { std::string_view cjson_; }; - void modifyField(IdType itemId, FieldData &field, Payload &pl, VariantArray &values); - void modifyCJSON(IdType itemId, FieldData &field, VariantArray &values); - void modifyIndexValues(IdType itemId, const FieldData &field, VariantArray &values, Payload &pl); + void modifyField(IdType itemId, FieldData& field, Payload& pl, VariantArray& values); + void modifyCJSON(IdType itemId, FieldData& field, VariantArray& values); + void modifyIndexValues(IdType itemId, const FieldData& field, VariantArray& values, Payload& pl); void deleteItemFromComposite(IdType itemId); void insertItemIntoComposite(IdType itemId); - NamespaceImpl &ns_; - const std::vector &updateEntries_; + NamespaceImpl& ns_; + const std::vector& updateEntries_; std::vector fieldsToModify_; CJsonCache cjsonCache_; @@ -82,7 +82,7 @@ class ItemModifier { class IndexRollBack { public: IndexRollBack(int indexCount) { data_.resize(indexCount); } - void Reset(PayloadValue &pv) { + void Reset(PayloadValue& pv) { pvSave_ = pv; pvSave_.Clone(); std::fill(data_.begin(), data_.end(), false); @@ -99,8 +99,8 @@ class ItemModifier { pkModified_ = pkModified_ || isPk; } void CjsonChanged() noexcept { cjsonChanged_ = true; } - PayloadValue &GetPayloadValueBackup() noexcept { return pvSave_; } - const std::vector &IndexStatus() const noexcept { return data_; } + PayloadValue& GetPayloadValueBackup() noexcept { return pvSave_; } + const std::vector& IndexStatus() const noexcept { return data_; } bool IsCjsonChanged() const noexcept { return cjsonChanged_; } bool IsPkModified() const noexcept { return pkModified_; } diff --git a/cpp_src/core/joincache.h b/cpp_src/core/joincache.h index 07fa4275c..63314f8d0 100644 --- a/cpp_src/core/joincache.h +++ b/cpp_src/core/joincache.h @@ -10,17 +10,17 @@ namespace reindexer { struct JoinCacheKey { JoinCacheKey() = default; - JoinCacheKey(JoinCacheKey &&other) = default; - JoinCacheKey(const JoinCacheKey &other) = default; - JoinCacheKey &operator=(JoinCacheKey &&other) = default; - JoinCacheKey &operator=(const JoinCacheKey &other) = delete; - void SetData(const Query &q) { + JoinCacheKey(JoinCacheKey&& other) = default; + JoinCacheKey(const JoinCacheKey& other) = default; + JoinCacheKey& operator=(JoinCacheKey&& other) = default; + JoinCacheKey& operator=(const JoinCacheKey& other) = delete; + void SetData(const Query& q) { WrSerializer ser; q.Serialize(ser, (SkipJoinQueries | SkipMergeQueries)); buf_.reserve(buf_.size() + ser.Len()); buf_.insert(buf_.end(), ser.Buf(), ser.Buf() + ser.Len()); } - void SetData(const Query &q1, const Query &q2) { + void SetData(const Query& q1, const Query& q2) { WrSerializer ser; q1.Serialize(ser, (SkipJoinQueries | SkipMergeQueries)); q2.Serialize(ser, (SkipJoinQueries | SkipMergeQueries)); @@ -32,12 +32,12 @@ struct JoinCacheKey { h_vector buf_; }; struct equal_join_cache_key { - bool operator()(const JoinCacheKey &lhs, const JoinCacheKey &rhs) const { + bool operator()(const JoinCacheKey& lhs, const JoinCacheKey& rhs) const { return (lhs.buf_.size() == rhs.buf_.size() && memcmp(lhs.buf_.data(), rhs.buf_.data(), lhs.buf_.size()) == 0); } }; struct hash_join_cache_key { - size_t operator()(const JoinCacheKey &cache) const { + size_t operator()(const JoinCacheKey& cache) const { uint64_t hash[2]; MurmurHash3_x64_128(cache.buf_.data(), cache.buf_.size(), 0, &hash); return hash[0]; diff --git a/cpp_src/core/keyvalue/key_string.h b/cpp_src/core/keyvalue/key_string.h index dd70626e2..94b678fec 100644 --- a/cpp_src/core/keyvalue/key_string.h +++ b/cpp_src/core/keyvalue/key_string.h @@ -17,19 +17,19 @@ class base_key_string : public std::string { bind(); } template - base_key_string(Args &&...args) : std::string(std::forward(args)...) { + base_key_string(Args&&... args) : std::string(std::forward(args)...) { export_hdr_.refcounter.store(0, std::memory_order_release); bind(); } template - void assign(Args &&...args) { + void assign(Args&&... args) { const_string::assign(std::forward(args)...); bind(); } static ptrdiff_t export_hdr_offset() noexcept { static base_key_string sample; - return ptrdiff_t(reinterpret_cast(&sample.export_hdr_) - reinterpret_cast(&sample)); + return ptrdiff_t(reinterpret_cast(&sample.export_hdr_) - reinterpret_cast(&sample)); } size_t heap_size() noexcept { // Check for SSO (small string optimization) @@ -42,38 +42,38 @@ class base_key_string : public std::string { // delete all modification methods - to be sure, that base_key_string is mutable, and export will not invalidate after construction iterator begin() = delete; iterator end() = delete; - char &operator[](int) = delete; + char& operator[](int) = delete; template - void insert(Args &&...args) = delete; + void insert(Args&&... args) = delete; template - void append(Args &&...args) = delete; + void append(Args&&... args) = delete; template - void copy(Args &&...args) = delete; + void copy(Args&&... args) = delete; template - void replace(Args &&...args) = delete; + void replace(Args&&... args) = delete; void push_back(char c) = delete; template - void erase(Args &&...args) = delete; + void erase(Args&&... args) = delete; template - void reserve(Args &&...args) = delete; + void reserve(Args&&... args) = delete; template - void resize(Args &&...args) = delete; + void resize(Args&&... args) = delete; void at(int) = delete; void shrink_to_fit() = delete; void clear() = delete; protected: - friend void intrusive_ptr_add_ref(base_key_string *x) noexcept { + friend void intrusive_ptr_add_ref(base_key_string* x) noexcept { if (x) { x->export_hdr_.refcounter.fetch_add(1, std::memory_order_relaxed); } } - friend void intrusive_ptr_release(base_key_string *x) noexcept { + friend void intrusive_ptr_release(base_key_string* x) noexcept { if (x && x->export_hdr_.refcounter.fetch_sub(1, std::memory_order_acq_rel) == 1) { delete x; // NOLINT(*.NewDelete) False positive } } - friend bool intrusive_ptr_is_unique(base_key_string *x) noexcept { + friend bool intrusive_ptr_is_unique(base_key_string* x) noexcept { // std::memory_order_acquire - is essential for COW constructions based on intrusive_ptr return !x || (x->export_hdr_.refcounter.load(std::memory_order_acquire) == 1); } @@ -84,7 +84,7 @@ class base_key_string : public std::string { } struct export_hdr { - const void *cstr; + const void* cstr; int32_t len; std::atomic refcounter; } export_hdr_; @@ -99,16 +99,16 @@ class key_string : public intrusive_ptr { }; template -key_string make_key_string(Args &&...args) { +key_string make_key_string(Args&&... args) { return key_string(new base_key_string(std::forward(args)...)); } -inline static bool operator==(const key_string &rhs, const key_string &lhs) noexcept { return *rhs == *lhs; } +inline static bool operator==(const key_string& rhs, const key_string& lhs) noexcept { return *rhs == *lhs; } // Unchecked cast to derived class! // It assumes, that all strings in payload are intrusive_ptr -inline void key_string_add_ref(std::string *str) noexcept { intrusive_ptr_add_ref(reinterpret_cast(str)); } -inline void key_string_release(std::string *str) noexcept { intrusive_ptr_release(reinterpret_cast(str)); } +inline void key_string_add_ref(std::string* str) noexcept { intrusive_ptr_add_ref(reinterpret_cast(str)); } +inline void key_string_release(std::string* str) noexcept { intrusive_ptr_release(reinterpret_cast(str)); } template <> struct is_recommends_sc_hash_map { @@ -120,13 +120,13 @@ namespace std { template <> struct hash { public: - size_t operator()(const reindexer::base_key_string &obj) const { return hash()(obj); } + size_t operator()(const reindexer::base_key_string& obj) const { return hash()(obj); } }; template <> struct hash { public: - size_t operator()(const reindexer::key_string &obj) const { return hash()(*obj); } + size_t operator()(const reindexer::key_string& obj) const { return hash()(*obj); } }; } // namespace std diff --git a/cpp_src/core/keyvalue/p_string.cc b/cpp_src/core/keyvalue/p_string.cc index f36990497..e63b07dae 100644 --- a/cpp_src/core/keyvalue/p_string.cc +++ b/cpp_src/core/keyvalue/p_string.cc @@ -3,14 +3,16 @@ #include namespace reindexer { -void p_string::Dump(std::ostream &os) const { +void p_string::Dump(std::ostream& os) const { os << "{p: " << std::hex << v << std::dec; if (v) { const auto l = length(); os << ", length: " << l << ", [" << std::hex; - const char *d = data(); + const char* d = data(); for (size_t i = 0; i < l; ++i) { - if (i != 0) os << ' '; + if (i != 0) { + os << ' '; + } os << static_cast(d[i]); } os << std::dec << ']'; diff --git a/cpp_src/core/keyvalue/p_string.h b/cpp_src/core/keyvalue/p_string.h index d4c76bebe..985a3df9b 100644 --- a/cpp_src/core/keyvalue/p_string.h +++ b/cpp_src/core/keyvalue/p_string.h @@ -20,11 +20,11 @@ struct v_string_hdr { struct l_msgpack_hdr { uint32_t size; - const char *ptr; + const char* ptr; }; struct json_string_ftr { - const char *data; + const char* data; }; // Dark @@ -49,67 +49,67 @@ struct p_string { constexpr static uint64_t tagShift = 59ULL; constexpr static uint64_t tagMask = 0x7ULL << tagShift; - explicit p_string(const l_string_hdr *lstr) noexcept : v((uintptr_t(lstr) & ~tagMask) | (tagLstr << tagShift)) {} - explicit p_string(const v_string_hdr *vstr) noexcept : v((uintptr_t(vstr) & ~tagMask) | (tagVstr << tagShift)) {} - explicit p_string(const l_msgpack_hdr *mstr) noexcept : v((uintptr_t(mstr) & ~tagMask) | (tagMsgPackStr << tagShift)) {} - explicit p_string(const char *cstr) noexcept : v((uintptr_t(cstr) & ~tagMask) | (tagCstr << tagShift)) {} + explicit p_string(const l_string_hdr* lstr) noexcept : v((uintptr_t(lstr) & ~tagMask) | (tagLstr << tagShift)) {} + explicit p_string(const v_string_hdr* vstr) noexcept : v((uintptr_t(vstr) & ~tagMask) | (tagVstr << tagShift)) {} + explicit p_string(const l_msgpack_hdr* mstr) noexcept : v((uintptr_t(mstr) & ~tagMask) | (tagMsgPackStr << tagShift)) {} + explicit p_string(const char* cstr) noexcept : v((uintptr_t(cstr) & ~tagMask) | (tagCstr << tagShift)) {} explicit p_string(const json_string_ftr jstr) noexcept : v((uintptr_t(jstr.data) & ~tagMask) | (tagJsonStr << tagShift)) {} - explicit p_string(const std::string *str) noexcept : v((uintptr_t(str) & ~tagMask) | (tagCxxstr << tagShift)) {} - explicit p_string(const key_string &str) noexcept : v((uintptr_t(str.get()) & ~tagMask) | (tagKeyString << tagShift)) {} - explicit p_string(const std::string_view *ptr) noexcept : v((uintptr_t(ptr) & ~tagMask) | (tagSlice << tagShift)) {} + explicit p_string(const std::string* str) noexcept : v((uintptr_t(str) & ~tagMask) | (tagCxxstr << tagShift)) {} + explicit p_string(const key_string& str) noexcept : v((uintptr_t(str.get()) & ~tagMask) | (tagKeyString << tagShift)) {} + explicit p_string(const std::string_view* ptr) noexcept : v((uintptr_t(ptr) & ~tagMask) | (tagSlice << tagShift)) {} p_string() noexcept : v(0) {} operator std::string_view() const noexcept { switch (type()) { case tagCstr: { - const auto str = reinterpret_cast(ptr()); + const auto str = reinterpret_cast(ptr()); return std::string_view(str, strlen(str)); } case tagMsgPackStr: { - const auto &str = *reinterpret_cast(ptr()); + const auto& str = *reinterpret_cast(ptr()); return std::string_view(str.ptr, str.size); } case tagCxxstr: case tagKeyString: - return std::string_view(*reinterpret_cast(ptr())); + return std::string_view(*reinterpret_cast(ptr())); case tagSlice: - return *reinterpret_cast(ptr()); + return *reinterpret_cast(ptr()); case tagLstr: { - const auto &str = *reinterpret_cast(ptr()); + const auto& str = *reinterpret_cast(ptr()); return std::string_view(&str.data[0], str.length); } case tagVstr: { - auto p = reinterpret_cast(ptr()); + auto p = reinterpret_cast(ptr()); auto l = scan_varint(10, p); - return std::string_view(reinterpret_cast(p) + l, parse_uint32(l, p)); + return std::string_view(reinterpret_cast(p) + l, parse_uint32(l, p)); } case tagJsonStr: { - return json_string::to_string_view(reinterpret_cast(ptr())); + return json_string::to_string_view(reinterpret_cast(ptr())); } default: abort(); } } - const char *data() const noexcept { + const char* data() const noexcept { switch (type()) { case tagCstr: - return reinterpret_cast(ptr()); + return reinterpret_cast(ptr()); case tagCxxstr: case tagKeyString: - return (reinterpret_cast(ptr()))->data(); + return (reinterpret_cast(ptr()))->data(); case tagMsgPackStr: - return (reinterpret_cast(ptr()))->ptr; + return (reinterpret_cast(ptr()))->ptr; case tagSlice: - return (reinterpret_cast(ptr()))->data(); + return (reinterpret_cast(ptr()))->data(); case tagLstr: - return &(reinterpret_cast(ptr()))->data[0]; + return &(reinterpret_cast(ptr()))->data[0]; case tagVstr: { - auto p = reinterpret_cast(ptr()); + auto p = reinterpret_cast(ptr()); auto l = scan_varint(10, p); - return reinterpret_cast(p) + l; + return reinterpret_cast(p) + l; } case tagJsonStr: { - const auto sv = json_string::to_string_view(reinterpret_cast(ptr())); + const auto sv = json_string::to_string_view(reinterpret_cast(ptr())); return sv.data(); } default: @@ -121,23 +121,23 @@ struct p_string { if (v) { switch (type()) { case tagCstr: - return strlen(reinterpret_cast(ptr())); + return strlen(reinterpret_cast(ptr())); case tagCxxstr: case tagKeyString: - return (reinterpret_cast(ptr()))->length(); + return (reinterpret_cast(ptr()))->length(); case tagSlice: - return (reinterpret_cast(ptr()))->size(); + return (reinterpret_cast(ptr()))->size(); case tagLstr: - return (reinterpret_cast(ptr()))->length; + return (reinterpret_cast(ptr()))->length; case tagMsgPackStr: - return (reinterpret_cast(ptr()))->size; + return (reinterpret_cast(ptr()))->size; case tagVstr: { - auto p = reinterpret_cast(ptr()); + auto p = reinterpret_cast(ptr()); auto l = scan_varint(10, p); return parse_uint32(l, p); } case tagJsonStr: { - return json_string::length(reinterpret_cast(ptr())); + return json_string::length(reinterpret_cast(ptr())); } default: abort(); @@ -156,23 +156,23 @@ struct p_string { bool operator==(p_string other) const noexcept { return compare(other) == 0; } bool operator>=(p_string other) const noexcept { return compare(other) >= 0; } bool operator<=(p_string other) const noexcept { return compare(other) <= 0; } - const std::string *getCxxstr() const noexcept { + const std::string* getCxxstr() const noexcept { assertrx(type() == tagCxxstr || type() == tagKeyString); - return reinterpret_cast(ptr()); + return reinterpret_cast(ptr()); } key_string getKeyString() const noexcept { assertrx(type() == tagKeyString); - auto str = reinterpret_cast(const_cast(ptr())); + auto str = reinterpret_cast(const_cast(ptr())); return key_string(str); } int type() const noexcept { return (v & tagMask) >> tagShift; } std::string toString() const { return std::string(data(), length()); } - void Dump(std::ostream &os) const; + void Dump(std::ostream& os) const; protected: - const void *ptr() const noexcept { return v ? reinterpret_cast(v & ~tagMask) : ""; } + const void* ptr() const noexcept { return v ? reinterpret_cast(v & ~tagMask) : ""; } uint64_t v; }; @@ -182,10 +182,10 @@ inline span giftStr(p_string s) noexcept { if (s.type() == p_string::tagCxxstr) { // Trying to avoid COW-string problems auto strPtr = s.getCxxstr(); - return span(const_cast(strPtr)->data(), strPtr->size()); + return span(const_cast(strPtr)->data(), strPtr->size()); } #endif // _GLIBCXX_USE_CXX11_ABI - return span(const_cast(s.data()), s.size()); + return span(const_cast(s.data()), s.size()); } } // namespace reindexer @@ -193,7 +193,7 @@ namespace std { template <> struct hash { public: - size_t operator()(const reindexer::p_string &str) const noexcept { return reindexer::_Hash_bytes(str.data(), str.length()); } + size_t operator()(const reindexer::p_string& str) const noexcept { return reindexer::_Hash_bytes(str.data(), str.length()); } }; } // namespace std diff --git a/cpp_src/core/keyvalue/relaxed_variant_hash.h b/cpp_src/core/keyvalue/relaxed_variant_hash.h index 3fa3d6ea2..95df74da1 100644 --- a/cpp_src/core/keyvalue/relaxed_variant_hash.h +++ b/cpp_src/core/keyvalue/relaxed_variant_hash.h @@ -7,7 +7,7 @@ namespace reindexer { template struct RelaxedComparator { - static bool equal(const Variant &lhs, const Variant &rhs) { + static bool equal(const Variant& lhs, const Variant& rhs) { return lhs.RelaxCompare(rhs) == ComparationResult::Eq; } }; @@ -15,26 +15,14 @@ struct RelaxedComparator { template struct RelaxedHasher { constexpr static size_t indexesCount = notComparable == NotComparable::Return ? 7 : 6; - static std::pair hash(const Variant &v) noexcept(notComparable == NotComparable::Return) { + static std::pair hash(const Variant& v) noexcept(notComparable == NotComparable::Return) { return v.Type().EvaluateOneOf( - overloaded{[&v](KeyValueType::Bool) noexcept { - return std::pair{0, v.Hash()}; - }, - [&v](KeyValueType::Int) noexcept { - return std::pair{1, v.Hash()}; - }, - [&v](KeyValueType::Int64) noexcept { - return std::pair{2, v.Hash()}; - }, - [&v](KeyValueType::Double) noexcept { - return std::pair{3, v.Hash()}; - }, - [&v](KeyValueType::String) noexcept { - return std::pair{4, v.Hash()}; - }, - [&v](KeyValueType::Uuid) noexcept { - return std::pair{5, v.Hash()}; - }, + overloaded{[&v](KeyValueType::Bool) noexcept { return std::pair{0, v.Hash()}; }, + [&v](KeyValueType::Int) noexcept { return std::pair{1, v.Hash()}; }, + [&v](KeyValueType::Int64) noexcept { return std::pair{2, v.Hash()}; }, + [&v](KeyValueType::Double) noexcept { return std::pair{3, v.Hash()}; }, + [&v](KeyValueType::String) noexcept { return std::pair{4, v.Hash()}; }, + [&v](KeyValueType::Uuid) noexcept { return std::pair{5, v.Hash()}; }, [&v](OneOf) noexcept( notComparable == NotComparable::Return) -> std::pair { if constexpr (notComparable == NotComparable::Return) { @@ -44,7 +32,7 @@ struct RelaxedHasher { } }}); } - static size_t hash(size_t i, const Variant &v) { + static size_t hash(size_t i, const Variant& v) { switch (i) { case 0: return v.Type().EvaluateOneOf( diff --git a/cpp_src/core/keyvalue/uuid.h b/cpp_src/core/keyvalue/uuid.h index 01c3ad81d..731450ad8 100644 --- a/cpp_src/core/keyvalue/uuid.h +++ b/cpp_src/core/keyvalue/uuid.h @@ -3,9 +3,9 @@ #include #include #include +#include "estl/comparation_result.h" #include "tools/assertrx.h" #include "variant.h" -#include "estl/comparation_result.h" namespace reindexer { class Uuid; @@ -62,7 +62,8 @@ class Uuid { [[nodiscard]] explicit operator std::string() const; [[nodiscard]] ComparationResult Compare(const Uuid& other) const noexcept { if (data_[0] == other.data_[0]) { - return data_[1] == other.data_[1] ? ComparationResult::Eq : (data_[1] < other.data_[1] ? ComparationResult::Lt : ComparationResult::Gt); + return data_[1] == other.data_[1] ? ComparationResult::Eq + : (data_[1] < other.data_[1] ? ComparationResult::Lt : ComparationResult::Gt); } else { return data_[0] < other.data_[0] ? ComparationResult::Lt : ComparationResult::Gt; } diff --git a/cpp_src/core/keyvalue/variant.cc b/cpp_src/core/keyvalue/variant.cc index 81616bbc0..5005ef5dd 100644 --- a/cpp_src/core/keyvalue/variant.cc +++ b/cpp_src/core/keyvalue/variant.cc @@ -21,10 +21,10 @@ Variant::Variant(p_string v, hold_t) : variant_{0, 0, KeyValueType::String{}} { *cast() = v; } } -Variant::Variant(const VariantArray &values) : variant_{0, 1, KeyValueType::Tuple{}} { +Variant::Variant(const VariantArray& values) : variant_{0, 1, KeyValueType::Tuple{}} { WrSerializer ser; ser.PutVarUint(values.size()); - for (const Variant &kv : values) { + for (const Variant& kv : values) { ser.PutVariant(kv); } new (cast()) key_string(make_key_string(ser.Slice())); @@ -44,23 +44,23 @@ Variant::Variant(Uuid uuid) noexcept : uuid_() { } } -static void serialize(WrSerializer &, const std::tuple<> &) noexcept {} +static void serialize(WrSerializer&, const std::tuple<>&) noexcept {} template -void serialize(WrSerializer &ser, const std::tuple &v) { +void serialize(WrSerializer& ser, const std::tuple& v) { ser.PutVariant(Variant{std::get<0>(v)}); serialize(ser, tail(v)); } template -Variant::Variant(const std::tuple &values) : variant_{0, 1, KeyValueType::Tuple{}} { +Variant::Variant(const std::tuple& values) : variant_{0, 1, KeyValueType::Tuple{}} { WrSerializer ser; ser.PutVarUint(sizeof...(Ts)); serialize(ser, values); new (cast()) key_string(make_key_string(ser.Slice())); } -template Variant::Variant(const std::tuple &); -template Variant::Variant(const std::tuple &); +template Variant::Variant(const std::tuple&); +template Variant::Variant(const std::tuple&); template inline static void assertKeyType([[maybe_unused]] KeyValueType got) noexcept { @@ -95,12 +95,16 @@ Variant::operator Point() const { return static_cast(getCompositeValues() template <> Point Variant::As() const { assertrx(!isUuid()); - if (!variant_.type.Is()) throw Error(errParams, "Can't convert %s to Point", variant_.type.Name()); + if (!variant_.type.Is()) { + throw Error(errParams, "Can't convert %s to Point", variant_.type.Name()); + } return static_cast(getCompositeValues()); } template <> Uuid Variant::As() const { - if (isUuid()) return Uuid{*this}; + if (isUuid()) { + return Uuid{*this}; + } return variant_.type.EvaluateOneOf( [&](KeyValueType::Uuid) { return Uuid{*this}; }, [&](KeyValueType::String) { return Uuid{this->As()}; }, [&](OneOf) noexcept {}); } -Variant &Variant::EnsureHold() & { - if (isUuid() || variant_.hold == 1) return *this; +Variant& Variant::EnsureHold() & { + if (isUuid() || variant_.hold == 1) { + return *this; + } variant_.type.EvaluateOneOf([&](OneOf) { *this = Variant(this->operator key_string()); }, - [&](KeyValueType::Composite) { *this = Variant(this->operator const PayloadValue &()); }, + [&](KeyValueType::Composite) { *this = Variant(this->operator const PayloadValue&()); }, [](OneOf) noexcept {}); return *this; @@ -180,9 +186,9 @@ p_string Variant::As() const { } template <> -std::string Variant::As(const PayloadType &pt, const FieldsSet &fields) const { +std::string Variant::As(const PayloadType& pt, const FieldsSet& fields) const { if (!isUuid() && variant_.type.Is()) { - ConstPayload pl(pt, operator const PayloadValue &()); + ConstPayload pl(pt, operator const PayloadValue&()); VariantArray va; size_t tagsPathIdx = 0; for (auto field : fields) { @@ -226,7 +232,9 @@ std::optional tryParseAs(std::string_view str) noexcept { template <> std::optional tryParseAs(std::string_view str) noexcept { - if (str.empty()) return 0.0; + if (str.empty()) { + return 0.0; + } using namespace double_conversion; static const StringToDoubleConverter converter{StringToDoubleConverter::ALLOW_LEADING_SPACES | StringToDoubleConverter::ALLOW_TRAILING_SPACES | @@ -272,7 +280,7 @@ int Variant::As() const { [&](KeyValueType::Uuid) -> int { throw Error(errParams, "Can't convert '%s' to number", std::string{Uuid{*this}}); }); } -static std::optional tryConvertToBool(const p_string &str) { +static std::optional tryConvertToBool(const p_string& str) { using namespace std::string_view_literals; if (iequals(str, "true"sv)) { return true; @@ -354,7 +362,7 @@ double Variant::As() const { } template -ComparationResult Variant::Compare(const Variant &other, const CollateOpts &collateOpts) const { +ComparationResult Variant::Compare(const Variant& other, const CollateOpts& collateOpts) const { if (isUuid()) { assertrx(other.Type().Is()); return Uuid{*this}.Compare(Uuid{other}); @@ -403,8 +411,8 @@ ComparationResult Variant::Compare(const Variant &other, const CollateOpts &coll [](OneOf) noexcept -> ComparationResult { abort(); }); } } -template ComparationResult Variant::Compare(const Variant &, const CollateOpts &) const; -template ComparationResult Variant::Compare(const Variant &, const CollateOpts &) const; +template ComparationResult Variant::Compare(const Variant&, const CollateOpts&) const; +template ComparationResult Variant::Compare(const Variant&, const CollateOpts&) const; template ComparationResult Variant::relaxCompareWithString(std::string_view str) const noexcept(notComparable == NotComparable::Return) { @@ -507,7 +515,7 @@ ComparationResult Variant::relaxCompareWithString(std::string_view str) const no class Comparator { public: - explicit Comparator(const Variant &v1, const Variant &v2) noexcept : v1_{v1}, v2_{v2} {} + explicit Comparator(const Variant& v1, const Variant& v2) noexcept : v1_{v1}, v2_{v2} {} RX_ALWAYS_INLINE ComparationResult operator()(KeyValueType::Bool, KeyValueType::Bool) const noexcept { return compare(v1_.As(), v2_.As()); } @@ -578,12 +586,12 @@ class Comparator { } private: - const Variant &v1_; - const Variant &v2_; + const Variant& v1_; + const Variant& v2_; }; template -ComparationResult Variant::RelaxCompare(const Variant &other, const CollateOpts &collateOpts) const { +ComparationResult Variant::RelaxCompare(const Variant& other, const CollateOpts& collateOpts) const { thread_local char uuidStrBuf[Uuid::kStrFormLen]; thread_local const std::string_view uuidStrBufView{uuidStrBuf, Uuid::kStrFormLen}; thread_local const p_string uuidStrBufPString{&uuidStrBufView}; @@ -653,10 +661,10 @@ ComparationResult Variant::RelaxCompare(const Variant &other, const CollateOpts } } -template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; +template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; size_t Variant::Hash() const noexcept { if (isUuid()) { @@ -689,7 +697,7 @@ void Variant::EnsureUTF8() const { } } -Variant Variant::convert(KeyValueType type, const PayloadType *payloadType, const FieldsSet *fields) const & { +Variant Variant::convert(KeyValueType type, const PayloadType* payloadType, const FieldsSet* fields) const& { if (Type().IsSame(type)) { return *this; } @@ -698,7 +706,7 @@ Variant Variant::convert(KeyValueType type, const PayloadType *payloadType, cons return dst; } -Variant &Variant::convert(KeyValueType type, const PayloadType *payloadType, const FieldsSet *fields) & { +Variant& Variant::convert(KeyValueType type, const PayloadType* payloadType, const FieldsSet* fields) & { if (isUuid()) { type.EvaluateOneOf([&](KeyValueType::Uuid) noexcept {}, [&](KeyValueType::String) { *this = Variant{std::string{Uuid{*this}}}; }, [&](KeyValueType::Composite) { @@ -714,7 +722,9 @@ Variant &Variant::convert(KeyValueType type, const PayloadType *payloadType, con }); return *this; } - if (type.IsSame(variant_.type) || type.Is() || variant_.type.Is()) return *this; + if (type.IsSame(variant_.type) || type.Is() || variant_.type.Is()) { + return *this; + } type.EvaluateOneOf( [&](KeyValueType::Int) { *this = Variant(As()); }, [&](KeyValueType::Bool) { *this = Variant(As()); }, [&](KeyValueType::Int64) { *this = Variant(As()); }, [&](KeyValueType::Double) { *this = Variant(As()); }, @@ -744,7 +754,7 @@ Variant &Variant::convert(KeyValueType type, const PayloadType *payloadType, con return *this; } -std::optional Variant::tryConvert(KeyValueType type, const PayloadType *payloadType, const FieldsSet *fields) const & { +std::optional Variant::tryConvert(KeyValueType type, const PayloadType* payloadType, const FieldsSet* fields) const& { if (Type().IsSame(type)) { return *this; } else { @@ -757,7 +767,7 @@ std::optional Variant::tryConvert(KeyValueType type, const PayloadType } } -bool Variant::tryConvert(KeyValueType type, const PayloadType *payloadType, const FieldsSet *fields) & { +bool Variant::tryConvert(KeyValueType type, const PayloadType* payloadType, const FieldsSet* fields) & { using namespace std::string_view_literals; if (isUuid()) { return type.EvaluateOneOf([&](KeyValueType::Uuid) noexcept { return true; }, @@ -972,20 +982,22 @@ bool Variant::tryConvert(KeyValueType type, const PayloadType *payloadType, cons return res; } -void Variant::convertToComposite(const PayloadType &payloadType, const FieldsSet &fields) { +void Variant::convertToComposite(const PayloadType& payloadType, const FieldsSet& fields) { assertrx(!isUuid()); assertrx(variant_.type.Is() && variant_.hold == 1); key_string val = *cast(); - if (variant_.hold == 1) free(); + if (variant_.hold == 1) { + free(); + } // Alloc usual payloadvalue + extra memory for hold string - auto &pv = *new (cast()) PayloadValue(payloadType.TotalSize() + val->size()); + auto& pv = *new (cast()) PayloadValue(payloadType.TotalSize() + val->size()); variant_.hold = 1; variant_.type = KeyValueType::Composite{}; // Copy serializer buffer with strings to extra payloadvalue memory - char *data = reinterpret_cast(pv.Ptr() + payloadType.TotalSize()); + char* data = reinterpret_cast(pv.Ptr() + payloadType.TotalSize()); memcpy(data, val->data(), val->size()); Serializer ser(std::string_view(data, val->size())); @@ -1046,7 +1058,7 @@ Variant::operator std::string_view() const noexcept { assertKeyType(variant_.type); return (variant_.hold == 1) ? std::string_view(**cast()) : *cast(); } -Variant::operator const PayloadValue &() const noexcept { +Variant::operator const PayloadValue&() const noexcept { assertrx(!isUuid()); assertKeyType(variant_.type); assertrx(variant_.hold == 1); @@ -1054,7 +1066,7 @@ Variant::operator const PayloadValue &() const noexcept { } template -void Variant::Dump(T &os, CheckIsStringPrintable checkPrintableString) const { +void Variant::Dump(T& os, CheckIsStringPrintable checkPrintableString) const { if (isUuid()) { os << Uuid{*this}; } else { @@ -1074,23 +1086,25 @@ void Variant::Dump(T &os, CheckIsStringPrintable checkPrintableString) const { } } -template void Variant::Dump(WrSerializer &, CheckIsStringPrintable) const; -template void Variant::Dump(std::ostream &, CheckIsStringPrintable) const; -template void Variant::Dump(std::stringstream &, CheckIsStringPrintable) const; +template void Variant::Dump(WrSerializer&, CheckIsStringPrintable) const; +template void Variant::Dump(std::ostream&, CheckIsStringPrintable) const; +template void Variant::Dump(std::stringstream&, CheckIsStringPrintable) const; template -void VariantArray::Dump(T &os, CheckIsStringPrintable checkPrintableString) const { +void VariantArray::Dump(T& os, CheckIsStringPrintable checkPrintableString) const { os << '{'; - for (auto &arg : *this) { - if (&arg != &at(0)) os << ", "; + for (auto& arg : *this) { + if (&arg != &at(0)) { + os << ", "; + } arg.Dump(os, checkPrintableString); } os << '}'; } -template void VariantArray::Dump(WrSerializer &, CheckIsStringPrintable) const; -template void VariantArray::Dump(std::ostream &, CheckIsStringPrintable) const; -template void VariantArray::Dump(std::stringstream &, CheckIsStringPrintable) const; +template void VariantArray::Dump(WrSerializer&, CheckIsStringPrintable) const; +template void VariantArray::Dump(std::ostream&, CheckIsStringPrintable) const; +template void VariantArray::Dump(std::stringstream&, CheckIsStringPrintable) const; VariantArray::operator Point() const { if (size() != 2) { @@ -1100,28 +1114,29 @@ VariantArray::operator Point() const { } template -ComparationResult VariantArray::RelaxCompare(const VariantArray &other, const CollateOpts &collateOpts) const { +ComparationResult VariantArray::RelaxCompare(const VariantArray& other, const CollateOpts& collateOpts) const { auto lhsIt{cbegin()}, rhsIt{other.cbegin()}; - auto const lhsEnd{cend()}, rhsEnd{other.cend()}; + const auto lhsEnd{cend()}, rhsEnd{other.cend()}; for (; lhsIt != lhsEnd && rhsIt != rhsEnd; ++lhsIt, ++rhsIt) { const auto res = lhsIt->RelaxCompare(*rhsIt, collateOpts); - if (res != ComparationResult::Eq) return res; + if (res != ComparationResult::Eq) { + return res; + } } if (lhsIt == lhsEnd) { - if (rhsIt == rhsEnd) return ComparationResult::Eq; + if (rhsIt == rhsEnd) { + return ComparationResult::Eq; + } return ComparationResult::Lt; } else { return ComparationResult::Gt; } } -template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; -template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; -template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; -template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; +template ComparationResult VariantArray::RelaxCompare(const VariantArray&, + const CollateOpts&) const; +template ComparationResult VariantArray::RelaxCompare(const VariantArray&, const CollateOpts&) const; +template ComparationResult VariantArray::RelaxCompare(const VariantArray&, const CollateOpts&) const; +template ComparationResult VariantArray::RelaxCompare(const VariantArray&, const CollateOpts&) const; } // namespace reindexer diff --git a/cpp_src/core/keyvalue/variant.h b/cpp_src/core/keyvalue/variant.h index 5c29f3e06..22c586620 100644 --- a/cpp_src/core/keyvalue/variant.h +++ b/cpp_src/core/keyvalue/variant.h @@ -35,24 +35,24 @@ class Variant { explicit Variant(bool v) noexcept : variant_{0, 0, KeyValueType::Bool{}, v} {} explicit Variant(int64_t v) noexcept : variant_{0, 0, KeyValueType::Int64{}, v} {} explicit Variant(double v) noexcept : variant_{0, 0, KeyValueType::Double{}, v} {} - explicit Variant(const char *v) noexcept : Variant(p_string(v), Variant::no_hold_t{}) {} + explicit Variant(const char* v) noexcept : Variant(p_string(v), Variant::no_hold_t{}) {} Variant(p_string v, no_hold_t) noexcept : variant_{0, 0, KeyValueType::String{}} { *cast() = v; } Variant(p_string v, hold_t); explicit Variant(p_string v) noexcept : Variant(v, no_hold_t{}) {} - explicit Variant(const std::string &v) : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(make_key_string(v)); } - explicit Variant(std::string &&v) : variant_{0, 1, KeyValueType::String{}} { + explicit Variant(const std::string& v) : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(make_key_string(v)); } + explicit Variant(std::string&& v) : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(make_key_string(std::move(v))); } - explicit Variant(const key_string &v) noexcept : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(v); } - explicit Variant(key_string &&v) noexcept : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(std::move(v)); } - explicit Variant(const PayloadValue &v) noexcept : variant_{0, 1, KeyValueType::Composite{}} { new (cast()) PayloadValue(v); } - explicit Variant(PayloadValue &&v) noexcept : variant_{0, 1, KeyValueType::Composite{}} { + explicit Variant(const key_string& v) noexcept : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(v); } + explicit Variant(key_string&& v) noexcept : variant_{0, 1, KeyValueType::String{}} { new (cast()) key_string(std::move(v)); } + explicit Variant(const PayloadValue& v) noexcept : variant_{0, 1, KeyValueType::Composite{}} { new (cast()) PayloadValue(v); } + explicit Variant(PayloadValue&& v) noexcept : variant_{0, 1, KeyValueType::Composite{}} { new (cast()) PayloadValue(std::move(v)); } - explicit Variant(const VariantArray &values); + explicit Variant(const VariantArray& values); explicit Variant(Point); explicit Variant(Uuid) noexcept; - Variant(const Variant &other) : uuid_{other.uuid_} { + Variant(const Variant& other) : uuid_{other.uuid_} { if (!isUuid()) { uuid_.~UUID(); new (&variant_) Var{other.variant_}; @@ -61,7 +61,7 @@ class Variant { } } } - Variant(Variant &&other) noexcept : uuid_{other.uuid_} { + Variant(Variant&& other) noexcept : uuid_{other.uuid_} { if (!isUuid()) { uuid_.~UUID(); new (&variant_) Var{other.variant_}; @@ -69,13 +69,17 @@ class Variant { } } ~Variant() { - if (!isUuid() && variant_.hold != 0) free(); + if (!isUuid() && variant_.hold != 0) { + free(); + } } template - Variant(const std::tuple &); + Variant(const std::tuple&); - Variant &operator=(Variant &&other) & noexcept { - if (this == &other) return *this; + Variant& operator=(Variant&& other) & noexcept { + if (this == &other) { + return *this; + } if (isUuid()) { if (other.isUuid()) { uuid_ = other.uuid_; @@ -85,7 +89,9 @@ class Variant { other.variant_.hold = 0; } } else { - if (variant_.hold != 0) free(); + if (variant_.hold != 0) { + free(); + } if (other.isUuid()) { variant_.~Var(); new (&uuid_) UUID{other.uuid_}; @@ -96,7 +102,7 @@ class Variant { } return *this; } - Variant &operator=(const Variant &other) & { + Variant& operator=(const Variant& other) & { if (this != &other) { Variant tmp{other}; operator=(std::move(tmp)); @@ -111,7 +117,7 @@ class Variant { explicit operator p_string() const noexcept; explicit operator std::string_view() const noexcept; - explicit operator const PayloadValue &() const noexcept; + explicit operator const PayloadValue&() const noexcept; explicit operator key_string() const; explicit operator Point() const; @@ -119,24 +125,24 @@ class Variant { [[nodiscard]] T As() const; template - [[nodiscard]] T As(const PayloadType &, const FieldsSet &) const; + [[nodiscard]] T As(const PayloadType&, const FieldsSet&) const; - bool operator==(const Variant &other) const { + bool operator==(const Variant& other) const { return Type().IsSame(other.Type()) && Compare(other) == ComparationResult::Eq; } - bool operator!=(const Variant &other) const { return !operator==(other); } - bool operator<(const Variant &other) const { return Compare(other) == ComparationResult::Lt; } - bool operator>(const Variant &other) const { return Compare(other) == ComparationResult::Gt; } - bool operator>=(const Variant &other) const { return Compare(other) & ComparationResult::Ge; } - bool operator<=(const Variant &other) const { return Compare(other) & ComparationResult::Le; } + bool operator!=(const Variant& other) const { return !operator==(other); } + bool operator<(const Variant& other) const { return Compare(other) == ComparationResult::Lt; } + bool operator>(const Variant& other) const { return Compare(other) == ComparationResult::Gt; } + bool operator>=(const Variant& other) const { return Compare(other) & ComparationResult::Ge; } + bool operator<=(const Variant& other) const { return Compare(other) & ComparationResult::Le; } template - ComparationResult Compare(const Variant &other, const CollateOpts &collateOpts = CollateOpts()) const; + ComparationResult Compare(const Variant& other, const CollateOpts& collateOpts = CollateOpts()) const; template - ComparationResult RelaxCompare(const Variant &other, const CollateOpts &collateOpts = CollateOpts()) const; + ComparationResult RelaxCompare(const Variant& other, const CollateOpts& collateOpts = CollateOpts()) const; size_t Hash() const noexcept; void EnsureUTF8() const; - Variant &EnsureHold() &; + Variant& EnsureHold() &; Variant EnsureHold() && { return std::move(EnsureHold()); } KeyValueType Type() const noexcept { @@ -146,57 +152,57 @@ class Variant { return variant_.type; } - Variant &convert(KeyValueType type, const PayloadType * = nullptr, const FieldsSet * = nullptr) &; - [[nodiscard]] Variant convert(KeyValueType type, const PayloadType *pt = nullptr, const FieldsSet *fs = nullptr) && { + Variant& convert(KeyValueType type, const PayloadType* = nullptr, const FieldsSet* = nullptr) &; + [[nodiscard]] Variant convert(KeyValueType type, const PayloadType* pt = nullptr, const FieldsSet* fs = nullptr) && { return std::move(convert(type, pt, fs)); } - [[nodiscard]] Variant convert(KeyValueType type, const PayloadType * = nullptr, const FieldsSet * = nullptr) const &; - [[nodiscard]] std::optional tryConvert(KeyValueType type, const PayloadType * = nullptr, const FieldsSet * = nullptr) const &; - [[nodiscard]] bool tryConvert(KeyValueType type, const PayloadType * = nullptr, const FieldsSet * = nullptr) &; - auto tryConvert(KeyValueType type, const PayloadType * = nullptr, const FieldsSet * = nullptr) const && = delete; + [[nodiscard]] Variant convert(KeyValueType type, const PayloadType* = nullptr, const FieldsSet* = nullptr) const&; + [[nodiscard]] std::optional tryConvert(KeyValueType type, const PayloadType* = nullptr, const FieldsSet* = nullptr) const&; + [[nodiscard]] bool tryConvert(KeyValueType type, const PayloadType* = nullptr, const FieldsSet* = nullptr) &; + auto tryConvert(KeyValueType type, const PayloadType* = nullptr, const FieldsSet* = nullptr) const&& = delete; VariantArray getCompositeValues() const; bool IsNullValue() const noexcept { return Type().Is(); } template - void Dump(T &os, CheckIsStringPrintable checkPrintableString = CheckIsStringPrintable::Yes) const; + void Dump(T& os, CheckIsStringPrintable checkPrintableString = CheckIsStringPrintable::Yes) const; class Less { public: - Less(const CollateOpts &collate) noexcept : collate_{&collate} {} - [[nodiscard]] bool operator()(const Variant &lhs, const Variant &rhs) const { + Less(const CollateOpts& collate) noexcept : collate_{&collate} {} + [[nodiscard]] bool operator()(const Variant& lhs, const Variant& rhs) const { return lhs.Compare(rhs, *collate_) == ComparationResult::Lt; } private: - const CollateOpts *collate_; + const CollateOpts* collate_; }; class EqualTo { public: - EqualTo(const CollateOpts &collate) noexcept : collate_{&collate} {} - [[nodiscard]] bool operator()(const Variant &lhs, const Variant &rhs) const { + EqualTo(const CollateOpts& collate) noexcept : collate_{&collate} {} + [[nodiscard]] bool operator()(const Variant& lhs, const Variant& rhs) const { return lhs.Compare(rhs, *collate_) == ComparationResult::Eq; } private: - const CollateOpts *collate_; + const CollateOpts* collate_; }; private: bool isUuid() const noexcept { return uuid_.isUuid != 0; } - void convertToComposite(const PayloadType &, const FieldsSet &); + void convertToComposite(const PayloadType&, const FieldsSet&); void free() noexcept; - void copy(const Variant &other); + void copy(const Variant& other); template - const T *cast() const noexcept { + const T* cast() const noexcept { assertrx(!isUuid()); - return reinterpret_cast(&variant_.value_uint64); + return reinterpret_cast(&variant_.value_uint64); } template - T *cast() noexcept { + T* cast() noexcept { assertrx(!isUuid()); - return reinterpret_cast(&variant_.value_uint64); + return reinterpret_cast(&variant_.value_uint64); } template ComparationResult relaxCompareWithString(std::string_view) const noexcept(notComparable == NotComparable::Return); @@ -240,12 +246,12 @@ class Variant { static_assert(sizeof(Variant) == 16); -extern template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -extern template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -extern template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -extern template ComparationResult Variant::RelaxCompare(const Variant &, const CollateOpts &) const; -extern template ComparationResult Variant::Compare(const Variant &, const CollateOpts &) const; -extern template ComparationResult Variant::Compare(const Variant &, const CollateOpts &) const; +extern template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +extern template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +extern template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +extern template ComparationResult Variant::RelaxCompare(const Variant&, const CollateOpts&) const; +extern template ComparationResult Variant::Compare(const Variant&, const CollateOpts&) const; +extern template ComparationResult Variant::Compare(const Variant&, const CollateOpts&) const; template <> int Variant::As() const; @@ -268,11 +274,11 @@ class VariantArray : public h_vector { emplace_back(p.Y()); } explicit operator Point() const; - VariantArray &MarkArray(bool v = true) & noexcept { + VariantArray& MarkArray(bool v = true) & noexcept { isArrayValue = v; return *this; } - VariantArray &&MarkArray(bool v = true) && noexcept { + VariantArray&& MarkArray(bool v = true) && noexcept { isArrayValue = v; return std::move(*this); } @@ -287,7 +293,9 @@ class VariantArray : public h_vector { } size_t Hash() const noexcept { size_t ret = this->size(); - for (size_t i = 0; i < this->size(); ++i) ret = (ret * 127) ^ this->at(i).Hash(); + for (size_t i = 0; i < this->size(); ++i) { + ret = (ret * 127) ^ this->at(i).Hash(); + } return ret; } bool IsArrayValue() const noexcept { return isArrayValue || (!isObjectValue && size() > 1); } @@ -295,21 +303,23 @@ class VariantArray : public h_vector { bool IsNullValue() const noexcept { return size() == 1 && front().IsNullValue(); } KeyValueType ArrayType() const noexcept { return empty() ? KeyValueType::Null{} : front().Type(); } template - void Dump(T &os, CheckIsStringPrintable checkPrintableString = CheckIsStringPrintable::Yes) const; + void Dump(T& os, CheckIsStringPrintable checkPrintableString = CheckIsStringPrintable::Yes) const; template - ComparationResult RelaxCompare(const VariantArray &other, const CollateOpts & = CollateOpts{}) const; + ComparationResult RelaxCompare(const VariantArray& other, const CollateOpts& = CollateOpts{}) const; void EnsureHold() { - for (Variant &v : *this) v.EnsureHold(); + for (Variant& v : *this) { + v.EnsureHold(); + } } template - static VariantArray Create(Ts &&...vs) { + static VariantArray Create(Ts&&... vs) { return VariantArray{Variant{std::forward(vs)}...}; } template static VariantArray Create(std::initializer_list vs) { VariantArray res; res.reserve(vs.size()); - for (auto &v : vs) { + for (auto& v : vs) { res.emplace_back(std::move(v)); } return res; @@ -329,19 +339,19 @@ class VariantArray : public h_vector { // Current implementation requires 3 allocations for each point inline Variant::Variant(Point p) : Variant{VariantArray{p}} {} -extern template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; -extern template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; -extern template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; -extern template ComparationResult VariantArray::RelaxCompare(const VariantArray &, - const CollateOpts &) const; +extern template ComparationResult VariantArray::RelaxCompare(const VariantArray&, + const CollateOpts&) const; +extern template ComparationResult VariantArray::RelaxCompare(const VariantArray&, + const CollateOpts&) const; +extern template ComparationResult VariantArray::RelaxCompare(const VariantArray&, + const CollateOpts&) const; +extern template ComparationResult VariantArray::RelaxCompare(const VariantArray&, + const CollateOpts&) const; } // namespace reindexer namespace std { template <> struct hash { - size_t operator()(const reindexer::Variant &kv) const { return kv.Hash(); } + size_t operator()(const reindexer::Variant& kv) const { return kv.Hash(); } }; } // namespace std diff --git a/cpp_src/core/lrucache.cc b/cpp_src/core/lrucache.cc index 5091c07e6..93210539e 100644 --- a/cpp_src/core/lrucache.cc +++ b/cpp_src/core/lrucache.cc @@ -10,8 +10,10 @@ namespace reindexer { constexpr uint32_t kMaxHitCountToCache = 1024; template -typename LRUCache::Iterator LRUCache::Get(const K &key) { - if rx_unlikely (cacheSizeLimit_ == 0) return Iterator(); +typename LRUCache::Iterator LRUCache::Get(const K& key) { + if rx_unlikely (cacheSizeLimit_ == 0) { + return Iterator(); + } std::lock_guard lk(lock_); @@ -38,12 +40,16 @@ typename LRUCache::Iterator LRUCache::Get( } template -void LRUCache::Put(const K &key, V &&v) { - if rx_unlikely (cacheSizeLimit_ == 0) return; +void LRUCache::Put(const K& key, V&& v) { + if rx_unlikely (cacheSizeLimit_ == 0) { + return; + } std::lock_guard lk(lock_); auto it = items_.find(key); - if (it == items_.end()) return; + if (it == items_.end()) { + return; + } totalCacheSize_ += v.Size() - it->second.val.Size(); it->second.val = std::move(v); diff --git a/cpp_src/core/lrucache.h b/cpp_src/core/lrucache.h index 4cac5b979..3acfb7dca 100644 --- a/cpp_src/core/lrucache.h +++ b/cpp_src/core/lrucache.h @@ -17,11 +17,11 @@ class LRUCache { using Key = K; LRUCache(size_t sizeLimit, uint32_t hitCount) noexcept : totalCacheSize_(0), cacheSizeLimit_(sizeLimit), hitCountToCache_(hitCount) {} struct Iterator { - Iterator(bool k = false, const V &v = V()) : valid(k), val(v) {} - Iterator(const Iterator &other) = delete; - Iterator &operator=(const Iterator &other) = delete; - Iterator(Iterator &&other) noexcept : valid(other.valid), val(std::move(other.val)) { other.valid = false; } - Iterator &operator=(Iterator &&other) noexcept { + Iterator(bool k = false, const V& v = V()) : valid(k), val(v) {} + Iterator(const Iterator& other) = delete; + Iterator& operator=(const Iterator& other) = delete; + Iterator(Iterator&& other) noexcept : valid(other.valid), val(std::move(other.val)) { other.valid = false; } + Iterator& operator=(Iterator&& other) noexcept { if (this != &other) { valid = other.valid; val = std::move(other.val); @@ -33,9 +33,9 @@ class LRUCache { V val; }; // Get cached val. Create new entry in cache if unexists - Iterator Get(const K &k); + Iterator Get(const K& k); // Put cached val - void Put(const K &k, V &&v); + void Put(const K& k, V&& v); LRUCacheMemStat GetMemStat(); @@ -45,7 +45,7 @@ class LRUCache { } template - void Dump(T &os, std::string_view step, std::string_view offset) const { + void Dump(T& os, std::string_view step, std::string_view offset) const { std::string newOffset{offset}; newOffset += step; os << "{\n" << newOffset << "totalCacheSize: "; @@ -59,7 +59,9 @@ class LRUCache { << newOffset << "items: ["; if (!items_.empty()) { for (auto b = items_.begin(), it = b, e = items_.end(); it != e; ++it) { - if (it != b) os << ','; + if (it != b) { + os << ','; + } os << '\n' << newOffset << '{' << it->first << ": "; it->second.Dump(os); os << '}'; @@ -68,14 +70,16 @@ class LRUCache { } os << "],\n" << newOffset << "lruList: ["; for (auto b = lru_.begin(), it = b, e = lru_.end(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << **it; } os << "]\n" << offset << '}'; } template - void Clear(const F &cond) { + void Clear(const F& cond) { std::lock_guard lock(lock_); for (auto it = lru_.begin(); it != lru_.end();) { if (!cond(**it)) { @@ -97,13 +101,13 @@ class LRUCache { } protected: - typedef std::list LRUList; + typedef std::list LRUList; struct Entry { V val; typename LRUList::iterator lruPos; int hitCount = 0; template - void Dump(T &os) const { + void Dump(T& os) const { os << "{val: " << val << ", hitCount: " << hitCount << '}'; } }; diff --git a/cpp_src/core/lsn.h b/cpp_src/core/lsn.h index 174540da4..69dd25ed5 100644 --- a/cpp_src/core/lsn.h +++ b/cpp_src/core/lsn.h @@ -21,19 +21,19 @@ struct lsn_t { static constexpr int64_t kDefaultCounter = kMaxCounter - 1; public: - void GetJSON(JsonBuilder &builder) const; + void GetJSON(JsonBuilder& builder) const; - void FromJSON(const gason::JsonNode &root) { + void FromJSON(const gason::JsonNode& root) { const int server = root["server_id"].As(0); const int64_t counter = root["counter"].As(kDefaultCounter); payload_ = int64_t(lsn_t(counter, server)); } lsn_t() noexcept = default; - lsn_t(const lsn_t &) noexcept = default; - lsn_t(lsn_t &&) noexcept = default; - lsn_t &operator=(const lsn_t &) noexcept = default; - lsn_t &operator=(lsn_t &&) noexcept = default; + lsn_t(const lsn_t&) noexcept = default; + lsn_t(lsn_t&&) noexcept = default; + lsn_t& operator=(const lsn_t&) noexcept = default; + lsn_t& operator=(lsn_t&&) noexcept = default; explicit lsn_t(int64_t v) : lsn_t(v % kMaxCounter, v / kMaxCounter) {} lsn_t(int64_t counter, int16_t server) { validateCounter(counter); @@ -60,11 +60,14 @@ struct lsn_t { bool isEmpty() const noexcept { return Counter() == kDefaultCounter; } int compare(lsn_t o) const { - if (Server() != o.Server()) throw Error(errLogic, "Compare lsn from different server"); - if (Counter() < o.Counter()) + if (Server() != o.Server()) { + throw Error(errLogic, "Compare lsn from different server"); + } + if (Counter() < o.Counter()) { return -1; - else if (Counter() > o.Counter()) + } else if (Counter() > o.Counter()) { return 1; + } return 0; } @@ -89,7 +92,7 @@ struct lsn_t { } } - [[noreturn]] static void throwValidation(ErrorCode, const char *, int64_t); + [[noreturn]] static void throwValidation(ErrorCode, const char*, int64_t); }; struct LSNPair { @@ -99,7 +102,7 @@ struct LSNPair { lsn_t originLSN_; }; -inline static std::ostream &operator<<(std::ostream &o, const reindexer::lsn_t &sv) { +inline static std::ostream& operator<<(std::ostream& o, const reindexer::lsn_t& sv) { o << sv.Server() << ":" << sv.Counter(); return o; } diff --git a/cpp_src/core/namespace/asyncstorage.cc b/cpp_src/core/namespace/asyncstorage.cc index 73e3d4759..14058e446 100644 --- a/cpp_src/core/namespace/asyncstorage.cc +++ b/cpp_src/core/namespace/asyncstorage.cc @@ -125,7 +125,9 @@ void AsyncStorage::InheritUpdatesFrom(AsyncStorage& src, AsyncStorage::FullLockT totalUpdatesCount_.fetch_add(src.curUpdatesChunck_.updatesCount, std::memory_order_release); src.totalUpdatesCount_.fetch_sub(src.curUpdatesChunck_.updatesCount, std::memory_order_release); finishedUpdateChuncks_.push_front(std::move(src.curUpdatesChunck_)); - if (lastBatchWithSyncUpdates_ >= 0) ++lastBatchWithSyncUpdates_; + if (lastBatchWithSyncUpdates_ >= 0) { + ++lastBatchWithSyncUpdates_; + } } while (src.finishedUpdateChuncks_.size()) { auto& upd = src.finishedUpdateChuncks_.back(); @@ -133,7 +135,9 @@ void AsyncStorage::InheritUpdatesFrom(AsyncStorage& src, AsyncStorage::FullLockT src.totalUpdatesCount_.fetch_sub(upd.updatesCount, std::memory_order_release); finishedUpdateChuncks_.push_front(std::move(upd)); src.finishedUpdateChuncks_.pop_back(); - if (lastBatchWithSyncUpdates_ >= 0) ++lastBatchWithSyncUpdates_; + if (lastBatchWithSyncUpdates_ >= 0) { + ++lastBatchWithSyncUpdates_; + } } src.storage_.reset(); // Do not update lockfree status here to avoid status flickering on ns copying diff --git a/cpp_src/core/namespace/asyncstorage.h b/cpp_src/core/namespace/asyncstorage.h index 541e969f3..a9a08e36a 100644 --- a/cpp_src/core/namespace/asyncstorage.h +++ b/cpp_src/core/namespace/asyncstorage.h @@ -164,12 +164,10 @@ class AsyncStorage { void flush(const StorageFlushOpts& opts); void beginNewUpdatesChunk(); void write(bool fromSyncCall, std::string_view key, std::string_view value) { - asyncOp( - fromSyncCall, [this](std::string_view k, std::string_view v) { curUpdatesChunck_->Put(k, v); }, key, value); + asyncOp(fromSyncCall, [this](std::string_view k, std::string_view v) { curUpdatesChunck_->Put(k, v); }, key, value); } void remove(bool fromSyncCall, std::string_view key) { - asyncOp( - fromSyncCall, [this](std::string_view k) { curUpdatesChunck_->Remove(k); }, key); + asyncOp(fromSyncCall, [this](std::string_view k) { curUpdatesChunck_->Remove(k); }, key); } template void asyncOp(bool fromSyncCall, StorageCall&& call, const Args&... args) { diff --git a/cpp_src/core/namespace/bgnamespacedeleter.h b/cpp_src/core/namespace/bgnamespacedeleter.h index 77e0cfd3b..8f1ab5bcd 100644 --- a/cpp_src/core/namespace/bgnamespacedeleter.h +++ b/cpp_src/core/namespace/bgnamespacedeleter.h @@ -29,4 +29,4 @@ class BackgroundNamespaceDeleter { std::list namespaces_; }; -} +} // namespace reindexer diff --git a/cpp_src/core/namespace/itemsloader.cc b/cpp_src/core/namespace/itemsloader.cc index ec97275b9..1a7f59b44 100644 --- a/cpp_src/core/namespace/itemsloader.cc +++ b/cpp_src/core/namespace/itemsloader.cc @@ -64,7 +64,7 @@ void ItemsLoader::reading() { } // Read LSN - int64_t lsn = *reinterpret_cast(dataSlice.data()); + int64_t lsn = *reinterpret_cast(dataSlice.data()); if (lsn < 0) { lastErr = Error(errParseBin, "Ivalid LSN value: %d", lsn); logPrintf(LogTrace, "Error load item to '%s' from storage: '%s'", ns_.name_, lastErr.what()); @@ -89,10 +89,10 @@ void ItemsLoader::reading() { if (terminated_) { return; } - auto &item = items_.PlaceItem(); + auto& item = items_.PlaceItem(); lck.unlock(); - auto &sliceStorageP = slices_[sliceId]; + auto& sliceStorageP = slices_[sliceId]; if (sliceStorageP.len < dataSlice.size()) { sliceStorageP.len = dataSlice.size() * 1.1; sliceStorageP.data.reset(new char[sliceStorageP.len]); @@ -103,7 +103,7 @@ void ItemsLoader::reading() { item.impl.Unsafe(true); try { item.impl.FromCJSON(dataSlice); - } catch (const Error &err) { + } catch (const Error& err) { logPrintf(LogTrace, "Error load item to '%s' from storage: '%s'", ns_.name_, err.what()); ++errCount; lastErr = err; @@ -179,7 +179,7 @@ void ItemsLoader::insertion() { for (unsigned i = 0; i < items.size(); ++i) { const auto id = i + startId; - auto &plData = ns_.items_[id]; + auto& plData = ns_.items_[id]; Payload pl(ns_.payloadType_, plData); Payload plNew(items[i].impl.GetPayload()); // Index [0] must be inserted after all other simple indexes @@ -190,7 +190,7 @@ void ItemsLoader::insertion() { indexInserters.BuildCompositeIndexesAsync(); } for (unsigned i = 0; i < items.size(); ++i) { - auto &plData = ns_.items_[i + startId]; + auto& plData = ns_.items_[i + startId]; Payload pl(ns_.payloadType_, plData); plData.SetLSN(items[i].impl.Value().GetLSN()); ns_.repl_.dataHash ^= pl.GetHash(); @@ -206,30 +206,33 @@ void ItemsLoader::insertion() { } void ItemsLoader::clearIndexCache() { - for (auto &idx : ns_.indexes_) { + for (auto& idx : ns_.indexes_) { idx->DestroyCache(); idx->Commit(); } } template -void ItemsLoader::doInsertField(NamespaceImpl::IndexesStorage &indexes, unsigned field, IdType id, Payload &pl, Payload &plNew, - VariantArray &krefs, VariantArray &skrefs, MutexT &mtx) { - Index &index = *indexes[field]; +void ItemsLoader::doInsertField(NamespaceImpl::IndexesStorage& indexes, unsigned field, IdType id, Payload& pl, Payload& plNew, + VariantArray& krefs, VariantArray& skrefs, MutexT& mtx) { + Index& index = *indexes[field]; const bool isIndexSparse = index.Opts().IsSparse(); if (isIndexSparse) { assertrx(index.Fields().getTagsPathsLength() > 0); try { plNew.GetByJsonPath(index.Fields().getTagsPath(0), skrefs, index.KeyType()); - } catch (const Error &) { + } catch (const Error&) { skrefs.resize(0); } } else { plNew.Get(field, skrefs); } - if (index.Opts().GetCollateMode() == CollateUTF8) - for (auto &key : skrefs) key.EnsureUTF8(); + if (index.Opts().GetCollateMode() == CollateUTF8) { + for (auto& key : skrefs) { + key.EnsureUTF8(); + } + } // Put value to index krefs.resize(0); @@ -252,7 +255,7 @@ void ItemsLoader::doInsertField(NamespaceImpl::IndexesStorage &indexes, unsigned } } -IndexInserters::IndexInserters(NamespaceImpl::IndexesStorage &indexes, PayloadType pt) : indexes_(indexes), pt_(std::move(pt)) { +IndexInserters::IndexInserters(NamespaceImpl::IndexesStorage& indexes, PayloadType pt) : indexes_(indexes), pt_(std::move(pt)) { for (int i = 1; i < indexes_.firstCompositePos(); ++i) { if (indexes_[i]->Opts().IsArray()) { hasArrayIndexes_ = true; @@ -277,7 +280,7 @@ void IndexInserters::Stop() { shared_.terminate = true; cvReady_.notify_all(); } - for (auto &th : threads_) { + for (auto& th : threads_) { th.join(); } threads_.clear(); @@ -332,7 +335,7 @@ void IndexInserters::insertionLoop(unsigned threadId) noexcept { if (shared_.composite) { for (unsigned i = 0; i < shared_.newItems.size(); ++i) { const auto id = startId + i; - const auto &plData = shared_.nsItems[i]; + const auto& plData = shared_.nsItems[i]; for (unsigned field = firstCompositeIndex + threadId - kTIDOffset; field < totalIndexes; field += threadsCnt) { bool needClearCache{false}; indexes_[field]->Upsert(Variant{plData}, id, needClearCache); @@ -342,8 +345,8 @@ void IndexInserters::insertionLoop(unsigned threadId) noexcept { if (hasArrayIndexes_) { for (unsigned i = 0; i < shared_.newItems.size(); ++i) { const auto id = startId + i; - auto &item = shared_.newItems[i].impl; - auto &plData = shared_.nsItems[i]; + auto& item = shared_.newItems[i].impl; + auto& plData = shared_.nsItems[i]; Payload pl(pt_, plData); Payload plNew = item.GetPayload(); for (unsigned field = threadId - kTIDOffset + 1; field < firstCompositeIndex; field += threadsCnt) { @@ -355,8 +358,8 @@ void IndexInserters::insertionLoop(unsigned threadId) noexcept { dummy_mutex dummyMtx; for (unsigned i = 0; i < shared_.newItems.size(); ++i) { const auto id = startId + i; - auto &item = shared_.newItems[i].impl; - auto &plData = shared_.nsItems[i]; + auto& item = shared_.newItems[i].impl; + auto& plData = shared_.nsItems[i]; Payload pl(pt_, plData); Payload plNew = item.GetPayload(); for (unsigned field = threadId - kTIDOffset + 1; field < firstCompositeIndex; field += threadsCnt) { @@ -366,7 +369,7 @@ void IndexInserters::insertionLoop(unsigned threadId) noexcept { } } onItemsHandled(); - } catch (Error &e) { + } catch (Error& e) { onException(e); } catch (...) { onException(Error(errLogic, "Unknown exception in insertion loop")); diff --git a/cpp_src/core/namespace/namespace.h b/cpp_src/core/namespace/namespace.h index c7e4eeb90..e1b0d4ee1 100644 --- a/cpp_src/core/namespace/namespace.h +++ b/cpp_src/core/namespace/namespace.h @@ -10,7 +10,7 @@ namespace reindexer { class Namespace { template - auto nsFuncWrapper(Args &&...args) const { + auto nsFuncWrapper(Args&&... args) const { while (true) { try { auto ns = atomicLoadMainNs(); @@ -18,7 +18,7 @@ class Namespace { throw Error(errLogic, "Ns is nullptr"); } return (*ns.*fn)(std::forward(args)...); - } catch (const Error &e) { + } catch (const Error& e) { if (e.code() != errNamespaceInvalidated) { throw; } else { @@ -28,18 +28,18 @@ class Namespace { } } - template - void nsFuncWrapper(Item &item, QueryResults &qr, NsContext ctx) const { + template + void nsFuncWrapper(Item& item, QueryResults& qr, NsContext ctx) const { nsFuncWrapper(item, qr, ctx); } - template - void nsFuncWrapper(const Query &query, QueryResults &qr, NsContext ctx) const { + template + void nsFuncWrapper(const Query& query, QueryResults& qr, NsContext ctx) const { nsFuncWrapper(query, qr, ctx); } template - void nsFuncWrapper(T &v, QueryResults &qr, NsContext ctx) const { + void nsFuncWrapper(T& v, QueryResults& qr, NsContext ctx) const { while (true) { NamespaceImpl::Ptr ns; bool added = false; @@ -73,11 +73,13 @@ class Namespace { static_assert(std::is_same_v || std::is_same_v); } return; - } catch (const Error &e) { + } catch (const Error& e) { if (e.code() != errNamespaceInvalidated) { throw; } else { - if (added) qr.RemoveNamespace(ns.get()); + if (added) { + qr.RemoveNamespace(ns.get()); + } std::this_thread::yield(); } } @@ -87,65 +89,65 @@ class Namespace { public: using Ptr = shared_ptr; - Namespace(const std::string &name, UpdatesObservers &observers, BackgroundNamespaceDeleter &bgDeleter) + Namespace(const std::string& name, UpdatesObservers& observers, BackgroundNamespaceDeleter& bgDeleter) : ns_(make_intrusive(name, observers)), bgDeleter_(bgDeleter) {} - void CommitTransaction(Transaction &tx, QueryResults &result, const RdxContext &ctx); - std::string GetName(const RdxContext &ctx) const { return nsFuncWrapper<&NamespaceImpl::GetName>(ctx); } - bool IsSystem(const RdxContext &ctx) const { return nsFuncWrapper<&NamespaceImpl::IsSystem>(ctx); } - bool IsTemporary(const RdxContext &ctx) const { return nsFuncWrapper<&NamespaceImpl::IsTemporary>(ctx); } - void EnableStorage(const std::string &path, StorageOpts opts, StorageType storageType, const RdxContext &ctx) { + void CommitTransaction(Transaction& tx, QueryResults& result, const RdxContext& ctx); + std::string GetName(const RdxContext& ctx) const { return nsFuncWrapper<&NamespaceImpl::GetName>(ctx); } + bool IsSystem(const RdxContext& ctx) const { return nsFuncWrapper<&NamespaceImpl::IsSystem>(ctx); } + bool IsTemporary(const RdxContext& ctx) const { return nsFuncWrapper<&NamespaceImpl::IsTemporary>(ctx); } + void EnableStorage(const std::string& path, StorageOpts opts, StorageType storageType, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::EnableStorage>(path, opts, storageType, ctx); } - void LoadFromStorage(unsigned threadsCount, const RdxContext &ctx) { + void LoadFromStorage(unsigned threadsCount, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::LoadFromStorage>(threadsCount, ctx); } - void DeleteStorage(const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::DeleteStorage>(ctx); } + void DeleteStorage(const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::DeleteStorage>(ctx); } uint32_t GetItemsCount() { return nsFuncWrapper<&NamespaceImpl::GetItemsCount>(); } - void AddIndex(const IndexDef &indexDef, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::AddIndex>(indexDef, ctx); } - void UpdateIndex(const IndexDef &indexDef, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::UpdateIndex>(indexDef, ctx); } - void DropIndex(const IndexDef &indexDef, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::DropIndex>(indexDef, ctx); } - void SetSchema(std::string_view schema, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::SetSchema>(schema, ctx); } - std::string GetSchema(int format, const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::GetSchema>(format, ctx); } - std::shared_ptr GetSchemaPtr(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::GetSchemaPtr>(ctx); } - void Insert(Item &item, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::Insert>(item, ctx); } - void Insert(Item &item, QueryResults &qr, const RdxContext &ctx) { + void AddIndex(const IndexDef& indexDef, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::AddIndex>(indexDef, ctx); } + void UpdateIndex(const IndexDef& indexDef, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::UpdateIndex>(indexDef, ctx); } + void DropIndex(const IndexDef& indexDef, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::DropIndex>(indexDef, ctx); } + void SetSchema(std::string_view schema, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::SetSchema>(schema, ctx); } + std::string GetSchema(int format, const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::GetSchema>(format, ctx); } + std::shared_ptr GetSchemaPtr(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::GetSchemaPtr>(ctx); } + void Insert(Item& item, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Insert>(item, ctx); } + void Insert(Item& item, QueryResults& qr, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::modifyItem, ItemModifyMode::ModeInsert>(item, qr, ctx); } - void Update(Item &item, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::Update>(item, ctx); } - void Update(Item &item, QueryResults &qr, const RdxContext &ctx) { + void Update(Item& item, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Update>(item, ctx); } + void Update(Item& item, QueryResults& qr, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::modifyItem, ItemModifyMode::ModeUpdate>(item, qr, ctx); } - void Update(const Query &query, QueryResults &result, const RdxContext &ctx) { + void Update(const Query& query, QueryResults& result, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::doUpdate, QueryType::QueryUpdate>(query, result, ctx); } - void Upsert(Item &item, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::Upsert>(item, ctx); } - void Upsert(Item &item, QueryResults &qr, const RdxContext &ctx) { + void Upsert(Item& item, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Upsert>(item, ctx); } + void Upsert(Item& item, QueryResults& qr, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::modifyItem, ItemModifyMode::ModeUpsert>(item, qr, ctx); } - void Delete(Item &item, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::Delete>(item, ctx); } - void Delete(Item &item, QueryResults &qr, const RdxContext &ctx) { + void Delete(Item& item, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Delete>(item, ctx); } + void Delete(Item& item, QueryResults& qr, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::modifyItem, ItemModifyMode::ModeDelete>(item, qr, ctx); } - void Delete(const Query &query, QueryResults &result, const RdxContext &ctx) { + void Delete(const Query& query, QueryResults& result, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::doDelete, QueryType::QueryDelete>(query, result, ctx); } - void Truncate(const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::Truncate>(ctx); } + void Truncate(const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Truncate>(ctx); } template - void Select(QueryResults &result, SelectCtxWithJoinPreSelect ¶ms, const RdxContext &ctx) { + void Select(QueryResults& result, SelectCtxWithJoinPreSelect& params, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Select>(result, params, ctx); } - NamespaceDef GetDefinition(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::GetDefinition>(ctx); } - NamespaceMemStat GetMemStat(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::GetMemStat>(ctx); } - NamespacePerfStat GetPerfStat(const RdxContext &ctx); - void ResetPerfStat(const RdxContext &ctx) { + NamespaceDef GetDefinition(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::GetDefinition>(ctx); } + NamespaceMemStat GetMemStat(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::GetMemStat>(ctx); } + NamespacePerfStat GetPerfStat(const RdxContext& ctx); + void ResetPerfStat(const RdxContext& ctx) { txStatsCounter_.Reset(); commitStatsCounter_.Reset(); copyStatsCounter_.Reset(); nsFuncWrapper<&NamespaceImpl::ResetPerfStat>(ctx); } - std::vector EnumMeta(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::EnumMeta>(ctx); } - void BackgroundRoutine(RdxActivityContext *ctx) { + std::vector EnumMeta(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::EnumMeta>(ctx); } + void BackgroundRoutine(RdxActivityContext* ctx) { if (hasCopy_.load(std::memory_order_acquire)) { return; } @@ -157,45 +159,45 @@ class Namespace { } nsFuncWrapper<&NamespaceImpl::StorageFlushingRoutine>(); } - void CloseStorage(const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::CloseStorage>(ctx); } - Transaction NewTransaction(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::NewTransaction>(ctx); } + void CloseStorage(const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::CloseStorage>(ctx); } + Transaction NewTransaction(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::NewTransaction>(ctx); } - Item NewItem(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::NewItem>(ctx); } - void ToPool(ItemImpl *item) { nsFuncWrapper<&NamespaceImpl::ToPool>(item); } - std::string GetMeta(const std::string &key, const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::GetMeta>(key, ctx); } - void PutMeta(const std::string &key, std::string_view data, const RdxContext &ctx) { + Item NewItem(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::NewItem>(ctx); } + void ToPool(ItemImpl* item) { nsFuncWrapper<&NamespaceImpl::ToPool>(item); } + std::string GetMeta(const std::string& key, const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::GetMeta>(key, ctx); } + void PutMeta(const std::string& key, std::string_view data, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::PutMeta>(key, data, ctx); } - void DeleteMeta(const std::string &key, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::DeleteMeta>(key, ctx); } - int64_t GetSerial(const std::string &field) { return nsFuncWrapper<&NamespaceImpl::GetSerial>(field); } + void DeleteMeta(const std::string& key, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::DeleteMeta>(key, ctx); } + int64_t GetSerial(const std::string& field) { return nsFuncWrapper<&NamespaceImpl::GetSerial>(field); } int getIndexByName(std::string_view index) const { return nsFuncWrapper<&NamespaceImpl::getIndexByName>(index); } - bool getIndexByName(std::string_view name, int &index) const { return nsFuncWrapper<&NamespaceImpl::tryGetIndexByName>(name, index); } - void FillResult(QueryResults &result, const IdSet &ids) const { nsFuncWrapper<&NamespaceImpl::FillResult>(result, ids); } + bool getIndexByName(std::string_view name, int& index) const { return nsFuncWrapper<&NamespaceImpl::tryGetIndexByName>(name, index); } + void FillResult(QueryResults& result, const IdSet& ids) const { nsFuncWrapper<&NamespaceImpl::FillResult>(result, ids); } void EnablePerfCounters(bool enable = true) { nsFuncWrapper<&NamespaceImpl::EnablePerfCounters>(enable); } - ReplicationState GetReplState(const RdxContext &ctx) const { return nsFuncWrapper<&NamespaceImpl::GetReplState>(ctx); } - void SetReplLSNs(LSNPair LSNs, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::SetReplLSNs>(LSNs, ctx); } - void SetSlaveReplStatus(ReplicationState::Status status, const Error &error, const RdxContext &ctx) { + ReplicationState GetReplState(const RdxContext& ctx) const { return nsFuncWrapper<&NamespaceImpl::GetReplState>(ctx); } + void SetReplLSNs(LSNPair LSNs, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::SetReplLSNs>(LSNs, ctx); } + void SetSlaveReplStatus(ReplicationState::Status status, const Error& error, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::SetSlaveReplStatus>(status, error, ctx); } - void SetSlaveReplMasterState(MasterState state, const RdxContext &ctx) { + void SetSlaveReplMasterState(MasterState state, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::SetSlaveReplMasterState>(state, ctx); } - Error ReplaceTagsMatcher(const TagsMatcher &tm, const RdxContext &ctx) { + Error ReplaceTagsMatcher(const TagsMatcher& tm, const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::ReplaceTagsMatcher>(tm, ctx); } - void Rename(const Namespace::Ptr &dst, const std::string &storagePath, const RdxContext &ctx) { + void Rename(const Namespace::Ptr& dst, const std::string& storagePath, const RdxContext& ctx) { if (this == dst.get() || dst == nullptr) { return; } doRename(dst, std::string(), storagePath, ctx); } - void Rename(const std::string &newName, const std::string &storagePath, const RdxContext &ctx) { + void Rename(const std::string& newName, const std::string& storagePath, const RdxContext& ctx) { if (newName.empty()) { return; } doRename(nullptr, newName, storagePath, ctx); } - void OnConfigUpdated(DBConfigProvider &configProvider, const RdxContext &ctx) { + void OnConfigUpdated(DBConfigProvider& configProvider, const RdxContext& ctx) { NamespaceConfigData configData; const auto nsName = GetName(ctx); std::string_view realNsName(nsName); @@ -210,11 +212,11 @@ class Namespace { longUpdDelLoggingParams_.store(configProvider.GetUpdDelLoggingParams(), std::memory_order_relaxed); nsFuncWrapper<&NamespaceImpl::OnConfigUpdated>(configProvider, ctx); } - StorageOpts GetStorageOpts(const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::GetStorageOpts>(ctx); } - void Refill(std::vector &items, const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::Refill>(items, ctx); } - void MergeTagsMatcher(const TagsMatcher &tm, const RdxContext &ctx) { return nsFuncWrapper<&NamespaceImpl::MergeTagsMatcher>(tm, ctx); } + StorageOpts GetStorageOpts(const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::GetStorageOpts>(ctx); } + void Refill(std::vector& items, const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::Refill>(items, ctx); } + void MergeTagsMatcher(const TagsMatcher& tm, const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::MergeTagsMatcher>(tm, ctx); } - void DumpIndex(std::ostream &os, std::string_view index, const RdxContext &ctx) { + void DumpIndex(std::ostream& os, std::string_view index, const RdxContext& ctx) { return nsFuncWrapper<&NamespaceImpl::DumpIndex>(os, index, ctx); } @@ -222,9 +224,9 @@ class Namespace { friend class ReindexerImpl; friend class QueryResults; void updateSelectTime() const { nsFuncWrapper<&NamespaceImpl::updateSelectTime>(); } - void setSlaveMode(const RdxContext &ctx) { nsFuncWrapper<&NamespaceImpl::setSlaveMode>(ctx); } + void setSlaveMode(const RdxContext& ctx) { nsFuncWrapper<&NamespaceImpl::setSlaveMode>(ctx); } NamespaceImpl::Ptr getMainNs() const { return atomicLoadMainNs(); } - NamespaceImpl::Ptr awaitMainNs(const RdxContext &ctx) const { + NamespaceImpl::Ptr awaitMainNs(const RdxContext& ctx) const { if (hasCopy_.load(std::memory_order_acquire)) { contexted_unique_lock lck(clonerMtx_, ctx); assertrx(!hasCopy_.load(std::memory_order_acquire)); @@ -234,14 +236,14 @@ class Namespace { } private: - bool needNamespaceCopy(const NamespaceImpl::Ptr &ns, const Transaction &tx) const noexcept; - bool isExpectingSelectsOnNamespace(const NamespaceImpl::Ptr &ns, const RdxContext &ctx); - void doRename(const Namespace::Ptr &dst, const std::string &newName, const std::string &storagePath, const RdxContext &ctx); + bool needNamespaceCopy(const NamespaceImpl::Ptr& ns, const Transaction& tx) const noexcept; + bool isExpectingSelectsOnNamespace(const NamespaceImpl::Ptr& ns, const RdxContext& ctx); + void doRename(const Namespace::Ptr& dst, const std::string& newName, const std::string& storagePath, const RdxContext& ctx); NamespaceImpl::Ptr atomicLoadMainNs() const { std::lock_guard lck(nsPtrSpinlock_); return ns_; } - void atomicStoreMainNs(NamespaceImpl *ns) { + void atomicStoreMainNs(NamespaceImpl* ns) { std::lock_guard lck(nsPtrSpinlock_); ns_.reset(ns); } @@ -260,7 +262,7 @@ class Namespace { PerfStatCounterMT copyStatsCounter_; std::atomic longTxLoggingParams_; std::atomic longUpdDelLoggingParams_; - BackgroundNamespaceDeleter &bgDeleter_; + BackgroundNamespaceDeleter& bgDeleter_; }; } // namespace reindexer diff --git a/cpp_src/core/namespace/namespaceimpl.cc b/cpp_src/core/namespace/namespaceimpl.cc index e71e2f1a3..0bb614cbf 100644 --- a/cpp_src/core/namespace/namespaceimpl.cc +++ b/cpp_src/core/namespace/namespaceimpl.cc @@ -98,7 +98,9 @@ NamespaceImpl::NamespaceImpl(const NamespaceImpl& src, AsyncStorage::FullLockT& strHolder_{makeStringsHolder()}, nsUpdateSortedContextMemory_{0}, dbDestroyed_(false) { - for (auto& idxIt : src.indexes_) indexes_.push_back(idxIt->Clone()); + for (auto& idxIt : src.indexes_) { + indexes_.push_back(idxIt->Clone()); + } markUpdated(IndexOptimization::Full); logPrintf(LogInfo, "Namespace::CopyContentsFrom (%s).Workers: %d, timeout: %d, tm: { state_token: 0x%08X, version: %d }", name_, @@ -285,7 +287,9 @@ void NamespaceImpl::OnConfigUpdated(DBConfigProvider& configProvider, const RdxC logPrintf(LogInfo, "[%s] WAL has been resized lsn #%s, max size %ld", name_, repl_.lastLsn, wal_.Capacity()); } - if (isSystem()) return; + if (isSystem()) { + return; + } if (serverId_ != replicationConf.serverId) { serverId_ = replicationConf.serverId; @@ -293,7 +297,9 @@ void NamespaceImpl::OnConfigUpdated(DBConfigProvider& configProvider, const RdxC replStateUpdates_.fetch_add(1, std::memory_order_release); } - if (repl_.temporary) return; + if (repl_.temporary) { + return; + } ReplicationRole curRole; if (repl_.slaveMode && !repl_.replicatorEnabled) { // read only @@ -310,9 +316,15 @@ void NamespaceImpl::OnConfigUpdated(DBConfigProvider& configProvider, const RdxC if (!replicationConf.namespaces.empty() && replicationConf.namespaces.find(name_) == replicationConf.namespaces.end()) { newRole = ReplicationMaster; } - if (newRole == curRole) return; - if (newRole == ReplicationNone && curRole == ReplicationMaster) return; - if (curRole == ReplicationReadOnly && serverIdChanged_) return; + if (newRole == curRole) { + return; + } + if (newRole == ReplicationNone && curRole == ReplicationMaster) { + return; + } + if (curRole == ReplicationReadOnly && serverIdChanged_) { + return; + } // switch slave -> master // switch master -> slave @@ -359,7 +371,9 @@ class NamespaceImpl::RollBack_recreateCompositeIndexes final : private RollBackB } } void RollBack() noexcept { - if (IsDisabled()) return; + if (IsDisabled()) { + return; + } for (size_t i = 0, s = indexes_.size(); i < s; ++i) { std::swap(ns_.indexes_[i + startIdx_], indexes_[i]); } @@ -435,7 +449,9 @@ class NamespaceImpl::RollBack_updateItems final : private RollBackBase { RollBackBase::Disable(); } void RollBack() noexcept { - if (IsDisabled()) return; + if (IsDisabled()) { + return; + } if (!items_.empty()) { ns_.repl_.dataHash = dataHash_; ns_.itemsDataSize_ = itemsDataSize_; @@ -573,8 +589,8 @@ NamespaceImpl::RollBack_updateItems NamespaceImpl::updateItems(con itemsDataSize_ = 0; auto indexesCacheCleaner{GetIndexesCacheCleaner()}; - auto &tuple = *indexes_[0]; - auto &index = *indexes_[changedField]; + auto& tuple = *indexes_[0]; + auto& index = *indexes_[changedField]; WrSerializer pk, data; for (size_t rowId = 0; rowId < items_.size(); ++rowId) { @@ -627,7 +643,7 @@ NamespaceImpl::RollBack_updateItems NamespaceImpl::updateItems(con for (int fieldIdx = compositeStartIdx; fieldIdx < compositeEndIdx; ++fieldIdx) { needClearCache = false; - auto &fieldIndex = *indexes_[fieldIdx]; + auto& fieldIndex = *indexes_[fieldIdx]; fieldIndex.Upsert(Variant(plNew), rowId, needClearCache); if (needClearCache && fieldIndex.IsOrdered()) { indexesCacheCleaner.Add(fieldIndex.SortId()); @@ -737,7 +753,9 @@ void NamespaceImpl::SetSchema(std::string_view schema, const RdxContext& ctx) { "0x%08X, version: %d }", name_, serverId_, tagsMatcher_.stateToken(), initTmVer, tagsMatcher_.stateToken(), tagsMatcher_.version()); const lsn_t lsn(wal_.Add(WALRecord(WalEmpty), lsn_t()), serverId_); - if (!ctx.fromReplication_) repl_.lastSelfLSN = lsn; + if (!ctx.fromReplication_) { + repl_.lastSelfLSN = lsn; + } if (!repl_.temporary) { WrSerializer wser; wser.PutVarint(tagsMatcher_.version()); @@ -759,7 +777,9 @@ std::string NamespaceImpl::GetSchema(int format, const RdxContext& ctx) { schema_->GetJSON(ser); } else if (format == ProtobufSchemaType) { Error err = schema_->GetProtobufSchema(ser); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } } else { throw Error(errParams, "Unknown schema type: %d", format); } @@ -792,12 +812,15 @@ void NamespaceImpl::dropIndex(const IndexDef& index) { int fieldIdx = itIdxName->second; std::unique_ptr& indexToRemove = indexes_[fieldIdx]; - if (!IsComposite(indexToRemove->Type()) && indexToRemove->Opts().IsSparse()) --sparseIndexesCount_; + if (!IsComposite(indexToRemove->Type()) && indexToRemove->Opts().IsSparse()) { + --sparseIndexesCount_; + } // Check, that index to remove is not a part of composite index for (int i = indexes_.firstCompositePos(); i < indexes_.totalSize(); ++i) { - if (indexes_[i]->Fields().contains(fieldIdx)) + if (indexes_[i]->Fields().contains(fieldIdx)) { throw Error(errLogic, "Cannot remove index %s : it's a part of a composite index %s", index.name_, indexes_[i]->Name()); + } } for (auto& namePair : indexesNames_) { if (namePair.second >= fieldIdx) { @@ -863,7 +886,9 @@ static void verifyConvertTypes(KeyValueType from, KeyValueType to, const Payload [&](KeyValueType::Bool) noexcept { value = Variant(false); }, [](KeyValueType::Null) noexcept {}, [&](KeyValueType::Int) noexcept { value = Variant(0); }, [&](KeyValueType::Uuid) noexcept { value = Variant{Uuid{}}; }, [&](OneOf) { - if (!to.IsSame(from)) throw Error(errParams, "Cannot convert key from type %s to %s", from.Name(), to.Name()); + if (!to.IsSame(from)) { + throw Error(errParams, "Cannot convert key from type %s to %s", from.Name(), to.Name()); + } }); value.convert(to, &payloadType, &fields); } @@ -1006,7 +1031,9 @@ class NamespaceImpl::RollBack_insertIndex final : private RollBackBase { RollBack_insertIndex(RollBack_insertIndex&&) noexcept = default; ~RollBack_insertIndex() override { RollBack(); } void RollBack() noexcept { - if (IsDisabled()) return; + if (IsDisabled()) { + return; + } if (insertedIdxName_) { try { ns_.indexesNames_.erase(*insertedIdxName_); @@ -1081,19 +1108,35 @@ class NamespaceImpl::RollBack_addIndex final : private RollBackBase { RollBack_addIndex(const RollBack_addIndex&) = delete; ~RollBack_addIndex() override { RollBack(); } void RollBack() noexcept { - if (IsDisabled()) return; - if (oldPayloadType_) ns_.payloadType_ = std::move(*oldPayloadType_); - if (needDecreaseSparseIndexCount_) --ns_.sparseIndexesCount_; - if (rollbacker_updateItems_) rollbacker_updateItems_->RollBack(); - if (rollbacker_insertIndex_) rollbacker_insertIndex_->RollBack(); - if (needResetPayloadTypeInTagsMatcher_) ns_.tagsMatcher_.UpdatePayloadType(ns_.payloadType_); + if (IsDisabled()) { + return; + } + if (oldPayloadType_) { + ns_.payloadType_ = std::move(*oldPayloadType_); + } + if (needDecreaseSparseIndexCount_) { + --ns_.sparseIndexesCount_; + } + if (rollbacker_updateItems_) { + rollbacker_updateItems_->RollBack(); + } + if (rollbacker_insertIndex_) { + rollbacker_insertIndex_->RollBack(); + } + if (needResetPayloadTypeInTagsMatcher_) { + ns_.tagsMatcher_.UpdatePayloadType(ns_.payloadType_); + } Disable(); } void RollBacker_insertIndex(RollBack_insertIndex&& rb) noexcept { rollbacker_insertIndex_.emplace(std::move(rb)); } void RollBacker_updateItems(RollBack_updateItems&& rb) noexcept { rollbacker_updateItems_.emplace(std::move(rb)); } void Disable() noexcept override { - if (rollbacker_insertIndex_) rollbacker_insertIndex_->Disable(); - if (rollbacker_updateItems_) rollbacker_updateItems_->Disable(); + if (rollbacker_insertIndex_) { + rollbacker_insertIndex_->Disable(); + } + if (rollbacker_updateItems_) { + rollbacker_updateItems_->Disable(); + } RollBackBase::Disable(); } void NeedDecreaseSparseIndexCount() noexcept { needDecreaseSparseIndexCount_ = true; } @@ -1196,7 +1239,9 @@ void NamespaceImpl::fillSparseIndex(Index& index, std::string_view jsonPath) { krefs.resize(0); bool needClearCache{false}; index.Upsert(krefs, skrefs, int(rowId), needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } scheduleIndexOptimization(IndexOptimization::Partial); } @@ -1256,7 +1301,9 @@ void NamespaceImpl::addCompositeIndex(const IndexDef& indexDef) { if (!items_[rowId].IsFree()) { bool needClearCache{false}; indexes_[idxPos]->Upsert(Variant(items_[rowId]), rowId, needClearCache); - if (needClearCache && indexes_[idxPos]->IsOrdered()) indexesCacheCleaner.Add(indexes_[idxPos]->SortId()); + if (needClearCache && indexes_[idxPos]->IsOrdered()) { + indexesCacheCleaner.Add(indexes_[idxPos]->SortId()); + } } } @@ -1336,7 +1383,9 @@ int NamespaceImpl::getScalarIndexByName(std::string_view index) const { bool NamespaceImpl::tryGetIndexByName(std::string_view name, int& index) const { auto it = indexesNames_.find(name); - if (it == indexesNames_.end()) return false; + if (it == indexesNames_.end()) { + return false; + } index = it->second; return true; } @@ -1396,9 +1445,15 @@ void NamespaceImpl::doUpdate(const Query& query, QueryResults& result, const NsC bool updateWithJson = false; bool withExpressions = false; for (const UpdateEntry& ue : query.UpdateFields()) { - if (ue.IsExpression()) withExpressions = true; - if (ue.Mode() == FieldModeSetJson) updateWithJson = true; - if (withExpressions && updateWithJson) break; + if (ue.IsExpression()) { + withExpressions = true; + } + if (ue.Mode() == FieldModeSetJson) { + updateWithJson = true; + } + if (withExpressions && updateWithJson) { + break; + } } if (ctx.rdxContext.fromReplication_ && withExpressions) { @@ -1477,14 +1532,19 @@ void NamespaceImpl::replicateItem(IdType itemId, const NsContext& ctx, bool stat auto sendWalUpdate = [this, itemId, &ctx, &pv](ItemModifyMode mode) { lsn_t lsn(wal_.Add(WALRecord(WalItemUpdate, itemId, ctx.inTransaction), lsn_t(pv.GetLSN())), serverId_); - if (!ctx.rdxContext.fromReplication_) repl_.lastSelfLSN = lsn; + if (!ctx.rdxContext.fromReplication_) { + repl_.lastSelfLSN = lsn; + } pv.SetLSN(int64_t(lsn)); ItemImpl item(payloadType_, pv, tagsMatcher_); std::string_view cjson = item.GetCJSON(false); - if (!repl_.temporary) + if (!repl_.temporary) { observers_->OnWALUpdate(LSNPair(lsn, ctx.rdxContext.fromReplication_ ? ctx.rdxContext.LSNs_.originLSN_ : lsn), name_, WALRecord(WalItemModify, cjson, tagsMatcher_.version(), mode, ctx.inTransaction)); - if (!ctx.rdxContext.fromReplication_) setReplLSNs(LSNPair(lsn_t(), lsn)); + } + if (!ctx.rdxContext.fromReplication_) { + setReplLSNs(LSNPair(lsn_t(), lsn)); + } }; if (!statementReplication) { @@ -1564,7 +1624,9 @@ void NamespaceImpl::doDelete(IdType id) { for (field = indexes_.firstCompositePos(); field < indexes_.totalSize(); ++field) { bool needClearCache{false}; indexes_[field]->Delete(Variant(items_[id]), id, *strHolder_, needClearCache); - if (needClearCache && indexes_[field]->IsOrdered()) indexesCacheCleaner.Add(indexes_[field]->SortId()); + if (needClearCache && indexes_[field]->IsOrdered()) { + indexesCacheCleaner.Add(indexes_[field]->SortId()); + } } // Holder for tuple. It is required for sparse indexes will be valid @@ -1594,7 +1656,9 @@ void NamespaceImpl::doDelete(IdType id) { // Delete value from index bool needClearCache{false}; index.Delete(skrefs, id, *strHolder_, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } while (++field != borderIdx); // free PayloadValue @@ -1640,7 +1704,9 @@ void NamespaceImpl::doDelete(const Query& query, QueryResults& result, const NsC for (auto it : result) { cjson.Reset(); const auto err = it.GetCJSON(cjson, false); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } const WALRecord wrec{WalItemModify, cjson.Slice(), tagsMatcher_.version(), ModeDelete, ctx.inTransaction}; processWalRecord(wrec, ctx.rdxContext); } @@ -1673,7 +1739,9 @@ void NamespaceImpl::Truncate(const RdxContext& ctx) { void NamespaceImpl::doTruncate(const NsContext& ctx) { if (storage_.IsValid()) { for (PayloadValue& pv : items_) { - if (pv.IsFree()) continue; + if (pv.IsFree()) { + continue; + } Payload pl(payloadType_, pv); WrSerializer pk; pk << kRxStorageItemPrefix; @@ -1698,11 +1766,16 @@ void NamespaceImpl::doTruncate(const NsContext& ctx) { WALRecord wrec(WalUpdateQuery, (ser << "TRUNCATE " << name_).Slice()); lsn_t lsn(wal_.Add(wrec), serverId_); - if (!ctx.rdxContext.fromReplication_) repl_.lastSelfLSN = lsn; + if (!ctx.rdxContext.fromReplication_) { + repl_.lastSelfLSN = lsn; + } markUpdated(IndexOptimization::Full); - if (!repl_.temporary) + if (!repl_.temporary) { observers_->OnWALUpdate(LSNPair(lsn, ctx.rdxContext.fromReplication_ ? ctx.rdxContext.LSNs_.originLSN_ : lsn), name_, wrec); - if (!ctx.rdxContext.fromReplication_) setReplLSNs(LSNPair(lsn_t(), lsn)); + } + if (!ctx.rdxContext.fromReplication_) { + setReplLSNs(LSNPair(lsn_t(), lsn)); + } } void NamespaceImpl::Refill(std::vector& items, const RdxContext& ctx) { @@ -1849,9 +1922,12 @@ void NamespaceImpl::CommitTransaction(Transaction& tx, QueryResults& result, NsC WALRecord initWrec(WalInitTransaction, 0, true); lsn_t lsn(wal_.Add(initWrec), serverId_); - if (!ctx.rdxContext.fromReplication_) repl_.lastSelfLSN = lsn; - if (!repl_.temporary) + if (!ctx.rdxContext.fromReplication_) { + repl_.lastSelfLSN = lsn; + } + if (!repl_.temporary) { observers_->OnWALUpdate(LSNPair(lsn, ctx.rdxContext.fromReplication_ ? ctx.rdxContext.LSNs_.originLSN_ : lsn), name_, initWrec); + } AsyncStorage::AdviceGuardT storageAdvice; if (tx.GetSteps().size() >= AsyncStorage::kLimitToAdviceBatching) { @@ -1928,7 +2004,9 @@ void NamespaceImpl::doUpsert(ItemImpl* ritem, IdType id, bool doUpdate) { for (size_t field = 0; field < compIndexesCount; ++field) { const auto& fields = indexes_[field + indexes_.firstCompositePos()]->Fields(); for (const auto f : fields) { - if (f == IndexValueType::SetByJsonPath) continue; + if (f == IndexValueType::SetByJsonPath) { + continue; + } pl.Get(f, skrefs); plNew.Get(f, krefs); if (skrefs != krefs) { @@ -1937,7 +2015,9 @@ void NamespaceImpl::doUpsert(ItemImpl* ritem, IdType id, bool doUpdate) { break; } } - if (needUpdateCompIndexes[field]) continue; + if (needUpdateCompIndexes[field]) { + continue; + } for (size_t i = 0, end = fields.getTagsPathsLength(); i < end; ++i) { const auto& tp = fields.getTagsPath(i); pl.GetByJsonPath(tp, skrefs, KeyValueType::Undefined{}); @@ -1983,7 +2063,9 @@ void NamespaceImpl::doUpsert(ItemImpl* ritem, IdType id, bool doUpdate) { } if (index.Opts().GetCollateMode() == CollateUTF8) { - for (auto& key : skrefs) key.EnsureUTF8(); + for (auto& key : skrefs) { + key.EnsureUTF8(); + } } // Check for update @@ -2001,16 +2083,22 @@ void NamespaceImpl::doUpsert(ItemImpl* ritem, IdType id, bool doUpdate) { } else { pl.Get(field, krefs); } - if ((krefs.ArrayType().Is() && skrefs.ArrayType().Is()) || krefs == skrefs) continue; + if ((krefs.ArrayType().Is() && skrefs.ArrayType().Is()) || krefs == skrefs) { + continue; + } bool needClearCache{false}; index.Delete(krefs, id, *strHolder_, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } } // Put value to index krefs.resize(0); bool needClearCache{false}; index.Upsert(krefs, skrefs, id, needClearCache); - if (needClearCache && index.IsOrdered()) indexesCacheCleaner.Add(index.SortId()); + if (needClearCache && index.IsOrdered()) { + indexesCacheCleaner.Add(index.SortId()); + } if (!isIndexSparse) { // Put value to payload @@ -2022,12 +2110,16 @@ void NamespaceImpl::doUpsert(ItemImpl* ritem, IdType id, bool doUpdate) { for (int field2 = indexes_.firstCompositePos(); field2 < indexes_.totalSize(); ++field2) { bool needClearCache{false}; if (doUpdate) { - if (!needUpdateCompIndexes[field2 - indexes_.firstCompositePos()]) continue; + if (!needUpdateCompIndexes[field2 - indexes_.firstCompositePos()]) { + continue; + } // Delete from composite indexes first indexes_[field2]->Delete(oldData, id, *strHolder_, needClearCache); } indexes_[field2]->Upsert(Variant{plData}, id, needClearCache); - if (needClearCache && indexes_[field2]->IsOrdered()) indexesCacheCleaner.Add(indexes_[field2]->SortId()); + if (needClearCache && indexes_[field2]->IsOrdered()) { + indexesCacheCleaner.Add(indexes_[field2]->SortId()); + } } repl_.dataHash ^= pl.GetHash(); itemsDataSize_ += plData.GetCapacity() + sizeof(PayloadValue::dataHeader); @@ -2083,10 +2175,13 @@ void NamespaceImpl::updateTagsMatcherFromItem(ItemImpl* ritem) { *ritem = std::move(tmpItem); auto err = ritem->FromJSON(jsonSliceBuf, nullptr); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } - if (ritem->tagsMatcher().isUpdated() && !tagsMatcher_.try_merge(ritem->tagsMatcher())) + if (ritem->tagsMatcher().isUpdated() && !tagsMatcher_.try_merge(ritem->tagsMatcher())) { throw Error(errLogic, "Could not insert item. TagsMatcher was not merged."); + } ritem->tagsMatcher() = tagsMatcher_; ritem->tagsMatcher().setUpdated(); } else if (ritem->tagsMatcher().isUpdated()) { @@ -2145,7 +2240,9 @@ void NamespaceImpl::doModifyItem(Item& item, ItemModifyMode mode, const NsContex IdType id = exists ? realItem.first : createItem(newPl.RealSize()); lsn_t lsn(wal_.Add(WALRecord(WalItemUpdate, id, ctx.inTransaction), exists ? lsn_t(items_[id].GetLSN()) : lsn_t()), serverId_); - if (!ctx.rdxContext.fromReplication_) repl_.lastSelfLSN = lsn; + if (!ctx.rdxContext.fromReplication_) { + repl_.lastSelfLSN = lsn; + } item.setLSN(int64_t(lsn)); item.setID(id); @@ -2163,11 +2260,14 @@ void NamespaceImpl::doModifyItem(Item& item, ItemModifyMode mode, const NsContex if (!repl_.temporary) { // not send row with fromReplication=true and originLSN_= empty - if (!ctx.rdxContext.fromReplication_ || !ctx.rdxContext.LSNs_.originLSN_.isEmpty()) + if (!ctx.rdxContext.fromReplication_ || !ctx.rdxContext.LSNs_.originLSN_.isEmpty()) { observers_->OnModifyItem(LSNPair(lsn, ctx.rdxContext.fromReplication_ ? ctx.rdxContext.LSNs_.originLSN_ : lsn), name_, item.impl_, mode, ctx.inTransaction); + } + } + if (!ctx.rdxContext.fromReplication_) { + setReplLSNs(LSNPair(lsn_t(), lsn)); } - if (!ctx.rdxContext.fromReplication_) setReplLSNs(LSNPair(lsn_t(), lsn)); markUpdated(exists ? IndexOptimization::Partial : IndexOptimization::Full); } @@ -2180,8 +2280,9 @@ RX_ALWAYS_INLINE VariantArray NamespaceImpl::getPkKeys(const ConstPayload& cpl, keys.emplace_back(*cpl.Value()); } else if (pkIndex->Opts().IsSparse()) { cpl.GetByJsonPath(pkIndex->Fields().getTagsPath(0), keys, pkIndex->KeyType()); - } else + } else { cpl.Get(fieldNum, keys); + } return keys; } @@ -2201,7 +2302,9 @@ RX_ALWAYS_INLINE SelectKeyResult NamespaceImpl::getPkDocs(const ConstPayload& cp // find id by PK. NOT THREAD SAFE! std::pair NamespaceImpl::findByPK(ItemImpl* ritem, bool inTransaction, const RdxContext& ctx) { SelectKeyResult res = getPkDocs(ritem->GetConstPayload(), inTransaction, ctx); - if (res.size() && res[0].ids_.size()) return {res[0].ids_[0], true}; + if (res.size() && res[0].ids_.size()) { + return {res[0].ids_[0], true}; + } return {-1, false}; } @@ -2232,7 +2335,9 @@ void NamespaceImpl::optimizeIndexes(const NsContext& ctx) { // This is read lock only atomics based implementation of rebuild indexes // If optimizationState_ == OptimizationCompleted is true, then indexes are completely built. // In this case reset optimizationState_ and/or any idset's and sort orders builds are allowed only protected by write lock - if (optimizationState_.load(std::memory_order_relaxed) == OptimizationCompleted) return; + if (optimizationState_.load(std::memory_order_relaxed) == OptimizationCompleted) { + return; + } auto lastUpdateTime = lastUpdateTime_.load(std::memory_order_acquire); Locker::RLockT rlck; @@ -2290,12 +2395,18 @@ void NamespaceImpl::optimizeIndexes(const NsContext& ctx) { } }); } - for (size_t i = 0; i < maxIndexWorkers; i++) thrs[i].join(); + for (size_t i = 0; i < maxIndexWorkers; i++) { + thrs[i].join(); + } + } + if (cancelCommitCnt_.load(std::memory_order_relaxed)) { + break; } - if (cancelCommitCnt_.load(std::memory_order_relaxed)) break; } - if (dbDestroyed_.load(std::memory_order_relaxed)) return; + if (dbDestroyed_.load(std::memory_order_relaxed)) { + return; + } if (maxIndexWorkers && !cancelCommitCnt_.load(std::memory_order_relaxed)) { optimizationState_.store(OptimizationCompleted, std::memory_order_release); @@ -2483,7 +2594,9 @@ void NamespaceImpl::ResetPerfStat(const RdxContext& ctx) { auto rlck = rLock(ctx); selectPerfCounter_.Reset(); updatePerfCounter_.Reset(); - for (auto& i : indexes_) i->ResetIndexPerfStat(); + for (auto& i : indexes_) { + i->ResetIndexPerfStat(); + } } Error NamespaceImpl::loadLatestSysRecord(std::string_view baseSysTag, uint64_t& version, std::string& content) { @@ -2664,7 +2777,9 @@ void NamespaceImpl::saveIndexesToStorage() { // clear ItemImpl pool on payload change pool_.clear(); - if (!storage_.IsValid()) return; + if (!storage_.IsValid()) { + return; + } logPrintf(LogTrace, "Namespace::saveIndexesToStorage (%s)", name_); @@ -2689,11 +2804,15 @@ void NamespaceImpl::saveIndexesToStorage() { } void NamespaceImpl::saveSchemaToStorage() { - if (!storage_.IsValid()) return; + if (!storage_.IsValid()) { + return; + } logPrintf(LogTrace, "Namespace::saveSchemaToStorage (%s)", name_); - if (!schema_) return; + if (!schema_) { + return; + } WrSerializer ser; ser.PutUInt64(sysRecordsVersions_.schemaVersion); @@ -2709,7 +2828,9 @@ void NamespaceImpl::saveSchemaToStorage() { } void NamespaceImpl::saveReplStateToStorage(bool direct) { - if (!storage_.IsValid()) return; + if (!storage_.IsValid()) { + return; + } if (direct) { replStateUpdates_.store(0, std::memory_order_release); @@ -2858,7 +2979,9 @@ void NamespaceImpl::removeExpiredItems(RdxActivityContext* ctx) { } lastExpirationCheckTs_ = now; for (const std::unique_ptr& index : indexes_) { - if ((index->Type() != IndexTtl) || (index->Size() == 0)) continue; + if ((index->Type() != IndexTtl) || (index->Size() == 0)) { + continue; + } const int64_t expirationThreshold = std::chrono::duration_cast(system_clock_w::now_coarse().time_since_epoch()).count() - index->GetTTLValue(); @@ -3068,16 +3191,23 @@ void NamespaceImpl::warmupFtIndexes() { } int NamespaceImpl::getSortedIdxCount() const noexcept { - if (!config_.optimizationSortWorkers) return 0; + if (!config_.optimizationSortWorkers) { + return 0; + } int cnt = 0; - for (auto& it : indexes_) - if (it->IsOrdered()) cnt++; + for (auto& it : indexes_) { + if (it->IsOrdered()) { + cnt++; + } + } return cnt; } void NamespaceImpl::updateSortedIdxCount() { int sortedIdxCount = getSortedIdxCount(); - for (auto& idx : indexes_) idx->SetSortedIdxCount(sortedIdxCount); + for (auto& idx : indexes_) { + idx->SetSortedIdxCount(sortedIdxCount); + } scheduleIndexOptimization(IndexOptimization::Full); } @@ -3165,13 +3295,17 @@ void NamespaceImpl::FillResult(QueryResults& result, const IdSet& ids) const { } void NamespaceImpl::getFromJoinCache(const Query& q, const JoinedQuery& jq, JoinCacheRes& out) const { - if (config_.cacheMode == CacheModeOff || optimizationState_ != OptimizationCompleted) return; + if (config_.cacheMode == CacheModeOff || optimizationState_ != OptimizationCompleted) { + return; + } out.key.SetData(jq, q); getFromJoinCacheImpl(out); } void NamespaceImpl::getFromJoinCache(const Query& q, JoinCacheRes& out) const { - if (config_.cacheMode == CacheModeOff || optimizationState_ != OptimizationCompleted) return; + if (config_.cacheMode == CacheModeOff || optimizationState_ != OptimizationCompleted) { + return; + } out.key.SetData(q); getFromJoinCacheImpl(out); } @@ -3191,7 +3325,9 @@ void NamespaceImpl::getFromJoinCacheImpl(JoinCacheRes& ctx) const { } void NamespaceImpl::getInsideFromJoinCache(JoinCacheRes& ctx) const { - if (config_.cacheMode != CacheModeAggressive || optimizationState_ != OptimizationCompleted) return; + if (config_.cacheMode != CacheModeAggressive || optimizationState_ != OptimizationCompleted) { + return; + } getFromJoinCacheImpl(ctx); } @@ -3219,14 +3355,24 @@ const FieldsSet& NamespaceImpl::pkFields() { void NamespaceImpl::processWalRecord(const WALRecord& wrec, const RdxContext& ctx, lsn_t itemLsn, Item* item) { lsn_t lsn(wal_.Add(wrec, itemLsn), serverId_); - if (!ctx.fromReplication_) repl_.lastSelfLSN = lsn; - if (item) item->setLSN(int64_t(lsn)); - if (!repl_.temporary) observers_->OnWALUpdate(LSNPair(lsn, ctx.fromReplication_ ? ctx.LSNs_.originLSN_ : lsn), name_, wrec); - if (!ctx.fromReplication_) setReplLSNs(LSNPair(lsn_t(), lsn)); + if (!ctx.fromReplication_) { + repl_.lastSelfLSN = lsn; + } + if (item) { + item->setLSN(int64_t(lsn)); + } + if (!repl_.temporary) { + observers_->OnWALUpdate(LSNPair(lsn, ctx.fromReplication_ ? ctx.LSNs_.originLSN_ : lsn), name_, wrec); + } + if (!ctx.fromReplication_) { + setReplLSNs(LSNPair(lsn_t(), lsn)); + } } NamespaceImpl::IndexesCacheCleaner::~IndexesCacheCleaner() { - for (auto& idx : ns_.indexes_) idx->ClearCache(sorts_); + for (auto& idx : ns_.indexes_) { + idx->ClearCache(sorts_); + } } int64_t NamespaceImpl::correctMaxIterationsIdSetPreResult(int64_t maxIterationsIdSetPreResult) const { diff --git a/cpp_src/core/namespace/namespaceimpl.h b/cpp_src/core/namespace/namespaceimpl.h index 0a1e41a0a..8e0d22d80 100644 --- a/cpp_src/core/namespace/namespaceimpl.h +++ b/cpp_src/core/namespace/namespaceimpl.h @@ -62,17 +62,17 @@ struct DistanceBetweenJoinedIndexesSameNs; } // namespace SortExprFuncs struct NsContext { - NsContext(const RdxContext &rdxCtx) noexcept : rdxContext{rdxCtx} {} - NsContext &InTransaction() noexcept { + NsContext(const RdxContext& rdxCtx) noexcept : rdxContext{rdxCtx} {} + NsContext& InTransaction() noexcept { inTransaction = true; return *this; } - NsContext &CopiedNsRequest() noexcept { + NsContext& CopiedNsRequest() noexcept { isCopiedNsRequest = true; return *this; } - const RdxContext &rdxContext; + const RdxContext& rdxContext; bool isCopiedNsRequest{false}; bool inTransaction{false}; }; @@ -100,11 +100,11 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor class RollBack_updateItems; class IndexesCacheCleaner { public: - explicit IndexesCacheCleaner(NamespaceImpl &ns) noexcept : ns_{ns} {} - IndexesCacheCleaner(const IndexesCacheCleaner &) = delete; - IndexesCacheCleaner(IndexesCacheCleaner &&) = delete; - IndexesCacheCleaner &operator=(const IndexesCacheCleaner &) = delete; - IndexesCacheCleaner &operator=(IndexesCacheCleaner &&) = delete; + explicit IndexesCacheCleaner(NamespaceImpl& ns) noexcept : ns_{ns} {} + IndexesCacheCleaner(const IndexesCacheCleaner&) = delete; + IndexesCacheCleaner(IndexesCacheCleaner&&) = delete; + IndexesCacheCleaner& operator=(const IndexesCacheCleaner&) = delete; + IndexesCacheCleaner& operator=(IndexesCacheCleaner&&) = delete; void Add(SortType s) { if rx_unlikely (s >= sorts_.size()) { throw Error(errLogic, "Index sort type overflow: %d. Limit is %d", s, sorts_.size() - 1); @@ -116,7 +116,7 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor ~IndexesCacheCleaner(); private: - const NamespaceImpl &ns_; + const NamespaceImpl& ns_; std::bitset sorts_; }; @@ -140,22 +140,23 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor class NSUpdateSortedContext final : public UpdateSortedContext { public: - NSUpdateSortedContext(const NamespaceImpl &ns, SortType curSortId) + NSUpdateSortedContext(const NamespaceImpl& ns, SortType curSortId) : ns_(ns), sorted_indexes_(ns_.getSortedIdxCount()), curSortId_(curSortId) { ids2Sorts_.reserve(ns.items_.size()); ids2SortsMemSize_ = ids2Sorts_.capacity() * sizeof(SortType); ns.nsUpdateSortedContextMemory_.fetch_add(ids2SortsMemSize_); - for (IdType i = 0; i < IdType(ns_.items_.size()); i++) + for (IdType i = 0; i < IdType(ns_.items_.size()); i++) { ids2Sorts_.push_back(ns_.items_[i].IsFree() ? SortIdUnexists : SortIdUnfilled); + } } ~NSUpdateSortedContext() override { ns_.nsUpdateSortedContextMemory_.fetch_sub(ids2SortsMemSize_); } int getSortedIdxCount() const noexcept override { return sorted_indexes_; } SortType getCurSortId() const noexcept override { return curSortId_; } - const std::vector &ids2Sorts() const noexcept override { return ids2Sorts_; } - std::vector &ids2Sorts() noexcept override { return ids2Sorts_; } + const std::vector& ids2Sorts() const noexcept override { return ids2Sorts_; } + std::vector& ids2Sorts() noexcept override { return ids2Sorts_; } private: - const NamespaceImpl &ns_; + const NamespaceImpl& ns_; const int sorted_indexes_{0}; const IdType curSortId_{-1}; std::vector ids2Sorts_; @@ -166,26 +167,26 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor public: using Base = std::vector>; - explicit IndexesStorage(const NamespaceImpl &ns); + explicit IndexesStorage(const NamespaceImpl& ns); - IndexesStorage(const IndexesStorage &src) = delete; - IndexesStorage &operator=(const IndexesStorage &src) = delete; + IndexesStorage(const IndexesStorage& src) = delete; + IndexesStorage& operator=(const IndexesStorage& src) = delete; - IndexesStorage(IndexesStorage &&src) = delete; - IndexesStorage &operator=(IndexesStorage &&src) noexcept = delete; + IndexesStorage(IndexesStorage&& src) = delete; + IndexesStorage& operator=(IndexesStorage&& src) noexcept = delete; int denseIndexesSize() const { return ns_.payloadType_.NumFields(); } int sparseIndexesSize() const { return ns_.sparseIndexesCount_; } int compositeIndexesSize() const { return totalSize() - denseIndexesSize() - sparseIndexesSize(); } - void MoveBase(IndexesStorage &&src); + void MoveBase(IndexesStorage&& src); int firstSparsePos() const { return ns_.payloadType_.NumFields(); } int firstCompositePos() const { return ns_.payloadType_.NumFields() + ns_.sparseIndexesCount_; } - int firstCompositePos(const PayloadType &pt, int sparseIndexes) const { return pt.NumFields() + sparseIndexes; } + int firstCompositePos(const PayloadType& pt, int sparseIndexes) const { return pt.NumFields() + sparseIndexes; } int totalSize() const { return size(); } private: - const NamespaceImpl &ns_; + const NamespaceImpl& ns_; }; class Items final : public std::vector { @@ -199,97 +200,97 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor using Ptr = intrusive_ptr; using Mutex = MarkedMutex; - NamespaceImpl(const std::string &_name, UpdatesObservers &observers); - NamespaceImpl &operator=(const NamespaceImpl &) = delete; + NamespaceImpl(const std::string& _name, UpdatesObservers& observers); + NamespaceImpl& operator=(const NamespaceImpl&) = delete; ~NamespaceImpl() override; - std::string GetName(const RdxContext &ctx) const { + std::string GetName(const RdxContext& ctx) const { auto rlck = rLock(ctx); return name_; } - bool IsSystem(const RdxContext &ctx) const { + bool IsSystem(const RdxContext& ctx) const { auto rlck = rLock(ctx); return isSystem(); } - bool IsTemporary(const RdxContext &ctx) const { return GetReplState(ctx).temporary; } + bool IsTemporary(const RdxContext& ctx) const { return GetReplState(ctx).temporary; } - void EnableStorage(const std::string &path, StorageOpts opts, StorageType storageType, const RdxContext &ctx); - void LoadFromStorage(unsigned threadsCount, const RdxContext &ctx); - void DeleteStorage(const RdxContext &); + void EnableStorage(const std::string& path, StorageOpts opts, StorageType storageType, const RdxContext& ctx); + void LoadFromStorage(unsigned threadsCount, const RdxContext& ctx); + void DeleteStorage(const RdxContext&); uint32_t GetItemsCount() const { return itemsCount_.load(std::memory_order_relaxed); } uint32_t GetItemsCapacity() const { return itemsCapacity_.load(std::memory_order_relaxed); } - void AddIndex(const IndexDef &indexDef, const RdxContext &ctx); - void UpdateIndex(const IndexDef &indexDef, const RdxContext &ctx); - void DropIndex(const IndexDef &indexDef, const RdxContext &ctx); - void SetSchema(std::string_view schema, const RdxContext &ctx); - std::string GetSchema(int format, const RdxContext &ctx); - - void Insert(Item &item, const RdxContext &ctx); - void Update(Item &item, const RdxContext &ctx); - void Upsert(Item &item, const RdxContext &); - void Delete(Item &item, const RdxContext &); - void ModifyItem(Item &item, ItemModifyMode mode, const RdxContext &ctx); - void Truncate(const RdxContext &); - void Refill(std::vector &, const RdxContext &); + void AddIndex(const IndexDef& indexDef, const RdxContext& ctx); + void UpdateIndex(const IndexDef& indexDef, const RdxContext& ctx); + void DropIndex(const IndexDef& indexDef, const RdxContext& ctx); + void SetSchema(std::string_view schema, const RdxContext& ctx); + std::string GetSchema(int format, const RdxContext& ctx); + + void Insert(Item& item, const RdxContext& ctx); + void Update(Item& item, const RdxContext& ctx); + void Upsert(Item& item, const RdxContext&); + void Delete(Item& item, const RdxContext&); + void ModifyItem(Item& item, ItemModifyMode mode, const RdxContext& ctx); + void Truncate(const RdxContext&); + void Refill(std::vector&, const RdxContext&); template - void Select(QueryResults &result, SelectCtxWithJoinPreSelect ¶ms, const RdxContext &); - NamespaceDef GetDefinition(const RdxContext &ctx); - NamespaceMemStat GetMemStat(const RdxContext &); - NamespacePerfStat GetPerfStat(const RdxContext &); - void ResetPerfStat(const RdxContext &); - std::vector EnumMeta(const RdxContext &ctx); - - void BackgroundRoutine(RdxActivityContext *); + void Select(QueryResults& result, SelectCtxWithJoinPreSelect& params, const RdxContext&); + NamespaceDef GetDefinition(const RdxContext& ctx); + NamespaceMemStat GetMemStat(const RdxContext&); + NamespacePerfStat GetPerfStat(const RdxContext&); + void ResetPerfStat(const RdxContext&); + std::vector EnumMeta(const RdxContext& ctx); + + void BackgroundRoutine(RdxActivityContext*); void StorageFlushingRoutine(); - void CloseStorage(const RdxContext &); + void CloseStorage(const RdxContext&); - Transaction NewTransaction(const RdxContext &ctx); - void CommitTransaction(Transaction &tx, QueryResults &result, NsContext ctx, - QueryStatCalculator &queryStatCalculator); + Transaction NewTransaction(const RdxContext& ctx); + void CommitTransaction(Transaction& tx, QueryResults& result, NsContext ctx, + QueryStatCalculator& queryStatCalculator); - Item NewItem(const RdxContext &ctx); - void ToPool(ItemImpl *item); + Item NewItem(const RdxContext& ctx); + void ToPool(ItemImpl* item); // Get metadata from storage by key - std::string GetMeta(const std::string &key, const RdxContext &ctx); + std::string GetMeta(const std::string& key, const RdxContext& ctx); // Put metadata to storage by key - void PutMeta(const std::string &key, std::string_view data, const RdxContext &ctx); + void PutMeta(const std::string& key, std::string_view data, const RdxContext& ctx); // Delete metadata from storage by key - void DeleteMeta(const std::string &key, const RdxContext &ctx); - int64_t GetSerial(const std::string &field); + void DeleteMeta(const std::string& key, const RdxContext& ctx); + int64_t GetSerial(const std::string& field); int getIndexByName(std::string_view index) const; int getIndexByNameOrJsonPath(std::string_view name) const; int getScalarIndexByName(std::string_view name) const; - bool tryGetIndexByName(std::string_view name, int &index) const; - bool getIndexByNameOrJsonPath(std::string_view name, int &index) const; - bool getScalarIndexByName(std::string_view name, int &index) const; - bool getSparseIndexByJsonPath(std::string_view jsonPath, int &index) const; + bool tryGetIndexByName(std::string_view name, int& index) const; + bool getIndexByNameOrJsonPath(std::string_view name, int& index) const; + bool getScalarIndexByName(std::string_view name, int& index) const; + bool getSparseIndexByJsonPath(std::string_view jsonPath, int& index) const; - void FillResult(QueryResults &result, const IdSet &ids) const; + void FillResult(QueryResults& result, const IdSet& ids) const; void EnablePerfCounters(bool enable = true) { enablePerfCounters_ = enable; } // Replication slave mode functions - ReplicationState GetReplState(const RdxContext &) const; - void SetReplLSNs(LSNPair LSNs, const RdxContext &ctx); + ReplicationState GetReplState(const RdxContext&) const; + void SetReplLSNs(LSNPair LSNs, const RdxContext& ctx); - void SetSlaveReplStatus(ReplicationState::Status, const Error &, const RdxContext &); - void SetSlaveReplMasterState(MasterState state, const RdxContext &); + void SetSlaveReplStatus(ReplicationState::Status, const Error&, const RdxContext&); + void SetSlaveReplMasterState(MasterState state, const RdxContext&); - Error ReplaceTagsMatcher(const TagsMatcher &tm, const RdxContext &); + Error ReplaceTagsMatcher(const TagsMatcher& tm, const RdxContext&); - void OnConfigUpdated(DBConfigProvider &configProvider, const RdxContext &ctx); - StorageOpts GetStorageOpts(const RdxContext &); - std::shared_ptr GetSchemaPtr(const RdxContext &ctx) const; + void OnConfigUpdated(DBConfigProvider& configProvider, const RdxContext& ctx); + StorageOpts GetStorageOpts(const RdxContext&); + std::shared_ptr GetSchemaPtr(const RdxContext& ctx) const; int getNsNumber() const noexcept { return schema_ ? schema_->GetProtobufNsNumber() : 0; } IndexesCacheCleaner GetIndexesCacheCleaner() { return IndexesCacheCleaner{*this}; } // Separate method for the v3/v4 replication compatibility. // It should not be used outside of this scenario - void MergeTagsMatcher(const TagsMatcher &tm, const RdxContext &ctx); + void MergeTagsMatcher(const TagsMatcher& tm, const RdxContext& ctx); void SetDestroyFlag() noexcept { dbDestroyed_ = true; } - Error FlushStorage(const RdxContext &ctx) { + Error FlushStorage(const RdxContext& ctx) { const auto flushOpts = StorageFlushOpts().WithImmediateReopen(); auto lck = rLock(ctx); storage_.Flush(flushOpts); @@ -309,8 +310,8 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor typedef contexted_shared_lock RLockT; typedef contexted_unique_lock WLockT; - RLockT RLock(const RdxContext &ctx) const { return RLockT(mtx_, ctx); } - WLockT WLock(const RdxContext &ctx) const { + RLockT RLock(const RdxContext& ctx) const { return RLockT(mtx_, ctx); } + WLockT WLock(const RdxContext& ctx) const { using namespace std::string_view_literals; WLockT lck(mtx_, ctx); if (readonly_.load(std::memory_order_acquire)) { @@ -318,9 +319,9 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor } return lck; } - bool IsNotLocked(const RdxContext &ctx) const { return WLockT(mtx_, std::try_to_lock_t{}, ctx).owns_lock(); } + bool IsNotLocked(const RdxContext& ctx) const { return WLockT(mtx_, std::try_to_lock_t{}, ctx).owns_lock(); } void MarkReadOnly() { readonly_.store(true, std::memory_order_release); } - std::atomic_bool &IsReadOnly() { return readonly_; } + std::atomic_bool& IsReadOnly() { return readonly_; } private: mutable Mutex mtx_; @@ -328,18 +329,18 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor }; struct PKModifyRevertData { - PKModifyRevertData(PayloadValue &p, lsn_t l) : pv(p), lsn(l) {} - PayloadValue &pv; + PKModifyRevertData(PayloadValue& p, lsn_t l) : pv(p), lsn(l) {} + PayloadValue& pv; lsn_t lsn; }; - Error rebuildIndexesTagsPaths(const TagsMatcher &newTm); + Error rebuildIndexesTagsPaths(const TagsMatcher& newTm); ReplicationState getReplState() const; std::string sysRecordName(std::string_view sysTag, uint64_t version); - void writeSysRecToStorage(std::string_view data, std::string_view sysTag, uint64_t &version, bool direct); + void writeSysRecToStorage(std::string_view data, std::string_view sysTag, uint64_t& version, bool direct); void saveIndexesToStorage(); void saveSchemaToStorage(); - Error loadLatestSysRecord(std::string_view baseSysTag, uint64_t &version, std::string &content); + Error loadLatestSysRecord(std::string_view baseSysTag, uint64_t& version, std::string& content); bool loadIndexesFromStorage(); void saveReplStateToStorage(bool direct = true); void saveTagsMatcherToStorage(bool clearUpdate); @@ -350,69 +351,69 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor void markUpdated(IndexOptimization requestedOptimization); void scheduleIndexOptimization(IndexOptimization requestedOptimization); - void doUpdate(const Query &query, QueryResults &result, const NsContext &); - void doDelete(const Query &query, QueryResults &result, const NsContext &); - void doTruncate(const NsContext &ctx); - void doUpsert(ItemImpl *ritem, IdType id, bool doUpdate); - void modifyItem(Item &item, ItemModifyMode mode, const NsContext &); - void doModifyItem(Item &item, ItemModifyMode mode, const NsContext &ctx); - void deleteItem(Item &item, const NsContext &ctx); - void updateTagsMatcherFromItem(ItemImpl *ritem); + void doUpdate(const Query& query, QueryResults& result, const NsContext&); + void doDelete(const Query& query, QueryResults& result, const NsContext&); + void doTruncate(const NsContext& ctx); + void doUpsert(ItemImpl* ritem, IdType id, bool doUpdate); + void modifyItem(Item& item, ItemModifyMode mode, const NsContext&); + void doModifyItem(Item& item, ItemModifyMode mode, const NsContext& ctx); + void deleteItem(Item& item, const NsContext& ctx); + void updateTagsMatcherFromItem(ItemImpl* ritem); template - [[nodiscard]] RollBack_updateItems updateItems(const PayloadType &oldPlType, int changedField); - void fillSparseIndex(Index &, std::string_view jsonPath); + [[nodiscard]] RollBack_updateItems updateItems(const PayloadType& oldPlType, int changedField); + void fillSparseIndex(Index&, std::string_view jsonPath); void doDelete(IdType id); - void optimizeIndexes(const NsContext &); - [[nodiscard]] RollBack_insertIndex insertIndex(std::unique_ptr newIndex, int idxNo, const std::string &realName); - bool addIndex(const IndexDef &indexDef); - void addCompositeIndex(const IndexDef &indexDef); + void optimizeIndexes(const NsContext&); + [[nodiscard]] RollBack_insertIndex insertIndex(std::unique_ptr newIndex, int idxNo, const std::string& realName); + bool addIndex(const IndexDef& indexDef); + void addCompositeIndex(const IndexDef& indexDef); template - void createCompositeFieldsSet(const std::string &idxName, const PathsT &paths, FieldsSet &fields); - void verifyCompositeIndex(const IndexDef &indexDef) const; + void createCompositeFieldsSet(const std::string& idxName, const PathsT& paths, FieldsSet& fields); + void verifyCompositeIndex(const IndexDef& indexDef) const; template - void verifyAddIndex(const IndexDef &indexDef, GetNameF &&) const; - void verifyUpdateIndex(const IndexDef &indexDef) const; - void verifyUpdateCompositeIndex(const IndexDef &indexDef) const; - bool updateIndex(const IndexDef &indexDef); - void dropIndex(const IndexDef &index); - void addToWAL(const IndexDef &indexDef, WALRecType type, const RdxContext &ctx); - void addToWAL(std::string_view json, WALRecType type, const RdxContext &ctx); - void replicateItem(IdType itemId, const NsContext &ctx, bool statementReplication, uint64_t oldPlHash, size_t oldItemCapacity, - std::optional &&modifyData); - void removeExpiredItems(RdxActivityContext *); - void removeExpiredStrings(RdxActivityContext *); - void mergeTagsMatcher(const TagsMatcher &tm, const NsContext &ctx); + void verifyAddIndex(const IndexDef& indexDef, GetNameF&&) const; + void verifyUpdateIndex(const IndexDef& indexDef) const; + void verifyUpdateCompositeIndex(const IndexDef& indexDef) const; + bool updateIndex(const IndexDef& indexDef); + void dropIndex(const IndexDef& index); + void addToWAL(const IndexDef& indexDef, WALRecType type, const RdxContext& ctx); + void addToWAL(std::string_view json, WALRecType type, const RdxContext& ctx); + void replicateItem(IdType itemId, const NsContext& ctx, bool statementReplication, uint64_t oldPlHash, size_t oldItemCapacity, + std::optional&& modifyData); + void removeExpiredItems(RdxActivityContext*); + void removeExpiredStrings(RdxActivityContext*); + void mergeTagsMatcher(const TagsMatcher& tm, const NsContext& ctx); Item newItem(); template [[nodiscard]] RollBack_recreateCompositeIndexes recreateCompositeIndexes(size_t startIdx, size_t endIdx); NamespaceDef getDefinition() const; - IndexDef getIndexDefinition(const std::string &indexName) const; + IndexDef getIndexDefinition(const std::string& indexName) const; IndexDef getIndexDefinition(size_t) const; - std::string getMeta(const std::string &key) const; - void putMeta(const std::string &key, std::string_view data, const RdxContext &ctx); - void deleteMeta(const std::string &key, const RdxContext &ctx); + std::string getMeta(const std::string& key) const; + void putMeta(const std::string& key, std::string_view data, const RdxContext& ctx); + void deleteMeta(const std::string& key, const RdxContext& ctx); - std::pair findByPK(ItemImpl *ritem, bool inTransaction, const RdxContext &); + std::pair findByPK(ItemImpl* ritem, bool inTransaction, const RdxContext&); - RX_ALWAYS_INLINE SelectKeyResult getPkDocs(const ConstPayload &cpl, bool inTransaction, const RdxContext &ctx); - RX_ALWAYS_INLINE VariantArray getPkKeys(const ConstPayload &cpl, Index *pkIndex, int fieldNum); - void checkUniquePK(const ConstPayload &cpl, bool inTransaction, const RdxContext &ctx); + RX_ALWAYS_INLINE SelectKeyResult getPkDocs(const ConstPayload& cpl, bool inTransaction, const RdxContext& ctx); + RX_ALWAYS_INLINE VariantArray getPkKeys(const ConstPayload& cpl, Index* pkIndex, int fieldNum); + void checkUniquePK(const ConstPayload& cpl, bool inTransaction, const RdxContext& ctx); int getSortedIdxCount() const noexcept; void updateSortedIdxCount(); - void setFieldsBasedOnPrecepts(ItemImpl *ritem); - - void putToJoinCache(JoinCacheRes &res, std::shared_ptr preResult) const; - void putToJoinCache(JoinCacheRes &res, JoinCacheVal &&val) const; - void getFromJoinCache(const Query &, const JoinedQuery &, JoinCacheRes &out) const; - void getFromJoinCache(const Query &, JoinCacheRes &out) const; - void getFromJoinCacheImpl(JoinCacheRes &out) const; - void getInsideFromJoinCache(JoinCacheRes &ctx) const; + void setFieldsBasedOnPrecepts(ItemImpl* ritem); + + void putToJoinCache(JoinCacheRes& res, std::shared_ptr preResult) const; + void putToJoinCache(JoinCacheRes& res, JoinCacheVal&& val) const; + void getFromJoinCache(const Query&, const JoinedQuery&, JoinCacheRes& out) const; + void getFromJoinCache(const Query&, JoinCacheRes& out) const; + void getFromJoinCacheImpl(JoinCacheRes& out) const; + void getInsideFromJoinCache(JoinCacheRes& ctx) const; int64_t lastUpdateTimeNano() const noexcept { return repl_.updatedUnixNano; } - const FieldsSet &pkFields(); + const FieldsSet& pkFields(); std::vector enumMeta() const; @@ -423,9 +424,9 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor } bool hadSelects() const noexcept { return lastSelectTime_.load(std::memory_order_relaxed) != 0; } void markReadOnly() { locker_.MarkReadOnly(); } - Locker::WLockT wLock(const RdxContext &ctx) const { return locker_.WLock(ctx); } - bool isNotLocked(const RdxContext &ctx) const { return locker_.IsNotLocked(ctx); } - Locker::RLockT rLock(const RdxContext &ctx) const { return locker_.RLock(ctx); } + Locker::WLockT wLock(const RdxContext& ctx) const { return locker_.WLock(ctx); } + bool isNotLocked(const RdxContext& ctx) const { return locker_.IsNotLocked(ctx); } + Locker::RLockT rLock(const RdxContext& ctx) const { return locker_.RLock(ctx); } bool SortOrdersBuilt() const noexcept { return optimizationState_.load(std::memory_order_acquire) == OptimizationCompleted; } @@ -460,33 +461,33 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor StringsHolderPtr strHolder() const noexcept { return strHolder_; } size_t itemsCount() const noexcept { return items_.size() - free_.size(); } - const NamespaceConfigData &config() const noexcept { return config_; } + const NamespaceConfigData& config() const noexcept { return config_; } - void DumpIndex(std::ostream &os, std::string_view index, const RdxContext &ctx) const; + void DumpIndex(std::ostream& os, std::string_view index, const RdxContext& ctx) const; - NamespaceImpl(const NamespaceImpl &src, AsyncStorage::FullLockT &storageLock); + NamespaceImpl(const NamespaceImpl& src, AsyncStorage::FullLockT& storageLock); bool isSystem() const { return isSystemNamespaceNameFast(name_); } IdType createItem(size_t realSize); void checkApplySlaveUpdate(bool v); - void processWalRecord(const WALRecord &wrec, const RdxContext &ctx, lsn_t itemLsn = lsn_t(), Item *item = nullptr); + void processWalRecord(const WALRecord& wrec, const RdxContext& ctx, lsn_t itemLsn = lsn_t(), Item* item = nullptr); void setReplLSNs(LSNPair LSNs); void setTemporary() { repl_.temporary = true; } - void setSlaveMode(const RdxContext &ctx); + void setSlaveMode(const RdxContext& ctx); - void removeIndex(std::unique_ptr &); - void dumpIndex(std::ostream &os, std::string_view index) const; - void tryForceFlush(Locker::WLockT &&wlck) { + void removeIndex(std::unique_ptr&); + void dumpIndex(std::ostream& os, std::string_view index) const; + void tryForceFlush(Locker::WLockT&& wlck) { if (wlck.owns_lock()) { wlck.unlock(); storage_.TryForceFlush(); } } - size_t getWalSize(const NamespaceConfigData &cfg) const noexcept { return isSystem() ? int64_t(1) : std::max(cfg.walSize, int64_t(1)); } + size_t getWalSize(const NamespaceConfigData& cfg) const noexcept { return isSystem() ? int64_t(1) : std::max(cfg.walSize, int64_t(1)); } void clearNamespaceCaches(); - std::vector pickJsonPath(const PayloadFieldType &fld); + std::vector pickJsonPath(const PayloadFieldType& fld); PerfStatCounterMT updatePerfCounter_, selectPerfCounter_; std::atomic_bool enablePerfCounters_{false}; @@ -497,7 +498,7 @@ class NamespaceImpl final : public intrusive_atomic_rc_base { // NOLINT(*perfor // Replication variables WALTracker wal_; ReplicationState repl_; - UpdatesObservers *observers_; + UpdatesObservers* observers_; StorageOpts storageOpts_; std::atomic_int64_t lastSelectTime_{0}; diff --git a/cpp_src/core/namespace/namespacestat.cc b/cpp_src/core/namespace/namespacestat.cc index 2b90620c1..de8a1d2a1 100644 --- a/cpp_src/core/namespace/namespacestat.cc +++ b/cpp_src/core/namespace/namespacestat.cc @@ -9,13 +9,15 @@ namespace reindexer { using namespace std::string_view_literals; -void NamespaceMemStat::GetJSON(WrSerializer &ser) { +void NamespaceMemStat::GetJSON(WrSerializer& ser) { JsonBuilder builder(ser); builder.Put("name", name); builder.Put("items_count", itemsCount); - if (emptyItemsCount) builder.Put("empty_items_count", emptyItemsCount); + if (emptyItemsCount) { + builder.Put("empty_items_count", emptyItemsCount); + } builder.Put("strings_waiting_to_be_deleted_size", stringsWaitingToBeDeletedSize); builder.Put("storage_ok", storageOK); @@ -47,31 +49,53 @@ void NamespaceMemStat::GetJSON(WrSerializer &ser) { } auto arr = builder.Array("indexes"); - for (auto &index : indexes) { + for (auto& index : indexes) { auto obj = arr.Object(); index.GetJSON(obj); } } -void LRUCacheMemStat::GetJSON(JsonBuilder &builder) { +void LRUCacheMemStat::GetJSON(JsonBuilder& builder) { builder.Put("total_size", totalSize); builder.Put("items_count", itemsCount); builder.Put("empty_count", emptyCount); builder.Put("hit_count_limit", hitCountLimit); } -void IndexMemStat::GetJSON(JsonBuilder &builder) { - if (uniqKeysCount) builder.Put("uniq_keys_count", uniqKeysCount); - if (trackedUpdatesCount) builder.Put("tracked_updates_count", trackedUpdatesCount); - if (trackedUpdatesBuckets) builder.Put("tracked_updates_buckets", trackedUpdatesBuckets); - if (trackedUpdatesSize) builder.Put("tracked_updates_size", trackedUpdatesSize); - if (trackedUpdatesOveflow) builder.Put("tracked_updates_overflow", trackedUpdatesOveflow); - if (dataSize) builder.Put("data_size", dataSize); - if (idsetBTreeSize) builder.Put("idset_btree_size", idsetBTreeSize); - if (idsetPlainSize) builder.Put("idset_plain_size", idsetPlainSize); - if (sortOrdersSize) builder.Put("sort_orders_size", sortOrdersSize); - if (fulltextSize) builder.Put("fulltext_size", fulltextSize); - if (columnSize) builder.Put("column_size", columnSize); +void IndexMemStat::GetJSON(JsonBuilder& builder) { + if (uniqKeysCount) { + builder.Put("uniq_keys_count", uniqKeysCount); + } + if (trackedUpdatesCount) { + builder.Put("tracked_updates_count", trackedUpdatesCount); + } + if (trackedUpdatesBuckets) { + builder.Put("tracked_updates_buckets", trackedUpdatesBuckets); + } + if (trackedUpdatesSize) { + builder.Put("tracked_updates_size", trackedUpdatesSize); + } + if (trackedUpdatesOveflow) { + builder.Put("tracked_updates_overflow", trackedUpdatesOveflow); + } + if (dataSize) { + builder.Put("data_size", dataSize); + } + if (idsetBTreeSize) { + builder.Put("idset_btree_size", idsetBTreeSize); + } + if (idsetPlainSize) { + builder.Put("idset_plain_size", idsetPlainSize); + } + if (sortOrdersSize) { + builder.Put("sort_orders_size", sortOrdersSize); + } + if (fulltextSize) { + builder.Put("fulltext_size", fulltextSize); + } + if (columnSize) { + builder.Put("column_size", columnSize); + } if (idsetCache.totalSize || idsetCache.itemsCount || idsetCache.emptyCount || idsetCache.hitCountLimit) { auto obj = builder.Object("idset_cache"); @@ -81,7 +105,7 @@ void IndexMemStat::GetJSON(JsonBuilder &builder) { builder.Put("name", name); } -void PerfStat::GetJSON(JsonBuilder &builder) { +void PerfStat::GetJSON(JsonBuilder& builder) { builder.Put("total_queries_count", totalHitCount); builder.Put("total_avg_latency_us", totalTimeUs); builder.Put("total_avg_lock_time_us", totalLockTimeUs); @@ -93,7 +117,7 @@ void PerfStat::GetJSON(JsonBuilder &builder) { builder.Put("max_latency_us", maxTimeUs); } -void NamespacePerfStat::GetJSON(WrSerializer &ser) { +void NamespacePerfStat::GetJSON(WrSerializer& ser) { JsonBuilder builder(ser); builder.Put("name", name); @@ -118,7 +142,7 @@ void NamespacePerfStat::GetJSON(WrSerializer &ser) { } } -void IndexPerfStat::GetJSON(JsonBuilder &builder) { +void IndexPerfStat::GetJSON(JsonBuilder& builder) { builder.Put("name", name); { auto obj = builder.Object("selects"); @@ -130,7 +154,7 @@ void IndexPerfStat::GetJSON(JsonBuilder &builder) { } } -void MasterState::GetJSON(JsonBuilder &builder) { +void MasterState::GetJSON(JsonBuilder& builder) { { auto lastUpstreamLSNmObj = builder.Object("last_upstream_lsn"); lastUpstreamLSNm.GetJSON(lastUpstreamLSNmObj); @@ -145,27 +169,28 @@ void MasterState::FromJSON(span json) { gason::JsonParser parser; auto root = parser.Parse(json); FromJSON(root); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { throw Error(errParseJson, "MasterState: %s", ex.what()); } } -void LoadLsn(lsn_t &to, const gason::JsonNode &node) { +void LoadLsn(lsn_t& to, const gason::JsonNode& node) { if (!node.empty()) { - if (node.value.getTag() == gason::JSON_OBJECT) + if (node.value.getTag() == gason::JSON_OBJECT) { to.FromJSON(node); - else + } else { to = lsn_t(node.As()); + } } } -void MasterState::FromJSON(const gason::JsonNode &root) { +void MasterState::FromJSON(const gason::JsonNode& root) { try { LoadLsn(lastUpstreamLSNm, root["last_upstream_lsn"]); dataHash = root["data_hash"].As(); dataCount = root["data_count"].As(); updatedUnixNano = root["updated_unix_nano"].As(); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { throw Error(errParseJson, "MasterState: %s", ex.what()); } } @@ -199,7 +224,7 @@ static ReplicationState::Status strToReplicationStatus(std::string_view status) return ReplicationState::Status::None; } -void ReplicationState::GetJSON(JsonBuilder &builder) { +void ReplicationState::GetJSON(JsonBuilder& builder) { builder.Put("last_lsn", int64_t(lastLsn)); { auto lastLsnObj = builder.Object("last_lsn_v2"); @@ -257,18 +282,18 @@ void ReplicationState::FromJSON(span json) { replError = Error(static_cast(errCode), root["error_message"].As()); try { masterState.FromJSON(root["master_state"]); - } catch (const Error &e) { + } catch (const Error& e) { logPrintf(LogError, "[repl] Can't load master state error %s", e.what()); - } catch (const gason::Exception &e) { + } catch (const gason::Exception& e) { logPrintf(LogError, "[repl] Can't load master state gasson error %s", e.what()); } } - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { throw Error(errParseJson, "ReplicationState: %s", ex.what()); } } -void ReplicationStat::GetJSON(JsonBuilder &builder) { +void ReplicationStat::GetJSON(JsonBuilder& builder) { ReplicationState::GetJSON(builder); if (!slaveMode) { builder.Put("wal_count", walCount); @@ -276,7 +301,7 @@ void ReplicationStat::GetJSON(JsonBuilder &builder) { } } -void TxPerfStat::GetJSON(JsonBuilder &builder) { +void TxPerfStat::GetJSON(JsonBuilder& builder) { builder.Put("total_count", totalCount); builder.Put("total_copy_count", totalCopyCount); builder.Put("avg_steps_count", avgStepsCount); diff --git a/cpp_src/core/namespace/namespacestat.h b/cpp_src/core/namespace/namespacestat.h index 9fe6ca7bb..2f3e861e8 100644 --- a/cpp_src/core/namespace/namespacestat.h +++ b/cpp_src/core/namespace/namespacestat.h @@ -16,7 +16,7 @@ class WrSerializer; class JsonBuilder; struct LRUCacheMemStat { - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); size_t totalSize = 0; size_t itemsCount = 0; @@ -25,7 +25,7 @@ struct LRUCacheMemStat { }; struct IndexMemStat { - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); std::string name; size_t uniqKeysCount = 0; size_t dataSize = 0; @@ -45,9 +45,9 @@ struct IndexMemStat { }; struct MasterState { - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); void FromJSON(span); - void FromJSON(const gason::JsonNode &root); + void FromJSON(const gason::JsonNode& root); // LSN of last change lsn_t lastUpstreamLSNm; @@ -62,7 +62,7 @@ struct MasterState { struct ReplicationState { enum class Status { None, Idle, Error, Fatal, Syncing }; - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); void FromJSON(span); // LSN of last change @@ -95,13 +95,13 @@ struct ReplicationState { }; struct ReplicationStat : public ReplicationState { - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); size_t walCount = 0; size_t walSize = 0; }; struct NamespaceMemStat { - void GetJSON(WrSerializer &ser); + void GetJSON(WrSerializer& ser); std::string name; std::string storagePath; @@ -126,7 +126,7 @@ struct NamespaceMemStat { }; struct PerfStat { - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); size_t totalHitCount; size_t totalTimeUs; @@ -140,7 +140,7 @@ struct PerfStat { }; struct TxPerfStat { - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); size_t totalCount; size_t totalCopyCount; @@ -160,9 +160,9 @@ struct TxPerfStat { struct IndexPerfStat { IndexPerfStat() = default; - IndexPerfStat(const std::string &n, const PerfStat &s, const PerfStat &c) : name(n), selects(s), commits(c) {} + IndexPerfStat(const std::string& n, const PerfStat& s, const PerfStat& c) : name(n), selects(s), commits(c) {} - void GetJSON(JsonBuilder &builder); + void GetJSON(JsonBuilder& builder); std::string name; PerfStat selects; @@ -170,7 +170,7 @@ struct IndexPerfStat { }; struct NamespacePerfStat { - void GetJSON(WrSerializer &ser); + void GetJSON(WrSerializer& ser); std::string name; PerfStat updates; diff --git a/cpp_src/core/namespacedef.cc b/cpp_src/core/namespacedef.cc index 0ccd79ea0..e06939579 100644 --- a/cpp_src/core/namespacedef.cc +++ b/cpp_src/core/namespacedef.cc @@ -9,21 +9,21 @@ namespace reindexer { Error NamespaceDef::FromJSON(span json) { try { FromJSON(gason::JsonParser().Parse(json)); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "NamespaceDef: %s", ex.what()); - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; } -void NamespaceDef::FromJSON(const gason::JsonNode &root) { +void NamespaceDef::FromJSON(const gason::JsonNode& root) { name = root["name"].As(); storage.Enabled(root["storage"]["enabled"].As(true)); storage.DropOnFileFormatError(root["storage"]["drop_on_file_format_error"].As()); storage.CreateIfMissing(root["storage"]["create_if_missing"].As(true)); - for (auto &arrelem : root["indexes"]) { + for (auto& arrelem : root["indexes"]) { IndexDef idx; idx.FromJSON(arrelem); indexes.push_back(idx); @@ -32,13 +32,13 @@ void NamespaceDef::FromJSON(const gason::JsonNode &root) { schemaJson = root["schema"].As(schemaJson); } -void NamespaceDef::GetJSON(WrSerializer &ser, int formatFlags) const { +void NamespaceDef::GetJSON(WrSerializer& ser, int formatFlags) const { JsonBuilder json(ser); json.Put("name", name); json.Object("storage").Put("enabled", storage.IsEnabled()); { auto arr = json.Array("indexes"); - for (auto &idx : indexes) { + for (auto& idx : indexes) { arr.Raw(nullptr, ""); idx.GetJSON(ser, formatFlags); } diff --git a/cpp_src/core/namespacedef.h b/cpp_src/core/namespacedef.h index db65c1549..05f12ecd5 100644 --- a/cpp_src/core/namespacedef.h +++ b/cpp_src/core/namespacedef.h @@ -14,29 +14,29 @@ class WrSerializer; struct NamespaceDef { NamespaceDef() = default; - NamespaceDef(const std::string &iname, StorageOpts istorage = StorageOpts().Enabled().CreateIfMissing()) + NamespaceDef(const std::string& iname, StorageOpts istorage = StorageOpts().Enabled().CreateIfMissing()) : name(iname), storage(istorage) {} - NamespaceDef &AddIndex(const std::string &iname, const std::string &indexType, const std::string &fieldType, + NamespaceDef& AddIndex(const std::string& iname, const std::string& indexType, const std::string& fieldType, IndexOpts opts = IndexOpts()) { indexes.emplace_back(iname, JsonPaths{iname}, indexType, fieldType, std::move(opts)); return *this; } - NamespaceDef &AddIndex(const std::string &iname, const JsonPaths &jsonPaths, const std::string &indexType, const std::string &fieldType, + NamespaceDef& AddIndex(const std::string& iname, const JsonPaths& jsonPaths, const std::string& indexType, const std::string& fieldType, IndexOpts opts = IndexOpts()) { indexes.emplace_back(iname, jsonPaths, indexType, fieldType, std::move(opts)); return *this; } - NamespaceDef &AddIndex(const IndexDef &idxDef) { + NamespaceDef& AddIndex(const IndexDef& idxDef) { indexes.emplace_back(idxDef); return *this; } Error FromJSON(span json); - void FromJSON(const gason::JsonNode &root); - void GetJSON(WrSerializer &, int formatFlags = 0) const; + void FromJSON(const gason::JsonNode& root); + void GetJSON(WrSerializer&, int formatFlags = 0) const; public: std::string name; @@ -63,24 +63,24 @@ struct EnumNamespacesOpts { } // Add not openened namespaces to enumeration - EnumNamespacesOpts &WithClosed(bool value = true) { + EnumNamespacesOpts& WithClosed(bool value = true) { options_ = value ? options_ | kEnumNamespacesWithClosed : options_ & ~(kEnumNamespacesWithClosed); return *this; } // Return only namespaces names (faster, and do not try rlock nses) - EnumNamespacesOpts &OnlyNames(bool value = true) { + EnumNamespacesOpts& OnlyNames(bool value = true) { options_ = value ? options_ | kEnumNamespacesOnlyNames : options_ & ~(kEnumNamespacesOnlyNames); return *this; } // Hide system namespaces from enumeration - EnumNamespacesOpts &HideSystem(bool value = true) { + EnumNamespacesOpts& HideSystem(bool value = true) { options_ = value ? options_ | kEnumNamespacesHideSystem : options_ & ~(kEnumNamespacesHideSystem); return *this; } // Add name filter - EnumNamespacesOpts &WithFilter(std::string_view flt) { + EnumNamespacesOpts& WithFilter(std::string_view flt) { filter_ = flt; return *this; } diff --git a/cpp_src/core/nsselecter/aggregator.cc b/cpp_src/core/nsselecter/aggregator.cc index a336862b9..3a02a6d4c 100644 --- a/cpp_src/core/nsselecter/aggregator.cc +++ b/cpp_src/core/nsselecter/aggregator.cc @@ -19,7 +19,7 @@ namespace { namespace reindexer { template -static void moveFrames(It &begin, It &end, size_t size, size_t offset, size_t limit) { +static void moveFrames(It& begin, It& end, size_t size, size_t offset, size_t limit) { if (offset > QueryEntry::kDefaultOffset) { std::advance(begin, offset); } @@ -30,7 +30,7 @@ static void moveFrames(It &begin, It &end, size_t size, size_t offset, size_t li } template -static void copy(It begin, It end, std::vector &facets, const FieldsSet &fields, const PayloadType &payloadType) { +static void copy(It begin, It end, std::vector& facets, const FieldsSet& fields, const PayloadType& payloadType) { for (; begin != end; ++begin) { facets.push_back({{}, begin->second}); int tagPathIdx = 0; @@ -39,10 +39,10 @@ static void copy(It begin, It end, std::vector &facets, const Field VariantArray va; if (fields[i] == IndexValueType::SetByJsonPath) { if (fields.isTagsPathIndexed(tagPathIdx)) { - const IndexedTagsPath &tagsPath = fields.getIndexedTagsPath(tagPathIdx++); + const IndexedTagsPath& tagsPath = fields.getIndexedTagsPath(tagPathIdx++); pl.GetByJsonPath(tagsPath, va, KeyValueType::Undefined{}); } else { - const TagsPath &tagsPath = fields.getTagsPath(tagPathIdx++); + const TagsPath& tagsPath = fields.getTagsPath(tagPathIdx++); pl.GetByJsonPath(tagsPath, va, KeyValueType::Undefined{}); } if (va.IsObjectValue()) { @@ -57,7 +57,7 @@ static void copy(It begin, It end, std::vector &facets, const Field } template -static void copy(It begin, It end, std::vector &facets) { +static void copy(It begin, It end, std::vector& facets) { for (; begin != end; ++begin) { facets.push_back({{begin->first.template As()}, begin->second}); } @@ -65,15 +65,15 @@ static void copy(It begin, It end, std::vector &facets) { class Aggregator::MultifieldComparator { public: - MultifieldComparator(const h_vector &, const FieldsSet &, const PayloadType &); + MultifieldComparator(const h_vector&, const FieldsSet&, const PayloadType&); bool HaveCompareByCount() const { return haveCompareByCount; } - bool operator()(const PayloadValue &lhs, const PayloadValue &rhs) const; - bool operator()(const std::pair &lhs, const std::pair &rhs) const; + bool operator()(const PayloadValue& lhs, const PayloadValue& rhs) const; + bool operator()(const std::pair& lhs, const std::pair& rhs) const; private: struct CompOpts { CompOpts() : direction{Asc} {} - CompOpts(const FieldsSet &fs, Direction d) : fields{fs}, direction{d} {} + CompOpts(const FieldsSet& fs, Direction d) : fields{fs}, direction{d} {} FieldsSet fields; // if empty - compare by count Direction direction = Asc; }; @@ -81,19 +81,19 @@ class Aggregator::MultifieldComparator { PayloadType type_; bool haveCompareByCount = false; - void insertField(size_t toIdx, const FieldsSet &from, size_t fromIdx, int &tagsPathIdx); + void insertField(size_t toIdx, const FieldsSet& from, size_t fromIdx, int& tagsPathIdx); }; class Aggregator::SinglefieldComparator { enum CompareBy { ByValue, ByCount }; public: - SinglefieldComparator(const h_vector &); + SinglefieldComparator(const h_vector&); bool HaveCompareByCount() const { return haveCompareByCount; } - bool operator()(const Variant &lhs, const Variant &rhs) const { + bool operator()(const Variant& lhs, const Variant& rhs) const { return toSigned(lhs.Compare(rhs)) * valueCompareDirection_ < 0; } - bool operator()(const std::pair &lhs, const std::pair &rhs) const; + bool operator()(const std::pair& lhs, const std::pair& rhs) const; private: struct CompOpts { @@ -107,8 +107,8 @@ class Aggregator::SinglefieldComparator { bool haveCompareByCount = false; }; -Aggregator::MultifieldComparator::MultifieldComparator(const h_vector &sortingEntries, const FieldsSet &fields, - const PayloadType &type) +Aggregator::MultifieldComparator::MultifieldComparator(const h_vector& sortingEntries, const FieldsSet& fields, + const PayloadType& type) : compOpts_{}, type_{type}, haveCompareByCount{false} { assertrx_throw(type_); if (sortingEntries.empty()) { @@ -135,7 +135,7 @@ Aggregator::MultifieldComparator::MultifieldComparator(const h_vector(rhs, opt.fields); - if (less == ComparationResult::Eq) continue; + if (less == ComparationResult::Eq) { + continue; + } return toSigned(less) * opt.direction < 0; } return false; } -bool Aggregator::MultifieldComparator::operator()(const std::pair &lhs, const std::pair &rhs) const { +bool Aggregator::MultifieldComparator::operator()(const std::pair& lhs, const std::pair& rhs) const { assertrx_throw(!lhs.first.IsFree()); assertrx_throw(!rhs.first.IsFree()); - for (const auto &opt : compOpts_) { + for (const auto& opt : compOpts_) { if (opt.fields.empty()) { - if (lhs.second == rhs.second) continue; + if (lhs.second == rhs.second) { + continue; + } return opt.direction * (lhs.second - rhs.second) < 0; } const auto less = ConstPayload(type_, lhs.first).Compare(rhs.first, opt.fields); - if (less == ComparationResult::Eq) continue; + if (less == ComparationResult::Eq) { + continue; + } return toSigned(less) * opt.direction < 0; } return false; } -void Aggregator::MultifieldComparator::insertField(size_t toIdx, const FieldsSet &from, size_t fromIdx, int &tagsPathIdx) { +void Aggregator::MultifieldComparator::insertField(size_t toIdx, const FieldsSet& from, size_t fromIdx, int& tagsPathIdx) { compOpts_[toIdx].fields.push_back(from[fromIdx]); if (from[fromIdx] == IndexValueType::SetByJsonPath) { compOpts_[toIdx].fields.push_back(from.getTagsPath(tagsPathIdx++)); @@ -183,10 +191,10 @@ struct Aggregator::MultifieldOrderedMap : public btree::btree_map &sortingEntries) +Aggregator::SinglefieldComparator::SinglefieldComparator(const h_vector& sortingEntries) : valueCompareDirection_(Asc), haveCompareByCount(false) { bool haveCompareByValue = false; - for (const SortingEntry &sortEntry : sortingEntries) { + for (const SortingEntry& sortEntry : sortingEntries) { CompareBy compareBy = ByValue; Direction direc = sortEntry.desc ? Desc : Asc; if (sortEntry.field == SortingEntry::Count) { @@ -203,24 +211,26 @@ Aggregator::SinglefieldComparator::SinglefieldComparator(const h_vector &lhs, const std::pair &rhs) const { - for (const CompOpts &opt : compOpts_) { +bool Aggregator::SinglefieldComparator::operator()(const std::pair& lhs, const std::pair& rhs) const { + for (const CompOpts& opt : compOpts_) { int less; if (opt.compareBy == ByValue) { less = toSigned(lhs.first.Compare(rhs.first)); } else { less = lhs.second - rhs.second; } - if (less != 0) return less * opt.direction < 0; + if (less != 0) { + return less * opt.direction < 0; + } } return false; } -Aggregator::Aggregator(Aggregator &&) noexcept = default; +Aggregator::Aggregator(Aggregator&&) noexcept = default; Aggregator::~Aggregator() = default; -Aggregator::Aggregator(const PayloadType &payloadType, const FieldsSet &fields, AggType aggType, const h_vector &names, - const h_vector &sort, size_t limit, size_t offset, bool compositeIndexFields) +Aggregator::Aggregator(const PayloadType& payloadType, const FieldsSet& fields, AggType aggType, const h_vector& names, + const h_vector& sort, size_t limit, size_t offset, bool compositeIndexFields) : payloadType_(payloadType), fields_(fields), aggType_(aggType), @@ -261,11 +271,13 @@ Aggregator::Aggregator(const PayloadType &payloadType, const FieldsSet &fields, } template -static void fillOrderedFacetResult(std::vector &result, const FacetMap &facets, size_t offset, size_t limit, - const Args &...args) { - if (offset >= static_cast(facets.size())) return; +static void fillOrderedFacetResult(std::vector& result, const FacetMap& facets, size_t offset, size_t limit, + const Args&... args) { + if (offset >= static_cast(facets.size())) { + return; + } result.reserve(std::min(limit, facets.size() - offset)); - const auto &comparator = facets.key_comp(); + const auto& comparator = facets.key_comp(); if (comparator.HaveCompareByCount()) { std::vector> tmpFacets(facets.begin(), facets.end()); auto begin = tmpFacets.begin(); @@ -283,9 +295,11 @@ static void fillOrderedFacetResult(std::vector &result, const Facet } template -static void fillUnorderedFacetResult(std::vector &result, const FacetMap &facets, size_t offset, size_t limit, - const Args &...args) { - if (offset >= static_cast(facets.size())) return; +static void fillUnorderedFacetResult(std::vector& result, const FacetMap& facets, size_t offset, size_t limit, + const Args&... args) { + if (offset >= static_cast(facets.size())) { + return; + } result.reserve(std::min(limit, facets.size() - offset)); auto begin = facets.begin(); auto end = facets.end(); @@ -300,20 +314,24 @@ AggregationResult Aggregator::GetResult() const { switch (aggType_) { case AggAvg: - if (result_) ret.SetValue(double(hitCount_ == 0 ? 0 : (*result_ / hitCount_))); + if (result_) { + ret.SetValue(double(hitCount_ == 0 ? 0 : (*result_ / hitCount_))); + } break; case AggSum: case AggMin: case AggMax: - if (result_) ret.SetValue(*result_); + if (result_) { + ret.SetValue(*result_); + } break; case AggFacet: - std::visit(overloaded{[&](const SinglefieldOrderedMap &fm) { fillOrderedFacetResult(ret.facets, fm, offset_, limit_); }, - [&](const SinglefieldUnorderedMap &fm) { fillUnorderedFacetResult(ret.facets, fm, offset_, limit_); }, - [&](const MultifieldOrderedMap &fm) { + std::visit(overloaded{[&](const SinglefieldOrderedMap& fm) { fillOrderedFacetResult(ret.facets, fm, offset_, limit_); }, + [&](const SinglefieldUnorderedMap& fm) { fillUnorderedFacetResult(ret.facets, fm, offset_, limit_); }, + [&](const MultifieldOrderedMap& fm) { fillOrderedFacetResult(ret.facets, fm, offset_, limit_, fields_, payloadType_); }, - [&](const MultifieldUnorderedMap &fm) { + [&](const MultifieldUnorderedMap& fm) { fillUnorderedFacetResult(ret.facets, fm, offset_, limit_, fields_, payloadType_); }}, *facets_); @@ -323,7 +341,7 @@ AggregationResult Aggregator::GetResult() const { ret.payloadType = payloadType_; ret.distinctsFields = fields_; ret.distincts.reserve(distincts_->size()); - for (const Variant &value : *distincts_) { + for (const Variant& value : *distincts_) { ret.distincts.push_back(value); } break; @@ -335,20 +353,22 @@ AggregationResult Aggregator::GetResult() const { return ret; } -void Aggregator::Aggregate(const PayloadValue &data) { +void Aggregator::Aggregate(const PayloadValue& data) { if (aggType_ == AggFacet) { const bool done = - std::visit(overloaded{[&data](MultifieldUnorderedMap &fm) { + std::visit(overloaded{[&data](MultifieldUnorderedMap& fm) { ++fm[data]; return true; }, - [&data](MultifieldOrderedMap &fm) { + [&data](MultifieldOrderedMap& fm) { ++fm[data]; return true; }, - [](SinglefieldOrderedMap &) { return false; }, [](SinglefieldUnorderedMap &) { return false; }}, + [](SinglefieldOrderedMap&) { return false; }, [](SinglefieldUnorderedMap&) { return false; }}, *facets_); - if (done) return; + if (done) { + return; + } } if (aggType_ == AggDistinct && compositeIndexFields_) { aggregate(Variant(data)); @@ -359,28 +379,30 @@ void Aggregator::Aggregate(const PayloadValue &data) { if (fields_[0] == IndexValueType::SetByJsonPath) { ConstPayload pl(payloadType_, data); VariantArray va; - const TagsPath &tagsPath = fields_.getTagsPath(0); + const TagsPath& tagsPath = fields_.getTagsPath(0); pl.GetByJsonPath(tagsPath, va, KeyValueType::Undefined{}); if (va.IsObjectValue()) { throw Error(errQueryExec, "Cannot aggregate object field"); } - for (const Variant &v : va) aggregate(v); + for (const Variant& v : va) { + aggregate(v); + } return; } - const auto &fieldType = payloadType_.Field(fields_[0]); + const auto& fieldType = payloadType_.Field(fields_[0]); if (!fieldType.IsArray()) { aggregate(PayloadFieldValue(fieldType, data.Ptr() + fieldType.Offset()).Get()); } else { - PayloadFieldValue::Array *arr = reinterpret_cast(data.Ptr() + fieldType.Offset()); - uint8_t *ptr = data.Ptr() + arr->offset; + PayloadFieldValue::Array* arr = reinterpret_cast(data.Ptr() + fieldType.Offset()); + uint8_t* ptr = data.Ptr() + arr->offset; for (int i = 0; i < arr->len; i++, ptr += fieldType.ElemSizeof()) { aggregate(PayloadFieldValue(fieldType, ptr).Get()); } } } -void Aggregator::aggregate(const Variant &v) { +void Aggregator::aggregate(const Variant& v) { switch (aggType_) { case AggSum: case AggAvg: @@ -394,8 +416,8 @@ void Aggregator::aggregate(const Variant &v) { result_ = result_ ? std::max(v.As(), *result_) : v.As(); break; case AggFacet: - std::visit(overloaded{[&v](SinglefieldUnorderedMap &fm) { ++fm[v]; }, [&v](SinglefieldOrderedMap &fm) { ++fm[v]; }, - [](MultifieldUnorderedMap &) { throw_as_assert; }, [](MultifieldOrderedMap &) { throw_as_assert; }}, + std::visit(overloaded{[&v](SinglefieldUnorderedMap& fm) { ++fm[v]; }, [&v](SinglefieldOrderedMap& fm) { ++fm[v]; }, + [](MultifieldUnorderedMap&) { throw_as_assert; }, [](MultifieldOrderedMap&) { throw_as_assert; }}, *facets_); break; case AggDistinct: diff --git a/cpp_src/core/nsselecter/aggregator.h b/cpp_src/core/nsselecter/aggregator.h index 24e1d6ce3..f4df61b4a 100644 --- a/cpp_src/core/nsselecter/aggregator.h +++ b/cpp_src/core/nsselecter/aggregator.h @@ -19,21 +19,21 @@ class Aggregator { enum { Count = -1 }; }; - Aggregator(const PayloadType &, const FieldsSet &, AggType aggType, const h_vector &names, - const h_vector &sort = {}, size_t limit = QueryEntry::kDefaultLimit, + Aggregator(const PayloadType&, const FieldsSet&, AggType aggType, const h_vector& names, + const h_vector& sort = {}, size_t limit = QueryEntry::kDefaultLimit, size_t offset = QueryEntry::kDefaultOffset, bool compositeIndexFields = false); - Aggregator(Aggregator &&) noexcept; + Aggregator(Aggregator&&) noexcept; ~Aggregator(); - void Aggregate(const PayloadValue &lhs); + void Aggregate(const PayloadValue& lhs); AggregationResult GetResult() const; - Aggregator(const Aggregator &) = delete; - Aggregator &operator=(const Aggregator &) = delete; - Aggregator &operator=(Aggregator &&) = delete; + Aggregator(const Aggregator&) = delete; + Aggregator& operator=(const Aggregator&) = delete; + Aggregator& operator=(Aggregator&&) = delete; AggType Type() const noexcept { return aggType_; } - const h_vector &Names() const noexcept { return names_; } + const h_vector& Names() const noexcept { return names_; } bool DistinctChanged() noexcept { return distinctChecker_(); } @@ -47,7 +47,7 @@ class Aggregator { using SinglefieldUnorderedMap = fast_hash_map; using Facets = std::variant; - void aggregate(const Variant &variant); + void aggregate(const Variant& variant); PayloadType payloadType_; FieldsSet fields_; @@ -62,14 +62,16 @@ class Aggregator { class RelaxVariantCompare { public: - RelaxVariantCompare(const PayloadType &type, const FieldsSet &fields) : type_(type), fields_(fields) {} - bool operator()(const Variant &v1, const Variant &v2) const { - if (!v1.Type().IsSame(v2.Type())) return false; + RelaxVariantCompare(const PayloadType& type, const FieldsSet& fields) : type_(type), fields_(fields) {} + bool operator()(const Variant& v1, const Variant& v2) const { + if (!v1.Type().IsSame(v2.Type())) { + return false; + } return v1.Type().EvaluateOneOf( [&](OneOf) { return v1.Compare(v2) == ComparationResult::Eq; }, [&](KeyValueType::Composite) { - return ConstPayload(type_, static_cast(v1)).IsEQ(static_cast(v2), fields_); + return ConstPayload(type_, static_cast(v1)).IsEQ(static_cast(v2), fields_); }, [](OneOf) -> bool { throw_as_assert; }); } @@ -79,12 +81,12 @@ class Aggregator { FieldsSet fields_; }; struct DistinctHasher { - DistinctHasher(const PayloadType &type, const FieldsSet &fields) : type_(type), fields_(fields) {} - size_t operator()(const Variant &v) const { + DistinctHasher(const PayloadType& type, const FieldsSet& fields) : type_(type), fields_(fields) {} + size_t operator()(const Variant& v) const { return v.Type().EvaluateOneOf( [&](OneOf) noexcept { return v.Hash(); }, - [&](KeyValueType::Composite) { return ConstPayload(type_, static_cast(v)).GetHash(fields_); }, + [&](KeyValueType::Composite) { return ConstPayload(type_, static_cast(v)).GetHash(fields_); }, [](OneOf) -> size_t { throw_as_assert; }); } @@ -95,7 +97,7 @@ class Aggregator { class DistinctChangeChecker { public: - DistinctChangeChecker(const Aggregator &aggregator) noexcept : aggregator_(aggregator) {} + DistinctChangeChecker(const Aggregator& aggregator) noexcept : aggregator_(aggregator) {} [[nodiscard]] bool operator()() noexcept { assertrx_dbg(aggregator_.Type() == AggType::AggDistinct); assertrx_dbg(aggregator_.distincts_); @@ -105,7 +107,7 @@ class Aggregator { } private: - const Aggregator &aggregator_; + const Aggregator& aggregator_; size_t lastCheckSize_ = 0; } distinctChecker_; diff --git a/cpp_src/core/nsselecter/btreeindexiterator.h b/cpp_src/core/nsselecter/btreeindexiterator.h index c31574eb7..6365f922a 100644 --- a/cpp_src/core/nsselecter/btreeindexiterator.h +++ b/cpp_src/core/nsselecter/btreeindexiterator.h @@ -21,7 +21,9 @@ class BtreeIndexIterator final : public IndexIterator { } else { impl_ = std::make_shared>(idxMap_, first_, last_); } - if (impl_->getSize() == 0) return; + if (impl_->getSize() == 0) { + return; + } impl_->shiftToBegin(); impl_->shiftIdsetToBegin(); } @@ -51,7 +53,9 @@ class BtreeIndexIterator final : public IndexIterator { return impl_->getValue(); } size_t GetMaxIterations(size_t limitIters) noexcept final { - if (cachedIters_ != std::numeric_limits::max()) return cachedIters_; + if (cachedIters_ != std::numeric_limits::max()) { + return cachedIters_; + } return BtreeIndexForwardIteratorImpl(idxMap_, first_, last_).getMaxIterations(limitIters); } void SetMaxIterations(size_t iters) noexcept final { cachedIters_ = iters; } diff --git a/cpp_src/core/nsselecter/btreeindexiteratorimpl.h b/cpp_src/core/nsselecter/btreeindexiteratorimpl.h index 26c2696c1..c1e340911 100644 --- a/cpp_src/core/nsselecter/btreeindexiteratorimpl.h +++ b/cpp_src/core/nsselecter/btreeindexiteratorimpl.h @@ -20,11 +20,15 @@ class BtreeIndexIteratorImpl { virtual void next() noexcept = 0; bool shiftToNextIdset() noexcept { - if (isOver()) return false; + if (isOver()) { + return false; + } for (next(); !isOver() && getCurrentIdsetSize() == 0;) { next(); } - if (isOver()) return false; + if (isOver()) { + return false; + } shiftIdsetToBegin(); updateCurrentValue(); return true; diff --git a/cpp_src/core/nsselecter/comparator/comparator_indexed.h b/cpp_src/core/nsselecter/comparator/comparator_indexed.h index 86979e73c..6f3e4e5b4 100644 --- a/cpp_src/core/nsselecter/comparator/comparator_indexed.h +++ b/cpp_src/core/nsselecter/comparator/comparator_indexed.h @@ -485,33 +485,49 @@ class ComparatorIndexedOffsetArray : private DataHolder { for (const auto* const end = ptr + arr.len; ptr != end; ++ptr) { switch (this->cond_) { case CondEq: - if (*ptr == this->value_) return true; + if (*ptr == this->value_) { + return true; + } continue; case CondLt: - if (*ptr < this->value_) return true; + if (*ptr < this->value_) { + return true; + } continue; case CondLe: - if (*ptr <= this->value_) return true; + if (*ptr <= this->value_) { + return true; + } continue; case CondGt: - if (*ptr > this->value_) return true; + if (*ptr > this->value_) { + return true; + } continue; case CondGe: - if (*ptr >= this->value_) return true; + if (*ptr >= this->value_) { + return true; + } continue; case CondRange: - if (this->range_.first <= *ptr && *ptr <= this->range_.second) return true; + if (this->range_.first <= *ptr && *ptr <= this->range_.second) { + return true; + } continue; case CondSet: assertrx_dbg(this->setPtr_); - if (this->setPtr_->find(*ptr) != this->setPtr_->cend()) return true; + if (this->setPtr_->find(*ptr) != this->setPtr_->cend()) { + return true; + } continue; case CondAllSet: { assertrx_dbg(this->allSetPtr_); const auto it = this->allSetPtr_->values_.find(*ptr); if (it != this->allSetPtr_->values_.cend()) { this->allSetPtr_->allSetValues_.insert(it->second); - if (this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) return true; + if (this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) { + return true; + } } } continue; @@ -548,26 +564,40 @@ class ComparatorIndexedOffsetArrayDistinct : private DataHolder { for (const auto* const end = ptr + arr.len; ptr != end; ++ptr) { switch (this->cond_) { case CondEq: - if (*ptr == this->value_ && distinct_.Compare(*ptr)) return true; + if (*ptr == this->value_ && distinct_.Compare(*ptr)) { + return true; + } continue; case CondLt: - if (*ptr < this->value_ && distinct_.Compare(*ptr)) return true; + if (*ptr < this->value_ && distinct_.Compare(*ptr)) { + return true; + } continue; case CondLe: - if (*ptr <= this->value_ && distinct_.Compare(*ptr)) return true; + if (*ptr <= this->value_ && distinct_.Compare(*ptr)) { + return true; + } continue; case CondGt: - if (*ptr > this->value_ && distinct_.Compare(*ptr)) return true; + if (*ptr > this->value_ && distinct_.Compare(*ptr)) { + return true; + } continue; case CondGe: - if (*ptr >= this->value_ && distinct_.Compare(*ptr)) return true; + if (*ptr >= this->value_ && distinct_.Compare(*ptr)) { + return true; + } continue; case CondRange: - if (this->range_.first <= *ptr && *ptr <= this->range_.second && distinct_.Compare(*ptr)) return true; + if (this->range_.first <= *ptr && *ptr <= this->range_.second && distinct_.Compare(*ptr)) { + return true; + } continue; case CondSet: assertrx_dbg(this->setPtr_); - if (this->setPtr_->find(*ptr) != this->setPtr_->cend() && distinct_.Compare(*ptr)) return true; + if (this->setPtr_->find(*ptr) != this->setPtr_->cend() && distinct_.Compare(*ptr)) { + return true; + } continue; case CondAllSet: { assertrx_dbg(this->allSetPtr_); @@ -576,7 +606,9 @@ class ComparatorIndexedOffsetArrayDistinct : private DataHolder { if (it != this->allSetPtr_->values_.cend()) { haveDistinct |= distinct_.Compare(*ptr); this->allSetPtr_->allSetValues_.insert(it->second); - if (haveDistinct && this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) return true; + if (haveDistinct && this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) { + return true; + } } } continue; @@ -621,38 +653,56 @@ class ComparatorIndexedJsonPath : private DataHolder { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::From()); for (Variant& value : buffer_) { - if rx_unlikely (value.IsNullValue()) continue; + if rx_unlikely (value.IsNullValue()) { + continue; + } switch (this->cond_) { case CondEq: - if (value.As() == this->value_) return true; + if (value.As() == this->value_) { + return true; + } continue; case CondLt: - if (value.As() < this->value_) return true; + if (value.As() < this->value_) { + return true; + } continue; case CondLe: - if (value.As() <= this->value_) return true; + if (value.As() <= this->value_) { + return true; + } continue; case CondGt: - if (value.As() > this->value_) return true; + if (value.As() > this->value_) { + return true; + } continue; case CondGe: - if (value.As() >= this->value_) return true; + if (value.As() >= this->value_) { + return true; + } continue; case CondRange: { const auto v = value.As(); - if (this->range_.first <= v && v <= this->range_.second) return true; + if (this->range_.first <= v && v <= this->range_.second) { + return true; + } } continue; case CondSet: assertrx_dbg(this->setPtr_); - if (this->setPtr_->find(value.As()) != this->setPtr_->cend()) return true; + if (this->setPtr_->find(value.As()) != this->setPtr_->cend()) { + return true; + } continue; case CondAllSet: { assertrx_dbg(this->allSetPtr_); const auto it = this->allSetPtr_->values_.find(value.As()); if (it != this->allSetPtr_->values_.cend()) { this->allSetPtr_->allSetValues_.insert(it->second); - if (this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) return true; + if (this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) { + return true; + } } } continue; @@ -692,32 +742,48 @@ class ComparatorIndexedJsonPathDistinct : private DataHolder { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::From()); for (Variant& v : buffer_) { - if rx_unlikely (v.IsNullValue()) continue; + if rx_unlikely (v.IsNullValue()) { + continue; + } const auto value = v.As(); switch (this->cond_) { case CondEq: - if (value == this->value_ && distinct_.Compare(value)) return true; + if (value == this->value_ && distinct_.Compare(value)) { + return true; + } continue; case CondLt: - if (value < this->value_ && distinct_.Compare(value)) return true; + if (value < this->value_ && distinct_.Compare(value)) { + return true; + } continue; case CondLe: - if (value <= this->value_ && distinct_.Compare(value)) return true; + if (value <= this->value_ && distinct_.Compare(value)) { + return true; + } continue; case CondGt: - if (value > this->value_ && distinct_.Compare(value)) return true; + if (value > this->value_ && distinct_.Compare(value)) { + return true; + } continue; case CondGe: - if (value >= this->value_ && distinct_.Compare(value)) return true; + if (value >= this->value_ && distinct_.Compare(value)) { + return true; + } continue; case CondRange: { const auto v = value; - if (this->range_.first <= v && v <= this->range_.second && distinct_.Compare(value)) return true; + if (this->range_.first <= v && v <= this->range_.second && distinct_.Compare(value)) { + return true; + } } continue; case CondSet: assertrx_dbg(this->setPtr_); - if (this->setPtr_->find(value) != this->setPtr_->cend() && distinct_.Compare(value)) return true; + if (this->setPtr_->find(value) != this->setPtr_->cend() && distinct_.Compare(value)) { + return true; + } continue; case CondAllSet: { assertrx_dbg(this->allSetPtr_); @@ -726,7 +792,9 @@ class ComparatorIndexedJsonPathDistinct : private DataHolder { if (it != this->allSetPtr_->values_.cend()) { haveDistinct |= distinct_.Compare(value); this->allSetPtr_->allSetValues_.insert(it->second); - if (haveDistinct && this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) return true; + if (haveDistinct && this->allSetPtr_->allSetValues_.size() == this->allSetPtr_->values_.size()) { + return true; + } } } continue; @@ -746,7 +814,9 @@ class ComparatorIndexedJsonPathDistinct : private DataHolder { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::From()); for (Variant& v : buffer_) { - if rx_unlikely (v.IsNullValue()) continue; + if rx_unlikely (v.IsNullValue()) { + continue; + } distinct_.ExcludeValues(v.As()); } } @@ -958,7 +1028,9 @@ class ComparatorIndexedOffsetArrayString : private DataHolder { const std::string_view value = *ptr; switch (cond_) { case CondSet: - if (setPtr_->find(value) != setPtr_->cend()) return true; + if (setPtr_->find(value) != setPtr_->cend()) { + return true; + } continue; case CondRange: if ((collateCompare(value, range_.value1_.valueView_, *collateOpts_) & ComparationResult::Ge) && @@ -971,27 +1043,41 @@ class ComparatorIndexedOffsetArrayString : private DataHolder { const auto it = allSetPtr_->values_.find(value); if (it != allSetPtr_->values_.cend()) { allSetPtr_->allSetValues_.insert(it->second); - if (allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) return true; + if (allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) { + return true; + } } } continue; case CondLike: - if (matchLikePattern(value, value_.valueView_)) return true; + if (matchLikePattern(value, value_.valueView_)) { + return true; + } continue; case CondEq: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq) { + return true; + } continue; case CondLt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt) { + return true; + } continue; case CondLe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le) { + return true; + } continue; case CondGt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt) { + return true; + } continue; case CondGe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge) { + return true; + } continue; case CondAny: case CondEmpty: @@ -1027,7 +1113,9 @@ class ComparatorIndexedOffsetArrayStringDistinct : private DataHoldersetPtr_); - if (setPtr_->find(value) != setPtr_->cend() && distinct_.Compare(value)) return true; + if (setPtr_->find(value) != setPtr_->cend() && distinct_.Compare(value)) { + return true; + } continue; case CondRange: if ((collateCompare(value, range_.value1_.valueView_, *collateOpts_) & ComparationResult::Ge) && @@ -1043,32 +1131,41 @@ class ComparatorIndexedOffsetArrayStringDistinct : private DataHoldervalues_.cend()) { haveDistinct |= distinct_.Compare(value); allSetPtr_->allSetValues_.insert(it->second); - if (haveDistinct && allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) return true; + if (haveDistinct && allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) { + return true; + } } } continue; case CondLike: - if (matchLikePattern(value, value_.valueView_) && distinct_.Compare(value)) return true; + if (matchLikePattern(value, value_.valueView_) && distinct_.Compare(value)) { + return true; + } continue; case CondEq: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq && distinct_.Compare(value)) { return true; + } continue; case CondLt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt && distinct_.Compare(value)) { return true; + } continue; case CondLe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le && distinct_.Compare(value)) { return true; + } continue; case CondGt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt && distinct_.Compare(value)) { return true; + } continue; case CondGe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge && distinct_.Compare(value)) { return true; + } continue; case CondAny: case CondEmpty: @@ -1113,7 +1210,9 @@ class ComparatorIndexedJsonPathString : private DataHolder { switch (cond_) { case CondSet: assertrx_dbg(this->setPtr_); - if (setPtr_->find(value) != setPtr_->cend()) return true; + if (setPtr_->find(value) != setPtr_->cend()) { + return true; + } break; case CondRange: if ((collateCompare(value, range_.value1_.valueView_, *collateOpts_) & ComparationResult::Ge) && @@ -1126,26 +1225,40 @@ class ComparatorIndexedJsonPathString : private DataHolder { const auto it = allSetPtr_->values_.find(value); if (it != allSetPtr_->values_.cend()) { allSetPtr_->allSetValues_.insert(it->second); - if (allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) return true; + if (allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) { + return true; + } } } break; case CondLike: - if (matchLikePattern(value, value_.valueView_)) return true; + if (matchLikePattern(value, value_.valueView_)) { + return true; + } break; case CondEq: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq) { + return true; + } break; case CondLt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt) { + return true; + } break; case CondLe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le) { + return true; + } break; case CondGt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt) { + return true; + } break; case CondGe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge) return true; + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge) { + return true; + } break; case CondEmpty: case CondAny: @@ -1185,7 +1298,9 @@ class ComparatorIndexedJsonPathStringDistinct : private DataHolder { switch (cond_) { case CondSet: assertrx_dbg(this->setPtr_); - if (setPtr_->find(value) != setPtr_->cend() && distinct_.Compare(value)) return true; + if (setPtr_->find(value) != setPtr_->cend() && distinct_.Compare(value)) { + return true; + } break; case CondRange: if ((collateCompare(value, range_.value1_.valueView_, *collateOpts_) & ComparationResult::Ge) && @@ -1201,31 +1316,40 @@ class ComparatorIndexedJsonPathStringDistinct : private DataHolder { if (it != allSetPtr_->values_.cend()) { haveDistinct |= distinct_.Compare(value); allSetPtr_->allSetValues_.insert(it->second); - if (haveDistinct && allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) return true; + if (haveDistinct && allSetPtr_->allSetValues_.size() == allSetPtr_->values_.size()) { + return true; + } } } break; case CondLike: - if (matchLikePattern(value, value_.valueView_) && distinct_.Compare(value)) return true; + if (matchLikePattern(value, value_.valueView_) && distinct_.Compare(value)) { + return true; + } break; case CondEq: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Eq && distinct_.Compare(value)) { return true; + } break; case CondLt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Lt && distinct_.Compare(value)) { return true; + } break; case CondLe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Le && distinct_.Compare(value)) { return true; + } break; case CondGt: - if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) == ComparationResult::Gt && distinct_.Compare(value)) { return true; + } break; case CondGe: - if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge && distinct_.Compare(value)) + if (collateCompare(value, value_.valueView_, *collateOpts_) & ComparationResult::Ge && distinct_.Compare(value)) { return true; + } break; case CondEmpty: case CondAny: @@ -1496,7 +1620,9 @@ class ComparatorIndexedOffsetArrayAnyDistinct { const PayloadFieldValue::Array& arr = *reinterpret_cast(item.Ptr() + offset_); const auto* ptr = reinterpret_cast(item.Ptr() + arr.offset); for (const auto* const end = ptr + arr.len; ptr != end; ++ptr) { - if (distinct_.Compare(*ptr)) return true; + if (distinct_.Compare(*ptr)) { + return true; + } } return false; } @@ -1524,7 +1650,9 @@ class ComparatorIndexedOffsetArrayAnyStringDistinct { const PayloadFieldValue::Array& arr = *reinterpret_cast(item.Ptr() + offset_); const p_string* ptr = reinterpret_cast(item.Ptr() + arr.offset); for (const auto* const end = ptr + arr.len; ptr != end; ++ptr) { - if (distinct_.Compare(*ptr)) return true; + if (distinct_.Compare(*ptr)) { + return true; + } } return false; } @@ -1552,7 +1680,9 @@ class ComparatorIndexedJsonPathAny { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::Undefined{}); for (Variant& value : buffer_) { - if (!value.IsNullValue()) return true; + if (!value.IsNullValue()) { + return true; + } } return false; } @@ -1576,7 +1706,9 @@ class ComparatorIndexedJsonPathAnyDistinct { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::From()); for (Variant& value : buffer_) { - if (!value.IsNullValue() && distinct_.Compare(value.As())) return true; + if (!value.IsNullValue() && distinct_.Compare(value.As())) { + return true; + } } return false; } @@ -1608,7 +1740,9 @@ class ComparatorIndexedJsonPathAnyStringDistinct { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::String{}); for (Variant& value : buffer_) { - if (!value.IsNullValue() && distinct_.Compare(std::string_view{value})) return true; + if (!value.IsNullValue() && distinct_.Compare(std::string_view{value})) { + return true; + } } return false; } @@ -1656,7 +1790,9 @@ class ComparatorIndexedJsonPathEmpty { buffer_.clear(); ConstPayload(payloadType_, item).GetByJsonPath(tagsPath_, buffer_, KeyValueType::Undefined{}); for (Variant& value : buffer_) { - if (!value.IsNullValue()) return false; + if (!value.IsNullValue()) { + return false; + } } return true; } diff --git a/cpp_src/core/nsselecter/comparator/comparator_not_indexed.h b/cpp_src/core/nsselecter/comparator/comparator_not_indexed.h index 4d5d45711..149fc9e1d 100644 --- a/cpp_src/core/nsselecter/comparator/comparator_not_indexed.h +++ b/cpp_src/core/nsselecter/comparator/comparator_not_indexed.h @@ -77,7 +77,9 @@ class ComparatorNotIndexedImplBase { ComparatorNotIndexedImplBase(const VariantArray&); [[nodiscard]] std::string ConditionStr() const; [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const Variant& v) const { - if (!v.Type().Is()) return false; + if (!v.Type().Is()) { + return false; + } return matchLikePattern(static_cast(v), valueView_); } @@ -99,7 +101,9 @@ class ComparatorNotIndexedImpl : private ComparatorNotIndexedImplBa [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); for (const Variant& v : buffer_) { - if (Base::Compare(v)) return true; + if (Base::Compare(v)) { + return true; + } } return false; } @@ -124,7 +128,9 @@ class ComparatorNotIndexedImpl : private ComparatorNotIndexedImplBas [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); for (const Variant& v : buffer_) { - if (Base::Compare(v) == 0 && distinct_.Compare(v)) return true; + if (Base::Compare(v) == 0 && distinct_.Compare(v)) { + return true; + } } return false; } @@ -154,7 +160,9 @@ class ComparatorNotIndexedImpl { [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); for (const Variant& v : buffer_) { - if (!v.IsNullValue()) return true; + if (!v.IsNullValue()) { + return true; + } } return false; } @@ -177,7 +185,9 @@ class ComparatorNotIndexedImpl { [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); for (const Variant& v : buffer_) { - if (!v.IsNullValue() && distinct_.Compare(v)) return true; + if (!v.IsNullValue() && distinct_.Compare(v)) { + return true; + } } return false; } @@ -206,7 +216,9 @@ class ComparatorNotIndexedImpl { [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); for (const Variant& v : buffer_) { - if rx_unlikely (!v.IsNullValue()) return false; + if rx_unlikely (!v.IsNullValue()) { + return false; + } } return true; } @@ -266,8 +278,9 @@ class ComparatorNotIndexedImpl : private ComparatorNotIndexed : Base{values, payloadType, fieldPath} {} [[nodiscard]] RX_ALWAYS_INLINE bool Compare(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); - if (buffer_.size() != 2 || !buffer_[0].Type().Is() || !buffer_[0].Type().Is()) + if (buffer_.size() != 2 || !buffer_[0].Type().Is() || !buffer_[0].Type().Is()) { return false; + } const Point p{buffer_[0].As(), buffer_[1].As()}; return DWithin(p, point_, distance_) && distinct_.Compare(Variant{p}); } @@ -275,7 +288,9 @@ class ComparatorNotIndexedImpl : private ComparatorNotIndexed void ClearDistinctValues() noexcept { distinct_.ClearValues(); } void ExcludeDistinctValues(const PayloadValue& item, IdType /*rowId*/) { ConstPayload{payloadType_, item}.GetByJsonPath(fieldPath_, buffer_, KeyValueType::Undefined{}); - if (buffer_.size() != 2 || !buffer_[0].Type().Is() || !buffer_[0].Type().Is()) return; + if (buffer_.size() != 2 || !buffer_[0].Type().Is() || !buffer_[0].Type().Is()) { + return; + } const Point p{buffer_[0].As(), buffer_[1].As()}; distinct_.ExcludeValues(Variant{p}); } @@ -304,7 +319,9 @@ class ComparatorNotIndexedImpl { const auto it = values_.find(v); if (it != values_.cend()) { allSetValues_.emplace(it->second); - if (allSetValues_.size() == values_.size()) return true; + if (allSetValues_.size() == values_.size()) { + return true; + } } } return false; @@ -341,7 +358,9 @@ class ComparatorNotIndexedImpl : private ComparatorNotIndexedI if (distinct_.Compare(it->first)) { haveNotDistinct = true; } - if (haveNotDistinct && allSetValues_.size() == values_.size()) return true; + if (haveNotDistinct && allSetValues_.size() == values_.size()) { + return true; + } } } return false; diff --git a/cpp_src/core/nsselecter/comparator/equalposition_comparator.cc b/cpp_src/core/nsselecter/comparator/equalposition_comparator.cc index c28eb467a..e280835c2 100644 --- a/cpp_src/core/nsselecter/comparator/equalposition_comparator.cc +++ b/cpp_src/core/nsselecter/comparator/equalposition_comparator.cc @@ -6,20 +6,20 @@ namespace reindexer { -void EqualPositionComparator::BindField(const std::string &name, int field, const VariantArray &values, CondType cond, - const CollateOpts &collate) { +void EqualPositionComparator::BindField(const std::string& name, int field, const VariantArray& values, CondType cond, + const CollateOpts& collate) { bindField(name, field, values, cond, collate); } -void EqualPositionComparator::BindField(const std::string &name, const FieldsPath &fieldPath, const VariantArray &values, CondType cond) { +void EqualPositionComparator::BindField(const std::string& name, const FieldsPath& fieldPath, const VariantArray& values, CondType cond) { bindField(name, fieldPath, values, cond, CollateOpts{}); } template -void EqualPositionComparator::bindField(const std::string &name, F field, const VariantArray &values, CondType cond, - const CollateOpts &collate) { +void EqualPositionComparator::bindField(const std::string& name, F field, const VariantArray& values, CondType cond, + const CollateOpts& collate) { fields_.push_back(field); - Context &ctx = ctx_.emplace_back(collate); + Context& ctx = ctx_.emplace_back(collate); ctx.cond = cond; ctx.cmpBool.SetValues(cond, values); @@ -33,7 +33,7 @@ void EqualPositionComparator::bindField(const std::string &name, F field, const name_ += ' ' + name; } -bool EqualPositionComparator::Compare(const PayloadValue &pv, IdType /*rowId*/) { +bool EqualPositionComparator::Compare(const PayloadValue& pv, IdType /*rowId*/) { ConstPayload pl(payloadType_, pv); size_t len = INT_MAX; @@ -41,7 +41,7 @@ bool EqualPositionComparator::Compare(const PayloadValue &pv, IdType /*rowId*/) size_t tagsPathIdx = 0; vals.reserve(fields_.size()); for (size_t j = 0; j < fields_.size(); ++j) { - auto &v = vals.emplace_back(); + auto& v = vals.emplace_back(); bool isRegularIndex = fields_[j] != IndexValueType::SetByJsonPath && fields_[j] < payloadType_.NumFields(); if (isRegularIndex) { pl.Get(fields_[j], v); @@ -49,7 +49,9 @@ bool EqualPositionComparator::Compare(const PayloadValue &pv, IdType /*rowId*/) assertrx_throw(tagsPathIdx < fields_.getTagsPathsLength()); pl.GetByJsonPath(fields_.getTagsPath(tagsPathIdx++), v, KeyValueType::Undefined{}); } - if (v.size() < len) len = vals.back().size(); + if (v.size() < len) { + len = vals.back().size(); + } } for (size_t i = 0; i < len; ++i) { @@ -57,7 +59,9 @@ bool EqualPositionComparator::Compare(const PayloadValue &pv, IdType /*rowId*/) for (size_t j = 0; j < fields_.size(); ++j) { assertrx_throw(i < vals[j].size()); cmpRes &= !vals[j][i].Type().Is() && compareField(j, vals[j][i]); - if (!cmpRes) break; + if (!cmpRes) { + break; + } } if (cmpRes) { @@ -68,7 +72,7 @@ bool EqualPositionComparator::Compare(const PayloadValue &pv, IdType /*rowId*/) return false; } -bool EqualPositionComparator::compareField(size_t field, const Variant &v) { +bool EqualPositionComparator::compareField(size_t field, const Variant& v) { return v.Type().EvaluateOneOf( [&](KeyValueType::Bool) { return ctx_[field].cmpBool.Compare(ctx_[field].cond, static_cast(v)); }, [&](KeyValueType::Int) { return ctx_[field].cmpInt.Compare(ctx_[field].cond, static_cast(v)); }, diff --git a/cpp_src/core/nsselecter/comparator/equalposition_comparator_impl.h b/cpp_src/core/nsselecter/comparator/equalposition_comparator_impl.h index 52566b39b..ea5818181 100644 --- a/cpp_src/core/nsselecter/comparator/equalposition_comparator_impl.h +++ b/cpp_src/core/nsselecter/comparator/equalposition_comparator_impl.h @@ -15,12 +15,12 @@ namespace reindexer { template class EqualPositionComparatorImpl { using ValuesSet = intrusive_atomic_rc_wrapper>; - using AllSetValuesSet = intrusive_atomic_rc_wrapper>; + using AllSetValuesSet = intrusive_atomic_rc_wrapper>; public: EqualPositionComparatorImpl() noexcept = default; - void SetValues(CondType cond, const VariantArray &values) { + void SetValues(CondType cond, const VariantArray& values) { if (cond == CondSet) { valuesS_.reset(new ValuesSet{}); } else if (cond == CondAllSet) { @@ -62,7 +62,9 @@ class EqualPositionComparatorImpl { return valuesS_->find(lhs) != valuesS_->end(); case CondAllSet: { const auto it = valuesS_->find(lhs); - if (it == valuesS_->end()) return false; + if (it == valuesS_->end()) { + return false; + } allSetValuesS_->insert(&*it); return allSetValuesS_->size() == valuesS_->size(); } @@ -88,17 +90,17 @@ class EqualPositionComparatorImpl { private: KeyValueType type() { - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) { return KeyValueType::Int{}; - else if constexpr (std::is_same_v) + } else if constexpr (std::is_same_v) { return KeyValueType::Bool{}; - else if constexpr (std::is_same_v) + } else if constexpr (std::is_same_v) { return KeyValueType::Int64{}; - else if constexpr (std::is_same_v) + } else if constexpr (std::is_same_v) { return KeyValueType::Double{}; - else if constexpr (std::is_same_v) + } else if constexpr (std::is_same_v) { return KeyValueType::Uuid{}; - else { + } else { static_assert(std::is_same_v, "Unknown KeyValueType"); } } @@ -115,12 +117,12 @@ class EqualPositionComparatorImpl { template <> class EqualPositionComparatorImpl { using ValuesSet = intrusive_atomic_rc_wrapper>; - using AllSetValuesSet = intrusive_atomic_rc_wrapper>; + using AllSetValuesSet = intrusive_atomic_rc_wrapper>; public: EqualPositionComparatorImpl() noexcept = default; - void SetValues(CondType cond, const VariantArray &values) { + void SetValues(CondType cond, const VariantArray& values) { if (cond == CondSet) { valuesS_.reset(new ValuesSet{}); } else if (cond == CondAllSet) { @@ -128,7 +130,7 @@ class EqualPositionComparatorImpl { allSetValuesS_.reset(new AllSetValuesSet{}); } - for (const Variant &key : values) { + for (const Variant& key : values) { key.Type().EvaluateOneOf( overloaded{[&](KeyValueType::Uuid) { addValue(cond, key.As()); }, [&](KeyValueType::String) { @@ -166,7 +168,9 @@ class EqualPositionComparatorImpl { return valuesS_->find(lhs) != valuesS_->end(); case CondAllSet: { const auto it = valuesS_->find(lhs); - if (it == valuesS_->end()) return false; + if (it == valuesS_->end()) { + return false; + } allSetValuesS_->insert(&*it); return allSetValuesS_->size() == valuesS_->size(); } @@ -202,14 +206,14 @@ class EqualPositionComparatorImpl { template <> class EqualPositionComparatorImpl { public: - EqualPositionComparatorImpl(const CollateOpts &collate) : collate_{collate} {} + EqualPositionComparatorImpl(const CollateOpts& collate) : collate_{collate} {} - void SetValues(CondType cond, const VariantArray &values) { + void SetValues(CondType cond, const VariantArray& values) { if (cond == CondSet) { valuesS_ = make_intrusive>(collate_); } else if (cond == CondAllSet) { valuesS_ = make_intrusive>(collate_); - allSetValuesS_ = make_intrusive>>(); + allSetValuesS_ = make_intrusive>>(); } for (Variant key : values) { @@ -241,7 +245,9 @@ class EqualPositionComparatorImpl { assertrx_dbg(valuesS_); assertrx_dbg(allSetValuesS_); auto it = valuesS_->find(lhs); - if (it == valuesS_->end()) return false; + if (it == valuesS_->end()) { + return false; + } allSetValuesS_->insert(&*it); return allSetValuesS_->size() == valuesS_->size(); } @@ -267,17 +273,17 @@ class EqualPositionComparatorImpl { class key_string_set : public tsl::hopscotch_sc_set { public: - key_string_set(const CollateOpts &opts) + key_string_set(const CollateOpts& opts) : tsl::hopscotch_sc_set( 1000, hash_key_string(CollateMode(opts.mode)), equal_key_string(opts), std::allocator(), less_key_string(opts)) {} }; intrusive_ptr> valuesS_; - intrusive_ptr>> allSetValuesS_; + intrusive_ptr>> allSetValuesS_; private: - void addValue(CondType cond, const key_string &value) { + void addValue(CondType cond, const key_string& value) { if (cond == CondSet || cond == CondAllSet) { valuesS_->emplace(value); } else { @@ -301,8 +307,10 @@ class EqualPositionComparatorImpl { public: EqualPositionComparatorImpl() noexcept = default; - void SetValues(const VariantArray &values) { - if (values.size() != 2) throw Error(errQueryExec, "CondDWithin expects two arguments"); + void SetValues(const VariantArray& values) { + if (values.size() != 2) { + throw Error(errQueryExec, "CondDWithin expects two arguments"); + } if (values[0].Type().Is()) { rhs_ = values[0].As(); distance_ = values[1].As(); diff --git a/cpp_src/core/nsselecter/comparator/fieldscomparator.cc b/cpp_src/core/nsselecter/comparator/fieldscomparator.cc index e0f191aef..6b84de8c0 100644 --- a/cpp_src/core/nsselecter/comparator/fieldscomparator.cc +++ b/cpp_src/core/nsselecter/comparator/fieldscomparator.cc @@ -12,42 +12,42 @@ namespace { class ArrayAdapter { class ConstIterator { public: - ConstIterator(const ArrayAdapter &aa, size_t i) noexcept : aa_{aa}, index_{i} {} - ConstIterator &operator++() noexcept { + ConstIterator(const ArrayAdapter& aa, size_t i) noexcept : aa_{aa}, index_{i} {} + ConstIterator& operator++() noexcept { ++index_; return *this; } - bool operator!=(const ConstIterator &other) const noexcept { + bool operator!=(const ConstIterator& other) const noexcept { assertrx_dbg(&aa_ == &other.aa_); return index_ != other.index_; } reindexer::Variant operator*() const { return aa_[index_]; } private: - const ArrayAdapter &aa_; + const ArrayAdapter& aa_; size_t index_; }; public: - ArrayAdapter(const uint8_t *ptr, size_t l, unsigned size_of, reindexer::KeyValueType t) noexcept + ArrayAdapter(const uint8_t* ptr, size_t l, unsigned size_of, reindexer::KeyValueType t) noexcept : ptr_{ptr}, len_{l}, sizeof_{size_of}, type_{t} {} size_t size() const noexcept { return len_; } reindexer::Variant operator[](size_t i) const { assertrx_dbg(i < len_); return type_.EvaluateOneOf( [&](reindexer::KeyValueType::Int64) noexcept { - return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; + return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, [&](reindexer::KeyValueType::Double) noexcept { - return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; + return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, [&](reindexer::KeyValueType::String) noexcept { - return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; + return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, - [&](reindexer::KeyValueType::Bool) noexcept { return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, - [&](reindexer::KeyValueType::Int) noexcept { return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, + [&](reindexer::KeyValueType::Bool) noexcept { return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, + [&](reindexer::KeyValueType::Int) noexcept { return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, [&](reindexer::KeyValueType::Uuid) noexcept { - return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; + return reindexer::Variant{*reinterpret_cast(ptr_ + sizeof_ * i)}; }, [&](reindexer::OneOf) -> reindexer::Variant { @@ -58,7 +58,7 @@ class ArrayAdapter { ConstIterator end() const noexcept { return {*this, len_}; } private: - const uint8_t *ptr_; + const uint8_t* ptr_; size_t len_; unsigned sizeof_; reindexer::KeyValueType type_; @@ -92,16 +92,18 @@ FieldsComparator::FieldsComparator(std::string_view lField, CondType cond, std:: } template -bool FieldsComparator::compare(const LArr &lhs, const RArr &rhs) { +bool FieldsComparator::compare(const LArr& lhs, const RArr& rhs) { static constexpr bool needCompareTypes{std::is_same_v || std::is_same_v}; switch (condition_) { case CondRange: if (rhs.size() < 2 || rhs[0].Type().template Is() || rhs[1].Type().template Is()) { throw Error{errQueryExec, "For condition range second field should be an array of 2 values"}; } - for (const Variant &v : lhs) { + for (const Variant& v : lhs) { if constexpr (needCompareTypes) { - if (!compareTypes(v.Type(), rhs[0].Type()) || !compareTypes(v.Type(), rhs[1].Type())) continue; + if (!compareTypes(v.Type(), rhs[0].Type()) || !compareTypes(v.Type(), rhs[1].Type())) { + continue; + } } if ((v.RelaxCompare(rhs[0], collateOpts_) & ComparationResult::Ge) && (v.RelaxCompare(rhs[1], collateOpts_) & ComparationResult::Le)) { @@ -110,30 +112,40 @@ bool FieldsComparator::compare(const LArr &lhs, const RArr &rhs) { } return false; case CondLike: - for (const Variant &lv : lhs) { - for (const Variant &rv : rhs) { + for (const Variant& lv : lhs) { + for (const Variant& rv : rhs) { if (!lv.Type().Is() || !rv.Type().Is()) { throw Error{errQueryExec, "For condition LIKE fields should be of string type"}; } - if (matchLikePattern(std::string_view(lv), std::string_view(rv))) return true; + if (matchLikePattern(std::string_view(lv), std::string_view(rv))) { + return true; + } } } return false; case CondAllSet: - for (const Variant &rv : rhs) { - if (rv.Type().Is()) continue; + for (const Variant& rv : rhs) { + if (rv.Type().Is()) { + continue; + } bool found = false; - for (const Variant &lv : lhs) { - if (lv.Type().Is()) continue; + for (const Variant& lv : lhs) { + if (lv.Type().Is()) { + continue; + } if constexpr (needCompareTypes) { - if (!compareTypes(lv.Type(), rv.Type())) continue; + if (!compareTypes(lv.Type(), rv.Type())) { + continue; + } } if (lv.RelaxCompare(rv, collateOpts_) == ComparationResult::Eq) { found = true; break; } } - if (!found) return false; + if (!found) { + return false; + } } return true; case CondAny: @@ -146,30 +158,46 @@ bool FieldsComparator::compare(const LArr &lhs, const RArr &rhs) { case CondEmpty: case CondDWithin: default: - for (const Variant &lv : lhs) { - if (lv.Type().Is()) continue; - for (const Variant &rv : rhs) { - if (rv.Type().Is()) continue; + for (const Variant& lv : lhs) { + if (lv.Type().Is()) { + continue; + } + for (const Variant& rv : rhs) { + if (rv.Type().Is()) { + continue; + } if constexpr (needCompareTypes) { - if (!compareTypes(lv.Type(), rv.Type())) continue; + if (!compareTypes(lv.Type(), rv.Type())) { + continue; + } } const auto compRes = lv.RelaxCompare(rv, collateOpts_); switch (condition_) { case CondEq: case CondSet: - if (compRes == ComparationResult::Eq) return true; + if (compRes == ComparationResult::Eq) { + return true; + } break; case CondLt: - if (compRes == ComparationResult::Lt) return true; + if (compRes == ComparationResult::Lt) { + return true; + } break; case CondLe: - if (compRes & ComparationResult::Le) return true; + if (compRes & ComparationResult::Le) { + return true; + } break; case CondGt: - if (compRes == ComparationResult::Gt) return true; + if (compRes == ComparationResult::Gt) { + return true; + } break; case CondGe: - if (compRes & ComparationResult::Ge) return true; + if (compRes & ComparationResult::Ge) { + return true; + } break; case CondAny: case CondEmpty: @@ -186,7 +214,7 @@ bool FieldsComparator::compare(const LArr &lhs, const RArr &rhs) { } } -bool FieldsComparator::compare(const PayloadValue &item, const Context &ctx) { +bool FieldsComparator::compare(const PayloadValue& item, const Context& ctx) { bool result; if (ctx.lCtx_.fields_.getTagsPathsLength() > 0) { VariantArray lhs; @@ -196,7 +224,7 @@ bool FieldsComparator::compare(const PayloadValue &item, const Context &ctx) { ConstPayload(payloadType_, item).GetByJsonPath(ctx.rCtx_.fields_.getTagsPath(0), rhs, ctx.rCtx_.type_); result = compare(lhs, rhs); } else if (ctx.rCtx_.isArray_) { - const PayloadFieldValue::Array *rArr = reinterpret_cast(item.Ptr() + ctx.rCtx_.offset_); + const PayloadFieldValue::Array* rArr = reinterpret_cast(item.Ptr() + ctx.rCtx_.offset_); result = compare(lhs, ArrayAdapter(item.Ptr() + rArr->offset, rArr->len, ctx.rCtx_.sizeof_, ctx.rCtx_.type_)); } else { result = compare(lhs, ArrayAdapter(item.Ptr() + ctx.rCtx_.offset_, 1, ctx.rCtx_.sizeof_, ctx.rCtx_.type_)); @@ -205,15 +233,15 @@ bool FieldsComparator::compare(const PayloadValue &item, const Context &ctx) { VariantArray rhs; ConstPayload(payloadType_, item).GetByJsonPath(ctx.rCtx_.fields_.getTagsPath(0), rhs, ctx.rCtx_.type_); if (ctx.lCtx_.isArray_) { - const PayloadFieldValue::Array *lArr = reinterpret_cast(item.Ptr() + ctx.lCtx_.offset_); + const PayloadFieldValue::Array* lArr = reinterpret_cast(item.Ptr() + ctx.lCtx_.offset_); result = compare(ArrayAdapter(item.Ptr() + lArr->offset, lArr->len, ctx.lCtx_.sizeof_, ctx.lCtx_.type_), rhs); } else { result = compare(ArrayAdapter(item.Ptr() + ctx.lCtx_.offset_, 1, ctx.lCtx_.sizeof_, ctx.lCtx_.type_), rhs); } } else if (ctx.lCtx_.isArray_) { - const PayloadFieldValue::Array *lArr = reinterpret_cast(item.Ptr() + ctx.lCtx_.offset_); + const PayloadFieldValue::Array* lArr = reinterpret_cast(item.Ptr() + ctx.lCtx_.offset_); if (ctx.rCtx_.isArray_) { - const PayloadFieldValue::Array *rArr = reinterpret_cast(item.Ptr() + ctx.rCtx_.offset_); + const PayloadFieldValue::Array* rArr = reinterpret_cast(item.Ptr() + ctx.rCtx_.offset_); result = compare(ArrayAdapter(item.Ptr() + lArr->offset, lArr->len, ctx.lCtx_.sizeof_, ctx.lCtx_.type_), ArrayAdapter(item.Ptr() + rArr->offset, rArr->len, ctx.rCtx_.sizeof_, ctx.rCtx_.type_)); } else { @@ -221,31 +249,31 @@ bool FieldsComparator::compare(const PayloadValue &item, const Context &ctx) { ArrayAdapter(item.Ptr() + ctx.rCtx_.offset_, 1, ctx.rCtx_.sizeof_, ctx.rCtx_.type_)); } } else if (ctx.rCtx_.isArray_) { - const PayloadFieldValue::Array *rArr = reinterpret_cast(item.Ptr() + ctx.rCtx_.offset_); + const PayloadFieldValue::Array* rArr = reinterpret_cast(item.Ptr() + ctx.rCtx_.offset_); result = compare(ArrayAdapter(item.Ptr() + ctx.lCtx_.offset_, 1, ctx.lCtx_.sizeof_, ctx.lCtx_.type_), ArrayAdapter(item.Ptr() + rArr->offset, rArr->len, ctx.rCtx_.sizeof_, ctx.rCtx_.type_)); } else { result = compare(ArrayAdapter(item.Ptr() + ctx.lCtx_.offset_, 1, ctx.lCtx_.sizeof_, ctx.lCtx_.type_), ArrayAdapter(item.Ptr() + ctx.rCtx_.offset_, 1, ctx.rCtx_.sizeof_, ctx.rCtx_.type_)); } - if (result) ++matchedCount_; + if (result) { + ++matchedCount_; + } return result; } void FieldsComparator::validateTypes(KeyValueType lType, KeyValueType rType) const { - if (lType.IsSame(rType) || lType.Is() || rType.Is()) return; + if (lType.IsSame(rType) || lType.Is() || rType.Is()) { + return; + } lType.EvaluateOneOf( - [&](KeyValueType::String) { - throw Error{errQueryExec, "Cannot compare a string field with a non-string one: %s", name_}; - }, + [&](KeyValueType::String) { throw Error{errQueryExec, "Cannot compare a string field with a non-string one: %s", name_}; }, [&](OneOf) { if (!rType.Is() && !rType.Is() && !rType.Is()) { throw Error{errQueryExec, "Cannot compare a numeric field with a non-numeric one: %s", name_}; } }, - [&](KeyValueType::Bool) { - throw Error{errQueryExec, "Cannot compare a boolean field with a non-boolean one: %s", name_}; - }, + [&](KeyValueType::Bool) { throw Error{errQueryExec, "Cannot compare a boolean field with a non-boolean one: %s", name_}; }, [&](OneOf) { throw Error{errQueryExec, "Field of type %s cannot be compared with another field: %s", lType.Name(), name_}; }); diff --git a/cpp_src/core/nsselecter/comparator/fieldscomparator.h b/cpp_src/core/nsselecter/comparator/fieldscomparator.h index e8f90730d..41a6fbd8f 100644 --- a/cpp_src/core/nsselecter/comparator/fieldscomparator.h +++ b/cpp_src/core/nsselecter/comparator/fieldscomparator.h @@ -16,7 +16,9 @@ class FieldsComparator { bool Compare(const PayloadValue& item, IdType /*rowId*/) { if (ctx_.size() > 1) { for (const auto& c : ctx_) { - if (!compare(item, c)) return false; + if (!compare(item, c)) { + return false; + } } return true; } @@ -130,7 +132,9 @@ class FieldsComparator { bool compare(const PayloadValue& item, const Context&); void validateTypes(KeyValueType lType, KeyValueType rType) const; inline static bool compareTypes(KeyValueType lType, KeyValueType rType) noexcept { - if (lType.IsSame(rType)) return true; + if (lType.IsSame(rType)) { + return true; + } return lType.EvaluateOneOf( [&](OneOf) noexcept { return rType.EvaluateOneOf( diff --git a/cpp_src/core/nsselecter/explaincalc.cc b/cpp_src/core/nsselecter/explaincalc.cc index 9cd5544f0..cba66acf2 100644 --- a/cpp_src/core/nsselecter/explaincalc.cc +++ b/cpp_src/core/nsselecter/explaincalc.cc @@ -23,23 +23,23 @@ void ExplainCalc::LogDump(int logLevel) { if (selectors_) { selectors_->VisitForEach( Skip{}, - [this](const SelectIterator &s) { + [this](const SelectIterator& s) { logPrintf(LogInfo, "%s: %d idsets, cost %g, matched %d, %s", s.name, s.size(), s.Cost(iters_), s.GetMatchedCount(), s.Dump()); }, - [this](const FieldsComparator &c) { + [this](const FieldsComparator& c) { logPrintf(LogInfo, "%s: cost %g, matched %d, %s", c.Name(), c.Cost(iters_), c.GetMatchedCount(), c.Dump()); }, Restricted>{}( - [this](const auto &c) { + [this](const auto& c) { logPrintf(LogInfo, "%s: cost %g, matched %d, %s", c.Name(), c.Cost(iters_), c.GetMatchedCount(), c.Dump()); }), - [](const AlwaysTrue &) { logPrintf(LogInfo, "AlwaysTrue"); }); + [](const AlwaysTrue&) { logPrintf(LogInfo, "AlwaysTrue"); }); } if (jselectors_) { - for (auto &js : *jselectors_) { + for (auto& js : *jselectors_) { if (js.Type() == JoinType::LeftJoin || js.Type() == JoinType::Merge) { logPrintf(LogInfo, "%s %s: called %d", JoinTypeName(js.Type()), js.RightNsName(), js.Called()); } else { @@ -51,7 +51,7 @@ void ExplainCalc::LogDump(int logLevel) { } } -constexpr static inline const char *joinTypeName(JoinType type) noexcept { +constexpr static inline const char* joinTypeName(JoinType type) noexcept { switch (type) { case JoinType::InnerJoin: return "inner_join "; @@ -66,7 +66,7 @@ constexpr static inline const char *joinTypeName(JoinType type) noexcept { } } -constexpr static inline const char *opName(OpType op, bool first = true) { +constexpr static inline const char* opName(OpType op, bool first = true) { switch (op) { case OpAnd: return first ? "" : "and "; @@ -93,12 +93,12 @@ constexpr std::string_view fieldKind(IteratorFieldKind fk) { } } -RX_NO_INLINE static std::string buildPreselectDescription(const JoinPreResult &result) { +RX_NO_INLINE static std::string buildPreselectDescription(const JoinPreResult& result) { assertrx_throw(result.properties); return std::visit( overloaded{ - [&](const IdSet &) -> std::string { - const PreselectProperties &props = *result.properties; + [&](const IdSet&) -> std::string { + const PreselectProperties& props = *result.properties; switch (result.storedValuesOptStatus) { case StoredValuesOptimizationStatus::DisabledByCompositeIndex: return fmt::sprintf( @@ -125,8 +125,8 @@ RX_NO_INLINE static std::string buildPreselectDescription(const JoinPreResult &r throw_as_assert; } }, - [&](const SelectIteratorContainer &) -> std::string { - const PreselectProperties &props = *result.properties; + [&](const SelectIteratorContainer&) -> std::string { + const PreselectProperties& props = *result.properties; if (props.isLimitExceeded) { return fmt::sprintf( "using no_preselect, because joined query's expected max iterations count of %d is larger than " @@ -138,14 +138,14 @@ RX_NO_INLINE static std::string buildPreselectDescription(const JoinPreResult &r return "using no_preselect, because joined query expects a sort a btree index that is not yet committed " "(optimization of indexes for the target namespace is not complete)"; }, - [&](const JoinPreResult::Values &) { + [&](const JoinPreResult::Values&) { return fmt::sprintf("using preselected_values, because the namespace's max iterations count is very small of %d", result.properties->qresMaxIteratios); }}, result.payload); } -static std::string addToJSON(JsonBuilder &builder, const JoinedSelector &js, OpType op = OpAnd) { +static std::string addToJSON(JsonBuilder& builder, const JoinedSelector& js, OpType op = OpAnd) { using namespace std::string_view_literals; auto jsonSel = builder.Object(); std::string name{joinTypeName(js.Type()) + js.RightNsName()}; @@ -157,15 +157,15 @@ static std::string addToJSON(JsonBuilder &builder, const JoinedSelector &js, OpT case JoinType::InnerJoin: case JoinType::OrInnerJoin: case JoinType::LeftJoin: - std::visit(overloaded{[&](const JoinPreResult::Values &values) { + std::visit(overloaded{[&](const JoinPreResult::Values& values) { jsonSel.Put("method"sv, "preselected_values"sv); jsonSel.Put("keys"sv, values.size()); }, - [&](const IdSet &ids) { + [&](const IdSet& ids) { jsonSel.Put("method"sv, "preselected_rows"sv); jsonSel.Put("keys"sv, ids.size()); }, - [&](const SelectIteratorContainer &iterators) { + [&](const SelectIteratorContainer& iterators) { jsonSel.Put("method"sv, "no_preselect"sv); jsonSel.Put("keys"sv, iterators.Size()); }}, @@ -186,7 +186,7 @@ static std::string addToJSON(JsonBuilder &builder, const JoinedSelector &js, OpT return name; } -static void addToJSON(JsonBuilder &builder, const ConditionInjection &injCond) { +static void addToJSON(JsonBuilder& builder, const ConditionInjection& injCond) { auto jsonSel = builder.Object(); using namespace std::string_view_literals; using namespace std::string_literals; @@ -220,7 +220,7 @@ static void addToJSON(JsonBuilder &builder, const ConditionInjection &injCond) { jsonSel.Put("new_condition"sv, injCond.newCond); } -static std::string addToJSON(JsonBuilder &builder, const JoinOnInjection &injCond) { +static std::string addToJSON(JsonBuilder& builder, const JoinOnInjection& injCond) { auto jsonSel = builder.Object(); std::string name{injCond.rightNsName}; using namespace std::string_view_literals; @@ -236,7 +236,7 @@ static std::string addToJSON(JsonBuilder &builder, const JoinOnInjection &injCon jsonSel.Put("injected_condition"sv, injCond.injectedCond.Slice()); if (!injCond.conditions.empty()) { auto jsonCondInjections = jsonSel.Array("conditions"sv); - for (const auto &cond : injCond.conditions) { + for (const auto& cond : injCond.conditions) { addToJSON(jsonCondInjections, cond); } } @@ -260,11 +260,11 @@ std::string ExplainCalc::GetJSON() { json.Put("general_sort_us"sv, To_us(sort_)); if (!subqueries_.empty()) { auto subQueries = json.Array("subqueries"); - for (const auto &sq : subqueries_) { + for (const auto& sq : subqueries_) { auto s = subQueries.Object(); s.Put("namespace", sq.NsName()); s.Raw("explain", sq.Explain()); - std::visit(overloaded{[&](size_t k) { s.Put("keys", k); }, [&](const std::string &f) { s.Put("field", f); }}, + std::visit(overloaded{[&](size_t k) { s.Put("keys", k); }, [&](const std::string& f) { s.Put("field", f); }}, sq.FieldOrKeys()); } } @@ -281,8 +281,10 @@ std::string ExplainCalc::GetJSON() { if (jselectors_) { // adding explain for LeftJoin-s and Merge subqueries - for (const JoinedSelector &js : *jselectors_) { - if (js.Type() == JoinType::InnerJoin || js.Type() == JoinType::OrInnerJoin) continue; + for (const JoinedSelector& js : *jselectors_) { + if (js.Type() == JoinType::InnerJoin || js.Type() == JoinType::OrInnerJoin) { + continue; + } addToJSON(jsonSelArr, js); } } @@ -290,7 +292,7 @@ std::string ExplainCalc::GetJSON() { if (onInjections_ && !onInjections_->empty()) { auto jsonOnInjections = json.Array("on_conditions_injections"sv); - for (const JoinOnInjection &injCond : *onInjections_) { + for (const JoinOnInjection& injCond : *onInjections_) { addToJSON(jsonOnInjections, injCond); } } @@ -299,17 +301,19 @@ std::string ExplainCalc::GetJSON() { return std::string(ser.Slice()); } -std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_iterator end, int iters, JsonBuilder &builder, - const JoinedSelectors *jselectors) { +std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_iterator end, int iters, JsonBuilder& builder, + const JoinedSelectors* jselectors) { using namespace std::string_literals; using namespace std::string_view_literals; std::stringstream name; name << '('; for (const_iterator it = begin; it != end; ++it) { - if (it != begin) name << ' '; + if (it != begin) { + name << ' '; + } it->Visit( - [&](const SelectIteratorsBracket &) { + [&](const SelectIteratorsBracket&) { auto jsonSel = builder.Object(); auto jsonSelArr = jsonSel.Array("selectors"sv); const std::string brName{explainJSON(it.cbegin(), it.cend(), iters, jsonSelArr, jselectors)}; @@ -317,7 +321,7 @@ std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_ite jsonSel.Put("field"sv, opName(it->operation) + brName); name << opName(it->operation, it == begin) << brName; }, - [&](const SelectIterator &siter) { + [&](const SelectIterator& siter) { auto jsonSel = builder.Object(); const bool isScanIterator{std::string_view(siter.name) == "-scan"sv}; if (!isScanIterator) { @@ -335,12 +339,12 @@ std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_ite jsonSel.Put("type"sv, siter.TypeName()); name << opName(it->operation, it == begin) << siter.name; }, - [&](const JoinSelectIterator &jiter) { + [&](const JoinSelectIterator& jiter) { assertrx_throw(jiter.joinIndex < jselectors->size()); const std::string jName{addToJSON(builder, (*jselectors)[jiter.joinIndex], it->operation)}; name << opName(it->operation, it == begin) << jName; }, - Restricted{}([&](const auto &c) { + Restricted{}([&](const auto& c) { auto jsonSel = builder.Object(); if constexpr (std::is_same_v) { jsonSel.Put("comparators"sv, c.FieldsCount()); @@ -356,7 +360,7 @@ std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_ite }), Restricted>{}( - [&](const auto &c) { + [&](const auto& c) { auto jsonSel = builder.Object(); jsonSel.Put("comparators"sv, 1); jsonSel.Put("field"sv, opName(it->operation) + std::string{c.Name()}); @@ -370,7 +374,7 @@ std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_ite : IteratorFieldKind::Indexed)); name << opName(it->operation, it == begin) << c.Name(); }), - [&](const AlwaysTrue &) { + [&](const AlwaysTrue&) { auto jsonSkipped = builder.Object(); jsonSkipped.Put("type"sv, "Skipped"sv); jsonSkipped.Put("description"sv, "always "s + (it->operation == OpNot ? "false" : "true")); @@ -382,6 +386,6 @@ std::string SelectIteratorContainer::explainJSON(const_iterator begin, const_ite return name.str(); } -int ExplainCalc::To_us(const ExplainCalc::Duration &d) noexcept { return duration_cast(d).count(); } +int ExplainCalc::To_us(const ExplainCalc::Duration& d) noexcept { return duration_cast(d).count(); } } // namespace reindexer diff --git a/cpp_src/core/nsselecter/explaincalc.h b/cpp_src/core/nsselecter/explaincalc.h index 02cce38d1..bc53f7177 100644 --- a/cpp_src/core/nsselecter/explaincalc.h +++ b/cpp_src/core/nsselecter/explaincalc.h @@ -49,39 +49,55 @@ class ExplainCalc { explicit ExplainCalc(bool enable) noexcept : enabled_(enable) {} void StartTiming() noexcept { - if (enabled_) lap(); + if (enabled_) { + lap(); + } } void StopTiming() noexcept { - if (enabled_) total_ = preselect_ + prepare_ + select_ + postprocess_ + loop_; + if (enabled_) { + total_ = preselect_ + prepare_ + select_ + postprocess_ + loop_; + } } void AddPrepareTime() noexcept { - if (enabled_) prepare_ += lap(); + if (enabled_) { + prepare_ += lap(); + } } void AddSelectTime() noexcept { - if (enabled_) select_ += lap(); + if (enabled_) { + select_ += lap(); + } } void AddPostprocessTime() noexcept { - if (enabled_) postprocess_ += lap(); + if (enabled_) { + postprocess_ += lap(); + } } void AddLoopTime() noexcept { - if (enabled_) loop_ += lap(); + if (enabled_) { + loop_ += lap(); + } } void AddIterations(int iters) noexcept { iters_ += iters; } void StartSort() noexcept { - if (enabled_) sort_start_point_ = Clock::now(); + if (enabled_) { + sort_start_point_ = Clock::now(); + } } void StopSort() noexcept { - if (enabled_) sort_ = Clock::now() - sort_start_point_; + if (enabled_) { + sort_ = Clock::now() - sort_start_point_; + } } void PutCount(int cnt) noexcept { count_ = cnt; } void PutSortIndex(std::string_view index) noexcept { sortIndex_ = index; } - void PutSelectors(const SelectIteratorContainer *qres) noexcept { selectors_ = qres; } - void PutJoinedSelectors(const JoinedSelectors *jselectors) noexcept { jselectors_ = jselectors; } + void PutSelectors(const SelectIteratorContainer* qres) noexcept { selectors_ = qres; } + void PutJoinedSelectors(const JoinedSelectors* jselectors) noexcept { jselectors_ = jselectors; } void SetPreselectTime(Duration preselectTime) noexcept { preselect_ = preselectTime; } - void PutOnConditionInjections(const OnConditionInjections *onCondInjections) noexcept { onInjections_ = onCondInjections; } + void PutOnConditionInjections(const OnConditionInjections* onCondInjections) noexcept { onInjections_ = onCondInjections; } void SetSortOptimization(bool enable) noexcept { sortOptimization_ = enable; } - void SetSubQueriesExplains(std::vector &&subQueriesExpl) noexcept { subqueries_ = std::move(subQueriesExpl); } + void SetSubQueriesExplains(std::vector&& subQueriesExpl) noexcept { subqueries_ = std::move(subQueriesExpl); } void LogDump(int logLevel); std::string GetJSON(); diff --git a/cpp_src/core/nsselecter/itemcomparator.cc b/cpp_src/core/nsselecter/itemcomparator.cc index e92896048..baedc2b59 100644 --- a/cpp_src/core/nsselecter/itemcomparator.cc +++ b/cpp_src/core/nsselecter/itemcomparator.cc @@ -24,15 +24,15 @@ struct FieldsCompRes { namespace reindexer { -bool ItemComparator::operator()(const ItemRef &lhs, const ItemRef &rhs) const { +bool ItemComparator::operator()(const ItemRef& lhs, const ItemRef& rhs) const { size_t expressionIndex{0}; FieldsCompRes mainNsRes; FieldsCompRes joinedNsRes; - for (const auto &comp : comparators_) { + for (const auto& comp : comparators_) { const ComparationResult res = std::visit( overloaded{[&](CompareByExpression c) noexcept { assertrx_throw(expressionIndex < ctx_.sortingContext.exprResults.size()); - const auto &eR{ctx_.sortingContext.exprResults[expressionIndex++]}; + const auto& eR{ctx_.sortingContext.exprResults[expressionIndex++]}; const auto lR{eR[lhs.SortExprResultsIdx()]}; const auto rR{eR[rhs.SortExprResultsIdx()]}; if (lR == rR) { @@ -45,8 +45,8 @@ bool ItemComparator::operator()(const ItemRef &lhs, const ItemRef &rhs) const { }, [&](CompareByJoinedField c) { if (joinedNsRes.firstDifferentFieldIdx == kNotComputed) { - const auto &jNs = joined_; - const auto &joinedSelector = *jNs.joinedSelector; + const auto& jNs = joined_; + const auto& joinedSelector = *jNs.joinedSelector; const joins::ItemIterator ljIt{joinResults_, lhs.Id()}; const joins::ItemIterator rjIt{joinResults_, rhs.Id()}; const auto ljfIt = ljIt.at(c.joinedNs); @@ -71,45 +71,47 @@ bool ItemComparator::operator()(const ItemRef &lhs, const ItemRef &rhs) const { return mainNsRes.GetResult(c.desc); }}, comp); - if (res != ComparationResult::Eq) return res == ComparationResult::Lt; + if (res != ComparationResult::Eq) { + return res == ComparationResult::Lt; + } } // If values are equal, then sort by row ID, to give consistent results - return std::visit([&](const auto &e) noexcept { return e.data.desc ? lhs.Id() > rhs.Id() : lhs.Id() < rhs.Id(); }, + return std::visit([&](const auto& e) noexcept { return e.data.desc ? lhs.Id() > rhs.Id() : lhs.Id() < rhs.Id(); }, ctx_.sortingContext.entries[0]); } class ItemComparator::BackInserter { public: - explicit BackInserter(ItemComparator &comparator) noexcept : comparator_(comparator) {} + explicit BackInserter(ItemComparator& comparator) noexcept : comparator_(comparator) {} void expr(bool desc) { comparator_.comparators_.emplace_back(CompareByExpression{desc}); } - void fields(TagsPath &&tp) { comparator_.fields_.push_back(std::move(tp)); } - void fields(Joined &joined, TagsPath &&tp) { joined.fields.push_back(std::move(tp)); } + void fields(TagsPath&& tp) { comparator_.fields_.push_back(std::move(tp)); } + void fields(Joined& joined, TagsPath&& tp) { joined.fields.push_back(std::move(tp)); } void fields(int fieldIdx) { if (fieldIdx != SetByJsonPath && !comparator_.fields_.contains(fieldIdx)) { comparator_.fields_.push_back(fieldIdx); - auto &rawDataRef = comparator_.rawData_.emplace_back(); + auto& rawDataRef = comparator_.rawData_.emplace_back(); if (auto rawData = comparator_.ns_.indexes_[fieldIdx]->ColumnData(); rawData) { rawDataRef.ptr = rawData; rawDataRef.type = comparator_.ns_.payloadType_.Field(fieldIdx).Type(); } } } - void fields(Joined &joined, int fieldIdx) { joined.fields.push_back(fieldIdx); } + void fields(Joined& joined, int fieldIdx) { joined.fields.push_back(fieldIdx); } void index(bool desc) { comparator_.comparators_.emplace_back(CompareByField{desc}); } void joined(size_t nsIdx, bool desc) { comparator_.comparators_.emplace_back(CompareByJoinedField{nsIdx, desc}); } - void collateOpts(const CollateOpts *opts) { comparator_.collateOpts_.emplace_back(opts); } - void collateOpts(Joined &joined, const CollateOpts *opts) { joined.collateOpts.emplace_back(opts); } + void collateOpts(const CollateOpts* opts) { comparator_.collateOpts_.emplace_back(opts); } + void collateOpts(Joined& joined, const CollateOpts* opts) { joined.collateOpts.emplace_back(opts); } private: - ItemComparator &comparator_; + ItemComparator& comparator_; }; class ItemComparator::FrontInserter { public: - FrontInserter(ItemComparator &comparator) noexcept : comparator_(comparator) {} + FrontInserter(ItemComparator& comparator) noexcept : comparator_(comparator) {} void expr(bool desc) { comparator_.comparators_.emplace(comparator_.comparators_.begin(), CompareByExpression{desc}); } - void fields(TagsPath &&tp) { comparator_.fields_.push_front(std::move(tp)); } - void fields(Joined &joined, TagsPath &&tp) { joined.fields.push_front(std::move(tp)); } + void fields(TagsPath&& tp) { comparator_.fields_.push_front(std::move(tp)); } + void fields(Joined& joined, TagsPath&& tp) { joined.fields.push_front(std::move(tp)); } void fields(int fieldIdx) { if (fieldIdx != SetByJsonPath && !comparator_.fields_.contains(fieldIdx)) { comparator_.fields_.push_front(fieldIdx); @@ -120,23 +122,23 @@ class ItemComparator::FrontInserter { } } } - void fields(Joined &joined, int fieldIdx) { joined.fields.push_front(fieldIdx); } + void fields(Joined& joined, int fieldIdx) { joined.fields.push_front(fieldIdx); } void index(bool desc) { comparator_.comparators_.emplace(comparator_.comparators_.begin(), CompareByField{desc}); } void joined(size_t nsIdx, bool desc) { comparator_.comparators_.emplace(comparator_.comparators_.begin(), CompareByJoinedField{nsIdx, desc}); } - void collateOpts(const CollateOpts *opts) { comparator_.collateOpts_.emplace(comparator_.collateOpts_.begin(), opts); } - void collateOpts(Joined &joined, const CollateOpts *opts) { joined.collateOpts.emplace(joined.collateOpts.begin(), opts); } + void collateOpts(const CollateOpts* opts) { comparator_.collateOpts_.emplace(comparator_.collateOpts_.begin(), opts); } + void collateOpts(Joined& joined, const CollateOpts* opts) { joined.collateOpts.emplace(joined.collateOpts.begin(), opts); } private: - ItemComparator &comparator_; + ItemComparator& comparator_; }; template -void ItemComparator::bindOne(const SortingContext::Entry &sortingEntry, Inserter insert, bool multiSort) { - std::visit(overloaded{[&](const SortingContext::ExpressionEntry &e) { insert.expr(e.data.desc); }, - [&](const SortingContext::JoinedFieldEntry &e) { - auto &jns = joined_; +void ItemComparator::bindOne(const SortingContext::Entry& sortingEntry, Inserter insert, bool multiSort) { + std::visit(overloaded{[&](const SortingContext::ExpressionEntry& e) { insert.expr(e.data.desc); }, + [&](const SortingContext::JoinedFieldEntry& e) { + auto& jns = joined_; if (jns.joinedSelector == nullptr) { assertrx_throw(ctx_.joinedSelectors); assertrx_throw(ctx_.joinedSelectors->size() > e.nsIdx); @@ -145,12 +147,12 @@ void ItemComparator::bindOne(const SortingContext::Entry &sortingEntry, Inserter assertrx_dbg(&(*ctx_.joinedSelectors)[e.nsIdx] == jns.joinedSelector); } assertrx_dbg(!std::holds_alternative(jns.joinedSelector->PreResult().payload)); - const auto &ns = *jns.joinedSelector->RightNs(); + const auto& ns = *jns.joinedSelector->RightNs(); const int fieldIdx = e.index; if (fieldIdx == IndexValueType::SetByJsonPath || ns.indexes_[fieldIdx]->Opts().IsSparse()) { TagsPath tagsPath; if (fieldIdx != IndexValueType::SetByJsonPath) { - const FieldsSet &fs = ns.indexes_[fieldIdx]->Fields(); + const FieldsSet& fs = ns.indexes_[fieldIdx]->Fields(); assertrx_throw(fs.getTagsPathsLength() > 0); tagsPath = fs.getTagsPath(0); } else { @@ -167,7 +169,7 @@ void ItemComparator::bindOne(const SortingContext::Entry &sortingEntry, Inserter insert.collateOpts(jns, nullptr); } } else { - const auto &idx = *ns.indexes_[fieldIdx]; + const auto& idx = *ns.indexes_[fieldIdx]; if (idx.Opts().IsArray()) { throw Error(errQueryExec, "Sorting cannot be applied to array field."); } @@ -192,12 +194,12 @@ void ItemComparator::bindOne(const SortingContext::Entry &sortingEntry, Inserter insert.collateOpts(jns, &idx.Opts().collateOpts_); } }, - [&](const SortingContext::FieldEntry &e) { + [&](const SortingContext::FieldEntry& e) { const int fieldIdx = e.data.index; if (fieldIdx == IndexValueType::SetByJsonPath || ns_.indexes_[fieldIdx]->Opts().IsSparse()) { TagsPath tagsPath; if (fieldIdx != IndexValueType::SetByJsonPath) { - const FieldsSet &fs = ns_.indexes_[fieldIdx]->Fields(); + const FieldsSet& fs = ns_.indexes_[fieldIdx]->Fields(); assertrx_throw(fs.getTagsPathsLength() > 0); tagsPath = fs.getTagsPath(0); } else { @@ -224,7 +226,7 @@ void ItemComparator::bindOne(const SortingContext::Entry &sortingEntry, Inserter const auto field(fields_[i]); assertrx_dbg(field != SetByJsonPath); comparators_.emplace_back(CompareByField{e.data.desc}); - auto &rawDataRef = rawData_.emplace_back(); + auto& rawDataRef = rawData_.emplace_back(); if (auto rawData = ns_.indexes_[field]->ColumnData(); rawData) { rawDataRef.ptr = rawData; rawDataRef.type = ns_.payloadType_.Field(field).Type(); @@ -244,8 +246,8 @@ void ItemComparator::bindOne(const SortingContext::Entry &sortingEntry, Inserter } void ItemComparator::BindForForcedSort() { - const auto &entries = ctx_.sortingContext.entries; - [[maybe_unused]] const auto &exprResults = ctx_.sortingContext.exprResults; + const auto& entries = ctx_.sortingContext.entries; + [[maybe_unused]] const auto& exprResults = ctx_.sortingContext.exprResults; assertrx_throw(entries.size() >= exprResults.size()); comparators_.reserve(entries.size()); const bool multiSort = entries.size() > 1; @@ -255,13 +257,13 @@ void ItemComparator::BindForForcedSort() { } void ItemComparator::BindForGeneralSort() { - const auto &entries = ctx_.sortingContext.entries; - [[maybe_unused]] const auto &exprResults = ctx_.sortingContext.exprResults; + const auto& entries = ctx_.sortingContext.entries; + [[maybe_unused]] const auto& exprResults = ctx_.sortingContext.exprResults; assertrx_throw(entries.size() >= exprResults.size()); const bool multiSort = entries.size() > 1; if (comparators_.empty()) { comparators_.reserve(entries.size()); - for (const auto &e : entries) { + for (const auto& e : entries) { bindOne(e, BackInserter{*this}, multiSort); } } else if (!entries.empty()) { @@ -269,42 +271,42 @@ void ItemComparator::BindForGeneralSort() { } } -ComparationResult ItemComparator::compareFields(IdType lId, IdType rId, size_t &firstDifferentFieldIdx) const { +ComparationResult ItemComparator::compareFields(IdType lId, IdType rId, size_t& firstDifferentFieldIdx) const { const bool commonOpts = (collateOpts_.size() == 1); size_t tagPathIdx = 0; size_t rawDataIdx = 0; for (size_t i = 0, sz = fields_.size(); i < sz; ++i) { - const CollateOpts *opts(commonOpts ? collateOpts_[0] : collateOpts_[i]); + const CollateOpts* opts(commonOpts ? collateOpts_[0] : collateOpts_[i]); ComparationResult cmpRes; const auto field(fields_[i]); if (field != SetByJsonPath && rawData_[rawDataIdx].ptr) { - const auto &rd = rawData_[rawDataIdx]; + const auto& rd = rawData_[rawDataIdx]; ++rawDataIdx; const auto rawData = rd.ptr; auto values = rd.type.EvaluateOneOf( [rawData, lId, rId](KeyValueType::Bool) noexcept { - return std::make_pair(Variant(*(static_cast(rawData) + lId)), - Variant(*(static_cast(rawData) + rId))); + return std::make_pair(Variant(*(static_cast(rawData) + lId)), + Variant(*(static_cast(rawData) + rId))); }, [rawData, lId, rId](KeyValueType::Int) noexcept { - return std::make_pair(Variant(*(static_cast(rawData) + lId)), - Variant(*(static_cast(rawData) + rId))); + return std::make_pair(Variant(*(static_cast(rawData) + lId)), + Variant(*(static_cast(rawData) + rId))); }, [rawData, lId, rId](KeyValueType::Int64) noexcept { - return std::make_pair(Variant(*(static_cast(rawData) + lId)), - Variant(*(static_cast(rawData) + rId))); + return std::make_pair(Variant(*(static_cast(rawData) + lId)), + Variant(*(static_cast(rawData) + rId))); }, [rawData, lId, rId](KeyValueType::Double) noexcept { - return std::make_pair(Variant(*(static_cast(rawData) + lId)), - Variant(*(static_cast(rawData) + rId))); + return std::make_pair(Variant(*(static_cast(rawData) + lId)), + Variant(*(static_cast(rawData) + rId))); }, [rawData, lId, rId](KeyValueType::String) noexcept { - return std::make_pair(Variant(p_string(static_cast(rawData) + lId), Variant::no_hold_t{}), - Variant(p_string(static_cast(rawData) + rId), Variant::no_hold_t{})); + return std::make_pair(Variant(p_string(static_cast(rawData) + lId), Variant::no_hold_t{}), + Variant(p_string(static_cast(rawData) + rId), Variant::no_hold_t{})); }, [rawData, lId, rId](KeyValueType::Uuid) noexcept { - return std::make_pair(Variant(*(static_cast(rawData) + lId)), - Variant(*(static_cast(rawData) + rId))); + return std::make_pair(Variant(*(static_cast(rawData) + lId)), + Variant(*(static_cast(rawData) + rId))); }, [](OneOf) -> std::pair { throw_as_assert; }); diff --git a/cpp_src/core/nsselecter/itemcomparator.h b/cpp_src/core/nsselecter/itemcomparator.h index 0489e097e..b51a22447 100644 --- a/cpp_src/core/nsselecter/itemcomparator.h +++ b/cpp_src/core/nsselecter/itemcomparator.h @@ -11,22 +11,22 @@ class ItemRef; class ItemComparator { public: - ItemComparator(const NamespaceImpl &ns, const SelectCtx &ctx, const joins::NamespaceResults *jr) noexcept + ItemComparator(const NamespaceImpl& ns, const SelectCtx& ctx, const joins::NamespaceResults* jr) noexcept : ns_(ns), ctx_(ctx), joinResults_(jr) {} - ItemComparator(const ItemComparator &) = delete; - ItemComparator(ItemComparator &&) = delete; - ItemComparator &operator=(const ItemComparator &) = delete; - ItemComparator &operator=(ItemComparator &&) = delete; + ItemComparator(const ItemComparator&) = delete; + ItemComparator(ItemComparator&&) = delete; + ItemComparator& operator=(const ItemComparator&) = delete; + ItemComparator& operator=(ItemComparator&&) = delete; - [[nodiscard]] bool operator()(const ItemRef &lhs, const ItemRef &rhs) const; + [[nodiscard]] bool operator()(const ItemRef& lhs, const ItemRef& rhs) const; void BindForForcedSort(); void BindForGeneralSort(); private: template - void bindOne(const SortingContext::Entry &sortingCtx, Inserter insert, bool multiSort); - ComparationResult compareFields(IdType lId, IdType rId, size_t &firstDifferentFieldIdx) const; + void bindOne(const SortingContext::Entry& sortingCtx, Inserter insert, bool multiSort); + ComparationResult compareFields(IdType lId, IdType rId, size_t& firstDifferentFieldIdx) const; class BackInserter; class FrontInserter; @@ -41,18 +41,18 @@ class ItemComparator { bool desc; }; struct Joined { - const JoinedSelector *joinedSelector{nullptr}; + const JoinedSelector* joinedSelector{nullptr}; FieldsSet fields; - h_vector collateOpts; + h_vector collateOpts; }; - const NamespaceImpl &ns_; - const SelectCtx &ctx_; - const joins::NamespaceResults *joinResults_; + const NamespaceImpl& ns_; + const SelectCtx& ctx_; + const joins::NamespaceResults* joinResults_; FieldsSet fields_; h_vector rawData_; Joined joined_; - h_vector collateOpts_; + h_vector collateOpts_; h_vector, 4> comparators_; }; diff --git a/cpp_src/core/nsselecter/joinedselector.cc b/cpp_src/core/nsselecter/joinedselector.cc index 9f0d433b4..c92900671 100644 --- a/cpp_src/core/nsselecter/joinedselector.cc +++ b/cpp_src/core/nsselecter/joinedselector.cc @@ -10,7 +10,7 @@ constexpr size_t kMaxIterationsScaleForInnerJoinOptimization = 100; namespace reindexer { -void JoinedSelector::selectFromRightNs(QueryResults &joinItemR, const Query &query, bool &found, bool &matchedAtLeastOnce) { +void JoinedSelector::selectFromRightNs(QueryResults& joinItemR, const Query& query, bool& found, bool& matchedAtLeastOnce) { assertrx_dbg(rightNs_); JoinCacheRes joinResLong; @@ -43,23 +43,25 @@ void JoinedSelector::selectFromRightNs(QueryResults &joinItemR, const Query &que JoinCacheVal val; val.ids_ = make_intrusive>(); val.matchedAtLeastOnce = matchedAtLeastOnce; - for (auto &r : joinItemR.Items()) { + for (auto& r : joinItemR.Items()) { val.ids_->Add(r.Id(), IdSet::Unordered, 0); } rightNs_->putToJoinCache(joinResLong, std::move(val)); } } -void JoinedSelector::selectFromPreResultValues(QueryResults &joinItemR, const Query &query, bool &found, bool &matchedAtLeastOnce) const { +void JoinedSelector::selectFromPreResultValues(QueryResults& joinItemR, const Query& query, bool& found, bool& matchedAtLeastOnce) const { size_t matched = 0; - const auto &entries = query.Entries(); - const JoinPreResult::Values &values = std::get(PreResult().payload); - const auto &pt = values.payloadType; - for (const ItemRef &item : values) { - auto &v = item.Value(); + const auto& entries = query.Entries(); + const JoinPreResult::Values& values = std::get(PreResult().payload); + const auto& pt = values.payloadType; + for (const ItemRef& item : values) { + auto& v = item.Value(); assertrx_throw(!v.IsFree()); if (entries.CheckIfSatisfyConditions({pt, v})) { - if (++matched > query.Limit()) break; + if (++matched > query.Limit()) { + break; + } found = true; joinItemR.Add(item); } @@ -77,11 +79,13 @@ bool JoinedSelector::Process(IdType rowId, int nsId, ConstPayload payload, bool const auto startTime = ExplainCalc::Clock::now(); // Put values to join conditions size_t i = 0; - if (itemQuery_.NeedExplain() && !explainOneSelect_.empty()) itemQuery_.Explain(false); + if (itemQuery_.NeedExplain() && !explainOneSelect_.empty()) { + itemQuery_.Explain(false); + } std::unique_ptr itemQueryCopy; - Query *itemQueryPtr = &itemQuery_; - for (auto &je : joinQuery_.joinEntries_) { - QueryEntry &qentry = itemQueryPtr->GetUpdatableEntry(i); + Query* itemQueryPtr = &itemQuery_; + for (auto& je : joinQuery_.joinEntries_) { + QueryEntry& qentry = itemQueryPtr->GetUpdatableEntry(i); { auto keyValues = qentry.UpdatableValues(QueryEntry::IgnoreEmptyValues{}); payload.GetByFieldsSet(je.LeftFields(), keyValues, je.LeftFieldType(), je.LeftCompositeFieldsTypes()); @@ -101,77 +105,80 @@ bool JoinedSelector::Process(IdType rowId, int nsId, ConstPayload payload, bool bool matchedAtLeastOnce = false; QueryResults joinItemR; std::visit( - overloaded{[&](const JoinPreResult::Values &) { selectFromPreResultValues(joinItemR, *itemQueryPtr, found, matchedAtLeastOnce); }, + overloaded{[&](const JoinPreResult::Values&) { selectFromPreResultValues(joinItemR, *itemQueryPtr, found, matchedAtLeastOnce); }, Restricted{}( - [&](const auto &) { selectFromRightNs(joinItemR, *itemQueryPtr, found, matchedAtLeastOnce); })}, + [&](const auto&) { selectFromRightNs(joinItemR, *itemQueryPtr, found, matchedAtLeastOnce); })}, PreResult().payload); if (match && found) { assertrx_throw(nsId < static_cast(result_.joined_.size())); - joins::NamespaceResults &nsJoinRes = result_.joined_[nsId]; + joins::NamespaceResults& nsJoinRes = result_.joined_[nsId]; assertrx_dbg(nsJoinRes.GetJoinedSelectorsCount()); nsJoinRes.Insert(rowId, joinedFieldIdx_, std::move(joinItemR)); } - if (matchedAtLeastOnce) ++matched_; + if (matchedAtLeastOnce) { + ++matched_; + } selectTime_ += (ExplainCalc::Clock::now() - startTime); return matchedAtLeastOnce; } template -VariantArray JoinedSelector::readValuesOfRightNsFrom(const Cont &data, const Fn &createPayload, const QueryJoinEntry &entry, - const PayloadType &pt) const { +VariantArray JoinedSelector::readValuesOfRightNsFrom(const Cont& data, const Fn& createPayload, const QueryJoinEntry& entry, + const PayloadType& pt) const { const auto rightFieldType = entry.RightFieldType(); const auto leftFieldType = entry.LeftFieldType(); VariantArray res; if (rightFieldType.Is()) { unordered_payload_ref_set set(data.size(), hash_composite_ref(pt, entry.RightFields()), equal_composite_ref(pt, entry.RightFields())); - for (const auto &v : data) { + for (const auto& v : data) { const auto pl = createPayload(v); if (!pl.Value()->IsFree()) { set.insert(*pl.Value()); } } res.reserve(set.size()); - for (auto &s : set) { + for (auto& s : set) { res.emplace_back(std::move(s)); } } else { tsl::sparse_set set(data.size()); - for (const auto &val : data) { + for (const auto& val : data) { const auto pl = createPayload(val); if (pl.Value()->IsFree()) { continue; } pl.GetByFieldsSet(entry.RightFields(), res, entry.RightFieldType(), entry.RightCompositeFieldsTypes()); if (!leftFieldType.Is() && !leftFieldType.Is()) { - for (Variant &v : res) set.insert(std::move(v.convert(leftFieldType))); + for (Variant& v : res) { + set.insert(std::move(v.convert(leftFieldType))); + } } else { - for (Variant &v : res) set.insert(std::move(v)); + for (Variant& v : res) { + set.insert(std::move(v)); + } } } res.clear(); - for (auto &s : set) { + for (auto& s : set) { res.emplace_back(std::move(s)); } } return res; } -VariantArray JoinedSelector::readValuesFromPreResult(const QueryJoinEntry &entry) const { - const JoinPreResult::Values &values = std::get(PreResult().payload); +VariantArray JoinedSelector::readValuesFromPreResult(const QueryJoinEntry& entry) const { + const JoinPreResult::Values& values = std::get(PreResult().payload); return readValuesOfRightNsFrom( - values, - [&values](const ItemRef &item) noexcept { - return ConstPayload{values.payloadType, item.Value()}; - }, - entry, values.payloadType); + values, [&values](const ItemRef& item) noexcept { return ConstPayload{values.payloadType, item.Value()}; }, entry, + values.payloadType); } -void JoinedSelector::AppendSelectIteratorOfJoinIndexData(SelectIteratorContainer &iterators, int *maxIterations, unsigned sortId, - const SelectFunction::Ptr &selectFnc, const RdxContext &rdxCtx) { +void JoinedSelector::AppendSelectIteratorOfJoinIndexData(SelectIteratorContainer& iterators, int* maxIterations, unsigned sortId, + const SelectFunction::Ptr& selectFnc, const RdxContext& rdxCtx) { if (joinType_ != JoinType::InnerJoin || preSelectCtx_.Mode() != JoinPreSelectMode::Execute || - std::visit(overloaded{[](const SelectIteratorContainer &) noexcept { return true; }, - Restricted{}([maxIterations](const auto &v) noexcept { + std::visit(overloaded{[](const SelectIteratorContainer&) noexcept { return true; }, + Restricted{}([maxIterations](const auto& v) noexcept { return v.size() > *maxIterations * kMaxIterationsScaleForInnerJoinOptimization; })}, PreResult().payload)) { @@ -181,32 +188,35 @@ void JoinedSelector::AppendSelectIteratorOfJoinIndexData(SelectIteratorContainer assertrx_throw(!std::holds_alternative(PreResult().payload) || itemQuery_.Entries().Size() == joinQuery_.joinEntries_.size()); for (size_t i = 0; i < joinQuery_.joinEntries_.size(); ++i) { - const QueryJoinEntry &joinEntry = joinQuery_.joinEntries_[i]; + const QueryJoinEntry& joinEntry = joinQuery_.joinEntries_[i]; if (!joinEntry.IsLeftFieldIndexed() || joinEntry.Operation() != OpAnd || (joinEntry.Condition() != CondEq && joinEntry.Condition() != CondSet) || (i + 1 < joinQuery_.joinEntries_.size() && joinQuery_.joinEntries_[i + 1].Operation() == OpOr)) { continue; } - const auto &leftIndex = leftNs_->indexes_[joinEntry.LeftIdxNo()]; + const auto& leftIndex = leftNs_->indexes_[joinEntry.LeftIdxNo()]; assertrx_throw(!IsFullText(leftIndex->Type())); - if (leftIndex->Opts().IsSparse()) continue; - - const VariantArray values = std::visit(overloaded{[&](const IdSet &preselected) { - return readValuesOfRightNsFrom( - preselected, - [this](IdType rowId) noexcept { - return ConstPayload{rightNs_->payloadType_, rightNs_->items_[rowId]}; - }, - joinEntry, rightNs_->payloadType_); - }, - [&](const JoinPreResult::Values &) { return readValuesFromPreResult(joinEntry); }, - [](const SelectIteratorContainer &) -> VariantArray { throw_as_assert; }}, - PreResult().payload); + if (leftIndex->Opts().IsSparse()) { + continue; + } + + const VariantArray values = std::visit( + overloaded{[&](const IdSet& preselected) { + return readValuesOfRightNsFrom( + preselected, + [this](IdType rowId) noexcept { return ConstPayload{rightNs_->payloadType_, rightNs_->items_[rowId]}; }, + joinEntry, rightNs_->payloadType_); + }, + [&](const JoinPreResult::Values&) { return readValuesFromPreResult(joinEntry); }, + [](const SelectIteratorContainer&) -> VariantArray { throw_as_assert; }}, + PreResult().payload); auto ctx = selectFnc ? selectFnc->CreateCtx(joinEntry.LeftIdxNo()) : BaseFunctionCtx::Ptr{}; assertrx_throw(!ctx || ctx->type != BaseFunctionCtx::kFtCtx); if (leftIndex->Opts().GetCollateMode() == CollateUTF8) { - for (auto &key : values) key.EnsureUTF8(); + for (auto& key : values) { + key.EnsureUTF8(); + } } Index::SelectOpts opts; opts.maxIterations = iterators.GetMaxIterations(); @@ -214,17 +224,21 @@ void JoinedSelector::AppendSelectIteratorOfJoinIndexData(SelectIteratorContainer opts.inTransaction = inTransaction_; auto selectResults = leftIndex->SelectKey(values, CondSet, sortId, opts, ctx, rdxCtx); - if (auto *selRes = std::get_if(&selectResults)) { + if (auto* selRes = std::get_if(&selectResults)) { bool wasAppended = false; - for (SelectKeyResult &res : *selRes) { + for (SelectKeyResult& res : *selRes) { SelectIterator selIter{std::move(res), false, std::string(joinEntry.LeftFieldName()), (joinEntry.LeftIdxNo() < 0 ? IteratorFieldKind::NonIndexed : IteratorFieldKind::Indexed), false}; const int curIterations = selIter.GetMaxIterations(); - if (curIterations && curIterations < *maxIterations) *maxIterations = curIterations; + if (curIterations && curIterations < *maxIterations) { + *maxIterations = curIterations; + } iterators.Append(OpAnd, std::move(selIter)); wasAppended = true; } - if (wasAppended) ++optimized; + if (wasAppended) { + ++optimized; + } } } optimized_ = optimized == joinQuery_.joinEntries_.size(); diff --git a/cpp_src/core/nsselecter/joinedselector.h b/cpp_src/core/nsselecter/joinedselector.h index e3047e4a0..96fd8553d 100644 --- a/cpp_src/core/nsselecter/joinedselector.h +++ b/cpp_src/core/nsselecter/joinedselector.h @@ -21,8 +21,8 @@ struct PreselectProperties { struct JoinPreResult { class Values : public std::vector { public: - Values(const PayloadType &pt, const TagsMatcher &tm) noexcept : payloadType{pt}, tagsMatcher{tm} {} - Values(Values &&other) noexcept + Values(const PayloadType& pt, const TagsMatcher& tm) noexcept : payloadType{pt}, tagsMatcher{tm} {} + Values(Values&& other) noexcept : std::vector(std::move(other)), payloadType(std::move(other.payloadType)), tagsMatcher(std::move(other.tagsMatcher)), @@ -30,18 +30,22 @@ struct JoinPreResult { other.locked_ = false; } Values() noexcept : locked_(false) {} - Values(const Values &) = delete; - Values &operator=(const Values &) = delete; - Values &operator=(Values &&) = delete; + Values(const Values&) = delete; + Values& operator=(const Values&) = delete; + Values& operator=(Values&&) = delete; ~Values() { if (locked_) { - for (size_t i = 0; i < size(); ++i) Payload{payloadType, (*this)[i].Value()}.ReleaseStrings(); + for (size_t i = 0; i < size(); ++i) { + Payload{payloadType, (*this)[i].Value()}.ReleaseStrings(); + } } } bool Locked() const { return locked_; } void Lock() { assertrx_throw(!locked_); - for (size_t i = 0; i < size(); ++i) Payload{payloadType, (*this)[i].Value()}.AddRefStrings(); + for (size_t i = 0; i < size(); ++i) { + Payload{payloadType, (*this)[i].Value()}.AddRefStrings(); + } locked_ = true; } bool IsPreselectAllowed() const noexcept { return preselectAllowed_; } @@ -71,10 +75,10 @@ enum class JoinPreSelectMode { Empty, Build, Execute, ForInjection, InjectionRej class JoinPreResultBuildCtx { public: explicit JoinPreResultBuildCtx(JoinPreResult::Ptr r) noexcept : result_{std::move(r)} {} - JoinPreResult &Result() & noexcept { return *result_; } + JoinPreResult& Result() & noexcept { return *result_; } JoinPreSelectMode Mode() const noexcept { return JoinPreSelectMode::Build; } - const JoinPreResult::Ptr &ResultPtr() const & noexcept { return result_; } - auto ResultPtr() const && = delete; + const JoinPreResult::Ptr& ResultPtr() const& noexcept { return result_; } + auto ResultPtr() const&& = delete; private: JoinPreResult::Ptr result_; @@ -85,20 +89,20 @@ class JoinPreResultExecuteCtx { explicit JoinPreResultExecuteCtx(JoinPreResult::CPtr r) noexcept : result_{std::move(r)}, mode_{JoinPreSelectMode::Execute} {} explicit JoinPreResultExecuteCtx(JoinPreResult::CPtr r, int maxIters) noexcept : result_{std::move(r)}, mode_{JoinPreSelectMode::ForInjection}, mainQueryMaxIterations_{maxIters} {} - const JoinPreResult &Result() const & noexcept { return *result_; } + const JoinPreResult& Result() const& noexcept { return *result_; } JoinPreSelectMode Mode() const noexcept { return mode_; } int MainQueryMaxIterations() const { assertrx_dbg(mode_ == JoinPreSelectMode::ForInjection); return mainQueryMaxIterations_; } - const JoinPreResult::CPtr &ResultPtr() const & noexcept { return result_; } + const JoinPreResult::CPtr& ResultPtr() const& noexcept { return result_; } void Reject() { assertrx_dbg(mode_ == JoinPreSelectMode::ForInjection); mode_ = JoinPreSelectMode::InjectionRejected; } - auto Result() const && = delete; - auto ResultPtr() const && = delete; + auto Result() const&& = delete; + auto ResultPtr() const&& = delete; private: JoinPreResult::CPtr result_; @@ -120,9 +124,9 @@ class JoinedSelector { friend QueryPreprocessor; public: - JoinedSelector(JoinType joinType, NamespaceImpl::Ptr leftNs, NamespaceImpl::Ptr rightNs, JoinCacheRes &&joinRes, Query &&itemQuery, - QueryResults &result, const JoinedQuery &joinQuery, JoinPreResultExecuteCtx &&preSelCtx, uint32_t joinedFieldIdx, - SelectFunctionsHolder &selectFunctions, bool inTransaction, int64_t lastUpdateTime, const RdxContext &rdxCtx) + JoinedSelector(JoinType joinType, NamespaceImpl::Ptr leftNs, NamespaceImpl::Ptr rightNs, JoinCacheRes&& joinRes, Query&& itemQuery, + QueryResults& result, const JoinedQuery& joinQuery, JoinPreResultExecuteCtx&& preSelCtx, uint32_t joinedFieldIdx, + SelectFunctionsHolder& selectFunctions, bool inTransaction, int64_t lastUpdateTime, const RdxContext& rdxCtx) : joinType_(joinType), called_(0), matched_(0), @@ -140,46 +144,46 @@ class JoinedSelector { inTransaction_{inTransaction}, lastUpdateTime_{lastUpdateTime} { #ifndef NDEBUG - for (const auto &jqe : joinQuery_.joinEntries_) { + for (const auto& jqe : joinQuery_.joinEntries_) { assertrx_throw(jqe.FieldsHaveBeenSet()); } #endif } - JoinedSelector(JoinedSelector &&) = default; - JoinedSelector &operator=(JoinedSelector &&) = delete; - JoinedSelector(const JoinedSelector &) = delete; - JoinedSelector &operator=(const JoinedSelector &) = delete; + JoinedSelector(JoinedSelector&&) = default; + JoinedSelector& operator=(JoinedSelector&&) = delete; + JoinedSelector(const JoinedSelector&) = delete; + JoinedSelector& operator=(const JoinedSelector&) = delete; bool Process(IdType, int nsId, ConstPayload, bool match); JoinType Type() const noexcept { return joinType_; } void SetType(JoinType type) noexcept { joinType_ = type; } - const std::string &RightNsName() const noexcept { return itemQuery_.NsName(); } + const std::string& RightNsName() const noexcept { return itemQuery_.NsName(); } int64_t LastUpdateTime() const noexcept { return lastUpdateTime_; } - const JoinedQuery &JoinQuery() const noexcept { return joinQuery_; } + const JoinedQuery& JoinQuery() const noexcept { return joinQuery_; } int Called() const noexcept { return called_; } int Matched() const noexcept { return matched_; } - void AppendSelectIteratorOfJoinIndexData(SelectIteratorContainer &, int *maxIterations, unsigned sortId, const SelectFunction::Ptr &, - const RdxContext &); + void AppendSelectIteratorOfJoinIndexData(SelectIteratorContainer&, int* maxIterations, unsigned sortId, const SelectFunction::Ptr&, + const RdxContext&); static constexpr int MaxIterationsForPreResultStoreValuesOptimization() noexcept { return 200; } - const JoinPreResult &PreResult() const & noexcept { return preSelectCtx_.Result(); } - const JoinPreResult::CPtr &PreResultPtr() const & noexcept { return preSelectCtx_.ResultPtr(); } + const JoinPreResult& PreResult() const& noexcept { return preSelectCtx_.Result(); } + const JoinPreResult::CPtr& PreResultPtr() const& noexcept { return preSelectCtx_.ResultPtr(); } JoinPreSelectMode PreSelectMode() const noexcept { return preSelectCtx_.Mode(); } - const NamespaceImpl::Ptr &RightNs() const noexcept { return rightNs_; } + const NamespaceImpl::Ptr& RightNs() const noexcept { return rightNs_; } ExplainCalc::Duration SelectTime() const noexcept { return selectTime_; } - const std::string &ExplainOneSelect() const & noexcept { return explainOneSelect_; } + const std::string& ExplainOneSelect() const& noexcept { return explainOneSelect_; } - auto ExplainOneSelect() const && = delete; - auto PreResult() const && = delete; - auto PreResultPtr() const && = delete; + auto ExplainOneSelect() const&& = delete; + auto PreResult() const&& = delete; + auto PreResultPtr() const&& = delete; private: - [[nodiscard]] VariantArray readValuesFromPreResult(const QueryJoinEntry &) const; + [[nodiscard]] VariantArray readValuesFromPreResult(const QueryJoinEntry&) const; template - [[nodiscard]] VariantArray readValuesOfRightNsFrom(const Cont &from, const Fn &createPayload, const QueryJoinEntry &, - const PayloadType &) const; - void selectFromRightNs(QueryResults &joinItemR, const Query &, bool &found, bool &matchedAtLeastOnce); - void selectFromPreResultValues(QueryResults &joinItemR, const Query &, bool &found, bool &matchedAtLeastOnce) const; + [[nodiscard]] VariantArray readValuesOfRightNsFrom(const Cont& from, const Fn& createPayload, const QueryJoinEntry&, + const PayloadType&) const; + void selectFromRightNs(QueryResults& joinItemR, const Query&, bool& found, bool& matchedAtLeastOnce); + void selectFromPreResultValues(QueryResults& joinItemR, const Query&, bool& found, bool& matchedAtLeastOnce) const; JoinType joinType_; int called_, matched_; @@ -187,13 +191,13 @@ class JoinedSelector { NamespaceImpl::Ptr rightNs_; JoinCacheRes joinRes_; Query itemQuery_; - QueryResults &result_; - const JoinedQuery &joinQuery_; + QueryResults& result_; + const JoinedQuery& joinQuery_; JoinPreResultExecuteCtx preSelectCtx_; std::string explainOneSelect_; uint32_t joinedFieldIdx_; - SelectFunctionsHolder &selectFunctions_; - const RdxContext &rdxCtx_; + SelectFunctionsHolder& selectFunctions_; + const RdxContext& rdxCtx_; bool optimized_ = false; bool inTransaction_ = false; int64_t lastUpdateTime_ = 0; diff --git a/cpp_src/core/nsselecter/nsselecter.cc b/cpp_src/core/nsselecter/nsselecter.cc index 7eeb3f40b..4f4316898 100644 --- a/cpp_src/core/nsselecter/nsselecter.cc +++ b/cpp_src/core/nsselecter/nsselecter.cc @@ -20,14 +20,14 @@ constexpr int kCancelCheckFrequency = 1024; namespace reindexer { template -void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect &ctx, const RdxContext &rdxCtx) { +void NsSelecter::operator()(QueryResults& result, SelectCtxWithJoinPreSelect& ctx, const RdxContext& rdxCtx) { // const std::string sql = ctx.query.GetSQL(); // std::cout << sql << std::endl; const size_t resultInitSize = result.Count(); ctx.sortingContext.enableSortOrders = ns_->SortOrdersBuilt(); const LogLevel logLevel = std::max(ns_->config_.logLevel, LogLevel(ctx.query.GetDebugLevel())); - auto &explain = ctx.explain; + auto& explain = ctx.explain; explain = ExplainCalc(ctx.query.NeedExplain() || logLevel >= LogInfo); explain.SetSubQueriesExplains(std::move(ctx.subQueriesExplains)); ActiveQueryScope queryScope(ctx, ns_->optimizationState_, explain, ns_->locker_.IsReadOnly(), ns_->strHolder_.get()); @@ -35,11 +35,11 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect) { assertrx_throw(ctx.preSelect.Mode() == JoinPreSelectMode::Execute || ctx.preSelect.Mode() == JoinPreSelectMode::ForInjection); - std::visit(overloaded{[&](const IdSet &ids) { + std::visit(overloaded{[&](const IdSet& ids) { SelectKeyResult res; res.emplace_back(ids); // Iterator Field Kind: Preselect IdSet -> None qres.Append(OpAnd, SelectIterator(std::move(res), false, "-preresult", IteratorFieldKind::None)); }, - [&](const SelectIteratorContainer &iterators) { + [&](const SelectIteratorContainer& iterators) { if (ctx.preSelect.Mode() == JoinPreSelectMode::Execute) { qres.Append(iterators.begin(), iterators.end()); } }, - [](const JoinPreResult::Values &) { throw_as_assert; }}, + [](const JoinPreResult::Values&) { throw_as_assert; }}, ctx.preSelect.Result().payload); } @@ -198,9 +198,9 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect) { // Building pre-result for next joins - auto &preResult = ctx.preSelect.Result(); + auto& preResult = ctx.preSelect.Result(); preResult.properties.emplace(qres.GetMaxIterations(true), ns_->config().maxIterationsIdSetPreResult); - auto &preselectProps = preResult.properties.value(); + auto& preselectProps = preResult.properties.value(); assertrx_throw(preselectProps.maxIterationsIdSetPreResult > JoinedSelector::MaxIterationsForPreResultStoreValuesOptimization()); if ((preResult.storedValuesOptStatus == StoredValuesOptimizationStatus::Enabled) && preselectProps.qresMaxIteratios <= JoinedSelector::MaxIterationsForPreResultStoreValuesOptimization()) { @@ -225,7 +225,9 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect(); // For building join pre-result always use ASC sort orders - for (SortingEntry &se : sortBy) se.desc = false; + for (SortingEntry& se : sortBy) { + se.desc = false; + } } } } // pre-select rejected @@ -242,9 +244,9 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect= size || qres.GetOperation(qres.Next(i)) != OpOr)) { - const JoinSelectIterator &jIter = qres.Get(i); + const JoinSelectIterator& jIter = qres.Get(i); assertrx_throw(ctx.joinedSelectors && ctx.joinedSelectors->size() > jIter.joinIndex); - JoinedSelector &js = (*ctx.joinedSelectors)[jIter.joinIndex]; + JoinedSelector& js = (*ctx.joinedSelectors)[jIter.joinIndex]; js.AppendSelectIteratorOfJoinIndexData(qres, &maxIterations, ctx.sortingContext.sortId(), fnc_, rdxCtx); } } @@ -252,7 +254,7 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelectitems_.size(); if (ctx.sortingContext.isIndexOrdered() && ctx.sortingContext.enableSortOrders) { - const Index *index = ctx.sortingContext.sortIndex(); + const Index* index = ctx.sortingContext.sortIndex(); assertrx_throw(index); limit = index->SortOrders().size(); } @@ -294,8 +296,8 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect{}, Restricted>{}( - [](auto &comp) { comp.ClearDistinctValues(); }), - [reverse, maxIterations](SelectIterator &it) { it.Start(reverse, maxIterations); }); + [](auto& comp) { comp.ClearDistinctValues(); }), + [reverse, maxIterations](SelectIterator& it) { it.Start(reverse, maxIterations); }); // Let iterators choose most efficient algorithm assertrx_throw(qres.Size()); @@ -356,24 +358,28 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect) { - std::visit(overloaded{[this](JoinPreResult::Values &values) { - for (auto &iref : values) { - if (!iref.ValueInitialized()) iref.SetValue(ns_->items_[iref.Id()]); + std::visit(overloaded{[this](JoinPreResult::Values& values) { + for (auto& iref : values) { + if (!iref.ValueInitialized()) { + iref.SetValue(ns_->items_[iref.Id()]); + } } }, - Restricted{}([](const auto &) {})}, + Restricted{}([](const auto&) {})}, ctx.preSelect.Result().payload); } else { for (size_t i = resultInitSize; i < result.Items().size(); ++i) { - auto &iref = result.Items()[i]; - if (!iref.ValueInitialized()) iref.SetValue(ns_->items_[iref.Id()]); + auto& iref = result.Items()[i]; + if (!iref.ValueInitialized()) { + iref.SetValue(ns_->items_[iref.Id()]); + } } } } if (rx_unlikely(ctx.isMergeQuerySubQuery())) { writeAggregationResultMergeSubQuery(result, aggregators, ctx); } else { - for (auto &aggregator : aggregators) { + for (auto& aggregator : aggregators) { result.aggregationResults.push_back(aggregator.GetResult()); } } @@ -384,7 +390,7 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelectName() : "-"sv); if constexpr (std::is_same_v) { - explain.PutCount(std::visit(overloaded{[](const IdSet &ids) noexcept -> size_t { return ids.size(); }, - [](const JoinPreResult::Values &values) noexcept { return values.size(); }, - [](const SelectIteratorContainer &) -> size_t { throw_as_assert; }}, + explain.PutCount(std::visit(overloaded{[](const IdSet& ids) noexcept -> size_t { return ids.size(); }, + [](const JoinPreResult::Values& values) noexcept { return values.size(); }, + [](const SelectIteratorContainer&) -> size_t { throw_as_assert; }}, ctx.preSelect.Result().payload)); } else { explain.PutCount(result.Count()); @@ -431,60 +437,60 @@ void NsSelecter::operator()(QueryResults &result, SelectCtxWithJoinPreSelect) { if rx_unlikely (logLevel >= LogTrace) { - std::visit(overloaded{[&](const IdSet &ids) { + std::visit(overloaded{[&](const IdSet& ids) { logPrintf(LogInfo, "Built idset preResult (expected %d iterations) with %d ids, q = '%s'", explain.Iterations(), ids.size(), ctx.query.GetSQL()); }, - [&](const JoinPreResult::Values &values) { + [&](const JoinPreResult::Values& values) { logPrintf(LogInfo, "Built values preResult (expected %d iterations) with %d values, q = '%s'", explain.Iterations(), values.size(), ctx.query.GetSQL()); }, - [](const SelectIteratorContainer &) { throw_as_assert; }}, + [](const SelectIteratorContainer&) { throw_as_assert; }}, ctx.preSelect.Result().payload); } } } template -const PayloadValue &getValue(const ItemRef &itemRef, const std::vector &items); +const PayloadValue& getValue(const ItemRef& itemRef, const std::vector& items); template <> -const PayloadValue &getValue(const ItemRef &itemRef, const std::vector &items) { +const PayloadValue& getValue(const ItemRef& itemRef, const std::vector& items) { return items[itemRef.Id()]; } template <> -const PayloadValue &getValue(const ItemRef &itemRef, const std::vector &) { +const PayloadValue& getValue(const ItemRef& itemRef, const std::vector&) { return itemRef.Value(); } template <> class NsSelecter::MainNsValueGetter { public: - explicit MainNsValueGetter(const NamespaceImpl &ns) noexcept : ns_{ns} {} - const PayloadValue &Value(const ItemRef &itemRef) const noexcept { return ns_.items_[itemRef.Id()]; } - ConstPayload Payload(const ItemRef &itemRef) const noexcept { return ConstPayload{ns_.payloadType_, Value(itemRef)}; } + explicit MainNsValueGetter(const NamespaceImpl& ns) noexcept : ns_{ns} {} + const PayloadValue& Value(const ItemRef& itemRef) const noexcept { return ns_.items_[itemRef.Id()]; } + ConstPayload Payload(const ItemRef& itemRef) const noexcept { return ConstPayload{ns_.payloadType_, Value(itemRef)}; } private: - const NamespaceImpl &ns_; + const NamespaceImpl& ns_; }; template <> class NsSelecter::MainNsValueGetter { public: - explicit MainNsValueGetter(const NamespaceImpl &ns) noexcept : ns_{ns} {} - const PayloadValue &Value(const ItemRef &itemRef) const noexcept { return itemRef.Value(); } - ConstPayload Payload(const ItemRef &itemRef) const noexcept { return ConstPayload{ns_.payloadType_, Value(itemRef)}; } + explicit MainNsValueGetter(const NamespaceImpl& ns) noexcept : ns_{ns} {} + const PayloadValue& Value(const ItemRef& itemRef) const noexcept { return itemRef.Value(); } + ConstPayload Payload(const ItemRef& itemRef) const noexcept { return ConstPayload{ns_.payloadType_, Value(itemRef)}; } private: - const NamespaceImpl &ns_; + const NamespaceImpl& ns_; }; class NsSelecter::JoinedNsValueGetter { public: - JoinedNsValueGetter(const NamespaceImpl &ns, const joins::NamespaceResults &jr, size_t nsIdx) noexcept + JoinedNsValueGetter(const NamespaceImpl& ns, const joins::NamespaceResults& jr, size_t nsIdx) noexcept : ns_{ns}, joinedResults_{jr}, nsIdx_{nsIdx} {} - const PayloadValue &Value(const ItemRef &itemRef) const { + const PayloadValue& Value(const ItemRef& itemRef) const { const joins::ItemIterator it{&joinedResults_, itemRef.Id()}; const auto jfIt = it.at(nsIdx_); if (jfIt == it.end() || jfIt.ItemsCount() == 0) { @@ -495,38 +501,39 @@ class NsSelecter::JoinedNsValueGetter { } return jfIt[0].Value(); } - ConstPayload Payload(const ItemRef &itemRef) const noexcept { return ConstPayload{ns_.payloadType_, Value(itemRef)}; } + ConstPayload Payload(const ItemRef& itemRef) const noexcept { return ConstPayload{ns_.payloadType_, Value(itemRef)}; } private: - const NamespaceImpl &ns_; - const joins::NamespaceResults &joinedResults_; + const NamespaceImpl& ns_; + const joins::NamespaceResults& joinedResults_; const size_t nsIdx_; }; template -It NsSelecter::applyForcedSort(It begin, It end, const ItemComparator &compare, const SelectCtx &ctx, const joins::NamespaceResults *jr) { +It NsSelecter::applyForcedSort(It begin, It end, const ItemComparator& compare, const SelectCtx& ctx, const joins::NamespaceResults* jr) { assertrx_throw(!ctx.sortingContext.entries.empty()); - if (ctx.query.GetMergeQueries().size() > 1) throw Error(errLogic, "Force sort could not be applied to 'merged' queries."); - return std::visit(overloaded{ - [](const SortingContext::ExpressionEntry &) -> It { - throw Error(errLogic, "Force sort could not be performed by expression."); - }, - [&](const SortingContext::FieldEntry &e) { - return applyForcedSortImpl(*ns_, begin, end, compare, ctx.query.forcedSortOrder_, - e.data.expression, MainNsValueGetter{*ns_}); - }, - [&](const SortingContext::JoinedFieldEntry &e) { - assertrx_throw(ctx.joinedSelectors); - assertrx_throw(ctx.joinedSelectors->size() >= e.nsIdx); - assertrx_throw(jr); - const auto &joinedSelector = (*ctx.joinedSelectors)[e.nsIdx]; - return applyForcedSortImpl( - *joinedSelector.RightNs(), begin, end, compare, ctx.query.forcedSortOrder_, std::string{e.field}, - JoinedNsValueGetter{*joinedSelector.RightNs(), *jr, e.nsIdx}); - }, - - }, - ctx.sortingContext.entries[0]); + if (ctx.query.GetMergeQueries().size() > 1) { + throw Error(errLogic, "Force sort could not be applied to 'merged' queries."); + } + return std::visit( + overloaded{ + [](const SortingContext::ExpressionEntry&) -> It { throw Error(errLogic, "Force sort could not be performed by expression."); }, + [&](const SortingContext::FieldEntry& e) { + return applyForcedSortImpl(*ns_, begin, end, compare, ctx.query.forcedSortOrder_, + e.data.expression, MainNsValueGetter{*ns_}); + }, + [&](const SortingContext::JoinedFieldEntry& e) { + assertrx_throw(ctx.joinedSelectors); + assertrx_throw(ctx.joinedSelectors->size() >= e.nsIdx); + assertrx_throw(jr); + const auto& joinedSelector = (*ctx.joinedSelectors)[e.nsIdx]; + return applyForcedSortImpl(*joinedSelector.RightNs(), begin, end, compare, + ctx.query.forcedSortOrder_, std::string{e.field}, + JoinedNsValueGetter{*joinedSelector.RightNs(), *jr, e.nsIdx}); + }, + + }, + ctx.sortingContext.entries[0]); } class ForcedSortMap { @@ -545,15 +552,15 @@ class ForcedSortMap { public: using Base::Base; - const auto *operator->() const { + const auto* operator->() const { return std::visit(overloaded{[](MultiMap::Iterator it) { return it.operator->(); }, [](SingleTypeMap::const_iterator it) { return it.operator->(); }}, - static_cast(*this)); + static_cast(*this)); } - const auto &operator*() const { - return std::visit(overloaded{[](MultiMap::Iterator it) -> const auto & { return *it; }, - [](SingleTypeMap::const_iterator it) -> const auto & { return *it; }}, - static_cast(*this)); + const auto& operator*() const { + return std::visit(overloaded{[](MultiMap::Iterator it) -> const auto& { return *it; }, + [](SingleTypeMap::const_iterator it) -> const auto& { return *it; }}, + static_cast(*this)); } }; @@ -562,15 +569,15 @@ class ForcedSortMap { : data_{k.Type().Is() || k.Type().Is() || k.Type().IsNumeric() ? DataType{MultiMap{size}} : DataType{SingleTypeMap{{}, k.Type()}}} { - std::visit(overloaded{[&](MultiMap &m) { m.emplace(std::move(k), v); }, [&](SingleTypeMap &m) { m.emplace(std::move(k), v); }}, + std::visit(overloaded{[&](MultiMap& m) { m.emplace(std::move(k), v); }, [&](SingleTypeMap& m) { m.emplace(std::move(k), v); }}, data_); } std::pair emplace(Variant k, mapped_type v) & { - return std::visit(overloaded{[&](MultiMap &m) { + return std::visit(overloaded{[&](MultiMap& m) { const auto [iter, success] = m.emplace(std::move(k), v); return std::make_pair(Iterator{iter}, success); }, - [&](SingleTypeMap &m) { + [&](SingleTypeMap& m) { if (!m.type_.IsSame(k.Type())) { throw Error{errQueryExec, "Items of different types in forced sort list"}; } @@ -579,9 +586,9 @@ class ForcedSortMap { }}, data_); } - bool contain(const Variant &k) const { - return std::visit(overloaded{[&k](const MultiMap &m) { return m.find(k) != m.cend(); }, - [&k](const SingleTypeMap &m) { + bool contain(const Variant& k) const { + return std::visit(overloaded{[&k](const MultiMap& m) { return m.find(k) != m.cend(); }, + [&k](const SingleTypeMap& m) { if (!m.type_.IsSame(k.Type())) { throw Error{errQueryExec, "Items of different types in forced sort list"}; } @@ -589,13 +596,13 @@ class ForcedSortMap { }}, data_); } - mapped_type get(const Variant &k) const { - return std::visit(overloaded{[&k](const MultiMap &m) { + mapped_type get(const Variant& k) const { + return std::visit(overloaded{[&k](const MultiMap& m) { const auto it = m.find(k); assertrx_throw(it != m.cend()); return it->second; }, - [&k](const SingleTypeMap &m) { + [&k](const SingleTypeMap& m) { if (!m.type_.IsSame(k.Type())) { throw Error{errQueryExec, "Items of different types in forced sort list"}; } @@ -613,9 +620,9 @@ class ForcedSortMap { template class ForcedMapInserter { public: - explicit ForcedMapInserter(Map &m) noexcept : map_{m} {} + explicit ForcedMapInserter(Map& m) noexcept : map_{m} {} template - void Insert(V &&value) { + void Insert(V&& value) { if (const auto [iter, success] = map_.emplace(std::forward(value), cost_); success) { ++cost_; } else if (iter->second != cost_ - 1) { @@ -629,28 +636,29 @@ class ForcedMapInserter { } private: - Map &map_; + Map& map_; typename Map::mapped_type cost_ = 1; }; template -It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const ItemComparator &compare, - const std::vector &forcedSortOrder, const std::string &fieldName, - const ValueGetter &valueGetter) { +It NsSelecter::applyForcedSortImpl(NamespaceImpl& ns, It begin, It end, const ItemComparator& compare, + const std::vector& forcedSortOrder, const std::string& fieldName, + const ValueGetter& valueGetter) { if (int idx; ns.getIndexByNameOrJsonPath(fieldName, idx)) { - if (ns.indexes_[idx]->Opts().IsArray()) + if (ns.indexes_[idx]->Opts().IsArray()) { throw Error(errQueryExec, "This type of sorting cannot be applied to a field of array type."); + } const KeyValueType fieldType{ns.indexes_[idx]->KeyType()}; if (idx < ns.indexes_.firstCompositePos()) { // implementation for regular indexes fast_hash_map sortMap; ForcedMapInserter inserter{sortMap}; - for (const auto &value : forcedSortOrder) { + for (const auto& value : forcedSortOrder) { inserter.Insert(value.convert(fieldType)); } VariantArray keyRefs; - const auto boundary = std::stable_partition(begin, end, [&](const ItemRef &itemRef) { + const auto boundary = std::stable_partition(begin, end, [&](const ItemRef& itemRef) { valueGetter.Payload(itemRef).Get(idx, keyRefs); if constexpr (desc) { return keyRefs.empty() || (sortMap.find(keyRefs[0]) == sortMap.end()); @@ -669,7 +677,7 @@ It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const It from = begin; to = boundary; } - std::sort(from, to, [&](const ItemRef &lhs, const ItemRef &rhs) { + std::sort(from, to, [&](const ItemRef& lhs, const ItemRef& rhs) { valueGetter.Payload(lhs).Get(idx, lhsItemValue); assertrx_throw(!lhsItemValue.empty()); const auto lhsIt = sortMap.find(lhsItemValue[0]); @@ -703,16 +711,16 @@ It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const It return boundary; } else { // implementation for composite indexes - const auto &payloadType = ns.payloadType_; - const FieldsSet &fields = ns.indexes_[idx]->Fields(); + const auto& payloadType = ns.payloadType_; + const FieldsSet& fields = ns.indexes_[idx]->Fields(); unordered_payload_map sortMap(0, PayloadType{payloadType}, FieldsSet{fields}); ForcedMapInserter inserter{sortMap}; for (auto value : forcedSortOrder) { value.convert(fieldType, &payloadType, &fields); - inserter.Insert(static_cast(value)); + inserter.Insert(static_cast(value)); } - const auto boundary = std::stable_partition(begin, end, [&](const ItemRef &itemRef) { + const auto boundary = std::stable_partition(begin, end, [&](const ItemRef& itemRef) { if constexpr (desc) { return (sortMap.find(valueGetter.Value(itemRef)) == sortMap.end()); } else { @@ -728,7 +736,7 @@ It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const It from = begin; to = boundary; } - std::sort(from, to, [&](const ItemRef &lhs, const ItemRef &rhs) { + std::sort(from, to, [&](const ItemRef& lhs, const ItemRef& rhs) { const auto lhsPos = sortMap.find(valueGetter.Value(lhs))->second; const auto rhsPos = sortMap.find(valueGetter.Value(rhs))->second; if (lhsPos == rhsPos) { @@ -759,7 +767,7 @@ It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const It } VariantArray keyRefs; - const auto boundary = std::stable_partition(begin, end, [&](const ItemRef &itemRef) { + const auto boundary = std::stable_partition(begin, end, [&](const ItemRef& itemRef) { valueGetter.Payload(itemRef).GetByJsonPath(fieldName, ns.tagsMatcher_, keyRefs, KeyValueType::Undefined{}); if constexpr (desc) { return keyRefs.empty() || !sortMap.contain(keyRefs[0]); @@ -778,7 +786,7 @@ It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const It from = begin; to = boundary; } - std::sort(from, to, [&](const ItemRef &lhs, const ItemRef &rhs) { + std::sort(from, to, [&](const ItemRef& lhs, const ItemRef& rhs) { valueGetter.Payload(lhs).GetByJsonPath(fieldName, ns.tagsMatcher_, lhsItemValue, KeyValueType::Undefined{}); valueGetter.Payload(rhs).GetByJsonPath(fieldName, ns.tagsMatcher_, rhsItemValue, KeyValueType::Undefined{}); @@ -808,7 +816,7 @@ It NsSelecter::applyForcedSortImpl(NamespaceImpl &ns, It begin, It end, const It } template -void NsSelecter::applyGeneralSort(It itFirst, It itLast, It itEnd, const ItemComparator &comparator, const SelectCtx &ctx) { +void NsSelecter::applyGeneralSort(It itFirst, It itLast, It itEnd, const ItemComparator& comparator, const SelectCtx& ctx) { if (ctx.query.GetMergeQueries().size() > 1) { throw Error(errLogic, "Sorting cannot be applied to merged queries."); } @@ -816,7 +824,7 @@ void NsSelecter::applyGeneralSort(It itFirst, It itLast, It itEnd, const ItemCom std::partial_sort(itFirst, itLast, itEnd, std::cref(comparator)); } -void NsSelecter::setLimitAndOffset(ItemRefVector &queryResult, size_t offset, size_t limit) { +void NsSelecter::setLimitAndOffset(ItemRefVector& queryResult, size_t offset, size_t limit) { const unsigned totalRows = queryResult.size(); if (offset > 0) { auto end = offset < totalRows ? queryResult.begin() + offset : queryResult.end(); @@ -827,31 +835,39 @@ void NsSelecter::setLimitAndOffset(ItemRefVector &queryResult, size_t offset, si } } -void NsSelecter::processLeftJoins(QueryResults &qr, SelectCtx &sctx, size_t startPos, const RdxContext &rdxCtx) { - if (!checkIfThereAreLeftJoins(sctx)) return; +void NsSelecter::processLeftJoins(QueryResults& qr, SelectCtx& sctx, size_t startPos, const RdxContext& rdxCtx) { + if (!checkIfThereAreLeftJoins(sctx)) { + return; + } for (size_t i = startPos; i < qr.Count(); ++i) { IdType rowid = qr[i].GetItemRef().Id(); ConstPayload pl(ns_->payloadType_, ns_->items_[rowid]); - for (auto &joinedSelector : *sctx.joinedSelectors) { - if (joinedSelector.Type() == JoinType::LeftJoin) joinedSelector.Process(rowid, sctx.nsid, pl, true); + for (auto& joinedSelector : *sctx.joinedSelectors) { + if (joinedSelector.Type() == JoinType::LeftJoin) { + joinedSelector.Process(rowid, sctx.nsid, pl, true); + } + } + if (!sctx.inTransaction && (i % kCancelCheckFrequency == 0)) { + ThrowOnCancel(rdxCtx); } - if (!sctx.inTransaction && (i % kCancelCheckFrequency == 0)) ThrowOnCancel(rdxCtx); } } -bool NsSelecter::checkIfThereAreLeftJoins(SelectCtx &sctx) const { - if (!sctx.joinedSelectors) return false; +bool NsSelecter::checkIfThereAreLeftJoins(SelectCtx& sctx) const { + if (!sctx.joinedSelectors) { + return false; + } return std::any_of(sctx.joinedSelectors->begin(), sctx.joinedSelectors->end(), - [](const auto &selector) { return selector.Type() == JoinType::LeftJoin; }); + [](const auto& selector) { return selector.Type() == JoinType::LeftJoin; }); } template -void NsSelecter::sortResults(LoopCtx &ctx, It begin, It end, const SortingOptions &sortingOptions, - const joins::NamespaceResults *jr) { - SelectCtx &sctx = ctx.sctx; +void NsSelecter::sortResults(LoopCtx& ctx, It begin, It end, const SortingOptions& sortingOptions, + const joins::NamespaceResults* jr) { + SelectCtx& sctx = ctx.sctx; ctx.explain.StartSort(); #ifdef RX_WITH_STDLIB_DEBUG - for (const auto &eR : sctx.sortingContext.exprResults) { + for (const auto& eR : sctx.sortingContext.exprResults) { assertrx_dbg(eR.size() == end - begin); } #endif // RX_WITH_STDLIB_DEBUG @@ -886,24 +902,24 @@ void NsSelecter::sortResults(LoopCtx &ctx, It begin, It end, c ctx.explain.StopSort(); } -static size_t resultSize(const QueryResults &qr) noexcept { return qr.Count(); } +static size_t resultSize(const QueryResults& qr) noexcept { return qr.Count(); } -static void resultReserve(QueryResults &qr, size_t s) { qr.Items().reserve(s); } -static void resultReserve(FtMergeStatuses &, size_t) {} +static void resultReserve(QueryResults& qr, size_t s) { qr.Items().reserve(s); } +static void resultReserve(FtMergeStatuses&, size_t) {} template -void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, const RdxContext &rdxCtx) { +void NsSelecter::selectLoop(LoopCtx& ctx, ResultsT& result, const RdxContext& rdxCtx) { static constexpr bool kPreprocessingBeforFT = !std::is_same_v; static const JoinedSelectors emptyJoinedSelectors; const auto selectLoopWard = rdxCtx.BeforeSelectLoop(); - SelectCtxWithJoinPreSelect &sctx = ctx.sctx; - const auto &joinedSelectors = sctx.joinedSelectors ? *sctx.joinedSelectors : emptyJoinedSelectors; - SelectIteratorContainer &qres = ctx.qres; + SelectCtxWithJoinPreSelect& sctx = ctx.sctx; + const auto& joinedSelectors = sctx.joinedSelectors ? *sctx.joinedSelectors : emptyJoinedSelectors; + SelectIteratorContainer& qres = ctx.qres; // Is not using during ft preprocessing size_t initCount = 0; if constexpr (!kPreprocessingBeforFT) { if constexpr (!std::is_same_v) { - if (auto *values = std::get_if(&sctx.preSelect.Result().payload); values) { + if (auto* values = std::get_if(&sctx.preSelect.Result().payload); values) { initCount = values->size(); } else { initCount = resultSize(result); @@ -921,7 +937,7 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co if (qres.Size() == 1 && qres.IsSelectIterator(0) && qres.Get(0).size() == 1) { const unsigned reserve = std::min(unsigned(qres.Get(0).GetMaxIterations()), ctx.count); if constexpr (std::is_same_v) { - if (auto *values = std::get_if(&sctx.preSelect.Result().payload); values) { + if (auto* values = std::get_if(&sctx.preSelect.Result().payload); values) { values->reserve(reserve + initCount); } else { resultReserve(result, initCount + reserve); @@ -934,7 +950,7 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co bool finish = (ctx.count == 0) && !sctx.reqMatchedOnceFlag && !ctx.calcTotal && !sctx.matchedAtLeastOnce; SortingOptions sortingOptions(sctx.sortingContext); - const Index *const firstSortIndex = sctx.sortingContext.sortIndexIfOrdered(); + const Index* const firstSortIndex = sctx.sortingContext.sortIndexIfOrdered(); bool multiSortFinished = !(sortingOptions.multiColumnByBtreeIndex && ctx.count > 0); VariantArray prevValues; @@ -942,10 +958,12 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co assertrx_throw(!qres.Empty()); assertrx_throw(qres.IsSelectIterator(0)); - SelectIterator &firstIterator = qres.begin()->Value(); + SelectIterator& firstIterator = qres.begin()->Value(); IdType rowId = firstIterator.Val(); while (firstIterator.Next(rowId) && !finish) { - if ((rowId % kCancelCheckFrequency == 0) && !sctx.inTransaction) ThrowOnCancel(rdxCtx); + if ((rowId % kCancelCheckFrequency == 0) && !sctx.inTransaction) { + ThrowOnCancel(rdxCtx); + } rowId = firstIterator.Val(); IdType properRowId = rowId; @@ -957,8 +975,10 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co } assertrx_throw(static_cast(properRowId) < ns_->items_.size()); - PayloadValue &pv = ns_->items_[properRowId]; - if (pv.IsFree()) continue; + PayloadValue& pv = ns_->items_[properRowId]; + if (pv.IsFree()) { + continue; + } if (qres.Process(pv, &finish, &rowId, properRowId, !ctx.start && ctx.count)) { sctx.matchedAtLeastOnce = true; // Check distinct condition: @@ -966,14 +986,14 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co // be removed qres.VisitForEach( Skip{}, - [rowId](SelectIterator &sit) { + [rowId](SelectIterator& sit) { if (sit.distinct) { sit.ExcludeLastSet(rowId); } }, Restricted>{}( - [&pv, properRowId](auto &comp) { comp.ExcludeDistinctValues(pv, properRowId); })); + [&pv, properRowId](auto& comp) { comp.ExcludeDistinctValues(pv, properRowId); })); if constexpr (!kPreprocessingBeforFT) { uint8_t proc = ft_ctx_ ? ft_ctx_->Proc(firstIterator.Pos()) : 0; if ((ctx.start || (ctx.count == 0)) && sortingOptions.multiColumnByBtreeIndex) { @@ -1009,9 +1029,10 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co } else if (ctx.count) { addSelectResult(proc, rowId, properRowId, sctx, ctx.aggregators, result, ctx.preselectForFt); --ctx.count; - if (!ctx.count && sortingOptions.multiColumn && !multiSortFinished) + if (!ctx.count && sortingOptions.multiColumn && !multiSortFinished) { getSortIndexValue(sctx.sortingContext, properRowId, prevValues, proc, sctx.nsid < result.joined_.size() ? &result.joined_[sctx.nsid] : nullptr, joinedSelectors); + } } if (!ctx.count && !ctx.calcTotal && multiSortFinished) { break; @@ -1075,35 +1096,35 @@ void NsSelecter::selectLoop(LoopCtx &ctx, ResultsT &result, co } } -void NsSelecter::getSortIndexValue(const SortingContext &sortCtx, IdType rowId, VariantArray &value, uint8_t proc, - const joins::NamespaceResults *joinResults, const JoinedSelectors &js) { +void NsSelecter::getSortIndexValue(const SortingContext& sortCtx, IdType rowId, VariantArray& value, uint8_t proc, + const joins::NamespaceResults* joinResults, const JoinedSelectors& js) { std::visit( overloaded{ - [&](const SortingContext::ExpressionEntry &e) { + [&](const SortingContext::ExpressionEntry& e) { assertrx_throw(e.expression < sortCtx.expressions.size()); ConstPayload pv(ns_->payloadType_, ns_->items_[rowId]); value = VariantArray{Variant{sortCtx.expressions[e.expression].Calculate(rowId, pv, joinResults, js, proc, ns_->tagsMatcher_)}}; }, - [&](const SortingContext::JoinedFieldEntry &e) { + [&](const SortingContext::JoinedFieldEntry& e) { assertrx_throw(joinResults); value = SortExpression::GetJoinedFieldValues(rowId, *joinResults, js, e.nsIdx, e.field, e.index); }, - [&](const SortingContext::FieldEntry &e) { + [&](const SortingContext::FieldEntry& e) { if (e.rawData.ptr) { value = VariantArray{e.rawData.type.EvaluateOneOf( - [&e, rowId](KeyValueType::Bool) noexcept { return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, - [&e, rowId](KeyValueType::Int) noexcept { return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, + [&e, rowId](KeyValueType::Bool) noexcept { return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, + [&e, rowId](KeyValueType::Int) noexcept { return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, [&e, rowId](KeyValueType::Int64) noexcept { - return Variant(*(static_cast(e.rawData.ptr) + rowId)); + return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, [&e, rowId](KeyValueType::Double) noexcept { - return Variant(*(static_cast(e.rawData.ptr) + rowId)); + return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, [&e, rowId](KeyValueType::String) noexcept { - return Variant(p_string(static_cast(e.rawData.ptr) + rowId), Variant::no_hold_t{}); + return Variant(p_string(static_cast(e.rawData.ptr) + rowId), Variant::no_hold_t{}); }, - [&e, rowId](KeyValueType::Uuid) noexcept { return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, + [&e, rowId](KeyValueType::Uuid) noexcept { return Variant(*(static_cast(e.rawData.ptr) + rowId)); }, [](OneOf) -> Variant { throw_as_assert; })}; @@ -1119,13 +1140,13 @@ void NsSelecter::getSortIndexValue(const SortingContext &sortCtx, IdType rowId, sortCtx.getFirstColumnEntry()); } -void NsSelecter::calculateSortExpressions(uint8_t proc, IdType rowId, IdType properRowId, SelectCtx &sctx, const QueryResults &result) { +void NsSelecter::calculateSortExpressions(uint8_t proc, IdType rowId, IdType properRowId, SelectCtx& sctx, const QueryResults& result) { static const JoinedSelectors emptyJoinedSelectors; - const auto &exprs = sctx.sortingContext.expressions; - auto &exprResults = sctx.sortingContext.exprResults; + const auto& exprs = sctx.sortingContext.expressions; + auto& exprResults = sctx.sortingContext.exprResults; assertrx_throw(exprs.size() == exprResults.size()); const ConstPayload pv(ns_->payloadType_, ns_->items_[properRowId]); - const auto &joinedSelectors = sctx.joinedSelectors ? *sctx.joinedSelectors : emptyJoinedSelectors; + const auto& joinedSelectors = sctx.joinedSelectors ? *sctx.joinedSelectors : emptyJoinedSelectors; const auto joinedResultPtr = sctx.nsid < result.joined_.size() ? &result.joined_[sctx.nsid] : nullptr; for (size_t i = 0; i < exprs.size(); ++i) { exprResults[i].push_back(exprs[i].Calculate(rowId, pv, joinedResultPtr, joinedSelectors, proc, ns_->tagsMatcher_)); @@ -1133,17 +1154,25 @@ void NsSelecter::calculateSortExpressions(uint8_t proc, IdType rowId, IdType pro } template -void NsSelecter::addSelectResult(uint8_t proc, IdType rowId, IdType properRowId, SelectCtxWithJoinPreSelect &sctx, - h_vector &aggregators, QueryResults &result, bool preselectForFt) { - if (preselectForFt) return; - for (auto &aggregator : aggregators) aggregator.Aggregate(ns_->items_[properRowId]); - if constexpr (aggregationsOnly) return; +void NsSelecter::addSelectResult(uint8_t proc, IdType rowId, IdType properRowId, SelectCtxWithJoinPreSelect& sctx, + h_vector& aggregators, QueryResults& result, bool preselectForFt) { + if (preselectForFt) { + return; + } + for (auto& aggregator : aggregators) { + aggregator.Aggregate(ns_->items_[properRowId]); + } + if constexpr (aggregationsOnly) { + return; + } // Due to how aggregationsOnly is calculated the aggregators here can either be empty or contain only one value with the Distinct type - if (!aggregators.empty() && !aggregators.front().DistinctChanged()) return; + if (!aggregators.empty() && !aggregators.front().DistinctChanged()) { + return; + } if constexpr (std::is_same_v) { - std::visit(overloaded{[rowId](IdSet &ids) { ids.AddUnordered(rowId); }, - [&](JoinPreResult::Values &values) { + std::visit(overloaded{[rowId](IdSet& ids) { ids.AddUnordered(rowId); }, + [&](JoinPreResult::Values& values) { if (!sctx.sortingContext.expressions.empty()) { values.emplace_back(properRowId, sctx.sortingContext.exprResults[0].size(), proc, sctx.nsid); calculateSortExpressions(proc, rowId, properRowId, sctx, result); @@ -1151,7 +1180,7 @@ void NsSelecter::addSelectResult(uint8_t proc, IdType rowId, IdType properRowId, values.emplace_back(properRowId, ns_->items_[properRowId], proc, sctx.nsid); } }, - [](const SelectIteratorContainer &) { throw_as_assert; }}, + [](const SelectIteratorContainer&) { throw_as_assert; }}, sctx.preSelect.Result().payload); } else { if (!sctx.sortingContext.expressions.empty()) { @@ -1170,9 +1199,11 @@ void NsSelecter::addSelectResult(uint8_t proc, IdType rowId, IdType properRowId, } } -void NsSelecter::checkStrictModeAgg(StrictMode strictMode, const std::string &name, const std::string &nsName, - const TagsMatcher &tagsMatcher) const { - if (int index = IndexValueType::SetByJsonPath; ns_->tryGetIndexByName(name, index)) return; +void NsSelecter::checkStrictModeAgg(StrictMode strictMode, const std::string& name, const std::string& nsName, + const TagsMatcher& tagsMatcher) const { + if (int index = IndexValueType::SetByJsonPath; ns_->tryGetIndexByName(name, index)) { + return; + } if (strictMode == StrictModeIndexes) { throw Error(errParams, @@ -1189,12 +1220,12 @@ void NsSelecter::checkStrictModeAgg(StrictMode strictMode, const std::string &na } } -h_vector NsSelecter::getAggregators(const std::vector &aggEntries, StrictMode strictMode) const { +h_vector NsSelecter::getAggregators(const std::vector& aggEntries, StrictMode strictMode) const { static constexpr int NotFilled = -2; h_vector ret; h_vector distinctIndexes; - for (const auto &ag : aggEntries) { + for (const auto& ag : aggEntries) { if (ag.Type() == AggCount || ag.Type() == AggCountCached) { continue; } @@ -1203,7 +1234,7 @@ h_vector NsSelecter::getAggregators(const std::vector sortingEntries; sortingEntries.reserve(ag.Sorting().size()); - for (const auto &s : ag.Sorting()) { + for (const auto& s : ag.Sorting()) { sortingEntries.push_back({(iequals("count"sv, s.expression) ? Aggregator::SortingEntry::Count : NotFilled), s.desc}); } int idx = -1; @@ -1239,14 +1270,20 @@ h_vector NsSelecter::getAggregators(const std::vectorpayloadType_, fields, ag.Type(), ag.Fields(), sortingEntries, ag.Limit(), ag.Offset(), compositeIndexFields); } - if (distinctIndexes.size() <= 1) return ret; - for (const Aggregator &agg : ret) { - if (agg.Type() == AggDistinct) continue; - for (const std::string &name : agg.Names()) { + if (distinctIndexes.size() <= 1) { + return ret; + } + for (const Aggregator& agg : ret) { + if (agg.Type() == AggDistinct) { + continue; + } + for (const std::string& name : agg.Names()) { if (std::find_if(distinctIndexes.cbegin(), distinctIndexes.cend(), [&ret, &name](size_t idx) { return ret[idx].Names()[0] == name; }) == distinctIndexes.cend()) { throw Error(errQueryExec, "Cannot be combined several distinct and non distinct aggregator on index %s", name); @@ -1257,7 +1294,7 @@ h_vector NsSelecter::getAggregators(const std::vector &joinedSelectors, bool &skipSortingEntry, StrictMode strictMode) { +void NsSelecter::prepareSortJoinedIndex(size_t nsIdx, std::string_view column, int& index, + const std::vector& joinedSelectors, bool& skipSortingEntry, StrictMode strictMode) { assertrx_throw(!column.empty()); index = IndexValueType::SetByJsonPath; assertrx_throw(nsIdx < joinedSelectors.size()); - const auto &js = joinedSelectors[nsIdx]; - std::visit(overloaded{[](const JoinPreResult::Values &values) noexcept -> const PayloadType & { return values.payloadType; }, + const auto& js = joinedSelectors[nsIdx]; + std::visit(overloaded{[](const JoinPreResult::Values& values) noexcept -> const PayloadType& { return values.payloadType; }, Restricted{}( - [&js](const auto &) noexcept -> const PayloadType & { return js.rightNs_->payloadType_; })}, + [&js](const auto&) noexcept -> const PayloadType& { return js.rightNs_->payloadType_; })}, js.PreResult().payload) .FieldByName(std::string{column}, index); if (index == IndexValueType::SetByJsonPath) { skipSortingEntry |= !validateField( strictMode, column, js.joinQuery_.NsName(), - std::visit(overloaded{[](const JoinPreResult::Values &values) noexcept -> const TagsMatcher & { return values.tagsMatcher; }, + std::visit(overloaded{[](const JoinPreResult::Values& values) noexcept -> const TagsMatcher& { return values.tagsMatcher; }, Restricted{}( - [&js](const auto &) noexcept -> const TagsMatcher & { return js.rightNs_->tagsMatcher_; })}, + [&js](const auto&) noexcept -> const TagsMatcher& { return js.rightNs_->tagsMatcher_; })}, js.PreResult().payload)); } } -bool NsSelecter::validateField(StrictMode strictMode, std::string_view name, std::string_view nsName, const TagsMatcher &tagsMatcher) { +bool NsSelecter::validateField(StrictMode strictMode, std::string_view name, std::string_view nsName, const TagsMatcher& tagsMatcher) { if (strictMode == StrictModeIndexes) { throw Error(errParams, "Current query strict mode allows sort by index fields only. There are no indexes with name '%s' in namespace '%s'", @@ -1309,21 +1346,21 @@ bool NsSelecter::validateField(StrictMode strictMode, std::string_view name, std return true; } -static void removeQuotesFromExpression(std::string &expression) { +static void removeQuotesFromExpression(std::string& expression) { expression.erase(std::remove(expression.begin(), expression.end(), '"'), expression.end()); } -void NsSelecter::prepareSortingContext(SortingEntries &sortBy, SelectCtx &ctx, bool isFt, bool availableSelectBySortIndex) { +void NsSelecter::prepareSortingContext(SortingEntries& sortBy, SelectCtx& ctx, bool isFt, bool availableSelectBySortIndex) { using namespace SortExprFuncs; const auto strictMode = ctx.inTransaction ? StrictModeNone : ((ctx.query.GetStrictMode() == StrictModeNotSet) ? ns_->config_.strictMode : ctx.query.GetStrictMode()); static const JoinedSelectors emptyJoinedSelectors; - const auto &joinedSelectors = ctx.joinedSelectors ? *ctx.joinedSelectors : emptyJoinedSelectors; + const auto& joinedSelectors = ctx.joinedSelectors ? *ctx.joinedSelectors : emptyJoinedSelectors; ctx.sortingContext.entries.clear(); ctx.sortingContext.expressions.clear(); for (size_t i = 0; i < sortBy.size(); ++i) { - SortingEntry &sortingEntry(sortBy[i]); + SortingEntry& sortingEntry(sortBy[i]); assertrx_throw(!sortingEntry.expression.empty()); SortExpression expr{SortExpression::Parse(sortingEntry.expression, joinedSelectors)}; if (expr.ByIndexField()) { @@ -1332,7 +1369,7 @@ void NsSelecter::prepareSortingContext(SortingEntries &sortBy, SelectCtx &ctx, b sortingEntry.index = IndexValueType::SetByJsonPath; ns_->getIndexByNameOrJsonPath(sortingEntry.expression, sortingEntry.index); if (sortingEntry.index >= 0) { - reindexer::Index *sortIndex = ns_->indexes_[sortingEntry.index].get(); + reindexer::Index* sortIndex = ns_->indexes_[sortingEntry.index].get(); entry.index = sortIndex; entry.rawData = SortingContext::RawDataParams(sortIndex->ColumnData(), ns_->payloadType_, sortingEntry.index); entry.opts = &sortIndex->Opts().collateOpts_; @@ -1356,9 +1393,9 @@ void NsSelecter::prepareSortingContext(SortingEntries &sortBy, SelectCtx &ctx, b } ctx.sortingContext.entries.emplace_back(std::move(entry)); } else if (expr.ByJoinedIndexField()) { - const auto &je{expr.GetJoinedIndex()}; + const auto& je{expr.GetJoinedIndex()}; SortingContext::JoinedFieldEntry entry{sortingEntry, je.nsIdx, je.column, IndexValueType::SetByJsonPath}; - const auto &js = joinedSelectors[entry.nsIdx]; + const auto& js = joinedSelectors[entry.nsIdx]; assertrx_throw(!std::holds_alternative(js.PreResult().payload)); bool skip{false}; prepareSortIndex(*js.RightNs(), entry.field, entry.index, skip, strictMode); @@ -1373,45 +1410,45 @@ void NsSelecter::prepareSortingContext(SortingEntries &sortBy, SelectCtx &ctx, b struct { bool skipSortingEntry; StrictMode strictMode; - const JoinedSelectors &joinedSelectors; + const JoinedSelectors& joinedSelectors; } lCtx{false, strictMode, joinedSelectors}; expr.VisitForEach( Skip{}, - [this, &lCtx](SortExprFuncs::Index &exprIndex) { + [this, &lCtx](SortExprFuncs::Index& exprIndex) { prepareSortIndex(*ns_, exprIndex.column, exprIndex.index, lCtx.skipSortingEntry, lCtx.strictMode); }, - [&lCtx](JoinedIndex &exprIndex) { + [&lCtx](JoinedIndex& exprIndex) { prepareSortJoinedIndex(exprIndex.nsIdx, exprIndex.column, exprIndex.index, lCtx.joinedSelectors, lCtx.skipSortingEntry, lCtx.strictMode); }, - [isFt](Rank &) { + [isFt](Rank&) { if (!isFt) { throw Error(errLogic, "Sorting by rank() is only available for full-text query"); } }, - [this, &lCtx](DistanceFromPoint &exprIndex) { + [this, &lCtx](DistanceFromPoint& exprIndex) { prepareSortIndex(*ns_, exprIndex.column, exprIndex.index, lCtx.skipSortingEntry, lCtx.strictMode); }, - [&lCtx](DistanceJoinedIndexFromPoint &exprIndex) { + [&lCtx](DistanceJoinedIndexFromPoint& exprIndex) { prepareSortJoinedIndex(exprIndex.nsIdx, exprIndex.column, exprIndex.index, lCtx.joinedSelectors, lCtx.skipSortingEntry, lCtx.strictMode); }, - [this, &lCtx](DistanceBetweenIndexes &exprIndex) { + [this, &lCtx](DistanceBetweenIndexes& exprIndex) { prepareSortIndex(*ns_, exprIndex.column1, exprIndex.index1, lCtx.skipSortingEntry, lCtx.strictMode); prepareSortIndex(*ns_, exprIndex.column2, exprIndex.index2, lCtx.skipSortingEntry, lCtx.strictMode); }, - [this, &lCtx](DistanceBetweenIndexAndJoinedIndex &exprIndex) { + [this, &lCtx](DistanceBetweenIndexAndJoinedIndex& exprIndex) { prepareSortIndex(*ns_, exprIndex.column, exprIndex.index, lCtx.skipSortingEntry, lCtx.strictMode); prepareSortJoinedIndex(exprIndex.jNsIdx, exprIndex.jColumn, exprIndex.jIndex, lCtx.joinedSelectors, lCtx.skipSortingEntry, lCtx.strictMode); }, - [&lCtx](DistanceBetweenJoinedIndexes &exprIndex) { + [&lCtx](DistanceBetweenJoinedIndexes& exprIndex) { prepareSortJoinedIndex(exprIndex.nsIdx1, exprIndex.column1, exprIndex.index1, lCtx.joinedSelectors, lCtx.skipSortingEntry, lCtx.strictMode); prepareSortJoinedIndex(exprIndex.nsIdx2, exprIndex.column2, exprIndex.index2, lCtx.joinedSelectors, lCtx.skipSortingEntry, lCtx.strictMode); }, - [&lCtx](DistanceBetweenJoinedIndexesSameNs &exprIndex) { + [&lCtx](DistanceBetweenJoinedIndexesSameNs& exprIndex) { prepareSortJoinedIndex(exprIndex.nsIdx, exprIndex.column1, exprIndex.index1, lCtx.joinedSelectors, lCtx.skipSortingEntry, lCtx.strictMode); prepareSortJoinedIndex(exprIndex.nsIdx, exprIndex.column2, exprIndex.index2, lCtx.joinedSelectors, @@ -1453,7 +1490,7 @@ class CostCalculator { curCost_ = 0; } bool IsInOrSequence() const noexcept { return isInSequence_; } - void Add(const SelectKeyResults &results, bool isTargetSortIndex) noexcept { + void Add(const SelectKeyResults& results, bool isTargetSortIndex) noexcept { if constexpr (countingPolicy == CostCountingPolicy::ExceptTargetSortIdxSeq) { if (!isInSequence_ && isTargetSortIndex) { return; @@ -1462,10 +1499,10 @@ class CostCalculator { onlyTargetSortIdxInSequence_ = onlyTargetSortIdxInSequence_ && isTargetSortIndex; Add(results); } - void Add(const SelectKeyResults &results) noexcept { + void Add(const SelectKeyResults& results) noexcept { std::visit( - overloaded{[this](const SelectKeyResultsVector &selRes) { - for (const SelectKeyResult &res : selRes) { + overloaded{[this](const SelectKeyResultsVector& selRes) { + for (const SelectKeyResult& res : selRes) { if (isInSequence_) { curCost_ += res.GetMaxIterations(totalCost_); } else { @@ -1481,7 +1518,7 @@ class CostCalculator { } size_t TotalCost() const noexcept { return totalCost_; } void MarkInapposite() noexcept { hasInappositeEntries_ = true; } - bool OnNewEntry(const QueryEntries &qentries, size_t i, size_t next) { + bool OnNewEntry(const QueryEntries& qentries, size_t i, size_t next) { const OpType op = qentries.GetOperation(i); switch (op) { case OpAnd: { @@ -1519,7 +1556,7 @@ class CostCalculator { size_t totalCost_ = std::numeric_limits::max(); }; -size_t NsSelecter::calculateNormalCost(const QueryEntries &qentries, SelectCtx &ctx, const RdxContext &rdxCtx) { +size_t NsSelecter::calculateNormalCost(const QueryEntries& qentries, SelectCtx& ctx, const RdxContext& rdxCtx) { const size_t totalItemsCount = ns_->itemsCount(); CostCalculator costCalculator(totalItemsCount); enum { SortIndexNotFound = 0, SortIndexFound, SortIndexHasUnorderedConditions } sortIndexSearchState = SortIndexNotFound; @@ -1527,16 +1564,16 @@ size_t NsSelecter::calculateNormalCost(const QueryEntries &qentries, SelectCtx & next = qentries.Next(i); const bool calculateEntry = costCalculator.OnNewEntry(qentries, i, next); qentries.Visit( - i, [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryEntry &) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryFieldEntry &) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, + i, [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, + [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryFieldEntry&) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, Skip{}, - [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntriesBracket &) + [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntriesBracket&) RX_POST_LMBD_ALWAYS_INLINE noexcept { costCalculator.MarkInapposite(); }, - [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const JoinQueryEntry &) + [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const JoinQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE noexcept { costCalculator.MarkInapposite(); }, - [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const BetweenFieldsQueryEntry &) + [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const BetweenFieldsQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE noexcept { costCalculator.MarkInapposite(); }, - [&](const QueryEntry &qe) { + [&](const QueryEntry& qe) { if (!qe.IsFieldIndexed()) { costCalculator.MarkInapposite(); return; @@ -1563,7 +1600,7 @@ size_t NsSelecter::calculateNormalCost(const QueryEntries &qentries, SelectCtx & return; } - auto &index = ns_->indexes_[qe.IndexNo()]; + auto& index = ns_->indexes_[qe.IndexNo()]; if (IsFullText(index->Type())) { costCalculator.MarkInapposite(); return; @@ -1578,7 +1615,7 @@ size_t NsSelecter::calculateNormalCost(const QueryEntries &qentries, SelectCtx & try { SelectKeyResults results = index->SelectKey(qe.Values(), qe.Condition(), 0, opts, nullptr, rdxCtx); costCalculator.Add(results, qe.IndexNo() == ctx.sortingContext.uncommitedIndex); - } catch (const Error &) { + } catch (const Error&) { costCalculator.MarkInapposite(); } }); @@ -1591,7 +1628,7 @@ size_t NsSelecter::calculateNormalCost(const QueryEntries &qentries, SelectCtx & return costCalculator.TotalCost(); } -size_t NsSelecter::calculateOptimizedCost(size_t costNormal, const QueryEntries &qentries, SelectCtx &ctx, const RdxContext &rdxCtx) { +size_t NsSelecter::calculateOptimizedCost(size_t costNormal, const QueryEntries& qentries, SelectCtx& ctx, const RdxContext& rdxCtx) { // 'costOptimized == costNormal + 1' reduces internal iterations count for the tree in the res.GetMaxIterations() call CostCalculator costCalculator(costNormal + 1); for (size_t next, i = 0, sz = qentries.Size(); i != sz; i = next) { @@ -1601,15 +1638,15 @@ size_t NsSelecter::calculateOptimizedCost(size_t costNormal, const QueryEntries } qentries.Visit( i, Skip{}, - [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryEntry &) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryFieldEntry &) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, - [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntriesBracket &) + [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, + [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryFieldEntry&) RX_POST_LMBD_ALWAYS_INLINE { throw_as_assert; }, + [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntriesBracket&) RX_POST_LMBD_ALWAYS_INLINE noexcept { costCalculator.MarkInapposite(); }, - [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const JoinQueryEntry &) + [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const JoinQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE noexcept { costCalculator.MarkInapposite(); }, - [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const BetweenFieldsQueryEntry &) + [&costCalculator] RX_PRE_LMBD_ALWAYS_INLINE(const BetweenFieldsQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE noexcept { costCalculator.MarkInapposite(); }, - [&](const QueryEntry &qe) { + [&](const QueryEntry& qe) { if (!qe.IsFieldIndexed() || qe.IndexNo() != ctx.sortingContext.uncommitedIndex) { costCalculator.MarkInapposite(); return; @@ -1626,7 +1663,7 @@ size_t NsSelecter::calculateOptimizedCost(size_t costNormal, const QueryEntries SelectKeyResults results = ns_->indexes_[qe.IndexNo()]->SelectKey(qe.Values(), qe.Condition(), 0, opts, nullptr, rdxCtx); costCalculator.Add(results); - } catch (const Error &) { + } catch (const Error&) { costCalculator.MarkInapposite(); } }); @@ -1635,12 +1672,12 @@ size_t NsSelecter::calculateOptimizedCost(size_t costNormal, const QueryEntries return costCalculator.TotalCost(); } -bool NsSelecter::isSortOptimizationEffective(const QueryEntries &qentries, SelectCtx &ctx, const RdxContext &rdxCtx) { +bool NsSelecter::isSortOptimizationEffective(const QueryEntries& qentries, SelectCtx& ctx, const RdxContext& rdxCtx) { if (qentries.Size() == 0) { return true; } if (qentries.Size() == 1 && qentries.Is(0)) { - const auto &qe = qentries.Get(0); + const auto& qe = qentries.Get(0); if (qe.IndexNo() == ctx.sortingContext.uncommitedIndex) { return SelectIteratorContainer::IsExpectingOrderedResults(qe); } @@ -1681,14 +1718,14 @@ bool NsSelecter::isSortOptimizationEffective(const QueryEntries &qentries, Selec return costOptimized <= costNormal; } -void NsSelecter::writeAggregationResultMergeSubQuery(QueryResults &result, h_vector &aggregators, SelectCtx &ctx) { +void NsSelecter::writeAggregationResultMergeSubQuery(QueryResults& result, h_vector& aggregators, SelectCtx& ctx) { if (result.aggregationResults.size() < aggregators.size()) { throw Error(errQueryExec, "Merged query(%s) aggregators count (%d) does not match to the parent query aggregations (%d)", ctx.query.GetSQL(false), aggregators.size(), result.aggregationResults.size()); } for (size_t i = 0; i < aggregators.size(); i++) { AggregationResult r = aggregators[i].GetResult(); - AggregationResult &parentRes = result.aggregationResults[i]; + AggregationResult& parentRes = result.aggregationResults[i]; if (r.type != parentRes.type || r.fields != parentRes.fields) { std::stringstream strParentRes; std::stringstream strR; @@ -1740,13 +1777,13 @@ void NsSelecter::writeAggregationResultMergeSubQuery(QueryResults &result, h_vec } } -RX_NO_INLINE void NsSelecter::throwIncorrectRowIdInSortOrders(int rowId, const Index &firstSortIndex, const SelectIterator &firstIterator) { +RX_NO_INLINE void NsSelecter::throwIncorrectRowIdInSortOrders(int rowId, const Index& firstSortIndex, const SelectIterator& firstIterator) { throw Error(errLogic, "FirstIterator: %s, firstSortIndex: %s, firstSortIndex size: %d, rowId: %d", firstIterator.name, firstSortIndex.Name(), static_cast(firstSortIndex.SortOrders().size()), rowId); } -template void NsSelecter::operator()(QueryResults &, SelectCtxWithJoinPreSelect &, const RdxContext &); -template void NsSelecter::operator()(QueryResults &, SelectCtxWithJoinPreSelect &, const RdxContext &); -template void NsSelecter::operator()(QueryResults &, SelectCtxWithJoinPreSelect &, const RdxContext &); +template void NsSelecter::operator()(QueryResults&, SelectCtxWithJoinPreSelect&, const RdxContext&); +template void NsSelecter::operator()(QueryResults&, SelectCtxWithJoinPreSelect&, const RdxContext&); +template void NsSelecter::operator()(QueryResults&, SelectCtxWithJoinPreSelect&, const RdxContext&); } // namespace reindexer diff --git a/cpp_src/core/nsselecter/nsselecter.h b/cpp_src/core/nsselecter/nsselecter.h index 487a1b901..0a1a1d2ec 100644 --- a/cpp_src/core/nsselecter/nsselecter.h +++ b/cpp_src/core/nsselecter/nsselecter.h @@ -11,10 +11,10 @@ enum class IsMergeQuery : bool { Yes = true, No = false }; enum class IsFTQuery { Yes, No, NotSet }; struct SelectCtx { - explicit SelectCtx(const Query &query_, const Query *parentQuery_) noexcept : query(query_), parentQuery(parentQuery_) {} - const Query &query; - JoinedSelectors *joinedSelectors = nullptr; - SelectFunctionsHolder *functions = nullptr; + explicit SelectCtx(const Query& query_, const Query* parentQuery_) noexcept : query(query_), parentQuery(parentQuery_) {} + const Query& query; + JoinedSelectors* joinedSelectors = nullptr; + SelectFunctionsHolder* functions = nullptr; ExplainCalc::Duration preResultTimeTotal = ExplainCalc::Duration::zero(); SortingContext sortingContext; @@ -29,7 +29,7 @@ struct SelectCtx { IsFTQuery isFtQuery = IsFTQuery::NotSet; QueryType crashReporterQueryType = QuerySelect; - const Query *parentQuery = nullptr; + const Query* parentQuery = nullptr; ExplainCalc explain; bool requiresCrashTracking = false; std::vector subQueriesExplains; @@ -39,16 +39,16 @@ struct SelectCtx { template struct SelectCtxWithJoinPreSelect : public SelectCtx { - explicit SelectCtxWithJoinPreSelect(const Query &query, const Query *parentQuery, JoinPreSelCtx preSel) noexcept + explicit SelectCtxWithJoinPreSelect(const Query& query, const Query* parentQuery, JoinPreSelCtx preSel) noexcept : SelectCtx(query, parentQuery), preSelect{std::move(preSel)} {} JoinPreSelCtx preSelect; }; template <> struct SelectCtxWithJoinPreSelect : public SelectCtx { - explicit SelectCtxWithJoinPreSelect(const Query &query, const Query *parentQuery) noexcept : SelectCtx(query, parentQuery) {} + explicit SelectCtxWithJoinPreSelect(const Query& query, const Query* parentQuery) noexcept : SelectCtx(query, parentQuery) {} }; -SelectCtxWithJoinPreSelect(const Query &, const Query *) -> SelectCtxWithJoinPreSelect; +SelectCtxWithJoinPreSelect(const Query&, const Query*) -> SelectCtxWithJoinPreSelect; class ItemComparator; class ExplainCalc; @@ -60,68 +60,68 @@ class NsSelecter { class JoinedNsValueGetter; public: - NsSelecter(NamespaceImpl *parent) noexcept : ns_(parent) {} + NsSelecter(NamespaceImpl* parent) noexcept : ns_(parent) {} template - void operator()(QueryResults &result, SelectCtxWithJoinPreSelect &ctx, const RdxContext &); + void operator()(QueryResults& result, SelectCtxWithJoinPreSelect& ctx, const RdxContext&); private: template struct LoopCtx { - LoopCtx(SelectIteratorContainer &sIt, SelectCtxWithJoinPreSelect &ctx, const QueryPreprocessor &qpp, - h_vector &agg, ExplainCalc &expl) + LoopCtx(SelectIteratorContainer& sIt, SelectCtxWithJoinPreSelect& ctx, const QueryPreprocessor& qpp, + h_vector& agg, ExplainCalc& expl) : qres(sIt), sctx(ctx), qPreproc(qpp), aggregators(agg), explain(expl) {} - SelectIteratorContainer &qres; + SelectIteratorContainer& qres; bool calcTotal = false; - SelectCtxWithJoinPreSelect &sctx; - const QueryPreprocessor &qPreproc; - h_vector &aggregators; - ExplainCalc &explain; + SelectCtxWithJoinPreSelect& sctx; + const QueryPreprocessor& qPreproc; + h_vector& aggregators; + ExplainCalc& explain; unsigned start = QueryEntry::kDefaultOffset; unsigned count = QueryEntry::kDefaultLimit; bool preselectForFt = false; }; template - void selectLoop(LoopCtx &ctx, ResultsT &result, const RdxContext &); + void selectLoop(LoopCtx& ctx, ResultsT& result, const RdxContext&); template - It applyForcedSort(It begin, It end, const ItemComparator &, const SelectCtx &ctx, const joins::NamespaceResults *); + It applyForcedSort(It begin, It end, const ItemComparator&, const SelectCtx& ctx, const joins::NamespaceResults*); template - static It applyForcedSortImpl(NamespaceImpl &, It begin, It end, const ItemComparator &, const std::vector &forcedSortOrder, - const std::string &fieldName, const ValueGetter &); + static It applyForcedSortImpl(NamespaceImpl&, It begin, It end, const ItemComparator&, const std::vector& forcedSortOrder, + const std::string& fieldName, const ValueGetter&); template - void applyGeneralSort(It itFirst, It itLast, It itEnd, const ItemComparator &, const SelectCtx &ctx); + void applyGeneralSort(It itFirst, It itLast, It itEnd, const ItemComparator&, const SelectCtx& ctx); - void calculateSortExpressions(uint8_t proc, IdType rowId, IdType properRowId, SelectCtx &, const QueryResults &); + void calculateSortExpressions(uint8_t proc, IdType rowId, IdType properRowId, SelectCtx&, const QueryResults&); template - void addSelectResult(uint8_t proc, IdType rowId, IdType properRowId, SelectCtxWithJoinPreSelect &sctx, - h_vector &aggregators, QueryResults &result, bool preselectForFt); - - h_vector getAggregators(const std::vector &aggEntrys, StrictMode strictMode) const; - void setLimitAndOffset(ItemRefVector &result, size_t offset, size_t limit); - void prepareSortingContext(SortingEntries &sortBy, SelectCtx &ctx, bool isFt, bool availableSelectBySortIndex); - static void prepareSortIndex(const NamespaceImpl &, std::string_view column, int &index, bool &skipSortingEntry, StrictMode); - static void prepareSortJoinedIndex(size_t nsIdx, std::string_view column, int &index, const std::vector &, - bool &skipSortingEntry, StrictMode); - void getSortIndexValue(const SortingContext &sortCtx, IdType rowId, VariantArray &value, uint8_t proc, const joins::NamespaceResults *, - const JoinedSelectors &); - void processLeftJoins(QueryResults &qr, SelectCtx &sctx, size_t startPos, const RdxContext &); - bool checkIfThereAreLeftJoins(SelectCtx &sctx) const; + void addSelectResult(uint8_t proc, IdType rowId, IdType properRowId, SelectCtxWithJoinPreSelect& sctx, + h_vector& aggregators, QueryResults& result, bool preselectForFt); + + h_vector getAggregators(const std::vector& aggEntrys, StrictMode strictMode) const; + void setLimitAndOffset(ItemRefVector& result, size_t offset, size_t limit); + void prepareSortingContext(SortingEntries& sortBy, SelectCtx& ctx, bool isFt, bool availableSelectBySortIndex); + static void prepareSortIndex(const NamespaceImpl&, std::string_view column, int& index, bool& skipSortingEntry, StrictMode); + static void prepareSortJoinedIndex(size_t nsIdx, std::string_view column, int& index, const std::vector&, + bool& skipSortingEntry, StrictMode); + void getSortIndexValue(const SortingContext& sortCtx, IdType rowId, VariantArray& value, uint8_t proc, const joins::NamespaceResults*, + const JoinedSelectors&); + void processLeftJoins(QueryResults& qr, SelectCtx& sctx, size_t startPos, const RdxContext&); + bool checkIfThereAreLeftJoins(SelectCtx& sctx) const; template - void sortResults(LoopCtx &sctx, It begin, It end, const SortingOptions &sortingOptions, - const joins::NamespaceResults *); - - size_t calculateNormalCost(const QueryEntries &qe, SelectCtx &ctx, const RdxContext &rdxCtx); - size_t calculateOptimizedCost(size_t costNormal, const QueryEntries &qe, SelectCtx &ctx, const RdxContext &rdxCtx); - bool isSortOptimizationEffective(const QueryEntries &qe, SelectCtx &ctx, const RdxContext &rdxCtx); - static bool validateField(StrictMode strictMode, std::string_view name, std::string_view nsName, const TagsMatcher &tagsMatcher); - void checkStrictModeAgg(StrictMode strictMode, const std::string &name, const std::string &nsName, - const TagsMatcher &tagsMatcher) const; - - void writeAggregationResultMergeSubQuery(QueryResults &result, h_vector &aggregators, SelectCtx &ctx); - [[noreturn]] RX_NO_INLINE void throwIncorrectRowIdInSortOrders(int rowId, const Index &firstSortIndex, - const SelectIterator &firstIterator); - NamespaceImpl *ns_; + void sortResults(LoopCtx& sctx, It begin, It end, const SortingOptions& sortingOptions, + const joins::NamespaceResults*); + + size_t calculateNormalCost(const QueryEntries& qe, SelectCtx& ctx, const RdxContext& rdxCtx); + size_t calculateOptimizedCost(size_t costNormal, const QueryEntries& qe, SelectCtx& ctx, const RdxContext& rdxCtx); + bool isSortOptimizationEffective(const QueryEntries& qe, SelectCtx& ctx, const RdxContext& rdxCtx); + static bool validateField(StrictMode strictMode, std::string_view name, std::string_view nsName, const TagsMatcher& tagsMatcher); + void checkStrictModeAgg(StrictMode strictMode, const std::string& name, const std::string& nsName, + const TagsMatcher& tagsMatcher) const; + + void writeAggregationResultMergeSubQuery(QueryResults& result, h_vector& aggregators, SelectCtx& ctx); + [[noreturn]] RX_NO_INLINE void throwIncorrectRowIdInSortOrders(int rowId, const Index& firstSortIndex, + const SelectIterator& firstIterator); + NamespaceImpl* ns_; SelectFunction::Ptr fnc_; FtCtx::Ptr ft_ctx_; }; diff --git a/cpp_src/core/nsselecter/querypreprocessor.cc b/cpp_src/core/nsselecter/querypreprocessor.cc index 519ae66c4..461eac063 100644 --- a/cpp_src/core/nsselecter/querypreprocessor.cc +++ b/cpp_src/core/nsselecter/querypreprocessor.cc @@ -15,7 +15,7 @@ namespace reindexer { -QueryPreprocessor::QueryPreprocessor(QueryEntries &&queries, NamespaceImpl *ns, const SelectCtx &ctx) +QueryPreprocessor::QueryPreprocessor(QueryEntries&& queries, NamespaceImpl* ns, const SelectCtx& ctx) : QueryEntries(std::move(queries)), ns_(*ns), query_{ctx.query}, @@ -29,11 +29,13 @@ QueryPreprocessor::QueryPreprocessor(QueryEntries &&queries, NamespaceImpl *ns, if (forcedSortOrder_ && (start_ > QueryEntry::kDefaultOffset || count_ < QueryEntry::kDefaultLimit)) { assertrx_throw(!query_.sortingEntries_.empty()); static const std::vector emptyJoinedSelectors; - const auto &sEntry = query_.sortingEntries_[0]; + const auto& sEntry = query_.sortingEntries_[0]; if (SortExpression::Parse(sEntry.expression, emptyJoinedSelectors).ByIndexField()) { VariantArray values; values.reserve(query_.forcedSortOrder_.size()); - for (const auto &v : query_.forcedSortOrder_) values.push_back(v); + for (const auto& v : query_.forcedSortOrder_) { + values.push_back(v); + } desc_ = sEntry.desc; QueryField fld{sEntry.expression}; SetQueryField(fld, ns_); @@ -52,14 +54,20 @@ QueryPreprocessor::QueryPreprocessor(QueryEntries &&queries, NamespaceImpl *ns, } } -void QueryPreprocessor::ExcludeFtQuery(const RdxContext &rdxCtx) { - if (queryEntryAddedByForcedSortOptimization_ || Size() <= 1) return; +void QueryPreprocessor::ExcludeFtQuery(const RdxContext& rdxCtx) { + if (queryEntryAddedByForcedSortOptimization_ || Size() <= 1) { + return; + } for (auto it = begin(), next = it, endIt = end(); it != endIt; it = next) { ++next; if (it->Is() && it->Value().IsFieldIndexed()) { - auto &index = ns_.indexes_[it->Value().IndexNo()]; - if (!IsFastFullText(index->Type())) continue; - if (it->operation != OpAnd || (next != endIt && next->operation == OpOr) || !index->EnablePreselectBeforeFt()) break; + auto& index = ns_.indexes_[it->Value().IndexNo()]; + if (!IsFastFullText(index->Type())) { + continue; + } + if (it->operation != OpAnd || (next != endIt && next->operation == OpOr) || !index->EnablePreselectBeforeFt()) { + break; + } ftPreselect_ = index->FtPreselect(rdxCtx); start_ = QueryEntry::kDefaultOffset; count_ = QueryEntry::kDefaultLimit; @@ -72,9 +80,11 @@ void QueryPreprocessor::ExcludeFtQuery(const RdxContext &rdxCtx) { } } -bool QueryPreprocessor::NeedNextEvaluation(unsigned start, unsigned count, bool &matchedAtLeastOnce, - QresExplainHolder &qresHolder) noexcept { - if (evaluationsCount_++) return false; +bool QueryPreprocessor::NeedNextEvaluation(unsigned start, unsigned count, bool& matchedAtLeastOnce, + QresExplainHolder& qresHolder) noexcept { + if (evaluationsCount_++) { + return false; + } if (queryEntryAddedByForcedSortOptimization_) { container_.back().operation = desc_ ? OpAnd : OpNot; assertrx_throw(start <= start_); @@ -83,7 +93,9 @@ bool QueryPreprocessor::NeedNextEvaluation(unsigned start, unsigned count, bool count_ = count; return count_ || (reqMatchedOnce_ && !matchedAtLeastOnce); } else if (ftEntry_) { - if (!matchedAtLeastOnce) return false; + if (!matchedAtLeastOnce) { + return false; + } qresHolder.BackupContainer(); if (isMergeQuery_) { if (QueryEntry::kDefaultLimit - query_.Offset() > query_.Limit()) { @@ -107,8 +119,10 @@ bool QueryPreprocessor::NeedNextEvaluation(unsigned start, unsigned count, bool return false; } -void QueryPreprocessor::checkStrictMode(const QueryField &field) const { - if (field.IsFieldIndexed()) return; +void QueryPreprocessor::checkStrictMode(const QueryField& field) const { + if (field.IsFieldIndexed()) { + return; + } switch (strictMode_) { case StrictModeIndexes: throw Error(errQueryExec, @@ -130,8 +144,8 @@ void QueryPreprocessor::checkStrictMode(const QueryField &field) const { class JoinOnExplainEnabled; class JoinOnExplainDisabled; -int QueryPreprocessor::calculateMaxIterations(const size_t from, const size_t to, int maxMaxIters, span &maxIterations, - bool inTransaction, bool enableSortOrders, const RdxContext &rdxCtx) const { +int QueryPreprocessor::calculateMaxIterations(const size_t from, const size_t to, int maxMaxIters, span& maxIterations, + bool inTransaction, bool enableSortOrders, const RdxContext& rdxCtx) const { int res = maxMaxIters; int current = maxMaxIters; for (size_t cur = from; cur < to; cur = Next(cur)) { @@ -139,12 +153,12 @@ int QueryPreprocessor::calculateMaxIterations(const size_t from, const size_t to maxMaxIters, Visit( cur, - [&](const QueryEntriesBracket &) { + [&](const QueryEntriesBracket&) { return calculateMaxIterations(cur + 1, Next(cur), maxMaxIters, maxIterations, inTransaction, enableSortOrders, rdxCtx); }, - [&](const QueryEntry &qe) { + [&](const QueryEntry& qe) { if (qe.IndexNo() >= 0) { - Index &index = *ns_.indexes_[qe.IndexNo()]; + Index& index = *ns_.indexes_[qe.IndexNo()]; if (IsFullText(index.Type()) || isStore(index.Type())) { return maxMaxIters; } @@ -157,9 +171,9 @@ int QueryPreprocessor::calculateMaxIterations(const size_t from, const size_t to opts.inTransaction = inTransaction; const auto selIters = index.SelectKey(qe.Values(), qe.Condition(), 0, opts, nullptr, rdxCtx); - if (auto *selRes = std::get_if(&selIters); selRes) { + if (auto* selRes = std::get_if(&selIters); selRes) { int res = 0; - for (const auto &sIt : *selRes) { + for (const auto& sIt : *selRes) { res += sIt.GetMaxIterations(); } return res; @@ -169,10 +183,10 @@ int QueryPreprocessor::calculateMaxIterations(const size_t from, const size_t to return maxMaxIters; } }, - [maxMaxIters](const BetweenFieldsQueryEntry &) noexcept { return maxMaxIters; }, - [maxMaxIters](const JoinQueryEntry &) noexcept { return maxMaxIters; }, - [](const SubQueryEntry &) -> int { throw_as_assert; }, [](const SubQueryFieldEntry &) -> int { throw_as_assert; }, - [maxMaxIters](const AlwaysTrue &) noexcept { return maxMaxIters; }, [&](const AlwaysFalse &) noexcept { return 0; })); + [maxMaxIters](const BetweenFieldsQueryEntry&) noexcept { return maxMaxIters; }, + [maxMaxIters](const JoinQueryEntry&) noexcept { return maxMaxIters; }, [](const SubQueryEntry&) -> int { throw_as_assert; }, + [](const SubQueryFieldEntry&) -> int { throw_as_assert; }, + [maxMaxIters](const AlwaysTrue&) noexcept { return maxMaxIters; }, [&](const AlwaysFalse&) noexcept { return 0; })); switch (GetOperation(cur)) { case OpAnd: res = std::min(res, current); @@ -195,8 +209,8 @@ int QueryPreprocessor::calculateMaxIterations(const size_t from, const size_t to return res; } -void QueryPreprocessor::InjectConditionsFromJoins(JoinedSelectors &js, OnConditionInjections &expalainOnInjections, LogLevel logLevel, - bool inTransaction, bool enableSortOrders, const RdxContext &rdxCtx) { +void QueryPreprocessor::InjectConditionsFromJoins(JoinedSelectors& js, OnConditionInjections& expalainOnInjections, LogLevel logLevel, + bool inTransaction, bool enableSortOrders, const RdxContext& rdxCtx) { h_vector maxIterations(Size()); span maxItersSpan(maxIterations.data(), maxIterations.size()); const int maxIters = calculateMaxIterations(0, Size(), ns_.itemsCount(), maxItersSpan, inTransaction, enableSortOrders, rdxCtx); @@ -263,11 +277,11 @@ bool QueryPreprocessor::removeAlwaysTrue() { bool QueryPreprocessor::containsJoin(size_t n) noexcept { return Visit( - n, [](const JoinQueryEntry &) noexcept { return true; }, [](const QueryEntry &) noexcept { return false; }, - [](const BetweenFieldsQueryEntry &) noexcept { return false; }, [](const AlwaysTrue &) noexcept { return false; }, - [](const AlwaysFalse &) noexcept { return false; }, [](const SubQueryEntry &) noexcept { return false; }, - [](const SubQueryFieldEntry &) noexcept { return false; }, - [&](const QueryEntriesBracket &) noexcept { + n, [](const JoinQueryEntry&) noexcept { return true; }, [](const QueryEntry&) noexcept { return false; }, + [](const BetweenFieldsQueryEntry&) noexcept { return false; }, [](const AlwaysTrue&) noexcept { return false; }, + [](const AlwaysFalse&) noexcept { return false; }, [](const SubQueryEntry&) noexcept { return false; }, + [](const SubQueryFieldEntry&) noexcept { return false; }, + [&](const QueryEntriesBracket&) noexcept { for (size_t i = n, e = Next(n); i < e; ++i) { if (Is(i)) { return true; @@ -362,7 +376,9 @@ void QueryPreprocessor::Reduce(bool isFt) { changed = LookupQueryIndexes() || changed; changed = removeAlwaysFalse() || changed; changed = removeAlwaysTrue() || changed; - if (!isFt) changed = SubstituteCompositeIndexes() || changed; + if (!isFt) { + changed = SubstituteCompositeIndexes() || changed; + } } while (changed); } @@ -374,8 +390,12 @@ bool QueryPreprocessor::canRemoveBracket(size_t i) const { } const size_t next = Next(i); const OpType op = GetOperation(i); - if (op != OpAnd && GetOperation(i + 1) != OpAnd) return false; - if (next == Next(i + 1)) return true; + if (op != OpAnd && GetOperation(i + 1) != OpAnd) { + return false; + } + if (next == Next(i + 1)) { + return true; + } return op == OpAnd && (next == Size() || GetOperation(next) != OpOr); } @@ -385,7 +405,9 @@ size_t QueryPreprocessor::removeBrackets(size_t begin, size_t end) { } size_t deleted = 0; for (size_t i = begin; i < end - deleted; i = Next(i)) { - if (!IsSubTree(i)) continue; + if (!IsSubTree(i)) { + continue; + } deleted += removeBrackets(i + 1, Next(i)); if (canRemoveBracket(i)) { if (const OpType op = GetOperation(i); op != OpAnd) { @@ -405,16 +427,17 @@ size_t QueryPreprocessor::lookupQueryIndexes(uint16_t dst, uint16_t srcBegin, ui for (size_t src = srcBegin, nextSrc; src < srcEnd; src = nextSrc) { nextSrc = Next(src); const auto mergeResult = container_[src].Visit( - [](const SubQueryEntry &) -> MergeResult { throw_as_assert; }, - [](const SubQueryFieldEntry &) -> MergeResult { throw_as_assert; }, - [&](const QueryEntriesBracket &) { - if (dst != src) container_[dst] = std::move(container_[src]); + [](const SubQueryEntry&) -> MergeResult { throw_as_assert; }, [](const SubQueryFieldEntry&) -> MergeResult { throw_as_assert; }, + [&](const QueryEntriesBracket&) { + if (dst != src) { + container_[dst] = std::move(container_[src]); + } const size_t mergedInBracket = lookupQueryIndexes(dst + 1, src + 1, nextSrc); container_[dst].Value().Erase(mergedInBracket); merged += mergedInBracket; return MergeResult::NotMerged; }, - [&](QueryEntry &entry) { + [&](QueryEntry& entry) { if (entry.IsFieldIndexed()) { // try to merge entries with AND operator if ((GetOperation(src) == OpAnd) && (nextSrc >= srcEnd || GetOperation(nextSrc) != OpOr)) { @@ -423,9 +446,9 @@ size_t QueryPreprocessor::lookupQueryIndexes(uint16_t dst, uint16_t srcBegin, ui iidx.resize(entry.IndexNo() + 1); std::fill(iidx.begin() + oldSize, iidx.begin() + iidx.size(), 0); } - auto &iidxRef = iidx[entry.IndexNo()]; - const Index &index = *ns_.indexes_[entry.IndexNo()]; - const auto &indexOpts = index.Opts(); + auto& iidxRef = iidx[entry.IndexNo()]; + const Index& index = *ns_.indexes_[entry.IndexNo()]; + const auto& indexOpts = index.Opts(); if (iidxRef > 0 && !indexOpts.IsArray()) { const auto orderedFlag = index.IsOrdered() ? MergeOrdered::Yes : MergeOrdered::No; const auto mergeRes = IsComposite(index.Type()) @@ -449,23 +472,33 @@ size_t QueryPreprocessor::lookupQueryIndexes(uint16_t dst, uint16_t srcBegin, ui } } } - if (dst != src) container_[dst] = std::move(container_[src]); + if (dst != src) { + container_[dst] = std::move(container_[src]); + } return MergeResult::NotMerged; }, - [dst, src, this](const JoinQueryEntry &) { - if (dst != src) container_[dst] = std::move(container_[src]); + [dst, src, this](const JoinQueryEntry&) { + if (dst != src) { + container_[dst] = std::move(container_[src]); + } return MergeResult::NotMerged; }, - [dst, src, this](const BetweenFieldsQueryEntry &) { - if (dst != src) container_[dst] = std::move(container_[src]); + [dst, src, this](const BetweenFieldsQueryEntry&) { + if (dst != src) { + container_[dst] = std::move(container_[src]); + } return MergeResult::NotMerged; }, - [dst, src, this](const AlwaysFalse &) { - if (dst != src) container_[dst] = std::move(container_[src]); + [dst, src, this](const AlwaysFalse&) { + if (dst != src) { + container_[dst] = std::move(container_[src]); + } return MergeResult::NotMerged; }, - [dst, src, this](const AlwaysTrue &) { - if (dst != src) container_[dst] = std::move(container_[src]); + [dst, src, this](const AlwaysTrue&) { + if (dst != src) { + container_[dst] = std::move(container_[src]); + } return MergeResult::NotMerged; }); switch (mergeResult) { @@ -485,8 +518,8 @@ void QueryPreprocessor::CheckUniqueFtQuery() const { bool found = false; VisitForEach( Skip{}, - [](const SubQueryEntry &) { throw_as_assert; }, [](const SubQueryFieldEntry &) { throw_as_assert; }, - [&](const QueryEntry &qe) { + [](const SubQueryEntry&) { throw_as_assert; }, [](const SubQueryFieldEntry&) { throw_as_assert; }, + [&](const QueryEntry& qe) { if (qe.IsFieldIndexed() && IsFullText(ns_.indexes_[qe.IndexNo()]->Type())) { if (found) { throw Error{errQueryExec, "Query cannot contain more than one full text condition"}; @@ -507,16 +540,16 @@ bool QueryPreprocessor::ContainsFullTextIndexes() const { return false; } -const std::vector *QueryPreprocessor::getCompositeIndex(int field) const noexcept { +const std::vector* QueryPreprocessor::getCompositeIndex(int field) const noexcept { if (auto f = ns_.indexesToComposites_.find(field); f != ns_.indexesToComposites_.end()) { return &f->second; } return nullptr; } -static void createCompositeKeyValues(span> values, Payload &pl, VariantArray &ret, +static void createCompositeKeyValues(span> values, Payload& pl, VariantArray& ret, uint32_t resultSetSize, uint32_t n) { - const auto &v = values[n]; + const auto& v = values[n]; for (auto it = v.second.cbegin(), end = v.second.cend(); it != end; ++it) { pl.Set(v.first, *it); if (n + 1 < values.size()) { @@ -531,7 +564,7 @@ static void createCompositeKeyValues(span> va } } -static VariantArray createCompositeKeyValues(span> values, const PayloadType &plType, +static VariantArray createCompositeKeyValues(span> values, const PayloadType& plType, uint32_t resultSetSize) { PayloadValue d(plType.TotalSize()); Payload pl(plType, d); @@ -551,7 +584,7 @@ size_t QueryPreprocessor::substituteCompositeIndexes(const size_t from, const si CompositeSearcher searcher(ns_); for (size_t cur = from, end = to; cur < end; cur = Next(cur), end = to - deleted) { if (IsSubTree(cur)) { - const auto &bracket = Get(cur); + const auto& bracket = Get(cur); auto bracketSize = bracket.Size(); deleted += substituteCompositeIndexes(cur + 1, cur + bracketSize); continue; @@ -563,13 +596,13 @@ size_t QueryPreprocessor::substituteCompositeIndexes(const size_t from, const si if ((next < end && GetOperation(next) == OpOr)) { continue; } - auto &qe = Get(cur); + auto& qe = Get(cur); if ((qe.Condition() != CondEq && qe.Condition() != CondSet) || !qe.IsFieldIndexed() || qe.IndexNo() >= ns_.payloadType_.NumFields()) { continue; } - const std::vector *found = getCompositeIndex(qe.IndexNo()); + const std::vector* found = getCompositeIndex(qe.IndexNo()); if (!found || found->empty()) { continue; } @@ -580,12 +613,12 @@ size_t QueryPreprocessor::substituteCompositeIndexes(const size_t from, const si h_vector, 4> values; auto resIdx = searcher.GetResult(); while (resIdx >= 0) { - auto &res = searcher[resIdx]; + auto& res = searcher[resIdx]; values.clear(); uint32_t resultSetSize = 1; uint32_t maxSetSize = 0; for (auto i : res.entries) { - auto &qe = Get(i); + auto& qe = Get(i); if rx_unlikely (!res.fields.contains(qe.IndexNo())) { throw Error(errLogic, "Error during composite index's fields substitution (this should not happen)"); } @@ -603,7 +636,7 @@ size_t QueryPreprocessor::substituteCompositeIndexes(const size_t from, const si } } for (auto i : res.entries) { - auto &qe = Get(i); + auto& qe = Get(i); qe.ConvertValuesToFieldType(); const int idxNo = qe.IndexNo(); values.emplace_back(idxNo, std::move(qe).Values()); @@ -629,10 +662,10 @@ size_t QueryPreprocessor::substituteCompositeIndexes(const size_t from, const si void QueryPreprocessor::initIndexedQueries(size_t begin, size_t end) { for (auto cur = begin; cur != end; cur = Next(cur)) { Visit( - cur, Skip{}, [](const SubQueryEntry &) { throw_as_assert; }, - [](const SubQueryFieldEntry &) { throw_as_assert; }, - [this, cur](const QueryEntriesBracket &) { initIndexedQueries(cur + 1, Next(cur)); }, - [this](BetweenFieldsQueryEntry &entry) { + cur, Skip{}, [](const SubQueryEntry&) { throw_as_assert; }, + [](const SubQueryFieldEntry&) { throw_as_assert; }, + [this, cur](const QueryEntriesBracket&) { initIndexedQueries(cur + 1, Next(cur)); }, + [this](BetweenFieldsQueryEntry& entry) { if (!entry.FieldsHaveBeenSet()) { SetQueryField(entry.LeftFieldData(), ns_); SetQueryField(entry.RightFieldData(), ns_); @@ -640,7 +673,7 @@ void QueryPreprocessor::initIndexedQueries(size_t begin, size_t end) { checkStrictMode(entry.LeftFieldData()); checkStrictMode(entry.RightFieldData()); }, - [this](QueryEntry &qe) { + [this](QueryEntry& qe) { if (!qe.FieldsHaveBeenSet()) { SetQueryField(qe.FieldData(), ns_); } @@ -651,8 +684,10 @@ void QueryPreprocessor::initIndexedQueries(size_t begin, size_t end) { } SortingEntries QueryPreprocessor::detectOptimalSortOrder() const { - if (!AvailableSelectBySortIndex()) return {}; - if (const Index *maxIdx = findMaxIndex(cbegin(), cend())) { + if (!AvailableSelectBySortIndex()) { + return {}; + } + if (const Index* maxIdx = findMaxIndex(cbegin(), cend())) { SortingEntries sortingEntries; sortingEntries.emplace_back(maxIdx->Name(), false); return sortingEntries; @@ -660,11 +695,11 @@ SortingEntries QueryPreprocessor::detectOptimalSortOrder() const { return {}; } -const Index *QueryPreprocessor::findMaxIndex(QueryEntries::const_iterator begin, QueryEntries::const_iterator end) const { +const Index* QueryPreprocessor::findMaxIndex(QueryEntries::const_iterator begin, QueryEntries::const_iterator end) const { thread_local h_vector foundIndexes; foundIndexes.clear(); findMaxIndex(begin, end, foundIndexes); - boost::sort::pdqsort(foundIndexes.begin(), foundIndexes.end(), [](const FoundIndexInfo &l, const FoundIndexInfo &r) noexcept { + boost::sort::pdqsort(foundIndexes.begin(), foundIndexes.end(), [](const FoundIndexInfo& l, const FoundIndexInfo& r) noexcept { if (l.isFitForSortOptimization > r.isFitForSortOptimization) { return true; } @@ -680,15 +715,15 @@ const Index *QueryPreprocessor::findMaxIndex(QueryEntries::const_iterator begin, } void QueryPreprocessor::findMaxIndex(QueryEntries::const_iterator begin, QueryEntries::const_iterator end, - h_vector &foundIndexes) const { + h_vector& foundIndexes) const { for (auto it = begin; it != end; ++it) { - const auto foundIdx = it->Visit([](const SubQueryEntry &) -> FoundIndexInfo { throw_as_assert; }, - [](const SubQueryFieldEntry &) -> FoundIndexInfo { throw_as_assert; }, - [this, &it, &foundIndexes](const QueryEntriesBracket &) { + const auto foundIdx = it->Visit([](const SubQueryEntry&) -> FoundIndexInfo { throw_as_assert; }, + [](const SubQueryFieldEntry&) -> FoundIndexInfo { throw_as_assert; }, + [this, &it, &foundIndexes](const QueryEntriesBracket&) { findMaxIndex(it.cbegin(), it.cend(), foundIndexes); return FoundIndexInfo(); }, - [this](const QueryEntry &entry) -> FoundIndexInfo { + [this](const QueryEntry& entry) -> FoundIndexInfo { if (entry.IsFieldIndexed() && !entry.Distinct()) { const auto idxPtr = ns_.indexes_[entry.IndexNo()].get(); if (idxPtr->IsOrdered() && !idxPtr->Opts().IsArray()) { @@ -701,13 +736,13 @@ void QueryPreprocessor::findMaxIndex(QueryEntries::const_iterator begin, QueryEn } return {}; }, - [](const JoinQueryEntry &) noexcept { return FoundIndexInfo(); }, - [](const BetweenFieldsQueryEntry &) noexcept { return FoundIndexInfo(); }, - [](const AlwaysFalse &) noexcept { return FoundIndexInfo(); }, - [](const AlwaysTrue &) noexcept { return FoundIndexInfo(); }); + [](const JoinQueryEntry&) noexcept { return FoundIndexInfo(); }, + [](const BetweenFieldsQueryEntry&) noexcept { return FoundIndexInfo(); }, + [](const AlwaysFalse&) noexcept { return FoundIndexInfo(); }, + [](const AlwaysTrue&) noexcept { return FoundIndexInfo(); }); if (foundIdx.index) { auto found = std::find_if(foundIndexes.begin(), foundIndexes.end(), - [foundIdx](const FoundIndexInfo &i) { return i.index == foundIdx.index; }); + [foundIdx](const FoundIndexInfo& i) { return i.index == foundIdx.index; }); if (found == foundIndexes.end()) { foundIndexes.emplace_back(foundIdx); } else { @@ -721,23 +756,23 @@ namespace { class CompositeLess : less_composite_ref { public: - CompositeLess(const PayloadType &type, const FieldsSet &fields) noexcept : less_composite_ref(type, fields) {} + CompositeLess(const PayloadType& type, const FieldsSet& fields) noexcept : less_composite_ref(type, fields) {} - bool operator()(const Variant &lhs, const Variant &rhs) const { + bool operator()(const Variant& lhs, const Variant& rhs) const { assertrx_dbg(lhs.Type().Is()); assertrx_dbg(rhs.Type().Is()); - return less_composite_ref::operator()(static_cast(lhs), static_cast(rhs)); + return less_composite_ref::operator()(static_cast(lhs), static_cast(rhs)); } }; class CompositeEqual : equal_composite_ref { public: - CompositeEqual(const PayloadType &type, const FieldsSet &fields) noexcept : equal_composite_ref(type, fields) {} + CompositeEqual(const PayloadType& type, const FieldsSet& fields) noexcept : equal_composite_ref(type, fields) {} - bool operator()(const Variant &lhs, const Variant &rhs) const { + bool operator()(const Variant& lhs, const Variant& rhs) const { assertrx_dbg(lhs.Type().Is()); assertrx_dbg(rhs.Type().Is()); - return equal_composite_ref::operator()(static_cast(lhs), static_cast(rhs)); + return equal_composite_ref::operator()(static_cast(lhs), static_cast(rhs)); } }; @@ -751,20 +786,20 @@ using MergeEqualT = std::conditional_t -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetSet(QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position, - const CmpArgs &...args) { +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetSet(QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position, + const CmpArgs&... args) { // intersect 2 queryentries on the same index if rx_unlikely (lqe.Values().empty() || rqe.Values().empty()) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } - auto &&[first, second] = lqe.Values().size() < rqe.Values().size() ? std::make_pair(std::move(lqe).Values(), std::move(rqe).Values()) + auto&& [first, second] = lqe.Values().size() < rqe.Values().size() ? std::make_pair(std::move(lqe).Values(), std::move(rqe).Values()) : std::make_pair(std::move(rqe).Values(), std::move(lqe).Values()); if (first.size() == 1) { - const Variant &firstV = first[0]; + const Variant& firstV = first[0]; const MergeEqualT equalTo{args...}; - for (const Variant &secondV : second) { + for (const Variant& secondV : second) { if (equalTo(firstV, secondV)) { lqe.SetCondAndValues(CondEq, VariantArray{std::move(first[0])}); // NOLINT (bugprone-use-after-move) lqe.Distinct(distinct); @@ -780,7 +815,7 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetSet(QueryE if (second.size() < kMinArraySizeToUseHashSet) { // Intersect via binary search + sort for small vectors boost::sort::pdqsort(first.begin(), first.end(), MergeLessT{args...}); - for (auto &&v : second) { + for (auto&& v : second) { if (std::binary_search(first.begin(), first.end(), v, MergeLessT{args...})) { setValues.emplace_back(std::move(v)); } @@ -789,10 +824,10 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetSet(QueryE // Intersect via hash_set for large vectors fast_hash_set_variant set{args...}; set.reserve(first.size()); - for (auto &&v : first) { + for (auto&& v : first) { set.emplace(std::move(v)); } - for (auto &&v : second) { + for (auto&& v : second) { if (set.erase(v)) { setValues.emplace_back(std::move(v)); } @@ -801,11 +836,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetSet(QueryE } else { // Intersect via hash_set for the composite values unordered_payload_ref_set set{first.size(), hash_composite_ref{args...}, equal_composite_ref{args...}}; - for (auto &v : first) { - set.emplace(static_cast(v)); + for (auto& v : first) { + set.emplace(static_cast(v)); } - for (auto &&v : second) { - if (set.erase(static_cast(v))) { + for (auto&& v : second) { + if (set.erase(static_cast(v))) { setValues.emplace_back(std::move(v)); } } @@ -821,23 +856,23 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetSet(QueryE } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetSet(NeedSwitch needSwitch, QueryEntry &allSet, QueryEntry &set, - bool distinct, size_t position, const CmpArgs &...args) { +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetSet(NeedSwitch needSwitch, QueryEntry& allSet, QueryEntry& set, + bool distinct, size_t position, const CmpArgs&... args) { if rx_unlikely (allSet.Values().empty() || set.Values().empty()) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } const MergeEqualT equalTo{args...}; const auto lvIt = allSet.Values().begin(); - const Variant &lv = *lvIt; + const Variant& lv = *lvIt; for (auto it = lvIt + 1, endIt = allSet.Values().end(); it != endIt; ++it) { if (!equalTo(lv, *it)) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } } - QueryEntry &dst = needSwitch == NeedSwitch::Yes ? set : allSet; - for (const Variant &rv : set.Values()) { + QueryEntry& dst = needSwitch == NeedSwitch::Yes ? set : allSet; + for (const Variant& rv : set.Values()) { if (equalTo(lv, rv)) { dst.Distinct(distinct); dst.SetCondAndValues(CondEq, VariantArray{std::move(std::move(allSet).Values()[0])}); @@ -849,22 +884,22 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetSet(Nee } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetAllSet(QueryEntry &lqe, QueryEntry &rqe, bool distinct, - size_t position, const CmpArgs &...args) { +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetAllSet(QueryEntry& lqe, QueryEntry& rqe, bool distinct, + size_t position, const CmpArgs&... args) { if rx_unlikely (lqe.Values().empty() || rqe.Values().empty()) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } const MergeEqualT equalTo{args...}; const auto lvIt = lqe.Values().begin(); - const Variant &lv = *lvIt; + const Variant& lv = *lvIt; for (auto it = lvIt + 1, endIt = lqe.Values().end(); it != endIt; ++it) { if (!equalTo(lv, *it)) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } } - for (const Variant &rv : rqe.Values()) { + for (const Variant& rv : rqe.Values()) { if (!equalTo(lv, rv)) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; @@ -875,7 +910,7 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetAllSet( return MergeResult::Merged; } -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAny(NeedSwitch needSwitch, QueryEntry &any, QueryEntry ¬Any, +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAny(NeedSwitch needSwitch, QueryEntry& any, QueryEntry& notAny, bool distinct, size_t position) { if (notAny.Condition() == CondEmpty) { SetValue(position, AlwaysFalse{}); @@ -889,7 +924,7 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAny(NeedSwitc } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetNotSet(NeedSwitch needSwitch, QueryEntry &set, QueryEntry ¬Set, +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetNotSet(NeedSwitch needSwitch, QueryEntry& set, QueryEntry& notSet, F filter, bool distinct, size_t position, MergeOrdered mergeOrdered) { if rx_unlikely (set.Values().empty()) { @@ -898,7 +933,7 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetNotSet(Nee } { auto updatableValues = set.UpdatableValues(); - VariantArray &values = updatableValues; + VariantArray& values = updatableValues; values.erase(std::remove_if(values.begin(), values.end(), filter), values.end()); } if rx_unlikely (set.Values().empty()) { @@ -917,16 +952,16 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesSetNotSet(Nee } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetNotSet(NeedSwitch needSwitch, QueryEntry &allSet, - QueryEntry ¬Set, F filter, bool distinct, - size_t position, const CmpArgs &...args) { +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetNotSet(NeedSwitch needSwitch, QueryEntry& allSet, + QueryEntry& notSet, F filter, bool distinct, + size_t position, const CmpArgs&... args) { if rx_unlikely (allSet.Values().empty()) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } const MergeEqualT equalTo{args...}; const auto lvIt = allSet.Values().begin(); - const Variant &lv = *lvIt; + const Variant& lv = *lvIt; for (auto it = lvIt + 1, endIt = allSet.Values().end(); it != endIt; ++it) { if (!equalTo(lv, *it)) { SetValue(position, AlwaysFalse{}); @@ -937,17 +972,17 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesAllSetNotSet( SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; } - QueryEntry &dst = needSwitch == NeedSwitch::Yes ? notSet : allSet; + QueryEntry& dst = needSwitch == NeedSwitch::Yes ? notSet : allSet; dst.Distinct(distinct); dst.SetCondAndValues(CondEq, VariantArray{std::move(std::move(allSet).Values()[0])}); return MergeResult::Merged; } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLt(QueryEntry &lqe, QueryEntry &rqe, bool distinct, - const CmpArgs &...args) { - const Variant &lv = lqe.Values()[0]; - const Variant &rv = rqe.Values()[0]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLt(QueryEntry& lqe, QueryEntry& rqe, bool distinct, + const CmpArgs&... args) { + const Variant& lv = lqe.Values()[0]; + const Variant& rv = rqe.Values()[0]; const MergeLessT less{args...}; if (less(rv, lv)) { lqe.SetCondAndValues(rqe.Condition(), std::move(rqe).Values()); // NOLINT (bugprone-use-after-move) @@ -959,10 +994,10 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLt(QueryEntry } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesGt(QueryEntry &lqe, QueryEntry &rqe, bool distinct, - const CmpArgs &...args) { - const Variant &lv = lqe.Values()[0]; - const Variant &rv = rqe.Values()[0]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesGt(QueryEntry& lqe, QueryEntry& rqe, bool distinct, + const CmpArgs&... args) { + const Variant& lv = lqe.Values()[0]; + const Variant& rv = rqe.Values()[0]; if (MergeLessT{args...}(lv, rv)) { lqe.SetCondAndValues(rqe.Condition(), std::move(rqe).Values()); // NOLINT (bugprone-use-after-move) } else if (MergeEqualT{args...}(lv, rv) && (lqe.Condition() != rqe.Condition())) { @@ -973,10 +1008,10 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesGt(QueryEntry } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLtGt(QueryEntry <, QueryEntry >, size_t position, - const CmpArgs &...args) { - const Variant <V = lt.Values()[0]; - const Variant >V = gt.Values()[0]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLtGt(QueryEntry& lt, QueryEntry& gt, size_t position, + const CmpArgs&... args) { + const Variant& ltV = lt.Values()[0]; + const Variant& gtV = gt.Values()[0]; if (MergeLessT{args...}(gtV, ltV)) { return MergeResult::NotMerged; } else { @@ -986,13 +1021,13 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLtGt(QueryEnt } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLeGe(NeedSwitch needSwitch, QueryEntry &le, QueryEntry &ge, - bool distinct, size_t position, const CmpArgs &...args) { - const Variant &leV = le.Values()[0]; - const Variant &geV = ge.Values()[0]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLeGe(NeedSwitch needSwitch, QueryEntry& le, QueryEntry& ge, + bool distinct, size_t position, const CmpArgs&... args) { + const Variant& leV = le.Values()[0]; + const Variant& geV = ge.Values()[0]; const MergeLessT less{args...}; - QueryEntry &target = needSwitch == NeedSwitch::Yes ? ge : le; - QueryEntry &source = needSwitch == NeedSwitch::Yes ? le : ge; + QueryEntry& target = needSwitch == NeedSwitch::Yes ? ge : le; + QueryEntry& source = needSwitch == NeedSwitch::Yes ? le : ge; if (less(leV, geV)) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; @@ -1006,11 +1041,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesLeGe(NeedSwit } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeLt(NeedSwitch needSwitch, QueryEntry &range, QueryEntry <, - bool distinct, size_t position, const CmpArgs &...args) { - const Variant <V = lt.Values()[0]; - const Variant &rngL = range.Values()[0]; - const Variant &rngR = range.Values()[1]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeLt(NeedSwitch needSwitch, QueryEntry& range, QueryEntry& lt, + bool distinct, size_t position, const CmpArgs&... args) { + const Variant& ltV = lt.Values()[0]; + const Variant& rngL = range.Values()[0]; + const Variant& rngR = range.Values()[1]; const MergeLessT less{args...}; if (!less(rngL, ltV)) { SetValue(position, AlwaysFalse{}); @@ -1027,11 +1062,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeLt(NeedS } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeGt(NeedSwitch needSwitch, QueryEntry &range, QueryEntry >, - bool distinct, size_t position, const CmpArgs &...args) { - const Variant >V = gt.Values()[0]; - const Variant &rngL = range.Values()[0]; - const Variant &rngR = range.Values()[1]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeGt(NeedSwitch needSwitch, QueryEntry& range, QueryEntry& gt, + bool distinct, size_t position, const CmpArgs&... args) { + const Variant& gtV = gt.Values()[0]; + const Variant& rngL = range.Values()[0]; + const Variant& rngR = range.Values()[1]; const MergeLessT less{args...}; if (!less(gtV, rngR)) { SetValue(position, AlwaysFalse{}); @@ -1048,13 +1083,13 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeGt(NeedS } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeLe(NeedSwitch needSwitch, QueryEntry &range, QueryEntry &le, - bool distinct, size_t position, const CmpArgs &...args) { - const Variant &leV = le.Values()[0]; - const Variant &rngL = range.Values()[0]; - const Variant &rngR = range.Values()[1]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeLe(NeedSwitch needSwitch, QueryEntry& range, QueryEntry& le, + bool distinct, size_t position, const CmpArgs&... args) { + const Variant& leV = le.Values()[0]; + const Variant& rngL = range.Values()[0]; + const Variant& rngR = range.Values()[1]; const MergeLessT less{args...}; - QueryEntry &target = needSwitch == NeedSwitch::Yes ? le : range; + QueryEntry& target = needSwitch == NeedSwitch::Yes ? le : range; if (less(leV, rngL)) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; @@ -1074,13 +1109,13 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeLe(NeedS } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeGe(NeedSwitch needSwitch, QueryEntry &range, QueryEntry &ge, - bool distinct, size_t position, const CmpArgs &...args) { - const Variant &geV = ge.Values()[0]; - const Variant &rngL = range.Values()[0]; - const Variant &rngR = range.Values()[1]; +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeGe(NeedSwitch needSwitch, QueryEntry& range, QueryEntry& ge, + bool distinct, size_t position, const CmpArgs&... args) { + const Variant& geV = ge.Values()[0]; + const Variant& rngL = range.Values()[0]; + const Variant& rngR = range.Values()[1]; const MergeLessT less{args...}; - QueryEntry &target = needSwitch == NeedSwitch::Yes ? ge : range; + QueryEntry& target = needSwitch == NeedSwitch::Yes ? ge : range; if (less(rngR, geV)) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; @@ -1100,11 +1135,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRangeGe(NeedS } template -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRange(QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position, - const CmpArgs &...args) { +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRange(QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position, + const CmpArgs&... args) { const MergeLessT less{args...}; - QueryEntry &left = less(lqe.Values()[0], rqe.Values()[0]) ? rqe : lqe; - QueryEntry &right = less(rqe.Values()[1], lqe.Values()[1]) ? rqe : lqe; + QueryEntry& left = less(lqe.Values()[0], rqe.Values()[0]) ? rqe : lqe; + QueryEntry& right = less(rqe.Values()[1], lqe.Values()[1]) ? rqe : lqe; if (less(right.Values()[1], left.Values()[0])) { SetValue(position, AlwaysFalse{}); return MergeResult::Annihilated; @@ -1118,7 +1153,7 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesRange(QueryEn return MergeResult::Merged; } -QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesDWithin(QueryEntry &lqe, QueryEntry &rqe, bool distinct, +QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesDWithin(QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position) { Point lp, rp; double ld, rd; @@ -1151,9 +1186,9 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntriesDWithin(Query template QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, size_t rhs, MergeOrdered mergeOrdered, - const CmpArgs &...args) { - auto &lqe = Get(lhs); - auto &rqe = Get(rhs); + const CmpArgs&... args) { + auto& lqe = Get(lhs); + auto& rqe = Get(rhs); const bool distinct = lqe.Distinct() || rqe.Distinct(); const MergeLessT less{args...}; switch (lqe.Condition()) { @@ -1167,24 +1202,24 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, return mergeQueryEntriesAllSetSet(NeedSwitch::Yes, rqe, lqe, distinct, lhs, args...); case CondLt: return mergeQueryEntriesSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return !less(v, rv); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return !less(v, rv); }, distinct, lhs, mergeOrdered); case CondLe: return mergeQueryEntriesSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return less(rv, v); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return less(rv, v); }, distinct, lhs, mergeOrdered); case CondGe: return mergeQueryEntriesSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return less(v, rv); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return less(v, rv); }, distinct, lhs, mergeOrdered); case CondGt: return mergeQueryEntriesSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return !less(rv, v); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return !less(rv, v); }, distinct, lhs, mergeOrdered); case CondRange: return mergeQueryEntriesSetNotSet( NeedSwitch::No, lqe, rqe, - [&rv1 = rqe.Values()[0], &rv2 = rqe.Values()[1], &less](const Variant &v) { return less(v, rv1) || less(rv2, v); }, + [&rv1 = rqe.Values()[0], &rv2 = rqe.Values()[1], &less](const Variant& v) { return less(v, rv1) || less(rv2, v); }, distinct, lhs, mergeOrdered); case CondAny: return mergeQueryEntriesAny(NeedSwitch::No, rqe, lqe, distinct, lhs); @@ -1205,24 +1240,24 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, return mergeQueryEntriesAllSetAllSet(lqe, rqe, distinct, lhs, args...); case CondLt: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return !less(v, rv); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return !less(v, rv); }, distinct, lhs, args...); case CondLe: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return less(rv, v); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return less(rv, v); }, distinct, lhs, args...); case CondGe: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return less(v, rv); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return less(v, rv); }, distinct, lhs, args...); case CondGt: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant &v) { return !less(rv, v); }, distinct, lhs, + NeedSwitch::No, lqe, rqe, [&rv = rqe.Values()[0], &less](const Variant& v) { return !less(rv, v); }, distinct, lhs, args...); case CondRange: return mergeQueryEntriesAllSetNotSet( NeedSwitch::No, lqe, rqe, - [&rv1 = rqe.Values()[0], &rv2 = rqe.Values()[1], &less](const Variant &v) { return less(v, rv1) || less(rv2, v); }, + [&rv1 = rqe.Values()[0], &rv2 = rqe.Values()[1], &less](const Variant& v) { return less(v, rv1) || less(rv2, v); }, distinct, lhs, args...); case CondAny: return mergeQueryEntriesAny(NeedSwitch::No, rqe, lqe, distinct, lhs); @@ -1239,11 +1274,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, case CondEq: case CondSet: return mergeQueryEntriesSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return !less(v, lv); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return !less(v, lv); }, distinct, lhs, mergeOrdered); case CondAllSet: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return !less(v, lv); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return !less(v, lv); }, distinct, lhs, args...); case CondAny: return mergeQueryEntriesAny(NeedSwitch::No, rqe, lqe, distinct, lhs); @@ -1268,11 +1303,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, case CondEq: case CondSet: return mergeQueryEntriesSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return less(lv, v); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return less(lv, v); }, distinct, lhs, mergeOrdered); case CondAllSet: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return less(lv, v); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return less(lv, v); }, distinct, lhs, args...); case CondLt: case CondLe: @@ -1298,11 +1333,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, case CondEq: case CondSet: return mergeQueryEntriesSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return !less(lv, v); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return !less(lv, v); }, distinct, lhs, mergeOrdered); case CondAllSet: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return !less(lv, v); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return !less(lv, v); }, distinct, lhs, args...); case CondGt: case CondGe: @@ -1327,11 +1362,11 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, case CondEq: case CondSet: return mergeQueryEntriesSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return less(v, lv); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return less(v, lv); }, distinct, lhs, mergeOrdered); case CondAllSet: return mergeQueryEntriesAllSetNotSet( - NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant &v) { return less(v, lv); }, distinct, lhs, + NeedSwitch::Yes, rqe, lqe, [&lv = lqe.Values()[0], &less](const Variant& v) { return less(v, lv); }, distinct, lhs, args...); case CondGt: case CondGe: @@ -1358,12 +1393,12 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, case CondSet: return mergeQueryEntriesSetNotSet( NeedSwitch::Yes, rqe, lqe, - [&lv1 = lqe.Values()[0], &lv2 = lqe.Values()[1], &less](const Variant &v) { return less(v, lv1) || less(lv2, v); }, + [&lv1 = lqe.Values()[0], &lv2 = lqe.Values()[1], &less](const Variant& v) { return less(v, lv1) || less(lv2, v); }, distinct, lhs, mergeOrdered); case CondAllSet: return mergeQueryEntriesAllSetNotSet( NeedSwitch::Yes, rqe, lqe, - [&lv1 = lqe.Values()[0], &lv2 = lqe.Values()[1], &less](const Variant &v) { return less(v, lv1) || less(lv2, v); }, + [&lv1 = lqe.Values()[0], &lv2 = lqe.Values()[1], &less](const Variant& v) { return less(v, lv1) || less(lv2, v); }, distinct, lhs, args...); case CondLt: return mergeQueryEntriesRangeLt(NeedSwitch::No, lqe, rqe, distinct, lhs, args...); @@ -1431,26 +1466,28 @@ QueryPreprocessor::MergeResult QueryPreprocessor::mergeQueryEntries(size_t lhs, return MergeResult::NotMerged; } -void QueryPreprocessor::AddDistinctEntries(const h_vector &aggregators) { +void QueryPreprocessor::AddDistinctEntries(const h_vector& aggregators) { bool wasAdded = false; - for (auto &ag : aggregators) { - if (ag.Type() != AggDistinct) continue; + for (auto& ag : aggregators) { + if (ag.Type() != AggDistinct) { + continue; + } assertrx_throw(ag.Names().size() == 1); Append(wasAdded ? OpOr : OpAnd, ag.Names()[0], QueryEntry::DistinctTag{}); - auto &qe = Get(LastAppendedElement()); + auto& qe = Get(LastAppendedElement()); SetQueryField(qe.FieldData(), ns_); checkStrictMode(qe.FieldData()); wasAdded = true; } } -std::pair QueryPreprocessor::queryValuesFromOnCondition(std::string &explainStr, AggType &oAggType, - NamespaceImpl &rightNs, Query joinQuery, +std::pair QueryPreprocessor::queryValuesFromOnCondition(std::string& explainStr, AggType& oAggType, + NamespaceImpl& rightNs, Query joinQuery, JoinPreResult::CPtr joinPreresult, - const QueryJoinEntry &joinEntry, CondType condition, - int mainQueryMaxIterations, const RdxContext &rdxCtx) { + const QueryJoinEntry& joinEntry, CondType condition, + int mainQueryMaxIterations, const RdxContext& rdxCtx) { size_t limit = 0; - const auto &rNsCfg = rightNs.config(); + const auto& rNsCfg = rightNs.config(); if (rNsCfg.maxPreselectSize == 0) { limit = std::max(rNsCfg.minPreselectSize, rightNs.itemsCount() * rNsCfg.maxPreselectPart); } else if (rNsCfg.maxPreselectPart == 0.0) { @@ -1497,7 +1534,7 @@ std::pair QueryPreprocessor::queryValuesFromOnCondition( return {CondAny, {}}; } assertrx_throw(qr.aggregationResults.size() == 1); - auto &aggRes = qr.aggregationResults[0]; + auto& aggRes = qr.aggregationResults[0]; explainStr = qr.explainResults; switch (condition) { case CondEq: @@ -1505,9 +1542,9 @@ std::pair QueryPreprocessor::queryValuesFromOnCondition( assertrx_throw(aggRes.type == AggDistinct); VariantArray values; values.reserve(aggRes.distincts.size()); - for (Variant &distValue : aggRes.distincts) { + for (Variant& distValue : aggRes.distincts) { if (distValue.Type().Is()) { - ConstPayload pl(aggRes.payloadType, distValue.operator const PayloadValue &()); + ConstPayload pl(aggRes.payloadType, distValue.operator const PayloadValue&()); values.emplace_back(pl.GetComposite(aggRes.distinctsFields, joinEntry.RightCompositeFieldsTypes())); } else { values.emplace_back(std::move(distValue)); @@ -1535,9 +1572,9 @@ std::pair QueryPreprocessor::queryValuesFromOnCondition( } } -std::pair QueryPreprocessor::queryValuesFromOnCondition(CondType condition, const QueryJoinEntry &joinEntry, - const JoinedSelector &joinedSelector, - const CollateOpts &collate) { +std::pair QueryPreprocessor::queryValuesFromOnCondition(CondType condition, const QueryJoinEntry& joinEntry, + const JoinedSelector& joinedSelector, + const CollateOpts& collate) { switch (condition) { case CondEq: case CondSet: @@ -1546,13 +1583,13 @@ std::pair QueryPreprocessor::queryValuesFromOnCondition( case CondLe: case CondGt: case CondGe: { - const JoinPreResult::Values &values = std::get(joinedSelector.PreResult().payload); + const JoinPreResult::Values& values = std::get(joinedSelector.PreResult().payload); VariantArray buffer, keyValues; - for (const ItemRef &item : values) { + for (const ItemRef& item : values) { assertrx_throw(!item.Value().IsFree()); const ConstPayload pl{values.payloadType, item.Value()}; pl.GetByFieldsSet(joinEntry.RightFields(), buffer, joinEntry.RightFieldType(), joinEntry.RightCompositeFieldsTypes()); - for (Variant &v : buffer) { + for (Variant& v : buffer) { if (keyValues.empty()) { keyValues.emplace_back(std::move(v)); } else { @@ -1583,41 +1620,41 @@ std::pair QueryPreprocessor::queryValuesFromOnCondition( } template -void QueryPreprocessor::briefDump(size_t from, size_t to, const std::vector &joinedSelectors, WrSerializer &ser) const { +void QueryPreprocessor::briefDump(size_t from, size_t to, const std::vector& joinedSelectors, WrSerializer& ser) const { { for (auto it = from; it < to; it = Next(it)) { if (it != from || container_[it].operation != OpAnd) { ser << container_[it].operation << ' '; } - container_[it].Visit([](const SubQueryEntry &) { throw_as_assert; }, [](const SubQueryFieldEntry &) { throw_as_assert; }, - [&](const QueryEntriesBracket &b) { + container_[it].Visit([](const SubQueryEntry&) { throw_as_assert; }, [](const SubQueryFieldEntry&) { throw_as_assert; }, + [&](const QueryEntriesBracket& b) { ser << "("; briefDump(it + 1, Next(it), joinedSelectors, ser); dumpEqualPositions(0, ser, b.equalPositions); ser << ")"; }, - [&ser](const QueryEntry &qe) { ser << qe.DumpBrief() << ' '; }, - [&joinedSelectors, &ser](const JoinQueryEntry &jqe) { ser << jqe.Dump(joinedSelectors) << ' '; }, - [&ser](const BetweenFieldsQueryEntry &qe) { ser << qe.Dump() << ' '; }, - [&ser](const AlwaysFalse &) { ser << "AlwaysFalse" << ' '; }, - [&ser](const AlwaysTrue &) { ser << "AlwaysTrue" << ' '; }); + [&ser](const QueryEntry& qe) { ser << qe.DumpBrief() << ' '; }, + [&joinedSelectors, &ser](const JoinQueryEntry& jqe) { ser << jqe.Dump(joinedSelectors) << ' '; }, + [&ser](const BetweenFieldsQueryEntry& qe) { ser << qe.Dump() << ' '; }, + [&ser](const AlwaysFalse&) { ser << "AlwaysFalse" << ' '; }, + [&ser](const AlwaysTrue&) { ser << "AlwaysTrue" << ' '; }); } } } template -size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to, JoinedSelectors &js, - OnConditionInjections &explainOnInjections, int embracedMaxIterations, - h_vector &maxIterations, bool inTransaction, bool enableSortOrders, - const RdxContext &rdxCtx) { +size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to, JoinedSelectors& js, + OnConditionInjections& explainOnInjections, int embracedMaxIterations, + h_vector& maxIterations, bool inTransaction, bool enableSortOrders, + const RdxContext& rdxCtx) { using namespace std::string_view_literals; size_t injectedCount = 0; for (size_t cur = from; cur < to; cur = Next(cur)) { container_[cur].Visit( - [](const SubQueryEntry &) { throw_as_assert; }, [](const SubQueryFieldEntry &) { throw_as_assert; }, + [](const SubQueryEntry&) { throw_as_assert; }, [](const SubQueryFieldEntry&) { throw_as_assert; }, Skip{}, - [&](const QueryEntriesBracket &) { + [&](const QueryEntriesBracket&) { const size_t injCount = injectConditionsFromJoins(cur + 1, Next(cur), js, explainOnInjections, maxIterations[cur], maxIterations, inTransaction, enableSortOrders, rdxCtx); @@ -1625,11 +1662,11 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to injectedCount += injCount; assertrx_throw(to <= container_.size()); }, - [&](const JoinQueryEntry &jqe) { + [&](const JoinQueryEntry& jqe) { const auto joinIndex = jqe.joinIndex; assertrx_throw(js.size() > joinIndex); - JoinedSelector &joinedSelector = js[joinIndex]; - const JoinPreResult &preResult = joinedSelector.PreResult(); + JoinedSelector& joinedSelector = js[joinIndex]; + const JoinPreResult& preResult = joinedSelector.PreResult(); assertrx_throw(joinedSelector.PreSelectMode() == JoinPreSelectMode::Execute); const bool byValues = std::holds_alternative(preResult.payload); @@ -1638,7 +1675,7 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to // Checking if we are able to preselect something from RightNs, or there are preselected results if (!byValues) { - const auto &rNsCfg = joinedSelector.RightNs()->config(); + const auto& rNsCfg = joinedSelector.RightNs()->config(); if (rNsCfg.maxPreselectSize == 0 && rNsCfg.maxPreselectPart == 0.0) { explainJoinOn.Skipped("maxPreselectSize and maxPreselectPart == 0"sv); return; @@ -1649,12 +1686,12 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to return; } } - const auto &joinEntries = joinedSelector.joinQuery_.joinEntries_; + const auto& joinEntries = joinedSelector.joinQuery_.joinEntries_; // LeftJoin-s shall not be in QueryEntries container_ by construction assertrx_throw(joinedSelector.Type() == InnerJoin || joinedSelector.Type() == OrInnerJoin); // Checking if we have anything to inject into main Where clause bool foundANDOrOR = false; - for (const auto &je : joinEntries) { + for (const auto& je : joinEntries) { if (je.Operation() != OpNot) { foundANDOrOR = true; break; @@ -1667,7 +1704,9 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to OpType op = GetOperation(cur); if (joinedSelector.Type() == OrInnerJoin) { - if (op == OpNot) throw Error(errQueryExec, "OR INNER JOIN with operation NOT"); + if (op == OpNot) { + throw Error(errQueryExec, "OR INNER JOIN with operation NOT"); + } op = OpOr; joinedSelector.SetType(InnerJoin); } @@ -1698,7 +1737,7 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to bool prevIsSkipped = false; size_t orChainLength = 0; for (size_t i = 0, s = joinEntries.size(); i < s; ++i) { - const QueryJoinEntry &joinEntry = joinEntries[i]; + const QueryJoinEntry& joinEntry = joinEntries[i]; auto explainEntry = explainJoinOn.AppendOnEntryExplain(); explainEntry.InitialCondition(joinEntry, joinedSelector); CondType condition = joinEntry.Condition(); @@ -1751,7 +1790,7 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to assertrx_throw(joinedSelector.itemQuery_.Entries().Is(i)); assertrx_throw(joinedSelector.itemQuery_.Entries().Get(i).FieldName() == joinEntry.RightFieldName()); static const CollateOpts collate; - const CollateOpts *collatePtr = &collate; + const CollateOpts* collatePtr = &collate; if (joinEntry.IsLeftFieldIndexed()) { collatePtr = &ns_.indexes_[joinEntry.LeftIdxNo()]->Opts().collateOpts_; } @@ -1836,7 +1875,7 @@ size_t QueryPreprocessor::injectConditionsFromJoins(const size_t from, size_t to maxIterations.insert(maxIterations.begin() + (cur - count), embracedMaxIterations); explainJoinOn.Succeed( - [this, cur, count, &js](WrSerializer &ser) { briefDump(cur - count, Next(cur - count), js, ser); }); + [this, cur, count, &js](WrSerializer& ser) { briefDump(cur - count, Next(cur - count), js, ser); }); ++cur; injectedCount += count + 1; @@ -1854,18 +1893,18 @@ class JoinOnExplainDisabled { struct OnEntryExplain { OnEntryExplain() noexcept = default; - RX_ALWAYS_INLINE void InitialCondition(const QueryJoinEntry &, const JoinedSelector &) const noexcept {} - RX_ALWAYS_INLINE void Succeed(const QueryEntry &) const noexcept {} + RX_ALWAYS_INLINE void InitialCondition(const QueryJoinEntry&, const JoinedSelector&) const noexcept {} + RX_ALWAYS_INLINE void Succeed(const QueryEntry&) const noexcept {} RX_ALWAYS_INLINE void Skipped(std::string_view) const noexcept {} RX_ALWAYS_INLINE void OrChainPart(bool) const noexcept {} - RX_ALWAYS_INLINE void ExplainSelect(std::string &&, AggType) const noexcept {} + RX_ALWAYS_INLINE void ExplainSelect(std::string&&, AggType) const noexcept {} }; public: - [[nodiscard]] RX_ALWAYS_INLINE static JoinOnExplainDisabled AppendJoinOnExplain(OnConditionInjections &) noexcept { return {}; } + [[nodiscard]] RX_ALWAYS_INLINE static JoinOnExplainDisabled AppendJoinOnExplain(OnConditionInjections&) noexcept { return {}; } - RX_ALWAYS_INLINE void Init(const JoinQueryEntry &, const JoinedSelectors &, bool) const noexcept {} - RX_ALWAYS_INLINE void Succeed(const std::function &) const noexcept {} + RX_ALWAYS_INLINE void Init(const JoinQueryEntry&, const JoinedSelectors&, bool) const noexcept {} + RX_ALWAYS_INLINE void Succeed(const std::function&) const noexcept {} RX_ALWAYS_INLINE void Skipped(std::string_view) const noexcept {} RX_ALWAYS_INLINE void ReserveOnEntries(size_t) const noexcept {} [[nodiscard]] RX_ALWAYS_INLINE OnEntryExplain AppendOnEntryExplain() const noexcept { return {}; } @@ -1876,17 +1915,17 @@ class JoinOnExplainDisabled { class JoinOnExplainEnabled { using time_point_t = ExplainCalc::Clock::time_point; struct OnEntryExplain { - OnEntryExplain(ConditionInjection &explainEntry) noexcept : startTime_(ExplainCalc::Clock::now()), explainEntry_(explainEntry) {} + OnEntryExplain(ConditionInjection& explainEntry) noexcept : startTime_(ExplainCalc::Clock::now()), explainEntry_(explainEntry) {} ~OnEntryExplain() noexcept { explainEntry_.totalTime_ = ExplainCalc::Clock::now() - startTime_; } - OnEntryExplain(const OnEntryExplain &) = delete; - OnEntryExplain(OnEntryExplain &&) = delete; - OnEntryExplain &operator=(const OnEntryExplain &) = delete; - OnEntryExplain &operator=(OnEntryExplain &&) = delete; + OnEntryExplain(const OnEntryExplain&) = delete; + OnEntryExplain(OnEntryExplain&&) = delete; + OnEntryExplain& operator=(const OnEntryExplain&) = delete; + OnEntryExplain& operator=(OnEntryExplain&&) = delete; - void InitialCondition(const QueryJoinEntry &joinEntry, const JoinedSelector &joinedSelector) { + void InitialCondition(const QueryJoinEntry& joinEntry, const JoinedSelector& joinedSelector) { explainEntry_.initCond = joinEntry.DumpCondition(joinedSelector); } - void Succeed(const QueryEntry &newEntry) { + void Succeed(const QueryEntry& newEntry) { explainEntry_.succeed = true; explainEntry_.reason = ""; explainEntry_.newCond = newEntry.DumpBrief(); @@ -1901,36 +1940,36 @@ class JoinOnExplainEnabled { } void OrChainPart(bool orChainPart) noexcept { explainEntry_.orChainPart_ = orChainPart; } - void ExplainSelect(std::string &&explain, AggType aggType) noexcept { + void ExplainSelect(std::string&& explain, AggType aggType) noexcept { explainEntry_.explain = std::move(explain); explainEntry_.aggType = aggType; } private: time_point_t startTime_; - ConditionInjection &explainEntry_; + ConditionInjection& explainEntry_; }; - JoinOnExplainEnabled(JoinOnInjection &joinOn) noexcept : explainJoinOn_(joinOn), startTime_(ExplainCalc::Clock::now()) {} + JoinOnExplainEnabled(JoinOnInjection& joinOn) noexcept : explainJoinOn_(joinOn), startTime_(ExplainCalc::Clock::now()) {} public: - JoinOnExplainEnabled(const JoinOnExplainEnabled &) = delete; - JoinOnExplainEnabled(JoinOnExplainEnabled &&) = delete; - JoinOnExplainEnabled &operator=(const JoinOnExplainEnabled &) = delete; - JoinOnExplainEnabled &operator=(JoinOnExplainEnabled &&) = delete; + JoinOnExplainEnabled(const JoinOnExplainEnabled&) = delete; + JoinOnExplainEnabled(JoinOnExplainEnabled&&) = delete; + JoinOnExplainEnabled& operator=(const JoinOnExplainEnabled&) = delete; + JoinOnExplainEnabled& operator=(JoinOnExplainEnabled&&) = delete; - [[nodiscard]] static JoinOnExplainEnabled AppendJoinOnExplain(OnConditionInjections &explainOnInjections) { + [[nodiscard]] static JoinOnExplainEnabled AppendJoinOnExplain(OnConditionInjections& explainOnInjections) { return {explainOnInjections.emplace_back()}; } ~JoinOnExplainEnabled() noexcept { explainJoinOn_.totalTime_ = ExplainCalc::Clock::now() - startTime_; } - void Init(const JoinQueryEntry &jqe, const JoinedSelectors &js, bool byValues) { - const JoinedSelector &joinedSelector = js[jqe.joinIndex]; + void Init(const JoinQueryEntry& jqe, const JoinedSelectors& js, bool byValues) { + const JoinedSelector& joinedSelector = js[jqe.joinIndex]; explainJoinOn_.rightNsName = joinedSelector.RightNsName(); explainJoinOn_.joinCond = jqe.DumpOnCondition(js); explainJoinOn_.type = byValues ? JoinOnInjection::ByValue : JoinOnInjection::Select; } - void Succeed(const std::function &setInjectedCond) { + void Succeed(const std::function& setInjectedCond) { explainJoinOn_.succeed = true; setInjectedCond(explainJoinOn_.injectedCond); } @@ -1942,11 +1981,11 @@ class JoinOnExplainEnabled { } void ReserveOnEntries(size_t count) { explainJoinOn_.conditions.reserve(count); } - [[nodiscard]] OnEntryExplain AppendOnEntryExplain() { return {explainJoinOn_.conditions.emplace_back()}; }; + [[nodiscard]] OnEntryExplain AppendOnEntryExplain() { return {explainJoinOn_.conditions.emplace_back()}; } void FailOnEntriesAsOrChain(size_t orChainLength) { using namespace std::string_view_literals; - auto &conditions = explainJoinOn_.conditions; + auto& conditions = explainJoinOn_.conditions; assertrx_throw(conditions.size() >= orChainLength); // Marking On-injections as fail for removed entries. for (size_t jsz = conditions.size(), j = jsz - orChainLength; j < jsz; ++j) { @@ -1956,18 +1995,18 @@ class JoinOnExplainEnabled { } private: - JoinOnInjection &explainJoinOn_; + JoinOnInjection& explainJoinOn_; time_point_t startTime_; }; -void QueryPreprocessor::setQueryIndex(QueryField &qField, int idxNo, const NamespaceImpl &ns) { - const auto &idx = *ns.indexes_[idxNo]; +void QueryPreprocessor::setQueryIndex(QueryField& qField, int idxNo, const NamespaceImpl& ns) { + const auto& idx = *ns.indexes_[idxNo]; QueryField::CompositeTypesVecT compositeFieldsTypes; if (idxNo >= ns.indexes_.firstCompositePos()) { #ifndef NDEBUG const bool ftIdx = IsFullText(idx.Type()); #endif - const auto &fields = idx.Fields(); + const auto& fields = idx.Fields(); compositeFieldsTypes.reserve(fields.size()); for (const auto f : fields) { if rx_likely (f != IndexValueType::SetByJsonPath) { @@ -1983,7 +2022,7 @@ void QueryPreprocessor::setQueryIndex(QueryField &qField, int idxNo, const Names qField.SetIndexData(idxNo, FieldsSet(idx.Fields()), idx.KeyType(), idx.SelectKeyType(), std::move(compositeFieldsTypes)); } -void QueryPreprocessor::SetQueryField(QueryField &qField, const NamespaceImpl &ns) { +void QueryPreprocessor::SetQueryField(QueryField& qField, const NamespaceImpl& ns) { int idxNo = IndexValueType::SetByJsonPath; if (ns.getIndexByNameOrJsonPath(qField.FieldName(), idxNo)) { setQueryIndex(qField, idxNo, ns); diff --git a/cpp_src/core/nsselecter/querypreprocessor.h b/cpp_src/core/nsselecter/querypreprocessor.h index d9c61b935..7c348a13f 100644 --- a/cpp_src/core/nsselecter/querypreprocessor.h +++ b/cpp_src/core/nsselecter/querypreprocessor.h @@ -17,8 +17,8 @@ class QueryPreprocessor : private QueryEntries { public: enum class ValuesType : bool { Scalar, Composite }; - QueryPreprocessor(QueryEntries &&, NamespaceImpl *, const SelectCtx &); - const QueryEntries &GetQueryEntries() const noexcept { return *this; } + QueryPreprocessor(QueryEntries&&, NamespaceImpl*, const SelectCtx&); + const QueryEntries& GetQueryEntries() const noexcept { return *this; } bool LookupQueryIndexes() { const unsigned lookupEnd = queryEntryAddedByForcedSortOptimization_ ? container_.size() - 1 : container_.size(); assertrx_throw(lookupEnd <= uint32_t(std::numeric_limits::max() - 1)); @@ -44,21 +44,23 @@ class QueryPreprocessor : private QueryEntries { return substituteCompositeIndexes(0, container_.size() - queryEntryAddedByForcedSortOptimization_) != 0; } void InitIndexedQueries() { initIndexedQueries(0, Size()); } - void AddDistinctEntries(const h_vector &); - bool NeedNextEvaluation(unsigned start, unsigned count, bool &matchedAtLeastOnce, QresExplainHolder &qresHolder) noexcept; + void AddDistinctEntries(const h_vector&); + bool NeedNextEvaluation(unsigned start, unsigned count, bool& matchedAtLeastOnce, QresExplainHolder& qresHolder) noexcept; unsigned Start() const noexcept { return start_; } unsigned Count() const noexcept { return count_; } bool MoreThanOneEvaluation() const noexcept { return queryEntryAddedByForcedSortOptimization_; } bool AvailableSelectBySortIndex() const noexcept { return !queryEntryAddedByForcedSortOptimization_ || !forcedStage(); } - void InjectConditionsFromJoins(JoinedSelectors &js, OnConditionInjections &expalainOnInjections, LogLevel, bool inTransaction, - bool enableSortOrders, const RdxContext &rdxCtx); + void InjectConditionsFromJoins(JoinedSelectors& js, OnConditionInjections& expalainOnInjections, LogLevel, bool inTransaction, + bool enableSortOrders, const RdxContext& rdxCtx); void Reduce(bool isFt); using QueryEntries::Size; using QueryEntries::Dump; using QueryEntries::ToDsl; template - [[nodiscard]] SortingEntries GetSortingEntries(const SelectCtxWithJoinPreSelect &) const { - if (ftEntry_) return {}; + [[nodiscard]] SortingEntries GetSortingEntries(const SelectCtxWithJoinPreSelect&) const { + if (ftEntry_) { + return {}; + } // DO NOT use deducted sort order in the following cases: // - query contains explicity specified sort order // - query contains FullText query. @@ -72,17 +74,17 @@ class QueryPreprocessor : private QueryEntries { } bool IsFtExcluded() const noexcept { return ftEntry_.has_value(); } - void ExcludeFtQuery(const RdxContext &); - FtMergeStatuses &GetFtMergeStatuses() noexcept { + void ExcludeFtQuery(const RdxContext&); + FtMergeStatuses& GetFtMergeStatuses() noexcept { assertrx_throw(ftPreselect_); return *ftPreselect_; } - FtPreselectT &&MoveFtPreselect() noexcept { + FtPreselectT&& MoveFtPreselect() noexcept { assertrx_throw(ftPreselect_); return std::move(*ftPreselect_); } bool IsFtPreselected() const noexcept { return ftPreselect_ && !ftEntry_; } - static void SetQueryField(QueryField &, const NamespaceImpl &); + static void SetQueryField(QueryField&, const NamespaceImpl&); private: enum class NeedSwitch : bool { Yes = true, No = false }; @@ -92,73 +94,71 @@ class QueryPreprocessor : private QueryEntries { enum class ConditionType { Incompatible = 0, Compatible = 1 }; FoundIndexInfo() noexcept : index(nullptr), size(0), isFitForSortOptimization(0) {} - FoundIndexInfo(const Index *i, ConditionType ct) noexcept : index(i), size(i->Size()), isFitForSortOptimization(unsigned(ct)) {} + FoundIndexInfo(const Index* i, ConditionType ct) noexcept : index(i), size(i->Size()), isFitForSortOptimization(unsigned(ct)) {} - const Index *index; + const Index* index; uint64_t size : 63; uint64_t isFitForSortOptimization : 1; }; - static void setQueryIndex(QueryField &, int idxNo, const NamespaceImpl &); + static void setQueryIndex(QueryField&, int idxNo, const NamespaceImpl&); [[nodiscard]] SortingEntries detectOptimalSortOrder() const; [[nodiscard]] bool forcedStage() const noexcept { return evaluationsCount_ == (desc_ ? 1 : 0); } [[nodiscard]] size_t lookupQueryIndexes(uint16_t dst, uint16_t srcBegin, uint16_t srcEnd); [[nodiscard]] size_t substituteCompositeIndexes(size_t from, size_t to); template - MergeResult mergeQueryEntries(size_t lhs, size_t rhs, MergeOrdered, const CmpArgs &...); + MergeResult mergeQueryEntries(size_t lhs, size_t rhs, MergeOrdered, const CmpArgs&...); template - MergeResult mergeQueryEntriesSetSet(QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesSetSet(QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesAllSetSet(NeedSwitch, QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position, - const CmpArgs &...); + MergeResult mergeQueryEntriesAllSetSet(NeedSwitch, QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesAllSetAllSet(QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position, const CmpArgs &...); - MergeResult mergeQueryEntriesAny(NeedSwitch, QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position); + MergeResult mergeQueryEntriesAllSetAllSet(QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position, const CmpArgs&...); + MergeResult mergeQueryEntriesAny(NeedSwitch, QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position); template - MergeResult mergeQueryEntriesSetNotSet(NeedSwitch, QueryEntry &lqe, QueryEntry &rqe, F filter, bool distinct, size_t position, + MergeResult mergeQueryEntriesSetNotSet(NeedSwitch, QueryEntry& lqe, QueryEntry& rqe, F filter, bool distinct, size_t position, MergeOrdered); template - MergeResult mergeQueryEntriesAllSetNotSet(NeedSwitch, QueryEntry &lqe, QueryEntry &rqe, F filter, bool distinct, size_t position, - const CmpArgs &...); - MergeResult mergeQueryEntriesDWithin(QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position); + MergeResult mergeQueryEntriesAllSetNotSet(NeedSwitch, QueryEntry& lqe, QueryEntry& rqe, F filter, bool distinct, size_t position, + const CmpArgs&...); + MergeResult mergeQueryEntriesDWithin(QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position); template - MergeResult mergeQueryEntriesLt(QueryEntry &lqe, QueryEntry &rqe, bool distinct, const CmpArgs &...); + MergeResult mergeQueryEntriesLt(QueryEntry& lqe, QueryEntry& rqe, bool distinct, const CmpArgs&...); template - MergeResult mergeQueryEntriesGt(QueryEntry &lqe, QueryEntry &rqe, bool distinct, const CmpArgs &...); + MergeResult mergeQueryEntriesGt(QueryEntry& lqe, QueryEntry& rqe, bool distinct, const CmpArgs&...); template - MergeResult mergeQueryEntriesLtGt(QueryEntry &lqe, QueryEntry &rqe, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesLtGt(QueryEntry& lqe, QueryEntry& rqe, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesLeGe(NeedSwitch, QueryEntry &lqe, QueryEntry &rqe, bool distinct, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesLeGe(NeedSwitch, QueryEntry& lqe, QueryEntry& rqe, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesRangeLt(NeedSwitch, QueryEntry &range, QueryEntry &ge, bool distinct, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesRangeLt(NeedSwitch, QueryEntry& range, QueryEntry& ge, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesRangeLe(NeedSwitch, QueryEntry &range, QueryEntry &ge, bool distinct, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesRangeLe(NeedSwitch, QueryEntry& range, QueryEntry& ge, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesRangeGt(NeedSwitch, QueryEntry &range, QueryEntry &ge, bool distinct, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesRangeGt(NeedSwitch, QueryEntry& range, QueryEntry& ge, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesRangeGe(NeedSwitch, QueryEntry &range, QueryEntry &ge, bool distinct, size_t position, const CmpArgs &...); + MergeResult mergeQueryEntriesRangeGe(NeedSwitch, QueryEntry& range, QueryEntry& ge, bool distinct, size_t position, const CmpArgs&...); template - MergeResult mergeQueryEntriesRange(QueryEntry &range, QueryEntry &ge, bool distinct, size_t position, const CmpArgs &...); - [[nodiscard]] const std::vector *getCompositeIndex(int field) const noexcept; + MergeResult mergeQueryEntriesRange(QueryEntry& range, QueryEntry& ge, bool distinct, size_t position, const CmpArgs&...); + [[nodiscard]] const std::vector* getCompositeIndex(int field) const noexcept; void initIndexedQueries(size_t begin, size_t end); - [[nodiscard]] const Index *findMaxIndex(QueryEntries::const_iterator begin, QueryEntries::const_iterator end) const; + [[nodiscard]] const Index* findMaxIndex(QueryEntries::const_iterator begin, QueryEntries::const_iterator end) const; void findMaxIndex(QueryEntries::const_iterator begin, QueryEntries::const_iterator end, - h_vector &foundIndexes) const; + h_vector& foundIndexes) const; /** @brief recurrently checks and injects Join ON conditions * @returns injected conditions and EntryBrackets count */ template - size_t injectConditionsFromJoins(size_t from, size_t to, JoinedSelectors &, OnConditionInjections &, int embracedMaxIterations, - h_vector &maxIterations, bool inTransaction, bool enableSortOrders, const RdxContext &); - [[nodiscard]] std::pair queryValuesFromOnCondition(std::string &outExplainStr, AggType &, - NamespaceImpl &rightNs, Query joinQuery, JoinPreResult::CPtr, - const QueryJoinEntry &, CondType, int mainQueryMaxIterations, - const RdxContext &); - [[nodiscard]] std::pair queryValuesFromOnCondition(CondType condition, const QueryJoinEntry &, - const JoinedSelector &, const CollateOpts &); - void checkStrictMode(const QueryField &) const; - [[nodiscard]] int calculateMaxIterations(const size_t from, const size_t to, int maxMaxIters, span &maxIterations, - bool inTransaction, bool enableSortOrders, const RdxContext &) const; + size_t injectConditionsFromJoins(size_t from, size_t to, JoinedSelectors&, OnConditionInjections&, int embracedMaxIterations, + h_vector& maxIterations, bool inTransaction, bool enableSortOrders, const RdxContext&); + [[nodiscard]] std::pair queryValuesFromOnCondition(std::string& outExplainStr, AggType&, NamespaceImpl& rightNs, + Query joinQuery, JoinPreResult::CPtr, const QueryJoinEntry&, + CondType, int mainQueryMaxIterations, const RdxContext&); + [[nodiscard]] std::pair queryValuesFromOnCondition(CondType condition, const QueryJoinEntry&, + const JoinedSelector&, const CollateOpts&); + void checkStrictMode(const QueryField&) const; + [[nodiscard]] int calculateMaxIterations(const size_t from, const size_t to, int maxMaxIters, span& maxIterations, + bool inTransaction, bool enableSortOrders, const RdxContext&) const; [[nodiscard]] bool removeBrackets(); [[nodiscard]] size_t removeBrackets(size_t begin, size_t end); [[nodiscard]] bool canRemoveBracket(size_t i) const; @@ -169,10 +169,10 @@ class QueryPreprocessor : private QueryEntries { [[nodiscard]] bool containsJoin(size_t) noexcept; template - void briefDump(size_t from, size_t to, const std::vector &joinedSelectors, WrSerializer &ser) const; + void briefDump(size_t from, size_t to, const std::vector& joinedSelectors, WrSerializer& ser) const; - NamespaceImpl &ns_; - const Query &query_; + NamespaceImpl& ns_; + const Query& query_; StrictMode strictMode_; bool queryEntryAddedByForcedSortOptimization_ = false; bool desc_ = false; diff --git a/cpp_src/core/nsselecter/selectiterator.cc b/cpp_src/core/nsselecter/selectiterator.cc index 816cbcf9b..4007c17bf 100644 --- a/cpp_src/core/nsselecter/selectiterator.cc +++ b/cpp_src/core/nsselecter/selectiterator.cc @@ -39,10 +39,16 @@ std::string_view SelectIterator::TypeName() const noexcept { std::string SelectIterator::Dump() const { std::string ret = name + ' ' + std::string(TypeName()) + "("; - for (auto &it : *this) { - if (it.useBtree_) ret += "btree;"; - if (it.isRange_) ret += "range;"; - if (it.bsearch_) ret += "bsearch;"; + for (auto& it : *this) { + if (it.useBtree_) { + ret += "btree;"; + } + if (it.isRange_) { + ret += "range;"; + } + if (it.bsearch_) { + ret += "bsearch;"; + } ret += ","; if (ret.length() > 256) { ret += "..."; diff --git a/cpp_src/core/nsselecter/selectiterator.h b/cpp_src/core/nsselecter/selectiterator.h index 25e2381ea..07941f405 100644 --- a/cpp_src/core/nsselecter/selectiterator.h +++ b/cpp_src/core/nsselecter/selectiterator.h @@ -23,7 +23,7 @@ class SelectIterator : public SelectKeyResult { UnbuiltSortOrdersIndex, }; - SelectIterator(SelectKeyResult &&res, bool dist, std::string &&n, IteratorFieldKind fKind, bool forcedFirst = false) noexcept + SelectIterator(SelectKeyResult&& res, bool dist, std::string&& n, IteratorFieldKind fKind, bool forcedFirst = false) noexcept : SelectKeyResult(std::move(res)), distinct(dist), name(std::move(n)), @@ -140,7 +140,9 @@ class SelectIterator : public SelectKeyResult { res = nextUnbuiltSortOrders(); break; } - if (res) ++matchedCount_; + if (res) { + ++matchedCount_; + } return res; } @@ -184,9 +186,11 @@ class SelectIterator : public SelectKeyResult { /// Appends result to an existing set. /// @param other - results to add. - void Append(SelectKeyResult &&other) { + void Append(SelectKeyResult&& other) { reserve(size() + other.size()); - for (auto &r : other) emplace_back(std::move(r)); + for (auto& r : other) { + emplace_back(std::move(r)); + } other.clear(); } /// Cost value used for sorting: object with a smaller @@ -216,7 +220,7 @@ class SelectIterator : public SelectKeyResult { /// mode if it's more efficient than just comparing /// each object in sequence. void SetExpectMaxIterations(int expectedIterations) noexcept { - for (SingleSelectKeyResult &r : *this) { + for (SingleSelectKeyResult& r : *this) { if (!r.isRange_ && r.ids_.size() > 1) { int itersloop = r.ids_.size(); int itersbsearch = int((std::log2(r.ids_.size()) - 1) * expectedIterations); @@ -240,7 +244,9 @@ class SelectIterator : public SelectKeyResult { // from minHint which is the least rowId. // Generic next implementation bool nextFwd(IdType minHint) noexcept { - if (minHint > lastVal_) lastVal_ = minHint - 1; + if (minHint > lastVal_) { + lastVal_ = minHint - 1; + } int minVal = INT_MAX; for (auto it = begin(), endIt = end(); it != endIt; ++it) { if (it->useBtree_) { @@ -274,7 +280,9 @@ class SelectIterator : public SelectKeyResult { return lastVal_ != INT_MAX; } bool nextRev(IdType maxHint) noexcept { - if (maxHint < lastVal_) lastVal_ = maxHint + 1; + if (maxHint < lastVal_) { + lastVal_ = maxHint + 1; + } int maxVal = INT_MIN; for (auto it = begin(), endIt = end(); it != endIt; ++it) { @@ -306,18 +314,26 @@ class SelectIterator : public SelectKeyResult { } // Single range next implementation bool nextFwdSingleRange(IdType minHint) noexcept { - if (minHint > lastVal_) lastVal_ = minHint - 1; + if (minHint > lastVal_) { + lastVal_ = minHint - 1; + } const auto begIt = begin(); - if (lastVal_ < begIt->rBegin_) lastVal_ = begIt->rBegin_ - 1; + if (lastVal_ < begIt->rBegin_) { + lastVal_ = begIt->rBegin_ - 1; + } lastVal_ = (lastVal_ < begIt->rEnd_) ? lastVal_ + 1 : begIt->rEnd_; - if (lastVal_ == begIt->rEnd_) lastVal_ = INT_MAX; + if (lastVal_ == begIt->rEnd_) { + lastVal_ = INT_MAX; + } return (lastVal_ != INT_MAX); } // Single idset next implementation bool nextFwdSingleIdset(IdType minHint) noexcept { - if (minHint > lastVal_) lastVal_ = minHint - 1; + if (minHint > lastVal_) { + lastVal_ = minHint - 1; + } auto it = begin(); if (it->useBtree_) { if (it->itset_ != it->setend_ && *it->itset_ <= lastVal_) { @@ -338,17 +354,25 @@ class SelectIterator : public SelectKeyResult { return !(lastVal_ == INT_MAX); } bool nextRevSingleRange(IdType maxHint) noexcept { - if (maxHint < lastVal_) lastVal_ = maxHint + 1; + if (maxHint < lastVal_) { + lastVal_ = maxHint + 1; + } const auto begIt = begin(); - if (lastVal_ > begIt->rrBegin_) lastVal_ = begIt->rrBegin_ + 1; + if (lastVal_ > begIt->rrBegin_) { + lastVal_ = begIt->rrBegin_ + 1; + } lastVal_ = (lastVal_ > begIt->rrEnd_) ? lastVal_ - 1 : begIt->rrEnd_; - if (lastVal_ == begIt->rrEnd_) lastVal_ = INT_MIN; + if (lastVal_ == begIt->rrEnd_) { + lastVal_ = INT_MIN; + } return (lastVal_ != INT_MIN); } bool nextRevSingleIdset(IdType maxHint) noexcept { - if (maxHint < lastVal_) lastVal_ = maxHint + 1; + if (maxHint < lastVal_) { + lastVal_ = maxHint + 1; + } auto it = begin(); diff --git a/cpp_src/core/nsselecter/selectiteratorcontainer.cc b/cpp_src/core/nsselecter/selectiteratorcontainer.cc index e3fa0ec14..21eddbe6e 100644 --- a/cpp_src/core/nsselecter/selectiteratorcontainer.cc +++ b/cpp_src/core/nsselecter/selectiteratorcontainer.cc @@ -24,7 +24,9 @@ void SelectIteratorContainer::SortByCost(int expectedIterations) { if (indexes[i] != i) { size_t positionOfTmp = i + 1; for (; positionOfTmp < indexes.size(); ++positionOfTmp) { - if (indexes[positionOfTmp] == i) break; + if (indexes[positionOfTmp] == i) { + break; + } } assertrx_throw(positionOfTmp < indexes.size()); Container::value_type tmp = std::move(container_[i]); @@ -67,8 +69,8 @@ void SelectIteratorContainer::sortByCost(span indexes, span co break; case 2: { auto it = indexes.begin() + from; - auto &a = *(it++); - auto &b = *(it); + auto& a = *(it++); + auto& b = *(it); if (costs[a] > costs[b]) { std::swap(a, b); } @@ -76,12 +78,18 @@ void SelectIteratorContainer::sortByCost(span indexes, span co } case 3: { auto it = indexes.begin() + from; - auto &a = *(it++); - auto &b = *(it++); - auto &c = *(it); - if (costs[a] > costs[b]) std::swap(a, b); - if (costs[b] > costs[c]) std::swap(b, c); - if (costs[a] > costs[b]) std::swap(a, b); + auto& a = *(it++); + auto& b = *(it++); + auto& c = *(it); + if (costs[a] > costs[b]) { + std::swap(a, b); + } + if (costs[b] > costs[c]) { + std::swap(b, c); + } + if (costs[a] > costs[b]) { + std::swap(a, b); + } break; } default: @@ -101,7 +109,7 @@ bool SelectIteratorContainer::markBracketsHavingJoins(iterator begin, iterator e OneOf>) RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(JoinSelectIterator &) RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; }) || + [] RX_PRE_LMBD_ALWAYS_INLINE(JoinSelectIterator&) RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; }) || result; } return result; @@ -109,7 +117,7 @@ bool SelectIteratorContainer::markBracketsHavingJoins(iterator begin, iterator e bool SelectIteratorContainer::haveJoins(size_t i) const noexcept { return container_[i].Visit( - [] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket &b) RX_POST_LMBD_ALWAYS_INLINE noexcept { return b.haveJoins; }, + [] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket& b) RX_POST_LMBD_ALWAYS_INLINE noexcept { return b.haveJoins; }, [] RX_PRE_LMBD_ALWAYS_INLINE(OneOf>) RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, @@ -153,14 +161,14 @@ void SelectIteratorContainer::moveJoinsToTheBeginingOfORs(span indexes double SelectIteratorContainer::cost(span indexes, unsigned cur, int expectedIterations) const noexcept { return container_[indexes[cur]].Visit( - [&] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket &) + [&] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket&) RX_POST_LMBD_ALWAYS_INLINE noexcept { return cost(indexes, cur + 1, cur + Size(indexes[cur]), expectedIterations); }, Restricted>{}( - [expectedIterations] RX_PRE_LMBD_ALWAYS_INLINE(const auto &c) + [expectedIterations] RX_PRE_LMBD_ALWAYS_INLINE(const auto& c) RX_POST_LMBD_ALWAYS_INLINE noexcept { return c.Cost(expectedIterations); }), - [] RX_PRE_LMBD_ALWAYS_INLINE(const JoinSelectIterator &jit) RX_POST_LMBD_ALWAYS_INLINE noexcept { return jit.Cost(); }, - [expectedIterations] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysTrue &) + [] RX_PRE_LMBD_ALWAYS_INLINE(const JoinSelectIterator& jit) RX_POST_LMBD_ALWAYS_INLINE noexcept { return jit.Cost(); }, + [expectedIterations] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysTrue&) RX_POST_LMBD_ALWAYS_INLINE noexcept -> double { return expectedIterations; }); } @@ -176,7 +184,9 @@ double SelectIteratorContainer::fullCost(span indexes, unsigned cur, u int expectedIterations) const noexcept { double result = 0.0; for (unsigned i = from; i <= cur; i += Size(indexes[i])) { - if (GetOperation(indexes[i]) != OpOr) from = i; + if (GetOperation(indexes[i]) != OpOr) { + from = i; + } } for (; from <= cur || (from < to && GetOperation(indexes[from]) == OpOr); from += Size(indexes[from])) { result += cost(indexes, from, expectedIterations); @@ -190,7 +200,9 @@ bool SelectIteratorContainer::isIdset(const_iterator it, const_iterator end) { bool SelectIteratorContainer::HasIdsets() const { for (const_iterator it = cbegin(), end = cend(); it != end; ++it) { - if (isIdset(it, end)) return true; + if (isIdset(it, end)) { + return true; + } } return false; } @@ -203,7 +215,9 @@ void SelectIteratorContainer::CheckFirstQuery() { Container::iterator src = (++it).PlainIterator() - 1; const Container::iterator dst = begin().PlainIterator(); auto tmp = std::move(*src); - for (; src != dst; --src) *src = std::move(*(src - 1)); + for (; src != dst; --src) { + *src = std::move(*(src - 1)); + } *dst = std::move(tmp); } return; @@ -223,7 +237,7 @@ void SelectIteratorContainer::SetExpectMaxIterations(int expectedIterations) { } } -SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry &qe, const NamespaceImpl &ns, StrictMode strictMode) { +SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry& qe, const NamespaceImpl& ns, StrictMode strictMode) { if (!qe.HaveEmptyField()) { return ComparatorNotIndexed{qe.FieldName(), qe.Condition(), qe.Values(), ns.payloadType_, qe.Fields().getTagsPath(0), qe.Distinct()}; @@ -243,9 +257,9 @@ SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry &qe } template -void SelectIteratorContainer::processField(FieldsComparator &fc, const QueryField &field, const NamespaceImpl &ns) const { +void SelectIteratorContainer::processField(FieldsComparator& fc, const QueryField& field, const NamespaceImpl& ns) const { if (field.IsFieldIndexed()) { - auto &index = ns.indexes_[field.IndexNo()]; + auto& index = ns.indexes_[field.IndexNo()]; if constexpr (left) { fc.SetCollateOpts(index->Opts().collateOpts_); fc.SetLeftField(field.Fields(), field.FieldType(), index->Opts().IsArray()); @@ -264,17 +278,19 @@ void SelectIteratorContainer::processField(FieldsComparator &fc, const QueryFiel } } -SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry &qe, bool enableSortIndexOptimize, const NamespaceImpl &ns, - unsigned sortId, bool isQueryFt, SelectFunction::Ptr &selectFnc, - bool &isIndexFt, bool &isIndexSparse, FtCtx::Ptr &ftCtx, - QueryPreprocessor &qPreproc, const RdxContext &rdxCtx) { - auto &index = ns.indexes_[qe.IndexNo()]; +SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry& qe, bool enableSortIndexOptimize, const NamespaceImpl& ns, + unsigned sortId, bool isQueryFt, SelectFunction::Ptr& selectFnc, + bool& isIndexFt, bool& isIndexSparse, FtCtx::Ptr& ftCtx, + QueryPreprocessor& qPreproc, const RdxContext& rdxCtx) { + auto& index = ns.indexes_[qe.IndexNo()]; isIndexFt = IsFullText(index->Type()); isIndexSparse = index->Opts().IsSparse(); Index::SelectOpts opts; opts.itemsCountInNamespace = ns.itemsCount(); - if (!ns.SortOrdersBuilt()) opts.disableIdSetCache = 1; + if (!ns.SortOrdersBuilt()) { + opts.disableIdSetCache = 1; + } if (isQueryFt) { opts.forceComparator = 1; } @@ -293,10 +309,14 @@ SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry &qe opts.inTransaction = ctx_->inTransaction; auto ctx = selectFnc ? selectFnc->CreateCtx(qe.IndexNo()) : BaseFunctionCtx::Ptr{}; - if (ctx && ctx->type == BaseFunctionCtx::kFtCtx) ftCtx = reindexer::static_ctx_pointer_cast(ctx); + if (ctx && ctx->type == BaseFunctionCtx::kFtCtx) { + ftCtx = reindexer::static_ctx_pointer_cast(ctx); + } if (index->Opts().GetCollateMode() == CollateUTF8 || isIndexFt) { - for (auto &key : qe.Values()) key.EnsureUTF8(); + for (auto& key : qe.Values()) { + key.EnsureUTF8(); + } } PerfStatCalculatorMT calc(index->GetSelectPerfCounter(), ns.enablePerfCounters_); if (qPreproc.IsFtPreselected()) { @@ -306,13 +326,21 @@ SelectKeyResults SelectIteratorContainer::processQueryEntry(const QueryEntry &qe } } -void SelectIteratorContainer::processJoinEntry(const JoinQueryEntry &jqe, OpType op) { - auto &js = (*ctx_->joinedSelectors)[jqe.joinIndex]; - if (js.JoinQuery().joinEntries_.empty()) throw Error(errQueryExec, "Join without ON conditions"); - if (js.JoinQuery().joinEntries_[0].Operation() == OpOr) throw Error(errQueryExec, "The first ON condition cannot have OR operation"); - if (js.Type() != InnerJoin && js.Type() != OrInnerJoin) throw Error(errLogic, "Not INNER JOIN in QueryEntry"); +void SelectIteratorContainer::processJoinEntry(const JoinQueryEntry& jqe, OpType op) { + auto& js = (*ctx_->joinedSelectors)[jqe.joinIndex]; + if (js.JoinQuery().joinEntries_.empty()) { + throw Error(errQueryExec, "Join without ON conditions"); + } + if (js.JoinQuery().joinEntries_[0].Operation() == OpOr) { + throw Error(errQueryExec, "The first ON condition cannot have OR operation"); + } + if (js.Type() != InnerJoin && js.Type() != OrInnerJoin) { + throw Error(errLogic, "Not INNER JOIN in QueryEntry"); + } if (js.Type() == OrInnerJoin) { - if (op == OpNot) throw Error(errQueryExec, "NOT operator with or_inner_join"); + if (op == OpNot) { + throw Error(errQueryExec, "NOT operator with or_inner_join"); + } js.SetType(InnerJoin); op = OpOr; } @@ -322,12 +350,12 @@ void SelectIteratorContainer::processJoinEntry(const JoinQueryEntry &jqe, OpType Append(op, JoinSelectIterator{static_cast(jqe.joinIndex)}); } -void SelectIteratorContainer::processQueryEntryResults(SelectKeyResults &&selectResults, OpType op, const NamespaceImpl &ns, - const QueryEntry &qe, bool isIndexFt, bool isIndexSparse, +void SelectIteratorContainer::processQueryEntryResults(SelectKeyResults&& selectResults, OpType op, const NamespaceImpl& ns, + const QueryEntry& qe, bool isIndexFt, bool isIndexSparse, std::optional nextOp) { std::visit( overloaded{ - [&](SelectKeyResultsVector &selResults) { + [&](SelectKeyResultsVector& selResults) { if (selResults.empty()) { if (op == OpAnd) { SelectKeyResult zeroScan; @@ -336,7 +364,7 @@ void SelectIteratorContainer::processQueryEntryResults(SelectKeyResults &&select } return; } - for (SelectKeyResult &res : selResults) { + for (SelectKeyResult& res : selResults) { switch (op) { case OpOr: { const iterator last = lastAppendedOrClosed(); @@ -345,7 +373,7 @@ void SelectIteratorContainer::processQueryEntryResults(SelectKeyResults &&select } if (last->Is() && !last->Value().distinct && last->operation != OpNot) { using namespace std::string_view_literals; - SelectIterator &it = last->Value(); + SelectIterator& it = last->Value(); it.Append(std::move(res)); it.name.append(" or "sv).append(qe.FieldName()); break; @@ -360,13 +388,17 @@ void SelectIteratorContainer::processQueryEntryResults(SelectKeyResults &&select isIndexFt); if (qe.IsFieldIndexed() && !isIndexSparse) { // last appended is always a SelectIterator - SelectIterator &lastAppended = lastAppendedOrClosed()->Value(); + SelectIterator& lastAppended = lastAppendedOrClosed()->Value(); lastAppended.SetNotOperationFlag(op == OpNot); if (!nextOp.has_value() || nextOp.value() != OpOr) { const auto maxIterations = lastAppended.GetMaxIterations(); const int cur = op == OpNot ? ns.items_.size() - maxIterations : maxIterations; - if (cur && cur < maxIterations_) maxIterations_ = cur; - if (!cur) wasZeroIterations_ = true; + if (cur && cur < maxIterations_) { + maxIterations_ = cur; + } + if (!cur) { + wasZeroIterations_ = true; + } } } break; @@ -380,19 +412,19 @@ void SelectIteratorContainer::processQueryEntryResults(SelectKeyResults &&select } }, Restricted>{}([&](auto &c) { + Template>{}([&](auto& c) { c.SetNotOperationFlag(op == OpNot); Append(op, std::move(c)); })}, selectResults.AsVariant()); } -void SelectIteratorContainer::processEqualPositions(const std::vector &equalPositions, const NamespaceImpl &ns, - const QueryEntries &queries) { - for (const auto &eqPos : equalPositions) { +void SelectIteratorContainer::processEqualPositions(const std::vector& equalPositions, const NamespaceImpl& ns, + const QueryEntries& queries) { + for (const auto& eqPos : equalPositions) { EqualPositionComparator cmp{ns.payloadType_}; for (size_t i = 0, s = eqPos.size(); i < s; ++i) { - const QueryEntry &qe = queries.Get(eqPos[i]); + const QueryEntry& qe = queries.Get(eqPos[i]); if (qe.Condition() == CondEmpty || (qe.Condition() == CondSet && qe.Values().empty())) { throw Error(errLogic, "Condition IN(with empty parameter list), IS NULL, IS EMPTY not allowed for equal position!"); } @@ -409,17 +441,19 @@ void SelectIteratorContainer::processEqualPositions(const std::vector SelectIteratorContainer::prepareEqualPositions(const QueryEntries &queries, +std::vector SelectIteratorContainer::prepareEqualPositions(const QueryEntries& queries, size_t begin, size_t end) { static const auto getFieldsStr = [](auto begin, auto end) { std::stringstream str; for (auto it = begin; it != end; ++it) { - if (it != begin) str << ", "; + if (it != begin) { + str << ", "; + } str << *it; } return str.str(); }; - const auto &eqPos = (begin == 0 ? queries.equalPositions : queries.Get(begin - 1).equalPositions); + const auto& eqPos = (begin == 0 ? queries.equalPositions : queries.Get(begin - 1).equalPositions); std::vector result{eqPos.size()}; for (size_t i = 0; i < eqPos.size(); ++i) { if (eqPos[i].size() < 2) { @@ -435,15 +469,17 @@ std::vector SelectIteratorContainer::pr for (size_t j = begin, next; j < end; j = next) { next = queries.Next(j); queries.Visit( - j, Skip{}, [](const SubQueryEntry &) { throw_as_assert; }, - [](const SubQueryFieldEntry &) { throw_as_assert; }, - [&](const QueryEntry &eq) { + j, Skip{}, [](const SubQueryEntry&) { throw_as_assert; }, + [](const SubQueryFieldEntry&) { throw_as_assert; }, + [&](const QueryEntry& eq) { if (foundFields.find(eq.FieldName()) != foundFields.end()) { throw Error(errParams, "Equal position field '%s' found twice in enclosing bracket; equal position fields: [%s]", eq.FieldName(), getEpFieldsStr()); } const auto it = epFields.find(eq.FieldName()); - if (it == epFields.end()) return; + if (it == epFields.end()) { + return; + } if (queries.GetOperation(j) != OpAnd || (next < end && queries.GetOperation(next) == OpOr)) { throw Error(errParams, "Only AND operation allowed for equal position; equal position field with not AND operation: '%s'; " @@ -453,7 +489,7 @@ std::vector SelectIteratorContainer::pr result[i].push_back(j); foundFields.insert(epFields.extract(it)); }, - [&](const BetweenFieldsQueryEntry &eq) { // TODO equal positions for BetweenFieldsQueryEntry #1092 + [&](const BetweenFieldsQueryEntry& eq) { // TODO equal positions for BetweenFieldsQueryEntry #1092 if (epFields.find(eq.LeftFieldName()) != epFields.end()) { throw Error( errParams, @@ -476,16 +512,16 @@ std::vector SelectIteratorContainer::pr return result; } -void SelectIteratorContainer::PrepareIteratorsForSelectLoop(QueryPreprocessor &qPreproc, unsigned sortId, bool isFt, - const NamespaceImpl &ns, SelectFunction::Ptr &selectFnc, FtCtx::Ptr &ftCtx, - const RdxContext &rdxCtx) { +void SelectIteratorContainer::PrepareIteratorsForSelectLoop(QueryPreprocessor& qPreproc, unsigned sortId, bool isFt, + const NamespaceImpl& ns, SelectFunction::Ptr& selectFnc, FtCtx::Ptr& ftCtx, + const RdxContext& rdxCtx) { prepareIteratorsForSelectLoop(qPreproc, 0, qPreproc.Size(), sortId, isFt, ns, selectFnc, ftCtx, rdxCtx); } -bool SelectIteratorContainer::prepareIteratorsForSelectLoop(QueryPreprocessor &qPreproc, size_t begin, size_t end, unsigned sortId, - bool isQueryFt, const NamespaceImpl &ns, SelectFunction::Ptr &selectFnc, - FtCtx::Ptr &ftCtx, const RdxContext &rdxCtx) { - const auto &queries = qPreproc.GetQueryEntries(); +bool SelectIteratorContainer::prepareIteratorsForSelectLoop(QueryPreprocessor& qPreproc, size_t begin, size_t end, unsigned sortId, + bool isQueryFt, const NamespaceImpl& ns, SelectFunction::Ptr& selectFnc, + FtCtx::Ptr& ftCtx, const RdxContext& rdxCtx) { + const auto& queries = qPreproc.GetQueryEntries(); auto equalPositions = prepareEqualPositions(queries, begin, end); bool sortIndexFound = false; bool containFT = false; @@ -494,8 +530,8 @@ bool SelectIteratorContainer::prepareIteratorsForSelectLoop(QueryPreprocessor &q const OpType op = queries.GetOperation(i); containFT = queries.Visit( - i, [](const SubQueryEntry &) -> bool { throw_as_assert; }, [](const SubQueryFieldEntry &) -> bool { throw_as_assert; }, - [&](const QueryEntriesBracket &) { + i, [](const SubQueryEntry&) -> bool { throw_as_assert; }, [](const SubQueryFieldEntry&) -> bool { throw_as_assert; }, + [&](const QueryEntriesBracket&) { OpenBracket(op); const bool contFT = prepareIteratorsForSelectLoop(qPreproc, i + 1, next, sortId, isQueryFt, ns, selectFnc, ftCtx, rdxCtx); @@ -505,7 +541,7 @@ bool SelectIteratorContainer::prepareIteratorsForSelectLoop(QueryPreprocessor &q CloseBracket(); return contFT; }, - [&](const QueryEntry &qe) { + [&](const QueryEntry& qe) { const bool isFT = qe.IsFieldIndexed() && IsFullText(ns.indexes_[qe.IndexNo()]->Type()); if (isFT && (op == OpOr || (next < end && queries.GetOperation(next) == OpOr))) { throw Error(errLogic, "OR operation is not allowed with fulltext index"); @@ -544,24 +580,24 @@ bool SelectIteratorContainer::prepareIteratorsForSelectLoop(QueryPreprocessor &q processQueryEntryResults(std::move(selectResults), op, ns, qe, isIndexFt, isIndexSparse, nextOp); return isFT; }, - [this, op](const JoinQueryEntry &jqe) { + [this, op](const JoinQueryEntry& jqe) { processJoinEntry(jqe, op); return false; }, - [&](const BetweenFieldsQueryEntry &qe) { + [&](const BetweenFieldsQueryEntry& qe) { FieldsComparator fc{qe.LeftFieldName(), qe.Condition(), qe.RightFieldName(), ns.payloadType_}; processField(fc, qe.LeftFieldData(), ns); processField(fc, qe.RightFieldData(), ns); Append(op, std::move(fc)); return false; }, - [this, op](const AlwaysFalse &) { + [this, op](const AlwaysFalse&) { SelectKeyResult zeroScan; zeroScan.emplace_back(0, 0); Append(op, SelectIterator{std::move(zeroScan), false, "always_false", IteratorFieldKind::None, true}); return false; }, - [this, op](const AlwaysTrue &) { + [this, op](const AlwaysTrue&) { Append(op, AlwaysTrue{}); return false; }) || @@ -572,7 +608,7 @@ bool SelectIteratorContainer::prepareIteratorsForSelectLoop(QueryPreprocessor &q } template -RX_ALWAYS_INLINE bool SelectIteratorContainer::checkIfSatisfyCondition(SelectIterator &it, bool *finish, IdType rowId) { +RX_ALWAYS_INLINE bool SelectIteratorContainer::checkIfSatisfyCondition(SelectIterator& it, bool* finish, IdType rowId) { if constexpr (reverse) { while (it.Val() > rowId && it.Next(rowId)) { } @@ -591,16 +627,16 @@ RX_ALWAYS_INLINE bool SelectIteratorContainer::checkIfSatisfyCondition(SelectIte } } -RX_ALWAYS_INLINE bool SelectIteratorContainer::checkIfSatisfyCondition(JoinSelectIterator &it, PayloadValue &pv, IdType properRowId, +RX_ALWAYS_INLINE bool SelectIteratorContainer::checkIfSatisfyCondition(JoinSelectIterator& it, PayloadValue& pv, IdType properRowId, bool match) { assertrx_throw(ctx_->joinedSelectors); ConstPayload pl(*pt_, pv); - auto &joinedSelector = (*ctx_->joinedSelectors)[it.joinIndex]; + auto& joinedSelector = (*ctx_->joinedSelectors)[it.joinIndex]; return joinedSelector.Process(properRowId, ctx_->nsid, pl, match); } template -bool SelectIteratorContainer::checkIfSatisfyAllConditions(iterator begin, iterator end, PayloadValue &pv, bool *finish, IdType rowId, +bool SelectIteratorContainer::checkIfSatisfyAllConditions(iterator begin, iterator end, PayloadValue& pv, bool* finish, IdType rowId, IdType properRowId, bool match) { bool result = true; bool currentFinish = false; @@ -611,22 +647,24 @@ bool SelectIteratorContainer::checkIfSatisfyAllConditions(iterator begin, iterat // suggest that all JOINs in chain of OR ... OR ... OR ... OR will be before all not JOINs (see SortByCost) if (result) { // check what it does not holds join - if (it->Visit([] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket &b) + if (it->Visit([] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket& b) RX_POST_LMBD_ALWAYS_INLINE noexcept { return !b.haveJoins; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const JoinSelectIterator &) - RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, + [] RX_PRE_LMBD_ALWAYS_INLINE(const JoinSelectIterator&) RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, [] RX_PRE_LMBD_ALWAYS_INLINE( OneOf>) - RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; })) + RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; })) { continue; + } } } else { - if (!result) break; + if (!result) { + break; + } } bool lastFinish = false; const bool lastResult = it->Visit( - [&] RX_PRE_LMBD_ALWAYS_INLINE(SelectIteratorsBracket &) RX_POST_LMBD_ALWAYS_INLINE { + [&] RX_PRE_LMBD_ALWAYS_INLINE(SelectIteratorsBracket&) RX_POST_LMBD_ALWAYS_INLINE { return checkIfSatisfyAllConditions(it.begin(), it.end(), pv, &lastFinish, rowId, properRowId, match); }, @@ -636,8 +674,8 @@ bool SelectIteratorContainer::checkIfSatisfyAllConditions(iterator begin, iterat RX_POST_LMBD_ALWAYS_INLINE { return checkIfSatisfyCondition(jit, pv, properRowId, match); }, Restricted>{}( - [&pv, properRowId] RX_PRE_LMBD_ALWAYS_INLINE(auto &c) RX_POST_LMBD_ALWAYS_INLINE { return c.Compare(pv, properRowId); }), - [] RX_PRE_LMBD_ALWAYS_INLINE(AlwaysTrue &) RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; }); + [&pv, properRowId] RX_PRE_LMBD_ALWAYS_INLINE(auto& c) RX_POST_LMBD_ALWAYS_INLINE { return c.Compare(pv, properRowId); }), + [] RX_PRE_LMBD_ALWAYS_INLINE(AlwaysTrue&) RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; }); if (op == OpOr) { result |= lastResult; currentFinish &= (!result && lastFinish); @@ -649,7 +687,9 @@ bool SelectIteratorContainer::checkIfSatisfyAllConditions(iterator begin, iterat currentFinish = false; } } - if (!result) *finish = currentFinish; + if (!result) { + *finish = currentFinish; + } return result; } @@ -660,15 +700,23 @@ IdType SelectIteratorContainer::getNextItemId(const_iterator begin, const_iterat switch (it->operation) { case OpOr: { auto next = it->Visit( - [it, from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket &) + [it, from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket&) RX_POST_LMBD_ALWAYS_INLINE { return getNextItemId(it.cbegin(), it.cend(), from); }, - [from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIterator &sit) RX_POST_LMBD_ALWAYS_INLINE { + [from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIterator& sit) RX_POST_LMBD_ALWAYS_INLINE { if constexpr (reverse) { - if (sit.End()) return std::numeric_limits::lowest(); - if (sit.Val() < from) return sit.Val() + 1; + if (sit.End()) { + return std::numeric_limits::lowest(); + } + if (sit.Val() < from) { + return sit.Val() + 1; + } } else { - if (sit.End()) return std::numeric_limits::max(); - if (sit.Val() > from) return sit.Val() - 1; + if (sit.End()) { + return std::numeric_limits::max(); + } + if (sit.Val() > from) { + return sit.Val() - 1; + } } return from; }, @@ -676,7 +724,7 @@ IdType SelectIteratorContainer::getNextItemId(const_iterator begin, const_iterat const OneOf>) RX_POST_LMBD_ALWAYS_INLINE { return from; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysFalse &) RX_POST_LMBD_ALWAYS_INLINE { + [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysFalse&) RX_POST_LMBD_ALWAYS_INLINE { return reverse ? std::numeric_limits::lowest() : std::numeric_limits::max(); }); if constexpr (reverse) { @@ -688,15 +736,23 @@ IdType SelectIteratorContainer::getNextItemId(const_iterator begin, const_iterat case OpAnd: from = result; result = it->Visit( - [it, from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket &) + [it, from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIteratorsBracket&) RX_POST_LMBD_ALWAYS_INLINE { return getNextItemId(it.cbegin(), it.cend(), from); }, - [from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIterator &sit) RX_POST_LMBD_ALWAYS_INLINE { + [from] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIterator& sit) RX_POST_LMBD_ALWAYS_INLINE { if constexpr (reverse) { - if (sit.End()) return std::numeric_limits::lowest(); - if (sit.Val() < from) return sit.Val() + 1; + if (sit.End()) { + return std::numeric_limits::lowest(); + } + if (sit.Val() < from) { + return sit.Val() + 1; + } } else { - if (sit.End()) return std::numeric_limits::max(); - if (sit.Val() > from) return sit.Val() - 1; + if (sit.End()) { + return std::numeric_limits::max(); + } + if (sit.Val() > from) { + return sit.Val() - 1; + } } return from; }, @@ -704,7 +760,7 @@ IdType SelectIteratorContainer::getNextItemId(const_iterator begin, const_iterat const OneOf>) RX_POST_LMBD_ALWAYS_INLINE { return from; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysFalse &) RX_POST_LMBD_ALWAYS_INLINE { + [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysFalse&) RX_POST_LMBD_ALWAYS_INLINE { return reverse ? std::numeric_limits::lowest() : std::numeric_limits::max(); }); break; @@ -716,7 +772,7 @@ IdType SelectIteratorContainer::getNextItemId(const_iterator begin, const_iterat } template -bool SelectIteratorContainer::Process(PayloadValue &pv, bool *finish, IdType *rowId, IdType properRowId, bool match) { +bool SelectIteratorContainer::Process(PayloadValue& pv, bool* finish, IdType* rowId, IdType properRowId, bool match) { if (auto it = begin(); checkIfSatisfyAllConditions(++it, end(), pv, finish, *rowId, properRowId, match)) { return true; } @@ -724,10 +780,10 @@ bool SelectIteratorContainer::Process(PayloadValue &pv, bool *finish, IdType *ro return false; } -template bool SelectIteratorContainer::Process(PayloadValue &, bool *, IdType *, IdType, bool); -template bool SelectIteratorContainer::Process(PayloadValue &, bool *, IdType *, IdType, bool); -template bool SelectIteratorContainer::Process(PayloadValue &, bool *, IdType *, IdType, bool); -template bool SelectIteratorContainer::Process(PayloadValue &, bool *, IdType *, IdType, bool); +template bool SelectIteratorContainer::Process(PayloadValue&, bool*, IdType*, IdType, bool); +template bool SelectIteratorContainer::Process(PayloadValue&, bool*, IdType*, IdType, bool); +template bool SelectIteratorContainer::Process(PayloadValue&, bool*, IdType*, IdType, bool); +template bool SelectIteratorContainer::Process(PayloadValue&, bool*, IdType*, IdType, bool); std::string SelectIteratorContainer::Dump() const { WrSerializer ser; @@ -736,7 +792,7 @@ std::string SelectIteratorContainer::Dump() const { } void SelectIteratorContainer::dump(size_t level, const_iterator begin, const_iterator end, - const std::vector &joinedSelectors, WrSerializer &ser) { + const std::vector& joinedSelectors, WrSerializer& ser) { for (const_iterator it = begin; it != end; ++it) { for (size_t i = 0; i < level; ++i) { ser << " "; @@ -745,7 +801,7 @@ void SelectIteratorContainer::dump(size_t level, const_iterator begin, const_ite ser << it->operation << ' '; } it->Visit( - [&](const SelectIteratorsBracket &) { + [&](const SelectIteratorsBracket&) { ser << "(\n"; dump(level + 1, it.cbegin(), it.cend(), joinedSelectors, ser); for (size_t i = 0; i < level; ++i) { @@ -755,19 +811,19 @@ void SelectIteratorContainer::dump(size_t level, const_iterator begin, const_ite }, Restricted>{}( - [&ser](const auto &c) { ser << c.Dump(); }), - [&ser, &joinedSelectors](const JoinSelectIterator &jit) { jit.Dump(ser, joinedSelectors); }, - [&ser](const AlwaysTrue &) { ser << "Always True"; }); + [&ser](const auto& c) { ser << c.Dump(); }), + [&ser, &joinedSelectors](const JoinSelectIterator& jit) { jit.Dump(ser, joinedSelectors); }, + [&ser](const AlwaysTrue&) { ser << "Always True"; }); ser << '\n'; } } -void JoinSelectIterator::Dump(WrSerializer &ser, const std::vector &joinedSelectors) const { - const auto &js = joinedSelectors.at(joinIndex); - const auto &q = js.JoinQuery(); +void JoinSelectIterator::Dump(WrSerializer& ser, const std::vector& joinedSelectors) const { + const auto& js = joinedSelectors.at(joinIndex); + const auto& q = js.JoinQuery(); ser << js.Type() << " (" << q.GetSQL() << ") ON "; ser << '('; - for (const auto &jqe : q.joinEntries_) { + for (const auto& jqe : q.joinEntries_) { if (&jqe != &q.joinEntries_.front()) { ser << ' ' << jqe.Operation() << ' '; } else { diff --git a/cpp_src/core/nsselecter/selectiteratorcontainer.h b/cpp_src/core/nsselecter/selectiteratorcontainer.h index e53eb8501..4f23b5446 100644 --- a/cpp_src/core/nsselecter/selectiteratorcontainer.h +++ b/cpp_src/core/nsselecter/selectiteratorcontainer.h @@ -21,14 +21,14 @@ class QueryPreprocessor; struct JoinSelectIterator { size_t joinIndex; double Cost() const noexcept { return std::numeric_limits::max(); } - void Dump(WrSerializer &, const std::vector &) const; + void Dump(WrSerializer&, const std::vector&) const; }; struct SelectIteratorsBracket : private Bracket { using Bracket::Bracket; using Bracket::Size; using Bracket::Append; - void CopyPayloadFrom(const SelectIteratorsBracket &other) noexcept { haveJoins = other.haveJoins; } + void CopyPayloadFrom(const SelectIteratorsBracket& other) noexcept { haveJoins = other.haveJoins; } bool haveJoins = false; }; @@ -43,7 +43,7 @@ class SelectIteratorContainer ComparatorIndexed, EqualPositionComparator, ComparatorNotIndexed>; public: - SelectIteratorContainer(PayloadType pt = PayloadType(), SelectCtx *ctx = nullptr) + SelectIteratorContainer(PayloadType pt = PayloadType(), SelectCtx* ctx = nullptr) : pt_(std::move(pt)), ctx_(ctx), maxIterations_(std::numeric_limits::max()), wasZeroIterations_(false) {} void SortByCost(int expectedIterations); @@ -52,10 +52,10 @@ class SelectIteratorContainer void CheckFirstQuery(); // Let iterators choose most effecive algorith void SetExpectMaxIterations(int expectedIterations); - void PrepareIteratorsForSelectLoop(QueryPreprocessor &, unsigned sortId, bool isFt, const NamespaceImpl &, SelectFunction::Ptr &, - FtCtx::Ptr &, const RdxContext &); + void PrepareIteratorsForSelectLoop(QueryPreprocessor&, unsigned sortId, bool isFt, const NamespaceImpl&, SelectFunction::Ptr&, + FtCtx::Ptr&, const RdxContext&); template - bool Process(PayloadValue &, bool *finish, IdType *rowId, IdType, bool match); + bool Process(PayloadValue&, bool* finish, IdType* rowId, IdType, bool match); bool IsSelectIterator(size_t i) const noexcept { assertrx_throw(i < Size()); @@ -71,12 +71,12 @@ class SelectIteratorContainer [] RX_PRE_LMBD_ALWAYS_INLINE( OneOf) RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIterator &sit) RX_POST_LMBD_ALWAYS_INLINE noexcept { return sit.distinct; }, + [] RX_PRE_LMBD_ALWAYS_INLINE(const SelectIterator& sit) RX_POST_LMBD_ALWAYS_INLINE noexcept { return sit.distinct; }, Restricted>{}( - [] RX_PRE_LMBD_ALWAYS_INLINE(const auto &comp) RX_POST_LMBD_ALWAYS_INLINE noexcept { return comp.IsDistinct(); })); + [] RX_PRE_LMBD_ALWAYS_INLINE(const auto& comp) RX_POST_LMBD_ALWAYS_INLINE noexcept { return comp.IsDistinct(); })); } - void ExplainJSON(int iters, JsonBuilder &builder, const std::vector *js) const { + void ExplainJSON(int iters, JsonBuilder& builder, const std::vector* js) const { explainJSON(cbegin(), cend(), iters, builder, js); } @@ -87,60 +87,66 @@ class SelectIteratorContainer } int GetMaxIterations(bool withZero = false) const noexcept { return (withZero && wasZeroIterations_) ? 0 : maxIterations_; } std::string Dump() const; - static bool IsExpectingOrderedResults(const QueryEntry &qe) noexcept { + static bool IsExpectingOrderedResults(const QueryEntry& qe) noexcept { return IsOrderedCondition(qe.Condition()) || (qe.Condition() != CondAny && qe.Values().size() <= 1); } private: - bool prepareIteratorsForSelectLoop(QueryPreprocessor &, size_t begin, size_t end, unsigned sortId, bool isFt, const NamespaceImpl &, - SelectFunction::Ptr &, FtCtx::Ptr &, const RdxContext &); + bool prepareIteratorsForSelectLoop(QueryPreprocessor&, size_t begin, size_t end, unsigned sortId, bool isFt, const NamespaceImpl&, + SelectFunction::Ptr&, FtCtx::Ptr&, const RdxContext&); void sortByCost(span indexes, span costs, unsigned from, unsigned to, int expectedIterations); double fullCost(span indexes, unsigned i, unsigned from, unsigned to, int expectedIterations) const noexcept; double cost(span indexes, unsigned cur, int expectedIterations) const noexcept; double cost(span indexes, unsigned from, unsigned to, int expectedIterations) const noexcept; void moveJoinsToTheBeginingOfORs(span indexes, unsigned from, unsigned to); // Check idset must be 1st - static void checkFirstQuery(Container &); + static void checkFirstQuery(Container&); template - bool checkIfSatisfyCondition(SelectIterator &, bool *finish, IdType rowId); - bool checkIfSatisfyCondition(JoinSelectIterator &, PayloadValue &, IdType properRowId, bool match); + bool checkIfSatisfyCondition(SelectIterator&, bool* finish, IdType rowId); + bool checkIfSatisfyCondition(JoinSelectIterator&, PayloadValue&, IdType properRowId, bool match); template - bool checkIfSatisfyAllConditions(iterator begin, iterator end, PayloadValue &, bool *finish, IdType rowId, IdType properRowId, + bool checkIfSatisfyAllConditions(iterator begin, iterator end, PayloadValue&, bool* finish, IdType rowId, IdType properRowId, bool match); - static std::string explainJSON(const_iterator it, const_iterator to, int iters, JsonBuilder &builder, - const std::vector *); + static std::string explainJSON(const_iterator it, const_iterator to, int iters, JsonBuilder& builder, + const std::vector*); template static IdType getNextItemId(const_iterator begin, const_iterator end, IdType from); static bool isIdset(const_iterator it, const_iterator end); static bool markBracketsHavingJoins(iterator begin, iterator end) noexcept; bool haveJoins(size_t i) const noexcept; - SelectKeyResults processQueryEntry(const QueryEntry &qe, const NamespaceImpl &ns, StrictMode strictMode); - SelectKeyResults processQueryEntry(const QueryEntry &qe, bool enableSortIndexOptimize, const NamespaceImpl &ns, unsigned sortId, - bool isQueryFt, SelectFunction::Ptr &selectFnc, bool &isIndexFt, bool &isIndexSparse, FtCtx::Ptr &, - QueryPreprocessor &qPreproc, const RdxContext &); + SelectKeyResults processQueryEntry(const QueryEntry& qe, const NamespaceImpl& ns, StrictMode strictMode); + SelectKeyResults processQueryEntry(const QueryEntry& qe, bool enableSortIndexOptimize, const NamespaceImpl& ns, unsigned sortId, + bool isQueryFt, SelectFunction::Ptr& selectFnc, bool& isIndexFt, bool& isIndexSparse, FtCtx::Ptr&, + QueryPreprocessor& qPreproc, const RdxContext&); template - void processField(FieldsComparator &, const QueryField &, const NamespaceImpl &) const; - void processJoinEntry(const JoinQueryEntry &, OpType); - void processQueryEntryResults(SelectKeyResults &&, OpType, const NamespaceImpl &, const QueryEntry &, bool isIndexFt, - bool isIndexSparse, std::optional nextOp); + void processField(FieldsComparator&, const QueryField&, const NamespaceImpl&) const; + void processJoinEntry(const JoinQueryEntry&, OpType); + void processQueryEntryResults(SelectKeyResults&&, OpType, const NamespaceImpl&, const QueryEntry&, bool isIndexFt, bool isIndexSparse, + std::optional nextOp); using EqualPositions = h_vector; - void processEqualPositions(const std::vector &equalPositions, const NamespaceImpl &ns, const QueryEntries &queries); - static std::vector prepareEqualPositions(const QueryEntries &queries, size_t begin, size_t end); + void processEqualPositions(const std::vector& equalPositions, const NamespaceImpl& ns, const QueryEntries& queries); + static std::vector prepareEqualPositions(const QueryEntries& queries, size_t begin, size_t end); /// @return end() if empty or last opened bracket is empty iterator lastAppendedOrClosed() { typename Container::iterator it = this->container_.begin(), end = this->container_.end(); - if (!this->activeBrackets_.empty()) it += (this->activeBrackets_.back() + 1); - if (it == end) return this->end(); + if (!this->activeBrackets_.empty()) { + it += (this->activeBrackets_.back() + 1); + } + if (it == end) { + return this->end(); + } iterator i = it, i2 = it, e = end; - while (++i2 != e) i = i2; + while (++i2 != e) { + i = i2; + } return i; } - static void dump(size_t level, const_iterator begin, const_iterator end, const std::vector &, WrSerializer &); + static void dump(size_t level, const_iterator begin, const_iterator end, const std::vector&, WrSerializer&); PayloadType pt_; - SelectCtx *ctx_; + SelectCtx* ctx_; int maxIterations_; bool wasZeroIterations_; }; diff --git a/cpp_src/core/nsselecter/sortexpression.cc b/cpp_src/core/nsselecter/sortexpression.cc index 6741df102..d8523a680 100644 --- a/cpp_src/core/nsselecter/sortexpression.cc +++ b/cpp_src/core/nsselecter/sortexpression.cc @@ -11,7 +11,7 @@ namespace { -static RX_NO_INLINE void throwParseError(const std::string_view sortExpr, char const* const pos, const std::string_view message) { +static RX_NO_INLINE void throwParseError(const std::string_view sortExpr, const char* const pos, const std::string_view message) { throw reindexer::Error(errParams, "'%s' is not valid sort expression. Parser failed at position %d.%s%s", sortExpr, pos - sortExpr.data(), message.empty() ? "" : " ", message); } @@ -51,8 +51,12 @@ const PayloadValue& SortExpression::getJoinedValue(IdType rowId, const joins::Na const auto& js = joinedSelectors[nsIdx]; const joins::ItemIterator jIt{&joinResults, rowId}; const auto jfIt = jIt.at(nsIdx); - if (jfIt == jIt.end() || jfIt.ItemsCount() == 0) throw Error(errQueryExec, "Not found value joined from ns %s", js.RightNsName()); - if (jfIt.ItemsCount() > 1) throw Error(errQueryExec, "Found more than 1 value joined from ns %s", js.RightNsName()); + if (jfIt == jIt.end() || jfIt.ItemsCount() == 0) { + throw Error(errQueryExec, "Not found value joined from ns %s", js.RightNsName()); + } + if (jfIt.ItemsCount() > 1) { + throw Error(errQueryExec, "Found more than 1 value joined from ns %s", js.RightNsName()); + } return jfIt[0].Value(); } @@ -96,7 +100,9 @@ const SortExprFuncs::JoinedIndex& SortExpression::GetJoinedIndex() const noexcep double SortExprFuncs::Index::GetValue(ConstPayload pv, TagsMatcher& tagsMatcher) const { const VariantArray values = getFieldValues(pv, tagsMatcher, index, column); - if (values.empty()) throw Error(errQueryExec, "Empty field in sort expression: %s", column); + if (values.empty()) { + throw Error(errQueryExec, "Empty field in sort expression: %s", column); + } if (values.size() > 1 || values[0].Type().Is() || values[0].Type().Is()) { throw Error(errQueryExec, "Array, composite or tuple field in sort expression"); } @@ -111,7 +117,9 @@ double DistanceFromPoint::GetValue(ConstPayload pv, TagsMatcher& tagsMatcher) co double JoinedIndex::GetValue(IdType rowId, const joins::NamespaceResults& joinResults, const std::vector& joinedSelectors) const { const VariantArray values = SortExpression::GetJoinedFieldValues(rowId, joinResults, joinedSelectors, nsIdx, column, index); - if (values.empty()) throw Error(errQueryExec, "Empty field in sort expression: %s %s", joinedSelectors[nsIdx].RightNsName(), column); + if (values.empty()) { + throw Error(errQueryExec, "Empty field in sort expression: %s %s", joinedSelectors[nsIdx].RightNsName(), column); + } if (values.size() > 1 || values[0].Type().Is() || values[0].Type().Is()) { throw Error(errQueryExec, "Array, composite or tuple field in sort expression"); } @@ -204,7 +212,9 @@ static ParseIndexNameResult parseIndexName(std::string_view& expr, const std: const auto end = expr.data() + expr.size(); auto joinedSelectorIt = joinedSelectors.cend(); bool joinedFieldInQuotes = false; - while (pos != end && *pos != '.' && kIndexNameSyms.test(*pos)) ++pos; + while (pos != end && *pos != '.' && kIndexNameSyms.test(*pos)) { + ++pos; + } if (pos != end && *pos == '.') { std::string_view namespaceName = {expr.data(), static_cast(pos - expr.data())}; @@ -228,7 +238,9 @@ static ParseIndexNameResult parseIndexName(std::string_view& expr, const std: joinedFieldInQuotes = false; } } - while (pos != end && kIndexNameSyms.test(*pos)) ++pos; + while (pos != end && kIndexNameSyms.test(*pos)) { + ++pos; + } std::string_view name{expr.data(), static_cast(pos - expr.data())}; if (name.empty()) { throwParseError(fullExpr, pos, "Expected index or function name."); @@ -264,53 +276,72 @@ static Point parsePoint(std::string_view& expr, std::string_view funcName, const } expr.remove_prefix(1); skipSpaces(); - if (expr.empty() || (expr[0] != '\'' && expr[0] != '"')) throwParseError(fullExpr, expr.data(), "Expected \" or '."); + if (expr.empty() || (expr[0] != '\'' && expr[0] != '"')) { + throwParseError(fullExpr, expr.data(), "Expected \" or '."); + } const char openQuote = expr[0]; expr.remove_prefix(1); skipSpaces(); - if (!checkIfStartsWith("point"sv, expr)) throwParseError(fullExpr, expr.data(), "Expected 'point'."); + if (!checkIfStartsWith("point"sv, expr)) { + throwParseError(fullExpr, expr.data(), "Expected 'point'."); + } expr.remove_prefix(5); skipSpaces(); - if (expr.empty() || expr[0] != '(') throwParseError(fullExpr, expr.data(), "Expected '('."); + if (expr.empty() || expr[0] != '(') { + throwParseError(fullExpr, expr.data(), "Expected '('."); + } expr.remove_prefix(1); skipSpaces(); int countOfCharsParsedAsDouble = 0; const double x = converter.StringToDouble(expr.data(), expr.size(), &countOfCharsParsedAsDouble); - if (countOfCharsParsedAsDouble == 0) throwParseError(fullExpr, expr.data(), "Expected number."); + if (countOfCharsParsedAsDouble == 0) { + throwParseError(fullExpr, expr.data(), "Expected number."); + } expr.remove_prefix(countOfCharsParsedAsDouble); skipSpaces(); countOfCharsParsedAsDouble = 0; const double y = converter.StringToDouble(expr.data(), expr.size(), &countOfCharsParsedAsDouble); - if (countOfCharsParsedAsDouble == 0) throwParseError(fullExpr, expr.data(), "Expected number."); + if (countOfCharsParsedAsDouble == 0) { + throwParseError(fullExpr, expr.data(), "Expected number."); + } expr.remove_prefix(countOfCharsParsedAsDouble); skipSpaces(); - if (expr.empty() || expr[0] != ')') throwParseError(fullExpr, expr.data(), "Expected ')'."); + if (expr.empty() || expr[0] != ')') { + throwParseError(fullExpr, expr.data(), "Expected ')'."); + } expr.remove_prefix(1); skipSpaces(); - if (expr.empty() || expr[0] != openQuote) throwParseError(fullExpr, expr.data(), std::string("Expected ") + openQuote + '.'); + if (expr.empty() || expr[0] != openQuote) { + throwParseError(fullExpr, expr.data(), std::string("Expected ") + openQuote + '.'); + } expr.remove_prefix(1); skipSpaces(); - if (expr.empty() || expr[0] != ')') throwParseError(fullExpr, expr.data(), "Expected ')'."); + if (expr.empty() || expr[0] != ')') { + throwParseError(fullExpr, expr.data(), "Expected ')'."); + } expr.remove_prefix(1); return Point{x, y}; } template -void SortExpression::parseDistance(std::string_view& expr, const std::vector& joinedSelectors, std::string_view const fullExpr, +void SortExpression::parseDistance(std::string_view& expr, const std::vector& joinedSelectors, const std::string_view fullExpr, const ArithmeticOpType op, const bool negative, const SkipSW& skipSpaces) { skipSpaces(); const auto parsedIndexName1 = parseIndexName(expr, joinedSelectors, fullExpr); skipSpaces(); if (parsedIndexName1.joinedSelectorIt != joinedSelectors.cend()) { - if (expr.empty() || expr[0] != ',') throwParseError(fullExpr, expr.data(), "Expected ','."); + if (expr.empty() || expr[0] != ',') { + throwParseError(fullExpr, expr.data(), "Expected ','."); + } expr.remove_prefix(1); skipSpaces(); const size_t jNsIdx1 = static_cast(parsedIndexName1.joinedSelectorIt - joinedSelectors.cbegin()); const auto parsedIndexName2 = parseIndexName(expr, joinedSelectors, fullExpr); if (parsedIndexName2.joinedSelectorIt != joinedSelectors.cend()) { if (parsedIndexName1.joinedSelectorIt == parsedIndexName2.joinedSelectorIt) { - if (toLower(parsedIndexName1.name) == toLower(parsedIndexName2.name)) + if (toLower(parsedIndexName1.name) == toLower(parsedIndexName2.name)) { throwParseError(fullExpr, expr.data(), "Distance between two same indexes"); + } Append({op, negative}, DistanceBetweenJoinedIndexesSameNs{jNsIdx1, parsedIndexName1.name, parsedIndexName2.name}); } else { Append({op, negative}, @@ -330,12 +361,16 @@ void SortExpression::parseDistance(std::string_view& expr, const std::vector& } else if (!expr.empty() && expr[0] == '(') { const auto point = parsePoint(expr, toLower(parsedIndexName1.name), fullExpr, skipSpaces); skipSpaces(); - if (expr.empty() || expr[0] != ',') throwParseError(fullExpr, expr.data(), "Expected ','."); + if (expr.empty() || expr[0] != ',') { + throwParseError(fullExpr, expr.data(), "Expected ','."); + } expr.remove_prefix(1); skipSpaces(); const auto parsedIndexName2 = parseIndexName(expr, joinedSelectors, fullExpr); skipSpaces(); - if (!expr.empty() && expr[0] == '(') throwParseError(fullExpr, expr.data(), "Allowed only one function inside ST_Geometry"); + if (!expr.empty() && expr[0] == '(') { + throwParseError(fullExpr, expr.data(), "Allowed only one function inside ST_Geometry"); + } if (parsedIndexName2.joinedSelectorIt != joinedSelectors.cend()) { Append({op, negative}, DistanceJoinedIndexFromPoint{static_cast(parsedIndexName2.joinedSelectorIt - joinedSelectors.cbegin()), @@ -344,7 +379,9 @@ void SortExpression::parseDistance(std::string_view& expr, const std::vector& Append({op, negative}, DistanceFromPoint{parsedIndexName2.name, point}); } } else { - if (expr.empty() || expr[0] != ',') throwParseError(fullExpr, expr.data(), "Expected ','."); + if (expr.empty() || expr[0] != ',') { + throwParseError(fullExpr, expr.data(), "Expected ','."); + } expr.remove_prefix(1); skipSpaces(); const auto parsedIndexName2 = parseIndexName(expr, joinedSelectors, fullExpr); @@ -359,8 +396,9 @@ void SortExpression::parseDistance(std::string_view& expr, const std::vector& const auto point = parsePoint(expr, toLower(parsedIndexName2.name), fullExpr, skipSpaces); Append({op, negative}, DistanceFromPoint{parsedIndexName1.name, point}); } else { - if (toLower(parsedIndexName1.name) == toLower(parsedIndexName2.name)) + if (toLower(parsedIndexName1.name) == toLower(parsedIndexName2.name)) { throwParseError(fullExpr, expr.data(), "Distance between two same indexes"); + } Append({op, negative}, DistanceBetweenIndexes{parsedIndexName1.name, parsedIndexName2.name}); } } @@ -369,7 +407,7 @@ void SortExpression::parseDistance(std::string_view& expr, const std::vector& } template -std::string_view SortExpression::parse(std::string_view expr, bool* containIndexOrFunction, std::string_view const fullExpr, +std::string_view SortExpression::parse(std::string_view expr, bool* containIndexOrFunction, const std::string_view fullExpr, const std::vector& joinedSelectors) { using namespace double_conversion; static const StringToDoubleConverter converter{StringToDoubleConverter::ALLOW_TRAILING_JUNK | @@ -381,7 +419,9 @@ std::string_view SortExpression::parse(std::string_view expr, bool* containIndex bool lastOperationPlusOrMinus = false; ArithmeticOpType op = OpPlus; const auto skipSpaces = [&expr]() { - while (!expr.empty() && isspace(expr[0])) expr.remove_prefix(1); + while (!expr.empty() && isspace(expr[0])) { + expr.remove_prefix(1); + } }; skipSpaces(); while (!expr.empty()) { @@ -397,13 +437,17 @@ std::string_view SortExpression::parse(std::string_view expr, bool* containIndex } expr.remove_prefix(1); skipSpaces(); - if (expr.empty()) throwParseError(fullExpr, expr.data(), "The expression unexpected ends after unary operator."); + if (expr.empty()) { + throwParseError(fullExpr, expr.data(), "The expression unexpected ends after unary operator."); + } } if (expr[0] == '(') { expr.remove_prefix(1); OpenBracket({op, negative}); expr = parse(expr, containIndexOrFunction, fullExpr, joinedSelectors); - if (expr.empty() || expr[0] != ')') throwParseError(fullExpr, expr.data(), "Expected ')'."); + if (expr.empty() || expr[0] != ')') { + throwParseError(fullExpr, expr.data(), "Expected ')'."); + } expr.remove_prefix(1); CloseBracket(); } else if (expr[0] == '"') { @@ -441,7 +485,9 @@ std::string_view SortExpression::parse(std::string_view expr, bool* containIndex } else { throwParseError(fullExpr, expr.data(), "Unsupported function name : '" + funcName + "'."); } - if (expr.empty() || expr[0] != ')') throwParseError(fullExpr, expr.data(), "Expected ')'."); + if (expr.empty() || expr[0] != ')') { + throwParseError(fullExpr, expr.data(), "Expected ')'."); + } expr.remove_prefix(1); } else { Append({op, negative}, SortExprFuncs::Index{parsedIndexName.name}); @@ -457,7 +503,9 @@ std::string_view SortExpression::parse(std::string_view expr, bool* containIndex } else { switch (expr[0]) { case ')': - if (needCloseBracket) CloseBracket(); + if (needCloseBracket) { + CloseBracket(); + } return expr; case '+': case '-': @@ -486,8 +534,12 @@ std::string_view SortExpression::parse(std::string_view expr, bool* containIndex } skipSpaces(); } - if (expectValue) throwParseError(fullExpr, expr.data(), "Expected value."); - if (needCloseBracket) CloseBracket(); + if (expectValue) { + throwParseError(fullExpr, expr.data(), "Expected value."); + } + if (needCloseBracket) { + CloseBracket(); + } return expr; } @@ -496,8 +548,12 @@ SortExpression SortExpression::Parse(const std::string_view expression, const st SortExpression result; bool containIndexOrFunction = false; const auto expr = result.parse(expression, &containIndexOrFunction, expression, joinedSelector); - if (!expr.empty()) throwParseError(expression, expr.data(), ""); - if (!containIndexOrFunction) throwParseError(expression, expr.data(), "Sort expression does not depend from namespace data"); + if (!expr.empty()) { + throwParseError(expression, expr.data(), ""); + } + if (!containIndexOrFunction) { + throwParseError(expression, expr.data(), "Sort expression does not depend from namespace data"); + } return result; } @@ -546,7 +602,9 @@ double SortExpression::calculate(const_iterator it, const_iterator end, IdType r assertrx_throw(joinedResults); return i.GetValue(rowId, *joinedResults, js); }); - if (it->operation.negative) value = -value; + if (it->operation.negative) { + value = -value; + } switch (it->operation.op) { case OpPlus: result += value; @@ -558,7 +616,9 @@ double SortExpression::calculate(const_iterator it, const_iterator end, IdType r result *= value; break; case OpDiv: - if (value == 0.0) throw Error(errQueryExec, "Division by zero in sort expression"); + if (value == 0.0) { + throw Error(errQueryExec, "Division by zero in sort expression"); + } result /= value; break; } @@ -593,7 +653,9 @@ void SortExpression::dump(const_iterator begin, const_iterator end, WrSerializer } ser << ' '; } - if (it->operation.negative) ser << "(-"; + if (it->operation.negative) { + ser << "(-"; + } it->Visit( [&it, &ser](const SortExpressionBracket& b) { ser << (b.IsAbs() ? "ABS(" : "("); @@ -618,7 +680,9 @@ void SortExpression::dump(const_iterator begin, const_iterator end, WrSerializer [&ser](const DistanceBetweenJoinedIndexesSameNs& i) { ser << "ST_Distance(joined " << i.nsIdx << ' ' << i.column1 << ", joined " << i.nsIdx << ' ' << i.column2 << ')'; }); - if (it->operation.negative) ser << ')'; + if (it->operation.negative) { + ser << ')'; + } } } diff --git a/cpp_src/core/nsselecter/sortingcontext.h b/cpp_src/core/nsselecter/sortingcontext.h index 8c8c4272c..d87e92b60 100644 --- a/cpp_src/core/nsselecter/sortingcontext.h +++ b/cpp_src/core/nsselecter/sortingcontext.h @@ -13,62 +13,70 @@ struct SortingEntry; struct SortingContext { struct RawDataParams { RawDataParams() = default; - RawDataParams(const void *p, const PayloadType &pt, int field) noexcept + RawDataParams(const void* p, const PayloadType& pt, int field) noexcept : ptr(p), type(ptr ? pt.Field(field).Type() : KeyValueType::Undefined{}) {} - const void *ptr = nullptr; + const void* ptr = nullptr; KeyValueType type = KeyValueType::Undefined{}; }; struct FieldEntry { - const SortingEntry &data; - Index *index = nullptr; + const SortingEntry& data; + Index* index = nullptr; RawDataParams rawData = {}; - const CollateOpts *opts = nullptr; + const CollateOpts* opts = nullptr; }; struct JoinedFieldEntry { - const SortingEntry &data; + const SortingEntry& data; size_t nsIdx; std::string_view field; int index = IndexValueType::NotSet; }; struct ExpressionEntry { - const SortingEntry &data; + const SortingEntry& data; size_t expression; }; using Entry = std::variant; [[nodiscard]] int sortId() const noexcept { - if (!enableSortOrders) return 0; - const Index *sortIdx = sortIndex(); + if (!enableSortOrders) { + return 0; + } + const Index* sortIdx = sortIndex(); return sortIdx ? int(sortIdx->SortId()) : 0; } - [[nodiscard]] Index *sortIndex() const noexcept { - if (entries.empty()) return nullptr; + [[nodiscard]] Index* sortIndex() const noexcept { + if (entries.empty()) { + return nullptr; + } // get_if is truly noexcept, so using it instead of std::visit - if (const auto *fe = std::get_if(&entries[0]); fe) { + if (const auto* fe = std::get_if(&entries[0]); fe) { return fe->index; } return nullptr; } - [[nodiscard]] const Index *sortIndexIfOrdered() const noexcept { - if (entries.empty() || !isIndexOrdered() || !enableSortOrders) return nullptr; + [[nodiscard]] const Index* sortIndexIfOrdered() const noexcept { + if (entries.empty() || !isIndexOrdered() || !enableSortOrders) { + return nullptr; + } // get_if is truly noexcept, so using it instead of std::visit - if (const auto *fe = std::get_if(&entries[0]); fe) { + if (const auto* fe = std::get_if(&entries[0]); fe) { return fe->index; } return nullptr; } [[nodiscard]] bool isOptimizationEnabled() const noexcept { return (uncommitedIndex >= 0) && sortIndex(); } [[nodiscard]] bool isIndexOrdered() const noexcept { - if (entries.empty()) return false; + if (entries.empty()) { + return false; + } // get_if is truly noexcept, so using it instead of std::visit - if (const auto *fe = std::get_if(&entries[0]); fe) { + if (const auto* fe = std::get_if(&entries[0]); fe) { return fe->index && fe->index->IsOrdered(); } return false; } - [[nodiscard]] const Entry &getFirstColumnEntry() const noexcept { + [[nodiscard]] const Entry& getFirstColumnEntry() const noexcept { assertrx_throw(!entries.empty()); return entries[0]; } @@ -76,7 +84,7 @@ struct SortingContext { uncommitedIndex = -1; if (!entries.empty()) { // get_if is truly noexcept, so using it instead of std::visit - if (auto *fe = std::get_if(&entries[0]); fe) { + if (auto* fe = std::get_if(&entries[0]); fe) { fe->index = nullptr; } } @@ -91,7 +99,7 @@ struct SortingContext { }; struct SortingOptions { - SortingOptions(const SortingContext &sortingContext) noexcept + SortingOptions(const SortingContext& sortingContext) noexcept : forcedMode{sortingContext.forcedMode}, multiColumn{sortingContext.entries.size() > 1}, haveExpression{!sortingContext.expressions.empty()} { @@ -100,7 +108,7 @@ struct SortingOptions { byBtreeIndex = false; } else { // get_if is truly noexcept, so using it instead of std::visit - if (auto *sortEntry = std::get_if(&sortingContext.entries[0]); sortEntry) { + if (auto* sortEntry = std::get_if(&sortingContext.entries[0]); sortEntry) { if (sortEntry->index && sortEntry->index->IsOrdered()) { byBtreeIndex = (sortingContext.isOptimizationEnabled() || sortingContext.enableSortOrders); multiColumnByBtreeIndex = (byBtreeIndex && multiColumn); diff --git a/cpp_src/core/nsselecter/substitutionhelpers.h b/cpp_src/core/nsselecter/substitutionhelpers.h index 8ebc54929..3c89f197b 100644 --- a/cpp_src/core/nsselecter/substitutionhelpers.h +++ b/cpp_src/core/nsselecter/substitutionhelpers.h @@ -32,9 +32,9 @@ class CompositeSearcher { h_vector entries; }; - CompositeSearcher(const NamespaceImpl &ns) noexcept : ns_(ns) {} + CompositeSearcher(const NamespaceImpl& ns) noexcept : ns_(ns) {} - void Add(int field, const std::vector &composites, unsigned entry) { + void Add(int field, const std::vector& composites, unsigned entry) { assertrx_throw(entry < std::numeric_limits::max()); for (auto composite : composites) { const auto idxType = ns_.indexes_[composite]->Type(); @@ -42,7 +42,7 @@ class CompositeSearcher { continue; } bool found = false; - for (auto &d : d_) { + for (auto& d : d_) { if (d.idx == composite) { d.fields.push_back(field); d.entries.push_back(entry); @@ -59,8 +59,8 @@ class CompositeSearcher { int res = -1; unsigned maxSize = 0; for (int i = 0; i < int(d_.size()); ++i) { - auto &data = d_[i]; - const auto &idxFields = ns_.indexes_[data.idx]->Fields(); + auto& data = d_[i]; + const auto& idxFields = ns_.indexes_[data.idx]->Fields(); // If all of the composite fields were found in query const auto dfCnt = data.fields.count(); if (dfCnt == idxFields.size() && idxFields.contains(data.fields)) { @@ -88,9 +88,9 @@ class CompositeSearcher { if (unsigned(curId) + 1 != d_.size()) { std::swap(d_[curId], d_.back()); } - const auto &cur = d_.back(); + const auto& cur = d_.back(); for (unsigned i = 0, sz = d_.size(); i < sz - deleted; ++i) { - auto &data = d_[i]; + auto& data = d_[i]; if (haveIntersection(data.entries, cur.entries)) { std::swap(data, d_[sz - ++deleted]); --i; @@ -107,7 +107,7 @@ class CompositeSearcher { } return res; } - const IndexData &operator[](uint16_t i) const noexcept { return d_[i]; } + const IndexData& operator[](uint16_t i) const noexcept { return d_[i]; } private: void remove(uint16_t i) noexcept { @@ -116,7 +116,7 @@ class CompositeSearcher { } d_.pop_back(); } - static bool haveIntersection(const h_vector &lEntries, const h_vector &rEntries) noexcept { + static bool haveIntersection(const h_vector& lEntries, const h_vector& rEntries) noexcept { for (auto lit = lEntries.begin(), rit = rEntries.begin(); lit != lEntries.end() && rit != rEntries.end();) { if (*lit < *rit) { ++lit; @@ -130,7 +130,7 @@ class CompositeSearcher { } h_vector d_; - const NamespaceImpl &ns_; + const NamespaceImpl& ns_; }; // EntriesRange - query entries range. [from; to) @@ -150,7 +150,7 @@ class EntriesRange { } --from_; } - bool Append(const EntriesRange &r) noexcept { + bool Append(const EntriesRange& r) noexcept { if (to_ == r.from_) { to_ = r.to_; return true; diff --git a/cpp_src/core/payload/fieldsset.cc b/cpp_src/core/payload/fieldsset.cc index 80a6b85c6..6f929ab16 100644 --- a/cpp_src/core/payload/fieldsset.cc +++ b/cpp_src/core/payload/fieldsset.cc @@ -7,8 +7,8 @@ namespace reindexer { throw Error(errLogic, "Can not push_back(%d) to IndexesFieldsSet. Value must be in scope [-1,%d]", f, kMaxIndexes - 1); } -FieldsSet::FieldsSet(const TagsMatcher &tagsMatcher, const h_vector &fields) : mask_(0) { - for (const std::string &str : fields) { +FieldsSet::FieldsSet(const TagsMatcher& tagsMatcher, const h_vector& fields) : mask_(0) { + for (const std::string& str : fields) { tagsPaths_.emplace_back(tagsMatcher.path2tag(str)); } } diff --git a/cpp_src/core/payload/fieldsset.h b/cpp_src/core/payload/fieldsset.h index 98c0062ae..849738f5b 100644 --- a/cpp_src/core/payload/fieldsset.h +++ b/cpp_src/core/payload/fieldsset.h @@ -28,14 +28,16 @@ class IndexesFieldsSet { IndexesFieldsSet(int f) { push_back(f); } bool contains(int f) const noexcept { return f >= 0 && f < kMaxIndexes && mask_.test(unsigned(f)); } void push_back(int f) { - if (f < 0) return; + if (f < 0) { + return; + } if (f >= kMaxIndexes) { throwMaxValueError(f); } mask_.set(unsigned(f)); } - const std::bitset &mask() const &noexcept { return mask_; } - const std::bitset &mask() const && = delete; + const std::bitset& mask() const& noexcept { return mask_; } + const std::bitset& mask() const&& = delete; unsigned count() const noexcept { return mask_.count(); } private: @@ -51,46 +53,54 @@ class FieldsSet : protected base_fields_set { using base_fields_set::size; using base_fields_set::empty; using base_fields_set::operator[]; - FieldsSet(const TagsMatcher &, const h_vector &fields); + FieldsSet(const TagsMatcher&, const h_vector& fields); FieldsSet(int f) { push_back(f); } FieldsSet(std::initializer_list l) { - for (auto f : l) push_back(f); + for (auto f : l) { + push_back(f); + } } FieldsSet(std::initializer_list l) { - for (const TagsPath &tagsPath : l) push_back(tagsPath); + for (const TagsPath& tagsPath : l) { + push_back(tagsPath); + } } FieldsSet(std::initializer_list l) { - for (const IndexedTagsPath &tagsPath : l) push_back(tagsPath); + for (const IndexedTagsPath& tagsPath : l) { + push_back(tagsPath); + } } FieldsSet() = default; - void push_back(const std::string &jsonPath) { + void push_back(const std::string& jsonPath) { if (!contains(jsonPath)) { jsonPaths_.push_back(jsonPath); } } - void push_back(std::string &&jsonPath) { + void push_back(std::string&& jsonPath) { if (!contains(jsonPath)) { jsonPaths_.emplace_back(std::move(jsonPath)); } } - void push_back(const TagsPath &tagsPath) { pushBack(tagsPath); } - void push_back(TagsPath &&tagsPath) { pushBack(std::move(tagsPath)); } - void push_front(TagsPath &&tagsPath) { + void push_back(const TagsPath& tagsPath) { pushBack(tagsPath); } + void push_back(TagsPath&& tagsPath) { pushBack(std::move(tagsPath)); } + void push_front(TagsPath&& tagsPath) { if (!contains(tagsPath)) { base_fields_set::insert(begin(), IndexValueType::SetByJsonPath); tagsPaths_.insert(tagsPaths_.begin(), std::move(tagsPath)); } } - void push_back(const IndexedTagsPath &tagsPath) { pushBack(tagsPath); } - void push_back(IndexedTagsPath &&tagsPath) { pushBack(std::move(tagsPath)); } - void push_back(const FieldsPath &fieldPath) { pushBack(fieldPath); } - void push_back(FieldsPath &&fieldPath) { pushBack(std::move(fieldPath)); } + void push_back(const IndexedTagsPath& tagsPath) { pushBack(tagsPath); } + void push_back(IndexedTagsPath&& tagsPath) { pushBack(std::move(tagsPath)); } + void push_back(const FieldsPath& fieldPath) { pushBack(fieldPath); } + void push_back(FieldsPath&& fieldPath) { pushBack(std::move(fieldPath)); } void push_back(int f) { - if (f < 0) return; + if (f < 0) { + return; + } if (f >= kMaxIndexes) { throwMaxValueError(f); } @@ -100,7 +110,9 @@ class FieldsSet : protected base_fields_set { } } void push_front(int f) { - if (f < 0) return; + if (f < 0) { + return; + } if (f >= kMaxIndexes) { throwMaxValueError(f); } @@ -116,44 +128,48 @@ class FieldsSet : protected base_fields_set { auto it = std::find(begin(), end(), f); assertrx(it != end()); base_fields_set::erase(it); - if (!byJsonPath) mask_.reset(unsigned(f)); + if (!byJsonPath) { + mask_.reset(unsigned(f)); + } } } bool contains(int f) const noexcept { return f >= 0 && f < kMaxIndexes && mask_.test(unsigned(f)); } - bool contains(const FieldsSet &f) const noexcept { return (mask_ & f.mask_) == f.mask_; } + bool contains(const FieldsSet& f) const noexcept { return (mask_ & f.mask_) == f.mask_; } bool contains(std::string_view jsonPath) const noexcept { return std::find(jsonPaths_.begin(), jsonPaths_.end(), jsonPath) != jsonPaths_.end(); } - bool contains(const IndexesFieldsSet &f) const noexcept { return (mask_ & f.mask()) == f.mask(); } - bool contains(const TagsPath &tagsPath) const noexcept { - for (const FieldsPath &path : tagsPaths_) { - if (std::visit(overloaded{[&tagsPath](const TagsPath &path) { return path == tagsPath; }, - [&tagsPath](const IndexedTagsPath &path) { return path.Compare(tagsPath); }}, + bool contains(const IndexesFieldsSet& f) const noexcept { return (mask_ & f.mask()) == f.mask(); } + bool contains(const TagsPath& tagsPath) const noexcept { + for (const FieldsPath& path : tagsPaths_) { + if (std::visit(overloaded{[&tagsPath](const TagsPath& path) { return path == tagsPath; }, + [&tagsPath](const IndexedTagsPath& path) { return path.Compare(tagsPath); }}, path)) { return true; } } return false; } - bool contains(const IndexedTagsPath &tagsPath) const noexcept { - for (const FieldsPath &path : tagsPaths_) { - if (std::visit(overloaded{[&tagsPath](const TagsPath &path) { return tagsPath.Compare(path); }, - [&tagsPath](const IndexedTagsPath &path) { return path == tagsPath; }}, + bool contains(const IndexedTagsPath& tagsPath) const noexcept { + for (const FieldsPath& path : tagsPaths_) { + if (std::visit(overloaded{[&tagsPath](const TagsPath& path) { return tagsPath.Compare(path); }, + [&tagsPath](const IndexedTagsPath& path) { return path == tagsPath; }}, path)) { return true; } } return false; } - bool contains(const FieldsPath &fieldsPath) const noexcept { - return std::visit([&](const auto &fp) { return contains(fp); }, fieldsPath); + bool contains(const FieldsPath& fieldsPath) const noexcept { + return std::visit([&](const auto& fp) { return contains(fp); }, fieldsPath); } - bool match(const TagsPath &tagsPath) const noexcept { - if (tagsPaths_.empty()) return true; - for (auto &path : tagsPaths_) { - if (std::visit(overloaded{[&tagsPath, this](const TagsPath &path) { return comparePaths(tagsPath, path); }, - [&tagsPath, this](const IndexedTagsPath &path) { return comparePaths(path, tagsPath); }}, + bool match(const TagsPath& tagsPath) const noexcept { + if (tagsPaths_.empty()) { + return true; + } + for (auto& path : tagsPaths_) { + if (std::visit(overloaded{[&tagsPath, this](const TagsPath& path) { return comparePaths(tagsPath, path); }, + [&tagsPath, this](const IndexedTagsPath& path) { return comparePaths(path, tagsPath); }}, path)) { return true; } @@ -161,11 +177,13 @@ class FieldsSet : protected base_fields_set { return false; } template - bool match(const IndexedTagsPathImpl &tagsPath) const noexcept { - if (tagsPaths_.empty()) return true; - for (auto &path : tagsPaths_) { - if (std::visit(overloaded{[&tagsPath, this](const TagsPath &path) { return comparePaths(tagsPath, path); }, - [&tagsPath, this](const IndexedTagsPath &path) { return comparePaths(tagsPath, path); }}, + bool match(const IndexedTagsPathImpl& tagsPath) const noexcept { + if (tagsPaths_.empty()) { + return true; + } + for (auto& path : tagsPaths_) { + if (std::visit(overloaded{[&tagsPath, this](const TagsPath& path) { return comparePaths(tagsPath, path); }, + [&tagsPath, this](const IndexedTagsPath& path) { return comparePaths(tagsPath, path); }}, path)) { return true; } @@ -181,43 +199,48 @@ class FieldsSet : protected base_fields_set { size_t getTagsPathsLength() const noexcept { return tagsPaths_.size(); } size_t getJsonPathsLength() const noexcept { return jsonPaths_.size(); } - const h_vector &getJsonPaths() const noexcept { return jsonPaths_; } + const h_vector& getJsonPaths() const noexcept { return jsonPaths_; } bool isTagsPathIndexed(size_t idx) const noexcept { assertrx(idx < tagsPaths_.size()); - return std::visit(overloaded{[](const TagsPath &) { return false; }, [](const IndexedTagsPath &) { return true; }}, - tagsPaths_[idx]); + return std::visit(overloaded{[](const TagsPath&) { return false; }, [](const IndexedTagsPath&) { return true; }}, tagsPaths_[idx]); } - const TagsPath &getTagsPath(size_t idx) const & { return std::get(tagsPaths_[idx]); } - const TagsPath &getTagsPath(size_t idx) const && = delete; - const IndexedTagsPath &getIndexedTagsPath(size_t idx) const & { return std::get(tagsPaths_[idx]); } - const IndexedTagsPath &getIndexedTagsPath(size_t idx) const && = delete; - const FieldsPath &getFieldsPath(size_t idx) const & { return tagsPaths_[idx]; } - const FieldsPath &getFieldsPath(size_t idx) const && = delete; - const std::string &getJsonPath(size_t idx) const &noexcept { return jsonPaths_[idx]; } - const std::string &getJsonPath(size_t idx) const && = delete; + const TagsPath& getTagsPath(size_t idx) const& { return std::get(tagsPaths_[idx]); } + const TagsPath& getTagsPath(size_t idx) const&& = delete; + const IndexedTagsPath& getIndexedTagsPath(size_t idx) const& { return std::get(tagsPaths_[idx]); } + const IndexedTagsPath& getIndexedTagsPath(size_t idx) const&& = delete; + const FieldsPath& getFieldsPath(size_t idx) const& { return tagsPaths_[idx]; } + const FieldsPath& getFieldsPath(size_t idx) const&& = delete; + const std::string& getJsonPath(size_t idx) const& noexcept { return jsonPaths_[idx]; } + const std::string& getJsonPath(size_t idx) const&& = delete; - bool operator==(const FieldsSet &f) const noexcept { + bool operator==(const FieldsSet& f) const noexcept { return (mask_ == f.mask_) && (tagsPaths_ == f.tagsPaths_) && (jsonPaths_ == jsonPaths_); } - bool operator!=(const FieldsSet &f) const noexcept { return !(*this == f); } + bool operator!=(const FieldsSet& f) const noexcept { return !(*this == f); } template - void Dump(T &os) const { - DumpFieldsPath const fieldsPathDumper{os}; + void Dump(T& os) const { + const DumpFieldsPath fieldsPathDumper{os}; os << "{["; for (auto b = begin(), it = b, e = end(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << *it; } os << "], mask: " << mask_ << ", tagsPaths: ["; for (auto b = tagsPaths_.cbegin(), it = b, e = tagsPaths_.cend(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } std::visit(fieldsPathDumper, *it); } os << "]}"; os << "], jsonPaths: ["; for (auto b = jsonPaths_.cbegin(), it = b, e = jsonPaths_.cend(); it != e; ++it) { - if (it != b) os << ", "; + if (it != b) { + os << ", "; + } os << *it; } os << "]}"; @@ -225,14 +248,14 @@ class FieldsSet : protected base_fields_set { private: template - void pushBack(F &&fieldPath) { + void pushBack(F&& fieldPath) { if (!contains(fieldPath)) { base_fields_set::push_back(IndexValueType::SetByJsonPath); tagsPaths_.emplace_back(std::forward(fieldPath)); } } template - bool comparePaths(const TPath1 &lhs, const TPath2 &rhs) const noexcept { + bool comparePaths(const TPath1& lhs, const TPath2& rhs) const noexcept { unsigned i = 0, count = std::min(lhs.size(), rhs.size()); for (; i < count && lhs[i] == rhs[i]; ++i) { } @@ -251,26 +274,30 @@ class FieldsSet : protected base_fields_set { template class DumpFieldsPath { public: - DumpFieldsPath(T &os) noexcept : os_{os} {} - void operator()(const TagsPath &tp) const { + DumpFieldsPath(T& os) noexcept : os_{os} {} + void operator()(const TagsPath& tp) const { os_ << '['; for (auto b = tp.cbegin(), it = b, e = tp.cend(); it != e; ++it) { - if (it != b) os_ << ", "; + if (it != b) { + os_ << ", "; + } os_ << *it; } os_ << ']'; } - void operator()(const IndexedTagsPath &tp) const { + void operator()(const IndexedTagsPath& tp) const { os_ << '['; for (auto b = tp.cbegin(), it = b, e = tp.cend(); it != e; ++it) { - if (it != b) os_ << ", "; + if (it != b) { + os_ << ", "; + } os_ << '?'; } os_ << ']'; } private: - T &os_; + T& os_; }; }; diff --git a/cpp_src/core/payload/payloadfieldtype.cc b/cpp_src/core/payload/payloadfieldtype.cc index 25622abe8..36d83f02f 100644 --- a/cpp_src/core/payload/payloadfieldtype.cc +++ b/cpp_src/core/payload/payloadfieldtype.cc @@ -7,7 +7,9 @@ namespace reindexer { size_t PayloadFieldType::Sizeof() const noexcept { - if (IsArray()) return sizeof(PayloadFieldValue::Array); + if (IsArray()) { + return sizeof(PayloadFieldValue::Array); + } return ElemSizeof(); } @@ -24,7 +26,9 @@ size_t PayloadFieldType::ElemSizeof() const noexcept { } size_t PayloadFieldType::Alignof() const noexcept { - if (IsArray()) return alignof(PayloadFieldValue::Array); + if (IsArray()) { + return alignof(PayloadFieldValue::Array); + } return Type().EvaluateOneOf( [](KeyValueType::Bool) noexcept { return alignof(bool); }, [](KeyValueType::Int) noexcept { return alignof(int); }, [](KeyValueType::Int64) noexcept { return alignof(int64_t); }, [](KeyValueType::Uuid) noexcept { return alignof(Uuid); }, diff --git a/cpp_src/core/payload/payloadfieldtype.h b/cpp_src/core/payload/payloadfieldtype.h index ba762c934..b4cb5588c 100644 --- a/cpp_src/core/payload/payloadfieldtype.h +++ b/cpp_src/core/payload/payloadfieldtype.h @@ -19,11 +19,11 @@ class PayloadFieldType { void SetOffset(size_t o) noexcept { offset_ = o; } size_t Offset() const noexcept { return offset_; } KeyValueType Type() const noexcept { return type_; } - const std::string &Name() const &noexcept { return name_; } - const std::string &Name() && = delete; - const std::vector &JsonPaths() const &noexcept { return jsonPaths_; } - const std::vector &JsonPaths() && = delete; - void AddJsonPath(const std::string &jsonPath) { jsonPaths_.push_back(jsonPath); } + const std::string& Name() const& noexcept { return name_; } + const std::string& Name() && = delete; + const std::vector& JsonPaths() const& noexcept { return jsonPaths_; } + const std::vector& JsonPaths() && = delete; + void AddJsonPath(const std::string& jsonPath) { jsonPaths_.push_back(jsonPath); } private: KeyValueType type_; diff --git a/cpp_src/core/payload/payloadfieldvalue.h b/cpp_src/core/payload/payloadfieldvalue.h index 21c549aac..af9fe371e 100644 --- a/cpp_src/core/payload/payloadfieldvalue.h +++ b/cpp_src/core/payload/payloadfieldvalue.h @@ -17,17 +17,23 @@ class PayloadFieldValue { int len; }; // Construct object - PayloadFieldValue(const PayloadFieldType &t, uint8_t *v) noexcept : t_(t), p_(v) {} + PayloadFieldValue(const PayloadFieldType& t, uint8_t* v) noexcept : t_(t), p_(v) {} // Single value operations void Set(Variant kv) { t_.Type().EvaluateOneOf(overloaded{[&kv](KeyValueType::Int64) { - if (kv.Type().Is()) kv.convert(KeyValueType::Int64{}); + if (kv.Type().Is()) { + kv.convert(KeyValueType::Int64{}); + } }, [&kv](KeyValueType::Int) { - if (kv.Type().Is()) kv.convert(KeyValueType::Int{}); + if (kv.Type().Is()) { + kv.convert(KeyValueType::Int{}); + } }, [&kv](KeyValueType::Uuid) { - if (kv.Type().Is()) kv.convert(KeyValueType::Uuid{}); + if (kv.Type().Is()) { + kv.convert(KeyValueType::Uuid{}); + } }, [](OneOf) noexcept {}}); @@ -36,12 +42,12 @@ class PayloadFieldValue { } t_.Type().EvaluateOneOf( - [&](KeyValueType::Int) noexcept { *reinterpret_cast(p_) = int(kv); }, - [&](KeyValueType::Bool) noexcept { *reinterpret_cast(p_) = bool(kv); }, - [&](KeyValueType::Int64) noexcept { *reinterpret_cast(p_) = int64_t(kv); }, - [&](KeyValueType::Double) noexcept { *reinterpret_cast(p_) = double(kv); }, - [&](KeyValueType::String) noexcept { *reinterpret_cast(p_) = p_string(kv); }, - [&](KeyValueType::Uuid) noexcept { *reinterpret_cast(p_) = Uuid{kv}; }, + [&](KeyValueType::Int) noexcept { *reinterpret_cast(p_) = int(kv); }, + [&](KeyValueType::Bool) noexcept { *reinterpret_cast(p_) = bool(kv); }, + [&](KeyValueType::Int64) noexcept { *reinterpret_cast(p_) = int64_t(kv); }, + [&](KeyValueType::Double) noexcept { *reinterpret_cast(p_) = double(kv); }, + [&](KeyValueType::String) noexcept { *reinterpret_cast(p_) = p_string(kv); }, + [&](KeyValueType::Uuid) noexcept { *reinterpret_cast(p_) = Uuid{kv}; }, [](OneOf) noexcept { assertrx(0); abort(); @@ -51,14 +57,14 @@ class PayloadFieldValue { template Variant Get(HoldT h) const noexcept(noexcept(Variant(std::declval(), h))) { return t_.Type().EvaluateOneOf( - [&](KeyValueType::Bool) noexcept { return Variant(*reinterpret_cast(p_)); }, - [&](KeyValueType::Int) noexcept { return Variant(*reinterpret_cast(p_)); }, - [&](KeyValueType::Int64) noexcept { return Variant(*reinterpret_cast(p_)); }, - [&](KeyValueType::Double) noexcept { return Variant(*reinterpret_cast(p_)); }, + [&](KeyValueType::Bool) noexcept { return Variant(*reinterpret_cast(p_)); }, + [&](KeyValueType::Int) noexcept { return Variant(*reinterpret_cast(p_)); }, + [&](KeyValueType::Int64) noexcept { return Variant(*reinterpret_cast(p_)); }, + [&](KeyValueType::Double) noexcept { return Variant(*reinterpret_cast(p_)); }, [&](KeyValueType::String) noexcept(noexcept(Variant(std::declval(), h))) { - return Variant(*reinterpret_cast(p_), h); + return Variant(*reinterpret_cast(p_), h); }, - [&](KeyValueType::Uuid) noexcept { return Variant(*reinterpret_cast(p_)); }, + [&](KeyValueType::Uuid) noexcept { return Variant(*reinterpret_cast(p_)); }, [](OneOf) noexcept -> Variant { assertrx(0); abort(); @@ -79,31 +85,31 @@ class PayloadFieldValue { // } size_t Hash() const noexcept { return t_.Type().EvaluateOneOf( - [&](KeyValueType::Bool) noexcept { return std::hash()(*reinterpret_cast(p_)); }, - [&](KeyValueType::Int) noexcept { return std::hash()(*reinterpret_cast(p_)); }, - [&](KeyValueType::Int64) noexcept { return std::hash()(*reinterpret_cast(p_)); }, - [&](KeyValueType::Double) noexcept { return std::hash()(*reinterpret_cast(p_)); }, - [&](KeyValueType::String) noexcept { return std::hash()(*reinterpret_cast(p_)); }, - [&](KeyValueType::Uuid) noexcept { return std::hash()(*reinterpret_cast(p_)); }, + [&](KeyValueType::Bool) noexcept { return std::hash()(*reinterpret_cast(p_)); }, + [&](KeyValueType::Int) noexcept { return std::hash()(*reinterpret_cast(p_)); }, + [&](KeyValueType::Int64) noexcept { return std::hash()(*reinterpret_cast(p_)); }, + [&](KeyValueType::Double) noexcept { return std::hash()(*reinterpret_cast(p_)); }, + [&](KeyValueType::String) noexcept { return std::hash()(*reinterpret_cast(p_)); }, + [&](KeyValueType::Uuid) noexcept { return std::hash()(*reinterpret_cast(p_)); }, [](OneOf) noexcept -> size_t { assertrx(0); abort(); }); } - bool IsEQ(const PayloadFieldValue &o) const { - if (!t_.Type().IsSame(o.t_.Type())) return false; + bool IsEQ(const PayloadFieldValue& o) const { + if (!t_.Type().IsSame(o.t_.Type())) { + return false; + } return t_.Type().EvaluateOneOf( - [&](KeyValueType::Bool) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, - [&](KeyValueType::Int) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, - [&](KeyValueType::Int64) noexcept { - return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); - }, - [&](KeyValueType::Double) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, + [&](KeyValueType::Bool) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, + [&](KeyValueType::Int) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, + [&](KeyValueType::Int64) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, + [&](KeyValueType::Double) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, [&](KeyValueType::String) { - return collateCompare(*reinterpret_cast(p_), *reinterpret_cast(o.p_), + return collateCompare(*reinterpret_cast(p_), *reinterpret_cast(o.p_), SortingPrioritiesTable()) == ComparationResult::Eq; }, - [&](KeyValueType::Uuid) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, + [&](KeyValueType::Uuid) noexcept { return *reinterpret_cast(p_) == *reinterpret_cast(o.p_); }, [](OneOf) noexcept -> bool { assertrx(0); abort(); @@ -111,12 +117,12 @@ class PayloadFieldValue { } // Type of value, not owning - const PayloadFieldType &t_; + const PayloadFieldType& t_; // Value data, not owning - uint8_t *p_; + uint8_t* p_; private: - [[noreturn]] void throwSetTypeMissmatch(const Variant &kv); + [[noreturn]] void throwSetTypeMissmatch(const Variant& kv); }; } // namespace reindexer diff --git a/cpp_src/core/payload/payloadiface.cc b/cpp_src/core/payload/payloadiface.cc index b2a5877a0..207a4d07d 100644 --- a/cpp_src/core/payload/payloadiface.cc +++ b/cpp_src/core/payload/payloadiface.cc @@ -12,11 +12,11 @@ namespace reindexer { // Get element(s) by field index template -void PayloadIface::Get(int field, VariantArray &keys, Variant::hold_t h) const { +void PayloadIface::Get(int field, VariantArray& keys, Variant::hold_t h) const { get(field, keys, h); } template -void PayloadIface::Get(int field, VariantArray &keys) const { +void PayloadIface::Get(int field, VariantArray& keys) const { get(field, keys, Variant::no_hold_t{}); } @@ -32,21 +32,21 @@ Variant PayloadIface::Get(int field, int idx) const { // Get element(s) by field name template -void PayloadIface::Get(std::string_view field, VariantArray &kvs, Variant::hold_t h) const { +void PayloadIface::Get(std::string_view field, VariantArray& kvs, Variant::hold_t h) const { get(t_.FieldByName(field), kvs, h); } template -void PayloadIface::Get(std::string_view field, VariantArray &kvs) const { +void PayloadIface::Get(std::string_view field, VariantArray& kvs) const { get(t_.FieldByName(field), kvs, Variant::no_hold_t{}); } template template -void PayloadIface::get(int field, VariantArray &keys, HoldT h) const { +void PayloadIface::get(int field, VariantArray& keys, HoldT h) const { assertrx(field < NumFields()); keys.clear(); if (t_.Field(field).IsArray()) { - auto *arr = reinterpret_cast(Field(field).p_); + auto* arr = reinterpret_cast(Field(field).p_); keys.reserve(arr->len); for (int i = 0; i < arr->len; i++) { @@ -64,7 +64,7 @@ Variant PayloadIface::get(int field, int idx, HoldT h) const { assertrx(field < NumFields()); if (t_.Field(field).IsArray()) { - auto *arr = reinterpret_cast(Field(field).p_); + auto* arr = reinterpret_cast(Field(field).p_); assertf(idx < arr->len, "Field '%s.%s' bound exceed idx %d > len %d", Type().Name(), Type().Field(field).Name(), idx, arr->len); PayloadFieldValue pv(t_.Field(field), v_->Ptr() + arr->offset + idx * t_.Field(field).ElemSizeof()); @@ -76,7 +76,7 @@ Variant PayloadIface::get(int field, int idx, HoldT h) const { } template -void PayloadIface::GetByJsonPath(std::string_view jsonPath, TagsMatcher &tagsMatcher, VariantArray &kvs, +void PayloadIface::GetByJsonPath(std::string_view jsonPath, TagsMatcher& tagsMatcher, VariantArray& kvs, KeyValueType expectedType) const { VariantArray krefs; Get(0, krefs); @@ -102,7 +102,7 @@ void PayloadIface::GetByJsonPath(std::string_view jsonPath, TagsMatcher &tags template template -void PayloadIface::getByJsonPath(const P &path, VariantArray &krefs, KeyValueType expectedType) const { +void PayloadIface::getByJsonPath(const P& path, VariantArray& krefs, KeyValueType expectedType) const { krefs.clear(); if (path.empty()) { return; @@ -115,18 +115,18 @@ void PayloadIface::getByJsonPath(const P &path, VariantArray &krefs, KeyValue } template -void PayloadIface::GetByJsonPath(const TagsPath &tagsPath, VariantArray &krefs, KeyValueType expectedType) const { +void PayloadIface::GetByJsonPath(const TagsPath& tagsPath, VariantArray& krefs, KeyValueType expectedType) const { getByJsonPath(tagsPath, krefs, expectedType); } template -void PayloadIface::GetByJsonPath(const IndexedTagsPath &tagsPath, VariantArray &krefs, KeyValueType expectedType) const { +void PayloadIface::GetByJsonPath(const IndexedTagsPath& tagsPath, VariantArray& krefs, KeyValueType expectedType) const { getByJsonPath(tagsPath, krefs, expectedType); } template -void PayloadIface::GetByFieldsSet(const FieldsSet &fields, VariantArray &kvs, KeyValueType expectedType, - const h_vector &expectedCompositeTypes) const { +void PayloadIface::GetByFieldsSet(const FieldsSet& fields, VariantArray& kvs, KeyValueType expectedType, + const h_vector& expectedCompositeTypes) const { if (expectedType.Is()) { kvs.Clear(); kvs.emplace_back(GetComposite(fields, expectedCompositeTypes)); @@ -146,7 +146,7 @@ void PayloadIface::GetByFieldsSet(const FieldsSet &fields, VariantArray &kvs, } template -Variant PayloadIface::GetComposite(const FieldsSet &fields, const h_vector &expectedTypes) const { +Variant PayloadIface::GetComposite(const FieldsSet& fields, const h_vector& expectedTypes) const { thread_local VariantArray buffer; buffer.clear(); assertrx_throw(fields.size() == expectedTypes.size()); @@ -173,7 +173,7 @@ Variant PayloadIface::GetComposite(const FieldsSet &fields, const h_vector -VariantArray PayloadIface::GetIndexedArrayData(const IndexedTagsPath &tagsPath, int field, int &offset, int &size) const { +VariantArray PayloadIface::GetIndexedArrayData(const IndexedTagsPath& tagsPath, int field, int& offset, int& size) const { if (tagsPath.empty()) { throw Error(errParams, "GetIndexedArrayData(): tagsPath shouldn't be empty!"); } @@ -194,8 +194,8 @@ VariantArray PayloadIface::GetIndexedArrayData(const IndexedTagsPath &tagsPat } template -template ::value>::type *> -void PayloadIface::SetSingleElement(int field, const Variant &key) { +template ::value>::type*> +void PayloadIface::SetSingleElement(int field, const Variant& key) { if (t_.Field(field).IsArray()) { throw Error(errLogic, "Unable to set array field via single field setter"); } @@ -204,11 +204,11 @@ void PayloadIface::SetSingleElement(int field, const Variant &key) { // Set element or array by field index and element index template -template ::value>::type *> -void PayloadIface::Set(int field, int idx, const Variant &v) { +template ::value>::type*> +void PayloadIface::Set(int field, int idx, const Variant& v) { assertrx(idx >= 0); assertrx(t_.Field(field).IsArray()); - auto const *const arr = reinterpret_cast(Field(field).p_); + const auto* const arr = reinterpret_cast(Field(field).p_); const auto elemSize = t_.Field(field).ElemSizeof(); assertrx(idx < arr->len); PayloadFieldValue pv(t_.Field(field), v_->Ptr() + arr->offset + idx * elemSize); @@ -220,7 +220,7 @@ int PayloadIface::ResizeArray(int field, int count, bool append) { assertrx(t_.Field(field).IsArray()); size_t realSize = RealSize(); - auto *arr = reinterpret_cast(Field(field).p_); + auto* arr = reinterpret_cast(Field(field).p_); auto elemSize = t_.Field(field).ElemSizeof(); size_t grow = elemSize * count; @@ -236,16 +236,21 @@ int PayloadIface::ResizeArray(int field, int count, bool append) { v_->Resize(realSize, realSize + grow - strip); memmove(v_->Ptr() + insert + grow - strip, v_->Ptr() + insert, realSize - insert); - arr = reinterpret_cast(Field(field).p_); - if (!arr->offset) arr->offset = insert; + arr = reinterpret_cast(Field(field).p_); + if (!arr->offset) { + arr->offset = insert; + } arr->len += count; // Move another arrays, after our - for (int f = 0; f < NumFields(); f++) + for (int f = 0; f < NumFields(); f++) { if (f != field && t_.Field(f).IsArray()) { - auto *arr = reinterpret_cast(Field(f).p_); - if (arr->offset >= insert) arr->offset += grow - strip; + auto* arr = reinterpret_cast(Field(f).p_); + if (arr->offset >= insert) { + arr->offset += grow - strip; + } } + } return arr->len - count; } @@ -253,28 +258,31 @@ int PayloadIface::ResizeArray(int field, int count, bool append) { template size_t PayloadIface::RealSize() const { size_t sz = t_.TotalSize(); - for (int field = 0; field < NumFields(); field++) + for (int field = 0; field < NumFields(); field++) { if (t_.Field(field).IsArray()) { - auto *arr = reinterpret_cast(Field(field).p_); - if (arr->offset >= sz) sz = arr->offset + arr->len * t_.Field(field).ElemSizeof(); + auto* arr = reinterpret_cast(Field(field).p_); + if (arr->offset >= sz) { + sz = arr->offset + arr->len * t_.Field(field).ElemSizeof(); + } } + } return sz; } // Serialize field values template -void PayloadIface::SerializeFields(WrSerializer &ser, const FieldsSet &fields) const { +void PayloadIface::SerializeFields(WrSerializer& ser, const FieldsSet& fields) const { size_t tagPathIdx = 0; VariantArray varr; for (int field : fields) { if (field == IndexValueType::SetByJsonPath) { assertrx(tagPathIdx < fields.getTagsPathsLength()); if (fields.isTagsPathIndexed(tagPathIdx)) { - const IndexedTagsPath &tagsPath = fields.getIndexedTagsPath(tagPathIdx); + const IndexedTagsPath& tagsPath = fields.getIndexedTagsPath(tagPathIdx); GetByJsonPath(tagsPath, varr, KeyValueType::Undefined{}); } else { - const TagsPath &tagsPath = fields.getTagsPath(tagPathIdx); + const TagsPath& tagsPath = fields.getTagsPath(tagPathIdx); GetByJsonPath(tagsPath, varr, KeyValueType::Undefined{}); } if (varr.empty()) { @@ -302,35 +310,43 @@ std::string PayloadIface::Dump() const { printString += Type().Field(i).Name(); printString += ": "; - if (Type().Field(i).IsArray()) printString += "["; + if (Type().Field(i).IsArray()) { + printString += "["; + } for (size_t j = 0; j < fieldValues.size(); ++j) { - auto &fieldValue = fieldValues[j]; + auto& fieldValue = fieldValues[j]; auto str = fieldValue.As(); - if (i != 0) + if (i != 0) { printString += str; - else { - for (size_t z = 0; z < str.length(); z++) printString += std::to_string(uint8_t(str[z])) + " "; + } else { + for (size_t z = 0; z < str.length(); z++) { + printString += std::to_string(uint8_t(str[z])) + " "; + } } if (j != fieldValues.size() - 1) { printString += ", "; } } - if (Type().Field(i).IsArray()) printString += "]"; - if (i != NumFields() - 1) printString += ", "; + if (Type().Field(i).IsArray()) { + printString += "]"; + } + if (i != NumFields() - 1) { + printString += ", "; + } } return printString; } template <> -void PayloadIface::GetJSON(const TagsMatcher &tm, WrSerializer &ser) { +void PayloadIface::GetJSON(const TagsMatcher& tm, WrSerializer& ser) { JsonBuilder b(ser); JsonEncoder e(&tm); e.Encode(*this, b); } template <> -std::string PayloadIface::GetJSON(const TagsMatcher &tm) { +std::string PayloadIface::GetJSON(const TagsMatcher& tm) { WrSerializer ser; GetJSON(tm, ser); return std::string(ser.Slice()); @@ -338,30 +354,31 @@ std::string PayloadIface::GetJSON(const TagsMatcher &tm) { // Get fields hash template -size_t PayloadIface::GetHash(const FieldsSet &fields) const { +size_t PayloadIface::GetHash(const FieldsSet& fields) const { size_t ret = 0; VariantArray keys1; size_t tagPathIdx = 0; for (auto field : fields) { ret *= 127; if (field != IndexValueType::SetByJsonPath) { - auto &f = t_.Field(field); + auto& f = t_.Field(field); if (f.IsArray()) { - auto *arr = reinterpret_cast(Field(field).p_); + auto* arr = reinterpret_cast(Field(field).p_); ret ^= arr->len; - uint8_t *p = v_->Ptr() + arr->offset; + uint8_t* p = v_->Ptr() + arr->offset; for (int i = 0; i < arr->len; i++, p += f.ElemSizeof()) { ret ^= PayloadFieldValue(f, p).Hash(); } - } else + } else { ret ^= Field(field).Hash(); + } } else { assertrx(tagPathIdx < fields.getTagsPathsLength()); if (fields.isTagsPathIndexed(tagPathIdx)) { - const IndexedTagsPath &tagsPath = fields.getIndexedTagsPath(tagPathIdx++); + const IndexedTagsPath& tagsPath = fields.getIndexedTagsPath(tagPathIdx++); GetByJsonPath(tagsPath, keys1, KeyValueType::Undefined{}); } else { - const TagsPath &tagsPath = fields.getTagsPath(tagPathIdx++); + const TagsPath& tagsPath = fields.getTagsPath(tagPathIdx++); GetByJsonPath(tagsPath, keys1, KeyValueType::Undefined{}); } ret ^= keys1.Hash(); @@ -377,49 +394,56 @@ uint64_t PayloadIface::GetHash() const noexcept { for (int field = 0; field < t_.NumFields(); field++) { ret <<= 1; - auto &f = t_.Field(field); + auto& f = t_.Field(field); if (f.IsArray()) { - auto *arr = reinterpret_cast(Field(field).p_); + auto* arr = reinterpret_cast(Field(field).p_); ret ^= arr->len; - uint8_t *p = v_->Ptr() + arr->offset; + uint8_t* p = v_->Ptr() + arr->offset; for (int i = 0; i < arr->len; i++, p += f.ElemSizeof()) { ret ^= PayloadFieldValue(f, p).Hash(); } - } else + } else { ret ^= Field(field).Hash(); + } } return ret; } template -bool PayloadIface::IsEQ(const T &other, const FieldsSet &fields) const { +bool PayloadIface::IsEQ(const T& other, const FieldsSet& fields) const { size_t tagPathIdx = 0; PayloadIface o(t_, other); VariantArray keys1, keys2; for (auto field : fields) { if (field != IndexValueType::SetByJsonPath) { - auto &f = t_.Field(field); + auto& f = t_.Field(field); if (f.IsArray()) { - auto *arr1 = reinterpret_cast(Field(field).p_); - auto *arr2 = reinterpret_cast(o.Field(field).p_); - if (arr1->len != arr2->len) return false; + auto* arr1 = reinterpret_cast(Field(field).p_); + auto* arr2 = reinterpret_cast(o.Field(field).p_); + if (arr1->len != arr2->len) { + return false; + } - uint8_t *p1 = v_->Ptr() + arr1->offset; - uint8_t *p2 = o.v_->Ptr() + arr2->offset; + uint8_t* p1 = v_->Ptr() + arr1->offset; + uint8_t* p2 = o.v_->Ptr() + arr2->offset; for (int i = 0; i < arr1->len; i++, p1 += f.ElemSizeof(), p2 += f.ElemSizeof()) { - if (!PayloadFieldValue(f, p1).IsEQ(PayloadFieldValue(f, p2))) return false; + if (!PayloadFieldValue(f, p1).IsEQ(PayloadFieldValue(f, p2))) { + return false; + } } } else { - if (!Field(field).IsEQ(o.Field(field))) return false; + if (!Field(field).IsEQ(o.Field(field))) { + return false; + } } } else { if (fields.isTagsPathIndexed(tagPathIdx)) { - const IndexedTagsPath &tagsPath = fields.getIndexedTagsPath(tagPathIdx++); + const IndexedTagsPath& tagsPath = fields.getIndexedTagsPath(tagPathIdx++); GetByJsonPath(tagsPath, keys1, KeyValueType::Undefined{}); o.GetByJsonPath(tagsPath, keys2, KeyValueType::Undefined{}); } else { - const TagsPath &tagsPath = fields.getTagsPath(tagPathIdx++); + const TagsPath& tagsPath = fields.getTagsPath(tagPathIdx++); GetByJsonPath(tagsPath, keys1, KeyValueType::Undefined{}); o.GetByJsonPath(tagsPath, keys2, KeyValueType::Undefined{}); } @@ -433,8 +457,8 @@ bool PayloadIface::IsEQ(const T &other, const FieldsSet &fields) const { template template -ComparationResult PayloadIface::CompareField(const T &other, int field, const FieldsSet &fields, size_t &tagPathIdx, - const CollateOpts &collateOpts) const { +ComparationResult PayloadIface::CompareField(const T& other, int field, const FieldsSet& fields, size_t& tagPathIdx, + const CollateOpts& collateOpts) const { VariantArray krefs1, krefs2; PayloadIface o(t_, other); @@ -446,11 +470,11 @@ ComparationResult PayloadIface::CompareField(const T &other, int field, const } else { assertrx(tagPathIdx < fields.getTagsPathsLength()); if (fields.isTagsPathIndexed(tagPathIdx)) { - const IndexedTagsPath &tagsPath = fields.getIndexedTagsPath(tagPathIdx++); + const IndexedTagsPath& tagsPath = fields.getIndexedTagsPath(tagPathIdx++); GetByJsonPath(tagsPath, krefs1, KeyValueType::Undefined{}); o.GetByJsonPath(tagsPath, krefs2, KeyValueType::Undefined{}); } else { - const TagsPath &tagsPath = fields.getTagsPath(tagPathIdx++); + const TagsPath& tagsPath = fields.getTagsPath(tagPathIdx++); GetByJsonPath(tagsPath, krefs1, KeyValueType::Undefined{}); o.GetByJsonPath(tagsPath, krefs2, KeyValueType::Undefined{}); } @@ -475,13 +499,13 @@ ComparationResult PayloadIface::CompareField(const T &other, int field, const template template -ComparationResult PayloadIface::Compare(const T &other, const FieldsSet &fields, size_t &firstDifferentFieldIdx, - const h_vector &collateOpts) const { +ComparationResult PayloadIface::Compare(const T& other, const FieldsSet& fields, size_t& firstDifferentFieldIdx, + const h_vector& collateOpts) const { size_t tagPathIdx = 0; const bool commonOpts = (collateOpts.size() == 1); for (size_t i = 0; i < fields.size(); ++i) { - const CollateOpts *opts(commonOpts ? collateOpts[0] : collateOpts[i]); + const CollateOpts* opts(commonOpts ? collateOpts[0] : collateOpts[i]); const auto cmpRes = CompareField(other, fields[i], fields, tagPathIdx, opts ? *opts : CollateOpts()); if (cmpRes != ComparationResult::Eq) { firstDifferentFieldIdx = i; @@ -493,96 +517,98 @@ ComparationResult PayloadIface::Compare(const T &other, const FieldsSet &fiel template template -ComparationResult PayloadIface::Compare(const T &other, const FieldsSet &fields, const CollateOpts &collateOpts) const { +ComparationResult PayloadIface::Compare(const T& other, const FieldsSet& fields, const CollateOpts& collateOpts) const { size_t firstDifferentFieldIdx = 0; return Compare(other, fields, firstDifferentFieldIdx, {&collateOpts}); } template void PayloadIface::AddRefStrings(int field) noexcept { - auto &f = t_.Field(field); + auto& f = t_.Field(field); assertrx(f.Type().template Is()); // direct payloadvalue manipulation for speed optimize if (!f.IsArray()) { - auto str = *reinterpret_cast((v_->Ptr() + f.Offset())); - key_string_add_ref(const_cast(str.getCxxstr())); + auto str = *reinterpret_cast((v_->Ptr() + f.Offset())); + key_string_add_ref(const_cast(str.getCxxstr())); } else { - auto arr = reinterpret_cast(v_->Ptr() + f.Offset()); + auto arr = reinterpret_cast(v_->Ptr() + f.Offset()); for (int i = 0; i < arr->len; i++) { - auto str = *reinterpret_cast(v_->Ptr() + arr->offset + i * t_.Field(field).ElemSizeof()); - key_string_add_ref(const_cast(str.getCxxstr())); + auto str = *reinterpret_cast(v_->Ptr() + arr->offset + i * t_.Field(field).ElemSizeof()); + key_string_add_ref(const_cast(str.getCxxstr())); } } } template void PayloadIface::AddRefStrings() noexcept { - for (auto field : t_.StrFields()) AddRefStrings(field); + for (auto field : t_.StrFields()) { + AddRefStrings(field); + } } template void PayloadIface::ReleaseStrings(int field) noexcept { - auto &f = t_.Field(field); + auto& f = t_.Field(field); assertrx(f.Type().template Is()); // direct payloadvalue manipulation for speed optimize if (!f.IsArray()) { - auto str = *reinterpret_cast((v_->Ptr() + f.Offset())); - key_string_release(const_cast(str.getCxxstr())); + auto str = *reinterpret_cast((v_->Ptr() + f.Offset())); + key_string_release(const_cast(str.getCxxstr())); } else { - auto arr = reinterpret_cast(v_->Ptr() + f.Offset()); + auto arr = reinterpret_cast(v_->Ptr() + f.Offset()); for (int i = 0; i < arr->len; i++) { - auto str = *reinterpret_cast(v_->Ptr() + arr->offset + i * t_.Field(field).ElemSizeof()); - key_string_release(const_cast(str.getCxxstr())); + auto str = *reinterpret_cast(v_->Ptr() + arr->offset + i * t_.Field(field).ElemSizeof()); + key_string_release(const_cast(str.getCxxstr())); } } } template template -void PayloadIface::copyOrMoveStrings(int field, StrHolder &dest, bool copy) { - auto &f = t_.Field(field); +void PayloadIface::copyOrMoveStrings(int field, StrHolder& dest, bool copy) { + auto& f = t_.Field(field); assertrx(f.Type().template Is()); // direct payloadvalue manipulation for speed optimize if (!f.IsArray()) { - auto str = *reinterpret_cast((v_->Ptr() + f.Offset())); - dest.emplace_back(reinterpret_cast(const_cast(str.getCxxstr())), copy); + auto str = *reinterpret_cast((v_->Ptr() + f.Offset())); + dest.emplace_back(reinterpret_cast(const_cast(str.getCxxstr())), copy); } else { - auto arr = reinterpret_cast(v_->Ptr() + f.Offset()); + auto arr = reinterpret_cast(v_->Ptr() + f.Offset()); for (int i = 0; i < arr->len; i++) { - auto str = *reinterpret_cast(v_->Ptr() + arr->offset + i * t_.Field(field).ElemSizeof()); - dest.emplace_back(reinterpret_cast(const_cast(str.getCxxstr())), copy); + auto str = *reinterpret_cast(v_->Ptr() + arr->offset + i * t_.Field(field).ElemSizeof()); + dest.emplace_back(reinterpret_cast(const_cast(str.getCxxstr())), copy); } } } template -template ::value>::type *> -void PayloadIface::setArray(int field, const VariantArray &keys, bool append) { +template ::value>::type*> +void PayloadIface::setArray(int field, const VariantArray& keys, bool append) { if (keys.IsNullValue()) { ResizeArray(field, 0, append); return; } int pos = ResizeArray(field, keys.size(), append); - auto const *const arr = reinterpret_cast(Field(field).p_); + const auto* const arr = reinterpret_cast(Field(field).p_); const auto elemSize = t_.Field(field).ElemSizeof(); - for (const Variant &kv : keys) { + for (const Variant& kv : keys) { PayloadFieldValue pv(t_.Field(field), v_->Ptr() + arr->offset + (pos++) * elemSize); pv.Set(kv); } } template -void PayloadIface::MoveStrings(int field, StringsHolder &dest) { +void PayloadIface::MoveStrings(int field, StringsHolder& dest) { copyOrMoveStrings(field, dest, false); } template -void PayloadIface::CopyStrings(std::vector &dest) { +void PayloadIface::CopyStrings(std::vector& dest) { for (auto field : t_.StrFields()) { copyOrMoveStrings(field, dest, true); } @@ -590,11 +616,13 @@ void PayloadIface::CopyStrings(std::vector &dest) { template void PayloadIface::ReleaseStrings() noexcept { - for (auto field : t_.StrFields()) ReleaseStrings(field); + for (auto field : t_.StrFields()) { + ReleaseStrings(field); + } } template -template ::value>::type *> +template ::value>::type*> T PayloadIface::CopyTo(PayloadType modifiedType, bool newOrUpdatedFields) { if (newOrUpdatedFields) { return CopyWithNewOrUpdatedFields(modifiedType); @@ -604,12 +632,12 @@ T PayloadIface::CopyTo(PayloadType modifiedType, bool newOrUpdatedFields) { } template -template ::value>::type *> +template ::value>::type*> T PayloadIface::CopyWithNewOrUpdatedFields(PayloadType modifiedType) { size_t totalGrow = 0; for (int idx = 1; idx < modifiedType.NumFields(); ++idx) { if (!t_.Contains(modifiedType.Field(idx).Name())) { - const PayloadFieldType &fieldType = modifiedType.Field(idx); + const PayloadFieldType& fieldType = modifiedType.Field(idx); totalGrow += fieldType.IsArray() ? sizeof(PayloadFieldValue::Array) : fieldType.Sizeof(); } else { if (modifiedType.Field(idx).IsArray() && !t_.Field(idx).IsArray()) { @@ -630,16 +658,16 @@ T PayloadIface::CopyWithNewOrUpdatedFields(PayloadType modifiedType) { } template -template ::value>::type *> +template ::value>::type*> T PayloadIface::CopyWithRemovedFields(PayloadType modifiedType) { size_t totalReduce = 0; std::vector fieldsLeft; for (int idx = 0; idx < t_.NumFields(); ++idx) { - const std::string &fieldname(t_.Field(idx).Name()); + const std::string& fieldname(t_.Field(idx).Name()); if (modifiedType.Contains(fieldname)) { fieldsLeft.emplace_back(fieldname); } else { - const PayloadFieldType &fieldType = t_.Field(idx); + const PayloadFieldType& fieldType = t_.Field(idx); totalReduce += fieldType.IsArray() ? sizeof(PayloadFieldValue::Array) : fieldType.Sizeof(); } } @@ -647,7 +675,7 @@ T PayloadIface::CopyWithRemovedFields(PayloadType modifiedType) { VariantArray kr; T pv(RealSize() - totalReduce); PayloadIface copyValueInterface(modifiedType, pv); - for (const auto &fieldname : fieldsLeft) { + for (const auto& fieldname : fieldsLeft) { Get(fieldname, kr); copyValueInterface.Set(fieldname, kr, false); } @@ -659,68 +687,64 @@ T PayloadIface::CopyWithRemovedFields(PayloadType modifiedType) { #pragma warning(disable : 5037) #endif -template void PayloadIface::Set(0)>(std::string_view, VariantArray const &, bool); -template void PayloadIface::Set(0)>(int, VariantArray const &, bool); -template void PayloadIface::Set(0)>(int, int, const Variant &); -template void PayloadIface::SetSingleElement(0)>(int, const Variant &); - -template PayloadValue PayloadIface::CopyTo(0)>(PayloadType t, bool newFields); -template PayloadValue PayloadIface::CopyWithNewOrUpdatedFields(0)>(PayloadType t); -template PayloadValue PayloadIface::CopyWithRemovedFields(0)>(PayloadType t); - -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; +template void PayloadIface::Set(0)>(std::string_view, const VariantArray&, bool); +template void PayloadIface::Set(0)>(int, const VariantArray&, bool); +template void PayloadIface::Set(0)>(int, int, const Variant&); +template void PayloadIface::SetSingleElement(0)>(int, const Variant&); + +template PayloadValue PayloadIface::CopyTo(0)>(PayloadType t, bool newFields); +template PayloadValue PayloadIface::CopyWithNewOrUpdatedFields(0)>(PayloadType t); +template PayloadValue PayloadIface::CopyWithRemovedFields(0)>(PayloadType t); + +template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; +template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; template ComparationResult PayloadIface::Compare( - const PayloadValue &, const FieldsSet &, size_t &, const h_vector &) const; - -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, - size_t &, - const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField( - const PayloadValue &, int, const FieldsSet &, size_t &, const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, size_t &, - const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, - size_t &, - const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, size_t &, - const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, size_t &, - const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, size_t &, - const CollateOpts &) const; -template ComparationResult PayloadIface::CompareField(const PayloadValue &, int, - const FieldsSet &, size_t &, - const CollateOpts &) const; + const PayloadValue&, const FieldsSet&, size_t&, const h_vector&) const; + +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; +template ComparationResult PayloadIface::CompareField(const PayloadValue&, int, + const FieldsSet&, size_t&, + const CollateOpts&) const; template class PayloadIface; template class PayloadIface; diff --git a/cpp_src/core/payload/payloadiface.h b/cpp_src/core/payload/payloadiface.h index c9fad0e4c..672f3be04 100644 --- a/cpp_src/core/payload/payloadiface.h +++ b/cpp_src/core/payload/payloadiface.h @@ -22,30 +22,30 @@ class PayloadIface { friend class PayloadIface; public: - PayloadIface(const PayloadType &t, T &v) noexcept : t_(*t.get()), v_(&v) {} - PayloadIface(const PayloadTypeImpl &t, T &v) noexcept : t_(t), v_(&v) {} + PayloadIface(const PayloadType& t, T& v) noexcept : t_(*t.get()), v_(&v) {} + PayloadIface(const PayloadTypeImpl& t, T& v) noexcept : t_(t), v_(&v) {} void Reset() noexcept { memset(v_->Ptr(), 0, t_.TotalSize()); } // Get element(s) by field index - void Get(int field, VariantArray &, Variant::hold_t) const; - void Get(int field, VariantArray &) const; + void Get(int field, VariantArray&, Variant::hold_t) const; + void Get(int field, VariantArray&) const; // Get element by field and array index [[nodiscard]] Variant Get(int field, int idx, Variant::hold_t) const; [[nodiscard]] Variant Get(int field, int idx) const; // Get array as span of typed elements template - span GetArray(int field) const & { + span GetArray(int field) const& { assertrx(field < Type().NumFields()); assertrx(Type().Field(field).IsArray()); - auto *arr = reinterpret_cast(Field(field).p_); - return span(reinterpret_cast(v_->Ptr() + arr->offset), arr->len); + auto* arr = reinterpret_cast(Field(field).p_); + return span(reinterpret_cast(v_->Ptr() + arr->offset), arr->len); } // Get array len int GetArrayLen(int field) const { assertrx(field < Type().NumFields()); assertrx(Type().Field(field).IsArray()); - auto *arr = reinterpret_cast(Field(field).p_); + auto* arr = reinterpret_cast(Field(field).p_); return arr->len; } @@ -55,24 +55,24 @@ class PayloadIface { int ResizeArray(int field, int grow, bool append); // Set element or array by field index - template ::value>::type * = nullptr> - void Set(int field, const VariantArray &keys, bool append = false) { + template ::value>::type* = nullptr> + void Set(int field, const VariantArray& keys, bool append = false) { if (!t_.Field(field).IsArray() && keys.size() >= 1) { Field(field).Set(keys[0]); } else { setArray(field, keys, append); } } - template ::value>::type * = nullptr> - void Set(int field, const Variant &key, bool append = false) { + template ::value>::type* = nullptr> + void Set(int field, const Variant& key, bool append = false) { if (t_.Field(field).IsArray()) { Set(field, VariantArray{key}, append); return; } Field(field).Set(key); } - template ::value>::type * = nullptr> - void Set(int field, Variant &&key, bool append = false) { + template ::value>::type* = nullptr> + void Set(int field, Variant&& key, bool append = false) { if (t_.Field(field).IsArray()) { Set(field, VariantArray{std::move(key)}, append); return; @@ -81,44 +81,44 @@ class PayloadIface { } // Set non-array element by field index - template ::value>::type * = nullptr> - void SetSingleElement(int field, const Variant &key); + template ::value>::type* = nullptr> + void SetSingleElement(int field, const Variant& key); // Set element or array by field index - template ::value>::type * = nullptr> - void Set(std::string_view field, const VariantArray &keys, bool append = false) { + template ::value>::type* = nullptr> + void Set(std::string_view field, const VariantArray& keys, bool append = false) { return Set(t_.FieldByName(field), keys, append); } - template ::value>::type * = nullptr> - void Set(std::string_view field, const Variant &key, bool append = false) { + template ::value>::type* = nullptr> + void Set(std::string_view field, const Variant& key, bool append = false) { return Set(t_.FieldByName(field), key, append); } - template ::value>::type * = nullptr> - void Set(std::string_view field, Variant &&key, bool append = false) { + template ::value>::type* = nullptr> + void Set(std::string_view field, Variant&& key, bool append = false) { return Set(t_.FieldByName(field), std::move(key), append); } // Set element or array by field index and element index - template ::value>::type * = nullptr> - void Set(int field, int idx, const Variant &v); + template ::value>::type* = nullptr> + void Set(int field, int idx, const Variant& v); // Copies current payload value to a new one // according to PayloadType format - template ::value>::type * = nullptr> + template ::value>::type* = nullptr> T CopyTo(PayloadType t, bool newFields = true); // Get element(s) by field name - void Get(std::string_view field, VariantArray &, Variant::hold_t) const; - void Get(std::string_view field, VariantArray &) const; + void Get(std::string_view field, VariantArray&, Variant::hold_t) const; + void Get(std::string_view field, VariantArray&) const; // Get element(s) by json path - void GetByJsonPath(std::string_view jsonPath, TagsMatcher &tagsMatcher, VariantArray &, KeyValueType expectedType) const; - void GetByJsonPath(const TagsPath &jsonPath, VariantArray &, KeyValueType expectedType) const; - void GetByJsonPath(const IndexedTagsPath &jsonPath, VariantArray &, KeyValueType expectedType) const; - void GetByFieldsSet(const FieldsSet &, VariantArray &, KeyValueType expectedType, - const h_vector &expectedCompositeTypes) const; - [[nodiscard]] Variant GetComposite(const FieldsSet &, const h_vector &expectedTypes) const; - VariantArray GetIndexedArrayData(const IndexedTagsPath &jsonPath, int field, int &offset, int &size) const; + void GetByJsonPath(std::string_view jsonPath, TagsMatcher& tagsMatcher, VariantArray&, KeyValueType expectedType) const; + void GetByJsonPath(const TagsPath& jsonPath, VariantArray&, KeyValueType expectedType) const; + void GetByJsonPath(const IndexedTagsPath& jsonPath, VariantArray&, KeyValueType expectedType) const; + void GetByFieldsSet(const FieldsSet&, VariantArray&, KeyValueType expectedType, + const h_vector& expectedCompositeTypes) const; + [[nodiscard]] Variant GetComposite(const FieldsSet&, const h_vector& expectedTypes) const; + VariantArray GetIndexedArrayData(const IndexedTagsPath& jsonPath, int field, int& offset, int& size) const; // Get fields count int NumFields() const noexcept { return t_.NumFields(); } @@ -126,30 +126,30 @@ class PayloadIface { // Real size of payload with arrays size_t RealSize() const; - inline const uint8_t *Ptr() const noexcept { return v_->Ptr(); } - const PayloadTypeImpl &Type() const noexcept { return t_; } - const T *Value() const noexcept { return v_; } + inline const uint8_t* Ptr() const noexcept { return v_->Ptr(); } + const PayloadTypeImpl& Type() const noexcept { return t_; } + const T* Value() const noexcept { return v_; } // Serialize field values - void SerializeFields(WrSerializer &ser, const FieldsSet &fields) const; + void SerializeFields(WrSerializer& ser, const FieldsSet& fields) const; // Get hash by fields mask - size_t GetHash(const FieldsSet &fields) const; + size_t GetHash(const FieldsSet& fields) const; // Compare is EQ by field mask - bool IsEQ(const T &other, const FieldsSet &fields) const; + bool IsEQ(const T& other, const FieldsSet& fields) const; // Get hash of all document uint64_t GetHash() const noexcept; // Compare single field (indexed or non-indexed) template - ComparationResult CompareField(const T &other, int field, const FieldsSet &fields, size_t &tagPathIdx, - const CollateOpts &collateOpts) const; + ComparationResult CompareField(const T& other, int field, const FieldsSet& fields, size_t& tagPathIdx, + const CollateOpts& collateOpts) const; // Compare 2 objects by field mask template - ComparationResult Compare(const T &other, const FieldsSet &fields, const CollateOpts &collateOpts = CollateOpts()) const; + ComparationResult Compare(const T& other, const FieldsSet& fields, const CollateOpts& collateOpts = CollateOpts()) const; template - ComparationResult Compare(const T &other, const FieldsSet &fields, size_t &firstDifferentFieldIdx, - const h_vector &collateOpts) const; + ComparationResult Compare(const T& other, const FieldsSet& fields, size_t& firstDifferentFieldIdx, + const h_vector& collateOpts) const; // Get PayloadFieldValue by field index PayloadFieldValue Field(int field) const noexcept { return PayloadFieldValue(t_.Field(field), v_->Ptr() + t_.Field(field).Offset()); } @@ -160,39 +160,39 @@ class PayloadIface { // Release strings void ReleaseStrings() noexcept; void ReleaseStrings(int field) noexcept; - void MoveStrings(int field, StringsHolder &dest); - void CopyStrings(std::vector &dest); + void MoveStrings(int field, StringsHolder& dest); + void CopyStrings(std::vector& dest); // Item values' string for printing std::string Dump() const; // Item as JSON - std::string GetJSON(const TagsMatcher &tm); - void GetJSON(const TagsMatcher &tm, WrSerializer &ser); + std::string GetJSON(const TagsMatcher& tm); + void GetJSON(const TagsMatcher& tm, WrSerializer& ser); private: enum class HoldPolicy : bool { Hold, NoHold }; - template ::value>::type * = nullptr> + template ::value>::type* = nullptr> T CopyWithNewOrUpdatedFields(PayloadType t); - template ::value>::type * = nullptr> + template ::value>::type* = nullptr> T CopyWithRemovedFields(PayloadType t); template - void copyOrMoveStrings(int field, StrHolder &dest, bool copy); + void copyOrMoveStrings(int field, StrHolder& dest, bool copy); template - void getByJsonPath(const P &path, VariantArray &, KeyValueType expectedType) const; - template ::value>::type * = nullptr> - void setArray(int field, const VariantArray &keys, bool append); + void getByJsonPath(const P& path, VariantArray&, KeyValueType expectedType) const; + template ::value>::type* = nullptr> + void setArray(int field, const VariantArray& keys, bool append); template - void get(int field, VariantArray &, HoldT h) const; + void get(int field, VariantArray&, HoldT h) const; template [[nodiscard]] Variant get(int field, int idx, HoldT h) const; template - void get(std::string_view field, VariantArray &, HoldT h) const; + void get(std::string_view field, VariantArray&, HoldT h) const; // Array of elements types , not owning - const PayloadTypeImpl &t_; + const PayloadTypeImpl& t_; // Data of elements, not owning - T *v_; + T* v_; }; template <> @@ -201,49 +201,50 @@ template <> int PayloadIface::ResizeArray(int, int, bool) = delete; template <> -void PayloadIface::GetJSON(const TagsMatcher &, WrSerializer &); +void PayloadIface::GetJSON(const TagsMatcher&, WrSerializer&); template <> -std::string PayloadIface::GetJSON(const TagsMatcher &); +std::string PayloadIface::GetJSON(const TagsMatcher&); template <> -void PayloadIface::GetJSON(const TagsMatcher &, WrSerializer &) = delete; +void PayloadIface::GetJSON(const TagsMatcher&, WrSerializer&) = delete; template <> -std::string PayloadIface::GetJSON(const TagsMatcher &) = delete; - -extern template void PayloadIface::Set(0)>(std::string_view, VariantArray const &, bool); -extern template void PayloadIface::Set(0)>(int, VariantArray const &, bool); -extern template void PayloadIface::Set(0)>(int, int, const Variant &); -extern template void PayloadIface::SetSingleElement(0)>(int, const Variant &); - -extern template PayloadValue PayloadIface::CopyTo(0)>(PayloadType t, bool newFields); -extern template PayloadValue PayloadIface::CopyWithNewOrUpdatedFields(0)>(PayloadType t); -extern template PayloadValue PayloadIface::CopyWithRemovedFields(0)>(PayloadType t); - -extern template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -extern template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; +std::string PayloadIface::GetJSON(const TagsMatcher&) = delete; + +extern template void PayloadIface::Set(0)>(std::string_view, const VariantArray&, bool); +extern template void PayloadIface::Set(0)>(int, const VariantArray&, bool); +extern template void PayloadIface::Set(0)>(int, int, const Variant&); +extern template void PayloadIface::SetSingleElement(0)>(int, const Variant&); + +extern template PayloadValue PayloadIface::CopyTo(0)>(PayloadType t, bool newFields); +extern template PayloadValue PayloadIface::CopyWithNewOrUpdatedFields(0)>(PayloadType t); +extern template PayloadValue PayloadIface::CopyWithRemovedFields(0)>(PayloadType t); + +extern template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; +extern template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; extern template ComparationResult PayloadIface::Compare( - const PayloadValue &, const FieldsSet &, const CollateOpts &) const; + const PayloadValue&, const FieldsSet&, const CollateOpts&) const; extern template ComparationResult PayloadIface::Compare( - const PayloadValue &, const FieldsSet &, const CollateOpts &) const; -extern template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; -extern template ComparationResult PayloadIface::Compare(const PayloadValue &, - const FieldsSet &, - const CollateOpts &) const; + const PayloadValue&, const FieldsSet&, const CollateOpts&) const; +extern template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; +extern template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; extern template ComparationResult PayloadIface::Compare( - const PayloadValue &, const FieldsSet &, const CollateOpts &) const; -extern template ComparationResult PayloadIface::Compare( - const PayloadValue &, const FieldsSet &, const CollateOpts &) const; + const PayloadValue&, const FieldsSet&, const CollateOpts&) const; +extern template ComparationResult PayloadIface::Compare(const PayloadValue&, + const FieldsSet&, + const CollateOpts&) const; extern template ComparationResult PayloadIface::Compare( - const PayloadValue &, const FieldsSet &, size_t &, const h_vector &) const; + const PayloadValue&, const FieldsSet&, size_t&, const h_vector&) const; extern template ComparationResult PayloadIface::CompareField( - const PayloadValue &, int, const FieldsSet &, size_t &, const CollateOpts &) const; + const PayloadValue&, int, const FieldsSet&, size_t&, const CollateOpts&) const; extern template class PayloadIface; extern template class PayloadIface; diff --git a/cpp_src/core/payload/payloadtype.cc b/cpp_src/core/payload/payloadtype.cc index 4aa4350f2..0f9bfd33d 100644 --- a/cpp_src/core/payload/payloadtype.cc +++ b/cpp_src/core/payload/payloadtype.cc @@ -15,24 +15,28 @@ size_t PayloadTypeImpl::TotalSize() const noexcept { std::string PayloadTypeImpl::ToString() const { std::stringstream ret; - for (auto &f : fields_) { + for (auto& f : fields_) { ret << f.Type().Name() << (f.IsArray() ? "[]" : "") << " '" << f.Name() << '\'' << " json:\""; - for (auto &jp : f.JsonPaths()) ret << jp << ";"; + for (auto& jp : f.JsonPaths()) { + ret << jp << ";"; + } ret << "\"\n"; } return ret.str(); } -void PayloadTypeImpl::Dump(std::ostream &os, std::string_view step, std::string_view offset) const { +void PayloadTypeImpl::Dump(std::ostream& os, std::string_view step, std::string_view offset) const { std::string newOffset{offset}; newOffset += step; os << '{'; - for (auto &f : fields_) { + for (auto& f : fields_) { os << '\n' << newOffset << f.Type().Name() << (f.IsArray() ? "[]" : "") << " '" << f.Name() << "'" << " json:\""; for (size_t i = 0, s = f.JsonPaths().size(); i < s; ++i) { - if (i != 0) os << ';'; + if (i != 0) { + os << ';'; + } os << f.JsonPaths()[i]; } os << '"'; @@ -47,14 +51,16 @@ void PayloadTypeImpl::Add(PayloadFieldType f) { auto it = fieldsByName_.find(f.Name()); if (it != fieldsByName_.end()) { // Non unique name -> check type, and upgrade to array if types are the same - auto &oldf = fields_[it->second]; + auto& oldf = fields_[it->second]; throw Error(errLogic, "Cannot add field with name '%s' and type '%s' to namespace '%s'. It already exists with type '%s'", f.Name(), f.Type().Name(), Name(), oldf.Type().Name()); } else { // Unique name -> just add field f.SetOffset(TotalSize()); - for (auto &jp : f.JsonPaths()) { - if (!jp.length()) continue; + for (auto& jp : f.JsonPaths()) { + if (!jp.length()) { + continue; + } auto res = fieldsByJsonPath_.emplace(jp, int(fields_.size())); if (!res.second && res.first->second != int(fields_.size())) { throw Error(errLogic, "Cannot add field with name '%s' to namespace '%s'. Json path '%s' already used in field '%s'", @@ -73,14 +79,20 @@ void PayloadTypeImpl::Add(PayloadFieldType f) { bool PayloadTypeImpl::Drop(std::string_view field) { auto itField = fieldsByName_.find(field); - if (itField == fieldsByName_.end()) return false; + if (itField == fieldsByName_.end()) { + return false; + } const auto fieldIdx = itField->second; - for (auto &f : fieldsByName_) { - if (f.second > fieldIdx) --f.second; + for (auto& f : fieldsByName_) { + if (f.second > fieldIdx) { + --f.second; + } } - for (auto &f : fieldsByJsonPath_) { - if (f.second > fieldIdx) --f.second; + for (auto& f : fieldsByJsonPath_) { + if (f.second > fieldIdx) { + --f.second; + } } const auto fieldType = fields_[fieldIdx].Type(); @@ -94,7 +106,7 @@ bool PayloadTypeImpl::Drop(std::string_view field) { ++it; } - for (auto &jp : fields_[fieldIdx].JsonPaths()) { + for (auto& jp : fields_[fieldIdx].JsonPaths()) { fieldsByJsonPath_.erase(jp); } @@ -103,7 +115,7 @@ bool PayloadTypeImpl::Drop(std::string_view field) { if (idx == 0) { fields_[idx].SetOffset(0); } else { - const PayloadFieldType &plTypePrev(fields_[idx - 1]); + const PayloadFieldType& plTypePrev(fields_[idx - 1]); fields_[idx].SetOffset(plTypePrev.Offset() + plTypePrev.Sizeof()); } } @@ -115,24 +127,30 @@ bool PayloadTypeImpl::Drop(std::string_view field) { int PayloadTypeImpl::FieldByName(std::string_view field) const { auto it = fieldsByName_.find(field); - if (it == fieldsByName_.end()) throw Error(errLogic, "Field '%s' not found in namespace '%s'", field, Name()); + if (it == fieldsByName_.end()) { + throw Error(errLogic, "Field '%s' not found in namespace '%s'", field, Name()); + } return it->second; } -bool PayloadTypeImpl::FieldByName(std::string_view name, int &field) const noexcept { +bool PayloadTypeImpl::FieldByName(std::string_view name, int& field) const noexcept { auto it = fieldsByName_.find(name); - if (it == fieldsByName_.end()) return false; + if (it == fieldsByName_.end()) { + return false; + } field = it->second; return true; } int PayloadTypeImpl::FieldByJsonPath(std::string_view jsonPath) const noexcept { auto it = fieldsByJsonPath_.find(jsonPath); - if (it == fieldsByJsonPath_.end()) return -1; + if (it == fieldsByJsonPath_.end()) { + return -1; + } return it->second; } -void PayloadTypeImpl::serialize(WrSerializer &ser) const { +void PayloadTypeImpl::serialize(WrSerializer& ser) const { ser.PutVarUint(base_key_string::export_hdr_offset()); ser.PutVarUint(NumFields()); for (int i = 0; i < NumFields(); i++) { @@ -142,11 +160,13 @@ void PayloadTypeImpl::serialize(WrSerializer &ser) const { ser.PutVarUint(Field(i).ElemSizeof()); ser.PutVarUint(Field(i).IsArray()); ser.PutVarUint(Field(i).JsonPaths().size()); - for (auto &jp : Field(i).JsonPaths()) ser.PutVString(jp); + for (auto& jp : Field(i).JsonPaths()) { + ser.PutVString(jp); + } } } -void PayloadTypeImpl::deserialize(Serializer &ser) { +void PayloadTypeImpl::deserialize(Serializer& ser) { fields_.clear(); fieldsByName_.clear(); fieldsByJsonPath_.clear(); @@ -165,40 +185,46 @@ void PayloadTypeImpl::deserialize(Serializer &ser) { bool isArray = ser.GetVarUint(); uint64_t jsonPathsCount = ser.GetVarUint(); - while (jsonPathsCount--) jsonPaths.push_back(std::string(ser.GetVString())); + while (jsonPathsCount--) { + jsonPaths.push_back(std::string(ser.GetVString())); + } (void)elemSizeof; PayloadFieldType ft(t, name, jsonPaths, isArray); - if (isArray) ft.SetArray(); + if (isArray) { + ft.SetArray(); + } ft.SetOffset(offset); fieldsByName_.emplace(name, fields_.size()); - if (t.Is()) strFields_.push_back(fields_.size()); + if (t.Is()) { + strFields_.push_back(fields_.size()); + } fields_.push_back(ft); } } -PayloadType::PayloadType(const std::string &name, std::initializer_list fields) +PayloadType::PayloadType(const std::string& name, std::initializer_list fields) : shared_cow_ptr(make_intrusive>(name, fields)) {} -PayloadType::PayloadType(const PayloadTypeImpl &impl) +PayloadType::PayloadType(const PayloadTypeImpl& impl) : shared_cow_ptr(make_intrusive>(impl)) {} PayloadType::~PayloadType() = default; -const PayloadFieldType &PayloadType::Field(int field) const { return get()->Field(field); } -const std::string &PayloadType::Name() const { return get()->Name(); } -void PayloadType::SetName(const std::string &name) { clone()->SetName(name); } +const PayloadFieldType& PayloadType::Field(int field) const { return get()->Field(field); } +const std::string& PayloadType::Name() const { return get()->Name(); } +void PayloadType::SetName(const std::string& name) { clone()->SetName(name); } int PayloadType::NumFields() const { return get()->NumFields(); } void PayloadType::Add(PayloadFieldType f) { clone()->Add(std::move(f)); } bool PayloadType::Drop(std::string_view field) { return clone()->Drop(field); } int PayloadType::FieldByName(std::string_view field) const { return get()->FieldByName(field); } -bool PayloadType::FieldByName(std::string_view name, int &field) const { return get()->FieldByName(name, field); } +bool PayloadType::FieldByName(std::string_view name, int& field) const { return get()->FieldByName(name, field); } bool PayloadType::Contains(std::string_view field) const { return get()->Contains(field); } int PayloadType::FieldByJsonPath(std::string_view jsonPath) const { return get()->FieldByJsonPath(jsonPath); } -const std::vector &PayloadType::StrFields() const { return get()->StrFields(); } +const std::vector& PayloadType::StrFields() const { return get()->StrFields(); } size_t PayloadType::TotalSize() const { return get()->TotalSize(); } std::string PayloadType::ToString() const { return get()->ToString(); } -void PayloadType::Dump(std::ostream &os, std::string_view step, std::string_view offset) const { +void PayloadType::Dump(std::ostream& os, std::string_view step, std::string_view offset) const { std::string newOffset{offset}; newOffset += step; os << "{\n" << newOffset << "name: " << Name() << ",\n" << newOffset; @@ -206,11 +232,11 @@ void PayloadType::Dump(std::ostream &os, std::string_view step, std::string_view os << '\n' << offset << '}'; } -void PayloadTypeImpl::checkNewJsonPathBeforeAdd(const PayloadFieldType &f, const std::string &jsonPath) const { +void PayloadTypeImpl::checkNewJsonPathBeforeAdd(const PayloadFieldType& f, const std::string& jsonPath) const { const auto pos = jsonPath.find('.'); if (pos < jsonPath.length() - 1) { - for (auto &fld : fields_) { - for (auto &jpfld : fld.JsonPaths()) { + for (auto& fld : fields_) { + for (auto& jpfld : fld.JsonPaths()) { // new field total overwrites existing one if ((jsonPath.rfind(jpfld, 0) == 0) && (jsonPath[jpfld.length()] == '.')) { throw Error(errLogic, diff --git a/cpp_src/core/payload/payloadtype.h b/cpp_src/core/payload/payloadtype.h index 07e512eb3..dd8292d6e 100644 --- a/cpp_src/core/payload/payloadtype.h +++ b/cpp_src/core/payload/payloadtype.h @@ -13,28 +13,28 @@ class PayloadTypeImpl; class PayloadType : public shared_cow_ptr { public: PayloadType() noexcept = default; - PayloadType(PayloadType &&) noexcept = default; - PayloadType(const PayloadType &) = default; - PayloadType &operator=(PayloadType &&) noexcept = default; - PayloadType &operator=(const PayloadType &) = default; - explicit PayloadType(const std::string &name, std::initializer_list fields = {}); - explicit PayloadType(const PayloadTypeImpl &impl); + PayloadType(PayloadType&&) noexcept = default; + PayloadType(const PayloadType&) = default; + PayloadType& operator=(PayloadType&&) noexcept = default; + PayloadType& operator=(const PayloadType&) = default; + explicit PayloadType(const std::string& name, std::initializer_list fields = {}); + explicit PayloadType(const PayloadTypeImpl& impl); ~PayloadType(); - const PayloadFieldType &Field(int field) const; + const PayloadFieldType& Field(int field) const; - const std::string &Name() const; - void SetName(const std::string &name); + const std::string& Name() const; + void SetName(const std::string& name); int NumFields() const; void Add(PayloadFieldType); bool Drop(std::string_view field); int FieldByName(std::string_view field) const; - bool FieldByName(std::string_view name, int &field) const; + bool FieldByName(std::string_view name, int& field) const; bool Contains(std::string_view field) const; int FieldByJsonPath(std::string_view jsonPath) const; - const std::vector &StrFields() const; + const std::vector& StrFields() const; size_t TotalSize() const; std::string ToString() const; - void Dump(std::ostream &, std::string_view step = " ", std::string_view offset = "") const; + void Dump(std::ostream&, std::string_view step = " ", std::string_view offset = "") const; }; } // namespace reindexer diff --git a/cpp_src/core/payload/payloadtypeimpl.h b/cpp_src/core/payload/payloadtypeimpl.h index 64db9568c..6347a3a49 100644 --- a/cpp_src/core/payload/payloadtypeimpl.h +++ b/cpp_src/core/payload/payloadtypeimpl.h @@ -20,34 +20,34 @@ class PayloadTypeImpl { public: PayloadTypeImpl(std::string name, std::initializer_list fields = {}) : fields_(fields), name_(std::move(name)) {} - const PayloadFieldType &Field(int field) const &noexcept { + const PayloadFieldType& Field(int field) const& noexcept { assertf(field < NumFields(), "%s: %d, %d", name_, field, NumFields()); return fields_[field]; } - const PayloadFieldType &Field(int) const && = delete; + const PayloadFieldType& Field(int) const&& = delete; - const std::string &Name() const &noexcept { return name_; } - const std::string &Name() const && = delete; + const std::string& Name() const& noexcept { return name_; } + const std::string& Name() const&& = delete; void SetName(std::string name) noexcept { name_ = std::move(name); } int NumFields() const noexcept { return fields_.size(); } void Add(PayloadFieldType f); bool Drop(std::string_view field); int FieldByName(std::string_view field) const; - bool FieldByName(std::string_view name, int &field) const noexcept; + bool FieldByName(std::string_view name, int& field) const noexcept; bool Contains(std::string_view field) const noexcept { return fieldsByName_.find(field) != fieldsByName_.end(); } int FieldByJsonPath(std::string_view jsonPath) const noexcept; - const std::vector &StrFields() const &noexcept { return strFields_; } - const std::vector &StrFields() const && = delete; + const std::vector& StrFields() const& noexcept { return strFields_; } + const std::vector& StrFields() const&& = delete; - void serialize(WrSerializer &ser) const; - void deserialize(Serializer &ser); + void serialize(WrSerializer& ser) const; + void deserialize(Serializer& ser); size_t TotalSize() const noexcept; std::string ToString() const; - void Dump(std::ostream &, std::string_view step, std::string_view offset) const; + void Dump(std::ostream&, std::string_view step, std::string_view offset) const; private: - void checkNewJsonPathBeforeAdd(const PayloadFieldType &f, const std::string &jsonPath) const; + void checkNewJsonPathBeforeAdd(const PayloadFieldType& f, const std::string& jsonPath) const; std::vector fields_; FieldMap fieldsByName_; diff --git a/cpp_src/core/payload/payloadvalue.cc b/cpp_src/core/payload/payloadvalue.cc index d1b07a365..692f2fa64 100644 --- a/cpp_src/core/payload/payloadvalue.cc +++ b/cpp_src/core/payload/payloadvalue.cc @@ -4,7 +4,7 @@ namespace reindexer { -PayloadValue::PayloadValue(size_t size, const uint8_t *ptr, size_t cap) : p_(nullptr) { +PayloadValue::PayloadValue(size_t size, const uint8_t* ptr, size_t cap) : p_(nullptr) { p_ = alloc((cap != 0) ? cap : size); if (ptr) { @@ -14,15 +14,16 @@ PayloadValue::PayloadValue(size_t size, const uint8_t *ptr, size_t cap) : p_(nul } } -uint8_t *PayloadValue::alloc(size_t cap) { - auto pn = reinterpret_cast(operator new(cap + sizeof(dataHeader))); - dataHeader *nheader = reinterpret_cast(pn); +uint8_t* PayloadValue::alloc(size_t cap) { + auto pn = reinterpret_cast(operator new(cap + sizeof(dataHeader))); + dataHeader* nheader = reinterpret_cast(pn); new (nheader) dataHeader(); nheader->cap = cap; if (p_) { nheader->lsn = header()->lsn; - } else + } else { nheader->lsn = -1; + } return pn; } @@ -58,7 +59,9 @@ void PayloadValue::Resize(size_t oldSize, size_t newSize) { assertrx(p_); assertrx(header()->refcount.load(std::memory_order_acquire) == 1); - if (newSize <= header()->cap) return; + if (newSize <= header()->cap) { + return; + } auto pn = alloc(newSize); memcpy(pn + sizeof(dataHeader), Ptr(), oldSize); @@ -69,21 +72,23 @@ void PayloadValue::Resize(size_t oldSize, size_t newSize) { p_ = pn; } -std::ostream &operator<<(std::ostream &os, const PayloadValue &pv) { - os << "{p_: " << std::hex << static_cast(pv.p_) << std::dec; +std::ostream& operator<<(std::ostream& os, const PayloadValue& pv) { + os << "{p_: " << std::hex << static_cast(pv.p_) << std::dec; if (pv.p_) { - const auto *header = pv.header(); + const auto* header = pv.header(); os << ", refcount: " << header->refcount.load(std::memory_order_relaxed) << ", cap: " << header->cap << ", lsn: " << header->lsn << ", [" << std::hex; - const uint8_t *ptr = pv.Ptr(); + const uint8_t* ptr = pv.Ptr(); const size_t cap = header->cap; for (size_t i = 0; i < cap; ++i) { - if (i != 0) os << ' '; + if (i != 0) { + os << ' '; + } os << static_cast(ptr[i]); } os << std::dec << "], tuple: "; assertrx(cap >= sizeof(p_string)); - const p_string &str = *reinterpret_cast(ptr); + const p_string& str = *reinterpret_cast(ptr); str.Dump(os); } return os << '}'; diff --git a/cpp_src/core/payload/payloadvalue.h b/cpp_src/core/payload/payloadvalue.h index 87772037b..33a0a59d0 100644 --- a/cpp_src/core/payload/payloadvalue.h +++ b/cpp_src/core/payload/payloadvalue.h @@ -21,24 +21,26 @@ class PayloadValue { }; PayloadValue() noexcept : p_(nullptr) {} - PayloadValue(const PayloadValue &other) noexcept : p_(other.p_) { + PayloadValue(const PayloadValue& other) noexcept : p_(other.p_) { if (p_) { header()->refcount.fetch_add(1, std::memory_order_relaxed); } } // Alloc payload store with size, and copy data from another array - PayloadValue(size_t size, const uint8_t *ptr = nullptr, size_t cap = 0); + PayloadValue(size_t size, const uint8_t* ptr = nullptr, size_t cap = 0); ~PayloadValue() { release(); } - PayloadValue &operator=(const PayloadValue &other) noexcept { + PayloadValue& operator=(const PayloadValue& other) noexcept { if (&other != this) { release(); p_ = other.p_; - if (p_) header()->refcount.fetch_add(1, std::memory_order_relaxed); + if (p_) { + header()->refcount.fetch_add(1, std::memory_order_relaxed); + } } return *this; } - PayloadValue(PayloadValue &&other) noexcept : p_(other.p_) { other.p_ = nullptr; } - PayloadValue &operator=(PayloadValue &&other) noexcept { + PayloadValue(PayloadValue&& other) noexcept : p_(other.p_) { other.p_ = nullptr; } + PayloadValue& operator=(PayloadValue&& other) noexcept { if (&other != this) { release(); p_ = other.p_; @@ -53,23 +55,23 @@ class PayloadValue { // Resize void Resize(size_t oldSize, size_t newSize); // Get data pointer - uint8_t *Ptr() const noexcept { return p_ + sizeof(dataHeader); } + uint8_t* Ptr() const noexcept { return p_ + sizeof(dataHeader); } void SetLSN(int64_t lsn) noexcept { header()->lsn = lsn; } int64_t GetLSN() const noexcept { return p_ ? header()->lsn : 0; } bool IsFree() const noexcept { return bool(p_ == nullptr); } void Free() noexcept { release(); } size_t GetCapacity() const noexcept { return header()->cap; } - const uint8_t *get() const noexcept { return p_; } + const uint8_t* get() const noexcept { return p_; } protected: - uint8_t *alloc(size_t cap); + uint8_t* alloc(size_t cap); void release() noexcept; - dataHeader *header() noexcept { return reinterpret_cast(p_); } - const dataHeader *header() const noexcept { return reinterpret_cast(p_); } - friend std::ostream &operator<<(std::ostream &os, const PayloadValue &); + dataHeader* header() noexcept { return reinterpret_cast(p_); } + const dataHeader* header() const noexcept { return reinterpret_cast(p_); } + friend std::ostream& operator<<(std::ostream& os, const PayloadValue&); // Data of elements, shared - uint8_t *p_; + uint8_t* p_; }; } // namespace reindexer diff --git a/cpp_src/core/perfstatcounter.cc b/cpp_src/core/perfstatcounter.cc index 41b58afc9..56547fea5 100644 --- a/cpp_src/core/perfstatcounter.cc +++ b/cpp_src/core/perfstatcounter.cc @@ -80,7 +80,9 @@ template void PerfStatCounter::lap() { auto now = system_clock_w::now(); std::chrono::microseconds elapsed = std::chrono::duration_cast(now - calcStartTime); - if (elapsed < std::chrono::microseconds(1000000)) return; + if (elapsed < std::chrono::microseconds(1000000)) { + return; + } avgHitCount = calcHitCount; avgTime = calcTime; calcTime = std::chrono::microseconds(0); diff --git a/cpp_src/core/perfstatcounter.h b/cpp_src/core/perfstatcounter.h index b26de1b47..d8fbfce37 100644 --- a/cpp_src/core/perfstatcounter.h +++ b/cpp_src/core/perfstatcounter.h @@ -64,15 +64,21 @@ using PerfStatCounterST = PerfStatCounter; template class PerfStatCalculator { public: - PerfStatCalculator(PerfStatCounter &counter, bool enable) noexcept : counter_(&counter), enable_(enable) { - if (enable_) tmStart = system_clock_w::now(); + PerfStatCalculator(PerfStatCounter& counter, bool enable) noexcept : counter_(&counter), enable_(enable) { + if (enable_) { + tmStart = system_clock_w::now(); + } } ~PerfStatCalculator() { - if (enable_) counter_->Hit(std::chrono::duration_cast(system_clock_w::now() - tmStart)); + if (enable_) { + counter_->Hit(std::chrono::duration_cast(system_clock_w::now() - tmStart)); + } } - void SetCounter(PerfStatCounter &counter) { counter_ = &counter; } + void SetCounter(PerfStatCounter& counter) { counter_ = &counter; } void LockHit() { - if (enable_) counter_->LockHit(std::chrono::duration_cast(system_clock_w::now() - tmStart)); + if (enable_) { + counter_->LockHit(std::chrono::duration_cast(system_clock_w::now() - tmStart)); + } } void HitManualy() { if (enable_) { @@ -82,7 +88,7 @@ class PerfStatCalculator { } system_clock_w::time_point tmStart; - PerfStatCounter *counter_; + PerfStatCounter* counter_; bool enable_; }; using PerfStatCalculatorMT = PerfStatCalculator; diff --git a/cpp_src/core/query/dsl/dslencoder.cc b/cpp_src/core/query/dsl/dslencoder.cc index 65662a9d4..82290d4ee 100644 --- a/cpp_src/core/query/dsl/dslencoder.cc +++ b/cpp_src/core/query/dsl/dslencoder.cc @@ -37,9 +37,11 @@ constexpr static auto kReqTotalValues = frozen::make_unordered_map -std::string_view get(frozen::unordered_map const& m, const T& key) { +std::string_view get(const frozen::unordered_map& m, const T& key) { auto it = m.find(key); - if (it != m.end()) return it->second; + if (it != m.end()) { + return it->second; + } assertrx(it != m.end()); return std::string_view(); } @@ -64,13 +66,17 @@ static void encodeJoins(const Query& query, JsonBuilder& builder) { } static void encodeEqualPositions(const EqualPositions_t& equalPositions, JsonBuilder& builder) { - if (equalPositions.empty()) return; + if (equalPositions.empty()) { + return; + } auto node = builder.Object(); auto epNodePositions = node.Array("equal_positions"); for (const auto& eqPos : equalPositions) { auto epNodePosition = epNodePositions.Object(std::string_view()); auto epNodePositionArr = epNodePosition.Array("positions"); - for (const auto& field : eqPos) epNodePositionArr.Put(nullptr, field); + for (const auto& field : eqPos) { + epNodePositionArr.Put(nullptr, field); + } } } @@ -94,12 +100,16 @@ static void encodeMergedQueries(const Query& query, JsonBuilder& builder) { static void encodeSelectFilter(const Query& query, JsonBuilder& builder) { auto arrNode = builder.Array("select_filter"); - for (auto& str : query.SelectFilters()) arrNode.Put(nullptr, str); + for (auto& str : query.SelectFilters()) { + arrNode.Put(nullptr, str); + } } static void encodeSelectFunctions(const Query& query, JsonBuilder& builder) { auto arrNode = builder.Array("select_functions"); - for (auto& str : query.selectFunctions_) arrNode.Put(nullptr, str); + for (auto& str : query.selectFunctions_) { + arrNode.Put(nullptr, str); + } } static void encodeAggregationFunctions(const Query& query, JsonBuilder& builder) { @@ -123,8 +133,12 @@ static void encodeAggregationFunctions(const Query& query, JsonBuilder& builder) break; } - if (entry.Limit() != QueryEntry::kDefaultLimit) aggNode.Put("limit", entry.Limit()); - if (entry.Offset() != QueryEntry::kDefaultOffset) aggNode.Put("offset", entry.Offset()); + if (entry.Limit() != QueryEntry::kDefaultLimit) { + aggNode.Put("limit", entry.Limit()); + } + if (entry.Offset() != QueryEntry::kDefaultOffset) { + aggNode.Put("offset", entry.Offset()); + } auto fldNode = aggNode.Array("fields"); for (const auto& field : entry.Fields()) { fldNode.Put(nullptr, field); @@ -179,7 +193,9 @@ static void putValues(JsonBuilder& builder, const VariantArray& values) { } static void encodeFilter(const QueryEntry& qentry, JsonBuilder& builder) { - if (qentry.Distinct()) return; + if (qentry.Distinct()) { + return; + } builder.Put("cond", get(kCondMap, CondType(qentry.Condition()))); builder.Put("field", qentry.FieldName()); putValues(builder, qentry.Values()); @@ -258,10 +274,14 @@ void toDsl(const Query& query, QueryScope scope, JsonBuilder& builder) { bool withDropEntries = false, withUpdateEntries = false; for (const UpdateEntry& updateEntry : query.UpdateFields()) { if (updateEntry.Mode() == FieldModeDrop) { - if (!withDropEntries) withDropEntries = true; + if (!withDropEntries) { + withDropEntries = true; + } } if (updateEntry.Mode() == FieldModeSet || updateEntry.Mode() == FieldModeSetJson) { - if (!withUpdateEntries) withUpdateEntries = true; + if (!withUpdateEntries) { + withUpdateEntries = true; + } } } if (withDropEntries) { @@ -333,7 +353,9 @@ void QueryEntries::toDsl(const_iterator it, const_iterator to, const Query& pare dsl::encodeEqualPositions(bracket.equalPositions, arrNode); }, [&node](const QueryEntry& qe) { - if (qe.Distinct()) return; + if (qe.Distinct()) { + return; + } dsl::encodeFilter(qe, node); }, [&node, &parentQuery](const JoinQueryEntry& jqe) { diff --git a/cpp_src/core/query/dsl/dslparser.cc b/cpp_src/core/query/dsl/dslparser.cc index 18aa54a39..05a0b7ce4 100644 --- a/cpp_src/core/query/dsl/dslparser.cc +++ b/cpp_src/core/query/dsl/dslparser.cc @@ -45,7 +45,7 @@ enum class UpdateField { Name, Type, Values, IsArray }; enum class UpdateFieldType { Object, Expression, Value }; template -constexpr auto MakeFastStrMap(std::pair const (&items)[N]) { +constexpr auto MakeFastStrMap(const std::pair (&items)[N]) { return frozen::make_unordered_map(items, frozen::nocase_hash_str{}, frozen::nocase_equal_str{}); } @@ -177,11 +177,13 @@ bool checkTag(const JsonValue& val, JsonTag tag, Tags... tags) noexcept { template void checkJsonValueType(const JsonValue& val, std::string_view name, JsonTags... possibleTags) { - if (!checkTag(val, possibleTags...)) throw Error(errParseJson, "Wrong type of field '%s'", name); + if (!checkTag(val, possibleTags...)) { + throw Error(errParseJson, "Wrong type of field '%s'", name); + } } template -T get(frozen::unordered_map const& m, std::string_view name, +T get(const frozen::unordered_map& m, std::string_view name, std::string_view mapName) { auto it = m.find(name); if (it == m.end()) { @@ -235,7 +237,9 @@ static void parseSortEntry(const JsonValue& entry, SortingEntries& sortingEntrie std::string_view name = subelement.key; switch (get(kSortMap, name, "sort"sv)) { case Sort::Desc: - if ((v.getTag() != JSON_TRUE) && (v.getTag() != JSON_FALSE)) throw Error(errParseJson, "Wrong type of field '%s'", name); + if ((v.getTag() != JSON_TRUE) && (v.getTag() != JSON_FALSE)) { + throw Error(errParseJson, "Wrong type of field '%s'", name); + } sortingEntry.desc = (v.getTag() == JSON_TRUE); break; @@ -259,7 +263,9 @@ static void parseSortEntry(const JsonValue& entry, SortingEntries& sortingEntrie static void parseSort(const JsonValue& v, SortingEntries& sortingEntries, std::vector& forcedSortOrder) { if (v.getTag() == JSON_ARRAY) { - for (auto entry : v) parseSort(entry.value, sortingEntries, forcedSortOrder); + for (auto entry : v) { + parseSort(entry.value, sortingEntries, forcedSortOrder); + } } else if (v.getTag() == JSON_OBJECT) { parseSortEntry(v, sortingEntries, forcedSortOrder); } else { @@ -326,7 +332,9 @@ static void parseFilter(const JsonValue& filter, Query& q, std::vector())) + if (isExpression && (values.size() != 1 || !values.front().Type().template Is())) { throw Error(errParseDSL, R"(The array "values" must contain only a string type value for the type "expression")"); + } if (isObject) { query.SetObject(fieldName, std::move(values)); @@ -645,7 +660,9 @@ void parse(const JsonValue& root, Query& q) { case Root::Filters: checkJsonValueType(v, name, JSON_ARRAY); - for (const auto& filter : v) parseFilter(filter.value, q, equalPositions, 0); + for (const auto& filter : v) { + parseFilter(filter.value, q, equalPositions, 0); + } break; case Root::Sort: @@ -675,7 +692,9 @@ void parse(const JsonValue& root, Query& q) { break; case Root::Aggregations: checkJsonValueType(v, name, JSON_ARRAY); - for (const auto& aggregation : v) parseAggregation(aggregation.value, q); + for (const auto& aggregation : v) { + parseAggregation(aggregation.value, q); + } break; case Root::Explain: checkJsonValueType(v, name, JSON_FALSE, JSON_TRUE); @@ -683,7 +702,9 @@ void parse(const JsonValue& root, Query& q) { break; case Root::WithRank: checkJsonValueType(v, name, JSON_FALSE, JSON_TRUE); - if (v.getTag() == JSON_TRUE) q.WithRank(); + if (v.getTag() == JSON_TRUE) { + q.WithRank(); + } break; case Root::StrictMode: checkJsonValueType(v, name, JSON_STRING); @@ -729,7 +750,9 @@ Error Parse(std::string_view str, Query& q) { gason::JsonParser parser; auto root = parser.Parse(str); Error err = schemaChecker.Check(root); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } dsl::parse(root.value, q); } catch (const gason::Exception& ex) { return Error(errParseJson, "Query: %s", ex.what()); diff --git a/cpp_src/core/query/expressionevaluator.cc b/cpp_src/core/query/expressionevaluator.cc index a4b8d3a77..2d43a4edc 100644 --- a/cpp_src/core/query/expressionevaluator.cc +++ b/cpp_src/core/query/expressionevaluator.cc @@ -320,7 +320,9 @@ double ExpressionEvaluator::performMultiplicationAndDivision(tokenizer& parser, // tok.text() == "/"sv parser.next_token(tokenizer::flags::treat_sign_as_token); const double val = performMultiplicationAndDivision(parser, v, tok); - if (val == 0) throw Error(errLogic, "Division by zero!"); + if (val == 0) { + throw Error(errLogic, "Division by zero!"); + } left /= val; } } diff --git a/cpp_src/core/query/query.cc b/cpp_src/core/query/query.cc index 00a0297fd..90f61590a 100644 --- a/cpp_src/core/query/query.cc +++ b/cpp_src/core/query/query.cc @@ -93,7 +93,7 @@ void Query::VerifyForUpdate() const { } } -bool Query::operator==(const Query &obj) const { +bool Query::operator==(const Query& obj) const { if (entries_ != obj.entries_ || aggregations_ != obj.aggregations_ || NsName() != obj.NsName() || sortingEntries_ != obj.sortingEntries_ || CalcTotal() != obj.CalcTotal() || Offset() != obj.Offset() || @@ -110,9 +110,13 @@ bool Query::operator==(const Query &obj) const { return true; } -bool JoinedQuery::operator==(const JoinedQuery &obj) const { - if (joinEntries_ != obj.joinEntries_) return false; - if (joinType != obj.joinType) return false; +bool JoinedQuery::operator==(const JoinedQuery& obj) const { + if (joinEntries_ != obj.joinEntries_) { + return false; + } + if (joinType != obj.joinType) { + return false; + } return Query::operator==(obj); } Query Query::FromSQL(std::string_view q) { return SQLParser::Parse(q); } @@ -121,8 +125,8 @@ Error Query::FromJSON(std::string_view dsl) { return dsl::Parse(dsl, *this); } std::string Query::GetJSON() const { return dsl::toDsl(*this); } -WrSerializer &Query::GetSQL(WrSerializer &ser, bool stripArgs) const { return SQLEncoder(*this).GetSQL(ser, stripArgs); } -WrSerializer &Query::GetSQL(WrSerializer &ser, QueryType realType, bool stripArgs) const { +WrSerializer& Query::GetSQL(WrSerializer& ser, bool stripArgs) const { return SQLEncoder(*this).GetSQL(ser, stripArgs); } +WrSerializer& Query::GetSQL(WrSerializer& ser, QueryType realType, bool stripArgs) const { return SQLEncoder(*this, realType).GetSQL(ser, stripArgs); } @@ -136,7 +140,7 @@ std::string Query::GetSQL(QueryType realType) const { return std::string(SQLEncoder(*this, realType).GetSQL(ser, false).Slice()); } -void Query::Join(JoinedQuery &&jq) & { +void Query::Join(JoinedQuery&& jq) & { switch (jq.joinType) { case JoinType::Merge: if (nextOp_ != OpAnd) { @@ -164,7 +168,7 @@ void Query::Join(JoinedQuery &&jq) & { adoptNested(joinQueries_.back()); } -VariantArray Query::deserializeValues(Serializer &ser, CondType cond) { +VariantArray Query::deserializeValues(Serializer& ser, CondType cond) { VariantArray values; auto cnt = ser.GetVarUint(); if (cond == CondDWithin) { @@ -180,12 +184,14 @@ VariantArray Query::deserializeValues(Serializer &ser, CondType cond) { values.emplace_back(ser.GetVariant().EnsureHold()); } else { values.reserve(cnt); - while (cnt--) values.emplace_back(ser.GetVariant().EnsureHold()); + while (cnt--) { + values.emplace_back(ser.GetVariant().EnsureHold()); + } } return values; } -void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { +void Query::deserialize(Serializer& ser, bool& hasJoinConditions) { bool end = false; std::vector> equalPositions; while (!end && !ser.Eof()) { @@ -231,11 +237,13 @@ void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { size_t fieldsCount = ser.GetVarUint(); h_vector fields; fields.reserve(fieldsCount); - while (fieldsCount--) fields.emplace_back(std::string(ser.GetVString())); + while (fieldsCount--) { + fields.emplace_back(std::string(ser.GetVString())); + } auto pos = ser.Pos(); bool aggEnd = false; aggregations_.emplace_back(type, std::move(fields)); - auto &ae = aggregations_.back(); + auto& ae = aggregations_.back(); while (!ser.Eof() && !aggEnd) { int atype = ser.GetVarUint(); switch (atype) { @@ -277,7 +285,9 @@ void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { throw Error(errParams, "Forced sort order is allowed for the first sorting entry only"); } forcedSortOrder_.reserve(cnt); - while (cnt--) forcedSortOrder_.push_back(ser.GetVariant().EnsureHold()); + while (cnt--) { + forcedSortOrder_.push_back(ser.GetVariant().EnsureHold()); + } break; } case QueryJoinOn: { @@ -285,8 +295,8 @@ void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { const CondType condition = static_cast(ser.GetVarUint()); std::string leftFieldName{ser.GetVString()}; std::string rightFieldName{ser.GetVString()}; - reinterpret_cast(this)->joinEntries_.emplace_back(op, condition, std::move(leftFieldName), - std::move(rightFieldName)); + reinterpret_cast(this)->joinEntries_.emplace_back(op, condition, std::move(leftFieldName), + std::move(rightFieldName)); break; } case QueryDebugLevel: @@ -311,7 +321,9 @@ void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { const unsigned bracketPosition = ser.GetVarUint(); const unsigned fieldsCount = ser.GetVarUint(); equalPositions.emplace_back(bracketPosition, fieldsCount); - for (auto &field : equalPositions.back().second) field = ser.GetVString(); + for (auto& field : equalPositions.back().second) { + field = ser.GetVString(); + } break; } case QueryExplain: @@ -402,7 +414,7 @@ void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { throw Error(errParseBin, "Unknown type %d while parsing binary buffer", qtype); } } - for (auto &&eqPos : equalPositions) { + for (auto&& eqPos : equalPositions) { if (eqPos.first == 0) { entries_.equalPositions.emplace_back(std::move(eqPos.second)); } else { @@ -412,19 +424,19 @@ void Query::deserialize(Serializer &ser, bool &hasJoinConditions) { return; } -void Query::Serialize(WrSerializer &ser, uint8_t mode) const { +void Query::Serialize(WrSerializer& ser, uint8_t mode) const { ser.PutVString(NsName()); entries_.Serialize(ser, subQueries_); if (!(mode & SkipAggregations)) { - for (const auto &agg : aggregations_) { + for (const auto& agg : aggregations_) { ser.PutVarUint(QueryAggregation); ser.PutVarUint(agg.Type()); ser.PutVarUint(agg.Fields().size()); - for (const auto &field : agg.Fields()) { + for (const auto& field : agg.Fields()) { ser.PutVString(field); } - for (const auto &se : agg.Sorting()) { + for (const auto& se : agg.Sorting()) { ser.PutVarUint(QueryAggregationSort); ser.PutVString(se.expression); ser.PutVarUint(se.desc); @@ -441,18 +453,20 @@ void Query::Serialize(WrSerializer &ser, uint8_t mode) const { } if (!(mode & SkipSortEntries)) { - for (const auto &sortingEntry : sortingEntries_) { + for (const auto& sortingEntry : sortingEntries_) { ser.PutVarUint(QuerySortIndex); ser.PutVString(sortingEntry.expression); ser.PutVarUint(sortingEntry.desc); int cnt = forcedSortOrder_.size(); ser.PutVarUint(cnt); - for (auto &kv : forcedSortOrder_) ser.PutVariant(kv); + for (auto& kv : forcedSortOrder_) { + ser.PutVariant(kv); + } } } if (mode & WithJoinEntries) { - for (const auto &qje : reinterpret_cast(this)->joinEntries_) { + for (const auto& qje : reinterpret_cast(this)->joinEntries_) { ser.PutVarUint(QueryJoinOn); ser.PutVarUint(qje.Operation()); ser.PutVarUint(qje.Condition()); @@ -461,20 +475,24 @@ void Query::Serialize(WrSerializer &ser, uint8_t mode) const { } } - for (const auto &equalPoses : entries_.equalPositions) { + for (const auto& equalPoses : entries_.equalPositions) { ser.PutVarUint(QueryEqualPosition); ser.PutVarUint(0); ser.PutVarUint(equalPoses.size()); - for (const auto &ep : equalPoses) ser.PutVString(ep); + for (const auto& ep : equalPoses) { + ser.PutVString(ep); + } } for (size_t i = 0; i < entries_.Size(); ++i) { if (entries_.IsSubTree(i)) { - const auto &bracket = entries_.Get(i); - for (const auto &equalPoses : bracket.equalPositions) { + const auto& bracket = entries_.Get(i); + for (const auto& equalPoses : bracket.equalPositions) { ser.PutVarUint(QueryEqualPosition); ser.PutVarUint(i + 1); ser.PutVarUint(equalPoses.size()); - for (const auto &ep : equalPoses) ser.PutVString(ep); + for (const auto& ep : equalPoses) { + ser.PutVString(ep); + } } } } @@ -506,7 +524,7 @@ void Query::Serialize(WrSerializer &ser, uint8_t mode) const { ser.PutVarUint(CalcTotal()); } - for (const auto &sf : selectFilter_) { + for (const auto& sf : selectFilter_) { ser.PutVarUint(QuerySelectFilter); ser.PutVString(sf); } @@ -520,13 +538,13 @@ void Query::Serialize(WrSerializer &ser, uint8_t mode) const { } } - for (const auto &field : updateFields_) { + for (const auto& field : updateFields_) { if (field.Mode() == FieldModeSet) { ser.PutVarUint(QueryUpdateFieldV2); ser.PutVString(field.Column()); ser.PutVarUint(field.Values().IsArrayValue()); ser.PutVarUint(field.Values().size()); - for (const Variant &val : field.Values()) { + for (const Variant& val : field.Values()) { ser.PutVarUint(field.IsExpression()); ser.PutVariant(val); } @@ -541,7 +559,7 @@ void Query::Serialize(WrSerializer &ser, uint8_t mode) const { ser.PutVarUint(QueryEnd); // finita la commedia... of root query if (!(mode & SkipJoinQueries)) { - for (const auto &jq : joinQueries_) { + for (const auto& jq : joinQueries_) { if (!(mode & SkipLeftJoinQueries) || jq.joinType != JoinType::LeftJoin) { ser.PutVarUint(static_cast(jq.joinType)); jq.Serialize(ser, WithJoinEntries); @@ -550,14 +568,14 @@ void Query::Serialize(WrSerializer &ser, uint8_t mode) const { } if (!(mode & SkipMergeQueries)) { - for (const auto &mq : mergeQueries_) { + for (const auto& mq : mergeQueries_) { ser.PutVarUint(static_cast(mq.joinType)); mq.Serialize(ser, (mode | WithJoinEntries) & (~SkipSortEntries)); } } } -Query Query::Deserialize(Serializer &ser) { +Query Query::Deserialize(Serializer& ser) { Query res(ser.GetVString()); bool hasJoinConditions = false; res.deserialize(ser, hasJoinConditions); @@ -573,7 +591,7 @@ Query Query::Deserialize(Serializer &ser) { res.mergeQueries_.emplace_back(std::move(q1)); nested = true; } else { - Query &q = nested ? res.mergeQueries_.back() : res; + Query& q = nested ? res.mergeQueries_.back() : res; if (joinType != JoinType::LeftJoin && !hasJoinConditions) { const size_t joinIdx = res.joinQueries_.size(); res.entries_.Append((joinType == JoinType::OrInnerJoin) ? OpOr : OpAnd, joinIdx); @@ -585,38 +603,38 @@ Query Query::Deserialize(Serializer &ser) { return res; } -Query &Query::Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, Query &&qr) & { +Query& Query::Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, Query&& qr) & { auto jq = JoinedQuery{joinType, std::move(qr)}; jq.joinEntries_.emplace_back(op, cond, std::move(leftField), std::move(rightField)); Join(std::move(jq)); return *this; } -Query &Query::Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, const Query &qr) & { +Query& Query::Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, const Query& qr) & { auto jq = JoinedQuery{joinType, qr}; jq.joinEntries_.emplace_back(op, cond, std::move(leftField), std::move(rightField)); Join(std::move(jq)); return *this; } -Query &Query::Merge(const Query &q) & { +Query& Query::Merge(const Query& q) & { mergeQueries_.emplace_back(JoinType::Merge, q); adoptNested(mergeQueries_.back()); return *this; } -Query &Query::Merge(Query &&q) & { +Query& Query::Merge(Query&& q) & { mergeQueries_.emplace_back(JoinType::Merge, std::move(q)); adoptNested(mergeQueries_.back()); return *this; } -void Query::AddJoinQuery(JoinedQuery &&jq) { +void Query::AddJoinQuery(JoinedQuery&& jq) { adoptNested(jq); joinQueries_.emplace_back(std::move(jq)); } -Query &Query::SortStDistance(std::string_view field, Point p, bool desc) & { +Query& Query::SortStDistance(std::string_view field, Point p, bool desc) & { if (field.empty()) { throw Error(errParams, "Field name for ST_Distance can not be empty"); } @@ -624,7 +642,7 @@ Query &Query::SortStDistance(std::string_view field, Point p, bool desc) & { return *this; } -Query &Query::SortStDistance(std::string_view field1, std::string_view field2, bool desc) & { +Query& Query::SortStDistance(std::string_view field1, std::string_view field2, bool desc) & { if (field1.empty() || field2.empty()) { throw Error(errParams, "Fields names for ST_Distance can not be empty"); } @@ -633,48 +651,64 @@ Query &Query::SortStDistance(std::string_view field1, std::string_view field2, b } void Query::walkNested(bool withSelf, bool withMerged, bool withSubQueries, - const std::function &visitor) noexcept(noexcept(visitor(std::declval()))) { - if (withSelf) visitor(*this); + const std::function& visitor) noexcept(noexcept(visitor(std::declval()))) { + if (withSelf) { + visitor(*this); + } if (withMerged) { - for (auto &mq : mergeQueries_) visitor(mq); + for (auto& mq : mergeQueries_) { + visitor(mq); + } if (withSubQueries) { - for (auto &mq : mergeQueries_) { - for (auto &nq : mq.subQueries_) { + for (auto& mq : mergeQueries_) { + for (auto& nq : mq.subQueries_) { nq.walkNested(true, true, true, visitor); } } } } - for (auto &jq : joinQueries_) visitor(jq); - for (auto &mq : mergeQueries_) { - for (auto &jq : mq.joinQueries_) visitor(jq); + for (auto& jq : joinQueries_) { + visitor(jq); + } + for (auto& mq : mergeQueries_) { + for (auto& jq : mq.joinQueries_) { + visitor(jq); + } } if (withSubQueries) { - for (auto &nq : subQueries_) { + for (auto& nq : subQueries_) { nq.walkNested(true, withMerged, true, visitor); } } } -void Query::WalkNested(bool withSelf, bool withMerged, bool withSubQueries, const std::function &visitor) const +void Query::WalkNested(bool withSelf, bool withMerged, bool withSubQueries, const std::function& visitor) const noexcept(noexcept(visitor(std::declval()))) { - if (withSelf) visitor(*this); + if (withSelf) { + visitor(*this); + } if (withMerged) { - for (auto &mq : mergeQueries_) visitor(mq); + for (auto& mq : mergeQueries_) { + visitor(mq); + } if (withSubQueries) { - for (auto &mq : mergeQueries_) { - for (auto &nq : mq.subQueries_) { + for (auto& mq : mergeQueries_) { + for (auto& nq : mq.subQueries_) { nq.WalkNested(true, true, true, visitor); } } } } - for (auto &jq : joinQueries_) visitor(jq); - for (auto &mq : mergeQueries_) { - for (auto &jq : mq.joinQueries_) visitor(jq); + for (auto& jq : joinQueries_) { + visitor(jq); + } + for (auto& mq : mergeQueries_) { + for (auto& jq : mq.joinQueries_) { + visitor(jq); + } } if (withSubQueries) { - for (auto &nq : subQueries_) { + for (auto& nq : subQueries_) { nq.WalkNested(true, withMerged, true, visitor); } } @@ -684,8 +718,8 @@ bool Query::IsWALQuery() const noexcept { if (entries_.Size() == 1 && entries_.Is(0) && kLsnIndexName == entries_.Get(0).FieldName()) { return true; } else if (entries_.Size() == 2 && entries_.Is(0) && entries_.Is(1)) { - const auto &index0 = entries_.Get(0).FieldName(); - const auto &index1 = entries_.Get(1).FieldName(); + const auto& index0 = entries_.Get(0).FieldName(); + const auto& index1 = entries_.Get(1).FieldName(); return (kLsnIndexName == index0 && kSlaveVersionIndexName == index1) || (kLsnIndexName == index1 && kSlaveVersionIndexName == index0); } diff --git a/cpp_src/core/query/query.h b/cpp_src/core/query/query.h index c44c4f1af..12b6ed234 100644 --- a/cpp_src/core/query/query.h +++ b/cpp_src/core/query/query.h @@ -28,15 +28,15 @@ class Query { /// @param start - number of the first row to get from selected set. Analog to sql OFFSET Offset. /// @param count - number of rows to get from result set. Analog to sql LIMIT RowsCount. /// @param calcTotal - calculation mode. - template > * = nullptr> - explicit Query(Str &&nsName, unsigned start = QueryEntry::kDefaultOffset, unsigned count = QueryEntry::kDefaultLimit, + template >* = nullptr> + explicit Query(Str&& nsName, unsigned start = QueryEntry::kDefaultOffset, unsigned count = QueryEntry::kDefaultLimit, CalcTotalMode calcTotal = ModeNoTotal) : namespace_(std::forward(nsName)), start_(start), count_(count), calcTotal_(calcTotal) {} Query() = default; /// Allows to compare 2 Query objects. - [[nodiscard]] bool operator==(const Query &) const; + [[nodiscard]] bool operator==(const Query&) const; /// Parses pure sql select query and initializes Query object data members as a result. /// @param q - sql query. @@ -45,13 +45,13 @@ class Query { /// Logs query in 'Select field1, ... field N from namespace ...' format. /// @param ser - serializer to store SQL string /// @param stripArgs - replace condition values with '?' - WrSerializer &GetSQL(WrSerializer &ser, bool stripArgs = false) const; + WrSerializer& GetSQL(WrSerializer& ser, bool stripArgs = false) const; /// Logs query in 'Select field1, ... field N from namespace ...' format. /// @param ser - serializer to store SQL string /// @param realType - replaces original query's type /// @param stripArgs - replace condition values with '?' - WrSerializer &GetSQL(WrSerializer &ser, QueryType realType, bool stripArgs = false) const; + WrSerializer& GetSQL(WrSerializer& ser, QueryType realType, bool stripArgs = false) const; /// Logs query in 'Select field1, ... field N from namespace ...' format. /// @param stripArgs - replace condition values with '?' @@ -74,11 +74,11 @@ class Query { /// Enable explain query /// @param on - signaling on/off /// @return Query object ready to be executed - Query &Explain(bool on = true) & noexcept { - walkNested(true, true, true, [on](Query &q) noexcept { q.explain_ = on; }); + Query& Explain(bool on = true) & noexcept { + walkNested(true, true, true, [on](Query& q) noexcept { q.explain_ = on; }); return *this; } - [[nodiscard]] Query &&Explain(bool on = true) && noexcept { return std::move(Explain(on)); } + [[nodiscard]] Query&& Explain(bool on = true) && noexcept { return std::move(Explain(on)); } [[nodiscard]] bool NeedExplain() const noexcept { return explain_; } /// Adds a condition with a single value. Analog to sql Where clause. @@ -86,14 +86,14 @@ class Query { /// @param cond - type of condition. /// @param val - value of index to be compared with. /// @return Query object ready to be executed. - template > * = nullptr, - std::enable_if_t> * = nullptr> - Query &Where(Str &&field, CondType cond, Input &&val) & { + template >* = nullptr, + std::enable_if_t>* = nullptr> + Query& Where(Str&& field, CondType cond, Input&& val) & { return Where(std::forward(field), cond, VariantArray{Variant{std::forward(val)}}); } - template > * = nullptr, - std::enable_if_t> * = nullptr> - [[nodiscard]] Query &&Where(Str &&field, CondType cond, Input &&val) && { + template >* = nullptr, + std::enable_if_t>* = nullptr> + [[nodiscard]] Query&& Where(Str&& field, CondType cond, Input&& val) && { return std::move(Where(std::forward(field), cond, VariantArray{Variant{std::forward(val)}})); } @@ -102,17 +102,19 @@ class Query { /// @param cond - type of condition. /// @param l - list of index values to be compared with. /// @return Query object ready to be executed. - template > * = nullptr> - Query &Where(Str &&field, CondType cond, std::initializer_list l) & { + template >* = nullptr> + Query& Where(Str&& field, CondType cond, std::initializer_list l) & { VariantArray values; values.reserve(l.size()); - for (auto it = l.begin(); it != l.end(); it++) values.emplace_back(*it); + for (auto it = l.begin(); it != l.end(); it++) { + values.emplace_back(*it); + } entries_.Append(nextOp_, std::forward(field), cond, std::move(values)); nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Where(Str &&field, CondType cond, std::initializer_list l) && { + template >* = nullptr> + [[nodiscard]] Query&& Where(Str&& field, CondType cond, std::initializer_list l) && { return std::move(Where(std::forward(field), cond, std::move(l))); } @@ -121,17 +123,19 @@ class Query { /// @param cond - type of condition. /// @param l - vector of index values to be compared with. /// @return Query object ready to be executed. - template > * = nullptr> - Query &Where(Str &&field, CondType cond, const std::vector &l) & { + template >* = nullptr> + Query& Where(Str&& field, CondType cond, const std::vector& l) & { VariantArray values; values.reserve(l.size()); - for (auto it = l.begin(); it != l.end(); it++) values.emplace_back(*it); + for (auto it = l.begin(); it != l.end(); it++) { + values.emplace_back(*it); + } entries_.Append(nextOp_, std::forward(field), cond, std::move(values)); nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Where(Str &&field, CondType cond, const std::vector &l) && { + template >* = nullptr> + [[nodiscard]] Query&& Where(Str&& field, CondType cond, const std::vector& l) && { return std::move(Where(std::forward(field), cond, l)); } @@ -140,14 +144,14 @@ class Query { /// @param cond - type of condition. /// @param l - vector of index values to be compared with. /// @return Query object ready to be executed. - template > * = nullptr> - Query &Where(Str &&field, CondType cond, VariantArray l) & { + template >* = nullptr> + Query& Where(Str&& field, CondType cond, VariantArray l) & { entries_.Append(nextOp_, std::forward(field), cond, std::move(l)); nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Where(Str &&field, CondType cond, VariantArray l) && { + template >* = nullptr> + [[nodiscard]] Query&& Where(Str&& field, CondType cond, VariantArray l) && { return std::move(Where(std::forward(field), cond, std::move(l))); } @@ -162,8 +166,8 @@ class Query { /// in case of CondRange) belongs to "bookid" and l[0][1] (and l[1][1] in case of CondRange) /// belongs to "price" indexes. /// @return Query object ready to be executed. - template > * = nullptr> - Query &WhereComposite(Str &&idx, CondType cond, std::initializer_list l) & { + template >* = nullptr> + Query& WhereComposite(Str&& idx, CondType cond, std::initializer_list l) & { VariantArray values; values.reserve(l.size()); for (auto it = l.begin(); it != l.end(); it++) { @@ -173,12 +177,12 @@ class Query { nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&WhereComposite(Str &&idx, CondType cond, std::initializer_list l) && { + template >* = nullptr> + [[nodiscard]] Query&& WhereComposite(Str&& idx, CondType cond, std::initializer_list l) && { return std::move(WhereComposite(std::forward(idx), cond, std::move(l))); } - template > * = nullptr> - Query &WhereComposite(Str &&idx, CondType cond, const std::vector &v) & { + template >* = nullptr> + Query& WhereComposite(Str&& idx, CondType cond, const std::vector& v) & { VariantArray values; values.reserve(v.size()); for (auto it = v.begin(); it != v.end(); it++) { @@ -188,34 +192,34 @@ class Query { nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&WhereComposite(Str &&idx, CondType cond, const std::vector &v) && { + template >* = nullptr> + [[nodiscard]] Query&& WhereComposite(Str&& idx, CondType cond, const std::vector& v) && { return std::move(WhereComposite(std::forward(idx), cond, v)); } template - Query &WhereBetweenFields(Str1 &&firstIdx, CondType cond, Str2 &&secondIdx) & { + Query& WhereBetweenFields(Str1&& firstIdx, CondType cond, Str2&& secondIdx) & { entries_.Append(nextOp_, std::forward(firstIdx), cond, std::forward(secondIdx)); nextOp_ = OpAnd; return *this; } template - [[nodiscard]] Query &&WhereBetweenFields(Str1 &&firstIdx, CondType cond, Str2 &&secondIdx) && { + [[nodiscard]] Query&& WhereBetweenFields(Str1&& firstIdx, CondType cond, Str2&& secondIdx) && { return std::move(WhereBetweenFields(std::forward(firstIdx), cond, std::forward(secondIdx))); } - template > * = nullptr> - Query &DWithin(Str &&field, Point p, double distance) & { + template >* = nullptr> + Query& DWithin(Str&& field, Point p, double distance) & { entries_.Append(nextOp_, std::forward(field), CondDWithin, VariantArray::Create(p, distance)); nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&DWithin(Str &&field, Point p, double distance) && { + template >* = nullptr> + [[nodiscard]] Query&& DWithin(Str&& field, Point p, double distance) && { return std::move(DWithin(std::forward(field), p, distance)); } - Query &DWithin(Query &&q, Point p, double distance) & { return Where(std::move(q), CondDWithin, VariantArray::Create(p, distance)); } - [[nodiscard]] Query &&DWithin(Query &&q, Point p, double distance) && { return std::move(DWithin(std::move(q), p, distance)); } - Query &Where(Query &&q, CondType cond, VariantArray &&values) & { + Query& DWithin(Query&& q, Point p, double distance) & { return Where(std::move(q), CondDWithin, VariantArray::Create(p, distance)); } + [[nodiscard]] Query&& DWithin(Query&& q, Point p, double distance) && { return std::move(DWithin(std::move(q), p, distance)); } + Query& Where(Query&& q, CondType cond, VariantArray&& values) & { if (cond == CondEmpty || cond == CondAny) { q.checkSubQueryNoData(); q.Limit(0); @@ -241,28 +245,28 @@ class Query { nextOp_ = OpAnd; return *this; } - [[nodiscard]] Query &&Where(Query &&q, CondType cond, VariantArray &&values) && { + [[nodiscard]] Query&& Where(Query&& q, CondType cond, VariantArray&& values) && { return std::move(Where(std::move(q), cond, std::move(values))); } template - Query &Where(Query &&q, CondType cond, std::initializer_list values) & { + Query& Where(Query&& q, CondType cond, std::initializer_list values) & { return Where(std::move(q), cond, VariantArray::Create(values)); } template - [[nodiscard]] Query &&Where(Query &&q, CondType cond, std::initializer_list values) && { + [[nodiscard]] Query&& Where(Query&& q, CondType cond, std::initializer_list values) && { return std::move(Where(std::move(q), cond, VariantArray::Create(values))); } - template > * = nullptr> - [[nodiscard]] Query &Where(Query &&q, CondType cond, Input &&val) & { + template >* = nullptr> + [[nodiscard]] Query& Where(Query&& q, CondType cond, Input&& val) & { return Where(std::move(q), cond, VariantArray{Variant{std::forward(val)}}); } - template > * = nullptr> - [[nodiscard]] Query &&Where(Query &&q, CondType cond, Input &&val) && { + template >* = nullptr> + [[nodiscard]] Query&& Where(Query&& q, CondType cond, Input&& val) && { return std::move(Where(std::move(q), cond, VariantArray{Variant{std::forward(val)}})); } - template > * = nullptr> - Query &Where(Str &&field, CondType cond, Query &&q) & { + template >* = nullptr> + Query& Where(Str&& field, CondType cond, Query&& q) & { if (cond == CondDWithin) { throw Error(errQueryExec, "DWithin between field and subquery"); } @@ -280,8 +284,8 @@ class Query { nextOp_ = OpAnd; return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Where(Str &&field, CondType cond, Query &&q) && { + template >* = nullptr> + [[nodiscard]] Query&& Where(Str&& field, CondType cond, Query&& q) && { return std::move(Where(std::forward(field), cond, std::move(q))); } @@ -289,106 +293,114 @@ class Query { /// @param field - field name. /// @param value - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &Set(Str &&field, ValueType value, bool hasExpressions = false) & { + template >* = nullptr> + Query& Set(Str&& field, ValueType value, bool hasExpressions = false) & { return Set(std::forward(field), {value}, hasExpressions); } - template > * = nullptr> - [[nodiscard]] Query &&Set(Str &&field, ValueType value, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& Set(Str&& field, ValueType value, bool hasExpressions = false) && { return std::move(Set(std::forward(field), std::move(value), hasExpressions)); } /// Sets a new value for a field. /// @param field - field name. /// @param l - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &Set(Str &&field, std::initializer_list l, bool hasExpressions = false) & { + template >* = nullptr> + Query& Set(Str&& field, std::initializer_list l, bool hasExpressions = false) & { VariantArray value; value.reserve(l.size()); - for (auto it = l.begin(); it != l.end(); it++) value.emplace_back(*it); + for (auto it = l.begin(); it != l.end(); it++) { + value.emplace_back(*it); + } return Set(std::forward(field), std::move(value), hasExpressions); } - template > * = nullptr> - [[nodiscard]] Query &&Set(Str &&field, std::initializer_list l, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& Set(Str&& field, std::initializer_list l, bool hasExpressions = false) && { return std::move(Set(std::forward(field), std::move(l), hasExpressions)); } /// Sets a new value for a field. /// @param field - field name. /// @param l - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &Set(Str &&field, const std::vector &l, bool hasExpressions = false) & { + template >* = nullptr> + Query& Set(Str&& field, const std::vector& l, bool hasExpressions = false) & { VariantArray value; value.reserve(l.size()); - for (auto it = l.begin(); it != l.end(); it++) value.emplace_back(*it); + for (auto it = l.begin(); it != l.end(); it++) { + value.emplace_back(*it); + } return Set(std::forward(field), std::move(value.MarkArray()), hasExpressions); } - template > * = nullptr> - [[nodiscard]] Query &&Set(Str &&field, const std::vector &l, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& Set(Str&& field, const std::vector& l, bool hasExpressions = false) && { return std::move(Set(std::forward(field), l, hasExpressions)); } /// Sets a new value for a field. /// @param field - field name. /// @param value - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &Set(Str &&field, VariantArray value, bool hasExpressions = false) & { + template >* = nullptr> + Query& Set(Str&& field, VariantArray value, bool hasExpressions = false) & { updateFields_.emplace_back(std::forward(field), std::move(value), FieldModeSet, hasExpressions); return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Set(Str &&field, VariantArray value, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& Set(Str&& field, VariantArray value, bool hasExpressions = false) && { return std::move(Set(std::forward(field), std::move(value), hasExpressions)); } /// Sets a value for a field as an object. /// @param field - field name. /// @param value - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &SetObject(Str &&field, ValueType value, bool hasExpressions = false) & { + template >* = nullptr> + Query& SetObject(Str&& field, ValueType value, bool hasExpressions = false) & { return SetObject(std::forward(field), {value}, hasExpressions); } - template > * = nullptr> - [[nodiscard]] Query &&SetObject(Str &&field, ValueType value, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& SetObject(Str&& field, ValueType value, bool hasExpressions = false) && { return std::move(SetObject(std::forward(field), std::move(value), hasExpressions)); } /// Sets a new value for a field as an object. /// @param field - field name. /// @param l - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &SetObject(Str &&field, std::initializer_list l, bool hasExpressions = false) & { + template >* = nullptr> + Query& SetObject(Str&& field, std::initializer_list l, bool hasExpressions = false) & { VariantArray value; value.reserve(l.size()); - for (auto it = l.begin(); it != l.end(); it++) value.emplace_back(*it); + for (auto it = l.begin(); it != l.end(); it++) { + value.emplace_back(*it); + } return SetObject(std::forward(field), std::move(value), hasExpressions); } - template > * = nullptr> - [[nodiscard]] Query &&SetObject(Str &&field, std::initializer_list l, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& SetObject(Str&& field, std::initializer_list l, bool hasExpressions = false) && { return std::move(SetObject(std::forward(field), std::move(l), hasExpressions)); } /// Sets a new value for a field as an object. /// @param field - field name. /// @param l - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &SetObject(Str &&field, const std::vector &l, bool hasExpressions = false) & { + template >* = nullptr> + Query& SetObject(Str&& field, const std::vector& l, bool hasExpressions = false) & { VariantArray value; value.reserve(l.size()); - for (auto it = l.begin(); it != l.end(); it++) value.emplace_back(Variant(*it)); + for (auto it = l.begin(); it != l.end(); it++) { + value.emplace_back(Variant(*it)); + } return SetObject(std::forward(field), std::move(value.MarkArray()), hasExpressions); } - template > * = nullptr> - [[nodiscard]] Query &&SetObject(Str &&field, const std::vector &l, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& SetObject(Str&& field, const std::vector& l, bool hasExpressions = false) && { return std::move(SetObject(std::forward(field), l, hasExpressions)); } /// Sets a value for a field as an object. /// @param field - field name. /// @param value - new value. /// @param hasExpressions - true: value has expressions in it - template > * = nullptr> - Query &SetObject(Str &&field, VariantArray value, bool hasExpressions = false) & { - for (auto &it : value) { + template >* = nullptr> + Query& SetObject(Str&& field, VariantArray value, bool hasExpressions = false) & { + for (auto& it : value) { if (!it.Type().Is()) { throw Error(errLogic, "Unexpected variant type in SetObject: %s. Expecting KeyValueType::String with JSON-content", it.Type().Name()); @@ -397,56 +409,56 @@ class Query { updateFields_.emplace_back(std::forward(field), std::move(value), FieldModeSetJson, hasExpressions); return *this; } - template > * = nullptr> - [[nodiscard]] Query &&SetObject(Str &&field, VariantArray value, bool hasExpressions = false) && { + template >* = nullptr> + [[nodiscard]] Query&& SetObject(Str&& field, VariantArray value, bool hasExpressions = false) && { return std::move(SetObject(std::forward(field), std::move(value), hasExpressions)); } /// Drops a value for a field. /// @param field - field name. - template > * = nullptr> - Query &Drop(Str &&field) & { + template >* = nullptr> + Query& Drop(Str&& field) & { updateFields_.emplace_back(std::forward(field), VariantArray(), FieldModeDrop); return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Drop(Str &&field) && { + template >* = nullptr> + [[nodiscard]] Query&& Drop(Str&& field) && { return std::move(Drop(std::forward(field))); } /// Add sql-function to query. /// @param function - function declaration. - template > * = nullptr> - void AddFunction(Str &&function) { + template >* = nullptr> + void AddFunction(Str&& function) { selectFunctions_.emplace_back(std::forward(function)); } /// Adds equal position fields to arrays queries. /// @param equalPosition - list of fields with equal array index position. - Query &AddEqualPosition(h_vector equalPosition) & { - auto *const bracket = entries_.LastOpenBracket(); - auto &eqPos = (bracket ? bracket->equalPositions : entries_.equalPositions); + Query& AddEqualPosition(h_vector equalPosition) & { + auto* const bracket = entries_.LastOpenBracket(); + auto& eqPos = (bracket ? bracket->equalPositions : entries_.equalPositions); eqPos.emplace_back(std::make_move_iterator(equalPosition.begin()), std::make_move_iterator(equalPosition.end())); return *this; } - Query &AddEqualPosition(std::vector equalPosition) & { - auto *const bracket = entries_.LastOpenBracket(); - auto &eqPos = (bracket ? bracket->equalPositions : entries_.equalPositions); + Query& AddEqualPosition(std::vector equalPosition) & { + auto* const bracket = entries_.LastOpenBracket(); + auto& eqPos = (bracket ? bracket->equalPositions : entries_.equalPositions); eqPos.emplace_back(std::make_move_iterator(equalPosition.begin()), std::make_move_iterator(equalPosition.end())); return *this; } - Query &AddEqualPosition(std::initializer_list l) & { - auto *const bracket = entries_.LastOpenBracket(); - auto &eqPos = (bracket ? bracket->equalPositions : entries_.equalPositions); + Query& AddEqualPosition(std::initializer_list l) & { + auto* const bracket = entries_.LastOpenBracket(); + auto& eqPos = (bracket ? bracket->equalPositions : entries_.equalPositions); eqPos.emplace_back(l); return *this; } - [[nodiscard]] Query &&AddEqualPosition(h_vector equalPosition) && { + [[nodiscard]] Query&& AddEqualPosition(h_vector equalPosition) && { return std::move(AddEqualPosition(std::move(equalPosition))); } - [[nodiscard]] Query &&AddEqualPosition(std::vector equalPosition) && { + [[nodiscard]] Query&& AddEqualPosition(std::vector equalPosition) && { return std::move(AddEqualPosition(std::move(equalPosition))); } - [[nodiscard]] Query &&AddEqualPosition(std::initializer_list l) && { return std::move(AddEqualPosition(l)); } + [[nodiscard]] Query&& AddEqualPosition(std::initializer_list l) && { return std::move(AddEqualPosition(l)); } /// Joins namespace with another namespace. Analog to sql JOIN. /// @param joinType - type of Join (Inner, Left or OrInner). @@ -456,21 +468,21 @@ class Query { /// @param op - operation type (and, or, not). /// @param qr - query of the namespace that is going to be joined with this one. /// @return Query object ready to be executed. - Query &Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, Query &&qr) &; + Query& Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, Query&& qr) &; template - [[nodiscard]] Query &&Join(JoinType joinType, StrL &&leftField, StrR &&rightField, CondType cond, OpType op, Query &&qr) && { + [[nodiscard]] Query&& Join(JoinType joinType, StrL&& leftField, StrR&& rightField, CondType cond, OpType op, Query&& qr) && { return std::move(Join(joinType, std::forward(leftField), std::forward(rightField), cond, op, std::move(qr))); } - Query &Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, const Query &qr) &; + Query& Join(JoinType joinType, std::string leftField, std::string rightField, CondType cond, OpType op, const Query& qr) &; template - [[nodiscard]] Query &&Join(JoinType joinType, StrL &&leftField, StrR &&rightField, CondType cond, OpType op, const Query &qr) && { + [[nodiscard]] Query&& Join(JoinType joinType, StrL&& leftField, StrR&& rightField, CondType cond, OpType op, const Query& qr) && { return std::move(Join(joinType, std::forward(leftField), std::forward(rightField), cond, op, qr)); } - [[nodiscard]] auto Join(JoinType joinType, Query &&q) &; - [[nodiscard]] auto Join(JoinType joinType, const Query &q) &; - [[nodiscard]] auto Join(JoinType joinType, Query &&q) &&; - [[nodiscard]] auto Join(JoinType joinType, const Query &q) &&; + [[nodiscard]] auto Join(JoinType joinType, Query&& q) &; + [[nodiscard]] auto Join(JoinType joinType, const Query& q) &; + [[nodiscard]] auto Join(JoinType joinType, Query&& q) &&; + [[nodiscard]] auto Join(JoinType joinType, const Query& q) &&; /// @public /// Inner Join of this namespace with another one. @@ -480,19 +492,19 @@ class Query { /// @param qr - query of the namespace that is going to be joined with this one. /// @return Query object ready to be executed. template - Query &InnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, Query &&qr) & { // -V1071 + Query& InnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, Query&& qr) & { // -V1071 return Join(JoinType::InnerJoin, std::forward(leftField), std::forward(rightField), cond, OpAnd, std::move(qr)); } template - [[nodiscard]] Query &&InnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, Query &&qr) && { + [[nodiscard]] Query&& InnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, Query&& qr) && { return std::move(InnerJoin(std::forward(leftField), std::forward(rightField), cond, std::move(qr))); } template - Query &InnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, const Query &qr) & { + Query& InnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, const Query& qr) & { return Join(JoinType::InnerJoin, std::forward(leftField), std::forward(rightField), cond, OpAnd, qr); } template - [[nodiscard]] Query &&InnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, const Query &qr) && { + [[nodiscard]] Query&& InnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, const Query& qr) && { return std::move(InnerJoin(std::forward(leftField), std::forward(rightField), cond, qr)); } @@ -503,19 +515,19 @@ class Query { /// @param qr - query of the namespace that is going to be joined with this one. /// @return Query object ready to be executed. template - Query &LeftJoin(StrL &&leftField, StrR &&rightField, CondType cond, Query &&qr) & { + Query& LeftJoin(StrL&& leftField, StrR&& rightField, CondType cond, Query&& qr) & { return Join(JoinType::LeftJoin, std::forward(leftField), std::forward(rightField), cond, OpAnd, std::move(qr)); } template - [[nodiscard]] Query &&LeftJoin(StrL &&leftField, StrR &&rightField, CondType cond, Query &&qr) && { + [[nodiscard]] Query&& LeftJoin(StrL&& leftField, StrR&& rightField, CondType cond, Query&& qr) && { return std::move(LeftJoin(std::forward(leftField), std::forward(rightField), cond, std::move(qr))); } template - Query &LeftJoin(StrL &&leftField, StrR &&rightField, CondType cond, const Query &qr) & { + Query& LeftJoin(StrL&& leftField, StrR&& rightField, CondType cond, const Query& qr) & { return Join(JoinType::LeftJoin, std::forward(leftField), std::forward(rightField), cond, OpAnd, qr); } template - [[nodiscard]] Query &&LeftJoin(StrL &&leftField, StrR &&rightField, CondType cond, const Query &qr) && { + [[nodiscard]] Query&& LeftJoin(StrL&& leftField, StrR&& rightField, CondType cond, const Query& qr) && { return std::move(LeftJoin(std::forward(leftField), std::forward(rightField), cond, qr)); } @@ -526,57 +538,59 @@ class Query { /// @param qr - query of the namespace that is going to be joined with this one. /// @return a reference to a query object ready to be executed. template - Query &OrInnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, Query &&qr) & { + Query& OrInnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, Query&& qr) & { return Join(JoinType::OrInnerJoin, std::forward(leftField), std::forward(rightField), cond, OpAnd, std::move(qr)); } template - [[nodiscard]] Query &&OrInnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, Query &&qr) && { + [[nodiscard]] Query&& OrInnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, Query&& qr) && { return std::move(OrInnerJoin(std::forward(leftField), std::forward(rightField), cond, std::move(qr))); } template - Query &OrInnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, const Query &qr) & { + Query& OrInnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, const Query& qr) & { return Join(JoinType::OrInnerJoin, std::forward(leftField), std::forward(rightField), cond, OpAnd, qr); } template - [[nodiscard]] Query &&OrInnerJoin(StrL &&leftField, StrR &&rightField, CondType cond, const Query &qr) && { + [[nodiscard]] Query&& OrInnerJoin(StrL&& leftField, StrR&& rightField, CondType cond, const Query& qr) && { return std::move(OrInnerJoin(std::forward(leftField), std::forward(rightField), cond, qr)); } - Query &Merge(const Query &q) &; - [[nodiscard]] Query &&Merge(const Query &q) && { return std::move(Merge(q)); } - Query &Merge(Query &&q) &; - [[nodiscard]] Query &&Merge(Query &&q) && { return std::move(Merge(std::move(q))); } + Query& Merge(const Query& q) &; + [[nodiscard]] Query&& Merge(const Query& q) && { return std::move(Merge(q)); } + Query& Merge(Query&& q) &; + [[nodiscard]] Query&& Merge(Query&& q) && { return std::move(Merge(std::move(q))); } /// Changes debug level. /// @param level - debug level. /// @return Query object. - Query &Debug(int level) & noexcept { - walkNested(true, true, true, [level](Query &q) noexcept { q.debugLevel_ = level; }); + Query& Debug(int level) & noexcept { + walkNested(true, true, true, [level](Query& q) noexcept { q.debugLevel_ = level; }); return *this; } - [[nodiscard]] Query &&Debug(int level) && noexcept { return std::move(Debug(level)); } + [[nodiscard]] Query&& Debug(int level) && noexcept { return std::move(Debug(level)); } [[nodiscard]] int GetDebugLevel() const noexcept { return debugLevel_; } /// Changes strict mode. /// @param mode - strict mode. /// @return Query object. - Query &Strict(StrictMode mode) & noexcept { - walkNested(true, true, true, [mode](Query &q) noexcept { q.strictMode_ = mode; }); + Query& Strict(StrictMode mode) & noexcept { + walkNested(true, true, true, [mode](Query& q) noexcept { q.strictMode_ = mode; }); return *this; } - [[nodiscard]] Query &&Strict(StrictMode mode) && noexcept { return std::move(Strict(mode)); } + [[nodiscard]] Query&& Strict(StrictMode mode) && noexcept { return std::move(Strict(mode)); } [[nodiscard]] StrictMode GetStrictMode() const noexcept { return strictMode_; } /// Performs sorting by certain column. Same as sql 'ORDER BY'. /// @param sort - sorting column name. /// @param desc - is sorting direction descending or ascending. /// @return Query object. - template > * = nullptr> - Query &Sort(Str &&sort, bool desc) & { // -V1071 - if (!strEmpty(sort)) sortingEntries_.emplace_back(std::forward(sort), desc); + template >* = nullptr> + Query& Sort(Str&& sort, bool desc) & { // -V1071 + if (!strEmpty(sort)) { + sortingEntries_.emplace_back(std::forward(sort), desc); + } return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Sort(Str &&sort, bool desc) && { + template >* = nullptr> + [[nodiscard]] Query&& Sort(Str&& sort, bool desc) && { return std::move(Sort(std::forward(sort), desc)); } @@ -585,8 +599,8 @@ class Query { /// @param p - target point. /// @param desc - is sorting direction descending or ascending. /// @return Query object. - Query &SortStDistance(std::string_view field, reindexer::Point p, bool desc) &; - [[nodiscard]] Query &&SortStDistance(std::string_view field, reindexer::Point p, bool desc) && { + Query& SortStDistance(std::string_view field, reindexer::Point p, bool desc) &; + [[nodiscard]] Query&& SortStDistance(std::string_view field, reindexer::Point p, bool desc) && { return std::move(SortStDistance(field, p, desc)); } /// Performs sorting by ST_Distance() expressions for geometry index. Sorting function will use distance 2 fields. @@ -594,8 +608,8 @@ class Query { /// @param field2 - second field name.This field must contain Point. /// @param desc - is sorting direction descending or ascending. /// @return Query object. - Query &SortStDistance(std::string_view field1, std::string_view field2, bool desc) &; - [[nodiscard]] Query &&SortStDistance(std::string_view field1, std::string_view field2, bool desc) && { + Query& SortStDistance(std::string_view field1, std::string_view field2, bool desc) &; + [[nodiscard]] Query&& SortStDistance(std::string_view field1, std::string_view field2, bool desc) && { return std::move(SortStDistance(field1, field2, desc)); } @@ -604,16 +618,19 @@ class Query { /// @param desc - is sorting direction descending or ascending. /// @param forcedSortOrder - list of values for forced sort order. /// @return Query object. - template > * = nullptr> - Query &Sort(Str &&sort, bool desc, std::initializer_list forcedSortOrder) & { - if (!sortingEntries_.empty() && !std::empty(forcedSortOrder)) + template >* = nullptr> + Query& Sort(Str&& sort, bool desc, std::initializer_list forcedSortOrder) & { + if (!sortingEntries_.empty() && !std::empty(forcedSortOrder)) { throw Error(errParams, "Forced sort order is allowed for the first sorting entry only"); + } sortingEntries_.emplace_back(std::forward(sort), desc); - for (const T &v : forcedSortOrder) forcedSortOrder_.emplace_back(v); + for (const T& v : forcedSortOrder) { + forcedSortOrder_.emplace_back(v); + } return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Sort(Str &&sort, bool desc, std::initializer_list forcedSortOrder) && { + template >* = nullptr> + [[nodiscard]] Query&& Sort(Str&& sort, bool desc, std::initializer_list forcedSortOrder) && { return std::move(Sort(std::forward(sort), desc, std::move(forcedSortOrder))); } @@ -622,57 +639,60 @@ class Query { /// @param desc - is sorting direction descending or ascending. /// @param forcedSortOrder - list of values for forced sort order. /// @return Query object. - template > * = nullptr> - Query &Sort(Str &&sort, bool desc, const T &forcedSortOrder) & { - if (!sortingEntries_.empty() && !forcedSortOrder.empty()) + template >* = nullptr> + Query& Sort(Str&& sort, bool desc, const T& forcedSortOrder) & { + if (!sortingEntries_.empty() && !forcedSortOrder.empty()) { throw Error(errParams, "Forced sort order is allowed for the first sorting entry only"); + } sortingEntries_.emplace_back(std::forward(sort), desc); - for (const auto &v : forcedSortOrder) forcedSortOrder_.emplace_back(v); + for (const auto& v : forcedSortOrder) { + forcedSortOrder_.emplace_back(v); + } return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Sort(Str &&sort, bool desc, const T &forcedSortOrder) && { + template >* = nullptr> + [[nodiscard]] Query&& Sort(Str&& sort, bool desc, const T& forcedSortOrder) && { return std::move(Sort(std::forward(sort), desc, forcedSortOrder)); } /// Performs distinct for a certain index. /// @param indexName - name of index for distinct operation. - template > * = nullptr> - Query &Distinct(Str &&indexName) & { + template >* = nullptr> + Query& Distinct(Str&& indexName) & { if (!strEmpty(indexName)) { aggregations_.emplace_back(AggDistinct, h_vector{std::forward(indexName)}); } return *this; } - template > * = nullptr> - [[nodiscard]] Query &&Distinct(Str &&indexName) && { + template >* = nullptr> + [[nodiscard]] Query&& Distinct(Str&& indexName) && { return std::move(Distinct(std::forward(indexName))); } /// Sets list of columns in this namespace to be finally selected. /// @param l - list of columns to be selected. - template > * = nullptr> - Query &Select(std::initializer_list l) & { + template >* = nullptr> + Query& Select(std::initializer_list l) & { return Select>(std::move(l)); } - template > * = nullptr> - [[nodiscard]] Query &&Select(std::initializer_list l) && { + template >* = nullptr> + [[nodiscard]] Query&& Select(std::initializer_list l) && { return std::move(Select>(std::move(l))); } template - Query &Select(StrCont &&l) & { + Query& Select(StrCont&& l) & { using namespace std::string_view_literals; if (!CanAddSelectFilter()) { throw Error(errConflict, kAggregationWithSelectFieldsMsgError); } selectFilter_.insert(selectFilter_.begin(), l.begin(), l.end()); - selectFilter_.erase(std::remove_if(selectFilter_.begin(), selectFilter_.end(), [](const auto &s) { return s == "*"sv; }), + selectFilter_.erase(std::remove_if(selectFilter_.begin(), selectFilter_.end(), [](const auto& s) { return s == "*"sv; }), selectFilter_.end()); return *this; } template - [[nodiscard]] Query &&Select(StrCont &&l) && { + [[nodiscard]] Query&& Select(StrCont&& l) && { return std::move(Select(std::forward(l))); } @@ -685,118 +705,118 @@ class Query { /// @param limit - number of rows to get from result set. /// @param offset - index of the first row to get from result set. /// @return Query object ready to be executed. - Query &Aggregate(AggType type, h_vector fields, const std::vector> &sort = {}, + Query& Aggregate(AggType type, h_vector fields, const std::vector>& sort = {}, unsigned limit = QueryEntry::kDefaultLimit, unsigned offset = QueryEntry::kDefaultOffset) & { if (!CanAddAggregation(type)) { throw Error(errConflict, kAggregationWithSelectFieldsMsgError); } SortingEntries sorting; sorting.reserve(sort.size()); - for (const auto &s : sort) { + for (const auto& s : sort) { sorting.emplace_back(s.first, s.second); } aggregations_.emplace_back(type, std::move(fields), std::move(sorting), limit, offset); return *this; } - [[nodiscard]] Query &&Aggregate(AggType type, h_vector fields, - const std::vector> &sort = {}, unsigned limit = QueryEntry::kDefaultLimit, + [[nodiscard]] Query&& Aggregate(AggType type, h_vector fields, + const std::vector>& sort = {}, unsigned limit = QueryEntry::kDefaultLimit, unsigned offset = QueryEntry::kDefaultOffset) && { return std::move(Aggregate(type, std::move(fields), sort, limit, offset)); } /// Sets next operation type to Or. /// @return Query object. - Query &Or() & { + Query& Or() & { assertrx_dbg(nextOp_ == OpAnd); nextOp_ = OpOr; return *this; } - [[nodiscard]] Query &&Or() && { return std::move(Or()); } + [[nodiscard]] Query&& Or() && { return std::move(Or()); } /// Sets next operation type to Not. /// @return Query object. - Query &Not() & { + Query& Not() & { assertrx_dbg(nextOp_ == OpAnd); nextOp_ = OpNot; return *this; } - [[nodiscard]] Query &&Not() && { return std::move(Not()); } + [[nodiscard]] Query&& Not() && { return std::move(Not()); } /// Sets next operation type to And. /// @return Query object. - Query &And() & { + Query& And() & { nextOp_ = OpAnd; return *this; } - [[nodiscard]] Query &&And() && { return std::move(And()); } - Query &NextOp(OpType op) & { + [[nodiscard]] Query&& And() && { return std::move(And()); } + Query& NextOp(OpType op) & { nextOp_ = op; return *this; } - [[nodiscard]] Query &&NextOp(OpType op) && { return std::move(NextOp(op)); } + [[nodiscard]] Query&& NextOp(OpType op) && { return std::move(NextOp(op)); } [[nodiscard]] OpType NextOp() const noexcept { return nextOp_; } /// Insert open bracket to order logic operations. /// @return Query object. - Query &OpenBracket() & { + Query& OpenBracket() & { entries_.OpenBracket(nextOp_); nextOp_ = OpAnd; return *this; } - [[nodiscard]] Query &&OpenBracket() && { return std::move(OpenBracket()); } + [[nodiscard]] Query&& OpenBracket() && { return std::move(OpenBracket()); } /// Insert close bracket to order logic operations. /// @return Query object. - Query &CloseBracket() & { + Query& CloseBracket() & { entries_.CloseBracket(); return *this; } - [[nodiscard]] Query &&CloseBracket() && { return std::move(CloseBracket()); } + [[nodiscard]] Query&& CloseBracket() && { return std::move(CloseBracket()); } /// Sets the limit of selected rows. /// Analog to sql LIMIT rowsNumber. /// @param limit - number of rows to get from result set. /// @return Query object. - Query &Limit(unsigned limit) & noexcept { + Query& Limit(unsigned limit) & noexcept { count_ = limit; return *this; } - [[nodiscard]] Query &&Limit(unsigned limit) && noexcept { return std::move(Limit(limit)); } + [[nodiscard]] Query&& Limit(unsigned limit) && noexcept { return std::move(Limit(limit)); } /// Sets the number of the first selected row from result query. /// Analog to sql LIMIT OFFSET. /// @param offset - index of the first row to get from result set. /// @return Query object. - Query &Offset(unsigned offset) & noexcept { + Query& Offset(unsigned offset) & noexcept { start_ = offset; return *this; } - [[nodiscard]] Query &&Offset(unsigned offset) && noexcept { return std::move(Offset(offset)); } + [[nodiscard]] Query&& Offset(unsigned offset) && noexcept { return std::move(Offset(offset)); } /// Set the total count calculation mode to Accurate /// @return Query object - Query &ReqTotal() & noexcept { + Query& ReqTotal() & noexcept { calcTotal_ = ModeAccurateTotal; return *this; } - [[nodiscard]] Query &&ReqTotal() && noexcept { return std::move(ReqTotal()); } + [[nodiscard]] Query&& ReqTotal() && noexcept { return std::move(ReqTotal()); } /// Set the total count calculation mode to Cached. /// It will be use LRUCache for total count result /// @return Query object - Query &CachedTotal() & noexcept { + Query& CachedTotal() & noexcept { calcTotal_ = ModeCachedTotal; return *this; } - [[nodiscard]] Query &&CachedTotal() && noexcept { return std::move(CachedTotal()); } + [[nodiscard]] Query&& CachedTotal() && noexcept { return std::move(CachedTotal()); } /// Output fulltext rank /// Allowed only with fulltext query /// @return Query object - Query &WithRank() & noexcept { + Query& WithRank() & noexcept { withRank_ = true; return *this; } - [[nodiscard]] Query &&WithRank() && noexcept { return std::move(WithRank()); } + [[nodiscard]] Query&& WithRank() && noexcept { return std::move(WithRank()); } [[nodiscard]] bool IsWithRank() const noexcept { return withRank_; } /// Can we add aggregation functions @@ -809,23 +829,23 @@ class Query { /// Serializes query data to stream. /// @param ser - serializer object for write. /// @param mode - serialization mode. - void Serialize(WrSerializer &ser, uint8_t mode = Normal) const; + void Serialize(WrSerializer& ser, uint8_t mode = Normal) const; /// Deserializes query data from stream. /// @param ser - serializer object. - [[nodiscard]] static Query Deserialize(Serializer &ser); + [[nodiscard]] static Query Deserialize(Serializer& ser); - void WalkNested(bool withSelf, bool withMerged, bool withSubQueries, const std::function &visitor) const + void WalkNested(bool withSelf, bool withMerged, bool withSubQueries, const std::function& visitor) const noexcept(noexcept(visitor(std::declval()))); [[nodiscard]] bool HasLimit() const noexcept { return count_ != QueryEntry::kDefaultLimit; } [[nodiscard]] bool HasOffset() const noexcept { return start_ != QueryEntry::kDefaultOffset; } [[nodiscard]] bool IsWALQuery() const noexcept; - [[nodiscard]] const std::vector &UpdateFields() const noexcept { return updateFields_; } + [[nodiscard]] const std::vector& UpdateFields() const noexcept { return updateFields_; } [[nodiscard]] QueryType Type() const noexcept { return type_; } - [[nodiscard]] const std::string &NsName() const & noexcept { return namespace_; } + [[nodiscard]] const std::string& NsName() const& noexcept { return namespace_; } template - void SetNsName(T &&nsName) & noexcept { + void SetNsName(T&& nsName) & noexcept { namespace_ = std::forward(nsName); } [[nodiscard]] unsigned Limit() const noexcept { return count_; } @@ -841,66 +861,68 @@ class Query { std::vector aggregations_; - auto NsName() const && = delete; - [[nodiscard]] const QueryEntries &Entries() const noexcept { return entries_; } + auto NsName() const&& = delete; + [[nodiscard]] const QueryEntries& Entries() const noexcept { return entries_; } template - [[nodiscard]] T &GetUpdatableEntry(size_t i) & noexcept { + [[nodiscard]] T& GetUpdatableEntry(size_t i) & noexcept { return entries_.Get(i); } template - void SetEntry(size_t i, Args &&...args) { + void SetEntry(size_t i, Args&&... args) { entries_.SetValue(i, T{std::forward(args)...}); } - void UpdateField(UpdateEntry &&ue) & { updateFields_.emplace_back(std::move(ue)); } - void SetEqualPositions(EqualPosition_t &&ep) & { entries_.equalPositions.emplace_back(std::move(ep)); } - void SetEqualPositions(size_t bracketPosition, EqualPosition_t &&ep) & { + void UpdateField(UpdateEntry&& ue) & { updateFields_.emplace_back(std::move(ue)); } + void SetEqualPositions(EqualPosition_t&& ep) & { entries_.equalPositions.emplace_back(std::move(ep)); } + void SetEqualPositions(size_t bracketPosition, EqualPosition_t&& ep) & { entries_.Get(bracketPosition).equalPositions.emplace_back(std::move(ep)); } - void Join(JoinedQuery &&) &; + void Join(JoinedQuery&&) &; void ReserveQueryEntries(size_t s) & { entries_.Reserve(s); } template - Query &AppendQueryEntry(OpType op, Args &&...args) & { + Query& AppendQueryEntry(OpType op, Args&&... args) & { entries_.Append(op, std::forward(args)...); return *this; } template - Query &&AppendQueryEntry(OpType op, Args &&...args) && { + Query&& AppendQueryEntry(OpType op, Args&&... args) && { entries_.Append(op, std::forward(args)...); return std::move(*this); } void SetLastOperation(OpType op) & { entries_.SetLastOperation(op); } - [[nodiscard]] const Query &GetSubQuery(size_t i) const & noexcept { return subQueries_.at(i); } - [[nodiscard]] const auto &GetSubQueries() const & noexcept { return subQueries_; } - [[nodiscard]] const auto &GetJoinQueries() const & noexcept { return joinQueries_; } - [[nodiscard]] const auto &GetMergeQueries() const & noexcept { return mergeQueries_; } - [[nodiscard]] const auto &SelectFilters() const & noexcept { return selectFilter_; } - void AddJoinQuery(JoinedQuery &&); + [[nodiscard]] const Query& GetSubQuery(size_t i) const& noexcept { return subQueries_.at(i); } + [[nodiscard]] const auto& GetSubQueries() const& noexcept { return subQueries_; } + [[nodiscard]] const auto& GetJoinQueries() const& noexcept { return joinQueries_; } + [[nodiscard]] const auto& GetMergeQueries() const& noexcept { return mergeQueries_; } + [[nodiscard]] const auto& SelectFilters() const& noexcept { return selectFilter_; } + void AddJoinQuery(JoinedQuery&&); void VerifyForUpdate() const; template - size_t InjectConditionsFromOnConditions(size_t position, const h_vector &joinEntries, - const QueryEntries &joinedQueryEntries, size_t joinedQueryNo, - const std::vector> *indexesFrom) { + size_t InjectConditionsFromOnConditions(size_t position, const h_vector& joinEntries, + const QueryEntries& joinedQueryEntries, size_t joinedQueryNo, + const std::vector>* indexesFrom) { return entries_.InjectConditionsFromOnConditions(position, joinEntries, joinedQueryEntries, joinedQueryNo, indexesFrom); } - auto GetSubQuery(size_t) const && = delete; - auto GetSubQueries() const && = delete; - auto GetJoinQueries() const && = delete; - auto GetMergeQueries() const && = delete; - auto SelectFilters() const && = delete; + auto GetSubQuery(size_t) const&& = delete; + auto GetSubQueries() const&& = delete; + auto GetJoinQueries() const&& = delete; + auto GetMergeQueries() const&& = delete; + auto SelectFilters() const&& = delete; private: class [[nodiscard]] PopBackQEGuard { public: - explicit PopBackQEGuard(QueryEntries *e) noexcept : e_{e} {} + explicit PopBackQEGuard(QueryEntries* e) noexcept : e_{e} {} ~PopBackQEGuard() { - if (e_) e_->PopBack(); + if (e_) { + e_->PopBack(); + } } void Reset() noexcept { e_ = nullptr; } private: - QueryEntries *e_; + QueryEntries* e_; }; template @@ -908,28 +930,28 @@ class Query { template class [[nodiscard]] OnHelperGroup { public: - [[nodiscard]] OnHelperGroup &&Not() && noexcept { + [[nodiscard]] OnHelperGroup&& Not() && noexcept { op_ = OpNot; return std::move(*this); } - [[nodiscard]] OnHelperGroup &&Or() && noexcept { + [[nodiscard]] OnHelperGroup&& Or() && noexcept { op_ = OpOr; return std::move(*this); } - [[nodiscard]] OnHelperGroup &&On(std::string index, CondType cond, std::string joinIndex) &&; + [[nodiscard]] OnHelperGroup&& On(std::string index, CondType cond, std::string joinIndex) &&; [[nodiscard]] Q CloseBracket() && noexcept { return std::forward(q_); } private: - OnHelperGroup(Q q, JoinedQuery &jq) noexcept : q_{std::forward(q)}, jq_{jq} {} + OnHelperGroup(Q q, JoinedQuery& jq) noexcept : q_{std::forward(q)}, jq_{jq} {} Q q_; - JoinedQuery &jq_; + JoinedQuery& jq_; OpType op_{OpAnd}; friend class OnHelperTempl; }; template class [[nodiscard]] OnHelperTempl { public: - [[nodiscard]] OnHelperTempl &&Not() && noexcept { + [[nodiscard]] OnHelperTempl&& Not() && noexcept { op_ = OpNot; return std::move(*this); } @@ -937,23 +959,23 @@ class Query { [[nodiscard]] OnHelperGroup OpenBracket() && noexcept { return {std::forward(q_), jq_}; } private: - OnHelperTempl(Q q, JoinedQuery &jq) noexcept : q_{std::forward(q)}, jq_{jq} {} + OnHelperTempl(Q q, JoinedQuery& jq) noexcept : q_{std::forward(q)}, jq_{jq} {} Q q_; - JoinedQuery &jq_; + JoinedQuery& jq_; OpType op_{OpAnd}; friend class Query; }; - using OnHelper = OnHelperTempl; - using OnHelperR = OnHelperTempl; + using OnHelper = OnHelperTempl; + using OnHelperR = OnHelperTempl; - void deserialize(Serializer &ser, bool &hasJoinConditions); - VariantArray deserializeValues(Serializer &, CondType); + void deserialize(Serializer& ser, bool& hasJoinConditions); + VariantArray deserializeValues(Serializer&, CondType); void checkSubQueryNoData() const; void checkSubQueryWithData() const; void checkSubQuery() const; void walkNested(bool withSelf, bool withMerged, bool withSubQueries, - const std::function &visitor) noexcept(noexcept(visitor(std::declval()))); - void adoptNested(Query &nq) const noexcept { nq.Strict(GetStrictMode()).Explain(NeedExplain()).Debug(GetDebugLevel()); } + const std::function& visitor) noexcept(noexcept(visitor(std::declval()))); + void adoptNested(Query& nq) const noexcept { nq.Strict(GetStrictMode()).Explain(NeedExplain()).Debug(GetDebugLevel()); } std::string namespace_; /// Name of the namespace. unsigned start_ = QueryEntry::kDefaultOffset; /// First row index from result set. @@ -974,10 +996,10 @@ class Query { class JoinedQuery : public Query { public: - JoinedQuery(JoinType jt, const Query &q) : Query(q), joinType{jt} {} - JoinedQuery(JoinType jt, Query &&q) : Query(std::move(q)), joinType{jt} {} + JoinedQuery(JoinType jt, const Query& q) : Query(q), joinType{jt} {} + JoinedQuery(JoinType jt, Query&& q) : Query(std::move(q)), joinType{jt} {} using Query::Query; - [[nodiscard]] bool operator==(const JoinedQuery &obj) const; + [[nodiscard]] bool operator==(const JoinedQuery& obj) const; JoinType joinType{JoinType::LeftJoin}; /// Default join type. h_vector joinEntries_; /// Condition for join. Filled in each subqueries, empty in root query @@ -990,28 +1012,28 @@ template } template -[[nodiscard]] Query::OnHelperGroup &&Query::OnHelperGroup::On(std::string index, CondType cond, std::string joinIndex) && { +[[nodiscard]] Query::OnHelperGroup&& Query::OnHelperGroup::On(std::string index, CondType cond, std::string joinIndex) && { jq_.joinEntries_.emplace_back(op_, cond, std::move(index), std::move(joinIndex)); op_ = OpAnd; return std::move(*this); } -[[nodiscard]] inline auto Query::Join(JoinType joinType, Query &&q) & { +[[nodiscard]] inline auto Query::Join(JoinType joinType, Query&& q) & { Join({joinType, std::move(q)}); return OnHelper{*this, joinQueries_.back()}; } -[[nodiscard]] inline auto Query::Join(JoinType joinType, const Query &q) & { +[[nodiscard]] inline auto Query::Join(JoinType joinType, const Query& q) & { Join({joinType, q}); return OnHelper{*this, joinQueries_.back()}; } -[[nodiscard]] inline auto Query::Join(JoinType joinType, Query &&q) && { +[[nodiscard]] inline auto Query::Join(JoinType joinType, Query&& q) && { Join({joinType, std::move(q)}); return OnHelperR{std::move(*this), joinQueries_.back()}; } -[[nodiscard]] inline auto Query::Join(JoinType joinType, const Query &q) && { +[[nodiscard]] inline auto Query::Join(JoinType joinType, const Query& q) && { Join({joinType, q}); return OnHelperR{std::move(*this), joinQueries_.back()}; } diff --git a/cpp_src/core/query/queryentry.cc b/cpp_src/core/query/queryentry.cc index 8a32d917b..6e2d8fcb6 100644 --- a/cpp_src/core/query/queryentry.cc +++ b/cpp_src/core/query/queryentry.cc @@ -13,13 +13,13 @@ namespace reindexer { template -std::string JoinQueryEntry::Dump(const std::vector &joinedSelectors) const { +std::string JoinQueryEntry::Dump(const std::vector& joinedSelectors) const { WrSerializer ser; - const auto &js = joinedSelectors.at(joinIndex); - const auto &q = js.JoinQuery(); + const auto& js = joinedSelectors.at(joinIndex); + const auto& q = js.JoinQuery(); ser << js.Type() << " (" << q.GetSQL() << ") ON "; ser << '('; - for (const auto &jqe : q.joinEntries_) { + for (const auto& jqe : q.joinEntries_) { if (&jqe != &q.joinEntries_.front()) { ser << ' ' << jqe.Operation() << ' '; } else { @@ -30,16 +30,16 @@ std::string JoinQueryEntry::Dump(const std::vector &joinedSelectors) const { ser << ')'; return std::string{ser.Slice()}; } -template std::string JoinQueryEntry::Dump(const JoinedSelectors &) const; -template std::string JoinQueryEntry::Dump(const std::vector &) const; +template std::string JoinQueryEntry::Dump(const JoinedSelectors&) const; +template std::string JoinQueryEntry::Dump(const std::vector&) const; template -std::string JoinQueryEntry::DumpOnCondition(const std::vector &joinedSelectors) const { +std::string JoinQueryEntry::DumpOnCondition(const std::vector& joinedSelectors) const { WrSerializer ser; - const auto &js = joinedSelectors.at(joinIndex); - const auto &q = js.JoinQuery(); + const auto& js = joinedSelectors.at(joinIndex); + const auto& q = js.JoinQuery(); ser << js.Type() << " ON ("; - for (const auto &jqe : q.joinEntries_) { + for (const auto& jqe : q.joinEntries_) { if (&jqe != &q.joinEntries_.front()) { ser << ' ' << jqe.Operation() << ' '; } @@ -48,9 +48,9 @@ std::string JoinQueryEntry::DumpOnCondition(const std::vector &joinedSelecto ser << ')'; return std::string{ser.Slice()}; } -template std::string JoinQueryEntry::DumpOnCondition(const JoinedSelectors &) const; +template std::string JoinQueryEntry::DumpOnCondition(const JoinedSelectors&) const; -bool QueryField::operator==(const QueryField &other) const noexcept { +bool QueryField::operator==(const QueryField& other) const noexcept { if (fieldName_ != other.fieldName_ || idxNo_ != other.idxNo_ || fieldsSet_ != other.fieldsSet_ || !fieldType_.IsSame(other.fieldType_) || !selectType_.IsSame(other.selectType_) || compositeFieldsTypes_.size() != other.compositeFieldsTypes_.size()) { @@ -58,10 +58,10 @@ bool QueryField::operator==(const QueryField &other) const noexcept { } return std::equal( compositeFieldsTypes_.begin(), compositeFieldsTypes_.end(), other.compositeFieldsTypes_.begin(), - [](const CompositeTypesVecT::value_type &l, const CompositeTypesVecT::value_type &r) noexcept { return l.IsSame(r); }); + [](const CompositeTypesVecT::value_type& l, const CompositeTypesVecT::value_type& r) noexcept { return l.IsSame(r); }); } -void QueryField::SetField(FieldsSet &&fields) & { +void QueryField::SetField(FieldsSet&& fields) & { assertrx_throw(fields.size() == 1); assertrx_throw(fields[0] == IndexValueType::SetByJsonPath); assertrx_throw(idxNo_ == IndexValueType::NotSet); @@ -69,8 +69,8 @@ void QueryField::SetField(FieldsSet &&fields) & { fieldsSet_ = std::move(fields); } -static void checkIndexData([[maybe_unused]] int idxNo, [[maybe_unused]] const FieldsSet &fields, KeyValueType fieldType, - [[maybe_unused]] const QueryField::CompositeTypesVecT &compositeFieldsTypes) { +static void checkIndexData([[maybe_unused]] int idxNo, [[maybe_unused]] const FieldsSet& fields, KeyValueType fieldType, + [[maybe_unused]] const QueryField::CompositeTypesVecT& compositeFieldsTypes) { assertrx_throw(idxNo >= 0); if (fieldType.Is()) { assertrx_throw(fields.size() == compositeFieldsTypes.size()); @@ -80,8 +80,8 @@ static void checkIndexData([[maybe_unused]] int idxNo, [[maybe_unused]] const Fi } } -void QueryField::SetIndexData(int idxNo, FieldsSet &&fields, KeyValueType fieldType, KeyValueType selectType, - QueryField::CompositeTypesVecT &&compositeFieldsTypes) & { +void QueryField::SetIndexData(int idxNo, FieldsSet&& fields, KeyValueType fieldType, KeyValueType selectType, + QueryField::CompositeTypesVecT&& compositeFieldsTypes) & { checkIndexData(idxNo, fields, fieldType, compositeFieldsTypes); idxNo_ = idxNo; fieldsSet_ = std::move(fields); @@ -103,13 +103,13 @@ bool QueryField::HaveEmptyField() const noexcept { return Fields().empty(); } -bool QueryEntry::operator==(const QueryEntry &other) const noexcept { +bool QueryEntry::operator==(const QueryEntry& other) const noexcept { return QueryField::operator==(other) && condition_ == other.condition_ && distinct_ == other.distinct_ && values_.RelaxCompare(other.values_) == ComparationResult::Eq; } template -void VerifyQueryEntryValues(CondType cond, const VariantArray &values) { +void VerifyQueryEntryValues(CondType cond, const VariantArray& values) { if constexpr (flags & VerifyQueryEntryFlags::ignoreEmptyValues) { if (values.empty()) { return; @@ -152,8 +152,8 @@ void VerifyQueryEntryValues(CondType cond, const VariantArray &values) { break; } } -template void VerifyQueryEntryValues(CondType, const VariantArray &); -template void VerifyQueryEntryValues(CondType, const VariantArray &); +template void VerifyQueryEntryValues(CondType, const VariantArray&); +template void VerifyQueryEntryValues(CondType, const VariantArray&); std::string QueryEntry::Dump() const { WrSerializer ser; @@ -162,12 +162,18 @@ std::string QueryEntry::Dump() const { } ser << FieldName() << ' ' << condition_ << ' '; const bool severalValues = (Values().size() > 1); - if (severalValues) ser << '('; - for (auto &v : Values()) { - if (&v != &*Values().begin()) ser << ','; + if (severalValues) { + ser << '('; + } + for (auto& v : Values()) { + if (&v != &*Values().begin()) { + ser << ','; + } ser << '\'' << v.As() << '\''; } - if (severalValues) ser << ')'; + if (severalValues) { + ser << ')'; + } return std::string{ser.Slice()}; } @@ -185,7 +191,7 @@ std::string QueryEntry::DumpBrief() const { return std::string(ser.Slice()); } -AggregateEntry::AggregateEntry(AggType type, h_vector &&fields, SortingEntries &&sort, unsigned limit, unsigned offset) +AggregateEntry::AggregateEntry(AggType type, h_vector&& fields, SortingEntries&& sort, unsigned limit, unsigned offset) : type_(type), fields_(std::move(fields)), sortingEntries_{std::move(sort)}, limit_(limit), offset_(offset) { switch (type_) { case AggFacet: @@ -233,7 +239,7 @@ AggregateEntry::AggregateEntry(AggType type, h_vector &&fields, } } -void AggregateEntry::AddSortingEntry(SortingEntry &&sorting) { +void AggregateEntry::AddSortingEntry(SortingEntry&& sorting) { if (type_ != AggFacet) { throw Error(errQueryExec, "Sort is not available for aggregation %s", AggTypeToStr(type_)); } @@ -254,7 +260,7 @@ void AggregateEntry::SetOffset(unsigned o) { offset_ = o; } -bool BetweenFieldsQueryEntry::operator==(const BetweenFieldsQueryEntry &other) const noexcept { +bool BetweenFieldsQueryEntry::operator==(const BetweenFieldsQueryEntry& other) const noexcept { return leftField_ == other.leftField_ && rightField_ == other.rightField_ && Condition() == other.Condition(); } @@ -264,7 +270,7 @@ std::string BetweenFieldsQueryEntry::Dump() const { return std::string{ser.Slice()}; } -void QueryEntries::serialize(CondType cond, const VariantArray &values, WrSerializer &ser) { +void QueryEntries::serialize(CondType cond, const VariantArray& values, WrSerializer& ser) { ser.PutVarUint(cond); if (cond == CondDWithin) { assertrx_throw(values.size() == 2); @@ -282,15 +288,17 @@ void QueryEntries::serialize(CondType cond, const VariantArray &values, WrSerial } } else { ser.PutVarUint(values.size()); - for (auto &kv : values) ser.PutVariant(kv); + for (auto& kv : values) { + ser.PutVariant(kv); + } } } -void QueryEntries::serialize(const_iterator it, const_iterator to, WrSerializer &ser, const std::vector &subQueries) { +void QueryEntries::serialize(const_iterator it, const_iterator to, WrSerializer& ser, const std::vector& subQueries) { for (; it != to; ++it) { const OpType op = it->operation; it->Visit( - [&ser, op, &subQueries](const SubQueryEntry &sqe) { + [&ser, op, &subQueries](const SubQueryEntry& sqe) { ser.PutVarUint(QuerySubQueryCondition); ser.PutVarUint(op); { @@ -299,7 +307,7 @@ void QueryEntries::serialize(const_iterator it, const_iterator to, WrSerializer } serialize(sqe.Condition(), sqe.Values(), ser); }, - [&ser, op, &subQueries](const SubQueryFieldEntry &sqe) { + [&ser, op, &subQueries](const SubQueryFieldEntry& sqe) { ser.PutVarUint(QueryFieldSubQueryCondition); ser.PutVarUint(op); ser.PutVString(sqe.FieldName()); @@ -309,103 +317,113 @@ void QueryEntries::serialize(const_iterator it, const_iterator to, WrSerializer subQueries.at(sqe.QueryIndex()).Serialize(ser); } }, - [&](const QueryEntriesBracket &) { + [&](const QueryEntriesBracket&) { ser.PutVarUint(QueryOpenBracket); ser.PutVarUint(op); serialize(it.cbegin(), it.cend(), ser, subQueries); ser.PutVarUint(QueryCloseBracket); }, - [&ser, op](const QueryEntry &entry) { + [&ser, op](const QueryEntry& entry) { entry.Distinct() ? ser.PutVarUint(QueryDistinct) : ser.PutVarUint(QueryCondition); ser.PutVString(entry.FieldName()); - if (entry.Distinct()) return; + if (entry.Distinct()) { + return; + } ser.PutVarUint(op); serialize(entry.Condition(), entry.Values(), ser); }, - [&ser, op](const JoinQueryEntry &jqe) { + [&ser, op](const JoinQueryEntry& jqe) { ser.PutVarUint(QueryJoinCondition); ser.PutVarUint((op == OpAnd) ? JoinType::InnerJoin : JoinType::OrInnerJoin); ser.PutVarUint(jqe.joinIndex); }, - [&ser, op](const BetweenFieldsQueryEntry &entry) { + [&ser, op](const BetweenFieldsQueryEntry& entry) { ser.PutVarUint(QueryBetweenFieldsCondition); ser.PutVarUint(op); ser.PutVString(entry.LeftFieldName()); ser.PutVarUint(entry.Condition()); ser.PutVString(entry.RightFieldName()); }, - [&ser, op](const AlwaysFalse &) { + [&ser, op](const AlwaysFalse&) { ser.PutVarUint(QueryAlwaysFalseCondition); ser.PutVarUint(op); }, - [&ser, op](const AlwaysTrue &) { + [&ser, op](const AlwaysTrue&) { ser.PutVarUint(QueryAlwaysTrueCondition); ser.PutVarUint(op); }); } } -bool UpdateEntry::operator==(const UpdateEntry &obj) const noexcept { +bool UpdateEntry::operator==(const UpdateEntry& obj) const noexcept { return isExpression_ == obj.isExpression_ && column_ == obj.column_ && mode_ == obj.mode_ && values_ == obj.values_; } -bool QueryJoinEntry::operator==(const QueryJoinEntry &other) const noexcept { +bool QueryJoinEntry::operator==(const QueryJoinEntry& other) const noexcept { return op_ == other.op_ && condition_ == other.condition_ && leftField_ == other.leftField_ && rightField_ == other.rightField_; } -bool AggregateEntry::operator==(const AggregateEntry &obj) const noexcept { +bool AggregateEntry::operator==(const AggregateEntry& obj) const noexcept { return fields_ == obj.fields_ && type_ == obj.type_ && sortingEntries_ == obj.sortingEntries_ && limit_ == obj.limit_ && offset_ == obj.offset_; } -bool SortingEntry::operator==(const SortingEntry &obj) const noexcept { - if (expression != obj.expression) return false; - if (desc != obj.desc) return false; - if (index != obj.index) return false; +bool SortingEntry::operator==(const SortingEntry& obj) const noexcept { + if (expression != obj.expression) { + return false; + } + if (desc != obj.desc) { + return false; + } + if (index != obj.index) { + return false; + } return true; } -bool QueryEntries::checkIfSatisfyConditions(const_iterator begin, const_iterator end, const ConstPayload &pl) { +bool QueryEntries::checkIfSatisfyConditions(const_iterator begin, const_iterator end, const ConstPayload& pl) { assertrx_dbg(begin != end && begin->operation != OpOr); bool result = true; for (auto it = begin; it != end; ++it) { if (it->operation == OpOr) { - if (result) continue; + if (result) { + continue; + } } else if (!result) { break; } const bool lastResult = it->Visit( - [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryEntry &) RX_POST_LMBD_ALWAYS_INLINE -> bool { + [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE -> bool { assertrx_throw(0); abort(); }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryFieldEntry &) RX_POST_LMBD_ALWAYS_INLINE -> bool { + [] RX_PRE_LMBD_ALWAYS_INLINE(const SubQueryFieldEntry&) RX_POST_LMBD_ALWAYS_INLINE -> bool { assertrx_throw(0); abort(); }, - [&it, &pl] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntriesBracket &) + [&it, &pl] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntriesBracket&) RX_POST_LMBD_ALWAYS_INLINE { return checkIfSatisfyConditions(it.cbegin(), it.cend(), pl); }, - [&pl] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntry &qe) RX_POST_LMBD_ALWAYS_INLINE { return checkIfSatisfyCondition(qe, pl); }, - [&pl] RX_PRE_LMBD_ALWAYS_INLINE(const BetweenFieldsQueryEntry &qe) + [&pl] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntry& qe) RX_POST_LMBD_ALWAYS_INLINE { return checkIfSatisfyCondition(qe, pl); }, + [&pl] RX_PRE_LMBD_ALWAYS_INLINE(const BetweenFieldsQueryEntry& qe) RX_POST_LMBD_ALWAYS_INLINE { return checkIfSatisfyCondition(qe, pl); }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const JoinQueryEntry &) RX_POST_LMBD_ALWAYS_INLINE -> bool { + [] RX_PRE_LMBD_ALWAYS_INLINE(const JoinQueryEntry&) RX_POST_LMBD_ALWAYS_INLINE -> bool { assertrx_throw(0); abort(); }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysFalse &) RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, - [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysTrue &) RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; }); + [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysFalse&) RX_POST_LMBD_ALWAYS_INLINE noexcept { return false; }, + [] RX_PRE_LMBD_ALWAYS_INLINE(const AlwaysTrue&) RX_POST_LMBD_ALWAYS_INLINE noexcept { return true; }); result = (lastResult != (it->operation == OpNot)); } return result; } -bool QueryEntries::checkIfSatisfyCondition(const QueryEntry &qEntry, const ConstPayload &pl) { +bool QueryEntries::checkIfSatisfyCondition(const QueryEntry& qEntry, const ConstPayload& pl) { VariantArray values; pl.GetByFieldsSet(qEntry.Fields(), values, qEntry.FieldType(), qEntry.CompositeFieldsTypes()); return CheckIfSatisfyCondition(values, qEntry.Condition(), qEntry.Values()); } -bool QueryEntries::checkIfSatisfyCondition(const BetweenFieldsQueryEntry &qEntry, const ConstPayload &pl) { +bool QueryEntries::checkIfSatisfyCondition(const BetweenFieldsQueryEntry& qEntry, const ConstPayload& pl) { VariantArray lValues; pl.GetByFieldsSet(qEntry.LeftFields(), lValues, qEntry.LeftFieldType(), qEntry.LeftCompositeFieldsTypes()); VariantArray rValues; @@ -413,7 +431,7 @@ bool QueryEntries::checkIfSatisfyCondition(const BetweenFieldsQueryEntry &qEntry return CheckIfSatisfyCondition(lValues, qEntry.Condition(), rValues); } -bool QueryEntries::CheckIfSatisfyCondition(const VariantArray &lValues, CondType condition, const VariantArray &rValues) { +bool QueryEntries::CheckIfSatisfyCondition(const VariantArray& lValues, CondType condition, const VariantArray& rValues) { switch (condition) { case CondType::CondAny: return !lValues.empty(); @@ -421,63 +439,82 @@ bool QueryEntries::CheckIfSatisfyCondition(const VariantArray &lValues, CondType return lValues.empty(); case CondType::CondEq: case CondType::CondSet: - for (const auto &lhs : lValues) { - for (const auto &rhs : rValues) { - if (lhs.RelaxCompare(rhs) == ComparationResult::Eq) return true; + for (const auto& lhs : lValues) { + for (const auto& rhs : rValues) { + if (lhs.RelaxCompare(rhs) == ComparationResult::Eq) { + return true; + } } } return false; case CondType::CondAllSet: - if (lValues.size() < rValues.size()) return false; - for (const auto &v : rValues) { + if (lValues.size() < rValues.size()) { + return false; + } + for (const auto& v : rValues) { auto it = lValues.cbegin(); for (; it != lValues.cend(); ++it) { - if (it->RelaxCompare(v) == ComparationResult::Eq) break; + if (it->RelaxCompare(v) == ComparationResult::Eq) { + break; + } + } + if (it == lValues.cend()) { + return false; } - if (it == lValues.cend()) return false; } return true; case CondType::CondLt: - for (const auto &lhs : lValues) { - for (const auto &rhs : rValues) { - if (lhs.RelaxCompare(rhs) == ComparationResult::Lt) return true; + for (const auto& lhs : lValues) { + for (const auto& rhs : rValues) { + if (lhs.RelaxCompare(rhs) == ComparationResult::Lt) { + return true; + } } } return false; case CondType::CondLe: - for (const auto &lhs : lValues) { - for (const auto &rhs : rValues) { - if (lhs.RelaxCompare(rhs) & ComparationResult::Le) return true; + for (const auto& lhs : lValues) { + for (const auto& rhs : rValues) { + if (lhs.RelaxCompare(rhs) & ComparationResult::Le) { + return true; + } } } return false; case CondType::CondGt: - for (const auto &lhs : lValues) { - for (const auto &rhs : rValues) { - if (lhs.RelaxCompare(rhs) == ComparationResult::Gt) return true; + for (const auto& lhs : lValues) { + for (const auto& rhs : rValues) { + if (lhs.RelaxCompare(rhs) == ComparationResult::Gt) { + return true; + } } } return false; case CondType::CondGe: - for (const auto &lhs : lValues) { - for (const auto &rhs : rValues) { - if (lhs.RelaxCompare(rhs) & ComparationResult::Ge) return true; + for (const auto& lhs : lValues) { + for (const auto& rhs : rValues) { + if (lhs.RelaxCompare(rhs) & ComparationResult::Ge) { + return true; + } } } return false; case CondType::CondRange: - for (const auto &v : lValues) { + for (const auto& v : lValues) { if (v.RelaxCompare(rValues[0]) == ComparationResult::Lt || - v.RelaxCompare(rValues[1]) == ComparationResult::Gt) + v.RelaxCompare(rValues[1]) == ComparationResult::Gt) { return false; + } } return true; case CondType::CondLike: - for (const auto &v : lValues) { + for (const auto& v : lValues) { if (!v.Type().Is()) { throw Error(errLogic, "Condition LIKE must be applied to data of string type, but %s was provided", v.Type().Name()); } - if (matchLikePattern(std::string_view(v), std::string_view(rValues[0]))) return true; + if (matchLikePattern(std::string_view(v), std::string_view(rValues[0]))) { + return true; + } } return false; case CondType::CondDWithin: { @@ -499,12 +536,12 @@ bool QueryEntries::CheckIfSatisfyCondition(const VariantArray &lValues, CondType } template -size_t QueryEntries::InjectConditionsFromOnConditions(size_t position, const h_vector &joinEntries, - const QueryEntries &joinedQueryEntries, size_t joinedQueryNo, - const std::vector> *indexesFrom) { +size_t QueryEntries::InjectConditionsFromOnConditions(size_t position, const h_vector& joinEntries, + const QueryEntries& joinedQueryEntries, size_t joinedQueryNo, + const std::vector>* indexesFrom) { size_t injectedCount = 0; for (size_t i = 0, s = joinEntries.size(); i < s; ++i) { - const QueryJoinEntry &jEntry = joinEntries[i]; + const QueryJoinEntry& jEntry = joinEntries[i]; if (i + 1 < s && joinEntries[i + 1].Operation() == OpOr) { continue; } @@ -546,16 +583,16 @@ size_t QueryEntries::InjectConditionsFromOnConditions(size_t position, const h_v } return injectedCount; } -template size_t QueryEntries::InjectConditionsFromOnConditions(size_t, const h_vector &, - const QueryEntries &, size_t, - const std::vector> *); -template size_t QueryEntries::InjectConditionsFromOnConditions(size_t, const h_vector &, - const QueryEntries &, size_t, - const std::vector> *); +template size_t QueryEntries::InjectConditionsFromOnConditions(size_t, const h_vector&, + const QueryEntries&, size_t, + const std::vector>*); +template size_t QueryEntries::InjectConditionsFromOnConditions(size_t, const h_vector&, + const QueryEntries&, size_t, + const std::vector>*); -size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std::string &fieldName, const std::string &joinedFieldName, - CondType condition, const QueryEntries &joinedQueryEntries, size_t injectedFrom, - size_t injectingInto, const std::vector> *indexesFrom) { +size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std::string& fieldName, const std::string& joinedFieldName, + CondType condition, const QueryEntries& joinedQueryEntries, size_t injectedFrom, + size_t injectingInto, const std::vector>* indexesFrom) { switch (condition) { case CondEq: case CondSet: @@ -581,7 +618,7 @@ size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std: if (!joinedQueryEntries.Is(j)) { continue; } - const QueryEntry &qe = joinedQueryEntries.Get(j); + const QueryEntry& qe = joinedQueryEntries.Get(j); if (qe.IsInjectedFrom(injectingInto)) { continue; } @@ -670,7 +707,7 @@ size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std: if (!qe.IsFieldIndexed() || !indexesFrom) { // TODO relax compare for not indexed after !1438 merge break; } - const CollateOpts &collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; + const CollateOpts& collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; Emplace(position, OpAnd, fieldName, CondLt, VariantArray{*std::max_element(qe.Values().begin(), qe.Values().end(), Variant::Less{collate})}, injectedFrom); @@ -705,7 +742,7 @@ size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std: if (!qe.IsFieldIndexed() || !indexesFrom) { // TODO relax compare for not indexed after !1438 merge break; } - const CollateOpts &collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; + const CollateOpts& collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; Emplace(position, OpAnd, fieldName, CondLe, VariantArray{*std::max_element(qe.Values().begin(), qe.Values().end(), Variant::Less{collate})}, injectedFrom); @@ -744,7 +781,7 @@ size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std: if (!qe.IsFieldIndexed() || !indexesFrom) { // TODO relax compare for not indexed after !1438 merge break; } - const CollateOpts &collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; + const CollateOpts& collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; Emplace(position, OpAnd, fieldName, CondGt, VariantArray{*std::min_element(qe.Values().begin(), qe.Values().end(), Variant::Less{collate})}, injectedFrom); @@ -779,7 +816,7 @@ size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std: if (!qe.IsFieldIndexed() || !indexesFrom) { // TODO relax compare for not indexed after !1438 merge break; } - const CollateOpts &collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; + const CollateOpts& collate = (*indexesFrom)[qe.IndexNo()]->Opts().collateOpts_; Emplace(position, OpAnd, fieldName, CondGe, VariantArray{*std::min_element(qe.Values().begin(), qe.Values().end(), Variant::Less{collate})}, injectedFrom); @@ -822,52 +859,60 @@ size_t QueryEntries::injectConditionsFromOnCondition(size_t position, const std: } template -std::string QueryJoinEntry::DumpCondition(const JS &joinedSelector, bool needOp) const { +std::string QueryJoinEntry::DumpCondition(const JS& joinedSelector, bool needOp) const { WrSerializer ser; - const auto &q = joinedSelector.JoinQuery(); + const auto& q = joinedSelector.JoinQuery(); if (needOp) { ser << ' ' << op_ << ' '; } ser << q.NsName() << '.' << RightFieldName() << ' ' << InvertJoinCondition(condition_) << ' ' << LeftFieldName(); return std::string{ser.Slice()}; } -template std::string QueryJoinEntry::DumpCondition(const JoinedSelector &, bool) const; +template std::string QueryJoinEntry::DumpCondition(const JoinedSelector&, bool) const; -void QueryEntries::dumpEqualPositions(size_t level, WrSerializer &ser, const EqualPositions_t &equalPositions) { - for (const auto &eq : equalPositions) { +void QueryEntries::dumpEqualPositions(size_t level, WrSerializer& ser, const EqualPositions_t& equalPositions) { + for (const auto& eq : equalPositions) { for (size_t i = 0; i < level; ++i) { ser << " "; } ser << "equal_positions("; for (size_t i = 0, s = eq.size(); i < s; ++i) { - if (i != 0) ser << ", "; + if (i != 0) { + ser << ", "; + } ser << eq[i]; } ser << ")\n"; } } -std::string SubQueryEntry::Dump(const std::vector &subQueries) const { +std::string SubQueryEntry::Dump(const std::vector& subQueries) const { std::stringstream ss; ss << '(' << subQueries.at(QueryIndex()).GetSQL() << ") " << Condition() << ' '; - if (Values().size() > 1) ss << '['; + if (Values().size() > 1) { + ss << '['; + } for (size_t i = 0, s = Values().size(); i != s; ++i) { - if (i != 0) ss << ','; + if (i != 0) { + ss << ','; + } ss << '\'' << Values()[i].As() << '\''; } - if (Values().size() > 1) ss << ']'; + if (Values().size() > 1) { + ss << ']'; + } return ss.str(); } -std::string SubQueryFieldEntry::Dump(const std::vector &subQueries) const { +std::string SubQueryFieldEntry::Dump(const std::vector& subQueries) const { std::stringstream ss; ss << FieldName() << ' ' << Condition() << " (" << subQueries.at(QueryIndex()).GetSQL() << ')'; return ss.str(); } template -void QueryEntries::dump(size_t level, const_iterator begin, const_iterator end, const std::vector &joinedSelectors, - const std::vector &subQueries, WrSerializer &ser) { +void QueryEntries::dump(size_t level, const_iterator begin, const_iterator end, const std::vector& joinedSelectors, + const std::vector& subQueries, WrSerializer& ser) { for (const_iterator it = begin; it != end; ++it) { for (size_t i = 0; i < level; ++i) { ser << " "; @@ -875,9 +920,9 @@ void QueryEntries::dump(size_t level, const_iterator begin, const_iterator end, if (it != begin || it->operation != OpAnd) { ser << it->operation << ' '; } - it->Visit([&ser, subQueries](const SubQueryEntry &sqe) { ser << sqe.Dump(subQueries); }, - [&ser, subQueries](const SubQueryFieldEntry &sqe) { ser << sqe.Dump(subQueries); }, - [&](const QueryEntriesBracket &b) { + it->Visit([&ser, subQueries](const SubQueryEntry& sqe) { ser << sqe.Dump(subQueries); }, + [&ser, subQueries](const SubQueryFieldEntry& sqe) { ser << sqe.Dump(subQueries); }, + [&](const QueryEntriesBracket& b) { ser << "(\n"; dump(level + 1, it.cbegin(), it.cend(), joinedSelectors, subQueries, ser); dumpEqualPositions(level + 1, ser, b.equalPositions); @@ -886,15 +931,15 @@ void QueryEntries::dump(size_t level, const_iterator begin, const_iterator end, } ser << ")\n"; }, - [&ser](const QueryEntry &qe) { ser << qe.Dump() << '\n'; }, - [&joinedSelectors, &ser](const JoinQueryEntry &jqe) { ser << jqe.Dump(joinedSelectors) << '\n'; }, - [&ser](const BetweenFieldsQueryEntry &qe) { ser << qe.Dump() << '\n'; }, - [&ser](const AlwaysFalse &) { ser << "AlwaysFalse\n"; }, [&ser](const AlwaysTrue &) { ser << "AlwaysTrue\n"; }); + [&ser](const QueryEntry& qe) { ser << qe.Dump() << '\n'; }, + [&joinedSelectors, &ser](const JoinQueryEntry& jqe) { ser << jqe.Dump(joinedSelectors) << '\n'; }, + [&ser](const BetweenFieldsQueryEntry& qe) { ser << qe.Dump() << '\n'; }, + [&ser](const AlwaysFalse&) { ser << "AlwaysFalse\n"; }, [&ser](const AlwaysTrue&) { ser << "AlwaysTrue\n"; }); } } -template void QueryEntries::dump(size_t, const_iterator, const_iterator, const std::vector &, const std::vector &, - WrSerializer &); -template void QueryEntries::dump(size_t, const_iterator, const_iterator, const std::vector &, - const std::vector &, WrSerializer &); +template void QueryEntries::dump(size_t, const_iterator, const_iterator, const std::vector&, const std::vector&, + WrSerializer&); +template void QueryEntries::dump(size_t, const_iterator, const_iterator, const std::vector&, const std::vector&, + WrSerializer&); } // namespace reindexer diff --git a/cpp_src/core/query/queryentry.h b/cpp_src/core/query/queryentry.h index a18b4dc1e..e8c7969fe 100644 --- a/cpp_src/core/query/queryentry.h +++ b/cpp_src/core/query/queryentry.h @@ -22,14 +22,14 @@ class TagsMatcher; struct JoinQueryEntry { JoinQueryEntry(size_t joinIdx) noexcept : joinIndex{joinIdx} {} size_t joinIndex; - bool operator==(const JoinQueryEntry &other) const noexcept { return joinIndex == other.joinIndex; } - bool operator!=(const JoinQueryEntry &other) const noexcept { return !operator==(other); } + bool operator==(const JoinQueryEntry& other) const noexcept { return joinIndex == other.joinIndex; } + bool operator!=(const JoinQueryEntry& other) const noexcept { return !operator==(other); } template - std::string Dump(const std::vector &joinedSelectors) const; + std::string Dump(const std::vector& joinedSelectors) const; template - std::string DumpOnCondition(const std::vector &joinedSelectors) const; + std::string DumpOnCondition(const std::vector& joinedSelectors) const; }; class QueryField { @@ -37,33 +37,33 @@ class QueryField { using CompositeTypesVecT = h_vector; template - explicit QueryField(Str &&fieldName) noexcept : fieldName_{std::forward(fieldName)} {} - QueryField(std::string &&fieldName, int idxNo, FieldsSet fields, KeyValueType fieldType, - std::vector &&compositeFieldsTypes); - QueryField(QueryField &&) noexcept = default; - QueryField(const QueryField &) = default; - QueryField &operator=(QueryField &&) noexcept = default; + explicit QueryField(Str&& fieldName) noexcept : fieldName_{std::forward(fieldName)} {} + QueryField(std::string&& fieldName, int idxNo, FieldsSet fields, KeyValueType fieldType, + std::vector&& compositeFieldsTypes); + QueryField(QueryField&&) noexcept = default; + QueryField(const QueryField&) = default; + QueryField& operator=(QueryField&&) noexcept = default; - [[nodiscard]] bool operator==(const QueryField &) const noexcept; - [[nodiscard]] bool operator!=(const QueryField &other) const noexcept { return !operator==(other); } + [[nodiscard]] bool operator==(const QueryField&) const noexcept; + [[nodiscard]] bool operator!=(const QueryField& other) const noexcept { return !operator==(other); } [[nodiscard]] int IndexNo() const noexcept { return idxNo_; } [[nodiscard]] bool IsFieldIndexed() const noexcept { return idxNo_ >= 0; } [[nodiscard]] bool FieldsHaveBeenSet() const noexcept { return idxNo_ != IndexValueType::NotSet; } - [[nodiscard]] const FieldsSet &Fields() const & noexcept { return fieldsSet_; } - [[nodiscard]] const std::string &FieldName() const & noexcept { return fieldName_; } + [[nodiscard]] const FieldsSet& Fields() const& noexcept { return fieldsSet_; } + [[nodiscard]] const std::string& FieldName() const& noexcept { return fieldName_; } [[nodiscard]] KeyValueType FieldType() const noexcept { return fieldType_; } [[nodiscard]] KeyValueType SelectType() const noexcept { return selectType_; } - [[nodiscard]] const CompositeTypesVecT &CompositeFieldsTypes() const & noexcept { return compositeFieldsTypes_; } + [[nodiscard]] const CompositeTypesVecT& CompositeFieldsTypes() const& noexcept { return compositeFieldsTypes_; } [[nodiscard]] bool HaveEmptyField() const noexcept; - void SetField(FieldsSet &&fields) &; - void SetIndexData(int idxNo, FieldsSet &&fields, KeyValueType fieldType, KeyValueType selectType, - CompositeTypesVecT &&compositeFieldsTypes) &; + void SetField(FieldsSet&& fields) &; + void SetIndexData(int idxNo, FieldsSet&& fields, KeyValueType fieldType, KeyValueType selectType, + CompositeTypesVecT&& compositeFieldsTypes) &; - QueryField &operator=(const QueryField &) = delete; - auto Fields() const && = delete; - auto FieldName() const && = delete; - auto CompositeFieldsTypes() const && = delete; + QueryField& operator=(const QueryField&) = delete; + auto Fields() const&& = delete; + auto FieldName() const&& = delete; + auto CompositeFieldsTypes() const&& = delete; private: std::string fieldName_; @@ -81,9 +81,9 @@ enum class VerifyQueryEntryFlags : unsigned { null = 0u, ignoreEmptyValues = 1u } template -void VerifyQueryEntryValues(CondType, const VariantArray &); -extern template void VerifyQueryEntryValues(CondType, const VariantArray &); -extern template void VerifyQueryEntryValues(CondType, const VariantArray &); +void VerifyQueryEntryValues(CondType, const VariantArray&); +extern template void VerifyQueryEntryValues(CondType, const VariantArray&); +extern template void VerifyQueryEntryValues(CondType, const VariantArray&); class QueryEntry : private QueryField { public: @@ -94,25 +94,25 @@ class QueryEntry : private QueryField { static constexpr unsigned kDefaultOffset = 0; template && std::is_constructible_v> * = nullptr> - QueryEntry(Str &&fieldName, CondType cond, VA &&v, size_t injectedFrom = NotInjected) + std::enable_if_t && std::is_constructible_v>* = nullptr> + QueryEntry(Str&& fieldName, CondType cond, VA&& v, size_t injectedFrom = NotInjected) : QueryField{std::forward(fieldName)}, values_{std::forward(v)}, condition_{cond}, injectedFrom_{injectedFrom} { Verify(); } template - QueryEntry(Str &&fieldName, DistinctTag) : QueryField{std::forward(fieldName)}, condition_{CondAny}, distinct_{true} { + QueryEntry(Str&& fieldName, DistinctTag) : QueryField{std::forward(fieldName)}, condition_{CondAny}, distinct_{true} { Verify(); } - template > * = nullptr> - QueryEntry(QueryField &&field, CondType cond, VA &&v) : QueryField{std::move(field)}, values_{std::forward(v)}, condition_{cond} { + template >* = nullptr> + QueryEntry(QueryField&& field, CondType cond, VA&& v) : QueryField{std::move(field)}, values_{std::forward(v)}, condition_{cond} { Verify(); } - QueryEntry(QueryField &&field, CondType cond, IgnoreEmptyValues) : QueryField{std::move(field)}, condition_{cond} { + QueryEntry(QueryField&& field, CondType cond, IgnoreEmptyValues) : QueryField{std::move(field)}, condition_{cond} { verifyIgnoringEmptyValues(); } [[nodiscard]] CondType Condition() const noexcept { return condition_; } - [[nodiscard]] const VariantArray &Values() const & noexcept { return values_; } - [[nodiscard]] VariantArray &&Values() && noexcept { return std::move(values_); } + [[nodiscard]] const VariantArray& Values() const& noexcept { return values_; } + [[nodiscard]] VariantArray&& Values() && noexcept { return std::move(values_); } [[nodiscard]] auto UpdatableValues(IgnoreEmptyValues) & noexcept { return VerifyingUpdater{*this}; } @@ -132,31 +132,31 @@ class QueryEntry : private QueryField { using QueryField::SetField; using QueryField::SetIndexData; using QueryField::HaveEmptyField; - void SetCondAndValues(CondType cond, VariantArray &&values) { + void SetCondAndValues(CondType cond, VariantArray&& values) { VerifyQueryEntryValues(cond, values); condition_ = cond; values_ = std::move(values); } - const QueryField &FieldData() const & noexcept { return static_cast(*this); } - QueryField &FieldData() & noexcept { return static_cast(*this); } + const QueryField& FieldData() const& noexcept { return static_cast(*this); } + QueryField& FieldData() & noexcept { return static_cast(*this); } void ConvertValuesToFieldType() & { - for (Variant &v : values_) { + for (Variant& v : values_) { v.convert(SelectType()); } } - void ConvertValuesToFieldType(const PayloadType &pt) & { + void ConvertValuesToFieldType(const PayloadType& pt) & { if (SelectType().Is() || Condition() == CondDWithin) { return; } - for (Variant &v : values_) { + for (Variant& v : values_) { v.convert(SelectType(), &pt, &Fields()); } } void Verify() const { VerifyQueryEntryValues(condition_, values_); } - [[nodiscard]] bool operator==(const QueryEntry &) const noexcept; - [[nodiscard]] bool operator!=(const QueryEntry &other) const noexcept { return !operator==(other); } + [[nodiscard]] bool operator==(const QueryEntry&) const noexcept; + [[nodiscard]] bool operator!=(const QueryEntry& other) const noexcept { return !operator==(other); } [[nodiscard]] std::string Dump() const; [[nodiscard]] std::string DumpBrief() const; @@ -164,8 +164,8 @@ class QueryEntry : private QueryField { [[nodiscard]] bool IsInjectedFrom(size_t joinedQueryNo) const noexcept { return injectedFrom_ == joinedQueryNo; } void InjectedFrom(size_t joinedQueryNo) noexcept { injectedFrom_ = joinedQueryNo; } - auto Values() const && = delete; - auto FieldData() const && = delete; + auto Values() const&& = delete; + auto FieldData() const&& = delete; private: void verifyIgnoringEmptyValues() const { VerifyQueryEntryValues(condition_, values_); } @@ -180,48 +180,48 @@ class QueryEntry : private QueryField { class BetweenFieldsQueryEntry { public: template - BetweenFieldsQueryEntry(StrL &&fstIdx, CondType cond, StrR &&sndIdx) + BetweenFieldsQueryEntry(StrL&& fstIdx, CondType cond, StrR&& sndIdx) : leftField_{std::forward(fstIdx)}, rightField_{std::forward(sndIdx)}, condition_{cond} { if (condition_ == CondAny || condition_ == CondEmpty || condition_ == CondDWithin) { throw Error{errLogic, "Condition '%s' is inapplicable between two fields", CondTypeToStr(condition_)}; } } - [[nodiscard]] bool operator==(const BetweenFieldsQueryEntry &) const noexcept; - [[nodiscard]] bool operator!=(const BetweenFieldsQueryEntry &other) const noexcept { return !operator==(other); } + [[nodiscard]] bool operator==(const BetweenFieldsQueryEntry&) const noexcept; + [[nodiscard]] bool operator!=(const BetweenFieldsQueryEntry& other) const noexcept { return !operator==(other); } [[nodiscard]] CondType Condition() const noexcept { return condition_; } [[nodiscard]] int LeftIdxNo() const noexcept { return leftField_.IndexNo(); } [[nodiscard]] int RightIdxNo() const noexcept { return rightField_.IndexNo(); } - [[nodiscard]] const std::string &LeftFieldName() const & noexcept { return leftField_.FieldName(); } - [[nodiscard]] const std::string &RightFieldName() const & noexcept { return rightField_.FieldName(); } - [[nodiscard]] const FieldsSet &LeftFields() const & noexcept { return leftField_.Fields(); } - [[nodiscard]] const FieldsSet &RightFields() const & noexcept { return rightField_.Fields(); } + [[nodiscard]] const std::string& LeftFieldName() const& noexcept { return leftField_.FieldName(); } + [[nodiscard]] const std::string& RightFieldName() const& noexcept { return rightField_.FieldName(); } + [[nodiscard]] const FieldsSet& LeftFields() const& noexcept { return leftField_.Fields(); } + [[nodiscard]] const FieldsSet& RightFields() const& noexcept { return rightField_.Fields(); } [[nodiscard]] KeyValueType LeftFieldType() const noexcept { return leftField_.FieldType(); } [[nodiscard]] KeyValueType RightFieldType() const noexcept { return rightField_.FieldType(); } - [[nodiscard]] const QueryField::CompositeTypesVecT &LeftCompositeFieldsTypes() const & noexcept { + [[nodiscard]] const QueryField::CompositeTypesVecT& LeftCompositeFieldsTypes() const& noexcept { return leftField_.CompositeFieldsTypes(); } - [[nodiscard]] const QueryField::CompositeTypesVecT &RightCompositeFieldsTypes() const & noexcept { + [[nodiscard]] const QueryField::CompositeTypesVecT& RightCompositeFieldsTypes() const& noexcept { return rightField_.CompositeFieldsTypes(); } - [[nodiscard]] const QueryField &LeftFieldData() const & noexcept { return leftField_; } - [[nodiscard]] QueryField &LeftFieldData() & noexcept { return leftField_; } - [[nodiscard]] const QueryField &RightFieldData() const & noexcept { return rightField_; } - [[nodiscard]] QueryField &RightFieldData() & noexcept { return rightField_; } + [[nodiscard]] const QueryField& LeftFieldData() const& noexcept { return leftField_; } + [[nodiscard]] QueryField& LeftFieldData() & noexcept { return leftField_; } + [[nodiscard]] const QueryField& RightFieldData() const& noexcept { return rightField_; } + [[nodiscard]] QueryField& RightFieldData() & noexcept { return rightField_; } [[nodiscard]] bool FieldsHaveBeenSet() const noexcept { return leftField_.FieldsHaveBeenSet() && rightField_.FieldsHaveBeenSet(); } [[nodiscard]] bool IsLeftFieldIndexed() const noexcept { return leftField_.IsFieldIndexed(); } [[nodiscard]] bool IsRightFieldIndexed() const noexcept { return rightField_.IsFieldIndexed(); } [[nodiscard]] std::string Dump() const; - auto LeftFieldName() const && = delete; - auto RightFieldName() const && = delete; - auto LeftFields() const && = delete; - auto RightFields() const && = delete; - auto LeftCompositeFieldsTypes() const && = delete; - auto RightCompositeFieldsTypes() const && = delete; - auto LeftFieldData() const && = delete; - auto RightFieldData() const && = delete; + auto LeftFieldName() const&& = delete; + auto RightFieldName() const&& = delete; + auto LeftFields() const&& = delete; + auto RightFields() const&& = delete; + auto LeftCompositeFieldsTypes() const&& = delete; + auto RightCompositeFieldsTypes() const&& = delete; + auto LeftFieldData() const&& = delete; + auto RightFieldData() const&& = delete; private: QueryField leftField_; @@ -241,7 +241,7 @@ using EqualPositions_t = std::vector; struct QueryEntriesBracket : public Bracket { using Bracket::Bracket; - bool operator==(const QueryEntriesBracket &other) const noexcept { + bool operator==(const QueryEntriesBracket& other) const noexcept { return Bracket::operator==(other) && equalPositions == other.equalPositions; } EqualPositions_t equalPositions; @@ -249,19 +249,19 @@ struct QueryEntriesBracket : public Bracket { class SubQueryEntry { public: - SubQueryEntry(CondType cond, size_t qIdx, VariantArray &&values) : condition_{cond}, queryIndex_{qIdx}, values_{std::move(values)} { + SubQueryEntry(CondType cond, size_t qIdx, VariantArray&& values) : condition_{cond}, queryIndex_{qIdx}, values_{std::move(values)} { VerifyQueryEntryValues(condition_, values_); } [[nodiscard]] CondType Condition() const noexcept { return condition_; } [[nodiscard]] size_t QueryIndex() const noexcept { return queryIndex_; } - [[nodiscard]] const VariantArray &Values() const & noexcept { return values_; } - [[nodiscard]] bool operator==(const SubQueryEntry &other) const noexcept { + [[nodiscard]] const VariantArray& Values() const& noexcept { return values_; } + [[nodiscard]] bool operator==(const SubQueryEntry& other) const noexcept { return condition_ == other.condition_ && queryIndex_ == other.queryIndex_; } - [[nodiscard]] bool operator!=(const SubQueryEntry &other) const noexcept { return !operator==(other); } - [[nodiscard]] std::string Dump(const std::vector &subQueries) const; + [[nodiscard]] bool operator!=(const SubQueryEntry& other) const noexcept { return !operator==(other); } + [[nodiscard]] std::string Dump(const std::vector& subQueries) const; - auto Values() const && = delete; + auto Values() const&& = delete; private: CondType condition_; @@ -273,22 +273,22 @@ class SubQueryEntry { class SubQueryFieldEntry { public: template - SubQueryFieldEntry(Str &&field, CondType cond, size_t qIdx) : field_{std::forward(field)}, condition_{cond}, queryIndex_{qIdx} { + SubQueryFieldEntry(Str&& field, CondType cond, size_t qIdx) : field_{std::forward(field)}, condition_{cond}, queryIndex_{qIdx} { if (cond == CondAny || cond == CondEmpty) { throw Error{errQueryExec, "Condition %s with field and subquery", cond == CondAny ? "Any" : "Empty"}; } } - [[nodiscard]] const std::string &FieldName() const & noexcept { return field_; } - [[nodiscard]] std::string &&FieldName() && noexcept { return std::move(field_); } + [[nodiscard]] const std::string& FieldName() const& noexcept { return field_; } + [[nodiscard]] std::string&& FieldName() && noexcept { return std::move(field_); } [[nodiscard]] CondType Condition() const noexcept { return condition_; } [[nodiscard]] size_t QueryIndex() const noexcept { return queryIndex_; } - [[nodiscard]] bool operator==(const SubQueryFieldEntry &other) const noexcept { + [[nodiscard]] bool operator==(const SubQueryFieldEntry& other) const noexcept { return field_ == other.field_ && condition_ == other.condition_ && queryIndex_ == other.queryIndex_; } - [[nodiscard]] bool operator!=(const SubQueryFieldEntry &other) const noexcept { return !operator==(other); } - [[nodiscard]] std::string Dump(const std::vector &subQueries) const; + [[nodiscard]] bool operator!=(const SubQueryFieldEntry& other) const noexcept { return !operator==(other); } + [[nodiscard]] std::string Dump(const std::vector& subQueries) const; - auto FieldName() const && = delete; + auto FieldName() const&& = delete; private: std::string field_; @@ -300,17 +300,17 @@ class SubQueryFieldEntry { class UpdateEntry { public: template - UpdateEntry(Str &&c, VariantArray &&v, FieldModifyMode m = FieldModeSet, bool e = false) + UpdateEntry(Str&& c, VariantArray&& v, FieldModifyMode m = FieldModeSet, bool e = false) : column_(std::forward(c)), values_(std::move(v)), mode_(m), isExpression_(e) { if (column_.empty()) { throw Error{errParams, "Empty update column name"}; } } - bool operator==(const UpdateEntry &) const noexcept; - bool operator!=(const UpdateEntry &obj) const noexcept { return !operator==(obj); } + bool operator==(const UpdateEntry&) const noexcept; + bool operator!=(const UpdateEntry& obj) const noexcept { return !operator==(obj); } std::string_view Column() const noexcept { return column_; } - VariantArray const &Values() const noexcept { return values_; } - VariantArray &Values() noexcept { return values_; } + const VariantArray& Values() const noexcept { return values_; } + VariantArray& Values() noexcept { return values_; } FieldModifyMode Mode() const noexcept { return mode_; } void SetMode(FieldModifyMode m) noexcept { mode_ = m; } bool IsExpression() const noexcept { return isExpression_; } @@ -325,56 +325,56 @@ class UpdateEntry { class QueryJoinEntry { public: - QueryJoinEntry(OpType op, CondType cond, std::string &&leftFld, std::string &&rightFld, bool reverseNs = false) noexcept + QueryJoinEntry(OpType op, CondType cond, std::string&& leftFld, std::string&& rightFld, bool reverseNs = false) noexcept : leftField_{std::move(leftFld)}, rightField_{std::move(rightFld)}, op_{op}, condition_{cond}, reverseNamespacesOrder_{reverseNs} {} - [[nodiscard]] bool operator==(const QueryJoinEntry &) const noexcept; - [[nodiscard]] bool operator!=(const QueryJoinEntry &other) const noexcept { return !operator==(other); } + [[nodiscard]] bool operator==(const QueryJoinEntry&) const noexcept; + [[nodiscard]] bool operator!=(const QueryJoinEntry& other) const noexcept { return !operator==(other); } [[nodiscard]] bool IsLeftFieldIndexed() const noexcept { return leftField_.IsFieldIndexed(); } [[nodiscard]] bool IsRightFieldIndexed() const noexcept { return rightField_.IsFieldIndexed(); } [[nodiscard]] int LeftIdxNo() const noexcept { return leftField_.IndexNo(); } [[nodiscard]] int RightIdxNo() const noexcept { return rightField_.IndexNo(); } - [[nodiscard]] const FieldsSet &LeftFields() const & noexcept { return leftField_.Fields(); } - [[nodiscard]] const FieldsSet &RightFields() const & noexcept { return rightField_.Fields(); } + [[nodiscard]] const FieldsSet& LeftFields() const& noexcept { return leftField_.Fields(); } + [[nodiscard]] const FieldsSet& RightFields() const& noexcept { return rightField_.Fields(); } [[nodiscard]] KeyValueType LeftFieldType() const noexcept { return leftField_.FieldType(); } [[nodiscard]] KeyValueType RightFieldType() const noexcept { return rightField_.FieldType(); } - [[nodiscard]] const QueryField::CompositeTypesVecT &LeftCompositeFieldsTypes() const & noexcept { + [[nodiscard]] const QueryField::CompositeTypesVecT& LeftCompositeFieldsTypes() const& noexcept { return leftField_.CompositeFieldsTypes(); } - [[nodiscard]] const QueryField::CompositeTypesVecT &RightCompositeFieldsTypes() const & noexcept { + [[nodiscard]] const QueryField::CompositeTypesVecT& RightCompositeFieldsTypes() const& noexcept { return rightField_.CompositeFieldsTypes(); } [[nodiscard]] OpType Operation() const noexcept { return op_; } [[nodiscard]] CondType Condition() const noexcept { return condition_; } - [[nodiscard]] const std::string &LeftFieldName() const & noexcept { return leftField_.FieldName(); } - [[nodiscard]] const std::string &RightFieldName() const & noexcept { return rightField_.FieldName(); } + [[nodiscard]] const std::string& LeftFieldName() const& noexcept { return leftField_.FieldName(); } + [[nodiscard]] const std::string& RightFieldName() const& noexcept { return rightField_.FieldName(); } [[nodiscard]] bool ReverseNamespacesOrder() const noexcept { return reverseNamespacesOrder_; } - [[nodiscard]] const QueryField &LeftFieldData() const & noexcept { return leftField_; } - [[nodiscard]] QueryField &LeftFieldData() & noexcept { return leftField_; } - [[nodiscard]] const QueryField &RightFieldData() const & noexcept { return rightField_; } - [[nodiscard]] QueryField &RightFieldData() & noexcept { return rightField_; } - void SetLeftIndexData(int idxNo, FieldsSet &&fields, KeyValueType fieldType, KeyValueType selectType, - QueryField::CompositeTypesVecT &&compositeFieldsTypes) & { + [[nodiscard]] const QueryField& LeftFieldData() const& noexcept { return leftField_; } + [[nodiscard]] QueryField& LeftFieldData() & noexcept { return leftField_; } + [[nodiscard]] const QueryField& RightFieldData() const& noexcept { return rightField_; } + [[nodiscard]] QueryField& RightFieldData() & noexcept { return rightField_; } + void SetLeftIndexData(int idxNo, FieldsSet&& fields, KeyValueType fieldType, KeyValueType selectType, + QueryField::CompositeTypesVecT&& compositeFieldsTypes) & { leftField_.SetIndexData(idxNo, std::move(fields), fieldType, selectType, std::move(compositeFieldsTypes)); } - void SetRightIndexData(int idxNo, FieldsSet &&fields, KeyValueType fieldType, KeyValueType selectType, - QueryField::CompositeTypesVecT &&compositeFieldsTypes) & { + void SetRightIndexData(int idxNo, FieldsSet&& fields, KeyValueType fieldType, KeyValueType selectType, + QueryField::CompositeTypesVecT&& compositeFieldsTypes) & { rightField_.SetIndexData(idxNo, std::move(fields), fieldType, selectType, std::move(compositeFieldsTypes)); } - void SetLeftField(FieldsSet &&fields) & { leftField_.SetField(std::move(fields)); } - void SetRightField(FieldsSet &&fields) & { rightField_.SetField(std::move(fields)); } + void SetLeftField(FieldsSet&& fields) & { leftField_.SetField(std::move(fields)); } + void SetRightField(FieldsSet&& fields) & { rightField_.SetField(std::move(fields)); } [[nodiscard]] bool FieldsHaveBeenSet() const noexcept { return leftField_.FieldsHaveBeenSet() && rightField_.FieldsHaveBeenSet(); } template - std::string DumpCondition(const JS &joinedSelector, bool needOp = false) const; + std::string DumpCondition(const JS& joinedSelector, bool needOp = false) const; - auto LeftFields() const && = delete; - auto RightFields() const && = delete; - auto LeftCompositeFieldsTypes() const && = delete; - auto RightCompositeFieldsTypes() const && = delete; - auto LeftFieldName() const && = delete; - auto RightFieldName() const && = delete; - auto LeftFieldData() const && = delete; - auto RightFieldData() const && = delete; + auto LeftFields() const&& = delete; + auto RightFields() const&& = delete; + auto LeftCompositeFieldsTypes() const&& = delete; + auto RightCompositeFieldsTypes() const&& = delete; + auto LeftFieldName() const&& = delete; + auto RightFieldName() const&& = delete; + auto LeftFieldData() const&& = delete; + auto RightFieldData() const&& = delete; private: QueryField leftField_; @@ -393,25 +393,25 @@ class QueryEntries : public ExpressionTree { using Base = ExpressionTree; - QueryEntries(Base &&b) : Base{std::move(b)} {} + QueryEntries(Base&& b) : Base{std::move(b)} {} public: QueryEntries() = default; - QueryEntries(QueryEntries &&) = default; - QueryEntries(const QueryEntries &) = default; - QueryEntries &operator=(QueryEntries &&) = default; - - void ToDsl(const Query &parentQuery, JsonBuilder &builder) const { return toDsl(cbegin(), cend(), parentQuery, builder); } - void WriteSQLWhere(const Query &parentQuery, WrSerializer &, bool stripArgs) const; - void Serialize(WrSerializer &ser, const std::vector &subQueries) const { serialize(cbegin(), cend(), ser, subQueries); } - bool CheckIfSatisfyConditions(const ConstPayload &pl) const { return checkIfSatisfyConditions(cbegin(), cend(), pl); } - static bool CheckIfSatisfyCondition(const VariantArray &lValues, CondType, const VariantArray &rValues); + QueryEntries(QueryEntries&&) = default; + QueryEntries(const QueryEntries&) = default; + QueryEntries& operator=(QueryEntries&&) = default; + + void ToDsl(const Query& parentQuery, JsonBuilder& builder) const { return toDsl(cbegin(), cend(), parentQuery, builder); } + void WriteSQLWhere(const Query& parentQuery, WrSerializer&, bool stripArgs) const; + void Serialize(WrSerializer& ser, const std::vector& subQueries) const { serialize(cbegin(), cend(), ser, subQueries); } + bool CheckIfSatisfyConditions(const ConstPayload& pl) const { return checkIfSatisfyConditions(cbegin(), cend(), pl); } + static bool CheckIfSatisfyCondition(const VariantArray& lValues, CondType, const VariantArray& rValues); template - [[nodiscard]] size_t InjectConditionsFromOnConditions(size_t position, const h_vector &joinEntries, - const QueryEntries &joinedQueryEntries, size_t joinedQueryNo, - const std::vector> *indexesFrom); + [[nodiscard]] size_t InjectConditionsFromOnConditions(size_t position, const h_vector& joinEntries, + const QueryEntries& joinedQueryEntries, size_t joinedQueryNo, + const std::vector>* indexesFrom); template - std::string Dump(const std::vector &joinedSelectors, const std::vector &subQueries) const { + std::string Dump(const std::vector& joinedSelectors, const std::vector& subQueries) const { WrSerializer ser; dump(0, cbegin(), cend(), joinedSelectors, subQueries, ser); dumpEqualPositions(0, ser, equalPositions); @@ -421,35 +421,35 @@ class QueryEntries : public ExpressionTree &subQueries); - static void serialize(CondType, const VariantArray &values, WrSerializer &); - static bool checkIfSatisfyConditions(const_iterator begin, const_iterator end, const ConstPayload &); - static bool checkIfSatisfyCondition(const QueryEntry &, const ConstPayload &); - static bool checkIfSatisfyCondition(const BetweenFieldsQueryEntry &, const ConstPayload &); - [[nodiscard]] size_t injectConditionsFromOnCondition(size_t position, const std::string &fieldName, const std::string &joinedFieldName, - CondType, const QueryEntries &joinedQueryEntries, size_t injectedFrom, - size_t injectingInto, const std::vector> *indexesFrom); + static void toDsl(const_iterator it, const_iterator to, const Query& parentQuery, JsonBuilder&); + static void writeSQL(const Query& parentQuery, const_iterator from, const_iterator to, WrSerializer&, bool stripArgs); + static void serialize(const_iterator it, const_iterator to, WrSerializer&, const std::vector& subQueries); + static void serialize(CondType, const VariantArray& values, WrSerializer&); + static bool checkIfSatisfyConditions(const_iterator begin, const_iterator end, const ConstPayload&); + static bool checkIfSatisfyCondition(const QueryEntry&, const ConstPayload&); + static bool checkIfSatisfyCondition(const BetweenFieldsQueryEntry&, const ConstPayload&); + [[nodiscard]] size_t injectConditionsFromOnCondition(size_t position, const std::string& fieldName, const std::string& joinedFieldName, + CondType, const QueryEntries& joinedQueryEntries, size_t injectedFrom, + size_t injectingInto, const std::vector>* indexesFrom); protected: - static void dumpEqualPositions(size_t level, WrSerializer &, const EqualPositions_t &); + static void dumpEqualPositions(size_t level, WrSerializer&, const EqualPositions_t&); template - static void dump(size_t level, const_iterator begin, const_iterator end, const std::vector &joinedSelectors, - const std::vector &subQueries, WrSerializer &); + static void dump(size_t level, const_iterator begin, const_iterator end, const std::vector& joinedSelectors, + const std::vector& subQueries, WrSerializer&); }; extern template size_t QueryEntries::InjectConditionsFromOnConditions( - size_t, const h_vector &, const QueryEntries &, size_t, const std::vector> *); + size_t, const h_vector&, const QueryEntries&, size_t, const std::vector>*); extern template size_t QueryEntries::InjectConditionsFromOnConditions( - size_t, const h_vector &, const QueryEntries &, size_t, const std::vector> *); + size_t, const h_vector&, const QueryEntries&, size_t, const std::vector>*); struct SortingEntry { SortingEntry() noexcept = default; template - SortingEntry(Str &&e, bool d) noexcept : expression(std::forward(e)), desc(d) {} - bool operator==(const SortingEntry &) const noexcept; - bool operator!=(const SortingEntry &se) const noexcept { return !operator==(se); } + SortingEntry(Str&& e, bool d) noexcept : expression(std::forward(e)), desc(d) {} + bool operator==(const SortingEntry&) const noexcept; + bool operator!=(const SortingEntry& se) const noexcept { return !operator==(se); } std::string expression; bool desc = false; int index = IndexValueType::NotSet; @@ -459,16 +459,16 @@ struct SortingEntries : public h_vector {}; class AggregateEntry { public: - AggregateEntry(AggType type, h_vector &&fields, SortingEntries &&sort = {}, unsigned limit = QueryEntry::kDefaultLimit, + AggregateEntry(AggType type, h_vector&& fields, SortingEntries&& sort = {}, unsigned limit = QueryEntry::kDefaultLimit, unsigned offset = QueryEntry::kDefaultOffset); - [[nodiscard]] bool operator==(const AggregateEntry &) const noexcept; - [[nodiscard]] bool operator!=(const AggregateEntry &ae) const noexcept { return !operator==(ae); } + [[nodiscard]] bool operator==(const AggregateEntry&) const noexcept; + [[nodiscard]] bool operator!=(const AggregateEntry& ae) const noexcept { return !operator==(ae); } [[nodiscard]] AggType Type() const noexcept { return type_; } - [[nodiscard]] const h_vector &Fields() const noexcept { return fields_; } - [[nodiscard]] const SortingEntries &Sorting() const noexcept { return sortingEntries_; } + [[nodiscard]] const h_vector& Fields() const noexcept { return fields_; } + [[nodiscard]] const SortingEntries& Sorting() const noexcept { return sortingEntries_; } [[nodiscard]] unsigned Limit() const noexcept { return limit_; } [[nodiscard]] unsigned Offset() const noexcept { return offset_; } - void AddSortingEntry(SortingEntry &&); + void AddSortingEntry(SortingEntry&&); void SetLimit(unsigned); void SetOffset(unsigned); diff --git a/cpp_src/core/query/sql/sqlencoder.cc b/cpp_src/core/query/sql/sqlencoder.cc index 9a4b6298a..aabbbf98b 100644 --- a/cpp_src/core/query/sql/sqlencoder.cc +++ b/cpp_src/core/query/sql/sqlencoder.cc @@ -10,7 +10,7 @@ enum class NeedQuote : bool { No = false, Yes = true }; template -static void indexToSql(std::string_view index, reindexer::WrSerializer &ser) { +static void indexToSql(std::string_view index, reindexer::WrSerializer& ser) { if (needQuote == NeedQuote::No || index.find('+') == std::string::npos) { ser << index; } else { @@ -18,7 +18,7 @@ static void indexToSql(std::string_view index, reindexer::WrSerializer &ser) { } } -static reindexer::WrSerializer &stringToSql(std::string_view str, reindexer::WrSerializer &ser) { +static reindexer::WrSerializer& stringToSql(std::string_view str, reindexer::WrSerializer& ser) { ser << '\''; for (auto c : str) { switch (c) { @@ -53,9 +53,9 @@ static reindexer::WrSerializer &stringToSql(std::string_view str, reindexer::WrS namespace reindexer { -void SQLEncoder::DumpSingleJoinQuery(size_t idx, WrSerializer &ser, bool stripArgs) const { +void SQLEncoder::DumpSingleJoinQuery(size_t idx, WrSerializer& ser, bool stripArgs) const { assertrx(idx < query_.GetJoinQueries().size()); - const auto &jq = query_.GetJoinQueries()[idx]; + const auto& jq = query_.GetJoinQueries()[idx]; ser << jq.joinType; if (jq.Entries().Empty() && !jq.HasLimit() && jq.sortingEntries_.empty()) { ser << ' ' << jq.NsName() << " ON "; @@ -64,8 +64,10 @@ void SQLEncoder::DumpSingleJoinQuery(size_t idx, WrSerializer &ser, bool stripAr jq.GetSQL(ser, stripArgs); ser << ") ON "; } - if (jq.joinEntries_.size() != 1) ser << "("; - for (auto &e : jq.joinEntries_) { + if (jq.joinEntries_.size() != 1) { + ser << "("; + } + for (auto& e : jq.joinEntries_) { if (&e != &*jq.joinEntries_.begin()) { ser << ' ' << e.Operation() << ' '; } @@ -76,10 +78,12 @@ void SQLEncoder::DumpSingleJoinQuery(size_t idx, WrSerializer &ser, bool stripAr ser << query_.NsName() << '.' << e.LeftFieldName() << ' ' << e.Condition() << ' ' << jq.NsName() << '.' << e.RightFieldName(); } } - if (jq.joinEntries_.size() != 1) ser << ')'; + if (jq.joinEntries_.size() != 1) { + ser << ')'; + } } -void SQLEncoder::dumpJoined(WrSerializer &ser, bool stripArgs) const { +void SQLEncoder::dumpJoined(WrSerializer& ser, bool stripArgs) const { for (size_t i = 0; i < query_.GetJoinQueries().size(); ++i) { if (query_.GetJoinQueries()[i].joinType == JoinType::LeftJoin) { ser << ' '; @@ -88,8 +92,8 @@ void SQLEncoder::dumpJoined(WrSerializer &ser, bool stripArgs) const { } } -void SQLEncoder::dumpMerged(WrSerializer &ser, bool stripArgs) const { - for (auto &me : query_.GetMergeQueries()) { +void SQLEncoder::dumpMerged(WrSerializer& ser, bool stripArgs) const { + for (auto& me : query_.GetMergeQueries()) { ser << ' ' << me.joinType << "( "; me.GetSQL(ser, stripArgs); ser << ')'; @@ -98,16 +102,20 @@ void SQLEncoder::dumpMerged(WrSerializer &ser, bool stripArgs) const { static std::string escapeQuotes(std::string str) { for (size_t i = 0; i < str.size(); ++i) { - if (str[i] == '\'' && (i == 0 || str[i - 1] != '\\')) str.insert(i++, 1, '\\'); + if (str[i] == '\'' && (i == 0 || str[i - 1] != '\\')) { + str.insert(i++, 1, '\\'); + } } return str; } -void SQLEncoder::dumpOrderBy(WrSerializer &ser, bool stripArgs) const { - if (query_.sortingEntries_.empty()) return; +void SQLEncoder::dumpOrderBy(WrSerializer& ser, bool stripArgs) const { + if (query_.sortingEntries_.empty()) { + return; + } ser << " ORDER BY "; for (size_t i = 0; i < query_.sortingEntries_.size(); ++i) { - const SortingEntry &sortingEntry(query_.sortingEntries_[i]); + const SortingEntry& sortingEntry(query_.sortingEntries_[i]); if (query_.forcedSortOrder_.empty()) { ser << '\'' << escapeQuotes(sortingEntry.expression) << '\''; } else { @@ -115,7 +123,7 @@ void SQLEncoder::dumpOrderBy(WrSerializer &ser, bool stripArgs) const { if (stripArgs) { ser << '?'; } else { - for (auto &v : query_.forcedSortOrder_) { + for (auto& v : query_.forcedSortOrder_) { ser << ", "; v.Dump(ser); } @@ -123,23 +131,27 @@ void SQLEncoder::dumpOrderBy(WrSerializer &ser, bool stripArgs) const { ser << ")"; } ser << (sortingEntry.desc ? " DESC" : ""); - if (i != query_.sortingEntries_.size() - 1) ser << ", "; + if (i != query_.sortingEntries_.size() - 1) { + ser << ", "; + } } } -void SQLEncoder::dumpEqualPositions(WrSerializer &ser, const EqualPositions_t &equalPositions) const { - for (const auto &ep : equalPositions) { +void SQLEncoder::dumpEqualPositions(WrSerializer& ser, const EqualPositions_t& equalPositions) const { + for (const auto& ep : equalPositions) { assertrx(ep.size() > 1); ser << " equal_position("; for (size_t i = 0; i < ep.size(); ++i) { - if (i != 0) ser << ", "; + if (i != 0) { + ser << ", "; + } ser << ep[i]; } ser << ")"; } } -WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { +WrSerializer& SQLEncoder::GetSQL(WrSerializer& ser, bool stripArgs) const { switch (realQueryType_) { case QuerySelect: { ser << "SELECT "; @@ -148,23 +160,29 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { ser << "RANK()"; needComma = true; } - for (const auto &a : query_.aggregations_) { + for (const auto& a : query_.aggregations_) { if (needComma) { ser << ", "; } else { needComma = true; } ser << AggTypeToStr(a.Type()) << "("; - for (const auto &f : a.Fields()) { - if (&f != &*a.Fields().begin()) ser << ", "; + for (const auto& f : a.Fields()) { + if (&f != &*a.Fields().begin()) { + ser << ", "; + } ser << f; } - for (const auto &se : a.Sorting()) { + for (const auto& se : a.Sorting()) { ser << " ORDER BY " << '\'' << escapeQuotes(se.expression) << '\'' << (se.desc ? " DESC" : " ASC"); } - if (a.Offset() != QueryEntry::kDefaultOffset && !stripArgs) ser << " OFFSET " << a.Offset(); - if (a.Limit() != QueryEntry::kDefaultLimit && !stripArgs) ser << " LIMIT " << a.Limit(); + if (a.Offset() != QueryEntry::kDefaultOffset && !stripArgs) { + ser << " OFFSET " << a.Offset(); + } + if (a.Limit() != QueryEntry::kDefaultLimit && !stripArgs) { + ser << " LIMIT " << a.Limit(); + } ser << ')'; } if (query_.aggregations_.empty() || (query_.aggregations_.size() == 1 && query_.aggregations_[0].Type() == AggDistinct)) { @@ -175,15 +193,19 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { } if (query_.SelectFilters().empty()) { if (query_.Limit() != 0 || !query_.HasCalcTotal()) { - if (needComma) ser << ", "; + if (needComma) { + ser << ", "; + } ser << '*'; if (query_.HasCalcTotal()) { needComma = true; } } } else { - for (const auto &filter : query_.SelectFilters()) { - if (filter == distinctIndex) continue; + for (const auto& filter : query_.SelectFilters()) { + if (filter == distinctIndex) { + continue; + } if (needComma) { ser << ", "; } else { @@ -194,9 +216,15 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { } } if (query_.HasCalcTotal()) { - if (needComma) ser << ", "; - if (query_.CalcTotal() == ModeAccurateTotal) ser << "COUNT(*)"; - if (query_.CalcTotal() == ModeCachedTotal) ser << "COUNT_CACHED(*)"; + if (needComma) { + ser << ", "; + } + if (query_.CalcTotal() == ModeAccurateTotal) { + ser << "COUNT(*)"; + } + if (query_.CalcTotal() == ModeCachedTotal) { + ser << "COUNT_CACHED(*)"; + } } ser << " FROM " << query_.NsName(); } break; @@ -204,7 +232,9 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { ser << "DELETE FROM " << query_.NsName(); break; case QueryUpdate: { - if (query_.UpdateFields().empty()) break; + if (query_.UpdateFields().empty()) { + break; + } ser << "UPDATE " << query_.NsName(); FieldModifyMode mode = query_.UpdateFields().front().Mode(); bool isUpdate = (mode == FieldModeSet || mode == FieldModeSetJson); @@ -213,15 +243,21 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { } else { ser << " DROP "; } - for (const UpdateEntry &field : query_.UpdateFields()) { - if (&field != &*query_.UpdateFields().begin()) ser << ','; + for (const UpdateEntry& field : query_.UpdateFields()) { + if (&field != &*query_.UpdateFields().begin()) { + ser << ','; + } ser << field.Column(); if (isUpdate) { ser << " = "; bool isArray = (field.Values().IsArrayValue() || field.Values().size() > 1); - if (isArray) ser << '['; - for (const Variant &v : field.Values()) { - if (&v != &*field.Values().begin()) ser << ','; + if (isArray) { + ser << '['; + } + for (const Variant& v : field.Values()) { + if (&v != &*field.Values().begin()) { + ser << ','; + } v.Type().EvaluateOneOf(overloaded{ [&](KeyValueType::String) { if (!field.IsExpression() && mode != FieldModeSetJson) { @@ -236,7 +272,9 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { ser << v.As(); }}); } - if (isArray) ser << "]"; + if (isArray) { + ser << "]"; + } } } break; @@ -253,15 +291,19 @@ WrSerializer &SQLEncoder::GetSQL(WrSerializer &ser, bool stripArgs) const { dumpMerged(ser, stripArgs); dumpOrderBy(ser, stripArgs); - if (query_.HasOffset() && !stripArgs) ser << " OFFSET " << query_.Offset(); - if (query_.HasLimit() && !stripArgs) ser << " LIMIT " << query_.Limit(); + if (query_.HasOffset() && !stripArgs) { + ser << " OFFSET " << query_.Offset(); + } + if (query_.HasLimit() && !stripArgs) { + ser << " LIMIT " << query_.Limit(); + } return ser; } constexpr static std::string_view kOpNames[] = {"-", "OR", "AND", "AND NOT"}; template -static void dumpCondWithValues(WrSerializer &ser, std::string_view fieldName, CondType cond, const VariantArray &values, bool stripArgs) { +static void dumpCondWithValues(WrSerializer& ser, std::string_view fieldName, CondType cond, const VariantArray& values, bool stripArgs) { switch (cond) { case CondDWithin: ser << "ST_DWithin("; @@ -301,21 +343,27 @@ static void dumpCondWithValues(WrSerializer &ser, std::string_view fieldName, Co if (stripArgs) { ser << '?'; } else { - if (values.size() != 1) ser << '('; - for (auto &v : values) { - if (&v != &values[0]) ser << ','; + if (values.size() != 1) { + ser << '('; + } + for (auto& v : values) { + if (&v != &values[0]) { + ser << ','; + } v.Type().EvaluateOneOf(overloaded{ [&](KeyValueType::String) { stringToSql(v.As(), ser); }, [&](KeyValueType::Uuid) { ser << '\'' << v.As() << '\''; }, [&](OneOf) { ser << v.As(); }}); } - if (values.size() != 1) ser << ")"; + if (values.size() != 1) { + ser << ")"; + } } } } -void SQLEncoder::dumpWhereEntries(QueryEntries::const_iterator from, QueryEntries::const_iterator to, WrSerializer &ser, +void SQLEncoder::dumpWhereEntries(QueryEntries::const_iterator from, QueryEntries::const_iterator to, WrSerializer& ser, bool stripArgs) const { int encodedEntries = 0; for (auto it = from; it != to; ++it) { @@ -326,22 +374,22 @@ void SQLEncoder::dumpWhereEntries(QueryEntries::const_iterator from, QueryEntrie ser << "NOT "; } it->Visit( - [&ser](const AlwaysTrue &) { + [&ser](const AlwaysTrue&) { logPrintf(LogTrace, "Not normalized query to dsl"); ser << "true"; }, - [&ser](const AlwaysFalse &) { + [&ser](const AlwaysFalse&) { logPrintf(LogTrace, "Not normalized query to dsl"); ser << "false"; }, - [&](const SubQueryEntry &sqe) { + [&](const SubQueryEntry& sqe) { if (encodedEntries) { ser << kOpNames[op] << ' '; } dumpCondWithValues(ser, '(' + query_.GetSubQuery(sqe.QueryIndex()).GetSQL(stripArgs) + ')', sqe.Condition(), sqe.Values(), stripArgs); }, - [&](const SubQueryFieldEntry &sqe) { + [&](const SubQueryFieldEntry& sqe) { if (encodedEntries) { ser << kOpNames[op] << ' '; } @@ -349,7 +397,7 @@ void SQLEncoder::dumpWhereEntries(QueryEntries::const_iterator from, QueryEntrie SQLEncoder{query_.GetSubQuery(sqe.QueryIndex())}.GetSQL(ser, stripArgs); ser << ')'; }, - [&](const QueryEntriesBracket &bracket) { + [&](const QueryEntriesBracket& bracket) { if (encodedEntries) { ser << kOpNames[op] << ' '; } @@ -358,19 +406,19 @@ void SQLEncoder::dumpWhereEntries(QueryEntries::const_iterator from, QueryEntrie dumpEqualPositions(ser, bracket.equalPositions); ser << ')'; }, - [&](const QueryEntry &entry) { + [&](const QueryEntry& entry) { if (encodedEntries) { ser << kOpNames[op] << ' '; } dumpCondWithValues(ser, entry.FieldName(), entry.Condition(), entry.Values(), stripArgs); }, - [&](const JoinQueryEntry &jqe) { + [&](const JoinQueryEntry& jqe) { if (encodedEntries && query_.GetJoinQueries()[jqe.joinIndex].joinType != JoinType::OrInnerJoin) { ser << kOpNames[op] << ' '; } SQLEncoder(query_).DumpSingleJoinQuery(jqe.joinIndex, ser, stripArgs); }, - [&](const BetweenFieldsQueryEntry &entry) { + [&](const BetweenFieldsQueryEntry& entry) { if (encodedEntries) { ser << kOpNames[op] << ' '; } @@ -382,8 +430,10 @@ void SQLEncoder::dumpWhereEntries(QueryEntries::const_iterator from, QueryEntrie } } -void SQLEncoder::dumpSQLWhere(WrSerializer &ser, bool stripArgs) const { - if (query_.Entries().Empty()) return; +void SQLEncoder::dumpSQLWhere(WrSerializer& ser, bool stripArgs) const { + if (query_.Entries().Empty()) { + return; + } ser << " WHERE "; dumpWhereEntries(query_.Entries().cbegin(), query_.Entries().cend(), ser, stripArgs); dumpEqualPositions(ser, query_.Entries().equalPositions); diff --git a/cpp_src/core/query/sql/sqlencoder.h b/cpp_src/core/query/sql/sqlencoder.h index 968f387df..3994555f0 100644 --- a/cpp_src/core/query/sql/sqlencoder.h +++ b/cpp_src/core/query/sql/sqlencoder.h @@ -11,47 +11,47 @@ class WrSerializer; class SQLEncoder { public: - SQLEncoder(const Query &q) noexcept : SQLEncoder(q, q.Type()) {} - SQLEncoder(const Query &q, QueryType queryType) noexcept : query_(q), realQueryType_(queryType) {} + SQLEncoder(const Query& q) noexcept : SQLEncoder(q, q.Type()) {} + SQLEncoder(const Query& q, QueryType queryType) noexcept : query_(q), realQueryType_(queryType) {} - WrSerializer &GetSQL(WrSerializer &ser, bool stripArgs = false) const; + WrSerializer& GetSQL(WrSerializer& ser, bool stripArgs = false) const; /// Gets printable sql version of joined query set by idx. /// @param idx - index of joined query in joinQueries_. /// @param ser - serializer to store SQL string. /// @param stripArgs - replace condition values with '?'. - void DumpSingleJoinQuery(size_t idx, WrSerializer &ser, bool stripArgs) const; + void DumpSingleJoinQuery(size_t idx, WrSerializer& ser, bool stripArgs) const; protected: /// Builds print version of a query with join in sql format. /// @param ser - serializer to store SQL string /// @param stripArgs - replace condition values with '?' - void dumpJoined(WrSerializer &ser, bool stripArgs) const; + void dumpJoined(WrSerializer& ser, bool stripArgs) const; /// Builds a print version of a query with merge queries in sql format. /// @param ser - serializer to store SQL string /// @param stripArgs - replace condition values with '?' - void dumpMerged(WrSerializer &ser, bool stripArgs) const; + void dumpMerged(WrSerializer& ser, bool stripArgs) const; /// Builds a print version of a query's order by statement /// @param ser - serializer to store SQL string /// @param stripArgs - replace condition values with '?' - void dumpOrderBy(WrSerializer &ser, bool stripArgs) const; + void dumpOrderBy(WrSerializer& ser, bool stripArgs) const; /// Builds a print version of all equal_position() functions in query. /// @param ser - serializer to store SQL string /// @param equalPositions - equal positions array - void dumpEqualPositions(WrSerializer &ser, const EqualPositions_t &equalPositions) const; + void dumpEqualPositions(WrSerializer& ser, const EqualPositions_t& equalPositions) const; /// Builds a print version of all where condition entries. /// @param from - iterator to first entry /// @param to - iterator to last entry /// @param ser - serializer to store SQL string /// @param stripArgs - replace condition values with '?' - void dumpWhereEntries(QueryEntries::const_iterator from, QueryEntries::const_iterator to, WrSerializer &ser, bool stripArgs) const; - void dumpSQLWhere(WrSerializer &ser, bool stripArgs) const; + void dumpWhereEntries(QueryEntries::const_iterator from, QueryEntries::const_iterator to, WrSerializer& ser, bool stripArgs) const; + void dumpSQLWhere(WrSerializer& ser, bool stripArgs) const; - const Query &query_; + const Query& query_; const QueryType realQueryType_; }; diff --git a/cpp_src/core/query/sql/sqlparser.cc b/cpp_src/core/query/sql/sqlparser.cc index ae4d7ba1c..6533e741e 100644 --- a/cpp_src/core/query/sql/sqlparser.cc +++ b/cpp_src/core/query/sql/sqlparser.cc @@ -21,12 +21,12 @@ Query SQLParser::Parse(std::string_view q) { return query; } -bool SQLParser::reachedAutocompleteToken(tokenizer &parser, const token &tok) const { +bool SQLParser::reachedAutocompleteToken(tokenizer& parser, const token& tok) const { size_t pos = parser.getPos() + tok.text().length(); return pos > ctx_.suggestionsPos; } -token SQLParser::peekSqlToken(tokenizer &parser, SqlTokenType tokenType, bool toLower) { +token SQLParser::peekSqlToken(tokenizer& parser, SqlTokenType tokenType, bool toLower) { token tok = parser.peek_token(toLower ? tokenizer::flags::to_lower : tokenizer::flags::no_flags); const bool eof = ((parser.getPos() + tok.text().length()) == parser.length()); if (ctx_.autocompleteMode && reachedAutocompleteToken(parser, tok)) { @@ -40,12 +40,16 @@ token SQLParser::peekSqlToken(tokenizer &parser, SqlTokenType tokenType, bool to ctx_.possibleSuggestionDetectedInThisClause = true; } } - if (!ctx_.foundPossibleSuggestions) ctx_.tokens.push_back(tokenType); - if (eof && ctx_.autocompleteMode) throw Error(errLogic, "SQLParser eof is reached!"); + if (!ctx_.foundPossibleSuggestions) { + ctx_.tokens.push_back(tokenType); + } + if (eof && ctx_.autocompleteMode) { + throw Error(errLogic, "SQLParser eof is reached!"); + } return tok; } -int SQLParser::Parse(tokenizer &parser) { +int SQLParser::Parse(tokenizer& parser) { parser.skip_space(); if (parser.length() == 0) { ctx_.suggestions.emplace_back(std::string(), Start); @@ -81,13 +85,15 @@ int SQLParser::Parse(tokenizer &parser) { tok = parser.next_token(); } parser.skip_space(); - if (!tok.text().empty() || !parser.end()) throw Error(errParseSQL, "Unexpected '%s' in query, %s", tok.text(), parser.where()); + if (!tok.text().empty() || !parser.end()) { + throw Error(errParseSQL, "Unexpected '%s' in query, %s", tok.text(), parser.where()); + } return 0; } template -int SQLParser::selectParse(tokenizer &parser) { +int SQLParser::selectParse(tokenizer& parser) { // Get filter token tok; bool wasSelectFilter = false; @@ -105,14 +111,18 @@ int SQLParser::selectParse(tokenizer &parser) { query_.Limit(0); } tok = parser.next_token(); - if (tok.text() != "*"sv) throw Error(errParseSQL, "Expected '*', but found '%s' in query, %s", tok.text(), parser.where()); + if (tok.text() != "*"sv) { + throw Error(errParseSQL, "Expected '*', but found '%s' in query, %s", tok.text(), parser.where()); + } } else if (name.text() == "count_cached"sv) { query_.CalcTotal(ModeCachedTotal); if (!wasSelectFilter) { query_.Limit(0); } tok = parser.next_token(); - if (tok.text() != "*"sv) throw Error(errParseSQL, "Expected '*', but found '%s' in query, %s", tok.text(), parser.where()); + if (tok.text() != "*"sv) { + throw Error(errParseSQL, "Expected '*', but found '%s' in query, %s", tok.text(), parser.where()); + } } else if (name.text() == "rank"sv) { query_.WithRank(); } else { @@ -187,7 +197,9 @@ int SQLParser::selectParse(tokenizer &parser) { wasSelectFilter = true; selectFilters.clear(); } - if (tok.text() != ","sv) break; + if (tok.text() != ","sv) { + break; + } tok = parser.next_token(); } if (wasSelectFilter) { @@ -211,14 +223,16 @@ int SQLParser::selectParse(tokenizer &parser) { } else if (tok.text() == "limit"sv) { parser.next_token(); tok = parser.next_token(); - if (tok.type != TokenNumber) + if (tok.type != TokenNumber) { throw Error(errParseSQL, "Expected number, but found '%s' in query, %s", tok.text(), parser.where()); + } query_.Limit(stoi(tok.text())); } else if (tok.text() == "offset"sv) { parser.next_token(); tok = parser.next_token(); - if (tok.type != TokenNumber) + if (tok.type != TokenNumber) { throw Error(errParseSQL, "Expected number, but found '%s' in query, %s", tok.text(), parser.where()); + } query_.Offset(stoi(tok.text())); } else if (tok.text() == "order"sv) { parser.next_token(); @@ -261,7 +275,7 @@ int SQLParser::selectParse(tokenizer &parser) { } template -static void moveAppend(T &dst, T &src) { +static void moveAppend(T& dst, T& src) { if (dst.empty()) { dst = std::move(src); } else { @@ -273,7 +287,7 @@ static void moveAppend(T &dst, T &src) { class SQLParser::ParserContextsAppendGuard { public: - ParserContextsAppendGuard(SqlParsingCtx &mainCtx, SqlParsingCtx &nestedCtx) noexcept : mainCtx_{mainCtx}, nestedCtx_{nestedCtx} {} + ParserContextsAppendGuard(SqlParsingCtx& mainCtx, SqlParsingCtx& nestedCtx) noexcept : mainCtx_{mainCtx}, nestedCtx_{nestedCtx} {} ~ParserContextsAppendGuard() { moveAppend(mainCtx_.suggestions, nestedCtx_.suggestions); if (!mainCtx_.foundPossibleSuggestions && nestedCtx_.foundPossibleSuggestions) { @@ -282,17 +296,19 @@ class SQLParser::ParserContextsAppendGuard { } private: - SqlParsingCtx &mainCtx_; - SqlParsingCtx &nestedCtx_; + SqlParsingCtx& mainCtx_; + SqlParsingCtx& nestedCtx_; }; -static KeyValueType detectValueType(const token &currTok) { +static KeyValueType detectValueType(const token& currTok) { const std::string_view val = currTok.text(); if (currTok.type == TokenNumber) { unsigned i = 0; bool flt = false; bool digit = val.length() < 21 && val.length() > 0; - if (val[i] == '+' || val[i] == '-') i++; + if (val[i] == '+' || val[i] == '-') { + i++; + } for (; i < val.length() && digit; i++) { if (val[i] == '.') { flt = true; @@ -311,7 +327,7 @@ static KeyValueType detectValueType(const token &currTok) { return {KeyValueType::String{}}; } -Variant token2kv(const token &currTok, tokenizer &parser, bool allowComposite) { +Variant token2kv(const token& currTok, tokenizer& parser, bool allowComposite) { if (currTok.text() == "{"sv) { // Composite value parsing if (!allowComposite) { @@ -333,8 +349,12 @@ Variant token2kv(const token &currTok, tokenizer &parser, bool allowComposite) { std::string_view value = currTok.text(); if (currTok.type == TokenName) { - if (iequals(value, "true"sv)) return Variant{true}; - if (iequals(value, "false"sv)) return Variant{false}; + if (iequals(value, "true"sv)) { + return Variant{true}; + } + if (iequals(value, "false"sv)) { + return Variant{false}; + } } if (currTok.type != TokenNumber && currTok.type != TokenString) { @@ -361,7 +381,7 @@ Variant token2kv(const token &currTok, tokenizer &parser, bool allowComposite) { }); } -int SQLParser::parseOrderBy(tokenizer &parser, SortingEntries &sortingEntries, std::vector &forcedSortOrder_) { +int SQLParser::parseOrderBy(tokenizer& parser, SortingEntries& sortingEntries, std::vector& forcedSortOrder_) { // Just skip token (BY) peekSqlToken(parser, BySqlToken); parser.next_token(); @@ -387,7 +407,9 @@ int SQLParser::parseOrderBy(tokenizer &parser, SortingEntries &sortingEntries, s tok = parser.next_token(tokenizer::flags::no_flags); for (;;) { tok = parser.next_token(); - if (tok.text() == ")"sv) break; + if (tok.text() == ")"sv) { + break; + } if (tok.text() != ","sv) { throw Error(errParseSQL, "Expected ')' or ',', but found '%s' in query, %s", tok.text(), parser.where()); } @@ -408,19 +430,22 @@ int SQLParser::parseOrderBy(tokenizer &parser, SortingEntries &sortingEntries, s sortingEntries.push_back(std::move(sortingEntry)); auto nextToken = parser.peek_token(); - if (nextToken.text() != ","sv) break; + if (nextToken.text() != ","sv) { + break; + } parser.next_token(); } return 0; } -int SQLParser::deleteParse(tokenizer &parser) { +int SQLParser::deleteParse(tokenizer& parser) { // Get filter token tok; peekSqlToken(parser, FromSqlToken); - if (parser.next_token().text() != "from"sv) + if (parser.next_token().text() != "from"sv) { throw Error(errParams, "Expected 'FROM', but found '%s' in query, %s", tok.text(), parser.where()); + } peekSqlToken(parser, NamespaceSqlToken); query_.SetNsName(parser.next_token().text()); @@ -431,7 +456,7 @@ int SQLParser::deleteParse(tokenizer &parser) { return 0; } -static void addUpdateValue(const token &currTok, tokenizer &parser, UpdateEntry &updateField) { +static void addUpdateValue(const token& currTok, tokenizer& parser, UpdateEntry& updateField) { if (currTok.type == TokenString) { updateField.Values().push_back(token2kv(currTok, parser, false)); } else { @@ -447,18 +472,24 @@ static void addUpdateValue(const token &currTok, tokenizer &parser, UpdateEntry updateField.Values().emplace_back(Variant(std::string(parser.begin() + jsonPos, jsonLength))); updateField.SetMode(FieldModeSetJson); parser.setPos(jsonPos + jsonLength); - } catch (const gason::Exception &e) { + } catch (const gason::Exception& e) { throw Error(errParseSQL, "%s, in query %s", e.what(), parser.where()); } } else { - auto eof = [](tokenizer &parser, bool &inArray) -> bool { - if (parser.end()) return true; + auto eof = [](tokenizer& parser, bool& inArray) -> bool { + if (parser.end()) { + return true; + } token nextTok = parser.peek_token(); bool result = (nextTok.text() == "where"sv) || (nextTok.text() == "order"sv) || (nextTok.text() == "limit"sv) || (nextTok.text() == "offset"sv) || (!inArray && nextTok.text() == "]"sv) || (!inArray && nextTok.text() == ","sv); - if (nextTok.text() == "["sv && !inArray) inArray = true; - if (nextTok.text() == "]"sv && inArray) inArray = false; + if (nextTok.text() == "["sv && !inArray) { + inArray = true; + } + if (nextTok.text() == "]"sv && inArray) { + inArray = false; + } return result; }; int count = 0; @@ -476,7 +507,7 @@ static void addUpdateValue(const token &currTok, tokenizer &parser, UpdateEntry try { Variant val = token2kv(currTok, parser, false); updateField.Values().push_back(val); - } catch (const Error &) { + } catch (const Error&) { updateField.Values().push_back(Variant(expression)); updateField.SetIsExpression(true); } @@ -485,7 +516,7 @@ static void addUpdateValue(const token &currTok, tokenizer &parser, UpdateEntry } } -void SQLParser::parseArray(tokenizer &parser, std::string_view tokText, UpdateEntry *updateField) const { +void SQLParser::parseArray(tokenizer& parser, std::string_view tokText, UpdateEntry* updateField) const { if (tokText != "["sv) { throw Error(errParams, "Expected '[' after field parameter, not %s", tokText); } @@ -493,21 +524,26 @@ void SQLParser::parseArray(tokenizer &parser, std::string_view tokText, UpdateEn for (;;) { auto tok = parser.next_token(tokenizer::flags::no_flags); if (tok.text() == "]"sv) { - if (updateField && updateField->Values().empty()) break; + if (updateField && updateField->Values().empty()) { + break; + } throw Error(errParseSQL, "Expected field value, but found ']' in query, %s", parser.where()); } - if (updateField) addUpdateValue(tok, parser, *updateField); + if (updateField) { + addUpdateValue(tok, parser, *updateField); + } tok = parser.next_token(tokenizer::flags::no_flags); - if (tok.text() == "]"sv) break; + if (tok.text() == "]"sv) { + break; + } if (tok.text() != ","sv) { throw Error(errParseSQL, "Expected ']' or ',', but found '%s' in query, %s", tok.text(), parser.where()); } } if (updateField && (updateField->Mode() == FieldModeSetJson)) { - for (const auto &it : updateField->Values()) { - if ((!it.Type().Is()) || - std::string_view(it).front() != '{') { + for (const auto& it : updateField->Values()) { + if ((!it.Type().Is()) || std::string_view(it).front() != '{') { throw Error(errLogic, "Unexpected variant type in Array: %s. Expecting KeyValueType::String with JSON-content", it.Type().Name()); } @@ -515,7 +551,7 @@ void SQLParser::parseArray(tokenizer &parser, std::string_view tokText, UpdateEn } } -void SQLParser::parseCommand(tokenizer &parser) const { +void SQLParser::parseCommand(tokenizer& parser) const { // parse input, for example: array_remove(array_field, [24, 3, 81]) || [11,22] auto tok = parser.next_token(); @@ -549,7 +585,9 @@ void SQLParser::parseCommand(tokenizer &parser) const { while (tok.text() == "|"sv) { parser.next_token(); tok = parser.next_token(); - if (tok.text() != "|"sv) throw Error(errLogic, "Expected '|', not '%s'", tok.text()); + if (tok.text() != "|"sv) { + throw Error(errLogic, "Expected '|', not '%s'", tok.text()); + } tok = parser.next_token(); if (tok.type == TokenSymbol) { parseArray(parser, tok.text(), nullptr); @@ -562,7 +600,7 @@ void SQLParser::parseCommand(tokenizer &parser) const { } } -UpdateEntry SQLParser::parseUpdateField(tokenizer &parser) { +UpdateEntry SQLParser::parseUpdateField(tokenizer& parser) { token tok = peekSqlToken(parser, FieldNameSqlToken, false); if (tok.type != TokenName) { throw Error(errParseSQL, "Expected field name but found '%s' in query %s", tok.text(), parser.where()); @@ -571,7 +609,9 @@ UpdateEntry SQLParser::parseUpdateField(tokenizer &parser) { parser.next_token(); tok = parser.next_token(); - if (tok.text() != "="sv) throw Error(errParams, "Expected '=' but found '%s' in query, '%s'", tok.text(), parser.where()); + if (tok.text() != "="sv) { + throw Error(errParams, "Expected '=' but found '%s' in query, '%s'", tok.text(), parser.where()); + } size_t startPos = parser.getPos(); bool withArrayExpressions = false; @@ -594,7 +634,9 @@ UpdateEntry SQLParser::parseUpdateField(tokenizer &parser) { while (tok.text() == "|"sv) { parser.next_token(); tok = parser.next_token(); - if (tok.text() != "|"sv) throw Error(errLogic, "Expected '|', not '%s'", tok.text()); + if (tok.text() != "|"sv) { + throw Error(errLogic, "Expected '|', not '%s'", tok.text()); + } tok = parser.next_token(); if (tok.type != TokenName) { throw Error(errParseSQL, "Expected field name, but found %s in query, %s", tok.text(), parser.where()); @@ -613,7 +655,7 @@ UpdateEntry SQLParser::parseUpdateField(tokenizer &parser) { return updateField; } -int SQLParser::updateParse(tokenizer &parser) { +int SQLParser::updateParse(tokenizer& parser) { parser.next_token(); token tok = peekSqlToken(parser, NamespaceSqlToken); @@ -627,7 +669,9 @@ int SQLParser::updateParse(tokenizer &parser) { while (!parser.end()) { query_.UpdateField(parseUpdateField(parser)); tok = parser.peek_token(); - if (tok.text() != ","sv) break; + if (tok.text() != ","sv) { + break; + } parser.next_token(); } } else if (tok.text() == "drop"sv) { @@ -640,7 +684,9 @@ int SQLParser::updateParse(tokenizer &parser) { query_.Drop(std::string(tok.text())); parser.next_token(); tok = parser.peek_token(); - if (tok.text() != ","sv) break; + if (tok.text() != ","sv) { + break; + } } } else { throw Error(errParseSQL, "Expected 'SET' or 'DROP' but found '%s' in query %s", tok.text(), parser.where()); @@ -651,7 +697,7 @@ int SQLParser::updateParse(tokenizer &parser) { return 0; } -void SQLParser::parseModifyConditions(tokenizer &parser) { +void SQLParser::parseModifyConditions(tokenizer& parser) { while (!parser.end()) { auto tok = peekSqlToken(parser, ModifyConditionsStart); if (tok.text() == "where"sv) { @@ -660,14 +706,16 @@ void SQLParser::parseModifyConditions(tokenizer &parser) { } else if (tok.text() == "limit"sv) { parser.next_token(); tok = parser.next_token(); - if (tok.type != TokenNumber) + if (tok.type != TokenNumber) { throw Error(errParseSQL, "Expected number, but found '%s' in query, %s", tok.text(), parser.where()); + } query_.Limit(stoi(tok.text())); } else if (tok.text() == "offset"sv) { parser.next_token(); tok = parser.next_token(); - if (tok.type != TokenNumber) + if (tok.type != TokenNumber) { throw Error(errParseSQL, "Expected number, but found '%s' in query, %s", tok.text(), parser.where()); + } query_.Offset(stoi(tok.text())); } else if (tok.text() == "order"sv) { parser.next_token(); @@ -679,7 +727,7 @@ void SQLParser::parseModifyConditions(tokenizer &parser) { } } -int SQLParser::truncateParse(tokenizer &parser) { +int SQLParser::truncateParse(tokenizer& parser) { parser.next_token(); token tok = peekSqlToken(parser, NamespaceSqlToken); query_.SetNsName(tok.text()); @@ -693,7 +741,7 @@ static bool isCondition(std::string_view text) noexcept { text == "<="sv || iequals(text, "in"sv) || iequals(text, "range"sv) || iequals(text, "like"sv) || iequals(text, "allset"sv); } -Query SQLParser::parseSubQuery(tokenizer &parser) { +Query SQLParser::parseSubQuery(tokenizer& parser) { Query subquery; SQLParser subparser(subquery); const ParserContextsAppendGuard guard{ctx_, subparser.ctx_}; @@ -714,17 +762,17 @@ Query SQLParser::parseSubQuery(tokenizer &parser) { } template -void SQLParser::parseWhereCondition(tokenizer &parser, T &&firstArg, OpType op) { +void SQLParser::parseWhereCondition(tokenizer& parser, T&& firstArg, OpType op) { // Operator CondType condition; auto tok = peekSqlToken(parser, ConditionSqlToken); if (tok.text() == "<>"sv) { condition = CondEq; - if (op == OpAnd) + if (op == OpAnd) { op = OpNot; - else if (op == OpNot) + } else if (op == OpNot) { op = OpAnd; - else { + } else { throw Error(errParseSQL, "<> condition with OR is not supported, %s", parser.where()); } } else { @@ -733,7 +781,9 @@ void SQLParser::parseWhereCondition(tokenizer &parser, T &&firstArg, OpType op) parser.next_token(); // Value - if (ctx_.autocompleteMode) peekSqlToken(parser, WhereFieldValueSqlToken, false); + if (ctx_.autocompleteMode) { + peekSqlToken(parser, WhereFieldValueSqlToken, false); + } tok = parser.next_token(); if (iequals(tok.text(), "null"sv) || iequals(tok.text(), "empty"sv)) { query_.NextOp(op).Where(std::forward(firstArg), CondEmpty, VariantArray{}); @@ -755,12 +805,17 @@ void SQLParser::parseWhereCondition(tokenizer &parser, T &&firstArg, OpType op) VariantArray values; for (;;) { tok = parser.next_token(); - if (tok.text() == ")"sv && tok.type == TokenSymbol) break; + if (tok.text() == ")"sv && tok.type == TokenSymbol) { + break; + } values.push_back(token2kv(tok, parser, true)); tok = parser.next_token(); - if (tok.text() == ")"sv) break; - if (tok.text() != ","sv) + if (tok.text() == ")"sv) { + break; + } + if (tok.text() != ","sv) { throw Error(errParseSQL, "Expected ')' or ',', but found '%s' in query, %s", tok.text(), parser.where()); + } } query_.NextOp(op).Where(std::forward(firstArg), condition, std::move(values)); } else if (tok.type != TokenName || iequals(tok.text(), "true"sv) || iequals(tok.text(), "false"sv)) { @@ -776,7 +831,7 @@ void SQLParser::parseWhereCondition(tokenizer &parser, T &&firstArg, OpType op) } template -int SQLParser::parseWhere(tokenizer &parser) { +int SQLParser::parseWhere(tokenizer& parser) { token tok; OpType nextOp = OpAnd; @@ -865,8 +920,9 @@ int SQLParser::parseWhere(tokenizer &parser) { if (iequals(tok.text(), "not"sv)) { parser.next_token(); nextOp = OpNot; - } else + } else { continue; + } } else if (iequals(tok.text(), "or"sv)) { parser.next_token(); peekSqlToken(parser, FieldNameSqlToken); @@ -875,7 +931,7 @@ int SQLParser::parseWhere(tokenizer &parser) { break; } } - for (auto &eqPos : equalPositions) { + for (auto& eqPos : equalPositions) { if (eqPos.first == 0) { query_.SetEqualPositions(std::move(eqPos.second)); } else { @@ -890,7 +946,7 @@ int SQLParser::parseWhere(tokenizer &parser) { return 0; } -void SQLParser::parseEqualPositions(tokenizer &parser, std::vector> &equalPositions, +void SQLParser::parseEqualPositions(tokenizer& parser, std::vector>& equalPositions, size_t lastBracketPosition) { parser.next_token(); auto tok = parser.next_token(); @@ -918,7 +974,9 @@ void SQLParser::parseEqualPositions(tokenizer &parser, std::vector()); } -void SQLParser::parseJoin(JoinType type, tokenizer &parser) { +void SQLParser::parseJoin(JoinType type, tokenizer& parser) { JoinedQuery jquery; SQLParser jparser(jquery); const ParserContextsAppendGuard guard{ctx_, jparser.ctx_}; @@ -1086,7 +1144,7 @@ void SQLParser::parseJoin(JoinType type, tokenizer &parser) { query_.Join(std::move(jquery)); } -void SQLParser::parseMerge(tokenizer &parser) { +void SQLParser::parseMerge(tokenizer& parser) { JoinedQuery mquery; SQLParser mparser(mquery); const ParserContextsAppendGuard guard{ctx_, mparser.ctx_}; @@ -1116,7 +1174,7 @@ void SQLParser::parseMerge(tokenizer &parser) { query_.Merge(std::move(mquery)); } -std::string SQLParser::parseJoinedFieldName(tokenizer &parser, std::string &name) { +std::string SQLParser::parseJoinedFieldName(tokenizer& parser, std::string& name) { auto tok = peekSqlToken(parser, JoinedFieldNameSqlToken); if (tok.type != TokenName) { throw Error(errParseSQL, "Expected name, but found %s, %s", tok.text(), parser.where()); @@ -1137,7 +1195,7 @@ std::string SQLParser::parseJoinedFieldName(tokenizer &parser, std::string &name return std::string(tok.text().substr(dotPos + 1)); } -void SQLParser::parseJoinEntries(tokenizer &parser, const std::string &mainNs, JoinedQuery &jquery) { +void SQLParser::parseJoinEntries(tokenizer& parser, const std::string& mainNs, JoinedQuery& jquery) { auto tok = peekSqlToken(parser, OnSqlToken); if (tok.text() != "on"sv) { throw Error(errParseSQL, "Expected 'ON', but found %s, %s", tok.text(), parser.where()); @@ -1147,7 +1205,9 @@ void SQLParser::parseJoinEntries(tokenizer &parser, const std::string &mainNs, J tok = parser.peek_token(); bool braces = tok.text() == "("sv; - if (braces) parser.next_token(); + if (braces) { + parser.next_token(); + } while (!parser.end()) { tok = peekSqlToken(parser, OpSqlToken); diff --git a/cpp_src/core/query/sql/sqlparser.h b/cpp_src/core/query/sql/sqlparser.h index e283b45ff..3a857b51a 100644 --- a/cpp_src/core/query/sql/sqlparser.h +++ b/cpp_src/core/query/sql/sqlparser.h @@ -27,7 +27,7 @@ class SQLParser { [[nodiscard]] static Query Parse(std::string_view sql); protected: - explicit SQLParser(Query &q) noexcept : query_(q) {} + explicit SQLParser(Query& q) noexcept : query_(q) {} /// Sql parser context struct SqlParsingCtx { struct SuggestionData { @@ -36,7 +36,7 @@ class SQLParser { SqlTokenType tokenType = Start; std::unordered_set variants; }; - void updateLinkedNs(const std::string &ns) { + void updateLinkedNs(const std::string& ns) { if (autocompleteMode && (!foundPossibleSuggestions || possibleSuggestionDetectedInThisClause)) { suggestionLinkedNs = ns; } @@ -54,79 +54,79 @@ class SQLParser { /// Parses query. /// @param tok - tokenizer object instance. /// @return always returns zero. - int Parse(tokenizer &tok); + int Parse(tokenizer& tok); /// Peeks next sql token. /// @param parser - tokenizer object instance. /// @param tokenType - token type. /// @param toLower - transform to lower representation. /// @return sql token object. - token peekSqlToken(tokenizer &parser, SqlTokenType tokenType, bool toLower = true); + token peekSqlToken(tokenizer& parser, SqlTokenType tokenType, bool toLower = true); /// Is current token last in autocomplete mode? - bool reachedAutocompleteToken(tokenizer &parser, const token &tok) const; + bool reachedAutocompleteToken(tokenizer& parser, const token& tok) const; /// Parses filter part of sql query. /// @param parser - tokenizer object instance. /// @return always returns zero. template - int selectParse(tokenizer &parser); + int selectParse(tokenizer& parser); /// Parses filter part of sql delete query. /// @param parser - tokenizer object instance. /// @return always returns zero. - int deleteParse(tokenizer &parser); + int deleteParse(tokenizer& parser); /// Parses filter part of sql update query. /// @param parser - tokenizer object instance. /// @return always returns zero. - int updateParse(tokenizer &parser); + int updateParse(tokenizer& parser); /// Parses filter part of sql truncate query. /// @param parser - tokenizer object instance. /// @return always returns zero. - int truncateParse(tokenizer &parser); + int truncateParse(tokenizer& parser); /// Parse where entries template - int parseWhere(tokenizer &parser); + int parseWhere(tokenizer& parser); template - void parseWhereCondition(tokenizer &, T &&firstArg, OpType); + void parseWhereCondition(tokenizer&, T&& firstArg, OpType); /// Parse order by - int parseOrderBy(tokenizer &parser, SortingEntries &sortingEntries, std::vector &forcedSortOrder); + int parseOrderBy(tokenizer& parser, SortingEntries& sortingEntries, std::vector& forcedSortOrder); /// Parse join entries - void parseJoin(JoinType type, tokenizer &tok); + void parseJoin(JoinType type, tokenizer& tok); /// Parse join entries - void parseJoinEntries(tokenizer &parser, const std::string &mainNs, JoinedQuery &jquery); + void parseJoinEntries(tokenizer& parser, const std::string& mainNs, JoinedQuery& jquery); /// Parse equal_positions - void parseEqualPositions(tokenizer &parser, std::vector> &equalPositions, size_t openBracketsCount); + void parseEqualPositions(tokenizer& parser, std::vector>& equalPositions, size_t openBracketsCount); - Point parseGeomFromText(tokenizer &parser) const; - void parseDWithin(tokenizer &parser, OpType nextOp); + Point parseGeomFromText(tokenizer& parser) const; + void parseDWithin(tokenizer& parser, OpType nextOp); /// Parse update field entries - UpdateEntry parseUpdateField(tokenizer &parser); + UpdateEntry parseUpdateField(tokenizer& parser); /// Parse joined Ns name: [Namespace.field] - std::string parseJoinedFieldName(tokenizer &parser, std::string &name); + std::string parseJoinedFieldName(tokenizer& parser, std::string& name); /// Parse merge entries - void parseMerge(tokenizer &parser); + void parseMerge(tokenizer& parser); - void parseModifyConditions(tokenizer &parser); + void parseModifyConditions(tokenizer& parser); - Query parseSubQuery(tokenizer &parser); + Query parseSubQuery(tokenizer& parser); - void parseArray(tokenizer &parser, std::string_view tokText, UpdateEntry *updateField) const; - void parseCommand(tokenizer &parser) const; + void parseArray(tokenizer& parser, std::string_view tokText, UpdateEntry* updateField) const; + void parseCommand(tokenizer& parser) const; static CondType getCondType(std::string_view cond); SqlParsingCtx ctx_; - Query &query_; + Query& query_; }; } // namespace reindexer diff --git a/cpp_src/core/query/sql/sqlsuggester.cc b/cpp_src/core/query/sql/sqlsuggester.cc index 249e05d06..0890c70ea 100644 --- a/cpp_src/core/query/sql/sqlsuggester.cc +++ b/cpp_src/core/query/sql/sqlsuggester.cc @@ -25,15 +25,15 @@ std::vector SQLSuggester::GetSuggestions(std::string_view q, size_t tokenizer tokens{q}; (void)(suggester.Parse(tokens)); // NOLINTBEGIN(bugprone-empty-catch) - } catch (const Error &) { + } catch (const Error&) { } // NOLINTEND(bugprone-empty-catch) - for (SqlParsingCtx::SuggestionData &item : suggester.ctx_.suggestions) { + for (SqlParsingCtx::SuggestionData& item : suggester.ctx_.suggestions) { suggester.checkForTokenSuggestions(item); } - for (auto &it : suggester.ctx_.suggestions) { + for (auto& it : suggester.ctx_.suggestions) { if (!it.variants.empty()) { return {it.variants.begin(), it.variants.end()}; } @@ -72,8 +72,8 @@ std::unordered_map> sqlTokenMatchings = { {ST_GeomFromTextSqlToken, {"ST_GeomFromText"}}, }; -static void getMatchingTokens(int tokenType, const std::string &token, std::unordered_set &variants) { - const std::unordered_set &suggestions = sqlTokenMatchings[tokenType]; +static void getMatchingTokens(int tokenType, const std::string& token, std::unordered_set& variants) { + const std::unordered_set& suggestions = sqlTokenMatchings[tokenType]; for (auto it = suggestions.begin(); it != suggestions.end(); ++it) { if (isBlank(token) || checkIfStartsWith(token, *it)) { variants.insert(*it); @@ -81,21 +81,27 @@ static void getMatchingTokens(int tokenType, const std::string &token, std::unor } } -void SQLSuggester::getMatchingNamespacesNames(const std::string &token, std::unordered_set &variants) { +void SQLSuggester::getMatchingNamespacesNames(const std::string& token, std::unordered_set& variants) { auto namespaces = enumNamespaces_(EnumNamespacesOpts().OnlyNames()); - for (auto &ns : namespaces) { - if (isBlank(token) || checkIfStartsWith(token, ns.name)) variants.insert(ns.name); + for (auto& ns : namespaces) { + if (isBlank(token) || checkIfStartsWith(token, ns.name)) { + variants.insert(ns.name); + } } } -void SQLSuggester::getMatchingFieldsNames(const std::string &token, std::unordered_set &variants) { +void SQLSuggester::getMatchingFieldsNames(const std::string& token, std::unordered_set& variants) { auto namespaces = enumNamespaces_(EnumNamespacesOpts().WithFilter(ctx_.suggestionLinkedNs)); - if (namespaces.empty() || (namespaces.size() > 1 && isBlank(token))) return; + if (namespaces.empty() || (namespaces.size() > 1 && isBlank(token))) { + return; + } auto dotPos = token.find('.'); - for (const auto &ns : namespaces) { - for (auto &idx : ns.indexes) { - if (idx.name_ == "#pk" || idx.name_ == "-tuple") continue; + for (const auto& ns : namespaces) { + for (auto& idx : ns.indexes) { + if (idx.name_ == "#pk" || idx.name_ == "-tuple") { + continue; + } if (isBlank(token) || (dotPos != std::string::npos ? checkIfStartsWith(token, idx.name_) : checkIfStartsWith(token, idx.name_))) { if (dotPos == std::string::npos) { @@ -108,11 +114,11 @@ void SQLSuggester::getMatchingFieldsNames(const std::string &token, std::unorder } if (getSchema_) { - for (const auto &ns : namespaces) { + for (const auto& ns : namespaces) { auto schema = getSchema_(ns.name); if (schema) { auto fieldsSuggestions = schema->GetSuggestions(token); - for (auto &suggestion : fieldsSuggestions) { + for (auto& suggestion : fieldsSuggestions) { variants.insert(std::move(suggestion)); } } @@ -120,7 +126,7 @@ void SQLSuggester::getMatchingFieldsNames(const std::string &token, std::unorder } } -void SQLSuggester::getSuggestionsForToken(SqlParsingCtx::SuggestionData &ctx) { +void SQLSuggester::getSuggestionsForToken(SqlParsingCtx::SuggestionData& ctx) { switch (ctx.tokenType) { case Start: case StartAfterExplain: @@ -203,16 +209,18 @@ void SQLSuggester::getSuggestionsForToken(SqlParsingCtx::SuggestionData &ctx) { } } -bool SQLSuggester::findInPossibleTokens(int type, const std::string &v) { - const std::unordered_set &values = sqlTokenMatchings[type]; +bool SQLSuggester::findInPossibleTokens(int type, const std::string& v) { + const std::unordered_set& values = sqlTokenMatchings[type]; return (values.find(v) != values.end()); } -bool SQLSuggester::findInPossibleFields(const std::string &tok) { +bool SQLSuggester::findInPossibleFields(const std::string& tok) { auto namespaces = enumNamespaces_(EnumNamespacesOpts().WithFilter(ctx_.suggestionLinkedNs)); - if (namespaces.empty()) return false; - if (std::find_if(namespaces[0].indexes.begin(), namespaces[0].indexes.end(), [&](const IndexDef &lhs) { return lhs.name_ == tok; }) != + if (namespaces.empty()) { + return false; + } + if (std::find_if(namespaces[0].indexes.begin(), namespaces[0].indexes.end(), [&](const IndexDef& lhs) { return lhs.name_ == tok; }) != namespaces[0].indexes.end()) { return true; } @@ -223,11 +231,11 @@ bool SQLSuggester::findInPossibleFields(const std::string &tok) { return false; } -bool SQLSuggester::findInPossibleNamespaces(const std::string &tok) { +bool SQLSuggester::findInPossibleNamespaces(const std::string& tok) { return !enumNamespaces_(EnumNamespacesOpts().WithFilter(tok).OnlyNames()).empty(); } -void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data) { +void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData& data) { switch (data.tokenType) { case Start: case StartAfterExplain: @@ -240,10 +248,14 @@ void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data) getSuggestionsForToken(data); break; } - if (data.token == "*") break; + if (data.token == "*") { + break; + } bool isIndex = false, isAggregationFunction = false; isIndex = findInPossibleFields(data.token); - if (!isIndex) isAggregationFunction = findInPossibleTokens(AggregationSqlToken, data.token); + if (!isIndex) { + isAggregationFunction = findInPossibleTokens(AggregationSqlToken, data.token); + } if (!isIndex && !isAggregationFunction) { getSuggestionsForToken(data); } @@ -272,7 +284,9 @@ void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data) getSuggestionsForToken(data); break; } - if (iequals(data.token, "not")) break; + if (iequals(data.token, "not")) { + break; + } if (!findInPossibleFields(data.token)) { getSuggestionsForToken(data); } @@ -283,7 +297,9 @@ void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data) } break; case WhereFieldValueSqlToken: - if (isBlank(data.token)) break; + if (isBlank(data.token)) { + break; + } if (checkIfTokenStartsWith(data.token, "null")) { getSuggestionsForToken(data); break; @@ -382,7 +398,9 @@ void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data) getSuggestionsForToken(data); break; } - if (findInPossibleFields(data.token)) break; + if (findInPossibleFields(data.token)) { + break; + } getSuggestionsForToken(data); break; case FieldNameSqlToken: @@ -395,7 +413,9 @@ void SQLSuggester::checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data) getSuggestionsForToken(data); break; } - if (data.token == "(") break; + if (data.token == "(") { + break; + } if (checkIfTokenStartsWith(data.token, "field")) { getSuggestionsForToken(data); break; diff --git a/cpp_src/core/query/sql/sqlsuggester.h b/cpp_src/core/query/sql/sqlsuggester.h index 274201ed0..0280c5254 100644 --- a/cpp_src/core/query/sql/sqlsuggester.h +++ b/cpp_src/core/query/sql/sqlsuggester.h @@ -26,20 +26,20 @@ class SQLSuggester : public SQLParser { private: /// Finds suggestions for token /// @param ctx - suggestion context. - void getSuggestionsForToken(SqlParsingCtx::SuggestionData &ctx); + void getSuggestionsForToken(SqlParsingCtx::SuggestionData& ctx); /// Checks if token suggestion is required - void checkForTokenSuggestions(SqlParsingCtx::SuggestionData &data); + void checkForTokenSuggestions(SqlParsingCtx::SuggestionData& data); /// Tries to find token value among accepted tokens. - [[nodiscard]] bool findInPossibleTokens(int type, const std::string &v); + [[nodiscard]] bool findInPossibleTokens(int type, const std::string& v); /// Tries to find token value among indexes. - [[nodiscard]] bool findInPossibleFields(const std::string &tok); + [[nodiscard]] bool findInPossibleFields(const std::string& tok); /// Tries to find among possible namespaces. - [[nodiscard]] bool findInPossibleNamespaces(const std::string &tok); + [[nodiscard]] bool findInPossibleNamespaces(const std::string& tok); /// Gets names of indexes that start with 'token'. - void getMatchingFieldsNames(const std::string &token, std::unordered_set &variants); - void getMatchingNamespacesNames(const std::string &token, std::unordered_set &variants); + void getMatchingFieldsNames(const std::string& token, std::unordered_set& variants); + void getMatchingNamespacesNames(const std::string& token, std::unordered_set& variants); EnumNamespacesF enumNamespaces_; GetSchemaF getSchema_; }; diff --git a/cpp_src/core/queryresults/aggregationresult.cc b/cpp_src/core/queryresults/aggregationresult.cc index 921f26382..d054cb93b 100644 --- a/cpp_src/core/queryresults/aggregationresult.cc +++ b/cpp_src/core/queryresults/aggregationresult.cc @@ -54,23 +54,29 @@ AggType AggregationResult::strToAggType(std::string_view type) { return AggUnknown; } -void AggregationResult::GetJSON(WrSerializer &ser) const { +void AggregationResult::GetJSON(WrSerializer& ser) const { JsonBuilder builder(ser); ParameterFieldGetter fieldsGetter; get(builder, ParametersFields(fieldsGetter)); } -void AggregationResult::GetMsgPack(WrSerializer &wrser) const { +void AggregationResult::GetMsgPack(WrSerializer& wrser) const { int elements = 2; - if (value_) ++elements; - if (!facets.empty()) ++elements; - if (!distincts.empty()) ++elements; + if (value_) { + ++elements; + } + if (!facets.empty()) { + ++elements; + } + if (!distincts.empty()) { + ++elements; + } MsgPackBuilder msgpackBuilder(wrser, ObjType::TypeObject, elements); ParameterFieldGetter fieldsGetter; get(msgpackBuilder, ParametersFields(fieldsGetter)); } -void AggregationResult::GetProtobuf(WrSerializer &wrser) const { +void AggregationResult::GetProtobuf(WrSerializer& wrser) const { ProtobufBuilder builder(&wrser, ObjType::TypePlain); get(builder, ParametersFields(kParametersFieldNumbers)); } @@ -84,7 +90,7 @@ Error AggregationResult::FromMsgPack(span msgpack) { return Error(errLogic, "Error unpacking aggregation data in msgpack"); } from(root); - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; @@ -95,13 +101,13 @@ Error AggregationResult::FromJSON(span json) { gason::JsonParser parser; auto root = parser.Parse(json); from(root); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "AggregationResult: %s", ex.what()); } return errOK; } -void AggregationResult::GetProtobufSchema(ProtobufSchemaBuilder &builder) { +void AggregationResult::GetProtobufSchema(ProtobufSchemaBuilder& builder) { ParametersFields fields(kParametersFieldNumbers); ProtobufSchemaBuilder results = builder.Object(0, "AggregationResults"); results.Field(Parameters::Value(), fields.Value(), FieldProps{KeyValueType::Double{}}); diff --git a/cpp_src/core/queryresults/aggregationresult.h b/cpp_src/core/queryresults/aggregationresult.h index 17f36a22f..deb3a9be5 100644 --- a/cpp_src/core/queryresults/aggregationresult.h +++ b/cpp_src/core/queryresults/aggregationresult.h @@ -35,7 +35,7 @@ struct Parameters { template class ParametersFields { public: - explicit ParametersFields(const T &fieldsStorage) : fieldsStorage_(fieldsStorage) {} + explicit ParametersFields(const T& fieldsStorage) : fieldsStorage_(fieldsStorage) {} K Value() const { return fieldsStorage_.at(Parameters::Value()); } K Type() const { return fieldsStorage_.at(Parameters::Type()); } @@ -46,11 +46,11 @@ class ParametersFields { K Fields() const { return fieldsStorage_.at(Parameters::Fields()); } private: - const T &fieldsStorage_; + const T& fieldsStorage_; }; struct FacetResult { - FacetResult(const h_vector &v, int c) noexcept : values(v), count(c) {} + FacetResult(const h_vector& v, int c) noexcept : values(v), count(c) {} FacetResult() noexcept : count(0) {} h_vector values; @@ -58,9 +58,9 @@ struct FacetResult { }; struct AggregationResult { - void GetJSON(WrSerializer &ser) const; - void GetMsgPack(WrSerializer &wrser) const; - void GetProtobuf(WrSerializer &wrser) const; + void GetJSON(WrSerializer& ser) const; + void GetMsgPack(WrSerializer& wrser) const; + void GetProtobuf(WrSerializer& wrser) const; Error FromJSON(span json); Error FromMsgPack(span msgpack); double GetValueOrZero() const noexcept { return value_ ? *value_ : 0; } @@ -74,15 +74,21 @@ struct AggregationResult { PayloadType payloadType; static AggType strToAggType(std::string_view type); - static void GetProtobufSchema(ProtobufSchemaBuilder &); + static void GetProtobufSchema(ProtobufSchemaBuilder&); template void from(Node root) { - const Node &node = root[Parameters::Value()]; + const Node& node = root[Parameters::Value()]; bool isValid = false; - if constexpr (std::is_same_v) isValid = node.isValid(); - if constexpr (std::is_same_v) isValid = !node.empty(); - if (isValid) value_ = node.template As(); + if constexpr (std::is_same_v) { + isValid = node.isValid(); + } + if constexpr (std::is_same_v) { + isValid = !node.empty(); + } + if (isValid) { + value_ = node.template As(); + } type = strToAggType(root[Parameters::Type()].template As()); @@ -105,35 +111,41 @@ struct AggregationResult { } template - void get(Builder &builder, const Fields ¶metersFields) const { - if (value_) builder.Put(parametersFields.Value(), *value_); + void get(Builder& builder, const Fields& parametersFields) const { + if (value_) { + builder.Put(parametersFields.Value(), *value_); + } builder.Put(parametersFields.Type(), AggTypeToStr(type)); if (!facets.empty()) { auto facetsArray = builder.Array(parametersFields.Facets(), facets.size()); - for (auto &facet : facets) { + for (auto& facet : facets) { auto facetObj = facetsArray.Object(0, 2); facetObj.Put(parametersFields.Count(), facet.count); auto valuesArray = facetObj.Array(parametersFields.Values(), facet.values.size()); - for (const auto &v : facet.values) valuesArray.Put(0, v); + for (const auto& v : facet.values) { + valuesArray.Put(0, v); + } } } if (!distincts.empty()) { auto distinctsArray = builder.Array(parametersFields.Distincts(), distincts.size()); - for (const Variant &v : distincts) { + for (const Variant& v : distincts) { distinctsArray.Put(0, v.As(payloadType, distinctsFields)); } } auto fieldsArray = builder.Array(parametersFields.Fields(), fields.size()); - for (auto &v : fields) fieldsArray.Put(0, v); + for (auto& v : fields) { + fieldsArray.Put(0, v); + } fieldsArray.End(); } template - S &DumpFields(S &os) { + S& DumpFields(S& os) { os << '['; bool first = true; - for (const auto &f : fields) { + for (const auto& f : fields) { if (!first) { os << ", "; } diff --git a/cpp_src/core/queryresults/itemref.h b/cpp_src/core/queryresults/itemref.h index 2c417fc3f..49a838cc5 100644 --- a/cpp_src/core/queryresults/itemref.h +++ b/cpp_src/core/queryresults/itemref.h @@ -21,7 +21,9 @@ class ItemRef { valueInitialized_(other.valueInitialized_), nsid_(other.nsid_), sortExprResultsIdx_(other.sortExprResultsIdx_) { - if (valueInitialized_) new (&value_) PayloadValue(std::move(other.value_)); + if (valueInitialized_) { + new (&value_) PayloadValue(std::move(other.value_)); + } } ItemRef(const ItemRef& other) : id_(other.id_), @@ -30,10 +32,14 @@ class ItemRef { valueInitialized_(other.valueInitialized_), nsid_(other.nsid_), sortExprResultsIdx_(other.sortExprResultsIdx_) { - if (valueInitialized_) new (&value_) PayloadValue(other.value_); + if (valueInitialized_) { + new (&value_) PayloadValue(other.value_); + } } ItemRef& operator=(ItemRef&& other) { - if (&other == this) return *this; + if (&other == this) { + return *this; + } id_ = other.id_; proc_ = other.proc_; raw_ = other.raw_; @@ -56,7 +62,9 @@ class ItemRef { return *this; } ItemRef& operator=(const ItemRef& other) { - if (&other == this) return *this; + if (&other == this) { + return *this; + } id_ = other.id_; proc_ = other.proc_; raw_ = other.raw_; @@ -79,7 +87,9 @@ class ItemRef { return *this; } ~ItemRef() { - if (valueInitialized_) value_.~PayloadValue(); + if (valueInitialized_) { + value_.~PayloadValue(); + } } IdType Id() const noexcept { return id_; } diff --git a/cpp_src/core/queryresults/joinresults.cc b/cpp_src/core/queryresults/joinresults.cc index 1b24c121a..496aab927 100644 --- a/cpp_src/core/queryresults/joinresults.cc +++ b/cpp_src/core/queryresults/joinresults.cc @@ -6,15 +6,23 @@ namespace reindexer { namespace joins { bool JoinedFieldIterator::operator==(const JoinedFieldIterator& other) const { - if (joinRes_ != other.joinRes_) throw Error(errLogic, "Comparising joined fields of different namespaces!"); - if (offsets_ != other.offsets_) throw Error(errLogic, "Comparising joined fields of different items!"); - if (order_ != other.order_) return false; + if (joinRes_ != other.joinRes_) { + throw Error(errLogic, "Comparising joined fields of different namespaces!"); + } + if (offsets_ != other.offsets_) { + throw Error(errLogic, "Comparising joined fields of different items!"); + } + if (order_ != other.order_) { + return false; + } return true; } void JoinedFieldIterator::updateOffset() noexcept { currField_ = -1; - if (order_ == joinRes_->GetJoinedSelectorsCount()) return; + if (order_ == joinRes_->GetJoinedSelectorsCount()) { + return; + } size_t i = 0; for (; i < offsets_->size(); ++i) { @@ -34,7 +42,9 @@ ItemImpl JoinedFieldIterator::GetItem(int itemIdx, const PayloadType& pt, const } QueryResults JoinedFieldIterator::ToQueryResults() const { - if (ItemsCount() == 0) return QueryResults(); + if (ItemsCount() == 0) { + return QueryResults(); + } ItemRefVector::const_iterator begin = joinRes_->items_.begin() + currOffset_; ItemRefVector::const_iterator end = begin + ItemsCount(); return QueryResults(begin, end); @@ -96,7 +106,9 @@ ItemIterator ItemIterator::CreateFrom(const QueryResults::Iterator& it) noexcept static NamespaceResults empty; static ItemIterator ret(&empty, 0); auto& itemRef = it.qr_->Items()[it.idx_]; - if ((itemRef.Nsid() >= it.qr_->joined_.size())) return ret; + if ((itemRef.Nsid() >= it.qr_->joined_.size())) { + return ret; + } return ItemIterator(&(it.qr_->joined_[itemRef.Nsid()]), itemRef.Id()); } diff --git a/cpp_src/core/queryresults/joinresults.h b/cpp_src/core/queryresults/joinresults.h index 07a2a062e..3fce7be52 100644 --- a/cpp_src/core/queryresults/joinresults.h +++ b/cpp_src/core/queryresults/joinresults.h @@ -73,7 +73,9 @@ class JoinedFieldIterator { JoinedFieldIterator(const NamespaceResults* parent, const ItemOffsets& offsets, uint8_t joinedFieldOrder) noexcept : joinRes_(parent), offsets_(&offsets), order_(joinedFieldOrder) { - if (offsets_->size() > 0) updateOffset(); + if (offsets_->size() > 0) { + updateOffset(); + } } bool operator==(const JoinedFieldIterator& other) const; diff --git a/cpp_src/core/queryresults/queryresults.cc b/cpp_src/core/queryresults/queryresults.cc index df94408b5..0a1ee037d 100644 --- a/cpp_src/core/queryresults/queryresults.cc +++ b/cpp_src/core/queryresults/queryresults.cc @@ -14,9 +14,9 @@ namespace reindexer { void QueryResults::AddNamespace(NamespaceImplPtr ns, [[maybe_unused]] bool noLock) { assertrx(noLock); - const NamespaceImpl *nsPtr = ns.get(); + const NamespaceImpl* nsPtr = ns.get(); auto strHolder = ns->strHolder(); - const auto it = std::find_if(nsData_.cbegin(), nsData_.cend(), [nsPtr](const NsDataHolder &nsData) { return nsData.ns == nsPtr; }); + const auto it = std::find_if(nsData_.cbegin(), nsData_.cend(), [nsPtr](const NsDataHolder& nsData) { return nsData.ns == nsPtr; }); if (it != nsData_.cend()) { assertrx(it->strHolder.get() == strHolder.get()); return; @@ -24,10 +24,10 @@ void QueryResults::AddNamespace(NamespaceImplPtr ns, [[maybe_unused]] bool noLoc nsData_.emplace_back(std::move(ns), std::move(strHolder)); } -void QueryResults::AddNamespace(NamespaceImpl *ns, [[maybe_unused]] bool noLock) { +void QueryResults::AddNamespace(NamespaceImpl* ns, [[maybe_unused]] bool noLock) { assertrx(noLock); auto strHolder = ns->strHolder(); - const auto it = std::find_if(nsData_.cbegin(), nsData_.cend(), [ns](const NsDataHolder &nsData) { return nsData.ns == ns; }); + const auto it = std::find_if(nsData_.cbegin(), nsData_.cend(), [ns](const NsDataHolder& nsData) { return nsData.ns == ns; }); if (it != nsData_.cend()) { assertrx(it->strHolder.get() == strHolder.get()); return; @@ -35,15 +35,15 @@ void QueryResults::AddNamespace(NamespaceImpl *ns, [[maybe_unused]] bool noLock) nsData_.emplace_back(ns, std::move(strHolder)); } -void QueryResults::RemoveNamespace(const NamespaceImpl *ns) { - const auto it = std::find_if(nsData_.begin(), nsData_.end(), [ns](const NsDataHolder &nsData) { return nsData.ns == ns; }); +void QueryResults::RemoveNamespace(const NamespaceImpl* ns) { + const auto it = std::find_if(nsData_.begin(), nsData_.end(), [ns](const NsDataHolder& nsData) { return nsData.ns == ns; }); assertrx(it != nsData_.end()); nsData_.erase(it); } struct QueryResults::Context { Context() = default; - Context(PayloadType type, TagsMatcher tagsMatcher, const FieldsSet &fieldsFilter, std::shared_ptr schema) + Context(PayloadType type, TagsMatcher tagsMatcher, const FieldsSet& fieldsFilter, std::shared_ptr schema) : type_(std::move(type)), tagsMatcher_(std::move(tagsMatcher)), fieldsFilter_(fieldsFilter), schema_(std::move(schema)) {} PayloadType type_; @@ -57,7 +57,7 @@ static_assert(QueryResults::kSizeofContext >= sizeof(QueryResults::Context), QueryResults::QueryResults(std::initializer_list l) : items_(l) {} QueryResults::QueryResults(int /*flags*/) {} -QueryResults::QueryResults(QueryResults &&obj) noexcept +QueryResults::QueryResults(QueryResults&& obj) noexcept : joined_(std::move(obj.joined_)), aggregationResults(std::move(obj.aggregationResults)), totalCount(obj.totalCount), @@ -74,9 +74,9 @@ QueryResults::QueryResults(QueryResults &&obj) noexcept obj.isWalQuery_ = false; } -QueryResults::QueryResults(const ItemRefVector::const_iterator &begin, const ItemRefVector::const_iterator &end) : items_(begin, end) {} +QueryResults::QueryResults(const ItemRefVector::const_iterator& begin, const ItemRefVector::const_iterator& end) : items_(begin, end) {} -QueryResults &QueryResults::operator=(QueryResults &&obj) noexcept { +QueryResults& QueryResults::operator=(QueryResults&& obj) noexcept { if (this != &obj) { items_ = std::move(obj.items_); assertrx(!obj.items_.size()); @@ -107,22 +107,30 @@ void QueryResults::Clear() { *this = QueryResults(); } void QueryResults::Erase(ItemRefVector::iterator start, ItemRefVector::iterator finish) { items_.erase(start, finish); } -void QueryResults::Add(const ItemRef &i) { items_.push_back(i); } +void QueryResults::Add(const ItemRef& i) { items_.push_back(i); } std::string QueryResults::Dump() const { std::string buf; for (size_t i = 0; i < items_.size(); ++i) { - if (&items_[i] != &*items_.begin()) buf += ","; + if (&items_[i] != &*items_.begin()) { + buf += ","; + } buf += std::to_string(items_[i].Id()); - if (joined_.empty()) continue; + if (joined_.empty()) { + continue; + } Iterator itemIt{this, int(i), errOK, {}}; auto joinIt = itemIt.GetJoined(); if (joinIt.getJoinedItemsCount() > 0) { buf += "["; for (auto fieldIt = joinIt.begin(); fieldIt != joinIt.end(); ++fieldIt) { - if (fieldIt != joinIt.begin()) buf += ";"; + if (fieldIt != joinIt.begin()) { + buf += ";"; + } for (int j = 0; j < fieldIt.ItemsCount(); ++j) { - if (j != 0) buf += ","; + if (j != 0) { + buf += ","; + } buf += std::to_string(fieldIt[j].Id()); } } @@ -135,7 +143,9 @@ std::string QueryResults::Dump() const { h_vector QueryResults::GetNamespaces() const { h_vector ret; ret.reserve(ctxs.size()); - for (auto &ctx : ctxs) ret.push_back(ctx.type_.Name()); + for (auto& ctx : ctxs) { + ret.push_back(ctx.type_.Name()); + } return ret; } @@ -149,7 +159,7 @@ int QueryResults::GetJoinedNsCtxIndex(int nsid) const noexcept { class QueryResults::EncoderDatasourceWithJoins final : public IEncoderDatasourceWithJoins { public: - EncoderDatasourceWithJoins(const joins::ItemIterator &joinedItemIt, const ContextsVector &ctxs, Iterator::NsNamesCache &nsNamesCache, + EncoderDatasourceWithJoins(const joins::ItemIterator& joinedItemIt, const ContextsVector& ctxs, Iterator::NsNamesCache& nsNamesCache, int ctxIdx, size_t nsid, size_t joinedCount) noexcept : joinedItemIt_(joinedItemIt), ctxs_(ctxs), nsNamesCache_(nsNamesCache), ctxId_(ctxIdx), nsid_{nsid} { if (nsNamesCache.size() <= nsid_) { @@ -161,14 +171,14 @@ class QueryResults::EncoderDatasourceWithJoins final : public IEncoderDatasource fast_hash_map namesCounters; assertrx_dbg(ctxs_.size() >= ctxId_ + joinedCount); for (size_t i = ctxId_, end = ctxId_ + joinedCount; i < end; ++i) { - const std::string &n = ctxs_[i].type_.Name(); + const std::string& n = ctxs_[i].type_.Name(); if (auto [it, emplaced] = namesCounters.emplace(n, -1); !emplaced) { --it->second; } } for (size_t i = ctxId_, end = ctxId_ + joinedCount; i < end; ++i) { - const std::string &n = ctxs_[i].type_.Name(); - int &count = namesCounters[n]; + const std::string& n = ctxs_[i].type_.Name(); + int& count = namesCounters[n]; if (count < 0) { if (count == -1) { nsNamesCache[nsid_].emplace_back(n); @@ -191,57 +201,59 @@ class QueryResults::EncoderDatasourceWithJoins final : public IEncoderDatasource } ConstPayload GetJoinedItemPayload(size_t rowid, size_t plIndex) const override final { auto fieldIt = joinedItemIt_.at(rowid); - const ItemRef &itemRef = fieldIt[plIndex]; - const Context &ctx = ctxs_[ctxId_ + rowid]; + const ItemRef& itemRef = fieldIt[plIndex]; + const Context& ctx = ctxs_[ctxId_ + rowid]; return ConstPayload(ctx.type_, itemRef.Value()); } - const TagsMatcher &GetJoinedItemTagsMatcher(size_t rowid) noexcept override final { - const Context &ctx = ctxs_[ctxId_ + rowid]; + const TagsMatcher& GetJoinedItemTagsMatcher(size_t rowid) noexcept override final { + const Context& ctx = ctxs_[ctxId_ + rowid]; return ctx.tagsMatcher_; } - virtual const FieldsSet &GetJoinedItemFieldsFilter(size_t rowid) noexcept override final { - const Context &ctx = ctxs_[ctxId_ + rowid]; + virtual const FieldsSet& GetJoinedItemFieldsFilter(size_t rowid) noexcept override final { + const Context& ctx = ctxs_[ctxId_ + rowid]; return ctx.fieldsFilter_; } - const std::string &GetJoinedItemNamespace(size_t rowid) const noexcept override final { return nsNamesCache_[nsid_][rowid]; } + const std::string& GetJoinedItemNamespace(size_t rowid) const noexcept override final { return nsNamesCache_[nsid_][rowid]; } private: - const joins::ItemIterator &joinedItemIt_; - const ContextsVector &ctxs_; - const Iterator::NsNamesCache &nsNamesCache_; + const joins::ItemIterator& joinedItemIt_; + const ContextsVector& ctxs_; + const Iterator::NsNamesCache& nsNamesCache_; const int ctxId_; const size_t nsid_; }; class AdditionalDatasource : public IAdditionalDatasource { public: - AdditionalDatasource(double r, IEncoderDatasourceWithJoins *jds) noexcept : joinsDs_(jds), withRank_(true), rank_(r) {} - AdditionalDatasource(IEncoderDatasourceWithJoins *jds) noexcept : joinsDs_(jds), withRank_(false), rank_(0.0) {} - void PutAdditionalFields(JsonBuilder &builder) const override final { - if (withRank_) builder.Put("rank()", rank_); + AdditionalDatasource(double r, IEncoderDatasourceWithJoins* jds) noexcept : joinsDs_(jds), withRank_(true), rank_(r) {} + AdditionalDatasource(IEncoderDatasourceWithJoins* jds) noexcept : joinsDs_(jds), withRank_(false), rank_(0.0) {} + void PutAdditionalFields(JsonBuilder& builder) const override final { + if (withRank_) { + builder.Put("rank()", rank_); + } } - IEncoderDatasourceWithJoins *GetJoinsDatasource() noexcept override final { return joinsDs_; } + IEncoderDatasourceWithJoins* GetJoinsDatasource() noexcept override final { return joinsDs_; } private: - IEncoderDatasourceWithJoins *joinsDs_; + IEncoderDatasourceWithJoins* joinsDs_; bool withRank_; double rank_; }; class AdditionalDatasourceCSV : public IAdditionalDatasource { public: - AdditionalDatasourceCSV(IEncoderDatasourceWithJoins *jds) noexcept : joinsDs_(jds) {} - void PutAdditionalFields(CsvBuilder &) const override final {} - IEncoderDatasourceWithJoins *GetJoinsDatasource() noexcept override final { return joinsDs_; } + AdditionalDatasourceCSV(IEncoderDatasourceWithJoins* jds) noexcept : joinsDs_(jds) {} + void PutAdditionalFields(CsvBuilder&) const override final {} + IEncoderDatasourceWithJoins* GetJoinsDatasource() noexcept override final { return joinsDs_; } private: - IEncoderDatasourceWithJoins *joinsDs_; + IEncoderDatasourceWithJoins* joinsDs_; }; -void QueryResults::encodeJSON(int idx, WrSerializer &ser, Iterator::NsNamesCache &nsNamesCache) const { - auto &itemRef = items_[idx]; +void QueryResults::encodeJSON(int idx, WrSerializer& ser, Iterator::NsNamesCache& nsNamesCache) const { + auto& itemRef = items_[idx]; assertrx(ctxs.size() > itemRef.Nsid()); - auto &ctx = ctxs[itemRef.Nsid()]; + auto& ctx = ctxs[itemRef.Nsid()]; if (itemRef.Value().IsFree()) { ser << "{}"; @@ -277,10 +289,10 @@ void QueryResults::encodeJSON(int idx, WrSerializer &ser, Iterator::NsNamesCache joins::ItemIterator QueryResults::Iterator::GetJoined() { return reindexer::joins::ItemIterator::CreateFrom(*this); } -Error QueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLen) { - auto &itemRef = qr_->items_[idx_]; +Error QueryResults::Iterator::GetMsgPack(WrSerializer& wrser, bool withHdrLen) { + auto& itemRef = qr_->items_[idx_]; assertrx(qr_->ctxs.size() > itemRef.Nsid()); - auto &ctx = qr_->ctxs[itemRef.Nsid()]; + auto& ctx = qr_->ctxs[itemRef.Nsid()]; if (itemRef.Value().IsFree()) { return Error(errNotFound, "Item not found"); @@ -289,8 +301,8 @@ Error QueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLen) { int startTag = 0; ConstPayload pl(ctx.type_, itemRef.Value()); MsgPackEncoder msgpackEncoder(&ctx.tagsMatcher_); - const TagsLengths &tagsLengths = msgpackEncoder.GetTagsMeasures(pl); - MsgPackBuilder msgpackBuilder(wrser, &tagsLengths, &startTag, ObjType::TypePlain, const_cast(&ctx.tagsMatcher_)); + const TagsLengths& tagsLengths = msgpackEncoder.GetTagsMeasures(pl); + MsgPackBuilder msgpackBuilder(wrser, &tagsLengths, &startTag, ObjType::TypePlain, const_cast(&ctx.tagsMatcher_)); if (withHdrLen) { auto slicePosSaver = wrser.StartSlice(); msgpackEncoder.Encode(pl, msgpackBuilder); @@ -300,10 +312,10 @@ Error QueryResults::Iterator::GetMsgPack(WrSerializer &wrser, bool withHdrLen) { return errOK; } -Error QueryResults::Iterator::GetProtobuf(WrSerializer &wrser, bool withHdrLen) { - auto &itemRef = qr_->items_[idx_]; +Error QueryResults::Iterator::GetProtobuf(WrSerializer& wrser, bool withHdrLen) { + auto& itemRef = qr_->items_[idx_]; assertrx(qr_->ctxs.size() > itemRef.Nsid()); - auto &ctx = qr_->ctxs[itemRef.Nsid()]; + auto& ctx = qr_->ctxs[itemRef.Nsid()]; if (itemRef.Value().IsFree()) { return Error(errNotFound, "Item not found"); @@ -311,7 +323,7 @@ Error QueryResults::Iterator::GetProtobuf(WrSerializer &wrser, bool withHdrLen) ConstPayload pl(ctx.type_, itemRef.Value()); ProtobufEncoder encoder(&ctx.tagsMatcher_); - ProtobufBuilder builder(&wrser, ObjType::TypePlain, ctx.schema_.get(), const_cast(&ctx.tagsMatcher_)); + ProtobufBuilder builder(&wrser, ObjType::TypePlain, ctx.schema_.get(), const_cast(&ctx.tagsMatcher_)); if (withHdrLen) { auto slicePosSaver = wrser.StartSlice(); encoder.Encode(pl, builder); @@ -322,7 +334,7 @@ Error QueryResults::Iterator::GetProtobuf(WrSerializer &wrser, bool withHdrLen) return errOK; } -Error QueryResults::Iterator::GetJSON(WrSerializer &ser, bool withHdrLen) { +Error QueryResults::Iterator::GetJSON(WrSerializer& ser, bool withHdrLen) { try { if (withHdrLen) { auto slicePosSaver = ser.StartSlice(); @@ -330,7 +342,7 @@ Error QueryResults::Iterator::GetJSON(WrSerializer &ser, bool withHdrLen) { } else { qr_->encodeJSON(idx_, ser, nsNamesCache); } - } catch (const Error &err) { + } catch (const Error& err) { err_ = err; return err; } @@ -341,7 +353,7 @@ CsvOrdering QueryResults::MakeCSVTagOrdering(unsigned limit, unsigned offset) co if (!ctxs[0].fieldsFilter_.empty()) { std::vector ordering; ordering.reserve(ctxs[0].fieldsFilter_.size()); - for (const auto &tag : ctxs[0].fieldsFilter_) { + for (const auto& tag : ctxs[0].fieldsFilter_) { ordering.emplace_back(tag); } return ordering; @@ -351,7 +363,7 @@ CsvOrdering QueryResults::MakeCSVTagOrdering(unsigned limit, unsigned offset) co ordering.reserve(128); fast_hash_set fieldsTmIds; WrSerializer ser; - const auto &tm = getTagsMatcher(0); + const auto& tm = getTagsMatcher(0); Iterator::NsNamesCache nsNamesCache; for (size_t i = offset; i < items_.size() && i < offset + limit; ++i) { ser.Reset(); @@ -360,7 +372,7 @@ CsvOrdering QueryResults::MakeCSVTagOrdering(unsigned limit, unsigned offset) co gason::JsonParser parser; auto jsonNode = parser.Parse(giftStr(ser.Slice())); - for (const auto &child : jsonNode) { + for (const auto& child : jsonNode) { auto [it, inserted] = fieldsTmIds.insert(tm.name2tag(child.key)); if (inserted && *it > 0) { ordering.emplace_back(*it); @@ -370,11 +382,11 @@ CsvOrdering QueryResults::MakeCSVTagOrdering(unsigned limit, unsigned offset) co return ordering; } -Error QueryResults::Iterator::GetCSV(WrSerializer &ser, CsvOrdering &ordering) noexcept { +Error QueryResults::Iterator::GetCSV(WrSerializer& ser, CsvOrdering& ordering) noexcept { try { - auto &itemRef = qr_->items_[idx_]; + auto& itemRef = qr_->items_[idx_]; assertrx(qr_->ctxs.size() > itemRef.Nsid()); - auto &ctx = qr_->ctxs[itemRef.Nsid()]; + auto& ctx = qr_->ctxs[itemRef.Nsid()]; if (itemRef.Value().IsFree()) { return Error(errNotFound, "Item not found"); @@ -401,11 +413,11 @@ Error QueryResults::Iterator::GetCSV(WrSerializer &ser, CsvOrdering &ordering) n return errOK; } -Error QueryResults::Iterator::GetCJSON(WrSerializer &ser, bool withHdrLen) { +Error QueryResults::Iterator::GetCJSON(WrSerializer& ser, bool withHdrLen) { try { - auto &itemRef = qr_->items_[idx_]; + auto& itemRef = qr_->items_[idx_]; assertrx(qr_->ctxs.size() > itemRef.Nsid()); - auto &ctx = qr_->ctxs[itemRef.Nsid()]; + auto& ctx = qr_->ctxs[itemRef.Nsid()]; if (itemRef.Value().IsFree()) { return Error(errNotFound, "Item not found"); @@ -421,7 +433,7 @@ Error QueryResults::Iterator::GetCJSON(WrSerializer &ser, bool withHdrLen) { } else { cjsonEncoder.Encode(pl, builder); } - } catch (const Error &err) { + } catch (const Error& err) { err_ = err; return err; } @@ -429,10 +441,10 @@ Error QueryResults::Iterator::GetCJSON(WrSerializer &ser, bool withHdrLen) { } Item QueryResults::Iterator::GetItem(bool enableHold) { - auto &itemRef = qr_->items_[idx_]; + auto& itemRef = qr_->items_[idx_]; assertrx(qr_->ctxs.size() > itemRef.Nsid()); - auto &ctx = qr_->ctxs[itemRef.Nsid()]; + auto& ctx = qr_->ctxs[itemRef.Nsid()]; if (itemRef.Value().IsFree()) { return Item(Error(errNotFound, "Item not found")); @@ -441,7 +453,9 @@ Item QueryResults::Iterator::GetItem(bool enableHold) { auto item = Item(new ItemImpl(ctx.type_, itemRef.Value(), ctx.tagsMatcher_, ctx.schema_)); item.impl_->payloadValue_.Clone(); if (enableHold) { - if (!item.impl_->keyStringsHolder_) item.impl_->keyStringsHolder_.reset(new std::vector); + if (!item.impl_->keyStringsHolder_) { + item.impl_->keyStringsHolder_.reset(new std::vector); + } Payload{ctx.type_, item.impl_->payloadValue_}.CopyStrings(*(item.impl_->keyStringsHolder_)); } @@ -449,7 +463,7 @@ Item QueryResults::Iterator::GetItem(bool enableHold) { return item; } -void QueryResults::AddItem(Item &item, bool withData, bool enableHold) { +void QueryResults::AddItem(Item& item, bool withData, bool enableHold) { auto ritem = item.impl_; if (item.GetID() != -1) { if (ctxs.empty()) { @@ -467,15 +481,15 @@ void QueryResults::AddItem(Item &item, bool withData, bool enableHold) { } } -const TagsMatcher &QueryResults::getTagsMatcher(int nsid) const noexcept { return ctxs[nsid].tagsMatcher_; } +const TagsMatcher& QueryResults::getTagsMatcher(int nsid) const noexcept { return ctxs[nsid].tagsMatcher_; } -const PayloadType &QueryResults::getPayloadType(int nsid) const noexcept { return ctxs[nsid].type_; } +const PayloadType& QueryResults::getPayloadType(int nsid) const noexcept { return ctxs[nsid].type_; } -const FieldsSet &QueryResults::getFieldsFilter(int nsid) const noexcept { return ctxs[nsid].fieldsFilter_; } +const FieldsSet& QueryResults::getFieldsFilter(int nsid) const noexcept { return ctxs[nsid].fieldsFilter_; } -TagsMatcher &QueryResults::getTagsMatcher(int nsid) noexcept { return ctxs[nsid].tagsMatcher_; } +TagsMatcher& QueryResults::getTagsMatcher(int nsid) noexcept { return ctxs[nsid].tagsMatcher_; } -PayloadType &QueryResults::getPayloadType(int nsid) noexcept { return ctxs[nsid].type_; } +PayloadType& QueryResults::getPayloadType(int nsid) noexcept { return ctxs[nsid].type_; } std::shared_ptr QueryResults::getSchema(int nsid) const noexcept { return ctxs[nsid].schema_; } @@ -484,17 +498,19 @@ int QueryResults::getNsNumber(int nsid) const noexcept { return ctxs[nsid].schema_->GetProtobufNsNumber(); } -void QueryResults::addNSContext(const PayloadType &type, const TagsMatcher &tagsMatcher, const FieldsSet &filter, +void QueryResults::addNSContext(const PayloadType& type, const TagsMatcher& tagsMatcher, const FieldsSet& filter, std::shared_ptr schema) { - if (filter.getTagsPathsLength()) nonCacheableData = true; + if (filter.getTagsPathsLength()) { + nonCacheableData = true; + } ctxs.push_back(Context(type, tagsMatcher, filter, std::move(schema))); } -QueryResults::NsDataHolder::NsDataHolder(QueryResults::NamespaceImplPtr &&_ns, StringsHolderPtr &&strHldr) noexcept +QueryResults::NsDataHolder::NsDataHolder(QueryResults::NamespaceImplPtr&& _ns, StringsHolderPtr&& strHldr) noexcept : nsPtr_{std::move(_ns)}, ns(nsPtr_.get()), strHolder{std::move(strHldr)} {} -QueryResults::NsDataHolder::NsDataHolder(NamespaceImpl *_ns, StringsHolderPtr &&strHldr) noexcept +QueryResults::NsDataHolder::NsDataHolder(NamespaceImpl* _ns, StringsHolderPtr&& strHldr) noexcept : ns(_ns), strHolder(std::move(strHldr)) {} } // namespace reindexer diff --git a/cpp_src/core/queryresults/queryresults.h b/cpp_src/core/queryresults/queryresults.h index dc3f510d5..880f1e80a 100644 --- a/cpp_src/core/queryresults/queryresults.h +++ b/cpp_src/core/queryresults/queryresults.h @@ -38,27 +38,27 @@ class QueryResults { using NamespaceImplPtr = intrusive_ptr; QueryResults(int flags = 0); - QueryResults(const ItemRefVector::const_iterator &b, const ItemRefVector::const_iterator &e); + QueryResults(const ItemRefVector::const_iterator& b, const ItemRefVector::const_iterator& e); QueryResults(std::initializer_list l); - QueryResults(const QueryResults &) = delete; - QueryResults(QueryResults &&) noexcept; + QueryResults(const QueryResults&) = delete; + QueryResults(QueryResults&&) noexcept; ~QueryResults(); - QueryResults &operator=(const QueryResults &) = delete; - QueryResults &operator=(QueryResults &&obj) noexcept; - void Add(const ItemRef &); + QueryResults& operator=(const QueryResults&) = delete; + QueryResults& operator=(QueryResults&& obj) noexcept; + void Add(const ItemRef&); // use enableHold = false only if you are sure that the queryResults will be destroyed before the item // or if data from the item are contained in namespace added to the queryResults // enableHold is ignored when withData = false - void AddItem(Item &item, bool withData = false, bool enableHold = true); + void AddItem(Item& item, bool withData = false, bool enableHold = true); std::string Dump() const; void Erase(ItemRefVector::iterator begin, ItemRefVector::iterator end); size_t Count() const noexcept { return items_.size(); } size_t TotalCount() const noexcept { return totalCount; } - const std::string &GetExplainResults() const & noexcept { return explainResults; } - const std::string &GetExplainResults() const && = delete; - std::string &&MoveExplainResults() & noexcept { return std::move(explainResults); } - const std::vector &GetAggregationResults() const & noexcept { return aggregationResults; } - const std::vector &GetAggregationResults() const && = delete; + const std::string& GetExplainResults() const& noexcept { return explainResults; } + const std::string& GetExplainResults() const&& = delete; + std::string&& MoveExplainResults() & noexcept { return std::move(explainResults); } + const std::vector& GetAggregationResults() const& noexcept { return aggregationResults; } + const std::vector& GetAggregationResults() const&& = delete; void Clear(); h_vector GetNamespaces() const; bool IsCacheEnabled() const { return !nonCacheableData; } @@ -67,38 +67,38 @@ class QueryResults { class Iterator { public: - Error GetJSON(WrSerializer &wrser, bool withHdrLen = true); - Error GetCJSON(WrSerializer &wrser, bool withHdrLen = true); - Error GetMsgPack(WrSerializer &wrser, bool withHdrLen = true); - Error GetProtobuf(WrSerializer &wrser, bool withHdrLen = true); - Error GetCSV(WrSerializer &wrser, CsvOrdering &ordering) noexcept; + Error GetJSON(WrSerializer& wrser, bool withHdrLen = true); + Error GetCJSON(WrSerializer& wrser, bool withHdrLen = true); + Error GetMsgPack(WrSerializer& wrser, bool withHdrLen = true); + Error GetProtobuf(WrSerializer& wrser, bool withHdrLen = true); + Error GetCSV(WrSerializer& wrser, CsvOrdering& ordering) noexcept; // use enableHold = false only if you are sure that the item will be destroyed before the queryResults Item GetItem(bool enableHold = true); joins::ItemIterator GetJoined(); - const ItemRef &GetItemRef() const noexcept { return qr_->items_[idx_]; } + const ItemRef& GetItemRef() const noexcept { return qr_->items_[idx_]; } int64_t GetLSN() const noexcept { return qr_->items_[idx_].Value().GetLSN(); } bool IsRaw() const noexcept { return qr_->items_[idx_].Raw(); } std::string_view GetRaw() const noexcept { - auto &itemRef = qr_->items_[idx_]; + auto& itemRef = qr_->items_[idx_]; assertrx(itemRef.Raw()); - return std::string_view(reinterpret_cast(itemRef.Value().Ptr()), itemRef.Value().GetCapacity()); + return std::string_view(reinterpret_cast(itemRef.Value().Ptr()), itemRef.Value().GetCapacity()); } - Iterator &operator++() noexcept { + Iterator& operator++() noexcept { idx_++; return *this; } - Iterator &operator+(int delta) noexcept { + Iterator& operator+(int delta) noexcept { idx_ += delta; return *this; } Error Status() const noexcept { return err_; } - bool operator==(const Iterator &other) const noexcept { return idx_ == other.idx_; } - bool operator!=(const Iterator &other) const noexcept { return !operator==(other); } - Iterator &operator*() noexcept { return *this; } + bool operator==(const Iterator& other) const noexcept { return idx_ == other.idx_; } + bool operator!=(const Iterator& other) const noexcept { return !operator==(other); } + Iterator& operator*() noexcept { return *this; } - const QueryResults *qr_; + const QueryResults* qr_; int idx_; Error err_; using NsNamesCache = h_vector, 1>; @@ -127,28 +127,28 @@ class QueryResults { using ContextsVector = h_vector; ContextsVector ctxs; - void addNSContext(const PayloadType &type, const TagsMatcher &tagsMatcher, const FieldsSet &fieldsFilter, + void addNSContext(const PayloadType& type, const TagsMatcher& tagsMatcher, const FieldsSet& fieldsFilter, std::shared_ptr schema); - const TagsMatcher &getTagsMatcher(int nsid) const noexcept; - const PayloadType &getPayloadType(int nsid) const noexcept; - const FieldsSet &getFieldsFilter(int nsid) const noexcept; - TagsMatcher &getTagsMatcher(int nsid) noexcept; - PayloadType &getPayloadType(int nsid) noexcept; + const TagsMatcher& getTagsMatcher(int nsid) const noexcept; + const PayloadType& getPayloadType(int nsid) const noexcept; + const FieldsSet& getFieldsFilter(int nsid) const noexcept; + TagsMatcher& getTagsMatcher(int nsid) noexcept; + PayloadType& getPayloadType(int nsid) noexcept; std::shared_ptr getSchema(int nsid) const noexcept; int getNsNumber(int nsid) const noexcept; int getMergedNSCount() const noexcept { return ctxs.size(); } - ItemRefVector &Items() noexcept { return items_; } - const ItemRefVector &Items() const { return items_; } + ItemRefVector& Items() noexcept { return items_; } + const ItemRefVector& Items() const { return items_; } int GetJoinedNsCtxIndex(int nsid) const noexcept; // Add owning ns pointer // noLock has always to be 'true' (i.e. this method can only be called unders Namespace's lock) void AddNamespace(NamespaceImplPtr, bool noLock); // Add non-owning ns pointer // noLock has always to be 'true' (i.e. this method can only be called unders Namespace's lock) - void AddNamespace(NamespaceImpl *, bool noLock); - void RemoveNamespace(const NamespaceImpl *ns); - bool IsNamespaceAdded(const NamespaceImpl *ns) const noexcept { - return std::find_if(nsData_.cbegin(), nsData_.cend(), [ns](const NsDataHolder &nsData) { return nsData.ns == ns; }) != + void AddNamespace(NamespaceImpl*, bool noLock); + void RemoveNamespace(const NamespaceImpl* ns); + bool IsNamespaceAdded(const NamespaceImpl* ns) const noexcept { + return std::find_if(nsData_.cbegin(), nsData_.cend(), [ns](const NsDataHolder& nsData) { return nsData.ns == ns; }) != nsData_.cend(); } void MarkAsWALQuery() noexcept { isWalQuery_ = true; } @@ -159,7 +159,7 @@ class QueryResults { private: class EncoderDatasourceWithJoins; class EncoderAdditionalDatasource; - void encodeJSON(int idx, WrSerializer &ser, Iterator::NsNamesCache &) const; + void encodeJSON(int idx, WrSerializer& ser, Iterator::NsNamesCache&) const; public: ItemRefVector items_; @@ -168,18 +168,18 @@ class QueryResults { friend SelectFunctionsHolder; class NsDataHolder { public: - NsDataHolder(NamespaceImplPtr &&_ns, StringsHolderPtr &&strHldr) noexcept; - NsDataHolder(NamespaceImpl *_ns, StringsHolderPtr &&strHldr) noexcept; - NsDataHolder(const NsDataHolder &) = delete; - NsDataHolder(NsDataHolder &&) noexcept = default; - NsDataHolder &operator=(const NsDataHolder &) = delete; - NsDataHolder &operator=(NsDataHolder &&) = default; + NsDataHolder(NamespaceImplPtr&& _ns, StringsHolderPtr&& strHldr) noexcept; + NsDataHolder(NamespaceImpl* _ns, StringsHolderPtr&& strHldr) noexcept; + NsDataHolder(const NsDataHolder&) = delete; + NsDataHolder(NsDataHolder&&) noexcept = default; + NsDataHolder& operator=(const NsDataHolder&) = delete; + NsDataHolder& operator=(NsDataHolder&&) = default; private: NamespaceImplPtr nsPtr_; public: - NamespaceImpl *ns; + NamespaceImpl* ns; StringsHolderPtr strHolder; }; diff --git a/cpp_src/core/queryresults/tableviewbuilder.cc b/cpp_src/core/queryresults/tableviewbuilder.cc index 9b13b5fce..64dc8ca3e 100644 --- a/cpp_src/core/queryresults/tableviewbuilder.cc +++ b/cpp_src/core/queryresults/tableviewbuilder.cc @@ -37,9 +37,13 @@ void TableCalculator::calculate(size_t limit) { const size_t size = std::min(limit, r_.Count()); rows_.reserve(size); for (auto it : r_) { - if (it.IsRaw()) continue; + if (it.IsRaw()) { + continue; + } Error err = it.GetJSON(ser, false); - if (!err.ok()) continue; + if (!err.ok()) { + continue; + } gason::JsonParser parser; gason::JsonNode root = parser.Parse(reindexer::giftStr(ser.Slice())); @@ -59,14 +63,18 @@ void TableCalculator::calculate(size_t limit) { columnData.maxWidthCh = std::max(columnData.maxWidthCh, reindexer::getStringTerminalWidth(fieldName)); } - if (fieldValue.empty()) ++columnData.emptyValues; + if (fieldValue.empty()) { + ++columnData.emptyValues; + } columnData.entries++; rowData[fieldName] = fieldValue; } rows_.emplace_back(std::move(rowData)); - if (++i == size) break; + if (++i == size) { + break; + } ser.Reset(); } @@ -141,7 +149,9 @@ TableViewBuilder::TableViewBuilder(const QueryResultsT& r) : r_(r template void TableViewBuilder::Build(std::ostream& o, const std::function& isCanceled) { - if (isCanceled()) return; + if (isCanceled()) { + return; + } TerminalSize terminalSize = reindexer::getTerminalSize(); TableCalculator tableCalculator(r_, terminalSize.width); BuildHeader(o, tableCalculator, isCanceled); @@ -151,7 +161,9 @@ void TableViewBuilder::Build(std::ostream& o, const std::function template void TableViewBuilder::BuildHeader(std::ostream& o, TableCalculator& tableCalculator, const std::function& isCanceled) { - if (isCanceled()) return; + if (isCanceled()) { + return; + } auto& header = tableCalculator.GetHeader(); auto& columnsData = tableCalculator.GetColumnsSettings(); @@ -166,7 +178,9 @@ void TableViewBuilder::BuildHeader(std::ostream& o, TableCalculat auto& columnData = columnsData[columnName]; ensureFieldWidthIsOk(columnName, columnData.widthCh); o << std::setw(computeFieldWidth(columnName, columnData.widthCh)) << columnName; - if (rowIdx != header.size() - 1) o << kSeparator; + if (rowIdx != header.size() - 1) { + o << kSeparator; + } } o << std::endl << headerLine << std::endl; } @@ -182,7 +196,9 @@ bool TableViewBuilder::isValueMultiline(std::string_view value, b template void TableViewBuilder::startLine(std::ostream& o, const int& currLineWidth) { o << std::endl; - for (size_t i = 0; i < currLineWidth - kSeparator.length(); ++i) o << " "; + for (size_t i = 0; i < currLineWidth - kSeparator.length(); ++i) { + o << " "; + } o << kSeparator; } @@ -217,7 +233,9 @@ void TableViewBuilder::BuildRow(std::ostream& o, int idx, TableCa for (wchar_t wc; (sz = std::mbtowc(&wc, cstr, end - cstr)) > 0; cstr += sz) { currWidth += mk_wcwidth(wc); if (currWidth >= symbolsTillTheEOFLine) { - if (pos != 0) startLine(o, currLineWidth); + if (pos != 0) { + startLine(o, currLineWidth); + } o << std::left; o << value.substr(pos, count); pos = total; @@ -228,7 +246,9 @@ void TableViewBuilder::BuildRow(std::ostream& o, int idx, TableCa } if (count > 0) { - if (pos != 0) startLine(o, currLineWidth); + if (pos != 0) { + startLine(o, currLineWidth); + } o << value.substr(pos, count); } } else { @@ -262,10 +282,14 @@ void TableViewBuilder::BuildRow(std::ostream& o, int idx, TableCa template void TableViewBuilder::BuildTable(std::ostream& o, TableCalculator& tableCalculator, const std::function& isCanceled) { - if (isCanceled()) return; + if (isCanceled()) { + return; + } auto& rows = tableCalculator.GetRows(); for (size_t i = 0; i < rows.size(); ++i) { - if (isCanceled()) return; + if (isCanceled()) { + return; + } BuildRow(o, i, tableCalculator); } } @@ -289,11 +313,15 @@ void TableViewBuilder::ensureFieldWidthIsOk(std::string& str, int int newWidth = 0; static const std::string dots = " ..."; bool withDots = (maxWidth > 10); - if (withDots) maxWidth -= dots.length(); + if (withDots) { + maxWidth -= dots.length(); + } try { for (auto it = str.begin(); it != str.end() && (sz = utf8::internal::sequence_length(it)) > 0;) { newWidth += mk_wcwidth(utf8::next(it, str.end())); - if (newWidth > maxWidth) break; + if (newWidth > maxWidth) { + break; + } n += sz; } } catch (const std::exception&) { @@ -301,7 +329,9 @@ void TableViewBuilder::ensureFieldWidthIsOk(std::string& str, int n = maxWidth; } str = str.substr(0, n); - if (withDots) str += dots; + if (withDots) { + str += dots; + } } } diff --git a/cpp_src/core/querystat.cc b/cpp_src/core/querystat.cc index 7a62b1885..59910ab3f 100644 --- a/cpp_src/core/querystat.cc +++ b/cpp_src/core/querystat.cc @@ -7,7 +7,7 @@ namespace reindexer { template -void QueriesStatTracer::hit(const QuerySQL &sql, std::chrono::microseconds time) { +void QueriesStatTracer::hit(const QuerySQL& sql, std::chrono::microseconds time) { std::unique_lock lck(mtx_); const auto it = stat_.find(sql.normalized); if (it == stat_.end()) { @@ -20,19 +20,21 @@ void QueriesStatTracer::hit(const QuerySQL &sql, std::chrono::microseconds time) } } } -template void QueriesStatTracer::hit<&PerfStatCounterST::Hit>(const QuerySQL &, std::chrono::microseconds); -template void QueriesStatTracer::hit<&PerfStatCounterST::LockHit>(const QuerySQL &, std::chrono::microseconds); +template void QueriesStatTracer::hit<&PerfStatCounterST::Hit>(const QuerySQL&, std::chrono::microseconds); +template void QueriesStatTracer::hit<&PerfStatCounterST::LockHit>(const QuerySQL&, std::chrono::microseconds); const std::vector QueriesStatTracer::Data() { std::unique_lock lck(mtx_); std::vector ret; ret.reserve(stat_.size()); - for (auto &stat : stat_) ret.push_back({stat.first, stat.second.Get(), stat.second.longestQuery}); + for (auto& stat : stat_) { + ret.push_back({stat.first, stat.second.Get(), stat.second.longestQuery}); + } return ret; } -void QueryPerfStat::GetJSON(WrSerializer &ser) const { +void QueryPerfStat::GetJSON(WrSerializer& ser) const { JsonBuilder builder(ser); builder.Put("query", query); diff --git a/cpp_src/core/querystat.h b/cpp_src/core/querystat.h index 2c7dafb08..a6de2b2c1 100644 --- a/cpp_src/core/querystat.h +++ b/cpp_src/core/querystat.h @@ -55,16 +55,22 @@ class QueryStatCalculator { QueryStatCalculator(std::function hitter, std::chrono::microseconds threshold, bool enable, Logger logger = Logger{}) : hitter_(std::move(hitter)), threshold_(threshold), enable_(enable), logger_(std::move(logger)) { - if (enable_) tmStart = system_clock_w::now(); + if (enable_) { + tmStart = system_clock_w::now(); + } } QueryStatCalculator(Logger logger, bool enable = true) : enable_(enable), logger_(std::move(logger)) { - if (enable_) tmStart = system_clock_w::now(); + if (enable_) { + tmStart = system_clock_w::now(); + } } ~QueryStatCalculator() { if (enable_) { auto time = std::chrono::duration_cast(system_clock_w::now() - tmStart); - if (hitter_ && time >= threshold_) hitter_(false, time); + if (hitter_ && time >= threshold_) { + hitter_(false, time); + } if constexpr (Logger::isEnabled) { logger_.Dump(time); @@ -74,7 +80,9 @@ class QueryStatCalculator { void LockHit() { if (enable_ && hitter_) { auto time = std::chrono::duration_cast(system_clock_w::now() - tmStart); - if (time >= threshold_) hitter_(true, time); + if (time >= threshold_) { + hitter_(true, time); + } } } diff --git a/cpp_src/core/rdxcontext.cc b/cpp_src/core/rdxcontext.cc index 37455582d..897203924 100644 --- a/cpp_src/core/rdxcontext.cc +++ b/cpp_src/core/rdxcontext.cc @@ -93,7 +93,9 @@ RdxContext InternalRdxContext::CreateRdxContext(std::string_view query, Activity RdxContext InternalRdxContext::CreateRdxContext(std::string_view query, ActivityContainer& activityContainer, QueryResults& qresults) const { - if (activityTracer_.empty() || query.empty()) return {(deadlineCtx_.IsCancelable() ? &deadlineCtx_ : nullptr), cmpl_}; + if (activityTracer_.empty() || query.empty()) { + return {(deadlineCtx_.IsCancelable() ? &deadlineCtx_ : nullptr), cmpl_}; + } assertrx(!qresults.activityCtx_); qresults.activityCtx_.emplace(activityTracer_, user_, query, activityContainer, connectionId_, true); return RdxContext{&*(qresults.activityCtx_), (deadlineCtx_.IsCancelable() ? &deadlineCtx_ : nullptr), cmpl_}; diff --git a/cpp_src/core/rdxcontext.h b/cpp_src/core/rdxcontext.h index 230d2c401..679b40e78 100644 --- a/cpp_src/core/rdxcontext.h +++ b/cpp_src/core/rdxcontext.h @@ -23,7 +23,9 @@ constexpr std::string_view kDefaultCancelError = "Context was canceled"; template void ThrowOnCancel(const Context& ctx, std::string_view errMsg = std::string_view()) { - if (!ctx.isCancelable()) return; + if (!ctx.isCancelable()) { + return; + } const auto cancel = ctx.checkCancel(); switch (cancel) { @@ -89,7 +91,9 @@ class RdxContext { cmpl_(std::move(cmpl)) {} explicit RdxContext(RdxActivityContext* ptr, const IRdxCancelContext* cancelCtx = nullptr, Completion cmpl = nullptr) noexcept : fromReplication_(false), holdStatus_(ptr ? kPtr : kEmpty), activityPtr_(ptr), cancelCtx_(cancelCtx), cmpl_(std::move(cmpl)) { - if (holdStatus_ == kPtr) activityPtr_->refCount_.fetch_add(1u, std::memory_order_relaxed); + if (holdStatus_ == kPtr) { + activityPtr_->refCount_.fetch_add(1u, std::memory_order_relaxed); + } } RdxContext(RdxContext&& other) noexcept; @@ -101,7 +105,9 @@ class RdxContext { bool isCancelable() const noexcept { return cancelCtx_ && cancelCtx_->IsCancelable(); } CancelType checkCancel() const noexcept { - if (!cancelCtx_) return CancelType::None; + if (!cancelCtx_) { + return CancelType::None; + } return cancelCtx_->GetCancelType(); } /// returning value should be assined to a local variable which will be destroyed after the locking complete diff --git a/cpp_src/core/reindexer.cc b/cpp_src/core/reindexer.cc index 74eb7c2f5..38201cab3 100644 --- a/cpp_src/core/reindexer.cc +++ b/cpp_src/core/reindexer.cc @@ -51,12 +51,8 @@ Error Reindexer::GetMeta(std::string_view nsName, const std::string& key, std::s Error Reindexer::PutMeta(std::string_view nsName, const std::string& key, std::string_view data) { return impl_->PutMeta(nsName, key, data, ctx_); } -Error Reindexer::EnumMeta(std::string_view nsName, std::vector& keys) { - return impl_->EnumMeta(nsName, keys, ctx_); -} -Error Reindexer::DeleteMeta(std::string_view nsName, const std::string& key) { - return impl_->DeleteMeta(nsName, key, ctx_); -} +Error Reindexer::EnumMeta(std::string_view nsName, std::vector& keys) { return impl_->EnumMeta(nsName, keys, ctx_); } +Error Reindexer::DeleteMeta(std::string_view nsName, const std::string& key) { return impl_->DeleteMeta(nsName, key, ctx_); } Error Reindexer::Delete(const Query& q, QueryResults& result) { return impl_->Delete(q, result, ctx_); } Error Reindexer::Select(std::string_view query, QueryResults& result) { return impl_->Select(query, result, ctx_); } Error Reindexer::Select(const Query& q, QueryResults& result) { return impl_->Select(q, result, ctx_); } diff --git a/cpp_src/core/reindexer.h b/cpp_src/core/reindexer.h index c8574cbbd..e3b8adc2b 100644 --- a/cpp_src/core/reindexer.h +++ b/cpp_src/core/reindexer.h @@ -28,7 +28,7 @@ class UpdatesFilters; class Reindexer { public: /// Completion routine - using Completion = std::function; + using Completion = std::function; /// Create Reindexer database object /// @param cfg - general database options @@ -37,10 +37,10 @@ class Reindexer { /// Destroy Reindexer database object ~Reindexer(); /// Create not holding copy - Reindexer(const Reindexer &) noexcept; - Reindexer(Reindexer &&) noexcept; - Reindexer &operator=(const Reindexer &) = delete; - Reindexer &operator=(Reindexer &&) = delete; + Reindexer(const Reindexer&) noexcept; + Reindexer(Reindexer&&) noexcept; + Reindexer& operator=(const Reindexer&) = delete; + Reindexer& operator=(Reindexer&&) = delete; /// Connect - connect to reindexer database in embedded mode /// Cancellation context doesn't affect this call @@ -48,12 +48,12 @@ class Reindexer { /// @param opts - Connect options. May contain any of
/// ConnectOpts::AllowNamespaceErrors() - true: Ignore errors during existing NS's load; false: Return error occurred during NS's load /// ConnectOpts::OpenNamespaces() - true: Need to open all the namespaces; false: Don't open namespaces - Error Connect(const std::string &dsn, ConnectOpts opts = ConnectOpts()); + Error Connect(const std::string& dsn, ConnectOpts opts = ConnectOpts()); /// Enable storage. Must be called before InitSystemNamespaces /// @param storagePath - file system path to database storage /// @param skipPlaceholderCheck - If set, then reindexer will not check folder for placeholder - Error EnableStorage(const std::string &storagePath, bool skipPlaceholderCheck = false); + Error EnableStorage(const std::string& storagePath, bool skipPlaceholderCheck = false); /// Open or create namespace /// @param nsName - Name of namespace @@ -61,10 +61,10 @@ class Reindexer { /// StorageOpts::Enabled() - Enable storage. If storage is disabled, then namespace will be completely in-memory
/// StorageOpts::CreateIfMissing () - Storage will be created, if missing /// @return errOK - On success - Error OpenNamespace(std::string_view nsName, const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing()); + Error OpenNamespace(std::string_view nsName, const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing()); /// Create new namespace. Will fail, if namespace already exists /// @param nsDef - NamespaceDef with namespace initial parameters - Error AddNamespace(const NamespaceDef &nsDef); + Error AddNamespace(const NamespaceDef& nsDef); /// Close namespace. Will free all memory resources, associated with namespace. Forces sync changes to disk /// @param nsName - Name of namespace Error CloseNamespace(std::string_view nsName); @@ -77,19 +77,19 @@ class Reindexer { /// Rename namespace. If namespace with dstNsName exists, then it is replaced. /// @param srcNsName - Name of namespace /// @param dstNsName - desired name of namespace - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName); /// Add index to namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error AddIndex(std::string_view nsName, const IndexDef &index); + Error AddIndex(std::string_view nsName, const IndexDef& index); /// Update index in namespace /// @param nsName - Name of namespace /// @param index - IndexDef with index name and parameters - Error UpdateIndex(std::string_view nsName, const IndexDef &index); + Error UpdateIndex(std::string_view nsName, const IndexDef& index); /// Drop index from namespace /// @param nsName - Name of namespace /// @param index - index name - Error DropIndex(std::string_view nsName, const IndexDef &index); + Error DropIndex(std::string_view nsName, const IndexDef& index); /// Set fields schema for namespace /// @param nsName - Name of namespace /// @param schema - JSON in JsonSchema format @@ -98,79 +98,79 @@ class Reindexer { /// @param nsName - Name of namespace /// @param format - type of Schema: JSON or Protobuf /// @param schema - text representation of schema - Error GetSchema(std::string_view nsName, int format, std::string &schema); + Error GetSchema(std::string_view nsName, int format, std::string& schema); /// Get list of all available namespaces /// @param defs - std::vector of NamespaceDef of available namespaces /// @param opts - Enumeration options - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts); /// Insert new Item to namespace. If item with same PK is already exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Insert(std::string_view nsName, Item &item); + Error Insert(std::string_view nsName, Item& item); /// Insert new Item to namespace. If item with same PK is already exists, when item.GetID will /// return -1, on success inserted item will be added to the result and item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace /// @param result - QueryResults with inserted item. - Error Insert(std::string_view nsName, Item &item, QueryResults &result); + Error Insert(std::string_view nsName, Item& item, QueryResults& result); /// Update Item in namespace. If item with same PK is not exists, when item.GetID will /// return -1, on success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Update(std::string_view nsName, Item &item); + Error Update(std::string_view nsName, Item& item); /// Update Item in namespace. If item with same PK is not exists, when item.GetID will /// return -1, on success updated item will be added to result and item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace /// @param result - QueryResults with updated item. - Error Update(std::string_view nsName, Item &item, QueryResults &result); + Error Update(std::string_view nsName, Item& item, QueryResults& result); /// Updates all items in namespace, that satisfy provided query /// @param query - Query to define items set for update. /// @param result - QueryResults with IDs of updated items. - Error Update(const Query &query, QueryResults &result); + Error Update(const Query& query, QueryResults& result); /// Update or Insert Item in namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Upsert(std::string_view nsName, Item &item); + Error Upsert(std::string_view nsName, Item& item); /// Update or Insert Item in namespace. /// On success upserted item will be added to result and item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace /// @param result - QueryResults with upserted item. - Error Upsert(std::string_view nsName, Item &item, QueryResults &result); + Error Upsert(std::string_view nsName, Item& item, QueryResults& result); /// Delete Item from namespace. On success item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace - Error Delete(std::string_view nsName, Item &item); + Error Delete(std::string_view nsName, Item& item); /// Delete Item from namespace. /// On success deleted item will be added to result and item.GetID() will return internal Item ID /// May be used with completion /// @param nsName - Name of namespace /// @param item - Item, obtained by call to NewItem of the same namespace /// @param result - QueryResults with deleted item. - Error Delete(std::string_view nsName, Item &item, QueryResults &result); + Error Delete(std::string_view nsName, Item& item, QueryResults& result); /// Delete all items from namespace, which matches provided Query /// @param query - Query with conditions /// @param result - QueryResults with IDs of deleted items - Error Delete(const Query &query, QueryResults &result); + Error Delete(const Query& query, QueryResults& result); /// Execute SQL Query and return results /// May be used with completion /// @param query - SQL query. Only "SELECT" semantic is supported /// @param result - QueryResults with found items - Error Select(std::string_view query, QueryResults &result); + Error Select(std::string_view query, QueryResults& result); /// Execute Query and return results /// May be used with completion /// @param query - Query object with query attributes /// @param result - QueryResults with found items - Error Select(const Query &query, QueryResults &result); + Error Select(const Query& query, QueryResults& result); /// Flush changes to storage /// Cancellation context doesn't affect this call /// @param nsName - Name of namespace @@ -186,35 +186,35 @@ class Reindexer { /// Commit transaction - transaction will be deleted after commit /// @param tr - transaction to commit /// @param result - QueryResults with IDs of changed by tx items. - Error CommitTransaction(Transaction &tr, QueryResults &result); + Error CommitTransaction(Transaction& tr, QueryResults& result); /// RollBack transaction - transaction will be deleted after rollback /// Cancellation context doesn't affect this call /// @param tr - transaction to rollback - Error RollBackTransaction(Transaction &tr); + Error RollBackTransaction(Transaction& tr); /// Get metadata from storage by key /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - output string with metadata - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data); /// Put metadata to storage by key /// @param nsName - Name of namespace /// @param key - string with meta key /// @param data - string with metadata - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data); /// Get list of all metadata keys /// @param nsName - Name of namespace /// @param keys - std::vector filled with meta keys - Error EnumMeta(std::string_view nsName, std::vector &keys); + Error EnumMeta(std::string_view nsName, std::vector& keys); /// Delete metadata from storage by key /// @param nsName - Name of namespace /// @param key - string with meta key - Error DeleteMeta(std::string_view nsName, const std::string &key); + Error DeleteMeta(std::string_view nsName, const std::string& key); /// Get possible suggestions for token (set by 'pos') in Sql query. /// Cancellation context doesn't affect this call /// @param sqlQuery - sql query. /// @param pos - position in sql query for suggestions. /// @param suggestions - all the suggestions for 'pos' position in query. - Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector &suggestions); + Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector& suggestions); /// Get current connection status Error Status(); @@ -227,20 +227,20 @@ class Reindexer { /// @param observer - Observer interface, which will receive updates /// @param filters - Subscription filters set /// @param opts - Subscription options (allows to either add new filters or reset them) - Error SubscribeUpdates(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts = SubscriptionOpts()); + Error SubscribeUpdates(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts = SubscriptionOpts()); /// Unsubscribe from updates of database /// Cancellation context doesn't affect this call /// @param observer - Observer interface, which will be unsubscribed updates - Error UnsubscribeUpdates(IUpdatesObserver *observer); + Error UnsubscribeUpdates(IUpdatesObserver* observer); /// Builds Protobuf schema in ser. /// @param ser - schema output buffer /// @param namespaces - list of namespaces to be embedded in .proto - Error GetProtobufSchema(WrSerializer &ser, std::vector &namespaces); + Error GetProtobufSchema(WrSerializer& ser, std::vector& namespaces); /// Add cancelable context /// @param ctx - context pointer - Reindexer WithContext(const IRdxCancelContext *ctx) const { return Reindexer(impl_, ctx_.WithCancelParent(ctx)); } + Reindexer WithContext(const IRdxCancelContext* ctx) const { return Reindexer(impl_, ctx_.WithCancelParent(ctx)); } /// Add execution timeout to the next query /// @param timeout - Execution timeout Reindexer WithTimeout(milliseconds timeout) const { return Reindexer(impl_, ctx_.WithTimeout(timeout)); } @@ -251,10 +251,10 @@ class Reindexer { /// @param activityTracer - name of activity tracer /// @param user - user identifying information /// @param connectionId - unique identifier for the connection - Reindexer WithActivityTracer(std::string_view activityTracer, std::string &&user, int connectionId) const { + Reindexer WithActivityTracer(std::string_view activityTracer, std::string&& user, int connectionId) const { return Reindexer(impl_, ctx_.WithActivityTracer(activityTracer, std::move(user), connectionId)); } - Reindexer WithActivityTracer(std::string_view activityTracer, std::string &&user) const { + Reindexer WithActivityTracer(std::string_view activityTracer, std::string&& user) const { return Reindexer(impl_, ctx_.WithActivityTracer(activityTracer, std::move(user))); } /// Allows to set multiple context params at once @@ -270,10 +270,10 @@ class Reindexer { /// @param activityTracer - name of activity tracer /// @param user - user identifying information /// @param connectionId - unique identifier for the connection - void SetActivityTracer(std::string &&activityTracer, std::string &&user, int connectionId) { + void SetActivityTracer(std::string&& activityTracer, std::string&& user, int connectionId) { ctx_.SetActivityTracer(std::move(activityTracer), std::move(user), connectionId); } - void SetActivityTracer(std::string &&activityTracer, std::string &&user) { + void SetActivityTracer(std::string&& activityTracer, std::string&& user) { ctx_.SetActivityTracer(std::move(activityTracer), std::move(user)); } @@ -282,12 +282,12 @@ class Reindexer { typedef QueryResults QueryResultsT; typedef Item ItemT; - Error DumpIndex(std::ostream &os, std::string_view nsName, std::string_view index); + Error DumpIndex(std::ostream& os, std::string_view nsName, std::string_view index); private: - Reindexer(ReindexerImpl *impl, InternalRdxContext &&ctx) noexcept : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} + Reindexer(ReindexerImpl* impl, InternalRdxContext&& ctx) noexcept : impl_(impl), owner_(false), ctx_(std::move(ctx)) {} - ReindexerImpl *impl_; + ReindexerImpl* impl_; bool owner_; InternalRdxContext ctx_; }; diff --git a/cpp_src/core/reindexer_impl/reindexerimpl.cc b/cpp_src/core/reindexer_impl/reindexerimpl.cc index 6913236c0..278d5cb91 100644 --- a/cpp_src/core/reindexer_impl/reindexerimpl.cc +++ b/cpp_src/core/reindexer_impl/reindexerimpl.cc @@ -114,7 +114,9 @@ Error ReindexerImpl::EnableStorage(const std::string& storagePath, bool skipPlac return Error(errParams, "Storage already enabled"); } - if (storagePath.empty()) return errOK; + if (storagePath.empty()) { + return errOK; + } if (fs::MkDirAll(storagePath) < 0) { return Error(errParams, "Can't create directory '%s' for reindexer storage - reason %s", storagePath, strerror(errno)); } @@ -129,7 +131,9 @@ Error ReindexerImpl::EnableStorage(const std::string& storagePath, bool skipPlac if (entry.name != "." && entry.name != ".." && entry.name != kStoragePlaceholderFilename) { isEmpty = false; } - if (entry.name == kConfigNamespace) isHaveConfig = true; + if (entry.name == kConfigNamespace) { + isHaveConfig = true; + } } if (!isEmpty && !skipPlaceholderCheck) { @@ -228,14 +232,18 @@ Error ReindexerImpl::Connect(const std::string& dsn, ConnectOpts opts) { bool enableStorage = (path.length() > 0 && path != "/"); if (enableStorage) { auto err = EnableStorage(path); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } if (fs::ReadDir(path, foundNs) < 0) { return Error(errParams, "Can't read database dir %s", path); } } Error err = InitSystemNamespaces(); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } if (enableStorage && opts.IsOpenNamespaces()) { boost::sort::pdqsort_branchless(foundNs.begin(), foundNs.end(), [](const fs::DirEntry& ld, const fs::DirEntry& rd) noexcept { @@ -277,7 +285,9 @@ Error ReindexerImpl::Connect(const std::string& dsn, ConnectOpts opts) { } }); } - for (size_t i = 0; i < maxLoadWorkers; ++i) thrs[i].join(); + for (size_t i = 0; i < maxLoadWorkers; ++i) { + thrs[i].join(); + } if (!opts.IsAllowNamespaceErrors() && hasNsErrors.test_and_set(std::memory_order_relaxed)) { return Error(errNotValid, "Namespaces load error"); @@ -286,7 +296,9 @@ Error ReindexerImpl::Connect(const std::string& dsn, ConnectOpts opts) { if (replicationEnabled_) { err = checkReplConf(opts); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } replicator_->Enable(); bool needStart = replicator_->Configure(configProvider_.GetReplicationConfig()); @@ -367,12 +379,16 @@ Error ReindexerImpl::addNamespace(const NamespaceDef& nsDef, const RdxContext& r ULock lock(mtx_, rdxCtx); namespaces_.insert({nsDef.name, ns}); } - if (!nsDef.isTemporary) observers_.OnWALUpdate(LSNPair(), nsDef.name, WALRecord(WalNamespaceAdd)); + if (!nsDef.isTemporary) { + observers_.OnWALUpdate(LSNPair(), nsDef.name, WALRecord(WalNamespaceAdd)); + } for (auto& indexDef : nsDef.indexes) { ns->AddIndex(indexDef, rdxCtx); } ns->SetSchema(nsDef.schemaJson, rdxCtx); - if (nsDef.storage.IsSlaveMode()) ns->setSlaveMode(rdxCtx); + if (nsDef.storage.IsSlaveMode()) { + ns->setSlaveMode(rdxCtx); + } } catch (const Error& err) { return err; @@ -387,7 +403,9 @@ Error ReindexerImpl::openNamespace(std::string_view name, const StorageOpts& sto SLock lock(mtx_, rdxCtx); auto nsIt = namespaces_.find(name); if (nsIt != namespaces_.end() && nsIt->second) { - if (storageOpts.IsSlaveMode()) nsIt->second->setSlaveMode(rdxCtx); + if (storageOpts.IsSlaveMode()) { + nsIt->second->setSlaveMode(rdxCtx); + } return {}; } } @@ -396,7 +414,9 @@ Error ReindexerImpl::openNamespace(std::string_view name, const StorageOpts& sto } std::string nameStr(name); auto ns = std::make_shared(nameStr, observers_, bgDeleter_); - if (storageOpts.IsSlaveMode()) ns->setSlaveMode(rdxCtx); + if (storageOpts.IsSlaveMode()) { + ns->setSlaveMode(rdxCtx); + } if (storageOpts.IsEnabled() && !storagePath_.empty()) { auto opts = storageOpts; ns->EnableStorage(storagePath_, opts.Autorepair(autorepairEnabled_), storageType_, rdxCtx); @@ -493,7 +513,9 @@ Error ReindexerImpl::TruncateNamespace(std::string_view nsName, const InternalRd } catch (const Error& e) { err = e; } - if (ctx.Compl()) ctx.Compl()(err); + if (ctx.Compl()) { + ctx.Compl()(err); + } return err; } @@ -582,7 +604,9 @@ Error ReindexerImpl::renameNamespace(std::string_view srcNsName, const std::stri } namespaces_.erase(srcIt); namespaces_[dstNsName] = std::move(srcNs); - if (needWalUpdate) observers_.OnWALUpdate(LSNPair(), srcNsName, WALRecord(WalNamespaceRename, dstNsName)); + if (needWalUpdate) { + observers_.OnWALUpdate(LSNPair(), srcNsName, WALRecord(WalNamespaceRename, dstNsName)); + } } else { return Error(errLogic, "Can't rename namespace in slave mode '%s'", srcNsName); } @@ -607,7 +631,9 @@ Error ReindexerImpl::applyNsFunction(std::string_view nsName, const InternalRdxC } catch (const Error& e) { err = e; } - if (ctx.Compl()) ctx.Compl()(err); + if (ctx.Compl()) { + ctx.Compl()(err); + } return err; } @@ -636,7 +662,9 @@ Error ReindexerImpl::applyNsFunction(std::string_view nsName, const InternalRdxC } catch (const Error& e) { err = e; } - if (ctx.Compl()) ctx.Compl()(err); + if (ctx.Compl()) { + ctx.Compl()(err); + } return err; } @@ -667,7 +695,9 @@ static WrSerializer& printPkFields(const Item& item, WrSerializer& ser) { size_t jsonPathIdx = 0; const FieldsSet fields = item.PkFields(); for (auto it = fields.begin(); it != fields.end(); ++it) { - if (it != fields.begin()) ser << " AND "; + if (it != fields.begin()) { + ser << " AND "; + } int field = *it; if (field == IndexValueType::SetByJsonPath) { assertrx(jsonPathIdx < fields.getTagsPathsLength()); @@ -846,14 +876,18 @@ Error ReindexerImpl::Select(std::string_view query, QueryResults& result, const err = e; } - if (ctx.Compl()) ctx.Compl()(err); + if (ctx.Compl()) { + ctx.Compl()(err); + } return err; } Error ReindexerImpl::Select(const Query& q, QueryResults& result, const InternalRdxContext& ctx) { try { WrSerializer normalizedSQL, nonNormalizedSQL; - if (ctx.NeedTraceActivity()) q.GetSQL(nonNormalizedSQL, false); + if (ctx.NeedTraceActivity()) { + q.GetSQL(nonNormalizedSQL, false); + } const auto rdxCtx = ctx.CreateRdxContext(ctx.NeedTraceActivity() ? nonNormalizedSQL.Slice() : "", activities_, result); RxSelector::NsLocker locks(rdxCtx); @@ -866,16 +900,20 @@ Error ReindexerImpl::Select(const Query& q, QueryResults& result, const Internal auto& tracker = queriesStatTracker_; if (queriesPerfStatsEnabled) { q.GetSQL(normalizedSQL, true); - if (!ctx.NeedTraceActivity()) q.GetSQL(nonNormalizedSQL, false); + if (!ctx.NeedTraceActivity()) { + q.GetSQL(nonNormalizedSQL, false); + } } const QueriesStatTracer::QuerySQL sql{normalizedSQL.Slice(), nonNormalizedSQL.Slice()}; auto hitter = queriesPerfStatsEnabled ? [&sql, &tracker](bool lockHit, std::chrono::microseconds time) { - if (lockHit) + if (lockHit){ tracker.LockHit(sql, time); - else +} + else{ tracker.Hit(sql, time); +} } : std::function{}; const bool isSystemNsRequest = isSystemNamespaceNameFast(q.NsName()); @@ -916,10 +954,14 @@ Error ReindexerImpl::Select(const Query& q, QueryResults& result, const Internal RxSelector::DoSelect(q, result, locks, func, rdxCtx, statCalculator); func.Process(result); } catch (const Error& err) { - if (ctx.Compl()) ctx.Compl()(err); + if (ctx.Compl()) { + ctx.Compl()(err); + } return err; } - if (ctx.Compl()) ctx.Compl()(Error()); + if (ctx.Compl()) { + ctx.Compl()(Error()); + } return Error(); } @@ -1016,7 +1058,9 @@ Error ReindexerImpl::EnumNamespaces(std::vector& defs, EnumNamespa const auto rdxCtx = ctx.CreateRdxContext("SELECT NAMESPACES", activities_); auto nsarray = getNamespaces(rdxCtx); for (auto& nspair : nsarray) { - if (!opts.MatchFilter(nspair.first)) continue; + if (!opts.MatchFilter(nspair.first)) { + continue; + } NamespaceDef nsDef(nspair.first); if (!opts.IsOnlyNames()) { nsDef = nspair.second->GetDefinition(rdxCtx); @@ -1028,13 +1072,17 @@ Error ReindexerImpl::EnumNamespaces(std::vector& defs, EnumNamespa if (opts.IsWithClosed() && !storagePath_.empty()) { std::vector dirs; - if (fs::ReadDir(storagePath_, dirs) != 0) return Error(errLogic, "Could not read database dir"); + if (fs::ReadDir(storagePath_, dirs) != 0) { + return Error(errLogic, "Could not read database dir"); + } for (auto& d : dirs) { if (d.isDir && d.name != "." && d.name != ".." && opts.MatchFilter(d.name)) { { SLock lock(mtx_, rdxCtx); - if (namespaces_.find(d.name) != namespaces_.end()) continue; + if (namespaces_.find(d.name) != namespaces_.end()) { + continue; + } } std::unique_ptr tmpNs{new NamespaceImpl(d.name, observers_)}; try { @@ -1188,7 +1236,9 @@ Error ReindexerImpl::InitSystemNamespaces() { QueryResults results; auto err = Select(Query(kConfigNamespace), results); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } bool hasReplicatorConfig = false; if (results.Count() == 0) { @@ -1206,11 +1256,17 @@ Error ReindexerImpl::InitSystemNamespaces() { } Item item = NewItem(kConfigNamespace); - if (!item.Status().ok()) return item.Status(); + if (!item.Status().ok()) { + return item.Status(); + } err = item.FromJSON(conf); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } err = Insert(kConfigNamespace, item); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } } else { // Load config from namespace #config @@ -1298,7 +1354,9 @@ void ReindexerImpl::updateToSystemNamespace(std::string_view nsName, Item& item, } } if (replicationEnabled_ && needStartReplicator && !dbDestroyed_) { - if (Error err = replicator_->Start()) throw err; + if (Error err = replicator_->Start()) { + throw err; + } } } catch (gason::Exception& e) { throw Error(errParseJson, "JSON parsing error: %s", e.what()); @@ -1306,7 +1364,9 @@ void ReindexerImpl::updateToSystemNamespace(std::string_view nsName, Item& item, } else if (nsName == kQueriesPerfStatsNamespace) { queriesStatTracker_.Reset(); } else if (nsName == kPerfStatsNamespace) { - for (auto& ns : getNamespaces(ctx)) ns.second->ResetPerfStat(ctx); + for (auto& ns : getNamespaces(ctx)) { + ns.second->ResetPerfStat(ctx); + } } } @@ -1317,7 +1377,9 @@ void ReindexerImpl::updateConfigProvider(const gason::JsonNode& config) { } catch (const gason::Exception& ex) { err = Error(errParseJson, "updateConfigProvider: %s", ex.what()); } - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } } void ReindexerImpl::updateReplicationConfFile() { @@ -1412,7 +1474,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) continue; } } - if (isSystemNamespaceNameFast(nspair.first) && !withSystem) continue; + if (isSystemNamespaceNameFast(nspair.first) && !withSystem) { + continue; + } ser.Reset(); if (filler(nspair.first, nspair.second, ser)) { auto& item = items.emplace_back(sysNs->NewItem(ctx)); @@ -1435,7 +1499,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) [&ctx](std::string_view nsName, const Namespace::Ptr& nsPtr, WrSerializer& ser) { auto stats = nsPtr->GetPerfStat(ctx); bool notRenamed = (stats.name == nsName); - if (notRenamed) stats.GetJSON(ser); + if (notRenamed) { + stats.GetJSON(ser); + } return notRenamed; }); } @@ -1445,7 +1511,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) [&ctx](std::string_view nsName, const Namespace::Ptr& nsPtr, WrSerializer& ser) { auto stats = nsPtr->GetMemStat(ctx); bool notRenamed = (stats.name == nsName); - if (notRenamed) stats.GetJSON(ser); + if (notRenamed) { + stats.GetJSON(ser); + } return notRenamed; }); } @@ -1454,7 +1522,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) [&ctx](std::string_view nsName, const Namespace::Ptr& nsPtr, WrSerializer& ser) { auto stats = nsPtr->GetDefinition(ctx); bool notRenamed = (stats.name == nsName); - if (notRenamed) stats.GetJSON(ser, kIndexJSONWithDescribe); + if (notRenamed) { + stats.GetJSON(ser, kIndexJSONWithDescribe); + } return notRenamed; }); } else if (sysNsName == kQueriesPerfStatsNamespace) { @@ -1472,7 +1542,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) throw item.Status(); } auto err = item.FromJSON(ser.Slice()); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } } queriesperfstatsNs->Refill(items, ctx); } @@ -1490,7 +1562,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) throw item.Status(); } auto err = item.FromJSON(ser.Slice()); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } } activityNs->Refill(items, ctx); } else if (sysNsName == kClientsStatsNamespace) { @@ -1521,7 +1595,9 @@ ReindexerImpl::FilterNsNamesT ReindexerImpl::detectFilterNsNames(const Query& q) throw item.Status(); } auto err = item.FromJSON(ser.Slice()); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } } clientsNs->Refill(items, ctx); } @@ -1587,7 +1663,9 @@ Error ReindexerImpl::GetProtobufSchema(WrSerializer& ser, std::vector; template - Error applyNsFunction(std::string_view nsName, const InternalRdxContext &ctx, const MakeCtxStrFn &makeCtxStr, Arg arg, Args... args); + Error applyNsFunction(std::string_view nsName, const InternalRdxContext& ctx, const MakeCtxStrFn& makeCtxStr, Arg arg, Args... args); template - Error applyNsFunction(std::string_view nsName, const InternalRdxContext &ctx, const MakeCtxStrFn &makeCtxStr, Arg &, Args...); + Error applyNsFunction(std::string_view nsName, const InternalRdxContext& ctx, const MakeCtxStrFn& makeCtxStr, Arg&, Args...); public: - using Completion = std::function; + using Completion = std::function; ReindexerImpl(ReindexerConfig cfg = ReindexerConfig()); ~ReindexerImpl(); - Error Connect(const std::string &dsn, ConnectOpts opts = ConnectOpts()); - Error EnableStorage(const std::string &storagePath, bool skipPlaceholderCheck = false, - const InternalRdxContext &ctx = InternalRdxContext()); - Error OpenNamespace(std::string_view nsName, const StorageOpts &opts = StorageOpts().Enabled().CreateIfMissing(), - const InternalRdxContext &ctx = InternalRdxContext()); - Error AddNamespace(const NamespaceDef &nsDef, const InternalRdxContext &ctx = InternalRdxContext()); - Error CloseNamespace(std::string_view nsName, const InternalRdxContext &ctx = InternalRdxContext()); - Error DropNamespace(std::string_view nsName, const InternalRdxContext &ctx = InternalRdxContext()); - Error TruncateNamespace(std::string_view nsName, const InternalRdxContext &ctx = InternalRdxContext()); - Error RenameNamespace(std::string_view srcNsName, const std::string &dstNsName, const InternalRdxContext &ctx = InternalRdxContext()); - Error AddIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx = InternalRdxContext()); - Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext &ctx = InternalRdxContext()); - Error GetSchema(std::string_view nsName, int format, std::string &schema, const InternalRdxContext &ctx = InternalRdxContext()); - Error UpdateIndex(std::string_view nsName, const IndexDef &indexDef, const InternalRdxContext &ctx = InternalRdxContext()); - Error DropIndex(std::string_view nsName, const IndexDef &index, const InternalRdxContext &ctx = InternalRdxContext()); - Error EnumNamespaces(std::vector &defs, EnumNamespacesOpts opts, const InternalRdxContext &ctx = InternalRdxContext()); - Error Insert(std::string_view nsName, Item &item, const InternalRdxContext &ctx = InternalRdxContext()); - Error Insert(std::string_view nsName, Item &item, QueryResults &, const InternalRdxContext &ctx = InternalRdxContext()); - Error Update(std::string_view nsName, Item &item, const InternalRdxContext &ctx = InternalRdxContext()); - Error Update(std::string_view nsName, Item &item, QueryResults &, const InternalRdxContext &ctx = InternalRdxContext()); - Error Update(const Query &query, QueryResults &result, const InternalRdxContext &ctx = InternalRdxContext()); - Error Upsert(std::string_view nsName, Item &item, const InternalRdxContext &ctx = InternalRdxContext()); - Error Upsert(std::string_view nsName, Item &item, QueryResults &, const InternalRdxContext &ctx = InternalRdxContext()); - Error Delete(std::string_view nsName, Item &item, const InternalRdxContext &ctx = InternalRdxContext()); - Error Delete(std::string_view nsName, Item &item, QueryResults &, const InternalRdxContext &ctx = InternalRdxContext()); - Error Delete(const Query &query, QueryResults &result, const InternalRdxContext &ctx = InternalRdxContext()); - Error Select(std::string_view query, QueryResults &result, const InternalRdxContext &ctx = InternalRdxContext()); - Error Select(const Query &query, QueryResults &result, const InternalRdxContext &ctx = InternalRdxContext()); + Error Connect(const std::string& dsn, ConnectOpts opts = ConnectOpts()); + Error EnableStorage(const std::string& storagePath, bool skipPlaceholderCheck = false, + const InternalRdxContext& ctx = InternalRdxContext()); + Error OpenNamespace(std::string_view nsName, const StorageOpts& opts = StorageOpts().Enabled().CreateIfMissing(), + const InternalRdxContext& ctx = InternalRdxContext()); + Error AddNamespace(const NamespaceDef& nsDef, const InternalRdxContext& ctx = InternalRdxContext()); + Error CloseNamespace(std::string_view nsName, const InternalRdxContext& ctx = InternalRdxContext()); + Error DropNamespace(std::string_view nsName, const InternalRdxContext& ctx = InternalRdxContext()); + Error TruncateNamespace(std::string_view nsName, const InternalRdxContext& ctx = InternalRdxContext()); + Error RenameNamespace(std::string_view srcNsName, const std::string& dstNsName, const InternalRdxContext& ctx = InternalRdxContext()); + Error AddIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx = InternalRdxContext()); + Error SetSchema(std::string_view nsName, std::string_view schema, const InternalRdxContext& ctx = InternalRdxContext()); + Error GetSchema(std::string_view nsName, int format, std::string& schema, const InternalRdxContext& ctx = InternalRdxContext()); + Error UpdateIndex(std::string_view nsName, const IndexDef& indexDef, const InternalRdxContext& ctx = InternalRdxContext()); + Error DropIndex(std::string_view nsName, const IndexDef& index, const InternalRdxContext& ctx = InternalRdxContext()); + Error EnumNamespaces(std::vector& defs, EnumNamespacesOpts opts, const InternalRdxContext& ctx = InternalRdxContext()); + Error Insert(std::string_view nsName, Item& item, const InternalRdxContext& ctx = InternalRdxContext()); + Error Insert(std::string_view nsName, Item& item, QueryResults&, const InternalRdxContext& ctx = InternalRdxContext()); + Error Update(std::string_view nsName, Item& item, const InternalRdxContext& ctx = InternalRdxContext()); + Error Update(std::string_view nsName, Item& item, QueryResults&, const InternalRdxContext& ctx = InternalRdxContext()); + Error Update(const Query& query, QueryResults& result, const InternalRdxContext& ctx = InternalRdxContext()); + Error Upsert(std::string_view nsName, Item& item, const InternalRdxContext& ctx = InternalRdxContext()); + Error Upsert(std::string_view nsName, Item& item, QueryResults&, const InternalRdxContext& ctx = InternalRdxContext()); + Error Delete(std::string_view nsName, Item& item, const InternalRdxContext& ctx = InternalRdxContext()); + Error Delete(std::string_view nsName, Item& item, QueryResults&, const InternalRdxContext& ctx = InternalRdxContext()); + Error Delete(const Query& query, QueryResults& result, const InternalRdxContext& ctx = InternalRdxContext()); + Error Select(std::string_view query, QueryResults& result, const InternalRdxContext& ctx = InternalRdxContext()); + Error Select(const Query& query, QueryResults& result, const InternalRdxContext& ctx = InternalRdxContext()); Error Commit(std::string_view nsName); - Item NewItem(std::string_view nsName, const InternalRdxContext &ctx = InternalRdxContext()); + Item NewItem(std::string_view nsName, const InternalRdxContext& ctx = InternalRdxContext()); - Transaction NewTransaction(std::string_view nsName, const InternalRdxContext &ctx = InternalRdxContext()); - Error CommitTransaction(Transaction &tr, QueryResults &result, const InternalRdxContext &ctx = InternalRdxContext()); - Error RollBackTransaction(Transaction &tr); + Transaction NewTransaction(std::string_view nsName, const InternalRdxContext& ctx = InternalRdxContext()); + Error CommitTransaction(Transaction& tr, QueryResults& result, const InternalRdxContext& ctx = InternalRdxContext()); + Error RollBackTransaction(Transaction& tr); - Error GetMeta(std::string_view nsName, const std::string &key, std::string &data, const InternalRdxContext &ctx = InternalRdxContext()); - Error PutMeta(std::string_view nsName, const std::string &key, std::string_view data, - const InternalRdxContext &ctx = InternalRdxContext()); - Error EnumMeta(std::string_view nsName, std::vector &keys, const InternalRdxContext &ctx = InternalRdxContext()); - Error DeleteMeta(std::string_view nsName, const std::string &key, const InternalRdxContext &ctx = InternalRdxContext()); + Error GetMeta(std::string_view nsName, const std::string& key, std::string& data, const InternalRdxContext& ctx = InternalRdxContext()); + Error PutMeta(std::string_view nsName, const std::string& key, std::string_view data, + const InternalRdxContext& ctx = InternalRdxContext()); + Error EnumMeta(std::string_view nsName, std::vector& keys, const InternalRdxContext& ctx = InternalRdxContext()); + Error DeleteMeta(std::string_view nsName, const std::string& key, const InternalRdxContext& ctx = InternalRdxContext()); Error InitSystemNamespaces(); - Error SubscribeUpdates(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts); - Error UnsubscribeUpdates(IUpdatesObserver *observer); - Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector &suggestions, - const InternalRdxContext &ctx = InternalRdxContext()); - Error GetProtobufSchema(WrSerializer &ser, std::vector &namespaces); + Error SubscribeUpdates(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts); + Error UnsubscribeUpdates(IUpdatesObserver* observer); + Error GetSqlSuggestions(std::string_view sqlQuery, int pos, std::vector& suggestions, + const InternalRdxContext& ctx = InternalRdxContext()); + Error GetProtobufSchema(WrSerializer& ser, std::vector& namespaces); Error Status(); bool NeedTraceActivity() const noexcept { return configProvider_.ActivityStatsEnabled(); } - Error DumpIndex(std::ostream &os, std::string_view nsName, std::string_view index, - const InternalRdxContext &ctx = InternalRdxContext()); + Error DumpIndex(std::ostream& os, std::string_view nsName, std::string_view index, + const InternalRdxContext& ctx = InternalRdxContext()); private: typedef contexted_shared_lock SLock; @@ -101,10 +101,10 @@ class ReindexerImpl { ~BackgroundThread() { Stop(); } template - void Run(F &&f) { + void Run(F&& f) { Stop(); async_.set(loop_); - async_.set([this](net::ev::async &) noexcept { loop_.break_loop(); }); + async_.set([this](net::ev::async&) noexcept { loop_.break_loop(); }); async_.start(); th_ = std::thread(std::forward(f), std::ref(loop_)); } @@ -127,37 +127,37 @@ class ReindexerImpl { using StatsLockT = contexted_unique_lock; StatsLocker(); - [[nodiscard]] StatsLockT LockIfRequired(std::string_view sysNsName, const RdxContext &); + [[nodiscard]] StatsLockT LockIfRequired(std::string_view sysNsName, const RdxContext&); private: std::unordered_map mtxMap_; }; - FilterNsNamesT detectFilterNsNames(const Query &q); - [[nodiscard]] StatsLocker::StatsLockT syncSystemNamespaces(std::string_view sysNsName, const FilterNsNamesT &, const RdxContext &); + FilterNsNamesT detectFilterNsNames(const Query& q); + [[nodiscard]] StatsLocker::StatsLockT syncSystemNamespaces(std::string_view sysNsName, const FilterNsNamesT&, const RdxContext&); void createSystemNamespaces(); - void updateToSystemNamespace(std::string_view nsName, Item &, const RdxContext &ctx); - void updateConfigProvider(const gason::JsonNode &config); + void updateToSystemNamespace(std::string_view nsName, Item&, const RdxContext& ctx); + void updateConfigProvider(const gason::JsonNode& config); void updateReplicationConfFile(); void onProfiligConfigLoad(); Error tryLoadReplicatorConfFromFile(); - Error tryLoadReplicatorConfFromYAML(const std::string &yamlReplConf); + Error tryLoadReplicatorConfFromYAML(const std::string& yamlReplConf); - void backgroundRoutine(net::ev::dynamic_loop &loop); - void storageFlushingRoutine(net::ev::dynamic_loop &loop); - Error closeNamespace(std::string_view nsName, const RdxContext &ctx, bool dropStorage, bool enableDropSlave = false); + void backgroundRoutine(net::ev::dynamic_loop& loop); + void storageFlushingRoutine(net::ev::dynamic_loop& loop); + Error closeNamespace(std::string_view nsName, const RdxContext& ctx, bool dropStorage, bool enableDropSlave = false); - Error syncDownstream(std::string_view nsName, bool force, const InternalRdxContext &ctx = InternalRdxContext()); + Error syncDownstream(std::string_view nsName, bool force, const InternalRdxContext& ctx = InternalRdxContext()); - Namespace::Ptr getNamespace(std::string_view nsName, const RdxContext &ctx); - Namespace::Ptr getNamespaceNoThrow(std::string_view nsName, const RdxContext &ctx); + Namespace::Ptr getNamespace(std::string_view nsName, const RdxContext& ctx); + Namespace::Ptr getNamespaceNoThrow(std::string_view nsName, const RdxContext& ctx); - std::vector> getNamespaces(const RdxContext &ctx); - std::vector getNamespacesNames(const RdxContext &ctx); - Error renameNamespace(std::string_view srcNsName, const std::string &dstNsName, bool fromReplication = false, - const InternalRdxContext &ctx = InternalRdxContext()); - Error openNamespace(std::string_view name, const StorageOpts &storageOpts, const RdxContext &rdxCtx); - Error addNamespace(const NamespaceDef &nsDef, const RdxContext &rdxCtx); + std::vector> getNamespaces(const RdxContext& ctx); + std::vector getNamespacesNames(const RdxContext& ctx); + Error renameNamespace(std::string_view srcNsName, const std::string& dstNsName, bool fromReplication = false, + const InternalRdxContext& ctx = InternalRdxContext()); + Error openNamespace(std::string_view name, const StorageOpts& storageOpts, const RdxContext& rdxCtx); + Error addNamespace(const NamespaceDef& nsDef, const RdxContext& rdxCtx); [[nodiscard]] bool isSystemNamespaceNameStrict(std::string_view name) noexcept; @@ -190,7 +190,7 @@ class ReindexerImpl { bool replicationEnabled_ = true; std::atomic connected_ = {false}; - IClientsStats *clientsStats_ = nullptr; + IClientsStats* clientsStats_ = nullptr; friend class Replicator; friend class TransactionImpl; diff --git a/cpp_src/core/reindexer_impl/rx_selector.cc b/cpp_src/core/reindexer_impl/rx_selector.cc index ac49dd50f..bba7b9e87 100644 --- a/cpp_src/core/reindexer_impl/rx_selector.cc +++ b/cpp_src/core/reindexer_impl/rx_selector.cc @@ -188,7 +188,9 @@ void RxSelector::DoSelect(const Query& q, QueryResults& result, NsLocker& loc } } // Adding context to QueryResults - for (const auto& jctx : joinQueryResultsContexts) result.addNSContext(jctx.type_, jctx.tagsMatcher_, jctx.fieldsFilter_, jctx.schema_); + for (const auto& jctx : joinQueryResultsContexts) { + result.addNSContext(jctx.type_, jctx.tagsMatcher_, jctx.fieldsFilter_, jctx.schema_); + } } [[nodiscard]] static bool byJoinedField(std::string_view sortExpr, std::string_view joinedNs) { @@ -198,21 +200,31 @@ void RxSelector::DoSelect(const Query& q, QueryResults& result, NsLocker& loc 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '.', '+'}; std::string_view::size_type i = 0; const auto s = sortExpr.size(); - while (i < s && isspace(sortExpr[i])) ++i; + while (i < s && isspace(sortExpr[i])) { + ++i; + } bool inQuotes = false; if (i < s && sortExpr[i] == '"') { ++i; inQuotes = true; } - while (i < s && isspace(sortExpr[i])) ++i; + while (i < s && isspace(sortExpr[i])) { + ++i; + } std::string_view::size_type j = 0, s2 = joinedNs.size(); for (; j < s2 && i < s; ++i, ++j) { - if (sortExpr[i] != joinedNs[j]) return false; + if (sortExpr[i] != joinedNs[j]) { + return false; + } + } + if (i >= s || sortExpr[i] != '.') { + return false; } - if (i >= s || sortExpr[i] != '.') return false; for (++i; i < s; ++i) { if (!kJoinedIndexNameSyms.test(sortExpr[i])) { - if (isspace(sortExpr[i])) break; + if (isspace(sortExpr[i])) { + break; + } if (inQuotes && sortExpr[i] == '"') { inQuotes = false; ++i; @@ -221,9 +233,15 @@ void RxSelector::DoSelect(const Query& q, QueryResults& result, NsLocker& loc return false; } } - while (i < s && isspace(sortExpr[i])) ++i; - if (inQuotes && i < s && sortExpr[i] == '"') ++i; - while (i < s && isspace(sortExpr[i])) ++i; + while (i < s && isspace(sortExpr[i])) { + ++i; + } + if (inQuotes && i < s && sortExpr[i] == '"') { + ++i; + } + while (i < s && isspace(sortExpr[i])) { + ++i; + } return i == s; } @@ -395,7 +413,9 @@ template JoinedSelectors RxSelector::prepareJoinedSelectors(const Query& q, QueryResults& result, NsLocker& locks, SelectFunctionsHolder& func, std::vector& queryResultsContexts, const RdxContext& rdxCtx) { JoinedSelectors joinedSelectors; - if (q.GetJoinQueries().empty()) return joinedSelectors; + if (q.GetJoinQueries().empty()) { + return joinedSelectors; + } auto ns = locks.Get(q.NsName()); assertrx_throw(ns); diff --git a/cpp_src/core/reindexer_impl/rx_selector.h b/cpp_src/core/reindexer_impl/rx_selector.h index a96209985..a0f4ccefc 100644 --- a/cpp_src/core/reindexer_impl/rx_selector.h +++ b/cpp_src/core/reindexer_impl/rx_selector.h @@ -17,7 +17,7 @@ class RxSelector { template class NsLocker : private h_vector { public: - explicit NsLocker(const Context &context) noexcept : context_(context) {} + explicit NsLocker(const Context& context) noexcept : context_(context) {} ~NsLocker() { // Unlock first for (auto it = rbegin(), re = rend(); it != re; ++it) { @@ -31,7 +31,7 @@ class RxSelector { // Clean (ns may will release, if locker holds last ref) } - void Add(NamespaceImpl::Ptr &&ns) { + void Add(NamespaceImpl::Ptr&& ns) { assertrx(!locked_); for (auto it = begin(), e = end(); it != e; ++it) { if (it->ns.get() == ns.get()) { @@ -42,10 +42,12 @@ class RxSelector { emplace_back(std::move(ns)); } - void Delete(const NamespaceImpl::Ptr &ns) noexcept { + void Delete(const NamespaceImpl::Ptr& ns) noexcept { for (auto it = begin(), e = end(); it != e; ++it) { if (it->ns.get() == ns.get()) { - if (!--(it->count)) erase(it); + if (!--(it->count)) { + erase(it); + } return; } } @@ -53,46 +55,48 @@ class RxSelector { } void Lock() { boost::sort::pdqsort_branchless( - begin(), end(), [](const NsLockerItem &lhs, const NsLockerItem &rhs) noexcept { return lhs.ns.get() < rhs.ns.get(); }); + begin(), end(), [](const NsLockerItem& lhs, const NsLockerItem& rhs) noexcept { return lhs.ns.get() < rhs.ns.get(); }); for (auto it = begin(), e = end(); it != e; ++it) { it->nsLck = it->ns->rLock(context_); } locked_ = true; } - NamespaceImpl::Ptr Get(const std::string &name) noexcept { + NamespaceImpl::Ptr Get(const std::string& name) noexcept { for (auto it = begin(), e = end(); it != e; ++it) { - if (iequals(it->ns->name_, name)) return it->ns; + if (iequals(it->ns->name_, name)) { + return it->ns; + } } return NamespaceImpl::Ptr(); } protected: bool locked_ = false; - const Context &context_; + const Context& context_; }; template - static void DoSelect(const Query &q, QueryResults &result, NsLocker &locks, SelectFunctionsHolder &func, const RdxContext &ctx, - QueryStatCalculator &queryStatCalculator); + static void DoSelect(const Query& q, QueryResults& result, NsLocker& locks, SelectFunctionsHolder& func, const RdxContext& ctx, + QueryStatCalculator& queryStatCalculator); private: struct QueryResultsContext; template - static JoinedSelectors prepareJoinedSelectors(const Query &q, QueryResults &result, NsLocker &locks, SelectFunctionsHolder &func, - std::vector &, const RdxContext &ctx); + static JoinedSelectors prepareJoinedSelectors(const Query& q, QueryResults& result, NsLocker& locks, SelectFunctionsHolder& func, + std::vector&, const RdxContext& ctx); template - [[nodiscard]] static std::vector preselectSubQueries(Query &mainQuery, std::vector &queryResultsHolder, - NsLocker &, SelectFunctionsHolder &, const RdxContext &); + [[nodiscard]] static std::vector preselectSubQueries(Query& mainQuery, std::vector& queryResultsHolder, + NsLocker&, SelectFunctionsHolder&, const RdxContext&); template - [[nodiscard]] static bool selectSubQuery(const Query &subQuery, const Query &mainQuery, NsLocker &, SelectFunctionsHolder &, - std::vector &, const RdxContext &); + [[nodiscard]] static bool selectSubQuery(const Query& subQuery, const Query& mainQuery, NsLocker&, SelectFunctionsHolder&, + std::vector&, const RdxContext&); template - [[nodiscard]] static VariantArray selectSubQuery(const Query &subQuery, const Query &mainQuery, NsLocker &, QueryResults &, - SelectFunctionsHolder &, std::variant fieldOrKeys, - std::vector &, const RdxContext &); - static StoredValuesOptimizationStatus isPreResultValuesModeOptimizationAvailable(const Query &jItemQ, const NamespaceImpl::Ptr &jns, - const Query &mainQ); + [[nodiscard]] static VariantArray selectSubQuery(const Query& subQuery, const Query& mainQuery, NsLocker&, QueryResults&, + SelectFunctionsHolder&, std::variant fieldOrKeys, + std::vector&, const RdxContext&); + static StoredValuesOptimizationStatus isPreResultValuesModeOptimizationAvailable(const Query& jItemQ, const NamespaceImpl::Ptr& jns, + const Query& mainQ); }; } // namespace reindexer diff --git a/cpp_src/core/rollback.h b/cpp_src/core/rollback.h index aa6d867df..2eb7eac93 100644 --- a/cpp_src/core/rollback.h +++ b/cpp_src/core/rollback.h @@ -8,10 +8,10 @@ class RollBackBase { protected: RollBackBase() noexcept = default; virtual ~RollBackBase() = default; - RollBackBase(RollBackBase &&other) noexcept : disabled_{other.disabled_} { other.Disable(); } - RollBackBase(const RollBackBase &) = delete; - RollBackBase &operator=(const RollBackBase &) = delete; - RollBackBase &operator=(RollBackBase &&) = delete; + RollBackBase(RollBackBase&& other) noexcept : disabled_{other.disabled_} { other.Disable(); } + RollBackBase(const RollBackBase&) = delete; + RollBackBase& operator=(const RollBackBase&) = delete; + RollBackBase& operator=(RollBackBase&&) = delete; virtual void Disable() noexcept { disabled_ = true; } [[nodiscard]] bool IsDisabled() const noexcept { return disabled_; } diff --git a/cpp_src/core/schema.cc b/cpp_src/core/schema.cc index 1ae0e2237..217c83d67 100644 --- a/cpp_src/core/schema.cc +++ b/cpp_src/core/schema.cc @@ -38,13 +38,17 @@ void SchemaFieldsTypes::AddField(KeyValueType type, bool isArray) { types_[tagsP bool SchemaFieldsTypes::NeedToEmbedType(const std::string& objectType) const noexcept { auto it = objectTypes_.find(objectType); - if (it == objectTypes_.end()) return false; + if (it == objectTypes_.end()) { + return false; + } return it->second < int(tagsPath_.size()); } KeyValueType SchemaFieldsTypes::GetField(const TagsPath& fieldPath, bool& isArray) const { auto it = types_.find(fieldPath); - if (it == types_.end()) return KeyValueType::Undefined{}; + if (it == types_.end()) { + return KeyValueType::Undefined{}; + } isArray = it->second.isArray_; return it->second.type_; } @@ -222,7 +226,9 @@ Error PrefixTree::buildProtobufSchema(ProtobufSchemaBuilder& builder, const Pref const std::unique_ptr& node = child.second; std::string path = basePath; - if (path.size() > 0) path += "."; + if (path.size() > 0) { + path += "."; + } path += name; int fieldNumber = tm.name2tag(name, true); diff --git a/cpp_src/core/selectfunc/ctx/ftctx.cc b/cpp_src/core/selectfunc/ctx/ftctx.cc index c185dc124..5964c81a0 100644 --- a/cpp_src/core/selectfunc/ctx/ftctx.cc +++ b/cpp_src/core/selectfunc/ctx/ftctx.cc @@ -2,16 +2,16 @@ namespace reindexer { -bool FtCtx::PrepareAreas(const RHashMap &fields, const std::string &name) { +bool FtCtx::PrepareAreas(const RHashMap& fields, const std::string& name) { assertrx_dbg(!NeedArea()); - auto &data = *data_; + auto& data = *data_; if (!fields.empty()) { data.isComposite_ = true; } bool needArea = false; if (data.isComposite_) { - for (auto &field : fields) { + for (auto& field : fields) { needArea = CheckFunction(field.first, {SelectFuncType::Snippet, SelectFuncType::SnippetN, SelectFuncType::Highlight}); if (needArea) { break; @@ -26,8 +26,8 @@ bool FtCtx::PrepareAreas(const RHashMap &fields, const std::st } template -void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, AreaHolder &&holder) { - auto &data = *data_; +void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, AreaHolder&& holder) { + auto& data = *data_; data.area_.emplace_back(std::move(holder)); for (; begin != end; ++begin) { data.proc_.emplace_back(proc); @@ -39,19 +39,21 @@ void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, AreaHolder template void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc) { - auto &data = *data_; + auto& data = *data_; for (; begin != end; ++begin) { data.proc_.emplace_back(proc); } } template -void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector &mask, AreaHolder &&holder) { - auto &data = *data_; +void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector& mask, AreaHolder&& holder) { + auto& data = *data_; data.area_.emplace_back(std::move(holder)); for (; begin != end; ++begin) { assertrx(static_cast(*begin) < mask.size()); - if (!mask[*begin]) continue; + if (!mask[*begin]) { + continue; + } data.proc_.emplace_back(proc); if (data.holders_.has_value()) { data.holders_->emplace(*begin, data.area_.size() - 1); @@ -60,21 +62,23 @@ void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, const std: } template -void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector &mask) { - auto &data = *data_; +void FtCtx::Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector& mask) { + auto& data = *data_; for (; begin != end; ++begin) { assertrx(static_cast(*begin) < mask.size()); - if (!mask[*begin]) continue; + if (!mask[*begin]) { + continue; + } data.proc_.emplace_back(proc); } } template void FtCtx::Add::iterator>(span::iterator begin, span::iterator end, int16_t proc, - AreaHolder &&holder); + AreaHolder&& holder); template void FtCtx::Add::iterator>(span::iterator begin, span::iterator end, int16_t proc, - const std::vector &, AreaHolder &&holder); + const std::vector&, AreaHolder&& holder); template void FtCtx::Add::iterator>(span::iterator begin, span::iterator end, int16_t proc); template void FtCtx::Add::iterator>(span::iterator begin, span::iterator end, int16_t proc, - const std::vector &); + const std::vector&); } // namespace reindexer diff --git a/cpp_src/core/selectfunc/ctx/ftctx.h b/cpp_src/core/selectfunc/ctx/ftctx.h index bc85c3218..2031cb8d5 100644 --- a/cpp_src/core/selectfunc/ctx/ftctx.h +++ b/cpp_src/core/selectfunc/ctx/ftctx.h @@ -31,22 +31,22 @@ class FtCtx : public BaseFunctionCtx { int16_t Proc(size_t pos) const noexcept { return (pos < data_->proc_.size()) ? data_->proc_[pos] : 0; } template - void Add(InputIterator begin, InputIterator end, int16_t proc, AreaHolder &&holder); + void Add(InputIterator begin, InputIterator end, int16_t proc, AreaHolder&& holder); template void Add(InputIterator begin, InputIterator end, int16_t proc); template - void Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector &mask, AreaHolder &&holder); + void Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector& mask, AreaHolder&& holder); template - void Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector &mask); + void Add(InputIterator begin, InputIterator end, int16_t proc, const std::vector& mask); void Reserve(size_t size) { data_->proc_.reserve(size); } size_t Size() const noexcept { return data_->proc_.size(); } bool NeedArea() const noexcept { return data_->NeedArea(); } - bool PrepareAreas(const RHashMap &fields, const std::string &name); + bool PrepareAreas(const RHashMap& fields, const std::string& name); void SetData(Data::Ptr data) noexcept { data_ = std::move(data); } - const Data::Ptr &GetData() const noexcept { return data_; } + const Data::Ptr& GetData() const noexcept { return data_; } private: Data::Ptr data_; diff --git a/cpp_src/core/selectfunc/functions/highlight.cc b/cpp_src/core/selectfunc/functions/highlight.cc index 508b51532..ecbad1e7d 100644 --- a/cpp_src/core/selectfunc/functions/highlight.cc +++ b/cpp_src/core/selectfunc/functions/highlight.cc @@ -7,13 +7,17 @@ namespace reindexer { -bool Highlight::Process(ItemRef &res, PayloadType &pl_type, const SelectFuncStruct &func, std::vector &stringsHolder) { - if (func.funcArgs.size() < 2) throw Error(errParams, "Invalid highlight params need minimum 2 - have %d", func.funcArgs.size()); +bool Highlight::Process(ItemRef& res, PayloadType& pl_type, const SelectFuncStruct& func, std::vector& stringsHolder) { + if (func.funcArgs.size() < 2) { + throw Error(errParams, "Invalid highlight params need minimum 2 - have %d", func.funcArgs.size()); + } - if (!func.ctx || func.ctx->type != BaseFunctionCtx::kFtCtx) return false; + if (!func.ctx || func.ctx->type != BaseFunctionCtx::kFtCtx) { + return false; + } FtCtx::Ptr ftctx = reindexer::static_ctx_pointer_cast(func.ctx); - auto &dataFtCtx = *ftctx->GetData(); + auto& dataFtCtx = *ftctx->GetData(); if (!dataFtCtx.holders_.has_value()) { return false; } @@ -35,10 +39,12 @@ bool Highlight::Process(ItemRef &res, PayloadType &pl_type, const SelectFuncStru throw Error(errLogic, "Unable to apply highlight function to the non-string field '%s'", func.field); } - const std::string *data = p_string(kr[0]).getCxxstr(); + const std::string* data = p_string(kr[0]).getCxxstr(); auto pva = dataFtCtx.area_[it->second].GetAreas(func.fieldNo); - if (!pva || pva->Empty()) return false; - auto &va = *pva; + if (!pva || pva->Empty()) { + return false; + } + auto& va = *pva; std::string result_string; result_string.reserve(data->size() + va.Size() * (func.funcArgs[0].size() + func.funcArgs[1].size())); diff --git a/cpp_src/core/selectfunc/functions/highlight.h b/cpp_src/core/selectfunc/functions/highlight.h index d82fc7ffe..32dc62137 100644 --- a/cpp_src/core/selectfunc/functions/highlight.h +++ b/cpp_src/core/selectfunc/functions/highlight.h @@ -8,7 +8,7 @@ struct SelectFuncStruct; class Highlight { public: - bool Process(ItemRef &res, PayloadType &pl_type, const SelectFuncStruct &func, std::vector &stringsHolder); + bool Process(ItemRef& res, PayloadType& pl_type, const SelectFuncStruct& func, std::vector& stringsHolder); }; } // namespace reindexer diff --git a/cpp_src/core/selectfunc/functions/snippet.cc b/cpp_src/core/selectfunc/functions/snippet.cc index 0f3bc28db..a6affd5fc 100644 --- a/cpp_src/core/selectfunc/functions/snippet.cc +++ b/cpp_src/core/selectfunc/functions/snippet.cc @@ -36,16 +36,20 @@ class Utf8CharCalculator { unsigned long charCounter_ = 0; }; -void Snippet::init(const SelectFuncStruct &func) { - if (isInit_) return; - if (func.funcArgs.size() < 4) throw Error(errParams, "Invalid snippet params need minimum 4 - have %d", func.funcArgs.size()); +void Snippet::init(const SelectFuncStruct& func) { + if (isInit_) { + return; + } + if (func.funcArgs.size() < 4) { + throw Error(errParams, "Invalid snippet params need minimum 4 - have %d", func.funcArgs.size()); + } try { std::size_t pos; before_ = stoul(func.funcArgs[2], &pos); if (pos != func.funcArgs[2].size()) { throw Error(errParams, "Invalid snippet param before - %s is not a number", func.funcArgs[2]); } - } catch (std::exception &) { + } catch (std::exception&) { throw Error(errParams, "Invalid snippet param before - %s is not a number", func.funcArgs[2]); } @@ -55,7 +59,7 @@ void Snippet::init(const SelectFuncStruct &func) { if (pos != func.funcArgs[3].size()) { throw Error(errParams, "Invalid snippet param after - %s is not a number", func.funcArgs[3]); } - } catch (std::exception &) { + } catch (std::exception&) { throw Error(errParams, "Invalid snippet param after - %s is not a number", func.funcArgs[3]); } if (std::holds_alternative(func.func)) { @@ -95,8 +99,8 @@ void Snippet::init(const SelectFuncStruct &func) { isInit_ = true; } -void Snippet::addSnippet(std::string &resultString, const std::string &data, const Area &snippetAreaPrev, - const Area &snippetAreaPrevChar) const { +void Snippet::addSnippet(std::string& resultString, const std::string& data, const Area& snippetAreaPrev, + const Area& snippetAreaPrevChar) const { resultString.append(preDelim_); resultString += '['; @@ -119,7 +123,7 @@ void Snippet::addSnippet(std::string &resultString, const std::string &data, con } resultString.append(data.begin() + zonesList_[z - 1].end, data.begin() + snippetAreaPrev.end); resultString.append(postDelim_); -}; +} struct Areas { Area zoneArea; @@ -131,7 +135,7 @@ struct AreasEx : public Areas { }; template -A Snippet::RecalcZoneHelper::RecalcZoneToOffset(const Area &area) { +A Snippet::RecalcZoneHelper::RecalcZoneToOffset(const Area& area) { using PointType = std::conditional_t, WordPosition, WordPositionEx>; constexpr bool needChar = std::is_same_v; A outAreas; @@ -181,13 +185,13 @@ A Snippet::RecalcZoneHelper::RecalcZoneToOffset(const Area &area) { return outAreas; } -void Snippet::buildResult(RecalcZoneHelper &recalcZoneHelper, const AreaBuffer &pva, const std::string &data, std::string &resultString) { +void Snippet::buildResult(RecalcZoneHelper& recalcZoneHelper, const AreaBuffer& pva, const std::string& data, std::string& resultString) { // resultString =preDelim_+with_area_str+data_str_before+marker_before+zone_str+marker_after+data_strAfter+postDelim_ Area snippetAreaPrev; Area snippetAreaPrevChar; zonesList_.clear(); - for (const auto &area : pva.GetData()) { + for (const auto& area : pva.GetData()) { Areas a = recalcZoneHelper.RecalcZoneToOffset(area); if (snippetAreaPrev.start == 0 && snippetAreaPrev.end == 0) { @@ -224,14 +228,14 @@ void Snippet::buildResult(RecalcZoneHelper &recalcZoneHelper, const AreaBuffer & resultString.append(postDelim_); } -void Snippet::buildResultWithPrefix(RecalcZoneHelper &recalcZoneHelper, const AreaBuffer &pva, const std::string &data, - std::string &resultString) { +void Snippet::buildResultWithPrefix(RecalcZoneHelper& recalcZoneHelper, const AreaBuffer& pva, const std::string& data, + std::string& resultString) { // resultString =preDelim_+with_area_str+data_str_before+marker_before+zone_str+marker_after+data_strAfter+postDelim_ Area snippetAreaPrev; Area snippetAreaPrevChar; zonesList_.clear(); - for (const auto &area : pva.GetData()) { + for (const auto& area : pva.GetData()) { AreasEx a = recalcZoneHelper.RecalcZoneToOffset(area); if (snippetAreaPrev.start == 0 && snippetAreaPrev.end == 0) { snippetAreaPrev = a.snippetArea; @@ -253,12 +257,14 @@ void Snippet::buildResultWithPrefix(RecalcZoneHelper &recalcZoneHelper, const Ar addSnippet(resultString, data, snippetAreaPrev, snippetAreaPrevChar); } -bool Snippet::Process(ItemRef &res, PayloadType &pl_type, const SelectFuncStruct &func, std::vector &stringsHolder) { - if (!func.ctx) return false; +bool Snippet::Process(ItemRef& res, PayloadType& pl_type, const SelectFuncStruct& func, std::vector& stringsHolder) { + if (!func.ctx) { + return false; + } init(func); FtCtx::Ptr ftctx = reindexer::static_ctx_pointer_cast(func.ctx); - auto &dataFtCtx = *ftctx->GetData(); + auto& dataFtCtx = *ftctx->GetData(); if (!dataFtCtx.isWordPositions_) { throw Error(errParams, "Snippet function does not work with ft_fuzzy index."); } @@ -280,9 +286,11 @@ bool Snippet::Process(ItemRef &res, PayloadType &pl_type, const SelectFuncStruct throw Error(errLogic, "Unable to apply snippet function to the non-string field '%s'", func.field); } - const std::string *data = p_string(kr[0]).getCxxstr(); + const std::string* data = p_string(kr[0]).getCxxstr(); auto pva = dataFtCtx.area_[it->second].GetAreas(func.fieldNo); - if (!pva || pva->Empty()) return false; + if (!pva || pva->Empty()) { + return false; + } std::string resultString; resultString.reserve(data->size()); diff --git a/cpp_src/core/selectfunc/functions/snippet.h b/cpp_src/core/selectfunc/functions/snippet.h index 25c371b93..d1240c8a5 100644 --- a/cpp_src/core/selectfunc/functions/snippet.h +++ b/cpp_src/core/selectfunc/functions/snippet.h @@ -9,15 +9,15 @@ struct SelectFuncStruct; class Snippet { public: - bool Process(ItemRef &res, PayloadType &pl_type, const SelectFuncStruct &func, std::vector &stringsHolder); + bool Process(ItemRef& res, PayloadType& pl_type, const SelectFuncStruct& func, std::vector& stringsHolder); private: - void init(const SelectFuncStruct &func); - void addSnippet(std::string &resultString, const std::string &data, const Area &snippetAreaPrev, const Area &snippetAreaPrevChar) const; + void init(const SelectFuncStruct& func); + void addSnippet(std::string& resultString, const std::string& data, const Area& snippetAreaPrev, const Area& snippetAreaPrevChar) const; class RecalcZoneHelper { public: - RecalcZoneHelper(std::string_view data, const std::string &extra, unsigned int after, unsigned int before, + RecalcZoneHelper(std::string_view data, const std::string& extra, unsigned int after, unsigned int before, std::string_view leftBound, std::string_view rightBound) noexcept : str_(data), data_(data), @@ -27,7 +27,7 @@ class Snippet { leftBound_(leftBound), rightBound_(rightBound) {} template - A RecalcZoneToOffset(const Area &area); + A RecalcZoneToOffset(const Area& area); private: std::string_view str_; @@ -35,14 +35,14 @@ class Snippet { size_t wordCount_ = 0; int stringBeginOffsetByte_ = 0; int stringBeginOffsetChar_ = 0; - const std::string &extraWordSymbols_; + const std::string& extraWordSymbols_; unsigned int after_, before_; std::string_view leftBound_, rightBound_; }; - void buildResult(RecalcZoneHelper &recalcZoneHelper, const AreaBuffer &pva, const std::string &data, std::string &resultString); - void buildResultWithPrefix(RecalcZoneHelper &recalcZoneHelper, const AreaBuffer &pva, const std::string &data, - std::string &resultString); + void buildResult(RecalcZoneHelper& recalcZoneHelper, const AreaBuffer& pva, const std::string& data, std::string& resultString); + void buildResultWithPrefix(RecalcZoneHelper& recalcZoneHelper, const AreaBuffer& pva, const std::string& data, + std::string& resultString); bool isInit_ = false; bool needAreaStr_ = false; diff --git a/cpp_src/core/selectfunc/nsselectfuncinterface.cc b/cpp_src/core/selectfunc/nsselectfuncinterface.cc index e5e470865..d2ec3c81a 100644 --- a/cpp_src/core/selectfunc/nsselectfuncinterface.cc +++ b/cpp_src/core/selectfunc/nsselectfuncinterface.cc @@ -3,7 +3,7 @@ #include "core/namespace/namespaceimpl.h" namespace reindexer { -const std::string& NsSelectFuncInterface::GetName() const noexcept { return nm_.name_; }; +const std::string& NsSelectFuncInterface::GetName() const noexcept { return nm_.name_; } int NsSelectFuncInterface::getIndexByName(std::string_view index) const noexcept { return nm_.getIndexByName(index); } bool NsSelectFuncInterface::getIndexByName(std::string_view name, int& index) const noexcept { return nm_.tryGetIndexByName(name, index); } int NsSelectFuncInterface::getIndexesCount() const noexcept { return nm_.indexes_.size(); } diff --git a/cpp_src/core/selectfunc/selectfunc.cc b/cpp_src/core/selectfunc/selectfunc.cc index 2c4bc50f2..734936b24 100644 --- a/cpp_src/core/selectfunc/selectfunc.cc +++ b/cpp_src/core/selectfunc/selectfunc.cc @@ -8,22 +8,22 @@ namespace reindexer { -inline void ltrim(std::string &s) { +inline void ltrim(std::string& s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); })); } // trim from end (in place) -inline void rtrim(std::string &s) { +inline void rtrim(std::string& s) { s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end()); } // trim from both ends (in place) -inline void trim(std::string &s) { +inline void trim(std::string& s) { ltrim(s); rtrim(s); } -SelectFunction::Ptr SelectFunctionsHolder::AddNamespace(const Query &q, const NamespaceImpl &nm, uint32_t nsid, bool force) { +SelectFunction::Ptr SelectFunctionsHolder::AddNamespace(const Query& q, const NamespaceImpl& nm, uint32_t nsid, bool force) { if (q.selectFunctions_.empty() && !force) { return nullptr; } else if (!q.selectFunctions_.empty()) { @@ -37,21 +37,25 @@ SelectFunction::Ptr SelectFunctionsHolder::AddNamespace(const Query &q, const Na return queries_[nsid]; } -SelectFunction::SelectFunction(const Query &q, NsSelectFuncInterface &&nm) : nm_(std::move(nm)), currCjsonFieldIdx_(nm_.getIndexesCount()) { - for (auto &func : q.selectFunctions_) { +SelectFunction::SelectFunction(const Query& q, NsSelectFuncInterface&& nm) : nm_(std::move(nm)), currCjsonFieldIdx_(nm_.getIndexesCount()) { + for (auto& func : q.selectFunctions_) { SelectFuncParser parser; - SelectFuncStruct &result = parser.Parse(func); - if (!result.isFunction) continue; + SelectFuncStruct& result = parser.Parse(func); + if (!result.isFunction) { + continue; + } createFunc(result); } } -void SelectFunction::createFunc(SelectFuncStruct &data) { +void SelectFunction::createFunc(SelectFuncStruct& data) { int indexNo = IndexValueType::NotSet; if (data.indexNo == IndexValueType::NotSet) { if (!nm_.getIndexByName(data.field, indexNo)) { trim(data.field); - if (!nm_.getIndexByName(data.field, indexNo)) return; + if (!nm_.getIndexByName(data.field, indexNo)) { + return; + } } } else { indexNo = data.indexNo; @@ -60,7 +64,7 @@ void SelectFunction::createFunc(SelectFuncStruct &data) { // if index is composite then create function for inner use only if (IsComposite(nm_.getIndexType(indexNo))) { int fieldNo = 0; - const FieldsSet &fields = nm_.getIndexFields(indexNo); + const FieldsSet& fields = nm_.getIndexFields(indexNo); int jsPathIdx = 0; for (auto field : fields) { @@ -92,7 +96,9 @@ BaseFunctionCtx::Ptr SelectFunction::createFuncForProc(int indexNo) { createFunc(data); if (IsComposite(nm_.getIndexType(indexNo))) { auto field = nm_.getIndexFields(indexNo)[0]; - if (field == IndexValueType::SetByJsonPath) field = lastCjsonIdx; + if (field == IndexValueType::SetByJsonPath) { + field = lastCjsonIdx; + } auto it = functions_.find(field); assertrx(it != functions_.end()); return createCtx(it->second, nullptr, nm_.getIndexType(indexNo)); @@ -104,7 +110,9 @@ BaseFunctionCtx::Ptr SelectFunction::createFuncForProc(int indexNo) { } bool SelectFunction::NeedArea(int indexNo) const { - if (functions_.empty()) return false; + if (functions_.empty()) { + return false; + } IndexType indexType = nm_.getIndexType(indexNo); auto checkField = [&](int field) -> bool { @@ -121,8 +129,12 @@ bool SelectFunction::NeedArea(int indexNo) const { if (IsComposite(indexType)) { int cjsonFieldIdx = nm_.getIndexesCount(); for (auto field : nm_.getIndexFields(indexNo)) { - if (field == IndexValueType::SetByJsonPath) field = cjsonFieldIdx++; - if (checkField(field)) return true; + if (field == IndexValueType::SetByJsonPath) { + field = cjsonFieldIdx++; + } + if (checkField(field)) { + return true; + } } } else { return checkField(indexNo); @@ -145,7 +157,9 @@ BaseFunctionCtx::Ptr SelectFunction::CreateCtx(int indexNo) { int fieldNo = 0; int cjsonFieldIdx = nm_.getIndexesCount(); for (auto field : nm_.getIndexFields(indexNo)) { - if (field == IndexValueType::SetByJsonPath) field = cjsonFieldIdx++; + if (field == IndexValueType::SetByJsonPath) { + field = cjsonFieldIdx++; + } auto it = functions_.find(field); if (it != functions_.end()) { it->second.fieldNo = fieldNo; @@ -165,48 +179,52 @@ BaseFunctionCtx::Ptr SelectFunction::CreateCtx(int indexNo) { } return ctx; } -void SelectFunctionsHolder::Process(QueryResults &res) { - if (queries_.empty() || force_only_) return; +void SelectFunctionsHolder::Process(QueryResults& res) { + if (queries_.empty() || force_only_) { + return; + } bool hasFuncs = false; - for (auto &q : queries_) { + for (auto& q : queries_) { if (q) { hasFuncs = true; break; } } - if (!hasFuncs) return; + if (!hasFuncs) { + return; + } bool changed = false; - for (auto &item : res.Items()) { + for (auto& item : res.Items()) { const auto nsid = item.Nsid(); if (queries_.size() <= nsid) { continue; } - if (auto &funcPtr = queries_[nsid]; funcPtr && funcPtr->ProcessItem(item, res.getPayloadType(nsid), res.stringsHolder_)) { + if (auto& funcPtr = queries_[nsid]; funcPtr && funcPtr->ProcessItem(item, res.getPayloadType(nsid), res.stringsHolder_)) { changed = true; } } res.nonCacheableData = changed; } -bool SelectFunction::ProcessItem(ItemRef &res, PayloadType &pl_type, std::vector &stringsHolder) { +bool SelectFunction::ProcessItem(ItemRef& res, PayloadType& pl_type, std::vector& stringsHolder) { bool changed = false; - for (auto &func : functions_) { + for (auto& func : functions_) { if (func.second.ctx && - std::visit([&](auto &f) -> bool { return f.Process(res, pl_type, func.second, stringsHolder); }, func.second.func)) { + std::visit([&](auto& f) -> bool { return f.Process(res, pl_type, func.second, stringsHolder); }, func.second.func)) { changed = true; } } return changed; } -BaseFunctionCtx::Ptr SelectFunction::createCtx(SelectFuncStruct &data, BaseFunctionCtx::Ptr ctx, IndexType index_type) { +BaseFunctionCtx::Ptr SelectFunction::createCtx(SelectFuncStruct& data, BaseFunctionCtx::Ptr ctx, IndexType index_type) { if (IsFullText(index_type)) { if (!ctx) { data.ctx = make_intrusive(); } else { data.ctx = std::move(ctx); } - const std::string &indexName = (data.indexNo >= nm_.getIndexesCount()) ? data.field : nm_.getIndexName(data.indexNo); + const std::string& indexName = (data.indexNo >= nm_.getIndexesCount()) ? data.field : nm_.getIndexName(data.indexNo); data.ctx->AddFunction(indexName, SelectFuncType(data.func.index())); } return data.ctx; diff --git a/cpp_src/core/selectfunc/selectfuncparser.cc b/cpp_src/core/selectfunc/selectfuncparser.cc index 265121e59..797d7e295 100644 --- a/cpp_src/core/selectfunc/selectfuncparser.cc +++ b/cpp_src/core/selectfunc/selectfuncparser.cc @@ -6,7 +6,7 @@ namespace reindexer { -SelectFuncStruct &SelectFuncParser::Parse(const std::string &query) { +SelectFuncStruct& SelectFuncParser::Parse(const std::string& query) { tokenizer parser(query); token tok = parser.next_token(tokenizer::flags::no_flags); @@ -41,7 +41,7 @@ SelectFuncStruct &SelectFuncParser::Parse(const std::string &query) { return selectFuncStruct_; } -void SelectFuncParser::parsePositionalAndNamedArgs(tokenizer &parser, const Args &args) { +void SelectFuncParser::parsePositionalAndNamedArgs(tokenizer& parser, const Args& args) { using namespace std::string_view_literals; token tok; tok = parser.next_token(tokenizer::flags::no_flags); @@ -176,7 +176,7 @@ void SelectFuncParser::parsePositionalAndNamedArgs(tokenizer &parser, const Args } } -SelectFuncStruct &SelectFuncParser::ParseFunction(tokenizer &parser, bool partOfExpression, token &tok) { +SelectFuncStruct& SelectFuncParser::ParseFunction(tokenizer& parser, bool partOfExpression, token& tok) { using namespace std::string_view_literals; if (tok.text().empty()) { tok = parser.next_token(); @@ -232,32 +232,56 @@ SelectFuncStruct &SelectFuncParser::ParseFunction(tokenizer &parser, bool partOf } bool SelectFuncParser::IsFunction(std::string_view val) noexcept { - if (val.length() < 3) return false; + if (val.length() < 3) { + return false; + } size_t i = 0; - if (!isalpha(val[i++])) return false; + if (!isalpha(val[i++])) { + return false; + } int openParenthesis = 0, closeParenthesis = 0; for (; i < val.length(); ++i) { char ch = val[i]; switch (ch) { case '(': - if (openParenthesis++ > 0) return false; - if (closeParenthesis > 0) return false; + if (openParenthesis++ > 0) { + return false; + } + if (closeParenthesis > 0) { + return false; + } break; case ')': - if (openParenthesis != 1) return false; - if (closeParenthesis++ > 0) return false; - if (i == val.length() - 1) return true; + if (openParenthesis != 1) { + return false; + } + if (closeParenthesis++ > 0) { + return false; + } + if (i == val.length() - 1) { + return true; + } break; case ',': - if (openParenthesis != 1) return false; - if (closeParenthesis != 0) return false; - if (i == val.length() - 1) return false; + if (openParenthesis != 1) { + return false; + } + if (closeParenthesis != 0) { + return false; + } + if (i == val.length() - 1) { + return false; + } break; default: - if (openParenthesis > 1) return false; - if (closeParenthesis > 0) return false; + if (openParenthesis > 1) { + return false; + } + if (closeParenthesis > 0) { + return false; + } break; } } @@ -265,9 +289,13 @@ bool SelectFuncParser::IsFunction(std::string_view val) noexcept { return false; } -bool SelectFuncParser::IsFunction(const VariantArray &val) noexcept { - if (val.size() != 1) return false; - if (!val.front().Type().Is()) return false; +bool SelectFuncParser::IsFunction(const VariantArray& val) noexcept { + if (val.size() != 1) { + return false; + } + if (!val.front().Type().Is()) { + return false; + } return IsFunction(static_cast(val.front())); } diff --git a/cpp_src/core/selectfunc/selectfuncparser.h b/cpp_src/core/selectfunc/selectfuncparser.h index 6e2427239..0d7894f9e 100644 --- a/cpp_src/core/selectfunc/selectfuncparser.h +++ b/cpp_src/core/selectfunc/selectfuncparser.h @@ -28,19 +28,19 @@ struct SelectFuncStruct { class SelectFuncParser { public: - SelectFuncStruct &Parse(const std::string &query); - SelectFuncStruct &ParseFunction(tokenizer &parser, bool partOfExpression, token &tok); + SelectFuncStruct& Parse(const std::string& query); + SelectFuncStruct& ParseFunction(tokenizer& parser, bool partOfExpression, token& tok); static bool IsFunction(std::string_view val) noexcept; - static bool IsFunction(const VariantArray &val) noexcept; + static bool IsFunction(const VariantArray& val) noexcept; protected: struct Args { - explicit Args(unsigned int p, std::unordered_set &&n) : posArgsCount(p), namedArgs(std::move(n)) {} + explicit Args(unsigned int p, std::unordered_set&& n) : posArgsCount(p), namedArgs(std::move(n)) {} unsigned int posArgsCount; std::unordered_set namedArgs; }; - void parsePositionalAndNamedArgs(tokenizer &parser, const Args &args); + void parsePositionalAndNamedArgs(tokenizer& parser, const Args& args); SelectFuncStruct selectFuncStruct_; }; diff --git a/cpp_src/core/selectkeyresult.h b/cpp_src/core/selectkeyresult.h index 665f1b8af..f45b67fc9 100644 --- a/cpp_src/core/selectkeyresult.h +++ b/cpp_src/core/selectkeyresult.h @@ -21,11 +21,11 @@ class SingleSelectKeyResult { public: SingleSelectKeyResult() noexcept {} - explicit SingleSelectKeyResult(IndexIterator::Ptr &&indexForwardIter) noexcept : indexForwardIter_(std::move(indexForwardIter)) { + explicit SingleSelectKeyResult(IndexIterator::Ptr&& indexForwardIter) noexcept : indexForwardIter_(std::move(indexForwardIter)) { assertrx(indexForwardIter_ != nullptr); } template - explicit SingleSelectKeyResult(const KeyEntryT &ids, SortType sortId) noexcept { + explicit SingleSelectKeyResult(const KeyEntryT& ids, SortType sortId) noexcept { if (ids.Unsorted().IsCommited()) { ids_ = ids.Sorted(sortId); } else { @@ -35,10 +35,10 @@ class SingleSelectKeyResult { useBtree_ = true; } } - explicit SingleSelectKeyResult(IdSet::Ptr &&ids) noexcept : tempIds_(std::move(ids)), ids_(*tempIds_) {} + explicit SingleSelectKeyResult(IdSet::Ptr&& ids) noexcept : tempIds_(std::move(ids)), ids_(*tempIds_) {} explicit SingleSelectKeyResult(IdSetCRef ids) noexcept : ids_(ids) {} explicit SingleSelectKeyResult(IdType rBegin, IdType rEnd) noexcept : rBegin_(rBegin), rEnd_(rEnd), isRange_(true) {} - SingleSelectKeyResult(const SingleSelectKeyResult &other) noexcept + SingleSelectKeyResult(const SingleSelectKeyResult& other) noexcept : tempIds_(other.tempIds_), ids_(other.ids_), set_(other.set_), @@ -65,7 +65,7 @@ class SingleSelectKeyResult { } } } - SingleSelectKeyResult &operator=(const SingleSelectKeyResult &other) noexcept { + SingleSelectKeyResult& operator=(const SingleSelectKeyResult& other) noexcept { if (&other != this) { tempIds_ = other.tempIds_; ids_ = other.ids_; @@ -100,7 +100,7 @@ class SingleSelectKeyResult { IdSetCRef ids_; protected: - const base_idsetset *set_ = nullptr; + const base_idsetset* set_ = nullptr; union { IdSetCRef::const_iterator begin_; @@ -175,7 +175,7 @@ class SelectKeyResult : public h_vector { /// @return amount of loops. size_t GetMaxIterations(size_t limitIters = std::numeric_limits::max()) const noexcept { size_t cnt = 0; - for (const SingleSelectKeyResult &r : *this) { + for (const SingleSelectKeyResult& r : *this) { if (r.indexForwardIter_) { cnt += r.indexForwardIter_->GetMaxIterations(limitIters); } else if (r.isRange_) { @@ -185,7 +185,9 @@ class SelectKeyResult : public h_vector { } else { cnt += r.ids_.size(); } - if (cnt > limitIters) break; + if (cnt > limitIters) { + break; + } } return cnt; } @@ -198,7 +200,7 @@ class SelectKeyResult : public h_vector { /// @param opts - merge customization options /// @return Pointer to a sorted IdSet object made /// from all the SingleSelectKeyResult inner objects. - IdSet::Ptr MergeIdsets(MergeOptions &&opts, size_t idsCount) { + IdSet::Ptr MergeIdsets(MergeOptions&& opts, size_t idsCount) { IdSet::Ptr mergedIds; if (opts.genericSort) { mergedIds = mergeGenericSort(idsCount); @@ -246,27 +248,27 @@ class SelectKeyResult : public h_vector { auto mergedIds = make_intrusive>(); mergedIds->reserve(idsCount); - auto firstSetIt = std::partition(begin(), end(), [](const SingleSelectKeyResult &v) noexcept { return !v.useBtree_; }); + auto firstSetIt = std::partition(begin(), end(), [](const SingleSelectKeyResult& v) noexcept { return !v.useBtree_; }); const auto vecsCnt = firstSetIt - begin(); - h_vector ptrsVec; + h_vector ptrsVec; ptrsVec.reserve(size()); - for (auto &v : *this) { + for (auto& v : *this) { if rx_unlikely (v.isRange_) { throw Error(errLogic, "Unable to merge 'range' idset ('merge sort mode')"); } ptrsVec.emplace_back(&v); } - span vecSpan(ptrsVec.data(), vecsCnt); - span setSpan(ptrsVec.data() + vecsCnt, size() - vecsCnt); + span vecSpan(ptrsVec.data(), vecsCnt); + span setSpan(ptrsVec.data() + vecsCnt, size() - vecsCnt); - for (auto &v : vecSpan) { + for (auto& v : vecSpan) { assertrx_dbg(!v->useBtree_); v->it_ = v->ids_.begin(); v->end_ = v->ids_.end(); } - for (auto &v : setSpan) { + for (auto& v : setSpan) { assertrx_dbg(v->useBtree_); v->itset_ = v->set_->begin(); v->setend_ = v->set_->end(); @@ -276,12 +278,12 @@ class SelectKeyResult : public h_vector { for (;;) { int curMin = INT_MAX; for (auto vsIt = vecSpan.begin(), vsItEnd = vecSpan.end(); vsIt != vsItEnd;) { - auto &itvec = (*vsIt)->it_; - auto &vecend = (*vsIt)->end_; + auto& itvec = (*vsIt)->it_; + auto& vecend = (*vsIt)->end_; for (;; ++itvec) { if (itvec == vecend) { std::swap(*vsIt, vecSpan.back()); - vecSpan = span(vecSpan.data(), vecSpan.size() - 1); + vecSpan = span(vecSpan.data(), vecSpan.size() - 1); --vsItEnd; break; } @@ -296,12 +298,12 @@ class SelectKeyResult : public h_vector { } } for (auto ssIt = setSpan.begin(), ssItEnd = setSpan.end(); ssIt != ssItEnd;) { - auto &itset = (*ssIt)->itset_; - auto &setend = (*ssIt)->setend_; + auto& itset = (*ssIt)->itset_; + auto& setend = (*ssIt)->setend_; for (;; ++itset) { if (itset == setend) { std::swap(*ssIt, setSpan.back()); - setSpan = span(setSpan.data(), setSpan.size() - 1); + setSpan = span(setSpan.data(), setSpan.size() - 1); --ssItEnd; break; } @@ -329,16 +331,16 @@ class SelectKeyResult : public h_vector { mergedIds->reserve(idsCount); struct IdSetGreater { - bool operator()(const value_type *l, const value_type *r) noexcept { + bool operator()(const value_type* l, const value_type* r) noexcept { const auto lval = l->useBtree_ ? *(l->itset_) : *(l->it_); const auto rval = r->useBtree_ ? *(r->itset_) : *(r->it_); return lval > rval; } }; - h_vector ptrsVec; + h_vector ptrsVec; ptrsVec.reserve(size()); - for (auto &v : *this) { + for (auto& v : *this) { if rx_unlikely (v.isRange_) { throw Error(errLogic, "Unable to merge 'range' idset ('merge sort mode')"); } @@ -357,10 +359,10 @@ class SelectKeyResult : public h_vector { } } } - span idsetsSpan(ptrsVec.data(), ptrsVec.size()); + span idsetsSpan(ptrsVec.data(), ptrsVec.size()); std::make_heap(idsetsSpan.begin(), idsetsSpan.end(), IdSetGreater{}); int min = INT_MIN; - auto handleMinValue = [&mergedIds, &idsetsSpan, &min](auto &it, auto end) noexcept { + auto handleMinValue = [&mergedIds, &idsetsSpan, &min](auto& it, auto end) noexcept { auto val = *it; if (val > min) { mergedIds->AddUnordered(val); @@ -369,20 +371,20 @@ class SelectKeyResult : public h_vector { do { if (++it == end) { std::swap(idsetsSpan.front(), idsetsSpan.back()); - idsetsSpan = span(idsetsSpan.begin(), idsetsSpan.size() - 1); + idsetsSpan = span(idsetsSpan.begin(), idsetsSpan.size() - 1); return; } } while (*it <= min); }; while (!idsetsSpan.empty()) { - auto &minV = *idsetsSpan.front(); + auto& minV = *idsetsSpan.front(); if (minV.useBtree_) { handleMinValue(minV.itset_, minV.setend_); } else { handleMinValue(minV.it_, minV.end_); } - heapifyRoot(idsetsSpan); + heapifyRoot(idsetsSpan); } return mergedIds; } @@ -390,14 +392,14 @@ class SelectKeyResult : public h_vector { template RX_ALWAYS_INLINE void heapifyRoot(span vec) noexcept { static_assert(std::is_pointer_v, "Expecting T being a pointer for the fast swaps"); - T *target = vec.begin(); - T *end = target + vec.size(); + T* target = vec.begin(); + T* end = target + vec.size(); CompareT c; for (size_t i = 0;;) { - T *cur = target; + T* cur = target; const auto lIdx = (i << 1) + 1; - T *left = vec.begin() + lIdx; - T *right = left + 1; + T* left = vec.begin() + lIdx; + T* right = left + 1; if (left < end && c(*target, *left)) { target = left; @@ -428,16 +430,16 @@ class SelectKeyResults : public std::variant - SelectKeyResults(ComparatorIndexed &&comp) noexcept : Base{std::move(comp)} {} - SelectKeyResults(ComparatorNotIndexed &&comp) noexcept : Base{std::move(comp)} {} + SelectKeyResults(ComparatorIndexed&& comp) noexcept : Base{std::move(comp)} {} + SelectKeyResults(ComparatorNotIndexed&& comp) noexcept : Base{std::move(comp)} {} void Clear() noexcept { std::get(*this).clear(); } - void EmplaceBack(SelectKeyResult &&sr) { std::get(*this).emplace_back(std::move(sr)); } - SelectKeyResult &&Front() && noexcept { return std::move(std::get(*this).front()); } - const Base &AsVariant() const & noexcept { return *this; } - Base &AsVariant() & noexcept { return *this; } - auto AsVariant() const && = delete; + void EmplaceBack(SelectKeyResult&& sr) { std::get(*this).emplace_back(std::move(sr)); } + SelectKeyResult&& Front() && noexcept { return std::move(std::get(*this).front()); } + const Base& AsVariant() const& noexcept { return *this; } + Base& AsVariant() & noexcept { return *this; } + auto AsVariant() const&& = delete; }; } // namespace reindexer diff --git a/cpp_src/core/sortingprioritiestable.cc b/cpp_src/core/sortingprioritiestable.cc index 8f86f59b1..c95a747a6 100644 --- a/cpp_src/core/sortingprioritiestable.cc +++ b/cpp_src/core/sortingprioritiestable.cc @@ -22,21 +22,26 @@ SortingPrioritiesTable::SortingPrioritiesTable(const std::string& sortOrderUTF8) for (int i = 0; i <= lastCharIdx; ++i) { auto ch(orderUtf16[i]); if (ch == '-') { - if ((i == 0) || (i == lastCharIdx)) + if ((i == 0) || (i == lastCharIdx)) { throw Error(errLogic, "Incorrect format of sort order string: '-' cannot be the first or the last character"); + } } else { if ((i != 0) && (orderUtf16[i - 1] == '-')) { - if (ch <= prevCh) throw Error(errLogic, "Incorrect format of sort order string: range should be ascending"); + if (ch <= prevCh) { + throw Error(errLogic, "Incorrect format of sort order string: range should be ascending"); + } for (auto it = prevCh; it <= ch; ++it) { - if (checkForRangeIntersection(ranges, it)) + if (checkForRangeIntersection(ranges, it)) { throw Error(errLogic, "There can't be 2 same formating characters in format string!"); + } sortOrder_->operator[](it) = priority++; } ranges.insert({prevCh, ch - prevCh + 1}); maxPriority = priority; } else if (((i + 1 <= lastCharIdx) && (orderUtf16[i + 1] != '-')) || (i == lastCharIdx)) { - if (checkForRangeIntersection(ranges, ch)) + if (checkForRangeIntersection(ranges, ch)) { throw Error(errLogic, "There can't be 2 same formating characters in format string!"); + } sortOrder_->operator[](ch) = priority++; ranges.insert({ch, 1}); maxPriority = priority; @@ -60,12 +65,18 @@ SortingPrioritiesTable::SortingPrioritiesTable(const std::string& sortOrderUTF8) } bool SortingPrioritiesTable::checkForRangeIntersection(std::map& ranges, wchar_t ch) { - if (ranges.empty()) return false; + if (ranges.empty()) { + return false; + } auto itLow = ranges.lower_bound(ch); - if (itLow == ranges.end()) itLow = ranges.begin(); + if (itLow == ranges.end()) { + itLow = ranges.begin(); + } auto itUp = ranges.upper_bound(ch); for (auto it = itLow; it != itUp; ++it) { - if ((ch >= it->first) && (ch < it->first + it->second)) return true; + if ((ch >= it->first) && (ch < it->first + it->second)) { + return true; + } } return false; } diff --git a/cpp_src/core/storage/leveldblogger.cc b/cpp_src/core/storage/leveldblogger.cc index eba6dc16c..8cc9b2bd0 100644 --- a/cpp_src/core/storage/leveldblogger.cc +++ b/cpp_src/core/storage/leveldblogger.cc @@ -11,7 +11,7 @@ namespace reindexer { namespace datastorage { class NoOpLogger : public leveldb::Logger { - void Logv(const char* /*format*/, va_list /*ap*/) override final {} + void Logv(const char* /*format*/, va_list /*ap*/) override final {} }; static NoOpLogger dummyLevelDBLogger; diff --git a/cpp_src/core/storage/leveldbstorage.cc b/cpp_src/core/storage/leveldbstorage.cc index 026a443d1..dae6241f4 100644 --- a/cpp_src/core/storage/leveldbstorage.cc +++ b/cpp_src/core/storage/leveldbstorage.cc @@ -26,63 +26,87 @@ static void toReadOptions(const StorageOpts& opts, leveldb::ReadOptions& ropts) LevelDbStorage::LevelDbStorage() = default; Error LevelDbStorage::Read(const StorageOpts& opts, std::string_view key, std::string& value) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } leveldb::ReadOptions options; toReadOptions(opts, options); leveldb::Status status = db_->Get(options, leveldb::Slice(key.data(), key.size()), &value); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(status.IsNotFound() ? errNotFound : errLogic, status.ToString()); } Error LevelDbStorage::Write(const StorageOpts& opts, std::string_view key, std::string_view value) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } leveldb::WriteOptions options; toWriteOptions(opts, options); leveldb::Status status = db_->Put(options, leveldb::Slice(key.data(), key.size()), leveldb::Slice(value.data(), value.size())); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(status.IsNotFound() ? errNotFound : errLogic, status.ToString()); } Error LevelDbStorage::Write(const StorageOpts& opts, UpdatesCollection& buffer) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } leveldb::WriteOptions options; toWriteOptions(opts, options); LevelDbBatchBuffer* batchBuffer = static_cast(&buffer); leveldb::Status status = db_->Write(options, &batchBuffer->batchWrite_); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(status.IsNotFound() ? errNotFound : errLogic, status.ToString()); } Error LevelDbStorage::Delete(const StorageOpts& opts, std::string_view key) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } leveldb::WriteOptions options; toWriteOptions(opts, options); leveldb::Status status = db_->Delete(options, leveldb::Slice(key.data(), key.size())); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(errLogic, status.ToString()); } Error LevelDbStorage::Repair(const std::string& path) { leveldb::Options options; auto status = leveldb::RepairDB(path, options); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(errLogic, status.ToString()); } Snapshot::Ptr LevelDbStorage::MakeSnapshot() { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } const leveldb::Snapshot* ldbSnapshot = db_->GetSnapshot(); assertrx(ldbSnapshot); return std::make_shared(ldbSnapshot); } void LevelDbStorage::ReleaseSnapshot(Snapshot::Ptr snapshot) { - if (!db_) throw Error(errParams, kStorageNotInitialized); - if (!snapshot) throw Error(errParams, "Storage pointer is null"); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } + if (!snapshot) { + throw Error(errParams, "Storage pointer is null"); + } const LevelDbSnapshot* levelDbSnpshot = static_cast(snapshot.get()); db_->ReleaseSnapshot(levelDbSnpshot->snapshot_); snapshot.reset(); @@ -110,7 +134,9 @@ Error LevelDbStorage::Reopen() { } Cursor* LevelDbStorage::GetCursor(StorageOpts& opts) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } leveldb::ReadOptions options; toReadOptions(opts, options); options.fill_cache = false; diff --git a/cpp_src/core/storage/rocksdbstorage.cc b/cpp_src/core/storage/rocksdbstorage.cc index 9535275b9..dc2dea9f1 100644 --- a/cpp_src/core/storage/rocksdbstorage.cc +++ b/cpp_src/core/storage/rocksdbstorage.cc @@ -25,63 +25,87 @@ static void toReadOptions(const StorageOpts& opts, rocksdb::ReadOptions& ropts) RocksDbStorage::RocksDbStorage() = default; Error RocksDbStorage::Read(const StorageOpts& opts, std::string_view key, std::string& value) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } rocksdb::ReadOptions options; toReadOptions(opts, options); rocksdb::Status status = db_->Get(options, rocksdb::Slice(key.data(), key.size()), &value); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(status.IsNotFound() ? errNotFound : errLogic, status.ToString()); } Error RocksDbStorage::Write(const StorageOpts& opts, std::string_view key, std::string_view value) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } rocksdb::WriteOptions options; toWriteOptions(opts, options); rocksdb::Status status = db_->Put(options, rocksdb::Slice(key.data(), key.size()), rocksdb::Slice(value.data(), value.size())); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(status.IsNotFound() ? errNotFound : errLogic, status.ToString()); } Error RocksDbStorage::Write(const StorageOpts& opts, UpdatesCollection& buffer) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } rocksdb::WriteOptions options; toWriteOptions(opts, options); RocksDbBatchBuffer* batchBuffer = static_cast(&buffer); rocksdb::Status status = db_->Write(options, &batchBuffer->batchWrite_); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(status.IsNotFound() ? errNotFound : errLogic, status.ToString()); } Error RocksDbStorage::Delete(const StorageOpts& opts, std::string_view key) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } rocksdb::WriteOptions options; toWriteOptions(opts, options); rocksdb::Status status = db_->Delete(options, rocksdb::Slice(key.data(), key.size())); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(errLogic, status.ToString()); } Error RocksDbStorage::Repair(const std::string& path) { rocksdb::Options options; auto status = rocksdb::RepairDB(path, options); - if (status.ok()) return Error(); + if (status.ok()) { + return Error(); + } return Error(errLogic, status.ToString()); } Snapshot::Ptr RocksDbStorage::MakeSnapshot() { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } const rocksdb::Snapshot* ldbSnapshot = db_->GetSnapshot(); assertrx(ldbSnapshot); return std::make_shared(ldbSnapshot); } void RocksDbStorage::ReleaseSnapshot(Snapshot::Ptr snapshot) { - if (!db_) throw Error(errParams, kStorageNotInitialized); - if (!snapshot) throw Error(errParams, "Storage pointer is null"); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } + if (!snapshot) { + throw Error(errParams, "Storage pointer is null"); + } const RocksDbSnapshot* levelDbSnpshot = static_cast(snapshot.get()); db_->ReleaseSnapshot(levelDbSnpshot->snapshot_); snapshot.reset(); @@ -109,7 +133,9 @@ Error RocksDbStorage::Reopen() { } Cursor* RocksDbStorage::GetCursor(StorageOpts& opts) { - if (!db_) throw Error(errParams, kStorageNotInitialized); + if (!db_) { + throw Error(errParams, kStorageNotInitialized); + } rocksdb::ReadOptions options; toReadOptions(opts, options); options.fill_cache = false; diff --git a/cpp_src/core/transaction.cc b/cpp_src/core/transaction.cc index 0bcb15282..1e63cd09f 100644 --- a/cpp_src/core/transaction.cc +++ b/cpp_src/core/transaction.cc @@ -2,65 +2,82 @@ #include "transactionimpl.h" namespace reindexer { -Transaction::Transaction(const std::string &nsName, const PayloadType &pt, const TagsMatcher &tm, const FieldsSet &pf, +Transaction::Transaction(const std::string& nsName, const PayloadType& pt, const TagsMatcher& tm, const FieldsSet& pf, std::shared_ptr schema) : impl_(new TransactionImpl(nsName, pt, tm, pf, std::move(schema))) {} -Transaction::Transaction(const Error &err) : status_(err) {} +Transaction::Transaction(const Error& err) : status_(err) {} Transaction::~Transaction() = default; -Transaction::Transaction(Transaction &&) noexcept = default; -Transaction &Transaction::operator=(Transaction &&) noexcept = default; +Transaction::Transaction(Transaction&&) noexcept = default; +Transaction& Transaction::operator=(Transaction&&) noexcept = default; -const std::string &Transaction::GetName() const { +const std::string& Transaction::GetName() const { static std::string empty; - if (impl_) + if (impl_) { return impl_->nsName_; - else + } else { return empty; + } } -void Transaction::Insert(Item &&item) { - if (impl_) impl_->Insert(std::move(item)); +void Transaction::Insert(Item&& item) { + if (impl_) { + impl_->Insert(std::move(item)); + } } -void Transaction::Update(Item &&item) { - if (impl_) impl_->Update(std::move(item)); +void Transaction::Update(Item&& item) { + if (impl_) { + impl_->Update(std::move(item)); + } } -void Transaction::Upsert(Item &&item) { - if (impl_) impl_->Upsert(std::move(item)); +void Transaction::Upsert(Item&& item) { + if (impl_) { + impl_->Upsert(std::move(item)); + } } -void Transaction::Delete(Item &&item) { - if (impl_) impl_->Delete(std::move(item)); +void Transaction::Delete(Item&& item) { + if (impl_) { + impl_->Delete(std::move(item)); + } } -void Transaction::Modify(Item &&item, ItemModifyMode mode) { - if (impl_) impl_->Modify(std::move(item), mode); +void Transaction::Modify(Item&& item, ItemModifyMode mode) { + if (impl_) { + impl_->Modify(std::move(item), mode); + } } -void Transaction::Modify(Query &&query) { - if (impl_) impl_->Modify(std::move(query)); +void Transaction::Modify(Query&& query) { + if (impl_) { + impl_->Modify(std::move(query)); + } } void Transaction::PutMeta(std::string_view key, std::string_view value) { - if (impl_) impl_->PutMeta(key, value); + if (impl_) { + impl_->PutMeta(key, value); + } } -void Transaction::MergeTagsMatcher(TagsMatcher &&tm) { - if (impl_) impl_->MergeTagsMatcher(std::move(tm)); +void Transaction::MergeTagsMatcher(TagsMatcher&& tm) { + if (impl_) { + impl_->MergeTagsMatcher(std::move(tm)); + } } Item Transaction::NewItem() { return impl_->NewItem(); } -std::vector &Transaction::GetSteps() { +std::vector& Transaction::GetSteps() { assertrx(impl_); return impl_->steps_; } -const std::vector &Transaction::GetSteps() const { +const std::vector& Transaction::GetSteps() const { assertrx(impl_); return impl_->steps_; } -Item Transaction::GetItem(TransactionStep &&st) { +Item Transaction::GetItem(TransactionStep&& st) { assertrx(impl_); return impl_->GetItem(std::move(st)); } @@ -75,8 +92,10 @@ Transaction::time_point Transaction::GetStartTime() const { return impl_->startTime_; } -void Transaction::ValidatePK(const FieldsSet &pkFields) { - if (impl_) impl_->ValidatePK(pkFields); +void Transaction::ValidatePK(const FieldsSet& pkFields) { + if (impl_) { + impl_->ValidatePK(pkFields); + } } } // namespace reindexer diff --git a/cpp_src/core/transaction.h b/cpp_src/core/transaction.h index 8e64e9886..8d0f92a48 100644 --- a/cpp_src/core/transaction.h +++ b/cpp_src/core/transaction.h @@ -15,36 +15,36 @@ class Transaction { public: using time_point = system_clock_w::time_point; - Transaction(const std::string &nsName, const PayloadType &pt, const TagsMatcher &tm, const FieldsSet &pf, + Transaction(const std::string& nsName, const PayloadType& pt, const TagsMatcher& tm, const FieldsSet& pf, std::shared_ptr schema); - Transaction(const Error &err); + Transaction(const Error& err); ~Transaction(); Transaction() = default; - Transaction(Transaction &&) noexcept; - Transaction &operator=(Transaction &&) noexcept; - - void Insert(Item &&item); - void Update(Item &&item); - void Upsert(Item &&item); - void Delete(Item &&item); - void Modify(Item &&item, ItemModifyMode mode); - void Modify(Query &&query); + Transaction(Transaction&&) noexcept; + Transaction& operator=(Transaction&&) noexcept; + + void Insert(Item&& item); + void Update(Item&& item); + void Upsert(Item&& item); + void Delete(Item&& item); + void Modify(Item&& item, ItemModifyMode mode); + void Modify(Query&& query); void PutMeta(std::string_view key, std::string_view value); - void MergeTagsMatcher(TagsMatcher &&tm); + void MergeTagsMatcher(TagsMatcher&& tm); bool IsFree() { return impl_ == nullptr; } Item NewItem(); - Item GetItem(TransactionStep &&st); + Item GetItem(TransactionStep&& st); Error Status() { return status_; } - const std::string &GetName() const; + const std::string& GetName() const; friend class ReindexerImpl; - std::vector &GetSteps(); - const std::vector &GetSteps() const; + std::vector& GetSteps(); + const std::vector& GetSteps() const; bool IsTagsUpdated() const; time_point GetStartTime() const; - void ValidatePK(const FieldsSet &pkFields); + void ValidatePK(const FieldsSet& pkFields); protected: std::unique_ptr impl_; diff --git a/cpp_src/core/transactionimpl.cc b/cpp_src/core/transactionimpl.cc index a9f33967a..f3380c9c2 100644 --- a/cpp_src/core/transactionimpl.cc +++ b/cpp_src/core/transactionimpl.cc @@ -4,9 +4,9 @@ namespace reindexer { -void TransactionImpl::checkTagsMatcher(Item &item) { +void TransactionImpl::checkTagsMatcher(Item& item) { if (item.IsTagsUpdated()) { - ItemImpl *ritem = item.impl_; + ItemImpl* ritem = item.impl_; if (ritem->Type().get() != payloadType_.get() || !tagsMatcher_.try_merge(ritem->tagsMatcher())) { std::string jsonSliceBuf(ritem->GetJSON()); @@ -15,10 +15,13 @@ void TransactionImpl::checkTagsMatcher(Item &item) { *ritem = std::move(tmpItem); auto err = ritem->FromJSON(jsonSliceBuf, nullptr); - if (!err.ok()) throw err; + if (!err.ok()) { + throw err; + } - if (ritem->tagsMatcher().isUpdated() && !tagsMatcher_.try_merge(ritem->tagsMatcher())) + if (ritem->tagsMatcher().isUpdated() && !tagsMatcher_.try_merge(ritem->tagsMatcher())) { throw Error(errLogic, "Could not insert item. TagsMatcher was not merged."); + } ritem->tagsMatcher() = tagsMatcher_; ritem->tagsMatcher().setUpdated(); } else { @@ -36,15 +39,15 @@ Item TransactionImpl::NewItem() { return item; } -Item TransactionImpl::GetItem(TransactionStep &&st) { +Item TransactionImpl::GetItem(TransactionStep&& st) { std::unique_lock lock(mtx_); - auto &data = std::get(st.data_); + auto& data = std::get(st.data_); auto item = Item(new ItemImpl(payloadType_, tagsMatcher_, pkFields_, schema_, std::move(data.data))); data.hadTmUpdate ? item.impl_->tagsMatcher().setUpdated() : item.impl_->tagsMatcher().clearUpdated(); return item; } -void TransactionImpl::ValidatePK(const FieldsSet &pkFields) { +void TransactionImpl::ValidatePK(const FieldsSet& pkFields) { std::lock_guard lck(mtx_); if (hasDeleteItemSteps_ && rx_unlikely(pkFields != pkFields_)) { throw Error( @@ -53,34 +56,34 @@ void TransactionImpl::ValidatePK(const FieldsSet &pkFields) { } } -void TransactionImpl::Insert(Item &&item) { +void TransactionImpl::Insert(Item&& item) { std::unique_lock lock(mtx_); checkTagsMatcher(item); steps_.emplace_back(std::move(item), ModeInsert); } -void TransactionImpl::Update(Item &&item) { +void TransactionImpl::Update(Item&& item) { std::unique_lock lock(mtx_); checkTagsMatcher(item); steps_.emplace_back(std::move(item), ModeUpdate); } -void TransactionImpl::Upsert(Item &&item) { +void TransactionImpl::Upsert(Item&& item) { std::unique_lock lock(mtx_); checkTagsMatcher(item); steps_.emplace_back(std::move(item), ModeUpsert); } -void TransactionImpl::Delete(Item &&item) { +void TransactionImpl::Delete(Item&& item) { std::unique_lock lock(mtx_); checkTagsMatcher(item); steps_.emplace_back(std::move(item), ModeDelete); } -void TransactionImpl::Modify(Item &&item, ItemModifyMode mode) { +void TransactionImpl::Modify(Item&& item, ItemModifyMode mode) { std::unique_lock lock(mtx_); checkTagsMatcher(item); hasDeleteItemSteps_ = hasDeleteItemSteps_ || (mode == ModeDelete); steps_.emplace_back(std::move(item), mode); } -void TransactionImpl::Modify(Query &&query) { +void TransactionImpl::Modify(Query&& query) { std::unique_lock lock(mtx_); steps_.emplace_back(std::move(query)); } @@ -94,7 +97,7 @@ void TransactionImpl::PutMeta(std::string_view key, std::string_view value) { steps_.emplace_back(key, value); } -void TransactionImpl::MergeTagsMatcher(TagsMatcher &&tm) { +void TransactionImpl::MergeTagsMatcher(TagsMatcher&& tm) { std::lock_guard lock(mtx_); if (!tagsMatcher_.try_merge(tm)) { throw Error(errParams, "Unable to merge incompatible TagsMatchers in transaction:\nCurrent:\n%s;\nNew:\n%s", tagsMatcher_.dump(), diff --git a/cpp_src/core/transactionimpl.h b/cpp_src/core/transactionimpl.h index 245ca6a6c..1ab14690b 100644 --- a/cpp_src/core/transactionimpl.h +++ b/cpp_src/core/transactionimpl.h @@ -28,20 +28,20 @@ class TransactionStep { public: enum class Type : uint8_t { Nop, ModifyItem, Query, PutMeta, DeleteMeta, SetTM }; - TransactionStep(Item &&item, ItemModifyMode modifyMode) + TransactionStep(Item&& item, ItemModifyMode modifyMode) : data_(TransactionItemStep{modifyMode, item.IsTagsUpdated(), std::move(*item.impl_)}), type_(Type::ModifyItem) { delete item.impl_; item.impl_ = nullptr; } - TransactionStep(TagsMatcher &&tm) : data_(TransactionTmStep{std::move(tm)}), type_(Type::SetTM) {} - TransactionStep(Query &&query) : data_(TransactionQueryStep{std::make_unique(std::move(query))}), type_(Type::Query) {} + TransactionStep(TagsMatcher&& tm) : data_(TransactionTmStep{std::move(tm)}), type_(Type::SetTM) {} + TransactionStep(Query&& query) : data_(TransactionQueryStep{std::make_unique(std::move(query))}), type_(Type::Query) {} TransactionStep(std::string_view key, std::string_view value) : data_(TransactionMetaStep{std::string(key), std::string(value)}), type_(Type::PutMeta) {} - TransactionStep(const TransactionStep &) = delete; - TransactionStep &operator=(const TransactionStep &) = delete; - TransactionStep(TransactionStep && /*rhs*/) = default; - TransactionStep &operator=(TransactionStep && /*rhs*/) = delete; + TransactionStep(const TransactionStep&) = delete; + TransactionStep& operator=(const TransactionStep&) = delete; + TransactionStep(TransactionStep&& /*rhs*/) = default; + TransactionStep& operator=(TransactionStep&& /*rhs*/) = delete; std::variant data_; Type type_; @@ -49,7 +49,7 @@ class TransactionStep { class TransactionImpl { public: - TransactionImpl(const std::string &nsName, const PayloadType &pt, const TagsMatcher &tm, const FieldsSet &pf, + TransactionImpl(const std::string& nsName, const PayloadType& pt, const TagsMatcher& tm, const FieldsSet& pf, std::shared_ptr schema) : payloadType_(pt), tagsMatcher_(tm), @@ -62,22 +62,22 @@ class TransactionImpl { tagsMatcher_.clearUpdated(); } - void Insert(Item &&item); - void Update(Item &&item); - void Upsert(Item &&item); - void Delete(Item &&item); - void Modify(Item &&item, ItemModifyMode mode); - void Modify(Query &&item); + void Insert(Item&& item); + void Update(Item&& item); + void Upsert(Item&& item); + void Delete(Item&& item); + void Modify(Item&& item, ItemModifyMode mode); + void Modify(Query&& item); void PutMeta(std::string_view key, std::string_view value); void MergeTagsMatcher(TagsMatcher&& tm); Item NewItem(); - Item GetItem(TransactionStep &&st); - void ValidatePK(const FieldsSet &pkFields); + Item GetItem(TransactionStep&& st); + void ValidatePK(const FieldsSet& pkFields); - const std::string &GetName() { return nsName_; } + const std::string& GetName() { return nsName_; } - void checkTagsMatcher(Item &item); + void checkTagsMatcher(Item& item); PayloadType payloadType_; TagsMatcher tagsMatcher_; diff --git a/cpp_src/coroutine/channel.h b/cpp_src/coroutine/channel.h index d436afe72..bc426b873 100644 --- a/cpp_src/coroutine/channel.h +++ b/cpp_src/coroutine/channel.h @@ -19,10 +19,10 @@ class channel { throw std::logic_error("Empty channels are not allowed"); } } - channel(const channel &) = delete; - channel(channel &&) = delete; - channel &operator=(const channel &) = delete; - channel &operator=(channel &&) = delete; + channel(const channel&) = delete; + channel(channel&&) = delete; + channel& operator=(const channel&) = delete; + channel& operator=(channel&&) = delete; /// Push object to channel. /// If channel is full, current coroutine will suspend and wait for pop()-calls from other coroutines. @@ -30,7 +30,7 @@ class channel { /// If there are readers awaiting data, current coroutine will call resume() and switch to those readers. /// @param obj - Object to push template - void push(U &&obj) { + void push(U&& obj) { assertrx(current()); // For now channels should not be used from main routine dew to current resume/suspend logic bool await = false; while (full() || closed_) { @@ -127,13 +127,13 @@ class channel { return std::make_pair(T(), false); } template - void push_impl(U &&obj) { + void push_impl(U&& obj) { buf_[w_ptr_] = std::forward(obj); w_ptr_ = (w_ptr_ + 1) % buf_.size(); ++data_size_; assertrx(data_size_ <= buf_.size()); } - static void remove_waiter(waiters_container &waiters) { waiters.erase(std::find(waiters.begin(), waiters.end(), current())); } + static void remove_waiter(waiters_container& waiters) { waiters.erase(std::find(waiters.begin(), waiters.end(), current())); } h_vector buf_; size_t r_ptr_ = 0; diff --git a/cpp_src/coroutine/coroutine.cc b/cpp_src/coroutine/coroutine.cc index fc2aa749f..b5c62aa14 100644 --- a/cpp_src/coroutine/coroutine.cc +++ b/cpp_src/coroutine/coroutine.cc @@ -1,8 +1,8 @@ #include "coroutine.h" +#include #include #include #include -#include #include "tools/clock.h" namespace reindexer { @@ -10,7 +10,7 @@ namespace coroutine { static void static_entry() { ordinator::instance().entry(); } -ordinator &ordinator::instance() noexcept { +ordinator& ordinator::instance() noexcept { static thread_local ordinator ord; return ord; } @@ -18,12 +18,12 @@ ordinator &ordinator::instance() noexcept { void ordinator::entry() { const routine_t index = current_ - 1; { - routine ¤t_routine = routines_[index]; + routine& current_routine = routines_[index]; if (current_routine.func) { try { auto func = std::move(current_routine.func); func(); - } catch (std::exception &e) { + } catch (std::exception& e) { fprintf(stderr, "Unhandled exception in coroutine \"%u\": %s\n", index + 1, e.what()); } catch (...) { fprintf(stderr, "Unhandled exception in coroutine \"%u\": some custom exception\n", index + 1); @@ -31,7 +31,7 @@ void ordinator::entry() { } } - routine ¤t_routine = routines_[index]; + routine& current_routine = routines_[index]; remove_from_call_stack(index + 1); current_ = pop_from_call_stack(); current_routine.finalize(); @@ -66,13 +66,19 @@ int ordinator::resume(routine_t id) { assertrx(id <= routines_.size()); assertrx(id); // For now the main routine should not be resumed explicitly - if (id > routines_.size()) return -1; + if (id > routines_.size()) { + return -1; + } { - routine &routine = routines_[id - 1]; - if (routine.is_finalized()) return -2; + routine& routine = routines_[id - 1]; + if (routine.is_finalized()) { + return -2; + } - if (routine.is_empty()) routine.create_fiber(); + if (routine.is_empty()) { + routine.create_fiber(); + } push_to_call_stack(current_); current_ = id; @@ -80,7 +86,7 @@ int ordinator::resume(routine_t id) { routine.resume(); } - routine &routine = routines_[id - 1]; + routine& routine = routines_[id - 1]; if (routine.is_dead()) { clear_finalized(); } @@ -119,7 +125,9 @@ void ordinator::remove_from_call_stack(routine_t id) noexcept { } bool ordinator::set_loop_completion_callback(ordinator::cmpl_cb_t cb) noexcept { - if (loop_completion_callback_) return false; + if (loop_completion_callback_) { + return false; + } loop_completion_callback_ = std::move(cb); return true; } @@ -130,7 +138,7 @@ int64_t ordinator::add_completion_callback(ordinator::cmpl_cb_t cb) { for (;;) { id = steady_clock_w::now().time_since_epoch().count(); auto found = - std::find_if(completion_callbacks_.begin(), completion_callbacks_.end(), [id](cmpl_cb_data &data) { return data.id == id; }); + std::find_if(completion_callbacks_.begin(), completion_callbacks_.end(), [id](cmpl_cb_data& data) { return data.id == id; }); if (found == completion_callbacks_.end()) { break; } else if (++cnt == 3) { @@ -155,7 +163,7 @@ bool ordinator::remove_loop_completion_callback() noexcept { int ordinator::remove_completion_callback(int64_t id) noexcept { auto old_sz = completion_callbacks_.size(); completion_callbacks_.erase( - std::remove_if(completion_callbacks_.begin(), completion_callbacks_.end(), [id](cmpl_cb_data &data) { return data.id == id; }), + std::remove_if(completion_callbacks_.begin(), completion_callbacks_.end(), [id](cmpl_cb_data& data) { return data.id == id; }), completion_callbacks_.end()); auto diff = old_sz - completion_callbacks_.size(); if (diff == 0) { @@ -201,7 +209,7 @@ ordinator::routine::~routine() { } } -ordinator::routine::routine(ordinator::routine &&o) noexcept +ordinator::routine::routine(ordinator::routine&& o) noexcept : func(std::move(o.func)), fiber_(o.fiber_), stack_size_(o.stack_size_), is_empty_(o.is_empty_), finalized_(o.finalized_) { o.fiber_ = nullptr; } @@ -220,7 +228,7 @@ void ordinator::clear_finalized() { assertrx(!finalized_indexes_.empty()); auto index = finalized_indexes_.back(); - auto &routine = routines_[index]; + auto& routine = routines_[index]; assertrx(routine.is_finalized()); routine.clear(); if (loop_completion_callback_) { @@ -228,7 +236,7 @@ void ordinator::clear_finalized() { } auto callbacks = completion_callbacks_; - for (auto &callback : callbacks) { + for (auto& callback : callbacks) { callback.cb(index + 1); } } diff --git a/cpp_src/coroutine/coroutine.h b/cpp_src/coroutine/coroutine.h index 886732019..8bf9db2ba 100644 --- a/cpp_src/coroutine/coroutine.h +++ b/cpp_src/coroutine/coroutine.h @@ -19,12 +19,12 @@ class ordinator { public: using cmpl_cb_t = std::function; - ordinator(const ordinator &) = delete; - ordinator(ordinator &&) = delete; - ordinator &operator=(const ordinator &) = delete; - ordinator &operator=(ordinator &&) = delete; + ordinator(const ordinator&) = delete; + ordinator(ordinator&&) = delete; + ordinator& operator=(const ordinator&) = delete; + ordinator& operator=(ordinator&&) = delete; - static ordinator &instance() noexcept; + static ordinator& instance() noexcept; /// Create new coroutine in current thread /// @param function - Function, that will be executed in this coroutine @@ -69,15 +69,15 @@ class ordinator { public: routine() noexcept = default; ~routine(); - routine(const routine &) = delete; - routine(routine &&other) noexcept; - routine(std::function _func, koishi_coroutine_t *fiber, size_t stack_size) noexcept + routine(const routine&) = delete; + routine(routine&& other) noexcept; + routine(std::function _func, koishi_coroutine_t* fiber, size_t stack_size) noexcept : func(std::move(_func)), fiber_(fiber), stack_size_(stack_size), is_empty_(true) { assertrx(stack_size_); assertrx(fiber_); } - routine &operator=(const routine &) = delete; - routine &operator=(routine &&) = delete; + routine& operator=(const routine&) = delete; + routine& operator=(routine&&) = delete; /// Check if coroutine is already finished it's execution and ready to be cleared /// @returns true - if coroutine is finalized, false - if couroutine is still in progress @@ -104,7 +104,7 @@ class ordinator { std::function func; private: - koishi_coroutine_t *fiber_ = nullptr; + koishi_coroutine_t* fiber_ = nullptr; size_t stack_size_ = k_default_stack_limit; bool is_empty_ = true; bool finalized_ = false; diff --git a/cpp_src/debug/allocdebug.cc b/cpp_src/debug/allocdebug.cc index f5163e96a..37557eb7f 100644 --- a/cpp_src/debug/allocdebug.cc +++ b/cpp_src/debug/allocdebug.cc @@ -32,20 +32,28 @@ static bool ismt; #include "tools/alloc_ext/tc_malloc_extension.h" -static void traced_new_mt(const void *ptr, size_t size) { - if (ptr && size) tracer_mt.traced_new(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); +static void traced_new_mt(const void* ptr, size_t size) { + if (ptr && size) { + tracer_mt.traced_new(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); + } } -static void traced_delete_mt(const void *ptr) { - if (ptr) tracer_mt.traced_delete(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); +static void traced_delete_mt(const void* ptr) { + if (ptr) { + tracer_mt.traced_delete(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); + } } -static void traced_new(const void *ptr, size_t size) { - if (ptr && size) tracer.traced_new(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); +static void traced_new(const void* ptr, size_t size) { + if (ptr && size) { + tracer.traced_new(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); + } } -static void traced_delete(const void *ptr) { - if (ptr) tracer.traced_delete(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); +static void traced_delete(const void* ptr) { + if (ptr) { + tracer.traced_delete(reindexer::alloc_ext::instance()->GetAllocatedSize(const_cast(ptr))); + } } void allocdebug_init() { diff --git a/cpp_src/debug/backtrace.cc b/cpp_src/debug/backtrace.cc index 92f1454d0..437100370 100644 --- a/cpp_src/debug/backtrace.cc +++ b/cpp_src/debug/backtrace.cc @@ -9,16 +9,16 @@ namespace reindexer { namespace debug { static std::recursive_mutex g_mutex; -static crash_query_reporter_t g_crash_query_reporter = [](std::ostream &sout) { sout << "" << std::endl; }; +static crash_query_reporter_t g_crash_query_reporter = [](std::ostream& sout) { sout << "" << std::endl; }; static backtrace_writer_t g_writer = [](std::string_view sv) { std::cerr << sv; }; static std::string g_assertion_message(""); -void backtrace_set_assertion_message(std::string &&msg) noexcept { +void backtrace_set_assertion_message(std::string&& msg) noexcept { std::lock_guard lck(g_mutex); g_assertion_message = std::move(msg); } -static void print_assertion_message(std::ostream &sout) { +static void print_assertion_message(std::ostream& sout) { std::string msg(""); { std::lock_guard lck(g_mutex); @@ -67,7 +67,7 @@ extern "C" void abort() { } } -extern "C" void __assert_fail(const char *expr, const char *file, int line, const char *func) { +extern "C" void __assert_fail(const char* expr, const char* file, int line, const char* func) { fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line); fflush(NULL); abort(); @@ -80,7 +80,7 @@ namespace debug { #if REINDEX_WITH_UNWIND class Unwinder { public: - size_t operator()(span trace) { + size_t operator()(span trace) { trace_ = trace; index_ = -1; _Unwind_Backtrace(&this->backtrace_trampoline, this); @@ -88,12 +88,14 @@ class Unwinder { } private: - static _Unwind_Reason_Code backtrace_trampoline(_Unwind_Context *ctx, void *self) { - return (static_cast(self))->backtrace(ctx); + static _Unwind_Reason_Code backtrace_trampoline(_Unwind_Context* ctx, void* self) { + return (static_cast(self))->backtrace(ctx); } - _Unwind_Reason_Code backtrace(_Unwind_Context *ctx) { - if (index_ >= 0 && size_t(index_) >= trace_.size()) return _URC_END_OF_STACK; + _Unwind_Reason_Code backtrace(_Unwind_Context* ctx) { + if (index_ >= 0 && size_t(index_) >= trace_.size()) { + return _URC_END_OF_STACK; + } int ip_before_instruction = 0; uintptr_t ip = _Unwind_GetIPInfo(ctx, &ip_before_instruction); @@ -107,17 +109,17 @@ class Unwinder { } if (index_ >= 0) { - trace_[index_] = reinterpret_cast(ip); + trace_[index_] = reinterpret_cast(ip); } index_++; return _URC_NO_REASON; } ssize_t index_; - span trace_; + span trace_; }; #endif -int backtrace_internal(void **addrlist, size_t size, void *ctx, std::string_view &method) { +int backtrace_internal(void** addrlist, size_t size, void* ctx, std::string_view& method) { using namespace std::string_view_literals; (void)ctx; size_t addrlen = 0; @@ -135,20 +137,20 @@ int backtrace_internal(void **addrlist, size_t size, void *ctx, std::string_view ctx = &uc; } - unw_init_local(&cursor, reinterpret_cast(ctx)); + unw_init_local(&cursor, reinterpret_cast(ctx)); addrlen = 1; do { unw_word_t ip; unw_get_reg(&cursor, UNW_REG_IP, &ip); - addrlist[addrlen++] = reinterpret_cast(ip); + addrlist[addrlen++] = reinterpret_cast(ip); } while (unw_step(&cursor) && addrlen < size); #endif #if REINDEX_WITH_UNWIND Unwinder unw; if (addrlen < 3) { // -V547 method = "unwind"sv; - addrlen = unw(span(addrlist, size)); + addrlen = unw(span(addrlist, size)); } #endif #if REINDEX_WITH_EXECINFO @@ -160,16 +162,18 @@ int backtrace_internal(void **addrlist, size_t size, void *ctx, std::string_view return addrlen; } -void print_backtrace(std::ostream &sout, void *ctx, int sig) { +void print_backtrace(std::ostream& sout, void* ctx, int sig) { #if !REINDEX_WITH_EXECINFO && !REINDEX_WITH_UNWIND && !REINDEX_WITH_LIBUNWIND sout << "Sorry, reindexer has been compiled without any backtrace methods." << std::endl; #else - void *addrlist[64] = {}; + void* addrlist[64] = {}; auto resolver = TraceResolver::New(); std::string_view method; int addrlen = backtrace_internal(addrlist, sizeof(addrlist) / sizeof(addrlist[0]), ctx, method); - if (sig >= 0) sout << "Signal " << sig << " "; + if (sig >= 0) { + sout << "Signal " << sig << " "; + } sout << "backtrace (" << method << "):" << std::endl; for (int i = 1; i < addrlen; i++) { auto te = TraceEntry(uintptr_t(addrlist[i])); @@ -179,7 +183,7 @@ void print_backtrace(std::ostream &sout, void *ctx, int sig) { #endif } -void print_crash_query(std::ostream &sout) { +void print_crash_query(std::ostream& sout) { auto crash_query_reporter = backtrace_get_crash_query_reporter(); if (crash_query_reporter) { crash_query_reporter(sout); @@ -188,7 +192,7 @@ void print_crash_query(std::ostream &sout) { } } -static void sighandler(int sig, siginfo_t *, void *ctx) { +static void sighandler(int sig, siginfo_t*, void* ctx) { const auto writer = backtrace_get_writer(); std::ostringstream sout; sout << "*** Backtrace on signal: " << sig << " ***" << std::endl; @@ -219,7 +223,7 @@ void backtrace_init() noexcept { sigaction(SIGBUS, &sa, nullptr); } -void set_minidump_path(const std::string &) { assert(false); } +void set_minidump_path(const std::string&) { assert(false); } void backtrace_set_writer(backtrace_writer_t writer) { std::lock_guard lck(g_mutex); @@ -256,7 +260,7 @@ namespace reindexer { namespace debug { static std::string g_pathMiniDump; -void outputDebugInfo(const backtrace_writer_t &writer, EXCEPTION_POINTERS *ExceptionInfo) { +void outputDebugInfo(const backtrace_writer_t& writer, EXCEPTION_POINTERS* ExceptionInfo) { std::ostringstream sout; print_crash_query(sout); writer(sout.str()); @@ -313,13 +317,13 @@ void backtrace_init() noexcept { SetUnhandledExceptionFilter(exceptionHandler); } -void set_minidump_path(const std::string &p) { g_pathMiniDump = p; } +void set_minidump_path(const std::string& p) { g_pathMiniDump = p; } void backtrace_set_writer(backtrace_writer_t writer) { std::lock_guard lck(g_mutex); g_writer = std::move(writer); } -int backtrace_internal(void **, size_t, void *, std::string_view &) { return 0; } +int backtrace_internal(void**, size_t, void*, std::string_view&) { return 0; } void backtrace_set_crash_query_reporter(crash_query_reporter_t reporter) { std::lock_guard lck(g_mutex); g_crash_query_reporter = std::move(reporter); @@ -332,10 +336,12 @@ crash_query_reporter_t backtrace_get_crash_query_reporter() { std::lock_guard lck(g_mutex); return g_crash_query_reporter; } -void print_backtrace(std::ostream &sout, void *, int) { cpptrace::generate_trace().print(sout, false); } -void print_crash_query(std::ostream &sout) { +void print_backtrace(std::ostream& sout, void*, int) { cpptrace::generate_trace().print(sout, false); } +void print_crash_query(std::ostream& sout) { auto reporter = backtrace_get_crash_query_reporter(); - if (reporter) reporter(sout); + if (reporter) { + reporter(sout); + } } } // namespace debug @@ -344,13 +350,13 @@ void print_crash_query(std::ostream &sout) { namespace reindexer { namespace debug { static std::recursive_mutex g_mutex; -static crash_query_reporter_t g_crash_query_reporter = [](std::ostream &) {}; +static crash_query_reporter_t g_crash_query_reporter = [](std::ostream&) {}; static backtrace_writer_t g_writer = [](std::string_view sv) { std::cerr << sv; }; void backtrace_init() noexcept {} -void set_minidump_path(const std::string &) { assert(false); } +void set_minidump_path(const std::string&) { assert(false); } void backtrace_set_writer(backtrace_writer_t) {} -int backtrace_internal(void **, size_t, void *, std::string_view &) { return 0; } +int backtrace_internal(void**, size_t, void*, std::string_view&) { return 0; } void backtrace_set_crash_query_reporter(crash_query_reporter_t reporter) { std::lock_guard lck(g_mutex); g_crash_query_reporter = std::move(reporter); @@ -363,10 +369,12 @@ crash_query_reporter_t backtrace_get_crash_query_reporter() { std::lock_guard lck(g_mutex); return g_crash_query_reporter; } -void print_backtrace(std::ostream &, void *, int) {} -void print_crash_query(std::ostream &sout) { +void print_backtrace(std::ostream&, void*, int) {} +void print_crash_query(std::ostream& sout) { auto reporter = backtrace_get_crash_query_reporter(); - if (reporter) reporter(sout); + if (reporter) { + reporter(sout); + } } } // namespace debug diff --git a/cpp_src/debug/backtrace.h b/cpp_src/debug/backtrace.h index 2339f3503..6d367a6ab 100644 --- a/cpp_src/debug/backtrace.h +++ b/cpp_src/debug/backtrace.h @@ -7,17 +7,17 @@ namespace reindexer { namespace debug { using backtrace_writer_t = std::function; -using crash_query_reporter_t = std::function; +using crash_query_reporter_t = std::function; void backtrace_init() noexcept; -void set_minidump_path(const std::string &p); +void set_minidump_path(const std::string& p); void backtrace_set_writer(backtrace_writer_t); void backtrace_set_crash_query_reporter(crash_query_reporter_t); backtrace_writer_t backtrace_get_writer(); crash_query_reporter_t backtrace_get_crash_query_reporter(); -int backtrace_internal(void **addrlist, size_t size, void *ctx, std::string_view &method); -void backtrace_set_assertion_message(std::string &&msg) noexcept; -void print_backtrace(std::ostream &sout, void *ctx, int sig); -void print_crash_query(std::ostream &sout); +int backtrace_internal(void** addrlist, size_t size, void* ctx, std::string_view& method); +void backtrace_set_assertion_message(std::string&& msg) noexcept; +void print_backtrace(std::ostream& sout, void* ctx, int sig); +void print_crash_query(std::ostream& sout); } // namespace debug } // namespace reindexer diff --git a/cpp_src/debug/crashqueryreporter.cc b/cpp_src/debug/crashqueryreporter.cc index b9d913d71..2ff28287f 100644 --- a/cpp_src/debug/crashqueryreporter.cc +++ b/cpp_src/debug/crashqueryreporter.cc @@ -8,7 +8,7 @@ namespace reindexer { struct QueryDebugContext { bool HasTrackedQuery() const noexcept { return mainQuery || externQuery || parentQuery || !externSql.empty(); } - std::string_view GetMainSQL(std::string &storage) const noexcept { + std::string_view GetMainSQL(std::string& storage) const noexcept { try { storage.clear(); if (mainQuery) { @@ -23,7 +23,7 @@ struct QueryDebugContext { return ""; } } - std::string_view GetParentSQL(std::string &storage) const noexcept { + std::string_view GetParentSQL(std::string& storage) const noexcept { try { storage.clear(); if (parentQuery) { @@ -39,22 +39,22 @@ struct QueryDebugContext { externSql = std::string_view(); } - const Query *mainQuery = nullptr; - const Query *externQuery = nullptr; + const Query* mainQuery = nullptr; + const Query* externQuery = nullptr; std::string_view externSql; - const Query *parentQuery = nullptr; - std::atomic *nsOptimizationState = nullptr; - ExplainCalc *explainCalc = nullptr; - std::atomic_bool *nsLockerState = nullptr; - StringsHolder *nsStrHolder = nullptr; + const Query* parentQuery = nullptr; + std::atomic* nsOptimizationState = nullptr; + ExplainCalc* explainCalc = nullptr; + std::atomic_bool* nsLockerState = nullptr; + StringsHolder* nsStrHolder = nullptr; QueryType realQueryType = QuerySelect; QueryType externRealQueryType = QuerySelect; }; thread_local QueryDebugContext g_queryDebugCtx; -ActiveQueryScope::ActiveQueryScope(SelectCtx &ctx, std::atomic &nsOptimizationState, ExplainCalc &explainCalc, - std::atomic_bool &nsLockerState, StringsHolder *strHolder) noexcept +ActiveQueryScope::ActiveQueryScope(SelectCtx& ctx, std::atomic& nsOptimizationState, ExplainCalc& explainCalc, + std::atomic_bool& nsLockerState, StringsHolder* strHolder) noexcept : type_(ctx.requiresCrashTracking ? Type::CoreQueryTracker : Type::NoTracking) { if (ctx.requiresCrashTracking) { g_queryDebugCtx.mainQuery = &ctx.query; @@ -67,8 +67,8 @@ ActiveQueryScope::ActiveQueryScope(SelectCtx &ctx, std::atomic &nsOptimizat } } -ActiveQueryScope::ActiveQueryScope(const Query &q, QueryType realQueryType, std::atomic &nsOptimizationState, - StringsHolder *strHolder) noexcept +ActiveQueryScope::ActiveQueryScope(const Query& q, QueryType realQueryType, std::atomic& nsOptimizationState, + StringsHolder* strHolder) noexcept : type_(Type::CoreQueryTracker) { g_queryDebugCtx.mainQuery = &q; g_queryDebugCtx.parentQuery = nullptr; @@ -79,7 +79,7 @@ ActiveQueryScope::ActiveQueryScope(const Query &q, QueryType realQueryType, std: g_queryDebugCtx.realQueryType = realQueryType; } -ActiveQueryScope::ActiveQueryScope(const Query &q, QueryType realQueryType) noexcept : type_(Type::ExternalQueryTracker) { +ActiveQueryScope::ActiveQueryScope(const Query& q, QueryType realQueryType) noexcept : type_(Type::ExternalQueryTracker) { g_queryDebugCtx.externQuery = &q; g_queryDebugCtx.externRealQueryType = realQueryType; } @@ -134,7 +134,7 @@ static std::string_view nsOptimizationStateName(int state) { } } -void PrintCrashedQuery(std::ostream &out) { +void PrintCrashedQuery(std::ostream& out) { if (!g_queryDebugCtx.HasTrackedQuery()) { out << "*** No additional info from crash query tracker ***" << std::endl; return; @@ -163,11 +163,13 @@ void PrintCrashedQuery(std::ostream &out) { out << " memstat = " << g_queryDebugCtx.nsStrHolder->MemStat() << std::endl; out << " holds indexes = " << std::boolalpha << g_queryDebugCtx.nsStrHolder->HoldsIndexes() << std::endl; if (g_queryDebugCtx.nsStrHolder->HoldsIndexes()) { - const auto &indexes = g_queryDebugCtx.nsStrHolder->Indexes(); + const auto& indexes = g_queryDebugCtx.nsStrHolder->Indexes(); out << " indexes.size = " << indexes.size() << std::endl; out << " indexes = ["; for (size_t i = 0; i < indexes.size(); ++i) { - if (i) out << " "; + if (i) { + out << " "; + } out << indexes[i]->Name(); } out << "]" << std::endl; diff --git a/cpp_src/debug/crashqueryreporter.h b/cpp_src/debug/crashqueryreporter.h index cf4472292..5d6746766 100644 --- a/cpp_src/debug/crashqueryreporter.h +++ b/cpp_src/debug/crashqueryreporter.h @@ -14,11 +14,11 @@ class Query; class ActiveQueryScope { public: // Core query scope - ActiveQueryScope(SelectCtx &ctx, std::atomic &nsOptimizationState, ExplainCalc &explainCalc, std::atomic_bool &nsLockerState, - StringsHolder *strHolder) noexcept; - ActiveQueryScope(const Query &q, QueryType realQueryType, std::atomic &nsOptimizationState, StringsHolder *strHolder) noexcept; + ActiveQueryScope(SelectCtx& ctx, std::atomic& nsOptimizationState, ExplainCalc& explainCalc, std::atomic_bool& nsLockerState, + StringsHolder* strHolder) noexcept; + ActiveQueryScope(const Query& q, QueryType realQueryType, std::atomic& nsOptimizationState, StringsHolder* strHolder) noexcept; // External query scope - ActiveQueryScope(const Query &q, QueryType realQueryType) noexcept; + ActiveQueryScope(const Query& q, QueryType realQueryType) noexcept; explicit ActiveQueryScope(std::string_view sql) noexcept; ~ActiveQueryScope(); @@ -28,6 +28,6 @@ class ActiveQueryScope { Type type_ = Type::NoTracking; }; -void PrintCrashedQuery(std::ostream &sout); +void PrintCrashedQuery(std::ostream& sout); } // namespace reindexer diff --git a/cpp_src/debug/resolver.cc b/cpp_src/debug/resolver.cc index ee2de5787..f6e5bd4c6 100644 --- a/cpp_src/debug/resolver.cc +++ b/cpp_src/debug/resolver.cc @@ -36,7 +36,7 @@ namespace debug { using namespace std::string_view_literals; -TraceEntry::TraceEntry(TraceEntry &&other) noexcept +TraceEntry::TraceEntry(TraceEntry&& other) noexcept : funcName_(other.funcName_), objFile_(other.objFile_), srcFile_(other.srcFile_), @@ -48,7 +48,7 @@ TraceEntry::TraceEntry(TraceEntry &&other) noexcept other.holder_ = nullptr; } -TraceEntry &TraceEntry::operator=(TraceEntry &&other) noexcept { +TraceEntry& TraceEntry::operator=(TraceEntry&& other) noexcept { if (this != &other) { funcName_ = other.funcName_; objFile_ = other.objFile_; @@ -73,7 +73,9 @@ TraceEntry::TraceEntry(uintptr_t addr) { #if REINDEX_WITH_LIBDL Dl_info dl_info; - if (!dladdr(reinterpret_cast(addr), &dl_info)) return; + if (!dladdr(reinterpret_cast(addr), &dl_info)) { + return; + } objFile_ = dl_info.dli_fname; ofs_ = uintptr_t(addr) - uintptr_t(dl_info.dli_saddr); @@ -90,7 +92,7 @@ TraceEntry::TraceEntry(uintptr_t addr) { #endif } -std::ostream &TraceEntry::Dump(std::ostream &os) const { +std::ostream& TraceEntry::Dump(std::ostream& os) const { os << "0x" << std::hex << std::setfill('0') << std::setw(14) << addr_ << " " << funcName_ << std::dec; if (srcLine_) { // pretty print file path: @@ -101,14 +103,17 @@ std::ostream &TraceEntry::Dump(std::ostream &os) const { std::string srcFile(srcFile_); if (fs::Stat(srcFile) != fs::StatFile) { auto pos = srcFile.find("cpp_src/"); - if (pos != std::string::npos) srcFile = srcFile.substr(pos); + if (pos != std::string::npos) { + srcFile = srcFile.substr(pos); + } } else { srcFile = fs::GetRelativePath(srcFile, 2); } os << " (" << srcFile << ":" << srcLine_ << ")"; - } else + } else { os << " + " << ofs_; + } return os; } @@ -117,7 +122,7 @@ class TraceResolverLibbacktrace : public TraceResolver { public: TraceResolverLibbacktrace() { init(); } - bool Resolve(TraceEntry &te) override final { + bool Resolve(TraceEntry& te) override final { backtrace_pcinfo(state_, te.addr_, callback, errorCallback, &te); return true; } @@ -127,12 +132,12 @@ class TraceResolverLibbacktrace : public TraceResolver { state_ = backtrace_create_state("/proc/self/exe", 1, errorCallback, NULL); return true; } - static void errorCallback(void * /*data*/, const char *msg, int errnum) { + static void errorCallback(void* /*data*/, const char* msg, int errnum) { std::cerr << "libbacktarce error:" << msg << " " << errnum << std::endl; } - static int callback(void *data, uintptr_t /*pc*/, const char *filename, int lineno, const char * /*function*/) { - TraceEntry *te = reinterpret_cast(data); + static int callback(void* data, uintptr_t /*pc*/, const char* filename, int lineno, const char* /*function*/) { + TraceEntry* te = reinterpret_cast(data); if (filename) { te->srcFile_ = filename; te->srcLine_ = lineno; @@ -144,7 +149,7 @@ class TraceResolverLibbacktrace : public TraceResolver { } protected: - backtrace_state *state_ = nullptr; + backtrace_state* state_ = nullptr; }; std::unique_ptr TraceResolver::New() { return std::unique_ptr(new TraceResolverLibbacktrace()); } @@ -156,15 +161,19 @@ class TraceResolverApple : public TraceResolver { TraceResolverApple() { init(); } ~TraceResolverApple() { CSRelease(cs_); } - bool Resolve(TraceEntry &te) { + bool Resolve(TraceEntry& te) { bool ret = false; - if (!cs_.csCppData || !cs_.csCppObj) return false; + if (!cs_.csCppData || !cs_.csCppObj) { + return false; + } auto info = CSSymbolicatorGetSourceInfoWithAddressAtTime(cs_, te.addr_, CS_NOW); auto sym = (info.csCppData && info.csCppObj) ? CSSourceInfoGetSymbol(info) : CSSymbolicatorGetSymbolWithAddressAtTime(cs_, te.addr_, CS_NOW); - if (!sym.csCppData || !sym.csCppObj) return false; + if (!sym.csCppData || !sym.csCppObj) { + return false; + } auto owner = CSSymbolGetSymbolOwner(sym); if (owner.csCppData && owner.csCppObj) { @@ -181,7 +190,9 @@ class TraceResolverApple : public TraceResolver { protected: bool init() { auto hlib = dlopen("/System/Library/PrivateFrameworks/CoreSymbolication.framework/Versions/A/CoreSymbolication", RTLD_NOW); - if (!hlib) return false; + if (!hlib) { + return false; + } CSSymbolicatorCreateWithPid = reinterpret_cast(dlsym(hlib, "CSSymbolicatorCreateWithPid")); CSRelease = reinterpret_cast(dlsym(hlib, "CSRelease")); @@ -197,13 +208,15 @@ class TraceResolverApple : public TraceResolver { bool ok = CSSymbolicatorCreateWithPid && CSRelease && CSSymbolicatorGetSymbolWithAddressAtTime && CSSymbolicatorGetSourceInfoWithAddressAtTime && CSSourceInfoGetLineNumber && CSSourceInfoGetPath && CSSourceInfoGetSymbol && CSSymbolGetSymbolOwner; - if (ok) cs_ = CSSymbolicatorCreateWithPid(getpid()); + if (ok) { + cs_ = CSSymbolicatorCreateWithPid(getpid()); + } return ok; } struct CSTypeRef { - void *csCppData; - void *csCppObj; + void* csCppData; + void* csCppObj; }; static uint64_t constexpr CS_NOW = 0x80000000; @@ -217,7 +230,7 @@ class TraceResolverApple : public TraceResolver { typedef CSSymbolRef (*pCSSymbolicatorGetSymbolWithAddressAtTime)(CSSymbolicatorRef cs, vm_address_t addr, uint64_t time); typedef CSSourceInfoRef (*pCSSymbolicatorGetSourceInfoWithAddressAtTime)(CSSymbolicatorRef cs, vm_address_t addr, uint64_t time); typedef int (*pCSSourceInfoGetLineNumber)(CSSourceInfoRef info); - typedef const char *(*pCSSourceInfoGetPath)(CSSourceInfoRef info); + typedef const char* (*pCSSourceInfoGetPath)(CSSourceInfoRef info); typedef CSSymbolRef (*pCSSourceInfoGetSymbol)(CSSourceInfoRef info); typedef CSSymbolOwnerRef (*pCSSymbolGetSymbolOwner)(CSSymbolRef sym); diff --git a/cpp_src/debug/resolver.h b/cpp_src/debug/resolver.h index 9d052af1f..f6ba3a281 100644 --- a/cpp_src/debug/resolver.h +++ b/cpp_src/debug/resolver.h @@ -15,11 +15,11 @@ class TraceEntry { TraceEntry(uintptr_t addr); TraceEntry() = default; ~TraceEntry(); - TraceEntry(TraceEntry &&other) noexcept; - TraceEntry &operator=(TraceEntry &&) noexcept; - TraceEntry(const TraceEntry &) = delete; - TraceEntry &operator=(const TraceEntry &) = delete; - std::ostream &Dump(std::ostream &os) const; + TraceEntry(TraceEntry&& other) noexcept; + TraceEntry& operator=(TraceEntry&&) noexcept; + TraceEntry(const TraceEntry&) = delete; + TraceEntry& operator=(const TraceEntry&) = delete; + std::ostream& Dump(std::ostream& os) const; std::string_view FuncName() { return funcName_; } // protected: @@ -38,16 +38,16 @@ class TraceEntry { /// Base address of object uintptr_t baseAddr_ = 0; /// Holder of temporary data - char *holder_ = nullptr; + char* holder_ = nullptr; }; -static inline std::ostream &operator<<(std::ostream &os, const TraceEntry &e) { return e.Dump(os); } +static inline std::ostream& operator<<(std::ostream& os, const TraceEntry& e) { return e.Dump(os); } class TraceResolver { public: static std::unique_ptr New(); virtual ~TraceResolver() = default; - virtual bool Resolve(TraceEntry &) { return false; } + virtual bool Resolve(TraceEntry&) { return false; } }; } // namespace debug diff --git a/cpp_src/debug/terminate_handler.cpp b/cpp_src/debug/terminate_handler.cpp index 4ed5ddb71..e69791d04 100644 --- a/cpp_src/debug/terminate_handler.cpp +++ b/cpp_src/debug/terminate_handler.cpp @@ -15,18 +15,18 @@ static void terminate_handler() { std::exception_ptr exptr = std::current_exception(); if (exptr) { #ifndef _WIN32 - const char *type = abi::__cxa_current_exception_type()->name(); + const char* type = abi::__cxa_current_exception_type()->name(); int status; - const char *demangled = abi::__cxa_demangle(type, NULL, NULL, &status); + const char* demangled = abi::__cxa_demangle(type, NULL, NULL, &status); sout << "*** Terminating with uncaught exception of type " << (demangled ? demangled : type); #else sout << "*** Terminating with uncaught exception "; #endif try { std::rethrow_exception(exptr); - } catch (std::exception &ex) { + } catch (std::exception& ex) { sout << ": " << ex.what(); - } catch (Error &err) { + } catch (Error& err) { sout << ": " << err.what(); } catch (...) { sout << ": "; diff --git a/cpp_src/estl/atomic_unique_ptr.h b/cpp_src/estl/atomic_unique_ptr.h index 53b916433..ef8c26912 100644 --- a/cpp_src/estl/atomic_unique_ptr.h +++ b/cpp_src/estl/atomic_unique_ptr.h @@ -25,7 +25,9 @@ class atomic_unique_ptr { void reset(pointer p = pointer(), std::memory_order order = std::memory_order_seq_cst) { auto old = ptr.exchange(p, order); - if (old) delete old; + if (old) { + delete old; + } } operator pointer() const { return ptr; } pointer operator->() const { return ptr; } @@ -60,7 +62,9 @@ class atomic_unique_ptr // for array types void reset(pointer p = pointer(), std::memory_order order = std::memory_order_seq_cst) { auto old = ptr.exchange(p, order); - if (old) delete[] old; + if (old) { + delete[] old; + } } operator pointer() const { return ptr; } pointer operator->() const { return ptr; } diff --git a/cpp_src/estl/cbuf.h b/cpp_src/estl/cbuf.h index db34f89b7..1b235ebe5 100644 --- a/cpp_src/estl/cbuf.h +++ b/cpp_src/estl/cbuf.h @@ -17,14 +17,14 @@ class cbuf { full_ = false; buf_size_ = bufsize; } - cbuf(cbuf &&other) noexcept + cbuf(cbuf&& other) noexcept : head_(other.head_), tail_(other.tail_), full_(other.full_), buf_size_(other.buf_size_), buf_(std::move(other.buf_)) { other.head_ = 0; other.tail_ = 0; other.full_ = false; other.buf_size_ = 0; } - cbuf &operator=(cbuf &&other) noexcept { + cbuf& operator=(cbuf&& other) noexcept { if (this != &other) { buf_ = std::move(other.buf_); head_ = other.head_; @@ -38,46 +38,64 @@ class cbuf { } return *this; } - cbuf(const cbuf &) = delete; - cbuf &operator=(const cbuf &) = delete; + cbuf(const cbuf&) = delete; + cbuf& operator=(const cbuf&) = delete; - size_t write(const T *p_ins, size_t s_ins) { - if (s_ins > available()) grow(std::max(s_ins - available(), buf_size_)); + size_t write(const T* p_ins, size_t s_ins) { + if (s_ins > available()) { + grow(std::max(s_ins - available(), buf_size_)); + } - if (!s_ins) return 0; + if (!s_ins) { + return 0; + } size_t lSize = buf_size_ - head_; std::copy(p_ins, p_ins + std::min(s_ins, lSize), &buf_[head_]); - if (s_ins > lSize) std::copy(p_ins + lSize, p_ins + s_ins, &buf_[0]); + if (s_ins > lSize) { + std::copy(p_ins + lSize, p_ins + s_ins, &buf_[0]); + } head_ = (head_ + s_ins) % buf_size_; full_ = (head_ == tail_); return s_ins; } - size_t read(T *p_ins, size_t s_ins) { - if (s_ins > size()) s_ins = size(); + size_t read(T* p_ins, size_t s_ins) { + if (s_ins > size()) { + s_ins = size(); + } - if (!s_ins) return 0; + if (!s_ins) { + return 0; + } size_t lSize = buf_size_ - tail_; std::copy(&buf_[tail_], &buf_[tail_ + std::min(s_ins, lSize)], p_ins); - if (s_ins > lSize) std::copy(&buf_[0], &buf_[s_ins - lSize], p_ins + lSize); + if (s_ins > lSize) { + std::copy(&buf_[0], &buf_[s_ins - lSize], p_ins + lSize); + } tail_ = (tail_ + s_ins) % buf_size_; full_ = false; return s_ins; } - size_t peek(T *p_ins, size_t s_ins) { - if (s_ins > size()) s_ins = size(); + size_t peek(T* p_ins, size_t s_ins) { + if (s_ins > size()) { + s_ins = size(); + } - if (!s_ins) return 0; + if (!s_ins) { + return 0; + } size_t lSize = buf_size_ - tail_; std::copy(&buf_[tail_], &buf_[tail_ + std::min(s_ins, lSize)], p_ins); - if (s_ins > lSize) std::copy(&buf_[0], &buf_[s_ins - lSize], p_ins + lSize); + if (s_ins > lSize) { + std::copy(&buf_[0], &buf_[s_ins - lSize], p_ins + lSize); + } return s_ins; } @@ -98,7 +116,9 @@ class cbuf { size_t size() noexcept { std::ptrdiff_t D = head_ - tail_; - if (D < 0 || (D == 0 && full_)) D += buf_size_; + if (D < 0 || (D == 0 && full_)) { + D += buf_size_; + } return D; } @@ -124,7 +144,9 @@ class cbuf { void unroll() { grow(0); } size_t available() noexcept { return (buf_size_ - size()); } void reserve(size_t sz) { - if (sz > capacity()) grow(sz - capacity()); + if (sz > capacity()) { + grow(sz - capacity()); + } } protected: @@ -135,7 +157,9 @@ class cbuf { std::unique_ptr new_buf(new T[new_size]); std::copy(&buf_[tail_], &buf_[tail_ + std::min(sz, lSize)], &new_buf[0]); - if (sz > lSize) std::copy(&buf_[0], &buf_[head_], &new_buf[lSize]); + if (sz > lSize) { + std::copy(&buf_[0], &buf_[head_], &new_buf[lSize]); + } tail_ = 0; head_ = sz % new_size; diff --git a/cpp_src/estl/chunk.h b/cpp_src/estl/chunk.h index 6c8db6b1d..0dff3802c 100644 --- a/cpp_src/estl/chunk.h +++ b/cpp_src/estl/chunk.h @@ -10,11 +10,11 @@ namespace reindexer { class chunk { public: chunk() noexcept : data_(nullptr), len_(0), offset_(0), cap_(0) {} - chunk(uint8_t *data, size_t len, size_t cap, size_t offset = 0) noexcept : data_(data), len_(len), offset_(offset), cap_(cap) {} + chunk(uint8_t* data, size_t len, size_t cap, size_t offset = 0) noexcept : data_(data), len_(len), offset_(offset), cap_(cap) {} ~chunk() { delete[] data_; } - chunk(const chunk &) = delete; - chunk &operator=(const chunk &) = delete; - chunk(chunk &&other) noexcept { + chunk(const chunk&) = delete; + chunk& operator=(const chunk&) = delete; + chunk(chunk&& other) noexcept { data_ = other.data_; len_ = other.len_; offset_ = other.offset_; @@ -24,7 +24,7 @@ class chunk { other.cap_ = 0; other.offset_ = 0; } - chunk &operator=(chunk &&other) noexcept { + chunk& operator=(chunk&& other) noexcept { if (this != &other) { delete[] data_; data_ = other.data_; @@ -42,7 +42,7 @@ class chunk { void append_strict(std::string_view data) { append_impl(data, len_ + data.size()); } size_t size() const noexcept { return len_ - offset_; } - uint8_t *data() const noexcept { return data_ + offset_; } + uint8_t* data() const noexcept { return data_ + offset_; } size_t capacity() const noexcept { return cap_; } size_t len() const noexcept { return len_; } size_t offset() const noexcept { return offset_; } @@ -52,7 +52,7 @@ class chunk { offset_ = 0; } void shift(size_t offset) noexcept { offset_ += offset; } - uint8_t *release() noexcept { + uint8_t* release() noexcept { auto res = data_; data_ = nullptr; return res; @@ -64,7 +64,7 @@ class chunk { } cap_ = k * size(); - uint8_t *newdata = new uint8_t[cap_]; + uint8_t* newdata = new uint8_t[cap_]; if (data_) { memcpy(newdata, data(), size()); len_ = size(); @@ -78,7 +78,7 @@ class chunk { void append_impl(std::string_view data, size_t newCapacity) { if (!data_ || len_ + data.size() > cap_) { cap_ = newCapacity; - uint8_t *newdata = new uint8_t[cap_]; + uint8_t* newdata = new uint8_t[cap_]; if (data_) { memcpy(newdata, data_, len_); } @@ -89,7 +89,7 @@ class chunk { len_ += data.size(); } - uint8_t *data_; + uint8_t* data_; size_t len_; size_t offset_; size_t cap_; diff --git a/cpp_src/estl/chunk_buf.h b/cpp_src/estl/chunk_buf.h index daed5c466..83c80de4d 100644 --- a/cpp_src/estl/chunk_buf.h +++ b/cpp_src/estl/chunk_buf.h @@ -15,7 +15,7 @@ template class chain_buf { public: chain_buf(size_t cap) : ring_(cap) {} - void write(chunk &&ch) { + void write(chunk&& ch) { if (ch.size()) { std::lock_guard lck(mtx_); const auto new_head = (head_ + 1) % ring_.size(); @@ -43,17 +43,18 @@ class chain_buf { data_size_ -= nread; while (nread) { assertrx(head_ != tail_); - chunk &cur = ring_[tail_]; + chunk& cur = ring_[tail_]; if (cur.size() > nread) { cur.shift(nread); break; } nread -= cur.size(); cur.clear(); - if (free_.size() < ring_.size() && cur.capacity() < 0x10000) + if (free_.size() < ring_.size() && cur.capacity() < 0x10000) { free_.push_back(std::move(cur)); - else + } else { cur = chunk(); + } tail_ = (tail_ + 1) % ring_.size(); } } diff --git a/cpp_src/estl/contexted_locks.h b/cpp_src/estl/contexted_locks.h index e633c95f1..f15d46333 100644 --- a/cpp_src/estl/contexted_locks.h +++ b/cpp_src/estl/contexted_locks.h @@ -40,14 +40,18 @@ class [[nodiscard]] contexted_unique_lock { lck._M_context = nullptr; } ~contexted_unique_lock() { - if (_M_owns) _M_mtx->unlock(); + if (_M_owns) { + _M_mtx->unlock(); + } } contexted_unique_lock(const contexted_unique_lock&) = delete; contexted_unique_lock& operator=(const contexted_unique_lock&) = delete; contexted_unique_lock& operator=(contexted_unique_lock&& lck) noexcept { if (this != &lck) { - if (_M_owns) unlock(); + if (_M_owns) { + unlock(); + } _M_mtx = lck._M_mtx; _M_owns = lck._M_owns; _M_context = lck._M_context; @@ -98,8 +102,12 @@ class [[nodiscard]] contexted_unique_lock { private: void _M_lockable() const noexcept { - if (_M_mtx == nullptr) assertrx(0); - if (_M_owns) assertrx(0); + if (_M_mtx == nullptr) { + assertrx(0); + } + if (_M_owns) { + assertrx(0); + } } MutexType* _M_mtx; @@ -130,14 +138,18 @@ class [[nodiscard]] contexted_shared_lock { lck._M_context = nullptr; } ~contexted_shared_lock() { - if (_M_owns) _M_mtx->unlock_shared(); + if (_M_owns) { + _M_mtx->unlock_shared(); + } } contexted_shared_lock(const contexted_shared_lock&) = delete; contexted_shared_lock& operator=(const contexted_shared_lock&) = delete; contexted_shared_lock& operator=(contexted_shared_lock&& lck) noexcept { if (this != &lck) { - if (_M_owns) unlock(); + if (_M_owns) { + unlock(); + } _M_mtx = lck._M_mtx; _M_owns = lck._M_owns; _M_context = lck._M_context; @@ -170,7 +182,9 @@ class [[nodiscard]] contexted_shared_lock { } void unlock() { - if (!_M_owns) assertrx(0); + if (!_M_owns) { + assertrx(0); + } _M_mtx->unlock_shared(); _M_owns = false; } @@ -188,8 +202,12 @@ class [[nodiscard]] contexted_shared_lock { private: void _M_lockable() const noexcept { - if (_M_mtx == nullptr) assertrx(0); - if (_M_owns) assertrx(0); + if (_M_mtx == nullptr) { + assertrx(0); + } + if (_M_owns) { + assertrx(0); + } } MutexType* _M_mtx; diff --git a/cpp_src/estl/cow.h b/cpp_src/estl/cow.h index b29eab677..8d545dfef 100644 --- a/cpp_src/estl/cow.h +++ b/cpp_src/estl/cow.h @@ -10,24 +10,26 @@ namespace reindexer { template class shared_cow_ptr { public: - explicit shared_cow_ptr(intrusive_ptr> &&ptr) noexcept : payload_(std::move(ptr)) {} + explicit shared_cow_ptr(intrusive_ptr>&& ptr) noexcept : payload_(std::move(ptr)) {} shared_cow_ptr() noexcept = default; ~shared_cow_ptr() = default; - const T *operator->() const noexcept { return payload_.get(); } + const T* operator->() const noexcept { return payload_.get(); } - const T *get() const noexcept { return payload_.get(); } - T *clone() { + const T* get() const noexcept { return payload_.get(); } + T* clone() { copy_if_not_owner(); return payload_.get(); } operator bool() const noexcept { return bool(payload_); } - const T &operator*() const noexcept { return *payload_; } + const T& operator*() const noexcept { return *payload_; } private: // If we are not the owner of the payload object, make a private copy of it void copy_if_not_owner() { - if (!payload_.unique()) payload_ = make_intrusive>(*payload_); + if (!payload_.unique()) { + payload_ = make_intrusive>(*payload_); + } } private: diff --git a/cpp_src/estl/fast_hash_traits.h b/cpp_src/estl/fast_hash_traits.h index 2cf5332f5..d2d9da975 100644 --- a/cpp_src/estl/fast_hash_traits.h +++ b/cpp_src/estl/fast_hash_traits.h @@ -1,28 +1,28 @@ #pragma once -#include #include +#include namespace reindexer { template struct is_recommends_sc_hash_map { - constexpr static bool value = false; + constexpr static bool value = false; }; template <> struct is_recommends_sc_hash_map { - constexpr static bool value = true; + constexpr static bool value = true; }; template inline constexpr bool is_recommends_sc_hash_map_v = is_recommends_sc_hash_map::value; template struct is_using_sc_version { - constexpr static bool value = is_recommends_sc_hash_map_v || - (std::is_trivially_copy_assignable_v && std::is_trivially_copy_constructible_v) || - !std::is_nothrow_move_assignable_v || !std::is_nothrow_move_constructible_v; + constexpr static bool value = is_recommends_sc_hash_map_v || + (std::is_trivially_copy_assignable_v && std::is_trivially_copy_constructible_v) || + !std::is_nothrow_move_assignable_v || !std::is_nothrow_move_constructible_v; }; template inline constexpr bool is_using_sc_version_v = is_using_sc_version::value; -} +} // namespace reindexer diff --git a/cpp_src/estl/flat_str_map.h b/cpp_src/estl/flat_str_map.h index 8824add5a..942488fdc 100644 --- a/cpp_src/estl/flat_str_map.h +++ b/cpp_src/estl/flat_str_map.h @@ -17,15 +17,15 @@ class flat_str_map { class holder_t : public base_holder_t { public: string_view_t get(size_t pos) const { - auto ptr = reinterpret_cast(base_holder_t::data() + pos); + auto ptr = reinterpret_cast(base_holder_t::data() + pos); auto l = scan_varint(10, ptr); size_t len = parse_uint32(l, ptr); - return std::string_view(reinterpret_cast(ptr + l), len); + return std::string_view(reinterpret_cast(ptr + l), len); } size_t put(string_view_t str) { size_t pos = base_holder_t::size(); base_holder_t::resize(str.size() + 8 + pos); - size_t l = string_pack(str.data(), str.size(), reinterpret_cast(base_holder_t::data()) + pos); + size_t l = string_pack(str.data(), str.size(), reinterpret_cast(base_holder_t::data()) + pos); base_holder_t::resize(pos + l); return pos; } @@ -33,23 +33,23 @@ class flat_str_map { struct equal_flat_str_map { using is_transparent = void; - equal_flat_str_map(const holder_t *buf) noexcept : buf_(buf) {} + equal_flat_str_map(const holder_t* buf) noexcept : buf_(buf) {} bool operator()(size_t lhs, size_t rhs) const { return buf_->get(lhs) == buf_->get(rhs); } bool operator()(string_view_t lhs, size_t rhs) const { return lhs == buf_->get(rhs); } bool operator()(size_t lhs, string_view_t rhs) const { return rhs == buf_->get(lhs); } private: - const holder_t *buf_; + const holder_t* buf_; }; struct hash_flat_str_map { using is_transparent = void; - hash_flat_str_map(const holder_t *buf) noexcept : buf_(buf) {} + hash_flat_str_map(const holder_t* buf) noexcept : buf_(buf) {} size_t operator()(string_view_t hs) const noexcept { return _Hash_bytes(hs.data(), hs.length()); } size_t operator()(size_t hs) const { return operator()(buf_->get(hs)); } private: - const holder_t *buf_; + const holder_t* buf_; }; using hash_map = tsl::hopscotch_map>, 30, false, @@ -59,17 +59,17 @@ class flat_str_map { flat_str_map() : holder_(std::make_unique()), map_(std::make_unique(16, hash_flat_str_map(holder_.get()), equal_flat_str_map(holder_.get()))) {} - flat_str_map(const flat_str_map &other) = delete; - flat_str_map &operator=(const flat_str_map &other) = delete; - flat_str_map(flat_str_map &&rhs) noexcept = default; - flat_str_map &operator=(flat_str_map &&rhs) noexcept = default; + flat_str_map(const flat_str_map& other) = delete; + flat_str_map& operator=(const flat_str_map& other) = delete; + flat_str_map(flat_str_map&& rhs) noexcept = default; + flat_str_map& operator=(flat_str_map&& rhs) noexcept = default; template class value_type : public std::pair { public: value_type(string_view_t k, VV v) : std::pair(k, v) {} - const value_type *operator->() const { return this; } - value_type *operator->() { return this; } + const value_type* operator->() const { return this; } + value_type* operator->() { return this; } }; template @@ -77,12 +77,12 @@ class flat_str_map { friend class flat_str_map; public: - base_iterator(map_iterator it, map_type *m, int multi_idx) noexcept : it_(it), m_(m), multi_idx_(multi_idx) {} - base_iterator(map_iterator it, map_type *m) noexcept + base_iterator(map_iterator it, map_type* m, int multi_idx) noexcept : it_(it), m_(m), multi_idx_(multi_idx) {} + base_iterator(map_iterator it, map_type* m) noexcept : it_(it), m_(m), multi_idx_((Multi && it_ != m_->map_->end() && it_->second.IsMultiValue()) ? it_->second.GetWordID() : -1) {} - base_iterator(const base_iterator &other) : it_(other.it_), m_(other.m_), multi_idx_(other.multi_idx_) {} + base_iterator(const base_iterator& other) : it_(other.it_), m_(other.m_), multi_idx_(other.multi_idx_) {} // NOLINTNEXTLINE(bugprone-unhandled-self-assignment) - base_iterator &operator=(const base_iterator &other) noexcept { + base_iterator& operator=(const base_iterator& other) noexcept { it_ = other.it_; m_ = other.m_; multi_idx_ = other.multi_idx_; @@ -109,11 +109,13 @@ class flat_str_map { return value_type(m_->holder_->get(it_->first), it_->second); } - base_iterator &operator++() noexcept { + base_iterator& operator++() noexcept { if constexpr (Multi) { if (multi_idx_ != -1) { multi_idx_ = m_->multi_[multi_idx_].next; - if (multi_idx_ != -1) return *this; + if (multi_idx_ != -1) { + return *this; + } } } ++it_; @@ -124,7 +126,7 @@ class flat_str_map { } return *this; } - base_iterator &operator--() noexcept { + base_iterator& operator--() noexcept { static_assert(Multi, "Sorry, flat_std_multimap::iterator::operator-- () is not implemented"); --it_; return *this; @@ -140,22 +142,22 @@ class flat_str_map { return ret; } template - bool operator!=(const it2 &rhs) const noexcept { + bool operator!=(const it2& rhs) const noexcept { return it_ != rhs.it_ || multi_idx_ != rhs.multi_idx_; } template - bool operator==(const it2 &rhs) const noexcept { + bool operator==(const it2& rhs) const noexcept { return it_ == rhs.it_ && multi_idx_ == rhs.multi_idx_; } protected: map_iterator it_; - map_type *m_; + map_type* m_; int multi_idx_; }; - using iterator = base_iterator>; - using const_iterator = base_iterator>; + using iterator = base_iterator>; + using const_iterator = base_iterator>; iterator begin() noexcept { return iterator(map_->begin(), this); } iterator end() noexcept { return iterator(map_->end(), this); } @@ -168,18 +170,22 @@ class flat_str_map { std::pair equal_range(string_view_t str) noexcept { auto it = map_->find(str); auto it2 = it; - if (it2 != map_->end()) ++it2; + if (it2 != map_->end()) { + ++it2; + } return {iterator(it, this), iterator(it2, this)}; } std::pair equal_range(string_view_t str) const noexcept { auto it = map_->find(str); auto it2 = it; - if (it2 != map_->end()) ++it2; + if (it2 != map_->end()) { + ++it2; + } return {const_iterator(it, this), const_iterator(it2, this)}; } - std::pair insert(string_view_t str, const V &v) { + std::pair insert(string_view_t str, const V& v) { size_t pos = holder_->put(str); const auto h = hash_flat_str_map(holder_.get())(str); auto res = map_->try_emplace_prehashed(h, pos, v); @@ -209,7 +215,7 @@ class flat_str_map { } } - std::pair emplace(string_view_t str, const V &v) { return insert(str, v); } + std::pair emplace(string_view_t str, const V& v) { return insert(str, v); } void reserve(size_t map_sz, size_t str_sz) { map_->reserve(map_sz); @@ -237,8 +243,8 @@ class flat_str_map { // Underlying map container std::unique_ptr map_; struct multi_node { - multi_node(const V &v, int n) : val(v), next(n) {} - multi_node(V &&v, int n) noexcept : val(std::move(v)), next(n) {} + multi_node(const V& v, int n) : val(v), next(n) {} + multi_node(V&& v, int n) noexcept : val(std::move(v)), next(n) {} V val; int next; diff --git a/cpp_src/estl/h_vector.h b/cpp_src/estl/h_vector.h index 8bf47715a..e5982f5bb 100644 --- a/cpp_src/estl/h_vector.h +++ b/cpp_src/estl/h_vector.h @@ -104,7 +104,9 @@ class h_vector { } if constexpr (!std::is_trivially_destructible_v) { const auto old_sz = size(); - for (; i < old_sz; i++) p[i].~T(); + for (; i < old_sz; i++) { + p[i].~T(); + } } size_ = other.size_; } @@ -139,9 +141,13 @@ class h_vector { bool operator==(const h_vector& other) const noexcept(noexcept(std::declval() == std::declval())) { if (&other != this) { const size_type sz = size_; - if (sz != other.size()) return false; + if (sz != other.size()) { + return false; + } for (size_t i = 0; i < sz; ++i) { - if (!(operator[](i) == other[i])) return false; + if (!(operator[](i) == other[i])) { + return false; + } } return true; } @@ -161,7 +167,9 @@ class h_vector { } else if constexpr (!std::is_trivially_destructible_v) { const pointer p = ptr(); const size_type sz = size_; - for (size_type i = 0; i < sz; ++i) p[i].~T(); + for (size_type i = 0; i < sz; ++i) { + p[i].~T(); + } } size_ = 0; } @@ -223,12 +231,16 @@ class h_vector { if constexpr (!reindexer::is_trivially_default_constructible::value) { const pointer p = ptr(); const size_type old_sz = size_; - for (size_type i = old_sz; i < sz; ++i) new (p + i) T(); + for (size_type i = old_sz; i < sz; ++i) { + new (p + i) T(); + } } if constexpr (!std::is_trivially_destructible_v) { const pointer p = ptr(); const size_type old_sz = size_; - for (size_type i = sz; i < old_sz; ++i) p[i].~T(); + for (size_type i = sz; i < old_sz; ++i) { + p[i].~T(); + } } size_ = sz; } @@ -267,7 +279,9 @@ class h_vector { } ++old_data; } - if (!is_hdata()) operator delete(oold_data); + if (!is_hdata()) { + operator delete(oold_data); + } e_.data_ = new_data; e_.cap_ = sz; is_hdata_ = 0; @@ -342,7 +356,9 @@ class h_vector { return begin() + i; } iterator insert(const_iterator pos, difference_type count, const T& v) { - if (count == 0) return const_cast(pos); + if (count == 0) { + return const_cast(pos); + } difference_type i = pos - begin(); rx_debug_check_subscript_le(i); const int64_t sz = size_; @@ -400,7 +416,9 @@ class h_vector { iterator insert(const_iterator pos, InputIt first, InputIt last) { rx_debug_check_valid_range(first, last); const difference_type cnt = last - first; - if (cnt == 0) return const_cast(pos); + if (cnt == 0) { + return const_cast(pos); + } const difference_type i = pos - begin(); rx_debug_check_subscript_le(i); const int64_t sz = size_; @@ -455,14 +473,18 @@ class h_vector { std::move(std::make_move_iterator(firstPtr + cnt), std::make_move_iterator(p + sz), firstPtr); const auto newSize = sz - cnt; if constexpr (!std::is_trivially_destructible_v) { - for (size_type j = newSize; j < sz; ++j) p[j].~T(); + for (size_type j = newSize; j < sz; ++j) { + p[j].~T(); + } } size_ = newSize; return firstPtr; } void shrink_to_fit() { const auto sz = size(); - if (is_hdata() || sz == capacity()) return; + if (is_hdata() || sz == capacity()) { + return; + } h_vector tmp; tmp.reserve(sz); @@ -480,12 +502,16 @@ class h_vector { if (is_hdata()) { if constexpr (!std::is_trivially_destructible_v) { const size_type sz = size_; - for (size_type i = 0; i < sz; ++i) reinterpret_cast(hdata_)[i].~T(); + for (size_type i = 0; i < sz; ++i) { + reinterpret_cast(hdata_)[i].~T(); + } } } else { if constexpr (!std::is_trivially_destructible_v) { const size_type sz = size_; - for (size_type i = 0; i < sz; ++i) e_.data_[i].~T(); + for (size_type i = 0; i < sz; ++i) { + e_.data_[i].~T(); + } } operator delete(e_.data_); } @@ -511,7 +537,9 @@ template inline static std::ostream& operator<<(std::ostream& o, const reindexer::h_vector& vec) { o << "["; for (unsigned i = 0; i < vec.size(); i++) { - if (i != 0) o << ","; + if (i != 0) { + o << ","; + } o << vec[i] << " "; } o << "]"; diff --git a/cpp_src/estl/intrusive_ptr.h b/cpp_src/estl/intrusive_ptr.h index 7aaca5230..6c5a0bb38 100644 --- a/cpp_src/estl/intrusive_ptr.h +++ b/cpp_src/estl/intrusive_ptr.h @@ -17,49 +17,57 @@ class intrusive_ptr { constexpr intrusive_ptr() noexcept = default; constexpr intrusive_ptr(std::nullptr_t) noexcept {} - intrusive_ptr(T *p, bool add_ref = true) noexcept : px(p) { - if (px != 0 && add_ref) intrusive_ptr_add_ref(px); + intrusive_ptr(T* p, bool add_ref = true) noexcept : px(p) { + if (px != 0 && add_ref) { + intrusive_ptr_add_ref(px); + } } template - intrusive_ptr(intrusive_ptr const &rhs) noexcept : px(rhs.get()) { - if (px != 0) intrusive_ptr_add_ref(px); + intrusive_ptr(const intrusive_ptr& rhs) noexcept : px(rhs.get()) { + if (px != 0) { + intrusive_ptr_add_ref(px); + } } - intrusive_ptr(intrusive_ptr const &rhs) noexcept : px(rhs.px) { - if (px != 0) intrusive_ptr_add_ref(px); + intrusive_ptr(const intrusive_ptr& rhs) noexcept : px(rhs.px) { + if (px != 0) { + intrusive_ptr_add_ref(px); + } } ~intrusive_ptr() { - if (px != 0) intrusive_ptr_release(px); + if (px != 0) { + intrusive_ptr_release(px); + } } template - intrusive_ptr &operator=(intrusive_ptr const &rhs) noexcept { + intrusive_ptr& operator=(const intrusive_ptr& rhs) noexcept { this_type(rhs).swap(*this); return *this; } - intrusive_ptr(intrusive_ptr &&rhs) noexcept : px(rhs.px) { rhs.px = 0; } + intrusive_ptr(intrusive_ptr&& rhs) noexcept : px(rhs.px) { rhs.px = 0; } - intrusive_ptr &operator=(intrusive_ptr &&rhs) noexcept { - this_type(static_cast(rhs)).swap(*this); + intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept { + this_type(static_cast(rhs)).swap(*this); return *this; } // NOLINTNEXTLINE(bugprone-unhandled-self-assignment) - intrusive_ptr &operator=(intrusive_ptr const &rhs) noexcept { + intrusive_ptr& operator=(const intrusive_ptr& rhs) noexcept { this_type(rhs).swap(*this); return *this; } - intrusive_ptr &operator=(T *rhs) noexcept { + intrusive_ptr& operator=(T* rhs) noexcept { this_type(rhs).swap(*this); return *this; } void reset() noexcept { this_type().swap(*this); } - void reset(T *rhs) noexcept { this_type(rhs).swap(*this); } + void reset(T* rhs) noexcept { this_type(rhs).swap(*this); } bool unique() const noexcept { if (px == 0) { return true; @@ -67,111 +75,111 @@ class intrusive_ptr { return intrusive_ptr_is_unique(px); } - T *get() const noexcept { return px; } + T* get() const noexcept { return px; } - T &operator*() const noexcept { + T& operator*() const noexcept { assertrx(px != 0); return *px; } - T *operator->() const noexcept { + T* operator->() const noexcept { assertrx(px != 0); return px; } - typedef T *this_type::*unspecified_bool_type; + typedef T* this_type::*unspecified_bool_type; operator unspecified_bool_type() const noexcept { return px == 0 ? 0 : &this_type::px; } - void swap(intrusive_ptr &rhs) noexcept { - T *tmp = px; + void swap(intrusive_ptr& rhs) noexcept { + T* tmp = px; px = rhs.px; rhs.px = tmp; } private: - T *px{nullptr}; + T* px{nullptr}; }; template -inline bool operator==(intrusive_ptr const &a, intrusive_ptr const &b) noexcept { +inline bool operator==(const intrusive_ptr& a, const intrusive_ptr& b) noexcept { return a.get() == b.get(); } template -inline bool operator!=(intrusive_ptr const &a, intrusive_ptr const &b) noexcept { +inline bool operator!=(const intrusive_ptr& a, const intrusive_ptr& b) noexcept { return a.get() != b.get(); } template -inline bool operator==(intrusive_ptr const &a, U *b) noexcept { +inline bool operator==(const intrusive_ptr& a, U* b) noexcept { return a.get() == b; } template -inline bool operator!=(intrusive_ptr const &a, U *b) noexcept { +inline bool operator!=(const intrusive_ptr& a, U* b) noexcept { return a.get() != b; } template -inline bool operator==(T *a, intrusive_ptr const &b) noexcept { +inline bool operator==(T* a, const intrusive_ptr& b) noexcept { return a == b.get(); } template -inline bool operator!=(T *a, intrusive_ptr const &b) noexcept { +inline bool operator!=(T* a, const intrusive_ptr& b) noexcept { return a != b.get(); } template -inline bool operator<(intrusive_ptr const &a, intrusive_ptr const &b) noexcept { - return std::less()(a.get(), b.get()); +inline bool operator<(const intrusive_ptr& a, const intrusive_ptr& b) noexcept { + return std::less()(a.get(), b.get()); } template -void swap(intrusive_ptr &lhs, intrusive_ptr &rhs) noexcept { +void swap(intrusive_ptr& lhs, intrusive_ptr& rhs) noexcept { lhs.swap(rhs); } template -T *get_pointer(intrusive_ptr const &p) noexcept { +T* get_pointer(const intrusive_ptr& p) noexcept { return p.get(); } template -intrusive_ptr static_pointer_cast(intrusive_ptr const &p) noexcept { - return static_cast(p.get()); +intrusive_ptr static_pointer_cast(const intrusive_ptr& p) noexcept { + return static_cast(p.get()); } template -intrusive_ptr const_pointer_cast(intrusive_ptr const &p) noexcept { - return const_cast(p.get()); +intrusive_ptr const_pointer_cast(const intrusive_ptr& p) noexcept { + return const_cast(p.get()); } template -intrusive_ptr dynamic_pointer_cast(intrusive_ptr const &p) noexcept { - return dynamic_cast(p.get()); +intrusive_ptr dynamic_pointer_cast(const intrusive_ptr& p) noexcept { + return dynamic_cast(p.get()); } template class intrusive_atomic_rc_wrapper; template -inline void intrusive_ptr_add_ref(intrusive_atomic_rc_wrapper *x) noexcept { +inline void intrusive_ptr_add_ref(intrusive_atomic_rc_wrapper* x) noexcept { if (x) { x->refcount.fetch_add(1, std::memory_order_relaxed); } } template -inline void intrusive_ptr_release(intrusive_atomic_rc_wrapper *x) noexcept { +inline void intrusive_ptr_release(intrusive_atomic_rc_wrapper* x) noexcept { if (x && x->refcount.fetch_sub(1, std::memory_order_acq_rel) == 1) { delete x; } } template -inline bool intrusive_ptr_is_unique(intrusive_atomic_rc_wrapper *x) noexcept { +inline bool intrusive_ptr_is_unique(intrusive_atomic_rc_wrapper* x) noexcept { // std::memory_order_acquire - is essential for COW constructions based on intrusive_ptr return !x || (x->refcount.load(std::memory_order_acquire) == 1); } @@ -180,36 +188,36 @@ template class intrusive_atomic_rc_wrapper : public T { public: template - intrusive_atomic_rc_wrapper(Args &&...args) : T(std::forward(args)...), refcount(0) {} - intrusive_atomic_rc_wrapper &operator=(const intrusive_atomic_rc_wrapper &) = delete; + intrusive_atomic_rc_wrapper(Args&&... args) : T(std::forward(args)...), refcount(0) {} + intrusive_atomic_rc_wrapper& operator=(const intrusive_atomic_rc_wrapper&) = delete; protected: std::atomic refcount; - friend void intrusive_ptr_add_ref<>(intrusive_atomic_rc_wrapper *x) noexcept; - friend void intrusive_ptr_release<>(intrusive_atomic_rc_wrapper *x) noexcept; - friend bool intrusive_ptr_is_unique<>(intrusive_atomic_rc_wrapper *x) noexcept; + friend void intrusive_ptr_add_ref<>(intrusive_atomic_rc_wrapper* x) noexcept; + friend void intrusive_ptr_release<>(intrusive_atomic_rc_wrapper* x) noexcept; + friend bool intrusive_ptr_is_unique<>(intrusive_atomic_rc_wrapper* x) noexcept; }; template class intrusive_rc_wrapper; template -inline void intrusive_ptr_add_ref(intrusive_rc_wrapper *x) noexcept { +inline void intrusive_ptr_add_ref(intrusive_rc_wrapper* x) noexcept { if (x) { ++x->refcount; } } template -inline void intrusive_ptr_release(intrusive_rc_wrapper *x) noexcept { +inline void intrusive_ptr_release(intrusive_rc_wrapper* x) noexcept { if (x && --x->refcount == 0) { delete x; } } template -inline bool intrusive_ptr_is_unique(intrusive_rc_wrapper *x) noexcept { +inline bool intrusive_ptr_is_unique(intrusive_rc_wrapper* x) noexcept { return !x || (x->refcount == 1); } @@ -217,50 +225,50 @@ template class intrusive_rc_wrapper : public T { public: template - intrusive_rc_wrapper(Args &&...args) : T(std::forward(args)...), refcount(0) {} - intrusive_rc_wrapper &operator=(const intrusive_rc_wrapper &) = delete; + intrusive_rc_wrapper(Args&&... args) : T(std::forward(args)...), refcount(0) {} + intrusive_rc_wrapper& operator=(const intrusive_rc_wrapper&) = delete; protected: int refcount; - friend void intrusive_ptr_add_ref<>(intrusive_rc_wrapper *x) noexcept; - friend void intrusive_ptr_release<>(intrusive_rc_wrapper *x) noexcept; - friend bool intrusive_ptr_is_unique<>(intrusive_rc_wrapper *x) noexcept; + friend void intrusive_ptr_add_ref<>(intrusive_rc_wrapper* x) noexcept; + friend void intrusive_ptr_release<>(intrusive_rc_wrapper* x) noexcept; + friend bool intrusive_ptr_is_unique<>(intrusive_rc_wrapper* x) noexcept; }; class intrusive_atomic_rc_base { public: intrusive_atomic_rc_base() noexcept : refcount(0) {} - intrusive_atomic_rc_base &operator=(const intrusive_atomic_rc_base &) = delete; + intrusive_atomic_rc_base& operator=(const intrusive_atomic_rc_base&) = delete; virtual ~intrusive_atomic_rc_base() = default; protected: std::atomic refcount; - friend void intrusive_ptr_add_ref(intrusive_atomic_rc_base *x) noexcept; - friend void intrusive_ptr_release(intrusive_atomic_rc_base *x) noexcept; - friend bool intrusive_ptr_is_unique(intrusive_atomic_rc_base *x) noexcept; + friend void intrusive_ptr_add_ref(intrusive_atomic_rc_base* x) noexcept; + friend void intrusive_ptr_release(intrusive_atomic_rc_base* x) noexcept; + friend bool intrusive_ptr_is_unique(intrusive_atomic_rc_base* x) noexcept; }; -inline void intrusive_ptr_add_ref(intrusive_atomic_rc_base *x) noexcept { +inline void intrusive_ptr_add_ref(intrusive_atomic_rc_base* x) noexcept { if (x) { x->refcount.fetch_add(1, std::memory_order_relaxed); } } -inline void intrusive_ptr_release(intrusive_atomic_rc_base *x) noexcept { +inline void intrusive_ptr_release(intrusive_atomic_rc_base* x) noexcept { if (x && x->refcount.fetch_sub(1, std::memory_order_acq_rel) == 1) { delete x; } } -inline bool intrusive_ptr_is_unique(intrusive_atomic_rc_base *x) noexcept { +inline bool intrusive_ptr_is_unique(intrusive_atomic_rc_base* x) noexcept { // std::memory_order_acquire - is essential for COW constructions based on intrusive_ptr return !x || (x->refcount.load(std::memory_order_acquire) == 1); } template -intrusive_ptr make_intrusive(Args &&...args) { +intrusive_ptr make_intrusive(Args&&... args) { return intrusive_ptr(new T(std::forward(args)...)); } @@ -271,13 +279,13 @@ namespace std { template struct hash> { public: - size_t operator()(const reindexer::intrusive_atomic_rc_wrapper &obj) const { return hash()(obj); } + size_t operator()(const reindexer::intrusive_atomic_rc_wrapper& obj) const { return hash()(obj); } }; template struct hash> { public: - size_t operator()(const reindexer::intrusive_ptr &obj) const { return hash()(*obj); } + size_t operator()(const reindexer::intrusive_ptr& obj) const { return hash()(*obj); } }; } // namespace std diff --git a/cpp_src/estl/multihash_set.h b/cpp_src/estl/multihash_set.h index 047ee53de..50c2d2f97 100644 --- a/cpp_src/estl/multihash_set.h +++ b/cpp_src/estl/multihash_set.h @@ -79,7 +79,9 @@ class MultiHashSetImpl { } } for (size_t i = 0; i < N; ++i) { - if (i == firstIdx) continue; + if (i == firstIdx) { + continue; + } size_t hash; try { hash = H::hash(i, k) % capacity_; diff --git a/cpp_src/estl/mutex.h b/cpp_src/estl/mutex.h index 616f267d3..decea44e9 100644 --- a/cpp_src/estl/mutex.h +++ b/cpp_src/estl/mutex.h @@ -51,8 +51,11 @@ class spinlock { ~spinlock() = default; void lock() { - for (unsigned int i = 1; !try_lock(); ++i) - if ((i & 0xff) == 0) std::this_thread::yield(); + for (unsigned int i = 1; !try_lock(); ++i) { + if ((i & 0xff) == 0) { + std::this_thread::yield(); + } + } } bool try_lock() { return !_M_lock.test_and_set(std::memory_order_acq_rel); } void unlock() { _M_lock.clear(std::memory_order_release); } diff --git a/cpp_src/estl/packed_vector.h b/cpp_src/estl/packed_vector.h index 4bbacb266..fb0f410a6 100644 --- a/cpp_src/estl/packed_vector.h +++ b/cpp_src/estl/packed_vector.h @@ -60,7 +60,9 @@ class packed_vector { } void erase_back(size_type pos) { - for (auto it = iterator(this, data_.begin() + pos); it != end(); ++it) size_--; + for (auto it = iterator(this, data_.begin() + pos); it != end(); ++it) { + size_--; + } data_.resize(pos); } @@ -76,7 +78,9 @@ class packed_vector { for (auto it = from; it != to; it++, i++) { if (!(i % 100)) { size_type sz = 0, j = 0; - for (auto iit = it; j < 100 && iit != to; iit++, j++) sz += iit->maxpackedsize(); + for (auto iit = it; j < 100 && iit != to; iit++, j++) { + sz += iit->maxpackedsize(); + } data_.resize(p + sz); } p += it->pack(&*(data_.begin() + p)); diff --git a/cpp_src/estl/protobufparser.h b/cpp_src/estl/protobufparser.h index d66ed3382..ac1c231af 100644 --- a/cpp_src/estl/protobufparser.h +++ b/cpp_src/estl/protobufparser.h @@ -24,7 +24,9 @@ struct ProtobufValue { throw reindexer::Error(errParseMsgPack, "Impossible to convert type [%s] to number", value.Type().Name()); }); - if (v < minv || v > maxv) throw reindexer::Error(errParams, "Value is out of bounds: [%d,%d]", minv, maxv); + if (v < minv || v > maxv) { + throw reindexer::Error(errParams, "Value is out of bounds: [%d,%d]", minv, maxv); + } return v; } diff --git a/cpp_src/estl/shared_mutex.h b/cpp_src/estl/shared_mutex.h index 609f87ba9..79315711c 100644 --- a/cpp_src/estl/shared_mutex.h +++ b/cpp_src/estl/shared_mutex.h @@ -63,7 +63,9 @@ class __shared_mutex_pthread { bool try_lock() noexcept { int __ret = pthread_rwlock_trywrlock(&_M_rwlock); - if (__ret == EBUSY) return false; + if (__ret == EBUSY) { + return false; + } assertrx(__ret == 0); return true; } @@ -76,14 +78,17 @@ class __shared_mutex_pthread { void lock_shared() noexcept { int __ret; - do __ret = pthread_rwlock_rdlock(&_M_rwlock); - while (__ret == EAGAIN || __ret == EBUSY); + do { + __ret = pthread_rwlock_rdlock(&_M_rwlock); + } while (__ret == EAGAIN || __ret == EBUSY); assertrx(__ret == 0); } bool try_lock_shared() noexcept { int __ret = pthread_rwlock_tryrdlock(&_M_rwlock); - if (__ret == EBUSY || __ret == EAGAIN) return false; + if (__ret == EBUSY || __ret == EAGAIN) { + return false; + } assertrx(__ret == 0); return true; } @@ -103,11 +108,13 @@ class shared_lock { explicit shared_lock(mutex_type& __m) noexcept : _M_pm(&__m), _M_owns(true) { __m.lock_shared(); } ~shared_lock() { - if (_M_owns) _M_pm->unlock_shared(); + if (_M_owns) { + _M_pm->unlock_shared(); + } } - shared_lock(shared_lock const&) = delete; - shared_lock& operator=(shared_lock const&) = delete; + shared_lock(const shared_lock&) = delete; + shared_lock& operator=(const shared_lock&) = delete; shared_lock(shared_lock&& __sl) noexcept : shared_lock() { swap(__sl); } diff --git a/cpp_src/estl/smart_lock.h b/cpp_src/estl/smart_lock.h index 5d9ff02a3..c781e2705 100644 --- a/cpp_src/estl/smart_lock.h +++ b/cpp_src/estl/smart_lock.h @@ -12,10 +12,11 @@ class smart_lock { smart_lock() noexcept : mtx_(nullptr), unique_(false), locked_(false) {} smart_lock(Mutex& mtx, bool unique = false) : mtx_(&mtx), unique_(unique), locked_(true) { - if (unique_) + if (unique_) { mtx_->lock(); - else + } else { mtx_->lock_shared(); + } } template smart_lock(Mutex& mtx, Context& context, bool unique = false, milliseconds chkTimeout = kDefaultCondChkTime) diff --git a/cpp_src/estl/span.h b/cpp_src/estl/span.h index 3896d3aed..b95a9fdad 100644 --- a/cpp_src/estl/span.h +++ b/cpp_src/estl/span.h @@ -86,9 +86,13 @@ class span { } bool operator==(const span& other) const noexcept { if (&other != this) { - if (size() != other.size()) return false; + if (size() != other.size()) { + return false; + } for (size_t i = 0; i < size(); ++i) { - if (!(at(i) == other.at(i))) return false; + if (!(at(i) == other.at(i))) { + return false; + } } return true; } diff --git a/cpp_src/estl/suffix_map.h b/cpp_src/estl/suffix_map.h index 57b46c276..d303b6b4e 100644 --- a/cpp_src/estl/suffix_map.h +++ b/cpp_src/estl/suffix_map.h @@ -11,40 +11,40 @@ class suffix_map { typedef size_t size_type; typedef unsigned char char_type; - class value_type : public std::pair { + class value_type : public std::pair { public: - value_type(std::pair &&v) noexcept : std::pair(std::move(v)) {} - value_type(const std::pair &v) : std::pair(v) {} - const value_type *operator->() const noexcept { return this; } + value_type(std::pair&& v) noexcept : std::pair(std::move(v)) {} + value_type(const std::pair& v) : std::pair(v) {} + const value_type* operator->() const noexcept { return this; } }; class iterator { friend class suffix_map; public: - iterator(size_type idx, const suffix_map *m) noexcept : idx_(idx), m_(m) {} - iterator(const iterator &other) noexcept : idx_(other.idx_), m_(other.m_) {} + iterator(size_type idx, const suffix_map* m) noexcept : idx_(idx), m_(m) {} + iterator(const iterator& other) noexcept : idx_(other.idx_), m_(other.m_) {} // NOLINTNEXTLINE(bugprone-unhandled-self-assignment) - iterator &operator=(const iterator &other) noexcept { + iterator& operator=(const iterator& other) noexcept { idx_ = other.idx; m_ = other.m_; return *this; } value_type operator->() { - auto *p = &m_->text_[m_->sa_[idx_]]; + auto* p = &m_->text_[m_->sa_[idx_]]; return value_type(std::make_pair(p, m_->mapped_[m_->sa_[idx_]])); } const value_type operator->() const { - auto *p = &m_->text_[m_->sa_[idx_]]; + auto* p = &m_->text_[m_->sa_[idx_]]; return value_type(std::make_pair(p, m_->mapped_[m_->sa_[idx_]])); } - iterator &operator++() noexcept { + iterator& operator++() noexcept { ++idx_; return *this; } - iterator &operator--() noexcept { + iterator& operator--() noexcept { --idx_; return *this; } @@ -59,28 +59,32 @@ class suffix_map { return ret; } int lcp() noexcept { return m_->lcp_[idx_]; } - bool operator!=(const iterator &rhs) const noexcept { return idx_ != rhs.idx_; } - bool operator==(const iterator &rhs) const noexcept { return idx_ == rhs.idx_; } + bool operator!=(const iterator& rhs) const noexcept { return idx_ != rhs.idx_; } + bool operator==(const iterator& rhs) const noexcept { return idx_ == rhs.idx_; } protected: size_type idx_; - const suffix_map *m_; + const suffix_map* m_; }; public: suffix_map() = default; - suffix_map(const suffix_map & /*other*/) = delete; - suffix_map &operator=(const suffix_map & /*other*/) = default; - suffix_map(suffix_map && /*rhs*/) noexcept = default; + suffix_map(const suffix_map& /*other*/) = delete; + suffix_map& operator=(const suffix_map& /*other*/) = default; + suffix_map(suffix_map&& /*rhs*/) noexcept = default; iterator begin() const noexcept { return iterator(0, this); } iterator end() const noexcept { return iterator(sa_.size(), this); } std::pair match_range(std::string_view str) const { iterator start = lower_bound(str); - if (start == end()) return {end(), end()}; + if (start == end()) { + return {end(), end()}; + } int idx_ = start.idx_ + 1; - while (idx_ < int(sa_.size()) && lcp_[idx_ - 1] >= int(str.length())) idx_++; + while (idx_ < int(sa_.size()) && lcp_[idx_ - 1] >= int(str.length())) { + idx_++; + } return {start, iterator(idx_, this)}; } @@ -91,13 +95,15 @@ class suffix_map { size_type lo = 0, hi = sa_.size(), mid; int lcp_lo = 0, lcp_hi = 0; - auto P = reinterpret_cast(str.data()); - auto T = reinterpret_cast(text_.data()); + auto P = reinterpret_cast(str.data()); + auto T = reinterpret_cast(text_.data()); while (lo <= hi) { mid = (lo + hi) / 2; int i = std::min(lcp_hi, lcp_lo); bool plt = true; - if (mid >= sa_.size()) return end(); + if (mid >= sa_.size()) { + return end(); + } while (i < int(str.length()) && sa_[mid] + i < int(text_.size())) { if (P[i] < T[sa_[mid] + i]) { break; @@ -109,15 +115,18 @@ class suffix_map { } if (plt) { if (mid == lo + 1) { - if (strncmp(str.data(), &text_[sa_[mid]], std::min(str.length(), strlen(&text_[sa_[mid]]))) != 0) return end(); + if (strncmp(str.data(), &text_[sa_[mid]], std::min(str.length(), strlen(&text_[sa_[mid]]))) != 0) { + return end(); + } return iterator(mid, this); } lcp_hi = i; hi = mid; } else { if (mid == hi - 1) { - if (hi >= sa_.size() || strncmp(str.data(), &text_[sa_[hi]], std::min(str.length(), strlen(&text_[sa_[hi]]))) != 0) + if (hi >= sa_.size() || strncmp(str.data(), &text_[sa_[hi]], std::min(str.length(), strlen(&text_[sa_[hi]]))) != 0) { return end(); + } return iterator(hi, this); } lcp_lo = i; @@ -127,8 +136,10 @@ class suffix_map { return end(); } - int insert(std::string_view word, const V &val, int virtual_len = -1) { - if (virtual_len == -1) virtual_len = word.length(); + int insert(std::string_view word, const V& val, int virtual_len = -1) { + if (virtual_len == -1) { + virtual_len = word.length(); + } int wpos = text_.size(); size_t real_len = word.length(); text_.insert(text_.end(), word.begin(), word.end()); @@ -140,16 +151,20 @@ class suffix_map { return wpos; } - const CharT *word_at(int idx) const noexcept { return &text_[words_[idx]]; } + const CharT* word_at(int idx) const noexcept { return &text_[words_[idx]]; } int16_t word_len_at(int idx) const noexcept { return words_len_[idx].first; } int16_t virtual_word_len(int idx) const noexcept { return words_len_[idx].second; } void build() { - if (built_) return; + if (built_) { + return; + } text_.shrink_to_fit(); sa_.resize(text_.size()); - if (!sa_.empty()) ::divsufsort(reinterpret_cast(text_.data()), &sa_[0], text_.size()); + if (!sa_.empty()) { + ::divsufsort(reinterpret_cast(text_.data()), &sa_[0], text_.size()); + } build_lcp(); built_ = true; } @@ -172,7 +187,7 @@ class suffix_map { size_type size() const noexcept { return sa_.size(); } size_type word_size() const noexcept { return words_.size(); } - const std::vector &text() const noexcept { return text_; } + const std::vector& text() const noexcept { return text_; } size_t heap_size() noexcept { return (sa_.capacity() + words_.capacity()) * sizeof(int) + // (lcp_.capacity() + words_len_.capacity()) * sizeof(int16_t) + // @@ -186,7 +201,9 @@ class suffix_map { lcp_.resize(sa_.size()); int k = 0, n = size(); - for (int i = 0; i < n; i++) rank_[sa_[i]] = i; + for (int i = 0; i < n; i++) { + rank_[sa_[i]] = i; + } for (int i = 0; i < n; i++, k ? k-- : 0) { auto r = rank_[i]; if (r == n - 1) { @@ -194,7 +211,9 @@ class suffix_map { continue; } int j = sa_[r + 1]; - while (i + k < n && j + k < n && text_[i + k] == text_[j + k]) k++; + while (i + k < n && j + k < n && text_[i + k] == text_[j + k]) { + k++; + } lcp_[r] = k; } } diff --git a/cpp_src/estl/tokenizer.cc b/cpp_src/estl/tokenizer.cc index bcf8a5ea7..b6946ef58 100644 --- a/cpp_src/estl/tokenizer.cc +++ b/cpp_src/estl/tokenizer.cc @@ -25,7 +25,9 @@ void tokenizer::skip_space() noexcept { token tokenizer::next_token(flags flgs) { skip_space(); - if (cur_ == q_.end()) return token(TokenEnd); + if (cur_ == q_.end()) { + return token(TokenEnd); + } token res(TokenSymbol); @@ -33,7 +35,9 @@ token tokenizer::next_token(flags flgs) { res.type = TokenName; int openBrackets{0}; do { - if (*cur_ == '*' && *(cur_ - 1) != '[') break; + if (*cur_ == '*' && *(cur_ - 1) != '[') { + break; + } res.text_.push_back(flgs.has_to_lower() ? tolower(*cur_++) : *cur_++); ++pos_; } while (cur_ != q_.end() && (isalpha(*cur_) || isdigit(*cur_) || *cur_ == '_' || *cur_ == '#' || *cur_ == '.' || *cur_ == '*' || @@ -94,7 +98,9 @@ token tokenizer::next_token(flags flgs) { auto c = *cur_; if (c == '\\') { ++pos_; - if (++cur_ == q_.end()) break; + if (++cur_ == q_.end()) { + break; + } c = *cur_; switch (c) { case 'n': @@ -163,8 +169,9 @@ std::string tokenizer::where() const { if (*pos == '\n') { line++; col = 0; - } else + } else { col++; + } } return std::string() .append("line: ") diff --git a/cpp_src/estl/tokenizer.h b/cpp_src/estl/tokenizer.h index 4ed8d193c..4387c7b29 100644 --- a/cpp_src/estl/tokenizer.h +++ b/cpp_src/estl/tokenizer.h @@ -11,14 +11,14 @@ enum token_type { TokenEnd, TokenName, TokenNumber, TokenString, TokenOp, TokenS class token { public: explicit token(token_type t = TokenSymbol) noexcept : type(t) {} - token(const token &) = delete; - token &operator=(const token &) = delete; - token(token &&) noexcept = default; - token &operator=(token &&) noexcept = default; + token(const token&) = delete; + token& operator=(const token&) = delete; + token(token&&) noexcept = default; + token& operator=(token&&) noexcept = default; [[nodiscard]] RX_ALWAYS_INLINE std::string_view text() const noexcept { return std::string_view(text_.data(), text_.size()); } void to_lower() noexcept { - for (auto &c : text_) { + for (auto& c : text_) { c = tolower(c); } } @@ -78,7 +78,7 @@ class tokenizer { } [[nodiscard]] std::string where() const; [[nodiscard]] size_t length() const noexcept { return q_.length(); } - [[nodiscard]] const char *begin() const noexcept { return q_.data(); } + [[nodiscard]] const char* begin() const noexcept { return q_.data(); } private: std::string_view q_; @@ -86,6 +86,6 @@ class tokenizer { size_t pos_ = 0; }; -Variant token2kv(const token &currTok, tokenizer &parser, bool allowComposite); +Variant token2kv(const token& currTok, tokenizer& parser, bool allowComposite); } // namespace reindexer diff --git a/cpp_src/estl/trivial_reverse_iterator.h b/cpp_src/estl/trivial_reverse_iterator.h index ccf1aa514..779614e7f 100644 --- a/cpp_src/estl/trivial_reverse_iterator.h +++ b/cpp_src/estl/trivial_reverse_iterator.h @@ -66,14 +66,18 @@ class trivial_reverse_iterator { // Assign operator overloading from const std::reverse_iterator template trivial_reverse_iterator& operator=(const std::reverse_iterator& u) noexcept { - if (current_ != u.base()) current_ = u.base(); + if (current_ != u.base()) { + current_ = u.base(); + } return *this; } // Assign operator overloading from non-const std::reverse_iterator template trivial_reverse_iterator& operator=(std::reverse_iterator& u) noexcept { - if (current_ != u.base()) current_ = u.base(); + if (current_ != u.base()) { + current_ = u.base(); + } return *this; } diff --git a/cpp_src/gtests/bench/fixtures/aggregation.cc b/cpp_src/gtests/bench/fixtures/aggregation.cc index 6523df608..6cffbfa1b 100644 --- a/cpp_src/gtests/bench/fixtures/aggregation.cc +++ b/cpp_src/gtests/bench/fixtures/aggregation.cc @@ -8,15 +8,21 @@ void Aggregation::Insert(State& state) { for (auto _ : state) { // NOLINT(*deadcode.DeadStores) for (size_t i = 0; i < N; ++i) { auto item = MakeItem(state); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } auto err = db_->Insert(nsdef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } auto err = db_->Commit(nsdef_.name); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void Aggregation::RegisterAllCases() { @@ -31,7 +37,9 @@ void Aggregation::RegisterAllCases() { reindexer::Error Aggregation::Initialize() { assertrx(db_); auto err = db_->AddNamespace(nsdef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } return {}; } @@ -53,7 +61,9 @@ reindexer::Item Aggregation::MakeItem(benchmark::State& state) { arr.End(); bld.End(); const auto err = item.FromJSON(wrSer_.Slice()); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } return item; } @@ -64,7 +74,9 @@ void Aggregation::Facet(benchmark::State& state) { q.Aggregate(AggFacet, {"int_data"}); reindexer::QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -75,7 +87,9 @@ void Aggregation::MultiFacet(benchmark::State& state) { q.Aggregate(AggFacet, {"int_data", "str_data"}); reindexer::QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -86,6 +100,8 @@ void Aggregation::ArrayFacet(benchmark::State& state) { q.Aggregate(AggFacet, {"int_array_data"}); reindexer::QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/api_encdec.cc b/cpp_src/gtests/bench/fixtures/api_encdec.cc index 710262027..337c5e628 100644 --- a/cpp_src/gtests/bench/fixtures/api_encdec.cc +++ b/cpp_src/gtests/bench/fixtures/api_encdec.cc @@ -65,11 +65,15 @@ reindexer::Error ApiEncDec::prepareBenchData() { .AddIndex("string_hash_array_index", "hash", "string", IndexOpts().Array()) .AddIndex("string_tree_array_index", "tree", "string", IndexOpts().Array()); auto err = db_->AddNamespace(nsDef); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } fieldsToExtract_.clear(); itemForCjsonBench_ = std::make_unique(db_->NewItem(nsName_)); - if (!itemForCjsonBench_->Status().ok()) return itemForCjsonBench_->Status(); + if (!itemForCjsonBench_->Status().ok()) { + return itemForCjsonBench_->Status(); + } reindexer::WrSerializer wser; reindexer::JsonBuilder bld(wser); constexpr size_t len = 10; @@ -146,10 +150,16 @@ reindexer::Error ApiEncDec::prepareBenchData() { } bld.End(); err = itemForCjsonBench_->FromJSON(wser.Slice()); - if (!err.ok()) return err; - if (!itemForCjsonBench_->Status().ok()) return itemForCjsonBench_->Status(); + if (!err.ok()) { + return err; + } + if (!itemForCjsonBench_->Status().ok()) { + return itemForCjsonBench_->Status(); + } err = db_->Insert(nsName_, *itemForCjsonBench_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } itemCJSON_ = itemForCjsonBench_->GetCJSON(); itemJSON_ = itemForCjsonBench_->GetJSON(); wser.Reset(); @@ -164,8 +174,12 @@ void ApiEncDec::FromCJSON(benchmark::State& state) { AllocsTracker allocsTracker(state); for (auto _ : state) { // NOLINT(*deadcode.DeadStores) const auto err = item.FromCJSON(itemCJSON_); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } } } @@ -175,8 +189,12 @@ void ApiEncDec::FromCJSONPKOnly(benchmark::State& state) { AllocsTracker allocsTracker(state); for (auto _ : state) { // NOLINT(*deadcode.DeadStores) const auto err = item.FromCJSON(itemCJSON_, true); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } } } assertrx(item["id"].Get() == kCjsonBenchItemID); @@ -198,7 +216,9 @@ void ApiEncDec::ExtractField(benchmark::State& state) { for (auto _ : state) { // NOLINT(*deadcode.DeadStores) const auto& fieldName = fieldsToExtract_[rand() % fieldsToExtract_.size()]; const auto va = VariantArray((*itemForCjsonBench_)[fieldName]); - if (va.size() != 1) state.SkipWithError(fmt::sprintf("Unexpected result size: %d", va.size()).c_str()); + if (va.size() != 1) { + state.SkipWithError(fmt::sprintf("Unexpected result size: %d", va.size()).c_str()); + } } } @@ -207,8 +227,12 @@ void ApiEncDec::FromJSON(benchmark::State& state) { AllocsTracker allocsTracker(state); for (auto _ : state) { // NOLINT(*deadcode.DeadStores) const auto err = item.FromJSON(itemJSON_); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } } } @@ -226,8 +250,12 @@ void ApiEncDec::FromPrettyJSON(benchmark::State& state) { AllocsTracker allocsTracker(state); for (auto _ : state) { // NOLINT(*deadcode.DeadStores) const auto err = item.FromJSON(itemPrettyJSON_); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } } } @@ -248,8 +276,12 @@ void ApiEncDec::FromMsgPack(benchmark::State& state) { for (auto _ : state) { // NOLINT(*deadcode.DeadStores) size_t offset = 0; const auto err = item.FromMsgPack(itemMsgPack_, offset); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/api_tv_composite.cc b/cpp_src/gtests/bench/fixtures/api_tv_composite.cc index dc131e080..0c365d7cb 100644 --- a/cpp_src/gtests/bench/fixtures/api_tv_composite.cc +++ b/cpp_src/gtests/bench/fixtures/api_tv_composite.cc @@ -11,7 +11,9 @@ using reindexer::Variant; reindexer::Error ApiTvComposite::Initialize() { auto err = BaseFixture::Initialize(); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } names_ = {"ox", "ant", "ape", "asp", "bat", "bee", "boa", "bug", "cat", "cod", "cow", "cub", "doe", "dog", "eel", "eft", "elf", "elk", "emu", "ewe", "fly", "fox", "gar", "gnu", "hen", "hog", "imp", "jay", @@ -127,9 +129,13 @@ void ApiTvComposite::GetByCompositePK(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -144,7 +150,9 @@ void ApiTvComposite::RangeTreeInt(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -157,7 +165,9 @@ void ApiTvComposite::RangeTreeStrCollateNumeric(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -172,9 +182,13 @@ void ApiTvComposite::RangeTreeDouble(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("empty qres"); + if (!qres.Count()) { + state.SkipWithError("empty qres"); + } } } @@ -192,7 +206,9 @@ void ApiTvComposite::RangeTreeCompositeIntInt(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -211,7 +227,9 @@ void ApiTvComposite::RangeTreeCompositeIntStr(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -225,7 +243,9 @@ void ApiTvComposite::RangeHashInt(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -240,7 +260,9 @@ void ApiTvComposite::RangeHashStringCollateASCII(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -256,7 +278,9 @@ void ApiTvComposite::RangeHashStringCollateUTF8(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -275,7 +299,9 @@ void ApiTvComposite::RangeHashCompositeIntInt(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -294,7 +320,9 @@ void ApiTvComposite::RangeHashCompositeIntStr(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -310,7 +338,9 @@ void ApiTvComposite::RangeTreeIntSortByHashInt(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -326,7 +356,9 @@ void ApiTvComposite::RangeTreeIntSortByTreeInt(State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -342,7 +374,9 @@ void ApiTvComposite::RangeTreeStrSortByHashInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -358,7 +392,9 @@ void ApiTvComposite::RangeTreeStrSortByTreeInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -375,7 +411,9 @@ void ApiTvComposite::RangeTreeDoubleSortByTreeInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -392,7 +430,9 @@ void ApiTvComposite::RangeTreeDoubleSortByHashInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -408,7 +448,9 @@ void ApiTvComposite::RangeTreeStrSortByHashStrCollateASCII(benchmark::State& sta QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -424,7 +466,9 @@ void ApiTvComposite::RangeTreeStrSortByHashStrCollateUTF8(benchmark::State& stat QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -438,7 +482,9 @@ void ApiTvComposite::SortByHashInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -452,7 +498,9 @@ void ApiTvComposite::ForcedSortByHashInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -466,7 +514,9 @@ void ApiTvComposite::ForcedSortWithSecondCondition(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -480,7 +530,9 @@ void ApiTvComposite::Query2CondIdSetComposite(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -494,7 +546,9 @@ void ApiTvComposite::SortByHashStrCollateASCII(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -508,7 +562,9 @@ void ApiTvComposite::SortByHashStrCollateUTF8(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -522,7 +578,9 @@ void ApiTvComposite::SortByHashCompositeIntInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -536,7 +594,9 @@ void ApiTvComposite::SortByHashCompositeIntStr(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -550,7 +610,9 @@ void ApiTvComposite::SortByTreeCompositeIntInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -564,6 +626,8 @@ void ApiTvComposite::SortByTreeCompositeIntStrCollateUTF8(benchmark::State& stat QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/api_tv_simple.cc b/cpp_src/gtests/bench/fixtures/api_tv_simple.cc index 032714d56..ad5219a47 100644 --- a/cpp_src/gtests/bench/fixtures/api_tv_simple.cc +++ b/cpp_src/gtests/bench/fixtures/api_tv_simple.cc @@ -105,7 +105,9 @@ reindexer::Error ApiTvSimple::Initialize() { assertrx(db_); auto err = db_->AddNamespace(nsdef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } countries_ = {"Portugal", "Afghanistan", @@ -175,31 +177,45 @@ reindexer::Error ApiTvSimple::Initialize() { .AddIndex("str_fuzzy_coll_num", "fuzzytext", "string", IndexOpts(0, CollateNumeric)); err = db_->AddNamespace(strNsDef); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } for (size_t i = 0; i < kTotalItemsStringSelectNs; ++i) { auto item = MakeStrItem(); - if (!item.Status().ok()) return item.Status(); + if (!item.Status().ok()) { + return item.Status(); + } err = db_->Insert(stringSelectNs_, item); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } err = db_->Commit(stringSelectNs_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } NamespaceDef mainNsDef{mainNs_}; mainNsDef.AddIndex("id", "hash", "int", IndexOpts().PK()).AddIndex("field", "hash", "int", IndexOpts()); err = db_->AddNamespace(mainNsDef); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } NamespaceDef rightNsDef{rightNs_}; rightNsDef.AddIndex("id", "hash", "int", IndexOpts().PK()) .AddIndex("field", "hash", "int", IndexOpts()) .AddIndex("id_tree", "tree", "int", IndexOpts()); err = db_->AddNamespace(rightNsDef); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } for (size_t i = 0; i < kTotalItemsMainJoinNs; ++i) { reindexer::Item mItem = db_->NewItem(mainNsDef.name); - if (!mItem.Status().ok()) return mItem.Status(); + if (!mItem.Status().ok()) { + return mItem.Status(); + } mItem.Unsafe(); wrSer_.Reset(); reindexer::JsonBuilder bld(wrSer_); @@ -207,12 +223,18 @@ reindexer::Error ApiTvSimple::Initialize() { bld.Put("field", i); bld.End(); err = mItem.FromJSON(wrSer_.Slice()); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } err = db_->Insert(mainNsDef.name, mItem); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } reindexer::Item rItem = db_->NewItem(rightNsDef.name); - if (!rItem.Status().ok()) return rItem.Status(); + if (!rItem.Status().ok()) { + return rItem.Status(); + } rItem.Unsafe(); wrSer_.Reset(); reindexer::JsonBuilder bld2(wrSer_); @@ -221,9 +243,13 @@ reindexer::Error ApiTvSimple::Initialize() { bld2.Put("id_tree", i); bld2.End(); err = rItem.FromJSON(wrSer_.Slice()); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } err = db_->Insert(rightNsDef.name, rItem); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } return {}; } @@ -257,7 +283,9 @@ reindexer::Item ApiTvSimple::MakeStrItem() { bld.Put("field_str", "value_" + idStr); bld.End(); const auto err = item.FromJSON(wrSer_.Slice()); - if (!err.ok()) assert(!item.Status().ok()); + if (!err.ok()) { + assert(!item.Status().ok()); + } } return item; } @@ -299,7 +327,9 @@ void ApiTvSimple::WarmUpIndexes(State& state) { Query q(nsdef_.name); q.Where("packages", CondSet, packages_.at(i % packages_.size())).Limit(20).Sort("start_time", false); err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } for (size_t i = 0; i < packages_.size() * 2; ++i) { @@ -307,7 +337,9 @@ void ApiTvSimple::WarmUpIndexes(State& state) { Query q(nsdef_.name); q.Where("packages", CondSet, packages_.at(i % packages_.size())).Limit(20).Sort("year", false); err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } for (size_t i = 0; i < priceIDs_.size() * 3; ++i) { @@ -315,7 +347,9 @@ void ApiTvSimple::WarmUpIndexes(State& state) { Query q(kJoinNamespace); q.Where("id", CondSet, priceIDs_.at(i % priceIDs_.size())).Limit(20); err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } } @@ -327,8 +361,12 @@ void ApiTvSimple::StringsSelect(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -340,9 +378,13 @@ void ApiTvSimple::GetByID(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -354,9 +396,13 @@ void ApiTvSimple::GetByIDInBrackets(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -368,9 +414,13 @@ void ApiTvSimple::GetEqInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -383,9 +433,13 @@ void ApiTvSimple::GetUuid(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -398,9 +452,13 @@ void ApiTvSimple::GetUuidStr(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -412,9 +470,13 @@ void ApiTvSimple::GetEqArrayInt(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -426,9 +488,13 @@ void ApiTvSimple::GetEqString(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -440,9 +506,13 @@ void ApiTvSimple::GetLikeString(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -456,9 +526,13 @@ void ApiTvSimple::GetByRangeIDAndSortByHash(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -472,9 +546,13 @@ void ApiTvSimple::GetByRangeIDAndSortByTree(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -486,7 +564,9 @@ void ApiTvSimple::Query1Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -498,7 +578,9 @@ void ApiTvSimple::Query1CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -510,7 +592,9 @@ void ApiTvSimple::Query1CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -528,7 +612,9 @@ void ApiTvSimple::Query2Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -540,7 +626,9 @@ void ApiTvSimple::Query2CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -552,7 +640,9 @@ void ApiTvSimple::Query2CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -572,7 +662,9 @@ void ApiTvSimple::Query2CondLeftJoin2Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -593,7 +685,9 @@ void ApiTvSimple::Query2CondLeftJoin2CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -614,7 +708,9 @@ void ApiTvSimple::Query2CondLeftJoin2CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -634,7 +730,9 @@ void ApiTvSimple::Query2CondLeftJoin3Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -655,7 +753,9 @@ void ApiTvSimple::Query2CondLeftJoin3CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -676,7 +776,9 @@ void ApiTvSimple::Query2CondLeftJoin3CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -700,7 +802,9 @@ void ApiTvSimple::Query0CondInnerJoinUnlimit(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } ++num; } } @@ -716,7 +820,9 @@ void ApiTvSimple::Query0CondInnerJoinUnlimitLowSelectivity(benchmark::State& sta QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -729,7 +835,9 @@ void ApiTvSimple::SubQueryEq(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -743,7 +851,9 @@ void ApiTvSimple::SubQuerySet(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -759,7 +869,9 @@ void ApiTvSimple::SubQueryAggregate(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -779,7 +891,9 @@ void ApiTvSimple::Query2CondInnerJoin2Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -799,7 +913,9 @@ void ApiTvSimple::Query2CondInnerJoin3Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -816,7 +932,9 @@ void ApiTvSimple::InnerJoinInjectConditionFromMain(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -830,7 +948,9 @@ void ApiTvSimple::InnerJoinRejectInjection(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -838,19 +958,25 @@ void ApiTvSimple::Query0CondInnerJoinPreResultStoreValues(benchmark::State& stat using reindexer::JoinedSelector; static const std::string rightNs = "rightNs"; static const std::vector leftNs = {"leftNs1", "leftNs2", "leftNs3", "leftNs4"}; - static constexpr char const* id = "id"; - static constexpr char const* data = "data"; + static constexpr const char* id = "id"; + static constexpr const char* data = "data"; static constexpr int maxDataValue = 10; static constexpr int maxRightNsRowCount = maxDataValue * (JoinedSelector::MaxIterationsForPreResultStoreValuesOptimization() - 1); static constexpr int maxLeftNsRowCount = 10000; const auto createNs = [this, &state](const std::string& ns) { reindexer::Error err = db_->OpenNamespace(ns); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } err = db_->AddIndex(ns, {id, "hash", "int", IndexOpts().PK()}); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } err = db_->AddIndex(ns, {data, "hash", "int", IndexOpts()}); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } }; const auto fill = [this, &state](const std::string& ns, int startId, int endId) { reindexer::Error err; @@ -859,10 +985,14 @@ void ApiTvSimple::Query0CondInnerJoinPreResultStoreValues(benchmark::State& stat item[id] = i; item[data] = i % maxDataValue; err = db_->Upsert(ns, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } err = db_->Commit(ns); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } }; createNs(rightNs); @@ -882,10 +1012,14 @@ void ApiTvSimple::Query0CondInnerJoinPreResultStoreValues(benchmark::State& stat QueryResults qres; reindexer::Error err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } }); } - for (auto& th : threads) th.join(); + for (auto& th : threads) { + th.join(); + } } } @@ -906,7 +1040,9 @@ void ApiTvSimple::Query2CondInnerJoin2CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -927,7 +1063,9 @@ void ApiTvSimple::Query2CondInnerJoin3CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -948,7 +1086,9 @@ void ApiTvSimple::Query2CondInnerJoin2CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -969,7 +1109,9 @@ void ApiTvSimple::Query2CondInnerJoin3CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -987,7 +1129,9 @@ void ApiTvSimple::Query3Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1006,7 +1150,9 @@ void ApiTvSimple::Query3CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1025,7 +1171,9 @@ void ApiTvSimple::Query3CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1041,7 +1189,9 @@ void ApiTvSimple::Query3CondKillIdsCache(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1059,7 +1209,9 @@ void ApiTvSimple::Query3CondRestoreIdsCache(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1078,7 +1230,9 @@ void ApiTvSimple::Query4Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1098,7 +1252,9 @@ void ApiTvSimple::Query4CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1118,7 +1274,9 @@ void ApiTvSimple::Query4CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1138,7 +1296,9 @@ void ApiTvSimple::Query4CondRange(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1159,7 +1319,9 @@ void ApiTvSimple::Query4CondRangeTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1180,7 +1342,9 @@ void ApiTvSimple::Query4CondRangeCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1202,7 +1366,9 @@ void ApiTvSimple::Query4CondRangeDropCache(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1225,7 +1391,9 @@ void ApiTvSimple::Query4CondRangeDropCacheTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1248,7 +1416,9 @@ void ApiTvSimple::Query4CondRangeDropCacheCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1261,6 +1431,8 @@ void ApiTvSimple::query2CondIdSet(benchmark::State& state, const std::vectorSelect(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/api_tv_simple_comparators.cc b/cpp_src/gtests/bench/fixtures/api_tv_simple_comparators.cc index 459ad9f17..f7da0f1b7 100644 --- a/cpp_src/gtests/bench/fixtures/api_tv_simple_comparators.cc +++ b/cpp_src/gtests/bench/fixtures/api_tv_simple_comparators.cc @@ -49,7 +49,9 @@ reindexer::Error ApiTvSimpleComparators::Initialize() { assertrx(db_); auto err = db_->AddNamespace(nsdef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } countries_ = {"Portugal", "Afghanistan", @@ -71,12 +73,18 @@ reindexer::Error ApiTvSimpleComparators::Initialize() { uuids_.emplace_back(randStrUuid()); } - for (int i = 0; i < 10; i++) packages_.emplace_back(randomNumArray(20, 10000, 10)); + for (int i = 0; i < 10; i++) { + packages_.emplace_back(randomNumArray(20, 10000, 10)); + } - for (int i = 0; i < 20; i++) priceIDs_.emplace_back(randomNumArray(10, 7000, 50)); + for (int i = 0; i < 20; i++) { + priceIDs_.emplace_back(randomNumArray(10, 7000, 50)); + } start_times_.resize(20); - for (int i = 0; i < 20; i++) start_times_[i] = random(0, 50000); + for (int i = 0; i < 20; i++) { + start_times_[i] = random(0, 50000); + } NamespaceDef strNsDef{stringSelectNs_}; strNsDef.AddIndex("id", "hash", "int", IndexOpts().PK()) @@ -86,13 +94,19 @@ reindexer::Error ApiTvSimpleComparators::Initialize() { .AddIndex("str_hash_coll_num", "-", "string", IndexOpts(0, CollateNumeric)); err = db_->AddNamespace(strNsDef); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } for (size_t i = 0; i < kTotalItemsStringSelectNs; ++i) { auto item = MakeStrItem(); - if (!item.Status().ok()) return item.Status(); + if (!item.Status().ok()) { + return item.Status(); + } err = db_->Insert(stringSelectNs_, item); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } err = db_->Commit(stringSelectNs_); return err; @@ -115,7 +129,9 @@ reindexer::Item ApiTvSimpleComparators::MakeStrItem() { bld.Put("field_str", "value_" + idStr); bld.End(); const auto err = item.FromJSON(wrSer_.Slice()); - if (!err.ok()) assert(!item.Status().ok()); + if (!err.ok()) { + assert(!item.Status().ok()); + } } return item; } @@ -160,8 +176,12 @@ void ApiTvSimpleComparators::StringsSelect(benchmark::State& state) { Query q(stringSelectNs_); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -172,8 +192,12 @@ void ApiTvSimpleComparators::GetEqInt(benchmark::State& state) { q.Where("start_time", CondEq, start_times_.at(random(0, start_times_.size() - 1))); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -184,8 +208,12 @@ void ApiTvSimpleComparators::GetEqArrayInt(benchmark::State& state) { q.Where("price_id", CondEq, priceIDs_[random(0, priceIDs_.size() - 1)]); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -196,8 +224,12 @@ void ApiTvSimpleComparators::GetEqString(benchmark::State& state) { q.Where("countries", CondEq, countries_[random(0, countries_.size() - 1)]); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -210,9 +242,13 @@ void ApiTvSimpleComparators::GetByRangeIDAndSort(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -224,8 +260,12 @@ void ApiTvSimpleComparators::GetUuidStr(benchmark::State& state) { q.Where("uuid_str", CondEq, uuid); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -237,7 +277,9 @@ void ApiTvSimpleComparators::Query1Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -249,7 +291,9 @@ void ApiTvSimpleComparators::Query1CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -261,7 +305,9 @@ void ApiTvSimpleComparators::Query1CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -273,7 +319,9 @@ void ApiTvSimpleComparators::Query2Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -285,7 +333,9 @@ void ApiTvSimpleComparators::Query2CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -297,7 +347,9 @@ void ApiTvSimpleComparators::Query2CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -314,7 +366,9 @@ void ApiTvSimpleComparators::Query3Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -332,7 +386,9 @@ void ApiTvSimpleComparators::Query3CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -350,7 +406,9 @@ void ApiTvSimpleComparators::Query3CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -366,7 +424,9 @@ void ApiTvSimpleComparators::Query3CondKillIdsCache(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -383,7 +443,9 @@ void ApiTvSimpleComparators::Query3CondRestoreIdsCache(benchmark::State& state) QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -401,7 +463,9 @@ void ApiTvSimpleComparators::Query4Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -420,7 +484,9 @@ void ApiTvSimpleComparators::Query4CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -439,7 +505,9 @@ void ApiTvSimpleComparators::Query4CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -458,7 +526,9 @@ void ApiTvSimpleComparators::Query4CondRange(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -478,7 +548,9 @@ void ApiTvSimpleComparators::Query4CondRangeTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -498,6 +570,8 @@ void ApiTvSimpleComparators::Query4CondRangeCachedTotal(benchmark::State& state) QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.cc b/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.cc index 404badf23..ae8a85533 100644 --- a/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.cc +++ b/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.cc @@ -55,7 +55,9 @@ reindexer::Error ApiTvSimpleSparse::Initialize() { assertrx(db_); auto err = db_->AddNamespace(nsdef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } countries_ = {"Portugal", "Afghanistan", @@ -139,7 +141,9 @@ void ApiTvSimpleSparse::WarmUpIndexes(State& state) { Query q(nsdef_.name); q.Where("packages", CondSet, packages_.at(i % packages_.size())).Limit(20).Sort("uuid", false); err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } for (size_t i = 0; i < packages_.size() * 2; ++i) { @@ -147,7 +151,9 @@ void ApiTvSimpleSparse::WarmUpIndexes(State& state) { Query q(nsdef_.name); q.Where("packages", CondSet, packages_.at(i % packages_.size())).Limit(20).Sort("countries", false); err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } for (size_t i = 0; i < priceIDs_.size() * 3; ++i) { @@ -155,7 +161,9 @@ void ApiTvSimpleSparse::WarmUpIndexes(State& state) { Query q(kJoinNamespace); q.Where("id", CondSet, priceIDs_.at(i % priceIDs_.size())).Limit(20); err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } } @@ -170,9 +178,13 @@ void ApiTvSimpleSparse::GetByRangeIDAndSortByHash(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -186,9 +198,13 @@ void ApiTvSimpleSparse::GetByRangeIDAndSortByTree(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } - if (!qres.Count()) state.SkipWithError("Results does not contain any value"); + if (!qres.Count()) { + state.SkipWithError("Results does not contain any value"); + } } } @@ -200,7 +216,9 @@ void ApiTvSimpleSparse::Query1Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -212,7 +230,9 @@ void ApiTvSimpleSparse::Query1CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -224,7 +244,9 @@ void ApiTvSimpleSparse::Query1CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -236,7 +258,9 @@ void ApiTvSimpleSparse::Query2Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -248,7 +272,9 @@ void ApiTvSimpleSparse::Query2CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -260,7 +286,9 @@ void ApiTvSimpleSparse::Query2CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -278,7 +306,9 @@ void ApiTvSimpleSparse::Query3Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -297,7 +327,9 @@ void ApiTvSimpleSparse::Query3CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -316,7 +348,9 @@ void ApiTvSimpleSparse::Query3CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -335,7 +369,9 @@ void ApiTvSimpleSparse::Query4Cond(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -355,7 +391,9 @@ void ApiTvSimpleSparse::Query4CondTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -375,7 +413,9 @@ void ApiTvSimpleSparse::Query4CondCachedTotal(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -394,7 +434,9 @@ void ApiTvSimpleSparse::QueryInnerJoinPreselectByValues(benchmark::State& state) QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -413,7 +455,9 @@ void ApiTvSimpleSparse::QueryInnerJoinNoPreselect(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -439,6 +483,8 @@ void ApiTvSimpleSparse::query4CondParameterizable(benchmark::State& state, std:: QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.h b/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.h index 05ebd9711..c4b9d2f36 100644 --- a/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.h +++ b/cpp_src/gtests/bench/fixtures/api_tv_simple_sparse.h @@ -20,7 +20,7 @@ class ApiTvSimpleSparse : private BaseFixture { .AddIndex("location", "hash", "string", IndexOpts().Sparse()) .AddIndex("end_time", "hash", "int", IndexOpts().Sparse()) .AddIndex("start_time", "tree", "int", IndexOpts().Sparse()) - .AddIndex("uuid", "hash", "uuid", IndexOpts()) // can't be sparse + .AddIndex("uuid", "hash", "uuid", IndexOpts()) // can't be sparse .AddIndex("uuid_str", "hash", "string", IndexOpts().Sparse()) .AddIndex("data10", "hash", "int", IndexOpts().Sparse()) .AddIndex("data33", "hash", "int", IndexOpts().Sparse()) diff --git a/cpp_src/gtests/bench/fixtures/base_fixture.cc b/cpp_src/gtests/bench/fixtures/base_fixture.cc index 303a6367e..a6d446f04 100644 --- a/cpp_src/gtests/bench/fixtures/base_fixture.cc +++ b/cpp_src/gtests/bench/fixtures/base_fixture.cc @@ -36,16 +36,22 @@ void BaseFixture::Insert(State& state) { for (auto _ : state) { // NOLINT(*deadcode.DeadStores) for (int i = 0; i < id_seq_->Count(); ++i) { auto item = MakeItem(state); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } auto err = db_->Insert(nsdef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } state.SetItemsProcessed(state.items_processed() + 1); } } auto err = db_->Commit(nsdef_.name); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void BaseFixture::Update(benchmark::State& state) { @@ -53,10 +59,14 @@ void BaseFixture::Update(benchmark::State& state) { id_seq_->Reset(); for (auto _ : state) { // NOLINT(*deadcode.DeadStores) auto item = MakeItem(state); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } auto err = db_->Update(nsdef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } if (item.GetID() < 0) { auto e = reindexer::Error(errConflict, "Item not exists [id = '%d']", item["id"].As()); @@ -65,7 +75,9 @@ void BaseFixture::Update(benchmark::State& state) { state.SetItemsProcessed(state.items_processed() + 1); } auto err = db_->Commit(nsdef_.name); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void BaseFixture::WaitForOptimization() { diff --git a/cpp_src/gtests/bench/fixtures/ft_fixture.cc b/cpp_src/gtests/bench/fixtures/ft_fixture.cc index 305d2ef94..4a14fdb14 100644 --- a/cpp_src/gtests/bench/fixtures/ft_fixture.cc +++ b/cpp_src/gtests/bench/fixtures/ft_fixture.cc @@ -58,19 +58,27 @@ void FullText::UpdateIndex(State& state) { q.Where(kFastIndexTextName_, CondEq, "lskfj"); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } reindexer::Error FullText::Initialize() { auto err = BaseFixture::Initialize(); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } err = db_->AddNamespace(lowWordsDiversityNsDef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } words_.reserve(140000); err = readDictFile(RX_BENCH_DICT_PATH, words_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } words2_ = {"стол", "столом", "столы", "cnjk", "stol", "бежит", "бегут", "бежали", ",tu", "beg", "дом", "доме", "ljv", "ракета", "ракетой", "ракеты", "hfrtn", "raketa", "летит", "летает", @@ -202,7 +210,9 @@ reindexer::Item FullText::MakeLowDiversityItem(int id) { for (size_t i = 0; i < wordCnt; i++) { r << words2_.at(randomGenerator_(randomEngine_, std::uniform_int_distribution::param_type{0, int(words2_.size() - 1)})); - if (i < wordCnt - 1) r << " "; + if (i < wordCnt - 1) { + r << " "; + } } return std::string(r.Slice()); }; @@ -221,7 +231,9 @@ reindexer::Item FullText::MakeItem(benchmark::State&) { auto phrase = CreatePhrase(); auto countries = GetRandomCountries(); raw_data_sz_ += phrase.size(); - for (auto& c : countries) raw_data_sz_ += c.size(); + for (auto& c : countries) { + raw_data_sz_ += c.size(); + } item["id"] = id_seq_->Next(); item["description"] = phrase; @@ -301,11 +313,15 @@ void FullText::Insert(State& state) { } auto err = db_->Insert(nsdef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } auto err = db_->Commit(nsdef_.name); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void FullText::BuildCommonIndexes(benchmark::State& state) { @@ -318,7 +334,9 @@ void FullText::BuildCommonIndexes(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -334,7 +352,9 @@ void FullText::BuildInsertLowDiversityNs(State& state) { } auto err = db_->Insert(lowWordsDiversityNsDef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } idCounter++; } @@ -342,7 +362,9 @@ void FullText::BuildInsertLowDiversityNs(State& state) { q.Where(kLowDiversityIndexName_, CondEq, words2_.at(0)).Limit(1); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void FullText::Fast3PhraseLowDiversity(State& state) { @@ -364,7 +386,9 @@ void FullText::Fast3PhraseLowDiversity(State& state) { auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -390,7 +414,9 @@ void FullText::Fast3WordsLowDiversity(State& state) { auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -414,7 +440,9 @@ void FullText::Fast2PhraseLowDiversity(State& state) { auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -438,7 +466,9 @@ void FullText::Fast2AndWordLowDiversity(State& state) { auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -465,7 +495,9 @@ void FullText::Fast3PhraseWithAreasLowDiversity(State& state) { q.AddFunction(hilightStr); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -484,7 +516,9 @@ void FullText::Fast1WordWithAreaHighDiversity(State& state) { q.AddFunction(hilightStr); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -510,7 +544,9 @@ void FullText::Fast3WordsWithAreasLowDiversity(State& state) { q.AddFunction(hilightStr); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -532,7 +568,9 @@ void FullText::BuildFastTextIndex(benchmark::State& state) { auto err = db_->Select(q, qres); mem = get_alloc_size() - mem; - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } double ratio = mem / double(raw_data_sz_); state.SetLabel("Commit ratio: " + std::to_string(ratio)); @@ -553,7 +591,9 @@ void FullText::BuildFuzzyTextIndex(benchmark::State& state) { auto err = db_->Select(q, qres); mem = get_alloc_size() - mem; - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } double ratio = mem / double(raw_data_sz_); state.SetLabel("Commit ratio: " + std::to_string(ratio)); @@ -574,7 +614,9 @@ void FullText::Fast1WordMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -594,7 +636,9 @@ void FullText::Fast2WordsMatch(benchmark::State& state) { q.Where(kFastIndexTextName_, CondEq, std::move(words)); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } @@ -612,7 +656,9 @@ void FullText::Fuzzy1WordMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -631,7 +677,9 @@ void FullText::Fuzzy2WordsMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -648,7 +696,9 @@ void FullText::Fast1PrefixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -665,7 +715,9 @@ void FullText::Fast2PrefixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -680,7 +732,9 @@ void FullText::Fuzzy1PrefixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -695,7 +749,9 @@ void FullText::Fuzzy2PrefixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -711,7 +767,9 @@ void FullText::Fast1SuffixMatch(benchmark::State& state) { q.Where(kFastIndexTextName_, CondEq, MakeSuffixWord()); QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -728,7 +786,9 @@ void FullText::Fast2SuffixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -743,7 +803,9 @@ void FullText::Fuzzy1SuffixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -758,7 +820,9 @@ void FullText::Fuzzy2SuffixMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -775,7 +839,9 @@ void FullText::Fast1TypoWordMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -792,7 +858,9 @@ void FullText::Fast2TypoWordMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -807,7 +875,9 @@ void FullText::Fuzzy1TypoWordMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -822,7 +892,9 @@ void FullText::Fuzzy2TypoWordMatch(benchmark::State& state) { QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } cnt += qres.Count(); } state.SetLabel(FormatString("RPR: %.1f", cnt / double(state.iterations()))); @@ -835,7 +907,9 @@ std::string FullText::CreatePhrase() { for (size_t i = 0; i < wordCnt; i++) { r << words_.at(randomGenerator_(randomEngine_, std::uniform_int_distribution::param_type{0, int(words_.size() - 1)})); - if (i < wordCnt - 1) r << " "; + if (i < wordCnt - 1) { + r << " "; + } } return std::string(r.Slice()); @@ -937,12 +1011,16 @@ void FullText::InitForAlternatingUpdatesAndSelects(State& state) { continue; } err = db_->Insert(alternatingNs_, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } auto err = db_->Commit(alternatingNs_); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } // Init index build assert(!values_.empty()); @@ -953,20 +1031,26 @@ void FullText::InitForAlternatingUpdatesAndSelects(State& state) { .search1); QueryResults qres; err = db_->Select(q1, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } size_t index = randomGenerator_(randomEngine_, std::uniform_int_distribution::param_type{0, int(values_.size() - 1)}); const Query q2 = Query(alternatingNs_).Where("search_comp", CondEq, values_[index].search1 + ' ' + values_[index].search2); qres.Clear(); err = db_->Select(q2, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } index = randomGenerator_(randomEngine_, std::uniform_int_distribution::param_type{0, int(values_.size() - 1)}); const Query q3 = Query(alternatingNs_).Where("search_comp_not_index_fields", CondEq, values_[index].field1 + ' ' + values_[index].field2); qres.Clear(); err = db_->Select(q3, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void FullText::updateAlternatingNs(reindexer::WrSerializer& ser, benchmark::State& state) { @@ -994,14 +1078,18 @@ void FullText::updateAlternatingNs(reindexer::WrSerializer& ser, benchmark::Stat return; } err = db_->Update(alternatingNs_, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } const std::string sql = "UPDATE "s + alternatingNs_ + " SET rand = " + std::to_string(rand()) + " WHERE id = " + std::to_string(randomGenerator_(randomEngine_, std::uniform_int_distribution::param_type{0, int(values_.size() - 1)})); QueryResults qres; err = db_->Select(sql, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } void FullText::AlternatingUpdatesAndSelects(benchmark::State& state) { @@ -1019,7 +1107,9 @@ void FullText::AlternatingUpdatesAndSelects(benchmark::State& state) { QueryResults qres; state.ResumeTiming(); const auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1035,7 +1125,9 @@ void FullText::AlternatingUpdatesAndSelectsByComposite(benchmark::State& state) QueryResults qres; state.ResumeTiming(); const auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -1051,14 +1143,18 @@ void FullText::AlternatingUpdatesAndSelectsByCompositeByNotIndexFields(benchmark QueryResults qres; state.ResumeTiming(); const auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } reindexer::Error FullText::readDictFile(const std::string& fileName, std::vector& words) { std::ifstream file; file.open(fileName); - if (!file) return reindexer::Error(errNotValid, "%s", strerror(errno)); + if (!file) { + return reindexer::Error(errNotValid, "%s", strerror(errno)); + } std::copy(std::istream_iterator(file), std::istream_iterator(), std::back_inserter(words)); return reindexer::Error(); } diff --git a/cpp_src/gtests/bench/fixtures/ft_fixture.h b/cpp_src/gtests/bench/fixtures/ft_fixture.h index 96cfba913..aae810c7f 100644 --- a/cpp_src/gtests/bench/fixtures/ft_fixture.h +++ b/cpp_src/gtests/bench/fixtures/ft_fixture.h @@ -146,8 +146,12 @@ class FullText : private BaseFixture { int tMin = INT32_MAX; int tMax = 0; for (auto v : timeOfTest_) { - if (v < tMin) tMin = v; - if (v > tMax) tMax = v; + if (v < tMin) { + tMin = v; + } + if (v > tMax) { + tMax = v; + } } std::vector gist; const int columnCount = 1000; @@ -168,7 +172,9 @@ class FullText : private BaseFixture { for (auto v : timeOfTest_) { averageTime += v; int indx = double(v - tMin) / dt; - if (indx >= columnCount) indx = columnCount - 1; + if (indx >= columnCount) { + indx = columnCount - 1; + } gist[indx]++; } averageTime /= timeOfTest_.size(); @@ -179,14 +185,18 @@ class FullText : private BaseFixture { fileOut << "\"data\":[" << std::endl; bool isFirst = true; for (auto v : gist) { - if (!isFirst) fileOut << "," << std::endl; + if (!isFirst) { + fileOut << "," << std::endl; + } fileOut << v; isFirst = false; } fileOut << "],\n \"raw_data\":[" << std::endl; isFirst = true; for (auto v : timeOfTest_) { - if (!isFirst) fileOut << "," << std::endl; + if (!isFirst) { + fileOut << "," << std::endl; + } fileOut << v; isFirst = false; } diff --git a/cpp_src/gtests/bench/fixtures/ft_merge_limit.cc b/cpp_src/gtests/bench/fixtures/ft_merge_limit.cc index ef7c78ec7..33f2daf89 100644 --- a/cpp_src/gtests/bench/fixtures/ft_merge_limit.cc +++ b/cpp_src/gtests/bench/fixtures/ft_merge_limit.cc @@ -110,7 +110,9 @@ void FullTextMergeLimit::Insert(State& state) { item["id"] = h; item[kFastIndexTextName_] = phrase; auto err = db_->Upsert(nsdef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } state.SetLabel("inserted " + std::to_string(id_seq_->Count()) + " documents"); @@ -124,7 +126,9 @@ void FullTextMergeLimit::BuildFastTextIndex(benchmark::State& state) { reindexer::QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -137,6 +141,8 @@ void FullTextMergeLimit::FastTextIndexSelect(benchmark::State& state, const std: reindexer::QueryResults qres; auto err = db_->Select(q, qres); state.SetLabel("select " + std::to_string(qres.Count()) + " documents"); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } diff --git a/cpp_src/gtests/bench/fixtures/geometry.cc b/cpp_src/gtests/bench/fixtures/geometry.cc index 455bfa000..ee917e484 100644 --- a/cpp_src/gtests/bench/fixtures/geometry.cc +++ b/cpp_src/gtests/bench/fixtures/geometry.cc @@ -14,15 +14,21 @@ void Geometry::Insert(State& state) { for (auto _ : state) { // NOLINT(*deadcode.DeadStores) for (size_t i = 0; i < N; ++i) { auto item = MakeItem(state); - if (!item.Status().ok()) state.SkipWithError(item.Status().what().c_str()); + if (!item.Status().ok()) { + state.SkipWithError(item.Status().what().c_str()); + } auto err = db_->Insert(nsdef_.name, item); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } auto err = db_->Commit(nsdef_.name); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } template @@ -33,7 +39,9 @@ void Geometry::GetDWithin(benchmark::State& state) { q.DWithin("point", reindexer::randPoint(kRange), kRange / N); reindexer::QueryResults qres; auto err = db_->Select(q, qres); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -46,10 +54,14 @@ void Geometry::Reset(State& state) { nsdef_.AddIndex("id", "hash", "int", IndexOpts().PK()).AddIndex("point", "rtree", "point", IndexOpts().RTreeType(rtreeType)); auto err = db_->DropNamespace(nsdef_.name); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } err = db_->AddNamespace(nsdef_); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } } } @@ -84,7 +96,9 @@ void Geometry::RegisterAllCases() { reindexer::Error Geometry::Initialize() { assertrx(db_); auto err = db_->AddNamespace(nsdef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } return {}; } @@ -101,7 +115,9 @@ reindexer::Item Geometry::MakeItem(benchmark::State& state) { bld.Array("point", {point.X(), point.Y()}); bld.End(); const auto err = item.FromJSON(wrSer_.Slice()); - if (!err.ok()) state.SkipWithError(err.what().c_str()); + if (!err.ok()) { + state.SkipWithError(err.what().c_str()); + } return item; } diff --git a/cpp_src/gtests/bench/fixtures/join_items.cc b/cpp_src/gtests/bench/fixtures/join_items.cc index 72a0f7de0..8bfc545d8 100644 --- a/cpp_src/gtests/bench/fixtures/join_items.cc +++ b/cpp_src/gtests/bench/fixtures/join_items.cc @@ -5,7 +5,9 @@ reindexer::Error JoinItems::Initialize() { assertrx(db_); auto err = db_->AddNamespace(nsdef_); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } adjectives_ = {"able", "above", "absolute", "balanced", "becoming", "beloved", "calm", "capable", "capital", "destined", "devoted", "direct", "enabled", "enabling", "endless", "factual", "fair", "faithful", "grand", "grateful", @@ -48,7 +50,9 @@ reindexer::Item JoinItems::MakeItem(benchmark::State&) { std::string JoinItems::randomString(const std::string& prefix) { std::string result; - if (!prefix.empty()) result += prefix + "_"; + if (!prefix.empty()) { + result += prefix + "_"; + } result += adjectives_.at(random(0, adjectives_.size() - 1)); result += "_"; result += names_.at(random(0, names_.size() - 1)); diff --git a/cpp_src/gtests/bench/ft_bench.cc b/cpp_src/gtests/bench/ft_bench.cc index 719a77093..c0bdb667d 100644 --- a/cpp_src/gtests/bench/ft_bench.cc +++ b/cpp_src/gtests/bench/ft_bench.cc @@ -32,12 +32,16 @@ int main(int argc, char** argv) { shared_ptr DB = std::make_shared(); auto err = DB->Connect("builtin://" + kStoragePath); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } FullText ft(DB.get(), "fulltext", kItemsInBenchDataset); err = ft.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } ::benchmark::Initialize(&argc, argv); std::optional slowIterationCount; diff --git a/cpp_src/gtests/bench/reindexer_bench.cc b/cpp_src/gtests/bench/reindexer_bench.cc index 065d04070..669055241 100644 --- a/cpp_src/gtests/bench/reindexer_bench.cc +++ b/cpp_src/gtests/bench/reindexer_bench.cc @@ -42,7 +42,9 @@ int main(int argc, char** argv) { shared_ptr DB = std::make_shared(); auto err = DB->Connect("builtin://" + kStoragePath); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } JoinItems joinItems(DB.get(), 50'000); ApiTvSimple apiTvSimple(DB.get(), "ApiTvSimple", kItemsInBenchDataset); @@ -54,31 +56,49 @@ int main(int argc, char** argv) { ApiEncDec decoding(DB.get(), "EncDec"); err = apiTvSimple.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = apiTvSimpleComparators.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = apiTvSimpleSparse.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = joinItems.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = apiTvComposite.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = geometry.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = aggregation.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } err = decoding.Initialize(); - if (!err.ok()) return err.code(); + if (!err.ok()) { + return err.code(); + } ::benchmark::Initialize(&argc, argv); - if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; + if (::benchmark::ReportUnrecognizedArguments(argc, argv)) { + return 1; + } joinItems.RegisterAllCases(); apiTvSimple.RegisterAllCases(); diff --git a/cpp_src/gtests/bench/tools/helpers.cc b/cpp_src/gtests/bench/tools/helpers.cc index 216c79f49..262e819e5 100644 --- a/cpp_src/gtests/bench/tools/helpers.cc +++ b/cpp_src/gtests/bench/tools/helpers.cc @@ -7,7 +7,9 @@ std::string HumanReadableNumber(size_t number, bool si, const std::string& unitL const std::string prefix = "KMGTPE"; size_t unit = si ? 1000 : 1024; - if (number < unit) return std::to_string(number) + " " + unitLabel; + if (number < unit) { + return std::to_string(number) + " " + unitLabel; + } int exp = static_cast(std::log(number) / std::log(unit)); std::string pre; @@ -38,11 +40,11 @@ std::string FormatString(const char* msg, va_list args) { va_end(args_cp); - if (ret == 0) // handle empty expansion + if (ret == 0) { // handle empty expansion return {}; - else if (static_cast(ret) < size) + } else if (static_cast(ret) < size) { return local_buff; - else { + } else { // we did not provide a long enough buffer on our first attempt. size = static_cast(ret) + 1; // + 1 for the null byte std::unique_ptr buff(new char[size]); diff --git a/cpp_src/gtests/bench/tools/helpers.h b/cpp_src/gtests/bench/tools/helpers.h index 061773258..b153bf54e 100644 --- a/cpp_src/gtests/bench/tools/helpers.h +++ b/cpp_src/gtests/bench/tools/helpers.h @@ -21,7 +21,9 @@ struct to_array_helper { static VariantArray to_array(const std::vector& vec) { VariantArray krs; krs.reserve(vec.size()); - for (auto& value : vec) krs.push_back(Variant{value}); + for (auto& value : vec) { + krs.push_back(Variant{value}); + } return krs; } }; @@ -31,7 +33,9 @@ struct to_array_helper { static VariantArray to_array(const std::vector& vec) { VariantArray krs; krs.reserve(vec.size()); - for (auto& value : vec) krs.push_back(Variant{p_string(value.c_str())}); + for (auto& value : vec) { + krs.push_back(Variant{p_string(value.c_str())}); + } return krs; } }; @@ -41,7 +45,9 @@ struct to_array_helper { static inline std::string randString(size_t size) { constexpr static std::string_view ch{"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"}; std::string ret(size, ' '); - for (size_t i = 0; i < size; ++i) ret[i] = ch[rand() % ch.size()]; + for (size_t i = 0; i < size; ++i) { + ret[i] = ch[rand() % ch.size()]; + } return ret; } @@ -112,7 +118,9 @@ template std::vector randomNumArray(int count, int start, int region) { std::vector result; result.reserve(count); - for (int i = 0; i < count; i++) result.emplace_back(random(start, start + region)); + for (int i = 0; i < count; i++) { + result.emplace_back(random(start, start + region)); + } return result; } diff --git a/cpp_src/gtests/bench/tools/reporter.h b/cpp_src/gtests/bench/tools/reporter.h index 88e4596f5..753d9b68b 100644 --- a/cpp_src/gtests/bench/tools/reporter.h +++ b/cpp_src/gtests/bench/tools/reporter.h @@ -16,7 +16,7 @@ class Reporter : public ConsoleReporter { std::string str = FormatString("%-*s %13s %13s", static_cast(name_field_width_), "Benchmark", "Time", "RPS"); if (!run.counters.empty()) { if (output_options_ & OO_Tabular) { - for (auto const& c : run.counters) { + for (const auto& c : run.counters) { if (std::string_view(c.first) == std::string_view("bytes_per_second") || std::string_view(c.first) == std::string_view("items_per_second")) { continue; @@ -60,7 +60,7 @@ class Reporter : public ConsoleReporter { continue; } const std::size_t cNameLen = std::max(std::string::size_type(10), c.first.length()); - auto const& s = HumanReadableNumber(c.second.value, true); + const auto& s = HumanReadableNumber(c.second.value, true); if (output_options_ & OO_Tabular) { if (c.second.flags & Counter::kIsRate) { IgnoreColorPrint(Out, " %*s/s", cNameLen - 2, s.c_str()); diff --git a/cpp_src/gtests/bench/tools/sequence.h b/cpp_src/gtests/bench/tools/sequence.h index 02f9d0331..8a0313884 100644 --- a/cpp_src/gtests/bench/tools/sequence.h +++ b/cpp_src/gtests/bench/tools/sequence.h @@ -37,7 +37,9 @@ class SequenceBase { value_type Next() { value_type val; - if ((val = counter_++) >= end_) return end_; + if ((val = counter_++) >= end_) { + return end_; + } return val; } diff --git a/cpp_src/gtests/tests/API/api.cc b/cpp_src/gtests/tests/API/api.cc index 013ecd2d1..517955efd 100644 --- a/cpp_src/gtests/tests/API/api.cc +++ b/cpp_src/gtests/tests/API/api.cc @@ -4,14 +4,14 @@ #include "tools/assertrx.h" #include "tools/fsops.h" -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { srand(time(NULL)); ::testing::InitGoogleTest(&argc, argv); reindexer::debug::backtrace_init(); #ifndef _WIN32 - const char *tmpDir = getenv("REINDEXER_TEST_DB_ROOT"); + const char* tmpDir = getenv("REINDEXER_TEST_DB_ROOT"); if (tmpDir && *tmpDir) { reindexer::fs::SetTempDir(std::string(tmpDir)); } diff --git a/cpp_src/gtests/tests/API/base_tests.cc b/cpp_src/gtests/tests/API/base_tests.cc index 4a4b16413..95440c435 100644 --- a/cpp_src/gtests/tests/API/base_tests.cc +++ b/cpp_src/gtests/tests/API/base_tests.cc @@ -824,8 +824,12 @@ TEST_F(ReindexerApi, SortByMultipleColumns) { err = rt.reindexer->Upsert(default_namespace, item); ASSERT_TRUE(err.ok()) << err.what(); - if (i % 5 == 0) sameOldValue += 5; - if (i % 3 == 0) ++stringValuedIdx; + if (i % 5 == 0) { + sameOldValue += 5; + } + if (i % 3 == 0) { + ++stringValuedIdx; + } stringValuedIdx %= possibleValues.size(); } @@ -857,11 +861,12 @@ TEST_F(ReindexerApi, SortByMultipleColumns) { cmpRes[j] = lastValues[j].Compare(sortedValue); bool needToVerify = true; if (j != 0) { - for (int k = j - 1; k >= 0; --k) + for (int k = j - 1; k >= 0; --k) { if (cmpRes[k] != reindexer::ComparationResult::Eq) { needToVerify = false; break; } + } } needToVerify = (j == 0) || needToVerify; if (needToVerify) { diff --git a/cpp_src/gtests/tests/fixtures/btree_idsets_api.h b/cpp_src/gtests/tests/fixtures/btree_idsets_api.h index 935a86585..df5482436 100644 --- a/cpp_src/gtests/tests/fixtures/btree_idsets_api.h +++ b/cpp_src/gtests/tests/fixtures/btree_idsets_api.h @@ -39,8 +39,12 @@ class BtreeIdsetsApi : public ReindexerApi { Upsert(default_namespace, item); EXPECT_TRUE(item.Status().ok()) << item.Status().what(); - if (i % 100 == 0) currStrValue = RandString(); - if (i % 200 == 0) currIntValue = rand() % 100000; + if (i % 100 == 0) { + currStrValue = RandString(); + } + if (i % 200 == 0) { + currIntValue = rand() % 100000; + } } lastStrValue = currStrValue; @@ -61,7 +65,9 @@ class BtreeIdsetsApi : public ReindexerApi { Upsert(joinedNsName, item); EXPECT_TRUE(item.Status().ok()) << item.Status().what(); - if (i % 300) currValue = rand() % 10000; + if (i % 300) { + currValue = rand() % 10000; + } } Commit(joinedNsName); } diff --git a/cpp_src/gtests/tests/fixtures/equalpositionapi.h b/cpp_src/gtests/tests/fixtures/equalpositionapi.h index a2e3175df..8fe3ce528 100644 --- a/cpp_src/gtests/tests/fixtures/equalpositionapi.h +++ b/cpp_src/gtests/tests/fixtures/equalpositionapi.h @@ -43,7 +43,9 @@ class EqualPositionApi : public ReindexerApi { vec.reserve(length); for (size_t i = 0; i < static_cast(length); ++i) { vec.emplace_back(val); - if (i % multipleCond) val += initVal; + if (i % multipleCond) { + val += initVal; + } } return vec; } diff --git a/cpp_src/gtests/tests/fixtures/ft_api.cc b/cpp_src/gtests/tests/fixtures/ft_api.cc index b3046db17..8db2d3aff 100644 --- a/cpp_src/gtests/tests/fixtures/ft_api.cc +++ b/cpp_src/gtests/tests/fixtures/ft_api.cc @@ -232,7 +232,9 @@ std::vector FTApi::CreateAllPermutatedQueries(const std::string& qu result.push_back(queryStart); std::string& query = result.back(); for (auto it = indexedWords.cbegin(); it != indexedWords.cend(); ++it) { - if (it != indexedWords.cbegin()) query += sep; + if (it != indexedWords.cbegin()) { + query += sep; + } query += it->second; } query += queryEnd; diff --git a/cpp_src/gtests/tests/fixtures/fuzzing/index.cc b/cpp_src/gtests/tests/fixtures/fuzzing/index.cc index 17f7f9c59..1dd3d3888 100644 --- a/cpp_src/gtests/tests/fixtures/fuzzing/index.cc +++ b/cpp_src/gtests/tests/fixtures/fuzzing/index.cc @@ -47,45 +47,79 @@ reindexer::IndexDef Index::IndexDef(RandomGenerator& rnd, const NsScheme& scheme } void Index::Dump(std::ostream& os, const NsScheme& scheme, size_t offset) const { - for (size_t i = 0; i < offset; ++i) os << " "; + for (size_t i = 0; i < offset; ++i) { + os << " "; + } os << "{\n"; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "name: " << name_ << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "type: " << type_ << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "pk: " << std::boolalpha << isPk_ << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "array: " << std::boolalpha << IsArray() << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "sparse: " << std::boolalpha << (IsSparse() == IsSparseT::Yes) << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } std::visit(reindexer::overloaded{[&](const Child& child) { os << "composite: false\n"; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "field: {\n"; - for (size_t i = 0; i < offset + 2; ++i) os << " "; + for (size_t i = 0; i < offset + 2; ++i) { + os << " "; + } os << "type: " << child.type << '\n'; - for (size_t i = 0; i < offset + 2; ++i) os << " "; + for (size_t i = 0; i < offset + 2; ++i) { + os << " "; + } os << "json: " << scheme.GetJsonPath(child.fieldPath) << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "}\n"; }, [&](const Children& children) { os << "composite: true\n"; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "fields: [\n"; for (const auto& c : children) { - for (size_t i = 0; i < offset + 2; ++i) os << " "; + for (size_t i = 0; i < offset + 2; ++i) { + os << " "; + } os << "{\n"; - for (size_t i = 0; i <= offset + 2; ++i) os << " "; + for (size_t i = 0; i <= offset + 2; ++i) { + os << " "; + } os << "type: " << c.type << '\n'; - for (size_t i = 0; i <= offset + 2; ++i) os << " "; + for (size_t i = 0; i <= offset + 2; ++i) { + os << " "; + } os << "json: " << scheme.GetJsonPath(c.fieldPath) << '\n'; - for (size_t i = 0; i < offset + 2; ++i) os << " "; + for (size_t i = 0; i < offset + 2; ++i) { + os << " "; + } os << "}\n"; } - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "]\n"; }}, content_); diff --git a/cpp_src/gtests/tests/fixtures/fuzzing/ns.cc b/cpp_src/gtests/tests/fixtures/fuzzing/ns.cc index 831653a84..18e2fea0e 100644 --- a/cpp_src/gtests/tests/fixtures/fuzzing/ns.cc +++ b/cpp_src/gtests/tests/fixtures/fuzzing/ns.cc @@ -8,7 +8,9 @@ struct FieldPathHash { size_t operator()(const fuzzing::FieldPath& fp) const noexcept { constexpr static std::hash hasher; size_t ret = fp.size(); - for (const size_t f : fp) ret = ((ret * 127) ^ (ret >> 3)) ^ hasher(f); + for (const size_t f : fp) { + ret = ((ret * 127) ^ (ret >> 3)) ^ hasher(f); + } return ret; } }; @@ -84,7 +86,9 @@ Ns::Ns(std::string name, RandomGenerator::ErrFactorType errorFactor) } } if (!uniqueName) { - if (!name.empty()) name += '+'; + if (!name.empty()) { + name += '+'; + } name += scheme_.GetJsonPath(fieldData.fieldPath); } containsUuid |= fieldData.type == FieldType::Uuid; @@ -107,10 +111,14 @@ Ns::Ns(std::string name, RandomGenerator::ErrFactorType errorFactor) do { fldPath = rndGen_.RndField(scheme_); } while (!withErr && ++tryCounts < kMaxTries && usedPaths.find(fldPath) != usedPaths.end()); - if (tryCounts >= kMaxTries) continue; + if (tryCounts >= kMaxTries) { + continue; + } usedPaths.insert(fldPath); if (scheme_.IsStruct(fldPath)) { - if (!rndGen_.RndErr()) continue; + if (!rndGen_.RndErr()) { + continue; + } const auto fldType = rndGen_.RndFieldType(); indexes_.emplace_back(rndGen_.IndexName(usedIndexNames), rndGen_.RndIndexType({fldType}), rndGen_.RndBool(0.5) ? IsArrayT::Yes : IsArrayT::No, @@ -185,7 +193,9 @@ void Ns::Dump(std::ostream& os) const { os << "{\n"; scheme_.Dump(os, 1); os << " indexes: [\n"; - for (const auto& i : indexes_) i.Dump(os, scheme_, 2); + for (const auto& i : indexes_) { + i.Dump(os, scheme_, 2); + } os << " ]\n}" << std::endl; } diff --git a/cpp_src/gtests/tests/fixtures/fuzzing/ns_scheme.cc b/cpp_src/gtests/tests/fixtures/fuzzing/ns_scheme.cc index 0a1c92829..24f33278e 100644 --- a/cpp_src/gtests/tests/fixtures/fuzzing/ns_scheme.cc +++ b/cpp_src/gtests/tests/fixtures/fuzzing/ns_scheme.cc @@ -29,14 +29,18 @@ void NsScheme::NewItem(reindexer::WrSerializer& ser, RandomGenerator& rnd, const } bool NsScheme::IsStruct(const FieldPath& path) const noexcept { - if (path.empty()) return true; + if (path.empty()) { + return true; + } const Node::Children& ref = findLastContainer(path); assertrx(ref.size() > path.back()); return std::holds_alternative(ref[path.back()].content); } bool NsScheme::IsPoint(const FieldPath& path) const noexcept { - if (path.empty()) return false; + if (path.empty()) { + return false; + } const Node::Children& ref = findLastContainer(path); assertrx(ref.size() > path.back()); return !std::holds_alternative(ref[path.back()].content) && @@ -54,7 +58,9 @@ bool NsScheme::isTtl(const std::vector& idxNumbers, const std::vector& indexes) const noexcept { - if (path.empty()) return false; + if (path.empty()) { + return false; + } const Node::Children& ref = findLastContainer(path); assertrx(ref.size() > path.back()); if (std::holds_alternative(ref[path.back()].content)) { @@ -78,12 +84,16 @@ size_t NsScheme::FieldsCount(const FieldPath& path) const noexcept { } IsArrayT NsScheme::IsArray(const FieldPath& path) const noexcept { - if (path.empty()) return ns_.array; + if (path.empty()) { + return ns_.array; + } const Node::Children* ptr = &std::get(ns_.content); for (size_t i = 0, s = path.size() - 1; i < s; ++i) { assertrx(ptr->size() > path[i]); const auto& idx = (*ptr)[path[i]]; - if (idx.array == IsArrayT::Yes) return IsArrayT::Yes; + if (idx.array == IsArrayT::Yes) { + return IsArrayT::Yes; + } std::visit( reindexer::overloaded{[&ptr](const Node::Children& c) noexcept { ptr = &c; }, [](const Node::Child&) noexcept { assertrx(0); }}, idx.content); @@ -110,7 +120,9 @@ void NsScheme::SetFieldType(const FieldPath& path, FieldType ft) noexcept { } std::string NsScheme::GetJsonPath(const FieldPath& path) const noexcept { - if (path.empty()) return {}; + if (path.empty()) { + return {}; + } std::string res; const Node::Children* ptr = &std::get(ns_.content); for (size_t i = 0, s = path.size() - 1; i < s; ++i) { @@ -255,7 +267,9 @@ void NsScheme::rndValueToJson(reindexer::JsonBuilder& builder, FieldType ft, std void NsScheme::toJson(reindexer::JsonBuilder& builder, const Node::Children& children, RandomGenerator& rnd, const std::vector& indexes) { for (const Node& n : children) { - if (!rnd.NeedThisNode(n.sparse)) continue; + if (!rnd.NeedThisNode(n.sparse)) { + continue; + } if (rnd.RndArrayField(n.array) == IsArrayT::Yes) { auto arr = builder.Array(n.name); const size_t arrSize = rnd.ArraySize(); @@ -289,27 +303,45 @@ void NsScheme::toJson(reindexer::JsonBuilder& builder, const Node::Children& chi } void NsScheme::Node::Dump(std::ostream& os, size_t offset) const { - for (size_t i = 0; i < offset; ++i) os << " "; + for (size_t i = 0; i < offset; ++i) { + os << " "; + } os << "{\n"; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "name: " << name << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "sparse: " << std::boolalpha << (sparse == IsSparseT::Yes) << '\n'; - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "array: " << std::boolalpha << (array == IsArrayT::Yes) << '\n'; std::visit(reindexer::overloaded{[&](const Child& child) { - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "type: " << child.type << '\n'; }, [&](const Children& children) { - for (size_t i = 0; i <= offset; ++i) os << " "; + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "fields: [\n"; - for (const Node& n : children) n.Dump(os, offset + 2); - for (size_t i = 0; i <= offset; ++i) os << " "; + for (const Node& n : children) { + n.Dump(os, offset + 2); + } + for (size_t i = 0; i <= offset; ++i) { + os << " "; + } os << "]\n"; }}, content); - for (size_t i = 0; i < offset; ++i) os << " "; + for (size_t i = 0; i < offset; ++i) { + os << " "; + } os << "}\n"; } diff --git a/cpp_src/gtests/tests/fixtures/fuzzing/query_generator.cc b/cpp_src/gtests/tests/fixtures/fuzzing/query_generator.cc index da0f1d081..f796ccc4e 100644 --- a/cpp_src/gtests/tests/fixtures/fuzzing/query_generator.cc +++ b/cpp_src/gtests/tests/fixtures/fuzzing/query_generator.cc @@ -20,7 +20,9 @@ reindexer::Query QueryGenerator::operator()() { [&](const Index::Children& c) { std::vector types; types.reserve(c.size()); - for (const auto& child : c) types.push_back(child.type); + for (const auto& child : c) { + types.push_back(child.type); + } rndGen_.RndWhereComposite(query, idx.Name(), std::move(types), idx.Type()); }}, idx.Content()); diff --git a/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.cc b/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.cc index 17fcd70b9..fc429300e 100644 --- a/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.cc +++ b/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.cc @@ -137,7 +137,9 @@ std::string RandomGenerator::FieldName(std::unordered_set& generate case Printable: for (size_t i = 0; i < len;) { res[i] = rndChar_(gen_); - if (true) ++i; // TODO + if (true) { + ++i; // TODO + } } break; case Available: @@ -199,7 +201,9 @@ FieldPath RandomGenerator::RndField(const NsScheme& nsScheme) { } } const auto size = nsScheme.FieldsCount(res); - if (size == 0) return res; + if (size == 0) { + return res; + } const int idx = RndInt(0, size - 1); res.push_back(idx); } while (nsScheme.IsStruct(res)); @@ -222,16 +226,22 @@ FieldPath RandomGenerator::RndScalarField(const NsScheme& nsScheme) { } } const auto size = nsScheme.FieldsCount(res); - if (size == 0) return res; + if (size == 0) { + return res; + } int idx = RndInt(0, size - 1); res.push_back(idx); const int end = idx + size; while (idx < end) { res.back() = idx % size; - if (nsScheme.IsArray(res) == IsArrayT::No && !nsScheme.IsPoint(res)) break; + if (nsScheme.IsArray(res) == IsArrayT::No && !nsScheme.IsPoint(res)) { + break; + } ++idx; } - if (idx == end) return {}; + if (idx == end) { + return {}; + } } while (nsScheme.IsStruct(res)); return res; } @@ -311,7 +321,9 @@ IndexType RandomGenerator::RndPkIndexType(const std::vector& fieldTyp } size_t RandomGenerator::ArraySize() { - if (RndErr()) return RndInt(0, 100'000); + if (RndErr()) { + return RndInt(0, 100'000); + } enum Size : uint8_t { Short, Normal, Long, VeryLong, END = VeryLong }; switch (RndWhich()) { case Short: diff --git a/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.h b/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.h index bebb70490..f4819ae62 100644 --- a/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.h +++ b/cpp_src/gtests/tests/fixtures/fuzzing/random_generator.h @@ -83,7 +83,9 @@ class RandomGenerator { std::vector RndFieldsForCompositeIndex(const std::vector& scalarIndexes); std::string FieldName(std::unordered_set& generatedNames); std::string FieldName(const std::string& fieldName, std::unordered_set& generatedNames) { - if (RndErr()) return FieldName(generatedNames); + if (RndErr()) { + return FieldName(generatedNames); + } return fieldName; } FieldPath RndField(const NsScheme&); @@ -94,7 +96,9 @@ class RandomGenerator { return FieldName(generatedNames); } std::string NsName(const std::string& nsName) { - if (RndErr()) return GenerateNsName(); + if (RndErr()) { + return GenerateNsName(); + } return nsName; } int RndInt(int min, int max) { return rndInt_(gen_, IntRndParams(min, max)); } @@ -198,7 +202,9 @@ class RandomGenerator { assertrx(false); std::abort(); } - for (char& ch : res) ch = availableChars[rndInt(availableRndParams)]; // TODO + for (char& ch : res) { + ch = availableChars[rndInt(availableRndParams)]; // TODO + } return res; } std::string RndStrUuidValue() { return rndStrUuidValue(false); } diff --git a/cpp_src/gtests/tests/fixtures/get_pk_api.h b/cpp_src/gtests/tests/fixtures/get_pk_api.h index 29e7ad876..703a729eb 100644 --- a/cpp_src/gtests/tests/fixtures/get_pk_api.h +++ b/cpp_src/gtests/tests/fixtures/get_pk_api.h @@ -42,11 +42,15 @@ class ExtractPK : public testing::Test { public: Error CreateNamespace(const NamespaceDef& nsDef) { Error err = db_->OpenNamespace(nsDef.name); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } for (const auto& index : nsDef.indexes) { err = db_->AddIndex(nsDef.name, index); - if (!err.ok()) break; + if (!err.ok()) { + break; + } } return err; @@ -54,7 +58,9 @@ class ExtractPK : public testing::Test { Error UpsertAndCommit(const string& ns, Item& item) { Error err = db_->Upsert(ns, item); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } return db_->Commit(ns); } @@ -63,7 +69,9 @@ class ExtractPK : public testing::Test { typedef std::tuple ResultType; Item item = db_->NewItem(ns); - if (!item.Status().ok()) return ResultType(item.Status(), std::move(item), Data{0, 0, nullptr, nullptr, 0, 0}); + if (!item.Status().ok()) { + return ResultType(item.Status(), std::move(item), Data{0, 0, nullptr, nullptr, 0, 0}); + } Data data = (d == nullptr) ? randomItemData() : *d; std::string json = StringFormat(jsonPattern, data.id, data.name, data.color, data.weight, data.height, data.fk_id); @@ -88,9 +96,13 @@ class ExtractPK : public testing::Test { QueryResults qres; Error err = db_->Select(query, qres); - if (!err.ok()) return ResultType(err, QueryResults{}); + if (!err.ok()) { + return ResultType(err, QueryResults{}); + } - if (print) printQueryResults(query.NsName(), qres); + if (print) { + printQueryResults(query.NsName(), qres); + } return ResultType(err, std::move(qres)); } diff --git a/cpp_src/gtests/tests/fixtures/grpcclient_api.h b/cpp_src/gtests/tests/fixtures/grpcclient_api.h index d8ce17632..fc839a091 100644 --- a/cpp_src/gtests/tests/fixtures/grpcclient_api.h +++ b/cpp_src/gtests/tests/fixtures/grpcclient_api.h @@ -10,8 +10,8 @@ #include "core/cjson/cjsondecoder.h" #include "core/cjson/jsonbuilder.h" #include "core/payload/payloadiface.h" -#include "server/server.h" #include "reindexer_api.h" +#include "server/server.h" #include "tools/fsops.h" #include "yaml-cpp/yaml.h" diff --git a/cpp_src/gtests/tests/fixtures/item_move_semantics_api.h b/cpp_src/gtests/tests/fixtures/item_move_semantics_api.h index ba10e1784..e60acac2f 100644 --- a/cpp_src/gtests/tests/fixtures/item_move_semantics_api.h +++ b/cpp_src/gtests/tests/fixtures/item_move_semantics_api.h @@ -10,7 +10,7 @@ class ItemMoveSemanticsApi : public ReindexerApi { protected: const std::string pkField = "bookid"; const int32_t itemsCount = 100000; - const char *jsonPattern = "{\"bookid\":%d,\"title\":\"title\",\"pages\":200,\"price\":299,\"genreid_fk\":3,\"authorid_fk\":10}"; + const char* jsonPattern = "{\"bookid\":%d,\"title\":\"title\",\"pages\":200,\"price\":299,\"genreid_fk\":3,\"authorid_fk\":10}"; std::map items_; void SetUp() override { @@ -47,8 +47,8 @@ class ItemMoveSemanticsApi : public ReindexerApi { } void verifyAndUpsertItems() { - for (auto &pair : items_) { - auto &&item = pair.second; + for (auto& pair : items_) { + auto&& item = pair.second; Error err = rt.reindexer->Upsert(default_namespace, item); ASSERT_TRUE(err.ok()) << err.what(); ASSERT_NO_THROW(gason::JsonParser().Parse(item.GetJSON())); @@ -62,7 +62,7 @@ class ItemMoveSemanticsApi : public ReindexerApi { if (itItem == items_.end()) { return Item(); } - auto &&item = itItem->second; + auto&& item = itItem->second; return std::move(item); } diff --git a/cpp_src/gtests/tests/fixtures/join_selects_api.h b/cpp_src/gtests/tests/fixtures/join_selects_api.h index d4be1cd89..b58ed111d 100644 --- a/cpp_src/gtests/tests/fixtures/join_selects_api.h +++ b/cpp_src/gtests/tests/fixtures/join_selects_api.h @@ -154,7 +154,9 @@ class JoinSelectsApi : public ReindexerApi { { reindexer::shared_lock lck(authorsMutex); - if (i % 4 == 0) authorIdIdx = rand() % authorsIds.size(); + if (i % 4 == 0) { + authorIdIdx = rand() % authorsIds.size(); + } } } @@ -214,7 +216,9 @@ class JoinSelectsApi : public ReindexerApi { for (auto& qr : queryRes) { wrSer.Reset(); err = qr.GetJSON(wrSer, false); - if (!err.ok()) break; + if (!err.ok()) { + break; + } gason::JsonParser().Parse(reindexer::giftStr(wrSer.Slice())); } } catch (const gason::Exception& ex) { @@ -238,7 +242,9 @@ class JoinSelectsApi : public ReindexerApi { } std::cout << std::endl; ++idx; - if (itemIt.getJoinedFieldsCount() > 1) std::cout << std::endl; + if (itemIt.getJoinedFieldsCount() > 1) { + std::cout << std::endl; + } } } } @@ -264,7 +270,9 @@ class JoinSelectsApi : public ReindexerApi { bool CompareQueriesResults(QueryResultRows& lhs, QueryResultRows& rhs) { EXPECT_EQ(lhs.size(), rhs.size()) << "Two queries results have different size!"; - if (lhs.size() != rhs.size()) return false; + if (lhs.size() != rhs.size()) { + return false; + } for (auto it = lhs.begin(); it != lhs.end(); ++it) { const BookId& bookId(it->first); @@ -282,11 +290,13 @@ class JoinSelectsApi : public ReindexerApi { const reindexer::VariantArray& fieldVal1(it2->second); const reindexer::VariantArray& fieldVal2(itFieldValue->second); EXPECT_TRUE(fieldVal1 == fieldVal2) << "Fields " << fieldName << " have different values!"; - } else + } else { return false; + } } - } else + } else { return false; + } } return true; diff --git a/cpp_src/gtests/tests/fixtures/ns_api.h b/cpp_src/gtests/tests/fixtures/ns_api.h index a31d2b3ef..efaa3a1cb 100644 --- a/cpp_src/gtests/tests/fixtures/ns_api.h +++ b/cpp_src/gtests/tests/fixtures/ns_api.h @@ -144,7 +144,9 @@ class NsApi : public ReindexerApi { IndexDeclaration{"ft21+ft22+ft23=ft24", "text", "composite", IndexOpts(), 0}}); static constexpr int itemsCount = 1000; - for (int i = 0; i < itemsCount; ++i) InsertNewTruncateItem(i); + for (int i = 0; i < itemsCount; ++i) { + InsertNewTruncateItem(i); + } const static Query q{truncate_namespace}; QueryResults qr1; diff --git a/cpp_src/gtests/tests/fixtures/queries_api.h b/cpp_src/gtests/tests/fixtures/queries_api.h index 64e2a99e1..74b02cac8 100644 --- a/cpp_src/gtests/tests/fixtures/queries_api.h +++ b/cpp_src/gtests/tests/fixtures/queries_api.h @@ -283,12 +283,16 @@ class QueriesApi : public ReindexerApi, public QueriesVerifier { if (ser.Len()) { ser << "\nExpected values:\n"; for (size_t i = 0; i < expectedValues.size(); ++i) { - if (i != 0) ser << ", "; + if (i != 0) { + ser << ", "; + } expectedValues[i].Dump(ser); } ser << "\nObtained values:\n"; for (size_t i = 0; i < qr.Count(); ++i) { - if (i != 0) ser << ", "; + if (i != 0) { + ser << ", "; + } reindexer::Item item(qr[i].GetItem(false)); const reindexer::Variant fieldValue = item[fieldName]; fieldValue.Dump(ser); @@ -436,7 +440,9 @@ class QueriesApi : public ReindexerApi, public QueriesVerifier { std::vector ForcedSortOffsetTestExpectedResults(size_t offset, size_t limit, bool desc, const std::vector& forcedSortOrder, Column column) const { - if (limit == 0 || offset >= forcedSortOffsetValues.size()) return {}; + if (limit == 0 || offset >= forcedSortOffsetValues.size()) { + return {}; + } std::vector res; res.resize(forcedSortOffsetValues.size()); std::transform( @@ -464,7 +470,9 @@ class QueriesApi : public ReindexerApi, public QueriesVerifier { bool desc2Column, const std::vector& forcedSortOrder, Column firstSortColumn) { - if (limit == 0 || offset >= forcedSortOffsetValues.size()) return {}; + if (limit == 0 || offset >= forcedSortOffsetValues.size()) { + return {}; + } if (firstSortColumn == First) { std::sort(forcedSortOffsetValues.begin(), forcedSortOffsetValues.end(), [desc1Column, desc2Column](std::pair lhs, std::pair rhs) { @@ -581,7 +589,9 @@ class QueriesApi : public ReindexerApi, public QueriesVerifier { std::vector RandStrVector(size_t count) { std::vector res; res.reserve(count); - for (size_t i = 0; i < count; ++i) res.emplace_back(RandString()); + for (size_t i = 0; i < count; ++i) { + res.emplace_back(RandString()); + } return res; } @@ -618,13 +628,17 @@ class QueriesApi : public ReindexerApi, public QueriesVerifier { item[kFieldNameUuid] = randUuid(); std::vector arr; arr.reserve(s); - for (size_t i = 0; i < s; ++i) arr.emplace_back(randUuid()); + for (size_t i = 0; i < s; ++i) { + arr.emplace_back(randUuid()); + } item[kFieldNameUuidArr] = std::move(arr); } else { item[kFieldNameUuid] = randStrUuid(); std::vector arr; arr.reserve(s); - for (size_t i = 0; i < s; ++i) arr.emplace_back(randStrUuid()); + for (size_t i = 0; i < s; ++i) { + arr.emplace_back(randStrUuid()); + } item[kFieldNameUuidArr] = std::move(arr); } @@ -922,7 +936,9 @@ class QueriesApi : public ReindexerApi, public QueriesVerifier { std::string name; int year; bool operator<(const MultifieldFacetItem& other) const { - if (year == other.year) return name < other.name; + if (year == other.year) { + return name < other.name; + } return year > other.year; } }; diff --git a/cpp_src/gtests/tests/fixtures/queries_verifier.h b/cpp_src/gtests/tests/fixtures/queries_verifier.h index bdc231aa5..945ac0384 100644 --- a/cpp_src/gtests/tests/fixtures/queries_verifier.h +++ b/cpp_src/gtests/tests/fixtures/queries_verifier.h @@ -25,7 +25,9 @@ class QueriesVerifier : public virtual ::testing::Test { struct PkHash { size_t operator()(const std::vector pk) const noexcept { size_t ret = pk.size(); - for (const auto& k : pk) ret = ((ret * 127) ^ (ret >> 3)) ^ k.Hash(); + for (const auto& k : pk) { + ret = ((ret * 127) ^ (ret >> 3)) ^ k.Hash(); + } return ret; } }; @@ -230,15 +232,21 @@ class QueriesVerifier : public virtual ::testing::Test { // Check non found items, to not match conditions // If query has limit and offset, skip verification - if (query.HasOffset() || query.HasLimit()) return; + if (query.HasOffset() || query.HasLimit()) { + return; + } // If query has distinct, skip verification for (const auto& agg : query.aggregations_) { - if (agg.Type() == AggDistinct) return; + if (agg.Type() == AggDistinct) { + return; + } } for (auto& insertedItem : insertedItems_[query.NsName()]) { - if (pks.find(insertedItem.first) != pks.end()) continue; + if (pks.find(insertedItem.first) != pks.end()) { + continue; + } bool conditionsSatisfied = checkConditions(insertedItem.second, query.Entries().cbegin(), query.Entries().cend(), joinedSelectors, indexesFields); @@ -280,7 +288,9 @@ class QueriesVerifier : public virtual ::testing::Test { auto& ii = insertedItems_[ns]; const auto it = ii.find(pk); if (it == ii.end()) { - for (auto& p : pk) p.EnsureHold(); + for (auto& p : pk) { + p.EnsureHold(); + } [[maybe_unused]] const auto res = ii.emplace(std::move(pk), std::move(item)); assertrx(res.second); } else { @@ -324,9 +334,11 @@ class QueriesVerifier : public virtual ::testing::Test { bool result = true; for (; it != to; ++it) { OpType op = it->operation; - if (op != OpOr && !result) return false; + if (op != OpOr && !result) { + return false; + } bool skip = false; - bool const iterationResult = it->Visit( + const bool iterationResult = it->Visit( [](const reindexer::SubQueryEntry&) -> bool { assertrx(0); std::abort(); @@ -367,7 +379,9 @@ class QueriesVerifier : public virtual ::testing::Test { return checkCondition(item, qe, indexesFields); }, [](const reindexer::AlwaysFalse&) noexcept { return false; }, [](const reindexer::AlwaysTrue&) noexcept { return true; }); - if (skip) continue; + if (skip) { + continue; + } switch (op) { case OpNot: result = !iterationResult; @@ -399,14 +413,20 @@ class QueriesVerifier : public virtual ::testing::Test { bool result = true; // check only on root level for (auto it = qr.Entries().cbegin(); it != qr.Entries().cend(); ++it) { - if (!it->Is()) continue; + if (!it->Is()) { + continue; + } const reindexer::QueryEntry& qentry = it->Value(); - if (!qentry.Distinct()) continue; + if (!qentry.Distinct()) { + continue; + } const std::string fieldName = getFieldName(qentry.FieldName(), indexesFields); reindexer::VariantArray fieldValue = item[fieldName]; EXPECT_EQ(fieldValue.size(), 1) << "Distinct field's size cannot be > 1"; - if (fieldValue.empty()) return false; + if (fieldValue.empty()) { + return false; + } std::unordered_set& values = distincts[fieldName]; reindexer::Variant keyValue(fieldValue[0]); @@ -428,7 +448,9 @@ class QueriesVerifier : public virtual ::testing::Test { assertrx(joinEntries[0].Operation() != OpOr); for (const auto& je : joinEntries) { if (je.Operation() == OpOr) { - if (result) continue; + if (result) { + continue; + } } else if (!result) { break; } @@ -448,7 +470,9 @@ class QueriesVerifier : public virtual ::testing::Test { assertrx(0); } } - if (result) return true; + if (result) { + return true; + } } return false; } @@ -522,7 +546,9 @@ class QueriesVerifier : public virtual ::testing::Test { static bool checkGeomConditions(const reindexer::Item& item, const reindexer::QueryEntry& qentry, const IndexesData& indexesFields) { assertrx(qentry.Values().size() == 2); const reindexer::VariantArray coordinates = item[getFieldName(qentry.FieldName(), indexesFields)]; - if (coordinates.empty()) return false; + if (coordinates.empty()) { + return false; + } assertrx(coordinates.size() == 2); const double x = coordinates[0].As(); const double y = coordinates[1].As(); @@ -591,12 +617,16 @@ class QueriesVerifier : public virtual ::testing::Test { case CondEq: case CondSet: for (const reindexer::Variant& kv : keyValues) { - if (compareCompositeValues(indexesValues, kv, opts) == reindexer::ComparationResult::Eq) return true; + if (compareCompositeValues(indexesValues, kv, opts) == reindexer::ComparationResult::Eq) { + return true; + } } return false; case CondAllSet: for (const reindexer::Variant& kv : keyValues) { - if (compareCompositeValues(indexesValues, kv, opts) != reindexer::ComparationResult::Eq) return false; + if (compareCompositeValues(indexesValues, kv, opts) != reindexer::ComparationResult::Eq) { + return false; + } } return !keyValues.empty(); case CondLike: @@ -704,7 +734,9 @@ class QueriesVerifier : public virtual ::testing::Test { case CondEq: case CondSet: for (const reindexer::Variant& kv : keys) { - if (compare(value, kv, opts, fieldType) == reindexer::ComparationResult::Eq) return true; + if (compare(value, kv, opts, fieldType) == reindexer::ComparationResult::Eq) { + return true; + } } return false; case CondLike: @@ -743,8 +775,9 @@ class QueriesVerifier : public virtual ::testing::Test { if (!checkCondition(item, reindexer::BetweenFieldsQueryEntry{std::string{firstFields[i].name}, qentry.Condition(), std::string{secondFields[i].name}}, - indexesFields)) + indexesFields)) { return false; + } } return !firstFields.empty(); } @@ -860,7 +893,9 @@ class QueriesVerifier : public virtual ::testing::Test { const auto values2 = getJoinedField(item.GetID(), qr, i.nsIdx, i.index2, i.column2); return distance(static_cast(values1), static_cast(values2)); }); - if (it->operation.negative) value = -value; + if (it->operation.negative) { + value = -value; + } switch (it->operation.op) { case OpPlus: result += value; @@ -926,7 +961,9 @@ class QueriesVerifier : public virtual ::testing::Test { } else { ret += '['; for (size_t i = 0, s = values.size(); i < s; ++i) { - if (i != 0) ret += ','; + if (i != 0) { + ret += ','; + } ret += values[i].template As(); } ret += ']'; @@ -973,7 +1010,9 @@ class QueriesVerifier : public virtual ::testing::Test { static void printFailedSortOrder(const reindexer::Query& query, const reindexer::QueryResults& qr, int itemIndex, int itemsToShow = 10) { - if (qr.Count() == 0) return; + if (qr.Count() == 0) { + return; + } TestCout() << "Sort order or last items:" << std::endl; reindexer::Item rdummy(qr[0].GetItem(false)); @@ -985,22 +1024,32 @@ class QueriesVerifier : public virtual ::testing::Test { TestCout().BoldOff(); int firstItem = itemIndex - itemsToShow; - if (firstItem < 0) firstItem = 0; + if (firstItem < 0) { + firstItem = 0; + } for (int i = firstItem; i <= itemIndex; ++i) { reindexer::Item item(qr[i].GetItem(false)); - if (i == itemIndex) TestCout().BoldOn(); + if (i == itemIndex) { + TestCout().BoldOn(); + } for (size_t j = 0; j < query.sortingEntries_.size(); ++j) { TestCout() << item[query.sortingEntries_[j].expression].As() << " "; } - if (i == itemIndex) TestCout().BoldOff(); + if (i == itemIndex) { + TestCout().BoldOff(); + } TestCout().Endl(); } firstItem = itemIndex + 1; int lastItem = firstItem + itemsToShow; const int count = static_cast(qr.Count()); - if (firstItem >= count) firstItem = count - 1; - if (lastItem > count) lastItem = count; + if (firstItem >= count) { + firstItem = count - 1; + } + if (lastItem > count) { + lastItem = count; + } for (int i = firstItem; i < lastItem; ++i) { reindexer::Item item(qr[i].GetItem(false)); for (size_t j = 0; j < query.sortingEntries_.size(); ++j) { diff --git a/cpp_src/gtests/tests/fixtures/reindexer_api.h b/cpp_src/gtests/tests/fixtures/reindexer_api.h index 70dbc4b5b..d425f6794 100644 --- a/cpp_src/gtests/tests/fixtures/reindexer_api.h +++ b/cpp_src/gtests/tests/fixtures/reindexer_api.h @@ -27,25 +27,25 @@ class ReindexerApi : public virtual ::testing::Test { void DefineNamespaceDataset(std::string_view ns, std::initializer_list fields) { rt.DefineNamespaceDataset(ns, fields); } - void DefineNamespaceDataset(std::string_view ns, const std::vector &fields) { rt.DefineNamespaceDataset(ns, fields); } - void DefineNamespaceDataset(Reindexer &rx, const std::string &ns, std::initializer_list fields) { + void DefineNamespaceDataset(std::string_view ns, const std::vector& fields) { rt.DefineNamespaceDataset(ns, fields); } + void DefineNamespaceDataset(Reindexer& rx, const std::string& ns, std::initializer_list fields) { rt.DefineNamespaceDataset(rx, ns, fields); } Item NewItem(std::string_view ns) { return rt.NewItem(ns); } void Commit(std::string_view ns) { rt.Commit(ns); } - void Upsert(std::string_view ns, Item &item) { rt.Upsert(ns, item); } - size_t Update(const Query &q) { return rt.Update(q); } - void Delete(std::string_view ns, Item &item) { rt.Delete(ns, item); } - size_t Delete(const Query &q) { return rt.Delete(q); } + void Upsert(std::string_view ns, Item& item) { rt.Upsert(ns, item); } + size_t Update(const Query& q) { return rt.Update(q); } + void Delete(std::string_view ns, Item& item) { rt.Delete(ns, item); } + size_t Delete(const Query& q) { return rt.Delete(q); } - void PrintQueryResults(const std::string &ns, const QueryResults &res) { rt.PrintQueryResults(ns, res); } + void PrintQueryResults(const std::string& ns, const QueryResults& res) { rt.PrintQueryResults(ns, res); } std::string RandString() { return rt.RandString(); } std::string RandLikePattern() { return rt.RandLikePattern(); } std::string RuRandString() { return rt.RuRandString(); } std::vector RandIntVector(size_t size, int start, int range) { return rt.RandIntVector(size, start, range); } - void AwaitIndexOptimization(const std::string &nsName) { + void AwaitIndexOptimization(const std::string& nsName) { bool optimization_completed = false; unsigned waitForIndexOptimizationCompleteIterations = 0; while (!optimization_completed) { @@ -73,7 +73,7 @@ class ReindexerApi : public virtual ::testing::Test { ASSERT_TRUE(err.ok()) << err.what(); } - static Item getMemStat(Reindexer &rx, const std::string &ns) { + static Item getMemStat(Reindexer& rx, const std::string& ns) { QueryResults qr; auto err = rx.Select(Query("#memstats").Where("name", CondEq, ns), qr); EXPECT_TRUE(err.ok()) << err.what(); diff --git a/cpp_src/gtests/tests/fixtures/reindexertestapi.h b/cpp_src/gtests/tests/fixtures/reindexertestapi.h index 7d0365d1c..66f7b9c65 100644 --- a/cpp_src/gtests/tests/fixtures/reindexertestapi.h +++ b/cpp_src/gtests/tests/fixtures/reindexertestapi.h @@ -28,9 +28,9 @@ class ReindexerTestApi { ReindexerTestApi() : reindexer(std::make_shared()) {} template - static void DefineNamespaceDataset(DB &rx, std::string_view ns, const FieldsT &fields) { + static void DefineNamespaceDataset(DB& rx, std::string_view ns, const FieldsT& fields) { auto err = reindexer::Error(); - for (const auto &field : fields) { + for (const auto& field : fields) { if (field.indexType != "composite") { err = rx.AddIndex(ns, {std::string{field.indexName}, {std::string{field.indexName}}, @@ -59,7 +59,7 @@ class ReindexerTestApi { void DefineNamespaceDataset(std::string_view ns, std::initializer_list fields) { DefineNamespaceDataset(*reindexer, ns, fields); } - void DefineNamespaceDataset(std::string_view ns, const std::vector &fields) { + void DefineNamespaceDataset(std::string_view ns, const std::vector& fields) { DefineNamespaceDataset(*reindexer, ns, fields); } @@ -76,7 +76,7 @@ class ReindexerTestApi { auto err = reindexer->OpenNamespace(ns); ASSERT_TRUE(err.ok()) << err.what() << "; namespace: " << ns; } - void AddIndex(std::string_view ns, const reindexer::IndexDef &idef) { + void AddIndex(std::string_view ns, const reindexer::IndexDef& idef) { auto err = reindexer->AddIndex(ns, idef); if (!err.ok()) { reindexer::WrSerializer ser; @@ -88,7 +88,7 @@ class ReindexerTestApi { auto err = reindexer->DropIndex(ns, reindexer::IndexDef(std::string(name))); ASSERT_TRUE(err.ok()) << err.what() << "; namespace: " << ns << "; name: " << name; } - void Upsert(std::string_view ns, ItemType &item) { + void Upsert(std::string_view ns, ItemType& item) { assertrx(!!item); auto err = reindexer->Upsert(ns, item); ASSERT_TRUE(err.ok()) << err.what(); @@ -101,45 +101,47 @@ class ReindexerTestApi { err = reindexer->Upsert(ns, item); ASSERT_TRUE(err.ok()) << err.what() << "; " << json; } - void Update(const reindexer::Query &q, QueryResultsType &qr) { + void Update(const reindexer::Query& q, QueryResultsType& qr) { auto err = reindexer->Update(q, qr); ASSERT_TRUE(err.ok()) << err.what() << "; " << q.GetSQL(QueryUpdate); } - size_t Update(const reindexer::Query &q) { + size_t Update(const reindexer::Query& q) { QueryResultsType qr; Update(q, qr); return qr.Count(); } - QueryResultsType UpdateQR(const reindexer::Query &q) { + QueryResultsType UpdateQR(const reindexer::Query& q) { QueryResultsType qr; Update(q, qr); return qr; } - void Select(const reindexer::Query &q, QueryResultsType &qr) { + void Select(const reindexer::Query& q, QueryResultsType& qr) { auto err = reindexer->Select(q, qr); ASSERT_TRUE(err.ok()) << err.what() << "; " << q.GetSQL(); } - QueryResultsType Select(const reindexer::Query &q) { + QueryResultsType Select(const reindexer::Query& q) { QueryResultsType qr; Select(q, qr); return qr; } - void Delete(std::string_view ns, ItemType &item) { + void Delete(std::string_view ns, ItemType& item) { assertrx(!!item); auto err = reindexer->Delete(ns, item); ASSERT_TRUE(err.ok()) << err.what(); } - size_t Delete(const reindexer::Query &q) { + size_t Delete(const reindexer::Query& q) { QueryResultsType qr; auto err = reindexer->Delete(q, qr); EXPECT_TRUE(err.ok()) << err.what() << "; " << q.GetSQL(QueryDelete); return qr.Count(); } - reindexer::Error DumpIndex(std::ostream &os, std::string_view ns, std::string_view index) { + reindexer::Error DumpIndex(std::ostream& os, std::string_view ns, std::string_view index) { return reindexer->DumpIndex(os, ns, index); } - void PrintQueryResults(const std::string &ns, const QueryResultsType &res) { - if (!verbose) return; + void PrintQueryResults(const std::string& ns, const QueryResultsType& res) { + if (!verbose) { + return; + } { ItemType rdummy(reindexer->NewItem(ns)); std::string outBuf; diff --git a/cpp_src/gtests/tests/fixtures/replication_api.cc b/cpp_src/gtests/tests/fixtures/replication_api.cc index f2a4c2982..ac10273ff 100644 --- a/cpp_src/gtests/tests/fixtures/replication_api.cc +++ b/cpp_src/gtests/tests/fixtures/replication_api.cc @@ -6,7 +6,9 @@ bool ReplicationApi::StopServer(size_t id) { std::lock_guard lock(m_); assertrx(id < svc_.size()); - if (!svc_[id].Get()) return false; + if (!svc_[id].Get()) { + return false; + } svc_[id].Drop(); auto now = std::chrono::milliseconds(0); const auto pause = std::chrono::milliseconds(10); @@ -24,7 +26,9 @@ bool ReplicationApi::StartServer(size_t id) { std::lock_guard lock(m_); assertrx(id < svc_.size()); - if (svc_[id].IsRunning()) return false; + if (svc_[id].IsRunning()) { + return false; + } svc_[id].InitServer(id, kDefaultRpcPort + id, kDefaultHttpPort + id, kStoragePath + "node/" + std::to_string(id), "node" + std::to_string(id), true); return true; @@ -92,21 +96,27 @@ void ReplicationApi::ForceSync() { } }); for (size_t i = 0; i < svc_.size(); i++) { - if (i != masterId_) GetSrv(i)->ForceSync(); + if (i != masterId_) { + GetSrv(i)->ForceSync(); + } } done = true; awaitForceSync.join(); } void ReplicationApi::SwitchMaster(size_t id, const ReplicationConfigTest::NsSet& namespaces) { - if (id == masterId_) return; + if (id == masterId_) { + return; + } masterId_ = id; ReplicationConfigTest config("master", false, true, id); GetSrv(masterId_)->MakeMaster(config); for (size_t i = 0; i < svc_.size(); i++) { std::string masterDsn = "cproto://127.0.0.1:" + std::to_string(kDefaultRpcPort + masterId_) + "/node" + std::to_string(masterId_); ReplicationConfigTest config("slave", false, true, i, masterDsn, "server_" + std::to_string(i), namespaces); - if (i != masterId_) GetSrv(i)->MakeSlave(config); + if (i != masterId_) { + GetSrv(i)->MakeSlave(config); + } } } @@ -150,10 +160,14 @@ void ReplicationApi::SetUp() { void ReplicationApi::TearDown() { std::lock_guard lock(m_); for (auto& server : svc_) { - if (server.Get()) server.Get()->Stop(); + if (server.Get()) { + server.Get()->Stop(); + } } for (auto& server : svc_) { - if (!server.Get()) continue; + if (!server.Get()) { + continue; + } server.Drop(); auto now = std::chrono::milliseconds(0); const auto pause = std::chrono::milliseconds(10); diff --git a/cpp_src/gtests/tests/fixtures/replication_load_api.h b/cpp_src/gtests/tests/fixtures/replication_load_api.h index e21b72cf4..758e0f3a4 100644 --- a/cpp_src/gtests/tests/fixtures/replication_load_api.h +++ b/cpp_src/gtests/tests/fixtures/replication_load_api.h @@ -9,7 +9,7 @@ class ReplicationLoadApi : public ReplicationApi { public: class UpdatesReciever : public reindexer::IUpdatesObserver { public: - void OnWALUpdate(reindexer::LSNPair, std::string_view nsName, const reindexer::WALRecord &) override final { + void OnWALUpdate(reindexer::LSNPair, std::string_view nsName, const reindexer::WALRecord&) override final { std::lock_guard lck(mtx_); auto found = updatesCounters_.find(nsName); if (found != updatesCounters_.end()) { @@ -18,7 +18,7 @@ class ReplicationLoadApi : public ReplicationApi { updatesCounters_.emplace(std::string(nsName), 1); } } - void OnConnectionState(const Error &) override final {} + void OnConnectionState(const Error&) override final {} void OnUpdatesLost(std::string_view) override final {} using map = tsl::hopscotch_map; @@ -34,7 +34,7 @@ class ReplicationLoadApi : public ReplicationApi { void Dump() const { std::cerr << "Reciever dump: " << std::endl; auto counters = Counters(); - for (auto &it : counters) { + for (auto& it : counters) { std::cerr << it.first << ": " << it.second << std::endl; } } @@ -51,7 +51,7 @@ class ReplicationLoadApi : public ReplicationApi { // untill we use shared ptr it will be not destroyed auto srv = GetSrv(masterId_); - auto &api = srv->api; + auto& api = srv->api; Error err = api.reindexer->OpenNamespace("some", opt); ASSERT_TRUE(err.ok()) << err.what(); @@ -75,7 +75,7 @@ class ReplicationLoadApi : public ReplicationApi { void FillData(size_t count) { // untill we use shared ptr it will be not destroyed auto srv = GetSrv(masterId_); - auto &api = srv->api; + auto& api = srv->api; reindexer::shared_lock lk(restartMutex_); @@ -98,7 +98,7 @@ class ReplicationLoadApi : public ReplicationApi { BaseApi::QueryResultsType SimpleSelect(size_t num) { reindexer::Query qr("some"); auto srv = GetSrv(num); - auto &api = srv->api; + auto& api = srv->api; BaseApi::QueryResultsType res(api.reindexer.get()); auto err = api.reindexer->Select(qr, res); EXPECT_TRUE(err.ok()) << err.what(); @@ -107,18 +107,18 @@ class ReplicationLoadApi : public ReplicationApi { } BaseApi::QueryResultsType DeleteFromMaster() { auto srv = GetSrv(masterId_); - auto &api = srv->api; + auto& api = srv->api; BaseApi::QueryResultsType res(api.reindexer.get()); auto err = api.reindexer->Delete(reindexer::Query("some"), res); EXPECT_TRUE(err.ok()) << err.what(); return res; } - void RestartWithConfigFile(size_t num, const std::string &configYaml) { + void RestartWithConfigFile(size_t num, const std::string& configYaml) { GetSrv(num)->WriteServerConfig(configYaml); StopServer(num); StartServer(num); } - void SetServerConfig(size_t num, const ReplicationConfigTest &config) { + void SetServerConfig(size_t num, const ReplicationConfigTest& config) { auto srv = GetSrv(num); if (num) { srv->MakeSlave(config); @@ -126,13 +126,13 @@ class ReplicationLoadApi : public ReplicationApi { srv->MakeMaster(config); } } - void CheckSlaveConfigFile(size_t num, const ReplicationConfigTest &config) { + void CheckSlaveConfigFile(size_t num, const ReplicationConfigTest& config) { assertrx(num); auto srv = GetSrv(num); auto curConfig = srv->GetServerConfig(ServerControl::ConfigType::File); EXPECT_TRUE(config == curConfig) << "config:\n" << config.GetJSON() << "\ncurConfig:\n" << curConfig.GetJSON(); } - void CheckSlaveConfigNamespace(size_t num, const ReplicationConfigTest &config, std::chrono::seconds awaitTime) { + void CheckSlaveConfigNamespace(size_t num, const ReplicationConfigTest& config, std::chrono::seconds awaitTime) { assertrx(num); auto srv = GetSrv(num); for (int i = 0; i < awaitTime.count(); ++i) { @@ -150,7 +150,7 @@ class ReplicationLoadApi : public ReplicationApi { versions.reserve(GetServersCount()); for (size_t i = 0; i < GetServersCount(); i++) { auto srv = GetSrv(i); - auto &api = srv->api; + auto& api = srv->api; BaseApi::QueryResultsType res(api.reindexer.get()); auto err = api.reindexer->Select(reindexer::Query(ns), res); EXPECT_TRUE(err.ok()) << err.what(); diff --git a/cpp_src/gtests/tests/fixtures/rpcclient_api.h b/cpp_src/gtests/tests/fixtures/rpcclient_api.h index bb22c9776..3cf36189d 100644 --- a/cpp_src/gtests/tests/fixtures/rpcclient_api.h +++ b/cpp_src/gtests/tests/fixtures/rpcclient_api.h @@ -43,7 +43,7 @@ class RPCClientTestApi : public ::testing::Test { void Stop(); const std::string& GetDsn() const { return dsn_; } RPCServerStatus Status() const { return server_->Status(); } - Error const& ErrorStatus() const { return err_; } + const Error& ErrorStatus() const { return err_; } size_t CloseQRRequestsCount() const { return server_->CloseQRRequestsCount(); } private: diff --git a/cpp_src/gtests/tests/fixtures/runtime_indexes_api.h b/cpp_src/gtests/tests/fixtures/runtime_indexes_api.h index a9bac392a..70d75dbe2 100644 --- a/cpp_src/gtests/tests/fixtures/runtime_indexes_api.h +++ b/cpp_src/gtests/tests/fixtures/runtime_indexes_api.h @@ -157,7 +157,9 @@ class RuntimeIndexesApi : public ReindexerApi { void AddRuntimeStringIndex(int indexNumber, bool pk = false) { IndexOpts opts; - if (pk) opts.PK(); + if (pk) { + opts.PK(); + } std::string indexName = getRuntimeStringIndexName(indexNumber); Error err = rt.reindexer->AddIndex(default_namespace, {indexName, "hash", "string", opts}); ASSERT_TRUE(err.ok()) << err.what(); diff --git a/cpp_src/gtests/tests/fixtures/selector_plan_test.h b/cpp_src/gtests/tests/fixtures/selector_plan_test.h index 8b4c2be1d..41e9552dd 100644 --- a/cpp_src/gtests/tests/fixtures/selector_plan_test.h +++ b/cpp_src/gtests/tests/fixtures/selector_plan_test.h @@ -100,14 +100,18 @@ class SelectorPlanTest : public ReindexerApi { static std::vector adoptValuesType(std::initializer_list values) { std::vector result; result.reserve(values.size()); - for (const T& v : values) result.push_back(v); + for (const T& v : values) { + result.push_back(v); + } return result; } static std::vector adoptValuesType(std::initializer_list values) { std::vector result; result.reserve(values.size()); - for (const char* v : values) result.emplace_back(v); + for (const char* v : values) { + result.emplace_back(v); + } return result; } diff --git a/cpp_src/gtests/tests/fixtures/servercontrol.cc b/cpp_src/gtests/tests/fixtures/servercontrol.cc index cd8ffc465..11385372d 100644 --- a/cpp_src/gtests/tests/fixtures/servercontrol.cc +++ b/cpp_src/gtests/tests/fixtures/servercontrol.cc @@ -61,7 +61,9 @@ void ServerControl::Interface::Stop() { std::string ServerControl::Interface::getLogName(const std::string& log, bool core) { std::string name = getTestLogPath(); name += (log + "_"); - if (!core) name += std::to_string(id_); + if (!core) { + name += std::to_string(id_); + } name += ".log"; return name; } @@ -313,7 +315,9 @@ void ServerControl::Interface::setReplicationConfig(const ReplicationConfigTest& replConf.Put("server_id", config.serverId_); auto nsArray = replConf.Array("namespaces"); - for (auto& ns : config.namespaces_) nsArray.Put(nullptr, ns); + for (auto& ns : config.namespaces_) { + nsArray.Put(nullptr, ns); + } nsArray.End(); replConf.End(); jb.End(); @@ -371,8 +375,9 @@ ReplicationTestState ServerControl::Interface::GetState(std::string_view ns) { state.ownLsn.FromJSON(root["replication"]["last_lsn_v2"]); if (!isSlave) { state.lsn.FromJSON(root["replication"]["last_lsn_v2"]); - } else + } else { state.lsn.FromJSON(root["replication"]["origin_lsn"]); + } state.dataCount = root["replication"]["data_count"].As(); state.dataHash = root["replication"]["data_hash"].As(); diff --git a/cpp_src/gtests/tests/fixtures/systemhelpers.cc b/cpp_src/gtests/tests/fixtures/systemhelpers.cc index fae4110cb..0a427745e 100644 --- a/cpp_src/gtests/tests/fixtures/systemhelpers.cc +++ b/cpp_src/gtests/tests/fixtures/systemhelpers.cc @@ -60,7 +60,9 @@ pid_t StartProcess(const std::string& program, const std::vector& p Error EndProcess(pid_t PID) { #ifdef __linux__ int r = kill(PID, SIGTERM); - if (r != 0) return Error(errLogic, "errno=%d (%s)", errno, strerror(errno)); + if (r != 0) { + return Error(errLogic, "errno=%d (%s)", errno, strerror(errno)); + } #else (void)PID; assertrx(false); @@ -78,7 +80,9 @@ Error WaitEndProcess(pid_t PID) { if (WEXITSTATUS(status)) { return Error(errLogic, "WEXITSTATUS(status) != 0. status: %d", WEXITSTATUS(status)); } - if (waitres != PID) return Error(errLogic, "waitres != PID. errno=%d (%s)", errno, strerror(errno)); + if (waitres != PID) { + return Error(errLogic, "waitres != PID. errno=%d (%s)", errno, strerror(errno)); + } #else (void)PID; assertrx(false); diff --git a/cpp_src/gtests/tests/fixtures/test_helpers.h b/cpp_src/gtests/tests/fixtures/test_helpers.h index 51d72cfe9..9940c0adb 100644 --- a/cpp_src/gtests/tests/fixtures/test_helpers.h +++ b/cpp_src/gtests/tests/fixtures/test_helpers.h @@ -13,11 +13,11 @@ struct QueryWatcher { } } - const reindexer::Query &q; + const reindexer::Query& q; }; template -std::string PrintItem(const ItemType &item) { +std::string PrintItem(const ItemType& item) { std::stringstream out; for (auto idx = 1; idx < item.NumFields(); idx++) { out << item[idx].Name() << '='; @@ -27,7 +27,9 @@ std::string PrintItem(const ItemType &item) { } else { out << '['; for (size_t i = 0, s = values.size(); i < s; ++i) { - if (i != 0) out << ", "; + if (i != 0) { + out << ", "; + } out << values[i].template As(); } out << "] "; diff --git a/cpp_src/gtests/tests/fixtures/ttl_index_api.h b/cpp_src/gtests/tests/fixtures/ttl_index_api.h index 8e434914f..98c1ddbcd 100644 --- a/cpp_src/gtests/tests/fixtures/ttl_index_api.h +++ b/cpp_src/gtests/tests/fixtures/ttl_index_api.h @@ -68,7 +68,9 @@ class TtlIndexApi : public ReindexerApi { for (size_t i = 0; i < 10; ++i) { std::this_thread::sleep_for(kStep); count = GetItemsCount(); - if (count == 0) break; + if (count == 0) { + break; + } } } return count; diff --git a/cpp_src/gtests/tests/fuzzing/fuzzing.cc b/cpp_src/gtests/tests/fuzzing/fuzzing.cc index 55a109b64..f4e753118 100644 --- a/cpp_src/gtests/tests/fuzzing/fuzzing.cc +++ b/cpp_src/gtests/tests/fuzzing/fuzzing.cc @@ -53,14 +53,20 @@ TEST_F(Fuzzing, BaseTest) { auto item = rx_.NewItem(ns.GetName()); err = item.Status(); EXPECT_TRUE(err.ok()) << err.what(); - if (!err.ok()) continue; + if (!err.ok()) { + continue; + } ns.NewItem(ser); // TODO not json err = item.FromJSON(ser.Slice()); EXPECT_TRUE(err.ok()) << err.what() << std::endl << "size: " << ser.Slice().size() << std::endl << ser.Slice(); - if (!err.ok()) continue; + if (!err.ok()) { + continue; + } err = item.Status(); EXPECT_TRUE(err.ok()) << err.what(); - if (!err.ok()) continue; + if (!err.ok()) { + continue; + } enum Op : uint8_t { Insert, Upsert, Update, Delete, END = Delete }; switch (rnd.RndWhich()) { case Insert: diff --git a/cpp_src/gtests/tests/mocks/rpcserver_fake.cc b/cpp_src/gtests/tests/mocks/rpcserver_fake.cc index c287d1f7b..27c2609a1 100644 --- a/cpp_src/gtests/tests/mocks/rpcserver_fake.cc +++ b/cpp_src/gtests/tests/mocks/rpcserver_fake.cc @@ -6,14 +6,14 @@ #include "net/listener.h" #include "reindexer_version.h" -RPCServerFake::RPCServerFake(const RPCServerConfig &conf) : startTs_(system_clock_w::now()), conf_(conf), state_(Init) {} +RPCServerFake::RPCServerFake(const RPCServerConfig& conf) : startTs_(system_clock_w::now()), conf_(conf), state_(Init) {} -Error RPCServerFake::Ping(cproto::Context &) { +Error RPCServerFake::Ping(cproto::Context&) { // return {}; } -Error RPCServerFake::Login(cproto::Context &ctx, p_string /*login*/, p_string /*password*/, p_string /*db*/) { +Error RPCServerFake::Login(cproto::Context& ctx, p_string /*login*/, p_string /*password*/, p_string /*db*/) { if (loginError_.ok()) { std::this_thread::sleep_for(conf_.loginDelay); } @@ -31,8 +31,8 @@ Error RPCServerFake::Login(cproto::Context &ctx, p_string /*login*/, p_string /* return loginError_.code(); } -Error RPCServerFake::CheckAuth(cproto::Context &ctx) { - auto clientData = dynamic_cast(ctx.GetClientData()); +Error RPCServerFake::CheckAuth(cproto::Context& ctx) { + auto clientData = dynamic_cast(ctx.GetClientData()); if (ctx.call->cmd == cproto::kCmdLogin || ctx.call->cmd == cproto::kCmdPing) { return {}; @@ -45,24 +45,24 @@ Error RPCServerFake::CheckAuth(cproto::Context &ctx) { return {}; } -Error RPCServerFake::OpenNamespace(cproto::Context &, p_string) { +Error RPCServerFake::OpenNamespace(cproto::Context&, p_string) { std::this_thread::sleep_for(conf_.openNsDelay); return {}; } -Error RPCServerFake::DropNamespace(cproto::Context &, p_string) { return Error(errOK); } +Error RPCServerFake::DropNamespace(cproto::Context&, p_string) { return Error(errOK); } Error RPCServerFake::Stop() { listener_->Stop(); state_ = Stopped; - if (int const openedQR = OpenedQRCount(); openedQR == 0) { + if (const int openedQR = OpenedQRCount(); openedQR == 0) { return errOK; } else { return Error{errLogic, "There are %d opened QueryResults", openedQR}; } } -Error RPCServerFake::Select(cproto::Context &ctx, p_string /*query*/, int /*flags*/, int /*limit*/, p_string /*ptVersions*/) { +Error RPCServerFake::Select(cproto::Context& ctx, p_string /*query*/, int /*flags*/, int /*limit*/, p_string /*ptVersions*/) { static constexpr size_t kQueryResultsPoolSize = 1024; std::this_thread::sleep_for(conf_.selectDelay); int qrId; @@ -87,7 +87,7 @@ Error RPCServerFake::Select(cproto::Context &ctx, p_string /*query*/, int /*flag return errOK; } -Error RPCServerFake::CloseResults(cproto::Context &ctx, int reqId, std::optional /*qrUID*/, std::optional doNotReply) { +Error RPCServerFake::CloseResults(cproto::Context& ctx, int reqId, std::optional /*qrUID*/, std::optional doNotReply) { if (doNotReply && *doNotReply) { ctx.respSent = true; } @@ -109,7 +109,7 @@ size_t RPCServerFake::OpenedQRCount() { return usedQrIds_.size(); } -bool RPCServerFake::Start(const std::string &addr, ev::dynamic_loop &loop, Error loginError) { +bool RPCServerFake::Start(const std::string& addr, ev::dynamic_loop& loop, Error loginError) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif diff --git a/cpp_src/gtests/tests/mocks/rpcserver_fake.h b/cpp_src/gtests/tests/mocks/rpcserver_fake.h index 07906fbb6..ae888cdc0 100644 --- a/cpp_src/gtests/tests/mocks/rpcserver_fake.h +++ b/cpp_src/gtests/tests/mocks/rpcserver_fake.h @@ -26,20 +26,20 @@ struct RPCClientData final : public cproto::ClientData { class RPCServerFake { public: - RPCServerFake(const RPCServerConfig &conf); + RPCServerFake(const RPCServerConfig& conf); - bool Start(const std::string &addr, ev::dynamic_loop &loop, Error loginError); + bool Start(const std::string& addr, ev::dynamic_loop& loop, Error loginError); Error Stop(); - Error Ping(cproto::Context &ctx); - Error Login(cproto::Context &ctx, p_string login, p_string password, p_string db); - Error Select(cproto::Context &ctx, p_string query, int flags, int limit, p_string ptVersions); - Error OpenNamespace(cproto::Context &ctx, p_string ns); - Error DropNamespace(cproto::Context &ctx, p_string ns); - Error CloseResults(cproto::Context &ctx, int reqId, std::optional qrUID, std::optional doNotReply); + Error Ping(cproto::Context& ctx); + Error Login(cproto::Context& ctx, p_string login, p_string password, p_string db); + Error Select(cproto::Context& ctx, p_string query, int flags, int limit, p_string ptVersions); + Error OpenNamespace(cproto::Context& ctx, p_string ns); + Error DropNamespace(cproto::Context& ctx, p_string ns); + Error CloseResults(cproto::Context& ctx, int reqId, std::optional qrUID, std::optional doNotReply); RPCServerStatus Status() const; - Error CheckAuth(cproto::Context &ctx); + Error CheckAuth(cproto::Context& ctx); size_t OpenedQRCount(); size_t CloseQRRequestsCount() const { return closeQRRequestsCounter_.load(std::memory_order_relaxed); } diff --git a/cpp_src/gtests/tests/unit/btree_idsets_tests.cc b/cpp_src/gtests/tests/unit/btree_idsets_tests.cc index a182d8941..47f1d209f 100644 --- a/cpp_src/gtests/tests/unit/btree_idsets_tests.cc +++ b/cpp_src/gtests/tests/unit/btree_idsets_tests.cc @@ -10,7 +10,7 @@ TEST_F(BtreeIdsetsApi, SelectByStringField) { std::string strValueToCheck = lastStrValue; Error err = rt.reindexer->Select(Query(default_namespace).Not().Where(kFieldOne, CondEq, strValueToCheck), qr); EXPECT_TRUE(err.ok()) << err.what(); - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant kr = item[kFieldOne]; EXPECT_TRUE(kr.Type().Is()); @@ -24,7 +24,7 @@ TEST_F(BtreeIdsetsApi, SelectByIntField) { QueryResults qr; Error err = rt.reindexer->Select(Query(default_namespace).Where(kFieldTwo, CondGe, Variant(static_cast(boundaryValue))), qr); EXPECT_TRUE(err.ok()) << err.what(); - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant kr = item[kFieldTwo]; EXPECT_TRUE(kr.Type().Is()); @@ -44,7 +44,7 @@ TEST_F(BtreeIdsetsApi, SelectByBothFields) { .Where(kFieldTwo, CondGe, Variant(static_cast(boundaryValue))), qr); EXPECT_TRUE(err.ok()) << err.what(); - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant krOne = item[kFieldOne]; EXPECT_TRUE(krOne.Type().Is()); @@ -154,7 +154,9 @@ TEST_F(ReindexerApi, BtreeUnbuiltIndexIteratorsTest) { pos = ids2.size() - 1; while (bIt2.Next()) { EXPECT_TRUE(bIt2.Value() == ids2[pos]) << "iterator value = " << bIt2.Value() << "; expected value = " << ids2[pos]; - if (pos) --pos; + if (pos) { + --pos; + } } EXPECT_TRUE(pos == 0); EXPECT_TRUE(!bIt2.Next()); diff --git a/cpp_src/gtests/tests/unit/csv2jsonconverter.cc b/cpp_src/gtests/tests/unit/csv2jsonconverter.cc index c92f0c16c..ae407e276 100644 --- a/cpp_src/gtests/tests/unit/csv2jsonconverter.cc +++ b/cpp_src/gtests/tests/unit/csv2jsonconverter.cc @@ -57,7 +57,7 @@ std::vector parseCSVRow(std::string_view row) { return fields; } -std::string csv2json(std::string_view row, const std::vector &schema) { +std::string csv2json(std::string_view row, const std::vector& schema) { auto fields = parseCSVRow(row); if (schema.size() < fields.size()) { @@ -72,7 +72,7 @@ std::string csv2json(std::string_view row, const std::vector &schem try { gason::JsonParser().Parse(std::string_view{fields[i]}); builder.Raw(schema[i], fields[i]); - } catch (const gason::Exception &) { + } catch (const gason::Exception&) { builder.Raw(schema[i], '"' + fields[i] + '"'); } } diff --git a/cpp_src/gtests/tests/unit/csv2jsonconverter.h b/cpp_src/gtests/tests/unit/csv2jsonconverter.h index 68d9acaf6..1d5606140 100644 --- a/cpp_src/gtests/tests/unit/csv2jsonconverter.h +++ b/cpp_src/gtests/tests/unit/csv2jsonconverter.h @@ -6,6 +6,6 @@ namespace reindexer { std::vector parseCSVRow(std::string_view row); -std::string csv2json(std::string_view row, const std::vector &schema); +std::string csv2json(std::string_view row, const std::vector& schema); } // namespace reindexer \ No newline at end of file diff --git a/cpp_src/gtests/tests/unit/equalposition_tests.cc b/cpp_src/gtests/tests/unit/equalposition_tests.cc index 1dfa80a3d..5051fcdc7 100644 --- a/cpp_src/gtests/tests/unit/equalposition_tests.cc +++ b/cpp_src/gtests/tests/unit/equalposition_tests.cc @@ -30,7 +30,7 @@ void VerifyQueryResult(const QueryResults& qr, const std::vector& f EXPECT_TRUE(fields.size() == keys.size()); EXPECT_TRUE(keys.size() == condTypes.size()); size_t totalFound = 0; - for (auto &item : qr) { + for (auto& item : qr) { size_t len = INT_MAX; Item it = item.GetItem(false); @@ -45,8 +45,12 @@ void VerifyQueryResult(const QueryResults& qr, const std::vector& f bool equal = true; for (;;) { size_t key = 0; - while ((j < len) && !Compare(vals[key][j], keys[key], condTypes[key])) ++j; - if (eof()) break; + while ((j < len) && !Compare(vals[key][j], keys[key], condTypes[key])) { + ++j; + } + if (eof()) { + break; + } equal = true; while (++key < keys.size()) { equal &= Compare(vals[key][j], keys[key], condTypes[key]); @@ -60,7 +64,9 @@ void VerifyQueryResult(const QueryResults& qr, const std::vector& f } ++j; } - if (!equal) TEST_COUT << it.GetJSON() << std::endl; + if (!equal) { + TEST_COUT << it.GetJSON() << std::endl; + } } EXPECT_TRUE(totalFound == qr.Count()) << " totalFound=" << totalFound << ", qr.Count()=" << qr.Count(); } diff --git a/cpp_src/gtests/tests/unit/ft/ft_generic.cc b/cpp_src/gtests/tests/unit/ft/ft_generic.cc index e039bd551..a64ea47fc 100644 --- a/cpp_src/gtests/tests/unit/ft/ft_generic.cc +++ b/cpp_src/gtests/tests/unit/ft/ft_generic.cc @@ -66,7 +66,9 @@ TEST_P(FTGenericApi, CompositeSelect) { auto ritem(it.GetItem(false)); for (auto idx = 1; idx < ritem.NumFields(); idx++) { auto field = ritem[idx].Name(); - if (field == "id") continue; + if (field == "id") { + continue; + } auto it = data.find(ritem[field].As()); ASSERT_TRUE(it != data.end()); data.erase(it); @@ -103,7 +105,9 @@ TEST_P(FTGenericApi, CompositeSelectWithFields) { auto ritem(it.GetItem(false)); for (auto idx = 1; idx < ritem.NumFields(); idx++) { auto curField = ritem[idx].Name(); - if (curField != field) continue; + if (curField != field) { + continue; + } auto it = data.find(ritem[curField].As()); ASSERT_TRUE(it != data.end()); data.erase(it); @@ -151,7 +155,9 @@ TEST_P(FTGenericApi, MergeWithSameNSAndSelectFunctions) { auto ritem(it.GetItem(false)); for (auto idx = 1; idx < ritem.NumFields(); idx++) { auto curField = ritem[idx].Name(); - if (curField != field) continue; + if (curField != field) { + continue; + } auto it = data.find(ritem[curField].As()); ASSERT_TRUE(it != data.end()); data.erase(it); diff --git a/cpp_src/gtests/tests/unit/index_tuple_test.cc b/cpp_src/gtests/tests/unit/index_tuple_test.cc index c02519341..2218f63cb 100644 --- a/cpp_src/gtests/tests/unit/index_tuple_test.cc +++ b/cpp_src/gtests/tests/unit/index_tuple_test.cc @@ -26,8 +26,8 @@ class IndexTupleTest : public ReindexerApi { return createNS(ns, pattern); } - void DoTestDefault(const std::shared_ptr &reindexer, std::string_view ns, const reindexer::IndexDef &indexDef, - std::string_view pattern, std::string_view field, const VariantArray &expectedValues, + void DoTestDefault(const std::shared_ptr& reindexer, std::string_view ns, const reindexer::IndexDef& indexDef, + std::string_view pattern, std::string_view field, const VariantArray& expectedValues, std::string_view description) const { auto err = rt.reindexer->AddIndex(ns, indexDef); ASSERT_TRUE(err.ok()) << err.what() << "\n" << description; @@ -35,7 +35,7 @@ class IndexTupleTest : public ReindexerApi { validateResults(reindexer, ns, pattern, field, expectedValues, description); } - void DoTestEmpty(const std::shared_ptr &reindexer, std::string_view ns, const reindexer::IndexDef &indexDef, + void DoTestEmpty(const std::shared_ptr& reindexer, std::string_view ns, const reindexer::IndexDef& indexDef, std::string_view pattern, std::string_view description) const { auto err = reindexer->AddIndex(ns, indexDef); ASSERT_TRUE(err.ok()) << err.what(); @@ -43,8 +43,8 @@ class IndexTupleTest : public ReindexerApi { checkExpectations(reindexer, ns, pattern, description); } - void DoCallAndCheckError(const std::shared_ptr &reindexer, std::string_view ns, - const reindexer::IndexDef &indexDef, std::string_view errMsg) const { + void DoCallAndCheckError(const std::shared_ptr& reindexer, std::string_view ns, + const reindexer::IndexDef& indexDef, std::string_view errMsg) const { std::vector items; getItems(reindexer, ns, items); @@ -55,8 +55,8 @@ class IndexTupleTest : public ReindexerApi { checkItems(reindexer, ns, items); } - void ValidateReloadState(const std::shared_ptr &reindexer, std::string_view ns, std::string_view pattern, - std::string_view description, const std::string &storagePath) const { + void ValidateReloadState(const std::shared_ptr& reindexer, std::string_view ns, std::string_view pattern, + std::string_view description, const std::string& storagePath) const { auto err = rt.reindexer->CloseNamespace(ns); ASSERT_TRUE(err.ok()) << err.what(); @@ -72,9 +72,8 @@ class IndexTupleTest : public ReindexerApi { reindexer::fs::RmDirAll(storagePath); } - void SpecialCheckForNull(const std::shared_ptr &reindexer, std::string_view ns, - std::string_view firstItemPattern, std::string_view itemPattern, - std::string_view description, const std::string &storagePath) const { + void SpecialCheckForNull(const std::shared_ptr& reindexer, std::string_view ns, std::string_view firstItemPattern, + std::string_view itemPattern, std::string_view description, const std::string& storagePath) const { specialCheckForNull(reindexer, ns, firstItemPattern, itemPattern, description); validateReloadStateForNull(reindexer, ns, firstItemPattern, itemPattern, description, storagePath); } @@ -83,7 +82,7 @@ class IndexTupleTest : public ReindexerApi { private: static constexpr char kStoragePath[] = "/tmp/reindex/"; - static constexpr uint32_t itemNumber_ = 5; // NOTE: minimum 2 + static constexpr uint32_t itemNumber_ = 5; // NOTE: minimum 2 [[nodiscard]] std::string createNS(std::string_view ns, std::string_view itemPattern) { std::string storage; @@ -92,7 +91,7 @@ class IndexTupleTest : public ReindexerApi { return storage; } - void createNamespace(std::string_view ns, std::string &storagePath) { + void createNamespace(std::string_view ns, std::string& storagePath) { storagePath = kStoragePath; storagePath.append(ns); @@ -118,15 +117,17 @@ class IndexTupleTest : public ReindexerApi { } } - void checkIfItemJSONValid(QueryResults::Iterator &it, bool print = false) const { + void checkIfItemJSONValid(QueryResults::Iterator& it, bool print = false) const { reindexer::WrSerializer wrser; Error err = it.GetJSON(wrser, false); EXPECT_TRUE(err.ok()) << err.what(); - if (err.ok() && print) std::cout << wrser.Slice() << std::endl; + if (err.ok() && print) { + std::cout << wrser.Slice() << std::endl; + } } - void validateResults(const std::shared_ptr &reindexer, std::string_view ns, std::string_view pattern, - std::string_view field, const VariantArray &expectedValues, std::string_view description) const { + void validateResults(const std::shared_ptr& reindexer, std::string_view ns, std::string_view pattern, + std::string_view field, const VariantArray& expectedValues, std::string_view description) const { SCOPED_TRACE(description); QueryResults qr; @@ -154,7 +155,7 @@ class IndexTupleTest : public ReindexerApi { } } - void checkExpectations(const std::shared_ptr &reindexer, std::string_view ns, std::string_view pattern, + void checkExpectations(const std::shared_ptr& reindexer, std::string_view ns, std::string_view pattern, std::string_view description) const { SCOPED_TRACE(description); @@ -176,22 +177,21 @@ class IndexTupleTest : public ReindexerApi { } } - void getItems(const std::shared_ptr &reindexer, std::string_view ns, - std::vector &items) const { + void getItems(const std::shared_ptr& reindexer, std::string_view ns, std::vector& items) const { QueryResults qr; auto err = reindexer->Select("SELECT * FROM " + std::string(ns), qr); ASSERT_TRUE(err.ok()) << err.what(); items.clear(); items.reserve(qr.Count()); - for (auto &it : qr) { + for (auto& it : qr) { auto item = it.GetItem(false); items.emplace_back(item.GetJSON()); } } - void checkItems(const std::shared_ptr &reindexer, std::string_view ns, - const std::vector &items) const { + void checkItems(const std::shared_ptr& reindexer, std::string_view ns, + const std::vector& items) const { QueryResults qr; auto err = reindexer->Select("SELECT * FROM " + std::string(ns), qr); ASSERT_TRUE(err.ok()) << err.what(); @@ -201,15 +201,14 @@ class IndexTupleTest : public ReindexerApi { auto itQR = qr.begin(); auto endItems = items.cend(); auto endQR = qr.end(); - for ( ; (itItems != endItems) && (itQR != endQR); ++itItems, ++itQR) { + for (; (itItems != endItems) && (itQR != endQR); ++itItems, ++itQR) { auto item = itQR.GetItem(false); ASSERT_EQ(*itItems, item.GetJSON()); } } - void specialCheckForNull(const std::shared_ptr &reindexer, std::string_view ns, - std::string_view firstItemPattern, std::string_view itemPattern, - std::string_view description) const { + void specialCheckForNull(const std::shared_ptr& reindexer, std::string_view ns, std::string_view firstItemPattern, + std::string_view itemPattern, std::string_view description) const { SCOPED_TRACE(description); // first element should not update values, all others should be initialized to default values @@ -224,7 +223,7 @@ class IndexTupleTest : public ReindexerApi { Item item = it.GetItem(false); checkIfItemJSONValid(it); const auto json = item.GetJSON(); - const auto &pattern = (idx == IdStart)? firstItemPattern : itemPattern; + const auto& pattern = (idx == IdStart) ? firstItemPattern : itemPattern; const auto expJson = fmt::sprintf(pattern.data(), idx); ASSERT_EQ(json, expJson); @@ -232,9 +231,9 @@ class IndexTupleTest : public ReindexerApi { } } - void validateReloadStateForNull(const std::shared_ptr &reindexer, std::string_view ns, - std::string_view firstItemPattern, std::string_view itemPattern, - std::string_view description, const std::string &storagePath) const { + void validateReloadStateForNull(const std::shared_ptr& reindexer, std::string_view ns, + std::string_view firstItemPattern, std::string_view itemPattern, std::string_view description, + const std::string& storagePath) const { auto err = rt.reindexer->CloseNamespace(ns); ASSERT_TRUE(err.ok()) << err.what(); @@ -320,17 +319,13 @@ TEST_F(IndexTupleTest, SparseItemTest) { const auto storage = CreateSparseNamespace(ns); DoTestEmpty(rt.reindexer, ns, {"sparse1", {"fld1"}, "hash", "int", IndexOpts().Sparse()}, - R"({"id":%d,"fld1":1,"fld2":{"nested":"test"}})", - "add some sparse index to present nested field. Do nothing"); - DoCallAndCheckError(rt.reindexer, ns, {"sparse2", {"fld2"}, "hash", "int", IndexOpts().Sparse()}, - "Can't convert 'test' to number"); + R"({"id":%d,"fld1":1,"fld2":{"nested":"test"}})", "add some sparse index to present nested field. Do nothing"); + DoCallAndCheckError(rt.reindexer, ns, {"sparse2", {"fld2"}, "hash", "int", IndexOpts().Sparse()}, "Can't convert 'test' to number"); DoCallAndCheckError(rt.reindexer, ns, {"sparse3", {"fld2.nested"}, "hash", "int", IndexOpts().Sparse()}, "Can't convert 'test' to number"); DoTestEmpty(rt.reindexer, ns, {"sparse2", {"fld2"}, "hash", "string", IndexOpts().Sparse()}, - R"({"id":%d,"fld1":1,"fld2":{"nested":"test"}})", - "add some sparse index to present part path field. Do nothing"); - ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"fld1":1,"fld2":{"nested":"test"}})", - "reload ns (SparseItemTest)", storage); + R"({"id":%d,"fld1":1,"fld2":{"nested":"test"}})", "add some sparse index to present part path field. Do nothing"); + ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"fld1":1,"fld2":{"nested":"test"}})", "reload ns (SparseItemTest)", storage); } TEST_F(IndexTupleTest, NestedUpdateTest) { @@ -365,18 +360,20 @@ TEST_F(IndexTupleTest, ArrayTest) { R"("array":[],"arr_fld":[],"arr_fld2":[])", "arr_fld2", VariantArray{}.MarkArray(), "add another array index (chooses last of two). Add empty array"); DoTestDefault(rt.reindexer, ns, {"obj.new.array", {"obj.new.array"}, "hash", "int64", IndexOpts().Array(), 0}, - R"("array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}})", - "obj.new.array", VariantArray{}, "add new nested (only) index. Add empty array"); + R"("array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}})", "obj.new.array", VariantArray{}, + "add new nested (only) index. Add empty array"); DoTestDefault(rt.reindexer, ns, {"arr", "hash", "int64", IndexOpts().Array()}, - R"("array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}},"arr":[])", - "arr", VariantArray{}, "add new field with nested (only) indexes. Add empty array"); + R"("array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}},"arr":[])", "arr", VariantArray{}, + "add new field with nested (only) indexes. Add empty array"); DoCallAndCheckError(rt.reindexer, ns, {"arr_restriction", {"arr_fld3", "arr_fld4", "arr.some.arr_1st"}, "hash", "int64", IndexOpts().Array(), 0}, "Cannot add field with name 'arr_restriction' (jsonpath 'arr.some.arr_1st') and type 'int64' to namespace" " 'testNSArray'. Already exists json path 'arr' with type 'int64' in field 'arr'. Rewriting is impossible"); DoTestEmpty(rt.reindexer, ns, {"new_sparse_array", {"new_sparse_array"}, "hash", "int64", IndexOpts().Array().Sparse(), 0}, - R"({"id":%d,"array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}},"arr":[]})", "add new sparse array index. Do nothing"); - ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}},"arr":[]})", "reload ns (ArrayTest)", storage); + R"({"id":%d,"array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}},"arr":[]})", + "add new sparse array index. Do nothing"); + ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"array":[],"arr_fld":[],"arr_fld2":[],"obj":{"new":{"array":[]}},"arr":[]})", + "reload ns (ArrayTest)", storage); } TEST_F(IndexTupleTest, ArrayNestedTest) { @@ -462,8 +459,7 @@ TEST_F(IndexTupleTest, NullTest) { // update only one first item { - const std::string sql = "UPDATE testNSNull SET fld1 = null, fld2 = [null, null] WHERE id = " + - std::to_string(IdStart); + const std::string sql = "UPDATE testNSNull SET fld1 = null, fld2 = [null, null] WHERE id = " + std::to_string(IdStart); Query query = Query::FromSQL(sql); QueryResults qr; auto err = rt.reindexer->Update(query, qr); @@ -479,8 +475,8 @@ TEST_F(IndexTupleTest, NullTest) { ASSERT_TRUE(err.ok()) << err.what(); } - SpecialCheckForNull(rt.reindexer, ns, R"({"id":%d,"fld1":null,"fld2":["null","null"]})", - R"({"id":%d,"fld1":0,"fld2":[]})", "null values test", storage); + SpecialCheckForNull(rt.reindexer, ns, R"({"id":%d,"fld1":null,"fld2":["null","null"]})", R"({"id":%d,"fld1":0,"fld2":[]})", + "null values test", storage); } TEST_F(IndexTupleTest, FailTest) { @@ -489,8 +485,8 @@ TEST_F(IndexTupleTest, FailTest) { DoTestDefault(rt.reindexer, ns, {"nest", {"obj.nest"}, "hash", "int", IndexOpts()}, R"("obj":{"nest":0})", "nest", VariantArray{Variant{0}}, "add nest. Add default value"); - DoTestDefault(rt.reindexer, ns, {"idx", {"idx"}, "-", "bool", IndexOpts()}, R"("obj":{"nest":0},"idx":false)", - "idx", VariantArray{Variant{false}}, "add idx. Add default value"); + DoTestDefault(rt.reindexer, ns, {"idx", {"idx"}, "-", "bool", IndexOpts()}, R"("obj":{"nest":0},"idx":false)", "idx", + VariantArray{Variant{false}}, "add idx. Add default value"); ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"obj":{"nest":0},"idx":false})", "reload ns (FailTest)", storage); } @@ -499,8 +495,8 @@ TEST_F(IndexTupleTest, NestedArrayTest) { const auto storage = CreateArrayNamespace(ns); DoTestDefault(rt.reindexer, ns, {"obj.obj1.arr", {"obj.obj1.arr"}, "hash", "int", IndexOpts().Array()}, - R"("obj":{"val":10,"obj1":{"arr":[]}},"arr":[1,2,3])", "obj.obj1.arr", - VariantArray{}, "add obj.obj1.arr. Add default value"); - ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"obj":{"val":10,"obj1":{"arr":[]}},"arr":[1,2,3]})", - "reload ns (NestedArrayTest)", storage); + R"("obj":{"val":10,"obj1":{"arr":[]}},"arr":[1,2,3])", "obj.obj1.arr", VariantArray{}, + "add obj.obj1.arr. Add default value"); + ValidateReloadState(rt.reindexer, ns, R"({"id":%d,"obj":{"val":10,"obj1":{"arr":[]}},"arr":[1,2,3]})", "reload ns (NestedArrayTest)", + storage); } diff --git a/cpp_src/gtests/tests/unit/join_test.cc b/cpp_src/gtests/tests/unit/join_test.cc index f143594cc..bd50e388f 100644 --- a/cpp_src/gtests/tests/unit/join_test.cc +++ b/cpp_src/gtests/tests/unit/join_test.cc @@ -183,7 +183,9 @@ TEST_F(JoinSelectsApi, LeftJoinTest) { const reindexer::ItemRef& rowid = rowIt.GetItemRef(); auto itemIt = rowIt.GetJoined(); - if (itemIt.getJoinedItemsCount() == 0) continue; + if (itemIt.getJoinedItemsCount() == 0) { + continue; + } for (auto joinedFieldIt = itemIt.begin(); joinedFieldIt != itemIt.end(); ++joinedFieldIt) { reindexer::ItemImpl item2(joinedFieldIt.GetItem(0, joinQueryRes.getPayloadType(1), joinQueryRes.getTagsMatcher(1))); Variant authorIdKeyRef2 = item2.GetField(joinQueryRes.getPayloadType(1).FieldByName(authorid_fk)); @@ -198,7 +200,9 @@ TEST_F(JoinSelectsApi, LeftJoinTest) { for (auto rowIt : joinQueryRes) { IdType rowid = rowIt.GetItemRef().Id(); auto itemIt = rowIt.GetJoined(); - if (itemIt.getJoinedItemsCount() == 0) continue; + if (itemIt.getJoinedItemsCount() == 0) { + continue; + } auto joinedFieldIt = itemIt.begin(); for (int i = 0; i < joinedFieldIt.ItemsCount(); ++i) { reindexer::ItemImpl item(joinedFieldIt.GetItem(i, joinQueryRes.getPayloadType(1), joinQueryRes.getTagsMatcher(1))); @@ -296,7 +300,9 @@ TEST_F(JoinSelectsApi, JoinTestSorting) { Variant key = item[authorid]; auto itemIt = rowIt.GetJoined(); - if (itemIt.getJoinedItemsCount() == 0) continue; + if (itemIt.getJoinedItemsCount() == 0) { + continue; + } auto joinedFieldIt = itemIt.begin(); Variant prevJoinedValue; @@ -443,17 +449,23 @@ TEST_F(JoinSelectsApi, JoinsEasyStressTest) { std::vector threads; for (size_t i = 0; i < 20; ++i) { threads.push_back(std::thread(selectTh)); - if (i % 2 == 0) threads.push_back(std::thread(removeTh)); - if (i % 4 == 0) threads.push_back(std::thread([this, since, count]() { FillBooksNamespace(since, count); })); + if (i % 2 == 0) { + threads.push_back(std::thread(removeTh)); + } + if (i % 4 == 0) { + threads.push_back(std::thread([this, since, count]() { FillBooksNamespace(since, count); })); + } since += 1000; } - for (size_t i = 0; i < threads.size(); ++i) threads[i].join(); + for (size_t i = 0; i < threads.size(); ++i) { + threads[i].join(); + } } TEST_F(JoinSelectsApi, JoinPreResultStoreValuesOptimizationStressTest) { using reindexer::JoinedSelector; static const std::string rightNs = "rightNs"; - static constexpr char const* data = "data"; + static constexpr const char* data = "data"; static constexpr int maxDataValue = 10; static constexpr int maxRightNsRowCount = maxDataValue * JoinedSelector::MaxIterationsForPreResultStoreValuesOptimization(); static constexpr int maxLeftNsRowCount = 10000; @@ -461,7 +473,9 @@ TEST_F(JoinSelectsApi, JoinPreResultStoreValuesOptimizationStressTest) { static std::vector leftNs; if (leftNs.empty()) { leftNs.reserve(leftNsCount); - for (size_t i = 0; i < leftNsCount; ++i) leftNs.push_back("leftNs" + std::to_string(i)); + for (size_t i = 0; i < leftNsCount; ++i) { + leftNs.push_back("leftNs" + std::to_string(i)); + } } const auto createNs = [this](const std::string& ns) { @@ -492,13 +506,17 @@ TEST_F(JoinSelectsApi, JoinPreResultStoreValuesOptimizationStressTest) { // about 50% of queries will use the optimization Query q{Query(leftNs[i]).InnerJoin(data, data, CondEq, Query(rightNs).Where(data, CondEq, rand() % maxDataValue))}; QueryResults qres; - while (!start) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + while (!start) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } Error err = rt.reindexer->Select(q, qres); ASSERT_TRUE(err.ok()) << err.what(); }); } start = true; - for (auto& th : threads) th.join(); + for (auto& th : threads) { + th.join(); + } } static void checkForAllowedJsonTags(const std::vector& tags, gason::JsonValue jsonValue) { diff --git a/cpp_src/gtests/tests/unit/namespace_test.cc b/cpp_src/gtests/tests/unit/namespace_test.cc index 5f0379291..134fe5dba 100644 --- a/cpp_src/gtests/tests/unit/namespace_test.cc +++ b/cpp_src/gtests/tests/unit/namespace_test.cc @@ -92,8 +92,8 @@ TEST_F(NsApi, AddTooManyIndexes) { } TEST_F(NsApi, TruncateNamespace) { - TruncateNamespace([&](const std::string &nsName) { return rt.reindexer->TruncateNamespace(nsName); }); - TruncateNamespace([&](const std::string &nsName) { + TruncateNamespace([&](const std::string& nsName) { return rt.reindexer->TruncateNamespace(nsName); }); + TruncateNamespace([&](const std::string& nsName) { QueryResults qr; return rt.reindexer->Select("TRUNCATE " + nsName, qr); }); @@ -214,7 +214,7 @@ TEST_F(NsApi, UpdateIndex) { rt.OpenNamespace(default_namespace); DefineNamespaceDataset(default_namespace, {IndexDeclaration{idIdxName, "hash", "int", IndexOpts().PK(), 0}}); - auto const wrongIdx = reindexer::IndexDef(idIdxName, reindexer::JsonPaths{"wrongPath"}, "hash", "double", IndexOpts().PK()); + const auto wrongIdx = reindexer::IndexDef(idIdxName, reindexer::JsonPaths{"wrongPath"}, "hash", "double", IndexOpts().PK()); err = rt.reindexer->UpdateIndex(default_namespace, wrongIdx); ASSERT_FALSE(err.ok()); EXPECT_EQ(err.what(), "Unsupported combination of field 'id' type 'double' and index type 'hash'"); @@ -228,12 +228,12 @@ TEST_F(NsApi, UpdateIndex) { ASSERT_TRUE(err.ok()) << err.what(); auto nsDefIt = - std::find_if(nsDefs.begin(), nsDefs.end(), [&](const reindexer::NamespaceDef &nsDef) { return nsDef.name == default_namespace; }); + std::find_if(nsDefs.begin(), nsDefs.end(), [&](const reindexer::NamespaceDef& nsDef) { return nsDef.name == default_namespace; }); ASSERT_TRUE(nsDefIt != nsDefs.end()) << "Namespace " + default_namespace + " is not found"; - auto &indexes = nsDefIt->indexes; - auto receivedIdx = std::find_if(indexes.begin(), indexes.end(), [&](const reindexer::IndexDef &idx) { return idx.name_ == idIdxName; }); + auto& indexes = nsDefIt->indexes; + auto receivedIdx = std::find_if(indexes.begin(), indexes.end(), [&](const reindexer::IndexDef& idx) { return idx.name_ == idIdxName; }); ASSERT_TRUE(receivedIdx != indexes.end()) << "Expect index was created, but it wasn't"; reindexer::WrSerializer newIdxSer; @@ -254,7 +254,7 @@ TEST_F(NsApi, QueryperfstatsNsDummyTest) { rt.OpenNamespace(default_namespace); DefineNamespaceDataset(default_namespace, {IndexDeclaration{idIdxName, "hash", "int", IndexOpts().PK(), 0}}); - const char *const configNs = "#config"; + const char* const configNs = "#config"; Item item = NewItem(configNs); ASSERT_TRUE(item.Status().ok()) << item.Status().what(); @@ -304,7 +304,7 @@ TEST_F(NsApi, QueryperfstatsNsDummyTest) { ASSERT_TRUE(err.ok()) << err.what(); }; - auto getPerformanceParams = [&](QueryPerformance &performanceRes) { + auto getPerformanceParams = [&](QueryPerformance& performanceRes) { QueryResults qres; auto err = rt.reindexer->Select(Query("#queriesperfstats").Where("query", CondEq, Variant(querySql)), qres); ASSERT_TRUE(err.ok()) << err.what(); @@ -312,7 +312,7 @@ TEST_F(NsApi, QueryperfstatsNsDummyTest) { auto qr = rt.Select(Query("#queriesperfstats")); ASSERT_TRUE(err.ok()) << err.what(); ASSERT_GT(qr.Count(), 0) << "#queriesperfstats table is empty!"; - for (auto &it : qr) { + for (auto& it : qr) { std::cout << it.GetItem(false).GetJSON() << std::endl; } } @@ -345,11 +345,13 @@ TEST_F(NsApi, QueryperfstatsNsDummyTest) { } } -static void checkIfItemJSONValid(QueryResults::Iterator &it, bool print = false) { +static void checkIfItemJSONValid(QueryResults::Iterator& it, bool print = false) { reindexer::WrSerializer wrser; Error err = it.GetJSON(wrser, false); ASSERT_TRUE(err.ok()) << err.what(); - if (err.ok() && print) std::cout << wrser.Slice() << std::endl; + if (err.ok() && print) { + std::cout << wrser.Slice() << std::endl; + } } TEST_F(NsApi, TestUpdateIndexedField) { @@ -442,7 +444,7 @@ TEST_F(NsApi, TestUpdateNewFieldCheckTmVersion) { DefineDefaultNamespace(); FillDefaultNamespace(); - auto check = [this](const Query &query, int tmVersion) { + auto check = [this](const Query& query, int tmVersion) { auto qrUpdate = rt.UpdateQR(query); ASSERT_EQ(qrUpdate.Count(), 1); ASSERT_EQ(qrUpdate.getTagsMatcher(0).version(), tmVersion); @@ -472,7 +474,7 @@ TEST_F(NsApi, TestUpdateNewFieldCheckTmVersion) { check(updateQuery, tmVersion); } -static void checkUpdateArrayFieldResults(std::string_view updateFieldPath, const QueryResults &results, const VariantArray &values) { +static void checkUpdateArrayFieldResults(std::string_view updateFieldPath, const QueryResults& results, const VariantArray& values) { for (auto it : results) { Item item = it.GetItem(false); VariantArray val = item[updateFieldPath]; @@ -495,8 +497,8 @@ static void checkUpdateArrayFieldResults(std::string_view updateFieldPath, const } } -static void updateArrayField(const std::shared_ptr &reindexer, const std::string &ns, - std::string_view updateFieldPath, const VariantArray &values) { +static void updateArrayField(const std::shared_ptr& reindexer, const std::string& ns, + std::string_view updateFieldPath, const VariantArray& values) { QueryResults qrUpdate; { SCOPED_TRACE("Checking array update"); @@ -610,8 +612,8 @@ TEST_F(NsApi, TestUpdateIndexedArrayField2) { ASSERT_NE(pos, std::string::npos) << "'indexed_array_field' was not updated properly" << json; } -static void addAndSetNonindexedField(const std::shared_ptr &reindexer, const std::string &ns, - const std::string &updateFieldPath) { +static void addAndSetNonindexedField(const std::shared_ptr& reindexer, const std::string& ns, + const std::string& updateFieldPath) { QueryResults qrUpdate; Query updateQuery{Query(ns).Where("nested.bonus", CondGe, Variant(500)).Set(updateFieldPath, static_cast(777))}; Error err = reindexer->Update(updateQuery, qrUpdate); @@ -648,7 +650,7 @@ TEST_F(NsApi, TestAddAndSetNonindexedField3) { addAndSetNonindexedField(rt.reindexer, default_namespace, "nested3.nested4.extrabonus"); } -static void setAndCheckArrayItem(const std::shared_ptr &reindexer, std::string_view ns, std::string_view fullItemPath, +static void setAndCheckArrayItem(const std::shared_ptr& reindexer, std::string_view ns, std::string_view fullItemPath, std::string_view jsonPath, std::string_view description, int i = IndexValueType::NotSet, int j = IndexValueType::NotSet) { SCOPED_TRACE(description); @@ -668,7 +670,7 @@ static void setAndCheckArrayItem(const std::shared_ptr &re // Check if array item with appropriate index equals to 777 and // is a type of Int64. - auto checkItem = [](const VariantArray &values, size_t index, std::string_view description) { + auto checkItem = [](const VariantArray& values, size_t index, std::string_view description) { SCOPED_TRACE(description); ASSERT_LT(index, values.size()); ASSERT_TRUE(values[index].Type().Is()); @@ -777,8 +779,8 @@ TEST_F(NsApi, TestAddAndSetArrayField4) { } } -static void dropArrayItem(const std::shared_ptr &reindexer, const std::string &ns, const std::string &fullItemPath, - const std::string &jsonPath, int i = IndexValueType::NotSet, int j = IndexValueType::NotSet) { +static void dropArrayItem(const std::shared_ptr& reindexer, const std::string& ns, const std::string& fullItemPath, + const std::string& jsonPath, int i = IndexValueType::NotSet, int j = IndexValueType::NotSet) { // Drop item(s) with name = fullItemPath QueryResults qrUpdate; Query updateQuery{Query(ns).Where("nested.bonus", CondGe, Variant(500)).Drop(fullItemPath)}; @@ -986,8 +988,8 @@ TEST_F(NsApi, ExtendArrayWithExpressions) { } } -static void validateResults(const std::shared_ptr &reindexer, const Query &baseQuery, const Query &testQuery, - std::string_view ns, std::string_view pattern, std::string_view field, const VariantArray &expectedValues, +static void validateResults(const std::shared_ptr& reindexer, const Query& baseQuery, const Query& testQuery, + std::string_view ns, std::string_view pattern, std::string_view field, const VariantArray& expectedValues, std::string_view description, int resCount = 5) { SCOPED_TRACE(description); @@ -1546,7 +1548,7 @@ TEST_F(NsApi, ArrayRemoveWithSql) { } } -static void validateUpdateJSONResults(const std::shared_ptr &reindexer, const Query &updateQuery, +static void validateUpdateJSONResults(const std::shared_ptr& reindexer, const Query& updateQuery, std::string_view expectation, std::string_view description) { SCOPED_TRACE(description); @@ -1747,7 +1749,7 @@ TEST_F(NsApi, UpdateObjectsArray4) { constexpr char kIndexName[] = "objects.array.field"; const Query kBaseQuery = Query(default_namespace).Where("id", CondSet, {1199, 1201, 1203, 1210, 1240}); - auto ValidateResults = [this, &kBaseQuery](const QueryResults &qr, std::string_view pattern, std::string_view description) { + auto ValidateResults = [this, &kBaseQuery](const QueryResults& qr, std::string_view pattern, std::string_view description) { SCOPED_TRACE(description); // Check initial result ASSERT_EQ(qr.Count(), 5); @@ -1772,7 +1774,7 @@ TEST_F(NsApi, UpdateObjectsArray4) { } }; - for (const auto &index : indexTypes) { + for (const auto& index : indexTypes) { Error err = rt.reindexer->TruncateNamespace(default_namespace); ASSERT_TRUE(err.ok()) << err.what(); // 2. Refill NS @@ -1973,14 +1975,14 @@ TEST_F(NsApi, UpdateArrayIndexFieldWithSeveralJsonPaths) { enum class OpT { Insert, Update }; - auto makeFieldsList = [&fieldsValues](const reindexer::fast_hash_set &indexes, OpT type) { + auto makeFieldsList = [&fieldsValues](const reindexer::fast_hash_set& indexes, OpT type) { auto quote = type == OpT::Insert ? '"' : '\''; std::vector Values::*list = type == OpT::Insert ? &Values::valsList : &Values::newValsList; const auto fieldsListTmplt = type == OpT::Insert ? R"("%sfield%d": [%s])" : R"(%sfield%d = [%s])"; std::string fieldsList; for (int idx : indexes) { std::string fieldList; - for (const auto &data : fieldsValues[idx].*list) { + for (const auto& data : fieldsValues[idx].*list) { fieldList += std::string(fieldList.empty() ? "" : ", ") + quote + data + quote; } fieldsList += fmt::sprintf(fieldsListTmplt, fieldsList.empty() ? "" : ", ", idx, fieldList); @@ -1988,12 +1990,12 @@ TEST_F(NsApi, UpdateArrayIndexFieldWithSeveralJsonPaths) { return fieldsList; }; - auto makeItem = [&makeFieldsList](int id, const reindexer::fast_hash_set &indexes) { + auto makeItem = [&makeFieldsList](int id, const reindexer::fast_hash_set& indexes) { auto list = makeFieldsList(indexes, OpT::Insert); return fmt::sprintf(R"({"id": %d%s})", id, (list.empty() ? "" : ", ") + list); }; - auto makeUpdate = [this, &makeFieldsList](int id, const reindexer::fast_hash_set &indexes) { + auto makeUpdate = [this, &makeFieldsList](int id, const reindexer::fast_hash_set& indexes) { return fmt::sprintf("UPDATE %s SET %s WHERE id = %d", default_namespace, makeFieldsList(indexes, OpT::Update), id); }; @@ -2042,8 +2044,8 @@ TEST_F(NsApi, UpdateArrayIndexFieldWithSeveralJsonPaths) { auto item = qr.begin().GetItem(false); for (auto idx : testCases[i].expected()) { int varArrCnt = 0; - for (auto &&var : VariantArray(item[fmt::sprintf("field%d", idx)])) { - const auto &data = testCases[i].updateIdxs.count(idx) ? fieldsValues[idx].newValsList : fieldsValues[idx].valsList; + for (auto&& var : VariantArray(item[fmt::sprintf("field%d", idx)])) { + const auto& data = testCases[i].updateIdxs.count(idx) ? fieldsValues[idx].newValsList : fieldsValues[idx].valsList; ASSERT_EQ(var.As(), data[varArrCnt++]); } } @@ -2114,33 +2116,33 @@ TEST_F(NsApi, UpdateOutOfBoundsArrayField) { const std::string_view name; const std::string baseUpdateExpr; const std::vector arrayIdx; - const std::function createQueryF; + const std::function createQueryF; }; const std::vector cases = { {.name = "update-index-array-field", .baseUpdateExpr = "indexed_array_field[%d]", .arrayIdx = {9, 10, 100, 10000, 5000000}, .createQueryF = - [&](const std::string &path) { + [&](const std::string& path) { return Query(default_namespace).Where("id", CondEq, kTargetID).Set(path, static_cast(777)); }}, {.name = "update-non-indexed-array-field", .baseUpdateExpr = "array_field[%d]", .arrayIdx = {3, 4, 100, 10000, 5000000}, .createQueryF = - [&](const std::string &path) { + [&](const std::string& path) { return Query(default_namespace).Where("id", CondEq, kTargetID).Set(path, static_cast(777)); }}, {.name = "update-object-array-field", .baseUpdateExpr = "nested.nested_array[%d]", .arrayIdx = {3, 4, 100, 10000, 5000000}, - .createQueryF = [&](const std::string &path) { + .createQueryF = [&](const std::string& path) { return Query(default_namespace) .Where("id", CondEq, kTargetID) .SetObject(path, Variant(std::string(R"({"id":5,"name":"fifth", "prices":[3,5,5]})")), false); }}}; - for (auto &c : cases) { + for (auto& c : cases) { SCOPED_TRACE(c.name); for (auto idx : c.arrayIdx) { @@ -2282,8 +2284,8 @@ TEST_F(NsApi, CheckIndexedArrayItem) { } } -static void checkFieldConversion(const std::shared_ptr &reindexer, const std::string &ns, - const std::string &updateFieldPath, const VariantArray &newValue, const VariantArray &updatedValue, +static void checkFieldConversion(const std::shared_ptr& reindexer, const std::string& ns, + const std::string& updateFieldPath, const VariantArray& newValue, const VariantArray& updatedValue, reindexer::KeyValueType sourceType, bool expectFail) { const Query selectQuery{Query(ns).Where("id", CondGe, Variant("500"))}; QueryResults qrUpdate; @@ -2292,7 +2294,9 @@ static void checkFieldConversion(const std::shared_ptr &re Error err = reindexer->Update(updateQuery, qrUpdate); if (expectFail) { if (err.ok()) { - for (auto it : qrUpdate) checkIfItemJSONValid(it, true); + for (auto it : qrUpdate) { + checkIfItemJSONValid(it, true); + } } ASSERT_TRUE(!err.ok()); } else { @@ -2308,7 +2312,7 @@ static void checkFieldConversion(const std::shared_ptr &re Item item = it.GetItem(false); VariantArray val = item[updateFieldPath.c_str()]; ASSERT_TRUE(val.size() == updatedValue.size()); - for (const Variant &v : val) { + for (const Variant& v : val) { ASSERT_TRUE(v.Type().IsSame(sourceType)) << v.Type().Name(); } ASSERT_TRUE(val == updatedValue); @@ -2444,7 +2448,7 @@ TEST_F(NsApi, TestUpdatePkFieldNoConditions) { ASSERT_GT(qr.Count(), 0); int i = 0; - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant intFieldVal = item[idIdxName]; ASSERT_EQ(static_cast(intFieldVal), i + qrCount.totalCount + 100); @@ -2461,7 +2465,7 @@ TEST_F(NsApi, TestUpdateIndexArrayWithNull) { ASSERT_TRUE(err.ok()) << err.what(); ASSERT_EQ(qr.Count(), 1); - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); VariantArray fieldVal = item[indexedArrayField]; ASSERT_TRUE(fieldVal.empty()); @@ -2553,7 +2557,7 @@ TEST_F(NsApi, TestUpdateNonIndexFieldWithNull) { ASSERT_TRUE(err.ok()) << err.what(); ASSERT_EQ(qr.Count(), 1); - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant fieldVal = item["extra"]; ASSERT_TRUE(fieldVal.Type().Is()); @@ -2710,7 +2714,7 @@ TEST_F(NsApi, TestUpdateFieldWithFunction) { ASSERT_GT(qr.Count(), 0); int i = 1; - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant intFieldVal = item[intField]; Variant extraFieldVal = item["extra"]; @@ -2734,7 +2738,7 @@ TEST_F(NsApi, TestUpdateFieldWithExpressions) { ASSERT_GT(qr.Count(), 0); int i = 1; - for (auto &it : qr) { + for (auto& it : qr) { Item item = it.GetItem(false); Variant intFieldVal = item[intField]; Variant extraFieldVal = item["extra"]; @@ -2746,7 +2750,7 @@ TEST_F(NsApi, TestUpdateFieldWithExpressions) { } } -static void checkQueryDsl(const Query &src) { +static void checkQueryDsl(const Query& src) { Query dst; const std::string dsl = src.GetJSON(); Error err = dst.FromJSON(dsl); @@ -2829,7 +2833,7 @@ TEST_F(NsApi, TestModifyQueriesSqlEncoder) { checkQueryDsl(q7); } -static void generateObject(reindexer::JsonBuilder &builder, const std::string &prefix, ReindexerApi *rtapi) { +static void generateObject(reindexer::JsonBuilder& builder, const std::string& prefix, ReindexerApi* rtapi) { builder.Put(prefix + "ID", rand() % 1000); builder.Put(prefix + "Name", rtapi->RandString()); builder.Put(prefix + "Rating", rtapi->RandString()); @@ -2839,7 +2843,9 @@ static void generateObject(reindexer::JsonBuilder &builder, const std::string &p builder.Put(prefix + "Subsription", bool(rand() % 100 > 50 ? 1 : 0)); { auto idsArray = builder.Array(prefix + "IDS"); - for (auto id : rtapi->RandIntVector(10, 10, 1000)) idsArray.Put(0, id); + for (auto id : rtapi->RandIntVector(10, 10, 1000)) { + idsArray.Put(0, id); + } } { auto homogeneousArray = builder.Array(prefix + "HomogeneousValues"); @@ -2857,7 +2863,7 @@ static void generateObject(reindexer::JsonBuilder &builder, const std::string &p } } -void addObjectsArray(reindexer::JsonBuilder &builder, bool withInnerArray, ReindexerApi *rtapi) { +void addObjectsArray(reindexer::JsonBuilder& builder, bool withInnerArray, ReindexerApi* rtapi) { size_t size = rand() % 10 + 5; reindexer::JsonBuilder array = builder.Array("object"); for (size_t i = 0; i < size; ++i) { @@ -2883,7 +2889,9 @@ TEST_F(NsApi, MsgPackEncodingTest) { jsonBuilder.Put("superName", RandString()); { auto priceArray = jsonBuilder.Array("superPrices"); - for (auto price : RandIntVector(10, 10, 1000)) priceArray.Put(0, price); + for (auto price : RandIntVector(10, 10, 1000)) { + priceArray.Put(0, price); + } } { reindexer::JsonBuilder objectBuilder = jsonBuilder.Object("nested1"); @@ -2908,7 +2916,7 @@ TEST_F(NsApi, MsgPackEncodingTest) { size_t offset = 0; Item item2 = NewItem(default_namespace); - err = item2.FromMsgPack(std::string_view(reinterpret_cast(wrSer2.Buf()), wrSer2.Len()), offset); + err = item2.FromMsgPack(std::string_view(reinterpret_cast(wrSer2.Buf()), wrSer2.Len()), offset); ASSERT_TRUE(err.ok()) << err.what(); std::string_view json1(item.GetJSON()); @@ -2925,7 +2933,7 @@ TEST_F(NsApi, MsgPackEncodingTest) { Item item = NewItem(default_namespace); ASSERT_TRUE(item.Status().ok()) << item.Status().what(); - Error err = item.FromMsgPack(std::string_view(reinterpret_cast(wrSer1.Buf()), wrSer1.Len()), offset); + Error err = item.FromMsgPack(std::string_view(reinterpret_cast(wrSer1.Buf()), wrSer1.Len()), offset); ASSERT_TRUE(err.ok()) << err.what(); err = rt.reindexer->Update(default_namespace, item, qr); @@ -2936,7 +2944,7 @@ TEST_F(NsApi, MsgPackEncodingTest) { } reindexer::WrSerializer wrSer3; - for (auto &it : qr) { + for (auto& it : qr) { const auto err = it.GetMsgPack(wrSer3, false); ASSERT_TRUE(err.ok()) << err.what(); } @@ -2947,7 +2955,7 @@ TEST_F(NsApi, MsgPackEncodingTest) { Item item = NewItem(default_namespace); ASSERT_TRUE(item.Status().ok()) << item.Status().what(); - Error err = item.FromMsgPack(std::string_view(reinterpret_cast(wrSer3.Buf()), wrSer3.Len()), offset); + Error err = item.FromMsgPack(std::string_view(reinterpret_cast(wrSer3.Buf()), wrSer3.Len()), offset); ASSERT_TRUE(err.ok()) << err.what(); std::string_view json(item.GetJSON()); @@ -3011,22 +3019,22 @@ TEST_F(NsApi, DeleteLastItems) { } TEST_F(NsApi, IncorrectNsName) { - auto check = [&](const std::vector &names, auto func) { - for (const auto &v : names) { + auto check = [&](const std::vector& names, auto func) { + for (const auto& v : names) { func(v); } }; std::vector variants = {"tes@t1", "@test1", "test1@", "tes#t1", "#test1", "test1#", "test 1", " test1", "test1 ", "'test1'", "\"test1\"", "test1", "/test1", "test1,test2"}; - auto open = [&](const std::string &name) { + auto open = [&](const std::string& name) { Error err = rt.reindexer->OpenNamespace(name); ASSERT_FALSE(err.ok()); ASSERT_EQ(err.what(), "Namespace name contains invalid character. Only alphas, digits,'_','-', are allowed"); }; check(variants, open); - auto add = [&](const std::string &name) { + auto add = [&](const std::string& name) { reindexer::NamespaceDef nsDef(name); Error err = rt.reindexer->AddNamespace(nsDef); ASSERT_FALSE(err.ok()); @@ -3034,7 +3042,7 @@ TEST_F(NsApi, IncorrectNsName) { }; check(variants, add); - auto rename = [&](const std::string &name) { + auto rename = [&](const std::string& name) { const std::string kNsName("test3"); reindexer::NamespaceDef nsDef(kNsName); Error err = rt.reindexer->AddNamespace(nsDef); @@ -3081,7 +3089,7 @@ TEST_F(NsApi, MultiDimensionalArrayQueryErrors) { "explicitly set in the query"; QueryResults qr; - auto &rx = *rt.reindexer; + auto& rx = *rt.reindexer; // Set tuple to the field auto err = rx.Update(Query(default_namespace).Set(field, {Variant{VariantArray::Create({1, 2, 3})}}), qr); diff --git a/cpp_src/gtests/tests/unit/protobuf_test.cc b/cpp_src/gtests/tests/unit/protobuf_test.cc index 2cc866167..885f0c65f 100644 --- a/cpp_src/gtests/tests/unit/protobuf_test.cc +++ b/cpp_src/gtests/tests/unit/protobuf_test.cc @@ -395,7 +395,9 @@ TEST_F(ReindexerApi, ProtobufSchemaFromNsSchema) { } } auto collection = jsonBuilder.Array("Collection"); - for (int i = 0; i < 10; ++i) collection.Put(0, i); + for (int i = 0; i < 10; ++i) { + collection.Put(0, i); + } collection.End(); jsonBuilder.End(); diff --git a/cpp_src/gtests/tests/unit/queries_test.cc b/cpp_src/gtests/tests/unit/queries_test.cc index b394f18fc..a040635bb 100644 --- a/cpp_src/gtests/tests/unit/queries_test.cc +++ b/cpp_src/gtests/tests/unit/queries_test.cc @@ -36,7 +36,9 @@ TEST_F(QueriesApi, QueriesStandardTestSet) { Error err = rt.reindexer->Delete(default_namespace, it->second); ASSERT_TRUE(err.ok()) << err.what(); it = items.erase(it); - if (++itemsCount == 4000) break; + if (++itemsCount == 4000) { + break; + } } FillDefaultNamespace(0, 500, 0); @@ -47,7 +49,9 @@ TEST_F(QueriesApi, QueriesStandardTestSet) { Error err = rt.reindexer->Delete(default_namespace, it->second); ASSERT_TRUE(err.ok()) << err.what(); it = items.erase(it); - if (++itemsCount == 5000) break; + if (++itemsCount == 5000) { + break; + } } for (size_t i = 0; i < 5000; ++i) { @@ -664,7 +668,9 @@ TEST_F(QueriesApi, DslGenerateParse) { static std::vector generateForcedSortOrder(int maxValue, size_t size) { std::set res; - while (res.size() < size) res.insert(rand() % maxValue); + while (res.size() < size) { + res.insert(rand() % maxValue); + } return {res.cbegin(), res.cend()}; } @@ -801,7 +807,9 @@ TEST_F(QueriesApi, JoinByNotIndexField) { ser.Reset(); reindexer::JsonBuilder json{ser}; json.Put("id", i); - if (i % 2 == 1) json.Put("f", i); + if (i % 2 == 1) { + json.Put("f", i); + } json.End(); Item item = rt.reindexer->NewItem(nsName); ASSERT_TRUE(item.Status().ok()) << item.Status().what(); @@ -1018,16 +1026,22 @@ TEST_F(QueriesApi, TestCsvProcessingWithSchema) { } { auto data0 = json.Array(fmt::sprintf("Array_level0_id_%d", id)); - for (int i = 0; i < 5; ++i) data0.Put(nullptr, fmt::sprintf("array_data_0_%d", i)); + for (int i = 0; i < 5; ++i) { + data0.Put(nullptr, fmt::sprintf("array_data_0_%d", i)); + } data0.Put(nullptr, std::string("\"arr_quoted_field(\"this is quoted too\")\"")); } { auto data0 = json.Object(fmt::sprintf("Object_level0_id_%d", id)); - for (int i = 0; i < 5; ++i) data0.Put(fmt::sprintf("Object_%d", i), fmt::sprintf("object_data_0_%d", i)); + for (int i = 0; i < 5; ++i) { + data0.Put(fmt::sprintf("Object_%d", i), fmt::sprintf("object_data_0_%d", i)); + } data0.Put("Quoted Field lvl0", std::string("\"obj_quoted_field(\"this is quoted too\")\"")); { auto data1 = data0.Object(fmt::sprintf("Object_level1_id_%d", id)); - for (int j = 0; j < 5; ++j) data1.Put(fmt::sprintf("objectData1 %d", j), fmt::sprintf("objectData1 %d", j)); + for (int j = 0; j < 5; ++j) { + data1.Put(fmt::sprintf("objectData1 %d", j), fmt::sprintf("objectData1 %d", j)); + } data1.Put("Quoted Field lvl1", std::string("\"obj_quoted_field(\"this is quoted too\")\"")); } } diff --git a/cpp_src/gtests/tests/unit/query_aggregate_strict_mode_test.h b/cpp_src/gtests/tests/unit/query_aggregate_strict_mode_test.h index 95b40c5fb..f9a3bdb85 100644 --- a/cpp_src/gtests/tests/unit/query_aggregate_strict_mode_test.h +++ b/cpp_src/gtests/tests/unit/query_aggregate_strict_mode_test.h @@ -160,7 +160,11 @@ void QueryAggStrictModeTest(const std::unique_ptr& client) { } }; - for (const auto& aggType : aggTypes) - for (const auto& [field, modes] : scenarios) - for (const auto& [mode, err] : modes) testUnit(field, aggType, mode, err); + for (const auto& aggType : aggTypes) { + for (const auto& [field, modes] : scenarios) { + for (const auto& [mode, err] : modes) { + testUnit(field, aggType, mode, err); + } + } + } } diff --git a/cpp_src/gtests/tests/unit/replication_master_master_test.cc b/cpp_src/gtests/tests/unit/replication_master_master_test.cc index 705eb87c6..bfbd46a2f 100644 --- a/cpp_src/gtests/tests/unit/replication_master_master_test.cc +++ b/cpp_src/gtests/tests/unit/replication_master_master_test.cc @@ -14,7 +14,9 @@ class ReplicationSlaveSlaveApi : public ::testing::Test { class ServerControlVec : public std::vector { public: ~ServerControlVec() { - for (auto& node : *this) node.Stop(); + for (auto& node : *this) { + node.Stop(); + } } }; @@ -714,8 +716,12 @@ TEST_F(ReplicationSlaveSlaveApi, NodeWithMasterAndSlaveNs1) { } { std::vector results_data; - for (unsigned int i = 0; i < n; i++) results_data.push_back(c1 + i); - for (unsigned int i = 0; i < n; i++) results_data.push_back(c2 + i); + for (unsigned int i = 0; i < n; i++) { + results_data.push_back(c1 + i); + } + for (unsigned int i = 0; i < n; i++) { + results_data.push_back(c2 + i); + } std::vector results_3; testns3.GetData(slave, results_3); @@ -771,8 +777,12 @@ TEST_F(ReplicationSlaveSlaveApi, NodeWithMasterAndSlaveNs2) { } { std::vector results_data; - for (unsigned int i = 0; i < n; i++) results_data.push_back(c1 + i); - for (unsigned int i = 0; i < n; i++) results_data.push_back(c2 + i); + for (unsigned int i = 0; i < n; i++) { + results_data.push_back(c1 + i); + } + for (unsigned int i = 0; i < n; i++) { + results_data.push_back(c2 + i); + } std::vector results_3; testns3.GetData(slave, results_3); @@ -979,7 +989,9 @@ TEST_F(ReplicationSlaveSlaveApi, RestrictUpdates) { ASSERT_TRUE(err.ok()) << err.what(); master.Get()->api.Upsert(nsName, item); ASSERT_TRUE(err.ok()) << err.what(); - if (i % 100 == 0) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + if (i % 100 == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } } }; @@ -1258,7 +1270,7 @@ class ServerIdChange : public ReplicationSlaveSlaveApi, public ::testing::WithPa ASSERT_TRUE(err.ok()) << err.what(); master.Get()->api.Upsert("ns1", item); } - }; + } void ChangeServerId(bool isMaster, ServerControl& node, int newServerId, int port) { if (isMaster) { diff --git a/cpp_src/gtests/tests/unit/replication_test.cc b/cpp_src/gtests/tests/unit/replication_test.cc index b038feef8..3a4449ff8 100644 --- a/cpp_src/gtests/tests/unit/replication_test.cc +++ b/cpp_src/gtests/tests/unit/replication_test.cc @@ -69,7 +69,9 @@ TEST_F(ReplicationLoadApi, Base) { SetWALSize(masterId_, 50000, "some"); for (size_t i = 0; i < 2; ++i) { - if (i % 3 == 0) DeleteFromMaster(); + if (i % 3 == 0) { + DeleteFromMaster(); + } SetWALSize(masterId_, (int64_t(i) + 1) * 25000, "some1"); FillData(1000); SetWALSize(masterId_, (int64_t(i) + 1) * 50000, "some"); @@ -112,7 +114,9 @@ TEST_F(ReplicationLoadApi, BaseTagsMatcher) { SetSchema(masterId_, "some1", kReplTestSchema2); FillData(1000); for (size_t i = 0; i < 2; ++i) { - if (i == 1) DeleteFromMaster(); + if (i == 1) { + DeleteFromMaster(); + } FillData(1000); } StartServer(1); @@ -312,7 +316,9 @@ TEST_F(ReplicationLoadApi, DISABLED_BasicTestNoMasterRestart) { }); for (size_t i = 0; i < 10; ++i) { - if (i % 3 == 0) DeleteFromMaster(); + if (i % 3 == 0) { + DeleteFromMaster(); + } FillData(1000); SimpleSelect(0); SimpleSelect(rand() % 3 + 1); @@ -341,7 +347,9 @@ TEST_F(ReplicationLoadApi, SingleSlaveTest) { counter++; RestartServer(i); - if (counter % 3 == 0) DeleteFromMaster(); + if (counter % 3 == 0) { + DeleteFromMaster(); + } } }); @@ -375,7 +383,9 @@ TEST_F(ReplicationLoadApi, DuplicatePKSlaveTest) { bool isFirst = true; for (const auto id : ids) { - if (!isFirst) changedIds += ", "; + if (!isFirst) { + changedIds += ", "; + } changedIds += std::to_string(id); isFirst = false; } diff --git a/cpp_src/gtests/tests/unit/rpcclient_test.cc b/cpp_src/gtests/tests/unit/rpcclient_test.cc index 153684471..f0181fe67 100644 --- a/cpp_src/gtests/tests/unit/rpcclient_test.cc +++ b/cpp_src/gtests/tests/unit/rpcclient_test.cc @@ -130,12 +130,16 @@ TEST_F(RPCClientTestApi, SeveralDsnReconnect) { ASSERT_TRUE(res.ok()) << res.what(); for (size_t i = 0; i < 100; ++i) { - if (CheckIfFakeServerConnected(uris[0])) break; + if (CheckIfFakeServerConnected(uris[0])) { + break; + } std::this_thread::sleep_for(std::chrono::milliseconds(10)); } for (size_t i = 0; i < 100; ++i) { - if (rx.Status().ok()) break; + if (rx.Status().ok()) { + break; + } std::this_thread::sleep_for(std::chrono::milliseconds(10)); } @@ -146,7 +150,9 @@ TEST_F(RPCClientTestApi, SeveralDsnReconnect) { for (size_t j = 0; j < 10; ++j) { client::QueryResults qr; res = rx.Select(queryConfingNs, qr); - if (res.ok()) break; + if (res.ok()) { + break; + } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } ASSERT_TRUE(res.ok()) << res.what(); @@ -404,7 +410,7 @@ TEST_F(RPCClientTestApi, CoroSelectTimeout) { for (size_t i = 0; i < kCorCount; ++i) { ASSERT_TRUE(finished[i]); } - Error const err = StopServer(); + const Error err = StopServer(); ASSERT_TRUE(err.ok()) << err.what(); } diff --git a/cpp_src/gtests/tests/unit/rtree_test.cc b/cpp_src/gtests/tests/unit/rtree_test.cc index 578bef44d..68ab5bc61 100644 --- a/cpp_src/gtests/tests/unit/rtree_test.cc +++ b/cpp_src/gtests/tests/unit/rtree_test.cc @@ -19,7 +19,9 @@ struct Compare; template <> struct Compare { bool operator()(reindexer::Point lhs, reindexer::Point rhs) const noexcept { - if (lhs.X() == rhs.X()) return lhs.Y() < rhs.Y(); + if (lhs.X() == rhs.X()) { + return lhs.Y() < rhs.Y(); + } return lhs.X() < rhs.X(); } }; @@ -101,7 +103,9 @@ static void TestIterators() { size_t dublicates = 0; for (size_t i = 0; i < 10000 + dublicates; ++i) { const auto res = tree.insert(reindexer::randPoint(kRange)); - if (!res.second) ++dublicates; + if (!res.second) { + ++dublicates; + } ASSERT_TRUE(tree.Check()); auto it = tree.begin(), end = tree.end(); auto cit = tree.cbegin(), cend = tree.cend(); @@ -152,7 +156,9 @@ static void TestSearch() { const reindexer::Point point{randPoint(kRange)}; const double distance = randBinDouble(0, 100); for (const auto& r : data) { - if (reindexer::DWithin(point, r, distance)) DWithinVisitor.Add(r); + if (reindexer::DWithin(point, r, distance)) { + DWithinVisitor.Add(r); + } } tree.DWithin(point, distance, DWithinVisitor); @@ -251,7 +257,9 @@ static void TestMap() { const reindexer::Point point{randPoint(kRange)}; const double distance = randBinDouble(0, 100); for (const auto& r : data) { - if (reindexer::DWithin(point, r.first, distance)) visitor.Add(r); + if (reindexer::DWithin(point, r.first, distance)) { + visitor.Add(r); + } } map.DWithin(point, distance, visitor); ASSERT_EQ(visitor.Size(), 0); diff --git a/cpp_src/gtests/tests/unit/selector_plan_test.cc b/cpp_src/gtests/tests/unit/selector_plan_test.cc index f7978eba4..df77be49e 100644 --- a/cpp_src/gtests/tests/unit/selector_plan_test.cc +++ b/cpp_src/gtests/tests/unit/selector_plan_test.cc @@ -231,7 +231,9 @@ TEST_F(SelectorPlanTest, SortByUnbuiltBtreeIndex) { } for (const char* additionalSearchField : {kFieldId, kFieldTree1, kFieldTree2, kFieldHash}) { - if (additionalSearchField == searchField) continue; + if (additionalSearchField == searchField) { + continue; + } for (const Query& query : {Query(unbuiltBtreeNs).Explain().Where(additionalSearchField, CondEq, RandInt()).Where(searchField, cond, RandInt()), Query(unbuiltBtreeNs) @@ -336,7 +338,9 @@ TEST_F(SelectorPlanTest, SortByUnbuiltBtreeIndex) { } for (const char* additionalSearchField : {kFieldId, kFieldTree1, kFieldTree2, kFieldHash}) { - if (additionalSearchField == searchField) continue; + if (additionalSearchField == searchField) { + continue; + } for (const Query& query : {Query(unbuiltBtreeNs) .Explain() .Where(additionalSearchField, CondEq, RandInt()) diff --git a/cpp_src/gtests/tests/unit/tolal_lru_cache.cc b/cpp_src/gtests/tests/unit/tolal_lru_cache.cc index f3e7890f7..80832acba 100644 --- a/cpp_src/gtests/tests/unit/tolal_lru_cache.cc +++ b/cpp_src/gtests/tests/unit/tolal_lru_cache.cc @@ -145,7 +145,7 @@ TEST(LruCache, StressTest) { threads.emplace_back([&]() { for (auto i = 0; i < iterCount; i++) { auto idx = rand() % qs.size(); - auto const& qce = qs.at(idx); + const auto& qce = qs.at(idx); QueryCacheKey ckey{qce, kCountCachedKeyMode, static_cast(nullptr)}; auto cached = cache.Get(ckey); diff --git a/cpp_src/gtests/tests/unit/ttl_index_api.cc b/cpp_src/gtests/tests/unit/ttl_index_api.cc index 51c2b6ecb..b2d8910d8 100644 --- a/cpp_src/gtests/tests/unit/ttl_index_api.cc +++ b/cpp_src/gtests/tests/unit/ttl_index_api.cc @@ -27,7 +27,9 @@ TEST_F(TtlIndexApi, ItemsVanishingAfterInsertRemove) { threads.emplace_back(std::thread(&TtlIndexApi::RemoveItemsSlowly, this)); threads.emplace_back(std::thread(&TtlIndexApi::SelectData, this)); - for (size_t i = 0; i < threads.size(); ++i) threads[i].join(); + for (size_t i = 0; i < threads.size(); ++i) { + threads[i].join(); + } std::this_thread::sleep_for(std::chrono::milliseconds(2000)); count = WaitForVanishing(); diff --git a/cpp_src/gtests/tests/unit/uuid.cc b/cpp_src/gtests/tests/unit/uuid.cc index 96c20878e..09801c0c9 100644 --- a/cpp_src/gtests/tests/unit/uuid.cc +++ b/cpp_src/gtests/tests/unit/uuid.cc @@ -155,7 +155,9 @@ static void fillItemThroughJson(reindexer::Item& item, int id, Values& v template static void fillItem(reindexer::Item& item, int id, Values& value) { - if (rand() % 3 == 0) return fillItemThroughJson(item, id, value); + if (rand() % 3 == 0) { + return fillItemThroughJson(item, id, value); + } ASSERT_TRUE(item.Status().ok()) << item.Status().what(); fillRndValue(value); item["id"] = id; @@ -359,7 +361,9 @@ TEST(UUID, DropIndex) { std::vector> testValues1; testValues1.reserve(uuidValues.size()); - for (const auto& value : uuidValues) testValues1.emplace_back(value.scalar, value.array); + for (const auto& value : uuidValues) { + testValues1.emplace_back(value.scalar, value.array); + } test(rx, testValues1, EmptyValues::CouldBeEmpty); err = rx.DropIndex(nsName, {"uuid_a"}); @@ -371,7 +375,9 @@ TEST(UUID, DropIndex) { if (value.array) { std::vector strUuidArray; strUuidArray.reserve(value.array->size()); - for (auto uuid : *value.array) strUuidArray.emplace_back(uuid); + for (auto uuid : *value.array) { + strUuidArray.emplace_back(uuid); + } testValues2.emplace_back(value.scalar, std::move(strUuidArray)); } else { testValues2.emplace_back(value.scalar, std::nullopt); @@ -400,7 +406,9 @@ TEST(UUID, AddIndex) { if (value.array) { std::vector strValues; strValues.reserve(value.array->size()); - for (auto uuid : *value.array) strValues.emplace_back(uuid); + for (auto uuid : *value.array) { + strValues.emplace_back(uuid); + } testValues1.emplace_back(value.scalar, std::move(strValues)); } else { testValues1.emplace_back(value.scalar, std::nullopt); @@ -446,7 +454,9 @@ TEST(UUID, UpdateIndexUuidToString) { std::vector> testValues1; testValues1.reserve(uuidValues.size()); - for (const auto& value : uuidValues) testValues1.emplace_back(value.scalar, value.array); + for (const auto& value : uuidValues) { + testValues1.emplace_back(value.scalar, value.array); + } test(rx, testValues1); err = rx.UpdateIndex(nsName, reindexer::IndexDef{"uuid_a", "hash", "string", IndexOpts().Array()}); @@ -458,7 +468,9 @@ TEST(UUID, UpdateIndexUuidToString) { if (value.array) { std::vector strUuidArray; strUuidArray.reserve(value.array->size()); - for (auto uuid : *value.array) strUuidArray.emplace_back(uuid); + for (auto uuid : *value.array) { + strUuidArray.emplace_back(uuid); + } testValues2.emplace_back(value.scalar, std::move(strUuidArray)); } else { testValues2.emplace_back(value.scalar, std::nullopt); @@ -487,7 +499,9 @@ TEST(UUID, UpdateIndexStringToUuid) { std::vector> testValues1; testValues1.reserve(strUuidValues.size()); - for (const auto& value : strUuidValues) testValues1.emplace_back(value.scalar, value.array); + for (const auto& value : strUuidValues) { + testValues1.emplace_back(value.scalar, value.array); + } test(rx, testValues1); err = rx.UpdateIndex(nsName, reindexer::IndexDef{"uuid_a", "hash", "uuid", IndexOpts().Array()}); @@ -499,7 +513,9 @@ TEST(UUID, UpdateIndexStringToUuid) { if (value.array) { std::vector uuidArray; uuidArray.reserve(value.array->size()); - for (const auto& uuid : *value.array) uuidArray.emplace_back(uuid); + for (const auto& uuid : *value.array) { + uuidArray.emplace_back(uuid); + } testValues2.emplace_back(value.scalar, std::move(uuidArray)); } else { testValues2.emplace_back(value.scalar, std::nullopt); @@ -528,7 +544,9 @@ TEST(UUID, AddArrayUuidIndexOnNotArrayField) { std::vector> testValues; testValues.reserve(strUuidValues.size()); - for (const auto& value : strUuidValues) testValues.emplace_back(value.scalar, value.array); + for (const auto& value : strUuidValues) { + testValues.emplace_back(value.scalar, value.array); + } test(rx, testValues); } catch (const reindexer::Error& e) { ASSERT_TRUE(false) << e.what() << std::endl; diff --git a/cpp_src/gtests/tests/unit/value_by_json_path.cc b/cpp_src/gtests/tests/unit/value_by_json_path.cc index ebf460ece..7c162d019 100644 --- a/cpp_src/gtests/tests/unit/value_by_json_path.cc +++ b/cpp_src/gtests/tests/unit/value_by_json_path.cc @@ -91,7 +91,9 @@ TEST_F(ReindexerApi, SelectByJsonPath) { std::string dumpField = "str_" + pk; snprintf(json, sizeof(json) - 1, jsonPattern, pk.c_str(), dumpField.c_str(), i); - if (i >= 5) properIntValues.push_back(i); + if (i >= 5) { + properIntValues.push_back(i); + } err = item.FromJSON(json); ASSERT_TRUE(err.ok()) << err.what(); diff --git a/cpp_src/gtests/tools.h b/cpp_src/gtests/tools.h index 7095193b9..d597d23ac 100644 --- a/cpp_src/gtests/tools.h +++ b/cpp_src/gtests/tools.h @@ -10,7 +10,9 @@ inline bool isUuidDelimPos(unsigned i) noexcept { } inline std::string randStrUuid() { - if (rand() % 1000 == 0) return std::string{nilUUID}; + if (rand() % 1000 == 0) { + return std::string{nilUUID}; + } std::string strUuid; strUuid.reserve(reindexer::Uuid::kStrFormLen); for (size_t i = 0; i < reindexer::Uuid::kStrFormLen; ++i) { diff --git a/cpp_src/net/connectinstatscollector.h b/cpp_src/net/connectinstatscollector.h index fa1a8dff8..77c54d10d 100644 --- a/cpp_src/net/connectinstatscollector.h +++ b/cpp_src/net/connectinstatscollector.h @@ -32,7 +32,7 @@ class connection_stats_collector { std::shared_ptr get_stat() const noexcept { return stat_; } - void attach(ev::dynamic_loop &loop) noexcept; + void attach(ev::dynamic_loop& loop) noexcept; void detach() noexcept; void restart(); void stop() noexcept; @@ -51,7 +51,7 @@ class connection_stats_collector { void update_send_buf_size(size_t size) noexcept { stat_->send_buf_bytes.store(size, std::memory_order_relaxed); } protected: - void stats_check_cb(ev::periodic &watcher, int) noexcept; + void stats_check_cb(ev::periodic& watcher, int) noexcept; private: std::shared_ptr stat_; diff --git a/cpp_src/net/connection.cc b/cpp_src/net/connection.cc index 219895efb..c8ee193ac 100644 --- a/cpp_src/net/connection.cc +++ b/cpp_src/net/connection.cc @@ -5,7 +5,7 @@ namespace reindexer { namespace net { template -Connection::Connection(socket &&s, ev::dynamic_loop &loop, bool enableStat, size_t readBufSize, size_t writeBufSize, int idleTimeout) +Connection::Connection(socket&& s, ev::dynamic_loop& loop, bool enableStat, size_t readBufSize, size_t writeBufSize, int idleTimeout) : sock_(std::move(s)), curEvents_(0), wrBuf_(writeBufSize), @@ -22,7 +22,7 @@ Connection::~Connection() { } template -void Connection::restart(socket &&s) { +void Connection::restart(socket&& s) { assertrx(!sock_.valid()); sock_ = std::move(s); wrBuf_.clear(); @@ -36,12 +36,14 @@ void Connection::restart(socket &&s) { } template -void Connection::attach(ev::dynamic_loop &loop) { +void Connection::attach(ev::dynamic_loop& loop) { assertrx(!attached_); io_.set(this); io_.set(loop); if (sock_.valid()) { - if (curEvents_) io_.start(sock_.fd(), curEvents_); + if (curEvents_) { + io_.start(sock_.fd(), curEvents_); + } clientAddr_ = sock_.addr(); } timeout_.set(this); @@ -81,15 +83,19 @@ void Connection::closeConn() { } timeout_.stop(); async_.stop(); - if (stats_) stats_->stop(); + if (stats_) { + stats_->stop(); + } onClose(); closeConn_ = false; } // Generic callback template -void Connection::callback(ev::io & /*watcher*/, int revents) { - if (ev::ERROR & revents) return; +void Connection::callback(ev::io& /*watcher*/, int revents) { + if (ev::ERROR & revents) { + return; + } ++rwCounter_; if (revents & ev::READ) { @@ -124,7 +130,9 @@ void Connection::write_cb() { ssize_t written = sock_.send(chunks); int err = sock_.last_error(); - if (written < 0 && err == EINTR) continue; + if (written < 0 && err == EINTR) { + continue; + } if (written < 0) { if (!socket::would_block(err)) { @@ -135,12 +143,18 @@ void Connection::write_cb() { } ssize_t toWrite = 0; - for (auto &chunk : chunks) toWrite += chunk.size(); + for (auto& chunk : chunks) { + toWrite += chunk.size(); + } wrBuf_.erase(written); - if (stats_) stats_->update_write_stats(written, wrBuf_.data_size()); + if (stats_) { + stats_->update_write_stats(written, wrBuf_.data_size()); + } - if (written < toWrite) return; + if (written < toWrite) { + return; + } } if (closeConn_) { closeConn(); @@ -155,7 +169,9 @@ typename Connection::ReadResT Connection::read_cb() { ssize_t nread = sock_.recv(it); int err = sock_.last_error(); - if (nread < 0 && err == EINTR) continue; + if (nread < 0 && err == EINTR) { + continue; + } if ((nread < 0 && !socket::would_block(err)) || nread == 0) { // Setting SO_LINGER with 0 timeout to avoid TIME_WAIT state on client @@ -173,12 +189,14 @@ typename Connection::ReadResT Connection::read_cb() { } } } - if (nread < ssize_t(it.size()) || !rdBuf_.available()) return ReadResT::Default; + if (nread < ssize_t(it.size()) || !rdBuf_.available()) { + return ReadResT::Default; + } } return ReadResT::Default; } template -void Connection::timeout_cb(ev::periodic & /*watcher*/, int /*time*/) { +void Connection::timeout_cb(ev::periodic& /*watcher*/, int /*time*/) { const bool isActive = lastCheckRWCounter_ != rwCounter_; lastCheckRWCounter_ = rwCounter_; if (isActive) { @@ -196,7 +214,7 @@ void Connection::timeout_cb(ev::periodic & /*watcher*/, int /*time*/) { closeConn(); } template -void Connection::async_cb(ev::async &) { +void Connection::async_cb(ev::async&) { callback(io_, ev::WRITE); } diff --git a/cpp_src/net/connection.h b/cpp_src/net/connection.h index e877d2d06..928395cad 100644 --- a/cpp_src/net/connection.h +++ b/cpp_src/net/connection.h @@ -35,7 +35,7 @@ using reindexer::cbuf; template class Connection { public: - Connection(socket &&s, ev::dynamic_loop &loop, bool enableStat, size_t readBufSize = kConnReadbufSize, + Connection(socket&& s, ev::dynamic_loop& loop, bool enableStat, size_t readBufSize = kConnReadbufSize, size_t writeBufSize = kConnWriteBufSize, int idleTimeout = -1); virtual ~Connection(); @@ -47,18 +47,16 @@ class Connection { virtual void onClose() = 0; // Generic callback - void callback(ev::io &watcher, int revents); + void callback(ev::io& watcher, int revents); void write_cb(); ReadResT read_cb(); - void async_cb(ev::async &watcher); - void timeout_cb(ev::periodic &watcher, int); + void async_cb(ev::async& watcher); + void timeout_cb(ev::periodic& watcher, int); void closeConn(); - void attach(ev::dynamic_loop &loop); + void attach(ev::dynamic_loop& loop); void detach(); - void restart(socket &&s); - - ssize_t async_read(); + void restart(socket&& s); socket sock_; ev::io io_; diff --git a/cpp_src/net/cproto/args.cc b/cpp_src/net/cproto/args.cc index efa7394bf..af5736e44 100644 --- a/cpp_src/net/cproto/args.cc +++ b/cpp_src/net/cproto/args.cc @@ -6,7 +6,7 @@ namespace reindexer { namespace net { namespace cproto { -void Args::Unpack(Serializer &ser) { +void Args::Unpack(Serializer& ser) { resize(0); unsigned count = ser.GetVarUint(); @@ -15,16 +15,16 @@ void Args::Unpack(Serializer &ser) { } } -void Args::Pack(WrSerializer &ser) const { +void Args::Pack(WrSerializer& ser) const { ser.PutVarUint(size()); - for (auto &arg : *this) { + for (auto& arg : *this) { ser.PutVariant(arg); } } -void Args::Dump(WrSerializer &wrser) const { +void Args::Dump(WrSerializer& wrser) const { wrser << '{'; - for (const auto &arg : *this) { + for (const auto& arg : *this) { if (&arg != &at(0)) { wrser << ", "; } diff --git a/cpp_src/net/cproto/args.h b/cpp_src/net/cproto/args.h index dfcbe3e8e..2035a9ada 100644 --- a/cpp_src/net/cproto/args.h +++ b/cpp_src/net/cproto/args.h @@ -11,9 +11,9 @@ using Arg = Variant; class Args : public h_vector { public: using h_vector::h_vector; - void Unpack(Serializer &ser); - void Pack(WrSerializer &ser) const; - void Dump(WrSerializer &wrser) const; + void Unpack(Serializer& ser); + void Pack(WrSerializer& ser) const; + void Dump(WrSerializer& wrser) const; }; } // namespace cproto } // namespace net diff --git a/cpp_src/net/cproto/clientconnection.cc b/cpp_src/net/cproto/clientconnection.cc index 241cb1caa..96977412c 100644 --- a/cpp_src/net/cproto/clientconnection.cc +++ b/cpp_src/net/cproto/clientconnection.cc @@ -18,7 +18,7 @@ bool ClientConnection::ConnectData::ThereAreReconnectOptions() const { return en bool ClientConnection::ConnectData::CurrDsnFailed(int failedDsnIdx) const { return failedDsnIdx == validEntryIdx; } int ClientConnection::ConnectData::GetNextDsnIndex() const { return (validEntryIdx.load(std::memory_order_acquire) + 1) % entries.size(); } -ClientConnection::ClientConnection(ev::dynamic_loop &loop, ConnectData *connectData, ConnectionFailCallback connectionFailCallback) +ClientConnection::ClientConnection(ev::dynamic_loop& loop, ConnectData* connectData, ConnectionFailCallback connectionFailCallback) : ConnectionMT(socket(), loop, false), state_(ConnInit), completions_(kMaxCompletions), @@ -38,7 +38,7 @@ ClientConnection::ClientConnection(ev::dynamic_loop &loop, ConnectData *connectD deadlineTimer_.set(this); deadlineTimer_.set(loop); reconnect_.set(loop); - reconnect_.set([this](ev::async &sig) { + reconnect_.set([this](ev::async& sig) { disconnect(); sig.loop.break_loop(); }); @@ -65,15 +65,17 @@ void ClientConnection::connectInternal() noexcept { mtx_.unlock(); assertrx(connectData_->validEntryIdx < int(connectData_->entries.size())); - ConnectData::Entry &connectEntry = connectData_->entries[actualDsnIdx_]; + ConnectData::Entry& connectEntry = connectData_->entries[actualDsnIdx_]; std::string port = connectEntry.uri.port().length() ? connectEntry.uri.port() : std::string("6534"); std::string dbName = connectEntry.uri.path(); std::string userName = connectEntry.uri.username(); std::string password = connectEntry.uri.password(); - if (dbName[0] == '/') dbName = dbName.substr(1); + if (dbName[0] == '/') { + dbName = dbName.substr(1); + } enableCompression_ = connectEntry.opts.enableCompression; - auto completion = [this](const RPCAnswer &ans, ClientConnection *) { + auto completion = [this](const RPCAnswer& ans, ClientConnection*) { std::unique_lock lck(mtx_); lastError_ = ans.Status(); state_ = ans.Status().ok() ? ConnConnected : ConnFailed; @@ -106,16 +108,18 @@ void ClientConnection::connectInternal() noexcept { } } -void ClientConnection::failInternal(const Error &error) { +void ClientConnection::failInternal(const Error& error) { std::unique_lock lck(mtx_); - if (lastError_.ok()) lastError_ = error; + if (lastError_.ok()) { + lastError_ = error; + } closeConn_ = true; } int ClientConnection::PendingCompletions() { int ret = 0; - for (auto &c : completions_) { - for (RPCCompletion *cc = &c; cc; cc = cc->next.get()) { + for (auto& c : completions_) { + for (RPCCompletion* cc = &c; cc; cc = cc->next.get()) { if (cc->used) { ret++; } @@ -148,10 +152,12 @@ Error ClientConnection::CheckConnection() { return lastError_; } -void ClientConnection::keep_alive_cb(ev::periodic &, int) { - if (terminate_.load(std::memory_order_acquire)) return; +void ClientConnection::keep_alive_cb(ev::periodic&, int) { + if (terminate_.load(std::memory_order_acquire)) { + return; + } call( - [this](RPCAnswer &&ans, ClientConnection *) { + [this](RPCAnswer&& ans, ClientConnection*) { if (!ans.Status().ok()) { failInternal(ans.Status()); closeConn(); @@ -161,11 +167,13 @@ void ClientConnection::keep_alive_cb(ev::periodic &, int) { callback(io_, ev::WRITE); } -void ClientConnection::deadline_check_cb(ev::timer &, int) { +void ClientConnection::deadline_check_cb(ev::timer&, int) { now_ += kDeadlineCheckInterval; - for (auto &c : completions_) { - for (RPCCompletion *cc = &c; cc; cc = cc->next.get()) { - if (!cc->used) continue; + for (auto& c : completions_) { + for (RPCCompletion* cc = &c; cc; cc = cc->next.get()) { + if (!cc->used) { + continue; + } bool expired = (cc->deadline.count() && cc->deadline.count() <= now_); if (expired || (cc->cancelCtx && cc->cancelCtx->IsCancelable() && (cc->cancelCtx->GetCancelType() == CancelType::Explicit))) { Error err(expired ? errTimeout : errCanceled, expired ? "Request deadline exceeded" : "Canceled"); @@ -216,11 +224,12 @@ void ClientConnection::onClose() { std::unique_lock lck(mtx_); wrBuf_.clear(); - if (lastError_.ok()) + if (lastError_.ok()) { lastError_ = Error(errNetwork, "Socket connection to %s closed", connectData_ && actualDsnIdx_ < int(connectData_->entries.size()) ? connectData_->entries[actualDsnIdx_].uri.hostname() : ""); + } closeConn_ = false; State prevState = state_; state_ = ConnClosing; @@ -230,16 +239,19 @@ void ClientConnection::onClose() { keep_alive_.stop(); deadlineTimer_.stop(); - for (auto &c : tmpCompletions) { - for (RPCCompletion *cc = &c; cc; cc = cc->next.get()) + for (auto& c : tmpCompletions) { + for (RPCCompletion* cc = &c; cc; cc = cc->next.get()) { if (cc->used && cc->cmd != kCmdLogin && cc->cmd != kCmdPing) { cc->cmpl(RPCAnswer(lastError_), this); } + } } std::unique_ptr tmpUpdatesHandler(updatesHandler_.release(std::memory_order_acq_rel)); - if (tmpUpdatesHandler) (*tmpUpdatesHandler)(RPCAnswer(lastError_), this); + if (tmpUpdatesHandler) { + (*tmpUpdatesHandler)(RPCAnswer(lastError_), this); + } lck.lock(); bufCond_.notify_all(); lck.unlock(); @@ -264,8 +276,10 @@ ClientConnection::ReadResT ClientConnection::onRead() { std::string uncompressed; while (!closeConn_) { - auto len = rdBuf_.peek(reinterpret_cast(&hdr), sizeof(hdr)); - if (len < sizeof(hdr)) return ReadResT::Default; + auto len = rdBuf_.peek(reinterpret_cast(&hdr), sizeof(hdr)); + if (len < sizeof(hdr)) { + return ReadResT::Default; + } if (hdr.magic != kCprotoMagic) { failInternal(Error(errNetwork, "Invalid cproto magic=%08x", hdr.magic)); @@ -283,7 +297,9 @@ ClientConnection::ReadResT ClientConnection::onRead() { rdBuf_.reserve(size_t(hdr.len) + sizeof(hdr) + 0x1000); } - if ((rdBuf_.size() - sizeof(hdr)) < size_t(hdr.len)) return ReadResT::Default; + if ((rdBuf_.size() - sizeof(hdr)) < size_t(hdr.len)) { + return ReadResT::Default; + } rdBuf_.erase(sizeof(hdr)); @@ -311,7 +327,7 @@ ClientConnection::ReadResT ClientConnection::onRead() { ans.status_ = Error(static_cast(errCode), std::string{errMsg}); } ans.data_ = span(ser.Buf() + ser.Pos(), ser.Len() - ser.Pos()); - } catch (const Error &err) { + } catch (const Error& err) { failInternal(err); return ReadResT::Default; } @@ -320,14 +336,14 @@ ClientConnection::ReadResT ClientConnection::onRead() { auto handler = updatesHandler_.release(std::memory_order_acq_rel); if (handler) { (*handler)(std::move(ans), this); - Completion *expected = nullptr; + Completion* expected = nullptr; if (!updatesHandler_.compare_exchange_strong(expected, handler, std::memory_order_acq_rel)) { delete handler; } } } else { auto complPtr = completions_.data(); - RPCCompletion *completion = &completions_[hdr.seq % completions_.size()]; + RPCCompletion* completion = &completions_[hdr.seq % completions_.size()]; for (; completion; completion = completion->next.get()) { if (!completion->used || completion->seq != hdr.seq) { @@ -380,7 +396,7 @@ Args RPCAnswer::GetArgs(int minArgs) const { Error RPCAnswer::Status() const { return status_; } -chunk ClientConnection::packRPC(CmdCode cmd, uint32_t seq, const Args &args, const Args &ctxArgs) { +chunk ClientConnection::packRPC(CmdCode cmd, uint32_t seq, const Args& args, const Args& ctxArgs) { CProtoHeader hdr; hdr.len = 0; hdr.magic = kCprotoMagic; @@ -392,7 +408,7 @@ chunk ClientConnection::packRPC(CmdCode cmd, uint32_t seq, const Args &args, con WrSerializer ser(wrBuf_.get_chunk()); - ser.Write(std::string_view(reinterpret_cast(&hdr), sizeof(hdr))); + ser.Write(std::string_view(reinterpret_cast(&hdr), sizeof(hdr))); args.Pack(ser); ctxArgs.Pack(ser); if (hdr.compressed) { @@ -403,12 +419,12 @@ chunk ClientConnection::packRPC(CmdCode cmd, uint32_t seq, const Args &args, con ser.Write(compressed); } assertrx(ser.Len() < size_t(std::numeric_limits::max())); - reinterpret_cast(ser.Buf())->len = ser.Len() - sizeof(hdr); + reinterpret_cast(ser.Buf())->len = ser.Len() - sizeof(hdr); return ser.DetachChunk(); } -ClientConnection::CallReturn ClientConnection::call(const Completion &cmpl, const CommandParams &opts, const Args &args) { +ClientConnection::CallReturn ClientConnection::call(const Completion& cmpl, const CommandParams& opts, const Args& args) { if (opts.cancelCtx) { switch (opts.cancelCtx->GetCancelType()) { case CancelType::Explicit: @@ -481,7 +497,7 @@ ClientConnection::CallReturn ClientConnection::call(const Completion &cmpl, cons CounterGuardIR32 cg(bufWait_); struct { uint32_t seq; - RPCCompletion *cmpl; + RPCCompletion* cmpl; } arg = {seq, completion}; bufCond_.wait(lck, [this, &arg]() { @@ -504,7 +520,9 @@ ClientConnection::CallReturn ClientConnection::call(const Completion &cmpl, cons completion = &completions_[seq % completions_.size()]; } while (completion->used) { - if (!completion->next) completion->next.reset(new RPCCompletion); + if (!completion->next) { + completion->next.reset(new RPCCompletion); + } completion = completion->next.get(); } } @@ -518,7 +536,7 @@ ClientConnection::CallReturn ClientConnection::call(const Completion &cmpl, cons try { wrBuf_.write(std::move(data)); - } catch (Error &e) { + } catch (Error& e) { fprintf(stderr, "RxClientConnection::wrBuf_.write exception: '%s'\n", e.what().c_str()); completion->used = false; lck.unlock(); diff --git a/cpp_src/net/cproto/clientconnection.h b/cpp_src/net/cproto/clientconnection.h index 05040b2d7..eeef53050 100644 --- a/cpp_src/net/cproto/clientconnection.h +++ b/cpp_src/net/cproto/clientconnection.h @@ -28,17 +28,21 @@ class RPCAnswer { public: Error Status() const; Args GetArgs(int minArgs = 0) const; - RPCAnswer(const Error &error) : status_(error) {} + RPCAnswer(const Error& error) : status_(error) {} ~RPCAnswer() { - if (hold_) delete[] data_.data(); + if (hold_) { + delete[] data_.data(); + } } - RPCAnswer(const RPCAnswer &other) = delete; - RPCAnswer(RPCAnswer &&other) : status_(std::move(other.status_)), data_(std::move(other.data_)), hold_(other.hold_) { + RPCAnswer(const RPCAnswer& other) = delete; + RPCAnswer(RPCAnswer&& other) : status_(std::move(other.status_)), data_(std::move(other.data_)), hold_(other.hold_) { other.hold_ = false; } - RPCAnswer &operator=(RPCAnswer &&other) { + RPCAnswer& operator=(RPCAnswer&& other) { if (this != &other) { - if (hold_) delete[] data_.data(); + if (hold_) { + delete[] data_.data(); + } status_ = std::move(other.status_); data_ = std::move(other.data_); hold_ = other.hold_; @@ -46,10 +50,10 @@ class RPCAnswer { } return *this; } - RPCAnswer &operator=(const RPCAnswer &other) = delete; + RPCAnswer& operator=(const RPCAnswer& other) = delete; void EnsureHold() { if (!hold_) { - uint8_t *data = new uint8_t[data_.size()]; + uint8_t* data = new uint8_t[data_.size()]; memcpy(data, data_.data(), data_.size()); data_ = {data, data_.size()}; hold_ = true; @@ -65,17 +69,17 @@ class RPCAnswer { }; struct CommandParams { - CommandParams(CmdCode c, seconds n, milliseconds e, const IRdxCancelContext *ctx) + CommandParams(CmdCode c, seconds n, milliseconds e, const IRdxCancelContext* ctx) : cmd(c), netTimeout(n), execTimeout(e), cancelCtx(ctx) {} CmdCode cmd; seconds netTimeout; milliseconds execTimeout; - const IRdxCancelContext *cancelCtx; + const IRdxCancelContext* cancelCtx; }; class ClientConnection : public ConnectionMT { public: - typedef std::function Completion; + typedef std::function Completion; typedef std::function ConnectionFailCallback; struct Options { Options() @@ -120,26 +124,26 @@ class ClientConnection : public ConnectionMT { int lastFailedEntryIdx = {-1}; }; - ClientConnection(ev::dynamic_loop &loop, ConnectData *connectData, + ClientConnection(ev::dynamic_loop& loop, ConnectData* connectData, ConnectionFailCallback connectionFailCallback = ConnectionFailCallback()); ~ClientConnection(); template - void Call(const Completion &cmpl, const CommandParams &opts, Argss... argss) { + void Call(const Completion& cmpl, const CommandParams& opts, Argss... argss) { Args args; args.reserve(sizeof...(argss)); call(cmpl, opts, args, argss...); } template - RPCAnswer Call(const CommandParams &opts, Argss... argss) { + RPCAnswer Call(const CommandParams& opts, Argss... argss) { Args args; args.reserve(sizeof...(argss)); RPCAnswer ret; std::atomic_bool set(false); auto callRet = call( - [&ret, &set](RPCAnswer &&ans, ClientConnection * /*conn*/) { + [&ret, &set](RPCAnswer&& ans, ClientConnection* /*conn*/) { ret = std::move(ans); ret.EnsureHold(); set = true; @@ -184,12 +188,12 @@ class ClientConnection : public ConnectionMT { protected: enum State { ConnInit, ConnConnecting, ConnConnected, ConnFailed, ConnClosing }; - void connect_async_cb(ev::async &) { connectInternal(); } - void keep_alive_cb(ev::periodic &, int); - void deadline_check_cb(ev::timer &, int); + void connect_async_cb(ev::async&) { connectInternal(); } + void keep_alive_cb(ev::periodic&, int); + void deadline_check_cb(ev::timer&, int); void connectInternal() noexcept; - void failInternal(const Error &error); + void failInternal(const Error& error); void disconnect(); struct RPCCompletion { @@ -200,32 +204,32 @@ class ClientConnection : public ConnectionMT { atomic_unique_ptr next; std::atomic used; seconds deadline; - const reindexer::IRdxCancelContext *cancelCtx; + const reindexer::IRdxCancelContext* cancelCtx; }; struct CallReturn { - explicit CallReturn(RPCCompletion *c = nullptr, uint32_t s = 0) noexcept : cmpl(c), seq(s) {} + explicit CallReturn(RPCCompletion* c = nullptr, uint32_t s = 0) noexcept : cmpl(c), seq(s) {} - RPCCompletion *cmpl; + RPCCompletion* cmpl; uint32_t seq; }; template - inline CallReturn call(const Completion &cmpl, const CommandParams &opts, Args &args, const std::string_view &val, Argss... argss) { + inline CallReturn call(const Completion& cmpl, const CommandParams& opts, Args& args, const std::string_view& val, Argss... argss) { args.emplace_back(p_string(&val)); return call(cmpl, opts, args, argss...); } template - inline CallReturn call(const Completion &cmpl, const CommandParams &opts, Args &args, const std::string &val, Argss... argss) { + inline CallReturn call(const Completion& cmpl, const CommandParams& opts, Args& args, const std::string& val, Argss... argss) { args.emplace_back(p_string(&val)); return call(cmpl, opts, args, argss...); } template - inline CallReturn call(const Completion &cmpl, const CommandParams &opts, Args &args, const T &val, Argss... argss) { + inline CallReturn call(const Completion& cmpl, const CommandParams& opts, Args& args, const T& val, Argss... argss) { args.emplace_back(val); return call(cmpl, opts, args, argss...); } - CallReturn call(const Completion &cmpl, const CommandParams &opts, const Args &args); - chunk packRPC(CmdCode cmd, uint32_t seq, const Args &args, const Args &ctxArgs); + CallReturn call(const Completion& cmpl, const CommandParams& opts, const Args& args); + chunk packRPC(CmdCode cmd, uint32_t seq, const Args& args, const Args& ctxArgs); ReadResT onRead() override; void onClose() override; @@ -246,7 +250,7 @@ class ClientConnection : public ConnectionMT { std::atomic now_; std::atomic terminate_; ConnectionFailCallback onConnectionFailed_; - ConnectData *connectData_; + ConnectData* connectData_; int currDsnIdx_, actualDsnIdx_; ev::async reconnect_; std::atomic enableSnappy_; diff --git a/cpp_src/net/cproto/coroclientconnection.cc b/cpp_src/net/cproto/coroclientconnection.cc index 51f17a789..38c2bf3da 100644 --- a/cpp_src/net/cproto/coroclientconnection.cc +++ b/cpp_src/net/cproto/coroclientconnection.cc @@ -41,7 +41,7 @@ CoroClientConnection::CoroClientConnection() CoroClientConnection::~CoroClientConnection() { Stop(); } -void CoroClientConnection::Start(ev::dynamic_loop &loop, ConnectData &&connectData) { +void CoroClientConnection::Start(ev::dynamic_loop& loop, ConnectData&& connectData) { if (!isRunning_) { // Don't allow to call Start, while error handling is in progress errSyncCh_.pop(); @@ -110,14 +110,14 @@ void CoroClientConnection::Stop() { } } -Error CoroClientConnection::Status(std::chrono::seconds netTimeout, std::chrono::milliseconds execTimeout, const IRdxCancelContext *ctx) { +Error CoroClientConnection::Status(std::chrono::seconds netTimeout, std::chrono::milliseconds execTimeout, const IRdxCancelContext* ctx) { if (loggedIn_) { return errOK; } return call({kCmdPing, netTimeout, execTimeout, ctx}, {}).Status(); } -CoroRPCAnswer CoroClientConnection::call(const CommandParams &opts, const Args &args) { +CoroRPCAnswer CoroClientConnection::call(const CommandParams& opts, const Args& args) { if (opts.cancelCtx) { switch (opts.cancelCtx->GetCancelType()) { case CancelType::Explicit: @@ -142,7 +142,7 @@ CoroRPCAnswer CoroClientConnection::call(const CommandParams &opts, const Args & errSyncCh_.pop(); uint32_t seq = seqp.first; - auto &call = rpcCalls_[seq % rpcCalls_.size()]; + auto& call = rpcCalls_[seq % rpcCalls_.size()]; call.seq = seq; call.used = true; call.deadline = deadline; @@ -165,7 +165,7 @@ CoroRPCAnswer CoroClientConnection::call(const CommandParams &opts, const Args & return ans; } -Error CoroClientConnection::callNoReply(const CommandParams &opts, uint32_t seq, const Args &args) { +Error CoroClientConnection::callNoReply(const CommandParams& opts, uint32_t seq, const Args& args) { if (opts.cancelCtx) { switch (opts.cancelCtx->GetCancelType()) { case CancelType::Explicit: @@ -191,7 +191,7 @@ Error CoroClientConnection::callNoReply(const CommandParams &opts, uint32_t seq, return errOK; } -CoroClientConnection::MarkedChunk CoroClientConnection::packRPC(CmdCode cmd, uint32_t seq, const Args &args, const Args &ctxArgs) { +CoroClientConnection::MarkedChunk CoroClientConnection::packRPC(CmdCode cmd, uint32_t seq, const Args& args, const Args& ctxArgs) { CProtoHeader hdr; hdr.len = 0; hdr.magic = kCprotoMagic; @@ -204,7 +204,7 @@ CoroClientConnection::MarkedChunk CoroClientConnection::packRPC(CmdCode cmd, uin chunk ch = getChunk(); WrSerializer ser(std::move(ch)); - ser.Write(std::string_view(reinterpret_cast(&hdr), sizeof(hdr))); + ser.Write(std::string_view(reinterpret_cast(&hdr), sizeof(hdr))); args.Pack(ser); ctxArgs.Pack(ser); if (hdr.compressed) { @@ -215,19 +215,19 @@ CoroClientConnection::MarkedChunk CoroClientConnection::packRPC(CmdCode cmd, uin ser.Write(compressed); } assertrx(ser.Len() < size_t(std::numeric_limits::max())); - reinterpret_cast(ser.Buf())->len = ser.Len() - sizeof(hdr); + reinterpret_cast(ser.Buf())->len = ser.Len() - sizeof(hdr); return {seq, ser.DetachChunk()}; } -void CoroClientConnection::appendChunck(std::vector &buf, chunk &&ch) { +void CoroClientConnection::appendChunck(std::vector& buf, chunk&& ch) { auto oldBufSize = buf.size(); buf.resize(buf.size() + ch.size()); memcpy(buf.data() + oldBufSize, ch.data(), ch.size()); recycleChunk(std::move(ch)); } -Error CoroClientConnection::login(std::vector &buf) { +Error CoroClientConnection::login(std::vector& buf) { assertrx(conn_.state() != manual_connection::conn_state::connecting); if (conn_.state() == manual_connection::conn_state::init) { readWg_.wait(); @@ -256,7 +256,9 @@ Error CoroClientConnection::login(std::vector &buf) { std::string userName = connectData_.uri.username(); std::string password = connectData_.uri.password(); - if (dbName[0] == '/') dbName = dbName.substr(1); + if (dbName[0] == '/') { + dbName = dbName.substr(1); + } enableCompression_ = connectData_.opts.enableCompression; requestDedicatedThread_ = connectData_.opts.requestDedicatedThread; Args args = {Arg{p_string(&userName)}, @@ -292,19 +294,19 @@ Error CoroClientConnection::login(std::vector &buf) { return errOK; } -void CoroClientConnection::closeConn(const Error &err) noexcept { +void CoroClientConnection::closeConn(const Error& err) noexcept { errSyncCh_.reopen(); lastError_ = err; conn_.close_conn(k_sock_closed_err); handleFatalError(err); } -void CoroClientConnection::handleFatalError(const Error &err) noexcept { +void CoroClientConnection::handleFatalError(const Error& err) noexcept { if (!errSyncCh_.opened()) { errSyncCh_.reopen(); } loggedIn_ = false; - for (auto &c : rpcCalls_) { + for (auto& c : rpcCalls_) { if (c.used && c.rspCh.opened() && !c.rspCh.full()) { c.rspCh.push(err); } @@ -325,7 +327,7 @@ chunk CoroClientConnection::getChunk() noexcept { return ch; } -void CoroClientConnection::recycleChunk(chunk &&ch) noexcept { +void CoroClientConnection::recycleChunk(chunk&& ch) noexcept { if (ch.capacity() <= kMaxChunckSizeToRecycle && recycledChuncks_.size() < kMaxRecycledChuncks) { recycledChuncks_.emplace_back(std::move(ch)); } @@ -430,7 +432,7 @@ void CoroClientConnection::readerRoutine() { ans.status_ = Error(static_cast(errCode), std::string{errMsg}); } ans.data_ = span(ser.Buf() + ser.Pos(), ser.Len() - ser.Pos()); - } catch (const Error &err) { + } catch (const Error& err) { // disconnect closeConn(err); break; @@ -449,7 +451,7 @@ void CoroClientConnection::readerRoutine() { closeConn(ans.Status()); } } else { - auto &rpcData = rpcCalls_[hdr.seq % rpcCalls_.size()]; + auto& rpcData = rpcCalls_[hdr.seq % rpcCalls_.size()]; if (!rpcData.used || rpcData.seq != hdr.seq) { auto cmdSv = CmdName(hdr.cmd); fprintf(stderr, "Unexpected RPC answer seq=%d cmd=%d(%.*s)\n", int(hdr.seq), hdr.cmd, int(cmdSv.size()), cmdSv.data()); @@ -466,7 +468,7 @@ void CoroClientConnection::readerRoutine() { } while (loggedIn_ && !terminate_); } -void CoroClientConnection::sendCloseResults(CProtoHeader const &hdr, CoroRPCAnswer const &ans) { +void CoroClientConnection::sendCloseResults(const CProtoHeader& hdr, const CoroRPCAnswer& ans) { if (!ans.Status().ok()) { return; } @@ -511,8 +513,10 @@ void CoroClientConnection::deadlineRoutine() { loop_->granular_sleep(kDeadlineCheckInterval, kCoroSleepGranularity, [this] { return terminate_; }); now_ += std::chrono::duration_cast(kDeadlineCheckInterval).count(); - for (auto &c : rpcCalls_) { - if (!c.used) continue; + for (auto& c : rpcCalls_) { + if (!c.used) { + continue; + } bool expired = (c.deadline.count() && c.deadline.count() <= now_); bool canceled = (c.cancelCtx && c.cancelCtx->IsCancelable() && (c.cancelCtx->GetCancelType() == CancelType::Explicit)); if (expired || canceled) { diff --git a/cpp_src/net/cproto/coroclientconnection.h b/cpp_src/net/cproto/coroclientconnection.h index 832fd2ea9..2236cd3b4 100644 --- a/cpp_src/net/cproto/coroclientconnection.h +++ b/cpp_src/net/cproto/coroclientconnection.h @@ -36,14 +36,14 @@ class CoroRPCAnswer { return ret; } CoroRPCAnswer() = default; - CoroRPCAnswer(const Error &error) : status_(error) {} - CoroRPCAnswer(const CoroRPCAnswer &other) = delete; - CoroRPCAnswer(CoroRPCAnswer &&other) = default; - CoroRPCAnswer &operator=(CoroRPCAnswer &&other) = default; - CoroRPCAnswer &operator=(const CoroRPCAnswer &other) = delete; - - void EnsureHold(chunk &&ch) { - ch.append(std::string_view(reinterpret_cast(data_.data()), data_.size())); + CoroRPCAnswer(const Error& error) : status_(error) {} + CoroRPCAnswer(const CoroRPCAnswer& other) = delete; + CoroRPCAnswer(CoroRPCAnswer&& other) = default; + CoroRPCAnswer& operator=(CoroRPCAnswer&& other) = default; + CoroRPCAnswer& operator=(const CoroRPCAnswer& other) = delete; + + void EnsureHold(chunk&& ch) { + ch.append(std::string_view(reinterpret_cast(data_.data()), data_.size())); storage_ = std::move(ch); data_ = {storage_.data(), storage_.size()}; } @@ -56,18 +56,18 @@ class CoroRPCAnswer { }; struct CommandParams { - CommandParams(CmdCode c, seconds n, milliseconds e, const IRdxCancelContext *ctx) + CommandParams(CmdCode c, seconds n, milliseconds e, const IRdxCancelContext* ctx) : cmd(c), netTimeout(n), execTimeout(e), cancelCtx(ctx) {} CmdCode cmd; seconds netTimeout; milliseconds execTimeout; - const IRdxCancelContext *cancelCtx; + const IRdxCancelContext* cancelCtx; }; class CoroClientConnection { public: - using UpdatesHandlerT = std::function; - using FatalErrorHandlerT = std::function; + using UpdatesHandlerT = std::function; + using FatalErrorHandlerT = std::function; struct Options { Options() @@ -109,16 +109,16 @@ class CoroClientConnection { CoroClientConnection(); ~CoroClientConnection(); - void Start(ev::dynamic_loop &loop, ConnectData &&connectData); + void Start(ev::dynamic_loop& loop, ConnectData&& connectData); void Stop(); bool IsRunning() const noexcept { return isRunning_; } - Error Status(seconds netTimeout, milliseconds execTimeout, const IRdxCancelContext *ctx); + Error Status(seconds netTimeout, milliseconds execTimeout, const IRdxCancelContext* ctx); seconds Now() const noexcept { return seconds(now_); } void SetUpdatesHandler(UpdatesHandlerT handler) noexcept { updatesHandler_ = std::move(handler); } void SetFatalErrorHandler(FatalErrorHandlerT handler) noexcept { fatalErrorHandler_ = std::move(handler); } template - CoroRPCAnswer Call(const CommandParams &opts, const Argss &...argss) { + CoroRPCAnswer Call(const CommandParams& opts, const Argss&... argss) { Args args; args.reserve(sizeof...(argss)); return call(opts, args, argss...); @@ -130,7 +130,7 @@ class CoroClientConnection { uint32_t seq; bool used; seconds deadline; - const reindexer::IRdxCancelContext *cancelCtx; + const reindexer::IRdxCancelContext* cancelCtx; coroutine::channel rspCh; }; @@ -140,32 +140,32 @@ class CoroClientConnection { }; template - inline CoroRPCAnswer call(const CommandParams &opts, Args &args, const std::string_view &val, const Argss &...argss) { + inline CoroRPCAnswer call(const CommandParams& opts, Args& args, const std::string_view& val, const Argss&... argss) { args.push_back(Variant(p_string(&val))); return call(opts, args, argss...); } template - inline CoroRPCAnswer call(const CommandParams &opts, Args &args, const std::string &val, const Argss &...argss) { + inline CoroRPCAnswer call(const CommandParams& opts, Args& args, const std::string& val, const Argss&... argss) { args.push_back(Variant(p_string(&val))); return call(opts, args, argss...); } template - inline CoroRPCAnswer call(const CommandParams &opts, Args &args, const T &val, const Argss &...argss) { + inline CoroRPCAnswer call(const CommandParams& opts, Args& args, const T& val, const Argss&... argss) { args.push_back(Variant(val)); return call(opts, args, argss...); } - CoroRPCAnswer call(const CommandParams &opts, const Args &args); - Error callNoReply(const CommandParams &opts, uint32_t seq, const Args &args); + CoroRPCAnswer call(const CommandParams& opts, const Args& args); + Error callNoReply(const CommandParams& opts, uint32_t seq, const Args& args); - MarkedChunk packRPC(CmdCode cmd, uint32_t seq, const Args &args, const Args &ctxArgs); - void appendChunck(std::vector &buf, chunk &&ch); - Error login(std::vector &buf); - void closeConn(const Error &err) noexcept; - void handleFatalError(const Error &err) noexcept; + MarkedChunk packRPC(CmdCode cmd, uint32_t seq, const Args& args, const Args& ctxArgs); + void appendChunck(std::vector& buf, chunk&& ch); + Error login(std::vector& buf); + void closeConn(const Error& err) noexcept; + void handleFatalError(const Error& err) noexcept; chunk getChunk() noexcept; - void recycleChunk(chunk &&) noexcept; - void sendCloseResults(CProtoHeader const &, CoroRPCAnswer const &); + void recycleChunk(chunk&&) noexcept; + void sendCloseResults(const CProtoHeader&, const CoroRPCAnswer&); void writerRoutine(); void readerRoutine(); @@ -176,7 +176,7 @@ class CoroClientConnection { uint32_t now_; bool terminate_ = false; bool isRunning_ = false; - ev::dynamic_loop *loop_ = nullptr; + ev::dynamic_loop* loop_ = nullptr; // seq -> rpc data std::vector rpcCalls_; diff --git a/cpp_src/net/cproto/dispatcher.h b/cpp_src/net/cproto/dispatcher.h index 97b7674c8..bf9c69cf9 100644 --- a/cpp_src/net/cproto/dispatcher.h +++ b/cpp_src/net/cproto/dispatcher.h @@ -33,28 +33,28 @@ struct ClientData { struct Context; struct IRPCCall { - void (*Get)(IRPCCall *, CmdCode &, std::string_view &nsName, Args &); + void (*Get)(IRPCCall*, CmdCode&, std::string_view& nsName, Args&); intrusive_ptr> data_; }; class Writer { public: virtual ~Writer() = default; - virtual void WriteRPCReturn(Context &ctx, const Args &args, const Error &status) = 0; - virtual void CallRPC(const IRPCCall &call) = 0; - virtual void SetClientData(std::unique_ptr &&data) noexcept = 0; - virtual ClientData *GetClientData() noexcept = 0; + virtual void WriteRPCReturn(Context& ctx, const Args& args, const Error& status) = 0; + virtual void CallRPC(const IRPCCall& call) = 0; + virtual void SetClientData(std::unique_ptr&& data) noexcept = 0; + virtual ClientData* GetClientData() noexcept = 0; virtual std::shared_ptr GetConnectionStat() noexcept = 0; }; struct Context { - void Return(const Args &args, const Error &status = errOK) { writer->WriteRPCReturn(*this, args, status); } - void SetClientData(std::unique_ptr &&data) noexcept { writer->SetClientData(std::move(data)); } - ClientData *GetClientData() noexcept { return writer->GetClientData(); } + void Return(const Args& args, const Error& status = errOK) { writer->WriteRPCReturn(*this, args, status); } + void SetClientData(std::unique_ptr&& data) noexcept { writer->SetClientData(std::move(data)); } + ClientData* GetClientData() noexcept { return writer->GetClientData(); } std::string_view clientAddr; - RPCCall *call; - Writer *writer; + RPCCall* call; + Writer* writer; Stat stat; bool respSent; }; @@ -67,7 +67,7 @@ class Dispatcher { /// @param object - handler class object /// @param func - handler template - void Register(CmdCode cmd, K *object, Error (K::*func)(Context &, Args... args)) { + void Register(CmdCode cmd, K* object, Error (K::*func)(Context&, Args... args)) { handlers_[cmd] = FuncWrapper{object, func}; } @@ -75,7 +75,7 @@ class Dispatcher { /// @param object - handler class object /// @param func - handler template - void Middleware(K *object, Error (K::*func)(Context &)) { + void Middleware(K* object, Error (K::*func)(Context&)) { middlewares_.push_back(FuncWrapper{object, func}); } @@ -83,41 +83,41 @@ class Dispatcher { /// @param object - logger class object /// @param func - logger template - void Logger(K *object, void (K::*func)(Context &ctx, const Error &err, const Args &ret)) { - logger_ = [=](Context &ctx, const Error &err, const Args &ret) { (static_cast(object)->*func)(ctx, err, ret); }; + void Logger(K* object, void (K::*func)(Context& ctx, const Error& err, const Args& ret)) { + logger_ = [=](Context& ctx, const Error& err, const Args& ret) { (static_cast(object)->*func)(ctx, err, ret); }; } /// Set closer notifier /// @param object close class object /// @param func function, to be called on connecion close template - void OnClose(K *object, void (K::*func)(Context &ctx, const Error &err)) { - onClose_ = [=](Context &ctx, const Error &err) { (static_cast(object)->*func)(ctx, err); }; + void OnClose(K* object, void (K::*func)(Context& ctx, const Error& err)) { + onClose_ = [=](Context& ctx, const Error& err) { (static_cast(object)->*func)(ctx, err); }; } /// Set response sent notifier /// @param object class object /// @param func function, to be called on response sent template - void OnResponse(K *object, void (K::*func)(Context &ctx)) { - onResponse_ = [=](Context &ctx) { (static_cast(object)->*func)(ctx); }; + void OnResponse(K* object, void (K::*func)(Context& ctx)) { + onResponse_ = [=](Context& ctx) { (static_cast(object)->*func)(ctx); }; } /// Get reference to the current logger functor /// @return Log handler reference - const std::function &LoggerRef() const noexcept { return logger_; } + const std::function& LoggerRef() const noexcept { return logger_; } /// Get reference to the current OnClose() functor /// @return OnClose callback reference - const std::function &OnCloseRef() const noexcept { return onClose_; } + const std::function& OnCloseRef() const noexcept { return onClose_; } /// Get reference to the current OnResponse() functor /// @return OnResponse callback reference - const std::function &OnResponseRef() const noexcept { return onResponse_; } + const std::function& OnResponseRef() const noexcept { return onResponse_; } /// Handle RPC fron the context /// @param ctx - RPC context - Error Handle(Context &ctx) { + Error Handle(Context& ctx) { if rx_likely (uint32_t(ctx.call->cmd) < uint32_t(handlers_.size())) { - for (auto &middleware : middlewares_) { + for (auto& middleware : middlewares_) { auto ret = middleware(ctx); if (!ret.ok()) { return ret; @@ -139,41 +139,41 @@ class Dispatcher { struct is_optional> : public std::true_type {}; template ::value, int> = 0> - static T get_arg(const Args &args, size_t index, const Context &ctx) { + static T get_arg(const Args& args, size_t index, const Context& ctx) { if (index >= args.size()) { throw Error(errParams, "Invalid args of %s call; argument %d is not submited", CmdName(ctx.call->cmd), static_cast(index)); } return T(args[index]); } template ::value, int> = 0> - static T get_arg(const Args &args, size_t index, const Context &) { + static T get_arg(const Args& args, size_t index, const Context&) { return index < args.size() ? T(typename T::value_type(args[index])) : T(); } template class FuncWrapper { public: - FuncWrapper(K *o, Error (K::*f)(Context &, Args...)) noexcept : obj_{o}, func_{f} {} - Error operator()(Context &ctx) const { return impl(ctx, std::index_sequence_for{}); } + FuncWrapper(K* o, Error (K::*f)(Context&, Args...)) noexcept : obj_{o}, func_{f} {} + Error operator()(Context& ctx) const { return impl(ctx, std::index_sequence_for{}); } private: template - Error impl(Context &ctx, std::index_sequence) const { + Error impl(Context& ctx, std::index_sequence) const { return (obj_->*func_)(ctx, get_arg(ctx.call->args, I, ctx)...); } - K *obj_; - Error (K::*func_)(Context &, Args...); + K* obj_; + Error (K::*func_)(Context&, Args...); }; - using Handler = std::function; + using Handler = std::function; std::array handlers_; std::vector middlewares_; - std::function logger_; - std::function onClose_; + std::function logger_; + std::function onClose_; // This should be called from the connection thread only to prevet access to other connection's ClientData - std::function onResponse_; + std::function onResponse_; }; } // namespace cproto } // namespace net diff --git a/cpp_src/net/cproto/serverconnection.cc b/cpp_src/net/cproto/serverconnection.cc index de8ce207e..5b71ef973 100644 --- a/cpp_src/net/cproto/serverconnection.cc +++ b/cpp_src/net/cproto/serverconnection.cc @@ -12,7 +12,7 @@ const auto kCProtoTimeoutSec = 300; const auto kUpdatesResendTimeout = 0.1; const auto kMaxUpdatesBufSize = 1024 * 1024 * 8; -ServerConnection::ServerConnection(socket &&s, ev::dynamic_loop &loop, Dispatcher &dispatcher, bool enableStat, size_t maxUpdatesSize, +ServerConnection::ServerConnection(socket&& s, ev::dynamic_loop& loop, Dispatcher& dispatcher, bool enableStat, size_t maxUpdatesSize, bool enableCustomBalancing) : ConnectionST(std::move(s), loop, enableStat, kConnReadbufSize, kConnWriteBufSize, kCProtoTimeoutSec), dispatcher_(dispatcher), @@ -33,14 +33,14 @@ ServerConnection::ServerConnection(socket &&s, ev::dynamic_loop &loop, Dispatche ServerConnection::~ServerConnection() { BaseConnT::closeConn(); } -bool ServerConnection::Restart(socket &&s) { +bool ServerConnection::Restart(socket&& s) { BaseConnT::restart(std::move(s)); updates_async_.start(); BaseConnT::callback(BaseConnT::io_, ev::READ); return true; } -void ServerConnection::Attach(ev::dynamic_loop &loop) { +void ServerConnection::Attach(ev::dynamic_loop& loop) { BaseConnT::async_.set(this); if (!BaseConnT::attached_) { BaseConnT::attach(loop); @@ -77,7 +77,7 @@ void ServerConnection::onClose() { } } -void ServerConnection::handleRPC(Context &ctx) { +void ServerConnection::handleRPC(Context& ctx) { Error err = dispatcher_.Handle(ctx); if (!ctx.respSent) { @@ -92,7 +92,7 @@ ServerConnection::BaseConnT::ReadResT ServerConnection::onRead() { Context ctx{BaseConnT::clientAddr_, nullptr, this, {{}, {}}, false}; std::string uncompressed; - auto len = BaseConnT::rdBuf_.peek(reinterpret_cast(&hdr), sizeof(hdr)); + auto len = BaseConnT::rdBuf_.peek(reinterpret_cast(&hdr), sizeof(hdr)); if (len < sizeof(hdr)) { return BaseConnT::ReadResT::Default; } @@ -100,7 +100,7 @@ ServerConnection::BaseConnT::ReadResT ServerConnection::onRead() { if (hdr.magic != kCprotoMagic) { try { responceRPC(ctx, Error(errParams, "Invalid cproto magic %08x", int(hdr.magic)), Args()); - } catch (const Error &err) { + } catch (const Error& err) { fprintf(stderr, "responceRPC unexpected error: %s\n", err.what().c_str()); } BaseConnT::closeConn_ = true; @@ -113,7 +113,7 @@ ServerConnection::BaseConnT::ReadResT ServerConnection::onRead() { ctx, Error(errParams, "Unsupported cproto version %04x. This server expects reindexer client v1.9.8+", int(hdr.version)), Args()); - } catch (const Error &err) { + } catch (const Error& err) { fprintf(stderr, "responceRPC unexpected error: %s\n", err.what().c_str()); } BaseConnT::closeConn_ = true; @@ -182,9 +182,9 @@ ServerConnection::BaseConnT::ReadResT ServerConnection::onRead() { } handleRPC(ctx); - } catch (const Error &err) { + } catch (const Error& err) { handleException(ctx, err); - } catch (const std::exception &err) { + } catch (const std::exception& err) { handleException(ctx, Error(errLogic, err.what())); } catch (...) { handleException(ctx, Error(errLogic, "Unknown exception")); @@ -195,7 +195,7 @@ ServerConnection::BaseConnT::ReadResT ServerConnection::onRead() { return BaseConnT::ReadResT::Default; } -static void packRPC(WrSerializer &ser, Context &ctx, const Error &status, const Args &args, bool enableSnappy) { +static void packRPC(WrSerializer& ser, Context& ctx, const Error& status, const Args& args, bool enableSnappy) { CProtoHeader hdr; hdr.len = 0; hdr.magic = kCprotoMagic; @@ -211,7 +211,7 @@ static void packRPC(WrSerializer &ser, Context &ctx, const Error &status, const } size_t savePos = ser.Len(); - ser.Write(std::string_view(reinterpret_cast(&hdr), sizeof(hdr))); + ser.Write(std::string_view(reinterpret_cast(&hdr), sizeof(hdr))); ser.PutVarUint(status.code()); ser.PutVString(status.what()); @@ -227,22 +227,22 @@ static void packRPC(WrSerializer &ser, Context &ctx, const Error &status, const if (ser.Len() - savePos >= size_t(std::numeric_limits::max())) { throw Error(errNetwork, "Too large RPC message(%d), size: %d bytes", hdr.cmd, ser.Len()); } - reinterpret_cast(ser.Buf() + savePos)->len = ser.Len() - savePos - sizeof(hdr); + reinterpret_cast(ser.Buf() + savePos)->len = ser.Len() - savePos - sizeof(hdr); } -static chunk packRPC(chunk chunk, Context &ctx, const Error &status, const Args &args, bool enableSnappy) { +static chunk packRPC(chunk chunk, Context& ctx, const Error& status, const Args& args, bool enableSnappy) { WrSerializer ser(std::move(chunk)); packRPC(ser, ctx, status, args, enableSnappy); return ser.DetachChunk(); } -void ServerConnection::responceRPC(Context &ctx, const Error &status, const Args &args) { +void ServerConnection::responceRPC(Context& ctx, const Error& status, const Args& args) { if rx_unlikely (ctx.respSent) { fprintf(stderr, "Warning - RPC responce already sent\n"); return; } - auto &&chunk = packRPC(BaseConnT::wrBuf_.get_chunk(), ctx, status, args, enableSnappy_); + auto&& chunk = packRPC(BaseConnT::wrBuf_.get_chunk(), ctx, status, args, enableSnappy_); auto len = chunk.len(); BaseConnT::wrBuf_.write(std::move(chunk)); if (BaseConnT::stats_) { @@ -264,7 +264,7 @@ void ServerConnection::responceRPC(Context &ctx, const Error &status, const Args } } -void ServerConnection::CallRPC(const IRPCCall &call) { +void ServerConnection::CallRPC(const IRPCCall& call) { std::lock_guard lck(updates_mtx_); updates_.emplace_back(call); updatesSize_ += call.data_->capacity(); @@ -279,7 +279,7 @@ void ServerConnection::CallRPC(const IRPCCall &call) { WrSerializer ser; ser.PutVString(nsName); - IRPCCall callLost = {[](IRPCCall *callLost, CmdCode &cmd, std::string_view &ns, Args &args) { + IRPCCall callLost = {[](IRPCCall* callLost, CmdCode& cmd, std::string_view& ns, Args& args) { Serializer s(callLost->data_->data(), callLost->data_->size()); cmd = kCmdUpdates; args = {Arg(std::string(s.GetVString()))}; @@ -381,7 +381,9 @@ void ServerConnection::sendUpdates() { updatesSize_ += updatesSizeCopy - updatesSizeBuffered; } - if (BaseConnT::stats_) stats_->update_pended_updates(updates.size()); + if (BaseConnT::stats_) { + stats_->update_pended_updates(updates.size()); + } } else if (BaseConnT::stats_) { if (auto stat = BaseConnT::stats_->get_stat(); stat) { std::lock_guard lck(updates_mtx_); @@ -401,16 +403,16 @@ void ServerConnection::sendUpdates() { BaseConnT::callback(BaseConnT::io_, ev::WRITE); } -void ServerConnection::handleException(Context &ctx, const Error &err) noexcept { +void ServerConnection::handleException(Context& ctx, const Error& err) noexcept { // Exception occurs on unrecoverable error. Send responce, and drop connection fprintf(stderr, "Dropping RPC-connection. Reason: %s\n", err.what().c_str()); try { if (!ctx.respSent) { responceRPC(ctx, err, Args()); } - } catch (const Error &e) { + } catch (const Error& e) { fprintf(stderr, "responceRPC unexpected error: %s\n", e.what().c_str()); - } catch (const std::exception &e) { + } catch (const std::exception& e) { fprintf(stderr, "responceRPC unexpected error (std::exception): %s\n", e.what()); } catch (...) { fprintf(stderr, "responceRPC unexpected error (unknow exception)\n"); diff --git a/cpp_src/net/cproto/serverconnection.h b/cpp_src/net/cproto/serverconnection.h index c035af899..133fcf2f1 100644 --- a/cpp_src/net/cproto/serverconnection.h +++ b/cpp_src/net/cproto/serverconnection.h @@ -16,20 +16,20 @@ class ServerConnection final : public ConnectionST, public IServerConnection, pu public: using BaseConnT = ConnectionST; - ServerConnection(socket &&s, ev::dynamic_loop &loop, Dispatcher &dispatcher, bool enableStat, size_t maxUpdatesSize, + ServerConnection(socket&& s, ev::dynamic_loop& loop, Dispatcher& dispatcher, bool enableStat, size_t maxUpdatesSize, bool enableCustomBalancing); ~ServerConnection() override; // IServerConnection interface implementation - static ConnectionFactory NewFactory(Dispatcher &dispatcher, bool enableStat, size_t maxUpdatesSize) { - return [&dispatcher, enableStat, maxUpdatesSize](ev::dynamic_loop &loop, socket &&s, bool allowCustomBalancing) { + static ConnectionFactory NewFactory(Dispatcher& dispatcher, bool enableStat, size_t maxUpdatesSize) { + return [&dispatcher, enableStat, maxUpdatesSize](ev::dynamic_loop& loop, socket&& s, bool allowCustomBalancing) { return new ServerConnection(std::move(s), loop, dispatcher, enableStat, maxUpdatesSize, allowCustomBalancing); }; } bool IsFinished() const noexcept override final { return !BaseConnT::sock_.valid(); } BalancingType GetBalancingType() const noexcept override final { return balancingType_; } - void SetRebalanceCallback(std::function cb) override final { + void SetRebalanceCallback(std::function cb) override final { assertrx(!rebalance_); rebalance_ = std::move(cb); } @@ -41,15 +41,15 @@ class ServerConnection final : public ConnectionST, public IServerConnection, pu } BaseConnT::callback(BaseConnT::io_, ev::READ); } - bool Restart(socket &&s) override final; + bool Restart(socket&& s) override final; void Detach() override final; - void Attach(ev::dynamic_loop &loop) override final; + void Attach(ev::dynamic_loop& loop) override final; // Writer iterface implementation - void WriteRPCReturn(Context &ctx, const Args &args, const Error &status) override final { responceRPC(ctx, status, args); } - void CallRPC(const IRPCCall &call) override final; - void SetClientData(std::unique_ptr &&data) noexcept override final { clientData_ = std::move(data); } - ClientData *GetClientData() noexcept override final { return clientData_.get(); } + void WriteRPCReturn(Context& ctx, const Args& args, const Error& status) override final { responceRPC(ctx, status, args); } + void CallRPC(const IRPCCall& call) override final; + void SetClientData(std::unique_ptr&& data) noexcept override final { clientData_ = std::move(data); } + ClientData* GetClientData() noexcept override final { return clientData_.get(); } std::shared_ptr GetConnectionStat() noexcept override final { return BaseConnT::stats_ ? BaseConnT::stats_->get_stat() : std::shared_ptr(); } @@ -57,14 +57,14 @@ class ServerConnection final : public ConnectionST, public IServerConnection, pu protected: typename BaseConnT::ReadResT onRead() override; void onClose() override; - void handleRPC(Context &ctx); - void responceRPC(Context &ctx, const Error &error, const Args &args); - void async_cb(ev::async &) { sendUpdates(); } - void timeout_cb(ev::periodic &, int) { sendUpdates(); } + void handleRPC(Context& ctx); + void responceRPC(Context& ctx, const Error& error, const Args& args); + void async_cb(ev::async&) { sendUpdates(); } + void timeout_cb(ev::periodic&, int) { sendUpdates(); } void sendUpdates(); - void handleException(Context &ctx, const Error &err) noexcept; + void handleException(Context& ctx, const Error& err) noexcept; - Dispatcher &dispatcher_; + Dispatcher& dispatcher_; std::unique_ptr clientData_; // keep here to prevent allocs RPCCall call_; @@ -80,7 +80,7 @@ class ServerConnection final : public ConnectionST, public IServerConnection, pu bool enableSnappy_; bool hasPendingData_ = false; BalancingType balancingType_ = BalancingType::NotSet; - std::function rebalance_; + std::function rebalance_; }; } // namespace cproto diff --git a/cpp_src/net/ev/ev.cc b/cpp_src/net/ev/ev.cc index 089ce2987..47f72a377 100644 --- a/cpp_src/net/ev/ev.cc +++ b/cpp_src/net/ev/ev.cc @@ -105,7 +105,7 @@ class loop_select_backend_private { loop_select_backend::loop_select_backend() : private_(new loop_select_backend_private) {} loop_select_backend::~loop_select_backend() = default; -void loop_select_backend::init(dynamic_loop *owner) noexcept { +void loop_select_backend::init(dynamic_loop* owner) noexcept { owner_ = owner; private_->maxfd_ = -1; FD_ZERO(&private_->rfds_); @@ -115,7 +115,9 @@ void loop_select_backend::init(dynamic_loop *owner) noexcept { void loop_select_backend::set(int fd, int events, int /*oldevents*/) noexcept { assertrx(fd < capacity()); - if (fd > private_->maxfd_) private_->maxfd_ = fd; + if (fd > private_->maxfd_) { + private_->maxfd_ = fd; + } if (events & READ) { FD_SET(fd, &private_->rfds_); @@ -134,7 +136,9 @@ void loop_select_backend::stop(int fd) noexcept { FD_CLR(fd, &private_->rfds_); FD_CLR(fd, &private_->wfds_); - if (fd == private_->maxfd_) private_->maxfd_--; + if (fd == private_->maxfd_) { + private_->maxfd_--; + } } int loop_select_backend::runonce(int64_t t) { @@ -147,12 +151,16 @@ int loop_select_backend::runonce(int64_t t) { memcpy(&wfds, &private_->wfds_, 1 + (private_->maxfd_ / 8)); int ret = select(private_->maxfd_ + 1, &rfds, &wfds, nullptr, t != -1 ? &tv : nullptr); - if (ret < 0) return ret; + if (ret < 0) { + return ret; + } for (int fd = 0; fd < private_->maxfd_ + 1; fd++) { int events = (FD_ISSET(fd, &rfds) ? READ : 0) | (FD_ISSET(fd, &wfds) ? WRITE : 0); if (events) { - if (!check_async(fd)) owner_->io_callback(fd, events); + if (!check_async(fd)) { + owner_->io_callback(fd, events); + } } } return ret; @@ -172,7 +180,7 @@ class loop_poll_backend_private { loop_poll_backend::loop_poll_backend() : private_(new loop_poll_backend_private) {} loop_poll_backend::~loop_poll_backend() {} -void loop_poll_backend::init(dynamic_loop *owner) { +void loop_poll_backend::init(dynamic_loop* owner) { owner_ = owner; private_->fds_.reserve(2048); private_->fds_.resize(0); @@ -180,7 +188,7 @@ void loop_poll_backend::init(dynamic_loop *owner) { void loop_poll_backend::set(int fd, int events, int /*oldevents*/) { short ev = ((events & READ) ? (POLLRDNORM | POLLIN) : 0) | ((events & WRITE) ? (POLLWRNORM | POLLOUT) : 0); - int &idx = owner_->fds_[fd].idx; + int& idx = owner_->fds_[fd].idx; if (idx < 0) { private_->fds_.push_back({fd, ev, 0}); @@ -193,7 +201,7 @@ void loop_poll_backend::set(int fd, int events, int /*oldevents*/) { } void loop_poll_backend::stop(int fd) { - int &idx = owner_->fds_[fd].idx; + int& idx = owner_->fds_[fd].idx; assertrx(idx >= 0 && !private_->fds_.empty()); if (static_cast(idx) < private_->fds_.size() - 1) { @@ -209,20 +217,28 @@ void loop_poll_backend::stop(int fd) { int loop_poll_backend::runonce(int64_t t) { int ret = poll(&private_->fds_[0], private_->fds_.size(), t != -1 ? t / 1000 : -1); - if (ret < 1) return ret; + if (ret < 1) { + return ret; + } for (size_t i = 0; i < private_->fds_.size();) { - pollfd &pfd = private_->fds_[i]; + pollfd& pfd = private_->fds_[i]; private_->wasErased_ = false; if (pfd.revents != 0) { int events = ((pfd.revents & (POLLRDNORM | POLLIN | POLLHUP)) ? READ : 0) | ((pfd.revents & (POLLWRNORM | POLLOUT)) ? WRITE : 0); if (events) { - if (!check_async(pfd.fd)) owner_->io_callback(pfd.fd, events); - if (!private_->wasErased_) pfd.revents = 0; + if (!check_async(pfd.fd)) { + owner_->io_callback(pfd.fd, events); + } + if (!private_->wasErased_) { + pfd.revents = 0; + } } } - if (!private_->wasErased_) ++i; + if (!private_->wasErased_) { + ++i; + } } return ret; } @@ -246,7 +262,7 @@ loop_epoll_backend::~loop_epoll_backend() { private_->ctlfd_ = -1; } -void loop_epoll_backend::init(dynamic_loop *owner) { +void loop_epoll_backend::init(dynamic_loop* owner) { owner_ = owner; private_->ctlfd_ = epoll_create1(EPOLL_CLOEXEC); if (private_->ctlfd_ < 0) { @@ -285,10 +301,12 @@ int loop_epoll_backend::runonce(int64_t t) { std::swap(private_->events_, private_->eventsTmp_); private_->events_.resize(private_->eventsTmp_.size()); for (int i = 0; i < ret; i++) { - auto &eventRef = private_->eventsTmp_[i]; + auto& eventRef = private_->eventsTmp_[i]; int events = ((eventRef.events & (EPOLLIN | EPOLLHUP)) ? READ : 0) | ((eventRef.events & EPOLLOUT) ? WRITE : 0); int fd = eventRef.data.fd; - if (!check_async(fd)) owner_->io_callback(fd, events); + if (!check_async(fd)) { + owner_->io_callback(fd, events); + } } return ret; } @@ -316,7 +334,7 @@ loop_wsa_backend::loop_wsa_backend() : private_(new loop_wsa_backend_private) { } loop_wsa_backend::~loop_wsa_backend() { - for (auto &fd : private_->wfds_) { + for (auto& fd : private_->wfds_) { CloseHandle(fd.hEvent); fd.hEvent = INVALID_HANDLE_VALUE; } @@ -325,10 +343,10 @@ loop_wsa_backend::~loop_wsa_backend() { } } -void loop_wsa_backend::init(dynamic_loop *owner) { owner_ = owner; } +void loop_wsa_backend::init(dynamic_loop* owner) { owner_ = owner; } void loop_wsa_backend::set(int fd, int events, int oldevents) { - auto it = std::find_if(private_->wfds_.begin(), private_->wfds_.end(), [&](const win_fd &wfd) { return wfd.fd == fd; }); + auto it = std::find_if(private_->wfds_.begin(), private_->wfds_.end(), [&](const win_fd& wfd) { return wfd.fd == fd; }); if (it == private_->wfds_.end()) { assertrx(int(private_->wfds_.size()) < capacity()); win_fd new_wfd; @@ -344,7 +362,7 @@ void loop_wsa_backend::set(int fd, int events, int oldevents) { } void loop_wsa_backend::stop(int fd) { - auto it = std::find_if(private_->wfds_.begin(), private_->wfds_.end(), [&](const win_fd &wfd) { return wfd.fd == fd; }); + auto it = std::find_if(private_->wfds_.begin(), private_->wfds_.end(), [&](const win_fd& wfd) { return wfd.fd == fd; }); if (it == private_->wfds_.end()) { return; } @@ -355,7 +373,9 @@ void loop_wsa_backend::stop(int fd) { int loop_wsa_backend::runonce(int64_t t) { HANDLE objs[WSA_MAXIMUM_WAIT_EVENTS]; unsigned ecount = private_->wfds_.size(); - for (unsigned i = 0; i < ecount; i++) objs[i] = private_->wfds_[i].hEvent; + for (unsigned i = 0; i < ecount; i++) { + objs[i] = private_->wfds_[i].hEvent; + } if (private_->hAsyncEvent != INVALID_HANDLE_VALUE) { objs[ecount] = private_->hAsyncEvent; @@ -467,7 +487,9 @@ void dynamic_loop::run() { if (!gEnableBusyLoop && timers_.size()) { tv = std::chrono::duration_cast(timers_.front()->deadline_ - now).count(); - if (tv < 0) tv = 0; + if (tv < 0) { + tv = 0; + } } int ret = backend_.runonce(tv); @@ -499,7 +521,7 @@ void dynamic_loop::run() { remove_coro_cb(); } -void dynamic_loop::set(int fd, io *watcher, int events) { +void dynamic_loop::set(int fd, io* watcher, int events) { if (fd < 0) { return; } @@ -522,7 +544,7 @@ void dynamic_loop::stop(int fd) { backend_.stop(fd); } -void dynamic_loop::set(timer *watcher, double t) { +void dynamic_loop::set(timer* watcher, double t) { auto it = std::find(timers_.begin(), timers_.end(), watcher); if (it != timers_.end()) { timers_.erase(it); @@ -531,18 +553,18 @@ void dynamic_loop::set(timer *watcher, double t) { watcher->deadline_ = steady_clock_w::now(); watcher->deadline_ += std::chrono::duration>(int64_t(t * 1000000)); it = std::lower_bound(timers_.begin(), timers_.end(), watcher, - [](const timer *lhs, const timer *rhs) { return lhs->deadline_ < rhs->deadline_; }); + [](const timer* lhs, const timer* rhs) { return lhs->deadline_ < rhs->deadline_; }); timers_.insert(it, watcher); } -void dynamic_loop::stop(timer *watcher) { +void dynamic_loop::stop(timer* watcher) { auto it = std::find(timers_.begin(), timers_.end(), watcher); if (it != timers_.end()) { timers_.erase(it); } } -void dynamic_loop::set(sig *watcher) { +void dynamic_loop::set(sig* watcher) { auto it = std::find(sigs_.begin(), sigs_.end(), watcher); if (it != sigs_.end()) { fprintf(stderr, "sig %d already set\n", watcher->signum_); @@ -566,7 +588,7 @@ void dynamic_loop::set(sig *watcher) { #endif } -void dynamic_loop::stop(sig *watcher) { +void dynamic_loop::stop(sig* watcher) { auto it = std::find(sigs_.begin(), sigs_.end(), watcher); if (it == sigs_.end()) { fprintf(stderr, "sig %d is not set\n", watcher->signum_); @@ -584,7 +606,7 @@ void dynamic_loop::stop(sig *watcher) { #endif } -void dynamic_loop::set(async *watcher) { +void dynamic_loop::set(async* watcher) { auto it = std::find(asyncs_.begin(), asyncs_.end(), watcher); if (it != asyncs_.end()) { return; @@ -593,7 +615,7 @@ void dynamic_loop::set(async *watcher) { asyncs_.push_back(watcher); } -void dynamic_loop::stop(async *watcher) { +void dynamic_loop::stop(async* watcher) { auto it = std::find(asyncs_.begin(), asyncs_.end(), watcher); if (it == asyncs_.end()) { return; @@ -601,7 +623,7 @@ void dynamic_loop::stop(async *watcher) { asyncs_.erase(it); } -void dynamic_loop::send(async *watcher) { +void dynamic_loop::send(async* watcher) { watcher->sent_ = true; bool was = async_sent_.exchange(true); if (!was) { @@ -609,7 +631,7 @@ void dynamic_loop::send(async *watcher) { } } -bool dynamic_loop::is_active(const timer *watcher) const noexcept { +bool dynamic_loop::is_active(const timer* watcher) const noexcept { return std::find(timers_.begin(), timers_.end(), watcher) != timers_.end(); } @@ -630,8 +652,9 @@ void dynamic_loop::async_callback() { (*async)->sent_ = false; (*async)->callback(); async = asyncs_.begin(); - } else + } else { async++; + } } } diff --git a/cpp_src/net/ev/ev.h b/cpp_src/net/ev/ev.h index 2c26539d9..337708975 100644 --- a/cpp_src/net/ev/ev.h +++ b/cpp_src/net/ev/ev.h @@ -51,7 +51,7 @@ class loop_posix_base { bool check_async(int fd); int async_fd_ = -1; - dynamic_loop *owner_ = nullptr; + dynamic_loop* owner_ = nullptr; }; #else // HAVE_EVENT_FD class loop_posix_base { @@ -65,7 +65,7 @@ class loop_posix_base { bool check_async(int fd); int async_fds_[2] = {-1, -1}; - dynamic_loop *owner_ = nullptr; + dynamic_loop* owner_ = nullptr; }; #endif // HAVE_EVENT_FD @@ -74,7 +74,7 @@ class loop_poll_backend : public loop_posix_base { public: loop_poll_backend(); ~loop_poll_backend(); - void init(dynamic_loop *owner); + void init(dynamic_loop* owner); void set(int fd, int events, int oldevents); void stop(int fd); int runonce(int64_t tv); @@ -89,7 +89,7 @@ class loop_select_backend : public loop_posix_base { public: loop_select_backend(); ~loop_select_backend(); - void init(dynamic_loop *owner) noexcept; + void init(dynamic_loop* owner) noexcept; void set(int fd, int events, int oldevents) noexcept; void stop(int fd) noexcept; int runonce(int64_t tv); @@ -106,7 +106,7 @@ class loop_epoll_backend : public loop_posix_base { public: loop_epoll_backend(); ~loop_epoll_backend(); - void init(dynamic_loop *owner); + void init(dynamic_loop* owner); void set(int fd, int events, int oldevents); void stop(int fd); int runonce(int64_t tv); @@ -123,7 +123,7 @@ class loop_wsa_backend { public: loop_wsa_backend(); ~loop_wsa_backend(); - void init(dynamic_loop *owner); + void init(dynamic_loop* owner); void set(int fd, int events, int oldevents); void stop(int fd); int runonce(int64_t tv); @@ -133,7 +133,7 @@ class loop_wsa_backend { static int capacity(); protected: - dynamic_loop *owner_; + dynamic_loop* owner_; std::unique_ptr private_; }; #endif @@ -170,19 +170,19 @@ class dynamic_loop { void sleep(std::chrono::duration dur); template void granular_sleep(std::chrono::duration dur, std::chrono::duration granularity, - Terminater const &terminate); + const Terminater& terminate); protected: - void set(int fd, io *watcher, int events); - void set(timer *watcher, double t); - void set(async *watcher); - void set(sig *watcher); + void set(int fd, io* watcher, int events); + void set(timer* watcher, double t); + void set(async* watcher); + void set(sig* watcher); void stop(int fd); - void stop(timer *watcher); - void stop(async *watcher); - void stop(sig *watcher); - void send(async *watcher); - bool is_active(const timer *watcher) const noexcept; + void stop(timer* watcher); + void stop(async* watcher); + void stop(sig* watcher); + void send(async* watcher); + bool is_active(const timer* watcher) const noexcept; void io_callback(int fd, int events); void async_callback(); @@ -193,13 +193,13 @@ class dynamic_loop { struct fd_handler { int emask_ = 0; int idx = -1; - io *watcher_ = nullptr; + io* watcher_ = nullptr; }; std::vector fds_; - std::vector timers_; - std::vector asyncs_; - std::vector sigs_; + std::vector timers_; + std::vector asyncs_; + std::vector sigs_; bool break_ = false; bool coro_cb_is_set_ = false; std::atomic async_sent_; @@ -230,35 +230,49 @@ class loop_ref { public: void break_loop() noexcept { - if (loop_) loop_->break_loop(); + if (loop_) { + loop_->break_loop(); + } } void run() { - if (loop_) loop_->run(); + if (loop_) { + loop_->run(); + } } void enable_asyncs() { - if (loop_) loop_->backend_.enable_asyncs(); + if (loop_) { + loop_->backend_.enable_asyncs(); + } } bool is_valid() const noexcept { return loop_; } protected: template void set(Args... args) { - if (loop_) loop_->set(args...); + if (loop_) { + loop_->set(args...); + } } template void stop(Args... args) { - if (loop_) loop_->stop(args...); + if (loop_) { + loop_->stop(args...); + } } template void send(Args... args) { - if (loop_) loop_->send(args...); + if (loop_) { + loop_->send(args...); + } } template bool is_active(Args... args) const noexcept { - if (loop_) return loop_->is_active(args...); + if (loop_) { + return loop_->is_active(args...); + } return false; } - dynamic_loop *loop_ = nullptr; + dynamic_loop* loop_ = nullptr; }; class io { @@ -266,10 +280,10 @@ class io { public: io() = default; - io(const io &) = delete; + io(const io&) = delete; ~io() { stop(); } - void set(dynamic_loop &loop_) noexcept { loop.loop_ = &loop_; } + void set(dynamic_loop& loop_) noexcept { loop.loop_ = &loop_; } void set(int events) { loop.set(fd, this, events); } void start(int _fd, int events) { fd = _fd; @@ -281,11 +295,11 @@ class io { } void reset() noexcept { loop.loop_ = nullptr; } - template - void set(K *object) { - func_ = [object](io &watcher, int events) { (static_cast(object)->*func)(watcher, events); }; + template + void set(K* object) { + func_ = [object](io& watcher, int events) { (static_cast(object)->*func)(watcher, events); }; } - void set(std::function func) noexcept { func_ = std::move(func); } + void set(std::function func) noexcept { func_ = std::move(func); } int fd = -1; loop_ref loop; @@ -295,19 +309,19 @@ class io { assertrx(func_ != nullptr); func_(*this, events); } - std::function func_ = nullptr; + std::function func_ = nullptr; }; class timer { friend class dynamic_loop; public: timer() = default; - timer(const timer &) = delete; - timer(timer &&) = default; - timer &operator=(timer &&) = default; + timer(const timer&) = delete; + timer(timer&&) = default; + timer& operator=(timer&&) = default; ~timer() { stop(); } - void set(dynamic_loop &loop_) noexcept { loop.loop_ = &loop_; } + void set(dynamic_loop& loop_) noexcept { loop.loop_ = &loop_; } void start(double t, double p = 0) { period_ = p; loop.set(this, t); @@ -315,11 +329,11 @@ class timer { void stop() { loop.stop(this); } void reset() noexcept { loop.loop_ = nullptr; } - template - void set(K *object) { - func_ = [object](timer &watcher, int t) { (static_cast(object)->*func)(watcher, t); }; + template + void set(K* object) { + func_ = [object](timer& watcher, int t) { (static_cast(object)->*func)(watcher, t); }; } - void set(std::function func) noexcept { func_ = std::move(func); } + void set(std::function func) noexcept { func_ = std::move(func); } bool is_active() const noexcept { return loop.is_active(this); } bool has_period() const noexcept { return period_ > 0.00000001; } @@ -344,7 +358,7 @@ class timer { } } - std::function func_ = nullptr; + std::function func_ = nullptr; double period_ = 0; bool in_coro_storage_ = false; }; @@ -356,7 +370,7 @@ void dynamic_loop::sleep(std::chrono::duration dur) { auto id = coroutine::current(); if (id) { timer tm(timer::coro_t{}); - tm.set([id](timer &, int) { coroutine::resume(id); }); + tm.set([id](timer&, int) { coroutine::resume(id); }); tm.set(*this); double awaitTime = std::chrono::duration_cast(dur).count(); tm.start(awaitTime / 1e6); @@ -371,7 +385,7 @@ void dynamic_loop::sleep(std::chrono::duration dur) { template void dynamic_loop::granular_sleep(std::chrono::duration dur, std::chrono::duration granularity, - Terminater const &terminate) { + const Terminater& terminate) { for (std::chrono::nanoseconds t = dur; t.count() > 0; t -= granularity) { if (terminate()) { return; @@ -385,10 +399,10 @@ class sig { public: sig() = default; // -V730 - sig(const sig &) = delete; + sig(const sig&) = delete; ~sig() { stop(); } - void set(dynamic_loop &loop_) noexcept { loop.loop_ = &loop_; } + void set(dynamic_loop& loop_) noexcept { loop.loop_ = &loop_; } void start(int signum) { signum_ = signum; loop.set(this); @@ -396,11 +410,11 @@ class sig { void stop() { loop.stop(this); } void reset() noexcept { loop.loop_ = nullptr; } - template - void set(K *object) { - func_ = [object](sig &watcher) { (static_cast(object)->*func)(watcher); }; + template + void set(K* object) { + func_ = [object](sig& watcher) { (static_cast(object)->*func)(watcher); }; } - void set(std::function func) noexcept { func_ = std::move(func); } + void set(std::function func) noexcept { func_ = std::move(func); } loop_ref loop; @@ -410,7 +424,7 @@ class sig { func_(*this); } - std::function func_ = nullptr; + std::function func_ = nullptr; #ifndef _WIN32 struct sigaction old_action_; #else @@ -424,10 +438,10 @@ class async { public: async() : sent_(false) {} - async(const async &) = delete; + async(const async&) = delete; ~async() { stop(); } - void set(dynamic_loop &loop_) { + void set(dynamic_loop& loop_) { loop.loop_ = &loop_; loop.enable_asyncs(); } @@ -436,11 +450,11 @@ class async { void reset() noexcept { loop.loop_ = nullptr; } void send() { loop.send(this); } - template - void set(K *object) { - func_ = [object](async &watcher) { (static_cast(object)->*func)(watcher); }; + template + void set(K* object) { + func_ = [object](async& watcher) { (static_cast(object)->*func)(watcher); }; } - void set(std::function func) noexcept { func_ = std::move(func); } + void set(std::function func) noexcept { func_ = std::move(func); } loop_ref loop; @@ -450,7 +464,7 @@ class async { func_(*this); } - std::function func_ = nullptr; + std::function func_ = nullptr; std::atomic sent_; }; diff --git a/cpp_src/net/http/router.cc b/cpp_src/net/http/router.cc index df31b3d6b..faacfc9e6 100644 --- a/cpp_src/net/http/router.cc +++ b/cpp_src/net/http/router.cc @@ -67,7 +67,7 @@ int Context::JSON(int code, std::string_view slice) { return 0; } -int Context::JSON(int code, chunk &&chunk) { +int Context::JSON(int code, chunk&& chunk) { writer->SetContentLength(chunk.len()); writer->SetRespCode(code); writer->SetHeader(http::Header{"Content-Type"sv, "application/json; charset=utf-8"sv}); @@ -75,7 +75,7 @@ int Context::JSON(int code, chunk &&chunk) { return 0; } -int Context::CSV(int code, chunk &&chunk) { +int Context::CSV(int code, chunk&& chunk) { writer->SetRespCode(code); writer->SetHeader(http::Header{"Content-Type"sv, "application/csv; charset=utf-8"sv}); if (auto filename = request->headers.Get("Save-Csv-To"sv); !filename.empty()) { @@ -85,7 +85,7 @@ int Context::CSV(int code, chunk &&chunk) { return 0; } -int Context::MSGPACK(int code, chunk &&chunk) { +int Context::MSGPACK(int code, chunk&& chunk) { writer->SetContentLength(chunk.len()); writer->SetRespCode(code); writer->SetHeader(http::Header{"Content-Type"sv, "application/x-msgpack; charset=utf-8"sv}); @@ -93,7 +93,7 @@ int Context::MSGPACK(int code, chunk &&chunk) { return 0; } -int Context::Protobuf(int code, chunk &&chunk) { +int Context::Protobuf(int code, chunk&& chunk) { writer->SetContentLength(chunk.len()); writer->SetRespCode(code); writer->SetHeader(http::Header{"Content-Type"sv, "application/protobuf; charset=utf-8"sv}); @@ -109,7 +109,7 @@ int Context::String(int code, std::string_view slice) { return 0; } -int Context::String(int code, chunk &&chunk) { +int Context::String(int code, chunk&& chunk) { writer->SetContentLength(chunk.len()); writer->SetRespCode(code); writer->SetHeader(http::Header{"Content-Type"sv, "text/plain; charset=utf-8"sv}); @@ -128,13 +128,27 @@ static std::string_view lookupContentType(std::string_view path) { return "application/octet-stream"sv; } p++; - if (path.substr(p, 4) == "html"sv) return "text/html; charset=utf-8"sv; - if (path.substr(p, 4) == "json"sv) return "application/json; charset=utf-8"sv; - if (path.substr(p, 3) == "yml"sv) return "application/yml; charset=utf-8"sv; - if (path.substr(p, 3) == "css"sv) return "text/css; charset=utf-8"sv; - if (path.substr(p, 2) == "js"sv) return "application/javascript; charset=utf-8"sv; - if (path.substr(p, 4) == "woff"sv) return "font/woff"sv; - if (path.substr(p, 5) == "woff2"sv) return "font/woff2"sv; + if (path.substr(p, 4) == "html"sv) { + return "text/html; charset=utf-8"sv; + } + if (path.substr(p, 4) == "json"sv) { + return "application/json; charset=utf-8"sv; + } + if (path.substr(p, 3) == "yml"sv) { + return "application/yml; charset=utf-8"sv; + } + if (path.substr(p, 3) == "css"sv) { + return "text/css; charset=utf-8"sv; + } + if (path.substr(p, 2) == "js"sv) { + return "application/javascript; charset=utf-8"sv; + } + if (path.substr(p, 4) == "woff"sv) { + return "font/woff"sv; + } + if (path.substr(p, 5) == "woff2"sv) { + return "font/woff2"sv; + } return "application/octet-stream"sv; } @@ -166,19 +180,22 @@ int Context::File(int code, std::string_view path, std::string_view data, bool i std::vector methodNames = {"GET"sv, "POST"sv, "OPTIONS"sv, "HEAD"sv, "PUT"sv, "DELETE"sv, "PATCH"sv}; HttpMethod lookupMethod(std::string_view method) { - for (auto &cm : methodNames) - if (method == cm) return HttpMethod(&cm - &methodNames[0]); + for (auto& cm : methodNames) { + if (method == cm) { + return HttpMethod(&cm - &methodNames[0]); + } + } return HttpMethod(kMethodUnknown); } -int Router::handle(Context &ctx) { +int Router::handle(Context& ctx) { auto method = lookupMethod(ctx.request->method); if (method < 0) { return ctx.String(StatusBadRequest, "Invalid method"sv); } int res = 0; - for (auto &r : routes_[method]) { + for (auto& r : routes_[method]) { std::string_view url = ctx.request->path; std::string_view route = r.path; ctx.request->urlParams.clear(); @@ -187,9 +204,11 @@ int Router::handle(Context &ctx) { auto patternPos = route.find(':'); auto asteriskPos = route.find('*'); if (patternPos == std::string_view::npos || asteriskPos != std::string_view::npos) { - if (url.substr(0, asteriskPos) != route.substr(0, asteriskPos)) break; + if (url.substr(0, asteriskPos) != route.substr(0, asteriskPos)) { + break; + } - for (auto &mw : middlewares_) { + for (auto& mw : middlewares_) { res = mw.func(mw.object, ctx); if (res != 0) { return res; @@ -198,7 +217,9 @@ int Router::handle(Context &ctx) { return r.h.func(r.h.object, ctx); } - if (url.substr(0, patternPos) != route.substr(0, patternPos)) break; + if (url.substr(0, patternPos) != route.substr(0, patternPos)) { + break; + } url = url.substr(patternPos); route = route.substr(patternPos); diff --git a/cpp_src/net/http/router.h b/cpp_src/net/http/router.h index b25d12d56..8ea390ca9 100644 --- a/cpp_src/net/http/router.h +++ b/cpp_src/net/http/router.h @@ -66,7 +66,7 @@ typedef std::string_view UrlParam; struct HttpStatus { HttpStatus() noexcept : code(StatusOK) {} HttpStatus(HttpStatusCode httpcode, std::string httpwhat) : code(httpcode), what(std::move(httpwhat)) {} - explicit HttpStatus(const Error &err) : what(err.what()) { code = errCodeToHttpStatus(err.code()); } + explicit HttpStatus(const Error& err) : what(err.what()) { code = errCodeToHttpStatus(err.code()); } HttpStatusCode code; std::string what; @@ -91,7 +91,7 @@ class Headers : public h_vector { public: using h_vector::h_vector; std::string_view Get(std::string_view name) noexcept { - auto it = std::find_if(begin(), end(), [=](const Header &hdr) { return iequals(name, hdr.name); }); + auto it = std::find_if(begin(), end(), [=](const Header& hdr) { return iequals(name, hdr.name); }); return it != end() ? it->val : std::string_view(); } }; @@ -100,7 +100,7 @@ class Params : public h_vector { public: using h_vector::h_vector; std::string_view Get(const std::string_view name) { - auto it = std::find_if(begin(), end(), [=](const Param ¶m) { return name == param.name; }); + auto it = std::find_if(begin(), end(), [=](const Param& param) { return name == param.name; }); return it != end() ? it->val : std::string_view(); } }; @@ -121,11 +121,11 @@ struct Request { class Writer { public: enum class WriteMode { Default = 0, PreChunkedBody = 1 }; - virtual ssize_t Write(chunk &&ch, WriteMode mode = WriteMode::Default) = 0; + virtual ssize_t Write(chunk&& ch, WriteMode mode = WriteMode::Default) = 0; virtual ssize_t Write(std::string_view data) = 0; virtual chunk GetChunk() = 0; - virtual bool SetHeader(const Header &hdr) = 0; + virtual bool SetHeader(const Header& hdr) = 0; virtual bool SetRespCode(int code) = 0; virtual bool SetContentLength(size_t len) = 0; virtual bool SetConnectionClose() = 0; @@ -137,7 +137,7 @@ class Writer { class Reader { public: - virtual ssize_t Read(void *buf, size_t size) = 0; + virtual ssize_t Read(void* buf, size_t size) = 0; virtual std::string Read(size_t size = INT_MAX) = 0; virtual ssize_t Pending() const = 0; virtual ~Reader() = default; @@ -151,18 +151,18 @@ static constexpr std::string_view kGzSuffix(".gz"); struct Context { int JSON(int code, std::string_view slice); - int JSON(int code, chunk &&chunk); - int CSV(int code, chunk &&chunk); - int MSGPACK(int code, chunk &&chunk); - int Protobuf(int code, chunk &&chunk); + int JSON(int code, chunk&& chunk); + int CSV(int code, chunk&& chunk); + int MSGPACK(int code, chunk&& chunk); + int Protobuf(int code, chunk&& chunk); int String(int code, std::string_view slice); - int String(int code, chunk &&chunk); + int String(int code, chunk&& chunk); int File(int code, std::string_view path, std::string_view data, bool isGzip, bool withCache); int Redirect(std::string_view url); - Request *request; - Writer *writer; - Reader *body; + Request* request; + Writer* writer; + Reader* body; std::unique_ptr clientData; Stat stat; @@ -179,77 +179,77 @@ class Router { /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - template - void POST(const char *path, K *object) { + template + void POST(const char* path, K* object) { addRoute(kMethodPOST, path, object); } /// Add handler for http GET method. /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - template - void GET(const char *path, K *object) { + template + void GET(const char* path, K* object) { addRoute(kMethodGET, path, object); } /// Add handler for http OPTIONS method. /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - template - void OPTIONS(const char *path, K *object) { + template + void OPTIONS(const char* path, K* object) { addRoute(kMethodOPTIONS, path, object); } - template + template /// Add handler for http DELETE method. /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - void DELETE(const char *path, K *object) { + void DELETE(const char* path, K* object) { addRoute(kMethodDELETE, path, object); } /// Add handler for http PUT method. /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - template - void PUT(const char *path, K *object) { + template + void PUT(const char* path, K* object) { addRoute(kMethodPUT, path, object); } /// Add handler for http PATCH method. /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - template - void PATCH(const char *path, K *object) { + template + void PATCH(const char* path, K* object) { addRoute(kMethodPATCH, path, object); } /// Add handler for http HEAD method. /// @param path - URI pattern /// @param object - handler class object /// @tparam func - handler - template - void HEAD(const char *path, K *object) { + template + void HEAD(const char* path, K* object) { addRoute(kMethodHEAD, path, object); } /// Add middleware /// @param object - handler class object /// @tparam func - handler - template - void Middleware(K *object) { + template + void Middleware(K* object) { middlewares_.emplace_back(func_wrapper, object); } /// Add logger for requests /// @param object - logger class object /// @tparam func - logger - template - void Logger(K *object) { - logger_ = [=](Context &ctx) { (static_cast(object)->*func)(ctx); }; + template + void Logger(K* object) { + logger_ = [=](Context& ctx) { (static_cast(object)->*func)(ctx); }; } /// Add default handler for not found URI's /// @tparam func - handler - template - void NotFound(K *object) { + template + void NotFound(K* object) { notFoundHandler_ = Handler{func_wrapper, object}; } @@ -257,36 +257,38 @@ class Router { /// @param object class object /// @param func function, to be called on response sent template - void OnResponse(K *object, void (K::*func)(Context &ctx)) { - onResponse_ = [=](Context &ctx) { (static_cast(object)->*func)(ctx); }; + void OnResponse(K* object, void (K::*func)(Context& ctx)) { + onResponse_ = [=](Context& ctx) { (static_cast(object)->*func)(ctx); }; } protected: - int handle(Context &ctx); - void log(Context &ctx) { - if (logger_) logger_(ctx); + int handle(Context& ctx); + void log(Context& ctx) { + if (logger_) { + logger_(ctx); + } } - template - void addRoute(HttpMethod method, const char *path, K *object) { + template + void addRoute(HttpMethod method, const char* path, K* object) { routes_[method].emplace_back(path, Handler{func_wrapper, object}); } - template - static int func_wrapper(void *obj, Context &ctx) { - return (static_cast(obj)->*func)(ctx); + template + static int func_wrapper(void* obj, Context& ctx) { + return (static_cast(obj)->*func)(ctx); } struct Handler { - using FuncT = std::function; - Handler(FuncT &&f, void *o) noexcept : func(std::move(f)), object(o) {} + using FuncT = std::function; + Handler(FuncT&& f, void* o) noexcept : func(std::move(f)), object(o) {} FuncT func; - void *object; + void* object; }; struct Route { - Route(std::string &&p, Handler &&_h) : path(std::move(p)), h(std::move(_h)) {} + Route(std::string&& p, Handler&& _h) : path(std::move(p)), h(std::move(_h)) {} std::string path; Handler h; @@ -296,8 +298,8 @@ class Router { std::vector middlewares_; Handler notFoundHandler_{{}, nullptr}; - std::function logger_; - std::function onResponse_; + std::function logger_; + std::function onResponse_; }; } // namespace http } // namespace net diff --git a/cpp_src/net/http/serverconnection.cc b/cpp_src/net/http/serverconnection.cc index 5c51b5814..464880c23 100644 --- a/cpp_src/net/http/serverconnection.cc +++ b/cpp_src/net/http/serverconnection.cc @@ -19,14 +19,14 @@ using namespace std::string_view_literals; static const std::string_view kStrEOL = "\r\n"sv; extern std::unordered_map kHTTPCodes; -ServerConnection::ServerConnection(socket &&s, ev::dynamic_loop &loop, Router &router, size_t maxRequestSize) +ServerConnection::ServerConnection(socket&& s, ev::dynamic_loop& loop, Router& router, size_t maxRequestSize) : ConnectionST(std::move(s), loop, false, maxRequestSize < kConnReadbufSize ? maxRequestSize : kConnReadbufSize), router_(router), maxRequestSize_(maxRequestSize) { callback(io_, ev::READ); } -bool ServerConnection::Restart(socket &&s) { +bool ServerConnection::Restart(socket&& s) { restart(std::move(s)); bodyLeft_ = 0; formData_ = false; @@ -36,24 +36,28 @@ bool ServerConnection::Restart(socket &&s) { return true; } -void ServerConnection::Attach(ev::dynamic_loop &loop) { - if (!attached_) attach(loop); +void ServerConnection::Attach(ev::dynamic_loop& loop) { + if (!attached_) { + attach(loop); + } } void ServerConnection::Detach() { - if (attached_) detach(); + if (attached_) { + detach(); + } } void ServerConnection::onClose() {} -void ServerConnection::handleException(Context &ctx, const Error &status) { - auto writer = dynamic_cast(ctx.writer); +void ServerConnection::handleException(Context& ctx, const Error& status) { + auto writer = dynamic_cast(ctx.writer); if (writer && !writer->IsRespSent()) { HttpStatus httpStatus(status); setJsonStatus(ctx, false, httpStatus.code, httpStatus.what); } } -void ServerConnection::setJsonStatus(Context &ctx, bool success, int responseCode, const std::string &status) { +void ServerConnection::setJsonStatus(Context& ctx, bool success, int responseCode, const std::string& status) { WrSerializer ser; JsonBuilder builder(ser); builder.Put("success", success); @@ -63,7 +67,7 @@ void ServerConnection::setJsonStatus(Context &ctx, bool success, int responseCod ctx.JSON(responseCode, ser.Slice()); } -void ServerConnection::handleRequest(Request &req) { +void ServerConnection::handleRequest(Request& req) { ResponseWriter writer(this); BodyReader reader(this); Context ctx; @@ -74,13 +78,13 @@ void ServerConnection::handleRequest(Request &req) { try { router_.handle(ctx); - } catch (const HttpStatus &status) { + } catch (const HttpStatus& status) { if (!writer.IsRespSent()) { setJsonStatus(ctx, false, status.code, status.what); } - } catch (const Error &status) { + } catch (const Error& status) { handleException(ctx, status); - } catch (const std::exception &e) { + } catch (const std::exception& e) { handleException(ctx, Error(errLogic, e.what())); } catch (...) { handleException(ctx, Error(errLogic, "Unknown exception")); @@ -94,7 +98,7 @@ void ServerConnection::handleRequest(Request &req) { } } -void ServerConnection::badRequest(int code, const char *msg) { +void ServerConnection::badRequest(int code, const char* msg) { ResponseWriter writer(this); HandlerStat stat; Context ctx; @@ -113,15 +117,19 @@ void ServerConnection::badRequest(int code, const char *msg) { } void ServerConnection::parseParams(std::string_view str) { - const char *p = str.data(); + const char* p = str.data(); const char *name = nullptr, *value = nullptr; size_t namelen = 0; for (size_t l = 0; l < str.length(); l++) { - if (!name) name = p; + if (!name) { + name = p; + } switch (*p) { case '&': - if (!value) namelen = p - name; + if (!value) { + namelen = p - name; + } if (name) { request_.params.emplace_back(std::string_view(name, namelen), std::string_view(value, value ? p - value : 0)); name = value = nullptr; @@ -140,7 +148,9 @@ void ServerConnection::parseParams(std::string_view str) { p++; } if (name) { - if (!value) namelen = p - name; + if (!value) { + namelen = p - name; + } request_.params.emplace_back(std::string_view(name, namelen), std::string_view(value, value ? p - value : 0)); } } @@ -151,7 +161,9 @@ void ServerConnection::writeHttpResponse(int code) { ser << "HTTP/1.1 "sv << code << ' '; auto it = kHTTPCodes.find(code); - if (it != kHTTPCodes.end()) ser << it->second; + if (it != kHTTPCodes.end()) { + ser << it->second; + } ser << kStrEOL; wrBuf_.write(ser.DetachChunk()); } @@ -279,32 +291,40 @@ ServerConnection::ReadResT ServerConnection::onRead() { break; } } - if (!rdBuf_.size() && !bodyLeft_) rdBuf_.clear(); - } catch (const Error &e) { + if (!rdBuf_.size() && !bodyLeft_) { + rdBuf_.clear(); + } + } catch (const Error& e) { fprintf(stderr, "Dropping HTTP-connection. Reason: %s\n", e.what().c_str()); closeConn_ = true; } return ReadResT::Default; } -bool ServerConnection::ResponseWriter::SetHeader(const Header &hdr) { - if (respSend_) return false; +bool ServerConnection::ResponseWriter::SetHeader(const Header& hdr) { + if (respSend_) { + return false; + } headers_ << hdr.name << ": "sv << hdr.val << kStrEOL; return true; } bool ServerConnection::ResponseWriter::SetRespCode(int code) { - if (respSend_) return false; + if (respSend_) { + return false; + } code_ = code; return true; } bool ServerConnection::ResponseWriter::SetContentLength(size_t length) { - if (respSend_) return false; + if (respSend_) { + return false; + } contentLength_ = length; return true; } -ssize_t ServerConnection::ResponseWriter::Write(chunk &&chunk, Writer::WriteMode mode) { +ssize_t ServerConnection::ResponseWriter::Write(chunk&& chunk, Writer::WriteMode mode) { char szBuf[64], dtBuf[128]; if (!respSend_) { conn_->writeHttpResponse(code_); @@ -361,9 +381,9 @@ bool ServerConnection::ResponseWriter::SetConnectionClose() { return true; } -ssize_t ServerConnection::BodyReader::Read(void *buf, size_t size) { +ssize_t ServerConnection::BodyReader::Read(void* buf, size_t size) { if (conn_->bodyLeft_ > 0) { - size_t readed = conn_->rdBuf_.read(reinterpret_cast(buf), std::min(ssize_t(size), conn_->bodyLeft_)); + size_t readed = conn_->rdBuf_.read(reinterpret_cast(buf), std::min(ssize_t(size), conn_->bodyLeft_)); conn_->bodyLeft_ -= readed; return readed; } diff --git a/cpp_src/net/http/serverconnection.h b/cpp_src/net/http/serverconnection.h index 261dca784..f95d0faa4 100644 --- a/cpp_src/net/http/serverconnection.h +++ b/cpp_src/net/http/serverconnection.h @@ -14,10 +14,10 @@ namespace http { const ssize_t kHttpMaxHeaders = 128; class ServerConnection final : public IServerConnection, public ConnectionST { public: - ServerConnection(socket &&s, ev::dynamic_loop &loop, Router &router, size_t maxRequestSize); + ServerConnection(socket&& s, ev::dynamic_loop& loop, Router& router, size_t maxRequestSize); - static ConnectionFactory NewFactory(Router &router, size_t maxRequestSize) { - return [&router, maxRequestSize](ev::dynamic_loop &loop, socket &&s, bool allowCustomBalancing) { + static ConnectionFactory NewFactory(Router& router, size_t maxRequestSize) { + return [&router, maxRequestSize](ev::dynamic_loop& loop, socket&& s, bool allowCustomBalancing) { (void)allowCustomBalancing; return new ServerConnection(std::move(s), loop, router, maxRequestSize); }; @@ -25,32 +25,32 @@ class ServerConnection final : public IServerConnection, public ConnectionST { bool IsFinished() const noexcept override final { return !sock_.valid(); } BalancingType GetBalancingType() const noexcept override final { return BalancingType::None; } - void SetRebalanceCallback(std::function cb) override final { (void)cb; } + void SetRebalanceCallback(std::function cb) override final { (void)cb; } bool HasPendingData() const noexcept override final { return false; } void HandlePendingData() override final {} - bool Restart(socket &&s) override final; + bool Restart(socket&& s) override final; void Detach() override final; - void Attach(ev::dynamic_loop &loop) override final; + void Attach(ev::dynamic_loop& loop) override final; protected: class BodyReader : public Reader { public: - BodyReader(ServerConnection *conn) : conn_(conn) {} - ssize_t Read(void *buf, size_t size) override final; + BodyReader(ServerConnection* conn) : conn_(conn) {} + ssize_t Read(void* buf, size_t size) override final; std::string Read(size_t size = INT_MAX) override final; ssize_t Pending() const override final; protected: - ServerConnection *conn_; + ServerConnection* conn_; }; class ResponseWriter : public Writer { public: - ResponseWriter(ServerConnection *conn) : headers_(conn->wrBuf_.get_chunk()), conn_(conn) {} - virtual bool SetHeader(const Header &hdr) override final; + ResponseWriter(ServerConnection* conn) : headers_(conn->wrBuf_.get_chunk()), conn_(conn) {} + virtual bool SetHeader(const Header& hdr) override final; virtual bool SetRespCode(int code) override final; virtual bool SetContentLength(size_t len) override final; virtual bool SetConnectionClose() override final; - ssize_t Write(chunk &&chunk, Writer::WriteMode mode = WriteMode::Default) override final; + ssize_t Write(chunk&& chunk, Writer::WriteMode mode = WriteMode::Default) override final; ssize_t Write(std::string_view data) override final; virtual chunk GetChunk() override final; @@ -66,20 +66,20 @@ class ServerConnection final : public IServerConnection, public ConnectionST { WrSerializer headers_; bool respSend_ = false; ssize_t contentLength_ = -1, written_ = 0; - ServerConnection *conn_; + ServerConnection* conn_; }; - void handleRequest(Request &req); - void badRequest(int code, const char *msg); + void handleRequest(Request& req); + void badRequest(int code, const char* msg); ReadResT onRead() override; void onClose() override; - void handleException(Context &ctx, const Error &err); + void handleException(Context& ctx, const Error& err); void parseParams(std::string_view str); void writeHttpResponse(int code); - void setJsonStatus(Context &ctx, bool success, int responseCode, const std::string &status); + void setJsonStatus(Context& ctx, bool success, int responseCode, const std::string& status); - Router &router_; + Router& router_; Request request_; ssize_t bodyLeft_ = 0; bool formData_ = false; diff --git a/cpp_src/net/iserverconnection.h b/cpp_src/net/iserverconnection.h index d51d3b3aa..b005a788c 100644 --- a/cpp_src/net/iserverconnection.h +++ b/cpp_src/net/iserverconnection.h @@ -23,7 +23,7 @@ class IServerConnection { virtual BalancingType GetBalancingType() const noexcept = 0; /// Set callback, which move this connection into separate thread - virtual void SetRebalanceCallback(std::function cb) = 0; + virtual void SetRebalanceCallback(std::function cb) = 0; /// @return true if this connection has pending data virtual bool HasPendingData() const noexcept = 0; @@ -33,10 +33,10 @@ class IServerConnection { /// Restart connection /// @param s - socket of the accepted connection. /// @return true - if successfuly restarted, false - if connection can't be restarted. - virtual bool Restart(socket &&s) = 0; + virtual bool Restart(socket&& s) = 0; /// Attach connection to another listener loop. Must be called from thread of loop /// @param loop - another loop to bind - virtual void Attach(ev::dynamic_loop &loop) = 0; + virtual void Attach(ev::dynamic_loop& loop) = 0; /// Detach connection from listener loop. Must be called from thread of current loop virtual void Detach() = 0; }; @@ -45,7 +45,7 @@ class IServerConnection { /// @param loop - Current loop of Listener's thread. /// @param s - Socket of the accepted connection. /// @param allowCustomBalancing - true, if caller supports custom balancing hints -typedef std::function ConnectionFactory; +typedef std::function ConnectionFactory; } // namespace net } // namespace reindexer diff --git a/cpp_src/net/listener.cc b/cpp_src/net/listener.cc index b574adec5..aa6b3ce6a 100644 --- a/cpp_src/net/listener.cc +++ b/cpp_src/net/listener.cc @@ -16,7 +16,7 @@ static std::atomic_uint_fast64_t counter_; constexpr int kListenCount = 500; template -Listener::Listener(ev::dynamic_loop &loop, std::shared_ptr shared) +Listener::Listener(ev::dynamic_loop& loop, std::shared_ptr shared) : loop_(loop), shared_(std::move(shared)), id_(counter_.fetch_add(1, std::memory_order_relaxed)) { io_.set(this); io_.set(loop); @@ -31,7 +31,7 @@ Listener::Listener(ev::dynamic_loop &loop, std::shared_ptr shared) } template -Listener::Listener(ev::dynamic_loop &loop, ConnectionFactory &&connFactory, int maxListeners) +Listener::Listener(ev::dynamic_loop& loop, ConnectionFactory&& connFactory, int maxListeners) : Listener(loop, std::make_shared(std::move(connFactory), (maxListeners ? maxListeners : std::thread::hardware_concurrency()) + 1)) {} @@ -76,7 +76,7 @@ bool Listener::Bind(std::string addr, socket_domain type) { } template -void Listener::io_accept(ev::io & /*watcher*/, int revents) { +void Listener::io_accept(ev::io& /*watcher*/, int revents) { if (ev::ERROR & revents) { perror("got invalid event"); return; @@ -131,7 +131,7 @@ void Listener::io_accept(ev::io & /*watcher*/, int revents) { auto c = conn.get(); accepted_.emplace(std::move(conn)); c->SetRebalanceCallback( - [this](IServerConnection *c, IServerConnection::BalancingType type) { rebalance_conn(c, type); }); + [this](IServerConnection* c, IServerConnection::BalancingType type) { rebalance_conn(c, type); }); break; } } @@ -149,7 +149,7 @@ void Listener::io_accept(ev::io & /*watcher*/, int revents) { } template -void Listener::timeout_cb(ev::periodic &, int) { +void Listener::timeout_cb(ev::periodic&, int) { const bool enableReuseIdle = !std::getenv("REINDEXER_NOREUSEIDLE"); std::unique_lock lck(shared_->mtx_); @@ -164,7 +164,9 @@ void Listener::timeout_cb(ev::periodic &, int) { connections_[i].reset(); } - if (i != connections_.size() - 1) connections_[i] = std::move(connections_.back()); + if (i != connections_.size() - 1) { + connections_[i] = std::move(connections_.back()); + } connections_.pop_back(); shared_->ts_ = steady_clock_w::now_coarse(); } else { @@ -250,7 +252,7 @@ void Listener::rebalance_from_acceptor() { } template -void Listener::rebalance_conn(IServerConnection *c, IServerConnection::BalancingType type) { +void Listener::rebalance_conn(IServerConnection* c, IServerConnection::BalancingType type) { std::unique_lock lck(shared_->mtx_); auto found = accepted_.find(c); if (found == accepted_.end()) { @@ -273,7 +275,7 @@ void Listener::rebalance_conn(IServerConnection *c, IServerConnection::Balan } template -void Listener::run_dedicated_thread(std::unique_ptr &&conn) { +void Listener::run_dedicated_thread(std::unique_ptr&& conn) { std::thread th([this, conn = std::move(conn)]() mutable { try { #if REINDEX_WITH_GPERFTOOLS @@ -283,7 +285,7 @@ void Listener::run_dedicated_thread(std::unique_ptr &&con #endif ev::dynamic_loop loop; ev::async async; - async.set([](ev::async &a) { a.loop.break_loop(); }); + async.set([](ev::async& a) { a.loop.break_loop(); }); async.set(loop); async.start(); @@ -304,13 +306,13 @@ void Listener::run_dedicated_thread(std::unique_ptr &&con } lck.lock(); const auto it = std::find_if(shared_->dedicatedWorkers_.begin(), shared_->dedicatedWorkers_.end(), - [&pc](const typename Shared::Worker &cw) { return cw.conn.get() == pc; }); + [&pc](const typename Shared::Worker& cw) { return cw.conn.get() == pc; }); assertrx(it != shared_->dedicatedWorkers_.end()); shared_->dedicatedWorkers_.erase(it); logPrintf(LogTrace, "Listener (%s) dedicated thread finished. %d left", shared_->addr_, shared_->dedicatedWorkers_.size()); - } catch (Error &e) { + } catch (Error& e) { logPrintf(LogError, "Unhandled excpetion in listener thread: %s", e.what()); - } catch (std::exception &e) { + } catch (std::exception& e) { logPrintf(LogError, "Unhandled excpetion in listener thread: %s", e.what()); } catch (...) { logPrintf(LogError, "Unhandled excpetion in listener thread"); @@ -333,12 +335,12 @@ void Listener::startup_shared_thread() { } template -void Listener::async_cb(ev::async &watcher) { +void Listener::async_cb(ev::async& watcher) { logPrintf(LogInfo, "Listener(%s) %d async received", shared_->addr_, id_); - h_vector conns; + h_vector conns; { std::lock_guard lck(shared_->mtx_); - for (auto &it : connections_) { + for (auto& it : connections_) { if (!it->IsFinished()) { if constexpr (LT == ListenerType::Mixed) { if (it->HasPendingData()) { @@ -353,7 +355,7 @@ void Listener::async_cb(ev::async &watcher) { } } if constexpr (LT == ListenerType::Mixed) { - for (auto &conn : conns) { + for (auto& conn : conns) { conn->Attach(loop_); conn->HandlePendingData(); } @@ -365,10 +367,10 @@ template void Listener::Stop() { std::unique_lock lck(shared_->mtx_); shared_->terminating_ = true; - for (auto &listener : shared_->listeners_) { + for (auto& listener : shared_->listeners_) { listener->async_.send(); } - for (auto &worker : shared_->dedicatedWorkers_) { + for (auto& worker : shared_->dedicatedWorkers_) { worker.async->send(); } assertrx(this == shared_->listeners_.front()); @@ -382,8 +384,8 @@ void Listener::Stop() { template void Listener::clone(std::unique_ptr d) noexcept { assertrx(d); - auto &shared = d->GetShared(); - const auto &listener = d->GetListener(); + auto& shared = d->GetShared(); + const auto& listener = d->GetListener(); try { #if REINDEX_WITH_GPERFTOOLS @@ -392,9 +394,9 @@ void Listener::clone(std::unique_ptr d) noexcept { } #endif d->Loop(); - } catch (Error &e) { + } catch (Error& e) { logPrintf(LogError, "Unhandled excpetion in listener thread (%s): %s", shared.addr_, e.what()); - } catch (std::exception &e) { + } catch (std::exception& e) { logPrintf(LogError, "Unhandled excpetion in listener thread (%s): %s", shared.addr_, e.what()); } catch (...) { logPrintf(LogError, "Unhandled excpetion in listener thread (%s): ", shared.addr_); @@ -411,11 +413,13 @@ void Listener::clone(std::unique_ptr d) noexcept { template void Listener::reserve_stack() { char placeholder[0x8000]; - for (size_t i = 0; i < sizeof(placeholder); i += 4096) placeholder[i] = i & 0xFF; + for (size_t i = 0; i < sizeof(placeholder); i += 4096) { + placeholder[i] = i & 0xFF; + } } template -Listener::Shared::Shared(ConnectionFactory &&connFactory, int maxListeners) +Listener::Shared::Shared(ConnectionFactory&& connFactory, int maxListeners) : maxListeners_(maxListeners), listenersCount_(1), connFactory_(std::move(connFactory)), terminating_(false) {} template @@ -425,7 +429,7 @@ Listener::Shared::~Shared() { } } -ForkedListener::ForkedListener(ev::dynamic_loop &loop, ConnectionFactory &&connFactory) +ForkedListener::ForkedListener(ev::dynamic_loop& loop, ConnectionFactory&& connFactory) : connFactory_(std::move(connFactory)), loop_(loop) { io_.set(this); io_.set(loop); @@ -464,7 +468,7 @@ bool ForkedListener::Bind(std::string addr, socket_domain type) { return true; } -void ForkedListener::io_accept(ev::io & /*watcher*/, int revents) { +void ForkedListener::io_accept(ev::io& /*watcher*/, int revents) { if (ev::ERROR & revents) { perror("got invalid event"); return; @@ -494,7 +498,7 @@ void ForkedListener::io_accept(ev::io & /*watcher*/, int revents) { #endif ev::dynamic_loop loop; ev::async async; - async.set([](ev::async &a) { a.loop.break_loop(); }); + async.set([](ev::async& a) { a.loop.break_loop(); }); async.set(loop); async.start(); @@ -515,14 +519,14 @@ void ForkedListener::io_accept(ev::io & /*watcher*/, int revents) { } } lck.lock(); - const auto it = std::find_if(workers_.begin(), workers_.end(), [&pc](const Worker &cw) { return cw.conn.get() == pc; }); + const auto it = std::find_if(workers_.begin(), workers_.end(), [&pc](const Worker& cw) { return cw.conn.get() == pc; }); assertrx(it != workers_.end()); workers_.erase(it); logPrintf(LogTrace, "Listener (%s) dedicated thread finished. %d left", addr_, workers_.size()); } - } catch (Error &e) { + } catch (Error& e) { logPrintf(LogError, "Unhandled excpetion in listener thread: %s", e.what()); - } catch (std::exception &e) { + } catch (std::exception& e) { logPrintf(LogError, "Unhandled excpetion in listener thread: %s", e.what()); } catch (...) { logPrintf(LogError, "Unhandled excpetion in listener thread"); @@ -532,7 +536,7 @@ void ForkedListener::io_accept(ev::io & /*watcher*/, int revents) { th.detach(); } -void ForkedListener::async_cb(ev::async &watcher) { +void ForkedListener::async_cb(ev::async& watcher) { logPrintf(LogInfo, "Listener(%s) async received", addr_); watcher.loop.break_loop(); } @@ -541,7 +545,7 @@ void ForkedListener::Stop() { terminating_ = true; async_.send(); std::unique_lock lck(mtx_); - for (auto &worker : workers_) { + for (auto& worker : workers_) { worker.async->send(); } while (runningThreadsCount_ || !workers_.empty()) { diff --git a/cpp_src/net/listener.h b/cpp_src/net/listener.h index 06453907e..ce51a8f28 100644 --- a/cpp_src/net/listener.h +++ b/cpp_src/net/listener.h @@ -28,9 +28,9 @@ class IListener { struct ConnPtrEqual { using is_transparent = void; - bool operator()(const IServerConnection *lhs, const std::unique_ptr &rhs) const noexcept { return lhs == rhs.get(); } - bool operator()(const std::unique_ptr &lhs, const IServerConnection *rhs) const noexcept { return lhs.get() == rhs; } - bool operator()(const std::unique_ptr &lhs, const std::unique_ptr &rhs) const noexcept { + bool operator()(const IServerConnection* lhs, const std::unique_ptr& rhs) const noexcept { return lhs == rhs.get(); } + bool operator()(const std::unique_ptr& lhs, const IServerConnection* rhs) const noexcept { return lhs.get() == rhs; } + bool operator()(const std::unique_ptr& lhs, const std::unique_ptr& rhs) const noexcept { return lhs == rhs; } }; @@ -38,8 +38,8 @@ struct ConnPtrEqual { struct ConnPtrHash { using transparent_key_equal = ConnPtrEqual; - size_t operator()(const IServerConnection *ptr) const noexcept { return std::hash()(uintptr_t(ptr)); } - size_t operator()(const std::unique_ptr &ptr) const noexcept { return std::hash()(uintptr_t(ptr.get())); } + size_t operator()(const IServerConnection* ptr) const noexcept { return std::hash()(uintptr_t(ptr)); } + size_t operator()(const std::unique_ptr& ptr) const noexcept { return std::hash()(uintptr_t(ptr.get())); } }; enum class ListenerType { @@ -65,7 +65,7 @@ class Listener final : public IListener { /// @param loop - ev::loop of caller's thread, listener's socket will be binded to that loop. /// @param connFactory - Connection factory, will create objects with IServerConnection interface implementation. /// @param maxListeners - Maximum number of threads, which listener will utilize. std::thread::hardware_concurrency() by default - Listener(ev::dynamic_loop &loop, ConnectionFactory &&connFactory, int maxListeners = 0); + Listener(ev::dynamic_loop& loop, ConnectionFactory&& connFactory, int maxListeners = 0); ~Listener() override; /// Bind listener to specified host:port /// @param addr - tcp host:port for bind or file path for the unix domain socket @@ -77,22 +77,22 @@ class Listener final : public IListener { protected: void reserve_stack(); - void io_accept(ev::io &watcher, int revents); - void timeout_cb(ev::periodic &watcher, int); - void async_cb(ev::async &watcher); + void io_accept(ev::io& watcher, int revents); + void timeout_cb(ev::periodic& watcher, int); + void async_cb(ev::async& watcher); void rebalance(); void rebalance_from_acceptor(); // Locks shared_->mtx_. Should not be calles under external lock. - void rebalance_conn(IServerConnection *, IServerConnection::BalancingType type); - void run_dedicated_thread(std::unique_ptr &&conn); + void rebalance_conn(IServerConnection*, IServerConnection::BalancingType type); + void run_dedicated_thread(std::unique_ptr&& conn); // May lock shared_->mtx_. Should not be calles under external lock. void startup_shared_thread(); struct Shared { struct Worker { - Worker(std::unique_ptr &&_conn, ev::async &_async) : conn(std::move(_conn)), async(&_async) {} - Worker(Worker &&other) noexcept : conn(std::move(other.conn)), async(other.async) {} - Worker &operator=(Worker &&other) noexcept { + Worker(std::unique_ptr&& _conn, ev::async& _async) : conn(std::move(_conn)), async(&_async) {} + Worker(Worker&& other) noexcept : conn(std::move(other.conn)), async(other.async) {} + Worker& operator=(Worker&& other) noexcept { if (&other != this) { conn = std::move(other.conn); async = other.async; @@ -101,16 +101,16 @@ class Listener final : public IListener { } std::unique_ptr conn; - ev::async *async; + ev::async* async; }; - Shared(ConnectionFactory &&connFactory, int maxListeners); + Shared(ConnectionFactory&& connFactory, int maxListeners); ~Shared(); lst_socket sock_; const int maxListeners_; std::atomic listenersCount_ = {0}; std::atomic connCount_ = {0}; - std::vector listeners_; + std::vector listeners_; std::mutex mtx_; ConnectionFactory connFactory_; std::atomic terminating_; @@ -131,8 +131,8 @@ class Listener final : public IListener { loop_.run(); } } - const Listener &GetListener() const noexcept { return listener_; } - Shared &GetShared() noexcept { return *shared_; } + const Listener& GetListener() const noexcept { return listener_; } + Shared& GetShared() noexcept { return *shared_; } private: ev::dynamic_loop loop_; @@ -140,12 +140,12 @@ class Listener final : public IListener { std::shared_ptr shared_; }; // Locks shared_->mtx_. Should not be calles under external lock. - Listener(ev::dynamic_loop &loop, std::shared_ptr shared); + Listener(ev::dynamic_loop& loop, std::shared_ptr shared); static void clone(std::unique_ptr d) noexcept; ev::io io_; ev::periodic timer_; - ev::dynamic_loop &loop_; + ev::dynamic_loop& loop_; ev::async async_; std::shared_ptr shared_; std::vector> connections_; @@ -159,7 +159,7 @@ class ForkedListener final : public IListener { /// Constructs new listner object. /// @param loop - ev::loop of caller's thread, listener's socket will be binded to that loop. /// @param connFactory - Connection factory, will create objects with IServerConnection interface implementation. - ForkedListener(ev::dynamic_loop &loop, ConnectionFactory &&connFactory); + ForkedListener(ev::dynamic_loop& loop, ConnectionFactory&& connFactory); ~ForkedListener() override; /// Bind listener to specified host:port /// @param addr - tcp host:port for bind or file path for the unix domain socket @@ -170,13 +170,13 @@ class ForkedListener final : public IListener { void Stop() override; protected: - void io_accept(ev::io &watcher, int revents); - void async_cb(ev::async &watcher); + void io_accept(ev::io& watcher, int revents); + void async_cb(ev::async& watcher); struct Worker { - Worker(std::unique_ptr &&conn, ev::async &async) : conn(std::move(conn)), async(&async) {} - Worker(Worker &&other) noexcept : conn(std::move(other.conn)), async(other.async) {} - Worker &operator=(Worker &&other) noexcept { + Worker(std::unique_ptr&& conn, ev::async& async) : conn(std::move(conn)), async(&async) {} + Worker(Worker&& other) noexcept : conn(std::move(other.conn)), async(other.async) {} + Worker& operator=(Worker&& other) noexcept { if (&other != this) { conn = std::move(other.conn); async = other.async; @@ -185,7 +185,7 @@ class ForkedListener final : public IListener { } std::unique_ptr conn; - ev::async *async; + ev::async* async; }; lst_socket sock_; @@ -195,7 +195,7 @@ class ForkedListener final : public IListener { std::string addr_; ev::io io_; - ev::dynamic_loop &loop_; + ev::dynamic_loop& loop_; ev::async async_; std::vector workers_; std::atomic runningThreadsCount_ = {0}; diff --git a/cpp_src/net/manualconnection.cc b/cpp_src/net/manualconnection.cc index bf0cc3fb1..d8f218eb4 100644 --- a/cpp_src/net/manualconnection.cc +++ b/cpp_src/net/manualconnection.cc @@ -7,14 +7,18 @@ namespace net { manual_connection::manual_connection(size_t rd_buf_size, bool enable_stat) : buffered_data_(rd_buf_size), stats_(enable_stat ? new connection_stats_collector : nullptr) {} -void manual_connection::attach(ev::dynamic_loop &loop) noexcept { +void manual_connection::attach(ev::dynamic_loop& loop) noexcept { assertrx(!attached_); io_.set(this); io_.set(loop); connect_timer_.set(this); connect_timer_.set(loop); - if (stats_) stats_->attach(loop); - if (cur_events_) io_.start(sock_.fd(), cur_events_); + if (stats_) { + stats_->attach(loop); + } + if (cur_events_) { + io_.start(sock_.fd(), cur_events_); + } attached_ = true; } @@ -24,7 +28,9 @@ void manual_connection::detach() noexcept { io_.reset(); connect_timer_.stop(); connect_timer_.reset(); - if (stats_) stats_->detach(); + if (stats_) { + stats_->detach(); + } attached_ = false; } @@ -51,13 +57,17 @@ void manual_connection::close_conn(int err) { if (hadWData) { on_async_op_done(w_data_, err); } - if (stats_) stats_->stop(); + if (stats_) { + stats_->stop(); + } } -void manual_connection::restart(socket &&s) { +void manual_connection::restart(socket&& s) { assertrx(!sock_.valid()); sock_ = std::move(s); - if (stats_) stats_->restart(); + if (stats_) { + stats_->restart(); + } } int manual_connection::async_connect(std::string_view addr, socket_domain type) noexcept { @@ -83,7 +93,7 @@ int manual_connection::async_connect(std::string_view addr, socket_domain type) return 0; } -ssize_t manual_connection::write(span wr_buf, transfer_data &transfer, int &err_ref) { +ssize_t manual_connection::write(span wr_buf, transfer_data& transfer, int& err_ref) { err_ref = 0; ssize_t written = -1; auto cur_buf = wr_buf.subspan(transfer.transfered_size()); @@ -109,7 +119,9 @@ ssize_t manual_connection::write(span wr_buf, transfer_data &transfer, int assertrx(wr_buf.size() >= transfer.transfered_size()); auto remaining = wr_buf.size() - transfer.transfered_size(); - if (stats_) stats_->update_write_stats(written, remaining); + if (stats_) { + stats_->update_write_stats(written, remaining); + } if (remaining == 0) { on_async_op_done(w_data_, 0); @@ -117,7 +129,7 @@ ssize_t manual_connection::write(span wr_buf, transfer_data &transfer, int return written; } -ssize_t manual_connection::read(span rd_buf, transfer_data &transfer, int &err_ref) { +ssize_t manual_connection::read(span rd_buf, transfer_data& transfer, int& err_ref) { bool need_read = !transfer.expected_size(); ssize_t nread = 0; ssize_t read_this_time = 0; @@ -133,10 +145,14 @@ ssize_t manual_connection::read(span rd_buf, transfer_data &transfer, int nread = sock_.recv(it); int err = sock_.last_error(); - if (nread < 0 && err == EINTR) continue; + if (nread < 0 && err == EINTR) { + continue; + } if ((nread < 0 && !socket::would_block(err)) || nread == 0) { - if (nread == 0) err = k_sock_closed_err; + if (nread == 0) { + err = k_sock_closed_err; + } err_ref = err; close_conn(err); return -1; @@ -144,7 +160,9 @@ ssize_t manual_connection::read(span rd_buf, transfer_data &transfer, int need_read = false; read_this_time += nread; buffered_data_.advance_head(nread); - if (stats_) stats_->update_read_stats(nread); + if (stats_) { + stats_->update_read_stats(nread); + } if (read_from_buf(rd_buf, transfer, true)) { on_async_op_done(r_data_, 0); return remain_to_transfer; @@ -158,20 +176,26 @@ ssize_t manual_connection::read(span rd_buf, transfer_data &transfer, int return read_this_time; } -void manual_connection::read_to_buf(int &err_ref) { +void manual_connection::read_to_buf(int& err_ref) { auto it = buffered_data_.head(); ssize_t nread = sock_.recv(it); int err = sock_.last_error(); - if (nread < 0 && err == EINTR) return; + if (nread < 0 && err == EINTR) { + return; + } if ((nread < 0 && !socket::would_block(err)) || nread == 0) { - if (nread == 0) err = k_sock_closed_err; + if (nread == 0) { + err = k_sock_closed_err; + } err_ref = err; close_conn(err); } else if (nread > 0) { buffered_data_.advance_head(nread); - if (stats_) stats_->update_read_stats(nread); + if (stats_) { + stats_->update_read_stats(nread); + } } else { err_ref = err; } @@ -200,8 +224,10 @@ void manual_connection::set_io_events(int events) noexcept { } } -void manual_connection::io_callback(ev::io &, int revents) { - if (ev::ERROR & revents) return; +void manual_connection::io_callback(ev::io&, int revents) { + if (ev::ERROR & revents) { + return; + } const auto conn_id = conn_id_; if (revents & ev::READ) { @@ -219,12 +245,14 @@ void manual_connection::io_callback(ev::io &, int revents) { if (sock_.valid()) { int nevents = (r_data_.buf.size() || buffered_data_.available()) ? ev::READ : 0; - if (hadWData || w_data_.buf.size()) nevents |= ev::WRITE; + if (hadWData || w_data_.buf.size()) { + nevents |= ev::WRITE; + } set_io_events(nevents); } } -void manual_connection::connect_timer_cb(ev::timer &, int) { close_conn(k_connect_timeout_err); } +void manual_connection::connect_timer_cb(ev::timer&, int) { close_conn(k_connect_timeout_err); } void manual_connection::write_cb() { if (state_ == conn_state::connecting && sock_.valid()) { @@ -248,7 +276,7 @@ int manual_connection::read_cb() { return err; } -bool manual_connection::read_from_buf(span rd_buf, transfer_data &transfer, bool read_full) noexcept { +bool manual_connection::read_from_buf(span rd_buf, transfer_data& transfer, bool read_full) noexcept { auto cur_buf = rd_buf.subspan(transfer.transfered_size()); const bool will_read_full = read_full && buffered_data_.size() >= cur_buf.size(); const bool will_read_any = !read_full && buffered_data_.size(); diff --git a/cpp_src/net/manualconnection.h b/cpp_src/net/manualconnection.h index aaaba8ce0..a4e8ec725 100644 --- a/cpp_src/net/manualconnection.h +++ b/cpp_src/net/manualconnection.h @@ -27,16 +27,16 @@ class manual_connection { void set_connect_timeout(std::chrono::milliseconds timeout) noexcept { connect_timeout_ = timeout; } void close_conn(int err); - void attach(ev::dynamic_loop &loop) noexcept; + void attach(ev::dynamic_loop& loop) noexcept; void detach() noexcept; - void restart(socket &&s); + void restart(socket&& s); template - void async_read(buf_t &data, size_t cnt, async_cb_t cb) { + void async_read(buf_t& data, size_t cnt, async_cb_t cb) { async_read_impl(data, cnt, std::move(cb)); } template - size_t async_read(buf_t &data, size_t cnt, int &err) noexcept { + size_t async_read(buf_t& data, size_t cnt, int& err) noexcept { auto co_id = coroutine::current(); auto l = [&err, co_id](int _err, size_t /*cnt*/, span /*buf*/) { err = _err; @@ -45,11 +45,11 @@ class manual_connection { return async_read_impl(data, cnt, std::move(l)); } template - void async_write(buf_t &data, async_cb_t cb, bool send_now = true) { + void async_write(buf_t& data, async_cb_t cb, bool send_now = true) { async_write_impl(data, std::move(cb), send_now); } template - size_t async_write(buf_t &data, int &err, bool send_now = true) noexcept { + size_t async_write(buf_t& data, int& err, bool send_now = true) noexcept { auto co_id = coroutine::current(); auto l = [&err, co_id](int _err, size_t /*cnt*/, span /*buf*/) { err = _err; @@ -95,10 +95,10 @@ class manual_connection { }; struct empty_switch_policy { - void operator()(async_data & /*data*/) {} + void operator()(async_data& /*data*/) {} }; struct suspend_switch_policy { - void operator()(async_data &data) { + void operator()(async_data& data) { while (!data.empty()) { coroutine::suspend(); } @@ -106,10 +106,10 @@ class manual_connection { }; template - size_t async_read_impl(buf_t &data, size_t cnt, cb_t cb) { + size_t async_read_impl(buf_t& data, size_t cnt, cb_t cb) { assertrx(r_data_.empty()); assertrx(data.size() >= cnt); - auto &transfer = r_data_.transfer; + auto& transfer = r_data_.transfer; transfer.set_expected(cnt); int int_err = 0; auto data_span = span(data.data(), cnt); @@ -132,9 +132,9 @@ class manual_connection { } template - size_t async_write_impl(buf_t &data, cb_t cb, bool send_now) { + size_t async_write_impl(buf_t& data, cb_t cb, bool send_now) { assertrx(w_data_.empty()); - auto &transfer = w_data_.transfer; + auto& transfer = w_data_.transfer; transfer.set_expected(data.size()); int int_err = 0; if (data.size()) { @@ -156,7 +156,7 @@ class manual_connection { return transfer.transfered_size(); } - void on_async_op_done(async_data &data, int err) { + void on_async_op_done(async_data& data, int err) { if (!data.empty()) { auto cb = std::move(data.cb); auto buf = data.buf; @@ -165,16 +165,16 @@ class manual_connection { cb(err, transfered, buf); } } - ssize_t write(span, transfer_data &transfer, int &err_ref); - ssize_t read(span, transfer_data &transfer, int &err_ref); - void read_to_buf(int &err_ref); + ssize_t write(span, transfer_data& transfer, int& err_ref); + ssize_t read(span, transfer_data& transfer, int& err_ref); + void read_to_buf(int& err_ref); void add_io_events(int events) noexcept; void set_io_events(int events) noexcept; - void io_callback(ev::io &watcher, int revents); - void connect_timer_cb(ev::timer &watcher, int); + void io_callback(ev::io& watcher, int revents); + void connect_timer_cb(ev::timer& watcher, int); void write_cb(); int read_cb(); - bool read_from_buf(span rd_buf, transfer_data &transfer, bool read_full) noexcept; + bool read_from_buf(span rd_buf, transfer_data& transfer, bool read_full) noexcept; ev::io io_; socket sock_; diff --git a/cpp_src/net/socket.cc b/cpp_src/net/socket.cc index a943b0428..60b47d173 100644 --- a/cpp_src/net/socket.cc +++ b/cpp_src/net/socket.cc @@ -21,7 +21,7 @@ int socket::connect(std::string_view addr, socket_domain t) { int ret = 0; type_ = t; if (domain() == socket_domain::tcp) { - struct addrinfo *results = nullptr; + struct addrinfo* results = nullptr; ret = create(addr, &results); if rx_likely (!ret) { assertrx(results != nullptr); @@ -49,7 +49,7 @@ int socket::connect(std::string_view addr, socket_domain t) { memcpy(address.sun_path, addr.data(), addr.size()); address.sun_path[addr.size()] = 0; - if rx_unlikely (::connect(fd_, reinterpret_cast(&address), sizeof(address)) != 0) { // -V595 + if rx_unlikely (::connect(fd_, reinterpret_cast(&address), sizeof(address)) != 0) { // -V595 if rx_unlikely (!would_block(last_error())) { perror("connect error"); close(); @@ -77,7 +77,7 @@ ssize_t socket::send(span chunks) { iov.resize(chunks.size()); for (unsigned i = 0; i < chunks.size(); i++) { - iov[i].buf = reinterpret_cast(chunks[i].data()); + iov[i].buf = reinterpret_cast(chunks[i].data()); iov[i].len = chunks[i].size(); } DWORD numberOfBytesSent; @@ -103,12 +103,12 @@ int socket::setLinger0() { struct linger sl; sl.l_onoff = 1; /* enable linger */ sl.l_linger = 0; /* with 0 seconds timeout */ - return setsockopt(fd(), SOL_SOCKET, SO_LINGER, reinterpret_cast(&sl), sizeof(sl)); + return setsockopt(fd(), SOL_SOCKET, SO_LINGER, reinterpret_cast(&sl), sizeof(sl)); } return -1; } -int socket::create(std::string_view addr, struct addrinfo **presults) { +int socket::create(std::string_view addr, struct addrinfo** presults) { assertrx(!valid()); if (domain() == socket_domain::tcp) { @@ -121,15 +121,17 @@ int socket::create(std::string_view addr, struct addrinfo **presults) { *presults = nullptr; std::string saddr(addr); - char *paddr = &saddr[0]; + char* paddr = &saddr[0]; - char *pport = strchr(paddr, ':'); + char* pport = strchr(paddr, ':'); if (pport == nullptr) { pport = paddr; paddr = nullptr; } else { *pport = 0; - if (*paddr == 0) paddr = nullptr; + if (*paddr == 0) { + paddr = nullptr; + } pport++; } @@ -147,7 +149,7 @@ int socket::create(std::string_view addr, struct addrinfo **presults) { } int enable = 1; - if rx_unlikely (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&enable), sizeof(enable)) < 0) { + if rx_unlikely (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&enable), sizeof(enable)) < 0) { perror("setsockopt(SO_REUSEADDR) failed"); } } else { @@ -178,11 +180,11 @@ int socket::create(std::string_view addr, struct addrinfo **presults) { std::string socket::addr() const { if (domain() == socket_domain::tcp) { struct sockaddr_storage saddr; - struct sockaddr *paddr = reinterpret_cast(&saddr); + struct sockaddr* paddr = reinterpret_cast(&saddr); socklen_t len = sizeof(saddr); if rx_likely (::getpeername(fd_, paddr, &len) == 0) { char buf[INET_ADDRSTRLEN] = {}; - auto port = ntohs(reinterpret_cast(paddr)->sin_port); + auto port = ntohs(reinterpret_cast(paddr)->sin_port); if rx_likely (getnameinfo(paddr, len, buf, INET_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST) == 0) { return std::string(buf) + ':' + std::to_string(port); } else { @@ -209,7 +211,7 @@ int socket::set_nonblock() { int socket::set_nodelay() noexcept { if (domain() == socket_domain::tcp) { int flag = 1; - return setsockopt(fd_, SOL_TCP, TCP_NODELAY, reinterpret_cast(&flag), sizeof(flag)); + return setsockopt(fd_, SOL_TCP, TCP_NODELAY, reinterpret_cast(&flag), sizeof(flag)); } else { return 0; } @@ -269,7 +271,7 @@ int lst_socket::bind(std::string_view addr, socket_domain t) { int ret = 0; sock_.domain(t); if (domain() == socket_domain::tcp) { - struct addrinfo *results = nullptr; + struct addrinfo* results = nullptr; ret = sock_.create(addr, &results); if rx_unlikely (!ret) { assertrx(results != nullptr); @@ -321,7 +323,7 @@ int lst_socket::bind(std::string_view addr, socket_domain t) { } unlink(unPath_.c_str()); - if rx_unlikely (::bind(sock_.fd(), reinterpret_cast(&address), sizeof(address)) < 0) { + if rx_unlikely (::bind(sock_.fd(), reinterpret_cast(&address), sizeof(address)) < 0) { perror("bind() error"); close(); return -1; diff --git a/cpp_src/net/socket.h b/cpp_src/net/socket.h index bba398989..92b5b64c5 100644 --- a/cpp_src/net/socket.h +++ b/cpp_src/net/socket.h @@ -17,10 +17,10 @@ class lst_socket; class socket { public: socket() = default; - socket(const socket &&other) = delete; - socket(socket &&other) noexcept : fd_(other.fd_), type_(other.type_) { other.fd_ = -1; } - socket &operator=(const socket &other) = delete; - socket &operator=(socket &&other) noexcept { + socket(const socket&& other) = delete; + socket(socket&& other) noexcept : fd_(other.fd_), type_(other.type_) { other.fd_ = -1; } + socket& operator=(const socket& other) = delete; + socket& operator=(socket&& other) noexcept { if rx_likely (this != &other) { if (valid()) { close(); @@ -59,7 +59,7 @@ class socket { friend class lst_socket; socket(int fd, socket_domain type) noexcept : fd_(fd), type_(type) {} - int create(std::string_view addr, struct addrinfo **pres); + int create(std::string_view addr, struct addrinfo** pres); void domain(socket_domain t) noexcept { type_ = t; } int fd_ = -1; @@ -69,13 +69,13 @@ class socket { class lst_socket { public: lst_socket() = default; - lst_socket(const lst_socket &&other) = delete; - lst_socket(lst_socket &&other) noexcept + lst_socket(const lst_socket&& other) = delete; + lst_socket(lst_socket&& other) noexcept : sock_(std::move(other.sock_)), lockFd_(other.lockFd_), unPath_(std::move(other.unPath_)), unLock_(std::move(other.unLock_)) { other.lockFd_ = -1; } - lst_socket &operator=(const lst_socket &other) = delete; - lst_socket &operator=(lst_socket &&other) noexcept { + lst_socket& operator=(const lst_socket& other) = delete; + lst_socket& operator=(lst_socket&& other) noexcept { if rx_likely (this != &other) { if (valid()) { close(); diff --git a/cpp_src/readme.md b/cpp_src/readme.md index cd760167e..acb9d4a5a 100644 --- a/cpp_src/readme.md +++ b/cpp_src/readme.md @@ -35,6 +35,16 @@ While using docker, you may pass reindexer server config options via envinronmen - `RX_DISABLE_NS_LEAK` - Disables namespaces memory leak on database destruction (will slow down server's termination). - `RX_MAX_HTTP_REQ` - allows to configure max HTTP request size (in bytes). Default value is `2097152` (= 2 MB). `0` means 'unlimited'. +### Image build + +Usually this is not required, but you may also build reindexer's docker image on your own, using this [Dockerfile](#cmd/reindexer_server/contrib/Dockerfile): + +```bash +docker build -t my-reindexer-image -f cpp_src/cmd/reindexer_server/contrib/Dockerfile . +``` + +Build command above must be executed from root project's directory. + ## Linux ### RHEL/Centos/Fedora @@ -298,6 +308,11 @@ If reindexer library was built with rocksdb, it requires Go build tag `rocksdb` Reindexer supports the following data formats to communicate with other applications (mainly via HTTP REST API): JSON, MSGPACK and Protobuf. +## Log rotation + +There are no builtin mechanis for automatic log rotation, however `reindexer server` is able to reopen logfiles on `SIGHUP`. +So, your external log manager (it may be even a simple `cron` script) have to move existing log files somewhere and then send `SIGHUP`-signal to the `reindexer server` process to recreate log files. + #### Protobuf Protocol buffers are language-neutral, platform-neutral, extensible mechanism for serializing structured data. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages (https://developers.google.com/protocol-buffers). diff --git a/cpp_src/replicator/replicator.cc b/cpp_src/replicator/replicator.cc index 7a3e79c53..27ca02228 100644 --- a/cpp_src/replicator/replicator.cc +++ b/cpp_src/replicator/replicator.cc @@ -10,7 +10,7 @@ namespace reindexer { using namespace net; using namespace std::string_view_literals; -Replicator::Replicator(ReindexerImpl *slave) +Replicator::Replicator(ReindexerImpl* slave) : slave_(slave), resyncUpdatesLostFlag_(false), terminate_(false), @@ -33,14 +33,18 @@ Error Replicator::Start() { return Error(errLogic, "Replicator is already started"); } - if (!(config_.role == ReplicationSlave)) return errOK; + if (!(config_.role == ReplicationSlave)) { + return errOK; + } master_.reset(new client::Reindexer( client::ReindexerConfig(config_.connPoolSize, config_.workerThreads, 10000, 0, std::chrono::seconds(config_.timeoutSec), std::chrono::seconds(config_.timeoutSec), config_.enableCompression, false, config_.appName))); auto err = master_->Connect(config_.masterDSN, client::ConnectOpts().WithExpectedClusterID(config_.clusterID)); - if (err.ok()) err = master_->Status(); + if (err.ok()) { + err = master_->Status(); + } if (err.code() == errOK || err.code() == errNetwork) { err = errOK; terminate_ = false; @@ -58,7 +62,7 @@ Error Replicator::Start() { return err; } -bool Replicator::Configure(const ReplicationConfigData &config) { +bool Replicator::Configure(const ReplicationConfigData& config) { if (!enabled_.load(std::memory_order_acquire)) { return false; } @@ -66,7 +70,9 @@ bool Replicator::Configure(const ReplicationConfigData &config) { bool changed = (config_ != config); if (changed) { - if (master_) stop(); + if (master_) { + stop(); + } config_ = config; } @@ -94,7 +100,7 @@ void Replicator::stop() { } void Replicator::run() { - stop_.set([&](ev::async &sig) { sig.loop.break_loop(); }); + stop_.set([&](ev::async& sig) { sig.loop.break_loop(); }); stop_.start(); logPrintf(LogInfo, "[repl] Replicator with %s started", config_.masterDSN); @@ -117,16 +123,16 @@ void Replicator::run() { state_.store(StateInit, std::memory_order_release); } - resync_.set([this](ev::async &) { + resync_.set([this](ev::async&) { auto err = syncDatabase("Resync"); (void)err; // ignore }); resync_.start(); - resyncTimer_.set([this](ev::timer &, int) { + resyncTimer_.set([this](ev::timer&, int) { auto err = syncDatabase("Reconnect"); (void)err; // ignore }); - walSyncAsync_.set([this](ev::async &) { + walSyncAsync_.set([this](ev::async&) { // check status of namespace NamespaceDef nsDef; bool forced; @@ -145,7 +151,7 @@ void Replicator::run() { } }); walSyncAsync_.start(); - resyncUpdatesLostAsync_.set([this](ev::async &) { + resyncUpdatesLostAsync_.set([this](ev::async&) { auto err = syncDatabase("UpdateLost"); (void)err; // ignore }); @@ -169,7 +175,7 @@ void Replicator::run() { logPrintf(LogInfo, "[repl] Replicator with %s stopped", config_.masterDSN); } -static bool errorIsFatal(const Error &err) { +static bool errorIsFatal(const Error& err) { switch (err.code()) { case errOK: case errNetwork: @@ -216,8 +222,10 @@ static bool errorIsFatal(const Error &err) { } } -bool Replicator::retryIfNetworkError(const Error &err) { - if (err.ok()) return false; +bool Replicator::retryIfNetworkError(const Error& err) { + if (err.ok()) { + return false; + } if (!errorIsFatal(err)) { state_.store(StateInit, std::memory_order_release); resyncTimer_.start(config_.retrySyncIntervalSec); @@ -227,7 +235,7 @@ bool Replicator::retryIfNetworkError(const Error &err) { return false; } -void Replicator::subscribeUpdatesIfRequired(const std::string &nsName) { +void Replicator::subscribeUpdatesIfRequired(const std::string& nsName) { if (config_.namespaces.find(nsName) != config_.namespaces.end()) { UpdatesFilters filters; filters.AddFilter(nsName, UpdatesFilters::Filter()); @@ -238,15 +246,15 @@ void Replicator::subscribeUpdatesIfRequired(const std::string &nsName) { } } -static bool shouldUpdateLsn(const WALRecord &wrec) { +static bool shouldUpdateLsn(const WALRecord& wrec) { return wrec.type != WalNamespaceDrop && (!wrec.inTransaction || wrec.type == WalCommitTransaction); } -Replicator::SyncNsResult Replicator::syncNamespace(const NamespaceDef &ns, std::string_view forceSyncReason, SyncQueue *sourceQueue, +Replicator::SyncNsResult Replicator::syncNamespace(const NamespaceDef& ns, std::string_view forceSyncReason, SyncQueue* sourceQueue, std::string_view initiator) { Error err = errOK; logPrintf(LogInfo, "[repl:%s:%s] ===Starting WAL synchronization. (initiated by %s)====", ns.name, slave_->storagePath_, initiator); - auto onPendedUpdatesApplied = [this, sourceQueue](std::string_view nsName, const std::unique_lock &lck) noexcept { + auto onPendedUpdatesApplied = [this, sourceQueue](std::string_view nsName, const std::unique_lock& lck) noexcept { pendedUpdates_.erase(nsName); if (sourceQueue) { sourceQueue->Pop(nsName, lck); @@ -283,9 +291,9 @@ Replicator::SyncNsResult Replicator::syncNamespace(const NamespaceDef &ns, std:: logPrintf(LogTrace, "[repl:%s:%s]: %d new updates", ns.name, slave_->storagePath_, walUpdates.size()); if (walUpdates.empty()) { onPendedUpdatesApplied(ns.name, lck); - if (replState.replicatorEnabled) + if (replState.replicatorEnabled) { slaveNs->SetSlaveReplStatus(ReplicationState::Status::Idle, errOK, dummyCtx_); - else { + } else { logPrintf( LogError, "[repl:%s:%s]:%d Sync namespace logical error. Set status Idle. Replication not allowed for namespace.", @@ -304,7 +312,7 @@ Replicator::SyncNsResult Replicator::syncNamespace(const NamespaceDef &ns, std:: } } try { - for (auto &rec : walUpdates) { + for (auto& rec : walUpdates) { bool forceApply = lastLsn.upstreamLSN_.isEmpty() ? replState.lastUpstreamLSN.isEmpty() || rec.first.upstreamLSN_.Counter() > replState.lastUpstreamLSN.Counter() @@ -328,7 +336,7 @@ Replicator::SyncNsResult Replicator::syncNamespace(const NamespaceDef &ns, std:: config_.serverId, rec.first.upstreamLSN_.Counter(), lastLsn.upstreamLSN_.Counter()); } } - } catch (const Error &e) { + } catch (const Error& e) { err = e; } walUpdates.clear(); @@ -401,14 +409,20 @@ Error Replicator::syncDatabase(std::string_view initiator) { resyncTimer_.stop(); // Loop for all master namespaces - for (auto &ns : nses) { + for (auto& ns : nses) { logPrintf(LogTrace, "[repl:%s:%s]:%d Loop for all master namespaces state=%d", ns.name, slave_->storagePath_, config_.serverId, state_.load()); // skip system & non enabled namespaces - if (!isSyncEnabled(ns.name)) continue; + if (!isSyncEnabled(ns.name)) { + continue; + } // skip temporary namespaces (namespace from upstream slave node) - if (ns.isTemporary) continue; - if (terminate_) break; + if (ns.isTemporary) { + continue; + } + if (terminate_) { + break; + } subscribeUpdatesIfRequired(ns.name); @@ -422,9 +436,9 @@ Error Replicator::syncDatabase(std::string_view initiator) { auto slaveNs = slave_->getNamespaceNoThrow(ns.name, dummyCtx_); if (err.ok() && slaveNs) { replState = slaveNs->GetReplState(dummyCtx_); - if (replState.replicatorEnabled) + if (replState.replicatorEnabled) { slaveNs->SetSlaveReplStatus(ReplicationState::Status::Syncing, errOK, dummyCtx_); - else { + } else { logPrintf(LogError, "[repl:%s:%s]:%d Sync namespace logical error. Set status Syncing. Replication not allowed for namespace.", ns.name, slave_->storagePath_, config_.serverId); @@ -454,16 +468,19 @@ Error Replicator::syncDatabase(std::string_view initiator) { slaveNs = slave_->getNamespaceNoThrow(ns.name, dummyCtx_); if (slaveNs) { replState = slaveNs->GetReplState(dummyCtx_); - if (replState.replicatorEnabled) + if (replState.replicatorEnabled) { slaveNs->SetSlaveReplStatus(errorIsFatal(err) ? ReplicationState::Status::Fatal : ReplicationState::Status::Error, err, dummyCtx_); - else + } else { logPrintf( LogError, "[repl:%s:%s]:%d Sync namespace logical error. Set status Fatal. Replication not allowed for namespace. Err= %s", ns.name, slave_->storagePath_, config_.serverId, err.what()); + } + } + if (retryIfNetworkError(err)) { + return err; } - if (retryIfNetworkError(err)) return err; } } state_.store(StateIdle, std::memory_order_release); @@ -471,9 +488,11 @@ Error Replicator::syncDatabase(std::string_view initiator) { return err; } -Error Replicator::syncNamespaceByWAL(const NamespaceDef &nsDef) { +Error Replicator::syncNamespaceByWAL(const NamespaceDef& nsDef) { auto slaveNs = slave_->getNamespaceNoThrow(nsDef.name, dummyCtx_); - if (!slaveNs) return Error(errNotFound, "Namespace %s not found", nsDef.name); + if (!slaveNs) { + return Error(errNotFound, "Namespace %s not found", nsDef.name); + } lsn_t lsn = slaveNs->GetReplState(dummyCtx_).lastUpstreamLSN; @@ -492,7 +511,9 @@ Error Replicator::syncNamespaceByWAL(const NamespaceDef &nsDef) { return syncNamespaceForced(nsDef, err.what()); case errOK: err = applyWAL(slaveNs, qr); - if (err.ok()) err = slave_->syncDownstream(nsDef.name, false); + if (err.ok()) { + err = slave_->syncDownstream(nsDef.name, false); + } return err; case errNoWAL: terminate_ = true; @@ -541,7 +562,7 @@ Error Replicator::syncNamespaceByWAL(const NamespaceDef &nsDef) { // Forced namespace sync // This will completely drop slave namespace // read all indexes and data from master, then apply to slave -Error Replicator::syncNamespaceForced(const NamespaceDef &ns, std::string_view reason) { +Error Replicator::syncNamespaceForced(const NamespaceDef& ns, std::string_view reason) { logPrintf(LogWarning, "[repl:%s:%s] Start FORCED sync: %s", ns.name, slave_->storagePath_, reason); // Create temporary namespace @@ -558,7 +579,9 @@ Error Replicator::syncNamespaceForced(const NamespaceDef &ns, std::string_view r auto tmpNs = slave_->getNamespaceNoThrow(tmpNsDef.name, dummyCtx_); if (tmpNs) { auto dropErr = slave_->closeNamespace(tmpNsDef.name, dummyCtx_, true, true); - if (!dropErr.ok()) logPrintf(LogWarning, "Unable to drop temporary namespace %s: %s", tmpNsDef.name, dropErr.what()); + if (!dropErr.ok()) { + logPrintf(LogWarning, "Unable to drop temporary namespace %s: %s", tmpNsDef.name, dropErr.what()); + } } }; @@ -574,9 +597,9 @@ Error Replicator::syncNamespaceForced(const NamespaceDef &ns, std::string_view r return Error(errNotFound, "Namespace %s not found", tmpNsDef.name); } auto replState = tmpNs->GetReplState(dummyCtx_); - if (replState.replicatorEnabled) + if (replState.replicatorEnabled) { tmpNs->SetSlaveReplStatus(ReplicationState::Status::Syncing, errOK, dummyCtx_); - else { + } else { logPrintf(LogError, "[repl:%s:%s] Sync namespace logical error. Set status Syncing tmpNs. Replication not allowed for namespace.", ns.name, slave_->storagePath_); dropTmpNs(); @@ -584,11 +607,15 @@ Error Replicator::syncNamespaceForced(const NamespaceDef &ns, std::string_view r } err = syncMetaForced(tmpNs, ns.name); - if (err.ok()) err = syncSchemaForced(tmpNs, ns.name); + if (err.ok()) { + err = syncSchemaForced(tmpNs, ns.name); + } // Make query to complete master's namespace data client::QueryResults qr(kResultsWithPayloadTypes | kResultsCJson | kResultsWithItemID | kResultsWithRaw); - if (err.ok()) err = master_->Select(Query(ns.name).Where("#lsn", CondAny, VariantArray{}), qr); + if (err.ok()) { + err = master_->Select(Query(ns.name).Where("#lsn", CondAny, VariantArray{}), qr); + } if (err.ok()) { ForceSyncContext fsyncCtx{.nsDef = ns, .replaceTagsMatcher = [&] { if (auto err = tmpNs->ReplaceTagsMatcher(qr.getTagsMatcher(0), dummyCtx_); !err.ok()) { @@ -601,8 +628,12 @@ Error Replicator::syncNamespaceForced(const NamespaceDef &ns, std::string_view r err = errOK; } } - if (err.ok()) err = slave_->renameNamespace(tmpNsDef.name, ns.name, true); - if (err.ok()) err = slave_->syncDownstream(ns.name, true); + if (err.ok()) { + err = slave_->renameNamespace(tmpNsDef.name, ns.name, true); + } + if (err.ok()) { + err = slave_->syncDownstream(ns.name, true); + } if (!err.ok()) { logPrintf(LogError, "[repl:%s] FORCED sync error: %s", ns.name, err.what()); @@ -612,7 +643,7 @@ Error Replicator::syncNamespaceForced(const NamespaceDef &ns, std::string_view r return err; } -Error Replicator::applyWAL(Namespace::Ptr &slaveNs, client::QueryResults &qr, const ForceSyncContext *fsyncCtx) { +Error Replicator::applyWAL(Namespace::Ptr& slaveNs, client::QueryResults& qr, const ForceSyncContext* fsyncCtx) { Error err; SyncStat stat; WrSerializer ser; @@ -634,7 +665,9 @@ Error Replicator::applyWAL(Namespace::Ptr &slaveNs, client::QueryResults &qr, co } if (err.ok()) { for (auto it : qr) { - if (terminate_) break; + if (terminate_) { + break; + } if (qr.Status().ok()) { try { if (it.IsRaw()) { @@ -662,7 +695,7 @@ Error Replicator::applyWAL(Namespace::Ptr &slaveNs, client::QueryResults &qr, co } } } - } catch (const Error &e) { + } catch (const Error& e) { err = e; } if (!err.ok()) { @@ -709,23 +742,29 @@ Error Replicator::applyWAL(Namespace::Ptr &slaveNs, client::QueryResults &qr, co logPrintf(!stat.lastError.ok() ? LogError : LogInfo, "[repl:%s:%s]:%d Sync %s: %s", nsName, slave_->storagePath_, config_.serverId, terminate_ ? "terminated" : "done", ser.Slice()); - if (terminate_) return Error(errCanceled, "terminated"); + if (terminate_) { + return Error(errCanceled, "terminated"); + } return stat.lastError; } -Error Replicator::applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr &slaveNs, const WALRecord &rec, SyncStat &stat) { +Error Replicator::applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr& slaveNs, const WALRecord& rec, SyncStat& stat) { switch (rec.type) { // Modify item case WalItemModify: { std::lock_guard lck(syncMtx_); - Transaction &tx = transactions_[slaveNs.get()]; - if (tx.IsFree()) return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + Transaction& tx = transactions_[slaveNs.get()]; + if (tx.IsFree()) { + return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + } Item item = tx.NewItem(); Error err = unpackItem(item, LSNs.upstreamLSN_, rec.itemModify.itemCJson, master_->NewItem(nsName).impl_->tagsMatcher()); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } try { tx.Modify(std::move(item), static_cast(rec.itemModify.modifyMode)); - } catch (Error &e) { + } catch (Error& e) { return e; } } break; @@ -734,18 +773,22 @@ Error Replicator::applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namesp QueryResults result; Query q = Query::FromSQL(rec.data); std::lock_guard lck(syncMtx_); - Transaction &tx = transactions_[slaveNs.get()]; - if (tx.IsFree()) return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + Transaction& tx = transactions_[slaveNs.get()]; + if (tx.IsFree()) { + return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + } try { tx.Modify(std::move(q)); - } catch (Error &e) { + } catch (Error& e) { return e; } } break; case WalInitTransaction: { std::lock_guard lck(syncMtx_); - Transaction &tx = transactions_[slaveNs.get()]; - if (!tx.IsFree()) logPrintf(LogError, "[repl:%s]:%d Init transaction befor commit of previous one.", nsName, config_.serverId); + Transaction& tx = transactions_[slaveNs.get()]; + if (!tx.IsFree()) { + logPrintf(LogError, "[repl:%s]:%d Init transaction befor commit of previous one.", nsName, config_.serverId); + } RdxContext rdxContext(true, LSNs); tx = slaveNs->NewTransaction(rdxContext); if (!tx.Status().ok()) { @@ -756,8 +799,10 @@ Error Replicator::applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namesp case WalCommitTransaction: { QueryResults res; std::lock_guard lck(syncMtx_); - Transaction &tx = transactions_[slaveNs.get()]; - if (tx.IsFree()) return Error(errLogic, "[repl:%s]:%d Commit of transaction befor initiate it.", nsName, config_.serverId); + Transaction& tx = transactions_[slaveNs.get()]; + if (tx.IsFree()) { + return Error(errLogic, "[repl:%s]:%d Commit of transaction befor initiate it.", nsName, config_.serverId); + } RdxContext rdxContext(true, LSNs); slaveNs->CommitTransaction(tx, res, rdxContext); stat.txEnds++; @@ -773,22 +818,26 @@ Error Replicator::applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namesp config_.serverId, stateToken, version); std::lock_guard lck(syncMtx_); - Transaction &tx = transactions_[slaveNs.get()]; - if (tx.IsFree()) return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + Transaction& tx = transactions_[slaveNs.get()]; + if (tx.IsFree()) { + return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + } try { tx.MergeTagsMatcher(std::move(tm)); - } catch (Error &e) { + } catch (Error& e) { return e; } } break; case WalPutMeta: { std::lock_guard lck(syncMtx_); - Transaction &tx = transactions_[slaveNs.get()]; - if (tx.IsFree()) return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + Transaction& tx = transactions_[slaveNs.get()]; + if (tx.IsFree()) { + return Error(errLogic, "[repl:%s]:%d Transaction was not initiated.", nsName, config_.serverId); + } try { tx.PutMeta(std::string(rec.itemMeta.key), rec.itemMeta.value); - } catch (Error &e) { + } catch (Error& e) { return e; } } break; @@ -813,7 +862,7 @@ Error Replicator::applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namesp return {}; } -void Replicator::checkNoOpenedTransaction(std::string_view nsName, Namespace::Ptr &slaveNs) { +void Replicator::checkNoOpenedTransaction(std::string_view nsName, Namespace::Ptr& slaveNs) { std::lock_guard lck(syncMtx_); auto txIt = transactions_.find(slaveNs.get()); if (txIt != transactions_.end() && !txIt->second.IsFree()) { @@ -822,7 +871,7 @@ void Replicator::checkNoOpenedTransaction(std::string_view nsName, Namespace::Pt } } -Error Replicator::applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr &slaveNs, const WALRecord &rec, SyncStat &stat) { +Error Replicator::applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr& slaveNs, const WALRecord& rec, SyncStat& stat) { Error err; IndexDef iDef; @@ -836,10 +885,12 @@ Error Replicator::applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespac checkNoOpenedTransaction(nsName, slaveNs); - auto sendSyncAsync = [this](const WALRecord &rec, bool forced) { + auto sendSyncAsync = [this](const WALRecord& rec, bool forced) { NamespaceDef nsDef; Error err = nsDef.FromJSON(giftStr(rec.data)); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } syncQueue_.Push(nsDef.name, std::move(nsDef), forced); walSyncAsync_.send(); return err; @@ -854,19 +905,25 @@ Error Replicator::applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespac // Index added case WalIndexAdd: err = iDef.FromJSON(giftStr(rec.data)); - if (err.ok()) slaveNs->AddIndex(iDef, rdxContext); + if (err.ok()) { + slaveNs->AddIndex(iDef, rdxContext); + } stat.updatedIndexes++; break; // Index dropped case WalIndexDrop: err = iDef.FromJSON(giftStr(rec.data)); - if (err.ok()) slaveNs->DropIndex(iDef, rdxContext); + if (err.ok()) { + slaveNs->DropIndex(iDef, rdxContext); + } stat.deletedIndexes++; break; // Index updated case WalIndexUpdate: err = iDef.FromJSON(giftStr(rec.data)); - if (err.ok()) slaveNs->UpdateIndex(iDef, rdxContext); + if (err.ok()) { + slaveNs->UpdateIndex(iDef, rdxContext); + } stat.updatedIndexes++; break; // Metadata updated @@ -961,7 +1018,7 @@ Error Replicator::applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespac return err; } -Error Replicator::unpackItem(Item &item, lsn_t lsn, std::string_view cjson, const TagsMatcher &tm) { +Error Replicator::unpackItem(Item& item, lsn_t lsn, std::string_view cjson, const TagsMatcher& tm) { if (item.impl_->tagsMatcher().size() < tm.size()) { const bool res = item.impl_->tagsMatcher().try_merge(tm); if (!res) { @@ -972,7 +1029,7 @@ Error Replicator::unpackItem(Item &item, lsn_t lsn, std::string_view cjson, cons return item.FromCJSON(cjson); } -void Replicator::pushPendingUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec) { +void Replicator::pushPendingUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec) { PackedWALRecord pwrec; pwrec.Pack(wrec); auto updatesIt = pendedUpdates_.find(nsName); @@ -981,13 +1038,13 @@ void Replicator::pushPendingUpdate(LSNPair LSNs, std::string_view nsName, const updates.container.emplace_back(std::make_pair(LSNs, std::move(pwrec))); pendedUpdates_.emplace(std::string(nsName), std::move(updates)); } else { - auto &updates = updatesIt.value().container; + auto& updates = updatesIt.value().container; updates.emplace_back(std::make_pair(LSNs, std::move(pwrec))); } } -Error Replicator::modifyItem(LSNPair LSNs, Namespace::Ptr &slaveNs, std::string_view cjson, int modifyMode, const TagsMatcher &tm, - SyncStat &stat) { +Error Replicator::modifyItem(LSNPair LSNs, Namespace::Ptr& slaveNs, std::string_view cjson, int modifyMode, const TagsMatcher& tm, + SyncStat& stat) { Item item = slaveNs->NewItem(dummyCtx_); Error err = unpackItem(item, LSNs.upstreamLSN_, cjson, tm); @@ -1017,8 +1074,8 @@ Error Replicator::modifyItem(LSNPair LSNs, Namespace::Ptr &slaveNs, std::string_ return err; } -Error Replicator::modifyItemTx(LSNPair LSNs, Transaction &tx, std::string_view cjson, int modifyMode, const TagsMatcher &tm, - SyncStat &stat) { +Error Replicator::modifyItemTx(LSNPair LSNs, Transaction& tx, std::string_view cjson, int modifyMode, const TagsMatcher& tm, + SyncStat& stat) { Item item = tx.NewItem(); Error err = unpackItem(item, LSNs.upstreamLSN_, cjson, tm); @@ -1047,29 +1104,49 @@ Error Replicator::modifyItemTx(LSNPair LSNs, Transaction &tx, std::string_view c return err; } -WrSerializer &Replicator::SyncStat::Dump(WrSerializer &ser) { - if (updated) ser << updated << " items updated; "; - if (deleted) ser << deleted << " items deleted; "; - if (updatedIndexes) ser << updatedIndexes << " indexes updated; "; - if (deletedIndexes) ser << deletedIndexes << " indexes deleted; "; - if (updatedMeta) ser << updatedMeta << " meta updated; "; - if (schemasSet) ser << "New schema was set; "; - if (txStarts || txEnds) ser << " started " << txStarts << ", completed " << txEnds << " transactions; "; - if (errors || !lastError.ok()) ser << errors << " errors (" << lastError.what() << ") "; - if (!ser.Len()) ser << "Up to date; "; - if (processed) ser << "processed " << processed << " WAL records "; +WrSerializer& Replicator::SyncStat::Dump(WrSerializer& ser) { + if (updated) { + ser << updated << " items updated; "; + } + if (deleted) { + ser << deleted << " items deleted; "; + } + if (updatedIndexes) { + ser << updatedIndexes << " indexes updated; "; + } + if (deletedIndexes) { + ser << deletedIndexes << " indexes deleted; "; + } + if (updatedMeta) { + ser << updatedMeta << " meta updated; "; + } + if (schemasSet) { + ser << "New schema was set; "; + } + if (txStarts || txEnds) { + ser << " started " << txStarts << ", completed " << txEnds << " transactions; "; + } + if (errors || !lastError.ok()) { + ser << errors << " errors (" << lastError.what() << ") "; + } + if (!ser.Len()) { + ser << "Up to date; "; + } + if (processed) { + ser << "processed " << processed << " WAL records "; + } return ser; } -Error Replicator::syncIndexesForced(Namespace::Ptr &slaveNs, const ForceSyncContext &fsyncCtx) { - const std::string &nsName = fsyncCtx.nsDef.name; +Error Replicator::syncIndexesForced(Namespace::Ptr& slaveNs, const ForceSyncContext& fsyncCtx) { + const std::string& nsName = fsyncCtx.nsDef.name; Error err; - for (auto &idx : fsyncCtx.nsDef.indexes) { + for (auto& idx : fsyncCtx.nsDef.indexes) { logPrintf(LogTrace, "[repl:%s] Updating index '%s'", nsName, idx.name_); try { slaveNs->AddIndex(idx, dummyCtx_); - } catch (const Error &e) { + } catch (const Error& e) { logPrintf(LogError, "[repl:%s] Error add index '%s': %s", nsName, idx.name_, err.what()); err = e; } @@ -1077,7 +1154,7 @@ Error Replicator::syncIndexesForced(Namespace::Ptr &slaveNs, const ForceSyncCont try { assertrx(fsyncCtx.replaceTagsMatcher); fsyncCtx.replaceTagsMatcher(); - } catch (const Error &e) { + } catch (const Error& e) { logPrintf(LogError, "[repl:%s] Error on TagsMatcher replace call: %s", nsName, err.what()); err = e; } @@ -1085,14 +1162,14 @@ Error Replicator::syncIndexesForced(Namespace::Ptr &slaveNs, const ForceSyncCont return err; } -Error Replicator::syncSchemaForced(Namespace::Ptr &slaveNs, const NamespaceDef &masterNsDef) { - const std::string &nsName = masterNsDef.name; +Error Replicator::syncSchemaForced(Namespace::Ptr& slaveNs, const NamespaceDef& masterNsDef) { + const std::string& nsName = masterNsDef.name; Error err = errOK; logPrintf(LogTrace, "[repl:%s] Setting schema", nsName); try { slaveNs->SetSchema(masterNsDef.schemaJson, dummyCtx_); - } catch (const Error &e) { + } catch (const Error& e) { logPrintf(LogError, "[repl:%s] Error in set schema: %s", nsName, err.what()); err = e; } @@ -1100,11 +1177,11 @@ Error Replicator::syncSchemaForced(Namespace::Ptr &slaveNs, const NamespaceDef & return err; } -Error Replicator::syncMetaForced(Namespace::Ptr &slaveNs, std::string_view nsName) { +Error Replicator::syncMetaForced(Namespace::Ptr& slaveNs, std::string_view nsName) { std::vector keys; auto err = master_->EnumMeta(nsName, keys); - for (auto &key : keys) { + for (auto& key : keys) { std::string data; err = master_->GetMeta(nsName, key, data); if (!err.ok()) { @@ -1113,7 +1190,7 @@ Error Replicator::syncMetaForced(Namespace::Ptr &slaveNs, std::string_view nsNam } try { slaveNs->PutMeta(key, data, dummyCtx_); - } catch (const Error &e) { + } catch (const Error& e) { logPrintf(LogError, "[repl:%s]:%d Error set meta '%s': %s", slaveNs->GetName(dummyCtx_), config_.serverId, key, e.what()); } } @@ -1121,14 +1198,14 @@ Error Replicator::syncMetaForced(Namespace::Ptr &slaveNs, std::string_view nsNam } // Callback from WAL updates pusher -void Replicator::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec) { +void Replicator::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec) { try { onWALUpdateImpl(LSNs, nsName, wrec); - } catch (Error &e) { + } catch (Error& e) { logPrintf(LogError, "[repl:%s]:%d Exception on WAL update: %s", nsName, config_.serverId, e.what()); assertrx_dbg(false); resync_.send(); - } catch (std::exception &e) { + } catch (std::exception& e) { logPrintf(LogError, "[repl:%s]:%d Exception on WAL update (std::exception): %s", nsName, config_.serverId, e.what()); assertrx_dbg(false); resync_.send(); @@ -1139,7 +1216,7 @@ void Replicator::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRec } } -void Replicator::onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec) { +void Replicator::onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec) { auto sId = LSNs.originLSN_.Server(); if (sId != 0) { // sId = 0 for configurations without specifying a server id if (sId == config_.serverId) { @@ -1150,7 +1227,9 @@ void Replicator::onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WA } logPrintf(LogTrace, "[repl:%s:%s]:%d OnWALUpdate state = %d upstreamLSN = %s", nsName, slave_->storagePath_, config_.serverId, state_.load(std::memory_order_acquire), LSNs.upstreamLSN_); - if (!canApplyUpdate(LSNs, nsName, wrec)) return; + if (!canApplyUpdate(LSNs, nsName, wrec)) { + return; + } Error err; auto slaveNs = slave_->getNamespaceNoThrow(nsName, dummyCtx_); @@ -1171,7 +1250,7 @@ void Replicator::onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WA SyncStat stat; try { err = applyWALRecord(LSNs, nsName, slaveNs, wrec, stat); - } catch (const Error &e) { + } catch (const Error& e) { err = e; } if (err.ok()) { @@ -1182,18 +1261,19 @@ void Replicator::onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WA if (slaveNs) { auto replState = slaveNs->GetReplState(dummyCtx_); if (replState.status != ReplicationState::Status::Fatal) { - if (replState.replicatorEnabled) + if (replState.replicatorEnabled) { slaveNs->SetSlaveReplStatus(ReplicationState::Status::Fatal, err, dummyCtx_); - else + } else { logPrintf(LogError, "[repl:%s:%s]:%d OnWALUpdate logical error. Replication not allowed for nanespace. Err = %s", nsName, slave_->storagePath_, config_.serverId, err.what()); + } } } auto lastErrIt = lastNsErrMsg_.find(nsName); if (lastErrIt == lastNsErrMsg_.end()) { lastErrIt = lastNsErrMsg_.emplace(std::string(nsName), NsErrorMsg{}).first; } - auto &lastErr = lastErrIt->second; + auto& lastErr = lastErrIt->second; bool isDifferentError = lastErr.err.what() != err.what(); if (isDifferentError || lastErr.count == static_cast(config_.onlineReplErrorsThreshold)) { if (!lastErr.err.ok() && lastErr.count > 1) { @@ -1233,7 +1313,7 @@ void Replicator::OnUpdatesLost(std::string_view nsName) { resyncUpdatesLostAsync_.send(); } -void Replicator::OnConnectionState(const Error &err) { +void Replicator::OnConnectionState(const Error& err) { if (err.ok()) { logPrintf(LogInfo, "[repl:] OnConnectionState connected"); } else { @@ -1244,8 +1324,10 @@ void Replicator::OnConnectionState(const Error &err) { resync_.send(); } -bool Replicator::canApplyUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec) { - if (!isSyncEnabled(nsName)) return false; +bool Replicator::canApplyUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec) { + if (!isSyncEnabled(nsName)) { + return false; + } if (terminate_.load(std::memory_order_acquire)) { logPrintf(LogTrace, "[repl:%s]:%d Skipping update due to replicator shutdown is in progress upstreamLSN %s (%d)", nsName, @@ -1327,7 +1409,9 @@ bool Replicator::canApplyUpdate(LSNPair LSNs, std::string_view nsName, const WAL bool Replicator::isSyncEnabled(std::string_view nsName) { // SKip system ns - if (isSystemNamespaceNameFast(nsName)) return false; + if (isSystemNamespaceNameFast(nsName)) { + return false; + } // skip non enabled namespaces if (config_.namespaces.size() && config_.namespaces.find(nsName) == config_.namespaces.end()) { @@ -1336,14 +1420,14 @@ bool Replicator::isSyncEnabled(std::string_view nsName) { return true; } -void Replicator::SyncQueue::Push(const std::string &nsName, NamespaceDef &&nsDef, bool forced) { +void Replicator::SyncQueue::Push(const std::string& nsName, NamespaceDef&& nsDef, bool forced) { std::lock_guard lock(mtx_); - auto &val = queue_[nsName]; + auto& val = queue_[nsName]; val = recordData(std::move(nsDef), val.forced || forced); size_.fetch_add(1, std::memory_order_release); } -bool Replicator::SyncQueue::Get(NamespaceDef &def, bool &forced) const { +bool Replicator::SyncQueue::Get(NamespaceDef& def, bool& forced) const { std::lock_guard lock(mtx_); if (!queue_.empty()) { auto it = queue_.begin(); @@ -1354,7 +1438,7 @@ bool Replicator::SyncQueue::Get(NamespaceDef &def, bool &forced) const { return false; } -bool Replicator::SyncQueue::Pop(std::string_view nsName, const std::unique_lock &replicatorLock) noexcept { +bool Replicator::SyncQueue::Pop(std::string_view nsName, const std::unique_lock& replicatorLock) noexcept { std::lock_guard lock(mtx_); // Pop() must be called under top level replicator's sync mutex assertrx(replicatorLock.owns_lock()); diff --git a/cpp_src/replicator/replicator.h b/cpp_src/replicator/replicator.h index 1c0ba9ddf..c91540abd 100644 --- a/cpp_src/replicator/replicator.h +++ b/cpp_src/replicator/replicator.h @@ -13,9 +13,9 @@ class ReindexerImpl; class Replicator : public IUpdatesObserver { public: - Replicator(ReindexerImpl *slave); + Replicator(ReindexerImpl* slave); ~Replicator(); - bool Configure(const ReplicationConfigData &config); + bool Configure(const ReplicationConfigData& config); Error Start(); void Stop(); void Enable() { enabled_.store(true, std::memory_order_release); } @@ -26,7 +26,7 @@ class Replicator : public IUpdatesObserver { Error lastError; int updated = 0, deleted = 0, errors = 0, updatedIndexes = 0, deletedIndexes = 0, updatedMeta = 0, processed = 0, schemasSet = 0, txStarts = 0, txEnds = 0; - WrSerializer &Dump(WrSerializer &ser); + WrSerializer& Dump(WrSerializer& ser); }; struct NsErrorMsg { Error err; @@ -34,10 +34,10 @@ class Replicator : public IUpdatesObserver { }; class SyncQueue { public: - SyncQueue(const std::mutex &replicatorMtx) noexcept : replicatorMtx_(replicatorMtx) {} - void Push(const std::string &nsName, NamespaceDef &&nsDef, bool force); - bool Get(NamespaceDef &def, bool &force) const; - bool Pop(std::string_view nsName, const std::unique_lock &replicatorLock) noexcept; + SyncQueue(const std::mutex& replicatorMtx) noexcept : replicatorMtx_(replicatorMtx) {} + void Push(const std::string& nsName, NamespaceDef&& nsDef, bool force); + bool Get(NamespaceDef& def, bool& force) const; + bool Pop(std::string_view nsName, const std::unique_lock& replicatorLock) noexcept; size_t Size() const noexcept { return size_.load(std::memory_order_acquire); } bool Contains(std::string_view nsName) noexcept; void Clear() noexcept; @@ -45,7 +45,7 @@ class Replicator : public IUpdatesObserver { private: struct recordData { recordData() = default; - recordData(NamespaceDef &&_def, bool _forced) : def(std::move(_def)), forced(_forced) {} + recordData(NamespaceDef&& _def, bool _forced) : def(std::move(_def)), forced(_forced) {} NamespaceDef def; bool forced = false; @@ -53,11 +53,11 @@ class Replicator : public IUpdatesObserver { fast_hash_map queue_; std::atomic size_ = {0}; mutable std::mutex mtx_; - const std::mutex &replicatorMtx_; + const std::mutex& replicatorMtx_; }; struct ForceSyncContext { - const NamespaceDef &nsDef; + const NamespaceDef& nsDef; std::function replaceTagsMatcher; }; @@ -69,47 +69,47 @@ class Replicator : public IUpdatesObserver { std::string_view forceSyncReason; }; // Sync single namespace - SyncNsResult syncNamespace(const NamespaceDef &ns, std::string_view forceSyncReason, SyncQueue *sourceQueue, + SyncNsResult syncNamespace(const NamespaceDef& ns, std::string_view forceSyncReason, SyncQueue* sourceQueue, std::string_view initiator); // Sync database Error syncDatabase(std::string_view initiator); // Read and apply WAL from master - Error syncNamespaceByWAL(const NamespaceDef &ns); + Error syncNamespaceByWAL(const NamespaceDef& ns); // Apply WAL from master to namespace - Error applyWAL(Namespace::Ptr &slaveNs, client::QueryResults &qr, const ForceSyncContext *fsyncCtx = nullptr); + Error applyWAL(Namespace::Ptr& slaveNs, client::QueryResults& qr, const ForceSyncContext* fsyncCtx = nullptr); // Sync indexes of namespace - Error syncIndexesForced(Namespace::Ptr &slaveNs, const ForceSyncContext &fsyncCtx); + Error syncIndexesForced(Namespace::Ptr& slaveNs, const ForceSyncContext& fsyncCtx); // Sync namespace schema - Error syncSchemaForced(Namespace::Ptr &slaveNs, const NamespaceDef &masterNsDef); + Error syncSchemaForced(Namespace::Ptr& slaveNs, const NamespaceDef& masterNsDef); // Forced sync of namespace - Error syncNamespaceForced(const NamespaceDef &ns, std::string_view reason); + Error syncNamespaceForced(const NamespaceDef& ns, std::string_view reason); // Sync metadata - Error syncMetaForced(Namespace::Ptr &slaveNs, std::string_view nsName); + Error syncMetaForced(Namespace::Ptr& slaveNs, std::string_view nsName); // Apply single WAL record - Error applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr &ns, const WALRecord &wrec, SyncStat &stat); + Error applyWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr& ns, const WALRecord& wrec, SyncStat& stat); // Apply single transaction WAL record - Error applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr &ns, const WALRecord &wrec, SyncStat &stat); - void checkNoOpenedTransaction(std::string_view nsName, Namespace::Ptr &slaveNs); + Error applyTxWALRecord(LSNPair LSNs, std::string_view nsName, Namespace::Ptr& ns, const WALRecord& wrec, SyncStat& stat); + void checkNoOpenedTransaction(std::string_view nsName, Namespace::Ptr& slaveNs); // Apply single cjson item - Error modifyItem(LSNPair LSNs, Namespace::Ptr &ns, std::string_view cjson, int modifyMode, const TagsMatcher &tm, SyncStat &stat); + Error modifyItem(LSNPair LSNs, Namespace::Ptr& ns, std::string_view cjson, int modifyMode, const TagsMatcher& tm, SyncStat& stat); // Add single cjson item into tx - Error modifyItemTx(LSNPair LSNs, Transaction &tx, std::string_view cjson, int modifyMode, const TagsMatcher &tm, SyncStat &stat); - static Error unpackItem(Item &, lsn_t, std::string_view cjson, const TagsMatcher &tm); + Error modifyItemTx(LSNPair LSNs, Transaction& tx, std::string_view cjson, int modifyMode, const TagsMatcher& tm, SyncStat& stat); + static Error unpackItem(Item&, lsn_t, std::string_view cjson, const TagsMatcher& tm); // Push update to the queue to apply it later - void pushPendingUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec); + void pushPendingUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec); - void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec) override final; - void onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec); + void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec) override final; + void onWALUpdateImpl(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec); void OnUpdatesLost(std::string_view nsName) override final; - void OnConnectionState(const Error &err) override final; + void OnConnectionState(const Error& err) override final; - bool canApplyUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &wrec); + bool canApplyUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& wrec); bool isSyncEnabled(std::string_view nsName); - bool retryIfNetworkError(const Error &err); - void subscribeUpdatesIfRequired(const std::string &nsName); + bool retryIfNetworkError(const Error& err); + void subscribeUpdatesIfRequired(const std::string& nsName); std::unique_ptr master_; - ReindexerImpl *slave_; + ReindexerImpl* slave_; net::ev::dynamic_loop loop_; std::thread thread_; @@ -141,7 +141,7 @@ class Replicator : public IUpdatesObserver { std::atomic enabled_; const RdxContext dummyCtx_; - std::unordered_map transactions_; + std::unordered_map transactions_; fast_hash_map lastNsErrMsg_; SyncQueue syncQueue_; }; diff --git a/cpp_src/replicator/updatesobserver.cc b/cpp_src/replicator/updatesobserver.cc index 62e07f98e..b0e6ba47f 100644 --- a/cpp_src/replicator/updatesobserver.cc +++ b/cpp_src/replicator/updatesobserver.cc @@ -8,7 +8,7 @@ using namespace std::string_view_literals; namespace reindexer { -void UpdatesFilters::Merge(const UpdatesFilters &rhs) { +void UpdatesFilters::Merge(const UpdatesFilters& rhs) { if (filters_.empty()) { return; } @@ -16,13 +16,13 @@ void UpdatesFilters::Merge(const UpdatesFilters &rhs) { filters_.clear(); return; } - for (auto &rhsFilter : rhs.filters_) { + for (auto& rhsFilter : rhs.filters_) { auto foundNs = filters_.find(rhsFilter.first); if (foundNs == filters_.end()) { filters_.emplace(rhsFilter.first, rhsFilter.second); } else { - auto &nsFilters = foundNs.value(); - for (auto &filter : rhsFilter.second) { + auto& nsFilters = foundNs.value(); + for (auto& filter : rhsFilter.second) { const auto foundFilter = std::find(nsFilters.cbegin(), nsFilters.cend(), filter); if (foundFilter == nsFilters.cend()) { nsFilters.emplace_back(filter); @@ -39,7 +39,7 @@ void UpdatesFilters::AddFilter(std::string_view ns, UpdatesFilters::Filter filte lst.emplace_back(std::move(filter)); // NOLINT(performance-move-const-arg) filters_.emplace(std::string(ns), std::move(lst)); } else { - auto &nsFilters = foundNs.value(); + auto& nsFilters = foundNs.value(); const auto foundFilter = std::find(nsFilters.cbegin(), nsFilters.cend(), filter); if (foundFilter == nsFilters.cend()) { nsFilters.emplace_back(std::move(filter)); // NOLINT(performance-move-const-arg) @@ -57,7 +57,7 @@ bool UpdatesFilters::Check(std::string_view ns) const { return false; } - for (auto &filter : found.value()) { + for (auto& filter : found.value()) { if (filter.Check()) { return true; } @@ -68,18 +68,18 @@ bool UpdatesFilters::Check(std::string_view ns) const { Error UpdatesFilters::FromJSON(span json) { try { FromJSON(gason::JsonParser().Parse(json)); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "UpdatesFilter: %s", ex.what()); - } catch (const Error &err) { + } catch (const Error& err) { return err; } return errOK; } -void UpdatesFilters::FromJSON(const gason::JsonNode &root) { - for (const auto &ns : root["namespaces"sv]) { +void UpdatesFilters::FromJSON(const gason::JsonNode& root) { + for (const auto& ns : root["namespaces"sv]) { auto name = ns["name"sv].As(); - for (const auto &f : ns["filters"sv]) { + for (const auto& f : ns["filters"sv]) { Filter filter; filter.FromJSON(f); AddFilter(name, std::move(filter)); // NOLINT(performance-move-const-arg) @@ -87,15 +87,15 @@ void UpdatesFilters::FromJSON(const gason::JsonNode &root) { } } -void UpdatesFilters::GetJSON(WrSerializer &ser) const { +void UpdatesFilters::GetJSON(WrSerializer& ser) const { JsonBuilder builder(ser); { auto arr = builder.Array("namespaces"sv); - for (const auto &nsFilters : filters_) { + for (const auto& nsFilters : filters_) { auto obj = arr.Object(); obj.Put("name"sv, nsFilters.first); auto arrFilters = obj.Array("filters"sv); - for (const auto &filter : nsFilters.second) { + for (const auto& filter : nsFilters.second) { auto filtersObj = arrFilters.Object(); filter.GetJSON(filtersObj); } @@ -103,12 +103,12 @@ void UpdatesFilters::GetJSON(WrSerializer &ser) const { } } -bool UpdatesFilters::operator==(const UpdatesFilters &rhs) const { +bool UpdatesFilters::operator==(const UpdatesFilters& rhs) const { if (filters_.size() != rhs.filters_.size()) { return false; } - for (const auto &nsFilters : filters_) { + for (const auto& nsFilters : filters_) { const auto rhsNsFilters = rhs.filters_.find(nsFilters.first); if (rhsNsFilters == rhs.filters_.cend()) { return false; @@ -116,7 +116,7 @@ bool UpdatesFilters::operator==(const UpdatesFilters &rhs) const { if (rhsNsFilters.value().size() != nsFilters.second.size()) { return false; } - for (const auto &filter : nsFilters.second) { + for (const auto& filter : nsFilters.second) { const auto rhsFilter = std::find(rhsNsFilters.value().cbegin(), rhsNsFilters.value().cend(), filter); if (rhsFilter == rhsNsFilters.value().cend()) { return false; @@ -127,9 +127,9 @@ bool UpdatesFilters::operator==(const UpdatesFilters &rhs) const { return true; } -void UpdatesObservers::Add(IUpdatesObserver *observer, const UpdatesFilters &filters, SubscriptionOpts opts) { +void UpdatesObservers::Add(IUpdatesObserver* observer, const UpdatesFilters& filters, SubscriptionOpts opts) { std::unique_lock lck(mtx_); - auto it = std::find_if(observers_.begin(), observers_.end(), [observer](const ObserverInfo &info) { return info.ptr == observer; }); + auto it = std::find_if(observers_.begin(), observers_.end(), [observer](const ObserverInfo& info) { return info.ptr == observer; }); if (it != observers_.end()) { if (opts.IsIncrementSubscription()) { it->filters.Merge(filters); @@ -141,9 +141,9 @@ void UpdatesObservers::Add(IUpdatesObserver *observer, const UpdatesFilters &fil } } -Error UpdatesObservers::Delete(IUpdatesObserver *observer) { +Error UpdatesObservers::Delete(IUpdatesObserver* observer) { std::unique_lock lck(mtx_); - auto it = std::find_if(observers_.begin(), observers_.end(), [observer](const ObserverInfo &info) { return info.ptr == observer; }); + auto it = std::find_if(observers_.begin(), observers_.end(), [observer](const ObserverInfo& info) { return info.ptr == observer; }); if (it == observers_.end()) { return Error(errParams, "Observer was not added"); } @@ -156,7 +156,7 @@ std::vector UpdatesObservers::Get() const { return observers_; } -void UpdatesObservers::OnModifyItem(LSNPair LSNs, std::string_view nsName, ItemImpl *impl, int modifyMode, bool inTransaction) { +void UpdatesObservers::OnModifyItem(LSNPair LSNs, std::string_view nsName, ItemImpl* impl, int modifyMode, bool inTransaction) { WrSerializer ser; WALRecord walRec(WalItemModify, impl->tagsMatcher().isUpdated() ? impl->GetCJSON(ser, true) : impl->GetCJSON(), impl->tagsMatcher().version(), modifyMode, inTransaction); @@ -164,9 +164,11 @@ void UpdatesObservers::OnModifyItem(LSNPair LSNs, std::string_view nsName, ItemI OnWALUpdate(LSNs, nsName, walRec); } -void UpdatesObservers::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &walRec) { +void UpdatesObservers::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& walRec) { // Disable updates of system namespaces (it may cause recursive lock) - if (isSystemNamespaceNameFast(nsName)) return; + if (isSystemNamespaceNameFast(nsName)) { + return; + } bool skipFilters = walRec.type == WalNamespaceAdd || walRec.type == WalNamespaceDrop || walRec.type == WalNamespaceRename || walRec.type == WalForceSync || nsName.empty(); @@ -191,9 +193,9 @@ void UpdatesObservers::OnUpdatesLost(std::string_view nsName) { } } -void UpdatesObservers::OnConnectionState(const Error &err) { +void UpdatesObservers::OnConnectionState(const Error& err) { shared_lock lck(mtx_); - for (auto &observer : observers_) { + for (auto& observer : observers_) { observer.ptr->OnConnectionState(err); } } @@ -201,13 +203,13 @@ void UpdatesObservers::OnConnectionState(const Error &err) { UpdatesFilters UpdatesObservers::GetMergedFilter() const { shared_lock lck(mtx_); UpdatesFilters filter = observers_.size() ? observers_.front().filters : UpdatesFilters(); - for (const auto &observer : observers_) { + for (const auto& observer : observers_) { filter.Merge(observer.filters); } return filter; } -std::ostream &operator<<(std::ostream &o, const reindexer::UpdatesFilters &filters) { +std::ostream& operator<<(std::ostream& o, const reindexer::UpdatesFilters& filters) { reindexer::WrSerializer ser; filters.GetJSON(ser); o << ser.Slice(); diff --git a/cpp_src/replicator/updatesobserver.h b/cpp_src/replicator/updatesobserver.h index dc3f5c6cb..05f58f777 100644 --- a/cpp_src/replicator/updatesobserver.h +++ b/cpp_src/replicator/updatesobserver.h @@ -23,10 +23,10 @@ class UpdatesFilters { public: // TODO: Any additional condition check should be added here bool Check() const { return true; } - void FromJSON(const gason::JsonNode &) {} - void GetJSON(JsonBuilder &) const {} + void FromJSON(const gason::JsonNode&) {} + void GetJSON(JsonBuilder&) const {} - bool operator==(const Filter &) const { return true; } + bool operator==(const Filter&) const { return true; } }; /// Merge two filters sets @@ -34,7 +34,7 @@ class UpdatesFilters { /// If one of the filters set contains some conditions for specific namespace, /// then result filters set will also contain this conditions /// @param rhs - Another filters set - void Merge(const UpdatesFilters &rhs); + void Merge(const UpdatesFilters& rhs); /// Add new filter for specified namespace. Doesn't merge filters, just concatenates it into disjunction sequence /// @param ns - Namespace /// @param filter - Filter to add @@ -45,10 +45,10 @@ class UpdatesFilters { bool Check(std::string_view ns) const; Error FromJSON(span json); - void FromJSON(const gason::JsonNode &root); - void GetJSON(WrSerializer &ser) const; + void FromJSON(const gason::JsonNode& root); + void GetJSON(WrSerializer& ser) const; - bool operator==(const UpdatesFilters &rhs) const; + bool operator==(const UpdatesFilters& rhs) const; private: using FiltersList = h_vector; @@ -59,29 +59,29 @@ class UpdatesFilters { class IUpdatesObserver { public: virtual ~IUpdatesObserver() = default; - virtual void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &rec) = 0; + virtual void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& rec) = 0; virtual void OnUpdatesLost(std::string_view nsName) = 0; - virtual void OnConnectionState(const Error &err) = 0; + virtual void OnConnectionState(const Error& err) = 0; }; class UpdatesObservers { public: struct ObserverInfo { - IUpdatesObserver *ptr; + IUpdatesObserver* ptr; UpdatesFilters filters; }; - void Add(IUpdatesObserver *observer, const UpdatesFilters &filter, SubscriptionOpts opts); - Error Delete(IUpdatesObserver *observer); + void Add(IUpdatesObserver* observer, const UpdatesFilters& filter, SubscriptionOpts opts); + Error Delete(IUpdatesObserver* observer); std::vector Get() const; - void OnModifyItem(LSNPair LSNs, std::string_view nsName, ItemImpl *item, int modifyMode, bool inTransaction); + void OnModifyItem(LSNPair LSNs, std::string_view nsName, ItemImpl* item, int modifyMode, bool inTransaction); - void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &rec); + void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& rec); void OnUpdatesLost(std::string_view nsName); - void OnConnectionState(const Error &err); + void OnConnectionState(const Error& err); bool Empty() { shared_lock lck(mtx_); return observers_.empty(); @@ -93,6 +93,6 @@ class UpdatesObservers { mutable shared_timed_mutex mtx_; }; -std::ostream &operator<<(std::ostream &o, const reindexer::UpdatesFilters &sv); +std::ostream& operator<<(std::ostream& o, const reindexer::UpdatesFilters& sv); } // namespace reindexer diff --git a/cpp_src/replicator/walrecord.cc b/cpp_src/replicator/walrecord.cc index f70831b19..0f37ad24b 100644 --- a/cpp_src/replicator/walrecord.cc +++ b/cpp_src/replicator/walrecord.cc @@ -8,14 +8,16 @@ namespace reindexer { enum { TxBit = (1 << 7) }; -void PackedWALRecord::Pack(const WALRecord &rec) { +void PackedWALRecord::Pack(const WALRecord& rec) { WrSerializer ser; rec.Pack(ser); assign(ser.Buf(), ser.Buf() + ser.Len()); } -void WALRecord::Pack(WrSerializer &ser) const { - if (type == WalEmpty) return; +void WALRecord::Pack(WrSerializer& ser) const { + if (type == WalEmpty) { + return; + } ser.PutVarUint(inTransaction ? (type | TxBit) : type); switch (type) { case WalItemUpdate: @@ -171,9 +173,11 @@ static std::string_view wrecType2Str(WALRecType t) { return ""sv; } -WrSerializer &WALRecord::Dump(WrSerializer &ser, const std::function &cjsonViewer) const { +WrSerializer& WALRecord::Dump(WrSerializer& ser, const std::function& cjsonViewer) const { ser << wrecType2Str(type); - if (inTransaction) ser << " InTransaction"; + if (inTransaction) { + ser << " InTransaction"; + } switch (type) { case WalEmpty: case WalNamespaceAdd: @@ -210,7 +214,7 @@ WrSerializer &WALRecord::Dump(WrSerializer &ser, const std::function &cjsonViewer) const { +void WALRecord::GetJSON(JsonBuilder& jb, const std::function& cjsonViewer) const { jb.Put("type", wrecType2Str(type)); jb.Put("in_transaction", inTransaction); @@ -270,7 +274,7 @@ void WALRecord::GetJSON(JsonBuilder &jb, const std::function(reinterpret_cast(data.data()), data.size())) {} +WALRecord::WALRecord(std::string_view data) : WALRecord(span(reinterpret_cast(data.data()), data.size())) {} SharedWALRecord WALRecord::GetShared(int64_t lsn, int64_t upstreamLSN, std::string_view nsName) const { if (!shared_.packed_) { @@ -278,7 +282,7 @@ SharedWALRecord WALRecord::GetShared(int64_t lsn, int64_t upstreamLSN, std::stri } return shared_; } -SharedWALRecord::SharedWALRecord(int64_t lsn, int64_t originLSN, std::string_view nsName, const WALRecord &rec) { +SharedWALRecord::SharedWALRecord(int64_t lsn, int64_t originLSN, std::string_view nsName, const WALRecord& rec) { const size_t kCapToSizeRelation = 4; WrSerializer ser; ser.PutVarint(lsn); diff --git a/cpp_src/replicator/walrecord.h b/cpp_src/replicator/walrecord.h index cee75b29d..19b5e8f7f 100644 --- a/cpp_src/replicator/walrecord.h +++ b/cpp_src/replicator/walrecord.h @@ -48,7 +48,7 @@ struct SharedWALRecord { p_string nsName, pwalRec; }; SharedWALRecord(intrusive_ptr> packed = nullptr) : packed_(std::move(packed)) {} - SharedWALRecord(int64_t upstreamLSN, int64_t originLSN, std::string_view nsName, const WALRecord &rec); + SharedWALRecord(int64_t upstreamLSN, int64_t originLSN, std::string_view nsName, const WALRecord& rec); Unpacked Unpack(); intrusive_ptr> packed_; @@ -62,9 +62,9 @@ struct WALRecord { explicit WALRecord(WALRecType _type, std::string_view key, std::string_view value) : type(_type), itemMeta{key, value} {} explicit WALRecord(WALRecType _type, std::string_view cjson, int tmVersion, int modifyMode, bool inTx = false) : type(_type), itemModify{cjson, tmVersion, modifyMode}, inTransaction(inTx) {} - WrSerializer &Dump(WrSerializer &ser, const std::function &cjsonViewer) const; - void GetJSON(JsonBuilder &jb, const std::function &cjsonViewer) const; - void Pack(WrSerializer &ser) const; + WrSerializer& Dump(WrSerializer& ser, const std::function& cjsonViewer) const; + void GetJSON(JsonBuilder& jb, const std::function& cjsonViewer) const; + void Pack(WrSerializer& ser) const; SharedWALRecord GetShared(int64_t lsn, int64_t upstreamLSN, std::string_view nsName) const; WALRecType type; @@ -91,6 +91,6 @@ struct WALRecord { struct PackedWALRecord : public h_vector { using h_vector::h_vector; - void Pack(const WALRecord &rec); + void Pack(const WALRecord& rec); }; } // namespace reindexer diff --git a/cpp_src/replicator/walselecter.cc b/cpp_src/replicator/walselecter.cc index 5356051ed..4c1c06b15 100644 --- a/cpp_src/replicator/walselecter.cc +++ b/cpp_src/replicator/walselecter.cc @@ -9,11 +9,11 @@ namespace reindexer { const SemVersion kMinUnknownReplSupportRxVersion("2.6.0"); -WALSelecter::WALSelecter(const NamespaceImpl *ns) : ns_(ns) {} +WALSelecter::WALSelecter(const NamespaceImpl* ns) : ns_(ns) {} -void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { +void WALSelecter::operator()(QueryResults& result, SelectCtx& params) { using namespace std::string_view_literals; - const Query &q = params.query; + const Query& q = params.query; int count = q.Limit(); int start = q.Offset(); result.totalCount = 0; @@ -29,7 +29,7 @@ void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { for (size_t i = 0; i < q.Entries().Size(); ++i) { q.Entries().Visit( i, - [&lsnIdx, &versionIdx, i] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntry &qe) RX_POST_LMBD_ALWAYS_INLINE { + [&lsnIdx, &versionIdx, i] RX_PRE_LMBD_ALWAYS_INLINE(const QueryEntry& qe) RX_POST_LMBD_ALWAYS_INLINE { if ("#lsn"sv == qe.FieldName()) { lsnIdx = i; } else if ("#slave_version"sv == qe.FieldName()) { @@ -38,23 +38,26 @@ void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { throw Error(errLogic, "Unexpected index in WAL select query: %s", qe.FieldName()); } }, - [&q] RX_PRE_LMBD_ALWAYS_INLINE(const auto &) + [&q] RX_PRE_LMBD_ALWAYS_INLINE(const auto&) RX_POST_LMBD_ALWAYS_INLINE { throw Error(errLogic, "Unexpected WAL select query: %s", q.GetSQL()); }); } auto slaveVersion = versionIdx < 0 ? SemVersion() : SemVersion(q.Entries().Get(versionIdx).Values()[0].As()); - auto &lsnEntry = q.Entries().Get(lsnIdx); + auto& lsnEntry = q.Entries().Get(lsnIdx); if (lsnEntry.Values().size() == 1 && lsnEntry.Condition() == CondGt) { lsn_t fromLSN = lsn_t(std::min(lsnEntry.Values()[0].As(), std::numeric_limits::max() - 1)); - if (ns_->wal_.LSNCounter() != (fromLSN.Counter() + 1) && ns_->wal_.is_outdated(fromLSN.Counter() + 1) && count) + if (ns_->wal_.LSNCounter() != (fromLSN.Counter() + 1) && ns_->wal_.is_outdated(fromLSN.Counter() + 1) && count) { throw Error(errOutdatedWAL, "Query to WAL with outdated LSN %ld, LSN counter %ld walSize = %d count = %d", int64_t(fromLSN.Counter()), ns_->wal_.LSNCounter(), ns_->wal_.size(), count); + } const auto walEnd = ns_->wal_.end(); for (auto it = ns_->wal_.upper_bound(fromLSN.Counter()); count && it != walEnd; ++it) { WALRecord rec = *it; switch (rec.type) { case WalItemUpdate: - if (ns_->items_[rec.id].IsFree()) break; + if (ns_->items_[rec.id].IsFree()) { + break; + } if (start) { start--; } else if (count) { @@ -115,7 +118,7 @@ void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { } } else if (lsnEntry.Condition() == CondAny) { if (start == 0 && !(slaveVersion < kMinUnknownReplSupportRxVersion)) { - auto addSpRecord = [&result](const WALRecord &wrec) { + auto addSpRecord = [&result](const WALRecord& wrec) { PackedWALRecord wr; wr.Pack(wrec); PayloadValue val(wr.size(), wr.data()); @@ -130,7 +133,7 @@ void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { addSpRecord(wrec); } std::vector metaKeys = ns_->enumMeta(); - for (const auto &key : metaKeys) { + for (const auto& key : metaKeys) { auto metaVal = ns_->getMeta(key); WALRecord wrec(WalPutMeta, key, metaVal); addSpRecord(wrec); @@ -143,7 +146,9 @@ void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { } } for (size_t id = 0; count && id < ns_->items_.size(); ++id) { - if (ns_->items_[id].IsFree()) continue; + if (ns_->items_[id].IsFree()) { + continue; + } if (start) { start--; } else if (count) { @@ -158,7 +163,7 @@ void WALSelecter::operator()(QueryResults &result, SelectCtx ¶ms) { putReplState(result); } -void WALSelecter::putReplState(QueryResults &result) { +void WALSelecter::putReplState(QueryResults& result) { WrSerializer ser; JsonBuilder jb(ser); // prepare json with replication state diff --git a/cpp_src/replicator/walselecter.h b/cpp_src/replicator/walselecter.h index 0e2c0ec5f..f0ab3faaf 100644 --- a/cpp_src/replicator/walselecter.h +++ b/cpp_src/replicator/walselecter.h @@ -8,12 +8,12 @@ class RdxContext; struct SelectCtx; class WALSelecter { public: - WALSelecter(const NamespaceImpl *ns); - void operator()(QueryResults &result, SelectCtx ¶ms); + WALSelecter(const NamespaceImpl* ns); + void operator()(QueryResults& result, SelectCtx& params); protected: - void putReplState(QueryResults &result); - const NamespaceImpl *ns_; + void putReplState(QueryResults& result); + const NamespaceImpl* ns_; }; } // namespace reindexer diff --git a/cpp_src/replicator/waltracker.cc b/cpp_src/replicator/waltracker.cc index 3273d8eff..159e9b9e8 100644 --- a/cpp_src/replicator/waltracker.cc +++ b/cpp_src/replicator/waltracker.cc @@ -10,7 +10,7 @@ namespace reindexer { WALTracker::WALTracker(int64_t sz) : walSize_(sz) { logPrintf(LogTrace, "[WALTracker] Create LSN=%ld", lsnCounter_); } -WALTracker::WALTracker(const WALTracker &wal, AsyncStorage &storage) +WALTracker::WALTracker(const WALTracker& wal, AsyncStorage& storage) : records_(wal.records_), lsnCounter_(wal.lsnCounter_), walSize_(wal.walSize_), @@ -18,7 +18,7 @@ WALTracker::WALTracker(const WALTracker &wal, AsyncStorage &storage) heapSize_(wal.heapSize_), storage_(&storage) {} -int64_t WALTracker::Add(const WALRecord &rec, lsn_t oldLsn) { +int64_t WALTracker::Add(const WALRecord& rec, lsn_t oldLsn) { int64_t lsn = lsnCounter_++; if (lsnCounter_ > 1 && walOffset_ == (lsnCounter_ - 1) % walSize_) { walOffset_ = lsnCounter_ % walSize_; @@ -34,7 +34,7 @@ int64_t WALTracker::Add(const WALRecord &rec, lsn_t oldLsn) { return lsn; } -bool WALTracker::Set(const WALRecord &rec, int64_t lsn) { +bool WALTracker::Set(const WALRecord& rec, int64_t lsn) { if (!available(lsn)) { return false; } @@ -71,7 +71,7 @@ bool WALTracker::Resize(int64_t sz) { return true; } -void WALTracker::Init(int64_t sz, int64_t minLSN, int64_t maxLSN, AsyncStorage &storage) { +void WALTracker::Init(int64_t sz, int64_t minLSN, int64_t maxLSN, AsyncStorage& storage) { logPrintf(LogTrace, "WALTracker::Init minLSN=%ld, maxLSN=%ld, size=%ld", minLSN, maxLSN, sz); storage_ = &storage; @@ -80,12 +80,12 @@ void WALTracker::Init(int64_t sz, int64_t minLSN, int64_t maxLSN, AsyncStorage & // new table initPositions(sz, minLSN, maxLSN); // Fill records from storage - for (auto &rec : data) { + for (auto& rec : data) { Set(WALRecord(std::string_view(rec.second)), rec.first); } } -void WALTracker::put(int64_t lsn, const WALRecord &rec) { +void WALTracker::put(int64_t lsn, const WALRecord& rec) { int64_t pos = lsn % walSize_; if (pos >= int64_t(records_.size())) { records_.resize(uint64_t(pos + 1)); @@ -103,17 +103,21 @@ void WALTracker::writeToStorage(int64_t lsn) { key << kStorageWALPrefix; key.PutUInt32(pos); data.PutUInt64(lsn); - data.Write(std::string_view(reinterpret_cast(records_[pos].data()), records_[pos].size())); - if (storage_ && storage_->IsValid()) storage_->WriteSync(StorageOpts(), key.Slice(), data.Slice()); + data.Write(std::string_view(reinterpret_cast(records_[pos].data()), records_[pos].size())); + if (storage_ && storage_->IsValid()) { + storage_->WriteSync(StorageOpts(), key.Slice(), data.Slice()); + } } -std::vector> WALTracker::readFromStorage(int64_t &maxLSN) { +std::vector> WALTracker::readFromStorage(int64_t& maxLSN) { std::vector> data; StorageOpts opts; opts.FillCache(false); - if (!storage_ || !storage_->IsValid()) return data; + if (!storage_ || !storage_->IsValid()) { + return data; + } auto dbIter = storage_->GetCursor(opts); for (dbIter->Seek(kStorageWALPrefix); @@ -122,7 +126,7 @@ std::vector> WALTracker::readFromStorage(int64_t std::string_view dataSlice = dbIter->Value(); if (dataSlice.size() >= sizeof(int64_t)) { // Read LSN - const int64_t lsn = *reinterpret_cast(dataSlice.data()); + const int64_t lsn = *reinterpret_cast(dataSlice.data()); assertrx(lsn >= 0); maxLSN = std::max(maxLSN, lsn); dataSlice = dataSlice.substr(sizeof(lsn)); diff --git a/cpp_src/replicator/waltracker.h b/cpp_src/replicator/waltracker.h index 772a34f48..13f3b5497 100644 --- a/cpp_src/replicator/waltracker.h +++ b/cpp_src/replicator/waltracker.h @@ -14,24 +14,24 @@ class AsyncStorage; class WALTracker { public: explicit WALTracker(int64_t sz); - WALTracker(const WALTracker &) = delete; - WALTracker(const WALTracker &wal, AsyncStorage &storage); + WALTracker(const WALTracker&) = delete; + WALTracker(const WALTracker& wal, AsyncStorage& storage); /// Initialize WAL tracker. /// @param sz - Max WAL size /// @param minLSN - Min available LSN number /// @param maxLSN - Current LSN counter value /// @param storage - Storage object for store WAL records - void Init(int64_t sz, int64_t minLSN, int64_t maxLSN, AsyncStorage &storage); + void Init(int64_t sz, int64_t minLSN, int64_t maxLSN, AsyncStorage& storage); /// Add new record to WAL tracker /// @param rec - Record to be added /// @param oldLsn - Optional, previous LSN value of changed object /// @return LSN value of record - int64_t Add(const WALRecord &rec, lsn_t oldLsn = lsn_t()); + int64_t Add(const WALRecord& rec, lsn_t oldLsn = lsn_t()); /// Set record in WAL tracker /// @param rec - Record to be added /// @param lsn - LSN value /// @return true if set successful, false, if lsn is outdated - bool Set(const WALRecord &rec, int64_t lsn); + bool Set(const WALRecord& rec, int64_t lsn); /// Get current LSN counter value /// @return current LSN counter value int64_t LSNCounter() const { return lsnCounter_; } @@ -46,11 +46,11 @@ class WALTracker { /// Iterator for WAL records class iterator { public: - iterator &operator++() noexcept { + iterator& operator++() noexcept { ++idx_; return *this; } - bool operator!=(const iterator &other) const noexcept { return idx_ != other.idx_; } + bool operator!=(const iterator& other) const noexcept { return idx_ != other.idx_; } WALRecord operator*() const { assertf(idx_ % wt_->walSize_ < int(wt_->records_.size()), "idx=%d,wt_->records_.size()=%d,lsnCounter=%d", idx_, wt_->records_.size(), wt_->lsnCounter_); @@ -60,7 +60,7 @@ class WALTracker { span GetRaw() const noexcept { return wt_->records_[idx_ % wt_->walSize_]; } int64_t GetLSN() const noexcept { return idx_; } int64_t idx_; - const WALTracker *wt_; + const WALTracker* wt_; }; /// Get end iterator @@ -96,13 +96,13 @@ class WALTracker { /// put WAL record into lsn position, grow ring buffer, if neccessary /// @param lsn LSN value /// @param rec - Record to be added - void put(int64_t lsn, const WALRecord &rec); + void put(int64_t lsn, const WALRecord& rec); /// check if lsn is available. e.g. in range of ring buffer bool available(int64_t lsn) const { return lsn < lsnCounter_ && lsnCounter_ - lsn <= size(); } /// flushes lsn value to storage /// @param lsn - lsn value void writeToStorage(int64_t lsn); - std::vector> readFromStorage(int64_t &maxLsn); + std::vector> readFromStorage(int64_t& maxLsn); void initPositions(int64_t sz, int64_t minLSN, int64_t maxLSN); /// Ring buffer of WAL records @@ -116,7 +116,7 @@ class WALTracker { /// Cached heap size of WAL object size_t heapSize_ = 0; - AsyncStorage *storage_ = nullptr; + AsyncStorage* storage_ = nullptr; }; } // namespace reindexer diff --git a/cpp_src/server/cbinding/server_c.cc b/cpp_src/server/cbinding/server_c.cc index 252a4ca0a..858b6f494 100644 --- a/cpp_src/server/cbinding/server_c.cc +++ b/cpp_src/server/cbinding/server_c.cc @@ -42,7 +42,9 @@ reindexer_error start_reindexer_server(uintptr_t psvc, reindexer_string _config) auto svc = reinterpret_cast(psvc); if (svc) { err = svc->InitFromYAML(str2c(_config)); - if (!err.ok()) return error2c(err); + if (!err.ok()) { + return error2c(err); + } err = (svc->Start() == 0 ? Error{} : Error(errLogic, "server startup error")); } return error2c(err); diff --git a/cpp_src/server/config.cc b/cpp_src/server/config.cc index ae1e94172..4bf8b12db 100644 --- a/cpp_src/server/config.cc +++ b/cpp_src/server/config.cc @@ -54,29 +54,29 @@ void ServerConfig::Reset() { AllocatorCachePart = -1; } -reindexer::Error ServerConfig::ParseYaml(const std::string &yaml) { +reindexer::Error ServerConfig::ParseYaml(const std::string& yaml) { Error err; try { YAML::Node root = YAML::Load(yaml); err = fromYaml(root); - } catch (const YAML::Exception &ex) { + } catch (const YAML::Exception& ex) { err = Error(errParams, "Error with config string. Reason: '%s'", ex.what()); } return err; } -Error ServerConfig::ParseFile(const std::string &filePath) { +Error ServerConfig::ParseFile(const std::string& filePath) { Error err; try { YAML::Node root = YAML::LoadFile(filePath); err = fromYaml(root); - } catch (const YAML::Exception &ex) { + } catch (const YAML::Exception& ex) { err = Error(errParams, "Error with config file '%s'. Reason: %s", filePath, ex.what()); } return err; } -Error ServerConfig::ParseCmd(int argc, char *argv[]) { +Error ServerConfig::ParseCmd(int argc, char* argv[]) { using reindexer::fs::GetDirPath; #ifndef LINK_RESOURCES WebRoot = GetDirPath(argv[0]); @@ -93,7 +93,7 @@ Error ServerConfig::ParseCmd(int argc, char *argv[]) { args::ValueFlag storageF(dbGroup, "PATH", "path to 'reindexer' storage", {'s', "db"}, StoragePath, args::Options::Single); auto availableStorageTypes = reindexer::datastorage::StorageFactory::getAvailableTypes(); std::string availabledStorages; - for (const auto &type : availableStorageTypes) { + for (const auto& type : availableStorageTypes) { if (!availabledStorages.empty()) { availabledStorages.append(", "); } @@ -192,72 +192,152 @@ Error ServerConfig::ParseCmd(int argc, char *argv[]) { try { parser.ParseCLI(argc, argv); - } catch (const args::Help &) { + } catch (const args::Help&) { return Error(errLogic, parser.Help()); - } catch (const args::Error &e) { + } catch (const args::Error& e) { return Error(errParams, "%s\n%s", e.what(), parser.Help()); } if (configF) { auto err = ParseFile(args::get(configF)); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } - if (storageF) StoragePath = args::get(storageF); - if (storageEngineF) StorageEngine = args::get(storageEngineF); - if (startWithErrorsF) StartWithErrors = args::get(startWithErrorsF); - if (autorepairF) Autorepair = args::get(autorepairF); - if (disableNamespaceLeakF) AllowNamespaceLeak = !args::get(disableNamespaceLeakF); - if (logLevelF) LogLevel = args::get(logLevelF); - if (httpAddrF) HTTPAddr = args::get(httpAddrF); - if (rpcAddrF) RPCAddr = args::get(rpcAddrF); + if (storageF) { + StoragePath = args::get(storageF); + } + if (storageEngineF) { + StorageEngine = args::get(storageEngineF); + } + if (startWithErrorsF) { + StartWithErrors = args::get(startWithErrorsF); + } + if (autorepairF) { + Autorepair = args::get(autorepairF); + } + if (disableNamespaceLeakF) { + AllowNamespaceLeak = !args::get(disableNamespaceLeakF); + } + if (logLevelF) { + LogLevel = args::get(logLevelF); + } + if (httpAddrF) { + HTTPAddr = args::get(httpAddrF); + } + if (rpcAddrF) { + RPCAddr = args::get(rpcAddrF); + } - if (rpcThreadingModeF) RPCThreadingMode = args::get(rpcThreadingModeF); - if (httpThreadingModeF) HttpThreadingMode = args::get(httpThreadingModeF); - if (webRootF) WebRoot = args::get(webRootF); - if (MaxHttpReqSizeF) MaxHttpReqSize = args::get(MaxHttpReqSizeF); + if (rpcThreadingModeF) { + RPCThreadingMode = args::get(rpcThreadingModeF); + } + if (httpThreadingModeF) { + HttpThreadingMode = args::get(httpThreadingModeF); + } + if (webRootF) { + WebRoot = args::get(webRootF); + } + if (MaxHttpReqSizeF) { + MaxHttpReqSize = args::get(MaxHttpReqSizeF); + } #ifndef _WIN32 - if (rpcUnixAddrF) RPCUnixAddr = args::get(rpcUnixAddrF); - if (rpcUnixThreadingModeF) RPCUnixThreadingMode = args::get(rpcUnixThreadingModeF); - if (userF) UserName = args::get(userF); - if (daemonizeF) Daemonize = args::get(daemonizeF); - if (daemonPidFileF) DaemonPidFile = args::get(daemonPidFileF); + if (rpcUnixAddrF) { + RPCUnixAddr = args::get(rpcUnixAddrF); + } + if (rpcUnixThreadingModeF) { + RPCUnixThreadingMode = args::get(rpcUnixThreadingModeF); + } + if (userF) { + UserName = args::get(userF); + } + if (daemonizeF) { + Daemonize = args::get(daemonizeF); + } + if (daemonPidFileF) { + DaemonPidFile = args::get(daemonPidFileF); + } #else - if (installF) InstallSvc = args::get(installF); - if (removeF) RemoveSvc = args::get(removeF); - if (serviceF) SvcMode = args::get(serviceF); + if (installF) { + InstallSvc = args::get(installF); + } + if (removeF) { + RemoveSvc = args::get(removeF); + } + if (serviceF) { + SvcMode = args::get(serviceF); + } #endif #if REINDEX_WITH_GPERFTOOLS - if (allocatorCacheLimit) AllocatorCacheLimit = args::get(allocatorCacheLimit); - if (allocatorCachePart) AllocatorCachePart = args::get(allocatorCachePart); + if (allocatorCacheLimit) { + AllocatorCacheLimit = args::get(allocatorCacheLimit); + } + if (allocatorCachePart) { + AllocatorCachePart = args::get(allocatorCachePart); + } #endif - if (securityF) EnableSecurity = args::get(securityF); + if (securityF) { + EnableSecurity = args::get(securityF); + } #if defined(WITH_GRPC) - if (grpcF) EnableGRPC = args::get(grpcF); - if (grpcAddrF) GRPCAddr = args::get(grpcAddrF); + if (grpcF) { + EnableGRPC = args::get(grpcF); + } + if (grpcAddrF) { + GRPCAddr = args::get(grpcAddrF); + } #endif - if (serverLogF) ServerLog = args::get(serverLogF); - if (coreLogF) CoreLog = args::get(coreLogF); - if (httpLogF) HttpLog = args::get(httpLogF); - if (rpcLogF) RpcLog = args::get(rpcLogF); - if (pprofF) DebugPprof = args::get(pprofF); - if (prometheusF) EnablePrometheus = args::get(prometheusF); - if (prometheusPeriodF) PrometheusCollectPeriod = std::chrono::milliseconds(args::get(prometheusPeriodF)); - if (clientsConnectionsStatF) EnableConnectionsStats = args::get(clientsConnectionsStatF); - if (httpReadTimeoutF) HttpReadTimeout = std::chrono::seconds(args::get(httpReadTimeoutF)); - if (httpWriteTimeoutF) HttpWriteTimeout = std::chrono::seconds(args::get(httpWriteTimeoutF)); - if (logAllocsF) DebugAllocs = args::get(logAllocsF); - if (txIdleTimeoutF) TxIdleTimeout = std::chrono::seconds(args::get(txIdleTimeoutF)); - if (rpcQrIdleTimeoutF) RPCQrIdleTimeout = std::chrono::seconds(args::get(rpcQrIdleTimeoutF)); - if (maxUpdatesSizeF) MaxUpdatesSize = args::get(maxUpdatesSizeF); + if (serverLogF) { + ServerLog = args::get(serverLogF); + } + if (coreLogF) { + CoreLog = args::get(coreLogF); + } + if (httpLogF) { + HttpLog = args::get(httpLogF); + } + if (rpcLogF) { + RpcLog = args::get(rpcLogF); + } + if (pprofF) { + DebugPprof = args::get(pprofF); + } + if (prometheusF) { + EnablePrometheus = args::get(prometheusF); + } + if (prometheusPeriodF) { + PrometheusCollectPeriod = std::chrono::milliseconds(args::get(prometheusPeriodF)); + } + if (clientsConnectionsStatF) { + EnableConnectionsStats = args::get(clientsConnectionsStatF); + } + if (httpReadTimeoutF) { + HttpReadTimeout = std::chrono::seconds(args::get(httpReadTimeoutF)); + } + if (httpWriteTimeoutF) { + HttpWriteTimeout = std::chrono::seconds(args::get(httpWriteTimeoutF)); + } + if (logAllocsF) { + DebugAllocs = args::get(logAllocsF); + } + if (txIdleTimeoutF) { + TxIdleTimeout = std::chrono::seconds(args::get(txIdleTimeoutF)); + } + if (rpcQrIdleTimeoutF) { + RPCQrIdleTimeout = std::chrono::seconds(args::get(rpcQrIdleTimeoutF)); + } + if (maxUpdatesSizeF) { + MaxUpdatesSize = args::get(maxUpdatesSizeF); + } return {}; } -reindexer::Error ServerConfig::fromYaml(YAML::Node &root) { +reindexer::Error ServerConfig::fromYaml(YAML::Node& root) { try { AllowNamespaceLeak = root["db"]["ns_leak"].as(AllowNamespaceLeak); StoragePath = root["storage"]["path"].as(StoragePath); @@ -298,7 +378,7 @@ reindexer::Error ServerConfig::fromYaml(YAML::Node &root) { DebugAllocs = root["debug"]["allocs"].as(DebugAllocs); DebugPprof = root["debug"]["pprof"].as(DebugPprof); - } catch (const YAML::Exception &ex) { + } catch (const YAML::Exception& ex) { return Error(errParams, "Unable to parse YML server config: %s", ex.what()); } return {}; diff --git a/cpp_src/server/dbmanager.cc b/cpp_src/server/dbmanager.cc index 8ec1e6cae..45e8046de 100644 --- a/cpp_src/server/dbmanager.cc +++ b/cpp_src/server/dbmanager.cc @@ -18,10 +18,10 @@ using namespace std::string_view_literals; const std::string kUsersYAMLFilename = "users.yml"; const std::string kUsersJSONFilename = "users.json"; -DBManager::DBManager(const std::string &dbpath, bool noSecurity, IClientsStats *clientsStats) +DBManager::DBManager(const std::string& dbpath, bool noSecurity, IClientsStats* clientsStats) : dbpath_(dbpath), noSecurity_(noSecurity), storageType_(datastorage::StorageType::LevelDB), clientsStats_(clientsStats) {} -Error DBManager::Init(const std::string &storageEngine, bool allowDBErrors, bool withAutorepair) { +Error DBManager::Init(const std::string& storageEngine, bool allowDBErrors, bool withAutorepair) { if (!noSecurity_) { auto status = readUsers(); if (!status.ok()) { @@ -36,11 +36,11 @@ Error DBManager::Init(const std::string &storageEngine, bool allowDBErrors, bool try { storageType_ = datastorage::StorageTypeFromString(storageEngine); - } catch (const Error &err) { + } catch (const Error& err) { return err; } - for (auto &de : foundDb) { + for (auto& de : foundDb) { if (de.isDir && validateObjectName(de.name, false)) { auto status = loadOrCreateDatabase(de.name, allowDBErrors, withAutorepair); if (!status.ok()) { @@ -56,13 +56,13 @@ Error DBManager::Init(const std::string &storageEngine, bool allowDBErrors, bool return {}; } -Error DBManager::OpenDatabase(const std::string &dbName, AuthContext &auth, bool canCreate) { +Error DBManager::OpenDatabase(const std::string& dbName, AuthContext& auth, bool canCreate) { RdxContext dummyCtx; auto status = Login(dbName, auth); if (!status.ok()) { return status; } - auto dbConnect = [&auth](Reindexer *db) { + auto dbConnect = [&auth](Reindexer* db) { if (auth.checkClusterID_) { return db->Connect(std::string(), ConnectOpts().WithExpectedClusterID(auth.expectedClusterID_)); } @@ -73,7 +73,9 @@ Error DBManager::OpenDatabase(const std::string &dbName, AuthContext &auth, bool auto it = dbs_.find(dbName); if (it != dbs_.end()) { status = dbConnect(it->second.get()); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } auth.db_ = it->second.get(); return errOK; } @@ -93,7 +95,9 @@ Error DBManager::OpenDatabase(const std::string &dbName, AuthContext &auth, bool it = dbs_.find(dbName); if (it != dbs_.end()) { status = dbConnect(it->second.get()); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } auth.db_ = it->second.get(); return errOK; } @@ -109,7 +113,7 @@ Error DBManager::OpenDatabase(const std::string &dbName, AuthContext &auth, bool return errOK; } -Error DBManager::loadOrCreateDatabase(const std::string &dbName, bool allowDBErrors, bool withAutorepair, const AuthContext &auth) { +Error DBManager::loadOrCreateDatabase(const std::string& dbName, bool allowDBErrors, bool withAutorepair, const AuthContext& auth) { std::string storagePath = !dbpath_.empty() ? fs::JoinPath(dbpath_, dbName) : ""; logPrintf(LogInfo, "Loading database %s", dbName); @@ -135,15 +139,15 @@ Error DBManager::loadOrCreateDatabase(const std::string &dbName, bool allowDBErr return status; } -Error DBManager::DropDatabase(AuthContext &auth) { +Error DBManager::DropDatabase(AuthContext& auth) { { - Reindexer *db = nullptr; + Reindexer* db = nullptr; auto status = auth.GetDB(kRoleOwner, &db); if (!status.ok()) { return status; } } - const std::string &dbName = auth.dbName_; + const std::string& dbName = auth.dbName_; std::unique_lock lck(mtx_); auto it = dbs_.find(dbName); @@ -162,11 +166,13 @@ std::vector DBManager::EnumDatabases() { shared_lock lck(mtx_); dbs.reserve(dbs_.size()); - for (const auto &it : dbs_) dbs.emplace_back(it.first); + for (const auto& it : dbs_) { + dbs.emplace_back(it.first); + } return dbs; } -Error DBManager::Login(const std::string &dbName, AuthContext &auth) { +Error DBManager::Login(const std::string& dbName, AuthContext& auth) { if (kRoleSystem == auth.role_) { auth.dbName_ = dbName; return {}; @@ -198,7 +204,7 @@ Error DBManager::Login(const std::string &dbName, AuthContext &auth) { auth.role_ = kRoleNone; if (!dbName.empty()) { - const UserRecord &urec = it->second; + const UserRecord& urec = it->second; auto dbIt = urec.roles.find("*"); if (dbIt != urec.roles.end()) { @@ -232,12 +238,14 @@ Error DBManager::readUsers() noexcept { Error DBManager::readUsersYAML() noexcept { std::string content; int res = fs::ReadFile(fs::JoinPath(dbpath_, kUsersYAMLFilename), content); - if (res < 0) return Error(errNotFound, "Can't read '%s' file", kUsersYAMLFilename); + if (res < 0) { + return Error(errNotFound, "Can't read '%s' file", kUsersYAMLFilename); + } try { YAML::ScannerOpts opts; opts.disableAnchors = true; YAML::Node root = YAML::Load(content, opts); - for (const auto &user : root) { + for (const auto& user : root) { UserRecord urec; urec.login = user.first.as(); auto userNode = user.second; @@ -248,11 +256,11 @@ Error DBManager::readUsersYAML() noexcept { } auto userRoles = userNode["roles"]; if (userRoles.IsMap()) { - for (const auto &role : userRoles) { + for (const auto& role : userRoles) { std::string db(role.first.as()); try { urec.roles.emplace(db, userRoleFromString(role.second.as())); - } catch (const Error &err) { + } catch (const Error& err) { logPrintf(LogWarning, "Skipping user '%s' for db '%s': ", urec.login, db, err.what()); } } @@ -265,7 +273,7 @@ Error DBManager::readUsersYAML() noexcept { logPrintf(LogWarning, "Skipping user '%s': no 'roles' node found", urec.login); } } - } catch (const YAML::Exception &ex) { + } catch (const YAML::Exception& ex) { return Error(errParseJson, "Users: %s", ex.what()); } return errOK; @@ -274,12 +282,14 @@ Error DBManager::readUsersYAML() noexcept { Error DBManager::readUsersJSON() noexcept { std::string content; int res = fs::ReadFile(fs::JoinPath(dbpath_, kUsersJSONFilename), content); - if (res < 0) return Error(errNotFound, "Can't read '%s' file", kUsersJSONFilename); + if (res < 0) { + return Error(errNotFound, "Can't read '%s' file", kUsersJSONFilename); + } try { gason::JsonParser parser; auto root = parser.Parse(giftStr(content)); - for (auto &userNode : root) { + for (auto& userNode : root) { UserRecord urec; urec.login = std::string(userNode.key); auto err = ParseMd5CryptString(userNode["hash"].As(), urec.hash, urec.salt); @@ -287,12 +297,12 @@ Error DBManager::readUsersJSON() noexcept { logPrintf(LogWarning, "Hash parsing error for user '%s': %s", urec.login, err.what()); continue; } - for (auto &roleNode : userNode["roles"]) { + for (auto& roleNode : userNode["roles"]) { std::string db(roleNode.key); try { UserRole role = userRoleFromString(roleNode.As()); urec.roles.emplace(db, role); - } catch (const Error &err) { + } catch (const Error& err) { logPrintf(LogWarning, "Skipping user '%s' for db '%s': ", urec.login, db, err.what()); } } @@ -302,7 +312,7 @@ Error DBManager::readUsersJSON() noexcept { users_.emplace(urec.login, urec); } } - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return Error(errParseJson, "Users: %s", ex.what()); } return errOK; diff --git a/cpp_src/server/dbmanager.h b/cpp_src/server/dbmanager.h index 8054f5214..1fc13b528 100644 --- a/cpp_src/server/dbmanager.h +++ b/cpp_src/server/dbmanager.h @@ -56,7 +56,7 @@ class AuthContext { /// Construct context with user credentials /// @param login - User's login /// @param password - User's password - AuthContext(const std::string &login, const std::string &password) : login_(login), password_(password) {} + AuthContext(const std::string& login, const std::string& password) : login_(login), password_(password) {} /// Set expected master's replication clusted ID /// @param clusterID - Expected cluster ID value void SetExpectedClusterID(int clusterID) noexcept { @@ -67,10 +67,11 @@ class AuthContext { /// @param role - Requested role one of UserRole enum /// @param ret - Pointer to returned database pointer /// @return Error - error object - Error GetDB(UserRole role, Reindexer **ret) noexcept { - if (role > role_) + Error GetDB(UserRole role, Reindexer** ret) noexcept { + if (role > role_) { return Error(errForbidden, "Forbidden: need role %s of db '%s' user '%s' have role=%s", UserRoleName(role), dbName_, login_, UserRoleName(role_)); + } *ret = db_; return errOK; } @@ -84,10 +85,10 @@ class AuthContext { bool HaveDB() const noexcept { return db_ != nullptr; } /// Get user login /// @return user login - const std::string &Login() const noexcept { return login_; } + const std::string& Login() const noexcept { return login_; } /// Get database name /// @return db name - const std::string &DBName() const noexcept { return dbName_; } + const std::string& DBName() const noexcept { return dbName_; } /// Get user rights /// @return user rights UserRole UserRights() const noexcept { return role_; } @@ -101,7 +102,7 @@ class AuthContext { std::string dbName_; int expectedClusterID_ = -1; bool checkClusterID_ = false; - Reindexer *db_ = nullptr; + Reindexer* db_ = nullptr; }; static inline AuthContext MakeSystemAuthContext() { return AuthContext(kRoleSystem); } @@ -113,7 +114,7 @@ class DBManager { /// @param dbpath - path to database on file system /// @param noSecurity - if true, then disable all security validations and users authentication /// @param clientsStats - object for receiving clients statistics - DBManager(const std::string &dbpath, bool noSecurity, IClientsStats *clientsStats = nullptr); + DBManager(const std::string& dbpath, bool noSecurity, IClientsStats* clientsStats = nullptr); /// Initialize database: /// Read all found databases to RAM /// Read user's database @@ -121,22 +122,22 @@ class DBManager { /// @param allowDBErrors - true: Ignore errors during existing DBs load; false: Return error if error occures during DBs load /// @param withAutorepair - true: Enable storage autorepair feature for this DB; false: Disable storage autorepair feature for this DB /// @return Error - error object - Error Init(const std::string &storageEngine, bool allowDBErrors, bool withAutorepair); + Error Init(const std::string& storageEngine, bool allowDBErrors, bool withAutorepair); /// Authenticate user, and grant roles to database with specified dbName /// @param dbName - database name. Can be empty. /// @param auth - AuthContext with user credentials /// @return Error - error object - Error Login(const std::string &dbName, AuthContext &auth); + Error Login(const std::string& dbName, AuthContext& auth); /// Open database and authentificate user /// @param dbName - database name, Can't be empty /// @param auth - AuthContext filled with user credentials or already authorized AuthContext /// @param canCreate - true: Create database, if not exists; false: return error, if database not exists /// @return Error - error object - Error OpenDatabase(const std::string &dbName, AuthContext &auth, bool canCreate); + Error OpenDatabase(const std::string& dbName, AuthContext& auth, bool canCreate); /// Drop database from disk storage and memory. Reindexer DB object will be destroyed /// @param auth - Authorized AuthContext, with valid Reindexer DB object and reasonale role /// @return Error - error object - Error DropDatabase(AuthContext &auth); + Error DropDatabase(AuthContext& auth); /// Check if security disabled /// @return bool - true: security checks are disabled; false: security checks are enabled bool IsNoSecurity() { return noSecurity_; } @@ -151,7 +152,7 @@ class DBManager { Error readUsersJSON() noexcept; Error createDefaultUsersYAML() noexcept; static UserRole userRoleFromString(std::string_view strRole); - Error loadOrCreateDatabase(const std::string &name, bool allowDBErrors, bool withAutorepair, const AuthContext &auth = AuthContext()); + Error loadOrCreateDatabase(const std::string& name, bool allowDBErrors, bool withAutorepair, const AuthContext& auth = AuthContext()); std::unordered_map, nocase_hash_str, nocase_equal_str> dbs_; std::unordered_map users_; @@ -160,7 +161,7 @@ class DBManager { bool noSecurity_; datastorage::StorageType storageType_; - IClientsStats *clientsStats_ = nullptr; + IClientsStats* clientsStats_ = nullptr; }; } // namespace reindexer_server diff --git a/cpp_src/server/grpc/reindexerservice.cc b/cpp_src/server/grpc/reindexerservice.cc index 3347373b8..23274350c 100644 --- a/cpp_src/server/grpc/reindexerservice.cc +++ b/cpp_src/server/grpc/reindexerservice.cc @@ -474,32 +474,44 @@ Error ReindexerService::buildItems(WrSerializer& wrser, const reindexer::QueryRe for (auto& item : qr) { array.Raw(nullptr, ""); status = item.GetJSON(wrser, false); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } } } if (qr.GetAggregationResults().size() > 0) { status = buildAggregation(builder, wrser, qr, opts); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } } break; } case EncodingType::MSGPACK: { int fields = 0; bool withItems = (qr.Count() > 0); - if (withItems) ++fields; + if (withItems) { + ++fields; + } bool withAggregation = (qr.GetAggregationResults().size() > 0); - if (withAggregation) ++fields; + if (withAggregation) { + ++fields; + } MsgPackBuilder builder(wrser, ObjType::TypeObject, fields); if (withItems) { MsgPackBuilder array = builder.Array("items", qr.Count()); for (auto& item : qr) { status = item.GetMsgPack(wrser, false); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } } } if (withAggregation) { status = buildAggregation(builder, wrser, qr, opts); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } } break; } @@ -508,7 +520,9 @@ Error ReindexerService::buildItems(WrSerializer& wrser, const reindexer::QueryRe ProtobufBuilder array = builder.Array("items"); for (auto& it : qr) { status = it.GetProtobuf(wrser, false); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } } break; } @@ -518,12 +532,16 @@ Error ReindexerService::buildItems(WrSerializer& wrser, const reindexer::QueryRe } for (auto& item : qr) { status = packCJSONItem(wrser, item, opts); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } auto jIt = item.GetJoined(); if (opts.withjoineditems() && jIt.getJoinedItemsCount() > 0) { wrser.PutVarUint(jIt.getJoinedItemsCount() > 0 ? jIt.getJoinedFieldsCount() : 0); - if (jIt.getJoinedItemsCount() == 0) continue; + if (jIt.getJoinedItemsCount() == 0) { + continue; + } size_t joinedField = item.qr_->joined_.size(); for (size_t ns = 0; ns < item.GetItemRef().Nsid(); ++ns) { @@ -531,13 +549,17 @@ Error ReindexerService::buildItems(WrSerializer& wrser, const reindexer::QueryRe } for (auto it = jIt.begin(); it != jIt.end(); ++it, ++joinedField) { wrser.PutVarUint(it.ItemsCount()); - if (it.ItemsCount() == 0) continue; + if (it.ItemsCount() == 0) { + continue; + } QueryResults jqr = it.ToQueryResults(); jqr.addNSContext(qr.getPayloadType(joinedField), qr.getTagsMatcher(joinedField), qr.getFieldsFilter(joinedField), qr.getSchema(joinedField)); for (size_t i = 0; i < jqr.Count(); i++) { status = packCJSONItem(wrser, jqr.begin() + i, opts); - if (!status.ok()) return status; + if (!status.ok()) { + return status; + } } } } diff --git a/cpp_src/server/httpserver.cc b/cpp_src/server/httpserver.cc index 458a396d8..bd59f8a18 100644 --- a/cpp_src/server/httpserver.cc +++ b/cpp_src/server/httpserver.cc @@ -40,8 +40,8 @@ namespace reindexer_server { constexpr size_t kTxIdLen = 20; constexpr auto kTxDeadlineCheckPeriod = std::chrono::seconds(1); -HTTPServer::HTTPServer(DBManager &dbMgr, LoggerWrapper &logger, const ServerConfig &serverConfig, Prometheus *prometheus, - IStatsWatcher *statsWatcher) +HTTPServer::HTTPServer(DBManager& dbMgr, LoggerWrapper& logger, const ServerConfig& serverConfig, Prometheus* prometheus, + IStatsWatcher* statsWatcher) : dbMgr_(dbMgr), serverConfig_(serverConfig), prometheus_(prometheus), @@ -50,7 +50,7 @@ HTTPServer::HTTPServer(DBManager &dbMgr, LoggerWrapper &logger, const ServerConf logger_(logger), startTs_(system_clock_w::now()) {} -Error HTTPServer::execSqlQueryByType(std::string_view sqlQuery, reindexer::QueryResults &res, http::Context &ctx) { +Error HTTPServer::execSqlQueryByType(std::string_view sqlQuery, reindexer::QueryResults& res, http::Context& ctx) { const auto q = reindexer::Query::FromSQL(sqlQuery); switch (q.Type()) { case QuerySelect: @@ -65,7 +65,7 @@ Error HTTPServer::execSqlQueryByType(std::string_view sqlQuery, reindexer::Query throw Error(errParams, "unknown query type %d", q.Type()); } -int HTTPServer::GetSQLQuery(http::Context &ctx) { +int HTTPServer::GetSQLQuery(http::Context& ctx) { std::string sqlQuery = urldecode2(ctx.request->params.Get("q")); std::string_view limitParam = ctx.request->params.Get("limit"); @@ -88,7 +88,7 @@ int HTTPServer::GetSQLQuery(http::Context &ctx) { return queryResults(ctx, res, true, limit, offset); } -int HTTPServer::GetSQLSuggest(http::Context &ctx) { +int HTTPServer::GetSQLSuggest(http::Context& ctx) { std::string sqlQuery = urldecode2(ctx.request->params.Get("q")); if (sqlQuery.empty()) { return jsonStatus(ctx, http::HttpStatus(http::StatusBadRequest, "Missing `q` parameter")); @@ -128,14 +128,16 @@ int HTTPServer::GetSQLSuggest(http::Context &ctx) { WrSerializer ser(ctx.writer->GetChunk()); reindexer::JsonBuilder builder(ser); auto node = builder.Array("suggests"); - for (auto &suggest : suggestions) node.Put(nullptr, suggest); + for (auto& suggest : suggestions) { + node.Put(nullptr, suggest); + } node.End(); builder.End(); return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::PostSQLQuery(http::Context &ctx) { +int HTTPServer::PostSQLQuery(http::Context& ctx) { reindexer::QueryResults res; std::string sqlQuery = ctx.body->Read(); if (!sqlQuery.length()) { @@ -149,7 +151,7 @@ int HTTPServer::PostSQLQuery(http::Context &ctx) { return queryResults(ctx, res, true); } -int HTTPServer::PostQuery(http::Context &ctx) { +int HTTPServer::PostQuery(http::Context& ctx) { auto db = getDB(ctx); reindexer::QueryResults res; std::string dsl = ctx.body->Read(); @@ -168,7 +170,7 @@ int HTTPServer::PostQuery(http::Context &ctx) { return queryResults(ctx, res, true); } -int HTTPServer::DeleteQuery(http::Context &ctx) { +int HTTPServer::DeleteQuery(http::Context& ctx) { auto db = getDB(ctx); std::string dsl = ctx.body->Read(); @@ -192,7 +194,7 @@ int HTTPServer::DeleteQuery(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::UpdateQuery(http::Context &ctx) { +int HTTPServer::UpdateQuery(http::Context& ctx) { auto db = getDB(ctx); std::string dsl = ctx.body->Read(); @@ -216,7 +218,7 @@ int HTTPServer::UpdateQuery(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::GetDatabases(http::Context &ctx) { +int HTTPServer::GetDatabases(http::Context& ctx) { std::string_view sortOrder = ctx.request->params.Get("sort_order"); auto dbs = dbMgr_.EnumDatabases(); @@ -231,7 +233,7 @@ int HTTPServer::GetDatabases(http::Context &ctx) { } if (sortDirection) { - boost::sort::pdqsort(dbs.begin(), dbs.end(), [sortDirection](const std::string &lhs, const std::string &rhs) { + boost::sort::pdqsort(dbs.begin(), dbs.end(), [sortDirection](const std::string& lhs, const std::string& rhs) { return (sortDirection > 0) ? (collateCompare(lhs, rhs, SortingPrioritiesTable()) == ComparationResult::Lt) : (collateCompare(lhs, rhs, SortingPrioritiesTable()) == ComparationResult::Gt); }); @@ -242,26 +244,28 @@ int HTTPServer::GetDatabases(http::Context &ctx) { JsonBuilder builder(ser); builder.Put("total_items", dbs.size()); auto arrNode = builder.Array("items"); - for (auto &db : dbs) arrNode.Put(nullptr, db); + for (auto& db : dbs) { + arrNode.Put(nullptr, db); + } } return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::PostDatabase(http::Context &ctx) { +int HTTPServer::PostDatabase(http::Context& ctx) { std::string newDbName = getNameFromJson(ctx.body->Read()); auto dbs = dbMgr_.EnumDatabases(); - for (auto &db : dbs) { + for (auto& db : dbs) { if (db == newDbName) { return jsonStatus(ctx, http::HttpStatus(http::StatusBadRequest, "Database already exists")); } } AuthContext dummyCtx; - AuthContext *actx = &dummyCtx; + AuthContext* actx = &dummyCtx; if (!dbMgr_.IsNoSecurity()) { - auto clientData = dynamic_cast(ctx.clientData.get()); + auto clientData = dynamic_cast(ctx.clientData.get()); assertrx(clientData); actx = &clientData->auth; // -V522 } @@ -274,13 +278,13 @@ int HTTPServer::PostDatabase(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::DeleteDatabase(http::Context &ctx) { +int HTTPServer::DeleteDatabase(http::Context& ctx) { std::string dbName(urldecode2(ctx.request->urlParams[0])); AuthContext dummyCtx; - AuthContext *actx = &dummyCtx; + AuthContext* actx = &dummyCtx; if (!dbMgr_.IsNoSecurity()) { - auto clientData = dynamic_cast(ctx.clientData.get()); + auto clientData = dynamic_cast(ctx.clientData.get()); assertrx(clientData); actx = &clientData->auth; // -V522 } @@ -304,7 +308,7 @@ int HTTPServer::DeleteDatabase(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::GetNamespaces(http::Context &ctx) { +int HTTPServer::GetNamespaces(http::Context& ctx) { std::string_view sortOrder = ctx.request->params.Get("sort_order"); std::vector nsDefs; @@ -323,7 +327,7 @@ int HTTPServer::GetNamespaces(http::Context &ctx) { } if (sortDirection) { - boost::sort::pdqsort(nsDefs.begin(), nsDefs.end(), [sortDirection](const NamespaceDef &lhs, const NamespaceDef &rhs) { + boost::sort::pdqsort(nsDefs.begin(), nsDefs.end(), [sortDirection](const NamespaceDef& lhs, const NamespaceDef& rhs) { return (sortDirection > 0) ? (collateCompare(lhs.name, rhs.name, SortingPrioritiesTable()) == ComparationResult::Lt) : (collateCompare(lhs.name, rhs.name, SortingPrioritiesTable()) == ComparationResult::Gt); @@ -335,7 +339,7 @@ int HTTPServer::GetNamespaces(http::Context &ctx) { JsonBuilder builder(ser); builder.Put("total_items", nsDefs.size()); auto arrNode = builder.Array("items"); - for (auto &nsDef : nsDefs) { + for (auto& nsDef : nsDefs) { auto objNode = arrNode.Object(nullptr); objNode.Put("name", nsDef.name); } @@ -343,7 +347,7 @@ int HTTPServer::GetNamespaces(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::GetNamespace(http::Context &ctx) { +int HTTPServer::GetNamespace(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -367,7 +371,7 @@ int HTTPServer::GetNamespace(http::Context &ctx) { return ctx.JSON(http::StatusOK, wrSer.DetachChunk()); } -int HTTPServer::PostNamespace(http::Context &ctx) { +int HTTPServer::PostNamespace(http::Context& ctx) { auto db = getDB(ctx); reindexer::NamespaceDef nsdef(""); @@ -385,7 +389,7 @@ int HTTPServer::PostNamespace(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::DeleteNamespace(http::Context &ctx) { +int HTTPServer::DeleteNamespace(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -403,7 +407,7 @@ int HTTPServer::DeleteNamespace(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::TruncateNamespace(http::Context &ctx) { +int HTTPServer::TruncateNamespace(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -421,7 +425,7 @@ int HTTPServer::TruncateNamespace(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::RenameNamespace(http::Context &ctx) { +int HTTPServer::RenameNamespace(http::Context& ctx) { auto db = getDB(ctx); std::string srcNsName = urldecode2(ctx.request->urlParams[1]); std::string dstNsName = urldecode2(ctx.request->urlParams[2]); @@ -444,7 +448,7 @@ int HTTPServer::RenameNamespace(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::GetItems(http::Context &ctx) { +int HTTPServer::GetItems(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -499,13 +503,13 @@ int HTTPServer::GetItems(http::Context &ctx) { return queryResults(ctx, res); } -int HTTPServer::DeleteItems(http::Context &ctx) { return modifyItems(ctx, ModeDelete); } +int HTTPServer::DeleteItems(http::Context& ctx) { return modifyItems(ctx, ModeDelete); } -int HTTPServer::PutItems(http::Context &ctx) { return modifyItems(ctx, ModeUpdate); } -int HTTPServer::PostItems(http::Context &ctx) { return modifyItems(ctx, ModeInsert); } -int HTTPServer::PatchItems(http::Context &ctx) { return modifyItems(ctx, ModeUpsert); } +int HTTPServer::PutItems(http::Context& ctx) { return modifyItems(ctx, ModeUpdate); } +int HTTPServer::PostItems(http::Context& ctx) { return modifyItems(ctx, ModeInsert); } +int HTTPServer::PatchItems(http::Context& ctx) { return modifyItems(ctx, ModeUpsert); } -int HTTPServer::GetMetaList(http::Context &ctx) { +int HTTPServer::GetMetaList(http::Context& ctx) { auto db = getDB(ctx); const std::string nsName = urldecode2(ctx.request->urlParams[1]); if (!nsName.length()) { @@ -569,7 +573,9 @@ int HTTPServer::GetMetaList(http::Context &ctx) { if (withValues) { std::string value; const Error err = db.GetMeta(nsName, *keysIt, value); - if (!err.ok()) return jsonStatus(ctx, http::HttpStatus(err)); + if (!err.ok()) { + return jsonStatus(ctx, http::HttpStatus(err)); + } objNode.Put("value", escapeString(value)); } objNode.End(); @@ -580,7 +586,7 @@ int HTTPServer::GetMetaList(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::GetMetaByKey(http::Context &ctx) { +int HTTPServer::GetMetaByKey(http::Context& ctx) { auto db = getDB(ctx); const std::string nsName = urldecode2(ctx.request->urlParams[1]); const std::string key = urldecode2(ctx.request->urlParams[2]); @@ -600,7 +606,7 @@ int HTTPServer::GetMetaByKey(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::PutMetaByKey(http::Context &ctx) { +int HTTPServer::PutMetaByKey(http::Context& ctx) { auto db = getDB(ctx); const std::string nsName = urldecode2(ctx.request->urlParams[1]); if (!nsName.length()) { @@ -616,13 +622,13 @@ int HTTPServer::PutMetaByKey(http::Context &ctx) { if (!err.ok()) { return jsonStatus(ctx, http::HttpStatus(err)); } - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { return jsonStatus(ctx, http::HttpStatus(Error(errParseJson, "Meta: %s", ex.what()))); } return jsonStatus(ctx); } -int HTTPServer::DeleteMetaByKey(http::Context &ctx) { +int HTTPServer::DeleteMetaByKey(http::Context& ctx) { auto db = getDB(ctx); const std::string nsName = urldecode2(ctx.request->urlParams[1]); const std::string keyName = urldecode2(ctx.request->urlParams[2]); @@ -637,7 +643,7 @@ int HTTPServer::DeleteMetaByKey(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::GetIndexes(http::Context &ctx) { +int HTTPServer::GetIndexes(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -660,7 +666,7 @@ int HTTPServer::GetIndexes(http::Context &ctx) { JsonBuilder builder(ser); builder.Put("total_items", nsDefs[0].indexes.size()); auto arrNode = builder.Array("items"); - for (auto &idxDef : nsDefs[0].indexes) { + for (auto& idxDef : nsDefs[0].indexes) { arrNode.Raw(nullptr, ""); idxDef.GetJSON(ser); } @@ -668,7 +674,7 @@ int HTTPServer::GetIndexes(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::PostIndex(http::Context &ctx) { +int HTTPServer::PostIndex(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -692,9 +698,9 @@ int HTTPServer::PostIndex(http::Context &ctx) { } if (!nsDefs.empty()) { - auto &indexes = nsDefs[0].indexes; + auto& indexes = nsDefs[0].indexes; auto foundIndexIt = - std::find_if(indexes.begin(), indexes.end(), [&newIdxName](const IndexDef &idx) { return idx.name_ == newIdxName; }); + std::find_if(indexes.begin(), indexes.end(), [&newIdxName](const IndexDef& idx) { return idx.name_ == newIdxName; }); if (foundIndexIt != indexes.end()) { return jsonStatus(ctx, http::HttpStatus(http::StatusBadRequest, "Index already exists")); } @@ -708,7 +714,7 @@ int HTTPServer::PostIndex(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::PutIndex(http::Context &ctx) { +int HTTPServer::PutIndex(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -729,7 +735,7 @@ int HTTPServer::PutIndex(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::PutSchema(http::Context &ctx) { +int HTTPServer::PutSchema(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -745,7 +751,7 @@ int HTTPServer::PutSchema(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::GetSchema(http::Context &ctx) { +int HTTPServer::GetSchema(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -762,11 +768,11 @@ int HTTPServer::GetSchema(http::Context &ctx) { return ctx.JSON(http::StatusOK, schema.length() ? schema : "{}"sv); } -int HTTPServer::GetProtobufSchema(http::Context &ctx) { +int HTTPServer::GetProtobufSchema(http::Context& ctx) { Reindexer db = getDB(ctx); std::vector nses; - for (auto &p : ctx.request->params) { + for (auto& p : ctx.request->params) { if (p.name == "ns"sv || p.name == "ns[]"sv) { nses.emplace_back(urldecode2(p.val)); } @@ -774,12 +780,14 @@ int HTTPServer::GetProtobufSchema(http::Context &ctx) { WrSerializer ser; Error err = db.GetProtobufSchema(ser, nses); - if (!err.ok()) return jsonStatus(ctx, http::HttpStatus(err)); + if (!err.ok()) { + return jsonStatus(ctx, http::HttpStatus(err)); + } return ctx.String(http::StatusOK, ser.Slice()); } -int HTTPServer::DeleteIndex(http::Context &ctx) { +int HTTPServer::DeleteIndex(http::Context& ctx) { auto db = getDB(ctx); std::string nsName = urldecode2(ctx.request->urlParams[1]); @@ -801,7 +809,7 @@ int HTTPServer::DeleteIndex(http::Context &ctx) { return jsonStatus(ctx); } -int HTTPServer::Check(http::Context &ctx) { +int HTTPServer::Check(http::Context& ctx) { WrSerializer ser(ctx.writer->GetChunk()); { JsonBuilder builder(ser); @@ -879,7 +887,7 @@ int HTTPServer::Check(http::Context &ctx) { return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::DocHandler(http::Context &ctx) { +int HTTPServer::DocHandler(http::Context& ctx) { std::string path(ctx.request->path.substr(1)); bool endsWithSlash = (path.length() > 0 && path.back() == '/'); @@ -911,7 +919,9 @@ int HTTPServer::DocHandler(http::Context &ctx) { } auto pos = path.find_last_of('/'); - if (pos == std::string::npos) break; + if (pos == std::string::npos) { + break; + } path = path.erase(pos); } @@ -919,13 +929,13 @@ int HTTPServer::DocHandler(http::Context &ctx) { return NotFoundHandler(ctx); } -int HTTPServer::NotFoundHandler(http::Context &ctx) { +int HTTPServer::NotFoundHandler(http::Context& ctx) { http::HttpStatus httpStatus(http::StatusNotFound, "Not found"); return jsonStatus(ctx, httpStatus); } -bool HTTPServer::Start(const std::string &addr, ev::dynamic_loop &loop) { +bool HTTPServer::Start(const std::string& addr, ev::dynamic_loop& loop) { router_.NotFound(this); router_.GET("/", this); @@ -1017,7 +1027,7 @@ bool HTTPServer::Start(const std::string &addr, ev::dynamic_loop &loop) { return listener_->Bind(addr, socket_domain::tcp); } -Error HTTPServer::modifyItem(Reindexer &db, std::string &nsName, Item &item, ItemModifyMode mode) { +Error HTTPServer::modifyItem(Reindexer& db, std::string& nsName, Item& item, ItemModifyMode mode) { Error status; switch (mode) { case ModeUpsert: @@ -1036,7 +1046,7 @@ Error HTTPServer::modifyItem(Reindexer &db, std::string &nsName, Item &item, Ite return status; } -Error HTTPServer::modifyItem(Reindexer &db, std::string &nsName, Item &item, QueryResults &qr, ItemModifyMode mode) { +Error HTTPServer::modifyItem(Reindexer& db, std::string& nsName, Item& item, QueryResults& qr, ItemModifyMode mode) { Error status; switch (mode) { case ModeUpsert: @@ -1055,21 +1065,21 @@ Error HTTPServer::modifyItem(Reindexer &db, std::string &nsName, Item &item, Que return status; } -int HTTPServer::modifyItemsJSON(http::Context &ctx, std::string &nsName, const std::vector &precepts, ItemModifyMode mode) { +int HTTPServer::modifyItemsJSON(http::Context& ctx, std::string& nsName, const std::vector& precepts, ItemModifyMode mode) { auto db = getDB(ctx); std::string itemJson = ctx.body->Read(); int cnt = 0; std::vector updatedItems; if (itemJson.size()) { - char *jsonPtr = &itemJson[0]; + char* jsonPtr = &itemJson[0]; size_t jsonLeft = itemJson.size(); while (jsonPtr && *jsonPtr) { Item item = db.NewItem(nsName); if (!item.Status().ok()) { return jsonStatus(ctx, http::HttpStatus(item.Status())); } - char *prevPtr = jsonPtr; + char* prevPtr = jsonPtr; auto str = std::string_view(jsonPtr, jsonLeft); if (jsonPtr != &itemJson[0] && isBlank(str)) { break; @@ -1090,7 +1100,9 @@ int HTTPServer::modifyItemsJSON(http::Context &ctx, std::string &nsName, const s if (item.GetID() != -1) { ++cnt; - if (!precepts.empty()) updatedItems.emplace_back(item.GetJSON()); + if (!precepts.empty()) { + updatedItems.emplace_back(item.GetJSON()); + } } } } @@ -1101,7 +1113,9 @@ int HTTPServer::modifyItemsJSON(http::Context &ctx, std::string &nsName, const s builder.Put(kParamSuccess, true); if (!precepts.empty()) { auto itemsArray = builder.Array(kParamItems); - for (const std::string &item : updatedItems) itemsArray.Raw(nullptr, item); + for (const std::string& item : updatedItems) { + itemsArray.Raw(nullptr, item); + } itemsArray.End(); } builder.End(); @@ -1109,7 +1123,7 @@ int HTTPServer::modifyItemsJSON(http::Context &ctx, std::string &nsName, const s return ctx.JSON(http::StatusOK, ser.DetachChunk()); } -int HTTPServer::modifyItemsMsgPack(http::Context &ctx, std::string &nsName, const std::vector &precepts, ItemModifyMode mode) { +int HTTPServer::modifyItemsMsgPack(http::Context& ctx, std::string& nsName, const std::vector& precepts, ItemModifyMode mode) { QueryResults qr; int totalItems = 0; @@ -1121,10 +1135,14 @@ int HTTPServer::modifyItemsMsgPack(http::Context &ctx, std::string &nsName, cons while (offset < length) { Item item = db.NewItem(nsName); - if (!item.Status().ok()) return msgpackStatus(ctx, http::HttpStatus(item.Status())); + if (!item.Status().ok()) { + return msgpackStatus(ctx, http::HttpStatus(item.Status())); + } Error status = item.FromMsgPack(std::string_view(sbuffer.data(), sbuffer.size()), offset); - if (!status.ok()) return msgpackStatus(ctx, http::HttpStatus(status)); + if (!status.ok()) { + return msgpackStatus(ctx, http::HttpStatus(status)); + } item.SetPrecepts(precepts); if (!precepts.empty()) { @@ -1132,7 +1150,9 @@ int HTTPServer::modifyItemsMsgPack(http::Context &ctx, std::string &nsName, cons } else { status = modifyItem(db, nsName, item, mode); } - if (!status.ok()) return msgpackStatus(ctx, http::HttpStatus(status)); + if (!status.ok()) { + return msgpackStatus(ctx, http::HttpStatus(status)); + } if (item.GetID() != -1) { ++totalItems; @@ -1145,7 +1165,7 @@ int HTTPServer::modifyItemsMsgPack(http::Context &ctx, std::string &nsName, cons msgpackBuilder.Put(kParamSuccess, true); if (!precepts.empty()) { auto itemsArray = msgpackBuilder.Array(kParamItems, qr.Count()); - for (auto &it : qr) { + for (auto& it : qr) { const auto err = it.GetMsgPack(wrSer, false); if (!err.ok()) { return msgpackStatus(ctx, http::HttpStatus(err)); @@ -1157,12 +1177,12 @@ int HTTPServer::modifyItemsMsgPack(http::Context &ctx, std::string &nsName, cons return ctx.MSGPACK(http::StatusOK, wrSer.DetachChunk()); } -int HTTPServer::modifyItemsProtobuf(http::Context &ctx, std::string &nsName, const std::vector &precepts, +int HTTPServer::modifyItemsProtobuf(http::Context& ctx, std::string& nsName, const std::vector& precepts, ItemModifyMode mode) { WrSerializer wrSer(ctx.writer->GetChunk()); ProtobufBuilder builder(&wrSer); - auto sendResponse = [&](int items, const Error &err) { + auto sendResponse = [&](int items, const Error& err) { if (err.ok()) { builder.Put(kProtoModifyResultsFields.at(kParamUpdated), int(items)); builder.Put(kProtoModifyResultsFields.at(kParamSuccess), err.ok()); @@ -1175,15 +1195,21 @@ int HTTPServer::modifyItemsProtobuf(http::Context &ctx, std::string &nsName, con auto db = getDB(ctx); Item item = db.NewItem(nsName); - if (!item.Status().ok()) return sendResponse(0, item.Status()); + if (!item.Status().ok()) { + return sendResponse(0, item.Status()); + } std::string sbuffer = ctx.body->Read(); Error status = item.FromProtobuf(std::string_view(sbuffer.data(), sbuffer.size())); - if (!status.ok()) return sendResponse(0, status); + if (!status.ok()) { + return sendResponse(0, status); + } item.SetPrecepts(precepts); status = modifyItem(db, nsName, item, mode); - if (!status.ok()) return sendResponse(0, item.Status()); + if (!status.ok()) { + return sendResponse(0, item.Status()); + } int totalItems = 0; if (item.GetID() != -1) { @@ -1198,11 +1224,11 @@ int HTTPServer::modifyItemsProtobuf(http::Context &ctx, std::string &nsName, con return sendResponse(totalItems, item.Status()); } -int HTTPServer::modifyItemsTxJSON(http::Context &ctx, Transaction &tx, const std::vector &precepts, ItemModifyMode mode) { +int HTTPServer::modifyItemsTxJSON(http::Context& ctx, Transaction& tx, const std::vector& precepts, ItemModifyMode mode) { std::string itemJson = ctx.body->Read(); if (itemJson.size()) { - char *jsonPtr = &itemJson[0]; + char* jsonPtr = &itemJson[0]; size_t jsonLeft = itemJson.size(); while (jsonPtr && *jsonPtr) { Item item = tx.NewItem(); @@ -1210,7 +1236,7 @@ int HTTPServer::modifyItemsTxJSON(http::Context &ctx, Transaction &tx, const std http::HttpStatus httpStatus(item.Status()); return jsonStatus(ctx, httpStatus); } - char *prevPtr = jsonPtr; + char* prevPtr = jsonPtr; auto str = std::string_view(jsonPtr, jsonLeft); if (jsonPtr != &itemJson[0] && isBlank(str)) { break; @@ -1231,17 +1257,21 @@ int HTTPServer::modifyItemsTxJSON(http::Context &ctx, Transaction &tx, const std return jsonStatus(ctx); } -int HTTPServer::modifyItemsTxMsgPack(http::Context &ctx, Transaction &tx, const std::vector &precepts, ItemModifyMode mode) { +int HTTPServer::modifyItemsTxMsgPack(http::Context& ctx, Transaction& tx, const std::vector& precepts, ItemModifyMode mode) { std::string sbuffer = ctx.body->Read(); size_t length = sbuffer.size(); size_t offset = 0; while (offset < length) { Item item = tx.NewItem(); - if (!item.Status().ok()) return msgpackStatus(ctx, http::HttpStatus(item.Status())); + if (!item.Status().ok()) { + return msgpackStatus(ctx, http::HttpStatus(item.Status())); + } Error status = item.FromMsgPack(std::string_view(sbuffer.data(), sbuffer.size()), offset); - if (!status.ok()) return msgpackStatus(ctx, http::HttpStatus(status)); + if (!status.ok()) { + return msgpackStatus(ctx, http::HttpStatus(status)); + } item.SetPrecepts(precepts); tx.Modify(std::move(item), mode); @@ -1250,14 +1280,14 @@ int HTTPServer::modifyItemsTxMsgPack(http::Context &ctx, Transaction &tx, const return msgpackStatus(ctx); } -int HTTPServer::modifyItems(http::Context &ctx, ItemModifyMode mode) { +int HTTPServer::modifyItems(http::Context& ctx, ItemModifyMode mode) { std::string nsName = urldecode2(ctx.request->urlParams[1]); if (nsName.empty()) { return jsonStatus(ctx, http::HttpStatus(http::StatusBadRequest, "Namespace is not specified")); } std::vector precepts; - for (auto &p : ctx.request->params) { + for (auto& p : ctx.request->params) { if (p.name == "precepts"sv) { precepts.emplace_back(urldecode2(p.val)); } @@ -1273,7 +1303,7 @@ int HTTPServer::modifyItems(http::Context &ctx, ItemModifyMode mode) { } } -int HTTPServer::modifyItemsTx(http::Context &ctx, ItemModifyMode mode) { +int HTTPServer::modifyItemsTx(http::Context& ctx, ItemModifyMode mode) { std::string dbName; auto db = getDB(ctx, &dbName); std::string txId = urldecode2(ctx.request->urlParams[1]); @@ -1282,7 +1312,7 @@ int HTTPServer::modifyItemsTx(http::Context &ctx, ItemModifyMode mode) { } std::vector precepts; - for (auto &p : ctx.request->params) { + for (auto& p : ctx.request->params) { if (p.name == "precepts"sv) { precepts.emplace_back(urldecode2(p.val)); } @@ -1293,7 +1323,7 @@ int HTTPServer::modifyItemsTx(http::Context &ctx, ItemModifyMode mode) { return format == "msgpack"sv ? modifyItemsTxMsgPack(ctx, *tx, precepts, mode) : modifyItemsTxJSON(ctx, *tx, precepts, mode); } -int HTTPServer::queryResultsJSON(http::Context &ctx, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, +int HTTPServer::queryResultsJSON(http::Context& ctx, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset, bool withColumns, int width) { WrSerializer wrSer(ctx.writer->GetChunk()); JsonBuilder builder(wrSer); @@ -1337,7 +1367,9 @@ int HTTPServer::queryResultsJSON(http::Context &ctx, const reindexer::QueryResul } } - if (i == offset) wrSer.Reserve(wrSer.Len() * (std::min(limit, unsigned(res.Count() - offset)) + 1)); + if (i == offset) { + wrSer.Reserve(wrSer.Len() * (std::min(limit, unsigned(res.Count() - offset)) + 1)); + } } iarray.End(); @@ -1354,7 +1386,7 @@ int HTTPServer::queryResultsJSON(http::Context &ctx, const reindexer::QueryResul return ctx.JSON(http::StatusOK, wrSer.DetachChunk()); } -int HTTPServer::queryResultsCSV(http::Context &ctx, reindexer::QueryResults &res, unsigned limit, unsigned offset) { +int HTTPServer::queryResultsCSV(http::Context& ctx, reindexer::QueryResults& res, unsigned limit, unsigned offset) { if (res.GetAggregationResults().size()) { throw Error(errForbidden, "Aggregations are not supported in CSV"); } @@ -1363,7 +1395,7 @@ int HTTPServer::queryResultsCSV(http::Context &ctx, reindexer::QueryResults &res } const size_t kChunkMaxSize = 0x1000; - auto createChunk = [](const WrSerializer &from, WrSerializer &to) { + auto createChunk = [](const WrSerializer& from, WrSerializer& to) { char szBuf[64]; size_t l = u32toax(from.Len(), szBuf) - szBuf; to << std::string_view(szBuf, l); @@ -1372,8 +1404,8 @@ int HTTPServer::queryResultsCSV(http::Context &ctx, reindexer::QueryResults &res to << "\r\n"; }; - auto createCSVHeaders = [&res](WrSerializer &ser, const CsvOrdering &ordering) { - const auto &tm = res.getTagsMatcher(0); + auto createCSVHeaders = [&res](WrSerializer& ser, const CsvOrdering& ordering) { + const auto& tm = res.getTagsMatcher(0); for (auto it = ordering.begin(); it != ordering.end(); ++it) { if (it != ordering.begin()) { ser << ','; @@ -1416,16 +1448,26 @@ int HTTPServer::queryResultsCSV(http::Context &ctx, reindexer::QueryResults &res return ctx.CSV(http::StatusOK, wrSerRes.DetachChunk()); } -int HTTPServer::queryResultsMsgPack(http::Context &ctx, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, +int HTTPServer::queryResultsMsgPack(http::Context& ctx, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset, bool withColumns, int width) { int paramsToSend = 3; bool withTotalItems = (!isQueryResults || limit != kDefaultLimit); - if (!res.aggregationResults.empty()) ++paramsToSend; - if (!res.GetExplainResults().empty()) ++paramsToSend; - if (withTotalItems) ++paramsToSend; - if (withColumns) ++paramsToSend; + if (!res.aggregationResults.empty()) { + ++paramsToSend; + } + if (!res.GetExplainResults().empty()) { + ++paramsToSend; + } + if (withTotalItems) { + ++paramsToSend; + } + if (withColumns) { + ++paramsToSend; + } if (isQueryResults && res.totalCount) { - if (limit == kDefaultLimit) ++paramsToSend; + if (limit == kDefaultLimit) { + ++paramsToSend; + } ++paramsToSend; } @@ -1454,7 +1496,7 @@ int HTTPServer::queryResultsMsgPack(http::Context &ctx, const reindexer::QueryRe return ctx.MSGPACK(http::StatusOK, wrSer.DetachChunk()); } -int HTTPServer::queryResultsProtobuf(http::Context &ctx, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, +int HTTPServer::queryResultsProtobuf(http::Context& ctx, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset, bool withColumns, int width) { WrSerializer wrSer(ctx.writer->GetChunk()); ProtobufBuilder protobufBuilder(&wrSer); @@ -1505,10 +1547,10 @@ int HTTPServer::queryResultsProtobuf(http::Context &ctx, const reindexer::QueryR if (withColumns) { reindexer::TableCalculator tableCalculator(res, width, limit); - auto &header = tableCalculator.GetHeader(); - auto &columnsSettings = tableCalculator.GetColumnsSettings(); + auto& header = tableCalculator.GetHeader(); + auto& columnsSettings = tableCalculator.GetColumnsSettings(); for (auto it = header.begin(); it != header.end(); ++it) { - ColumnData &data = columnsSettings[*it]; + ColumnData& data = columnsSettings[*it]; auto parameteresObj = protobufBuilder.Object(kProtoQueryResultsFields.at(kParamColumns)); parameteresObj.Put(kProtoColumnsFields.at(kParamName), *it); parameteresObj.Put(kProtoColumnsFields.at(kParamWidthPercents), data.widthTerminalPercentage); @@ -1523,7 +1565,7 @@ int HTTPServer::queryResultsProtobuf(http::Context &ctx, const reindexer::QueryR } template -void HTTPServer::queryResultParams(Builder &builder, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, +void HTTPServer::queryResultParams(Builder& builder, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, bool withColumns, int width) { h_vector namespaces(res.GetNamespaces()); auto namespacesArray = builder.Array(kParamNamespaces, namespaces.size()); @@ -1551,11 +1593,11 @@ void HTTPServer::queryResultParams(Builder &builder, const reindexer::QueryResul if (withColumns) { reindexer::TableCalculator tableCalculator(res, width, limit); - auto &header = tableCalculator.GetHeader(); - auto &columnsSettings = tableCalculator.GetColumnsSettings(); + auto& header = tableCalculator.GetHeader(); + auto& columnsSettings = tableCalculator.GetColumnsSettings(); auto headerArray = builder.Array(kParamColumns, header.size()); for (auto it = header.begin(); it != header.end(); ++it) { - ColumnData &data = columnsSettings[*it]; + ColumnData& data = columnsSettings[*it]; auto parameteresObj = headerArray.Object(nullptr, 4); parameteresObj.Put(kParamName, *it); parameteresObj.Put(kParamWidthPercents, data.widthTerminalPercentage); @@ -1565,7 +1607,7 @@ void HTTPServer::queryResultParams(Builder &builder, const reindexer::QueryResul } } -int HTTPServer::queryResults(http::Context &ctx, reindexer::QueryResults &res, bool isQueryResults, unsigned limit, unsigned offset) { +int HTTPServer::queryResults(http::Context& ctx, reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset) { std::string_view widthParam = ctx.request->params.Get("width"sv); int width = widthParam.empty() ? 0 : stoi(widthParam); @@ -1588,7 +1630,7 @@ int HTTPServer::queryResults(http::Context &ctx, reindexer::QueryResults &res, b } } -int HTTPServer::status(http::Context &ctx, const http::HttpStatus &status) { +int HTTPServer::status(http::Context& ctx, const http::HttpStatus& status) { std::string_view format = ctx.request->params.Get("format"sv); if (format == "msgpack"sv) { return msgpackStatus(ctx, status); @@ -1599,7 +1641,7 @@ int HTTPServer::status(http::Context &ctx, const http::HttpStatus &status) { } } -int HTTPServer::msgpackStatus(http::Context &ctx, const http::HttpStatus &status) { +int HTTPServer::msgpackStatus(http::Context& ctx, const http::HttpStatus& status) { WrSerializer wrSer(ctx.writer->GetChunk()); MsgPackBuilder msgpackBuilder(wrSer, ObjType::TypeObject, 3); msgpackBuilder.Put(kParamSuccess, status.code == http::StatusOK); @@ -1609,7 +1651,7 @@ int HTTPServer::msgpackStatus(http::Context &ctx, const http::HttpStatus &status return ctx.MSGPACK(status.code, wrSer.DetachChunk()); } -int HTTPServer::jsonStatus(http::Context &ctx, const http::HttpStatus &status) { +int HTTPServer::jsonStatus(http::Context& ctx, const http::HttpStatus& status) { WrSerializer ser(ctx.writer->GetChunk()); JsonBuilder builder(ser); builder.Put(kParamSuccess, status.code == http::StatusOK); @@ -1619,7 +1661,7 @@ int HTTPServer::jsonStatus(http::Context &ctx, const http::HttpStatus &status) { return ctx.JSON(status.code, ser.DetachChunk()); } -int HTTPServer::protobufStatus(http::Context &ctx, const http::HttpStatus &status) { +int HTTPServer::protobufStatus(http::Context& ctx, const http::HttpStatus& status) { WrSerializer ser(ctx.writer->GetChunk()); ProtobufBuilder builder(&ser); builder.Put(kProtoErrorResultsFields.at(kParamSuccess), status.code == http::StatusOK); @@ -1634,7 +1676,9 @@ unsigned HTTPServer::prepareLimit(std::string_view limitParam, int limitDefault) if (limitParam.length()) { limit = stoi(limitParam); - if (limit < 0) limit = 0; + if (limit < 0) { + limit = 0; + } } return static_cast(limit); @@ -1645,13 +1689,15 @@ unsigned HTTPServer::prepareOffset(std::string_view offsetParam, int offsetDefau if (offsetParam.length()) { offset = stoi(offsetParam); - if (offset < 0) offset = 0; + if (offset < 0) { + offset = 0; + } } return static_cast(offset); } -int HTTPServer::modifyQueryTxImpl(http::Context &ctx, const std::string &dbName, std::string_view txId, Query &q) { +int HTTPServer::modifyQueryTxImpl(http::Context& ctx, const std::string& dbName, std::string_view txId, Query& q) { reindexer::QueryResults res; auto tx = getTx(dbName, txId); if (!q.GetMergeQueries().empty()) { @@ -1665,17 +1711,17 @@ int HTTPServer::modifyQueryTxImpl(http::Context &ctx, const std::string &dbName, } template -Reindexer HTTPServer::getDB(http::Context &ctx, std::string *dbNameOut) { +Reindexer HTTPServer::getDB(http::Context& ctx, std::string* dbNameOut) { (void)ctx; - Reindexer *db = nullptr; + Reindexer* db = nullptr; std::string dbName(urldecode2(ctx.request->urlParams[0])); AuthContext dummyCtx; - AuthContext *actx = &dummyCtx; + AuthContext* actx = &dummyCtx; if (!dbMgr_.IsNoSecurity()) { - auto clientData = dynamic_cast(ctx.clientData.get()); + auto clientData = dynamic_cast(ctx.clientData.get()); assertrx(clientData); actx = &clientData->auth; // -V522 } @@ -1721,12 +1767,12 @@ std::string HTTPServer::getNameFromJson(std::string_view json) { gason::JsonParser parser; auto root = parser.Parse(json); return root["name"].As(); - } catch (const gason::Exception &ex) { + } catch (const gason::Exception& ex) { throw Error(errParseJson, "getNameFromJson: %s", ex.what()); } } -std::shared_ptr HTTPServer::getTx(const std::string &dbName, std::string_view txId) { +std::shared_ptr HTTPServer::getTx(const std::string& dbName, std::string_view txId) { std::lock_guard lck(txMtx_); auto found = txMap_.find(txId); if (found == txMap_.end()) { @@ -1739,7 +1785,7 @@ std::shared_ptr HTTPServer::getTx(const std::string &dbName, std::s return found.value().tx; } -std::string HTTPServer::addTx(std::string dbName, Transaction &&tx) { +std::string HTTPServer::addTx(std::string dbName, Transaction&& tx) { const auto now = TxDeadlineClock::now_coarse(); auto ts = std::chrono::duration_cast(now.time_since_epoch()); std::string txId = randStringAlph(kTxIdLen) + "_" + std::to_string(ts.count()); @@ -1756,7 +1802,7 @@ std::string HTTPServer::addTx(std::string dbName, Transaction &&tx) { return txId; } -void HTTPServer::removeTx(const std::string &dbName, std::string_view txId) { +void HTTPServer::removeTx(const std::string& dbName, std::string_view txId) { std::lock_guard lck(txMtx_); auto found = txMap_.find(txId); if (found == txMap_.end() || !iequals(found.value().dbName, dbName)) { @@ -1774,7 +1820,7 @@ void HTTPServer::removeExpiredTx() { auto ctx = MakeSystemAuthContext(); auto status = dbMgr_.OpenDatabase(it->second.dbName, ctx, false); if (status.ok()) { - reindexer::Reindexer *db = nullptr; + reindexer::Reindexer* db = nullptr; status = ctx.GetDB(kRoleSystem, &db); if (db && status.ok()) { logger_.info("Rollback tx {} on idle deadline", it->first); @@ -1788,7 +1834,7 @@ void HTTPServer::removeExpiredTx() { } } -int HTTPServer::CheckAuth(http::Context &ctx) { +int HTTPServer::CheckAuth(http::Context& ctx) { (void)ctx; if (dbMgr_.IsNoSecurity()) { return 0; @@ -1803,10 +1849,12 @@ int HTTPServer::CheckAuth(http::Context &ctx) { } h_vector credVec(authHeader.length()); - char *credBuf = &credVec.front(); + char* credBuf = &credVec.front(); Base64decode(credBuf, authHeader.data() + 6); - char *password = strchr(credBuf, ':'); - if (password != nullptr) *password++ = 0; + char* password = strchr(credBuf, ':'); + if (password != nullptr) { + *password++ = 0; + } AuthContext auth(credBuf, password ? password : ""); auto status = dbMgr_.Login("", auth); @@ -1822,7 +1870,7 @@ int HTTPServer::CheckAuth(http::Context &ctx) { return 0; } -int HTTPServer::BeginTx(http::Context &ctx) { +int HTTPServer::BeginTx(http::Context& ctx) { std::string nsName = urldecode2(ctx.request->urlParams[1]); if (nsName.empty()) { return jsonStatus(ctx, http::HttpStatus(http::StatusBadRequest, "Namespace is not specified")); @@ -1849,7 +1897,7 @@ int HTTPServer::BeginTx(http::Context &ctx) { } } -int HTTPServer::CommitTx(http::Context &ctx) { +int HTTPServer::CommitTx(http::Context& ctx) { std::string txId = urldecode2(ctx.request->urlParams[1]); if (txId.empty()) { return status(ctx, http::HttpStatus(http::StatusBadRequest, "Tx ID is not specified")); @@ -1867,7 +1915,7 @@ int HTTPServer::CommitTx(http::Context &ctx) { return queryResults(ctx, qr); } -int HTTPServer::RollbackTx(http::Context &ctx) { +int HTTPServer::RollbackTx(http::Context& ctx) { std::string txId = urldecode2(ctx.request->urlParams[1]); if (txId.empty()) { return status(ctx, http::HttpStatus(http::StatusBadRequest, "Tx ID is not specified")); @@ -1885,15 +1933,15 @@ int HTTPServer::RollbackTx(http::Context &ctx) { return status(ctx); } -int HTTPServer::PostItemsTx(http::Context &ctx) { return modifyItemsTx(ctx, ModeInsert); } +int HTTPServer::PostItemsTx(http::Context& ctx) { return modifyItemsTx(ctx, ModeInsert); } -int HTTPServer::PutItemsTx(http::Context &ctx) { return modifyItemsTx(ctx, ModeUpdate); } +int HTTPServer::PutItemsTx(http::Context& ctx) { return modifyItemsTx(ctx, ModeUpdate); } -int HTTPServer::PatchItemsTx(http::Context &ctx) { return modifyItemsTx(ctx, ModeUpsert); } +int HTTPServer::PatchItemsTx(http::Context& ctx) { return modifyItemsTx(ctx, ModeUpsert); } -int HTTPServer::DeleteItemsTx(http::Context &ctx) { return modifyItemsTx(ctx, ModeDelete); } +int HTTPServer::DeleteItemsTx(http::Context& ctx) { return modifyItemsTx(ctx, ModeDelete); } -int HTTPServer::GetSQLQueryTx(http::Context &ctx) { +int HTTPServer::GetSQLQueryTx(http::Context& ctx) { std::string dbName; auto db = getDB(ctx, &dbName); std::string txId = urldecode2(ctx.request->urlParams[1]); @@ -1917,12 +1965,12 @@ int HTTPServer::GetSQLQueryTx(http::Context &ctx) { return status(ctx, http::HttpStatus(http::StatusInternalServerError, "Transactions support update/delete queries only")); } return status(ctx, http::HttpStatus(Error(errLogic, "Unexpected query type: %d", q.type_))); - } catch (const Error &e) { + } catch (const Error& e) { return status(ctx, http::HttpStatus(e)); } } -int HTTPServer::DeleteQueryTx(http::Context &ctx) { +int HTTPServer::DeleteQueryTx(http::Context& ctx) { std::string dbName; auto db = getDB(ctx, &dbName); std::string dsl = ctx.body->Read(); @@ -1942,7 +1990,7 @@ int HTTPServer::DeleteQueryTx(http::Context &ctx) { return modifyQueryTxImpl(ctx, dbName, txId, q); } -int HTTPServer::PostMemReset(http::Context &ctx) { +int HTTPServer::PostMemReset(http::Context& ctx) { #if REINDEX_WITH_GPERFTOOLS if (alloc_ext::TCMallocIsAvailable()) { alloc_ext::instance()->ReleaseFreeMemory(); @@ -1955,7 +2003,7 @@ int HTTPServer::PostMemReset(http::Context &ctx) { #endif } -int HTTPServer::GetMemInfo(http::Context &ctx) { +int HTTPServer::GetMemInfo(http::Context& ctx) { #if REINDEX_WITH_GPERFTOOLS if (alloc_ext::TCMallocIsAvailable()) { const int bufSize = 32000; @@ -1970,9 +2018,9 @@ int HTTPServer::GetMemInfo(http::Context &ctx) { #endif } -void HTTPServer::Logger(http::Context &ctx) { +void HTTPServer::Logger(http::Context& ctx) { HandlerStat statDiff = HandlerStat() - ctx.stat.allocStat; - auto clientData = reinterpret_cast(ctx.clientData.get()); + auto clientData = reinterpret_cast(ctx.clientData.get()); if (serverConfig_.DebugAllocs) { logger_.info("{} - {} {} {} {} {} {}us | allocs: {}, allocated: {} byte(s)", ctx.request->clientAddr, clientData ? clientData->auth.Login() : "", ctx.request->method, ctx.request->uri, ctx.writer->RespCode(), @@ -1983,11 +2031,11 @@ void HTTPServer::Logger(http::Context &ctx) { } } -void HTTPServer::OnResponse(http::Context &ctx) { +void HTTPServer::OnResponse(http::Context& ctx) { if (statsWatcher_) { static const std::string kUnknownDBName = ""; std::string dbName; - const std::string *dbNamePtr = &kUnknownDBName; + const std::string* dbNamePtr = &kUnknownDBName; if (nullptr != ctx.request && !ctx.request->urlParams.empty() && 0 == ctx.request->path.find("/api/v1/db/"sv)) { dbName = urldecode2(ctx.request->urlParams[0]); dbNamePtr = &dbName; diff --git a/cpp_src/server/httpserver.h b/cpp_src/server/httpserver.h index be5a140c3..234e0495d 100644 --- a/cpp_src/server/httpserver.h +++ b/cpp_src/server/httpserver.h @@ -23,107 +23,107 @@ struct HTTPClientData final : public http::ClientData { class HTTPServer { public: - HTTPServer(DBManager &dbMgr, LoggerWrapper &logger, const ServerConfig &serverConfig, Prometheus *prometheusI = nullptr, - IStatsWatcher *statsWatcherI = nullptr); + HTTPServer(DBManager& dbMgr, LoggerWrapper& logger, const ServerConfig& serverConfig, Prometheus* prometheusI = nullptr, + IStatsWatcher* statsWatcherI = nullptr); - bool Start(const std::string &addr, ev::dynamic_loop &loop); + bool Start(const std::string& addr, ev::dynamic_loop& loop); void Stop() { listener_->Stop(); } - int NotFoundHandler(http::Context &ctx); - int DocHandler(http::Context &ctx); - int Check(http::Context &ctx); - int PostQuery(http::Context &ctx); - int DeleteQuery(http::Context &ctx); - int UpdateQuery(http::Context &ctx); - int GetSQLQuery(http::Context &ctx); - int PostSQLQuery(http::Context &ctx); - int GetSQLSuggest(http::Context &ctx); - int GetDatabases(http::Context &ctx); - int PostDatabase(http::Context &ctx); - int DeleteDatabase(http::Context &ctx); - int GetNamespaces(http::Context &ctx); - int GetNamespace(http::Context &ctx); - int PostNamespace(http::Context &ctx); - int DeleteNamespace(http::Context &ctx); - int TruncateNamespace(http::Context &ctx); - int RenameNamespace(http::Context &ctx); - int GetItems(http::Context &ctx); - int PostItems(http::Context &ctx); - int PatchItems(http::Context &ctx); - int PutItems(http::Context &ctx); - int DeleteItems(http::Context &ctx); - int GetIndexes(http::Context &ctx); - int PostIndex(http::Context &ctx); - int PutIndex(http::Context &ctx); - int PutSchema(http::Context &ctx); - int GetSchema(http::Context &ctx); - int GetProtobufSchema(http::Context &ctx); - int GetMetaList(http::Context &ctx); - int GetMetaByKey(http::Context &ctx); - int PutMetaByKey(http::Context &ctx); - int DeleteMetaByKey(http::Context &ctx); - int DeleteIndex(http::Context &ctx); - int CheckAuth(http::Context &ctx); - int BeginTx(http::Context &ctx); - int CommitTx(http::Context &ctx); - int RollbackTx(http::Context &ctx); - int PostItemsTx(http::Context &ctx); - int PutItemsTx(http::Context &ctx); - int PatchItemsTx(http::Context &ctx); - int DeleteItemsTx(http::Context &ctx); - int GetSQLQueryTx(http::Context &ctx); - int DeleteQueryTx(http::Context &ctx); - int PostMemReset(http::Context &ctx); - int GetMemInfo(http::Context &ctx); - void Logger(http::Context &ctx); - void OnResponse(http::Context &ctx); + int NotFoundHandler(http::Context& ctx); + int DocHandler(http::Context& ctx); + int Check(http::Context& ctx); + int PostQuery(http::Context& ctx); + int DeleteQuery(http::Context& ctx); + int UpdateQuery(http::Context& ctx); + int GetSQLQuery(http::Context& ctx); + int PostSQLQuery(http::Context& ctx); + int GetSQLSuggest(http::Context& ctx); + int GetDatabases(http::Context& ctx); + int PostDatabase(http::Context& ctx); + int DeleteDatabase(http::Context& ctx); + int GetNamespaces(http::Context& ctx); + int GetNamespace(http::Context& ctx); + int PostNamespace(http::Context& ctx); + int DeleteNamespace(http::Context& ctx); + int TruncateNamespace(http::Context& ctx); + int RenameNamespace(http::Context& ctx); + int GetItems(http::Context& ctx); + int PostItems(http::Context& ctx); + int PatchItems(http::Context& ctx); + int PutItems(http::Context& ctx); + int DeleteItems(http::Context& ctx); + int GetIndexes(http::Context& ctx); + int PostIndex(http::Context& ctx); + int PutIndex(http::Context& ctx); + int PutSchema(http::Context& ctx); + int GetSchema(http::Context& ctx); + int GetProtobufSchema(http::Context& ctx); + int GetMetaList(http::Context& ctx); + int GetMetaByKey(http::Context& ctx); + int PutMetaByKey(http::Context& ctx); + int DeleteMetaByKey(http::Context& ctx); + int DeleteIndex(http::Context& ctx); + int CheckAuth(http::Context& ctx); + int BeginTx(http::Context& ctx); + int CommitTx(http::Context& ctx); + int RollbackTx(http::Context& ctx); + int PostItemsTx(http::Context& ctx); + int PutItemsTx(http::Context& ctx); + int PatchItemsTx(http::Context& ctx); + int DeleteItemsTx(http::Context& ctx); + int GetSQLQueryTx(http::Context& ctx); + int DeleteQueryTx(http::Context& ctx); + int PostMemReset(http::Context& ctx); + int GetMemInfo(http::Context& ctx); + void Logger(http::Context& ctx); + void OnResponse(http::Context& ctx); protected: - Error modifyItem(Reindexer &db, std::string &nsName, Item &item, ItemModifyMode mode); - Error modifyItem(Reindexer &db, std::string &nsName, Item &item, QueryResults &, ItemModifyMode mode); - int modifyItems(http::Context &ctx, ItemModifyMode mode); - int modifyItemsTx(http::Context &ctx, ItemModifyMode mode); - int modifyItemsProtobuf(http::Context &ctx, std::string &nsName, const std::vector &precepts, ItemModifyMode mode); - int modifyItemsMsgPack(http::Context &ctx, std::string &nsName, const std::vector &precepts, ItemModifyMode mode); - int modifyItemsJSON(http::Context &ctx, std::string &nsName, const std::vector &precepts, ItemModifyMode mode); - int modifyItemsTxMsgPack(http::Context &ctx, Transaction &tx, const std::vector &precepts, ItemModifyMode mode); - int modifyItemsTxJSON(http::Context &ctx, Transaction &tx, const std::vector &precepts, ItemModifyMode mode); - int queryResults(http::Context &ctx, reindexer::QueryResults &res, bool isQueryResults = false, unsigned limit = kDefaultLimit, + Error modifyItem(Reindexer& db, std::string& nsName, Item& item, ItemModifyMode mode); + Error modifyItem(Reindexer& db, std::string& nsName, Item& item, QueryResults&, ItemModifyMode mode); + int modifyItems(http::Context& ctx, ItemModifyMode mode); + int modifyItemsTx(http::Context& ctx, ItemModifyMode mode); + int modifyItemsProtobuf(http::Context& ctx, std::string& nsName, const std::vector& precepts, ItemModifyMode mode); + int modifyItemsMsgPack(http::Context& ctx, std::string& nsName, const std::vector& precepts, ItemModifyMode mode); + int modifyItemsJSON(http::Context& ctx, std::string& nsName, const std::vector& precepts, ItemModifyMode mode); + int modifyItemsTxMsgPack(http::Context& ctx, Transaction& tx, const std::vector& precepts, ItemModifyMode mode); + int modifyItemsTxJSON(http::Context& ctx, Transaction& tx, const std::vector& precepts, ItemModifyMode mode); + int queryResults(http::Context& ctx, reindexer::QueryResults& res, bool isQueryResults = false, unsigned limit = kDefaultLimit, unsigned offset = kDefaultOffset); - int queryResultsMsgPack(http::Context &ctx, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, unsigned offset, + int queryResultsMsgPack(http::Context& ctx, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset, bool withColumns, int width = 0); - int queryResultsProtobuf(http::Context &ctx, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, unsigned offset, + int queryResultsProtobuf(http::Context& ctx, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset, bool withColumns, int width = 0); - int queryResultsJSON(http::Context &ctx, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, unsigned offset, + int queryResultsJSON(http::Context& ctx, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, unsigned offset, bool withColumns, int width = 0); - int queryResultsCSV(http::Context &ctx, reindexer::QueryResults &res, unsigned limit, unsigned offset); + int queryResultsCSV(http::Context& ctx, reindexer::QueryResults& res, unsigned limit, unsigned offset); template - void queryResultParams(Builder &builder, const reindexer::QueryResults &res, bool isQueryResults, unsigned limit, bool withColumns, + void queryResultParams(Builder& builder, const reindexer::QueryResults& res, bool isQueryResults, unsigned limit, bool withColumns, int width); - int status(http::Context &ctx, const http::HttpStatus &status = http::HttpStatus()); - int jsonStatus(http::Context &ctx, const http::HttpStatus &status = http::HttpStatus()); - int msgpackStatus(http::Context &ctx, const http::HttpStatus &status = http::HttpStatus()); - int protobufStatus(http::Context &ctx, const http::HttpStatus &status = http::HttpStatus()); + int status(http::Context& ctx, const http::HttpStatus& status = http::HttpStatus()); + int jsonStatus(http::Context& ctx, const http::HttpStatus& status = http::HttpStatus()); + int msgpackStatus(http::Context& ctx, const http::HttpStatus& status = http::HttpStatus()); + int protobufStatus(http::Context& ctx, const http::HttpStatus& status = http::HttpStatus()); unsigned prepareLimit(std::string_view limitParam, int limitDefault = kDefaultLimit); unsigned prepareOffset(std::string_view offsetParam, int offsetDefault = kDefaultOffset); - int modifyQueryTxImpl(http::Context &ctx, const std::string &dbName, std::string_view txId, Query &q); + int modifyQueryTxImpl(http::Context& ctx, const std::string& dbName, std::string_view txId, Query& q); template - Reindexer getDB(http::Context &ctx, std::string *dbNameOut = nullptr); + Reindexer getDB(http::Context& ctx, std::string* dbNameOut = nullptr); std::string getNameFromJson(std::string_view json); constexpr static std::string_view statsSourceName() { return std::string_view{"http"}; } - std::shared_ptr getTx(const std::string &dbName, std::string_view txId); - std::string addTx(std::string dbName, Transaction &&tx); - void removeTx(const std::string &dbName, std::string_view txId); + std::shared_ptr getTx(const std::string& dbName, std::string_view txId); + std::string addTx(std::string dbName, Transaction&& tx); + void removeTx(const std::string& dbName, std::string_view txId); void removeExpiredTx(); - void deadlineTimerCb(ev::periodic &, int) { removeExpiredTx(); } + void deadlineTimerCb(ev::periodic&, int) { removeExpiredTx(); } - DBManager &dbMgr_; + DBManager& dbMgr_; Pprof pprof_; - const ServerConfig &serverConfig_; - Prometheus *prometheus_; - IStatsWatcher *statsWatcher_; + const ServerConfig& serverConfig_; + Prometheus* prometheus_; + IStatsWatcher* statsWatcher_; const std::string webRoot_; http::Router router_; @@ -150,7 +150,7 @@ class HTTPServer { std::atomic currentCsvDownloads_ = {0}; private: - Error execSqlQueryByType(std::string_view sqlQuery, reindexer::QueryResults &res, http::Context &ctx); + Error execSqlQueryByType(std::string_view sqlQuery, reindexer::QueryResults& res, http::Context& ctx); }; } // namespace reindexer_server diff --git a/cpp_src/server/loggerwrapper.h b/cpp_src/server/loggerwrapper.h index 08737a9d8..b175f04e3 100644 --- a/cpp_src/server/loggerwrapper.h +++ b/cpp_src/server/loggerwrapper.h @@ -8,47 +8,47 @@ namespace reindexer_server { class LoggerWrapper { public: LoggerWrapper() = default; - LoggerWrapper(const char *name) : logger_(spdlog::get(name)) {} + LoggerWrapper(const char* name) : logger_(spdlog::get(name)) {} operator bool() const noexcept { return logger_ != nullptr; } template - void error(Args &&...args) const { + void error(Args&&... args) const { if (logger_) { logger_->error(std::forward(args)...); } } template - void warn(Args &&...args) const { + void warn(Args&&... args) const { if (logger_) { logger_->warn(std::forward(args)...); } } template - void info(Args &&...args) const { + void info(Args&&... args) const { if (logger_) { logger_->info(std::forward(args)...); } } template - void trace(Args &&...args) const { + void trace(Args&&... args) const { if (logger_) { logger_->trace(std::forward(args)...); } } template - void critical(Args &&...args) const { + void critical(Args&&... args) const { if (logger_) { logger_->critical(std::forward(args)...); } } template - void debug(Args &&...args) const { + void debug(Args&&... args) const { if (logger_) { logger_->debug(std::forward(args)...); } @@ -61,7 +61,7 @@ class LoggerWrapper { } // namespace reindexer_server namespace fmt { -static inline void format_arg(fmt::BasicFormatter &f, const char *&, std::string_view s) { +static inline void format_arg(fmt::BasicFormatter& f, const char*&, std::string_view s) { f.writer() << fmt::BasicStringRef(s.data(), s.length()); } } // namespace fmt diff --git a/cpp_src/server/pidfile.h b/cpp_src/server/pidfile.h index dc9c3acfd..99c9fd254 100644 --- a/cpp_src/server/pidfile.h +++ b/cpp_src/server/pidfile.h @@ -16,16 +16,22 @@ namespace reindexer_server { class PidFile { public: PidFile(const std::string& name = std::string(), pid_t pid = -1) : file_(-1) { - if (!name.empty()) Open(name, pid); + if (!name.empty()) { + Open(name, pid); + } } ~PidFile() { - if (IsOpen()) Close(); + if (IsOpen()) { + Close(); + } } bool IsOpen() const { return file_ != -1; } bool Open(const std::string& name, pid_t pid = -1) { - if (IsOpen() || name.empty()) return false; + if (IsOpen() || name.empty()) { + return false; + } // open file int fd = ::open(name.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP); if (fd == -1) { @@ -38,7 +44,9 @@ class PidFile { return false; } // get PID value and convert it to string - if (pid == -1) pid = ::getpid(); + if (pid == -1) { + pid = ::getpid(); + } std::string buf = fmt::sprintf("%d\n", pid); // write PID to file size_t rc = static_cast(::write(fd, buf.c_str(), buf.size())); @@ -68,8 +76,8 @@ class PidFile { reindexer::Error Status() { return error_; } private: - PidFile(PidFile const&); - PidFile& operator=(PidFile const&); + PidFile(const PidFile&); + PidFile& operator=(const PidFile&); private: int file_; diff --git a/cpp_src/server/pprof/gperf_profiler.cc b/cpp_src/server/pprof/gperf_profiler.cc index 50dd72fa2..116920809 100644 --- a/cpp_src/server/pprof/gperf_profiler.cc +++ b/cpp_src/server/pprof/gperf_profiler.cc @@ -9,17 +9,17 @@ #include #include #include "tools/alloc_ext/tc_malloc_extension.h" -#endif // _WIN32 +#endif // _WIN32 #ifdef _WIN32 void ProfilerRegisterThread() { ::ProfilerRegisterThread(); } -int ProfilerStart(const char *fname) { return ::ProfilerStart(); } +int ProfilerStart(const char* fname) { return ::ProfilerStart(); } void ProfilerStop() { ::ProfilerStop(); } -char *GetHeapProfile() { return ::GetHeapProfile(); } +char* GetHeapProfile() { return ::GetHeapProfile(); } bool GperfProfilerIsAvailable() { return true; } @@ -29,9 +29,9 @@ namespace reindexer_server { namespace pprof { using ProfilerRegisterThreadFn = void (*)(); -using ProfilerStartFn = int (*)(const char *); +using ProfilerStartFn = int (*)(const char*); using ProfilerStopFn = void (*)(); -using GetHeapProfileFn = char *(*)(); +using GetHeapProfileFn = char* (*)(); static ProfilerRegisterThreadFn getProfilerRegisterThreadFn() { static auto profiler_register_thread_fn = reinterpret_cast(dlsym(RTLD_DEFAULT, "ProfilerRegisterThread")); @@ -61,7 +61,7 @@ void ProfilerRegisterThread() { return; } -int ProfilerStart(const char *fname) { +int ProfilerStart(const char* fname) { auto profiler_start_fn = getProfilerStartFn(); if (profiler_start_fn) { return profiler_start_fn(fname); @@ -77,7 +77,7 @@ void ProfilerStop() { return; } -char *GetHeapProfile() { +char* GetHeapProfile() { auto get_heap_profile_fn = getGetHeapProfileFn(); if (get_heap_profile_fn) { return get_heap_profile_fn(); @@ -90,9 +90,9 @@ bool GperfProfilerIsAvailable() { (getProfilerStopFn() != nullptr) && (getGetHeapProfileFn() != nullptr); } -#endif //_WIN32 +#endif //_WIN32 } // namespace pprof } // namespace reindexer_server -#endif // REINDEX_WITH_GPERFTOOLS +#endif // REINDEX_WITH_GPERFTOOLS diff --git a/cpp_src/server/pprof/gperf_profiler.h b/cpp_src/server/pprof/gperf_profiler.h index dc761e92f..5b2c02c71 100644 --- a/cpp_src/server/pprof/gperf_profiler.h +++ b/cpp_src/server/pprof/gperf_profiler.h @@ -6,12 +6,12 @@ namespace reindexer_server { namespace pprof { void ProfilerRegisterThread(); -int ProfilerStart(const char *fname); +int ProfilerStart(const char* fname); void ProfilerStop(); -char *GetHeapProfile(); +char* GetHeapProfile(); bool GperfProfilerIsAvailable(); } // namespace pprof } // namespace reindexer_server -#endif // REINDEX_WITH_GPERFTOOLS +#endif // REINDEX_WITH_GPERFTOOLS diff --git a/cpp_src/server/pprof/pprof.cc b/cpp_src/server/pprof/pprof.cc index 489685d99..c85446aa2 100644 --- a/cpp_src/server/pprof/pprof.cc +++ b/cpp_src/server/pprof/pprof.cc @@ -16,7 +16,7 @@ static const std::string kProfileNamePrefix = "reindexer_server"; namespace reindexer_server { using namespace reindexer; -void Pprof::Attach(http::Router &router) { +void Pprof::Attach(http::Router& router) { router.GET("/debug/pprof/profile", this); router.GET("/pprof/profile", this); @@ -36,18 +36,24 @@ void Pprof::Attach(http::Router &router) { router.POST("/symbolz", this); } -int Pprof::Profile(http::Context &ctx) { +int Pprof::Profile(http::Context& ctx) { #if REINDEX_WITH_GPERFTOOLS long long seconds = 30; std::string_view secondsParam; std::string filePath = fs::JoinPath(fs::GetTempDir(), kProfileNamePrefix + ".profile"); for (auto p : ctx.request->params) { - if (p.name == "seconds") secondsParam = p.val; + if (p.name == "seconds") { + secondsParam = p.val; + } } - if (secondsParam.length()) seconds = stoi(secondsParam); - if (seconds < 1) seconds = 30; + if (secondsParam.length()) { + seconds = stoi(secondsParam); + } + if (seconds < 1) { + seconds = 30; + } if (alloc_ext::TCMallocIsAvailable()) { pprof::ProfilerStart(filePath.c_str()); @@ -66,11 +72,11 @@ int Pprof::Profile(http::Context &ctx) { #endif } -int Pprof::ProfileHeap(http::Context &ctx) { +int Pprof::ProfileHeap(http::Context& ctx) { #if REINDEX_WITH_GPERFTOOLS if (pprof::GperfProfilerIsAvailable()) { if (std::getenv("HEAPPROFILE")) { - char *profile = pprof::GetHeapProfile(); + char* profile = pprof::GetHeapProfile(); int res = ctx.String(http::StatusOK, profile); free(profile); return res; @@ -84,7 +90,7 @@ int Pprof::ProfileHeap(http::Context &ctx) { #elif REINDEX_WITH_JEMALLOC std::string content; std::string filePath = fs::JoinPath(fs::GetTempDir(), kProfileNamePrefix + ".heapprofile"); - const char *pfp = &filePath[0]; + const char* pfp = &filePath[0]; alloc_ext::mallctl("prof.dump", NULL, NULL, &pfp, sizeof(pfp)); if (fs::ReadFile(filePath, content) < 0) { @@ -97,7 +103,7 @@ int Pprof::ProfileHeap(http::Context &ctx) { #endif } -int Pprof::Growth(http::Context &ctx) { +int Pprof::Growth(http::Context& ctx) { #if REINDEX_WITH_GPERFTOOLS if (alloc_ext::TCMallocIsAvailable()) { std::string output; @@ -111,9 +117,9 @@ int Pprof::Growth(http::Context &ctx) { #endif } -int Pprof::CmdLine(http::Context &ctx) { return ctx.String(http::StatusOK, "reindexer_server"); } +int Pprof::CmdLine(http::Context& ctx) { return ctx.String(http::StatusOK, "reindexer_server"); } -int Pprof::Symbol(http::Context &ctx) { +int Pprof::Symbol(http::Context& ctx) { using namespace std::string_view_literals; WrSerializer ser; @@ -130,10 +136,12 @@ int Pprof::Symbol(http::Context &ctx) { return ctx.String(http::StatusOK, ser.DetachChunk()); } - char *endp; + char* endp; for (size_t pos = 0; pos != std::string::npos; pos = req.find_first_of(" +", pos)) { pos = req.find_first_not_of(" +", pos); - if (pos == std::string::npos) break; + if (pos == std::string::npos) { + break; + } uintptr_t addr = strtoull(&req[pos], &endp, 16); ser << std::string_view(&req[pos], endp - &req[pos]) << '\t'; resolveSymbol(addr, ser); @@ -143,7 +151,7 @@ int Pprof::Symbol(http::Context &ctx) { return ctx.String(http::StatusOK, ser.DetachChunk()); } -void Pprof::resolveSymbol(uintptr_t ptr, WrSerializer &out) { +void Pprof::resolveSymbol(uintptr_t ptr, WrSerializer& out) { auto te = debug::TraceEntry(ptr); std::string_view symbol = te.FuncName(); @@ -169,7 +177,9 @@ void Pprof::resolveSymbol(uintptr_t ptr, WrSerializer &out) { tmpl--; break; default: - if (tmpl == 0) out << symbol[p]; + if (tmpl == 0) { + out << symbol[p]; + } } p++; } diff --git a/cpp_src/server/pprof/pprof.h b/cpp_src/server/pprof/pprof.h index d1c1c6e13..f76cc15a8 100644 --- a/cpp_src/server/pprof/pprof.h +++ b/cpp_src/server/pprof/pprof.h @@ -12,16 +12,16 @@ using reindexer::WrSerializer; class Pprof { public: - void Attach(http::Router &router); + void Attach(http::Router& router); - int Profile(http::Context &ctx); - int ProfileHeap(http::Context &ctx); - int Growth(http::Context &ctx); - int CmdLine(http::Context &ctx); - int Symbol(http::Context &ctx); + int Profile(http::Context& ctx); + int ProfileHeap(http::Context& ctx); + int Growth(http::Context& ctx); + int CmdLine(http::Context& ctx); + int Symbol(http::Context& ctx); protected: - void resolveSymbol(uintptr_t ptr, WrSerializer &out); + void resolveSymbol(uintptr_t ptr, WrSerializer& out); }; } // namespace reindexer_server diff --git a/cpp_src/server/resources_wrapper.cc b/cpp_src/server/resources_wrapper.cc index 0e6e86442..108cb2ca1 100644 --- a/cpp_src/server/resources_wrapper.cc +++ b/cpp_src/server/resources_wrapper.cc @@ -7,7 +7,9 @@ namespace reindexer_server { DocumentStatus web::fsStatus(const std::string& target) { - if (webRoot_.empty()) return DocumentStatus{}; + if (webRoot_.empty()) { + return DocumentStatus{}; + } DocumentStatus status; status.fstatus = reindexer::fs::Stat(webRoot_ + target); if (status.fstatus == reindexer::fs::StatError) { diff --git a/cpp_src/server/rpcqrwatcher.h b/cpp_src/server/rpcqrwatcher.h index 82a1e3aca..54bab1eb9 100644 --- a/cpp_src/server/rpcqrwatcher.h +++ b/cpp_src/server/rpcqrwatcher.h @@ -48,7 +48,9 @@ class RPCQrWatcher { } } QueryResults& operator*() { - if (!d_.qr) throw Error(errLogic, "Query results' pointer is nullptr"); + if (!d_.qr) { + throw Error(errLogic, "Query results' pointer is nullptr"); + } return *d_.qr; } uint32_t ID() const noexcept { return d_.id; } @@ -149,8 +151,7 @@ class RPCQrWatcher { }; UID() noexcept : freed(0), state(Uninitialized), refs(0), val(0) {} - UID(int64_t uid, bool addRef) - noexcept + UID(int64_t uid, bool addRef) noexcept : freed(0), state(uid >= 0 ? InitializedUIDEnabled : InitializedUIDDisabled), refs(addRef ? 1 : 0), val(uid >= 0 ? uid : 0) { assertf(uid == kDisabled || val == (uid & kUIDValueBitmask), "UID: %d, val: %d", uid, val); } diff --git a/cpp_src/server/rpcserver.cc b/cpp_src/server/rpcserver.cc index 5e4133c7f..837a56423 100644 --- a/cpp_src/server/rpcserver.cc +++ b/cpp_src/server/rpcserver.cc @@ -16,8 +16,8 @@ using namespace std::string_view_literals; const reindexer::SemVersion kMinUnknownReplSupportRxVersion("2.6.0"); constexpr size_t kMaxTxCount = 1024; -RPCServer::RPCServer(DBManager &dbMgr, LoggerWrapper &logger, IClientsStats *clientsStats, const ServerConfig &scfg, - IStatsWatcher *statsCollector) +RPCServer::RPCServer(DBManager& dbMgr, LoggerWrapper& logger, IClientsStats* clientsStats, const ServerConfig& scfg, + IStatsWatcher* statsCollector) : dbMgr_(dbMgr), serverConfig_(scfg), logger_(logger), @@ -33,14 +33,14 @@ RPCServer::~RPCServer() { listener_.reset(); } -Error RPCServer::Ping(cproto::Context &) { +Error RPCServer::Ping(cproto::Context&) { // return {}; } static std::atomic connCounter; -Error RPCServer::Login(cproto::Context &ctx, p_string login, p_string password, p_string db, std::optional createDBIfMissing, +Error RPCServer::Login(cproto::Context& ctx, p_string login, p_string password, p_string db, std::optional createDBIfMissing, std::optional checkClusterID, std::optional expectedClusterID, std::optional clientRxVersion, std::optional appName) { if (ctx.GetClientData()) { @@ -48,7 +48,7 @@ Error RPCServer::Login(cproto::Context &ctx, p_string login, p_string password, } auto clientData = std::make_unique(); - auto &clientDataRef = *clientData; + auto& clientDataRef = *clientData; clientData->connID = connCounter.fetch_add(1, std::memory_order_relaxed); clientData->pusher.SetWriter(ctx.writer); @@ -72,7 +72,7 @@ Error RPCServer::Login(cproto::Context &ctx, p_string login, p_string password, clientData->rxVersion = SemVersion(); } if (clientData->rxVersion < kMinUnknownReplSupportRxVersion) { - clientData->pusher.SetFilter([](WALRecord &rec) { + clientData->pusher.SetFilter([](WALRecord& rec) { if (rec.type == WalCommitTransaction || rec.type == WalInitTransaction || rec.type == WalSetSchema) { return true; } @@ -111,16 +111,18 @@ Error RPCServer::Login(cproto::Context &ctx, p_string login, p_string password, return status; } -static RPCClientData *getClientDataUnsafe(cproto::Context &ctx) noexcept { return dynamic_cast(ctx.GetClientData()); } +static RPCClientData* getClientDataUnsafe(cproto::Context& ctx) noexcept { return dynamic_cast(ctx.GetClientData()); } -static RPCClientData *getClientDataSafe(cproto::Context &ctx) noexcept { - auto ret = dynamic_cast(ctx.GetClientData()); - if rx_unlikely (!ret) std::abort(); // It has to be set by the middleware +static RPCClientData* getClientDataSafe(cproto::Context& ctx) noexcept { + auto ret = dynamic_cast(ctx.GetClientData()); + if rx_unlikely (!ret) { + std::abort(); // It has to be set by the middleware + } return ret; } -Error RPCServer::OpenDatabase(cproto::Context &ctx, p_string db, std::optional createDBIfMissing) { - auto *clientData = getClientDataSafe(ctx); +Error RPCServer::OpenDatabase(cproto::Context& ctx, p_string db, std::optional createDBIfMissing) { + auto* clientData = getClientDataSafe(ctx); if (clientData->auth.HaveDB()) { return Error(errParams, "Database already opened"); } @@ -131,11 +133,11 @@ Error RPCServer::OpenDatabase(cproto::Context &ctx, p_string db, std::optionalauth.ResetDB(); return {}; } -Error RPCServer::DropDatabase(cproto::Context &ctx) { +Error RPCServer::DropDatabase(cproto::Context& ctx) { auto clientData = getClientDataSafe(ctx); if (statsWatcher_) { // Avoid database access from the stats collecting thread during database drop @@ -145,9 +147,9 @@ Error RPCServer::DropDatabase(cproto::Context &ctx) { return dbMgr_.DropDatabase(clientData->auth); } -Error RPCServer::CheckAuth(cproto::Context &ctx) { - cproto::ClientData *ptr = ctx.GetClientData(); - auto clientData = dynamic_cast(ptr); +Error RPCServer::CheckAuth(cproto::Context& ctx) { + cproto::ClientData* ptr = ctx.GetClientData(); + auto clientData = dynamic_cast(ptr); if (ctx.call->cmd == cproto::kCmdLogin || ctx.call->cmd == cproto::kCmdPing) { return {}; @@ -160,7 +162,7 @@ Error RPCServer::CheckAuth(cproto::Context &ctx) { return {}; } -void RPCServer::OnClose(cproto::Context &ctx, const Error &err) { +void RPCServer::OnClose(cproto::Context& ctx, const Error& err) { (void)ctx; (void)err; @@ -172,7 +174,7 @@ void RPCServer::OnClose(cproto::Context &ctx, const Error &err) { if (clientsStats_) { clientsStats_->DeleteConnection(clientData->connID); } - for (auto &qrId : clientData->results) { + for (auto& qrId : clientData->results) { if (qrId.main >= 0) { try { qrWatcher_.FreeQueryResults(qrId, false); @@ -186,11 +188,11 @@ void RPCServer::OnClose(cproto::Context &ctx, const Error &err) { logger_.info("RPC: Client disconnected"); } -void RPCServer::OnResponse(cproto::Context &ctx) { +void RPCServer::OnResponse(cproto::Context& ctx) { if (statsWatcher_) { auto clientData = getClientDataUnsafe(ctx); static const std::string kUnknownDbName(""); - const std::string &dbName = (clientData != nullptr) ? clientData->auth.DBName() : kUnknownDbName; + const std::string& dbName = (clientData != nullptr) ? clientData->auth.DBName() : kUnknownDbName; statsWatcher_->OnOutputTraffic(dbName, statsSourceName(), protocolName_, ctx.stat.sizeStat.respSizeBytes); if (ctx.stat.sizeStat.respSizeBytes) { // Don't update stats on responses like "updates push" @@ -199,7 +201,7 @@ void RPCServer::OnResponse(cproto::Context &ctx) { } } -Error RPCServer::execSqlQueryByType(std::string_view sqlQuery, QueryResults &res, cproto::Context &ctx) noexcept { +Error RPCServer::execSqlQueryByType(std::string_view sqlQuery, QueryResults& res, cproto::Context& ctx) noexcept { try { const auto q = Query::FromSQL(sqlQuery); switch (q.Type()) { @@ -217,7 +219,7 @@ Error RPCServer::execSqlQueryByType(std::string_view sqlQuery, QueryResults &res CATCH_AND_RETURN; } -void RPCServer::Logger(cproto::Context &ctx, const Error &err, const cproto::Args &ret) { +void RPCServer::Logger(cproto::Context& ctx, const Error& err, const cproto::Args& ret) { const auto clientData = getClientDataUnsafe(ctx); uint8_t buf[0x500]; WrSerializer ser(buf); @@ -251,7 +253,7 @@ void RPCServer::Logger(cproto::Context &ctx, const Error &err, const cproto::Arg logger_.info("{}", ser.Slice()); } -Error RPCServer::OpenNamespace(cproto::Context &ctx, p_string nsDefJson) { +Error RPCServer::OpenNamespace(cproto::Context& ctx, p_string nsDefJson) { NamespaceDef nsDef; if (auto err = nsDef.FromJSON(giftStr(nsDefJson)); !err.ok()) { @@ -263,29 +265,33 @@ Error RPCServer::OpenNamespace(cproto::Context &ctx, p_string nsDefJson) { return getDB(ctx, kRoleDataRead).OpenNamespace(nsDef.name, nsDef.storage); } -Error RPCServer::DropNamespace(cproto::Context &ctx, p_string ns) { +Error RPCServer::DropNamespace(cproto::Context& ctx, p_string ns) { // return getDB(ctx, kRoleDBAdmin).DropNamespace(ns); } -Error RPCServer::TruncateNamespace(cproto::Context &ctx, p_string ns) { return getDB(ctx, kRoleDBAdmin).TruncateNamespace(ns); } +Error RPCServer::TruncateNamespace(cproto::Context& ctx, p_string ns) { return getDB(ctx, kRoleDBAdmin).TruncateNamespace(ns); } -Error RPCServer::RenameNamespace(cproto::Context &ctx, p_string srcNsName, p_string dstNsName) { +Error RPCServer::RenameNamespace(cproto::Context& ctx, p_string srcNsName, p_string dstNsName) { return getDB(ctx, kRoleDBAdmin).RenameNamespace(srcNsName, dstNsName.toString()); } -Error RPCServer::CloseNamespace(cproto::Context &ctx, p_string ns) { +Error RPCServer::CloseNamespace(cproto::Context& ctx, p_string ns) { // Do not close. // TODO: add reference counters // return getDB(ctx, kRoleDataRead)->CloseNamespace(ns); return getDB(ctx, kRoleDataRead).Commit(ns); } -Error RPCServer::EnumNamespaces(cproto::Context &ctx, std::optional opts, std::optional filter) { +Error RPCServer::EnumNamespaces(cproto::Context& ctx, std::optional opts, std::optional filter) { std::vector nsDefs; EnumNamespacesOpts eopts; - if (opts) eopts.options_ = *opts; - if (filter) eopts.filter_ = *filter; + if (opts) { + eopts.options_ = *opts; + } + if (filter) { + eopts.filter_ = *filter; + } auto err = getDB(ctx, kRoleDataRead).EnumNamespaces(nsDefs, eopts); if (!err.ok()) { @@ -294,7 +300,9 @@ Error RPCServer::EnumNamespaces(cproto::Context &ctx, std::optional opts, s WrSerializer ser; ser << "{\"items\":["; for (unsigned i = 0; i < nsDefs.size(); i++) { - if (i != 0) ser << ','; + if (i != 0) { + ser << ','; + } nsDefs[i].GetJSON(ser); } ser << "]}"; @@ -304,7 +312,7 @@ Error RPCServer::EnumNamespaces(cproto::Context &ctx, std::optional opts, s return errOK; } -Error RPCServer::EnumDatabases(cproto::Context &ctx) { +Error RPCServer::EnumDatabases(cproto::Context& ctx) { auto dbList = dbMgr_.EnumDatabases(); WrSerializer ser; @@ -319,7 +327,7 @@ Error RPCServer::EnumDatabases(cproto::Context &ctx) { return errOK; } -Error RPCServer::AddIndex(cproto::Context &ctx, p_string ns, p_string indexDef) { +Error RPCServer::AddIndex(cproto::Context& ctx, p_string ns, p_string indexDef) { IndexDef iDef; auto err = iDef.FromJSON(giftStr(indexDef)); if (!err.ok()) { @@ -328,7 +336,7 @@ Error RPCServer::AddIndex(cproto::Context &ctx, p_string ns, p_string indexDef) return getDB(ctx, kRoleDBAdmin).AddIndex(ns, iDef); } -Error RPCServer::UpdateIndex(cproto::Context &ctx, p_string ns, p_string indexDef) { +Error RPCServer::UpdateIndex(cproto::Context& ctx, p_string ns, p_string indexDef) { IndexDef iDef; auto err = iDef.FromJSON(giftStr(indexDef)); if (!err.ok()) { @@ -337,29 +345,29 @@ Error RPCServer::UpdateIndex(cproto::Context &ctx, p_string ns, p_string indexDe return getDB(ctx, kRoleDBAdmin).UpdateIndex(ns, iDef); } -Error RPCServer::DropIndex(cproto::Context &ctx, p_string ns, p_string index) { +Error RPCServer::DropIndex(cproto::Context& ctx, p_string ns, p_string index) { IndexDef idef(index.toString()); return getDB(ctx, kRoleDBAdmin).DropIndex(ns, idef); } -Error RPCServer::SetSchema(cproto::Context &ctx, p_string ns, p_string schema) { +Error RPCServer::SetSchema(cproto::Context& ctx, p_string ns, p_string schema) { return getDB(ctx, kRoleDBAdmin).SetSchema(ns, std::string_view(schema)); } -Error RPCServer::StartTransaction(cproto::Context &ctx, p_string nsName) { +Error RPCServer::StartTransaction(cproto::Context& ctx, p_string nsName) { int64_t id = -1; try { id = addTx(ctx, nsName); - } catch (reindexer::Error &e) { + } catch (reindexer::Error& e) { return e; } ctx.Return({cproto::Arg(id)}); return errOK; } -Error RPCServer::AddTxItem(cproto::Context &ctx, int format, p_string itemData, int mode, p_string perceptsPack, int stateToken, +Error RPCServer::AddTxItem(cproto::Context& ctx, int format, p_string itemData, int mode, p_string perceptsPack, int stateToken, int64_t txID) { - Transaction &tr = getTx(ctx, txID); + Transaction& tr = getTx(ctx, txID); auto item = tr.NewItem(); Error err; @@ -395,11 +403,11 @@ Error RPCServer::AddTxItem(cproto::Context &ctx, int format, p_string itemData, return err; } -Error RPCServer::DeleteQueryTx(cproto::Context &ctx, p_string queryBin, int64_t txID) noexcept { +Error RPCServer::DeleteQueryTx(cproto::Context& ctx, p_string queryBin, int64_t txID) noexcept { try { auto db = getDB(ctx, kRoleDataWrite); - Transaction &tr = getTx(ctx, txID); + Transaction& tr = getTx(ctx, txID); Serializer ser(queryBin.data(), queryBin.size()); Query query = Query::Deserialize(ser); query.type_ = QueryDelete; @@ -409,11 +417,11 @@ Error RPCServer::DeleteQueryTx(cproto::Context &ctx, p_string queryBin, int64_t CATCH_AND_RETURN; } -Error RPCServer::UpdateQueryTx(cproto::Context &ctx, p_string queryBin, int64_t txID) noexcept { +Error RPCServer::UpdateQueryTx(cproto::Context& ctx, p_string queryBin, int64_t txID) noexcept { try { auto db = getDB(ctx, kRoleDataWrite); - Transaction &tr = getTx(ctx, txID); + Transaction& tr = getTx(ctx, txID); Serializer ser(queryBin.data(), queryBin.size()); Query query = Query::Deserialize(ser); query.type_ = QueryUpdate; @@ -423,10 +431,10 @@ Error RPCServer::UpdateQueryTx(cproto::Context &ctx, p_string queryBin, int64_t CATCH_AND_RETURN; } -Error RPCServer::CommitTx(cproto::Context &ctx, int64_t txId, std::optional flagsOpts) { +Error RPCServer::CommitTx(cproto::Context& ctx, int64_t txId, std::optional flagsOpts) { auto db = getDB(ctx, kRoleDataWrite); - Transaction &tr = getTx(ctx, txId); + Transaction& tr = getTx(ctx, txId); QueryResults qres; auto err = db.CommitTransaction(tr, qres); @@ -438,7 +446,9 @@ Error RPCServer::CommitTx(cproto::Context &ctx, int64_t txId, std::optional flags = *flagsOpts; } else { flags = kResultsWithItemID; - if (tr.IsTagsUpdated()) flags |= kResultsWithPayloadTypes; + if (tr.IsTagsUpdated()) { + flags |= kResultsWithPayloadTypes; + } } if (tr.IsTagsUpdated()) { opts = ResultFetchOpts{ @@ -453,16 +463,16 @@ Error RPCServer::CommitTx(cproto::Context &ctx, int64_t txId, std::optional return err; } -Error RPCServer::RollbackTx(cproto::Context &ctx, int64_t txId) { +Error RPCServer::RollbackTx(cproto::Context& ctx, int64_t txId) { auto db = getDB(ctx, kRoleDataWrite); - Transaction &tr = getTx(ctx, txId); + Transaction& tr = getTx(ctx, txId); auto err = db.RollBackTransaction(tr); clearTx(ctx, txId); return err; } -Error RPCServer::ModifyItem(cproto::Context &ctx, p_string ns, int format, p_string itemData, int mode, p_string perceptsPack, +Error RPCServer::ModifyItem(cproto::Context& ctx, p_string ns, int format, p_string itemData, int mode, p_string perceptsPack, int stateToken, int /*txID*/) { using std::chrono::milliseconds; using std::chrono::duration_cast; @@ -516,7 +526,9 @@ Error RPCServer::ModifyItem(cproto::Context &ctx, p_string ns, int format, p_str precepts.emplace_back(ser.GetVString()); } item.SetPrecepts(precepts); - if (preceptsCount) sendItemBack = true; + if (preceptsCount) { + sendItemBack = true; + } } QueryResults qres; if (sendItemBack) { @@ -536,7 +548,9 @@ Error RPCServer::ModifyItem(cproto::Context &ctx, p_string ns, int format, p_str default: return Error(errParams, "Unexpected ItemModifyMode: %d", mode); } - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } else { switch (mode) { case ModeUpsert: @@ -554,7 +568,9 @@ Error RPCServer::ModifyItem(cproto::Context &ctx, p_string ns, int format, p_str default: return Error(errParams, "Unexpected ItemModifyMode: %d", mode); } - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } qres.AddItem(item); } int32_t ptVers = -1; @@ -580,7 +596,7 @@ Error RPCServer::ModifyItem(cproto::Context &ctx, p_string ns, int format, p_str return sendResults(ctx, qres, RPCQrId(), opts); } -Error RPCServer::DeleteQuery(cproto::Context &ctx, p_string queryBin, std::optional flagsOpts) noexcept { +Error RPCServer::DeleteQuery(cproto::Context& ctx, p_string queryBin, std::optional flagsOpts) noexcept { try { Serializer ser(queryBin.data(), queryBin.size()); Query query = Query::Deserialize(ser); @@ -605,7 +621,7 @@ Error RPCServer::DeleteQuery(cproto::Context &ctx, p_string queryBin, std::optio CATCH_AND_RETURN; } -Error RPCServer::UpdateQuery(cproto::Context &ctx, p_string queryBin, std::optional flagsOpts) noexcept { +Error RPCServer::UpdateQuery(cproto::Context& ctx, p_string queryBin, std::optional flagsOpts) noexcept { try { Serializer ser(queryBin.data(), queryBin.size()); Query query = Query::Deserialize(ser); @@ -621,7 +637,9 @@ Error RPCServer::UpdateQuery(cproto::Context &ctx, p_string queryBin, std::optio int32_t ptVersion = -1; int flags = kResultsWithItemID | kResultsWithPayloadTypes | kResultsCJson; - if (flagsOpts) flags = *flagsOpts; + if (flagsOpts) { + flags = *flagsOpts; + } ResultFetchOpts opts{ .flags = flags, .ptVersions = {&ptVersion, 1}, .fetchOffset = 0, .fetchLimit = INT_MAX, .withAggregations = true}; return sendResults(ctx, qres, RPCQrId(), opts); @@ -629,10 +647,10 @@ Error RPCServer::UpdateQuery(cproto::Context &ctx, p_string queryBin, std::optio CATCH_AND_RETURN; } -Reindexer RPCServer::getDB(cproto::Context &ctx, UserRole role) { - auto clientData = dynamic_cast(ctx.GetClientData()); +Reindexer RPCServer::getDB(cproto::Context& ctx, UserRole role) { + auto clientData = dynamic_cast(ctx.GetClientData()); if (rx_likely(clientData)) { - Reindexer *db = nullptr; + Reindexer* db = nullptr; auto status = clientData->auth.GetDB(role, &db); if rx_unlikely (!status.ok()) { throw status; @@ -646,7 +664,7 @@ Reindexer RPCServer::getDB(cproto::Context &ctx, UserRole role) { throw Error(errParams, "Database is not opened, you should open it first"); } -Error RPCServer::sendResults(cproto::Context &ctx, QueryResults &qres, RPCQrId id, const ResultFetchOpts &opts) { +Error RPCServer::sendResults(cproto::Context& ctx, QueryResults& qres, RPCQrId id, const ResultFetchOpts& opts) { uint8_t serBuf[0x2000]; WrResultSerializer rser(serBuf, opts); try { @@ -658,7 +676,7 @@ Error RPCServer::sendResults(cproto::Context &ctx, QueryResults &qres, RPCQrId i } std::string_view resSlice = rser.Slice(); ctx.Return({cproto::Arg(p_string(&resSlice)), cproto::Arg(int(id.main)), cproto::Arg(int64_t(id.uid))}); - } catch (Error &err) { + } catch (Error& err) { if (id.main >= 0) { try { freeQueryResults(ctx, id); @@ -675,7 +693,7 @@ Error RPCServer::sendResults(cproto::Context &ctx, QueryResults &qres, RPCQrId i return errOK; } -Error RPCServer::processTxItem(DataFormat format, std::string_view itemData, Item &item, ItemModifyMode mode, +Error RPCServer::processTxItem(DataFormat format, std::string_view itemData, Item& item, ItemModifyMode mode, int stateToken) const noexcept { switch (format) { case FormatJson: @@ -696,7 +714,7 @@ Error RPCServer::processTxItem(DataFormat format, std::string_view itemData, Ite } } -RPCQrWatcher::Ref RPCServer::createQueryResults(cproto::Context &ctx, RPCQrId &id) { +RPCQrWatcher::Ref RPCServer::createQueryResults(cproto::Context& ctx, RPCQrId& id) { auto data = getClientDataSafe(ctx); assertrx(id.main < 0); @@ -704,7 +722,7 @@ RPCQrWatcher::Ref RPCServer::createQueryResults(cproto::Context &ctx, RPCQrId &i RPCQrWatcher::Ref qres; qres = qrWatcher_.GetQueryResults(id); - for (auto &qrId : data->results) { + for (auto& qrId : data->results) { if (qrId.main < 0 || qrId.main == id.main) { qrId = id; return qres; @@ -729,9 +747,9 @@ RPCQrWatcher::Ref RPCServer::createQueryResults(cproto::Context &ctx, RPCQrId &i return qres; } -void RPCServer::freeQueryResults(cproto::Context &ctx, RPCQrId id) { +void RPCServer::freeQueryResults(cproto::Context& ctx, RPCQrId id) { auto data = getClientDataSafe(ctx); - for (auto &qrId : data->results) { + for (auto& qrId : data->results) { if (qrId.main == id.main) { if (qrId.uid != id.uid) { if (!qrWatcher_.AreQueryResultsValid(RPCQrId{qrId.main, qrId.uid})) { @@ -748,7 +766,7 @@ void RPCServer::freeQueryResults(cproto::Context &ctx, RPCQrId id) { } } -Transaction &RPCServer::getTx(cproto::Context &ctx, int64_t id) { +Transaction& RPCServer::getTx(cproto::Context& ctx, int64_t id) { auto data = getClientDataSafe(ctx); if (size_t(id) >= data->txs.size() || data->txs[id].IsFree()) { @@ -757,7 +775,7 @@ Transaction &RPCServer::getTx(cproto::Context &ctx, int64_t id) { return data->txs[id]; } -int64_t RPCServer::addTx(cproto::Context &ctx, std::string_view nsName) { +int64_t RPCServer::addTx(cproto::Context& ctx, std::string_view nsName) { auto db = getDB(ctx, kRoleDataWrite); int64_t id = -1; auto data = getClientDataSafe(ctx); @@ -784,7 +802,7 @@ int64_t RPCServer::addTx(cproto::Context &ctx, std::string_view nsName) { data->txs.emplace_back(std::move(tr)); return int64_t(data->txs.size() - 1); } -void RPCServer::clearTx(cproto::Context &ctx, uint64_t txId) { +void RPCServer::clearTx(cproto::Context& ctx, uint64_t txId) { auto data = getClientDataSafe(ctx); if (txId >= data->txs.size()) { throw Error(errLogic, "Invalid tx id %d", txId); @@ -800,16 +818,18 @@ static h_vector pack2vec(p_string pack) { h_vector vec; int cnt = ser.GetVarUint(); vec.reserve(cnt); - for (int i = 0; i < cnt; i++) vec.emplace_back(ser.GetVarUint()); + for (int i = 0; i < cnt; i++) { + vec.emplace_back(ser.GetVarUint()); + } return vec; } -Error RPCServer::Select(cproto::Context &ctx, p_string queryBin, int flags, int limit, p_string ptVersionsPck) { +Error RPCServer::Select(cproto::Context& ctx, p_string queryBin, int flags, int limit, p_string ptVersionsPck) { Query query; Serializer ser(queryBin); try { query = Query::Deserialize(ser); - } catch (Error &err) { + } catch (Error& err) { return err; } @@ -824,7 +844,7 @@ Error RPCServer::Select(cproto::Context &ctx, p_string queryBin, int flags, int RPCQrId id{-1, (flags & kResultsSupportIdleTimeout) ? RPCQrWatcher::kUninitialized : RPCQrWatcher::kDisabled}; try { qres = createQueryResults(ctx, id); - } catch (Error &e) { + } catch (Error& e) { if (e.code() == errQrUIDMissmatch) { return e; } @@ -843,12 +863,12 @@ Error RPCServer::Select(cproto::Context &ctx, p_string queryBin, int flags, int return sendResults(ctx, *qres, id, opts); } -Error RPCServer::SelectSQL(cproto::Context &ctx, p_string querySql, int flags, int limit, p_string ptVersionsPck) { +Error RPCServer::SelectSQL(cproto::Context& ctx, p_string querySql, int flags, int limit, p_string ptVersionsPck) { RPCQrId id{-1, (flags & kResultsSupportIdleTimeout) ? RPCQrWatcher::kUninitialized : RPCQrWatcher::kDisabled}; RPCQrWatcher::Ref qres; try { qres = createQueryResults(ctx, id); - } catch (Error &e) { + } catch (Error& e) { if (e.code() == errQrUIDMissmatch) { return e; } @@ -869,13 +889,13 @@ Error RPCServer::SelectSQL(cproto::Context &ctx, p_string querySql, int flags, i return sendResults(ctx, *qres, id, opts); } -Error RPCServer::FetchResults(cproto::Context &ctx, int reqId, int flags, int offset, int limit, std::optional qrUID) { +Error RPCServer::FetchResults(cproto::Context& ctx, int reqId, int flags, int offset, int limit, std::optional qrUID) { flags &= ~kResultsWithPayloadTypes; RPCQrId id{reqId, qrUID ? *qrUID : RPCQrWatcher::kDisabled}; RPCQrWatcher::Ref qres; try { qres = qrWatcher_.GetQueryResults(id); - } catch (Error &e) { + } catch (Error& e) { if (e.code() == errQrUIDMissmatch) { return e; } @@ -887,35 +907,37 @@ Error RPCServer::FetchResults(cproto::Context &ctx, int reqId, int flags, int of return sendResults(ctx, *qres, id, opts); } -Error RPCServer::CloseResults(cproto::Context &ctx, int reqId, std::optional qrUID, std::optional doNotReply) { +Error RPCServer::CloseResults(cproto::Context& ctx, int reqId, std::optional qrUID, std::optional doNotReply) { if (doNotReply && *doNotReply) { ctx.respSent = true; } const RPCQrId id{reqId, qrUID ? *qrUID : RPCQrWatcher::kDisabled}; try { freeQueryResults(ctx, id); - } catch (Error &e) { + } catch (Error& e) { return e; } return errOK; } -Error RPCServer::GetSQLSuggestions(cproto::Context &ctx, p_string query, int pos) { +Error RPCServer::GetSQLSuggestions(cproto::Context& ctx, p_string query, int pos) { std::vector suggests; Error err = getDB(ctx, kRoleDataRead).GetSqlSuggestions(query, pos, suggests); if (err.ok()) { cproto::Args ret; ret.reserve(suggests.size()); - for (auto &suggest : suggests) ret.emplace_back(std::move(suggest)); + for (auto& suggest : suggests) { + ret.emplace_back(std::move(suggest)); + } ctx.Return(ret); } return err; } -Error RPCServer::Commit(cproto::Context &ctx, p_string ns) { return getDB(ctx, kRoleDataWrite).Commit(ns); } +Error RPCServer::Commit(cproto::Context& ctx, p_string ns) { return getDB(ctx, kRoleDataWrite).Commit(ns); } -Error RPCServer::GetMeta(cproto::Context &ctx, p_string ns, p_string key) { +Error RPCServer::GetMeta(cproto::Context& ctx, p_string ns, p_string key) { std::string data; auto err = getDB(ctx, kRoleDataRead).GetMeta(ns, key.toString(), data); if (!err.ok()) { @@ -926,29 +948,29 @@ Error RPCServer::GetMeta(cproto::Context &ctx, p_string ns, p_string key) { return errOK; } -Error RPCServer::PutMeta(cproto::Context &ctx, p_string ns, p_string key, p_string data) { +Error RPCServer::PutMeta(cproto::Context& ctx, p_string ns, p_string key, p_string data) { return getDB(ctx, kRoleDataWrite).PutMeta(ns, key.toString(), data); } -Error RPCServer::EnumMeta(cproto::Context &ctx, p_string ns) { +Error RPCServer::EnumMeta(cproto::Context& ctx, p_string ns) { std::vector keys; auto err = getDB(ctx, kRoleDataWrite).EnumMeta(ns, keys); if (!err.ok()) { return err; } cproto::Args ret; - for (auto &key : keys) { + for (auto& key : keys) { ret.emplace_back(std::move(key)); } ctx.Return(ret); return errOK; } -Error RPCServer::DeleteMeta(cproto::Context &ctx, p_string ns, p_string key) { +Error RPCServer::DeleteMeta(cproto::Context& ctx, p_string ns, p_string key) { return getDB(ctx, kRoleDataWrite).DeleteMeta(ns, key.toString()); } -Error RPCServer::SubscribeUpdates(cproto::Context &ctx, int flag, std::optional filterJson, std::optional options) { +Error RPCServer::SubscribeUpdates(cproto::Context& ctx, int flag, std::optional filterJson, std::optional options) { UpdatesFilters filters; Error ret; if (filterJson) { @@ -969,11 +991,13 @@ Error RPCServer::SubscribeUpdates(cproto::Context &ctx, int flag, std::optional< } else { ret = db.UnsubscribeUpdates(&clientData->pusher); } - if (ret.ok()) clientData->subscribed = bool(flag); + if (ret.ok()) { + clientData->subscribed = bool(flag); + } return ret; } -bool RPCServer::Start(const std::string &addr, ev::dynamic_loop &loop, RPCSocketT sockDomain, std::string_view threadingMode) { +bool RPCServer::Start(const std::string& addr, ev::dynamic_loop& loop, RPCSocketT sockDomain, std::string_view threadingMode) { dispatcher_.Register(cproto::kCmdPing, this, &RPCServer::Ping); dispatcher_.Register(cproto::kCmdLogin, this, &RPCServer::Login); dispatcher_.Register(cproto::kCmdOpenDatabase, this, &RPCServer::OpenDatabase); @@ -1034,7 +1058,7 @@ bool RPCServer::Start(const std::string &addr, ev::dynamic_loop &loop, RPCSocket assertrx(!qrWatcherThread_.joinable()); auto thLoop = std::make_unique(); - qrWatcherTerminateAsync_.set([](ev::async &a) { a.loop.break_loop(); }); + qrWatcherTerminateAsync_.set([](ev::async& a) { a.loop.break_loop(); }); qrWatcherTerminateAsync_.set(*thLoop); qrWatcherTerminateAsync_.start(); qrWatcherThread_ = std::thread([this, thLoop = std::move(thLoop)]() { @@ -1051,7 +1075,7 @@ bool RPCServer::Start(const std::string &addr, ev::dynamic_loop &loop, RPCSocket } RPCClientData::~RPCClientData() { - Reindexer *db = nullptr; + Reindexer* db = nullptr; auto err = auth.GetDB(kRoleNone, &db); if (subscribed && db && err.ok()) { err = db->UnsubscribeUpdates(&pusher); diff --git a/cpp_src/server/rpcserver.h b/cpp_src/server/rpcserver.h index a2d633e3d..d274ea48f 100644 --- a/cpp_src/server/rpcserver.h +++ b/cpp_src/server/rpcserver.h @@ -40,11 +40,11 @@ struct RPCClientData final : public cproto::ClientData { class RPCServer { public: - RPCServer(DBManager &dbMgr, LoggerWrapper &logger, IClientsStats *clientsStats, const ServerConfig &serverConfig, - IStatsWatcher *statsCollector = nullptr); + RPCServer(DBManager& dbMgr, LoggerWrapper& logger, IClientsStats* clientsStats, const ServerConfig& serverConfig, + IStatsWatcher* statsCollector = nullptr); ~RPCServer(); - bool Start(const std::string &addr, ev::dynamic_loop &loop, RPCSocketT sockDomain, std::string_view threadingMode); + bool Start(const std::string& addr, ev::dynamic_loop& loop, RPCSocketT sockDomain, std::string_view threadingMode); void Stop() { terminate_ = true; if (qrWatcherThread_.joinable()) { @@ -55,86 +55,86 @@ class RPCServer { terminate_ = false; } - Error Ping(cproto::Context &ctx); - Error Login(cproto::Context &ctx, p_string login, p_string password, p_string db, std::optional createDBIfMissing, + Error Ping(cproto::Context& ctx); + Error Login(cproto::Context& ctx, p_string login, p_string password, p_string db, std::optional createDBIfMissing, std::optional checkClusterID, std::optional expectedClusterID, std::optional clientRxVersion, std::optional appName); - Error OpenDatabase(cproto::Context &ctx, p_string db, std::optional createDBIfMissing); - Error CloseDatabase(cproto::Context &ctx); - Error DropDatabase(cproto::Context &ctx); + Error OpenDatabase(cproto::Context& ctx, p_string db, std::optional createDBIfMissing); + Error CloseDatabase(cproto::Context& ctx); + Error DropDatabase(cproto::Context& ctx); - Error OpenNamespace(cproto::Context &ctx, p_string ns); - Error DropNamespace(cproto::Context &ctx, p_string ns); - Error TruncateNamespace(cproto::Context &ctx, p_string ns); - Error RenameNamespace(cproto::Context &ctx, p_string srcNsName, p_string dstNsName); + Error OpenNamespace(cproto::Context& ctx, p_string ns); + Error DropNamespace(cproto::Context& ctx, p_string ns); + Error TruncateNamespace(cproto::Context& ctx, p_string ns); + Error RenameNamespace(cproto::Context& ctx, p_string srcNsName, p_string dstNsName); - Error CloseNamespace(cproto::Context &ctx, p_string ns); - Error EnumNamespaces(cproto::Context &ctx, std::optional opts, std::optional filter); - Error EnumDatabases(cproto::Context &ctx); + Error CloseNamespace(cproto::Context& ctx, p_string ns); + Error EnumNamespaces(cproto::Context& ctx, std::optional opts, std::optional filter); + Error EnumDatabases(cproto::Context& ctx); - Error AddIndex(cproto::Context &ctx, p_string ns, p_string indexDef); - Error UpdateIndex(cproto::Context &ctx, p_string ns, p_string indexDef); - Error DropIndex(cproto::Context &ctx, p_string ns, p_string index); + Error AddIndex(cproto::Context& ctx, p_string ns, p_string indexDef); + Error UpdateIndex(cproto::Context& ctx, p_string ns, p_string indexDef); + Error DropIndex(cproto::Context& ctx, p_string ns, p_string index); - Error SetSchema(cproto::Context &ctx, p_string ns, p_string schema); + Error SetSchema(cproto::Context& ctx, p_string ns, p_string schema); - Error Commit(cproto::Context &ctx, p_string ns); + Error Commit(cproto::Context& ctx, p_string ns); - Error ModifyItem(cproto::Context &ctx, p_string nsName, int format, p_string itemData, int mode, p_string percepsPack, int stateToken, + Error ModifyItem(cproto::Context& ctx, p_string nsName, int format, p_string itemData, int mode, p_string percepsPack, int stateToken, int txID); - Error StartTransaction(cproto::Context &ctx, p_string nsName); + Error StartTransaction(cproto::Context& ctx, p_string nsName); - Error AddTxItem(cproto::Context &ctx, int format, p_string itemData, int mode, p_string percepsPack, int stateToken, int64_t txID); - Error DeleteQueryTx(cproto::Context &ctx, p_string query, int64_t txID) noexcept; - Error UpdateQueryTx(cproto::Context &ctx, p_string query, int64_t txID) noexcept; + Error AddTxItem(cproto::Context& ctx, int format, p_string itemData, int mode, p_string percepsPack, int stateToken, int64_t txID); + Error DeleteQueryTx(cproto::Context& ctx, p_string query, int64_t txID) noexcept; + Error UpdateQueryTx(cproto::Context& ctx, p_string query, int64_t txID) noexcept; - Error CommitTx(cproto::Context &ctx, int64_t txId, std::optional flags); - Error RollbackTx(cproto::Context &ctx, int64_t txId); + Error CommitTx(cproto::Context& ctx, int64_t txId, std::optional flags); + Error RollbackTx(cproto::Context& ctx, int64_t txId); - Error DeleteQuery(cproto::Context &ctx, p_string query, std::optional flags) noexcept; - Error UpdateQuery(cproto::Context &ctx, p_string query, std::optional flags) noexcept; + Error DeleteQuery(cproto::Context& ctx, p_string query, std::optional flags) noexcept; + Error UpdateQuery(cproto::Context& ctx, p_string query, std::optional flags) noexcept; - Error Select(cproto::Context &ctx, p_string query, int flags, int limit, p_string ptVersions); - Error SelectSQL(cproto::Context &ctx, p_string query, int flags, int limit, p_string ptVersions); - Error FetchResults(cproto::Context &ctx, int reqId, int flags, int offset, int limit, std::optional qrUID); - Error CloseResults(cproto::Context &ctx, int reqId, std::optional qrUID, std::optional doNotReply); - Error GetSQLSuggestions(cproto::Context &ctx, p_string query, int pos); + Error Select(cproto::Context& ctx, p_string query, int flags, int limit, p_string ptVersions); + Error SelectSQL(cproto::Context& ctx, p_string query, int flags, int limit, p_string ptVersions); + Error FetchResults(cproto::Context& ctx, int reqId, int flags, int offset, int limit, std::optional qrUID); + Error CloseResults(cproto::Context& ctx, int reqId, std::optional qrUID, std::optional doNotReply); + Error GetSQLSuggestions(cproto::Context& ctx, p_string query, int pos); - Error GetMeta(cproto::Context &ctx, p_string ns, p_string key); - Error PutMeta(cproto::Context &ctx, p_string ns, p_string key, p_string data); - Error EnumMeta(cproto::Context &ctx, p_string ns); - Error DeleteMeta(cproto::Context &ctx, p_string ns, p_string key); - Error SubscribeUpdates(cproto::Context &ctx, int subscribe, std::optional filterJson, std::optional options); + Error GetMeta(cproto::Context& ctx, p_string ns, p_string key); + Error PutMeta(cproto::Context& ctx, p_string ns, p_string key, p_string data); + Error EnumMeta(cproto::Context& ctx, p_string ns); + Error DeleteMeta(cproto::Context& ctx, p_string ns, p_string key); + Error SubscribeUpdates(cproto::Context& ctx, int subscribe, std::optional filterJson, std::optional options); - Error CheckAuth(cproto::Context &ctx); - void Logger(cproto::Context &ctx, const Error &err, const cproto::Args &ret); - void OnClose(cproto::Context &ctx, const Error &err); - void OnResponse(cproto::Context &ctx); + Error CheckAuth(cproto::Context& ctx); + void Logger(cproto::Context& ctx, const Error& err, const cproto::Args& ret); + void OnClose(cproto::Context& ctx, const Error& err); + void OnResponse(cproto::Context& ctx); protected: - Error execSqlQueryByType(std::string_view sqlQuery, reindexer::QueryResults &res, cproto::Context &ctx) noexcept; - Error sendResults(cproto::Context &ctx, QueryResults &qr, RPCQrId id, const ResultFetchOpts &opts); - Error processTxItem(DataFormat format, std::string_view itemData, Item &item, ItemModifyMode mode, int stateToken) const noexcept; + Error execSqlQueryByType(std::string_view sqlQuery, reindexer::QueryResults& res, cproto::Context& ctx) noexcept; + Error sendResults(cproto::Context& ctx, QueryResults& qr, RPCQrId id, const ResultFetchOpts& opts); + Error processTxItem(DataFormat format, std::string_view itemData, Item& item, ItemModifyMode mode, int stateToken) const noexcept; - RPCQrWatcher::Ref createQueryResults(cproto::Context &ctx, RPCQrId &id); - void freeQueryResults(cproto::Context &ctx, RPCQrId id); - Transaction &getTx(cproto::Context &ctx, int64_t id); - int64_t addTx(cproto::Context &ctx, std::string_view nsName); - void clearTx(cproto::Context &ctx, uint64_t txId); + RPCQrWatcher::Ref createQueryResults(cproto::Context& ctx, RPCQrId& id); + void freeQueryResults(cproto::Context& ctx, RPCQrId id); + Transaction& getTx(cproto::Context& ctx, int64_t id); + int64_t addTx(cproto::Context& ctx, std::string_view nsName); + void clearTx(cproto::Context& ctx, uint64_t txId); - Reindexer getDB(cproto::Context &ctx, UserRole role); + Reindexer getDB(cproto::Context& ctx, UserRole role); constexpr static std::string_view statsSourceName() noexcept { return std::string_view{"rpc"}; } - DBManager &dbMgr_; + DBManager& dbMgr_; cproto::Dispatcher dispatcher_; std::unique_ptr listener_; - const ServerConfig &serverConfig_; + const ServerConfig& serverConfig_; LoggerWrapper logger_; - IStatsWatcher *statsWatcher_; + IStatsWatcher* statsWatcher_; - IClientsStats *clientsStats_; + IClientsStats* clientsStats_; system_clock_w::time_point startTs_; std::thread qrWatcherThread_; diff --git a/cpp_src/server/rpcupdatespusher.cc b/cpp_src/server/rpcupdatespusher.cc index a7f7e279e..d6348b3ac 100644 --- a/cpp_src/server/rpcupdatespusher.cc +++ b/cpp_src/server/rpcupdatespusher.cc @@ -8,7 +8,7 @@ namespace cproto { RPCUpdatesPusher::RPCUpdatesPusher() : writer_(nullptr), seq_(0) {} -void RPCUpdatesPusher::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &walRec) { +void RPCUpdatesPusher::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& walRec) { SharedWALRecord pwalRec; if (filter_) { WALRecord rec = walRec; @@ -21,7 +21,7 @@ void RPCUpdatesPusher::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const pwalRec = walRec.GetShared(int64_t(LSNs.upstreamLSN_), int64_t(LSNs.originLSN_), nsName); } - writer_->CallRPC({[](IRPCCall *self, CmdCode &cmd, std::string_view &ns, Args &args) { + writer_->CallRPC({[](IRPCCall* self, CmdCode& cmd, std::string_view& ns, Args& args) { auto unpacked = SharedWALRecord(self->data_).Unpack(); cmd = kCmdUpdates; args = {Arg(unpacked.upstreamLSN), Arg(unpacked.nsName), Arg(unpacked.pwalRec), Arg(unpacked.originLSN)}; @@ -34,7 +34,7 @@ void RPCUpdatesPusher::OnWALUpdate(LSNPair LSNs, std::string_view nsName, const void RPCUpdatesPusher::OnUpdatesLost(std::string_view) {} -void RPCUpdatesPusher::OnConnectionState(const Error &) {} +void RPCUpdatesPusher::OnConnectionState(const Error&) {} } // namespace cproto } // namespace net diff --git a/cpp_src/server/rpcupdatespusher.h b/cpp_src/server/rpcupdatespusher.h index eda5fc2c5..f72ec952d 100644 --- a/cpp_src/server/rpcupdatespusher.h +++ b/cpp_src/server/rpcupdatespusher.h @@ -13,16 +13,16 @@ class Writer; class RPCUpdatesPusher : public reindexer::IUpdatesObserver { public: RPCUpdatesPusher(); - void SetWriter(Writer *writer) { writer_ = writer; } - void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord &walRec) override final; + void SetWriter(Writer* writer) { writer_ = writer; } + void OnWALUpdate(LSNPair LSNs, std::string_view nsName, const WALRecord& walRec) override final; void OnUpdatesLost(std::string_view nsName) override final; - void OnConnectionState(const Error &err) override final; - void SetFilter(std::function filter) { filter_ = std::move(filter); } + void OnConnectionState(const Error& err) override final; + void SetFilter(std::function filter) { filter_ = std::move(filter); } protected: - Writer *writer_; + Writer* writer_; std::atomic seq_; - std::function filter_; + std::function filter_; }; } // namespace cproto } // namespace net diff --git a/cpp_src/server/server.cc b/cpp_src/server/server.cc index 46801ff21..46d5082ae 100644 --- a/cpp_src/server/server.cc +++ b/cpp_src/server/server.cc @@ -5,16 +5,16 @@ namespace reindexer_server { Server::Server(ServerMode mode) : impl_(new ServerImpl(mode)) {} Server::~Server() {} -Error Server::InitFromCLI(int argc, char *argv[]) { return impl_->InitFromCLI(argc, argv); } -Error Server::InitFromFile(const char *filePath) { return impl_->InitFromFile(filePath); } -Error Server::InitFromYAML(const std::string &yaml) { return impl_->InitFromYAML(yaml); } +Error Server::InitFromCLI(int argc, char* argv[]) { return impl_->InitFromCLI(argc, argv); } +Error Server::InitFromFile(const char* filePath) { return impl_->InitFromFile(filePath); } +Error Server::InitFromYAML(const std::string& yaml) { return impl_->InitFromYAML(yaml); } int Server::Start() { return impl_->Start(); } void Server::Stop() { return impl_->Stop(); } void Server::EnableHandleSignals(bool enable) noexcept { impl_->EnableHandleSignals(enable); } -DBManager &Server::GetDBManager() noexcept { return impl_->GetDBManager(); } +DBManager& Server::GetDBManager() noexcept { return impl_->GetDBManager(); } bool Server::IsReady() const noexcept { return impl_->IsReady(); } bool Server::IsRunning() const noexcept { return impl_->IsRunning(); } void Server::ReopenLogFiles() { impl_->ReopenLogFiles(); } -std::string Server::GetCoreLogPath() const { return impl_->GetCoreLogPath(); }; +std::string Server::GetCoreLogPath() const { return impl_->GetCoreLogPath(); } } // namespace reindexer_server diff --git a/cpp_src/server/server.h b/cpp_src/server/server.h index 531d76722..cacd56b96 100644 --- a/cpp_src/server/server.h +++ b/cpp_src/server/server.h @@ -28,6 +28,7 @@ class Server { bool IsRunning() const noexcept; void ReopenLogFiles(); std::string GetCoreLogPath() const; + protected: std::unique_ptr impl_; }; diff --git a/cpp_src/server/serverimpl.cc b/cpp_src/server/serverimpl.cc index 6b2d15677..c0510686b 100644 --- a/cpp_src/server/serverimpl.cc +++ b/cpp_src/server/serverimpl.cc @@ -61,7 +61,7 @@ ServerImpl::ServerImpl(ServerMode mode) async_.set(loop_); } -Error ServerImpl::InitFromCLI(int argc, char *argv[]) { +Error ServerImpl::InitFromCLI(int argc, char* argv[]) { Error err = config_.ParseCmd(argc, argv); if (!err.ok()) { if (err.code() == errParams) { @@ -75,7 +75,7 @@ Error ServerImpl::InitFromCLI(int argc, char *argv[]) { return init(); } -Error ServerImpl::InitFromFile(const char *filePath) { +Error ServerImpl::InitFromFile(const char* filePath) { Error err = config_.ParseFile(filePath); if (!err.ok()) { if (err.code() == errParams) { @@ -89,7 +89,7 @@ Error ServerImpl::InitFromFile(const char *filePath) { return init(); } -Error ServerImpl::InitFromYAML(const std::string &yaml) { +Error ServerImpl::InitFromYAML(const std::string& yaml) { Error err = config_.ParseYaml(yaml); if (!err.ok()) { if (err.code() == errParams) { @@ -118,19 +118,25 @@ Error ServerImpl::init() { GetDirPath(config_.CoreLog), GetDirPath(config_.HttpLog), GetDirPath(config_.RpcLog), GetDirPath(config_.ServerLog), config_.StoragePath}; - for (const std::string &dir : dirs) { + for (const std::string& dir : dirs) { err = TryCreateDirectory(dir); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } #ifndef _WIN32 err = ChownDir(dir, config_.UserName); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } #endif } #ifndef _WIN32 if (!config_.UserName.empty()) { err = ChangeUser(config_.UserName.c_str()); - if (!err.ok()) return err; + if (!err.ok()) { + return err; + } } signal(SIGPIPE, SIG_IGN); #endif @@ -166,7 +172,7 @@ int ServerImpl::Start() { }); if (config_.InstallSvc) { - auto &args = config_.Args(); + auto& args = config_.Args(); std::string cmdline = args.front(); for (size_t i = 1; i < args.size(); i++) { cmdline += " "; @@ -196,7 +202,7 @@ void ServerImpl::Stop() { void ServerImpl::ReopenLogFiles() { #ifndef _WIN32 - for (auto &sync : sinks_) { + for (auto& sync : sinks_) { sync.second->reopen(); } #endif @@ -273,7 +279,7 @@ int ServerImpl::run() { heapWatcher = TCMallocHeapWathcher(alloc_ext::instance(), config_.AllocatorCacheLimit, config_.AllocatorCachePart, spdlog::get("server")); tcmallocHeapWatchDog.set(loop_); - tcmallocHeapWatchDog.set([&heapWatcher](ev::timer &, int) { heapWatcher.CheckHeapUsagePeriodic(); }); + tcmallocHeapWatchDog.set([&heapWatcher](ev::timer&, int) { heapWatcher.CheckHeapUsagePeriodic(); }); if (config_.AllocatorCacheLimit > 0 || config_.AllocatorCachePart > 0) { using fpSeconds = std::chrono::duration; @@ -342,7 +348,7 @@ int ServerImpl::run() { return EXIT_FAILURE; } #if defined(WITH_GRPC) - void *hGRPCService = nullptr; + void* hGRPCService = nullptr; #if REINDEX_WITH_LIBDL #ifdef __APPLE__ auto hGRPCServiceLib = dlopen("libreindexer_grpc_library.dylib", RTLD_NOW); @@ -366,7 +372,7 @@ int ServerImpl::run() { #endif #endif - auto sigCallback = [&](ev::sig &sig) { + auto sigCallback = [&](ev::sig& sig) { logger_.info("Signal received. Terminating..."); #ifndef REINDEX_WITH_ASAN if (config_.AllowNamespaceLeak && mode_ == ServerMode::Standalone) { @@ -377,7 +383,9 @@ int ServerImpl::run() { sig.loop.break_loop(); }; - if (statsCollector) statsCollector->Start(); + if (statsCollector) { + statsCollector->Start(); + } ev::sig sterm, sint, shup; @@ -389,7 +397,7 @@ int ServerImpl::run() { sint.set(sigCallback); sint.start(SIGINT); #ifndef _WIN32 - auto sigHupCallback = [&](ev::sig &sig) { + auto sigHupCallback = [&](ev::sig& sig) { (void)sig; ReopenLogFiles(); }; @@ -399,7 +407,7 @@ int ServerImpl::run() { #endif } - async_.set([](ev::async &a) { a.loop.break_loop(); }); + async_.set([](ev::async& a) { a.loop.break_loop(); }); async_.start(); running_ = true; @@ -408,7 +416,9 @@ int ServerImpl::run() { } logger_.info("Reindexer server terminating..."); - if (statsCollector) statsCollector->Stop(); + if (statsCollector) { + statsCollector->Stop(); + } logger_.info("Stats collector shutdown completed."); if (rpcServerUnix) { rpcServerUnix->Stop(); @@ -433,7 +443,7 @@ int ServerImpl::run() { #endif #endif - } catch (const Error &err) { + } catch (const Error& err) { logger_.error("Unhandled exception occurred: {0}", err.what()); } logger_.info("Reindexer server shutdown completed."); @@ -452,7 +462,9 @@ Error ServerImpl::daemonize() { switch (pid) { // child process case 0: - if (!pid_.Open(config_.DaemonPidFile.c_str())) return pid_.Status(); + if (!pid_.Open(config_.DaemonPidFile.c_str())) { + return pid_.Status(); + } umask(0); setsid(); if (chdir("/")) { @@ -488,8 +500,8 @@ Error ServerImpl::loggerConfigure() { std::vector> loggers = { {"server", config_.ServerLog}, {"core", config_.CoreLog}, {"http", config_.HttpLog}, {"rpc", config_.RpcLog}}; - for (auto &logger : loggers) { - auto &fileName = logger.second; + for (auto& logger : loggers) { + auto& fileName = logger.second; try { if (fileName == "stdout" || fileName == "-") { spdlog::stdout_color_mt(logger.first); @@ -500,7 +512,7 @@ Error ServerImpl::loggerConfigure() { } spdlog::create(logger.first, sink->second); } - } catch (const spdlog::spdlog_ex &e) { + } catch (const spdlog::spdlog_ex& e) { return Error(errLogic, "Can't create logger for '%s' to file '%s': %s\n", logger.first, logger.second, e.what()); } } @@ -511,7 +523,7 @@ Error ServerImpl::loggerConfigure() { void ServerImpl::initCoreLogger() { std::weak_ptr logger = spdlog::get("core"); - auto callback = [this, logger](int level, char *buf) { + auto callback = [this, logger](int level, char* buf) { auto slogger = logger.lock(); if (slogger && level <= coreLogLevel_) { switch (level) { diff --git a/cpp_src/server/statscollect/prometheus.h b/cpp_src/server/statscollect/prometheus.h index 36829a47d..921a17ad8 100644 --- a/cpp_src/server/statscollect/prometheus.h +++ b/cpp_src/server/statscollect/prometheus.h @@ -18,62 +18,62 @@ class Prometheus { using PRegistry = prometheus::Registry; using PCollectable = prometheus::Collectable; - void Attach(http::Router &router); - void RegisterQPS(const std::string &db, const std::string &ns, std::string_view queryType, size_t qps) { + void Attach(http::Router& router); + void RegisterQPS(const std::string& db, const std::string& ns, std::string_view queryType, size_t qps) { setMetricValue(qps_, qps, currentEpoch_, db, ns, queryType); } - void RegisterLatency(const std::string &db, const std::string &ns, std::string_view queryType, size_t latencyUS) { + void RegisterLatency(const std::string& db, const std::string& ns, std::string_view queryType, size_t latencyUS) { setMetricValue(latency_, static_cast(latencyUS) / 1e6, currentEpoch_, db, ns, queryType); } - void RegisterCachesSize(const std::string &db, const std::string &ns, size_t size) { + void RegisterCachesSize(const std::string& db, const std::string& ns, size_t size) { setMetricValue(caches_, size, currentEpoch_, db, ns); } - void RegisterIndexesSize(const std::string &db, const std::string &ns, size_t size) { + void RegisterIndexesSize(const std::string& db, const std::string& ns, size_t size) { setMetricValue(indexes_, size, currentEpoch_, db, ns); } - void RegisterDataSize(const std::string &db, const std::string &ns, size_t size) { setMetricValue(data_, size, currentEpoch_, db, ns); } - void RegisterItemsCount(const std::string &db, const std::string &ns, size_t count) { + void RegisterDataSize(const std::string& db, const std::string& ns, size_t size) { setMetricValue(data_, size, currentEpoch_, db, ns); } + void RegisterItemsCount(const std::string& db, const std::string& ns, size_t count) { setMetricValue(itemsCount_, count, currentEpoch_, db, ns); } void RegisterAllocatedMemory(size_t memoryConsumationBytes) { setMetricValue(memory_, memoryConsumationBytes, prometheus::kNoEpoch); } - void RegisterRPCClients(const std::string &db, std::string_view protocol, size_t count) { + void RegisterRPCClients(const std::string& db, std::string_view protocol, size_t count) { setNetMetricValue(rpcClients_, count, currentEpoch_, db, std::string_view(), protocol); } - void RegisterInputTraffic(const std::string &db, std::string_view type, std::string_view protocol, size_t bytes) { + void RegisterInputTraffic(const std::string& db, std::string_view type, std::string_view protocol, size_t bytes) { setNetMetricValue(inputTraffic_, bytes, prometheus::kNoEpoch, db, type, protocol); } - void RegisterOutputTraffic(const std::string &db, std::string_view type, std::string_view protocol, size_t bytes) { + void RegisterOutputTraffic(const std::string& db, std::string_view type, std::string_view protocol, size_t bytes) { setNetMetricValue(outputTraffic_, bytes, prometheus::kNoEpoch, db, type, protocol); } - void RegisterStorageStatus(const std::string &db, const std::string &ns, bool isOK) { + void RegisterStorageStatus(const std::string& db, const std::string& ns, bool isOK) { setMetricValue(storageStatus_, isOK ? 1.0 : 0.0, prometheus::kNoEpoch, db, ns); } void NextEpoch(); private: - static void setMetricValue(PFamily *metricFamily, double value, int64_t epoch); - static void setMetricValue(PFamily *metricFamily, double value, int64_t epoch, const std::string &db, const std::string &ns, + static void setMetricValue(PFamily* metricFamily, double value, int64_t epoch); + static void setMetricValue(PFamily* metricFamily, double value, int64_t epoch, const std::string& db, const std::string& ns, std::string_view queryType = ""); - static void setNetMetricValue(PFamily *metricFamily, double value, int64_t epoch, const std::string &db, std::string_view type, + static void setNetMetricValue(PFamily* metricFamily, double value, int64_t epoch, const std::string& db, std::string_view type, std::string_view protocol); void fillRxInfo(); - int collect(http::Context &ctx); + int collect(http::Context& ctx); PRegistry registry_; int64_t currentEpoch_ = 1; - PFamily *qps_{nullptr}; - PFamily *latency_{nullptr}; - PFamily *caches_{nullptr}; - PFamily *indexes_{nullptr}; - PFamily *data_{nullptr}; - PFamily *memory_{nullptr}; - PFamily *rpcClients_{nullptr}; - PFamily *inputTraffic_{nullptr}; - PFamily *outputTraffic_{nullptr}; - PFamily *storageStatus_{nullptr}; - PFamily *itemsCount_{nullptr}; - PFamily *rxInfo_{nullptr}; + PFamily* qps_{nullptr}; + PFamily* latency_{nullptr}; + PFamily* caches_{nullptr}; + PFamily* indexes_{nullptr}; + PFamily* data_{nullptr}; + PFamily* memory_{nullptr}; + PFamily* rpcClients_{nullptr}; + PFamily* inputTraffic_{nullptr}; + PFamily* outputTraffic_{nullptr}; + PFamily* storageStatus_{nullptr}; + PFamily* itemsCount_{nullptr}; + PFamily* rxInfo_{nullptr}; }; } // namespace reindexer_server diff --git a/cpp_src/server/winservice.cc b/cpp_src/server/winservice.cc index 3c36f8349..e9dd8892f 100644 --- a/cpp_src/server/winservice.cc +++ b/cpp_src/server/winservice.cc @@ -13,11 +13,13 @@ namespace reindexer_server { #define SERVER_STOP_WAIT 5000 #define SERVER_START_WAIT 5000 -WinService *g_Service; +WinService* g_Service; static std::string GetLastErrorAsString() { // Get the error message, if any. DWORD errorMessageID = ::GetLastError(); - if (errorMessageID == 0) return std::string(); // No error message has been recorded + if (errorMessageID == 0) { + return std::string(); // No error message has been recorded + } LPSTR messageBuffer = nullptr; size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, @@ -30,7 +32,7 @@ static std::string GetLastErrorAsString() { return message; } -WinService::WinService(const std::string &name, const std::string &displayName, std::function run, +WinService::WinService(const std::string& name, const std::string& displayName, std::function run, std::function terminate, std::function status) : name_(name), displayName_(displayName), run_(run), terminate_(terminate), status_(status) { g_Service = this; @@ -39,7 +41,7 @@ WinService::WinService(const std::string &name, const std::string &displayName, WinService::~WinService() { g_Service = 0; } -void WinService::Message(bool bError, const char *fmt, ...) { +void WinService::Message(bool bError, const char* fmt, ...) { va_list va; va_start(va, fmt); char tempBuf[4096]; @@ -95,24 +97,26 @@ void WinService::MainInternal(DWORD dwArgc, LPTSTR lpszArgv[]) { bool WinService::ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint) { static DWORD dwCheckPoint = 1; - if (dwCurrentState == SERVICE_START_PENDING) + if (dwCurrentState == SERVICE_START_PENDING) { ssStatus_.dwControlsAccepted = 0; - else + } else { ssStatus_.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; + } ssStatus_.dwCurrentState = dwCurrentState; ssStatus_.dwWin32ExitCode = dwWin32ExitCode; ssStatus_.dwWaitHint = dwWaitHint; - if ((dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED)) + if ((dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED)) { ssStatus_.dwCheckPoint = 0; - else + } else { ssStatus_.dwCheckPoint = dwCheckPoint++; + } return SetServiceStatus(sshStatusHandle_, &ssStatus_); } -bool WinService::Install(const char *cmdline) { +bool WinService::Install(const char* cmdline) { SC_HANDLE schService = NULL, schSCManager = NULL; Remove(true); @@ -136,8 +140,9 @@ bool WinService::Install(const char *cmdline) { CloseServiceHandle(schSCManager); return true; - } else + } else { Message(false, "CreateService failed:\n%s\n", GetLastErrorAsString().c_str()); + } CloseServiceHandle(schSCManager); } else { @@ -147,7 +152,7 @@ bool WinService::Install(const char *cmdline) { } int WinService::Start() { - SERVICE_TABLE_ENTRY DispTable[] = {{const_cast(name_.c_str()), ServiceMain}, {NULL, NULL}}; + SERVICE_TABLE_ENTRY DispTable[] = {{const_cast(name_.c_str()), ServiceMain}, {NULL, NULL}}; StartServiceCtrlDispatcher(DispTable); return 0; } @@ -165,8 +170,9 @@ bool WinService::Remove(bool silent) { while (QueryServiceStatus(schService, &ssStatus_)) { if (ssStatus_.dwCurrentState == SERVICE_STOP_PENDING) { Sleep(1000); - } else + } else { break; + } } } @@ -175,16 +181,19 @@ bool WinService::Remove(bool silent) { CloseServiceHandle(schSCManager); return true; - } else if (!silent) + } else if (!silent) { Message(true, "DeleteService failed:\n%s\n", GetLastErrorAsString().c_str()); + } CloseServiceHandle(schService); - } else if (!silent) + } else if (!silent) { Message(true, "OpenService failed:\n%s\n", GetLastErrorAsString().c_str()); + } CloseServiceHandle(schSCManager); - } else if (!silent) + } else if (!silent) { Message(true, "OpenSCManager failed:\n%s\n", GetLastErrorAsString().c_str()); + } return false; } diff --git a/cpp_src/server/winservice.h b/cpp_src/server/winservice.h index 1571d8497..262e7b898 100644 --- a/cpp_src/server/winservice.h +++ b/cpp_src/server/winservice.h @@ -12,12 +12,12 @@ namespace reindexer_server { class WinService { public: - WinService(const std::string &name, const std::string &displayName, std::function run, std::function terminate, + WinService(const std::string& name, const std::string& displayName, std::function run, std::function terminate, std::function status); virtual ~WinService(); - void Message(bool bError, const char *fmt, ...); + void Message(bool bError, const char* fmt, ...); int Start(); - bool Install(const char *cmdline); + bool Install(const char* cmdline); bool Remove(bool silent = false); void ServiceCtrl(DWORD dwCtrlCode); diff --git a/cpp_src/tools/alloc_ext/je_malloc_extension.cc b/cpp_src/tools/alloc_ext/je_malloc_extension.cc index b4baaee40..cdc5cbdff 100644 --- a/cpp_src/tools/alloc_ext/je_malloc_extension.cc +++ b/cpp_src/tools/alloc_ext/je_malloc_extension.cc @@ -6,8 +6,8 @@ #include #include #include -#endif // _WIN32 -#endif // REINDEX_WITH_JEMALLOC && !defined(_WIN32) +#endif // _WIN32 +#endif // REINDEX_WITH_JEMALLOC && !defined(_WIN32) namespace reindexer { namespace alloc_ext { @@ -20,14 +20,14 @@ bool JEMallocIsAvailable() { return true; } #elif REINDEX_WITH_JEMALLOC -using MallctlFn = int (*)(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen); +using MallctlFn = int (*)(const char* name, void* oldp, size_t* oldlenp, void* newp, size_t newlen); static MallctlFn getMallctlFn() { static auto getInstanceFn = reinterpret_cast(dlsym(RTLD_DEFAULT, "mallctl")); return getInstanceFn; } -int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen) { +int mallctl(const char* name, void* oldp, size_t* oldlenp, void* newp, size_t newlen) { auto getInstanceFn = getMallctlFn(); if (getInstanceFn) { return getInstanceFn(name, oldp, oldlenp, newp, newlen); @@ -40,7 +40,7 @@ bool JEMallocIsAvailable() { return (getMallctlFn() != nullptr); } #else // suppress clang warngig int ___je_malloc_extension_dummy_suppress_warning; -#endif // REINDEX_WITH_JEMALLOC +#endif // REINDEX_WITH_JEMALLOC } // namespace alloc_ext } // namespace reindexer diff --git a/cpp_src/tools/alloc_ext/je_malloc_extension.h b/cpp_src/tools/alloc_ext/je_malloc_extension.h index af78b7a2a..b43b39ebe 100644 --- a/cpp_src/tools/alloc_ext/je_malloc_extension.h +++ b/cpp_src/tools/alloc_ext/je_malloc_extension.h @@ -7,11 +7,11 @@ namespace reindexer { namespace alloc_ext { -int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen); +int mallctl(const char* name, void* oldp, size_t* oldlenp, void* newp, size_t newlen); bool JEMallocIsAvailable(); } // namespace alloc_ext } // namespace reindexer -#endif // REINDEX_WITH_JEMALLOC +#endif // REINDEX_WITH_JEMALLOC diff --git a/cpp_src/tools/alloc_ext/tc_malloc_extension.cc b/cpp_src/tools/alloc_ext/tc_malloc_extension.cc index 32964d311..d6afdde4d 100644 --- a/cpp_src/tools/alloc_ext/tc_malloc_extension.cc +++ b/cpp_src/tools/alloc_ext/tc_malloc_extension.cc @@ -4,7 +4,7 @@ #include #include #include -#endif // REINDEX_WITH_GPERFTOOLS && !defined(_WIN32) +#endif // REINDEX_WITH_GPERFTOOLS && !defined(_WIN32) namespace reindexer { namespace alloc_ext { @@ -33,7 +33,7 @@ MallocExtension* instance() { bool TCMallocIsAvailable() { return (getGetInstanceFn() != nullptr); } -#endif // REINDEX_WITH_GPERFTOOLS +#endif // REINDEX_WITH_GPERFTOOLS } // namespace alloc_ext } // namespace reindexer diff --git a/cpp_src/tools/alloc_ext/tc_malloc_extension.h b/cpp_src/tools/alloc_ext/tc_malloc_extension.h index 5fabb41db..fdc9f7550 100644 --- a/cpp_src/tools/alloc_ext/tc_malloc_extension.h +++ b/cpp_src/tools/alloc_ext/tc_malloc_extension.h @@ -17,4 +17,4 @@ int MallocHook_AddDeleteHook(MallocHook_DeleteHook hook); } // namespace alloc_ext } // namespace reindexer -#endif // REINDEX_WITH_GPERFTOOLS +#endif // REINDEX_WITH_GPERFTOOLS diff --git a/cpp_src/tools/alloc_ext/tc_malloc_hook.cc b/cpp_src/tools/alloc_ext/tc_malloc_hook.cc index 41abc0688..de2ddaa72 100644 --- a/cpp_src/tools/alloc_ext/tc_malloc_hook.cc +++ b/cpp_src/tools/alloc_ext/tc_malloc_hook.cc @@ -4,7 +4,7 @@ #include #include #include -#endif // REINDEX_WITH_GPERFTOOLS +#endif // REINDEX_WITH_GPERFTOOLS namespace reindexer { namespace alloc_ext { @@ -44,7 +44,7 @@ int MallocHook_AddDeleteHook(MallocHook_DeleteHook hook) { bool TCMallocHooksAreAvailable() { return (getAddNewHookFn() != nullptr) && (getAddDeleteHookFn() != nullptr); } -#endif // REINDEX_WITH_GPERFTOOLS +#endif // REINDEX_WITH_GPERFTOOLS } // namespace alloc_ext } // namespace reindexer diff --git a/cpp_src/tools/assertrx.cc b/cpp_src/tools/assertrx.cc index aa468d08b..0a8788e6b 100644 --- a/cpp_src/tools/assertrx.cc +++ b/cpp_src/tools/assertrx.cc @@ -8,7 +8,7 @@ namespace reindexer { -void fail_assertrx(const char *assertion, const char *file, unsigned line, const char *function) noexcept { +void fail_assertrx(const char* assertion, const char* file, unsigned line, const char* function) noexcept { auto msg = fmt::sprintf("Assertion failed: %s (%s:%u: %s)", assertion, file, line, function); std::cerr << msg << "\n"; debug::backtrace_set_assertion_message(std::move(msg)); @@ -16,7 +16,7 @@ void fail_assertrx(const char *assertion, const char *file, unsigned line, const std::abort(); } -void fail_throwrx(const char *assertion, const char *file, unsigned line, const char *function) { +void fail_throwrx(const char* assertion, const char* file, unsigned line, const char* function) { std::string errText{fmt::sprintf("Assertion failed (handled via exception): %s (%s:%u: %s)\n", assertion, file, line, function)}; std::cerr << errText; debug::print_crash_query(std::cerr); diff --git a/cpp_src/tools/assertrx.h b/cpp_src/tools/assertrx.h index fc8d76875..a3c80388b 100644 --- a/cpp_src/tools/assertrx.h +++ b/cpp_src/tools/assertrx.h @@ -4,7 +4,7 @@ namespace reindexer { -[[noreturn]] void fail_throwrx (const char *assertion, const char *file, unsigned line, const char *function) noexcept(false); +[[noreturn]] void fail_throwrx(const char* assertion, const char* file, unsigned line, const char* function) noexcept(false); #define throw_assert(expr) reindexer::fail_throwrx(#expr, __FILE__, __LINE__, __FUNCTION__) #define throw_as_assert throw_assert(false) @@ -17,7 +17,7 @@ namespace reindexer { // fail_assertrx can actually throw, but this exception can not be handled properly, // so it was marked as 'noexcept' for the optimization purposes -[[noreturn]] void fail_assertrx(const char *assertion, const char *file, unsigned line, const char *function) noexcept; +[[noreturn]] void fail_assertrx(const char* assertion, const char* file, unsigned line, const char* function) noexcept; #ifdef __cplusplus #define assertrx(expr) (rx_likely(static_cast(expr)) ? void(0) : reindexer::fail_assertrx(#expr, __FILE__, __LINE__, __FUNCTION__)) diff --git a/cpp_src/tools/customlocal.cc b/cpp_src/tools/customlocal.cc index bd44b2ea4..4761e6cbe 100644 --- a/cpp_src/tools/customlocal.cc +++ b/cpp_src/tools/customlocal.cc @@ -201,7 +201,9 @@ constexpr std::pair kAlphabet[] = { constexpr int checkAlphabetSorted() { for (int i = 1; i < std::end(kAlphabet) - std::begin(kAlphabet); i++) { - if (kAlphabet[i - 1] >= kAlphabet[i]) return i; + if (kAlphabet[i - 1] >= kAlphabet[i]) { + return i; + } } return -1; } diff --git a/cpp_src/tools/errors.h b/cpp_src/tools/errors.h index 43376cba6..bce85e26a 100644 --- a/cpp_src/tools/errors.h +++ b/cpp_src/tools/errors.h @@ -15,7 +15,7 @@ namespace reindexer { #if defined(REINDEX_CORE_BUILD) template -void assertf_fmt(const char *fmt, const Args &...args) { +void assertf_fmt(const char* fmt, const Args&... args) { fmt::fprintf(std::cerr, fmt, args...); } #if defined(NDEBUG) @@ -75,7 +75,7 @@ class Error { // TODO: Enable nodiscard once python binding will be updated } } } - Error(ErrorCode code, const char *what) noexcept : code_{code} { + Error(ErrorCode code, const char* what) noexcept : code_{code} { if (code_ != errOK) { try { what_ = make_intrusive(what); @@ -84,26 +84,26 @@ class Error { // TODO: Enable nodiscard once python binding will be updated } } } - Error(const std::exception &e) noexcept : code_{errSystem} { + Error(const std::exception& e) noexcept : code_{errSystem} { try { what_ = make_intrusive(e.what()); } catch (...) { what_ = defaultErrorText_; } } - Error(const Error &) noexcept = default; - Error(Error &&) noexcept = default; - Error &operator=(const Error &) noexcept = default; - Error &operator=(Error &&) noexcept = default; + Error(const Error&) noexcept = default; + Error(Error&&) noexcept = default; + Error& operator=(const Error&) noexcept = default; + Error& operator=(Error&&) noexcept = default; #ifdef REINDEX_CORE_BUILD template - Error(ErrorCode code, const char *fmt, const Args &...args) noexcept : code_{code} { + Error(ErrorCode code, const char* fmt, const Args&... args) noexcept : code_{code} { if (code_ != errOK) { try { try { what_ = make_intrusive(fmt::sprintf(fmt, args...)); - } catch (const fmt::FormatError &) { + } catch (const fmt::FormatError&) { assertf_dbg(false, "Incorrect error format: '%s'", fmt); what_ = make_intrusive(fmt); } @@ -114,7 +114,7 @@ class Error { // TODO: Enable nodiscard once python binding will be updated } #endif // REINDEX_CORE_BUILD - [[nodiscard]] const std::string &what() const & noexcept { + [[nodiscard]] const std::string& what() const& noexcept { static const std::string noerr; return what_ ? *what_ : noerr; } @@ -129,8 +129,8 @@ class Error { // TODO: Enable nodiscard once python binding will be updated [[nodiscard]] bool ok() const noexcept { return code_ == errOK; } explicit operator bool() const noexcept { return !ok(); } - [[nodiscard]] bool operator==(const Error &other) const noexcept { return code() == other.code() && what() == other.what(); } - [[nodiscard]] bool operator!=(const Error &other) const noexcept { return !(*this == other); } + [[nodiscard]] bool operator==(const Error& other) const noexcept { return code() == other.code() && what() == other.what(); } + [[nodiscard]] bool operator!=(const Error& other) const noexcept { return !(*this == other); } private: WhatPtr what_; diff --git a/cpp_src/tools/frozen_str_tools.h b/cpp_src/tools/frozen_str_tools.h index 29075237b..505ad7374 100644 --- a/cpp_src/tools/frozen_str_tools.h +++ b/cpp_src/tools/frozen_str_tools.h @@ -10,7 +10,9 @@ namespace frozen { template constexpr std::size_t hash_ascii_string_nocase(const String& value, std::size_t seed) { std::size_t d = (0x811c9dc5 ^ seed) * static_cast(0x01000193); - for (const auto& c : value) d = (d ^ (static_cast(c) | 0x20)) * static_cast(0x01000193); + for (const auto& c : value) { + d = (d ^ (static_cast(c) | 0x20)) * static_cast(0x01000193); + } return d >> 8; } diff --git a/cpp_src/tools/fsops.cc b/cpp_src/tools/fsops.cc index bb3ba36a6..d76ba7f5d 100644 --- a/cpp_src/tools/fsops.cc +++ b/cpp_src/tools/fsops.cc @@ -13,31 +13,35 @@ namespace reindexer { namespace fs { -[[nodiscard]] int MkDirAll(const std::string &path) noexcept { +[[nodiscard]] int MkDirAll(const std::string& path) noexcept { try { std::string tmpStr = path; char *p = nullptr, *tmp = tmpStr.data(); int err; const auto len = tmpStr.size(); - if (tmp[len - 1] == '/' || tmp[len - 1] == '\\') tmp[len - 1] = 0; + if (tmp[len - 1] == '/' || tmp[len - 1] == '\\') { + tmp[len - 1] = 0; + } for (p = tmp + 1; *p; p++) { if (*p == '/' || *p == '\\') { *p = 0; err = mkdir(tmp, S_IRWXU); - if ((err < 0) && (errno != EEXIST)) return err; + if ((err < 0) && (errno != EEXIST)) { + return err; + } *p = '/'; } } return ((mkdir(tmp, S_IRWXU) < 0) && errno != EEXIST) ? -1 : 0; - } catch (std::exception &) { + } catch (std::exception&) { return -1; } } -int RmDirAll(const std::string &path) noexcept { +int RmDirAll(const std::string& path) noexcept { #ifndef _WIN32 return nftw( - path.c_str(), [](const char *fpath, const struct stat *, int, struct FTW *) { return ::remove(fpath); }, 64, FTW_DEPTH | FTW_PHYS); + path.c_str(), [](const char* fpath, const struct stat*, int, struct FTW*) { return ::remove(fpath); }, 64, FTW_DEPTH | FTW_PHYS); #else WIN32_FIND_DATA entry; if (HANDLE hFind = FindFirstFile((path + "/*.*").c_str(), &entry); hFind != INVALID_HANDLE_VALUE) { @@ -73,9 +77,9 @@ int RmDirAll(const std::string &path) noexcept { #endif } -[[nodiscard]] int ReadFile(const std::string &path, std::string &content) noexcept { +[[nodiscard]] int ReadFile(const std::string& path, std::string& content) noexcept { try { - FILE *f = fopen(path.c_str(), "rb"); + FILE* f = fopen(path.c_str(), "rb"); if (!f) { return -1; } @@ -86,13 +90,13 @@ int RmDirAll(const std::string &path) noexcept { auto nread = fread(&content[0], 1, sz, f); fclose(f); return nread; - } catch (std::exception &) { + } catch (std::exception&) { return -1; } } -[[nodiscard]] int64_t WriteFile(const std::string &path, std::string_view content) noexcept { - FILE *f = fopen(path.c_str(), "w"); +[[nodiscard]] int64_t WriteFile(const std::string& path, std::string_view content) noexcept { + FILE* f = fopen(path.c_str(), "w"); if (!f) { return -1; } @@ -102,9 +106,9 @@ int RmDirAll(const std::string &path) noexcept { return static_cast((written > 0) ? content.size() : written); } -[[nodiscard]] int ReadDir(const std::string &path, std::vector &content) noexcept { +[[nodiscard]] int ReadDir(const std::string& path, std::vector& content) noexcept { #ifndef _WIN32 - struct dirent *entry; + struct dirent* entry; auto dir = opendir(path.c_str()); if (!dir) { @@ -193,36 +197,44 @@ std::string GetTempDir() { ::GetTempPathA(sizeof(tmpBuf), tmpBuf); return tmpBuf; #else - const char *tmpDir = getenv("TMPDIR"); - if (tmpDir && *tmpDir) return tmpDir; + const char* tmpDir = getenv("TMPDIR"); + if (tmpDir && *tmpDir) { + return tmpDir; + } return "/tmp"; #endif } -void SetTempDir(std::string &&dir) noexcept { +void SetTempDir(std::string&& dir) noexcept { std::lock_guard lck(tmpDirMtx); tmpDir = std::move(dir); } std::string GetHomeDir() { - const char *homeDir = getenv("HOME"); - if (homeDir && *homeDir) return homeDir; + const char* homeDir = getenv("HOME"); + if (homeDir && *homeDir) { + return homeDir; + } return "."; } -FileStatus Stat(const std::string &path) { +FileStatus Stat(const std::string& path) { #ifdef _WIN32 struct _stat state; - if (_stat(path.c_str(), &state) < 0) return StatError; + if (_stat(path.c_str(), &state) < 0) { + return StatError; + } return (state.st_mode & _S_IFDIR) ? StatDir : StatFile; #else struct stat state; - if (stat(path.c_str(), &state) < 0) return StatError; + if (stat(path.c_str(), &state) < 0) { + return StatError; + } return S_ISDIR(state.st_mode) ? StatDir : StatFile; #endif } -TimeStats StatTime(const std::string &path) { +TimeStats StatTime(const std::string& path) { #ifdef _WIN32 FILETIME ftCreate, ftAccess, ftWrite; HANDLE hFile = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); @@ -256,32 +268,38 @@ TimeStats StatTime(const std::string &path) { return {-1, -1, -1}; } -[[nodiscard]] bool DirectoryExists(const std::string &directory) noexcept { +[[nodiscard]] bool DirectoryExists(const std::string& directory) noexcept { if (!directory.empty()) { #ifdef _WIN32 if (_access(directory.c_str(), 0) == 0) { struct _stat status; _stat(directory.c_str(), &status); - if (status.st_mode & _S_IFDIR) return true; + if (status.st_mode & _S_IFDIR) { + return true; + } } #else if (access(directory.c_str(), F_OK) == 0) { struct stat status; stat(directory.c_str(), &status); - if (status.st_mode & S_IFDIR) return true; + if (status.st_mode & S_IFDIR) { + return true; + } } #endif } return false; } -Error TryCreateDirectory(const std::string &dir) { +Error TryCreateDirectory(const std::string& dir) { using reindexer::fs::MkDirAll; using reindexer::fs::DirectoryExists; using reindexer::fs::GetTempDir; if (!dir.empty()) { if (!DirectoryExists(dir) && dir != GetTempDir()) { - if (MkDirAll(dir) < 0) return Error(errLogic, "Could not create '%s'. Reason: %s\n", dir.c_str(), strerror(errno)); + if (MkDirAll(dir) < 0) { + return Error(errLogic, "Could not create '%s'. Reason: %s\n", dir.c_str(), strerror(errno)); + } #ifdef _WIN32 } else if (_access(dir.c_str(), 6) < 0) { #else @@ -293,12 +311,12 @@ Error TryCreateDirectory(const std::string &dir) { return {}; } -std::string GetDirPath(const std::string &path) { +std::string GetDirPath(const std::string& path) { size_t lastSlashPos = path.find_last_of("/\\"); return lastSlashPos == std::string::npos ? std::string() : path.substr(0, lastSlashPos + 1); } -Error ChownDir(const std::string &path, const std::string &user) { +Error ChownDir(const std::string& path, const std::string& user) { #ifndef _WIN32 if (!user.empty() && !path.empty()) { struct passwd pwd, *usr; @@ -327,7 +345,7 @@ Error ChownDir(const std::string &path, const std::string &user) { return {}; } -Error ChangeUser(const char *userName) { +Error ChangeUser(const char* userName) { #ifndef _WIN32 struct passwd pwd, *result; char buf[0x4000]; @@ -342,30 +360,42 @@ Error ChangeUser(const char *userName) { } } - if (setgid(pwd.pw_gid) != 0) return Error(errLogic, "Could not change user to `%s`. Reason: %s", userName, strerror(errno)); - if (setuid(pwd.pw_uid) != 0) return Error(errLogic, "Could not change user to `%s`. Reason: %s", userName, strerror(errno)); + if (setgid(pwd.pw_gid) != 0) { + return Error(errLogic, "Could not change user to `%s`. Reason: %s", userName, strerror(errno)); + } + if (setuid(pwd.pw_uid) != 0) { + return Error(errLogic, "Could not change user to `%s`. Reason: %s", userName, strerror(errno)); + } #else (void)userName; #endif return {}; } -std::string GetRelativePath(const std::string &path, unsigned maxUp) { +std::string GetRelativePath(const std::string& path, unsigned maxUp) { std::string cwd = GetCwd(); unsigned same = 0, slashes = 0; for (; same < std::min(cwd.size(), path.size()) && cwd[same] == path[same]; ++same) { } for (unsigned i = same; i < cwd.size(); ++i) { - if (cwd[i] == '/' || i == same) slashes++; + if (cwd[i] == '/' || i == same) { + slashes++; + } + } + if (!slashes && same < path.size()) { + same++; } - if (!slashes && same < path.size()) same++; - if (same < 2 || (slashes > maxUp)) return path; + if (same < 2 || (slashes > maxUp)) { + return path; + } std::string rpath; rpath.reserve(slashes * 3 + path.size() - same + 1); - while (slashes--) rpath += "../"; + while (slashes--) { + rpath += "../"; + } rpath.append(path.begin() + same, path.end()); return rpath; } diff --git a/cpp_src/tools/fsops.h b/cpp_src/tools/fsops.h index 015ef0a66..7f615bbac 100644 --- a/cpp_src/tools/fsops.h +++ b/cpp_src/tools/fsops.h @@ -26,27 +26,27 @@ struct TimeStats { int64_t mtime; }; -[[nodiscard]] int MkDirAll(const std::string &path) noexcept; -int RmDirAll(const std::string &path) noexcept; -[[nodiscard]] int ReadFile(const std::string &path, std::string &content) noexcept; -[[nodiscard]] int64_t WriteFile(const std::string &path, std::string_view content) noexcept; -[[nodiscard]] int ReadDir(const std::string &path, std::vector &content) noexcept; -[[nodiscard]] bool DirectoryExists(const std::string &directory) noexcept; -FileStatus Stat(const std::string &path); -TimeStats StatTime(const std::string &path); +[[nodiscard]] int MkDirAll(const std::string& path) noexcept; +int RmDirAll(const std::string& path) noexcept; +[[nodiscard]] int ReadFile(const std::string& path, std::string& content) noexcept; +[[nodiscard]] int64_t WriteFile(const std::string& path, std::string_view content) noexcept; +[[nodiscard]] int ReadDir(const std::string& path, std::vector& content) noexcept; +[[nodiscard]] bool DirectoryExists(const std::string& directory) noexcept; +FileStatus Stat(const std::string& path); +TimeStats StatTime(const std::string& path); std::string GetCwd(); -std::string GetDirPath(const std::string &path); +std::string GetDirPath(const std::string& path); std::string GetTempDir(); -void SetTempDir(std::string &&dir) noexcept; +void SetTempDir(std::string&& dir) noexcept; std::string GetHomeDir(); -std::string GetRelativePath(const std::string &path, unsigned maxUp = 1024); -inline int Rename(const std::string &from, const std::string &to) { return rename(from.c_str(), to.c_str()); } +std::string GetRelativePath(const std::string& path, unsigned maxUp = 1024); +inline int Rename(const std::string& from, const std::string& to) { return rename(from.c_str(), to.c_str()); } -Error TryCreateDirectory(const std::string &dir); -Error ChangeUser(const char *userName); -Error ChownDir(const std::string &path, const std::string &user); +Error TryCreateDirectory(const std::string& dir); +Error ChangeUser(const char* userName); +Error ChownDir(const std::string& path, const std::string& user); -inline std::string JoinPath(const std::string &base, const std::string &name) { +inline std::string JoinPath(const std::string& base, const std::string& name) { return base + ((!base.empty() && base.back() != '/') ? "/" : "") + name; } } // namespace fs diff --git a/cpp_src/tools/json2kv.cc b/cpp_src/tools/json2kv.cc index e4aec7e2d..c22131e1f 100644 --- a/cpp_src/tools/json2kv.cc +++ b/cpp_src/tools/json2kv.cc @@ -7,7 +7,7 @@ namespace reindexer { -Variant jsonValue2Variant(const gason::JsonValue &v, KeyValueType t, std::string_view fieldName) { +Variant jsonValue2Variant(const gason::JsonValue& v, KeyValueType t, std::string_view fieldName) { switch (v.getTag()) { case gason::JSON_NUMBER: return t.EvaluateOneOf( @@ -61,7 +61,7 @@ Variant jsonValue2Variant(const gason::JsonValue &v, KeyValueType t, std::string [](KeyValueType::Double) noexcept { return Variant(0.0); }, [](KeyValueType::Bool) noexcept { return Variant(false); }, [](KeyValueType::Int) noexcept { return Variant(0); }, [](KeyValueType::Int64) noexcept { return Variant(static_cast(0)); }, - [](KeyValueType::String) { return Variant(static_cast(nullptr)); }, + [](KeyValueType::String) { return Variant(static_cast(nullptr)); }, [](KeyValueType::Uuid) noexcept { return Variant{Uuid{}}; }, [&](OneOf) -> Variant { throw Error(errLogic, "Error parsing json field '%s' - got null, expected %s", fieldName, t.Name()); @@ -70,7 +70,7 @@ Variant jsonValue2Variant(const gason::JsonValue &v, KeyValueType t, std::string throw Error(errLogic, "Error parsing json field '%s' - unable to use object in this context", fieldName); case gason::JSON_ARRAY: { VariantArray variants; - for (const auto &elem : v) { + for (const auto& elem : v) { if (elem.value.getTag() != gason::JSON_NULL) { variants.emplace_back(jsonValue2Variant(elem.value, KeyValueType::Undefined{}, fieldName)); } diff --git a/cpp_src/tools/jsonstring.h b/cpp_src/tools/jsonstring.h index 8df4bb0c8..da87622ca 100644 --- a/cpp_src/tools/jsonstring.h +++ b/cpp_src/tools/jsonstring.h @@ -21,7 +21,7 @@ namespace json_string { constexpr static unsigned kLargeJSONStrFlag = 0x80; template -inline size_t length(const uint8_t *p) noexcept { +inline size_t length(const uint8_t* p) noexcept { if constexpr (isLargeString) { return p[-1] | (unsigned(p[0]) << 8) | (unsigned(p[1]) << 16) | ((unsigned(p[2]) & ~kLargeJSONStrFlag) << 24); } else { @@ -29,14 +29,14 @@ inline size_t length(const uint8_t *p) noexcept { } } -inline size_t length(const uint8_t *p) noexcept { +inline size_t length(const uint8_t* p) noexcept { if (p[2] & kLargeJSONStrFlag) { return length(p); } return length(p); } -inline std::string_view to_string_view(const uint8_t *p) noexcept { +inline std::string_view to_string_view(const uint8_t* p) noexcept { if (p[2] & kLargeJSONStrFlag) { const auto len = length(p); uintptr_t uptr; @@ -49,13 +49,13 @@ inline std::string_view to_string_view(const uint8_t *p) noexcept { #else static_assert(false, "Unexpected uintptr_t size"); #endif - return std::string_view(reinterpret_cast(uptr), len); + return std::string_view(reinterpret_cast(uptr), len); } const auto len = length(p); - return std::string_view(reinterpret_cast(p - len), len); + return std::string_view(reinterpret_cast(p - len), len); } -inline void encode(uint8_t *p, uint64_t l, std::vector> &storage) { +inline void encode(uint8_t* p, uint64_t l, std::vector>& storage) { if (l >= (uint64_t(1) << 23)) { storage.emplace_back(new char[l]); std::copy(p - l, p, storage.back().get()); diff --git a/cpp_src/tools/jsontools.cc b/cpp_src/tools/jsontools.cc index 33b3031f1..a8fc8996e 100644 --- a/cpp_src/tools/jsontools.cc +++ b/cpp_src/tools/jsontools.cc @@ -5,7 +5,7 @@ namespace reindexer { -void jsonValueToString(gason::JsonValue o, WrSerializer &ser, int shift, int indent, bool escapeStrings) { +void jsonValueToString(gason::JsonValue o, WrSerializer& ser, int shift, int indent, bool escapeStrings) { using namespace std::string_view_literals; bool enableEol = (shift != 0) || (indent != 0); switch (o.getTag()) { @@ -28,13 +28,19 @@ void jsonValueToString(gason::JsonValue o, WrSerializer &ser, int shift, int ind break; } ser << '['; - if (enableEol) ser << '\n'; + if (enableEol) { + ser << '\n'; + } - for (const auto &i : o) { + for (const auto& i : o) { ser.Fill(' ', indent + shift); jsonValueToString(i.value, ser, shift, indent + shift); - if (i.next) ser << ','; - if (enableEol) ser << '\n'; + if (i.next) { + ser << ','; + } + if (enableEol) { + ser << '\n'; + } } ser.Fill(' ', indent); ser << ']'; @@ -45,15 +51,21 @@ void jsonValueToString(gason::JsonValue o, WrSerializer &ser, int shift, int ind break; } ser << '{'; - if (enableEol) ser << '\n'; + if (enableEol) { + ser << '\n'; + } - for (const auto &i : o) { + for (const auto& i : o) { ser.Fill(' ', indent + shift); ser.PrintJsonString(i.key); ser << ": "; jsonValueToString(i.value, ser, shift, indent + shift); - if (i.next) ser << ','; - if (enableEol) ser << '\n'; + if (i.next) { + ser << ','; + } + if (enableEol) { + ser << '\n'; + } } ser.Fill(' ', indent); ser << '}'; @@ -75,11 +87,11 @@ void jsonValueToString(gason::JsonValue o, WrSerializer &ser, int shift, int ind } } -void prettyPrintJSON(span json, WrSerializer &ser, int shift) { +void prettyPrintJSON(span json, WrSerializer& ser, int shift) { jsonValueToString(gason::JsonParser().Parse(json).value, ser, shift, 0); } -std::string stringifyJson(const gason::JsonNode &elem) { +std::string stringifyJson(const gason::JsonNode& elem) { WrSerializer ser; jsonValueToString(elem.value, ser, 0, 0); diff --git a/cpp_src/tools/jsontools.h b/cpp_src/tools/jsontools.h index f17222aed..fe186c1e3 100644 --- a/cpp_src/tools/jsontools.h +++ b/cpp_src/tools/jsontools.h @@ -9,9 +9,9 @@ class WrSerializer; constexpr int kJsonShiftWidth = 4; -void jsonValueToString(gason::JsonValue o, WrSerializer &ser, int shift = kJsonShiftWidth, int indent = 0, bool escapeStrings = true); -void prettyPrintJSON(span json, WrSerializer &ser, int shift = kJsonShiftWidth); +void jsonValueToString(gason::JsonValue o, WrSerializer& ser, int shift = kJsonShiftWidth, int indent = 0, bool escapeStrings = true); +void prettyPrintJSON(span json, WrSerializer& ser, int shift = kJsonShiftWidth); -std::string stringifyJson(const gason::JsonNode &elem); +std::string stringifyJson(const gason::JsonNode& elem); } // namespace reindexer diff --git a/cpp_src/tools/md5crypt.cc b/cpp_src/tools/md5crypt.cc index 384ab8e13..1427388f9 100644 --- a/cpp_src/tools/md5crypt.cc +++ b/cpp_src/tools/md5crypt.cc @@ -16,8 +16,8 @@ namespace reindexer { constexpr std::string_view kMD5CryptDelimiter = "$"; -std::string MD5crypt(const std::string &passwd, const std::string &salt) noexcept { - static unsigned const char cov2char[64] = { +std::string MD5crypt(const std::string& passwd, const std::string& salt) noexcept { + static const unsigned char cov2char[64] = { /* from crypto/des/fcrypt.c */ 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, @@ -59,21 +59,21 @@ std::string MD5crypt(const std::string &passwd, const std::string &salt) noexcep for (size_t i = 0; i < 1000; i++) { MD5 md; - md.add((i & 1) ? reinterpret_cast(passwd.c_str()) : buf, (i & 1) ? passwd.size() : sizeof(buf)); + md.add((i & 1) ? reinterpret_cast(passwd.c_str()) : buf, (i & 1) ? passwd.size() : sizeof(buf)); if (i % 3) { md.add(trunkatedSalt, saltLen); } if (i % 7) { md.add(passwd.c_str(), passwd.size()); } - md.add((i & 1) ? buf : reinterpret_cast(passwd.c_str()), (i & 1) ? sizeof(buf) : passwd.size()); + md.add((i & 1) ? buf : reinterpret_cast(passwd.c_str()), (i & 1) ? sizeof(buf) : passwd.size()); md.getHash(buf); } char resultBuf[23] = {0}; /* transform buf into output string */ unsigned char bufPerm[sizeof(buf)]; - char *output = resultBuf; + char* output = resultBuf; for (int dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17) { bufPerm[dest] = buf[source]; } @@ -93,7 +93,7 @@ std::string MD5crypt(const std::string &passwd, const std::string &salt) noexcep return std::string(resultBuf); } -Error ParseMd5CryptString(const std::string &input, std::string &outHash, std::string &outSalt) { +Error ParseMd5CryptString(const std::string& input, std::string& outHash, std::string& outSalt) { if (input.empty() || input.find(kMD5CryptDelimiter) != 0) { outHash = input; outSalt.clear(); diff --git a/cpp_src/tools/md5crypt.h b/cpp_src/tools/md5crypt.h index b018f13a8..33c8b441b 100644 --- a/cpp_src/tools/md5crypt.h +++ b/cpp_src/tools/md5crypt.h @@ -7,7 +7,7 @@ namespace reindexer { constexpr std::string_view kMD5CryptMagic{"1"}; -std::string MD5crypt(const std::string &passwd, const std::string &salt) noexcept; -Error ParseMd5CryptString(const std::string &input, std::string &outHash, std::string &outSalt); +std::string MD5crypt(const std::string& passwd, const std::string& salt) noexcept; +Error ParseMd5CryptString(const std::string& input, std::string& outHash, std::string& outSalt); } // namespace reindexer diff --git a/cpp_src/tools/semversion.h b/cpp_src/tools/semversion.h index 0e37f746f..c02a384b3 100644 --- a/cpp_src/tools/semversion.h +++ b/cpp_src/tools/semversion.h @@ -12,14 +12,14 @@ class SemVersion { SemVersion() = default; SemVersion(std::string_view version) { parse(version); } - bool operator<(const SemVersion &rVersion) const { + bool operator<(const SemVersion& rVersion) const { return std::lexicographical_compare(versionDigits_.begin(), versionDigits_.end(), rVersion.versionDigits_.begin(), rVersion.versionDigits_.end()); } - bool operator==(const SemVersion &rVersion) const { return (versionDigits_ == rVersion.versionDigits_); } - bool operator!=(const SemVersion &rVersion) const { return (versionDigits_ != rVersion.versionDigits_); } + bool operator==(const SemVersion& rVersion) const { return (versionDigits_ == rVersion.versionDigits_); } + bool operator!=(const SemVersion& rVersion) const { return (versionDigits_ != rVersion.versionDigits_); } - const std::string &StrippedString() const { return versionStr_; } + const std::string& StrippedString() const { return versionStr_; } private: void parse(std::string_view input) { @@ -37,7 +37,7 @@ class SemVersion { } version = splitted[0]; split(version, ".", false, splitted); - for (auto &it : splitted) { + for (auto& it : splitted) { bool valid = true; int res = jsteemann::atoi(it.data(), it.data() + it.size(), valid); if (!valid) { diff --git a/cpp_src/tools/serializer.cc b/cpp_src/tools/serializer.cc index c7016b7c9..46dbe79ea 100644 --- a/cpp_src/tools/serializer.cc +++ b/cpp_src/tools/serializer.cc @@ -9,7 +9,7 @@ namespace reindexer { p_string Serializer::GetPVString() { return p_string(getPVStringPtr()); } p_string Serializer::GetPSlice() { - auto ret = reinterpret_cast(buf_ + pos_); + auto ret = reinterpret_cast(buf_ + pos_); auto l = GetUInt32(); checkbound(pos_, l, len_); pos_ += l; @@ -30,8 +30,8 @@ p_string Serializer::GetPSlice() { Variant Serializer::getPVStringVariant() { return Variant(GetPVString()); } -const v_string_hdr *Serializer::getPVStringPtr() { - auto ret = reinterpret_cast(buf_ + pos_); +const v_string_hdr* Serializer::getPVStringPtr() { + auto ret = reinterpret_cast(buf_ + pos_); auto l = GetVarUint(); checkbound(pos_, l, len_); pos_ += l; @@ -80,10 +80,10 @@ void WrSerializer::VStringHelper::End() { } void WrSerializer::PrintJsonString(std::string_view str, PrintJsonStringMode mode) { - const char *s = str.data(); + const char* s = str.data(); size_t l = str.size(); grow(l * 6 + 3); - char *d = reinterpret_cast(buf_ + len_); + char* d = reinterpret_cast(buf_ + len_); *d++ = '"'; while (l--) { @@ -131,24 +131,24 @@ void WrSerializer::PrintJsonString(std::string_view str, PrintJsonStringMode mod } } *d++ = '"'; - len_ = d - reinterpret_cast(buf_); + len_ = d - reinterpret_cast(buf_); } void WrSerializer::PutStrUuid(Uuid uuid) { grow(Uuid::kStrFormLen + 10); len_ += uint32_pack(Uuid::kStrFormLen, buf_ + len_); - uuid.PutToStr({reinterpret_cast(buf_ + len_), cap_ - len_}); + uuid.PutToStr({reinterpret_cast(buf_ + len_), cap_ - len_}); len_ += Uuid::kStrFormLen; } void WrSerializer::PrintJsonUuid(Uuid uuid) { grow(Uuid::kStrFormLen + 2); - char *d = reinterpret_cast(buf_ + len_); + char* d = reinterpret_cast(buf_ + len_); *d++ = '"'; uuid.PutToStr({d, cap_ - len_}); d += Uuid::kStrFormLen; *d++ = '"'; - len_ = d - reinterpret_cast(buf_); + len_ = d - reinterpret_cast(buf_); } const int kHexDumpBytesInRow = 16; @@ -156,7 +156,7 @@ const int kHexDumpBytesInRow = 16; void WrSerializer::PrintHexDump(std::string_view str) { grow((kHexDumpBytesInRow * 4 + 12) * (1 + (str.size() / kHexDumpBytesInRow))); - char *d = reinterpret_cast(buf_ + len_); + char* d = reinterpret_cast(buf_ + len_); for (int row = 0; row < int(str.size()); row += kHexDumpBytesInRow) { d = u32toax(row, d, 8); @@ -178,11 +178,11 @@ void WrSerializer::PrintHexDump(std::string_view str) { } *d++ = '\n'; } - len_ = d - reinterpret_cast(buf_); + len_ = d - reinterpret_cast(buf_); } std::unique_ptr WrSerializer::DetachLStr() { - reinterpret_cast(buf_)->length = len_ - sizeof(uint32_t); + reinterpret_cast(buf_)->length = len_ - sizeof(uint32_t); return DetachBuf(); } diff --git a/cpp_src/tools/serializer.h b/cpp_src/tools/serializer.h index ecd52da7c..f6a532815 100644 --- a/cpp_src/tools/serializer.h +++ b/cpp_src/tools/serializer.h @@ -11,8 +11,8 @@ #include "tools/stringstools.h" #include "tools/varint.h" -char *i32toa(int32_t value, char *buffer); -char *i64toa(int64_t value, char *buffer); +char* i32toa(int32_t value, char* buffer); +char* i64toa(int64_t value, char* buffer); namespace reindexer { @@ -25,8 +25,8 @@ constexpr auto kFalseSV = std::string_view("false"); class Serializer { public: - Serializer(const void *buf, size_t len) noexcept : buf_(static_cast(buf)), len_(len), pos_(0) {} - explicit Serializer(std::string_view buf) noexcept : buf_(reinterpret_cast(buf.data())), len_(buf.length()), pos_(0) {} + Serializer(const void* buf, size_t len) noexcept : buf_(static_cast(buf)), len_(len), pos_(0) {} + explicit Serializer(std::string_view buf) noexcept : buf_(reinterpret_cast(buf.data())), len_(buf.length()), pos_(0) {} bool Eof() const noexcept { return pos_ >= len_; } [[nodiscard]] RX_ALWAYS_INLINE KeyValueType GetKeyValueType() { return KeyValueType::fromNumber(GetVarUint()); } [[nodiscard]] Variant GetVariant() { @@ -65,7 +65,7 @@ class Serializer { } RX_ALWAYS_INLINE std::string_view GetSlice() { auto l = GetUInt32(); - std::string_view b(reinterpret_cast(buf_ + pos_), l); + std::string_view b(reinterpret_cast(buf_ + pos_), l); checkbound(pos_, b.size(), len_); pos_ += b.size(); return b; @@ -123,7 +123,7 @@ class Serializer { auto l = GetVarUint(); checkbound(pos_, l, len_); pos_ += l; - return {reinterpret_cast(buf_ + pos_ - l), std::string_view::size_type(l)}; + return {reinterpret_cast(buf_ + pos_ - l), std::string_view::size_type(l)}; } p_string GetPVString(); p_string GetPSlice(); @@ -131,7 +131,7 @@ class Serializer { RX_ALWAYS_INLINE bool GetBool() { return bool(GetVarUint()); } size_t Pos() const noexcept { return pos_; } void SetPos(size_t p) noexcept { pos_ = p; } - const uint8_t *Buf() const noexcept { return buf_; } + const uint8_t* Buf() const noexcept { return buf_; } size_t Len() const noexcept { return len_; } void Reset() noexcept { pos_ = 0; } @@ -145,9 +145,9 @@ class Serializer { [[noreturn]] void throwScanIntError(std::string_view type); [[noreturn]] void throwUnknownTypeError(std::string_view type); Variant getPVStringVariant(); - const v_string_hdr *getPVStringPtr(); + const v_string_hdr* getPVStringPtr(); - const uint8_t *buf_; + const uint8_t* buf_; size_t len_; size_t pos_; }; @@ -157,15 +157,15 @@ class WrSerializer { WrSerializer() noexcept : buf_(inBuf_), len_(0), cap_(sizeof(inBuf_)) {} template explicit WrSerializer(uint8_t (&buf)[N]) noexcept : buf_(buf), len_(0), cap_(N), hasExternalBuf_(true) {} - explicit WrSerializer(chunk &&ch) noexcept : buf_(ch.release()), len_(ch.len()), cap_(ch.capacity()) { + explicit WrSerializer(chunk&& ch) noexcept : buf_(ch.release()), len_(ch.len()), cap_(ch.capacity()) { if (!buf_) { buf_ = inBuf_; cap_ = sizeof(inBuf_); len_ = 0; } } - WrSerializer(const WrSerializer &) = delete; - WrSerializer(WrSerializer &&other) noexcept : len_(other.len_), cap_(other.cap_), hasExternalBuf_(other.hasExternalBuf_) { + WrSerializer(const WrSerializer&) = delete; + WrSerializer(WrSerializer&& other) noexcept : len_(other.len_), cap_(other.cap_), hasExternalBuf_(other.hasExternalBuf_) { if (other.buf_ == other.inBuf_) { buf_ = inBuf_; memcpy(buf_, other.buf_, other.len_ * sizeof(other.inBuf_[0])); @@ -180,12 +180,16 @@ class WrSerializer { other.hasExternalBuf_ = false; } ~WrSerializer() { - if (HasAllocatedBuffer()) delete[] buf_; // NOLINT(*.NewDelete) False positive + if (HasAllocatedBuffer()) { + delete[] buf_; // NOLINT(*.NewDelete) False positive + } } - WrSerializer &operator=(const WrSerializer &) = delete; - WrSerializer &operator=(WrSerializer &&other) noexcept { + WrSerializer& operator=(const WrSerializer&) = delete; + WrSerializer& operator=(WrSerializer&& other) noexcept { if (this != &other) { - if (HasAllocatedBuffer()) delete[] buf_; + if (HasAllocatedBuffer()) { + delete[] buf_; + } len_ = other.len_; cap_ = other.cap_; @@ -209,13 +213,13 @@ class WrSerializer { bool HasAllocatedBuffer() const noexcept { return buf_ != inBuf_ && !hasExternalBuf_; } RX_ALWAYS_INLINE void PutKeyValueType(KeyValueType t) { PutVarUint(t.toNumber()); } - void PutVariant(const Variant &kv) { + void PutVariant(const Variant& kv) { PutKeyValueType(kv.Type()); kv.Type().EvaluateOneOf( [&](KeyValueType::Tuple) { auto compositeValues = kv.getCompositeValues(); PutVarUint(compositeValues.size()); - for (auto &v : compositeValues) { + for (auto& v : compositeValues) { PutVariant(v); } }, @@ -240,11 +244,11 @@ class WrSerializer { private: class [[nodiscard]] SliceHelper { public: - SliceHelper(WrSerializer *ser, size_t pos) noexcept : ser_(ser), pos_(pos) {} - SliceHelper(const SliceHelper &) = delete; - SliceHelper operator=(const SliceHelper &) = delete; - SliceHelper(SliceHelper &&other) noexcept : ser_(other.ser_), pos_(other.pos_) { other.ser_ = nullptr; } - SliceHelper &operator=(SliceHelper &&other) noexcept { + SliceHelper(WrSerializer* ser, size_t pos) noexcept : ser_(ser), pos_(pos) {} + SliceHelper(const SliceHelper&) = delete; + SliceHelper operator=(const SliceHelper&) = delete; + SliceHelper(SliceHelper&& other) noexcept : ser_(other.ser_), pos_(other.pos_) { other.ser_ = nullptr; } + SliceHelper& operator=(SliceHelper&& other) noexcept { if (this != &other) { ser_ = other.ser_; pos_ = other.pos_; @@ -261,7 +265,7 @@ class WrSerializer { } private: - WrSerializer *ser_; + WrSerializer* ser_; size_t pos_; }; @@ -269,11 +273,11 @@ class WrSerializer { class [[nodiscard]] VStringHelper { public: VStringHelper() noexcept : ser_(nullptr), pos_(0) {} - VStringHelper(WrSerializer *ser, size_t pos) noexcept : ser_(ser), pos_(pos) {} - VStringHelper(const VStringHelper &) = delete; - VStringHelper operator=(const VStringHelper &) = delete; - VStringHelper(VStringHelper &&other) noexcept : ser_(other.ser_), pos_(other.pos_) { other.ser_ = nullptr; } - VStringHelper &operator=(VStringHelper &&other) noexcept { + VStringHelper(WrSerializer* ser, size_t pos) noexcept : ser_(ser), pos_(pos) {} + VStringHelper(const VStringHelper&) = delete; + VStringHelper operator=(const VStringHelper&) = delete; + VStringHelper(VStringHelper&& other) noexcept : ser_(other.ser_), pos_(other.pos_) { other.ser_ = nullptr; } + VStringHelper& operator=(VStringHelper&& other) noexcept { if (this != &other) { ser_ = other.ser_; pos_ = other.pos_; @@ -285,7 +289,7 @@ class WrSerializer { void End(); private: - WrSerializer *ser_; + WrSerializer* ser_; size_t pos_; }; @@ -315,52 +319,54 @@ class WrSerializer { } void PutDoubleStrNoTrailing(double v) { grow(32); - len_ += double_to_str_no_trailing(v, reinterpret_cast(buf_ + len_), 32); + len_ += double_to_str_no_trailing(v, reinterpret_cast(buf_ + len_), 32); } - template ::value>::type * = nullptr> - WrSerializer &operator<<(T k) { + template ::value>::type* = nullptr> + WrSerializer& operator<<(T k) { grow(32); - char *b = i64toa(k, reinterpret_cast(buf_ + len_)); - len_ = b - reinterpret_cast(buf_); + char* b = i64toa(k, reinterpret_cast(buf_ + len_)); + len_ = b - reinterpret_cast(buf_); return *this; } - template ::value>::type * = nullptr> - WrSerializer &operator<<(T k) { + template ::value>::type* = nullptr> + WrSerializer& operator<<(T k) { grow(32); - char *b = i32toa(k, reinterpret_cast(buf_ + len_)); - len_ = b - reinterpret_cast(buf_); + char* b = i32toa(k, reinterpret_cast(buf_ + len_)); + len_ = b - reinterpret_cast(buf_); return *this; } - WrSerializer &operator<<(char c) { - if (len_ + 1 >= cap_) grow(1); + WrSerializer& operator<<(char c) { + if (len_ + 1 >= cap_) { + grow(1); + } buf_[len_++] = c; return *this; } - WrSerializer &operator<<(std::string_view sv) { + WrSerializer& operator<<(std::string_view sv) { Write(sv); return *this; } - WrSerializer &operator<<(const char *sv) { + WrSerializer& operator<<(const char* sv) { Write(std::string_view(sv)); return *this; } - WrSerializer &operator<<(bool v) { + WrSerializer& operator<<(bool v) { using namespace std::string_view_literals; Write(v ? kTrueSV : kFalseSV); return *this; } - WrSerializer &operator<<(double v) { + WrSerializer& operator<<(double v) { grow(32); - len_ += double_to_str(v, reinterpret_cast(buf_ + len_), 32); + len_ += double_to_str(v, reinterpret_cast(buf_ + len_), 32); return *this; } - WrSerializer &operator<<(Uuid uuid) { + WrSerializer& operator<<(Uuid uuid) { grow(Uuid::kStrFormLen + 2); buf_[len_] = '\''; ++len_; - uuid.PutToStr(span{reinterpret_cast(&buf_[len_]), Uuid::kStrFormLen}); + uuid.PutToStr(span{reinterpret_cast(&buf_[len_]), Uuid::kStrFormLen}); len_ += Uuid::kStrFormLen; buf_[len_] = '\''; ++len_; @@ -377,27 +383,27 @@ class WrSerializer { memset(&buf_[len_], c, count); len_ += count; } - template > * = nullptr> + template >* = nullptr> RX_ALWAYS_INLINE void PutVarint(T v) { grow(10); len_ += sint64_pack(v, buf_ + len_); } - template > * = nullptr> + template >* = nullptr> RX_ALWAYS_INLINE void PutVarUint(T v) { grow(10); len_ += uint64_pack(v, buf_ + len_); } - template > * = nullptr> + template >* = nullptr> RX_ALWAYS_INLINE void PutVarint(T v) { grow(10); len_ += sint32_pack(v, buf_ + len_); } - template > * = nullptr> + template >* = nullptr> RX_ALWAYS_INLINE void PutVarUint(T v) { grow(10); len_ += uint32_pack(v, buf_ + len_); } - template > * = nullptr> + template >* = nullptr> RX_ALWAYS_INLINE void PutVarUint(T v) { assertrx(v >= 0 && v < 128); grow(1); @@ -424,7 +430,7 @@ class WrSerializer { memcpy(&buf_[len_], slice.data(), slice.size()); len_ += slice.size(); } - RX_ALWAYS_INLINE uint8_t *Buf() const noexcept { return buf_; } + RX_ALWAYS_INLINE uint8_t* Buf() const noexcept { return buf_; } std::unique_ptr DetachBuf() { std::unique_ptr ret; @@ -460,20 +466,22 @@ class WrSerializer { void Reserve(size_t cap) { if (cap > cap_) { cap_ = cap; - uint8_t *b = new uint8_t[cap_]; + uint8_t* b = new uint8_t[cap_]; memcpy(b, buf_, len_); - if (HasAllocatedBuffer()) delete[] buf_; // NOLINT(*.NewDelete) False positive + if (HasAllocatedBuffer()) { + delete[] buf_; // NOLINT(*.NewDelete) False positive + } buf_ = b; hasExternalBuf_ = false; } } - RX_ALWAYS_INLINE std::string_view Slice() const noexcept { return {reinterpret_cast(buf_), len_}; } - const char *c_str() noexcept { + RX_ALWAYS_INLINE std::string_view Slice() const noexcept { return {reinterpret_cast(buf_), len_}; } + const char* c_str() noexcept { if (!len_ || buf_[len_] != 0) { grow(1); buf_[len_] = 0; } - return reinterpret_cast(buf_); + return reinterpret_cast(buf_); } protected: @@ -485,15 +493,15 @@ class WrSerializer { Reserve((newCap == newCapAligned) ? newCap : (newCapAligned + 0x1000)); } } - uint8_t *buf_ = nullptr; + uint8_t* buf_ = nullptr; size_t len_ = 0; size_t cap_ = 0; uint8_t inBuf_[0x100]; bool hasExternalBuf_ = false; }; -inline int msgpack_wrserializer_write(void *data, const char *buf, size_t len) { - reinterpret_cast(data)->Write(std::string_view(buf, len)); +inline int msgpack_wrserializer_write(void* data, const char* buf, size_t len) { + reinterpret_cast(data)->Write(std::string_view(buf, len)); return 0; } diff --git a/cpp_src/tools/string_regexp_functions.cc b/cpp_src/tools/string_regexp_functions.cc index f1b95044e..75de60ce8 100644 --- a/cpp_src/tools/string_regexp_functions.cc +++ b/cpp_src/tools/string_regexp_functions.cc @@ -8,11 +8,15 @@ namespace reindexer { std::string makeLikePattern(std::string_view utf8Str) { std::wstring utf16Str = reindexer::utf8_to_utf16(utf8Str); - for (wchar_t &ch : utf16Str) { - if (rand() % 4 == 0) ch = L'_'; + for (wchar_t& ch : utf16Str) { + if (rand() % 4 == 0) { + ch = L'_'; + } } std::wstring result; - if (rand() % 4 == 0) result += L'%'; + if (rand() % 4 == 0) { + result += L'%'; + } std::wstring::size_type next = rand() % (utf16Str.size() + 1); std::wstring::size_type last = next; for (std::wstring::size_type current = 0; current < utf16Str.size();) { @@ -22,9 +26,13 @@ std::string makeLikePattern(std::string_view utf8Str) { current = (rand() % (utf16Str.size() - last + 1)) + last; } next = (rand() % (utf16Str.size() - current + 1)) + current; - if (current > last || rand() % 4 == 0) result += L'%'; + if (current > last || rand() % 4 == 0) { + result += L'%'; + } + } + if (rand() % 4 == 0) { + result += L'%'; } - if (rand() % 4 == 0) result += L'%'; return reindexer::utf16_to_utf8(result); } @@ -35,7 +43,7 @@ std::string sqlLikePattern2ECMAScript(std::string pattern) { } else if (pattern[pos] == '%') { pattern.replace(pos, 1, ".*"); } - const char *ptr = &pattern[pos]; + const char* ptr = &pattern[pos]; utf8::unchecked::next(ptr); pos = ptr - pattern.data(); } @@ -44,10 +52,10 @@ std::string sqlLikePattern2ECMAScript(std::string pattern) { bool matchLikePattern(std::string_view utf8Str, std::string_view utf8Pattern) { constexpr static wchar_t anyChar = L'_', wildChar = L'%'; - const char *pIt = utf8Pattern.data(); - const char * const pEnd = utf8Pattern.data() + utf8Pattern.size(); - const char *sIt = utf8Str.data(); - const char * const sEnd = utf8Str.data() + utf8Str.size(); + const char* pIt = utf8Pattern.data(); + const char* const pEnd = utf8Pattern.data() + utf8Pattern.size(); + const char* sIt = utf8Str.data(); + const char* const sEnd = utf8Str.data() + utf8Str.size(); bool haveWildChar = false; while (pIt != pEnd && sIt != sEnd) { @@ -56,12 +64,14 @@ bool matchLikePattern(std::string_view utf8Str, std::string_view utf8Pattern) { haveWildChar = true; break; } - if (ToLower(pCh) != ToLower(utf8::unchecked::next(sIt)) && pCh != anyChar) return false; + if (ToLower(pCh) != ToLower(utf8::unchecked::next(sIt)) && pCh != anyChar) { + return false; + } } while (pIt != pEnd && sIt != sEnd) { - const char *tmpSIt = sIt; - const char *tmpPIt = pIt; + const char* tmpSIt = sIt; + const char* tmpPIt = pIt; while (tmpPIt != pEnd) { const wchar_t pCh = utf8::unchecked::next(tmpPIt); if (pCh == wildChar) { @@ -70,7 +80,9 @@ bool matchLikePattern(std::string_view utf8Str, std::string_view utf8Pattern) { haveWildChar = true; break; } - if (tmpSIt == sEnd) return false; + if (tmpSIt == sEnd) { + return false; + } if (ToLower(pCh) != ToLower(utf8::unchecked::next(tmpSIt)) && pCh != anyChar) { utf8::unchecked::next(sIt); break; @@ -83,16 +95,24 @@ bool matchLikePattern(std::string_view utf8Str, std::string_view utf8Pattern) { } while (pIt != pEnd) { - if (utf8::unchecked::next(pIt) != wildChar) return false; + if (utf8::unchecked::next(pIt) != wildChar) { + return false; + } haveWildChar = true; } - if (!haveWildChar && sIt != sEnd) return false; + if (!haveWildChar && sIt != sEnd) { + return false; + } for (pIt = pEnd, sIt = sEnd; pIt != utf8Pattern.data() && sIt != utf8Str.data();) { const wchar_t pCh = utf8::unchecked::prior(pIt); - if (pCh == wildChar) return true; - if (ToLower(pCh) != ToLower(utf8::unchecked::prior(sIt)) && pCh != anyChar) return false; + if (pCh == wildChar) { + return true; + } + if (ToLower(pCh) != ToLower(utf8::unchecked::prior(sIt)) && pCh != anyChar) { + return false; + } } return true; } diff --git a/cpp_src/tools/stringstools.cc b/cpp_src/tools/stringstools.cc index 2a9b1afd1..8637fffea 100644 --- a/cpp_src/tools/stringstools.cc +++ b/cpp_src/tools/stringstools.cc @@ -19,7 +19,7 @@ namespace reindexer { namespace stringtools_impl { -static int double_to_str(double v, char *buf, int capacity, int flags) { +static int double_to_str(double v, char* buf, int capacity, int flags) { double_conversion::StringBuilder builder(buf, capacity); double_conversion::DoubleToStringConverter dc(flags, NULL, NULL, 'e', -6, 21, 0, 0); @@ -47,14 +47,16 @@ static std::pair word2Pos(std::string_view str, int wordPos, int endPo while (IsAlpha(ch) || IsDigit(ch) || extraWordSymbols.find(ch) != std::string::npos) { wordEndIt = it; - if (it == endIt) break; + if (it == endIt) { + break; + } ch = utf8::unchecked::next(it); } if (wordStartIt != it) { - if (!wordPos) + if (!wordPos) { break; - else { + } else { wordPos--; wordStartIt = it; } @@ -70,7 +72,9 @@ static std::pair word2Pos(std::string_view str, int wordPos, int endPo while (IsAlpha(ch) || IsDigit(ch) || extraWordSymbols.find(ch) != std::string::npos) { wordEndIt = it; - if (it == endIt) break; + if (it == endIt) { + break; + } ch = utf8::unchecked::next(it); } } @@ -78,23 +82,29 @@ static std::pair word2Pos(std::string_view str, int wordPos, int endPo return {int(std::distance(str.begin(), wordStartIt)), int(std::distance(str.begin(), wordEndIt))}; } -static std::string_view urldecode2(char *buf, std::string_view str) { +static std::string_view urldecode2(char* buf, std::string_view str) { char a, b; - const char *src = str.data(); - char *dst = buf; + const char* src = str.data(); + char* dst = buf; for (size_t l = 0; l < str.length(); l++) { if (l + 2 < str.length() && (*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) { - if (a >= 'a') a -= 'a' - 'A'; - if (a >= 'A') + if (a >= 'a') { + a -= 'a' - 'A'; + } + if (a >= 'A') { a -= ('A' - 10); - else + } else { a -= '0'; - if (b >= 'a') b -= 'a' - 'A'; - if (b >= 'A') + } + if (b >= 'a') { + b -= 'a' - 'A'; + } + if (b >= 'A') { b -= ('A' - 10); - else + } else { b -= '0'; + } *dst++ = 16 * a + b; src += 3; l += 2; @@ -111,11 +121,13 @@ static std::string_view urldecode2(char *buf, std::string_view str) { // Sat Jul 15 14 : 18 : 56 2017 GMT -static const char *kDaysOfWeek[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; -static const char *kMonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; +static const char* kDaysOfWeek[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; +static const char* kMonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; -inline static char *strappend(char *dst, const char *src) noexcept { - while (*src) *dst++ = *src++; +inline static char* strappend(char* dst, const char* src) noexcept { + while (*src) { + *dst++ = *src++; + } return dst; } @@ -128,7 +140,9 @@ constexpr static auto kStrictModes = frozen::make_unordered_map calcUtf8AfterDelims(std::string_view str, size_t limit, std::string_view delims) noexcept { - const char *ptr; - const char *strEnd; - const char *ptrDelims; - const char *delimsEnd; + const char* ptr; + const char* strEnd; + const char* ptrDelims; + const char* delimsEnd; size_t charCounter = 0; for (ptr = str.data(), strEnd = str.data() + str.size(); limit && ptr < strEnd; limit--) { uint32_t c = utf8::unchecked::next(ptr); @@ -214,16 +232,18 @@ std::pair calcUtf8AfterDelims(std::string_view str, size_t limit } // This functions calculate how many bytes takes limit symbols in UTF8 backward -size_t calcUtf8Before(const char *str, int pos, size_t limit) noexcept { - const char *ptr = str + pos; - for (; limit && ptr > str; limit--) utf8::unchecked::prior(ptr); +size_t calcUtf8Before(const char* str, int pos, size_t limit) noexcept { + const char* ptr = str + pos; + for (; limit && ptr > str; limit--) { + utf8::unchecked::prior(ptr); + } return str + pos - ptr; } -std::pair calcUtf8BeforeDelims(const char *str, int pos, size_t limit, std::string_view delims) noexcept { - const char *ptr = str + pos; - const char *ptrDelim; - const char *delimsEnd; +std::pair calcUtf8BeforeDelims(const char* str, int pos, size_t limit, std::string_view delims) noexcept { + const char* ptr = str + pos; + const char* ptrDelim; + const char* delimsEnd; int charCounter = 0; for (; limit && ptr > str; limit--) { uint32_t c = utf8::unchecked::prior(ptr); @@ -239,7 +259,7 @@ std::pair calcUtf8BeforeDelims(const char *str, int pos, size_t return std::make_pair(str + pos - ptr, charCounter); } -void split(std::string_view str, std::string &buf, std::vector &words, std::string_view extraWordSymbols) { +void split(std::string_view str, std::string& buf, std::vector& words, std::string_view extraWordSymbols) { // assuming that the 'ToLower' function and the 'check for replacement' function should not change the character size in bytes buf.resize(str.length()); words.resize(0); @@ -341,11 +361,13 @@ std::pair Word2PosHelper::convert(int wordPos, int endPos) { return ret; } -void split(std::string_view utf8Str, std::wstring &utf16str, std::vector &words, std::string_view extraWordSymbols) { +void split(std::string_view utf8Str, std::wstring& utf16str, std::vector& words, std::string_view extraWordSymbols) { utf8_to_utf16(utf8Str, utf16str); words.resize(0); for (auto it = utf16str.begin(); it != utf16str.end();) { - while (it != utf16str.end() && !IsAlpha(*it) && !IsDigit(*it)) it++; + while (it != utf16str.end() && !IsAlpha(*it) && !IsDigit(*it)) { + it++; + } auto begIt = it; while (it != utf16str.end() && (IsAlpha(*it) || IsDigit(*it) || extraWordSymbols.find(*it) != std::string::npos)) { @@ -361,15 +383,23 @@ void split(std::string_view utf8Str, std::wstring &utf16str, std::vector bool checkIfStartsWith(std::string_view pattern, std::string_view str) noexcept { - if (pattern.empty() || str.empty()) return false; - if (pattern.length() > str.length()) return false; + if (pattern.empty() || str.empty()) { + return false; + } + if (pattern.length() > str.length()) { + return false; + } if constexpr (sensitivity == CaseSensitive::Yes) { for (size_t i = 0; i < pattern.length(); ++i) { - if (pattern[i] != str[i]) return false; + if (pattern[i] != str[i]) { + return false; + } } } else { for (size_t i = 0; i < pattern.length(); ++i) { - if (tolower(pattern[i]) != tolower(str[i])) return false; + if (tolower(pattern[i]) != tolower(str[i])) { + return false; + } } } return true; @@ -379,16 +409,24 @@ template bool checkIfStartsWith(std::string_view pattern, std template bool checkIfEndsWith(std::string_view pattern, std::string_view src) noexcept { - if (pattern.length() > src.length()) return false; - if (pattern.length() == 0) return true; + if (pattern.length() > src.length()) { + return false; + } + if (pattern.length() == 0) { + return true; + } const auto offset = src.length() - pattern.length(); if constexpr (sensitivity == CaseSensitive::Yes) { for (size_t i = 0; i < pattern.length(); ++i) { - if (src[offset + i] != pattern[i]) return false; + if (src[offset + i] != pattern[i]) { + return false; + } } } else { for (size_t i = 0; i < pattern.length(); ++i) { - if (tolower(src[offset + i]) != tolower(pattern[i])) return false; + if (tolower(src[offset + i]) != tolower(pattern[i])) { + return false; + } } } return true; @@ -397,7 +435,7 @@ template bool checkIfEndsWith(std::string_view pattern, std: template bool checkIfEndsWith(std::string_view pattern, std::string_view src) noexcept; template <> -ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable &) noexcept { +ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable&) noexcept { auto itl = lhs.begin(); auto itr = rhs.begin(); @@ -405,8 +443,12 @@ ComparationResult collateCompare(std::string_view lhs, std::string auto chl = tolower(*itl++); auto chr = tolower(*itr++); - if (chl > chr) return ComparationResult::Gt; - if (chl < chr) return ComparationResult::Lt; + if (chl > chr) { + return ComparationResult::Gt; + } + if (chl < chr) { + return ComparationResult::Lt; + } } if (lhs.size() > rhs.size()) { @@ -419,7 +461,7 @@ ComparationResult collateCompare(std::string_view lhs, std::string } template <> -ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable &) noexcept { +ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable&) noexcept { auto itl = lhs.data(); auto itr = rhs.data(); @@ -427,8 +469,12 @@ ComparationResult collateCompare(std::string_view lhs, std::string_ auto chl = ToLower(utf8::unchecked::next(itl)); auto chr = ToLower(utf8::unchecked::next(itr)); - if (chl > chr) return ComparationResult::Gt; - if (chl < chr) return ComparationResult::Lt; + if (chl > chr) { + return ComparationResult::Gt; + } + if (chl < chr) { + return ComparationResult::Lt; + } } if (lhs.size() > rhs.size()) { @@ -440,9 +486,9 @@ ComparationResult collateCompare(std::string_view lhs, std::string_ } template <> -ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable &) noexcept { - char *posl = nullptr; - char *posr = nullptr; +ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable&) noexcept { + char* posl = nullptr; + char* posr = nullptr; int numl = strtol(lhs.data(), &posl, 10); int numr = strtol(rhs.data(), &posr, 10); @@ -451,7 +497,9 @@ ComparationResult collateCompare(std::string_view lhs, std::stri auto minlen = std::min(lhs.size() - (posl - lhs.data()), rhs.size() - (posr - rhs.data())); auto res = strncmp(posl, posr, minlen); - if (res != 0) return res < 0 ? ComparationResult::Lt : ComparationResult::Gt; + if (res != 0) { + return res < 0 ? ComparationResult::Lt : ComparationResult::Gt; + } return lhs.size() > rhs.size() ? ComparationResult::Gt : (lhs.size() < rhs.size() ? ComparationResult::Lt : ComparationResult::Eq); } @@ -461,7 +509,7 @@ ComparationResult collateCompare(std::string_view lhs, std::stri template <> ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, - const SortingPrioritiesTable &sortOrderTable) noexcept { + const SortingPrioritiesTable& sortOrderTable) noexcept { auto itl = lhs.data(); auto itr = rhs.data(); @@ -472,8 +520,12 @@ ComparationResult collateCompare(std::string_view lhs, std::strin int chlPriority = sortOrderTable.GetPriority(chl); int chrPriority = sortOrderTable.GetPriority(chr); - if (chlPriority > chrPriority) return ComparationResult::Gt; - if (chlPriority < chrPriority) return ComparationResult::Lt; + if (chlPriority > chrPriority) { + return ComparationResult::Gt; + } + if (chlPriority < chrPriority) { + return ComparationResult::Lt; + } } if (lhs.size() > rhs.size()) { @@ -485,7 +537,7 @@ ComparationResult collateCompare(std::string_view lhs, std::strin } template <> -ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable &) noexcept { +ComparationResult collateCompare(std::string_view lhs, std::string_view rhs, const SortingPrioritiesTable&) noexcept { size_t l1 = lhs.size(); size_t l2 = rhs.size(); int res = memcmp(lhs.data(), rhs.data(), std::min(l1, l2)); @@ -503,16 +555,18 @@ std::string urldecode2(std::string_view str) { return ret; } -int fast_strftime(char *buf, const tm *tm) { - char *d = buf; +int fast_strftime(char* buf, const tm* tm) { + char* d = buf; - if (unsigned(tm->tm_wday) < sizeof(stringtools_impl::kDaysOfWeek) / sizeof stringtools_impl::kDaysOfWeek[0]) + if (unsigned(tm->tm_wday) < sizeof(stringtools_impl::kDaysOfWeek) / sizeof stringtools_impl::kDaysOfWeek[0]) { d = stringtools_impl::strappend(d, stringtools_impl::kDaysOfWeek[tm->tm_wday]); + } d = stringtools_impl::strappend(d, ", "); d = i32toa(tm->tm_mday, d); *d++ = ' '; - if (unsigned(tm->tm_mon) < sizeof(stringtools_impl::kMonths) / sizeof stringtools_impl::kMonths[0]) + if (unsigned(tm->tm_mon) < sizeof(stringtools_impl::kMonths) / sizeof stringtools_impl::kMonths[0]) { d = stringtools_impl::strappend(d, stringtools_impl::kMonths[tm->tm_mon]); + } *d++ = ' '; d = i32toa(tm->tm_year + 1900, d); *d++ = ' '; @@ -571,7 +625,7 @@ StrictMode strictModeFromString(std::string_view strStrictMode) { } std::string_view strictModeToString(StrictMode mode) { - for (auto &it : stringtools_impl::kStrictModes) { + for (auto& it : stringtools_impl::kStrictModes) { if (it.second == mode) { return it.first; } @@ -584,15 +638,21 @@ bool isPrintable(std::string_view str) noexcept { return false; } for (auto c : str) { - if (c < 0x20) return false; + if (c < 0x20) { + return false; + } } return true; } bool isBlank(std::string_view str) noexcept { - if (str.empty()) return true; + if (str.empty()) { + return true; + } for (auto c : str) { - if (!isspace(c)) return false; + if (!isspace(c)) { + return false; + } } return true; } @@ -603,7 +663,7 @@ int getUTF8StringCharactersCount(std::string_view str) noexcept { for (auto it = str.begin(); it != str.end(); ++len) { utf8::next(it, str.end()); } - } catch (const std::exception &) { + } catch (const std::exception&) { return str.length(); } return len; @@ -636,14 +696,14 @@ int64_t stoll(std::string_view sl) { return ret; } -int double_to_str(double v, char *buf, int capacity) { +int double_to_str(double v, char* buf, int capacity) { const int flags = double_conversion::DoubleToStringConverter::UNIQUE_ZERO | double_conversion::DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN | double_conversion::DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT | double_conversion::DoubleToStringConverter::EMIT_TRAILING_ZERO_AFTER_POINT; return stringtools_impl::double_to_str(v, buf, capacity, flags); } -int double_to_str_no_trailing(double v, char *buf, int capacity) { +int double_to_str_no_trailing(double v, char* buf, int capacity) { const int flags = double_conversion::DoubleToStringConverter::UNIQUE_ZERO | double_conversion::DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN; return stringtools_impl::double_to_str(v, buf, capacity, flags); @@ -668,7 +728,7 @@ std::string randStringAlph(size_t len) { } template -void toNextCh(std::string_view::iterator &it, std::string_view str) { +void toNextCh(std::string_view::iterator& it, std::string_view str) { if (isUtf8) { utf8::next(it, str.end()); } else { @@ -677,7 +737,7 @@ void toNextCh(std::string_view::iterator &it, std::string_view str) { } template -void toPrevCh(std::string_view::iterator &it, std::string_view str) { +void toPrevCh(std::string_view::iterator& it, std::string_view str) { if (isUtf8) { utf8::previous(it, str.begin()); } else { @@ -686,7 +746,7 @@ void toPrevCh(std::string_view::iterator &it, std::string_view str) { } template -Error getBytePosInMultilineString(std::string_view str, const size_t line, const size_t charPos, size_t &bytePos) { +Error getBytePosInMultilineString(std::string_view str, const size_t line, const size_t charPos, size_t& bytePos) { auto it = str.begin(); size_t currLine = 0, currCharPos = 0; for (size_t i = 0; it != str.end() && ((currLine != line) || (currCharPos != charPos)); toNextCh(it, str), ++i) { @@ -703,10 +763,10 @@ Error getBytePosInMultilineString(std::string_view str, const size_t line, const return Error(errNotValid, "Wrong cursor position: line=%d, pos=%d", line, charPos); } -Error cursosPosToBytePos(std::string_view str, size_t line, size_t charPos, size_t &bytePos) { +Error cursosPosToBytePos(std::string_view str, size_t line, size_t charPos, size_t& bytePos) { try { return getBytePosInMultilineString(str, line, charPos, bytePos); - } catch (const utf8::exception &) { + } catch (const utf8::exception&) { return getBytePosInMultilineString(str, line, charPos, bytePos); } } diff --git a/cpp_src/tools/stringstools.h b/cpp_src/tools/stringstools.h index b182a7436..ce91c74e4 100644 --- a/cpp_src/tools/stringstools.h +++ b/cpp_src/tools/stringstools.h @@ -43,7 +43,9 @@ Container& split(const typename Container::value_type& str, std::string_view del pos = str.find_first_of(delimiters, lastPos); if (pos == Container::value_type::npos) { pos = str.length(); - if (pos != lastPos || !trimEmpty) tokens.push_back(str.substr(lastPos, pos - lastPos)); + if (pos != lastPos || !trimEmpty) { + tokens.push_back(str.substr(lastPos, pos - lastPos)); + } break; } else if (pos != lastPos || !trimEmpty) { tokens.push_back(str.substr(lastPos, pos - lastPos)); @@ -174,9 +176,13 @@ StrictMode strictModeFromString(std::string_view strStrictMode); std::string_view strictModeToString(StrictMode mode); inline constexpr bool iequals(std::string_view lhs, std::string_view rhs) noexcept { - if (lhs.size() != rhs.size()) return false; + if (lhs.size() != rhs.size()) { + return false; + } for (auto itl = lhs.begin(), itr = rhs.begin(); itl != lhs.end() && itr != rhs.end();) { - if (tolower(*itl++) != tolower(*itr++)) return false; + if (tolower(*itl++) != tolower(*itr++)) { + return false; + } } return true; } diff --git a/cpp_src/tools/tcmallocheapwathcher.cc b/cpp_src/tools/tcmallocheapwathcher.cc index 2942e5f2d..2b284b2bf 100644 --- a/cpp_src/tools/tcmallocheapwathcher.cc +++ b/cpp_src/tools/tcmallocheapwathcher.cc @@ -13,7 +13,7 @@ static const size_t kHeapFreeMinThreshold = 200 * 1048576; ///< Do not release p TCMallocHeapWathcher::TCMallocHeapWathcher() : TCMallocHeapWathcher(nullptr, -1, -1.0) {} -TCMallocHeapWathcher::TCMallocHeapWathcher(MallocExtension *mallocExtention, int64_t cacheLimit, float maxCacheRatio, +TCMallocHeapWathcher::TCMallocHeapWathcher(MallocExtension* mallocExtention, int64_t cacheLimit, float maxCacheRatio, std::shared_ptr logger) : mallocExtention_(mallocExtention), cacheLimit_(cacheLimit), @@ -22,11 +22,11 @@ TCMallocHeapWathcher::TCMallocHeapWathcher(MallocExtension *mallocExtention, int heapChunkReleasePeriod_(std::chrono::milliseconds(100)), logger_(std::move(logger)) {} -TCMallocHeapWathcher::TCMallocHeapWathcher(MallocExtension *mallocExtention, int64_t cacheLimit, float maxCacheRatio) +TCMallocHeapWathcher::TCMallocHeapWathcher(MallocExtension* mallocExtention, int64_t cacheLimit, float maxCacheRatio) : TCMallocHeapWathcher(mallocExtention, cacheLimit, maxCacheRatio, nullptr) {} template -void TCMallocHeapWathcher::logDebug(Args &&...args) { +void TCMallocHeapWathcher::logDebug(Args&&... args) { if (logger_) { logger_->debug(std::forward(args)...); } @@ -47,7 +47,9 @@ void TCMallocHeapWathcher::CheckHeapUsagePeriodic() { std::chrono::duration_cast(deadline_.time_since_epoch()).count()); }); - if (!mallocExtention_) return; + if (!mallocExtention_) { + return; + } if ((cacheLimit_ > 0) || (maxCacheRatio_ > 0)) { if (ClockT::now_coarse() < deadline_) { diff --git a/cpp_src/tools/tcmallocheapwathcher.h b/cpp_src/tools/tcmallocheapwathcher.h index 3f2132f8e..0f4bec7db 100644 --- a/cpp_src/tools/tcmallocheapwathcher.h +++ b/cpp_src/tools/tcmallocheapwathcher.h @@ -17,14 +17,14 @@ namespace reindexer { class TCMallocHeapWathcher { public: TCMallocHeapWathcher(); - explicit TCMallocHeapWathcher(MallocExtension *mallocExtention, int64_t cacheLimit, float maxCacheRatio); - explicit TCMallocHeapWathcher(MallocExtension *mallocExtention, int64_t cacheLimit, float maxCacheRatio, + explicit TCMallocHeapWathcher(MallocExtension* mallocExtention, int64_t cacheLimit, float maxCacheRatio); + explicit TCMallocHeapWathcher(MallocExtension* mallocExtention, int64_t cacheLimit, float maxCacheRatio, std::shared_ptr logger); - TCMallocHeapWathcher(const TCMallocHeapWathcher &) = delete; - TCMallocHeapWathcher &operator=(const TCMallocHeapWathcher &) = delete; - TCMallocHeapWathcher(TCMallocHeapWathcher &&) = default; - TCMallocHeapWathcher &operator=(TCMallocHeapWathcher &&) = default; + TCMallocHeapWathcher(const TCMallocHeapWathcher&) = delete; + TCMallocHeapWathcher& operator=(const TCMallocHeapWathcher&) = delete; + TCMallocHeapWathcher(TCMallocHeapWathcher&&) = default; + TCMallocHeapWathcher& operator=(TCMallocHeapWathcher&&) = default; ~TCMallocHeapWathcher(); void CheckHeapUsagePeriodic(); @@ -32,7 +32,7 @@ class TCMallocHeapWathcher { private: using ClockT = steady_clock_w; - MallocExtension *mallocExtention_; + MallocExtension* mallocExtention_; int64_t cacheLimit_; float maxCacheRatio_; std::chrono::microseconds heapInspectionPeriod_; @@ -41,7 +41,7 @@ class TCMallocHeapWathcher { ClockT::time_point deadline_; template - void logDebug(Args &&...args); + void logDebug(Args&&... args); }; } // namespace reindexer diff --git a/cpp_src/tools/terminalutils.cc b/cpp_src/tools/terminalutils.cc index fa578a9b8..468836eb7 100644 --- a/cpp_src/tools/terminalutils.cc +++ b/cpp_src/tools/terminalutils.cc @@ -55,7 +55,9 @@ TerminalSize getTerminalSize() { const int fds[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; for (const int fd : fds) { err = getTerminalSize(fd, size.width, size.height); - if (err.ok()) break; + if (err.ok()) { + break; + } } #endif if (!err.ok()) { diff --git a/cpp_src/tools/varint.h b/cpp_src/tools/varint.h index 3ad36927d..1fbddd201 100644 --- a/cpp_src/tools/varint.h +++ b/cpp_src/tools/varint.h @@ -46,7 +46,7 @@ RX_ALWAYS_INLINE uint64_t zigzag64(int64_t v) noexcept { return (v < 0) ? (((-(u * \return * Number of bytes written to `out`. */ -inline size_t uint32_pack(uint32_t value, uint8_t *out) noexcept { +inline size_t uint32_pack(uint32_t value, uint8_t* out) noexcept { unsigned rv = 0; if (value >= 0x80) { @@ -81,7 +81,7 @@ inline size_t uint32_pack(uint32_t value, uint8_t *out) noexcept { * \return * Number of bytes written to `out`. */ -inline size_t int32_pack(int32_t value, uint8_t *out) noexcept { +inline size_t int32_pack(int32_t value, uint8_t* out) noexcept { if (value < 0) { out[0] = value | 0x80; out[1] = (value >> 7) | 0x80; @@ -107,7 +107,7 @@ inline size_t int32_pack(int32_t value, uint8_t *out) noexcept { * \return * Number of bytes written to `out`. */ -inline size_t sint32_pack(int32_t value, uint8_t *out) noexcept { return uint32_pack(zigzag32(value), out); } +inline size_t sint32_pack(int32_t value, uint8_t* out) noexcept { return uint32_pack(zigzag32(value), out); } /** * Pack a 64-bit unsigned integer using base-128 varint encoding and return the @@ -120,12 +120,14 @@ inline size_t sint32_pack(int32_t value, uint8_t *out) noexcept { return uint32_ * \return * Number of bytes written to `out`. */ -inline size_t uint64_pack(uint64_t value, uint8_t *out) noexcept { +inline size_t uint64_pack(uint64_t value, uint8_t* out) noexcept { uint32_t hi = (uint32_t)(value >> 32); uint32_t lo = (uint32_t)value; unsigned rv; - if (hi == 0) return uint32_pack((uint32_t)lo, out); + if (hi == 0) { + return uint32_pack((uint32_t)lo, out); + } out[0] = (lo) | 0x80; out[1] = (lo >> 7) | 0x80; out[2] = (lo >> 14) | 0x80; @@ -157,14 +159,14 @@ inline size_t uint64_pack(uint64_t value, uint8_t *out) noexcept { * \return * Number of bytes written to `out`. */ -inline size_t sint64_pack(int64_t value, uint8_t *out) noexcept { return uint64_pack(zigzag64(value), out); } +inline size_t sint64_pack(int64_t value, uint8_t* out) noexcept { return uint64_pack(zigzag64(value), out); } -inline size_t boolean_pack(bool value, uint8_t *out) noexcept { +inline size_t boolean_pack(bool value, uint8_t* out) noexcept { *out = value ? 1 : 0; return 1; } -inline size_t string_pack(const char *str, uint8_t *out) noexcept { +inline size_t string_pack(const char* str, uint8_t* out) noexcept { if (str == nullptr) { out[0] = 0; return 1; @@ -176,7 +178,7 @@ inline size_t string_pack(const char *str, uint8_t *out) noexcept { } } -inline size_t string_pack(const char *str, const size_t len, uint8_t *out) noexcept { +inline size_t string_pack(const char* str, const size_t len, uint8_t* out) noexcept { if (str == nullptr) { out[0] = 0; return 1; @@ -187,7 +189,7 @@ inline size_t string_pack(const char *str, const size_t len, uint8_t *out) noexc } } -inline uint32_t parse_uint32(unsigned len, const uint8_t *data) noexcept { +inline uint32_t parse_uint32(unsigned len, const uint8_t* data) noexcept { uint32_t rv = data[0] & 0x7f; if (len > 1) { rv |= ((uint32_t)(data[1] & 0x7f) << 7); @@ -195,28 +197,32 @@ inline uint32_t parse_uint32(unsigned len, const uint8_t *data) noexcept { rv |= ((uint32_t)(data[2] & 0x7f) << 14); if (len > 3) { rv |= ((uint32_t)(data[3] & 0x7f) << 21); - if (len > 4) rv |= ((uint32_t)(data[4]) << 28); + if (len > 4) { + rv |= ((uint32_t)(data[4]) << 28); + } } } } return rv; } -inline uint32_t parse_int32(unsigned len, const uint8_t *data) noexcept { return parse_uint32(len, data); } +inline uint32_t parse_int32(unsigned len, const uint8_t* data) noexcept { return parse_uint32(len, data); } RX_ALWAYS_INLINE int32_t unzigzag32(uint32_t v) noexcept { return (v & 1) ? (-(v >> 1) - 1) : (v >> 1); } -inline uint32_t parse_fixed_uint32(const uint8_t *data) noexcept { +inline uint32_t parse_fixed_uint32(const uint8_t* data) noexcept { uint32_t t; memcpy(&t, data, 4); return t; } -inline uint64_t parse_uint64(unsigned len, const uint8_t *data) noexcept { +inline uint64_t parse_uint64(unsigned len, const uint8_t* data) noexcept { unsigned shift, i; uint64_t rv; - if (len < 5) return parse_uint32(len, data); + if (len < 5) { + return parse_uint32(len, data); + } rv = ((uint64_t)(data[0] & 0x7f)) | ((uint64_t)(data[1] & 0x7f) << 7) | ((uint64_t)(data[2] & 0x7f) << 14) | ((uint64_t)(data[3] & 0x7f) << 21); shift = 28; @@ -229,18 +235,25 @@ inline uint64_t parse_uint64(unsigned len, const uint8_t *data) noexcept { RX_ALWAYS_INLINE int64_t unzigzag64(uint64_t v) noexcept { return (v & 1) ? (-(v >> 1) - 1) : (v >> 1); } -inline uint64_t parse_fixed_uint64(const uint8_t *data) noexcept { +inline uint64_t parse_fixed_uint64(const uint8_t* data) noexcept { uint64_t t; memcpy(&t, data, 8); return t; } -inline unsigned scan_varint(unsigned len, const uint8_t *data) noexcept { +inline unsigned scan_varint(unsigned len, const uint8_t* data) noexcept { unsigned i; - if (len > 10) len = 10; - for (i = 0; i < len; i++) - if ((data[i] & 0x80) == 0) break; - if (i == len) return 0; + if (len > 10) { + len = 10; + } + for (i = 0; i < len; i++) { + if ((data[i] & 0x80) == 0) { + break; + } + } + if (i == len) { + return 0; + } return i + 1; } diff --git a/cpp_src/vendor/gason/gason.cc b/cpp_src/vendor/gason/gason.cc index e13c27768..db4bdb701 100644 --- a/cpp_src/vendor/gason/gason.cc +++ b/cpp_src/vendor/gason/gason.cc @@ -15,7 +15,7 @@ namespace gason { using double_conversion::StringToDoubleConverter; -const char *jsonStrError(int err) { +const char* jsonStrError(int err) { switch (err) { #define XX(no, str) \ case JSON_##no: \ @@ -27,18 +27,20 @@ const char *jsonStrError(int err) { } } -void *JsonAllocator::allocate(size_t size) { +void* JsonAllocator::allocate(size_t size) { size = (size + 7) & ~7; if (head && head->used + size <= JSON_ZONE_SIZE) { - char *p = (char *)head + head->used; + char* p = (char*)head + head->used; head->used += size; return p; } size_t allocSize = sizeof(Zone) + size; - Zone *zone = (Zone *)malloc(allocSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : allocSize); - if (zone == nullptr) return nullptr; + Zone* zone = (Zone*)malloc(allocSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : allocSize); + if (zone == nullptr) { + return nullptr; + } zone->used = allocSize; if (allocSize <= JSON_ZONE_SIZE || head == nullptr) { zone->next = head; @@ -47,12 +49,12 @@ void *JsonAllocator::allocate(size_t size) { zone->next = head->next; head->next = zone; } - return (char *)zone + sizeof(Zone); + return (char*)zone + sizeof(Zone); } void JsonAllocator::deallocate() { while (head) { - Zone *next = head->next; + Zone* next = head->next; free(head); head = next; } @@ -68,11 +70,13 @@ static inline bool isdigit(char c) { return c >= '0' && c <= '9'; } static inline bool isxdigit(char c) { return (c >= '0' && c <= '9') || ((c & ~' ') >= 'A' && (c & ~' ') <= 'F'); } static inline int char2int(char c) { - if (c <= '9') return c - '0'; + if (c <= '9') { + return c - '0'; + } return (c & ~' ') - 'A' + 10; } -bool isDouble(char *s, size_t &size) { +bool isDouble(char* s, size_t& size) { size = 0; if (*s == '-') { @@ -111,7 +115,7 @@ bool isDouble(char *s, size_t &size) { return res; } -static double string2double(char *s, char **endptr, size_t size) { +static double string2double(char* s, char** endptr, size_t size) { StringToDoubleConverter sd(StringToDoubleConverter::NO_FLAGS, 0, 0, "inf", "nan"); int len; double vv = sd.StringToDouble(s, size, &len); @@ -119,14 +123,16 @@ static double string2double(char *s, char **endptr, size_t size) { return vv; } -static inline JsonNode *insertAfter(JsonNode *tail, JsonNode *node) { - if (!tail) return node->next = node; +static inline JsonNode* insertAfter(JsonNode* tail, JsonNode* node) { + if (!tail) { + return node->next = node; + } node->next = tail->next; tail->next = node; return node; } -static inline JsonValue listToValue(JsonTag tag, JsonNode *tail) { +static inline JsonValue listToValue(JsonTag tag, JsonNode* tail) { if (tail) { auto head = tail->next; tail->next = nullptr; @@ -135,25 +141,29 @@ static inline JsonValue listToValue(JsonTag tag, JsonNode *tail) { return JsonValue(tag, nullptr); } -static int jsonParse(span str, char **endptr, JsonValue *value, JsonAllocator &allocator, LargeStringStorageT &largeStrings) { - JsonNode *tails[JSON_STACK_SIZE]; +static int jsonParse(span str, char** endptr, JsonValue* value, JsonAllocator& allocator, LargeStringStorageT& largeStrings) { + JsonNode* tails[JSON_STACK_SIZE]; JsonTag tags[JSON_STACK_SIZE]; JsonString keys[JSON_STACK_SIZE]; JsonValue o; int pos = -1; - char *s = str.data(); + char* s = str.data(); size_t l = str.size(); bool separator = true; - JsonNode *node; + JsonNode* node; *endptr = s; size_t size = 0; while (l) { while (l && isspace(*s)) { ++s, --l; - if (!l) break; + if (!l) { + break; + } + } + if (!l) { + break; } - if (!l) break; *endptr = s++; --l; bool negative = false; @@ -193,8 +203,10 @@ static int jsonParse(span str, char **endptr, JsonValue *value, JsonAlloca } break; case '"': - if (s - str.data() < 2) return JSON_UNEXPECTED_CHARACTER; - for (char *it = s - 2; l; ++it, ++s, --l) { + if (s - str.data() < 2) { + return JSON_UNEXPECTED_CHARACTER; + } + for (char* it = s - 2; l; ++it, ++s, --l) { int c = *it = *s; if (c == '\\') { c = *++s; @@ -257,51 +269,75 @@ static int jsonParse(span str, char **endptr, JsonValue *value, JsonAlloca } break; case 't': - if (!(s[0] == 'r' && s[1] == 'u' && s[2] == 'e' && isdelim(s[3]))) return JSON_BAD_IDENTIFIER; + if (!(s[0] == 'r' && s[1] == 'u' && s[2] == 'e' && isdelim(s[3]))) { + return JSON_BAD_IDENTIFIER; + } o = JsonValue(JSON_TRUE); s += 3, l -= 3; break; case 'f': - if (!(s[0] == 'a' && s[1] == 'l' && s[2] == 's' && s[3] == 'e' && isdelim(s[4]))) return JSON_BAD_IDENTIFIER; + if (!(s[0] == 'a' && s[1] == 'l' && s[2] == 's' && s[3] == 'e' && isdelim(s[4]))) { + return JSON_BAD_IDENTIFIER; + } o = JsonValue(JSON_FALSE); s += 4, l -= 4; break; case 'n': - if (!(s[0] == 'u' && s[1] == 'l' && s[2] == 'l' && isdelim(s[3]))) return JSON_BAD_IDENTIFIER; + if (!(s[0] == 'u' && s[1] == 'l' && s[2] == 'l' && isdelim(s[3]))) { + return JSON_BAD_IDENTIFIER; + } o = JsonValue(JSON_NULL); s += 3, l -= 3; break; case ']': - if (pos == -1) return JSON_STACK_UNDERFLOW; - if (tags[pos] != JSON_ARRAY) return JSON_MISMATCH_BRACKET; + if (pos == -1) { + return JSON_STACK_UNDERFLOW; + } + if (tags[pos] != JSON_ARRAY) { + return JSON_MISMATCH_BRACKET; + } o = listToValue(JSON_ARRAY, tails[pos--]); break; case '}': - if (pos == -1) return JSON_STACK_UNDERFLOW; - if (tags[pos] != JSON_OBJECT) return JSON_MISMATCH_BRACKET; - if (keys[pos].ptr != nullptr) return JSON_UNEXPECTED_CHARACTER; + if (pos == -1) { + return JSON_STACK_UNDERFLOW; + } + if (tags[pos] != JSON_OBJECT) { + return JSON_MISMATCH_BRACKET; + } + if (keys[pos].ptr != nullptr) { + return JSON_UNEXPECTED_CHARACTER; + } o = listToValue(JSON_OBJECT, tails[pos--]); break; case '[': - if (++pos == JSON_STACK_SIZE) return JSON_STACK_OVERFLOW; + if (++pos == JSON_STACK_SIZE) { + return JSON_STACK_OVERFLOW; + } tails[pos] = nullptr; tags[pos] = JSON_ARRAY; keys[pos] = JsonString(); separator = true; continue; case '{': - if (++pos == JSON_STACK_SIZE) return JSON_STACK_OVERFLOW; + if (++pos == JSON_STACK_SIZE) { + return JSON_STACK_OVERFLOW; + } tails[pos] = nullptr; tags[pos] = JSON_OBJECT; keys[pos] = JsonString(); separator = true; continue; case ':': - if (separator || keys[pos].ptr == nullptr) return JSON_UNEXPECTED_CHARACTER; + if (separator || keys[pos].ptr == nullptr) { + return JSON_UNEXPECTED_CHARACTER; + } separator = true; continue; case ',': - if (separator || keys[pos].ptr != nullptr) return JSON_UNEXPECTED_CHARACTER; + if (separator || keys[pos].ptr != nullptr) { + return JSON_UNEXPECTED_CHARACTER; + } separator = true; continue; case '\0': @@ -320,16 +356,22 @@ static int jsonParse(span str, char **endptr, JsonValue *value, JsonAlloca if (tags[pos] == JSON_OBJECT) { if (!keys[pos].ptr) { - if (o.getTag() != JSON_STRING) return JSON_UNQUOTED_KEY; + if (o.getTag() != JSON_STRING) { + return JSON_UNQUOTED_KEY; + } keys[pos] = o.sval; continue; } - if ((node = (JsonNode *)allocator.allocate(sizeof(JsonNode))) == nullptr) return JSON_ALLOCATION_FAILURE; + if ((node = (JsonNode*)allocator.allocate(sizeof(JsonNode))) == nullptr) { + return JSON_ALLOCATION_FAILURE; + } tails[pos] = insertAfter(tails[pos], node); tails[pos]->key = keys[pos]; keys[pos] = JsonString(); } else { - if ((node = (JsonNode *)allocator.allocate(sizeof(JsonNode))) == nullptr) return JSON_ALLOCATION_FAILURE; + if ((node = (JsonNode*)allocator.allocate(sizeof(JsonNode))) == nullptr) { + return JSON_ALLOCATION_FAILURE; + } tails[pos] = insertAfter(tails[pos], node); tails[pos]->key = JsonString(); } @@ -338,39 +380,47 @@ static int jsonParse(span str, char **endptr, JsonValue *value, JsonAlloca return JSON_BREAKING_BAD; } -JsonNode *JsonNode::toNode() const { - if (empty() || value.getTag() == JSON_NULL) return nullptr; - if (value.getTag() != JSON_OBJECT && value.getTag() != JSON_ARRAY) +JsonNode* JsonNode::toNode() const { + if (empty() || value.getTag() == JSON_NULL) { + return nullptr; + } + if (value.getTag() != JSON_OBJECT && value.getTag() != JSON_ARRAY) { throw Exception(std::string("Can't convert json field '") + std::string(key) + "' to object or array"); + } return value.toNode(); } -const JsonNode &JsonNode::operator[](std::string_view key) const { +const JsonNode& JsonNode::operator[](std::string_view key) const { if (value.getTag() != JSON_OBJECT && value.getTag() != JSON_NULL) { throw Exception(std::string("Can't obtain json field '") + std::string(key) + "' from non-object json node"); } - for (auto &v : (*this)) - if (std::string_view(v.key) == key) return v; + for (auto& v : (*this)) { + if (std::string_view(v.key) == key) { + return v; + } + } // TODO: Remove NOLINT after pyreindexer update. Issue #1736 static JsonNode empty_node{{JsonTag(JSON_EMPTY)}, nullptr, {}}; // NOLINT(*EnumCastOutOfRange) return empty_node; } -JsonNode JsonParser::Parse(span str, size_t *length) { +JsonNode JsonParser::Parse(span str, size_t* length) { largeStrings_->clear(); - char *endp = nullptr; + char* endp = nullptr; JsonNode val{{}, nullptr, {}}; - const char *begin = str.data(); + const char* begin = str.data(); int status = jsonParse(str, &endp, &val.value, alloc_, *largeStrings_); if (status != JSON_OK) { size_t pos = endp - str.data(); throw Exception(std::string("Error parsing json: ") + jsonStrError(status) + ", pos " + std::to_string(pos)); } - if (length) *length = endp - begin; + if (length) { + *length = endp - begin; + } return val; } -JsonNode JsonParser::Parse(std::string_view str, size_t *length) { +JsonNode JsonParser::Parse(std::string_view str, size_t* length) { tmp_.reserve(str.size()); tmp_.assign(str.begin(), str.end()); return Parse(span(&tmp_[0], tmp_.size()), length); @@ -380,12 +430,14 @@ static inline bool haveEqualType(JsonTag lt, JsonTag rt) noexcept { return lt == rt || (lt == JSON_TRUE && rt == JSON_FALSE) || (lt == JSON_FALSE && rt == JSON_TRUE); } -bool isHomogeneousArray(const gason::JsonValue &v) noexcept { +bool isHomogeneousArray(const gason::JsonValue& v) noexcept { bool hasTag = false; gason::JsonTag prevTag; - for (const auto &elem : v) { + for (const auto& elem : v) { if (hasTag) { - if (!haveEqualType(prevTag, elem.value.getTag())) return false; + if (!haveEqualType(prevTag, elem.value.getTag())) { + return false; + } } else { hasTag = true; } diff --git a/cpp_src/vendor/gason/gason.h b/cpp_src/vendor/gason/gason.h index 906634e67..2fee68380 100644 --- a/cpp_src/vendor/gason/gason.h +++ b/cpp_src/vendor/gason/gason.h @@ -37,33 +37,35 @@ using Exception = std::runtime_error; using LargeStringStorageT = std::vector>; struct JsonString { - JsonString(char *beg, char *end, LargeStringStorageT &largeStrings) { + JsonString(char* beg, char* end, LargeStringStorageT& largeStrings) { ptr = end; const uint64_t l = end - beg; if (l >= (uint64_t(1) << 31)) { throw Exception("JSON string too long. Limit is 2^31 bytes"); } - reindexer::json_string::encode(reinterpret_cast(end), l, largeStrings); + reindexer::json_string::encode(reinterpret_cast(end), l, largeStrings); } - JsonString(const char *end = nullptr) : ptr(end) {} + JsonString(const char* end = nullptr) : ptr(end) {} size_t length() const noexcept { assertrx(ptr); - return reindexer::json_string::length(reinterpret_cast(ptr)); + return reindexer::json_string::length(reinterpret_cast(ptr)); } size_t size() const noexcept { return ptr ? length() : 0; } - const char *data() const noexcept { - if (!ptr) return nullptr; + const char* data() const noexcept { + if (!ptr) { + return nullptr; + } - return reindexer::json_string::to_string_view(reinterpret_cast(ptr)).data(); + return reindexer::json_string::to_string_view(reinterpret_cast(ptr)).data(); } explicit operator std::string() const { return ptr ? std::string(data(), length()) : std::string(); } operator std::string_view() const noexcept { return ptr ? std::string_view(data(), length()) : std::string_view(); } - const char *ptr; + const char* ptr; }; -inline static std::ostream &operator<<(std::ostream &o, const gason::JsonString &sv) { +inline static std::ostream& operator<<(std::ostream& o, const gason::JsonString& sv) { o.write(sv.data(), sv.length()); return o; } @@ -73,7 +75,7 @@ union JsonValue { JsonValue(int64_t x) : ival(x) { u.tag = JSON_NUMBER; } JsonValue(JsonString x) : sval(x) { u.tag = JSON_STRING; } - JsonValue(JsonTag tag = JSON_NULL, void *payload = nullptr) { + JsonValue(JsonTag tag = JSON_NULL, void* payload = nullptr) { u.tag = tag; ival = uintptr_t(payload); } @@ -82,19 +84,23 @@ union JsonValue { int64_t toNumber() const { assertrx(getTag() == JSON_NUMBER || getTag() == JSON_DOUBLE); - if (getTag() == JSON_NUMBER) return ival; + if (getTag() == JSON_NUMBER) { + return ival; + } return fval; } double toDouble() const { assertrx(getTag() == JSON_NUMBER || getTag() == JSON_DOUBLE); - if (getTag() == JSON_DOUBLE) return fval; + if (getTag() == JSON_DOUBLE) { + return fval; + } return ival; } std::string_view toString() const { assertrx(getTag() == JSON_STRING); return sval; } - JsonNode *toNode() const { + JsonNode* toNode() const { assertrx(getTag() == JSON_ARRAY || getTag() == JSON_OBJECT); return node; } @@ -106,41 +112,52 @@ union JsonValue { int64_t ival; double fval; JsonString sval; - JsonNode *node; + JsonNode* node; }; struct JsonNode { JsonValue value; - JsonNode *next; + JsonNode* next; JsonString key; template ::value || std::is_floating_point::value) && - !std::is_same::value>::type * = nullptr> + !std::is_same::value>::type* = nullptr> T As(T defval = T(), T minv = std::numeric_limits::lowest(), T maxv = std::numeric_limits::max()) const { - if (empty()) return defval; - if (value.getTag() != JSON_DOUBLE && value.getTag() != JSON_NUMBER) + if (empty()) { + return defval; + } + if (value.getTag() != JSON_DOUBLE && value.getTag() != JSON_NUMBER) { throw Exception(std::string("Can't convert json field '") + std::string(key) + "' to number"); + } T v; - if (std::is_integral::value) + if (std::is_integral::value) { v = value.toNumber(); - else + } else { v = value.toDouble(); + } - if (v < minv || v > maxv) + if (v < minv || v > maxv) { throw Exception(std::string("Value of '") + std::string(key) + "' - " + std::to_string(v) + " is out of bounds: [" + std::to_string(minv) + "," + std::to_string(maxv) + "]"); + } return v; } template ::value || std::is_same::value>::type * = nullptr> + typename std::enable_if::value || std::is_same::value>::type* = nullptr> T As(T defval = T()) const { - if (empty()) return defval; - if (value.getTag() != JSON_STRING) throw Exception(std::string("Can't convert json field '") + std::string(key) + "' to string"); + if (empty()) { + return defval; + } + if (value.getTag() != JSON_STRING) { + throw Exception(std::string("Can't convert json field '") + std::string(key) + "' to string"); + } return T(value.toString()); } - template ::value>::type * = nullptr> + template ::value>::type* = nullptr> T As(T defval = T()) const { - if (empty()) return defval; + if (empty()) { + return defval; + } switch (value.getTag()) { case JSON_TRUE: return true; @@ -157,35 +174,35 @@ struct JsonNode { } } - const JsonNode &operator[](std::string_view sv) const; + const JsonNode& operator[](std::string_view sv) const; bool empty() const noexcept { return uint8_t(value.getTag()) == JSON_EMPTY; } bool isObject() const noexcept { return value.getTag() == JSON_OBJECT; } - JsonNode *toNode() const; + JsonNode* toNode() const; }; struct JsonIterator { - JsonNode *p; + JsonNode* p; void operator++() noexcept { p = p->next; } - bool operator!=(const JsonIterator &x) const noexcept { return p != x.p; } - JsonNode &operator*() const noexcept { return *p; } - JsonNode *operator->() const noexcept { return p; } + bool operator!=(const JsonIterator& x) const noexcept { return p != x.p; } + JsonNode& operator*() const noexcept { return *p; } + JsonNode* operator->() const noexcept { return p; } }; inline JsonIterator begin(const JsonValue o) { return JsonIterator{o.toNode()}; } inline JsonIterator end(JsonValue) noexcept { return JsonIterator{nullptr}; } struct JsonNodeIterator { - const JsonNode *p; + const JsonNode* p; void operator++() noexcept { p = p->next; } - bool operator!=(const JsonNodeIterator &x) const noexcept { return p != x.p; } - const JsonNode &operator*() const noexcept { return *p; } - const JsonNode *operator->() const noexcept { return p; } + bool operator!=(const JsonNodeIterator& x) const noexcept { return p != x.p; } + const JsonNode& operator*() const noexcept { return *p; } + const JsonNode* operator->() const noexcept { return p; } }; -inline JsonNodeIterator begin(const JsonNode &w) { return JsonNodeIterator{w.toNode()}; } -inline JsonNodeIterator end(const JsonNode &) noexcept { return JsonNodeIterator{nullptr}; } +inline JsonNodeIterator begin(const JsonNode& w) { return JsonNodeIterator{w.toNode()}; } +inline JsonNodeIterator end(const JsonNode&) noexcept { return JsonNodeIterator{nullptr}; } #define JSON_ERRNO_MAP(XX) \ XX(OK, "ok") \ @@ -206,20 +223,20 @@ enum JsonErrno { #undef XX }; -const char *jsonStrError(int err); +const char* jsonStrError(int err); class JsonAllocator { struct Zone { - Zone *next; + Zone* next; size_t used; - } *head; + }* head; public: JsonAllocator() : head(nullptr) {} - JsonAllocator(const JsonAllocator &) = delete; - JsonAllocator &operator=(const JsonAllocator &) = delete; - JsonAllocator(JsonAllocator &&x) : head(x.head) { x.head = nullptr; } - JsonAllocator &operator=(JsonAllocator &&x) noexcept { + JsonAllocator(const JsonAllocator&) = delete; + JsonAllocator& operator=(const JsonAllocator&) = delete; + JsonAllocator(JsonAllocator&& x) : head(x.head) { x.head = nullptr; } + JsonAllocator& operator=(JsonAllocator&& x) noexcept { if (this != &x) { deallocate(); head = x.head; @@ -228,26 +245,26 @@ class JsonAllocator { return *this; } ~JsonAllocator() { deallocate(); } - void *allocate(size_t size); + void* allocate(size_t size); void deallocate(); }; -bool isHomogeneousArray(const JsonValue &v) noexcept; +bool isHomogeneousArray(const JsonValue& v) noexcept; // Parser wrapper class JsonParser { public: - JsonParser(LargeStringStorageT *strings = nullptr) : largeStrings_(strings ? strings : &internalLargeStrings_) {} + JsonParser(LargeStringStorageT* strings = nullptr) : largeStrings_(strings ? strings : &internalLargeStrings_) {} // Inplace parse. Buffer pointed by str will be changed - JsonNode Parse(span str, size_t *length = nullptr); + JsonNode Parse(span str, size_t* length = nullptr); // Copy str. Buffer pointed by str will be copied - JsonNode Parse(std::string_view str, size_t *length = nullptr); + JsonNode Parse(std::string_view str, size_t* length = nullptr); private: JsonAllocator alloc_; std::string tmp_; LargeStringStorageT internalLargeStrings_; - LargeStringStorageT *largeStrings_; + LargeStringStorageT* largeStrings_; }; } // namespace gason diff --git a/readme.md b/readme.md index 3669b7007..93e615986 100644 --- a/readme.md +++ b/readme.md @@ -1209,6 +1209,11 @@ Example code for aggregate `items` by `price` and `name` } ``` +Sorting by aggregated`FACET`'s fields has distinct syntax in it's SQL version: +```sql +SELECT FACET(name, price ORDER BY "name" ASC, "count" DESC) FROM items +``` + ### Search in array fields with matching array indexes Reindexer allows to search data in array fields when matching values have same indexes positions.