Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * See the COPYRIGHT.txt file distributed with this work for information
   * regarding copyright ownership.  Some portions may be licensed
   * to Red Hat, Inc. under one or more contributor license agreements.
   * 
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  * 
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  * 
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301 USA.
  */
 package org.teiid.resource.adapter.simpledb;
 
 import java.util.*;
 
 
 
 
 public class SimpleDBConnectionImpl extends BasicConnection implements SimpleDBConnection {
     private AmazonSimpleDBClient client;
     private List<Stringdomains;
 
     public SimpleDBConnectionImpl(String accessKeyString secretAccessKey) {
         this. = new AmazonSimpleDBClient(new BasicAWSCredentials(accessKeysecretAccessKey));
     }
 
     public void close() throws ResourceException {
 
     }
 
     @Override
     public void createDomain(String domainNamethrows TranslatorException {
         try {
             this..createDomain(new CreateDomainRequest(domainName));
         } catch (AmazonServiceException e) {
             throw new TranslatorException(e);
         } catch (AmazonClientException e) {
             throw new TranslatorException(e);            
         }
     }
 
     @Override
     public void deleteDomain(String domainNamethrows TranslatorException {
         try {
             this..deleteDomain(new DeleteDomainRequest(domainName));
             if (this..contains(domainName)) {
                 this..remove(domainName);
             }
         } catch (AmazonServiceException e) {
             throw new TranslatorException(e);
         } catch (AmazonClientException e) {
             throw new TranslatorException(e);            
         }
     }
 
     @Override
     public List<StringgetDomains() throws TranslatorException {
         return .listDomains().getDomainNames();
     }
 
     @Override
     public Set<SimpleDBAttributegetAttributeNames(String domainNamethrows TranslatorException {
         DomainMetadataRequest domainMetadataRequest = new DomainMetadataRequest(domainName);
         DomainMetadataResult metadataResult = .domainMetadata(domainMetadataRequest);
         int attributesCount = metadataResult.getAttributeNameCount();
         SelectResult selectResult = .select(new SelectRequest("SELECT * FROM " + domainName)); //$NON-NLS-1$
         return getAttributeNamesFromSelectResult(selectResultattributesCount);
     }

    
Removes item with given ItemName from domain

Parameters:
domainName
itemName
 
     @Override
     public int performDelete(String domainNameString selectExpressionthrows TranslatorException {
         try {
            List<DeletableItemdeleteItems = new ArrayList<DeletableItem>();
            int count = 0;
            String nextToken = null;
            do {
                SelectResult result = performSelect(selectExpressionnextToken);
                nextToken = result.getNextToken();
                Iterator<Itemiter = result.getItems().iterator();
                while (iter.hasNext()) {
                    Item item = iter.next();
                    deleteItems.add(new DeletableItem(item.getName(), null));
                    count++;
                    if (count%25 == 0) {
                        BatchDeleteAttributesRequest request = new BatchDeleteAttributesRequest(domainNamedeleteItems);
                        this..batchDeleteAttributes(request);
                        deleteItems.clear();
                    }
                }
                // http://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/SDB_API_BatchDeleteAttributes.html
                // 25 limit we may need to batch; but if batch atomicity is gone
                if (!deleteItems.isEmpty()) {
                    BatchDeleteAttributesRequest request = new BatchDeleteAttributesRequest(domainNamedeleteItems);
                    this..batchDeleteAttributes(request);
                }
            } while (nextToken != null);
            return count;
        } catch (AmazonServiceException e) {
            throw new TranslatorException(e);
        } catch (AmazonClientException e) {
            throw new TranslatorException(e);
        }
    }

    
Performs select expression. This expression must be in format which is understandable to SimpleDB database

Parameters:
selectExpression
columns
Returns:
Iterator of List<String> results
    @Override
    public SelectResult performSelect(String selectExpressionString nextTokenthrows TranslatorException{
        try {
            SelectRequest selectRequest = new SelectRequest(selectExpression);
            if (nextToken != null) {
                selectRequest.setNextToken(nextToken);
            }
            selectRequest.setConsistentRead(true);
            return .select(selectRequest);
        } catch (AmazonServiceException e) {
            throw new TranslatorException(e);
        } catch (AmazonClientException e) {
            throw new TranslatorException(e);
        }
    }

    
Performs update on given domain and items

Parameters:
domainName
items
    @Override
    public int performUpdate(String domainNameMap<StringObjectupdateAttributesString selectExpressionthrows TranslatorException {
        try {
            List<ReplaceableAttributeattributes = new ArrayList<ReplaceableAttribute>();
            for (Map.Entry<StringObjectcolumn : updateAttributes.entrySet()) {
                addAttribute(column.getKey(), column.getValue(), attributes);
            }
            
            List<ReplaceableItemupdateItems = new ArrayList<ReplaceableItem>();
            int count = 0;
            String nextToken = null;
            do {
                SelectResult result = performSelect(selectExpressionnextToken);
                nextToken = result.getNextToken();
                Iterator<Itemiter = result.getItems().iterator();
                while (iter.hasNext()) {
                    Item item = iter.next();                                
                    updateItems.add(new ReplaceableItem(item.getName(), attributes));
                    count++;
                    if (count%25 == 0) {
                        executeBatch(domainNameupdateItems);
                        updateItems.clear();
                    }
                }
                executeBatch(domainNameupdateItems);
            } while (nextToken != null);
            return count;
        } catch (AmazonServiceException e) {
            throw new TranslatorException(e);
        } catch (AmazonClientException e) {
            throw new TranslatorException(e);
        }        
    }

    
Inserts item into given domain.

Parameters:
domainName
itemName
columnsMap
Returns:
    @Override
    public int performInsert(String domainNameList<ColumncolumnsIterator<? extends List<?>> valueListthrows TranslatorException {
        try {
            if (this. == null) {
                this. = getDomains();
            }
            
            if (!this..contains(domainName)) {
                createDomain(domainName);
            }
            
            int count = 0;
            List<ReplaceableIteminsertItems = new ArrayList<ReplaceableItem>();
            while(valueList.hasNext()) {
                List<?> values = valueList.next();
                List<ReplaceableAttributeattributes = new ArrayList<ReplaceableAttribute>();
                String itemName = null;
                for (int i = 0; i < columns.size(); i++) {
                    Column column = columns.get(i);
                    if (SQLStringVisitor.getRecordName(column).equals(.)) {
                        itemName = (String)values.get(i);
                    }
                    else {
                        addAttribute(SQLStringVisitor.getRecordName(column), SimpleDBDataTypeManager.convertToSimpleDBType(values.get(i), column.getJavaType()), attributes);
                    }
                }
                if (itemName == null) {
                    throw new TranslatorException("ItemName() column value is not specified, it can not be null. Please provide a value.");
                }
                insertItems.add(new ReplaceableItem(itemNameattributes));
                count++;
                if (count%25 == 0) {
                    executeBatch(domainNameinsertItems);
                    insertItems.clear();
                }
            }
            // http://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/SDB_API_BatchPutAttributes.html
            // TODO: 25 limit we may need to batch; but if batch atomicity is gone
            executeBatch(domainNameinsertItems);
            return count;
        } catch (AmazonServiceException e) {
            throw new TranslatorException(e);
        } catch (AmazonClientException e) {
            throw new TranslatorException(e);
        }
    }
    private void executeBatch(String domainNameList<ReplaceableIteminsertItems) {
        if (!insertItems.isEmpty()) {
            BatchPutAttributesRequest request = new BatchPutAttributesRequest(domainNameinsertItems);
            this..batchPutAttributes(request);
        }
    }
    
    private void addAttribute(String nameObject valueList<ReplaceableAttributeattributes) {
        if (value.getClass().isArray()) { 
            String[] values = (String[])value;
            for (int i = 0; i < values.lengthi++) {
                addAttribute(namevalues[i], attributes);
            }
        }
        else {
            ReplaceableAttribute attribute = new ReplaceableAttribute();
            attribute.setName(name);
            attribute.setReplace(true);            
            attribute.setValue((String)value);
            attributes.add(attribute);
        }        
    }
    
    private Set<SimpleDBAttributegetAttributeNamesFromSelectResult(SelectResult selectResultint attributesCount) {
        Set<SimpleDBAttributeattributes = new LinkedHashSet<SimpleDBAttribute>();
        Iterator<ItemitemsIterator = selectResult.getItems().iterator();
        while (attributes.size() < attributesCount) {
            Item item = itemsIterator.next();
            Map<StringList<String>> valueMap = createAttributeMap(item.getAttributes());
            for (String attributeName : valueMap.keySet()) {
                List<Stringvalues = valueMap.get(attributeName);
                attributes.add(new SimpleDBAttribute(attributeNamevalues.size() > 1));
            }
        }
        return attributes;
    }
    private Map<StringList<String>> createAttributeMap(List<Attributeattributes) {
        Map<StringList<String>> map = new HashMap<StringList<String>>();
        for (Attribute attribute : attributes) {
            if (map.get(attribute.getName()) == null) {
                List<Stringlist = new ArrayList<String>();
                list.add(attribute.getValue());
                map.put(attribute.getName(), list);
            } else {
                map.get(attribute.getName()).add(attribute.getValue());
            }
        }
        return map;
    }
New to GrepCode? Check out our FAQ X