summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/components
diff options
context:
space:
mode:
Diffstat (limited to 'sca-cpp/trunk/components')
-rw-r--r--sca-cpp/trunk/components/cache/partitioner.cpp58
-rw-r--r--sca-cpp/trunk/components/sqldb/pgsql.hpp70
2 files changed, 115 insertions, 13 deletions
diff --git a/sca-cpp/trunk/components/cache/partitioner.cpp b/sca-cpp/trunk/components/cache/partitioner.cpp
index c7c49c04de..8469456fc8 100644
--- a/sca-cpp/trunk/components/cache/partitioner.cpp
+++ b/sca-cpp/trunk/components/cache/partitioner.cpp
@@ -31,6 +31,7 @@
#include "string.hpp"
#include "function.hpp"
#include "list.hpp"
+#include "tree.hpp"
#include "value.hpp"
#include "monad.hpp"
@@ -78,9 +79,53 @@ const failable<list<value> > getlist(const value& key, const list<value>& partit
}
/**
+ * Return the rank of a result item if available.
+ */
+const value itemrank(const value& val) {
+ if (isList(val) && !isNull(val)) {
+ const value e = car<value>(val);
+ if (isList(e) && !isNull(e)) {
+ if (car<value>(e) == "entry") {
+ const list<value> ae = cdr<value>(e);
+ const list<value> li = assoc<value>("id", ae);
+ const list<value> lr = assoc<value>("rank", ae);
+ if (!isNull(li) && !isNull(lr))
+ return mklist<value>(lr, li);
+ }
+ }
+ }
+ return val;
+}
+
+/**
+ * Compare the ranks of two result items.
+ */
+const int rankitems(const value& a, const value& b) {
+ const value ra = itemrank(a);
+ const value rb = itemrank(b);
+ if (ra == rb)
+ return 0;
+ if (ra < rb)
+ return -1;
+ return 1;
+}
+
+/**
+ * Convert a key to a (param name, value) assoc.
+ */
+const list<value> keyparams(const list<value>& key) {
+ if (isNull(key))
+ return nilListValue;
+ if (!isList(car(key)))
+ return keyparams(cdr(key));
+ return cons<value>(car(key), keyparams(cdr(key)));
+}
+
+/**
* Get an item from a partition.
*/
const failable<value> get(const value& key, const lvvlambda& selector, const list<value>& partitions) {
+ debug(key, "partitioner::get::key");
// Select partition
const failable<list<value> > fp = partition(key, selector, partitions);
@@ -97,6 +142,7 @@ const failable<value> get(const value& key, const lvvlambda& selector, const lis
os << "Couldn't get entry from partition: " << key;
return mkfailure<value>(str(os), 404, false);
}
+ debug(val, "partitioner::get::val");
return val;
}
@@ -104,7 +150,17 @@ const failable<value> get(const value& key, const lvvlambda& selector, const lis
const failable<list<value> > val = getlist(key, p);
if (!hasContent(val))
return mkfailure<value>(val);
- return (value)content(val);
+ const list<value> cval = content(val);
+ debug(cval, "partitioner::get::cval");
+
+ // Apply any ranking sort and result limit
+ const list<value> kparams = isList(key)? keyparams(key) : nilListValue;
+ const list<value> sval = (!isNull(assoc<value>("textsearch", kparams)) || !isNull(assoc<value>("rank", kparams)))? reverse<value>(sort<value>(cval, rankitems)) : cval;
+ const list<value> limit = assoc<value>("limit", kparams);
+ const list<value> lval = !isNull(limit)? listHead(sval, (int)cadr(limit)) : sval;
+ debug(lval, "partitioner::get::lval");
+
+ return (value)lval;
}
/**
diff --git a/sca-cpp/trunk/components/sqldb/pgsql.hpp b/sca-cpp/trunk/components/sqldb/pgsql.hpp
index e7c7c5d5cd..1b23e9f84b 100644
--- a/sca-cpp/trunk/components/sqldb/pgsql.hpp
+++ b/sca-cpp/trunk/components/sqldb/pgsql.hpp
@@ -339,13 +339,50 @@ const list<value> keyparams(const list<value>& key) {
}
/**
- * Convert a get result to a list of items.
+ * Convert an SQL result row to a result item.
*/
-const list<value> getitems(PGresult* const r, const int i, const int n) {
+const value getitem(PGresult* const r, const int i, const int rk) {
+ const value val(content(scheme::readValue(string(PQgetvalue(r, i, 1)))));
+ if (rk > 0) {
+ // Add row tsrank and rank to result item if it's an ATOM entry
+ if (isList(val) && !isNull(val)) {
+ const value e = car<value>(val);
+ if (isList(e) && !isNull(e)) {
+ if (car<value>(e) == "entry") {
+ const list<value> ae = cdr<value>(e);
+ const list<value> lt = assoc<value>("title", ae);
+ const list<value> li = assoc<value>("id", ae);
+ const list<value> la = assoc<value>("author", ae);
+ const list<value> lu = assoc<value>("updated", ae);
+ const list<value> lc = assoc<value>("content", ae);
+
+ ostringstream ros;
+ ros << string(PQgetvalue(r, i, 2));
+ if (rk > 1)
+ ros << " " << string(PQgetvalue(r, i, 3));
+ const list<value> lr = mklist<value>("rank", str(ros));
+
+ const value rval = mklist<value>("entry") +
+ (isNull(lt)? nilListValue : lt) + (isNull(li)? nilListValue : li) + (isNull(la)? nilListValue : la) +
+ (isNull(lu)? nilListValue : lu) + lr + (isNull(lc)? nilListValue : lc);
+ debug(rval, "pgsql::getitem::rval");
+ return mklist<value>(rval);
+ }
+ }
+ }
+ }
+
+ debug(val, "pgsql::getitem::val");
+ return val;
+}
+
+/**
+ * Convert an SQL result to a list of result items.
+ */
+const list<value> getitems(PGresult* const r, const int i, const int n, const int rk) {
if (i == n)
return nilListValue;
- const value val(content(scheme::readValue(string(PQgetvalue(r, i, 1)))));
- return cons<value>(val, getitems(r, i + 1, n));
+ return cons<value>(getitem(r, i, rk), getitems(r, i + 1, n, rk));
}
/**
@@ -396,42 +433,51 @@ const failable<value> get(const value& key, const PGSql& pgsql) {
const char* sqlparams[6];
int p = 0;
int w = 0;
+ int rk = 0;
ostringstream sqlos;
sqlos << "select data." << kname << ", data." << vname;
if (!isNull(textsearch)) {
// Text search, setup text result ranking
sqlos << ", ts_rank_cd(to_tsvector(data." << vname << "), tsquery, 32) as tsrank";
+ rk++;
}
if (!isNull(rank)) {
// Ranking, setup rank expression
const string rs = (string)cadr(rank);
- sqlparams[p++] = c_str(rs);
- sqlos << ", $" << p << " as rank";
+ sqlos << ", " << rs << " as rank";
+ rk++;
}
sqlos << " from " << table << " data";
if (!isNull(textsearch)) {
// Text search, define the query
const string ts = tstranslate((string)cadr(textsearch));
+ debug(ts, "pgsql::get::sqlparam");
sqlparams[p++] = c_str(ts);
sqlos << ", plainto_tsquery($" << p << ") tsquery";
}
if (!lk || !isNull(id)) {
// Query of the form key = id
- sqlparams[p++] = c_str(write(content(scheme::writeValue(lk? (value)id : key))));
+ const string ks = write(content(scheme::writeValue(lk? (value)id : key)));
+ debug(ks, "pgsql::get::sqlparam");
+ sqlparams[p++] = c_str(ks);
sqlos << (w == 0? " where" : " and");
sqlos << " data." << kname << " = $" << p;
w++;
}
if (!isNull(regex)) {
// Query of the form key ~ param
- sqlparams[p++] = c_str((string)cadr(regex));
+ const string rs = cadr(regex);
+ debug(rs, "pgsql::get::sqlparam");
+ sqlparams[p++] = c_str(rs);
sqlos << (w == 0? " where" : " and");
sqlos << " data." << kname << " ~ $" << p;
w++;
}
if (!isNull(like)) {
// Query of the form key like param
- sqlparams[p++] = c_str((string)cadr(like));
+ const string ls = cadr(like);
+ debug(ls, "pgsql::get::sqlparam");
+ sqlparams[p++] = c_str(ls);
sqlos << (w == 0? " where" : " and");
sqlos << " data." << kname << " like $" << p;
w++;
@@ -442,7 +488,7 @@ const failable<value> get(const value& key, const PGSql& pgsql) {
sqlos << " tsquery @@ to_tsvector(data." << vname << ")";
w++;
}
- if (!isNull(textsearch) || !isNull(rank)) {
+ if (!isNull(rank) || !isNull(textsearch)) {
// Result ordering
sqlos << " order by" << (isNull(rank)? "" : " rank desc") << ((isNull(rank) || isNull(textsearch))? "" : ",") << (isNull(textsearch)? "" : " tsrank desc");
}
@@ -473,14 +519,14 @@ const failable<value> get(const value& key, const PGSql& pgsql) {
// Return a collection of items
if (l != 1) {
- const list<value> lval = getitems(r, 0, n);
+ const list<value> lval = getitems(r, 0, n, rk);
PQclear(r);
debug(lval, "pgsql::get::result");
return (value)lval;
}
// Return a single item
- const value val(content(scheme::readValue(string(PQgetvalue(r, 0, 1)))));
+ const value val = getitem(r, 0, rk);
PQclear(r);
debug(val, "pgsql::get::result");
return val;