alf (red_alf) wrote in changelog,
alf
red_alf
changelog

[livejournal] r21230: LJSUP-10900: Implement MessagePack proto...

Committer: afedorov
LJSUP-10900: Implement MessagePack protocol driver to access to Relation Service.

U   trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm
U   trunk/cgi-bin/LJ/RelationService.pm
U   trunk/cgi-bin/LJ/User.pm
U   trunk/cgi-bin/ljrelation.pl
Modified: trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm
===================================================================
--- trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm	2012-02-21 13:52:51 UTC (rev 21229)
+++ trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm	2012-02-21 15:16:47 UTC (rev 21230)
@@ -171,39 +171,85 @@
 
 ##
 sub create_relation_to {
-    my $class = shift;
-    my $u     = shift;
-    my %opts  = @_;
+    my $class  = shift;
+    my $u      = shift;
+    my $friend = shift;
+    my $type   = shift;
+    my %opts   = @_;
+    
+    if ( $type eq 'F' ) {
+        return $class->_create_relation_to_type_f($u, $friend, %opts);
+    } else {
+        return $class->_create_relation_to_type_other($u, $friend, $type, %opts);
+    }
+}
 
-    my $friendid   = $opts{friendid};
-    my $qfg        = $opts{qfg};
-    my $qbg        = $opts{qbg};
-    my $groupmask  = int($opts{groupmask});
+sub _create_relation_to_type_f {
+    my $class  = shift;
+    my $u      = shift;
+    my $friend = shift;
+    my %opts   = @_;
 
     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);
+                        ", undef, $u->userid, $friend->userid, $opts{fgcolor}, $opts{bgcolor}, $opts{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');
+    my $memkey = [$u->userid, "frgmask:" . $u->userid . ":" . $friend->userid];
+    LJ::MemCache::set($memkey, $opts{groupmask}, time()+60*15);
+    LJ::memcache_kill($friend->userid, 'friendofs');
+    LJ::memcache_kill($friend->userid, 'friendofs2');
 
     # invalidate memcache of friends
     LJ::memcache_kill($u->userid, "friends");
     LJ::memcache_kill($u->userid, "friends2");
+    LJ::mark_dirty($u->userid, "friends");
 
-    LJ::run_hooks('befriended', $u, LJ::load_userid($friendid));
-    LJ::User->increase_friendsof_counter($friendid);
-
+    LJ::run_hooks('befriended', $u, $friend);
+    LJ::User->increase_friendsof_counter($friend->userid);
+    
     return $cnt;
 }
 
+sub _create_relation_to_type_other {
+    my $class  = shift;
+    my $u      = shift;
+    my $friend = shift;
+    my $type   = shift;
+    my %opts   = @_;
 
+    my $typeid = LJ::get_reluser_id($type)+0;
+    my $eff_type = $typeid || $type;
+
+    # working on reluser or reluser2?
+    my ($db, $table);
+    if ($typeid) {
+        # clustered reluser2 table
+        $db = LJ::get_cluster_master($u);
+        $table = "reluser2";
+    } else {
+        # non-clustered reluser global table
+        $db = LJ::get_db_writer();
+        $table = "reluser";
+    }
+    return undef unless $db;
+
+    # set in database
+    $db->do("REPLACE INTO $table (userid, targetid, type) VALUES (?, ?, ?)",
+            undef, $u->userid, $friend->userid, $eff_type);
+    return undef if $db->err;
+
+    # set in memcache
+    LJ::_set_rel_memcache($u->userid, $friend->userid, $eff_type, 1);
+
+    return 1;
+}
+
+
 sub remove_relation_to {
     my $class = shift;
     my $u     = shift;

Modified: trunk/cgi-bin/LJ/RelationService.pm
===================================================================
--- trunk/cgi-bin/LJ/RelationService.pm	2012-02-21 13:52:51 UTC (rev 21229)
+++ trunk/cgi-bin/LJ/RelationService.pm	2012-02-21 15:16:47 UTC (rev 21230)
@@ -105,39 +105,38 @@
     $opts{offset} ||= 0;
     $opts{limit}  ||= 50000;
 
-    my $result;
-
     if ($class->_load_alt_api('read', $type)){
         my $alt = $class->alt_api($u);
         if ($alt) {
-            $result = $alt->load_relation_destinations($u, $type, %opts);
-            warn Dumper({load_relation_destinations_rs2=>$result});
+            $alt->load_relation_destinations($u, $type, %opts);
         }
     }
 
-
-
-
     my $interface = $class->relation_api($u);
-    $result = $interface->load_relation_destinations($u, $type, %opts);
-    warn Dumper({load_relation_destinations_mysql=>$result});
-    return $result;
+    return $interface->load_relation_destinations($u, $type, %opts);
 }
 
 sub create_relation_to {
-    my $class = shift;
-    my $u     = shift;
-
+    my $class  = shift;
+    my $u      = shift;
+    my $friend = shift;
+    my $type   = shift;
+    my %opts   = @_;
+    
     $u = LJ::want_user($u);
+    $friend = LJ::want_user($friend);
+    
+    return undef unless $type and $u and $friend;
+
     if ($class->_load_alt_api('update')){
         my $alt = $class->alt_api($u);
         if ($alt){
-            $alt->create_relation_to($u, @_);
+            $alt->create_relation_to($u, $friend, $type, %opts);
         }
     }
 
     my $interface = $class->relation_api($u);
-    return $interface->create_relation_to($u, @_);
+    return $interface->create_relation_to($u, $friend, $type, %opts);
 }
 
 

Modified: trunk/cgi-bin/LJ/User.pm
===================================================================
--- trunk/cgi-bin/LJ/User.pm	2012-02-21 13:52:51 UTC (rev 21229)
+++ trunk/cgi-bin/LJ/User.pm	2012-02-21 15:16:47 UTC (rev 21230)
@@ -8871,7 +8871,6 @@
         return 0 unless LJ::RateLimit->check($friender, [ $cond ]);
     }
 
-    my $dbh      = LJ::get_db_writer();
     my $sclient  = LJ::theschwartz();
 
     my $fgcol = LJ::color_todb($opts->{'fgcolor'}) || LJ::color_todb("#000000");
@@ -8892,32 +8891,23 @@
         $groupmask |= (1 << $grp) if $grp;
     }
 
-    foreach my $add_id (@add_ids) {
-        my $cnt = $dbh->do("REPLACE INTO friends (userid, friendid, fgcolor, bgcolor, groupmask) " .
-                           "VALUES ($userid, $add_id, $fgcol, $bgcol, $groupmask)");
-
-        if (!$dbh->err && $cnt == 1) {
-            LJ::run_hooks('befriended', $friender, LJ::load_userid($add_id));
-            LJ::User->increase_friendsof_counter($add_id);
-        }
-    }
-    
     # part of the criteria for whether to fire befriended event
     my $notify = !$LJ::DISABLED{esn} && !$opts->{nonotify}
                  && $friender->is_visible && $friender->is_person;
 
-    # delete friend-of memcache keys for anyone who was added
-    foreach my $fid (@add_ids) {
-        LJ::MemCache::delete([ $userid, "frgmask:$userid:$fid" ]);
-        LJ::memcache_kill($fid, 'friendofs');
-        LJ::memcache_kill($fid, 'friendofs2');
+    foreach my $add_id (@add_ids) {
+        LJ::RelationService->create_relation_to(
+            $friender, $add_id, 'F', 
+            groupmask => $groupmask, 
+            fgcolor   => $fgcol,
+            bgcolor   => $bgcol,
+        );
 
         if ($sclient) {
             my @jobs;
 
             # only fire event if the friender is a person and not banned and visible
-            my $friender = LJ::load_userid($userid);
-            my $friendee = LJ::load_userid($fid);
+            my $friendee = LJ::load_userid($add_id);
             if ($notify && !$friendee->is_banned($friender)) {
                 require LJ::Event::BefriendedDelayed;
                 LJ::Event::BefriendedDelayed->send($friendee, $friender);
@@ -8925,17 +8915,13 @@
 
             push @jobs, TheSchwartz::Job->new(
                                               funcname => "LJ::NewWorker::TheSchwartz::FriendChange",
-                                              arg      => [$userid, 'add', $fid],
+                                              arg      => [$userid, 'add', $add_id],
                                               ) unless $LJ::DISABLED{'friendchange-schwartz'};
 
             $sclient->insert_jobs(@jobs) if @jobs;
         }
     }
 
-    LJ::memcache_kill($userid, 'friends');
-    LJ::memcache_kill($userid, 'friends2');
-    LJ::mark_dirty($userid, "friends");
-
     # WARNING: always returns "true". Check result of executing "REPLACE INTO friends ..." statement above.
     return 1;
 }

Modified: trunk/cgi-bin/ljrelation.pl
===================================================================
--- trunk/cgi-bin/ljrelation.pl	2012-02-21 13:52:51 UTC (rev 21229)
+++ trunk/cgi-bin/ljrelation.pl	2012-02-21 15:16:47 UTC (rev 21230)
@@ -336,39 +336,9 @@
 # </LJFUNC>
 sub set_rel
 {
-    &nodb;
     my ($userid, $targetid, $type) = @_;
-    return undef unless $type and $userid and $targetid;
 
-    my $u = LJ::want_user($userid);
-    $userid = LJ::want_userid($userid);
-    $targetid = LJ::want_userid($targetid);
-
-    my $typeid = LJ::get_reluser_id($type)+0;
-    my $eff_type = $typeid || $type;
-
-    # working on reluser or reluser2?
-    my ($db, $table);
-    if ($typeid) {
-        # clustered reluser2 table
-        $db = LJ::get_cluster_master($u);
-        $table = "reluser2";
-    } else {
-        # non-clustered reluser global table
-        $db = LJ::get_db_writer();
-        $table = "reluser";
-    }
-    return undef unless $db;
-
-    # set in database
-    $db->do("REPLACE INTO $table (userid, targetid, type) VALUES (?, ?, ?)",
-            undef, $userid, $targetid, $eff_type);
-    return undef if $db->err;
-
-    # set in memcache
-    LJ::_set_rel_memcache($userid, $targetid, $eff_type, 1);
-
-    return 1;
+    return LJ::RelationService->create_relation_to($userid, $targetid, $type);
 }
 
 # <LJFUNC>

Tags: afedorov, livejournal, pl, pm, red_alf
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments