All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ebuddy.cassandra.dao.ThriftSuperStructuredDataSupport Maven / Gradle / Ivy

The newest version!
package com.ebuddy.cassandra.dao;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.ebuddy.cassandra.BatchContext;
import com.ebuddy.cassandra.Path;
import com.ebuddy.cassandra.StructuredDataSupport;
import com.ebuddy.cassandra.TypeReference;
import com.ebuddy.cassandra.databind.CustomTypeResolverBuilder;
import com.ebuddy.cassandra.structure.Composer;
import com.ebuddy.cassandra.structure.Decomposer;
import com.ebuddy.cassandra.structure.JacksonTypeReference;

/**
 * Implementation of StructuredDataSupport for a Thrift SuperColumnFamily.
 *
 * @param type of row key
 *
 * @author Eric Zoerner [email protected]
 */
public class ThriftSuperStructuredDataSupport extends AbstractThriftStructuredDataSupport {

    private final SuperColumnFamilyOperations operations;

    /**
     * Create and configure an instance with a SuperColumnFamilyOperations.
     * @param operations a SuperColumnFamilyOperations that has String column and supercolumn names and a
     *                   StructureSerializer for the valueSerializer.
     */
    public ThriftSuperStructuredDataSupport(SuperColumnFamilyOperations operations) {
        this.operations = operations;
    }

    @Override
    public BatchContext beginBatch() {
        return operations.begin();
    }

    @Override
    public void applyBatch(BatchContext batchContext) {
        operations.commit(batchContext);
    }

    @Override
    public  T readFromPath(K rowKey, Path path, TypeReference type) {
        validateArgs(rowKey, path);
        int count = Integer.MAX_VALUE;
        boolean reversed = false;

        // converting from a string and back normalizes the path, e.g. makes sure ends with the delimiter character
        String superColumnName = path.head();
        Path rest = path.tail();
        String start = rest.toString();
        String finish = getFinishString(start);
        Map columnsMap = operations.readColumnsAsMap(rowKey,
                                                                    superColumnName,
                                                                    start,
                                                                    finish,
                                                                    count,
                                                                    reversed);
        if (columnsMap.isEmpty()) {
            return null;
        }

        Map pathMap = getTerminalPathMap(rest, columnsMap);
        Object structure = Composer.get().compose(pathMap);

        // convert object structure into POJO of type referred to by TypeReference
        return readMapper.convertValue(structure, new JacksonTypeReference(type));
    }

    @Override
    public void writeToPath(K rowKey, Path path, Object value, BatchContext batchContext) {
        validateArgs(rowKey, path);

        Object structure = writeMapper.convertValue(value, Object.class);

        String superColumnName = path.head();
        Path rest = path.tail();

        Map pathMap = Collections.singletonMap(rest, structure);
        Map objectMap = Decomposer.get().decompose(pathMap);

        Map stringMap = new HashMap();
        for (Map.Entry entry : objectMap.entrySet()) {
            stringMap.put(entry.getKey().toString(), entry.getValue());
        }

        if (batchContext == null) {
            operations.writeColumns(rowKey, superColumnName, stringMap);
        } else {
            operations.writeColumns(rowKey, superColumnName, stringMap, batchContext);
        }
    }

    @Override
    public void deletePath(K rowKey, Path path, BatchContext batchContext) {
        String superColumnName = path.head();

        String start = path.tail().toString();
        String finish = getFinishString(start);
        if (batchContext == null) {
            operations.deleteColumns(rowKey, superColumnName, start, finish);
        } else {
            operations.deleteColumns(rowKey, superColumnName, start, finish, batchContext);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy