@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Completely remove "LiskDAOSet" and "loadRelatives/loadOneRelative"

Summary: Fixes T13218. We have no more callers to any of this and can get rid of it forever.

Test Plan: Grepped for all four API methods, `LiskDAOSet`, and `inSet`.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13218

Differential Revision: https://secure.phabricator.com/D19879

-273
-2
src/__phutil_library_map__.php
··· 1633 1633 'LegalpadTransactionView' => 'applications/legalpad/view/LegalpadTransactionView.php', 1634 1634 'LiskChunkTestCase' => 'infrastructure/storage/lisk/__tests__/LiskChunkTestCase.php', 1635 1635 'LiskDAO' => 'infrastructure/storage/lisk/LiskDAO.php', 1636 - 'LiskDAOSet' => 'infrastructure/storage/lisk/LiskDAOSet.php', 1637 1636 'LiskDAOTestCase' => 'infrastructure/storage/lisk/__tests__/LiskDAOTestCase.php', 1638 1637 'LiskEphemeralObjectException' => 'infrastructure/storage/lisk/LiskEphemeralObjectException.php', 1639 1638 'LiskFixtureTestCase' => 'infrastructure/storage/lisk/__tests__/LiskFixtureTestCase.php', ··· 7200 7199 'Phobject', 7201 7200 'AphrontDatabaseTableRefInterface', 7202 7201 ), 7203 - 'LiskDAOSet' => 'Phobject', 7204 7202 'LiskDAOTestCase' => 'PhabricatorTestCase', 7205 7203 'LiskEphemeralObjectException' => 'Exception', 7206 7204 'LiskFixtureTestCase' => 'PhabricatorTestCase',
-170
src/infrastructure/storage/lisk/LiskDAO.php
··· 193 193 194 194 private static $connections = array(); 195 195 196 - private $inSet = null; 197 - 198 196 protected $id; 199 197 protected $phid; 200 198 protected $dateCreated; ··· 659 657 } else { 660 658 $result[] = $obj->loadFromArray($row); 661 659 } 662 - if ($this->inSet) { 663 - $this->inSet->addToSet($obj); 664 - } 665 660 } 666 661 667 662 return $result; 668 - } 669 - 670 - /** 671 - * This method helps to prevent the 1+N queries problem. It happens when you 672 - * execute a query for each row in a result set. Like in this code: 673 - * 674 - * COUNTEREXAMPLE, name=Easy to write but expensive to execute 675 - * $diffs = id(new DifferentialDiff())->loadAllWhere( 676 - * 'revisionID = %d', 677 - * $revision->getID()); 678 - * foreach ($diffs as $diff) { 679 - * $changesets = id(new DifferentialChangeset())->loadAllWhere( 680 - * 'diffID = %d', 681 - * $diff->getID()); 682 - * // Do something with $changesets. 683 - * } 684 - * 685 - * One can solve this problem by reading all the dependent objects at once and 686 - * assigning them later: 687 - * 688 - * COUNTEREXAMPLE, name=Cheaper to execute but harder to write and maintain 689 - * $diffs = id(new DifferentialDiff())->loadAllWhere( 690 - * 'revisionID = %d', 691 - * $revision->getID()); 692 - * $all_changesets = id(new DifferentialChangeset())->loadAllWhere( 693 - * 'diffID IN (%Ld)', 694 - * mpull($diffs, 'getID')); 695 - * $all_changesets = mgroup($all_changesets, 'getDiffID'); 696 - * foreach ($diffs as $diff) { 697 - * $changesets = idx($all_changesets, $diff->getID(), array()); 698 - * // Do something with $changesets. 699 - * } 700 - * 701 - * The method @{method:loadRelatives} abstracts this approach which allows 702 - * writing a code which is simple and efficient at the same time: 703 - * 704 - * name=Easy to write and cheap to execute 705 - * $diffs = $revision->loadRelatives(new DifferentialDiff(), 'revisionID'); 706 - * foreach ($diffs as $diff) { 707 - * $changesets = $diff->loadRelatives( 708 - * new DifferentialChangeset(), 709 - * 'diffID'); 710 - * // Do something with $changesets. 711 - * } 712 - * 713 - * This will load dependent objects for all diffs in the first call of 714 - * @{method:loadRelatives} and use this result for all following calls. 715 - * 716 - * The method supports working with set of sets, like in this code: 717 - * 718 - * $diffs = $revision->loadRelatives(new DifferentialDiff(), 'revisionID'); 719 - * foreach ($diffs as $diff) { 720 - * $changesets = $diff->loadRelatives( 721 - * new DifferentialChangeset(), 722 - * 'diffID'); 723 - * foreach ($changesets as $changeset) { 724 - * $hunks = $changeset->loadRelatives( 725 - * new DifferentialHunk(), 726 - * 'changesetID'); 727 - * // Do something with hunks. 728 - * } 729 - * } 730 - * 731 - * This code will execute just three queries - one to load all diffs, one to 732 - * load all their related changesets and one to load all their related hunks. 733 - * You can try to write an equivalent code without using this method as 734 - * a homework. 735 - * 736 - * The method also supports retrieving referenced objects, for example authors 737 - * of all diffs (using shortcut @{method:loadOneRelative}): 738 - * 739 - * foreach ($diffs as $diff) { 740 - * $author = $diff->loadOneRelative( 741 - * new PhabricatorUser(), 742 - * 'phid', 743 - * 'getAuthorPHID'); 744 - * // Do something with author. 745 - * } 746 - * 747 - * It is also possible to specify additional conditions for the `WHERE` 748 - * clause. Similarly to @{method:loadAllWhere}, you can specify everything 749 - * after `WHERE` (except `LIMIT`). Contrary to @{method:loadAllWhere}, it is 750 - * allowed to pass only a constant string (`%` doesn't have a special 751 - * meaning). This is intentional to avoid mistakes with using data from one 752 - * row in retrieving other rows. Example of a correct usage: 753 - * 754 - * $status = $author->loadOneRelative( 755 - * new PhabricatorCalendarEvent(), 756 - * 'userPHID', 757 - * 'getPHID', 758 - * '(UNIX_TIMESTAMP() BETWEEN dateFrom AND dateTo)'); 759 - * 760 - * @param LiskDAO Type of objects to load. 761 - * @param string Name of the column in target table. 762 - * @param string Method name in this table. 763 - * @param string Additional constraints on returned rows. It supports no 764 - * placeholders and requires putting the WHERE part into 765 - * parentheses. It's not possible to use LIMIT. 766 - * @return list Objects of type $object. 767 - * 768 - * @task load 769 - */ 770 - public function loadRelatives( 771 - LiskDAO $object, 772 - $foreign_column, 773 - $key_method = 'getID', 774 - $where = '') { 775 - 776 - if (!$this->inSet) { 777 - id(new LiskDAOSet())->addToSet($this); 778 - } 779 - $relatives = $this->inSet->loadRelatives( 780 - $object, 781 - $foreign_column, 782 - $key_method, 783 - $where); 784 - return idx($relatives, $this->$key_method(), array()); 785 - } 786 - 787 - /** 788 - * Load referenced row. See @{method:loadRelatives} for details. 789 - * 790 - * @param LiskDAO Type of objects to load. 791 - * @param string Name of the column in target table. 792 - * @param string Method name in this table. 793 - * @param string Additional constraints on returned rows. It supports no 794 - * placeholders and requires putting the WHERE part into 795 - * parentheses. It's not possible to use LIMIT. 796 - * @return LiskDAO Object of type $object or null if there's no such object. 797 - * 798 - * @task load 799 - */ 800 - final public function loadOneRelative( 801 - LiskDAO $object, 802 - $foreign_column, 803 - $key_method = 'getID', 804 - $where = '') { 805 - 806 - $relatives = $this->loadRelatives( 807 - $object, 808 - $foreign_column, 809 - $key_method, 810 - $where); 811 - 812 - if (!$relatives) { 813 - return null; 814 - } 815 - 816 - if (count($relatives) > 1) { 817 - throw new AphrontCountQueryException( 818 - pht( 819 - 'More than one result from %s!', 820 - __FUNCTION__.'()')); 821 - } 822 - 823 - return reset($relatives); 824 - } 825 - 826 - final public function putInSet(LiskDAOSet $set) { 827 - $this->inSet = $set; 828 - return $this; 829 - } 830 - 831 - final protected function getInSet() { 832 - return $this->inSet; 833 663 } 834 664 835 665
-101
src/infrastructure/storage/lisk/LiskDAOSet.php
··· 1 - <?php 2 - 3 - /** 4 - * You usually don't need to use this class directly as it is controlled by 5 - * @{class:LiskDAO}. You can create it if you want to work with objects of same 6 - * type from different sources as with one set. Let's say you want to get 7 - * e-mails of all users involved in a revision: 8 - * 9 - * $users = new LiskDAOSet(); 10 - * $users->addToSet($author); 11 - * foreach ($reviewers as $reviewer) { 12 - * $users->addToSet($reviewer); 13 - * } 14 - * foreach ($ccs as $cc) { 15 - * $users->addToSet($cc); 16 - * } 17 - * // Preload e-mails of all involved users and return e-mails of author. 18 - * $author_emails = $author->loadRelatives( 19 - * new PhabricatorUserEmail(), 20 - * 'userPHID', 21 - * 'getPHID'); 22 - */ 23 - final class LiskDAOSet extends Phobject { 24 - private $daos = array(); 25 - private $relatives = array(); 26 - private $subsets = array(); 27 - 28 - public function addToSet(LiskDAO $dao) { 29 - if ($this->relatives) { 30 - throw new Exception( 31 - pht( 32 - "Don't call %s after loading data!", 33 - __FUNCTION__.'()')); 34 - } 35 - $this->daos[] = $dao; 36 - $dao->putInSet($this); 37 - return $this; 38 - } 39 - 40 - /** 41 - * The main purpose of this method is to break cyclic dependency. 42 - * It removes all objects from this set and all subsets created by it. 43 - */ 44 - public function clearSet() { 45 - $this->daos = array(); 46 - $this->relatives = array(); 47 - foreach ($this->subsets as $set) { 48 - $set->clearSet(); 49 - } 50 - $this->subsets = array(); 51 - return $this; 52 - } 53 - 54 - 55 - /** 56 - * See @{method:LiskDAO::loadRelatives}. 57 - */ 58 - public function loadRelatives( 59 - LiskDAO $object, 60 - $foreign_column, 61 - $key_method = 'getID', 62 - $where = '') { 63 - 64 - $relatives = &$this->relatives[ 65 - get_class($object)."-{$foreign_column}-{$key_method}-{$where}"]; 66 - 67 - if ($relatives === null) { 68 - $ids = array(); 69 - foreach ($this->daos as $dao) { 70 - $id = $dao->$key_method(); 71 - if ($id !== null) { 72 - $ids[$id] = $id; 73 - } 74 - } 75 - if (!$ids) { 76 - $relatives = array(); 77 - } else { 78 - $set = new LiskDAOSet(); 79 - $this->subsets[] = $set; 80 - 81 - $conn = $object->establishConnection('r'); 82 - 83 - if (strlen($where)) { 84 - $where_clause = qsprintf($conn, 'AND %Q', $where); 85 - } else { 86 - $where_clause = qsprintf($conn, ''); 87 - } 88 - 89 - $relatives = $object->putInSet($set)->loadAllWhere( 90 - '%C IN (%Ls) %Q', 91 - $foreign_column, 92 - $ids, 93 - $where_clause); 94 - $relatives = mgroup($relatives, 'get'.$foreign_column); 95 - } 96 - } 97 - 98 - return $relatives; 99 - } 100 - 101 - }