Search in sources :

Example 86 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class RedisStore method put.

@Override
public void put(K key, T obj) throws GoraException {
    try {
        if (obj.isDirty()) {
            Schema objectSchema = obj.getSchema();
            List<Schema.Field> fields = objectSchema.getFields();
            RBatch batchInstance = redisInstance.createBatch();
            // update secundary index
            if (isNumericKey()) {
                RScoredSortedSetAsync<Object> secundaryIndex = batchInstance.getScoredSortedSet(generateIndexKey());
                secundaryIndex.addAsync(obtainDoubleValue(key), key);
            } else {
                RLexSortedSetAsync secundaryIndex = batchInstance.getLexSortedSet(generateIndexKey());
                secundaryIndex.addAsync(key.toString());
            }
            if (mode == StorageMode.SINGLEKEY) {
                RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
                fields.forEach((field) -> {
                    Object fieldValue = handler.serializeFieldValue(field.schema(), obj.get(field.pos()));
                    if (fieldValue != null) {
                        map.fastPutAsync(mapping.getFields().get(field.name()), fieldValue);
                    } else {
                        map.fastRemoveAsync(mapping.getFields().get(field.name()));
                    }
                });
            } else {
                for (Schema.Field field : fields) {
                    Object fieldValue = obj.get(field.pos());
                    String redisField = mapping.getFields().get(field.name());
                    RedisType redisType = mapping.getTypes().get(field.name());
                    switch(redisType) {
                        case STRING:
                            RBucketAsync<Object> bucket = batchInstance.getBucket(generateKeyString(redisField, key));
                            bucket.deleteAsync();
                            if (fieldValue != null) {
                                fieldValue = handler.serializeFieldValue(field.schema(), fieldValue);
                                bucket.setAsync(fieldValue);
                            }
                            break;
                        case LIST:
                            RListAsync<Object> rlist = batchInstance.getList(generateKeyString(redisField, key));
                            rlist.deleteAsync();
                            if (fieldValue != null) {
                                List<Object> list = handler.serializeFieldList(field.schema(), fieldValue);
                                rlist.addAllAsync(list);
                            }
                            break;
                        case HASH:
                            RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyString(redisField, key));
                            map.deleteAsync();
                            if (fieldValue != null) {
                                Map<Object, Object> mp = handler.serializeFieldMap(field.schema(), fieldValue);
                                map.putAllAsync(mp);
                            }
                            break;
                        default:
                            throw new AssertionError(redisType.name());
                    }
                }
            }
            batchInstance.execute();
        } else {
            LOG.info("Ignored putting object {} in the store as it is neither " + "new, neither dirty.", new Object[] { obj });
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Schema(org.apache.avro.Schema) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync) GoraException(org.apache.gora.util.GoraException) RBatch(org.redisson.api.RBatch)

Example 87 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class RedisStore method initialize.

/**
 * Initialize the data store by reading the credentials, setting the client's
 * properties up and reading the mapping file. Initialize is called when then
 * the call to {@link org.apache.gora.store.DataStoreFactory#createDataStore}
 * is made.
 *
 * @param keyClass Gora's key class
 * @param persistentClass Persistent class
 * @param properties Configurations for the data store
 * @throws org.apache.gora.util.GoraException Unexpected exception during initialization
 */
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    try {
        super.initialize(keyClass, persistentClass, properties);
        InputStream mappingStream;
        if (properties.containsKey(XML_MAPPING_DEFINITION)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("{} = {}", XML_MAPPING_DEFINITION, properties.getProperty(XML_MAPPING_DEFINITION));
            }
            mappingStream = IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION), (Charset) null);
        } else {
            mappingStream = getClass().getClassLoader().getResourceAsStream(getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE));
        }
        RedisMappingBuilder mappingBuilder = new RedisMappingBuilder(this);
        mapping = mappingBuilder.readMapping(mappingStream);
        Config config = new Config();
        String storage = getConf().get(GORA_REDIS_STORAGE, properties.getProperty(GORA_REDIS_STORAGE));
        mode = StorageMode.valueOf(storage);
        String modeString = getConf().get(GORA_REDIS_MODE, properties.getProperty(GORA_REDIS_MODE));
        ServerMode connectionMode = ServerMode.valueOf(modeString);
        String name = getConf().get(GORA_REDIS_MASTERNAME, properties.getProperty(GORA_REDIS_MASTERNAME));
        String readm = getConf().get(GORA_REDIS_READMODE, properties.getProperty(GORA_REDIS_READMODE));
        // Override address in tests
        String[] hosts = getConf().get(GORA_REDIS_ADDRESS, properties.getProperty(GORA_REDIS_ADDRESS)).split(",");
        for (int indexHosts = 0; indexHosts < hosts.length; indexHosts++) {
            hosts[indexHosts] = PREFIX + hosts[indexHosts];
        }
        switch(connectionMode) {
            case SINGLE:
                config.useSingleServer().setAddress(hosts[0]).setDatabase(mapping.getDatabase());
                break;
            case CLUSTER:
                config.useClusterServers().addNodeAddress(hosts);
                break;
            case REPLICATED:
                config.useReplicatedServers().addNodeAddress(hosts).setDatabase(mapping.getDatabase());
                break;
            case SENTINEL:
                config.useSentinelServers().setMasterName(name).setReadMode(ReadMode.valueOf(readm)).addSentinelAddress(hosts);
                break;
            default:
                throw new AssertionError(connectionMode.name());
        }
        redisInstance = Redisson.create(config);
        if (autoCreateSchema && !schemaExists()) {
            createSchema();
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) InputStream(java.io.InputStream) Config(org.redisson.config.Config) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ServerMode(org.apache.gora.redis.util.ServerMode)

Example 88 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class RedisStore method delete.

@Override
public boolean delete(K key) throws GoraException {
    try {
        RBatch batchInstance = redisInstance.createBatch();
        // update secundary index
        if (isNumericKey()) {
            RScoredSortedSetAsync<Object> secundaryIndex = batchInstance.getScoredSortedSet(generateIndexKey());
            secundaryIndex.removeAsync(key);
        } else {
            RLexSortedSetAsync secundaryIndex = batchInstance.getLexSortedSet(generateIndexKey());
            secundaryIndex.removeAsync(key.toString());
        }
        if (mode == StorageMode.SINGLEKEY) {
            RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
            RFuture<Boolean> deleteAsync = map.deleteAsync();
            batchInstance.execute();
            return deleteAsync.get();
        } else {
            batchInstance.execute();
            return redisInstance.getKeys().deleteByPattern(generateKeyStringBase(key) + WILDCARD) > 0;
        }
    } catch (Exception ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) RBatch(org.redisson.api.RBatch) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync)

Example 89 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class RethinkDBStore method get.

/**
 * {@inheritDoc}
 */
@Override
public T get(K key, String[] fields) throws GoraException {
    try {
        boolean isExists = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).getAll(key).count().run(connection, Boolean.class).first();
        if (isExists) {
            String[] dbFields = getFieldsToQuery(fields);
            MapObject<String, Object> document = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).get(key).run(connection, MapObject.class).first();
            return convertRethinkDBDocToAvroBean(document, dbFields);
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) MapObject(com.rethinkdb.model.MapObject) MapObject(com.rethinkdb.model.MapObject) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 90 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class RethinkDBStore method convertDocFieldToAvroUnion.

private Object convertDocFieldToAvroUnion(final Schema fieldSchema, final RethinkDBMapping.DocumentFieldType storeType, final Schema.Field field, final String docf, final MapObject<String, Object> doc) throws GoraException {
    Object result;
    Schema.Type type0 = fieldSchema.getTypes().get(0).getType();
    Schema.Type type1 = fieldSchema.getTypes().get(1).getType();
    if (!type0.equals(type1) && (type0.equals(Schema.Type.NULL) || type1.equals(Schema.Type.NULL))) {
        Schema innerSchema = null;
        if (type0.equals(Schema.Type.NULL)) {
            innerSchema = fieldSchema.getTypes().get(1);
        } else {
            innerSchema = fieldSchema.getTypes().get(0);
        }
        LOG.debug("Load from ODocument (UNION), schemaType:{}, docField:{}, storeType:{}", new Object[] { innerSchema.getType(), docf, storeType });
        result = convertDocFieldToAvroField(innerSchema, storeType, field, docf, doc);
    } else {
        throw new GoraException("RethinkDBStore only supports Union of two types field.");
    }
    return result;
}
Also used : GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) MapObject(com.rethinkdb.model.MapObject)

Aggregations

GoraException (org.apache.gora.util.GoraException)174 IOException (java.io.IOException)119 ArrayList (java.util.ArrayList)30 Schema (org.apache.avro.Schema)25 SQLException (java.sql.SQLException)17 HashMap (java.util.HashMap)17 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 PreparedStatement (java.sql.PreparedStatement)11 Map (java.util.Map)11 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)9 SolrServerException (org.apache.solr.client.solrj.SolrServerException)9 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)8 AccumuloException (org.apache.accumulo.core.client.AccumuloException)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)8 Field (org.apache.avro.Schema.Field)8 KuduException (org.apache.kudu.client.KuduException)8 ResultSet (com.datastax.driver.core.ResultSet)7 SimpleStatement (com.datastax.driver.core.SimpleStatement)7 SAXBuilder (org.jdom.input.SAXBuilder)7 InputStream (java.io.InputStream)6