List of usage examples for com.amazonaws.services.simpledb.model DeleteAttributesRequest setDomainName
public void setDomainName(String domainName)
From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java
License:Apache License
/** * SimpleDB?????// www. j a v a 2 s . c om * * @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); } }