Skip to content

Commit 40e0314

Browse files
committed
fix(server): ensure graph clear API does not clear meta table
1 parent 4ad56cb commit 40e0314

3 files changed

Lines changed: 76 additions & 54 deletions

File tree

hugegraph-server/hugegraph-mysql/src/main/java/org/apache/hugegraph/backend/store/mysql/MysqlStore.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.stream.Collectors;
2728

2829
import org.apache.hugegraph.backend.BackendException;
2930
import org.apache.hugegraph.backend.id.Id;
@@ -335,7 +336,7 @@ protected void clearTables() {
335336

336337
protected void truncateTables() {
337338
Session session = this.sessions.session();
338-
for (MysqlTable table : this.tables()) {
339+
for (MysqlTable table : this.getTruncatedTables()) {
339340
table.truncate(session);
340341
}
341342
}
@@ -344,6 +345,14 @@ protected Collection<MysqlTable> tables() {
344345
return this.tables.values();
345346
}
346347

348+
protected Collection<MysqlTable> getTruncatedTables() {
349+
// Exclude meta table to preserve system metadata during graph clear
350+
return this.tables.entrySet().stream()
351+
.filter(e -> !(HugeType.META == e.getKey()))
352+
.map(Map.Entry::getValue)
353+
.collect(Collectors.toList());
354+
}
355+
347356
@Override
348357
protected final MysqlTable table(HugeType type) {
349358
assert type != null;
@@ -505,6 +514,7 @@ public String storedVersion() {
505514
@Override
506515
protected Collection<MysqlTable> tables() {
507516
List<MysqlTable> tables = new ArrayList<>(super.tables());
517+
tables.add(this.meta);
508518
return tables;
509519
}
510520
}

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/mysql/BaseMysqlUnitTest.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
28
*
3-
* * Licensed to the Apache Software Foundation (ASF) under one or more
4-
* * contributor license agreements. See the NOTICE file distributed with
5-
* * this work for additional information regarding copyright ownership.
6-
* * The ASF licenses this file to You under the Apache License, Version 2.0
7-
* * (the "License"); you may not use this file except in compliance with
8-
* * the License. You may obtain a copy of the License at
9-
* *
10-
* * http://www.apache.org/licenses/LICENSE-2.0
11-
* *
12-
* * Unless required by applicable law or agreed to in writing, software
13-
* * distributed under the License is distributed on an "AS IS" BASIS,
14-
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* * See the License for the specific language governing permissions and
16-
* * limitations under the License.
9+
* http://www.apache.org/licenses/LICENSE-2.0
1710
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
1816
*/
1917

2018
package org.apache.hugegraph.unit.mysql;
@@ -49,12 +47,19 @@ public void setup() throws Exception {
4947
}
5048

5149
@After
52-
public void down(){
50+
public void down() {
5351
if (this.provider != null) {
5452
try {
5553
this.provider.close();
5654
} catch (Exception e) {
57-
LOG.warn("Mysql provider close failed",e);
55+
LOG.warn("Mysql provider close failed ", e);
56+
}
57+
}
58+
if (this.sessions != null) {
59+
try {
60+
this.sessions.close();
61+
} catch (Exception e) {
62+
LOG.warn("Mysql sessions close failed ", e);
5863
}
5964
}
6065
}

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/mysql/MysqlTest.java

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
import org.apache.hugegraph.testutil.Assert;
2626
import org.junit.Before;
2727
import org.junit.Test;
28-
import java.sql.PreparedStatement;
28+
2929
import java.sql.SQLException;
3030

31-
public class MysqlTest extends BaseMysqlUnitTest{
31+
public class MysqlTest extends BaseMysqlUnitTest {
32+
3233
@Before
3334
public void setUp() throws Exception {
3435
super.setup();
@@ -44,67 +45,73 @@ public void testMysqlMetaVersion() throws SQLException {
4445
// Get sessions for direct table operations
4546
MysqlSessions.Session session = this.sessions.session();
4647
session.open();
47-
48+
4849
// Get the actual table names from the store
4950
String vertexTable = "g_v";
5051
String edgeOutTable = "g_oe";
5152
String edgeInTable = "g_ie";
52-
53+
5354
// Insert test vertex data directly using simple SQL
5455
String insertVertex = String.format(
55-
"INSERT INTO %s (ID, LABEL, PROPERTIES, EXPIRED_TIME) VALUES ('test_vertex', 1, '{\"name\":\"test_vertex\"}', 0)",
56-
vertexTable
56+
"INSERT INTO %s (ID, LABEL, PROPERTIES, EXPIRED_TIME) VALUES ('test_vertex', 1, " +
57+
"'{\"name\":\"test_vertex\"}', 0)",
58+
vertexTable
5759
);
5860
session.execute(insertVertex);
59-
61+
6062
// Insert test edge data directly using simple SQL
6163
String insertEdgeOut = String.format(
62-
"INSERT INTO %s (OWNER_VERTEX, DIRECTION, LABEL, SUB_LABEL, SORT_VALUES, OTHER_VERTEX, PROPERTIES, EXPIRED_TIME) VALUES ('test_vertex', 0, 1, 0, '', 'target_vertex', '{\"weight\":1.0}', 0)",
63-
edgeOutTable
64+
"INSERT INTO %s (OWNER_VERTEX, DIRECTION, LABEL, SUB_LABEL, SORT_VALUES, " +
65+
"OTHER_VERTEX, PROPERTIES, EXPIRED_TIME) VALUES ('test_vertex', 0, 1, 0, '', " +
66+
"'target_vertex', '{\"weight\":1.0}', 0)",
67+
edgeOutTable
6468
);
6569
session.execute(insertEdgeOut);
66-
70+
6771
String insertEdgeIn = String.format(
68-
"INSERT INTO %s (OWNER_VERTEX, DIRECTION, LABEL, SUB_LABEL, SORT_VALUES, OTHER_VERTEX, PROPERTIES, EXPIRED_TIME) VALUES ('target_vertex', 1, 1, 0, '', 'test_vertex', '{\"weight\":1.0}', 0)",
69-
edgeInTable
72+
"INSERT INTO %s (OWNER_VERTEX, DIRECTION, LABEL, SUB_LABEL, SORT_VALUES, " +
73+
"OTHER_VERTEX, PROPERTIES, EXPIRED_TIME) VALUES ('target_vertex', 1, 1, 0, '', " +
74+
"'test_vertex', '{\"weight\":1.0}', 0)",
75+
edgeInTable
7076
);
7177
session.execute(insertEdgeIn);
7278

7379
// Verify data exists by querying
7480
String selectVertex = String.format("SELECT * FROM %s WHERE ID = 'test_vertex'", vertexTable);
75-
ResultSetWrapper vResult = session.select(selectVertex);
76-
Assert.assertTrue("vertex data should exist", vResult.next());
77-
vResult.close();
78-
79-
String selectEdgeOut = String.format("SELECT * FROM %s WHERE OWNER_VERTEX = 'test_vertex' AND DIRECTION = 0", edgeOutTable);
80-
ResultSetWrapper oeResult = session.select(selectEdgeOut);
81-
Assert.assertTrue("out edge data should exist", oeResult.next());
82-
oeResult.close();
83-
84-
String selectEdgeIn = String.format("SELECT * FROM %s WHERE OWNER_VERTEX = 'target_vertex' AND DIRECTION = 1", edgeInTable);
85-
ResultSetWrapper ieResult = session.select(selectEdgeIn);
86-
Assert.assertTrue("in edge data should exist", ieResult.next());
87-
ieResult.close();
81+
String selectEdgeOut = String.format(
82+
"SELECT * FROM %s WHERE OWNER_VERTEX = 'test_vertex' AND DIRECTION = 0",
83+
edgeOutTable);
84+
String selectEdgeIn = String.format(
85+
"SELECT * FROM %s WHERE OWNER_VERTEX = 'target_vertex' AND DIRECTION = 1",
86+
edgeInTable);
87+
try (
88+
ResultSetWrapper vResult = session.select(selectVertex);
89+
ResultSetWrapper oeResult = session.select(selectEdgeOut);
90+
ResultSetWrapper ieResult = session.select(selectEdgeIn);
91+
) {
92+
Assert.assertTrue("vertex data should exist", vResult.next());
93+
Assert.assertTrue("out edge data should exist", oeResult.next());
94+
Assert.assertTrue("in edge data should exist", ieResult.next());
95+
}
8896

8997
// Execute truncate operation, clears all graph data but preserves system tables
9098
this.provider.truncate();
9199

92100
// Verify system version remains unchanged after truncation
93101
String afterVersion = systemStore.storedVersion();
94102
Assert.assertNotNull("System metadata version should exist", afterVersion);
95-
Assert.assertEquals("System metadata version should remain unchanged after truncation", beforeVersion, afterVersion);
103+
Assert.assertEquals("System metadata version should remain unchanged after truncation",
104+
beforeVersion, afterVersion);
96105

97106
// Verify data has been cleared
98-
vResult = session.select(selectVertex);
99-
Assert.assertFalse("vertex data should not exist", vResult.next());
100-
vResult.close();
101-
102-
oeResult = session.select(selectEdgeOut);
103-
Assert.assertFalse("out edge data should not exist", oeResult.next());
104-
oeResult.close();
105-
106-
ieResult = session.select(selectEdgeIn);
107-
Assert.assertFalse("in edge data should not exist", ieResult.next());
108-
ieResult.close();
107+
try (
108+
ResultSetWrapper vResult = session.select(selectVertex);
109+
ResultSetWrapper oeResult = session.select(selectEdgeOut);
110+
ResultSetWrapper ieResult = session.select(selectEdgeIn);
111+
) {
112+
Assert.assertFalse("vertex data should not exist", vResult.next());
113+
Assert.assertFalse("out edge data should not exist", oeResult.next());
114+
Assert.assertFalse("in edge data should not exist", ieResult.next());
115+
}
109116
}
110117
}

0 commit comments

Comments
 (0)