Example usage for com.amazonaws.services.dynamodbv2.document Item withMap

List of usage examples for com.amazonaws.services.dynamodbv2.document Item withMap

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Item withMap.

Prototype

public Item withMap(String attrName, Map<String, ?> val) 

Source Link

Document

Sets the value of the specified attribute in the current item to the given value.

Usage

From source file:ws.salient.aws.dynamodb.DynamoDBStore.java

License:Apache License

public void put(Session session, Command command, int requestIndex) {
    try {/*w  w  w  .  j  av a 2 s .  co m*/

        eventsToPut.offer(encrypt(
                new Item()
                        .withPrimaryKey("sessionId", session.getSessionId(), "timestamp",
                                NANO_INSTANT.format(command.getTimestamp().plusNanos(requestIndex)))
                        .withBinary("command", json.writeValueAsBytes(command)),
                session.getSecretKey(), "command"));

        if (session.store(command)) {
            byte[] sessionBytes = session.toByteArray();
            byte[] properties = json.writeValueAsBytes(session.getProperties());
            Item item = new Item()
                    .withPrimaryKey("sessionId", command.getSessionId(), "timestamp",
                            command.getTimestamp().toString())
                    .withString("accountId", command.getAccountId())
                    .withMap("factCount", session.getFactCount())
                    .withInt("processCount", session.getProcessCount())
                    .withString("knowledgeBaseId", command.getKnowledgeBaseId())
                    .withBinary("session", sessionBytes).withBinary("properties", properties);

            if (session.getSecretKey() != null) {
                item.withMap("secretKey", new LinkedHashMap());
                item.getMap("secretKey").put("encrypted", session.getEncryptedKey());
                item.getMap("secretKey").put("algorithm", session.getSecretKey().getAlgorithm());
            }

            if (session.getSecretKey() != null) {
                item = encrypt(item, session.getSecretKey(), "properties", "session");
            }
            sessionsToPut.offer(item);
        }

        putItemExecutor.execute(() -> {
            List<Item> eventItems = new LinkedList();
            Item eventItem = eventsToPut.poll();
            while (eventItem != null) {
                eventItems.add(eventItem);
                eventItem = eventsToPut.poll();
            }
            if (!eventItems.isEmpty()) {
                TableWriteItems eventWriteItems = new TableWriteItems("SalientSessionEvent")
                        .withItemsToPut(eventItems);
                log.info("Storing events: " + eventItems.size());
                BatchWriteItemOutcome result = dynamodb.batchWriteItem(eventWriteItems);
                if (!result.getUnprocessedItems().isEmpty()) {
                    log.error("Unprocessed items: " + result.toString());
                }
            }
            Map<String, Item> sessionItems = new LinkedHashMap();
            Item sessionItem = sessionsToPut.poll();
            while (sessionItem != null) {
                // Only store latest session item
                sessionItems.put(sessionItem.getString("sessionId"), sessionItem);
                sessionItem = sessionsToPut.poll();
            }
            if (!sessionItems.isEmpty()) {
                TableWriteItems sessionWriteItems = new TableWriteItems("SalientSession")
                        .withItemsToPut(sessionItems.values());
                log.info("Storing sessions: " + sessionItems.size());
                BatchWriteItemOutcome result = dynamodb.batchWriteItem(sessionWriteItems);
                if (!result.getUnprocessedItems().isEmpty()) {
                    log.error("Unprocessed items: " + result.toString());
                }
            }
        });
    } catch (JsonProcessingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:ws.salient.aws.dynamodb.DynamoDBStore.java

License:Apache License

public Item encrypt(Item item, SecretKeySpec key, String... attributes) {
    try {/*from   w  w w. j a va  2  s .  co m*/
        if (key != null) {
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] iv = cipher.getIV();
            for (String attribute : attributes) {
                byte[] value = item.getBinary(attribute);
                item.withBinary(attribute, cipher.doFinal(value));
            }
            item.withMap("cipher", new LinkedHashMap());
            item.getMap("cipher").put("transformation", transformation);
            item.getMap("cipher").put("iv", iv);
        }
        return item;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        throw new RuntimeException(ex);
    }
}