Java Code Examples for com.amazonaws.services.dynamodbv2.model.AttributeValue#setN()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.AttributeValue#setN() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamoDSETranslatorJSONBlob.java    From dynamo-cassandra-proxy with Apache License 2.0 5 votes vote down vote up
private AttributeValue rowToAV(ColumnDefinition columnDefinition, Row row) {
    AttributeValue av = new AttributeValue();
    CqlIdentifier name = columnDefinition.getName();
    switch (columnDefinition.getType().getProtocolCode())
    {
        case BLOB:
            av.setB(row.getByteBuffer(name));
            break;
        case BIGINT:
        case BOOLEAN:
        case COUNTER:
        case DECIMAL:
        case DOUBLE:
        case FLOAT:
        case INT:
        case VARINT:
        case TINYINT:
        case SMALLINT:
            String v = String.valueOf(row.getDouble(name));
            if (v.endsWith(".0")) //Keep non doubles looking like non-doubles
                v = v.substring(0, v.length() - 2);
            av.setN(v);
            break;
        case TIMEUUID:
        case UUID:
        case INET:
        case DATE:
        case VARCHAR:
        case ASCII:
        case TIME:
            av.setS(row.getString(name));
            break;
        default:
            throw new IllegalArgumentException("Type not supported: " + name.asInternal() + " " + columnDefinition.getType());
    }
    return av;
}
 
Example 2
Source File: AttributeValueCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPassForNumberType() throws IOException {
  AttributeValue expected = new AttributeValue();
  expected.setN("123");

  AttributeValueCoder coder = AttributeValueCoder.of();
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  coder.encode(expected, output);

  ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());

  AttributeValue actual = coder.decode(in);

  Assert.assertEquals(expected, actual);
}
 
Example 3
Source File: AttributeValueCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeValue decode(InputStream inStream) throws IOException {
  AttributeValue attrValue = new AttributeValue();

  String type = StringUtf8Coder.of().decode(inStream);
  AttributeValueType attrType = AttributeValueType.valueOf(type);

  switch (attrType) {
    case s:
      attrValue.setS(StringUtf8Coder.of().decode(inStream));
      break;
    case n:
      attrValue.setN(StringUtf8Coder.of().decode(inStream));
      break;
    case bOOL:
      attrValue.setBOOL(BooleanCoder.of().decode(inStream));
      break;
    case b:
      attrValue.setB(ByteBuffer.wrap(ByteArrayCoder.of().decode(inStream)));
      break;
    case sS:
      attrValue.setSS(LIST_STRING_CODER.decode(inStream));
      break;
    case nS:
      attrValue.setNS(LIST_STRING_CODER.decode(inStream));
      break;
    case bS:
      attrValue.setBS(convertToListByteBuffer(LIST_BYTE_CODER.decode(inStream)));
      break;
    case l:
      attrValue.setL(LIST_ATTRIBUTE_CODER.decode(inStream));
      break;
    case m:
      attrValue.setM(MAP_ATTRIBUTE_CODER.decode(inStream));
      break;
    case nULLValue:
      attrValue.setNULL(BooleanCoder.of().decode(inStream));
      break;
    default:
      throw new CoderException("Unknown Type");
  }

  return attrValue;
}
 
Example 4
Source File: DynamoDBCertRecordStoreConnectionTest.java    From athenz with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestamp() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;

    Map<String, AttributeValue> unNotified = generateAttributeValues(
            "home.test.service2",
            "testInstance2",
            null,
            null,
            null,
            null,
            "testHost1");

    Map<String, AttributeValue> reNotified = generateAttributeValues(
            "home.test.service3",
            "testInstance3",
            Long.toString(fiveDaysAgo),
            Long.toString(fiveDaysAgo),
            "testServer",
            null,
            "testHost1");

    Item item1 = ItemUtils.toItem(unNotified);
    Item item2 = ItemUtils.toItem(reNotified);

    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, true, false);
    when(iteratorSupport.next()).thenReturn(item1).thenReturn(item2);

    Mockito.doReturn(itemCollection).when(index).query(any(QuerySpec.class));

    AttributeValue lastNotifiedTimeAttrValue = new AttributeValue();
    lastNotifiedTimeAttrValue.setN(Long.toString(nowL));
    AttributeValue lastNotifiedServerAttrValue = new AttributeValue();
    lastNotifiedServerAttrValue.setS("localhost");
    unNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    unNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);

    reNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    reNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);

    Item updatedItem1 = ItemUtils.toItem(unNotified);
    Item updatedItem2 = ItemUtils.toItem(reNotified);

    UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome1.getItem()).thenReturn(updatedItem1);

    UpdateItemOutcome updateItemOutcome2 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome2.getItem()).thenReturn(updatedItem2);

    when(table.updateItem(any(UpdateItemSpec.class))).thenReturn(updateItemOutcome1).thenReturn(updateItemOutcome2);
    List<X509CertRecord> records = dbConn.updateUnrefreshedCertificatesNotificationTimestamp(
            "localhost",
            nowL,
            "provider");

    assertEquals(records.size(), 2);
    assertNull(records.get(0).getCurrentTime());
    assertEquals(records.get(0).getService(), "home.test.service2");
    assertEquals(records.get(0).getLastNotifiedTime(), now);
    assertEquals(records.get(1).getCurrentTime().getTime(), fiveDaysAgo);
    assertEquals(records.get(1).getService(), "home.test.service3");
    assertEquals(records.get(1).getLastNotifiedTime(), now);
}
 
Example 5
Source File: DynamoDBCertRecordStoreConnectionTest.java    From athenz with Apache License 2.0 4 votes vote down vote up
private Map<String, AttributeValue> generateAttributeValues(String service,
                                                            String instanceId,
                                                            String currentTime,
                                                            String lastNotifiedTime,
                                                            String lastNotifiedServer,
                                                            String expiryTime,
                                                            String hostName) {
    String provider = "provider";
    String primaryKey = provider + ":" + service + ":" + instanceId;
    Map<String, AttributeValue> item = new HashMap<>();
    item.put("primaryKey", new AttributeValue(primaryKey));
    item.put("service", new AttributeValue(service));
    item.put("provider", new AttributeValue(provider));
    item.put("instanceId", new AttributeValue(instanceId));
    item.put("currentSerial", new AttributeValue("currentSerial"));

    AttributeValue currentTimeVal = new AttributeValue();
    currentTimeVal.setN(currentTime);

    if (!StringUtil.isEmpty(currentTime)) {
        item.put("currentTime", currentTimeVal);
        item.put("prevTime", currentTimeVal);
    }

    item.put("currentIP", new AttributeValue("currentIP"));
    item.put("prevSerial", new AttributeValue("prevSerial"));
    item.put("prevIP", new AttributeValue("prevIP"));

    AttributeValue clientCertVal = new AttributeValue();
    clientCertVal.setBOOL(false);
    item.put("clientCert", clientCertVal);

    if (!StringUtil.isEmpty(lastNotifiedTime)) {
        AttributeValue lastNotifiedTimeVal = new AttributeValue();
        lastNotifiedTimeVal.setN(lastNotifiedTime);
        item.put("lastNotifiedTime", lastNotifiedTimeVal);
    }

    if (!StringUtil.isEmpty(lastNotifiedServer)) {
        item.put("lastNotifiedServer", new AttributeValue(lastNotifiedServer));
    }

    if (!StringUtil.isEmpty(expiryTime)) {
        AttributeValue expiryTimeVal = new AttributeValue();
        expiryTimeVal.setN(expiryTime);
        item.put("expiryTime", expiryTimeVal);
    }

    if (!StringUtil.isEmpty(hostName)) {
        item.put("hostName", new AttributeValue(hostName));
    }

    return item;
}
 
Example 6
Source File: DynamoDbDelegate.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 4 votes vote down vote up
/**
 * 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
 */
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;
}
 
Example 7
Source File: AttributeValueMarshaller.java    From aws-dynamodb-encryption-java with Apache License 2.0 4 votes vote down vote up
private static AttributeValue unmarshall(final DataInputStream in) throws IOException {
    char type = in.readChar();
    AttributeValue result = new AttributeValue();
    switch (type) {
    case '\0':
        result.setNULL(Boolean.TRUE);
        break;
    case 'b':
        result.setB(readBytes(in));
        break;
    case 'B':
        result.setBS(readBytesList(in));
        break;
    case 'n':
        result.setN(readString(in));
        break;
    case 'N':
        result.setNS(readStringList(in));
        break;
    case 's':
        result.setS(readString(in));
        break;
    case 'S':
        result.setSS(readStringList(in));
        break;
    case '?':
        final byte boolValue = in.readByte();

        if (boolValue == TRUE_FLAG) {
            result.setBOOL(Boolean.TRUE);
        } else if (boolValue == FALSE_FLAG) {
            result.setBOOL(Boolean.FALSE);
        } else {
            throw new IllegalArgumentException("Improperly formatted data");
        }
        break;
    case 'L':
        final int lCount = in.readInt();
        final List<AttributeValue> l = new ArrayList<AttributeValue>(lCount);
        for (int lIdx = 0; lIdx < lCount; lIdx++) {
            l.add(unmarshall(in));
        }
        result.setL(l);
        break;
    case 'M':
        final int mCount = in.readInt();
        final Map<String, AttributeValue> m = new HashMap<String, AttributeValue>();
        for (int mIdx = 0; mIdx < mCount; mIdx++) {
            final AttributeValue key = unmarshall(in);
            if (key.getS() == null) {
                throw new IllegalArgumentException("Improperly formatted data");
            }
            AttributeValue value = unmarshall(in);
            m.put(key.getS(), value);
        }
        result.setM(m);
        break;
    default:
        throw new IllegalArgumentException("Unsupported data encoding");
    }

    return result;
}