List of usage examples for com.amazonaws.services.simpledb.model UpdateCondition setValue
public void setValue(String value)
The value of an attribute.
From source file:com.aipo.aws.simpledb.SimpleDB.java
License:Open Source License
private static Integer counterJob(String domain) { AmazonSimpleDB client = getClient(); GetAttributesRequest getAttributesRequest = new GetAttributesRequest(); getAttributesRequest.setDomainName(DEFAULT_COUNTER_DOMAIN); getAttributesRequest.setItemName(domain); getAttributesRequest.setConsistentRead(true); Integer count = null;/*from w w w.ja va 2 s . c om*/ try { GetAttributesResult attributes = client.getAttributes(getAttributesRequest); List<Attribute> list = attributes.getAttributes(); for (Attribute item : list) { if ("c".equals(item.getName())) { try { count = Integer.valueOf(item.getValue()); } catch (Throwable ignore) { } } } } catch (Throwable t) { t.printStackTrace(); } if (count == null) { CreateDomainRequest createDomainRequest = new CreateDomainRequest(DEFAULT_COUNTER_DOMAIN); client.createDomain(createDomainRequest); count = 0; } int next = count + 1; PutAttributesRequest putAttributesRequest = new PutAttributesRequest(); putAttributesRequest.setDomainName(DEFAULT_COUNTER_DOMAIN); putAttributesRequest.setItemName(domain); List<ReplaceableAttribute> attr = new ArrayList<ReplaceableAttribute>(); attr.add(new ReplaceableAttribute("c", String.valueOf(next), true)); putAttributesRequest.setAttributes(attr); UpdateCondition updateCondition = new UpdateCondition(); if (next == 1) { updateCondition.setExists(false); updateCondition.setName("c"); } else { updateCondition.setExists(true); updateCondition.setName("c"); updateCondition.setValue(String.valueOf(count)); } putAttributesRequest.setExpected(updateCondition); client.putAttributes(putAttributesRequest); return next; }
From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java
License:Apache License
/** * SimpleDB?????// ww w . jav a2 s . c o m * * @param object * {@link SimpleDBDomain}????POJO * {@link SimpleDBVersionAttribute} * ??????????????<a href= * "http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/ConditionalPut.html" * >Conditional Put</a>???? */ public <T> void save(T object) { Class<?> clazz = object.getClass(); String domainName = getDomainName(clazz); Field itemNameField = this.reflector.findItemNameField(clazz); if (itemNameField == null) { throw new SimpleDBMapperException(object + "@SimpleDBItemName????"); } String itemName = null; itemName = this.reflector.encodeItemNameAsSimpleDBFormat(object, itemNameField); Set<Field> allFields = this.reflector.listAllFields(clazz); Map<String, Object> attributeMap = new HashMap<String, Object>(); List<S3BlobReference> blobList = new ArrayList<S3BlobReference>(); for (Field field : allFields) { try { String attributeName = this.reflector.getAttributeName(field); if (attributeName != null) { if (this.reflector.isAttributeField(field)) { attributeMap.put(attributeName, field.get(object)); } else if (this.reflector.isBlobField(field)) { String s3BucketName = this.reflector.getS3BucketName(clazz); String s3KeyPrefix = this.reflector.getS3KeyPrefix(clazz); String s3ContentType = this.reflector.getS3ContentType(field); // FIXME S3BlobReference s3BlobRef = new S3BlobReference(attributeName, s3BucketName, s3KeyPrefix, s3ContentType, field.get(object)); blobList.add(s3BlobRef); } } } catch (Exception e) { throw new SimpleDBMapperException(e); } } List<String> nullKeys = new ArrayList<String>(); List<ReplaceableAttribute> replacableAttrs = new ArrayList<ReplaceableAttribute>(); // SimpleDBAttribute for (Map.Entry<String, Object> entry : attributeMap.entrySet()) { String sdbAttributeName = entry.getKey(); Object sdbValue = entry.getValue(); if (sdbValue == null) { nullKeys.add(sdbAttributeName);// ? } else if (sdbValue instanceof Set) { // Set Set<?> c = (Set<?>) sdbValue; for (Object val : c) { replacableAttrs.add(new ReplaceableAttribute(sdbAttributeName, this.reflector.encodeObjectAsSimpleDBFormat(val), true)); } } else { replacableAttrs.add(new ReplaceableAttribute(sdbAttributeName, this.reflector.encodeObjectAsSimpleDBFormat(sdbValue), true)); } } // SimpleDBBlob // Upload?Blob? List<S3Task> uploadTasks = new ArrayList<S3Task>(); for (S3BlobReference s3BlobRef : blobList) { String bucketName = s3BlobRef.getS3BucketName(); if (bucketName == null) { throw new SimpleDBMapperException("Blob??s3BucketName????"); } StringBuilder s3Key = new StringBuilder(); String prefix = s3BlobRef.getPrefix(); if (prefix == null) { throw new SimpleDBMapperException("Blob?prefix?null??????"); } prefix = prefix.trim(); s3Key.append(prefix); if (!prefix.isEmpty() && !prefix.endsWith("/")) { s3Key.append("/"); } s3Key.append(itemName).append("/").append(s3BlobRef.getAttributeName()); Object blobObject = s3BlobRef.getObject(); if (blobObject == null) { nullKeys.add(s3BlobRef.getAttributeName()); // ???Delete Object? // FIXME ????????SDB???DeleteAttribute? this.s3.deleteObject(bucketName, s3Key.toString()); } else { // ?Blob????S3?????????????????????? InputStream input = null; if (blobObject instanceof String) { // Blob?String // FIXME encoding??? input = new ByteArrayInputStream(((String) blobObject).getBytes(Charset.forName("UTF-8"))); } else if (blobObject.getClass().getSimpleName().equals("byte[]")) { // Blob?Byte? input = new ByteArrayInputStream((byte[]) blobObject); } else { throw new SimpleDBMapperException( "Blob?????String????byte[]????"); } S3Task uploadTask = new S3Task(this.s3, s3BlobRef.getAttributeName(), input, bucketName, s3Key.toString(), s3BlobRef.getContentType()); uploadTasks.add(uploadTask); } } // PutAttribute PutAttributesRequest req = new PutAttributesRequest(); req.setDomainName(domainName); req.setItemName(itemName); // Version??object???Conditional PUT? Long nowVersion = System.currentTimeMillis(); Field versionField = this.reflector.findVersionAttributeField(clazz); if (versionField != null) { try { Object versionObject = versionField.get(object); String versionAttributeName = versionField.getAnnotation(SimpleDBVersionAttribute.class) .attributeName(); if (versionObject != null) { if (versionObject instanceof Long) { Long currentVersion = (Long) versionObject; UpdateCondition expected = new UpdateCondition(); expected.setName(versionAttributeName); expected.setValue(currentVersion.toString()); req.setExpected(expected); } else { throw new SimpleDBMapperException( "version?Long???????" + versionField); } } replacableAttrs.add(new ReplaceableAttribute(versionAttributeName, nowVersion.toString(), true)); } catch (Exception e) { throw new SimpleDBMapperException("object?version??: " + object, e); } } // S3?? List<S3TaskResult> taskFailures = new ArrayList<S3TaskResult>(); ExecutorService executor = Executors.newFixedThreadPool(this.config.geS3AccessThreadPoolSize()); try { List<Future<S3TaskResult>> futures = executor.invokeAll(uploadTasks); for (Future<S3TaskResult> future : futures) { S3TaskResult result = future.get(); // SimpleDB????? replacableAttrs.add(new ReplaceableAttribute(result.getSimpleDBAttributeName(), result.toSimpleDBAttributeValue(), true)); if (!result.isSuccess()) { // Upload taskFailures.add(result); } } } catch (Exception e) { throw new SimpleDBMapperS3HandleException("S3??", e); } // UploadTask??? if (!taskFailures.isEmpty()) { throw new SimpleDBMapperS3HandleException(taskFailures); } // SDB?PUT req.setAttributes(replacableAttrs); this.sdb.putAttributes(req); // version if (versionField != null) { try { versionField.set(object, nowVersion); } catch (Exception ignore) { throw new SimpleDBMapperException("version??", ignore); } } // DeleteAttribute if (!nullKeys.isEmpty()) { DeleteAttributesRequest delReq = new DeleteAttributesRequest(); delReq.setDomainName(domainName); delReq.setItemName(itemName); Collection<Attribute> delAttrs = new ArrayList<Attribute>(nullKeys.size()); for (String nullKey : nullKeys) { delAttrs.add(new Attribute(nullKey, null)); } delReq.setAttributes(delAttrs); this.sdb.deleteAttributes(delReq); } }
From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java
License:Apache License
/** * SimpleDB????//from ww w.j a v a 2s. c o m * * @param object * {@link SimpleDBDomain}????POJO * {@link SimpleDBVersionAttribute} * ??????????????<a href= * "http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/ConditionalDelete.html" * >Conditional Delete</a>???? */ public void delete(Object object) { String domainName = this.reflector.getDomainName(object.getClass()); Field itemNameField = this.reflector.findItemNameField(object.getClass()); String itemName = this.reflector.encodeItemNameAsSimpleDBFormat(object, itemNameField); // S3 Blob GetAttributesResult results = this.sdb.getAttributes(new GetAttributesRequest(domainName, itemName)); List<Attribute> sdbAllAttrs = results.getAttributes(); Set<Field> blobFields = this.reflector.findBlobFields(object.getClass()); List<S3TaskResult> s3TaskResults = new ArrayList<S3TaskResult>(); for (Field field : blobFields) { SimpleDBBlob blobAnnon = field.getAnnotation(SimpleDBBlob.class); String attributeName = blobAnnon.attributeName(); for (Attribute attr : sdbAllAttrs) { if (attr.getName().equals(attributeName)) { S3TaskResult taskResult = new S3TaskResult(Operation.DELETE, attributeName, null, null); taskResult.setSimpleDBAttributeValue(attr.getValue()); s3TaskResults.add(taskResult); } } } DeleteAttributesRequest req = new DeleteAttributesRequest(domainName, itemName); // version?????Conditional Delete Field versionField = this.reflector.findVersionAttributeField(object.getClass()); if (versionField != null) { try { Object versionObject = versionField.get(object); String versionAttributeName = versionField.getAnnotation(SimpleDBVersionAttribute.class) .attributeName(); if (versionObject != null) { if (versionObject instanceof Long) { Long currentVersion = (Long) versionObject; UpdateCondition expected = new UpdateCondition(); expected.setName(versionAttributeName); expected.setValue(currentVersion.toString()); req.setExpected(expected); } else { throw new SimpleDBMapperException( "version?Long???????" + versionField); } } } catch (Exception e) { throw new SimpleDBMapperException("object?version??: " + object, e); } } this.sdb.deleteAttributes(req); // S3 for (S3TaskResult s3TaskResult : s3TaskResults) { this.s3.deleteObject(s3TaskResult.getBucketName(), s3TaskResult.getKey()); } }
From source file:squash.booking.lambdas.core.OptimisticPersister.java
License:Apache License
@Override public int put(String itemName, Optional<Integer> version, ReplaceableAttribute attribute) throws Exception { if (!initialised) { throw new IllegalStateException("The optimistic persister has not been initialised"); }/* w ww. ja v a2s.co m*/ logger.log("About to add attrbutes to simpledb item: " + itemName); AmazonSimpleDB client = getSimpleDBClient(); // Check the put will not take us over the maximum number of attributes: // N.B. if (replace == true) then this check could be over-eager, but not // worth refining it, since this effectively just alters the limit by one. ImmutablePair<Optional<Integer>, Set<Attribute>> versionedAttributes = get(itemName); if (versionedAttributes.left.isPresent()) { logger.log("Retrieved versioned attributes(Count: " + versionedAttributes.right.size() + ") have version number: " + versionedAttributes.left.get()); } else { // There should be no attributes in this case. logger.log("Retrieved versioned attributes(Count: " + versionedAttributes.right.size() + ") have no version number"); } Boolean tooManyAttributes = versionedAttributes.right.size() >= maxNumberOfAttributes; if (tooManyAttributes && !attribute.getValue().startsWith("Inactive")) { // We allow puts to inactivate attributes even when on the limit - // otherwise we could never delete when we're on the limit. logger.log("Cannot create attribute - the maximum number of attributes already exists (" + maxNumberOfAttributes + ") so throwing a 'Database put failed - too many attributes' exception"); throw new Exception("Database put failed - too many attributes"); } // Do a conditional put - so we don't overwrite someone else's attributes UpdateCondition updateCondition = new UpdateCondition(); updateCondition.setName(versionAttributeName); ReplaceableAttribute versionAttribute = new ReplaceableAttribute(); versionAttribute.setName(versionAttributeName); versionAttribute.setReplace(true); // Update will proceed unless the version number has changed if (version.isPresent()) { // A version number attribute exists - so it should be unchanged updateCondition.setValue(Integer.toString(version.get())); // Bump up our version number attribute versionAttribute.setValue(Integer.toString(version.get() + 1)); } else { // A version number attribute did not exist - so it still should not updateCondition.setExists(false); // Set initial value for our version number attribute versionAttribute.setValue("0"); } List<ReplaceableAttribute> replaceableAttributes = new ArrayList<>(); replaceableAttributes.add(versionAttribute); // Add the new attribute replaceableAttributes.add(attribute); PutAttributesRequest simpleDBPutRequest = new PutAttributesRequest(simpleDbDomainName, itemName, replaceableAttributes, updateCondition); try { client.putAttributes(simpleDBPutRequest); } catch (AmazonServiceException ase) { if (ase.getErrorCode().contains("ConditionalCheckFailed")) { // Someone else has mutated an attribute since we read them. This is // likely to be rare, and a retry should almost always succeed. However, // we leave it to clients of this class to retry the call if they wish, // as the new mutation may mean they no longer want to do the put. logger.log("Caught AmazonServiceException for ConditionalCheckFailed whilst creating" + " attribute(s) so throwing as 'Database put failed' instead"); throw new Exception("Database put failed - conditional check failed"); } throw ase; } logger.log("Created attribute(s) in simpledb"); return Integer.parseInt(versionAttribute.getValue()); }
From source file:squash.booking.lambdas.core.OptimisticPersister.java
License:Apache License
@Override public void delete(String itemName, Attribute attribute) throws Exception { if (!initialised) { throw new IllegalStateException("The optimistic persister has not been initialised"); }/*from w w w . j a va 2s.c o m*/ logger.log("About to delete attribute from simpledb item: " + itemName); AmazonSimpleDB client = getSimpleDBClient(); // We retry the delete if necessary if we get a // ConditionalCheckFailed exception, i.e. if someone else modifies the // database between us reading and writing it. RetryHelper.DoWithRetries(() -> { try { // Get existing attributes (and version number), via consistent // read: ImmutablePair<Optional<Integer>, Set<Attribute>> versionedAttributes = get(itemName); if (!versionedAttributes.left.isPresent()) { logger.log( "A version number attribute did not exist - this means no attributes exist, so we have nothing to delete."); return null; } if (!versionedAttributes.right.contains(attribute)) { logger.log("The attribute did not exist - so we have nothing to delete."); return null; } // Since it seems impossible to update the version number while // deleting an attribute, we first mark the attribute as inactive, // and then delete it. ReplaceableAttribute inactiveAttribute = new ReplaceableAttribute(); inactiveAttribute.setName(attribute.getName()); inactiveAttribute.setValue("Inactive" + attribute.getValue()); inactiveAttribute.setReplace(true); put(itemName, versionedAttributes.left, inactiveAttribute); // Now we can safely delete the attribute, as other readers will now // ignore it. UpdateCondition updateCondition = new UpdateCondition(); updateCondition.setName(inactiveAttribute.getName()); updateCondition.setValue(inactiveAttribute.getValue()); // Update will proceed unless the attribute no longer exists updateCondition.setExists(true); Attribute attributeToDelete = new Attribute(); attributeToDelete.setName(inactiveAttribute.getName()); attributeToDelete.setValue(inactiveAttribute.getValue()); List<Attribute> attributesToDelete = new ArrayList<>(); attributesToDelete.add(attributeToDelete); DeleteAttributesRequest simpleDBDeleteRequest = new DeleteAttributesRequest(simpleDbDomainName, itemName, attributesToDelete, updateCondition); client.deleteAttributes(simpleDBDeleteRequest); logger.log("Deleted attribute from simpledb"); return null; } catch (AmazonServiceException ase) { if (ase.getErrorCode().contains("AttributeDoesNotExist")) { // Case of trying to delete an attribute that no longer exists - // that's ok - it probably just means more than one person was // trying to delete the attribute at once. So swallow this // exception logger.log( "Caught AmazonServiceException for AttributeDoesNotExist whilst deleting attribute so" + " swallowing and continuing"); return null; } else { throw ase; } } }, Exception.class, Optional.of("Database put failed - conditional check failed"), logger); }
From source file:wwutil.jsoda.SimpleDBService.java
License:Mozilla Public License
private UpdateCondition buildExpectedValue(String modelName, String expectedField, Object expectedValue, boolean expectedExists) throws Exception { if (expectedValue == null) throw new IllegalArgumentException("ExpectedValue cannot be null."); String attrName = jsoda.getFieldAttrMap(modelName).get(expectedField); String fieldValue = DataUtil.encodeValueToAttrStr(expectedValue, jsoda.getField(modelName, expectedField).getType()); UpdateCondition cond = new UpdateCondition(); cond.setExists(expectedExists);/* ww w.j av a 2 s . c o m*/ cond.setName(attrName); if (expectedExists) { cond.setValue(fieldValue); } return cond; }