File indexing completed on 2025-03-09 04:43:29

0001 /*
0002  * This file is part of the KDE Akonadi Search Project
0003  * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #include "agepostingsource.h"
0010 
0011 #include <QDateTime>
0012 #include <QString>
0013 
0014 #include <cmath>
0015 
0016 using namespace Akonadi::Search;
0017 
0018 AgePostingSource::AgePostingSource(Xapian::valueno slot_)
0019     : Xapian::ValuePostingSource(slot_)
0020     , m_currentTime_t(QDateTime::currentDateTimeUtc().toSecsSinceEpoch())
0021 {
0022 }
0023 
0024 Xapian::weight AgePostingSource::get_weight() const
0025 {
0026     const std::string s = *value_it;
0027     const QString str = QString::fromUtf8(s.c_str(), s.length());
0028 
0029     bool ok = false;
0030     const uint time = str.toUInt(&ok);
0031 
0032     if (!ok) {
0033         return 0.0;
0034     }
0035 
0036     const uint diff = m_currentTime_t - time;
0037 
0038     // Each day is given a penalty of penalty of 1.0
0039     const double penalty = 1.0 / (24 * 60 * 60);
0040     const double result = 1000.0 - (diff * penalty);
0041 
0042     if (result < 0.0) {
0043         return 0.0;
0044     }
0045 
0046     return result;
0047 }
0048 
0049 Xapian::PostingSource *AgePostingSource::clone() const
0050 {
0051     return new AgePostingSource(slot);
0052 }
0053 
0054 void AgePostingSource::init(const Xapian::Database &db_)
0055 {
0056     Xapian::ValuePostingSource::init(db_);
0057     set_maxweight(1000.0);
0058 }