add new "votes" field in bookmarks database with the total sum of votes

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@509 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
cweiske 2009-11-03 09:14:30 +00:00
parent 02051fcf67
commit b80df612d5
3 changed files with 72 additions and 4 deletions

View file

@ -17,6 +17,7 @@ CREATE TABLE `sc_bookmarks` (
`bDescription` text default NULL, `bDescription` text default NULL,
`bPrivateNote` text default NULL, `bPrivateNote` text default NULL,
`bHash` varchar(32) NOT NULL default '', `bHash` varchar(32) NOT NULL default '',
`bVotes` int(11) NOT NULL,
`bVoting` int(11) NOT NULL, `bVoting` int(11) NOT NULL,
PRIMARY KEY (`bId`), PRIMARY KEY (`bId`),
KEY `sc_bookmarks_usd` (`uId`,`bStatus`,`bDatetime`), KEY `sc_bookmarks_usd` (`uId`,`bStatus`,`bDatetime`),

View file

@ -21,10 +21,10 @@
* *
* @internal * @internal
* Votes are saved in a separate "votes" table. * Votes are saved in a separate "votes" table.
* Additionally to that, the voting of a bookmark is also * Additionally to that, the voting and number of votes
* stored in the bookmarks table. This is done to make * of a bookmark is also stored in the bookmarks table.
* sure lookups are really fast, since every bookmarks * This is done to make sure lookups are really fast, since
* in a list shows its voting. * every bookmark in a list shows its voting.
* *
* @category Bookmarking * @category Bookmarking
* @package SemanticScuttle * @package SemanticScuttle
@ -104,6 +104,12 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
* Returns the number of users that voted for or * Returns the number of users that voted for or
* against the given bookmark. * against the given bookmark.
* *
* @internal
* This method uses the votes table to calculate the
* number of votes for a bookmark. In normal life, it
* is more efficient to use the "bVotes" field in the
* bookmarks table.
*
* @param integer $bookmark Bookmark ID * @param integer $bookmark Bookmark ID
* *
* @return integer Number of votes * @return integer Number of votes
@ -248,6 +254,7 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
$res = $this->db->sql_query( $res = $this->db->sql_query(
$sql='UPDATE ' . $bm->getTableName() $sql='UPDATE ' . $bm->getTableName()
. ' SET bVoting = bVoting + ' . (int)$vote . ' SET bVoting = bVoting + ' . (int)$vote
. ' , bVotes = bVotes + 1'
. ' WHERE bId = ' . (int)$bookmark . ' WHERE bId = ' . (int)$bookmark
); );
$this->db->sql_freeresult($res); $this->db->sql_freeresult($res);
@ -283,6 +290,7 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
$res = $this->db->sql_query( $res = $this->db->sql_query(
$sql='UPDATE ' . $bm->getTableName() $sql='UPDATE ' . $bm->getTableName()
. ' SET bVoting = bVoting - ' . (int)$vote . ' SET bVoting = bVoting - ' . (int)$vote
. ' , bVotes = bVotes - 1'
. ' WHERE bId = ' . (int)$bookmark . ' WHERE bId = ' . (int)$bookmark
); );
$this->db->sql_freeresult($res); $this->db->sql_freeresult($res);
@ -306,6 +314,9 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService
$bm = SemanticScuttle_Service_Factory::get('Bookmark'); $bm = SemanticScuttle_Service_Factory::get('Bookmark');
$query = 'UPDATE ' . $bm->getTableName() . ' as B SET bVoting = ' $query = 'UPDATE ' . $bm->getTableName() . ' as B SET bVoting = '
. '(SELECT SUM(vote) FROM ' . $this->getTableName() . ' as V' . '(SELECT SUM(vote) FROM ' . $this->getTableName() . ' as V'
. ' WHERE V.bId = B.bId GROUP BY bid)'
. ', bVotes = '
. '(SELECT COUNT(vote) FROM ' . $this->getTableName() . ' as V'
. ' WHERE V.bId = B.bId GROUP BY bid)'; . ' WHERE V.bId = B.bId GROUP BY bid)';
$this->db->sql_query($query); $this->db->sql_query($query);
} }

View file

@ -80,6 +80,10 @@ class VoteTest extends TestBase
{ {
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->assertEquals(0, $this->vs->getVoting($bid)); $this->assertEquals(0, $this->vs->getVoting($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(0, $bm['bVoting']);
$this->assertEquals(0, $bm['bVotes']);
} }
@ -94,6 +98,10 @@ class VoteTest extends TestBase
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->vs->vote($bid, 1, 1); $this->vs->vote($bid, 1, 1);
$this->assertEquals(1, $this->vs->getVoting($bid)); $this->assertEquals(1, $this->vs->getVoting($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
} }
@ -108,6 +116,10 @@ class VoteTest extends TestBase
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->vs->vote($bid, 1, -1); $this->vs->vote($bid, 1, -1);
$this->assertEquals(-1, $this->vs->getVoting($bid)); $this->assertEquals(-1, $this->vs->getVoting($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
} }
@ -125,6 +137,10 @@ class VoteTest extends TestBase
$this->vs->vote($bid, 3, 1); $this->vs->vote($bid, 3, 1);
$this->vs->vote($bid, 4, 1); $this->vs->vote($bid, 4, 1);
$this->assertEquals(2, $this->vs->getVoting($bid)); $this->assertEquals(2, $this->vs->getVoting($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(2, $bm['bVoting']);
$this->assertEquals(4, $bm['bVotes']);
} }
@ -138,6 +154,10 @@ class VoteTest extends TestBase
{ {
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->assertEquals(0, $this->vs->getVotes($bid)); $this->assertEquals(0, $this->vs->getVotes($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(0, $bm['bVoting']);
$this->assertEquals(0, $bm['bVotes']);
} }
@ -152,6 +172,10 @@ class VoteTest extends TestBase
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->vs->vote($bid, 1, 1); $this->vs->vote($bid, 1, 1);
$this->assertEquals(1, $this->vs->getVotes($bid)); $this->assertEquals(1, $this->vs->getVotes($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
} }
@ -169,6 +193,10 @@ class VoteTest extends TestBase
$this->vs->vote($bid, 3, 1); $this->vs->vote($bid, 3, 1);
$this->vs->vote($bid, 4, 1); $this->vs->vote($bid, 4, 1);
$this->assertEquals(4, $this->vs->getVotes($bid)); $this->assertEquals(4, $this->vs->getVotes($bid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(2, $bm['bVoting']);
$this->assertEquals(4, $bm['bVotes']);
} }
@ -223,6 +251,7 @@ class VoteTest extends TestBase
$bm2 = $this->bs->getBookmark($bid2); $bm2 = $this->bs->getBookmark($bid2);
$this->assertEquals(0, $bm2['bVoting']); $this->assertEquals(0, $bm2['bVoting']);
$this->assertEquals(0, $bm2['bVotes']);
} }
@ -318,9 +347,17 @@ class VoteTest extends TestBase
$this->assertTrue($this->vs->vote($bid, $uid, 1)); $this->assertTrue($this->vs->vote($bid, $uid, 1));
$this->assertTrue($this->vs->vote($bid, $uid, 1)); $this->assertTrue($this->vs->vote($bid, $uid, 1));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->assertTrue($this->vs->vote($bid, $uid, -1)); $this->assertTrue($this->vs->vote($bid, $uid, -1));
$this->assertTrue($this->vs->vote($bid, $uid, 1)); $this->assertTrue($this->vs->vote($bid, $uid, 1));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
} }
@ -336,6 +373,10 @@ class VoteTest extends TestBase
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->assertTrue($this->vs->vote($bid, $uid, 1)); $this->assertTrue($this->vs->vote($bid, $uid, 1));
$this->assertEquals(1, $this->vs->getVote($bid, $uid)); $this->assertEquals(1, $this->vs->getVote($bid, $uid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
} }
@ -351,6 +392,10 @@ class VoteTest extends TestBase
$bid = $this->addBookmark(); $bid = $this->addBookmark();
$this->assertTrue($this->vs->vote($bid, $uid, -1)); $this->assertTrue($this->vs->vote($bid, $uid, -1));
$this->assertEquals(-1, $this->vs->getVote($bid, $uid)); $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
$bm = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
} }
@ -372,6 +417,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']); $this->assertEquals(1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
//change vote //change vote
$this->assertTrue($this->vs->vote($bid, $uid, -1)); $this->assertTrue($this->vs->vote($bid, $uid, -1));
@ -380,6 +426,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']); $this->assertEquals(-1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
} }
@ -401,6 +448,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']); $this->assertEquals(-1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
//change vote //change vote
$this->assertTrue($this->vs->vote($bid, $uid, 1)); $this->assertTrue($this->vs->vote($bid, $uid, 1));
@ -409,6 +457,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']); $this->assertEquals(1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
} }
@ -430,6 +479,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']); $this->assertEquals(1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
//change vote //change vote
$this->assertTrue($this->vs->vote($bid, $uid, 1)); $this->assertTrue($this->vs->vote($bid, $uid, 1));
@ -438,6 +488,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(1, $b['bVoting']); $this->assertEquals(1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
} }
@ -459,6 +510,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']); $this->assertEquals(-1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
//change vote to same value //change vote to same value
$this->assertTrue($this->vs->vote($bid, $uid, -1)); $this->assertTrue($this->vs->vote($bid, $uid, -1));
@ -467,6 +519,7 @@ class VoteTest extends TestBase
$b = $this->bs->getBookmark($bid); $b = $this->bs->getBookmark($bid);
$this->assertEquals(-1, $b['bVoting']); $this->assertEquals(-1, $b['bVoting']);
$this->assertEquals(1, $b['bVotes']);
} }
@ -484,17 +537,20 @@ class VoteTest extends TestBase
$bm = $this->bs->getBookmark($bid); $bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']); $this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
$this->vs->deleteAll(); $this->vs->deleteAll();
//we assume that $vs->deleteAll() does *not* reset //we assume that $vs->deleteAll() does *not* reset
//voting in bookmarks table //voting in bookmarks table
$bm = $this->bs->getBookmark($bid); $bm = $this->bs->getBookmark($bid);
$this->assertEquals(1, $bm['bVoting']); $this->assertEquals(1, $bm['bVoting']);
$this->assertEquals(1, $bm['bVotes']);
$this->vs->rewriteVotings(); $this->vs->rewriteVotings();
$bm = $this->bs->getBookmark($bid); $bm = $this->bs->getBookmark($bid);
//now it should be reset to 0 //now it should be reset to 0
$this->assertEquals(0, $bm['bVoting']); $this->assertEquals(0, $bm['bVoting']);
$this->assertEquals(0, $bm['bVotes']);
} }
}//class VoteTest extends TestBase }//class VoteTest extends TestBase