szetszwo commented on a change in pull request #364:
URL: https://github.com/apache/incubator-ratis/pull/364#discussion_r546485981



##########
File path: 
ratis-client/src/main/java/org/apache/ratis/client/api/DataStreamApi.java
##########
@@ -44,4 +48,7 @@ default DataStreamOutput stream() {
 
   /** Create a stream by providing a customized header message. */
   DataStreamOutput stream(ByteBuffer headerMessage);
+
+  /** Create a stream by providing a customized header message and route 
table. */
+  DataStreamOutput stream(ByteBuffer headerMessage, Map<RaftPeerId, 
List<RaftPeerId>> routingTable);

Review comment:
       The build() method needs to be updated.  We should also change the 
Builder to use a reference so that it cannot be built more than once.
   ```
   public interface RoutingTable {
     Set<RaftPeerId> getSuccessors(RaftPeerId peerId);
   
     RoutingTableProto toProto();
   
     class Builder {
       private final AtomicReference<Map<RaftPeerId, Set<RaftPeerId>>> ref = 
new AtomicReference<>(new HashMap<>());
   
       private Set<RaftPeerId> computeIfAbsent(RaftPeerId peerId) {
         return Optional.ofNullable(ref.get())
             .map(map -> map.computeIfAbsent(peerId, key -> new HashSet<>()))
             .orElseThrow(() -> new IllegalStateException("Already built"));
       }
   
       public Builder addSuccessor(RaftPeerId peerId, RaftPeerId successor) {
         computeIfAbsent(peerId).add(successor);
         return this;
       }
   
       public Builder addSuccessors(RaftPeerId peerId, Collection<RaftPeerId> 
successors) {
         computeIfAbsent(peerId).addAll(successors);
         return this;
       }
   
       public Builder addSuccessors(RaftPeerId peerId, RaftPeerId... 
successors) {
         return addSuccessors(peerId, Arrays.asList(successors));
       }
   
       public RoutingTable build() {
         return Optional.ofNullable(ref.getAndSet(null))
             .map(RoutingTable::newRoutingTable)
             .orElseThrow(() -> new IllegalStateException("Already built"));
       }
     }
   
     static RoutingTable newRoutingTable(Map<RaftPeerId, Set<RaftPeerId>> map){
       return new RoutingTable() {
         @Override
         public Set<RaftPeerId> getSuccessors(RaftPeerId peerId) {
           return 
Optional.ofNullable(map.get(peerId)).orElseGet(Collections::emptySet);
         }
   
         @Override
         public RoutingTableProto toProto() {
           return 
RoutingTableProto.newBuilder().addAllRoutingTable(ProtoUtils.toRouteProtos(map)).build();
         }
       };
     }
   }
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to