package com.hpe.caf.api.worker;


import com.hpe.caf.api.Codec;
import com.hpe.caf.api.CodecException;
import com.hpe.caf.api.QuietResource;
import com.hpe.caf.util.ref.DataSource;
import com.hpe.caf.util.ref.DataSourceException;
import com.hpe.caf.util.ref.SourceNotFoundException;

import java.io.InputStream;
import java.util.Objects;


/**
 * An implementation of a DataSource that uses a Worker DataStore and a CAF Codec.
 * @since 9.0
 */
public class DataStoreSource extends DataSource
{
    private final DataStore store;
    private final Codec codec;


    /**
     * Create a new DataStoreSource.
     * @param dataStore the store of remote data
     * @param codec the method to decode remote data into returned objects
     */
    public DataStoreSource(final DataStore dataStore, final Codec codec)
    {
        this.store = Objects.requireNonNull(dataStore);
        this.codec = Objects.requireNonNull(codec);
    }


    @Override
    public <T> T getObject(final String ref, final Class<T> clazz)
        throws DataSourceException
    {
        try ( QuietResource<InputStream> qr = new QuietResource<>(getStream(ref))) {
            try {
                return codec.deserialise(qr.get(), clazz);
            } catch (CodecException e) {
                throw new DataSourceException("Could not deserialise stream", e);
            }
        }
    }


    @Override
    public InputStream getStream(final String ref)
        throws DataSourceException
    {
        try {
            return store.retrieve(ref);
        } catch (ReferenceNotFoundException e) {
            throw new SourceNotFoundException("Reference not found: " + ref, e);
        } catch (DataStoreException e) {
            throw new DataSourceException("Failed to get data stream", e);
        }
    }
}

@@ -1789,10 +1789,432 @@
 %7D%0A    %7D%0A
+%0A%0A    @Override%0A    public long getDataSize(final String ref)%0A        throws DataSourceException%0A    %7B%0A        try %7B%0A            return store.getDataSize(ref);%0A        %7D catch (ReferenceNotFoundException e) %7B%0A            throw new SourceNotFoundException(%22Reference not found: %22 + ref, e);%0A        %7D catch (DataStoreException e) %7B%0A            throw new DataSourceException(%22Failed to get data stream%22, e);%0A        %7D%0A    %7D%0A
 %7D%0A

Add ability to retrieve size of data from a ReferencedData