NCBI C++ Toolkit Cross Reference

  C++/src/db/bdb/bdb_query.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
/* $Id: bdb_query.cpp 65025 2014-10-30 16:24:42Z vasilche $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Anatoliy Kuznetsov * * File Description: BDB libarary query implementation * */ #include <ncbi_pch.hpp> #include <db/bdb/bdb_query.hpp> #include <db/bdb/bdb_cursor.hpp> #include <db/bdb/bdb_util.hpp> #include <util/resource_pool.hpp> #include <util/strsearch.hpp> BEGIN_NCBI_SCOPE ///////////////////////////////////////////////////////////////////////////// // CBDB_QueryNode // CBDB_QueryNode::CBDB_QueryNode(string value) : m_NodeType(eValue), m_Value(value) { } CBDB_QueryNode::CBDB_QueryNode(ELogicalType ltype) : m_NodeType(eLogical) { m_SubType.LogicalType = ltype; } CBDB_QueryNode::CBDB_QueryNode(EOperatorType otype, bool not_flag) : m_NodeType(eOperator), m_NotFlag(not_flag) { m_SubType.OperatorType = otype; } CBDB_QueryNode::CBDB_QueryNode(const CBDB_QueryNode& qnode) { m_NodeType = qnode.m_NodeType; m_Value = qnode.m_Value; m_SubType.LogicalType = qnode.m_SubType.LogicalType; m_NotFlag = qnode.m_NotFlag; } CBDB_QueryNode& CBDB_QueryNode::operator=(const CBDB_QueryNode& qnode) { m_NodeType = qnode.m_NodeType; m_Value = qnode.m_Value; m_SubType.LogicalType = qnode.m_SubType.LogicalType; m_NotFlag = qnode.m_NotFlag; return *this; } CBDB_QueryNode::~CBDB_QueryNode() { } CBDB_QueryNode::ELogicalType CBDB_QueryNode::GetLogicType() const { if (m_NodeType == eLogical) { return m_SubType.LogicalType; } // Caller asking to get sub-type as logical when the node type is not BDB_THROW(eQueryError, "Incorrect query node type"); } CBDB_QueryNode::EOperatorType CBDB_QueryNode::GetOperatorType() const { if (m_NodeType == eOperator) { return m_SubType.OperatorType; } // Caller asking to get sub-type as operator when the node type is not BDB_THROW(eQueryError, "Incorrect query node type"); } void CBDB_QueryNode::SetField(int field_idx) { m_NodeType = eDBField; m_SubType.FieldIdx = field_idx; } ///////////////////////////////////////////////////////////////////////////// // CBDB_Query // CBDB_Query::CBDB_Query(TQueryClause* qc) { if (qc) { m_QueryClause = qc; } else { m_QueryClause = new TQueryClause; } } CBDB_Query::~CBDB_Query() { delete m_QueryClause; } /// Reset all intermidiate results of tree evaluation (values) /// value constants will not be destroyed static ETreeTraverseCode s_ResetQueryNode(CTreeNode<CBDB_QueryNode>& tr, int delta) { if (delta < 0) return eTreeTraverse; CBDB_QueryNode& qnode = tr.GetValue(); if (qnode.GetType() != CBDB_QueryNode::eValue) { qnode.SetValue(kEmptyStr); } qnode.SetAltValue(kEmptyStr); return eTreeTraverse; } void CBDB_Query::ResetQueryClause() { TreeDepthFirstTraverse(*m_QueryClause, s_ResetQueryNode); } void CBDB_Query::SetQueryClause(TQueryClause* query_clause) { delete m_QueryClause; m_QueryClause = query_clause; } CBDB_Query::TQueryClause* CBDB_Query::NewOperatorNode(CBDB_QueryNode::EOperatorType otype, const string& arg1, const string& arg2) { auto_ptr<TQueryClause> tr(new TQueryClause(CBDB_QueryNode(otype))); tr->AddNode(new TQueryClause(CBDB_QueryNode(arg1))); tr->AddNode(new TQueryClause(CBDB_QueryNode(arg2))); return tr.release(); } CBDB_Query::TQueryClause* CBDB_Query::NewOperatorNode(CBDB_QueryNode::EOperatorType otype, TQueryClause* arg1, TQueryClause* arg2) { auto_ptr<TQueryClause> tr(new TQueryClause(CBDB_QueryNode(otype))); if (arg1) tr->AddNode(arg1); if (arg2) tr->AddNode(arg2); return tr.release(); } CBDB_Query::TQueryClause* CBDB_Query::NewLogicalNode(CBDB_QueryNode::ELogicalType ltype, TQueryClause* arg1, TQueryClause* arg2) { auto_ptr<TQueryClause> tr(new TQueryClause(CBDB_QueryNode(ltype))); if (arg1) tr->AddNode(arg1); if (arg2) tr->AddNode(arg2); return tr.release(); } ///////////////////////////////////////////////////////////////////////////// // Internal evaluation functors // /// Field resolution functor /// /// Every node is inspected if it contains a field value and /// marks it using CBDB_File::TUnifiedFieldIndex /// class CQueryTreeFieldResolveFunc { public: CQueryTreeFieldResolveFunc(CBDB_File& db_file) : m_File(db_file) {} ETreeTraverseCode operator()(CTreeNode<CBDB_QueryNode>& tr, int delta) { CBDB_QueryNode& qnode = tr.GetValue(); if (delta == 0 || delta == 1) { // If node has children, we skip it and process on the way back if (!tr.IsLeaf()) return eTreeTraverse; } if (qnode.GetType() == CBDB_QueryNode::eDBField) return eTreeTraverse; if (qnode.HasValue()) { string& fvalue = qnode.GetValue(); size_t len = fvalue.length(); // if string value is in apostrophe marks: remove it if (fvalue[0] == '\'' && fvalue[len-1] == '\'') { len -= 2; if (len) { qnode.SetValue(fvalue.substr(1, len)); } else { qnode.SetValue(kEmptyStr); } } else { CBDB_File::TUnifiedFieldIndex fidx = m_File.GetFieldIdx(fvalue); if (fidx) { qnode.SetField(fidx); } } } return eTreeTraverse; } private: CBDB_File& m_File; }; /// Query execition environment class CQueryExecEnv { public: CQueryExecEnv(CBDB_File& db_file) : m_File(db_file) {} CBDB_File& GetFile(void) { return m_File; } CResourcePool<string>& GetStrPool() { return m_StrPool; } private: CQueryExecEnv(const CQueryExecEnv& qenv); CQueryExecEnv& operator=(const CQueryExecEnv& qenv); private: CBDB_File& m_File; /// Query database file CResourcePool<string> m_StrPool; /// Pool of string variables }; /// Create new matcher /// /// @internal static CBoyerMooreMatcher* s_MakeNewMatcher(const string& search_value) { CBoyerMooreMatcher* matcher = new CBoyerMooreMatcher(search_value, NStr::eNocase, CBoyerMooreMatcher::ePrefixMatch); matcher->InitCommonDelimiters(); return matcher; } /// Base class for functions of the evaluation engine /// /// @internal class CScannerFunctor { public: CScannerFunctor(CQueryExecEnv& env) : m_QueryEnv(env) {} private: CScannerFunctor(const CScannerFunctor&); CScannerFunctor& operator=(const CScannerFunctor&); protected: CQueryExecEnv& m_QueryEnv; }; /// Base class for N argument functions /// /// @internal class CScannerFunctorArgN : public CScannerFunctor { public: /// Enum indicates how to interpret the plain value /// tree elements. It can be takes as is (eNoCheck) or /// converted to a "in any field" variant of check /// (similar to what PubMed does) enum EAllFieldsCheck { eNoCheck, eCheckAll }; /// Vector of strings borrowed from the query environment /// pool to keep temporary values during the query execution typedef vector<string*> TArgValueVector; /// Vector of arguments, elements can point on values from /// TArgValueVector or variables located in the query tree itself typedef vector<const string*> TArgVector; /// String matchers used for substring search typedef vector<CBoyerMooreMatcher*> TStringMatcherVector; /// If argument is a db field corresponding vector element /// contains the field pointer typedef vector<const CBDB_Field*> TFieldVector; public: CScannerFunctorArgN(CQueryExecEnv& env) : CScannerFunctor(env) {} ~CScannerFunctorArgN(); /// Checks if value is equal to any field in the database bool IsAnyField(CBDB_File& dbf, const string& search_value, unsigned int arg_idx) { CBoyerMooreMatcher* matcher = GetMatcher(search_value, arg_idx); CBDB_File::TUnifiedFieldIndex fidx = BDB_find_field(dbf, *matcher, &m_TmpStr); return (fidx != 0); } CBoyerMooreMatcher* GetMatcher(const string& search_value, unsigned int arg_idx) { CBoyerMooreMatcher* matcher = m_MatcherVector[arg_idx]; if (!matcher) { m_MatcherVector[arg_idx] = matcher = s_MakeNewMatcher(search_value); } return matcher; } /// Extract function arguments from the parsing tree void GetArguments(CTreeNode<CBDB_QueryNode>& tr, EAllFieldsCheck check_mode = eNoCheck) { CBDB_File& db_file = m_QueryEnv.GetFile(); CResourcePool<string>& str_pool = m_QueryEnv.GetStrPool(); typedef CTreeNode<CBDB_QueryNode> TTree; TTree::TNodeList_CI it = tr.SubNodeBegin(); TTree::TNodeList_CI it_end = tr.SubNodeEnd(); for (unsigned i = 0; it != it_end; ++it, ++i) { const TTree* t = *it; const CBDB_QueryNode& qnode = t->GetValue(); ResizeVectors(i + 1); // Check if the argument should be taken from the // db field if (qnode.GetType() == CBDB_QueryNode::eDBField) { int fidx = qnode.GetFiledIdx(); const CBDB_Field& fld = db_file.GetField(fidx); SyncArgValue(i, str_pool); string& pool_str = *m_ArgValueVector[i]; if (fld.IsNull()) { pool_str = kEmptyStr; } else { fld.ToString(pool_str); } m_FieldVector[i] = &fld; continue; } // field value check mode // eCheckAll is coming from logical functions if they // support "value AND anothervalue" notation // (search services work like that) if ((check_mode == eCheckAll) && (qnode.GetType() == CBDB_QueryNode::eValue)) { const string& v = qnode.GetValue(); const char* sz = "0"; if (IsAnyField(db_file, v, i)) { sz = "1"; } SyncArgValue(i, str_pool); *m_ArgValueVector[i] = sz; continue; } // Get constant value or return type of the subnodes const string& str = qnode.GetValue(); m_ArgVector[i] = &str; } // for } const string* GetArg(size_t idx) const { _ASSERT(idx < m_ArgVector.size()); return m_ArgVector[idx]; } const CBDB_Field* GetArgField(size_t idx) const { _ASSERT(idx < m_FieldVector.size()); return m_FieldVector[idx]; } void SetResult(CBDB_QueryNode& qnode, bool res) { if (res) { qnode.SetValue("1"); } else { qnode.SetValue("0"); } } private: /// Syncronously resize all arguments vectors void ResizeVectors(unsigned int new_size) { TArgVector::size_type old_size = m_ArgVector.size(); _ASSERT(new_size <= old_size + 1); // 1 increments only if (new_size > old_size) { m_ArgVector.push_back(0); m_ArgValueVector.push_back(0); m_MatcherVector.push_back(0); m_FieldVector.push_back(0); } } /// m_ArgVector[idx] = m_ArgValueVector[idx] void SyncArgValue(unsigned int idx, CResourcePool<string>& str_pool) { if (m_ArgValueVector[idx] == 0) { m_ArgValueVector[idx] = str_pool.Get(); } m_ArgVector[idx] = m_ArgValueVector[idx]; } protected: TArgVector m_ArgVector; TArgValueVector m_ArgValueVector; TStringMatcherVector m_MatcherVector; TFieldVector m_FieldVector; string m_TmpStr; }; CScannerFunctorArgN::~CScannerFunctorArgN() { CResourcePool<string>& str_pool = m_QueryEnv.GetStrPool(); size_t size = m_ArgValueVector.size(); for (size_t i = 0; i < size; ++i) { string* str = m_ArgValueVector[i]; if (str) { str_pool.Return(str); } CBoyerMooreMatcher* matcher = m_MatcherVector[i]; delete matcher; } } /// EQ function /// /// @internal class CScannerFunctorEQ : public CScannerFunctorArgN { public: CScannerFunctorEQ(CQueryExecEnv& env, bool not_flag) : CScannerFunctorArgN(env), m_NotFlag(not_flag) {} bool IsNot() const { return m_NotFlag; } void Eval(CTreeNode<CBDB_QueryNode>& tr) { GetArguments(tr); CBDB_QueryNode& qnode = tr.GetValue(); const string* arg0 = GetArg(0); const string* arg1 = GetArg(1); const CBDB_Field* fld0 = GetArgField(0); const CBDB_Field* fld1 = GetArgField(1); // here we check two cases: // field = "value" // "value" == field if (fld0 != 0 && fld1 == 0) { CBoyerMooreMatcher* matcher = GetMatcher(*arg1, 0); size_t pos = matcher->Search(*arg0); if (pos == NPOS) { // not found qnode.SetValue("0"); } else { qnode.SetValue("1"); } return; } if (fld0 == 0 && fld1 != 0) { CBoyerMooreMatcher* matcher = GetMatcher(*arg0, 0); size_t pos = matcher->Search(*arg1); if (pos == NPOS) { // not found qnode.SetValue("0"); } else { qnode.SetValue("1"); } return; } // Plain equal bool res = (*arg0 == *arg1); if (IsNot()) res = !res; SetResult(qnode, res); } protected: bool m_NotFlag; }; /// Basic class for all comparison (GT, LT, etc) functors /// /// @internal class CScannerFunctorComp : public CScannerFunctorArgN { public: CScannerFunctorComp(CQueryExecEnv& env) : CScannerFunctorArgN(env) {} int CmpEval(CTreeNode<CBDB_QueryNode>& tr) { GetArguments(tr); const string* arg0 = GetArg(0); const string* arg1 = GetArg(1); int cmpres = arg0->compare(*arg1); return cmpres; } }; /// GT function /// /// @internal class CScannerFunctorGT : public CScannerFunctorComp { public: CScannerFunctorGT(CQueryExecEnv& env) : CScannerFunctorComp(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { int cmpres = CmpEval(tr); bool res = cmpres > 0; SetResult(tr.GetValue(), res); } }; /// GE function /// /// @internal class CScannerFunctorGE : public CScannerFunctorComp { public: CScannerFunctorGE(CQueryExecEnv& env) : CScannerFunctorComp(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { int cmpres = CmpEval(tr); bool res = cmpres >= 0; SetResult(tr.GetValue(), res); } }; /// LT function /// /// @internal class CScannerFunctorLT : public CScannerFunctorComp { public: CScannerFunctorLT(CQueryExecEnv& env) : CScannerFunctorComp(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { int cmpres = CmpEval(tr); bool res = cmpres < 0; SetResult(tr.GetValue(), res); } }; /// LE function /// /// @internal class CScannerFunctorLE : public CScannerFunctorComp { public: CScannerFunctorLE(CQueryExecEnv& env) : CScannerFunctorComp(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { int cmpres = CmpEval(tr); bool res = cmpres <= 0; SetResult(tr.GetValue(), res); } }; /// AND function /// /// @internal class CScannerFunctorAND : public CScannerFunctorArgN { public: CScannerFunctorAND(CQueryExecEnv& env) : CScannerFunctorArgN(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { GetArguments(tr, eCheckAll); CBDB_QueryNode& qnode = tr.GetValue(); size_t size = m_ArgVector.size(); _ASSERT(size); for (size_t i = 0; i < size; ++i) { const string* arg = GetArg(i); if (*arg == "0") { qnode.SetValue("0"); return; } } qnode.SetValue("1"); } }; /// OR function /// /// @internal class CScannerFunctorOR : public CScannerFunctorArgN { public: CScannerFunctorOR(CQueryExecEnv& env) : CScannerFunctorArgN(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { GetArguments(tr, eCheckAll); CBDB_QueryNode& qnode = tr.GetValue(); size_t size = m_ArgVector.size(); _ASSERT(size); for (size_t i = 0; i < size; ++i) { const string* arg = GetArg(i); if (*arg == "1") { qnode.SetValue("1"); return; } } qnode.SetValue("0"); } }; /// NOT function /// /// @internal class CScannerFunctorNOT : public CScannerFunctorArgN { public: CScannerFunctorNOT(CQueryExecEnv& env) : CScannerFunctorArgN(env) {} void Eval(CTreeNode<CBDB_QueryNode>& tr) { GetArguments(tr, eCheckAll); CBDB_QueryNode& qnode = tr.GetValue(); const string* arg = GetArg(0); if (*arg == "0") { qnode.SetValue("1"); } else { qnode.SetValue("0"); } } }; ///////////////////////////////////////////////////////////////////////////// // CBDB_FileScanner // CBDB_FileScanner::CBDB_FileScanner(CBDB_File& db_file) : m_File(db_file) { } CBDB_FileScanner::~CBDB_FileScanner() { } CBDB_FileScanner::EScanAction CBDB_FileScanner::OnRecordFound() { return eContinue; } void CBDB_FileScanner::Scan(CBDB_Query& query) { ResolveFields(query); CBDB_FileCursor cur(m_File); cur.SetCondition(CBDB_FileCursor::eFirst); while (cur.Fetch() == eBDB_Ok) { bool res = Evaluate(query); if (res) { EScanAction act = OnRecordFound(); if (act == eStop) { break; } } query.ResetQueryClause(); } // while } void CBDB_FileScanner::Scan(CBDB_FileCursor& cur, CBDB_Query& query) { ResolveFields(query); while (cur.Fetch() == eBDB_Ok) { bool res = Evaluate(query); if (res) { EScanAction act = OnRecordFound(); if (act == eStop) { break; } } query.ResetQueryClause(); } // while } /// The main tree evaluation functor /// /// @internal class CScannerEvaluateFunc { public: CScannerEvaluateFunc(CQueryExecEnv& env) : m_QueryEnv(env), m_Matcher(0) { } ~CScannerEvaluateFunc() { delete m_Matcher; } CScannerEvaluateFunc(const CScannerEvaluateFunc& func) : m_QueryEnv(func.m_QueryEnv), m_Matcher(0) { } ETreeTraverseCode operator()(CTreeNode<CBDB_QueryNode>& tr, int delta); protected: CQueryExecEnv& m_QueryEnv; CBoyerMooreMatcher* m_Matcher; }; ETreeTraverseCode CScannerEvaluateFunc::operator()(CTreeNode<CBDB_QueryNode>& tr, int delta) { CBDB_QueryNode& qnode = tr.GetValue(); // cout << delta << " " << tr.GetValue().GetValue() << endl; if (delta == 0 || delta == 1) { // If node has children, we skip it and process on the way back if (!tr.IsLeaf()) return eTreeTraverse; } if (qnode.GetType() == CBDB_QueryNode::eValue) { if (tr.GetParent() == 0) { // single top node CBDB_File& dbf = m_QueryEnv.GetFile(); if (!m_Matcher) { const string& search_value = qnode.GetValue(); m_Matcher = s_MakeNewMatcher(search_value); } CBDB_File::TUnifiedFieldIndex fidx; fidx = BDB_find_field(dbf, *m_Matcher); qnode.SetAltValue(fidx ? "1" : "0"); } } if (!qnode.HasValue()) { switch (qnode.GetType()) { case CBDB_QueryNode::eValue: break; case CBDB_QueryNode::eOperator: { CBDB_QueryNode::EOperatorType eop = qnode.GetOperatorType(); switch (eop) { case CBDB_QueryNode::eEQ: { CScannerFunctorEQ func(m_QueryEnv, qnode.IsNot()); func.Eval(tr); } break; case CBDB_QueryNode::eGT: { CScannerFunctorGT func(m_QueryEnv); func.Eval(tr); } break; case CBDB_QueryNode::eGE: { CScannerFunctorGE func(m_QueryEnv); func.Eval(tr); } break; case CBDB_QueryNode::eLT: { CScannerFunctorLT func(m_QueryEnv); func.Eval(tr); } break; case CBDB_QueryNode::eLE: { CScannerFunctorLE func(m_QueryEnv); func.Eval(tr); } break; default: _ASSERT(0); } // switch eop } break; case CBDB_QueryNode::eLogical: { CBDB_QueryNode::ELogicalType elogic = qnode.GetLogicType(); switch (elogic) { case CBDB_QueryNode::eAnd: { CScannerFunctorAND func(m_QueryEnv); func.Eval(tr); } break; case CBDB_QueryNode::eOr: { CScannerFunctorOR func(m_QueryEnv); func.Eval(tr); } break; case CBDB_QueryNode::eNot: { CScannerFunctorNOT func(m_QueryEnv); func.Eval(tr); } break; default: _ASSERT(0); } // switch elogic } break; default: break; } // switch node type } // if return eTreeTraverse; } bool CBDB_FileScanner::Evaluate(CBDB_Query& query) { ResolveFields(query); CBDB_Query::TQueryClause& qtree = query.GetQueryClause(); CQueryExecEnv query_env(m_File); CScannerEvaluateFunc scanner_eval(query_env); TreeDepthFirstTraverse(qtree, scanner_eval); const CBDB_QueryNode& qnode = qtree.GetValue(); const string& v_alt = qnode.GetAltValue(); if (v_alt.empty()) { const string& v = qnode.GetValue(); if (v == "0") return false; return true; } else { if (v_alt == "0") return false; } return true; } void CBDB_FileScanner::ResolveFields(CBDB_Query& query) { CBDB_Query::TQueryClause& qtree = query.GetQueryClause(); CQueryTreeFieldResolveFunc resolve_func(m_File); TreeDepthFirstTraverse(qtree, resolve_func); } /// The main tree printing functor class. /// Used for internal debugging purposes. /// /// @internal /// class CQueryTreePrintFunc { public: CQueryTreePrintFunc(CNcbiOstream& os) : m_OStream(os), m_Level(0) {} void PrintLevelMargin() { for (int i = 0; i < m_Level; ++i) { m_OStream << " "; } } ETreeTraverseCode operator()(const CTreeNode<CBDB_QueryNode>& tr, int delta) { const CBDB_QueryNode& qnode = tr.GetValue(); m_Level += delta; if (delta < 0) return eTreeTraverse; PrintLevelMargin(); switch (qnode.GetType()) { case CBDB_QueryNode::eValue: m_OStream << qnode.GetValue(); break; case CBDB_QueryNode::eOperator: { CBDB_QueryNode::EOperatorType eop = qnode.GetOperatorType(); switch (eop) { case CBDB_QueryNode::eEQ: if (qnode.IsNot()) { m_OStream << "NOT EQ"; } else { m_OStream << "EQ"; } break; case CBDB_QueryNode::eGT: m_OStream << "GT"; break; case CBDB_QueryNode::eGE: m_OStream << "GE"; break; case CBDB_QueryNode::eLT: m_OStream << "LT"; break; case CBDB_QueryNode::eLE: m_OStream << "LE"; break; default: _ASSERT(0); } // switch eop } if (qnode.HasValue()) { m_OStream << " => " << qnode.GetValue(); } break; case CBDB_QueryNode::eLogical: { CBDB_QueryNode::ELogicalType elogic = qnode.GetLogicType(); switch (elogic) { case CBDB_QueryNode::eAnd: m_OStream << "AND"; break; case CBDB_QueryNode::eOr: m_OStream << "OR"; break; case CBDB_QueryNode::eNot: m_OStream << "NOT"; break; default: _ASSERT(0); } // switch elogic } if (qnode.HasValue()) { m_OStream << " => " << qnode.GetValue(); } break; case CBDB_QueryNode::eDBField: m_OStream << "@" << qnode.GetValue(); break; default: if (qnode.HasValue()) { m_OStream << qnode.GetValue(); } break; } // switch node type m_OStream << "\n"; return eTreeTraverse; } private: CNcbiOstream& m_OStream; int m_Level; }; void BDB_PrintQueryTree(CNcbiOstream& os, const CBDB_Query& query) { // Here I use a const cast hack because TreeDepthFirstTraverse // uses a non-cost iterators and semantics in the algorithm. // When a const version of TreeDepthFirstTraverse is ready // we can get rid of this... const CBDB_Query::TQueryClause& qtree = query.GetQueryClause(); CBDB_Query::TQueryClause& qtree_nc = const_cast<CBDB_Query::TQueryClause&>(qtree); CQueryTreePrintFunc func(os); TreeDepthFirstTraverse(qtree_nc, func); } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.