From 214e7fc16055cf819db175016472cf8ab35486a0 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Fri, 21 Jun 2019 11:49:15 +0800 Subject: [PATCH] don't print non-valuable parameters for Query Change-Id: I45c9b481bdee667ea175320f6dc55191a7ebde48 --- .../hugegraph/backend/page/PageState.java | 30 ++++--- .../baidu/hugegraph/backend/query/Query.java | 53 +++++++++---- .../baidu/hugegraph/unit/UnitTestSuite.java | 33 +++++--- .../hugegraph/unit/core/DirectionsTest.java | 30 ++++--- .../baidu/hugegraph/unit/core/QueryTest.java | 78 +++++++++++++++++++ .../hugegraph/unit/core/SerialEnumTest.java | 30 ++++--- .../unit/{core => util}/JsonUtilTest.java | 2 +- .../unit/{core => util}/StringUtilTest.java | 2 +- .../unit/{core => util}/VersionTest.java | 2 +- 9 files changed, 185 insertions(+), 75 deletions(-) create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/QueryTest.java rename hugegraph-test/src/main/java/com/baidu/hugegraph/unit/{core => util}/JsonUtilTest.java (99%) rename hugegraph-test/src/main/java/com/baidu/hugegraph/unit/{core => util}/StringUtilTest.java (98%) rename hugegraph-test/src/main/java/com/baidu/hugegraph/unit/{core => util}/VersionTest.java (97%) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java index 0bf4db9f7a..95f56b4067 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageState.java @@ -1,22 +1,20 @@ /* + * Copyright 2017 HugeGraph Authors * - * * Copyright 2017 HugeGraph Authors - * * - * * 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. + * 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 com.baidu.hugegraph.backend.page; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java index 0053982f8d..4b0eb106f3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Query.java @@ -20,7 +20,6 @@ package com.baidu.hugegraph.backend.query; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -32,6 +31,7 @@ import com.baidu.hugegraph.type.HugeType; import com.baidu.hugegraph.type.define.HugeKeys; import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.InsertionOrderUtil; import com.google.common.collect.ImmutableSet; public class Query implements Cloneable { @@ -62,7 +62,7 @@ public Query(HugeType resultType, Query originQuery) { this.resultType = resultType; this.originQuery = originQuery; - this.orders = new LinkedHashMap<>(); + this.orders = null; this.offset = 0L; this.limit = NO_LIMIT; @@ -100,15 +100,23 @@ protected void originQuery(Query originQuery) { } public Map orders() { - return Collections.unmodifiableMap(this.orders); + return Collections.unmodifiableMap(this.getOrNewOrders()); } public void orders(Map orders) { - this.orders = new LinkedHashMap<>(orders); + this.orders = InsertionOrderUtil.newMap(orders); } public void order(HugeKeys key, Order order) { - this.orders.put(key, order); + this.getOrNewOrders().put(key, order); + } + + private Map getOrNewOrders() { + if (this.orders != null) { + return this.orders; + } + this.orders = InsertionOrderUtil.newMap(); + return this.orders; } public long offset() { @@ -266,7 +274,7 @@ public boolean equals(Object object) { } Query other = (Query) object; return this.resultType.equals(other.resultType) && - this.orders.equals(other.orders) && + this.orders().equals(other.orders()) && this.offset == other.offset && this.limit == other.limit && Objects.equals(this.page, other.page) && @@ -277,7 +285,7 @@ public boolean equals(Object object) { @Override public int hashCode() { return this.resultType.hashCode() ^ - this.orders.hashCode() ^ + this.orders().hashCode() ^ Long.hashCode(this.offset) ^ Long.hashCode(this.limit) ^ Objects.hashCode(this.page) ^ @@ -287,12 +295,31 @@ public int hashCode() { @Override public String toString() { - return String.format("Query for %s page=%s, offset=%d, limit=%d, order by %s", - this.resultType, - this.page, - this.offset, - this.limit, - this.orders.toString()); + Map pairs = InsertionOrderUtil.newMap(); + if (this.page != null) { + pairs.put("page", this.page); + } + if (this.offset != 0) { + pairs.put("offset", this.offset); + } + if (this.limit != NO_LIMIT) { + pairs.put("limit", this.limit); + } + if (!this.orders().isEmpty()) { + pairs.put("order by", this.orders()); + } + + StringBuilder sb = new StringBuilder(64); + sb.append("Query for ").append(this.resultType); + for (Map.Entry entry : pairs.entrySet()) { + sb.append(' ').append(entry.getKey()) + .append(' ').append(entry.getValue()).append(','); + } + if (!pairs.isEmpty()) { + // Delete last comma + sb.deleteCharAt(sb.length() - 1); + } + return sb.toString(); } public static long defaultCapacity(long capacity) { diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java index 72ca47f866..7420694831 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java @@ -33,36 +33,47 @@ import com.baidu.hugegraph.unit.core.DataTypeTest; import com.baidu.hugegraph.unit.core.DirectionsTest; import com.baidu.hugegraph.unit.core.EdgeIdTest; -import com.baidu.hugegraph.unit.core.JsonUtilTest; import com.baidu.hugegraph.unit.core.LocksTableTest; +import com.baidu.hugegraph.unit.core.QueryTest; import com.baidu.hugegraph.unit.core.SerialEnumTest; -import com.baidu.hugegraph.unit.core.StringUtilTest; -import com.baidu.hugegraph.unit.core.VersionTest; import com.baidu.hugegraph.unit.rocksdb.RocksDBCountersTest; import com.baidu.hugegraph.unit.rocksdb.RocksDBSessionsTest; +import com.baidu.hugegraph.unit.util.JsonUtilTest; +import com.baidu.hugegraph.unit.util.StringUtilTest; +import com.baidu.hugegraph.unit.util.VersionTest; @RunWith(Suite.class) @Suite.SuiteClasses({ + /* cache */ RamCacheTest.class, CachedSchemaTransactionTest.class, CachedGraphTransactionTest.class, CacheManagerTest.class, - VersionTest.class, + /* types */ DataTypeTest.class, DirectionsTest.class, SerialEnumTest.class, + + /* core */ + LocksTableTest.class, + AnalyzerTest.class, + EdgeIdTest.class, BackendMutationTest.class, - CassandraTest.class, ConditionQueryFlattenTest.class, - EdgeIdTest.class, - AnalyzerTest.class, - JsonUtilTest.class, - StringUtilTest.class, - LocksTableTest.class, + QueryTest.class, + + /* cassandra */ + CassandraTest.class, + /* rocksdb */ RocksDBSessionsTest.class, - RocksDBCountersTest.class + RocksDBCountersTest.class, + + /* utils */ + VersionTest.class, + JsonUtilTest.class, + StringUtilTest.class }) public class UnitTestSuite { } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DirectionsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DirectionsTest.java index 2bb487afb1..6f1cd1676e 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DirectionsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DirectionsTest.java @@ -1,22 +1,20 @@ /* + * Copyright 2017 HugeGraph Authors * - * * Copyright 2017 HugeGraph Authors - * * - * * 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. + * 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 com.baidu.hugegraph.unit.core; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/QueryTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/QueryTest.java new file mode 100644 index 0000000000..3c3dccfff4 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/QueryTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.unit.core; + +import org.junit.Test; + +import com.baidu.hugegraph.backend.query.Query; +import com.baidu.hugegraph.backend.query.Query.Order; +import com.baidu.hugegraph.testutil.Assert; +import com.baidu.hugegraph.type.HugeType; +import com.baidu.hugegraph.type.define.HugeKeys; +import com.google.common.collect.ImmutableMap; + +public class QueryTest { + + @Test + public void testOrderBy() { + Query query = new Query(HugeType.VERTEX); + Assert.assertTrue(query.orders().isEmpty()); + + query.order(HugeKeys.NAME, Order.ASC); + Assert.assertEquals(ImmutableMap.of(HugeKeys.NAME, Order.ASC), + query.orders()); + } + + @Test + public void testToString() { + Query query = new Query(HugeType.VERTEX); + Assert.assertEquals("Query for VERTEX", query.toString()); + + query.page("page1"); + Assert.assertEquals("Query for VERTEX page page1", query.toString()); + + query = new Query(HugeType.VERTEX); + query.limit(10L); + Assert.assertEquals("Query for VERTEX limit 10", query.toString()); + + query = new Query(HugeType.VERTEX); + query.page("page2"); + query.limit(10L); + Assert.assertEquals("Query for VERTEX page page2, limit 10", + query.toString()); + + query = new Query(HugeType.VERTEX); + query.page("page3"); + query.offset(100L); + query.limit(10L); + Assert.assertEquals("Query for VERTEX page page3, offset 100, limit 10", + query.toString()); + + query = new Query(HugeType.VERTEX); + query.page("page4"); + query.offset(100L); + query.limit(10L); + query.order(HugeKeys.NAME, Order.ASC); + query.order(HugeKeys.FIELDS, Order.DESC); + Assert.assertEquals("Query for VERTEX page page4, offset 100, " + + "limit 10, order by {NAME=ASC, FIELDS=DESC}", + query.toString()); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/SerialEnumTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/SerialEnumTest.java index 9b9211a75e..eb438bebb6 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/SerialEnumTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/SerialEnumTest.java @@ -1,22 +1,20 @@ /* + * Copyright 2017 HugeGraph Authors * - * * Copyright 2017 HugeGraph Authors - * * - * * 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. + * 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 com.baidu.hugegraph.unit.core; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/JsonUtilTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java similarity index 99% rename from hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/JsonUtilTest.java rename to hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java index 946d0eb9df..bae5412a79 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/JsonUtilTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/JsonUtilTest.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.unit.core; +package com.baidu.hugegraph.unit.util; import java.util.Arrays; import java.util.Map; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/StringUtilTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/StringUtilTest.java similarity index 98% rename from hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/StringUtilTest.java rename to hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/StringUtilTest.java index 04e4c21246..b0ca8897d6 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/StringUtilTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/StringUtilTest.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.unit.core; +package com.baidu.hugegraph.unit.util; import java.util.List; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/VersionTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/VersionTest.java similarity index 97% rename from hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/VersionTest.java rename to hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/VersionTest.java index 943a9c65f1..6da79fb86a 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/VersionTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/util/VersionTest.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.unit.core; +package com.baidu.hugegraph.unit.util; import org.junit.Test;