Skip to content

Commit

Permalink
Fewer redundant queries in updatecontactdb.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 26, 2023
1 parent 612abeb commit 25902d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
47 changes: 23 additions & 24 deletions batch/updatecontactdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private function try_cdb() {
foreach ($this->conf->submission_round_list() as $sr) {
$max_sub = max($max_sub, $sr->register, $sr->update, $sr->submit);
}
if ($max_sub && $max_sub !== $this->confrow->submission_deadline_at) {
if ($max_sub && $max_sub != $this->confrow->submission_deadline_at) {
$qf[] = "submission_deadline_at=?";
$qv[] = $max_sub;
}
Expand Down Expand Up @@ -102,15 +102,12 @@ private function cdb() {
/** @param \mysqli $cdb */
private function run_users($cdb) {
// read current cdb roles
$result = Dbl::ql($cdb, "select Roles.*, email, password
from Roles
join ContactInfo using (contactDbId)
where confid=?", $this->cdb_confid);
$cdb_users = [];
while ($result && ($user = $result->fetch_object())) {
$cdb_users[$user->email] = $user;
$result = Dbl::ql($cdb, "select contactDbId from Roles where confid=?", $this->cdb_confid);
$ecdbids = [];
while (($row = $result->fetch_row())) {
$ecdbids[(int) $row[0]] = true;
}
Dbl::free($result);
$result->close();

// read current db roles
$result = Dbl::ql($this->conf->dblink, "select ContactInfo.contactId, email, firstName, lastName, unaccentedName, disabled,
Expand All @@ -120,37 +117,39 @@ private function run_users($cdb) {
" . (Contact::ROLE_DBMASK | Contact::ROLE_AUTHOR | Contact::ROLE_REVIEWER) . " role_mask,
lastLogin
from ContactInfo");
$us = ContactSet::make_result($result, $this->conf);

$cdbids = [];
$qv = [];
while (($u = Contact::fetch($result, $this->conf))) {
foreach ($us as $u) {
$cdb_roles = $u->cdb_roles();
if ($cdb_roles == 0 || $u->is_anonymous_user()) {
if ($cdb_roles === 0 || $u->is_anonymous_user()) {
continue;
}
$cdbu = $cdb_users[$u->email] ?? null;
$cdbid = $cdbu ? (int) $cdbu->contactDbId : 0;
$cdbu = $u->cdb_user();
if ($cdbu) {
unset($ecdbids[$cdbu->contactDbId]);
}
if ($cdbu
&& (int) $cdbu->roles === $cdb_roles
&& $cdbu->activity_at) {
&& (($cdbu->roles ^ $cdb_roles) & Contact::ROLE_CDBMASK) === 0
&& ($cdbu->activity_at || ($u->activity_at ?? 0) === 0)) {
/* skip */;
} else if ($cdbu && $cdbu->password !== null) {
$qv[] = [$cdbid, $this->cdb_confid, $cdb_roles, $u->activity_at ?? 0];
} else if ($cdbu) {
$qv[] = [$cdbu->contactDbId, $this->cdb_confid, $cdb_roles, $u->activity_at ?? 0];
} else {
$cdbid = $u->update_cdb();
}
if ($cdbid) {
$cdbids[] = $cdbid;
$u->update_cdb();
}
}
Dbl::free($result);

// perform role updates
if (!empty($qv)) {
Dbl::ql($cdb, "insert into Roles (contactDbId,confid,roles,activity_at) values ?v ?U on duplicate key update roles=?U(roles), activity_at=?U(activity_at)", $qv);
}

// remove old roles
Dbl::ql($cdb, "delete from Roles where confid=? and contactDbId?A", $this->cdb_confid, $cdbids);
if (!empty($ecdbids)) {
Dbl::ql($cdb, "delete from Roles where confid=? and contactDbId?a", $this->cdb_confid, array_keys($ecdbids));
}
}

/** @param \mysqli $cdb */
Expand Down Expand Up @@ -248,7 +247,7 @@ private function run_papers($cdb) {
if (!empty($epapers)) {
Dbl::ql($cdb, "delete from ConferencePapers where confid=? and paperId?a", $this->cdb_confid, array_keys($epapers));
}
if ($this->confrow->last_submission_at != $max_submitted
if ($this->confrow->last_submission_at < $max_submitted
|| $this->confrow->submission_count != $nsubmitted) {
Dbl::ql($cdb, "update Conferences set submission_count=?, last_submission_at=greatest(coalesce(last_submission_at,0), ?) where confid=?", $nsubmitted, $max_submitted, $this->cdb_confid);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ static function convert_utf8($dblink, $qstr = null) {
$dblink = self::$default_dblink;
}
$utf8 = $dblink->server_version >= 50503 ? "utf8mb4" : "utf8";
return "convert($qstr using $utf8)";
return "convert({$qstr} using {$utf8})";
}

/** @param string $str
Expand Down
2 changes: 1 addition & 1 deletion src/conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,7 @@ private function _fresh_cdb_user_list($ids, $emails) {
if (!$cdb || (empty($ids) && empty($emails))) {
return [];
}
$q = "select ContactInfo.*, roles, activity_at";
$q = "select ContactInfo.*, roles, " . Contact::ROLE_CDBMASK . " role_mask, activity_at";
if (($confid = $this->opt("contactdbConfid") ?? 0) > 0) {
$q .= ", ? cdb_confid from ContactInfo left join Roles on (Roles.contactDbId=ContactInfo.contactDbId and Roles.confid=?)";
$qv = [$confid, $confid];
Expand Down

0 comments on commit 25902d9

Please sign in to comment.