$title"); } function printHTMLHighlighted($msg) { print("\n"); print("\n"); print("
$msg

\n"); } function printHTMLfooter($scriptName, $startTime) { $endTime = getMicroTime(); $totalTime = $endTime - $startTime; printf("


RUBiS (C) Rice University/INRIA
Page generated by $scriptName in %.3f seconds.
\n", $totalTime); printf("Virtual time is %s.
\n", virtualTimeSQL()); print("\n"); print("\n"); } function printError($scriptName, $startTime, $title, $error) { printHTMLheader("RUBiS ERROR: $title"); print("

We cannot process your request due to the following error :


\n"); print($error); printHTMLfooter($scriptName, $startTime); } function authenticateImpl($link, $nickname, $password) { txcache_invaltag("users", "nickname", $nickname); $result = sql_query("SELECT id FROM users WHERE nickname=\"$nickname\" AND password=\"$password\"", $link) or die("ERROR: Authentification query failed"); if (sql_num_rows($result) == 0) return -1; $row = sql_fetch_array($result); return $row["id"]; } function authenticate($nickname, $password, $link) { return wrap(true, 'authenticateImpl', $link, $nickname, $password); } $runningTransaction = null; $transactionIsReadOnly = false; function beginRO($link) { global $runningTransaction, $transactionIsReadOnly, $postgres, $TX; if ($runningTransaction) die("ERROR: runningTransaction is non-null"); if (TXCACHE) { if (RANDOMIZE_FRESHNESS) $freshness = rand(FRESHNESS*RANDOMIZE_FRESHNESS_FACTOR, FRESHNESS); else $freshness = FRESHNESS; txcache_begin_ro($TX, $freshness) or die("ERROR: Failed to beginRO"); } elseif ($postgres) sql_query("BEGIN READ ONLY", $link); else sql_query("BEGIN", $link); register_shutdown_function('maybe_rollback'); $runningTransaction = $link; $transactionIsReadOnly = true; } function beginRW($link) { global $runningTransaction, $transactionIsReadOnly; if ($runningTransaction) die("ERROR: runningTransaction is non-null"); sql_query("BEGIN", $link); register_shutdown_function('maybe_rollback'); $runningTransaction = $link; $transactionIsReadOnly = false; } function commit($link) { global $runningTransaction, $transactionIsReadOnly, $TX; if ($transactionIsReadOnly && TXCACHE) txcache_commit($TX) or die("ERROR: Failed to commit"); else sql_query("COMMIT", $link); $runningTransaction = null; } function rollback($link) { global $runningTransaction, $transactionIsReadOnly, $TX; if ($transactionIsReadOnly && TXCACHE) txcache_commit($TX) or die("ERROR: Failed to commit"); else sql_query("ROLLBACK", $link); $runningTransaction = null; } function maybe_rollback() { global $runningTransaction; if ($runningTransaction) { echo "

Rolling back from abort


\n"; rollback($runningTransaction); } } if (TXCACHE) { function txcache_inval($table, $index, $value) { global $TX; if ($index == "*") { $tag = "$table:"; } else { $tag = "$table:$index=$value:"; } if (TXCACHEINVALIDATIONS) { txcache_explicitly_invalidate($TX, $tag); } } function txcache_invaltag($table, $index, $value) { global $TX; if ($index == "*") { $tag = "$table:"; } else { $tag = "$table:$index=$value:"; } if (TXCACHEINVALIDATIONS) { txcache_add_explicit_invalidation_tag($TX, $tag); } } function wrap() { $args = func_get_args(); // if (!TXCACHEINVALIDATIONS) // $args[0] = false; array_unshift($args, $args[2]); return call_user_func_array('txcache_wrap', $args); } } else { function txcache_inval($table, $index, $value) { } function txcache_invaltag($table, $index, $value) { } function wrap() { $args = func_get_args(); array_shift($args); return call_user_func_array('call_user_func', $args); } } if (MICROCACHE) { function getUserImpl($link, $userId) { txcache_invaltag("users", "id", $userId); $res = sql_query("SELECT * FROM users WHERE users.id=$userId", $link) or die("ERROR: Query for user $userId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } function getUser($link, $userId) { global $TX; return txcache_wrap($TX, true, 'getUserImpl', $link, $userId); } function getItemImpl($link, $itemId) { txcache_invaltag("items", "id", $itemId); $res = sql_query("SELECT * FROM items WHERE id=$itemId", $link) or die("ERROR: Query for item $itemId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } function getItem($link, $itemId) { global $TX; return txcache_wrap($TX, true, 'getItemImpl', $link, $itemId); } function getOldItemImpl($link, $itemId) { txcache_invaltag("old_items", "id", $itemId); $res = sql_query("SELECT * FROM old_items WHERE id=$itemId", $link) or die("ERROR: Query for old item $itemId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } function getOldItem($link, $itemId) { global $TX; return txcache_wrap($TX, true, 'getOldItemImpl', $link, $itemId); } function getAnyItemImpl($link, $itemId) { $row = getItem($link, $itemId); if ($row) return $row; $row = getOldItemImpl($link, $itemId); return $row; } function getAnyItem($link, $itemId) { global $TX; return txcache_wrap($TX, true, 'getAnyItemImpl', $link, $itemId); } } else { function getUser($link, $userId) { $res = sql_query("SELECT * FROM users WHERE users.id=$userId", $link) or die("ERROR: Query for user $userId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } function getItem($link, $itemId) { $res = sql_query("SELECT * FROM items WHERE id=$itemId", $link) or die("ERROR: Query for item $itemId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } function getAnyItem($link, $itemId) { $row = getItem($link, $itemId); if ($row) return $row; $res = sql_query("SELECT * FROM old_items WHERE id=$itemId", $link) or die("ERROR: Query for old item $itemId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } function getOldItem($link, $itemId) { $res = sql_query("SELECT * FROM old_items WHERE id=$itemId", $link) or die("ERROR: Query for old item $itemId failed"); if (sql_num_rows($res) == 0) return NULL; $row = pg_fetch_assoc($res); sql_free_result($res); return $row; } } function printCommentTableImpl($link, $userId) { // Get the comments about the user txcache_invaltag("comments", "to_user", $userId); $commentsResult = sql_query("SELECT * FROM comments WHERE comments.to_user_id=$userId", $link) or die("ERROR: Query failed for the list of comments."); if (sql_num_rows($commentsResult) == 0) print("

There is no comment for this user.


\n"); else { print("
\n"); while ($commentsRow = sql_fetch_array($commentsResult)) { $authorId = $commentsRow["from_user_id"]; txcache_invaltag("users", "id", $authorId); $authorRow = getUser($link, $authorId); $authorName = $authorRow["nickname"]; $date = $commentsRow["date"]; $comment = $commentsRow["comment"]; print("
$authorName"." wrote the ".$date."
".$comment."

\n"); } print("

\n"); } sql_free_result($commentsResult); } function printCommentTable($link, $userId) { global $TX; return txcache_wrap($TX, true, 'printCommentTableImpl', $link, $userId); } if (VIRTUALIZE_TIME) { function virtualTime() { return time() - REAL_TIME_BASE + VIRTUAL_TIME_BASE; } } else { function virtualTime() { return time(); } } function virtualTimeSQL() { return date("Y-m-d H:i:s", virtualTime()); } ?>