GUI restable: fix sorting by file and doc size
This commit is contained in:
parent
6b04fe7f2c
commit
48e86c99b5
@ -154,6 +154,16 @@ static string gengetter(const string& fld, const Rcl::Doc& doc)
|
||||
return it->second;
|
||||
}
|
||||
|
||||
static string sizegetter(const string& fld, const Rcl::Doc& doc)
|
||||
{
|
||||
map<string, string>::const_iterator it = doc.meta.find(fld);
|
||||
if (it == doc.meta.end()) {
|
||||
return string();
|
||||
}
|
||||
off_t size = atoll(it->second.c_str());
|
||||
return displayableBytes(size) + " (" + it->second + ")";
|
||||
}
|
||||
|
||||
static string dategetter(const string&, const Rcl::Doc& doc)
|
||||
{
|
||||
char datebuf[100];
|
||||
@ -162,7 +172,11 @@ static string dategetter(const string&, const Rcl::Doc& doc)
|
||||
time_t mtime = doc.dmtime.empty() ?
|
||||
atol(doc.fmtime.c_str()) : atol(doc.dmtime.c_str());
|
||||
struct tm *tm = localtime(&mtime);
|
||||
#ifndef sun
|
||||
strftime(datebuf, 99, "%Y-%m-%d", tm);
|
||||
#else
|
||||
strftime(datebuf, 99, "%x", tm);
|
||||
#endif
|
||||
}
|
||||
return datebuf;
|
||||
}
|
||||
@ -212,6 +226,8 @@ FieldGetter *RecollModel::chooseGetter(const string& field)
|
||||
return dategetter;
|
||||
else if (!stringlowercmp("datetime", field))
|
||||
return datetimegetter;
|
||||
else if (!stringlowercmp("bytes", field.substr(1)))
|
||||
return sizegetter;
|
||||
else
|
||||
return gengetter;
|
||||
}
|
||||
|
||||
@ -138,12 +138,6 @@ static inline string make_parentterm(const string& udi)
|
||||
return pterm;
|
||||
}
|
||||
|
||||
static inline void leftzeropad(string& s, unsigned len)
|
||||
{
|
||||
if (s.length() && s.length() < len)
|
||||
s = s.insert(0, len - s.length(), '0');
|
||||
}
|
||||
|
||||
/* See comment in class declaration: return all subdocuments of a
|
||||
* document given by its unique id.
|
||||
*/
|
||||
|
||||
@ -54,13 +54,17 @@ static const string& docfToDatf(const string& df)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort helper class
|
||||
// Sort helper class. As Xapian sorting is lexicographic, we do some
|
||||
// special processing for special fields like dates and sizes. User
|
||||
// custom field data will have to be processed before insertion to
|
||||
// achieve equivalent results.
|
||||
class QSorter : public Xapian::Sorter {
|
||||
public:
|
||||
QSorter(const string& f)
|
||||
: m_fld(docfToDatf(f) + "=")
|
||||
{
|
||||
m_ismtime = !m_fld.compare("dmtime=");
|
||||
m_issize = !m_fld.compare("fbytes=") || !m_fld.compare("dbytes=");
|
||||
}
|
||||
|
||||
virtual std::string operator()(const Xapian::Document& xdoc) const
|
||||
@ -88,14 +92,22 @@ public:
|
||||
i2 = data.find_first_of("\n\r", i1);
|
||||
if (i2 == string::npos)
|
||||
return string();
|
||||
|
||||
|
||||
string term = data.substr(i1, i2-i1);
|
||||
if (m_ismtime) {
|
||||
return term;
|
||||
} else if (m_issize) {
|
||||
// Left zeropad values for appropriate numeric sorting
|
||||
leftzeropad(term, 12);
|
||||
return term;
|
||||
}
|
||||
|
||||
// Process data for better sorting. We should actually do the
|
||||
// unicode thing
|
||||
// (http://unicode.org/reports/tr10/#Introduction), but just
|
||||
// removing accents and majuscules will remove the most
|
||||
// glaring weirdnesses (or not, depending on your national
|
||||
// approach to collating...)
|
||||
string term = data.substr(i1, i2-i1);
|
||||
string sortterm;
|
||||
// We're not even sure the term is utf8 here (ie: url)
|
||||
if (!unacmaybefold(term, sortterm, "UTF-8", true)) {
|
||||
@ -114,6 +126,7 @@ public:
|
||||
private:
|
||||
string m_fld;
|
||||
bool m_ismtime;
|
||||
bool m_issize;
|
||||
};
|
||||
|
||||
Query::Query(Db *db)
|
||||
|
||||
@ -562,16 +562,22 @@ bool pcSubst(const string& in, string& out, map<string, string>& subs)
|
||||
// Convert byte count into unit (KB/MB...) appropriate for display
|
||||
string displayableBytes(off_t size)
|
||||
{
|
||||
char sizebuf[30];
|
||||
const char * unit = " B ";
|
||||
char sizebuf[50];
|
||||
const char *unit;
|
||||
|
||||
if (size > 1024 && size < 1024*1024) {
|
||||
if (size < 1024) {
|
||||
unit = " B ";
|
||||
} else if (size < 1024*1024) {
|
||||
unit = " KB ";
|
||||
size /= 1024;
|
||||
} else if (size >= 1024*1204) {
|
||||
} else if (size < 1024*1024*1024) {
|
||||
unit = " MB ";
|
||||
size /= (1024*1024);
|
||||
} else {
|
||||
unit = " GB ";
|
||||
size /= (1024*1024*1024);
|
||||
}
|
||||
|
||||
sprintf(sizebuf, OFFTPC "%s", size, unit);
|
||||
return string(sizebuf);
|
||||
}
|
||||
|
||||
@ -178,6 +178,12 @@ struct TempBuf {
|
||||
char *m_buf;
|
||||
};
|
||||
|
||||
inline void leftzeropad(string& s, unsigned len)
|
||||
{
|
||||
if (s.length() && s.length() < len)
|
||||
s = s.insert(0, len - s.length(), '0');
|
||||
}
|
||||
|
||||
#ifndef deleteZ
|
||||
#define deleteZ(X) {delete X;X = 0;}
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user