Uploaded image for project: 'ZooKeeper'
  1. ZooKeeper
  2. ZOOKEEPER-4000

use the computeIfAbsent to simplify the Leader#processSync method

    XMLWordPrintableJSON

Details

    Description

      Look at the Leader#processSync method:

      public synchronized void processSync(LearnerSyncRequest r) {
          if (outstandingProposals.isEmpty()) {
              sendSync(r);
          } else {
              List<LearnerSyncRequest> l = pendingSyncs.get(lastProposed);
              if (l == null) {
                  l = new ArrayList<LearnerSyncRequest>();
              }
              l.add(r);
              pendingSyncs.put(lastProposed, l);
          }
      }
      

      we can use theĀ computeIfAbsent to make the code more clean and elegant. For example(just one line code):

      pendingSyncs.computeIfAbsent(lastProposed, k -> new ArrayList<>()).add(r);
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              maoling Ling Mao
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 50m
                  50m