Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support range sortKey feature #296

Merged
merged 1 commit into from
Dec 29, 2018
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
2 changes: 1 addition & 1 deletion hugegraph-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.9.0</version>
<version>0.9.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-cassandra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.9.0</version>
<version>0.9.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public String version() {
* Versions history:
* [1.0] HugeGraph-1328: supports backend table version checking
* [1.1] HugeGraph-1322: add support for full-text search
* [1.2] #296: support range sortKey feature
*/
return "1.1";
return "1.2";
}
}
6 changes: 3 additions & 3 deletions hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph</artifactId>
<version>0.9.0</version>
<version>0.9.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>hugegraph-core</artifactId>
Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.5.3</version>
<version>1.5.6</version>
</dependency>

<!-- tinkerpop -->
Expand Down Expand Up @@ -157,7 +157,7 @@
</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>0.9.0.0</Implementation-Version>
<Implementation-Version>0.9.1.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public boolean isLogic() {
this.type() == ConditionType.OR;
}

public boolean isFlattened() {
return this.isRelation();
}

public static Condition and(Condition left, Condition right) {
return new And(left, right);
}
Expand Down Expand Up @@ -393,6 +397,12 @@ public boolean test(HugeElement element) {
public Condition copy() {
return new And(this.left().copy(), this.right().copy());
}

@Override
public boolean isFlattened() {
// If this is flattened, its sub-condition should not be nested
return this.left().isRelation() && this.right().isRelation();
}
}

public static class Or extends BinCondition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Object condition(Object key) {
for (Condition c : this.conditions) {
if (c.isRelation()) {
Condition.Relation r = (Condition.Relation) c;
if (r.key().equals(key)) {
if (r.key().equals(key) && r.relation() == RelationType.EQ) {
values.add(r.value());
}
}
Expand Down Expand Up @@ -277,11 +277,11 @@ public Set<Id> userpropKeys() {

/**
* This method is only used for secondary index scenario,
* relation must be EQ
* its relation must be EQ
* @param fields the user property fields
javeme marked this conversation as resolved.
Show resolved Hide resolved
* @return the corresponding user property values of fileds
* @return the corresponding user property serial values of fields
*/
public List<Object> userpropValues(List<Id> fields) {
public String userpropValuesString(List<Id> fields) {
List<Object> values = new ArrayList<>(fields.size());
for (Id field : fields) {
boolean got = false;
Expand All @@ -290,9 +290,9 @@ public List<Object> userpropValues(List<Id> fields) {
E.checkState(r.relation == RelationType.EQ,
"Method userpropValues(List<String>) only " +
"used for secondary index, " +
"relation must be EQ, but got '%s'",
"relation must be EQ, but got %s",
r.relation());
values.add(r.value());
values.add(r.serialValue());
got = true;
}
}
Expand All @@ -302,18 +302,14 @@ public List<Object> userpropValues(List<Id> fields) {
field, this);
}
}
return values;
}

public String userpropValuesString(List<Id> fields) {
return SplicingIdGenerator.concatValues(this.userpropValues(fields));
return SplicingIdGenerator.concatValues(values);
}

public Set<Object> userpropValues(Id field) {
Set<Object> values = new HashSet<>();
for (Relation r : this.userpropRelations()) {
if (r.key().equals(field)) {
values.add(r.value());
values.add(r.serialValue());
}
}
return values;
Expand All @@ -325,8 +321,8 @@ public Object userpropValue(Id field) {
return null;
}
E.checkState(values.size() == 1,
"Expect one user-property value of field '%s', but got %s",
field, values.size());
"Expect one user-property value of field '%s', " +
"but got '%s'", field, values.size());
return values.iterator().next();
}

Expand Down Expand Up @@ -393,7 +389,7 @@ public boolean test(HugeElement element) {

public void checkFlattened() {
for (Condition condition : this.conditions) {
E.checkState(condition.isRelation(),
E.checkState(condition.isFlattened(),
"Condition Query has none-flatten condition '%s'",
condition);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.backend.query;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.structure.HugeElement;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.E;

public class IdPrefixQuery extends Query {

private final Id start;
private final boolean inclusiveStart;
private final Id prefix;

public IdPrefixQuery(HugeType resultType, Id prefix) {
this(resultType, null, prefix, true, prefix);
}

public IdPrefixQuery(Query originQuery, Id prefix) {
this(originQuery.resultType(), originQuery, prefix, true, prefix);
}

public IdPrefixQuery(Query originQuery,
Id start, boolean inclusive, Id prefix) {
this(originQuery.resultType(), originQuery, start, inclusive, prefix);
}

public IdPrefixQuery(HugeType resultType, Query originQuery,
Id start, boolean inclusive, Id prefix) {
super(resultType, originQuery);
E.checkArgumentNotNull(start, "The start parameter can't be null");
this.start = start;
this.inclusiveStart = inclusive;
this.prefix = prefix;
}

public Id start() {
return this.start;
}

public boolean inclusiveStart() {
return this.inclusiveStart;
}

public Id prefix() {
return this.prefix;
}

@Override
public boolean empty() {
return false;
}

@Override
public boolean test(HugeElement element) {
byte[] elem = element.id().asBytes();
int cmp = Bytes.compare(elem, this.start.asBytes());
boolean matchedStart = this.inclusiveStart ? cmp >= 0 : cmp > 0;
boolean matchedPrefix = Bytes.prefixWith(elem, this.prefix.asBytes());
Linary marked this conversation as resolved.
Show resolved Hide resolved
return matchedStart && matchedPrefix;
}

@Override
public IdPrefixQuery copy() {
return (IdPrefixQuery) super.copy();
}

@Override
public String toString() {
return String.format("%s where id prefix with %s and start with %s(%s)",
super.toString(), this.prefix, this.start,
this.inclusiveStart ? "inclusive" : "exclusive");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.backend.query;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.structure.HugeElement;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.E;

public class IdRangeQuery extends Query {

private final Id start;
private final Id end;
private final boolean inclusiveStart;
private final boolean inclusiveEnd;

public IdRangeQuery(HugeType resultType, Id start, Id end) {
this(resultType, null, start, end);
}

public IdRangeQuery(HugeType resultType, Query originQuery,
Id start, Id end) {
this(resultType, originQuery, start, true, end, false);
}

public IdRangeQuery(Query originQuery,
Id start, boolean inclusiveStart,
Id end, boolean inclusiveEnd) {
this(originQuery.resultType(), originQuery,
start, inclusiveStart, end, inclusiveEnd);
}

public IdRangeQuery(HugeType resultType, Query originQuery,
Id start, boolean inclusiveStart,
Id end, boolean inclusiveEnd) {
super(resultType, originQuery);
E.checkArgumentNotNull(start, "The start parameter can't be null");
this.start = start;
this.end = end;
this.inclusiveStart = inclusiveStart;
this.inclusiveEnd = inclusiveEnd;
}

public Id start() {
return this.start;
}

public Id end() {
return this.end;
}

public boolean inclusiveStart() {
return this.inclusiveStart;
}

public boolean inclusiveEnd() {
return this.inclusiveEnd;
}

@Override
public boolean empty() {
return false;
}

@Override
public boolean test(HugeElement element) {
int cmp1 = Bytes.compare(element.id().asBytes(), this.start.asBytes());
Linary marked this conversation as resolved.
Show resolved Hide resolved
int cmp2 = Bytes.compare(element.id().asBytes(), this.end.asBytes());
return (this.inclusiveStart ? cmp1 >= 0 : cmp1 > 0) &&
(this.inclusiveEnd ? cmp2 <= 0 : cmp2 < 0);
}

@Override
public IdRangeQuery copy() {
return (IdRangeQuery) super.copy();
}

@Override
public String toString() {
return String.format("%s where id in range %s%s,%s%s",
super.toString(),
this.inclusiveStart ? "[" : "(",
this.start, this.end,
this.inclusiveEnd ? "]" : ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected BackendEntry convertEntry(BackendEntry entry) {

protected abstract Id writeQueryId(HugeType type, Id id);

protected abstract Id writeQueryEdgeCondition(Query query);
protected abstract Query writeQueryEdgeCondition(Query query);

protected abstract Query writeQueryCondition(Query query);

Expand All @@ -53,9 +53,9 @@ public Query writeQuery(Query query) {
"and by condition at the same time");
}

Id id = this.writeQueryEdgeCondition(query);
if (id != null) {
return new IdQuery(query, id);
Query result = this.writeQueryEdgeCondition(query);
if (result != null) {
return result;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.StringEncoding;

public class BinaryBackendEntry implements BackendEntry {

Expand Down Expand Up @@ -229,7 +228,7 @@ public boolean equals(Object other) {

@Override
public String toString() {
return StringEncoding.decode(this.bytes);
return "0x" + Bytes.toHex(this.bytes);
}
}
}
Loading