package leigh.conflux;

import leigh.eo.EO;
import leigh.eo.EOLoop;
import leigh.eo.EditEventGenerator;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.io.Serializable;
import java.util.UUID;

/**
 * A simple implementation of the {@link DataSource} interface provided by heap storage.
 *
 * @author Alex Leigh
 */
public class RamDataSource implements DataSource, Serializable {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(RamDataSource.class);
    private static final long serialVersionUID = 1L;
    private final EOLoop data;


    public RamDataSource() {
        data = new EOLoop();
        data.setEditTracker(new EditEventGenerator());
    }

    public RamDataSource(EOLoop data) {
        // For debugging
        assert (data != null);
        this.data = data;
    }

    @Override
    public EOLoop getData() {
        return data;
    }

    public EO getObject(UUID id) {
        for (EO eo : data) {
            UUID objId = eo.getValueUUID("_id");
            if (id.equals(objId)) return eo;
        }
        return null;
    }
}
