Skip to content

Commit

Permalink
fixes ConfigCenter api, reduce method. (#3460)
Browse files Browse the repository at this point in the history
  • Loading branch information
董宗磊 authored and terrymanu committed Nov 6, 2019
1 parent ef5103a commit 34c0d0e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Config center.
*
* @author wangguangyuan
* @author dongzonglei
*/
public interface ConfigCenter extends TypeBasedSPI {

Expand All @@ -46,24 +47,6 @@ public interface ConfigCenter extends TypeBasedSPI {
*/
String get(String key);

/**
* Get data from config center directly.
*
* <p>Cannot use cache.</p>
*
* @param key key of data
* @return value of data
*/
String getDirectly(String key);

/**
* Judge data is existed or not.
*
* @param key key of data
* @return data is existed or not
*/
boolean isExisted(String key);

/**
* Get node's sub-nodes list.
*
Expand All @@ -80,22 +63,6 @@ public interface ConfigCenter extends TypeBasedSPI {
*/
void persist(String key, String value);

/**
* Update data.
*
* @param key key of data
* @param value value of data
*/
void update(String key, String value);

/**
* Persist ephemeral data.
*
* @param key key of data
* @param value value of data
*/
void persistEphemeral(String key, String value);

/**
* Watch key or path of the config server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -65,16 +64,6 @@ public String get(final String key) {
return apolloConfig.getProperty(key.replace("/", "."), "");
}

@Override
public String getDirectly(final String key) {
return get(key);
}

@Override
public boolean isExisted(final String key) {
return !Strings.isNullOrEmpty(get(key));
}

@Override
public List<String> getChildrenKeys(final String key) {
throw new UnsupportedOperationException();
Expand All @@ -85,16 +74,6 @@ public void persist(final String key, final String value) {
throw new UnsupportedOperationException();
}

@Override
public void update(final String key, final String value) {
throw new UnsupportedOperationException();
}

@Override
public void persistEphemeral(final String key, final String value) {
throw new UnsupportedOperationException();
}

@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
apolloConfig.addChangeListener(new ConfigChangeListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,4 @@ public static void init() {
public void assertGet() {
assertThat(configCenter.get("key1"), is("value1"));
}

@Test
public void assertGetDirectly() {
assertThat(configCenter.getDirectly("key2"), is("value2"));
}

@Test
public void assertIsExisted() {
assertThat(configCenter.isExisted("key1"), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* Config center for zookeeper with curator.
*
* @author wangguangyuan
* @author dongzonglei
*/
public final class CuratorZookeeperConfigCenter implements ConfigCenter {

Expand Down Expand Up @@ -133,8 +134,7 @@ private TreeCache findTreeCache(final String key) {
return null;
}

@Override
public String getDirectly(final String key) {
private String getDirectly(final String key) {
try {
return new String(client.getData().forPath(key), Charsets.UTF_8);
// CHECKSTYLE:OFF
Expand All @@ -145,18 +145,6 @@ public String getDirectly(final String key) {
}
}

@Override
public boolean isExisted(final String key) {
try {
return null != client.checkExists().forPath(key);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
CuratorZookeeperExceptionHandler.handleException(ex);
return false;
}
}

@Override
public List<String> getChildrenKeys(final String key) {
try {
Expand Down Expand Up @@ -191,25 +179,21 @@ public void persist(final String key, final String value) {
CuratorZookeeperExceptionHandler.handleException(ex);
}
}

@Override
public void update(final String key, final String value) {

private boolean isExisted(final String key) {
try {
client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit();
return null != client.checkExists().forPath(key);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
CuratorZookeeperExceptionHandler.handleException(ex);
return false;
}
}

@Override
public void persistEphemeral(final String key, final String value) {
private void update(final String key, final String value) {
try {
if (isExisted(key)) {
client.delete().deletingChildrenIfNeeded().forPath(key);
}
client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charsets.UTF_8));
client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ public void assertPersistEphemeral() {
assertThat(curatorZookeeperConfigCenter.get("/test/ephemeral"), is("value3"));
}

@Test
public void assertGetDirectly() {
curatorZookeeperConfigCenter.persist("/test", "value4");
assertThat(curatorZookeeperConfigCenter.getDirectly("/test"), is("value4"));
}

@Test
public void assertIsExisted() {
curatorZookeeperConfigCenter.persist("/test/existed", "value5");
assertThat(curatorZookeeperConfigCenter.isExisted("/test/existed"), is(true));
}

@Test
public void assertGetChildrenKeys() {
curatorZookeeperConfigCenter.persist("/test/children/1", "value11");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ public String get(final String key) {
return "";
}

@Override
public String getDirectly(final String key) {
return "";
}

@Override
public boolean isExisted(final String key) {
return true;
}

@Override
public List<String> getChildrenKeys(final String key) {
return Collections.emptyList();
Expand All @@ -61,14 +51,6 @@ public List<String> getChildrenKeys(final String key) {
public void persist(final String key, final String value) {
}

@Override
public void update(final String key, final String value) {
}

@Override
public void persistEphemeral(final String key, final String value) {
}

@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
}
Expand Down

0 comments on commit 34c0d0e

Please sign in to comment.