Search in sources :

Example 1 with BucketMapping

use of io.jans.orm.couchbase.model.BucketMapping in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method updateEntry.

@Override
public boolean updateEntry(String key, List<MutationSpec> mods, Integer expiration) throws UnsupportedOperationException, PersistenceException {
    Instant startTime = OperationDurationUtil.instance().now();
    BucketMapping bucketMapping = connectionProvider.getBucketMappingByKey(key);
    boolean result = updateEntryImpl(bucketMapping, key, mods, expiration);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("Couchbase operation: modify, duration: {}, bucket: {}, key: {}, mods: {}", duration, bucketMapping.getBucketName(), key, mods);
    return result;
}
Also used : Instant(java.time.Instant) Duration(java.time.Duration) BucketMapping(io.jans.orm.couchbase.model.BucketMapping)

Example 2 with BucketMapping

use of io.jans.orm.couchbase.model.BucketMapping 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 3 with BucketMapping

use of io.jans.orm.couchbase.model.BucketMapping in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method authenticateImpl.

private boolean authenticateImpl(final String key, final String password) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    boolean result = false;
    if (password != null) {
        JsonObject entry = lookup(key, null, USER_PASSWORD);
        Object userPasswordObj = entry.get(USER_PASSWORD);
        String userPassword = null;
        if (userPasswordObj instanceof JsonArray) {
            userPassword = ((JsonArray) userPasswordObj).getString(0);
        } else if (userPasswordObj instanceof String) {
            userPassword = (String) userPasswordObj;
        }
        if (userPassword != null) {
            if (persistenceExtension == null) {
                result = PasswordEncryptionHelper.compareCredentials(password, userPassword);
            } else {
                result = persistenceExtension.compareHashedPasswords(password, userPassword);
            }
        }
    }
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    BucketMapping bucketMapping = connectionProvider.getBucketMappingByKey(key);
    OperationDurationUtil.instance().logDebug("Couchbase operation: bind, duration: {}, bucket: {}, key: {}", duration, bucketMapping.getBucketName(), key);
    return result;
}
Also used : JsonArray(com.couchbase.client.java.document.json.JsonArray) Instant(java.time.Instant) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonObject(com.couchbase.client.java.document.json.JsonObject) Duration(java.time.Duration) BucketMapping(io.jans.orm.couchbase.model.BucketMapping)

Example 4 with BucketMapping

use of io.jans.orm.couchbase.model.BucketMapping in project jans by JanssenProject.

the class CouchbaseConnectionProvider method open.

private void open() {
    this.bucketToBaseNameMapping = new HashMap<String, BucketMapping>();
    this.baseNameToBucketMapping = new HashMap<String, BucketMapping>();
    this.cluster = CouchbaseCluster.create(couchbaseEnvironment, servers);
    cluster.authenticate(userName, userPassword);
    // Open required buckets
    for (String bucketName : buckets) {
        String baseNamesProp = props.getProperty(String.format("bucket.%s.mapping", bucketName), "");
        String[] baseNames = StringHelper.split(baseNamesProp, ",");
        Bucket bucket = cluster.openBucket(bucketName);
        BucketMapping bucketMapping = new BucketMapping(bucketName, bucket);
        // Store in separate map to speed up search by base name
        bucketToBaseNameMapping.put(bucketName, bucketMapping);
        for (String baseName : baseNames) {
            baseNameToBucketMapping.put(baseName, bucketMapping);
        }
        if (StringHelper.equalsIgnoreCase(bucketName, defaultBucket)) {
            this.defaultBucketMapping = bucketMapping;
        }
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) BucketMapping(io.jans.orm.couchbase.model.BucketMapping)

Example 5 with BucketMapping

use of io.jans.orm.couchbase.model.BucketMapping in project jans by JanssenProject.

the class CouchbaseConnectionProvider method getBucketMappingByKey.

public BucketMapping getBucketMappingByKey(String key) {
    if ("_".equals(key)) {
        return defaultBucketMapping;
    }
    String[] baseNameParts = key.split("_");
    if (ArrayHelper.isEmpty(baseNameParts)) {
        throw new KeyConversionException("Failed to determine base key part!");
    }
    BucketMapping bucketMapping = baseNameToBucketMapping.get(baseNameParts[0]);
    if (bucketMapping != null) {
        return bucketMapping;
    }
    return defaultBucketMapping;
}
Also used : KeyConversionException(io.jans.orm.exception.KeyConversionException) BucketMapping(io.jans.orm.couchbase.model.BucketMapping)

Aggregations

BucketMapping (io.jans.orm.couchbase.model.BucketMapping)11 Duration (java.time.Duration)8 Instant (java.time.Instant)8 JsonObject (com.couchbase.client.java.document.json.JsonObject)3 ScanConsistency (com.couchbase.client.java.query.consistency.ScanConsistency)3 Bucket (com.couchbase.client.java.Bucket)2 SearchException (io.jans.orm.exception.operation.SearchException)2 JsonArray (com.couchbase.client.java.document.json.JsonArray)1 KeyConversionException (io.jans.orm.exception.KeyConversionException)1