Commit d6bb4fac authored by Karl Denninger's avatar Karl Denninger

ZP-1284 Add ChangesSink code to the back end; style change on a few queries.

Released under the Affero GNU General Public License (AGPL) version 3.
parent 0de16943
...@@ -40,6 +40,9 @@ class BackendStickyNote extends BackendDiff { ...@@ -40,6 +40,9 @@ class BackendStickyNote extends BackendDiff {
private $_dbconn; private $_dbconn;
private $_result; private $_result;
private $_changessinkinit;
private $_sinkdata;
/** /**
* Constructor * Constructor
*/ */
...@@ -47,6 +50,9 @@ class BackendStickyNote extends BackendDiff { ...@@ -47,6 +50,9 @@ class BackendStickyNote extends BackendDiff {
if (!function_exists("pg_connect")) { if (!function_exists("pg_connect")) {
throw new FatalException("BackendStickyNote(): Postgres extension php-pgsql not found", 0, null, LOGLEVEL_FATAL); throw new FatalException("BackendStickyNote(): Postgres extension php-pgsql not found", 0, null, LOGLEVEL_FATAL);
} }
$this->_changessinkinit = false;
$this->_sinkdata = 0;
} }
/** /**
...@@ -90,6 +96,8 @@ class BackendStickyNote extends BackendDiff { ...@@ -90,6 +96,8 @@ class BackendStickyNote extends BackendDiff {
ZLog::Write(LOGLEVEL_DEBUG, "BackendStickyNote->Logoff(): disconnected from Postgres server"); ZLog::Write(LOGLEVEL_DEBUG, "BackendStickyNote->Logoff(): disconnected from Postgres server");
unset($this->_sinkdata);
return true; return true;
} }
...@@ -187,10 +195,10 @@ class BackendStickyNote extends BackendDiff { ...@@ -187,10 +195,10 @@ class BackendStickyNote extends BackendDiff {
$_param = array(); $_param = array();
if ($cutoffdate) { if ($cutoffdate) {
array_push($_param, $this->_user, $this->_domain, $cutoffdate); array_push($_param, $this->_user, $this->_domain, $cutoffdate);
$this->_result = pg_query_params($this->_dbconn, "select ordinal, extract(epoch from modified)::integer from note where modified <= timestamptz 'epoch' + $3 * interval '1 second' and login=$1 and domain=$2 and deleted is not true", $_param); $this->_result = pg_query_params($this->_dbconn, "select ordinal, extract(epoch from modified)::integer from note where modified <= timestamptz 'epoch' + $3 * interval '1 second' and login=$1 and domain=$2 and deleted is false", $_param);
} else { } else {
array_push($_param, $this->_user, $this->_domain); array_push($_param, $this->_user, $this->_domain);
$this->_result = pg_query_params($this->_dbconn, "select ordinal, extract(epoch from modified)::integer from note where login=$1 and domain=$2 and deleted is not true", $_param); $this->_result = pg_query_params($this->_dbconn, "select ordinal, extract(epoch from modified)::integer from note where login=$1 and domain=$2 and deleted is false", $_param);
} }
if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) { if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendStickyNote->GetMessageList(Failed to return a valid message list from database)")); ZLog::Write(LOGLEVEL_WARN, sprintf("BackendStickyNote->GetMessageList(Failed to return a valid message list from database)"));
...@@ -220,7 +228,7 @@ class BackendStickyNote extends BackendDiff { ...@@ -220,7 +228,7 @@ class BackendStickyNote extends BackendDiff {
$_params = array(); $_params = array();
array_push($_params, $id, $this->_user, $this->_domain); array_push($_params, $id, $this->_user, $this->_domain);
$this->_result = pg_query_params($this->_dbconn, "select *, extract(epoch from modified)::integer as changed from note where ordinal = $1 and login = $2 and domain = $3 and deleted is not true", $_params); $this->_result = pg_query_params($this->_dbconn, "select *, extract(epoch from modified)::integer as changed from note where ordinal = $1 and login = $2 and domain = $3 and deleted is false", $_params);
if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) { if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendStickyNote->GetMessage(FAILED query for '%s','%s')", $folderid, $id)); ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendStickyNote->GetMessage(FAILED query for '%s','%s')", $folderid, $id));
return false; return false;
...@@ -274,7 +282,7 @@ class BackendStickyNote extends BackendDiff { ...@@ -274,7 +282,7 @@ class BackendStickyNote extends BackendDiff {
$message = array(); $message = array();
$_params = array(); $_params = array();
array_push($_params, $id, $this->_user, $this->_domain); array_push($_params, $id, $this->_user, $this->_domain);
$this->_result = pg_query_params($this->_dbconn, "select extract(epoch from modified)::integer from note where ordinal=$1 and login=$2 and domain=$3 and deleted is not true", $_params); $this->_result = pg_query_params($this->_dbconn, "select extract(epoch from modified)::integer from note where ordinal=$1 and login=$2 and domain=$3 and deleted is false", $_params);
if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) { if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendStickyNote->StatMessage(Stat call failed for '%s')", $id)); ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendStickyNote->StatMessage(Stat call failed for '%s')", $id));
return $message; return $message;
...@@ -456,6 +464,76 @@ class BackendStickyNote extends BackendDiff { ...@@ -456,6 +464,76 @@ class BackendStickyNote extends BackendDiff {
return false; return false;
} }
/*
* Tell the upper layers that we have a ChangesSink. It's simple since we
* can easily track the last modification time of any item for a given user,
* so we can tell the system very quickly when something has changed.
*
* @access public
* @return boolean
*/
public function HasChangesSink() {
return true;
}
/*
* Initialize the sink for a folder (there's only one)
*
* @param string $folderid
*
* @access public
* return boolean Always true since there's only one folder
*/
public function ChangesSinkInitialize($folderid) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendStickyNote->ChangesSinkInitialize(): folderid '%s'", $folderid));
$this->changessinkinit = true;
return $this->changessinkinit;
}
/*
* The Changesink.
* If there's nothing to return in changes, wait 30 seconds. Otherwise
* send back the FolderId -- there's only one.
*
* @param int $timeout How long to block if specified
*
* @access public
* @return array
*/
public function ChangesSink($timeout = 30) {
$_notifications = array();
// Apparently this can get called before we've initialized, which in
// our case wouldn't matter, but for consistency return nothing if
// that happens - or if it gets called before the database is connected.
if ((!$this->changessinkinit) || ($this->_dbconn == false)) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendStickyNote->ChangesSink - Not initialized ChangesSink, sleep and exit"));
sleep($timeout);
return $notifications;
}
$_param = array();
array_push($_param, $_user, $_domain);
$this->_result = pg_query_params($this->_dbconn, "select extract(epoch from modified)::integer from note where login=$1 and domain=$2 and deleted is false order by modified desc limit 1", $_param);
if (pg_result_status($this->_result) != PGSQL_TUPLES_OK) {
ZLog::Write(LOGLEVEL_WARN, sprintf("BackendStickyNote->ChangesSink(Failed to return a valid change list)"));
} else {
if (pg_affected_rows($this->_result) == 1) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendStickyNote->ChangesSink(There are valid notes stored)"));
$_lastchange = pg_fetch_result($this->_result, 0, 0);
if ($_lastchange != $this->_sinkdata) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendStickyNote->ChangesSink(Change noted; tell the upper layers)"));
array_push($_notifications, "N");
$this->sinkdata = $_lastchange;
}
}
}
pg_free_result($this->_result);
if (empty($_notifications)) {
sleep($_timeout);
}
return($_notifications);
}
/** /**
* Indicates which AS version is supported by the backend. * Indicates which AS version is supported by the backend.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment