diff --git a/src/query/docseqdb.cpp b/src/query/docseqdb.cpp index be0737d8..c5c46c98 100644 --- a/src/query/docseqdb.cpp +++ b/src/query/docseqdb.cpp @@ -124,17 +124,7 @@ bool DocSequenceDb::setSortSpec(const DocSeqSortSpec &sortspec) { LOGDEB(("DocSequenceDb::setSortSpec\n")); if (sortspec.isNotNull()) { - bool ascending = false; - for (unsigned int i = 0; i < sortspec.crits.size(); i++) { - switch (sortspec.crits[i]) { - case DocSeqSortSpec::RCLFLD_MTIME: - ascending = !sortspec.dirs[i]; - break; - default: - break; - } - } - m_q->setSortBy("mtime", ascending); + m_q->setSortBy(sortspec.field, !sortspec.desc); } else { m_q->setSortBy(string(), true); } diff --git a/src/query/sortseq.cpp b/src/query/sortseq.cpp index 6a9d605a..259778aa 100644 --- a/src/query/sortseq.cpp +++ b/src/query/sortseq.cpp @@ -35,56 +35,33 @@ public: { LOGDEB1(("Comparing .. \n")); - // Compare using each criterion in term. Further comparisons must only - // be made if previous order ones are equal. - for (unsigned int i = 0; i < ss.crits.size(); i++) { - switch (ss.crits[i]) { - case DocSeqSortSpec::RCLFLD_MTIME: - { - long xmtime = x->dmtime.empty() ? atol(x->fmtime.c_str()) : - atol(x->dmtime.c_str()); - long ymtime = y->dmtime.empty() ? atol(y->fmtime.c_str()) : - atol(y->dmtime.c_str()); - - LOGDEB1((" MTIME %ld %ld\n", xmtime, ymtime)); - if (ss.dirs[i] ? xmtime > ymtime : xmtime < ymtime) - return 1; - else if (xmtime != ymtime) - return 0; - } - break; - case DocSeqSortSpec::RCLFLD_MIMETYPE: - LOGDEB1((" MIMETYPE\n")); - if (ss.dirs[i] ? x->mimetype > y->mimetype : - x->mimetype < y->mimetype) - return 1; - else if (x->mimetype != y->mimetype) - return 0; - break; - } - } - // Did all comparisons - return 0; + map::const_iterator xit, yit; + xit = x->meta.find(ss.field); + yit = y->meta.find(ss.field); + if (xit == x->meta.end() || yit == y->meta.end()) + return 0; + return ss.desc ? yit->second < xit->second : xit->second < yit->second; } }; bool DocSeqSorted::setSortSpec(DocSeqSortSpec &sortspec) { + LOGDEB(("DocSeqSorted::setSortSpec\n")); m_spec = sortspec; - LOGDEB(("DocSeqSorted:: count %d\n", m_spec.sortdepth)); - m_docs.resize(m_spec.sortdepth); + int count = m_seq->getResCnt(); + LOGDEB(("DocSeqSorted:: count %d\n", count)); + m_docs.resize(count); int i; - for (i = 0; i < m_spec.sortdepth; i++) { + for (i = 0; i < count; i++) { if (!m_seq->getDoc(i, m_docs[i])) { LOGERR(("DocSeqSorted: getDoc failed for doc %d\n", i)); + count = i; break; } } - m_spec.sortdepth = i; - LOGDEB(("DocSeqSorted:: m_count %d\n", m_spec.sortdepth)); - m_docs.resize(m_spec.sortdepth); - m_docsp.resize(m_spec.sortdepth); - for (i = 0; i < m_spec.sortdepth; i++) + m_docs.resize(count); + m_docsp.resize(count); + for (i = 0; i < count; i++) m_docsp[i] = &m_docs[i]; CompareDocs cmp(sortspec); @@ -94,9 +71,8 @@ bool DocSeqSorted::setSortSpec(DocSeqSortSpec &sortspec) bool DocSeqSorted::getDoc(int num, Rcl::Doc &doc, string *) { - LOGDEB1(("DocSeqSorted: fetching %d\n", num)); - - if (num >= m_spec.sortdepth) + LOGDEB(("DocSeqSorted::getDoc(%d)\n", num)); + if (num < 0 || num >= int(m_docsp.size())) return false; doc = *m_docsp[num]; return true; diff --git a/src/query/sortseq.h b/src/query/sortseq.h index e5c32d3b..c8235331 100644 --- a/src/query/sortseq.h +++ b/src/query/sortseq.h @@ -39,7 +39,7 @@ class DocSeqSorted : public DocSeqModifier { virtual bool canSort() {return true;} virtual bool setSortSpec(DocSeqSortSpec &sortspec); virtual bool getDoc(int num, Rcl::Doc &doc, string *sh = 0); - virtual int getResCnt() {return m_spec.sortdepth;} + virtual int getResCnt() {return m_docsp.size();} private: DocSeqSortSpec m_spec; std::vector m_docs;