use of org.apache.gora.util.GoraException in project gora by apache.
the class AerospikeStore method put.
/**
* Method to insert the persistent objects with the given key to the aerospike database server.
* In writing the records, the policy defined in the mapping file is used to decide on the
* behaviour of transaction handling.
*
* @param key key of the object
* @param persistent object to be persisted
*/
@Override
public void put(K key, T persistent) throws GoraException {
try {
Key recordKey = getAerospikeKey(key);
List<Field> fields = persistent.getSchema().getFields();
for (int i = 0; i < fields.size(); i++) {
if (!persistent.isDirty(i)) {
continue;
}
Object persistentValue = persistent.get(i);
String mappingBinName = aerospikeParameters.getAerospikeMapping().getBinMapping().get(fields.get(i).name());
if (mappingBinName == null) {
LOG.error("Aerospike mapping for field {}#{} not found. Wrong gora-aerospike-mapping.xml?", persistent.getClass().getName(), fields.get(i).name());
throw new RuntimeException("Aerospike mapping for field [" + persistent.getClass().getName() + "#" + fields.get(i).name() + "] not found. Wrong gora-aerospike-mapping.xml?");
}
Bin bin;
if (persistentValue != null) {
bin = new Bin(mappingBinName, getSerializableValue(persistentValue, fields.get(i).schema()));
} else {
bin = Bin.asNull(mappingBinName);
}
aerospikeClient.put(aerospikeParameters.getAerospikeMapping().getWritePolicy(), recordKey, bin);
}
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class AerospikeStore method initialize.
/**
* {@inheritDoc}
* In initializing the aerospike datastore, read the mapping file, sets the basic
* aerospike specific parameters and creates the client with the user defined policies
*
* @param keyClass key class
* @param persistentClass persistent class
* @param properties properties
*/
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
super.initialize(keyClass, persistentClass, properties);
try {
AerospikeMappingBuilder aerospikeMappingBuilder = new AerospikeMappingBuilder();
aerospikeMappingBuilder.readMappingFile(getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE), keyClass, persistentClass);
aerospikeParameters = new AerospikeParameters(aerospikeMappingBuilder.getAerospikeMapping(), properties, getConf());
ClientPolicy policy = new ClientPolicy();
policy.writePolicyDefault = aerospikeParameters.getAerospikeMapping().getWritePolicy();
policy.readPolicyDefault = aerospikeParameters.getAerospikeMapping().getReadPolicy();
// 'SendKey' property is enabled by default as the key is needed in query execution
policy.readPolicyDefault.sendKey = true;
policy.writePolicyDefault.sendKey = true;
// Set the credentials for servers with restricted access
if (aerospikeParameters.getUsername() != null) {
policy.user = aerospikeParameters.getUsername();
}
if (aerospikeParameters.getPassword() != null) {
policy.password = aerospikeParameters.getPassword();
}
aerospikeClient = new AerospikeClient(policy, aerospikeParameters.getHost(), aerospikeParameters.getPort());
aerospikeParameters.setServerSpecificParameters(aerospikeClient);
aerospikeParameters.validateServerBinConfiguration(persistentClass.getFields());
LOG.info("Aerospike Gora datastore initialized successfully.");
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class AerospikeStore method get.
/**
* {@inheritDoc}
*
* @param key the key of the object
* @param fields the fields required in the object. Pass null, to retrieve all fields
* @return the Object corresponding to the key or null if it cannot be found
*/
@Override
public T get(K key, String[] fields) throws GoraException {
try {
Key recordKey = getAerospikeKey(key);
fields = getFieldsToQuery(fields);
Record record = aerospikeClient.get(aerospikeParameters.getAerospikeMapping().getReadPolicy(), recordKey, fields);
if (record == null) {
return null;
}
return createPersistentInstance(record, fields);
} catch (GoraException e) {
throw e;
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class GoraClientTest method testCorrectness.
@Test
public void testCorrectness() {
Status result = benchmarkClient.insert(Constants.TEST_TABLE, Constants.TEST_KEY_4, INTEGER_DATA);
assertEquals(result, Status.OK);
try {
User user = readRecord(Constants.TEST_KEY_4);
assertEquals(190, sum(user));
} catch (GoraException e) {
LOG.info("There is a problem reading record from the datastore", e.getMessage(), e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class AccumuloStore method fromBytes.
public Object fromBytes(Schema schema, byte[] data) throws IOException {
Schema fromSchema = null;
if (schema.getType() == Type.UNION) {
try {
Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
int unionIndex = decoder.readIndex();
List<Schema> possibleTypes = schema.getTypes();
fromSchema = possibleTypes.get(unionIndex);
Schema effectiveSchema = possibleTypes.get(unionIndex);
if (effectiveSchema.getType() == Type.NULL) {
decoder.readNull();
return null;
} else {
data = decoder.readBytes(null).array();
}
} catch (IOException e) {
LOG.error(e.getMessage());
throw new GoraException("Error decoding union type: ", e);
}
} else {
fromSchema = schema;
}
return fromBytes(encoder, fromSchema, data);
}
Aggregations