summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/backend/db/CassandraCollectionProvider.java
blob: 5e83247e7a76cbfc599c4570ca6cedd6af1750b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.grow.backend.db;

import java.io.IOException;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnList;

import com.p4square.grow.provider.CollectionProvider;
import com.p4square.grow.provider.JsonEncodedProvider;

/**
 * CollectionProvider implementation backed by a Cassandra ColumnFamily.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class CassandraCollectionProvider<V> implements CollectionProvider<String, String, V> {
    private final CassandraDatabase mDb;
    private final String mCF;
    private final Class<V> mClazz;

    public CassandraCollectionProvider(CassandraDatabase db, String columnFamily, Class<V> clazz) {
        mDb = db;
        mCF = columnFamily;
        mClazz = clazz;
    }

    @Override
    public V get(String collection, String key) throws IOException {
        String blob = mDb.getKey(mCF, 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<>();

        ColumnList<String> row = mDb.getRow(mCF, collection);
        if (!row.isEmpty()) {
            int count = 0;
            for (Column<String> c : row) {
                if (limit >= 0 && ++count > limit) {
                    break; // Limit reached.
                }

                String key = c.getName();
                String blob = c.getStringValue();
                V obj = decode(blob);

                result.put(key, obj);
            }
        }

        return Collections.unmodifiableMap(result);
    }

    @Override
    public void put(String collection, String key, V obj) throws IOException {
        String blob = encode(obj);
        mDb.putKey(mCF, 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 {
        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;
        }

        V obj = JsonEncodedProvider.MAPPER.readValue(blob, mClazz);
        return obj;
    }
}