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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations