[livejournal] r20443: LJSUP-10247: New RS driver
Committer: vsukhanov
LJSUP-10247: New RS driverU trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm U trunk/cgi-bin/LJ/RelationService.pm
Modified: trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm
===================================================================
--- trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm 2011-10-27 08:39:43 UTC (rev 20442)
+++ trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm 2011-10-27 14:21:25 UTC (rev 20443)
@@ -71,6 +71,75 @@
##
+sub create_relation_to {
+ my $class = shift;
+ my $u = shift;
+ my %opts = @_;
+
+ my $friendid = $opts{friendid};
+ my $qfg = $opts{qfg};
+ my $qbg = $opts{qbg};
+ my $groupmask = int($opts{groupmask});
+
+ my $dbh = LJ::get_db_writer();
+ my $cnt = $dbh->do("REPLACE INTO friends
+ (userid, friendid, fgcolor, bgcolor, groupmask)
+ VALUES
+ (?, ?, ?, ?, ?)
+ ", undef, $u->userid, $friendid, $qfg, $qbg, $groupmask);
+ die "create_relation_to error: " . DBI->errstr if DBI->errstr;
+
+ my $memkey = [$u->userid,"frgmask:" . $u->userid . ":$friendid"];
+ LJ::MemCache::set($memkey, $groupmask, time()+60*15);
+ LJ::memcache_kill($friendid, 'friendofs');
+ LJ::memcache_kill($friendid, 'friendofs2');
+
+ # invalidate memcache of friends
+ LJ::memcache_kill($u->userid, "friends");
+ LJ::memcache_kill($u->userid, "friends2");
+
+ LJ::run_hooks('befriended', $u, LJ::load_userid($friendid));
+ LJ::User->increase_friendsof_counter($friendid);
+
+ return $cnt;
+}
+
+
+sub remove_relation_to {
+ my $class = shift;
+ my $u = shift;
+ my %opts = @_;
+
+ my $friendid = int $opts{friendid};
+
+ my $dbh = LJ::get_db_writer()
+ or return 0;
+
+ my $cnt = $dbh->do("
+ DELETE
+ FROM friends WHERE
+ userid = ? AND
+ friendid=?
+ ", undef, $u->userid, $friendid);
+
+ if (!$dbh->err && $cnt > 0) {
+ LJ::run_hooks('defriended', $u, LJ::load_userid($friendid));
+ LJ::User->decrease_friendsof_counter($friendid);
+
+ # delete friend-of memcache keys for anyone who was removed
+ LJ::MemCache::delete([ $u->userid, "frgmask:" . $u->userid . ":$friendid" ]);
+ LJ::memcache_kill($friendid, 'friendofs');
+ LJ::memcache_kill($friendid, 'friendofs2');
+
+ LJ::memcache_kill($u->userid, 'friends');
+ LJ::memcache_kill($u->userid, 'friends2');
+ }
+
+ return $cnt;
+}
+
+
+##
## Private methods
##
@@ -437,9 +506,9 @@
# database and insert those into memcache
my $dbh = LJ::get_db_writer();
- my $limit = $limit ? '' : " LIMIT " . ($LJ::MAX_FRIENDOF_LOAD+1);
+ my $limit_sql = $limit ? '' : " LIMIT " . ($LJ::MAX_FRIENDOF_LOAD+1);
my $friendofs = $dbh->selectcol_arrayref
- ("SELECT userid FROM friends WHERE friendid=? $limit",
+ ("SELECT userid FROM friends WHERE friendid=? $limit_sql",
undef, $u->userid) || [];
die $dbh->errstr if $dbh->err;
Modified: trunk/cgi-bin/LJ/RelationService.pm
===================================================================
--- trunk/cgi-bin/LJ/RelationService.pm 2011-10-27 08:39:43 UTC (rev 20442)
+++ trunk/cgi-bin/LJ/RelationService.pm 2011-10-27 14:21:25 UTC (rev 20443)
@@ -1,21 +1,49 @@
package LJ::RelationService;
use strict;
+use LJ::RelationService::RSAPI;
use LJ::RelationService::MysqlAPI;
+sub _load_alt_api {
+ my $class = shift;
+ my $user = shift;
+ return 0 unless LJ::is_enable('send_test_load_to_rs2');
+
+ my $rate = $LJ::RELATION_SERVICE_LOAD_RATE; ## 0 .. 100
+ return 0 unless $rate;
+
+ ##
+ return 1 if ( (int rand(100)) > $rate );
+
+ return 0;
+}
+
+
sub relation_api {
my $class = shift;
my $u = shift;
return "LJ::RelationService::MysqlAPI";
}
+sub alt_api {
+ my $class = shift;
+ my $u = shift;
+ return "LJ::RelationService::RSAPI";
+}
## findRelationDestinations
sub find_relation_destinations {
my $class = shift;
my $u = shift;
+ if ($class->_load_alt_api($u)){
+ my $alt = $class->alt_api($u);
+ if ($alt){
+ $alt->find_relation_destinations($u, @_);
+ }
+ }
+
my $interface = $class->relation_api($u);
return $interface->find_relation_destinations($u, @_);
@@ -26,6 +54,13 @@
my $class = shift;
my $u = shift;
+ if ($class->_load_alt_api($u)){
+ my $alt = $class->alt_api($u);
+ if ($alt){
+ $alt->find_relation_sources($u, @_);
+ }
+ }
+
my $interface = $class->relation_api($u);
return $interface->find_relation_sources($u, @_);
@@ -40,4 +75,37 @@
}
+sub create_relation_to {
+ my $class = shift;
+ my $u = shift;
+
+ if ($class->_load_alt_api($u)){
+ my $alt = $class->alt_api($u);
+ if ($alt){
+ $alt->create_relation_to($u, @_);
+ }
+ }
+
+ my $interface = $class->relation_api($u);
+ return $interface->create_relation_to($u, @_);
+}
+
+
+sub remove_relation_to {
+ my $class = shift;
+ my $u = shift;
+
+ if ($class->_load_alt_api($u)){
+ my $alt = $class->alt_api($u);
+ if ($alt){
+ $alt->create_relation_to($u, @_);
+ }
+ }
+ my $interface = $class->relation_api($u);
+ return $interface->remove_relation_to($u, @_);
+}
+
+
+
+
1
