use of com.baidu.hugegraph.type.define.DataType in project incubator-hugegraph by apache.
the class IndexLabelBuilder method checkFields4Range.
private void checkFields4Range() {
if (this.indexType != IndexType.RANGE) {
return;
}
List<String> fields = this.indexFields;
E.checkArgument(fields.size() == 1, "Range index can only build on " + "one field, but got %s fields: '%s'", fields.size(), fields);
String field = fields.iterator().next();
PropertyKey property = this.graph().propertyKey(field);
E.checkArgument(!property.cardinality().multiple(), "Not allowed to build range index on property " + "'%s' whose cardinality is multiple", field);
DataType dataType = this.graph().propertyKey(field).dataType();
E.checkArgument(dataType.isNumber() || dataType.isDate(), "Range index can only build on numeric or " + "date property, but got %s(%s)", dataType, field);
switch(dataType) {
case BYTE:
case INT:
this.indexType = IndexType.RANGE_INT;
break;
case FLOAT:
this.indexType = IndexType.RANGE_FLOAT;
break;
case LONG:
case DATE:
this.indexType = IndexType.RANGE_LONG;
break;
case DOUBLE:
this.indexType = IndexType.RANGE_DOUBLE;
break;
default:
throw new AssertionError("Invalid datatype: " + dataType);
}
}
use of com.baidu.hugegraph.type.define.DataType in project incubator-hugegraph by apache.
the class IndexLabelBuilder method checkFields.
private void checkFields(Set<Id> propertyIds) {
List<String> fields = this.indexFields;
E.checkNotEmpty(fields, "index fields", this.name);
Set<Id> olapPks = new IdSet(CollectionType.EC);
for (String field : fields) {
PropertyKey pkey = this.propertyKeyOrNull(field);
// In general this will not happen
E.checkArgument(pkey != null, "Can't build index on undefined property key " + "'%s' for '%s': '%s'", field, this.baseType.readableName(), this.baseValue);
E.checkArgument(pkey.aggregateType().isIndexable(), "The aggregate type %s is not indexable", pkey.aggregateType());
if (pkey.cardinality().multiple()) {
E.checkArgument(fields.size() == 1, "Not allowed to build union index on property" + " key '%s' whose cardinality is multiple", pkey.name());
}
if (pkey.olap()) {
olapPks.add(pkey.id());
}
}
if (!olapPks.isEmpty()) {
E.checkArgument(olapPks.size() == 1, "Can't build index on multiple olap properties, " + "but got fields '%s' for index label '%s'", fields, this.name);
E.checkArgument(olapPks.size() == fields.size(), "Can't build index on olap properties and oltp " + "properties in one index label, " + "but got fields '%s' for index label '%s'", fields, this.name);
E.checkArgument(this.indexType == IndexType.SECONDARY || this.indexType == IndexType.RANGE, "Only secondary and range index can be built on " + "olap property, but got index type '%s' on olap " + "property key '%s' for index label '%s'", this.indexType, fields.get(0), this.name);
}
List<String> properties = this.graph().mapPkId2Name(propertyIds);
E.checkArgument(properties.containsAll(fields), "Not all index fields '%s' are contained in " + "schema properties '%s'", fields, properties);
// Range index must build on single numeric column
if (this.indexType == IndexType.RANGE) {
this.checkFields4Range();
}
// Search index must build on single text column
if (this.indexType.isSearch()) {
E.checkArgument(fields.size() == 1, "Search index can only build on " + "one field, but got %s fields: '%s'", fields.size(), fields);
String field = fields.iterator().next();
DataType dataType = this.graph().propertyKey(field).dataType();
E.checkArgument(dataType.isText(), "Search index can only build on text property, " + "but got %s(%s)", dataType, field);
}
}
use of com.baidu.hugegraph.type.define.DataType in project incubator-hugegraph by apache.
the class IndexLabelBuilder method oneNumericField.
private boolean oneNumericField() {
if (this.indexFields.size() != 1) {
return false;
}
String field = this.indexFields.get(0);
PropertyKey propertyKey = this.graph().propertyKey(field);
DataType dataType = propertyKey.dataType();
return dataType.isNumber() || dataType.isDate();
}
use of com.baidu.hugegraph.type.define.DataType in project incubator-hugegraph by apache.
the class HugeIndex method parseIndexId.
public static HugeIndex parseIndexId(HugeGraph graph, HugeType type, byte[] id) {
Object values;
IndexLabel indexLabel;
if (type.isStringIndex()) {
Id idObject = IdGenerator.of(id, IdType.STRING);
String[] parts = SplicingIdGenerator.parse(idObject);
E.checkState(parts.length == 2, "Invalid secondary index id");
Id label = IdGenerator.ofStoredString(parts[0], IdType.LONG);
indexLabel = IndexLabel.label(graph, label);
values = parts[1];
} else {
assert type.isRange4Index() || type.isRange8Index();
final int labelLength = 4;
E.checkState(id.length > labelLength, "Invalid range index id");
BytesBuffer buffer = BytesBuffer.wrap(id);
Id label = IdGenerator.of(buffer.readInt());
indexLabel = IndexLabel.label(graph, label);
List<Id> fields = indexLabel.indexFields();
E.checkState(fields.size() == 1, "Invalid range index fields");
DataType dataType = graph.propertyKey(fields.get(0)).dataType();
E.checkState(dataType.isNumber() || dataType.isDate(), "Invalid range index field type");
Class<?> clazz = dataType.isNumber() ? dataType.clazz() : DataType.LONG.clazz();
values = bytes2number(buffer.read(id.length - labelLength), clazz);
}
HugeIndex index = new HugeIndex(graph, indexLabel);
index.fieldValues(values);
return index;
}
use of com.baidu.hugegraph.type.define.DataType in project incubator-hugegraph by apache.
the class TableSerializer method readPropertyKey.
@Override
public PropertyKey readPropertyKey(HugeGraph graph, BackendEntry backendEntry) {
if (backendEntry == null) {
return null;
}
TableBackendEntry entry = this.convertEntry(backendEntry);
Number id = schemaColumn(entry, HugeKeys.ID);
String name = schemaColumn(entry, HugeKeys.NAME);
DataType dataType = schemaEnum(entry, HugeKeys.DATA_TYPE, DataType.class);
Cardinality cardinality = schemaEnum(entry, HugeKeys.CARDINALITY, Cardinality.class);
AggregateType aggregateType = schemaEnum(entry, HugeKeys.AGGREGATE_TYPE, AggregateType.class);
WriteType writeType = schemaEnumOrDefault(entry, HugeKeys.WRITE_TYPE, WriteType.class, WriteType.OLTP);
Object properties = schemaColumn(entry, HugeKeys.PROPERTIES);
SchemaStatus status = schemaEnum(entry, HugeKeys.STATUS, SchemaStatus.class);
PropertyKey propertyKey = new PropertyKey(graph, this.toId(id), name);
propertyKey.dataType(dataType);
propertyKey.cardinality(cardinality);
propertyKey.aggregateType(aggregateType);
propertyKey.writeType(writeType);
propertyKey.properties(this.toIdArray(properties));
propertyKey.status(status);
this.readUserdata(propertyKey, entry);
return propertyKey;
}
Aggregations