summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2016-04-09 14:22:20 -0700
committerJesse Morgan <jesse@jesterpm.net>2016-04-09 15:48:01 -0700
commit3102d8bce3426d9cf41aeaf201c360d342677770 (patch)
tree38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java
parentbbf907e51dfcf157bdee24dead1d531122aa25db (diff)
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java')
-rw-r--r--src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java109
1 files changed, 0 insertions, 109 deletions
diff --git a/src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java b/src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java
deleted file mode 100644
index b53e9f7..0000000
--- a/src/com/p4square/grow/backend/dynamo/DynamoCollectionProviderImpl.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright 2014 Jesse Morgan
- */
-
-package com.p4square.grow.backend.dynamo;
-
-import java.io.IOException;
-
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import com.p4square.grow.provider.CollectionProvider;
-import com.p4square.grow.provider.JsonEncodedProvider;
-
-/**
- *
- * @author Jesse Morgan <jesse@jesterpm.net>
- */
-public class DynamoCollectionProviderImpl<V> implements CollectionProvider<String, String, V> {
- private final DynamoDatabase mDb;
- private final String mTable;
- private final Class<V> mClazz;
-
- public DynamoCollectionProviderImpl(DynamoDatabase db, String table, Class<V> clazz) {
- mDb = db;
- mTable = table;
- mClazz = clazz;
- }
-
- @Override
- public V get(String collection, String key) throws IOException {
- String blob = mDb.getAttribute(DynamoKey.newAttributeKey(mTable, collection, key));
- return decode(blob);
- }
-
- @Override
- public Map<String, V> query(String collection) throws IOException {
- return query(collection, -1);
- }
-
- @Override
- public Map<String, V> query(String collection, int limit) throws IOException {
- Map<String, V> result = new LinkedHashMap<>();
-
- Map<String, String> row = mDb.getKey(DynamoKey.newKey(mTable, collection));
- if (row.size() > 0) {
- int count = 0;
- for (Map.Entry<String, String> c : row.entrySet()) {
- if (limit >= 0 && ++count > limit) {
- break; // Limit reached.
- }
-
- String key = c.getKey();
- String blob = c.getValue();
- V obj = decode(blob);
-
- result.put(key, obj);
- }
- }
-
- return Collections.unmodifiableMap(result);
- }
-
- @Override
- public void put(String collection, String key, V obj) throws IOException {
- if (obj == null) {
- mDb.deleteAttribute(DynamoKey.newAttributeKey(mTable, collection, key));
- } else {
- String blob = encode(obj);
- mDb.putAttribute(DynamoKey.newAttributeKey(mTable, collection, key), blob);
- }
- }
-
- /**
- * Encode the object as JSON.
- *
- * @param obj The object to encode.
- * @return The JSON encoding of obj.
- * @throws IOException if the object cannot be encoded.
- */
- protected String encode(V obj) throws IOException {
- if (mClazz == String.class) {
- return (String) obj;
- } else {
- return JsonEncodedProvider.MAPPER.writeValueAsString(obj);
- }
- }
-
- /**
- * Decode the JSON string as an object.
- *
- * @param blob The JSON data to decode.
- * @return The decoded object or null if blob is null.
- * @throws IOException If an object cannot be decoded.
- */
- protected V decode(String blob) throws IOException {
- if (blob == null) {
- return null;
- }
-
- if (mClazz == String.class) {
- return (V) blob;
- }
-
- V obj = JsonEncodedProvider.MAPPER.readValue(blob, mClazz);
- return obj;
- }
-}