se.l4.silo.structured.DefaultObjectEntity Maven / Gradle / Ivy
The newest version!
package se.l4.silo.structured;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Optional;
import java.util.function.Function;
import se.l4.commons.serialization.Serializer;
import se.l4.commons.serialization.format.BinaryInput;
import se.l4.commons.serialization.format.StreamingInput;
import se.l4.silo.FetchResult;
import se.l4.silo.StorageException;
import se.l4.silo.query.Query;
import se.l4.silo.query.QueryResult;
import se.l4.silo.query.QueryRunner;
import se.l4.silo.query.QueryType;
/**
* Implementation of {@link ObjectEntity}.
*
* @author Andreas Holstenson
*
* @param
*/
public class DefaultObjectEntity
implements ObjectEntity
{
private final StructuredEntity parent;
private final Serializer serializer;
private final Function identityMapper;
public DefaultObjectEntity(
StructuredEntity parent,
Serializer serializer,
Function identityMapper)
{
this.parent = parent;
this.serializer = serializer;
this.identityMapper = identityMapper;
}
@Override
public String getName()
{
return parent.getName();
}
@Override
public Optional get(Object id)
{
try(FetchResult fr = parent.get(id))
{
Iterator it = fr.iterator();
if(! it.hasNext())
{
return Optional.empty();
}
try(StreamingInput in = it.next())
{
return Optional.of(serializer.read(in));
}
catch(IOException e)
{
throw new StorageException("Unable to read object; " + e.getMessage(), e);
}
}
}
@Override
public void deleteViaId(Object id)
{
parent.delete(id);
}
@Override
public void delete(T object)
{
Object id = identityMapper.apply(object);
parent.delete(id);
}
@Override
public void store(T data)
{
try
{
Object id = identityMapper.apply(data);
byte[] binary = serializer.toBytes(data);
parent.store(id, new BinaryInput(new ByteArrayInputStream(binary)));
}
catch(Exception e)
{
throw new StorageException("Unable to store object; " + e.getMessage(), e);
}
}
@Override
public
© 2015 - 2025 Weber Informatics LLC | Privacy Policy