Search in sources :

Example 21 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method search.

@Override
public <O> PagedResult<JsonObject> search(String key, ScanConsistency scanConsistency, Expression expression, SearchScope scope, String[] attributes, Sort[] orderBy, CouchbaseBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    BucketMapping bucketMapping = connectionProvider.getBucketMappingByKey(key);
    boolean secondTry = false;
    ScanConsistency useScanConsistency = getScanConsistency(scanConsistency, attemptWithoutAttributeScanConsistency);
    PagedResult<JsonObject> result = null;
    int attemps = 20;
    do {
        attemps--;
        try {
            result = searchImpl(bucketMapping, key, useScanConsistency, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize);
            break;
        } catch (SearchException ex) {
            if (ex.getErrorCode() != 5000) {
                throw ex;
            }
            LOG.warn("Waiting for Indexer Warmup...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex2) {
            }
        }
    } while (attemps > 0);
    if ((result == null) || (result.getEntriesCount() == 0)) {
        ScanConsistency useScanConsistency2 = getScanConsistency(scanConsistency, false);
        if (!useScanConsistency2.equals(useScanConsistency)) {
            useScanConsistency = useScanConsistency2;
            result = searchImpl(bucketMapping, key, useScanConsistency, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize);
            secondTry = true;
        }
    }
    String attemptInfo = getScanAttemptLogInfo(scanConsistency, useScanConsistency, secondTry);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("Couchbase operation: search, duration: {}, bucket: {}, key: {}, expression: {}, scope: {}, attributes: {}, orderBy: {}, batchOperationWraper: {}, returnDataType: {}, start: {}, count: {}, pageSize: {}, consistency: {}{}", duration, bucketMapping.getBucketName(), key, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize, useScanConsistency, attemptInfo);
    return result;
}
Also used : Instant(java.time.Instant) ScanConsistency(com.couchbase.client.java.query.consistency.ScanConsistency) JsonObject(com.couchbase.client.java.document.json.JsonObject) SearchException(io.jans.orm.exception.operation.SearchException) Duration(java.time.Duration) BucketMapping(io.jans.orm.couchbase.model.BucketMapping)

Example 22 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method deleteRecursivelyImpl.

private boolean deleteRecursivelyImpl(BucketMapping bucketMapping, String key) throws SearchException, EntryNotFoundException {
    try {
        if (enableScopeSupport) {
            MutateLimitPath deleteQuery = Delete.deleteFrom(Expression.i(bucketMapping.getBucketName())).where(Expression.path("META().id").like(Expression.s(key + "%")));
            N1qlQueryResult result = bucketMapping.getBucket().query(deleteQuery);
            if (!result.finalSuccess()) {
                throw new SearchException(String.format("Failed to delete entries. Query: '%s'. Error: '%s', Error count: '%d'", deleteQuery, result.errors(), result.info().errorCount()), result.errors().get(0).getInt("code"));
            }
        } else {
            LOG.warn("Removing only base key without sub-tree: " + key);
            delete(key);
        }
        return true;
    } catch (CouchbaseException ex) {
        throw new EntryNotFoundException("Failed to delete entry", ex);
    }
}
Also used : CouchbaseException(com.couchbase.client.core.CouchbaseException) MutateLimitPath(com.couchbase.client.java.query.dsl.path.MutateLimitPath) EntryNotFoundException(io.jans.orm.exception.operation.EntryNotFoundException) SearchException(io.jans.orm.exception.operation.SearchException) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Example 23 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method lookup.

@Override
public JsonObject lookup(String key, ScanConsistency scanConsistency, String... attributes) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    BucketMapping bucketMapping = connectionProvider.getBucketMappingByKey(key);
    boolean secondTry = false;
    ScanConsistency useScanConsistency = getScanConsistency(scanConsistency, attemptWithoutAttributeScanConsistency);
    JsonObject result = null;
    SearchException lastException = null;
    try {
        result = lookupImpl(bucketMapping, key, useScanConsistency, attributes);
    } catch (SearchException ex) {
        lastException = ex;
    }
    if ((result == null) || result.isEmpty()) {
        ScanConsistency useScanConsistency2 = getScanConsistency(scanConsistency, false);
        if (!useScanConsistency2.equals(useScanConsistency)) {
            useScanConsistency = useScanConsistency2;
            secondTry = true;
            result = lookupImpl(bucketMapping, key, useScanConsistency, attributes);
        } else {
            if (lastException != null) {
                throw lastException;
            }
        }
    }
    String attemptInfo = getScanAttemptLogInfo(scanConsistency, useScanConsistency, secondTry);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("Couchbase operation: lookup, duration: {}, bucket: {}, key: {}, attributes: {}, consistency: {}{}", duration, bucketMapping.getBucketName(), key, attributes, useScanConsistency, attemptInfo);
    return result;
}
Also used : Instant(java.time.Instant) ScanConsistency(com.couchbase.client.java.query.consistency.ScanConsistency) JsonObject(com.couchbase.client.java.document.json.JsonObject) SearchException(io.jans.orm.exception.operation.SearchException) Duration(java.time.Duration) BucketMapping(io.jans.orm.couchbase.model.BucketMapping)

Example 24 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method searchImpl.

private <O> PagedResult<JsonObject> searchImpl(BucketMapping bucketMapping, String key, ScanConsistency scanConsistency, Expression expression, SearchScope scope, String[] attributes, Sort[] orderBy, CouchbaseBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    Bucket bucket = bucketMapping.getBucket();
    BatchOperation<O> batchOperation = null;
    if (batchOperationWraper != null) {
        batchOperation = (BatchOperation<O>) batchOperationWraper.getBatchOperation();
    }
    if (LOG.isTraceEnabled()) {
        // Find whole DB search
        if (StringHelper.equalsIgnoreCase(key, "_")) {
            LOG.trace("Search in whole DB tree", new Exception());
        }
    }
    Expression finalExpression = expression;
    if (enableScopeSupport) {
        Expression scopeExpression;
        if (scope == null) {
            scopeExpression = null;
        } else if (SearchScope.BASE == scope) {
            scopeExpression = Expression.path("META().id").like(Expression.s(key + "%")).and(Expression.path("META().id").notLike(Expression.s(key + "\\\\_%\\\\_")));
        } else {
            scopeExpression = Expression.path("META().id").like(Expression.s(key + "%"));
        }
        if (scopeExpression != null) {
            finalExpression = scopeExpression.and(expression);
        }
    } else {
        if (scope != null) {
            LOG.debug("Ignoring scope '" + scope + " for expression: " + expression);
        }
    }
    String[] select = attributes;
    if (select == null) {
        select = new String[] { "jans_doc.*", CouchbaseOperationService.DN };
    } else if ((select.length == 1) && StringHelper.isEmpty(select[0])) {
        // Compatibility with base persistence layer when application pass filter new String[] { "" }
        select = new String[] { CouchbaseOperationService.DN };
    } else {
        boolean hasDn = Arrays.asList(select).contains(CouchbaseOperationService.DN);
        if (!hasDn) {
            select = ArrayHelper.arrayMerge(select, new String[] { CouchbaseOperationService.DN });
        }
    }
    GroupByPath selectQuery = Select.select(select).from(Expression.i(bucketMapping.getBucketName())).as("jans_doc").where(finalExpression);
    LimitPath baseQuery = selectQuery;
    if (orderBy != null) {
        baseQuery = selectQuery.orderBy(orderBy);
    }
    List<N1qlQueryRow> searchResultList = new ArrayList<N1qlQueryRow>();
    if ((SearchReturnDataType.SEARCH == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        N1qlQueryResult lastResult = null;
        if (pageSize > 0) {
            boolean collectSearchResult;
            Statement query = null;
            int currentLimit;
            try {
                List<N1qlQueryRow> lastSearchResultList;
                int resultCount = 0;
                do {
                    collectSearchResult = true;
                    currentLimit = pageSize;
                    if (count > 0) {
                        currentLimit = Math.min(pageSize, count - resultCount);
                    }
                    query = baseQuery.limit(currentLimit).offset(start + resultCount);
                    LOG.debug("Execution query: '" + query + "'");
                    lastResult = bucket.query(N1qlQuery.simple(query, N1qlParams.build().consistency(scanConsistency)));
                    if (!lastResult.finalSuccess()) {
                        throw new SearchException(String.format("Failed to search entries. Query: '%s'. Error: '%s', Error count: '%d'", query, lastResult.errors(), lastResult.info().errorCount()), lastResult.errors().get(0).getInt("code"));
                    }
                    lastSearchResultList = lastResult.allRows();
                    if (batchOperation != null) {
                        collectSearchResult = batchOperation.collectSearchResult(lastSearchResultList.size());
                    }
                    if (collectSearchResult) {
                        searchResultList.addAll(lastSearchResultList);
                    }
                    if (batchOperation != null) {
                        List<O> entries = batchOperationWraper.createEntities(lastSearchResultList);
                        batchOperation.performAction(entries);
                    }
                    resultCount += lastSearchResultList.size();
                    if ((count > 0) && (resultCount >= count)) {
                        break;
                    }
                } while (lastSearchResultList.size() > 0);
            } catch (CouchbaseException ex) {
                throw new SearchException("Failed to search entries. Query: '" + query + "'", ex);
            }
        } else {
            try {
                Statement query = baseQuery;
                if (count > 0) {
                    query = ((LimitPath) query).limit(count);
                }
                if (start > 0) {
                    query = ((OffsetPath) query).offset(start);
                }
                LOG.debug("Execution query: '" + query + "'");
                lastResult = bucket.query(N1qlQuery.simple(query, N1qlParams.build().consistency(scanConsistency)));
                if (!lastResult.finalSuccess()) {
                    throw new SearchException(String.format("Failed to search entries. Query: '%s'. Error: '%s', Error count: '%d'", baseQuery, lastResult.errors(), lastResult.info().errorCount()), lastResult.errors().get(0).getInt("code"));
                }
                searchResultList.addAll(lastResult.allRows());
            } catch (CouchbaseException ex) {
                throw new SearchException("Failed to search entries. Query: '" + baseQuery.toString() + "'", ex);
            }
        }
    }
    List<JsonObject> resultRows = new ArrayList<JsonObject>(searchResultList.size());
    for (N1qlQueryRow row : searchResultList) {
        resultRows.add(row.value());
    }
    PagedResult<JsonObject> result = new PagedResult<JsonObject>();
    result.setEntries(resultRows);
    result.setEntriesCount(resultRows.size());
    result.setStart(start);
    if ((SearchReturnDataType.COUNT == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        GroupByPath selectCountQuery = Select.select("COUNT(*) as TOTAL").from(Expression.i(bucketMapping.getBucketName())).where(finalExpression);
        try {
            LOG.debug("Calculating count. Execution query: '" + selectCountQuery + "'");
            N1qlQueryResult countResult = bucket.query(N1qlQuery.simple(selectCountQuery, N1qlParams.build().consistency(scanConsistency)));
            if (!countResult.finalSuccess() || (countResult.info().resultCount() != 1)) {
                throw new SearchException(String.format("Failed to calculate count entries. Query: '%s'. Error: '%s', Error count: '%d'", selectCountQuery, countResult.errors(), countResult.info().errorCount()), countResult.errors().get(0).getInt("code"));
            }
            result.setTotalEntriesCount(countResult.allRows().get(0).value().getInt("TOTAL"));
        } catch (CouchbaseException ex) {
            throw new SearchException("Failed to calculate count entries. Query: '" + selectCountQuery.toString() + "'", ex);
        }
    }
    return result;
}
Also used : Statement(com.couchbase.client.java.query.Statement) MutateLimitPath(com.couchbase.client.java.query.dsl.path.MutateLimitPath) LimitPath(com.couchbase.client.java.query.dsl.path.LimitPath) ArrayList(java.util.ArrayList) SearchException(io.jans.orm.exception.operation.SearchException) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult) DeleteException(io.jans.orm.exception.operation.DeleteException) PersistenceException(io.jans.orm.exception.operation.PersistenceException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchException(io.jans.orm.exception.operation.SearchException) CouchbaseException(com.couchbase.client.core.CouchbaseException) AuthenticationException(io.jans.orm.exception.AuthenticationException) EntryNotFoundException(io.jans.orm.exception.operation.EntryNotFoundException) N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) CouchbaseException(com.couchbase.client.core.CouchbaseException) Bucket(com.couchbase.client.java.Bucket) Expression(com.couchbase.client.java.query.dsl.Expression) GroupByPath(com.couchbase.client.java.query.dsl.path.GroupByPath) PagedResult(io.jans.orm.model.PagedResult)

Example 25 with SearchException

use of io.jans.orm.exception.operation.SearchException in project jans by JanssenProject.

the class CouchbaseEntryManager method findEntriesImpl.

protected <T> PagedResult<JsonObject> findEntriesImpl(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, BatchOperation<T> batchOperation, SearchReturnDataType returnDataType, int start, int count, int chunkSize) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
    }
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", ArrayHelper.toString(objectClasses));
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare default sort
    Sort[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        Sort requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new Sort[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new Sort[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toCouchbaseKey(baseDN);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toCouchbaseFilter(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter %s to expression", searchFilter));
    }
    PagedResult<JsonObject> searchResult = null;
    try {
        CouchbaseBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new CouchbaseBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(keyWithInum.getKey(), getScanConsistency(convertedExpression), convertedExpression.expression(), scope, currentLdapReturnAttributes, defaultSort, batchOperationWraper, returnDataType, start, count, chunkSize);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entries with key: %s, expression: %s", keyWithInum.getKey(), convertedExpression));
        }
        return searchResult;
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: %s, expression: %s", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) JsonObject(com.couchbase.client.java.document.json.JsonObject) MappingException(io.jans.orm.exception.MappingException) DateTimeException(java.time.DateTimeException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) DateTimeParseException(java.time.format.DateTimeParseException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) ISO_INSTANT(java.time.format.DateTimeFormatter.ISO_INSTANT) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.couchbase.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) Sort(com.couchbase.client.java.query.dsl.Sort)

Aggregations

SearchException (io.jans.orm.exception.operation.SearchException)34 Filter (io.jans.orm.search.filter.Filter)19 AuthenticationException (io.jans.orm.exception.AuthenticationException)18 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)16 MappingException (io.jans.orm.exception.MappingException)16 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)14 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)13 DateTimeParseException (java.time.format.DateTimeParseException)9 EntryData (io.jans.orm.model.EntryData)8 JsonObject (com.couchbase.client.java.document.json.JsonObject)7 ArrayList (java.util.ArrayList)7 ParsedKey (io.jans.orm.impl.model.ParsedKey)6 ConvertedExpression (io.jans.orm.couchbase.model.ConvertedExpression)5 ConvertedExpression (io.jans.orm.cloud.spanner.model.ConvertedExpression)4 AttributeData (io.jans.orm.model.AttributeData)4 FilterType (io.jans.orm.search.filter.FilterType)4 ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)4 DateTimeException (java.time.DateTimeException)4 Duration (java.time.Duration)4 Instant (java.time.Instant)4