use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.
the class IndexSerializer method createQueryString.
/* ################################################
Common code used by executeQuery and executeTotals
################################################### */
private String createQueryString(IndexQueryBuilder query, final ElementCategory resultType, final StandardJanusGraphTx transaction, MixedIndexType index) {
Preconditions.checkArgument(index.getElement() == resultType, "Index is not configured for the desired result type: %s", resultType);
final String backingIndexName = index.getBackingIndexName();
final IndexProvider indexInformation = (IndexProvider) mixedIndexes.get(backingIndexName);
final StringBuilder qB = new StringBuilder(query.getQuery());
final String prefix = query.getPrefix();
Preconditions.checkNotNull(prefix);
// Convert query string by replacing
int replacements = 0;
int pos = 0;
while (pos < qB.length()) {
pos = qB.indexOf(prefix, pos);
if (pos < 0)
break;
final int startPos = pos;
pos += prefix.length();
final StringBuilder keyBuilder = new StringBuilder();
final boolean quoteTerminated = qB.charAt(pos) == '"';
if (quoteTerminated)
pos++;
while (pos < qB.length() && (Character.isLetterOrDigit(qB.charAt(pos)) || (quoteTerminated && qB.charAt(pos) != '"') || qB.charAt(pos) == '*')) {
keyBuilder.append(qB.charAt(pos));
pos++;
}
if (quoteTerminated)
pos++;
final int endPos = pos;
final String keyName = keyBuilder.toString();
Preconditions.checkArgument(StringUtils.isNotBlank(keyName), "Found reference to empty key at position [%s]", startPos);
String replacement;
if (keyName.equals("*")) {
replacement = indexInformation.getFeatures().getWildcardField();
} else if (transaction.containsRelationType(keyName)) {
final PropertyKey key = transaction.getPropertyKey(keyName);
Preconditions.checkNotNull(key);
Preconditions.checkArgument(index.indexesKey(key), "The used key [%s] is not indexed in the targeted index [%s]", key.name(), query.getIndex());
replacement = key2Field(index, key);
} else {
Preconditions.checkArgument(query.getUnknownKeyName() != null, "Found reference to nonexistent property key in query at position [%s]: %s", startPos, keyName);
replacement = query.getUnknownKeyName();
}
Preconditions.checkArgument(StringUtils.isNotBlank(replacement));
qB.replace(startPos, endPos, replacement);
pos = startPos + replacement.length();
replacements++;
}
final String queryStr = qB.toString();
if (replacements <= 0)
log.warn("Could not convert given {} index query: [{}]", resultType, query.getQuery());
log.info("Converted query string with {} replacements: [{}] => [{}]", replacements, query.getQuery(), queryStr);
return queryStr;
}
use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.
the class ElasticsearchConfigTest method testClientThrowsExceptionIfServerNotReachable.
@Test
public void testClientThrowsExceptionIfServerNotReachable() throws BackendException, InterruptedException {
final ModifiableConfiguration config = esr.setConfiguration(GraphDatabaseConfiguration.buildGraphConfiguration(), INDEX_NAME);
Configuration indexConfig = config.restrictTo(INDEX_NAME);
IndexProvider idx = open(indexConfig);
simpleWriteAndQuery(idx);
idx.close();
config.set(INDEX_HOSTS, new String[] { "localhost:" + esr.getPort() + 1 }, INDEX_NAME);
final Configuration wrongHostConfig = config.restrictTo(INDEX_NAME);
assertThrows(Exception.class, () -> new ElasticSearchIndex(wrongHostConfig));
}
use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.
the class ElasticsearchConfigTest method testExternalDynamic.
private void testExternalDynamic(boolean withUpdateMapping, boolean useMappingsForES7) throws Exception {
final Duration maxWrite = Duration.ofMillis(2000L);
final String storeName = "test_mapping";
final Configuration indexConfig = buildIndexConfigurationForExternalDynamic(withUpdateMapping, useMappingsForES7);
final IndexProvider idx = open(indexConfig);
// Test that the "date" property throws an exception.
final KeyInformation.IndexRetriever indexRetriever = IndexProviderTest.getIndexRetriever(IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD));
final BaseTransactionConfig txConfig = StandardBaseTransactionConfig.of(TimestampProviders.MILLI);
final IndexTransaction itx = new IndexTransaction(idx, indexRetriever, txConfig, maxWrite);
try {
idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
fail("should fail");
} catch (final PermanentBackendException e) {
log.debug(e.getMessage(), e);
}
if (isMappingUsed(idx)) {
executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypesMapping("/dynamic_mapping.json"));
} else {
executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypelessMapping("/typeless_dynamic_mapping.json"));
}
// Test that the "date" property works well.
idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
// Test that the "weight" property works well due to dynamic mapping.
idx.register(storeName, "weight", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("weight"), itx);
itx.rollback();
idx.close();
final ElasticSearchClient client = ElasticSearchSetup.REST_CLIENT.connect(indexConfig).getClient();
final Map<String, Object> properties = client.getMapping("janusgraph_" + storeName, storeName).getProperties();
assertEquals(withUpdateMapping, properties.containsKey("weight"), properties.toString());
}
use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.
the class ElasticsearchConfigTest method testExternalMappingsViaMapping.
@ParameterizedTest
@MethodSource("useMappingsForES7Configuration")
public void testExternalMappingsViaMapping(Boolean useMappingsForES7) throws Exception {
final Duration maxWrite = Duration.ofMillis(2000L);
final String storeName = "test_mapping";
final ModifiableConfiguration modifiableConfiguration = esr.setConfiguration(GraphDatabaseConfiguration.buildGraphConfiguration(), INDEX_NAME).set(USE_EXTERNAL_MAPPINGS, true, INDEX_NAME);
if (useMappingsForES7) {
modifiableConfiguration.set(USE_MAPPING_FOR_ES7, true, INDEX_NAME);
}
final Configuration indexConfig = modifiableConfiguration.restrictTo(INDEX_NAME);
final IndexProvider idx = open(indexConfig);
// Test that the "date" property throws an exception.
final KeyInformation.IndexRetriever indexRetriever = IndexProviderTest.getIndexRetriever(IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD));
final BaseTransactionConfig txConfig = StandardBaseTransactionConfig.of(TimestampProviders.MILLI);
final IndexTransaction itx = new IndexTransaction(idx, indexRetriever, txConfig, maxWrite);
try {
idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
fail("should fail");
} catch (final PermanentBackendException e) {
log.debug(e.getMessage(), e);
}
if (isMappingUsed(idx)) {
executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypesMapping("/strict_mapping.json"));
} else {
executeRequestWithStringEntity(idx, "janusgraph_" + storeName, readTypelessMapping("/typeless_strict_mapping.json"));
}
// Test that the "date" property works well.
idx.register(storeName, "date", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("date"), itx);
// Test that the "weight" property throws an exception.
try {
idx.register(storeName, "weight", IndexProviderTest.getMapping(idx.getFeatures(), ANALYZER_ENGLISH, ANALYZER_KEYWORD).get("weight"), itx);
fail("should fail");
} catch (final BackendException e) {
log.debug(e.getMessage(), e);
}
itx.rollback();
idx.close();
}
use of org.janusgraph.diskstorage.indexing.IndexProvider in project janusgraph by JanusGraph.
the class Backend method beginTransaction.
/**
* Opens a new transaction against all registered backend system wrapped in one {@link BackendTransaction}.
*
* @return
* @throws BackendException
*/
public BackendTransaction beginTransaction(TransactionConfiguration configuration, KeyInformation.Retriever indexKeyRetriever) throws BackendException {
StoreTransaction tx = storeManagerLocking.beginTransaction(configuration);
// Cache
CacheTransaction cacheTx = new CacheTransaction(tx, storeManagerLocking, bufferSize, maxWriteTime, configuration.hasEnabledBatchLoading());
// Index transactions
final Map<String, IndexTransaction> indexTx = new HashMap<>(indexes.size());
for (Map.Entry<String, IndexProvider> entry : indexes.entrySet()) {
indexTx.put(entry.getKey(), new IndexTransaction(entry.getValue(), indexKeyRetriever.get(entry.getKey()), configuration, maxWriteTime));
}
return new BackendTransaction(cacheTx, configuration, storeFeatures, edgeStore, indexStore, txLogStore, maxReadTime, indexTx, threadPool);
}
Aggregations