Example usage for com.amazonaws.services.dynamodbv2.document BatchWriteItemOutcome getUnprocessedItems

List of usage examples for com.amazonaws.services.dynamodbv2.document BatchWriteItemOutcome getUnprocessedItems

Introduction

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

Prototype

public Map<String, List<WriteRequest>> getUnprocessedItems() 

Source Link

Document

Convenient method to return the low-level unprocessed items.

Usage

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

License:Apache License

public void put(Session session, Command command, int requestIndex) {
    try {/*from  w  w  w . j a v  a2 s.c o 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);
    }
}