private void put(String key, Value value, Scope scope) { if (!putAllowed) { String msg = String.format("Failed to put key '%s' from node '%s' in the WorkflowToken. Put operation is not " + "allowed from the Mapper and Reducer classes and from Spark executor.", key, nodeName); throw new UnsupportedOperationException(msg); } Preconditions.checkNotNull(key, "Null key cannot be added in the WorkflowToken."); Preconditions.checkNotNull(value, String.format("Null value provided for the key '%s'.", key)); Preconditions.checkNotNull(value.toString(), String.format("Null value provided for the key '%s'.", key)); Preconditions.checkState(nodeName != null, "nodeName cannot be null."); List<NodeValue> nodeValueList = tokenValueMap.get(scope).get(key); if (nodeValueList == null) { nodeValueList = Lists.newArrayList(); tokenValueMap.get(scope).put(key, nodeValueList); } NodeValue nodeValueToAddUpdate = new NodeValue(nodeName, value); // Check if the current node already added the key to the token. // In that case replace that entry with the new one for (int i = 0; i < nodeValueList.size(); i++) { NodeValue existingNodeValue = nodeValueList.get(i); if (existingNodeValue.getNodeName().equals(nodeName)) { addOrUpdate(key, nodeValueToAddUpdate, nodeValueList, i); return; } } addOrUpdate(key, nodeValueToAddUpdate, nodeValueList, -1); }
private void put(String key, Value value, Scope scope) { if (!putAllowed) { String msg = String.format("Failed to put key '%s' from node '%s' in the WorkflowToken. Put operation is not " + "allowed from the Mapper and Reducer classes and from Spark executor.", key, nodeName); throw new UnsupportedOperationException(msg); } Preconditions.checkNotNull(key, "Null key cannot be added in the WorkflowToken."); Preconditions.checkNotNull(value, String.format("Null value provided for the key '%s'.", key)); Preconditions.checkNotNull(value.toString(), String.format("Null value provided for the key '%s'.", key)); Preconditions.checkState(nodeName != null, "nodeName cannot be null."); List<NodeValue> nodeValueList = tokenValueMap.get(scope).get(key); if (nodeValueList == null) { nodeValueList = Lists.newArrayList(); tokenValueMap.get(scope).put(key, nodeValueList); } NodeValue nodeValueToAddUpdate = new NodeValue(nodeName, value); // Check if the current node already added the key to the token. // In that case replace that entry with the new one for (int i = 0; i < nodeValueList.size(); i++) { NodeValue existingNodeValue = nodeValueList.get(i); if (existingNodeValue.getNodeName().equals(nodeName)) { addOrUpdate(key, nodeValueToAddUpdate, nodeValueList, i); return; } } addOrUpdate(key, nodeValueToAddUpdate, nodeValueList, -1); }
/** * Merge the other WorkflowToken passed to the method as a parameter * with the WorkflowToken on which the method is invoked. * @param other the other WorkflowToken to be merged */ synchronized void mergeToken(WorkflowToken other) { for (Scope scope : Scope.values()) { Map<String, List<NodeValue>> thisTokenValueMapForScope = this.tokenValueMap.get(scope); for (Map.Entry<String, List<NodeValue>> otherTokenValueMapForScopeEntry : other.getAll(scope).entrySet()) { String otherKey = otherTokenValueMapForScopeEntry.getKey(); if (!thisTokenValueMapForScope.containsKey(otherKey)) { thisTokenValueMapForScope.put(otherKey, Lists.<NodeValue>newArrayList()); } // Iterate over the list of NodeValue corresponding to the current key. // Only add those NodeValue to the merged token which already do not exist. for (NodeValue otherNodeValue : otherTokenValueMapForScopeEntry.getValue()) { boolean otherNodeValueExist = false; for (NodeValue thisNodeValue : thisTokenValueMapForScope.get(otherKey)) { if (thisNodeValue.equals(otherNodeValue)) { otherNodeValueExist = true; break; } } if (!otherNodeValueExist) { addOrUpdate(otherKey, otherNodeValue, thisTokenValueMapForScope.get(otherKey), -1); } } } } }
/** * Merge the other WorkflowToken passed to the method as a parameter * with the WorkflowToken on which the method is invoked. * @param other the other WorkflowToken to be merged */ synchronized void mergeToken(WorkflowToken other) { for (Scope scope : Scope.values()) { Map<String, List<NodeValue>> thisTokenValueMapForScope = this.tokenValueMap.get(scope); for (Map.Entry<String, List<NodeValue>> otherTokenValueMapForScopeEntry : other.getAll(scope).entrySet()) { String otherKey = otherTokenValueMapForScopeEntry.getKey(); if (!thisTokenValueMapForScope.containsKey(otherKey)) { thisTokenValueMapForScope.put(otherKey, Lists.<NodeValue>newArrayList()); } // Iterate over the list of NodeValue corresponding to the current key. // Only add those NodeValue to the merged token which already do not exist. for (NodeValue otherNodeValue : otherTokenValueMapForScopeEntry.getValue()) { boolean otherNodeValueExist = false; for (NodeValue thisNodeValue : thisTokenValueMapForScope.get(otherKey)) { if (thisNodeValue.equals(otherNodeValue)) { otherNodeValueExist = true; break; } } if (!otherNodeValueExist) { addOrUpdate(otherKey, otherNodeValue, thisTokenValueMapForScope.get(otherKey), -1); } } } } }