KYLIN-3090, complete UUID if entity hasn't.
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/aa57e4c3 Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/aa57e4c3 Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/aa57e4c3 Branch: refs/heads/sync Commit: aa57e4c391933b4bd17d17d145be6375fd9d996b Parents: b00661d Author: tttMelody <245915...@qq.com> Authored: Mon Jan 15 18:48:57 2018 +0800 Committer: Li Yang <liy...@apache.org> Committed: Fri Jan 26 18:55:26 2018 +0800 ---------------------------------------------------------------------- .../metadata/cachesync/CachedCrudAssist.java | 8 ++- .../cachesync/CachedCrudAssistTest.java | 63 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/aa57e4c3/core-metadata/src/main/java/org/apache/kylin/metadata/cachesync/CachedCrudAssist.java ---------------------------------------------------------------------- diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/cachesync/CachedCrudAssist.java b/core-metadata/src/main/java/org/apache/kylin/metadata/cachesync/CachedCrudAssist.java index c7244fe..b3c200e 100644 --- a/core-metadata/src/main/java/org/apache/kylin/metadata/cachesync/CachedCrudAssist.java +++ b/core-metadata/src/main/java/org/apache/kylin/metadata/cachesync/CachedCrudAssist.java @@ -173,7 +173,7 @@ abstract public class CachedCrudAssist<T extends RootPersistentEntity> { public T save(T entity) throws IOException { Preconditions.checkArgument(entity != null); - Preconditions.checkArgument(entity.getUuid() != null); + completeUuidIfNeeded(entity); Preconditions.checkArgument(entityType.isInstance(entity)); String resName = entity.resourceName(); @@ -199,6 +199,12 @@ abstract public class CachedCrudAssist<T extends RootPersistentEntity> { return reload(resName); } + private void completeUuidIfNeeded(T entity) { + if (entity.getUuid() == null) { + entity.updateRandomUuid(); + } + } + public void delete(T entity) throws IOException { delete(entity.resourceName()); } http://git-wip-us.apache.org/repos/asf/kylin/blob/aa57e4c3/core-metadata/src/test/java/org/apache/kylin/metadata/cachesync/CachedCrudAssistTest.java ---------------------------------------------------------------------- diff --git a/core-metadata/src/test/java/org/apache/kylin/metadata/cachesync/CachedCrudAssistTest.java b/core-metadata/src/test/java/org/apache/kylin/metadata/cachesync/CachedCrudAssistTest.java new file mode 100644 index 0000000..482ec09 --- /dev/null +++ b/core-metadata/src/test/java/org/apache/kylin/metadata/cachesync/CachedCrudAssistTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kylin.metadata.cachesync; + +import java.io.IOException; + +import org.apache.kylin.common.persistence.RootPersistentEntity; +import org.apache.kylin.common.util.LocalFileMetadataTestCase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CachedCrudAssistTest extends LocalFileMetadataTestCase { + @Before + public void setup() throws Exception { + staticCreateTestMetadata(); + } + + @Test + public void testSave() throws IOException { + CachedCrudAssist<TestEntity> crudAssist = new CachedCrudAssist<TestEntity>(getStore() // + , "/test" // + , TestEntity.class // + , new CaseInsensitiveStringCache<TestEntity>(getTestConfig(), "test")) { // + @Override + protected TestEntity initEntityAfterReload(TestEntity entity, String resourceName) { + return entity; + } + }; + + TestEntity entity = new TestEntity(); + Assert.assertNull(entity.getUuid()); + crudAssist.save(entity); + Assert.assertNotNull(entity.getUuid()); + } + + @After + public void after() throws Exception { + cleanAfterClass(); + } +} + +class TestEntity extends RootPersistentEntity { + public TestEntity() { + } +} \ No newline at end of file