From a75e4c5ff36ac58b8ab3ecf42b68920a063bb7e1 Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Sat, 12 Mar 2022 21:00:46 +0800 Subject: [PATCH 1/7] Update CachedGraphTransaction.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. fix the bug when vertex updated, but the edge cache associated with that vertex was not updated. Here we simply clear all edge cache when  any vertex changes. --- .../hugegraph/backend/cache/CachedGraphTransaction.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java index 7462ead734..3ea9b43f5e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java @@ -394,8 +394,11 @@ protected final void commitMutation2Backend(BackendMutation... mutations) { } } - // Update edge cache if any edges change - if (edgesInTxSize > 0 && this.enableCacheEdge()) { + // Update edge cache if any vertex or edge change + // for vertex change, the edge associated with that vertex should also be updated + // here we just clear all the edge cache , before we use a more precise strategy + boolean invalidEdgesCache = (this.edgesInTxSize() + updates.size() + deletions.size()) > 0; + if (invalidEdgesCache) { // TODO: Use a more precise strategy to update the edge cache this.edgesCache.clear(); this.notifyChanges(Cache.ACTION_CLEARED, HugeType.EDGE, null); From 376700c8d93b1e9c3a75bf9bdca898cc4b759448 Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Sat, 12 Mar 2022 21:02:17 +0800 Subject: [PATCH 2/7] Update CachedGraphTransactionTest.java 1.add two test case for the fixing --- .../cache/CachedGraphTransactionTest.java | 104 +++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java index 6583a0730c..a6e65ce614 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java @@ -19,6 +19,8 @@ package com.baidu.hugegraph.unit.cache; +import com.baidu.hugegraph.structure.HugeEdge; +import com.baidu.hugegraph.structure.HugeVertexProperty; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -65,14 +67,27 @@ private CachedGraphTransaction cache() { private HugeVertex newVertex(Id id) { HugeGraph graph = this.cache().graph(); + graph.schema().propertyKey("name").asText() + .checkExist(false).create(); graph.schema().vertexLabel("person") - .idStrategy(IdStrategy.CUSTOMIZE_NUMBER) - .checkExist(false) - .create(); + .idStrategy(IdStrategy.CUSTOMIZE_NUMBER) + .properties("name").nullableKeys("name") + .checkExist(false) + .create(); VertexLabel vl = graph.vertexLabel("person"); return new HugeVertex(graph, id, vl); } + private HugeEdge newEdge(HugeVertex out, HugeVertex in){ + HugeGraph graph = this.cache().graph(); + graph.schema().edgeLabel("person_know_person") + .sourceLabel("person") + .targetLabel("person") + .checkExist(false) + .create(); + return out.addEdge("person_know_person",in); + } + @Test public void testEventClear() throws Exception { CachedGraphTransaction cache = this.cache(); @@ -125,4 +140,87 @@ public void testEventInvalid() throws Exception { Assert.assertEquals(2L, Whitebox.invoke(cache, "verticesCache", "size")); } + + + @Test + public void testClearEdgeCacheWhenDeleteVertex(){ + CachedGraphTransaction cache = this.cache(); + HugeVertex v1 = this.newVertex(IdGenerator.of(1)); + HugeVertex v2 = this.newVertex(IdGenerator.of(2)); + HugeVertex v3 = this.newVertex(IdGenerator.of(3)); + + cache.addVertex(v1); + cache.addVertex(v2); + cache.commit(); + HugeEdge edge = this.newEdge(v1,v2); + cache.addEdge(edge); + cache.commit(); + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); + + Assert.assertEquals(2L, + Whitebox.invoke(cache, "edgesCache", "size")); + cache.removeVertex(v3); + cache.commit(); + Assert.assertEquals(0L, + Whitebox.invoke(cache, "edgesCache", "size")); + + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); + Assert.assertEquals(2L, + Whitebox.invoke(cache, "edgesCache", "size")); + + cache.removeVertex(v1); + cache.commit(); + + Assert.assertEquals(0L, + Whitebox.invoke(cache, "edgesCache", "size")); + Assert.assertFalse(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); + + } + + @Test + public void testClearEdgeCacheWhenUpdateVertex(){ + CachedGraphTransaction cache = this.cache(); + HugeVertex v1 = this.newVertex(IdGenerator.of(1)); + HugeVertex v2 = this.newVertex(IdGenerator.of(2)); + HugeVertex v3 = this.newVertex(IdGenerator.of(3)); + + cache.addVertex(v1); + cache.addVertex(v2); + cache.commit(); + HugeEdge edge = this.newEdge(v1,v2); + cache.addEdge(edge); + cache.commit(); + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); + + Assert.assertEquals(2L, + Whitebox.invoke(cache, "edgesCache", "size")); + + ///update property of v3 + cache.addVertexProperty(new HugeVertexProperty<>(v3, + cache.graph().schema().getPropertyKey("name"),"test-name")); + cache.commit(); + Assert.assertEquals(0L, + Whitebox.invoke(cache, "edgesCache", "size")); + + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); + Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); + Assert.assertEquals(2L, + Whitebox.invoke(cache, "edgesCache", "size")); + + cache.addVertexProperty(new HugeVertexProperty<>(v1, + cache.graph().schema().getPropertyKey("name"),"test-name")); + cache.commit(); + + Assert.assertEquals(0L, + Whitebox.invoke(cache, "edgesCache", "size")); + String name = cache.queryEdgesByVertex(IdGenerator.of(1)).next().outVertex() + .value("name"); + Assert.assertEquals("test-name",name); + + + } } + From 3ef5d3816bbb41d1eb244054a5116504444f4cf1 Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Sat, 12 Mar 2022 21:11:26 +0800 Subject: [PATCH 3/7] Update CachedGraphTransaction.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. fix the bug when vertex updated, but the edge cache associated with that vertex was not updated. Here we simply clear all edge cache when  any vertex changes. --- .../baidu/hugegraph/backend/cache/CachedGraphTransaction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java index 3ea9b43f5e..85f94a7e9e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java @@ -398,7 +398,7 @@ protected final void commitMutation2Backend(BackendMutation... mutations) { // for vertex change, the edge associated with that vertex should also be updated // here we just clear all the edge cache , before we use a more precise strategy boolean invalidEdgesCache = (this.edgesInTxSize() + updates.size() + deletions.size()) > 0; - if (invalidEdgesCache) { + if (invalidEdgesCache && this.enableCacheEdge()) { // TODO: Use a more precise strategy to update the edge cache this.edgesCache.clear(); this.notifyChanges(Cache.ACTION_CLEARED, HugeType.EDGE, null); From 9f21a98904dbf037e15b88dc4cb9bfcc6af30116 Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Mon, 14 Mar 2022 12:30:02 +0800 Subject: [PATCH 4/7] Update CachedGraphTransaction.java format code using hugegraph code style --- .../backend/cache/CachedGraphTransaction.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java index 85f94a7e9e..a63e73cd67 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java @@ -394,10 +394,12 @@ protected final void commitMutation2Backend(BackendMutation... mutations) { } } - // Update edge cache if any vertex or edge change - // for vertex change, the edge associated with that vertex should also be updated - // here we just clear all the edge cache , before we use a more precise strategy - boolean invalidEdgesCache = (this.edgesInTxSize() + updates.size() + deletions.size()) > 0; + /* + * Update edge cache if any vertex or edge changed + * For vertex change, the edges linked with should also be updated + * Before we use a more precise strategy,now we just clear all the edge cache + */ + boolean invalidEdgesCache = (edgesInTxSize + updates.size() + deletions.size()) > 0; if (invalidEdgesCache && this.enableCacheEdge()) { // TODO: Use a more precise strategy to update the edge cache this.edgesCache.clear(); From 652670ff33a9dd756c7f06f8aabeffea346f98fa Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Mon, 14 Mar 2022 12:31:46 +0800 Subject: [PATCH 5/7] Update CachedGraphTransactionTest.java format code using hugegraph code style --- .../cache/CachedGraphTransactionTest.java | 65 +++++++++---------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java index a6e65ce614..19bb17c8cb 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java @@ -19,8 +19,6 @@ package com.baidu.hugegraph.unit.cache; -import com.baidu.hugegraph.structure.HugeEdge; -import com.baidu.hugegraph.structure.HugeVertexProperty; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,7 +30,9 @@ import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.backend.id.IdGenerator; import com.baidu.hugegraph.schema.VertexLabel; +import com.baidu.hugegraph.structure.HugeEdge; import com.baidu.hugegraph.structure.HugeVertex; +import com.baidu.hugegraph.structure.HugeVertexProperty; import com.baidu.hugegraph.testutil.Assert; import com.baidu.hugegraph.testutil.Whitebox; import com.baidu.hugegraph.type.HugeType; @@ -68,24 +68,24 @@ private CachedGraphTransaction cache() { private HugeVertex newVertex(Id id) { HugeGraph graph = this.cache().graph(); graph.schema().propertyKey("name").asText() - .checkExist(false).create(); + .checkExist(false).create(); graph.schema().vertexLabel("person") - .idStrategy(IdStrategy.CUSTOMIZE_NUMBER) - .properties("name").nullableKeys("name") - .checkExist(false) - .create(); + .idStrategy(IdStrategy.CUSTOMIZE_NUMBER) + .properties("name").nullableKeys("name") + .checkExist(false) + .create(); VertexLabel vl = graph.vertexLabel("person"); return new HugeVertex(graph, id, vl); } - private HugeEdge newEdge(HugeVertex out, HugeVertex in){ + private HugeEdge newEdge(HugeVertex out, HugeVertex in) { HugeGraph graph = this.cache().graph(); graph.schema().edgeLabel("person_know_person") - .sourceLabel("person") - .targetLabel("person") - .checkExist(false) - .create(); - return out.addEdge("person_know_person",in); + .sourceLabel("person") + .targetLabel("person") + .checkExist(false) + .create(); + return out.addEdge("person_know_person", in); } @Test @@ -143,7 +143,7 @@ public void testEventInvalid() throws Exception { @Test - public void testClearEdgeCacheWhenDeleteVertex(){ + public void testEdgeCacheClearWhenDeleteVertex() { CachedGraphTransaction cache = this.cache(); HugeVertex v1 = this.newVertex(IdGenerator.of(1)); HugeVertex v2 = this.newVertex(IdGenerator.of(2)); @@ -152,35 +152,34 @@ public void testClearEdgeCacheWhenDeleteVertex(){ cache.addVertex(v1); cache.addVertex(v2); cache.commit(); - HugeEdge edge = this.newEdge(v1,v2); + HugeEdge edge = this.newEdge(v1, v2); cache.addEdge(edge); cache.commit(); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); Assert.assertEquals(2L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); cache.removeVertex(v3); cache.commit(); Assert.assertEquals(0L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); Assert.assertEquals(2L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); cache.removeVertex(v1); cache.commit(); Assert.assertEquals(0L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); Assert.assertFalse(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); - } @Test - public void testClearEdgeCacheWhenUpdateVertex(){ + public void testEdgeCacheClearWhenUpdateVertex() { CachedGraphTransaction cache = this.cache(); HugeVertex v1 = this.newVertex(IdGenerator.of(1)); HugeVertex v2 = this.newVertex(IdGenerator.of(2)); @@ -189,38 +188,36 @@ public void testClearEdgeCacheWhenUpdateVertex(){ cache.addVertex(v1); cache.addVertex(v2); cache.commit(); - HugeEdge edge = this.newEdge(v1,v2); + HugeEdge edge = this.newEdge(v1, v2); cache.addEdge(edge); cache.commit(); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); Assert.assertEquals(2L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); - ///update property of v3 cache.addVertexProperty(new HugeVertexProperty<>(v3, - cache.graph().schema().getPropertyKey("name"),"test-name")); + cache.graph().schema().getPropertyKey("name"), + "test-name")); cache.commit(); Assert.assertEquals(0L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext()); Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext()); Assert.assertEquals(2L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); cache.addVertexProperty(new HugeVertexProperty<>(v1, - cache.graph().schema().getPropertyKey("name"),"test-name")); + cache.graph().schema().getPropertyKey("name"), + "test-name")); cache.commit(); Assert.assertEquals(0L, - Whitebox.invoke(cache, "edgesCache", "size")); + Whitebox.invoke(cache, "edgesCache", "size")); String name = cache.queryEdgesByVertex(IdGenerator.of(1)).next().outVertex() - .value("name"); - Assert.assertEquals("test-name",name); - - + .value("name"); + Assert.assertEquals("test-name", name); } } - From 5751a1ec791943a011c26085579f832c5747ab5b Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Mon, 14 Mar 2022 14:28:43 +0800 Subject: [PATCH 6/7] Update CachedGraphTransaction.java enhance the comment --- .../baidu/hugegraph/backend/cache/CachedGraphTransaction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java index a63e73cd67..eb064f14d0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedGraphTransaction.java @@ -397,7 +397,7 @@ protected final void commitMutation2Backend(BackendMutation... mutations) { /* * Update edge cache if any vertex or edge changed * For vertex change, the edges linked with should also be updated - * Before we use a more precise strategy,now we just clear all the edge cache + * Before we find a more precise strategy, just clear all the edge cache now */ boolean invalidEdgesCache = (edgesInTxSize + updates.size() + deletions.size()) > 0; if (invalidEdgesCache && this.enableCacheEdge()) { From fdbe63a20cdcc8bc8f64143b41a4a62c335d99c4 Mon Sep 17 00:00:00 2001 From: AisinGiorro <54138330+sunlujing@users.noreply.github.com> Date: Mon, 14 Mar 2022 15:18:18 +0800 Subject: [PATCH 7/7] Update CachedGraphTransactionTest.java remove one blank line --- .../baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java index 19bb17c8cb..20fee3d735 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CachedGraphTransactionTest.java @@ -141,7 +141,6 @@ public void testEventInvalid() throws Exception { Whitebox.invoke(cache, "verticesCache", "size")); } - @Test public void testEdgeCacheClearWhenDeleteVertex() { CachedGraphTransaction cache = this.cache();