Example usage for com.amazonaws.services.dynamodbv2.model AttributeValue getB

List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeValue getB

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model AttributeValue getB.

Prototype


public java.nio.ByteBuffer getB() 

Source Link

Document

An attribute of type Binary.

Usage

From source file:com.amazon.janusgraph.diskstorage.dynamodb.builder.AbstractBuilder.java

License:Open Source License

protected StaticBuffer decodeValue(final AttributeValue val) {
    if (null == val) {
        return null;
    }//from   w w w  .  j  a  va2s.  com
    // Dynamo does not allow empty binary values, so we use a placeholder
    // for empty values
    if (Constants.EMPTY_BUFFER_PLACEHOLDER.equals(val.getS())) {
        return BufferUtil.emptyBuffer();
    }
    return StaticArrayBuffer.of(val.getB());
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.DynamoDbDelegate.java

License:Open Source License

/**
 * Helper method that can clone an Attribute Value
 *
 * @param val the AttributeValue to copy
 * @param sourceDestinationMap used to avoid loops by keeping track of references
 * @return a copy of val/*  w ww .  j a  v a  2  s  . c o  m*/
 */
public static AttributeValue clone(final AttributeValue val,
        final IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap) {
    if (val == null) {
        return null;
    }

    if (sourceDestinationMap.containsKey(val)) {
        return sourceDestinationMap.get(val);
    }

    final AttributeValue clonedVal = new AttributeValue();
    sourceDestinationMap.put(val, clonedVal);
    if (val.getN() != null) {
        clonedVal.setN(val.getN());
    } else if (val.getS() != null) {
        clonedVal.setS(val.getS());
    } else if (val.getB() != null) {
        clonedVal.setB(val.getB());
    } else if (val.getNS() != null) {
        clonedVal.setNS(val.getNS());
    } else if (val.getSS() != null) {
        clonedVal.setSS(val.getSS());
    } else if (val.getBS() != null) {
        clonedVal.setBS(val.getBS());
    } else if (val.getBOOL() != null) {
        clonedVal.setBOOL(val.getBOOL());
    } else if (val.getNULL() != null) {
        clonedVal.setNULL(val.getNULL());
    } else if (val.getL() != null) {
        final List<AttributeValue> list = new ArrayList<>(val.getL().size());
        for (AttributeValue listItemValue : val.getL()) {
            if (!sourceDestinationMap.containsKey(listItemValue)) {
                sourceDestinationMap.put(listItemValue, clone(listItemValue, sourceDestinationMap));
            }
            list.add(sourceDestinationMap.get(listItemValue));
        }
        clonedVal.setL(list);
    } else if (val.getM() != null) {
        final Map<String, AttributeValue> map = new HashMap<>(val.getM().size());
        for (Entry<String, AttributeValue> pair : val.getM().entrySet()) {
            if (!sourceDestinationMap.containsKey(pair.getValue())) {
                sourceDestinationMap.put(pair.getValue(), clone(pair.getValue(), sourceDestinationMap));
            }
            map.put(pair.getKey(), sourceDestinationMap.get(pair.getValue()));
        }
        clonedVal.setM(map);
    }
    return clonedVal;
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.DynamoDbDelegate.java

License:Open Source License

/**Calculate attribute value size*/
private static int calculateAttributeSizeInBytes(final AttributeValue value) {
    int attrValSize = 0;
    if (value == null) {
        return attrValSize;
    }/* ww  w .  ja  va 2 s  .  com*/

    if (value.getB() != null) {
        final ByteBuffer b = value.getB();
        attrValSize += b.remaining();
    } else if (value.getS() != null) {
        final String s = value.getS();
        attrValSize += s.getBytes(UTF8).length;
    } else if (value.getN() != null) {
        attrValSize += MAX_NUMBER_OF_BYTES_FOR_NUMBER;
    } else if (value.getBS() != null) {
        final List<ByteBuffer> bs = value.getBS();
        for (ByteBuffer b : bs) {
            if (b != null) {
                attrValSize += b.remaining();
            }
        }
    } else if (value.getSS() != null) {
        final List<String> ss = value.getSS();
        for (String s : ss) {
            if (s != null) {
                attrValSize += s.getBytes(UTF8).length;
            }
        }
    } else if (value.getNS() != null) {
        final List<String> ns = value.getNS();
        for (String n : ns) {
            if (n != null) {
                attrValSize += MAX_NUMBER_OF_BYTES_FOR_NUMBER;
            }
        }
    } else if (value.getBOOL() != null) {
        attrValSize += 1;
    } else if (value.getNULL() != null) {
        attrValSize += 1;
    } else if (value.getM() != null) {
        for (Map.Entry<String, AttributeValue> entry : value.getM().entrySet()) {
            attrValSize += entry.getKey().getBytes(UTF8).length;
            attrValSize += calculateAttributeSizeInBytes(entry.getValue());
            attrValSize += BASE_LOGICAL_SIZE_OF_NESTED_TYPES;
        }
        attrValSize += LOGICAL_SIZE_OF_EMPTY_DOCUMENT;
    } else if (value.getL() != null) {
        final List<AttributeValue> list = value.getL();
        for (Integer i = 0; i < list.size(); i++) {
            attrValSize += calculateAttributeSizeInBytes(list.get(i));
            attrValSize += BASE_LOGICAL_SIZE_OF_NESTED_TYPES;
        }
        attrValSize += LOGICAL_SIZE_OF_EMPTY_DOCUMENT;
    }
    return attrValSize;
}

From source file:com.dell.doradus.db.dynamodb.DDBColumnIterator.java

License:Apache License

private void loadAttributes(Map<String, AttributeValue> attributeMap, final boolean bReversed,
        ColumnFilter filter) {/*from   w  w w  .jav a2 s.  c o m*/
    if (attributeMap == null) {
        return;
    }
    for (Map.Entry<String, AttributeValue> mapEntry : attributeMap.entrySet()) {
        String colName = mapEntry.getKey();
        if (!colName.equals(DynamoDBService.ROW_KEY_ATTR_NAME) && // Don't add row key attribute as a column
                filter.select(colName)) {
            AttributeValue attrValue = mapEntry.getValue();
            if (attrValue.getB() != null) {
                m_columns.add(new DColumn(colName, Utils.getBytes(attrValue.getB())));
            } else if (attrValue.getS() != null) {
                String value = attrValue.getS();
                if (value.equals(DynamoDBService.NULL_COLUMN_MARKER)) {
                    value = "";
                }
                m_columns.add(new DColumn(colName, value));
            } else {
                throw new RuntimeException("Unknown AttributeValue type: " + attrValue);
            }
        }
    }

    // Sort or reverse sort column names.
    Collections.sort(m_columns, new Comparator<DColumn>() {
        @Override
        public int compare(DColumn col1, DColumn col2) {
            if (bReversed) {
                return col2.getName().compareTo(col1.getName());
            } else {
                return col1.getName().compareTo(col2.getName());
            }
        }
    });
}

From source file:com.dell.doradus.db.dynamodb.DynamoDBService.java

License:Apache License

private List<DColumn> loadAttributes(Map<String, AttributeValue> attributeMap,
        Predicate<String> colNamePredicate) {
    List<DColumn> columns = new ArrayList<>();
    if (attributeMap != null) {
        for (Map.Entry<String, AttributeValue> mapEntry : attributeMap.entrySet()) {
            String colName = mapEntry.getKey();
            if (!colName.equals(DynamoDBService.ROW_KEY_ATTR_NAME) && // Don't add row key attribute as a column
                    colNamePredicate.test(colName)) {
                AttributeValue attrValue = mapEntry.getValue();
                if (attrValue.getB() != null) {
                    columns.add(new DColumn(colName, Utils.getBytes(attrValue.getB())));
                } else if (attrValue.getS() != null) {
                    String value = attrValue.getS();
                    if (value.equals(DynamoDBService.NULL_COLUMN_MARKER)) {
                        value = "";
                    }/*w  ww .  j ava  2 s.c  o m*/
                    columns.add(new DColumn(colName, value));
                } else {
                    throw new RuntimeException("Unknown AttributeValue type: " + attrValue);
                }
            }
        }
    }

    // Sort or reverse sort column names.
    Collections.sort(columns, new Comparator<DColumn>() {
        @Override
        public int compare(DColumn col1, DColumn col2) {
            return col1.getName().compareTo(col2.getName());
        }
    });
    return columns;
}

From source file:com.github.sdmcraft.slingdynamo.demo.App.java

License:Open Source License

/**
 * Prints the item.//from w w w. j  a  va  2  s.  c om
 *
 * @param attributeList the attribute list
 */
private static void printItem(Map<String, AttributeValue> attributeList) {
    for (Map.Entry<String, AttributeValue> item : attributeList.entrySet()) {
        String attributeName = item.getKey();
        AttributeValue value = item.getValue();
        System.out.println(attributeName + " " + ((value.getS() == null) ? "" : ("S=[" + value.getS() + "]"))
                + ((value.getN() == null) ? "" : ("N=[" + value.getN() + "]"))
                + ((value.getB() == null) ? "" : ("B=[" + value.getB() + "]"))
                + ((value.getSS() == null) ? "" : ("SS=[" + value.getSS() + "]"))
                + ((value.getNS() == null) ? "" : ("NS=[" + value.getNS() + "]"))
                + ((value.getBS() == null) ? "" : ("BS=[" + value.getBS() + "] \n")));
    }
}

From source file:com.rapid7.diskstorage.dynamodb.builder.AbstractBuilder.java

License:Open Source License

protected StaticBuffer decodeValue(AttributeValue val) {
    if (null == val) {
        return null;
    }/*www  .  java2  s. com*/
    // Dynamo does not allow empty binary values, so we use a placeholder
    // for empty values
    if (Constants.EMPTY_BUFFER_PLACEHOLDER.equals(val.getS())) {
        return BufferUtil.emptyBuffer();
    }
    return StaticArrayBuffer.of(val.getB());
}

From source file:com.rapid7.diskstorage.dynamodb.DynamoDBDelegate.java

License:Open Source License

/**
 * Helper method that can clone an Attribute Value
 *
 * @param val the AttributeValue to copy
 * @param sourceDestinationMap used to avoid loops by keeping track of references
 * @return a copy of val/*from  w w  w  . j av a  2 s . c  o  m*/
 */
public static AttributeValue clone(AttributeValue val,
        IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap) {
    if (val == null) {
        return null;
    }

    if (sourceDestinationMap.containsKey(val)) {
        return sourceDestinationMap.get(val);
    }

    AttributeValue clonedVal = new AttributeValue();
    sourceDestinationMap.put(val, clonedVal);
    if (val.getN() != null) {
        clonedVal.setN(val.getN());
    } else if (val.getS() != null) {
        clonedVal.setS(val.getS());
    } else if (val.getB() != null) {
        clonedVal.setB(val.getB());
    } else if (val.getNS() != null) {
        clonedVal.setNS(val.getNS());
    } else if (val.getSS() != null) {
        clonedVal.setSS(val.getSS());
    } else if (val.getBS() != null) {
        clonedVal.setBS(val.getBS());
    } else if (val.getBOOL() != null) {
        clonedVal.setBOOL(val.getBOOL());
    } else if (val.getNULL() != null) {
        clonedVal.setNULL(val.getNULL());
    } else if (val.getL() != null) {
        List<AttributeValue> list = new ArrayList<>(val.getL().size());
        for (AttributeValue listItemValue : val.getL()) {
            if (!sourceDestinationMap.containsKey(listItemValue)) {
                sourceDestinationMap.put(listItemValue, clone(listItemValue, sourceDestinationMap));
            }
            list.add(sourceDestinationMap.get(listItemValue));
        }
        clonedVal.setL(list);
    } else if (val.getM() != null) {
        Map<String, AttributeValue> map = new HashMap<>(val.getM().size());
        for (Entry<String, AttributeValue> pair : val.getM().entrySet()) {
            if (!sourceDestinationMap.containsKey(pair.getValue())) {
                sourceDestinationMap.put(pair.getValue(), clone(pair.getValue(), sourceDestinationMap));
            }
            map.put(pair.getKey(), sourceDestinationMap.get(pair.getValue()));
        }
        clonedVal.setM(map);
    }
    return clonedVal;
}

From source file:com.rapid7.diskstorage.dynamodb.DynamoDBDelegate.java

License:Open Source License

/**Calculate attribute value size*/
private static int calculateAttributeSizeInBytes(AttributeValue value) {
    int attrValSize = 0;
    if (value == null) {
        return attrValSize;
    }/*  w w  w.  ja  v a2  s  .c om*/

    if (value.getB() != null) {
        ByteBuffer b = value.getB();
        attrValSize += b.remaining();
    } else if (value.getS() != null) {
        String s = value.getS();
        attrValSize += s.getBytes(UTF8).length;
    } else if (value.getN() != null) {
        attrValSize += MAX_NUMBER_OF_BYTES_FOR_NUMBER;
    } else if (value.getBS() != null) {
        List<ByteBuffer> bs = value.getBS();
        for (ByteBuffer b : bs) {
            if (b != null) {
                attrValSize += b.remaining();
            }
        }
    } else if (value.getSS() != null) {
        List<String> ss = value.getSS();
        for (String s : ss) {
            if (s != null) {
                attrValSize += s.getBytes(UTF8).length;
            }
        }
    } else if (value.getNS() != null) {
        List<String> ns = value.getNS();
        for (String n : ns) {
            if (n != null) {
                attrValSize += MAX_NUMBER_OF_BYTES_FOR_NUMBER;
            }
        }
    } else if (value.getBOOL() != null) {
        attrValSize += 1;
    } else if (value.getNULL() != null) {
        attrValSize += 1;
    } else if (value.getM() != null) {
        for (Map.Entry<String, AttributeValue> entry : value.getM().entrySet()) {
            attrValSize += entry.getKey().getBytes(UTF8).length;
            attrValSize += calculateAttributeSizeInBytes(entry.getValue());
            attrValSize += BASE_LOGICAL_SIZE_OF_NESTED_TYPES;
        }
        attrValSize += LOGICAL_SIZE_OF_EMPTY_DOCUMENT;
    } else if (value.getL() != null) {
        List<AttributeValue> list = value.getL();
        for (Integer i = 0; i < list.size(); i++) {
            attrValSize += calculateAttributeSizeInBytes(list.get(i));
            attrValSize += BASE_LOGICAL_SIZE_OF_NESTED_TYPES;
        }
        attrValSize += LOGICAL_SIZE_OF_EMPTY_DOCUMENT;
    }
    return attrValSize;
}

From source file:dynamok.source.RecordMapper.java

License:Apache License

public static Map<String, Struct> toConnect(Map<String, AttributeValue> attributes) {
    Map<String, Struct> connectAttributes = new HashMap<>(attributes.size());
    for (Map.Entry<String, AttributeValue> attribute : attributes.entrySet()) {
        final String attributeName = attribute.getKey();
        final AttributeValue attributeValue = attribute.getValue();
        final Struct attributeValueStruct = new Struct(AV_SCHEMA);
        if (attributeValue.getS() != null) {
            attributeValueStruct.put("S", attributeValue.getS());
        } else if (attributeValue.getN() != null) {
            attributeValueStruct.put("N", attributeValue.getN());
        } else if (attributeValue.getB() != null) {
            attributeValueStruct.put("B", attributeValue.getB());
        } else if (attributeValue.getSS() != null) {
            attributeValueStruct.put("SS", attributeValue.getSS());
        } else if (attributeValue.getNS() != null) {
            attributeValueStruct.put("NS", attributeValue.getNS());
        } else if (attributeValue.getBS() != null) {
            attributeValueStruct.put("BS", attributeValue.getBS());
        } else if (attributeValue.getNULL() != null) {
            attributeValueStruct.put("NULL", attributeValue.getNULL());
        } else if (attributeValue.getBOOL() != null) {
            attributeValueStruct.put("BOOL", attributeValue.getBOOL());
        }/*w  w  w .j  ava  2s.  co  m*/
        connectAttributes.put(attributeName, attributeValueStruct);
    }
    return connectAttributes;
}