You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/11/04 07:48:49 UTC
[dubbo] branch 3.0 updated: lazy init attributes in URL (#9201)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new ba173fd lazy init attributes in URL (#9201)
ba173fd is described below
commit ba173fdc5fe7ca6b8bd17809776e76d5b74324a2
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Thu Nov 4 15:48:37 2021 +0800
lazy init attributes in URL (#9201)
---
.../src/main/java/org/apache/dubbo/common/URL.java | 29 ++++++++++++++--------
.../common/url/component/ServiceConfigURL.java | 14 ++++++++---
2 files changed, 29 insertions(+), 14 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
index dea541c..60734d0 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
@@ -129,12 +129,12 @@ class URL implements Serializable {
private transient String serviceKey;
private transient String protocolServiceKey;
- protected final Map<String, Object> attributes;
+ protected volatile Map<String, Object> attributes;
protected URL() {
this.urlAddress = null;
this.urlParam = null;
- this.attributes = new HashMap<>();
+ this.attributes = null;
}
public URL(URLAddress urlAddress, URLParam urlParam) {
@@ -144,7 +144,7 @@ class URL implements Serializable {
public URL(URLAddress urlAddress, URLParam urlParam, Map<String, Object> attributes) {
this.urlAddress = urlAddress;
this.urlParam = urlParam;
- this.attributes = (attributes != null ? attributes : new HashMap<>());
+ this.attributes = (attributes != null ? attributes.isEmpty() ? null : attributes : null);
}
public URL(String protocol, String host, int port) {
@@ -193,7 +193,7 @@ class URL implements Serializable {
this.urlAddress = new PathURLAddress(protocol, username, password, path, host, port);
this.urlParam = URLParam.parse(parameters);
- this.attributes = new HashMap<>();
+ this.attributes = null;
}
protected URL(String protocol,
@@ -211,7 +211,7 @@ class URL implements Serializable {
this.urlAddress = new PathURLAddress(protocol, username, password, path, host, port);
this.urlParam = URLParam.parse(parameters);
- this.attributes = new HashMap<>();
+ this.attributes = null;
}
public static URL cacheableValueOf(String url) {
@@ -1609,35 +1609,42 @@ class URL implements Serializable {
/* Service Config URL, START*/
public Map<String, Object> getAttributes() {
- return attributes;
+ return attributes == null ? Collections.emptyMap() : attributes;
}
public URL addAttributes(Map<String, Object> attributes) {
- attributes.putAll(attributes);
+ if (attributes != null) {
+ attributes.putAll(attributes);
+ }
return this;
}
public Object getAttribute(String key) {
- return attributes.get(key);
+ return attributes == null ? null : attributes.get(key);
}
public Object getAttribute(String key, Object defaultValue) {
- Object val = attributes.get(key);
+ Object val = attributes == null ? null : attributes.get(key);
return val != null ? val : defaultValue;
}
public URL putAttribute(String key, Object obj) {
+ if (attributes == null) {
+ this.attributes = new HashMap<>();
+ }
attributes.put(key, obj);
return this;
}
public URL removeAttribute(String key) {
- attributes.remove(key);
+ if (attributes != null) {
+ attributes.remove(key);
+ }
return this;
}
public boolean hasAttribute(String key) {
- return attributes.containsKey(key);
+ return attributes != null && attributes.containsKey(key);
}
/* Service Config URL, END*/
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/url/component/ServiceConfigURL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/url/component/ServiceConfigURL.java
index 5fcd263..fc67804 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/url/component/ServiceConfigURL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/url/component/ServiceConfigURL.java
@@ -99,21 +99,29 @@ public class ServiceConfigURL extends URL {
@Override
public URL addAttributes(Map<String, Object> attributes) {
Map<String, Object> newAttributes = new HashMap<>();
- newAttributes.putAll(this.attributes);
+ if (this.attributes != null) {
+ newAttributes.putAll(this.attributes);
+ }
newAttributes.putAll(attributes);
return new ServiceConfigURL(getUrlAddress(), getUrlParam(), newAttributes);
}
@Override
public ServiceConfigURL putAttribute(String key, Object obj) {
- Map<String, Object> newAttributes = new HashMap<>(attributes);
+ Map<String, Object> newAttributes = new HashMap<>();
+ if (attributes != null) {
+ newAttributes.putAll(attributes);
+ }
newAttributes.put(key, obj);
return new ServiceConfigURL(getUrlAddress(), getUrlParam(), newAttributes);
}
@Override
public URL removeAttribute(String key) {
- Map<String, Object> newAttributes = new HashMap<>(attributes);
+ Map<String, Object> newAttributes = new HashMap<>();
+ if (attributes != null) {
+ newAttributes.putAll(attributes);
+ }
newAttributes.remove(key);
return new ServiceConfigURL(getUrlAddress(), getUrlParam(), newAttributes);
}