Skip to content

Commit 46a971a

Browse files
authored
Merge pull request #4755 from ntisseyre/management_api
Returning Composite index structure and field values for hex string
2 parents 613996f + 2db56c9 commit 46a971a

File tree

7 files changed

+172
-27
lines changed

7 files changed

+172
-27
lines changed

janusgraph-backend-testutils/src/main/java/org/janusgraph/graphdb/JanusGraphIndexTest.java

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.janusgraph.core.attribute.Geoshape;
5555
import org.janusgraph.core.attribute.Text;
5656
import org.janusgraph.core.log.TransactionRecovery;
57+
import org.janusgraph.core.schema.CompositeIndexInfo;
5758
import org.janusgraph.core.schema.JanusGraphIndex;
5859
import org.janusgraph.core.schema.JanusGraphManagement;
5960
import org.janusgraph.core.schema.Mapping;
@@ -97,7 +98,6 @@
9798
import org.janusgraph.graphdb.tinkerpop.optimize.step.JanusGraphStep;
9899
import org.janusgraph.graphdb.tinkerpop.optimize.strategy.JanusGraphMixedIndexCountStrategy;
99100
import org.janusgraph.graphdb.transaction.StandardJanusGraphTx;
100-
import org.janusgraph.graphdb.types.CompositeIndexType;
101101
import org.janusgraph.graphdb.types.IndexField;
102102
import org.janusgraph.graphdb.types.MixedIndexType;
103103
import org.janusgraph.graphdb.types.ParameterType;
@@ -162,7 +162,6 @@
162162
import static org.junit.jupiter.api.Assertions.assertTrue;
163163
import static org.junit.jupiter.api.Assertions.fail;
164164

165-
166165
/**
167166
* @author Matthias Broecheler ([email protected])
168167
*/
@@ -4357,29 +4356,82 @@ public void testGetIndexInfo() throws DecoderException {
43574356

43584357
mgmt.makeVertexLabel("cat").make();
43594358

4360-
makeKey("id", Integer.class);
4359+
final PropertyKey idKey = makeKey("id", Integer.class);
43614360
final PropertyKey nameKey = makeKey("name", String.class);
4361+
final PropertyKey metaKey = makeKey("meta", Object.class);
4362+
4363+
String searchByNameIndex = "searchByName";
4364+
mgmt.buildIndex(searchByNameIndex, Vertex.class)
4365+
.addKey(nameKey)
4366+
.buildCompositeIndex();
43624367

4363-
String indexName = "searchByName";
4364-
mgmt.buildIndex(indexName, Vertex.class)
4368+
String searchByMetaIndex = "searchByMeta";
4369+
mgmt.buildIndex(searchByMetaIndex, Vertex.class)
43654370
.addKey(nameKey)
4371+
.addKey(metaKey)
43664372
.buildCompositeIndex();
4373+
4374+
String inlinePropIndex = "inlineProp";
4375+
mgmt.buildIndex(inlinePropIndex, Vertex.class)
4376+
.addKey(nameKey)
4377+
.addKey(metaKey)
4378+
.addInlinePropertyKey(idKey)
4379+
.buildCompositeIndex();
4380+
43674381
mgmt.commit();
43684382

43694383
mgmt = graph.openManagement();
43704384

4385+
//Read index with single property
43714386
Map<String, Object> fieldValues = new HashMap<>();
43724387
fieldValues.put("name", "someName");
43734388

4374-
String hexString = mgmt.getIndexKey(indexName, fieldValues);
4375-
CompositeIndexType indexType = mgmt.getIndexInfo(hexString);
4389+
String hexString = mgmt.getIndexKey(searchByNameIndex, fieldValues);
4390+
CompositeIndexInfo indexInfo = mgmt.getIndexInfo(hexString);
43764391

43774392
IndexField[] indexFields = new IndexField[1];
43784393
indexFields[0] = IndexField.of(nameKey);
43794394

4380-
assertEquals(indexName, indexType.getName());
4381-
assertEquals(1, indexType.getFieldKeys().length);
4382-
assertEquals(nameKey, indexType.getFieldKeys()[0].getFieldKey());
4383-
assertEquals(0, indexType.getInlineFieldKeys().length);
4395+
assertEquals(searchByNameIndex, indexInfo.getIndexName());
4396+
assertEquals(1, indexInfo.getCompositeIndexType().getFieldKeys().length);
4397+
assertEquals("someName", indexInfo.getValues().get(nameKey));
4398+
assertEquals(0, indexInfo.getCompositeIndexType().getInlineFieldKeys().length);
4399+
4400+
//Read index with multi properties
4401+
fieldValues = new HashMap<>();
4402+
fieldValues.put("name", "name1");
4403+
fieldValues.put("meta", "hello");
4404+
4405+
hexString = mgmt.getIndexKey(searchByMetaIndex, fieldValues);
4406+
indexInfo = mgmt.getIndexInfo(hexString);
4407+
4408+
indexFields = new IndexField[2];
4409+
indexFields[0] = IndexField.of(nameKey);
4410+
indexFields[1] = IndexField.of(metaKey);
4411+
4412+
assertEquals(searchByMetaIndex, indexInfo.getIndexName());
4413+
assertEquals(2, indexInfo.getCompositeIndexType().getFieldKeys().length);
4414+
assertEquals("name1", indexInfo.getValues().get(nameKey));
4415+
assertEquals("hello", indexInfo.getValues().get(metaKey));
4416+
assertEquals(0, indexInfo.getCompositeIndexType().getInlineFieldKeys().length);
4417+
4418+
//Read index with inline properties
4419+
fieldValues = new HashMap<>();
4420+
fieldValues.put("name", "name2");
4421+
fieldValues.put("meta", "bye");
4422+
4423+
hexString = mgmt.getIndexKey(inlinePropIndex, fieldValues);
4424+
indexInfo = mgmt.getIndexInfo(hexString);
4425+
4426+
indexFields = new IndexField[2];
4427+
indexFields[0] = IndexField.of(nameKey);
4428+
indexFields[1] = IndexField.of(metaKey);
4429+
4430+
assertEquals(inlinePropIndex, indexInfo.getIndexName());
4431+
assertEquals(2, indexInfo.getCompositeIndexType().getFieldKeys().length);
4432+
assertEquals("name2", indexInfo.getValues().get(nameKey));
4433+
assertEquals("bye", indexInfo.getValues().get(metaKey));
4434+
assertEquals(1, indexInfo.getCompositeIndexType().getInlineFieldKeys().length);
4435+
assertEquals("id", indexInfo.getCompositeIndexType().getInlineFieldKeys()[0]);
43844436
}
43854437
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 JanusGraph Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.janusgraph.core.schema;
16+
17+
import org.janusgraph.core.PropertyKey;
18+
import org.janusgraph.graphdb.types.CompositeIndexType;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class CompositeIndexInfo {
24+
final CompositeIndexType compositeIndexType;
25+
final Object[] indexFieldValues;
26+
27+
final Map<PropertyKey, Object> indexValues;
28+
29+
public CompositeIndexInfo(CompositeIndexType compositeIndexType, Object[] indexFieldValues) {
30+
this.compositeIndexType = compositeIndexType;
31+
this.indexFieldValues = indexFieldValues;
32+
33+
this.indexValues = new HashMap<>();
34+
for (int i = 0; i < compositeIndexType.getFieldKeys().length; i++) {
35+
PropertyKey p = compositeIndexType.getFieldKeys()[i].getFieldKey();
36+
Object v = indexFieldValues[i];
37+
this.indexValues.put(p, v);
38+
}
39+
}
40+
41+
public CompositeIndexType getCompositeIndexType() {
42+
return compositeIndexType;
43+
}
44+
45+
public String getIndexName() {
46+
return compositeIndexType.getName();
47+
}
48+
49+
public Map<PropertyKey, Object> getValues() {
50+
return indexValues;
51+
}
52+
}

janusgraph-core/src/main/java/org/janusgraph/core/schema/JanusGraphManagement.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.janusgraph.core.RelationType;
2525
import org.janusgraph.core.VertexLabel;
2626
import org.janusgraph.diskstorage.keycolumnvalue.scan.ScanJobFuture;
27-
import org.janusgraph.graphdb.types.CompositeIndexType;
2827

2928
import java.time.Duration;
3029
import java.util.List;
@@ -500,5 +499,5 @@ interface IndexBuilder {
500499
* @param hexString
501500
* @return composite index info
502501
*/
503-
CompositeIndexType getIndexInfo(String hexString) throws DecoderException;
502+
CompositeIndexInfo getIndexInfo(String hexString) throws DecoderException;
504503
}

janusgraph-core/src/main/java/org/janusgraph/graphdb/database/IndexSerializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.janusgraph.core.PropertyKey;
2727
import org.janusgraph.core.SchemaViolationException;
2828
import org.janusgraph.core.attribute.Geoshape;
29+
import org.janusgraph.core.schema.CompositeIndexInfo;
2930
import org.janusgraph.core.schema.Parameter;
3031
import org.janusgraph.core.schema.SchemaStatus;
3132
import org.janusgraph.diskstorage.BackendException;
@@ -525,6 +526,10 @@ public long getIndexIdFromKey(StaticBuffer key) {
525526
return IndexRecordUtil.getIndexIdFromKey(key, hashKeys, hashLength);
526527
}
527528

529+
public CompositeIndexInfo getIndexInfoFromKey(StaticBuffer key, Function<Long, CompositeIndexType> compositeIndexTypeFunction) {
530+
return IndexRecordUtil.getIndexInfoFromKey(serializer, compositeIndexTypeFunction, key, hashKeys, hashLength);
531+
}
532+
528533
public StaticBuffer getIndexKey(CompositeIndexType index, Map<String, Object> fieldValues) {
529534
return IndexRecordUtil.getIndexKey(index, fieldValues, serializer, hashKeys, hashLength);
530535
}

janusgraph-core/src/main/java/org/janusgraph/graphdb/database/management/ManagementSystem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.janusgraph.core.PropertyKey;
3838
import org.janusgraph.core.RelationType;
3939
import org.janusgraph.core.VertexLabel;
40+
import org.janusgraph.core.schema.CompositeIndexInfo;
4041
import org.janusgraph.core.schema.ConsistencyModifier;
4142
import org.janusgraph.core.schema.EdgeLabelMaker;
4243
import org.janusgraph.core.schema.Index;
@@ -566,7 +567,7 @@ public String getIndexKey(String indexName, Map<String, Object> fieldValues) {
566567
}
567568

568569
@Override
569-
public CompositeIndexType getIndexInfo(String hexString) throws DecoderException {
570+
public CompositeIndexInfo getIndexInfo(String hexString) throws DecoderException {
570571
StaticArrayBuffer indexKey = StaticArrayBuffer.of(Hex.decodeHex(hexString));
571572
return transaction.getCompositeIndexInfo(indexKey);
572573
}

janusgraph-core/src/main/java/org/janusgraph/graphdb/database/util/IndexRecordUtil.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.janusgraph.graphdb.database.util;
1616

17+
import com.google.common.base.Function;
1718
import com.google.common.base.Preconditions;
1819
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.Iterators;
@@ -27,6 +28,7 @@
2728
import org.janusgraph.core.JanusGraphVertex;
2829
import org.janusgraph.core.JanusGraphVertexProperty;
2930
import org.janusgraph.core.PropertyKey;
31+
import org.janusgraph.core.schema.CompositeIndexInfo;
3032
import org.janusgraph.core.schema.SchemaStatus;
3133
import org.janusgraph.diskstorage.Entry;
3234
import org.janusgraph.diskstorage.ReadBuffer;
@@ -367,8 +369,36 @@ public static StaticBuffer getIndexKey(CompositeIndexType index, Object[] values
367369
}
368370

369371
public static long getIndexIdFromKey(StaticBuffer key, boolean hashKeys, HashingUtil.HashLength hashLength) {
370-
if (hashKeys) key = HashingUtil.getKey(hashLength,key);
371-
return VariableLong.readPositive(key.asReadBuffer());
372+
ReadBuffer readBuffer = getIndexKeyReadBuffer(key, hashKeys, hashLength);
373+
return VariableLong.readPositive(readBuffer);
374+
}
375+
376+
public static CompositeIndexInfo getIndexInfoFromKey(Serializer serializer,
377+
Function<Long, CompositeIndexType> compositeIndexTypeFunction,
378+
StaticBuffer key,
379+
boolean hashKeys,
380+
HashingUtil.HashLength hashLength) {
381+
382+
ReadBuffer readBuffer = getIndexKeyReadBuffer(key, hashKeys, hashLength);
383+
long indexId = VariableLong.readPositive(readBuffer);//read index id
384+
CompositeIndexType compositeIndexType = compositeIndexTypeFunction.apply(indexId);
385+
IndexField[] fieldKeys = compositeIndexType.getFieldKeys();
386+
Object[] values = new Object[fieldKeys.length];
387+
for (int i = 0; i < fieldKeys.length; i++) {
388+
IndexField f = fieldKeys[i];
389+
if (InternalAttributeUtil.hasGenericDataType(f.getFieldKey())) {
390+
values[i] = serializer.readClassAndObject(readBuffer);
391+
} else {
392+
values[i] = serializer.readObjectNotNull(readBuffer, f.getFieldKey().dataType());
393+
}
394+
395+
}
396+
return new CompositeIndexInfo(compositeIndexType, values);
397+
}
398+
399+
private static ReadBuffer getIndexKeyReadBuffer(StaticBuffer key, boolean hashKeys, HashingUtil.HashLength hashLength) {
400+
if (hashKeys) key = HashingUtil.getKey(hashLength, key);
401+
return key.asReadBuffer();
372402
}
373403

374404
public static IndexUpdate<StaticBuffer, Entry> getCompositeIndexUpdate(CompositeIndexType index, IndexMutationType indexMutationType, IndexRecordEntry[] record,

janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/StandardJanusGraphTx.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.janusgraph.core.SchemaViolationException;
4141
import org.janusgraph.core.VertexLabel;
4242
import org.janusgraph.core.attribute.Cmp;
43+
import org.janusgraph.core.schema.CompositeIndexInfo;
4344
import org.janusgraph.core.schema.ConsistencyModifier;
4445
import org.janusgraph.core.schema.EdgeLabelMaker;
4546
import org.janusgraph.core.schema.JanusGraphSchemaElement;
@@ -1258,19 +1259,24 @@ public StaticBuffer getCompositeIndexKey(String indexName, Map<String, Object> f
12581259
}
12591260
}
12601261

1261-
public CompositeIndexType getCompositeIndexInfo(StaticArrayBuffer indexKey) {
1262-
long schemaVertexId = indexSerializer.getIndexIdFromKey(indexKey);
1263-
InternalVertex typeVertex = vertexCache.get(schemaVertexId, existingVertexRetriever);
1262+
public CompositeIndexInfo getCompositeIndexInfo(StaticArrayBuffer indexKey) {
12641263

1265-
Preconditions.checkNotNull(typeVertex, "Index with key [" + indexKey + "] was not found");
1266-
JanusGraphSchemaVertex schemaVertex = (JanusGraphSchemaVertex) typeVertex;
1264+
com.google.common.base.Function<Long, CompositeIndexType> compositeIndexTypeFunction = input -> {
1265+
long schemaVertexId = indexSerializer.getIndexIdFromKey(indexKey);
1266+
InternalVertex typeVertex = vertexCache.get(schemaVertexId, existingVertexRetriever);
12671267

1268-
IndexType indexType = schemaVertex.asIndexType();
1269-
if (indexType instanceof CompositeIndexType) {
1270-
return (CompositeIndexType) indexType;
1271-
} else {
1272-
throw new IllegalArgumentException("Index with key [" + indexKey + "] is not a composite index");
1273-
}
1268+
Preconditions.checkNotNull(typeVertex, "Index with key [" + indexKey + "] was not found");
1269+
JanusGraphSchemaVertex schemaVertex = (JanusGraphSchemaVertex) typeVertex;
1270+
1271+
IndexType indexType = schemaVertex.asIndexType();
1272+
if (indexType instanceof CompositeIndexType) {
1273+
return (CompositeIndexType) indexType;
1274+
} else {
1275+
throw new IllegalArgumentException("Index with key [" + indexKey + "] is not a composite index");
1276+
}
1277+
};
1278+
1279+
return indexSerializer.getIndexInfoFromKey(indexKey, compositeIndexTypeFunction);
12741280
}
12751281

12761282
@Override

0 commit comments

Comments
 (0)