Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -100,15 +100,23 @@ protected void originQuery(Query originQuery) {
}

public Map<HugeKeys, Order> orders() {
return Collections.unmodifiableMap(this.orders);
return Collections.unmodifiableMap(this.getOrNewOrders());
}

public void orders(Map<HugeKeys, Order> 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<HugeKeys, Order> getOrNewOrders() {
if (this.orders != null) {
return this.orders;
}
this.orders = InsertionOrderUtil.newMap();
return this.orders;
}

public long offset() {
Expand Down Expand Up @@ -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) &&
Expand All @@ -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) ^
Expand All @@ -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<String, Object> 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<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.unit.core;
package com.baidu.hugegraph.unit.util;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.unit.core;
package com.baidu.hugegraph.unit.util;

import org.junit.Test;

Expand Down