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

com.amazonaws.services.kinesis.leases.impl.LeaseSerializer Maven / Gradle / Ivy

/*
 * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Amazon Software License (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://aws.amazon.com/asl/
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.amazonaws.services.kinesis.leases.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.model.AttributeAction;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.kinesis.leases.interfaces.ILeaseSerializer;
import com.amazonaws.services.kinesis.leases.util.DynamoUtils;

/**
 * An implementation of ILeaseSerializer for basic Lease objects. Can also instantiate subclasses of Lease so that
 * LeaseSerializer can be decorated by other classes if you need to add fields to leases.
 */
public class LeaseSerializer implements ILeaseSerializer {

    public final String LEASE_KEY_KEY = "leaseKey";
    public final String LEASE_OWNER_KEY = "leaseOwner";
    public final String LEASE_COUNTER_KEY = "leaseCounter";
    public final Class clazz;

    public LeaseSerializer() {
        this.clazz = Lease.class;
    }

    public LeaseSerializer(Class clazz) {
        this.clazz = clazz;
    }

    @Override
    public Map toDynamoRecord(Lease lease) {
        Map result = new HashMap();

        result.put(LEASE_KEY_KEY, DynamoUtils.createAttributeValue(lease.getLeaseKey()));
        result.put(LEASE_COUNTER_KEY, DynamoUtils.createAttributeValue(lease.getLeaseCounter()));

        if (lease.getLeaseOwner() != null) {
            result.put(LEASE_OWNER_KEY, DynamoUtils.createAttributeValue(lease.getLeaseOwner()));
        }

        return result;
    }

    @Override
    public Lease fromDynamoRecord(Map dynamoRecord) {
        Lease result;
        try {
            result = clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        result.setLeaseKey(DynamoUtils.safeGetString(dynamoRecord, LEASE_KEY_KEY));
        result.setLeaseOwner(DynamoUtils.safeGetString(dynamoRecord, LEASE_OWNER_KEY));
        result.setLeaseCounter(DynamoUtils.safeGetLong(dynamoRecord, LEASE_COUNTER_KEY));

        return result;
    }

    @Override
    public Map getDynamoHashKey(String leaseKey) {
        Map result = new HashMap();

        result.put(LEASE_KEY_KEY, DynamoUtils.createAttributeValue(leaseKey));

        return result;
    }

    @Override
    public Map getDynamoHashKey(Lease lease) {
        return getDynamoHashKey(lease.getLeaseKey());
    }

    @Override
    public Map getDynamoLeaseCounterExpectation(Lease lease) {
        return getDynamoLeaseCounterExpectation(lease.getLeaseCounter());
    }

    public Map getDynamoLeaseCounterExpectation(Long leaseCounter) {
        Map result = new HashMap();

        ExpectedAttributeValue eav = new ExpectedAttributeValue(DynamoUtils.createAttributeValue(leaseCounter));
        result.put(LEASE_COUNTER_KEY, eav);

        return result;
    }

    @Override
    public Map getDynamoLeaseOwnerExpectation(Lease lease) {
        Map result = new HashMap();

        ExpectedAttributeValue eav = null;
        
        if (lease.getLeaseOwner() == null) {
            eav = new ExpectedAttributeValue(false);
        } else {
            eav = new ExpectedAttributeValue(DynamoUtils.createAttributeValue(lease.getLeaseOwner()));
        }
        
        result.put(LEASE_OWNER_KEY, eav);

        return result;
    }

    @Override
    public Map getDynamoNonexistantExpectation() {
        Map result = new HashMap();

        ExpectedAttributeValue expectedAV = new ExpectedAttributeValue(false);
        result.put(LEASE_KEY_KEY, expectedAV);

        return result;
    }

    @Override
    public Map getDynamoLeaseCounterUpdate(Lease lease) {
        return getDynamoLeaseCounterUpdate(lease.getLeaseCounter());
    }

    public Map getDynamoLeaseCounterUpdate(Long leaseCounter) {
        Map result = new HashMap();

        AttributeValueUpdate avu =
                new AttributeValueUpdate(DynamoUtils.createAttributeValue(leaseCounter + 1), AttributeAction.PUT);
        result.put(LEASE_COUNTER_KEY, avu);

        return result;
    }

    @Override
    public Map getDynamoTakeLeaseUpdate(Lease lease, String owner) {
        Map result = new HashMap();

        result.put(LEASE_OWNER_KEY, new AttributeValueUpdate(DynamoUtils.createAttributeValue(owner),
                AttributeAction.PUT));

        return result;
    }

    @Override
    public Map getDynamoEvictLeaseUpdate(Lease lease) {
        Map result = new HashMap();

        result.put(LEASE_OWNER_KEY, new AttributeValueUpdate(null, AttributeAction.DELETE));

        return result;
    }

    @Override
    public Map getDynamoUpdateLeaseUpdate(Lease lease) {
        // There is no application-specific data in Lease - just return a map that increments the counter.
        return new HashMap();
    }

    @Override
    public Collection getKeySchema() {
        List keySchema = new ArrayList();
        keySchema.add(new KeySchemaElement().withAttributeName(LEASE_KEY_KEY).withKeyType(KeyType.HASH));

        return keySchema;
    }

    @Override
    public Collection getAttributeDefinitions() {
        List definitions = new ArrayList();
        definitions.add(new AttributeDefinition().withAttributeName(LEASE_KEY_KEY)
                .withAttributeType(ScalarAttributeType.S));

        return definitions;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy