package meerkat.bulletinboard.sqlserver.mappers;

import meerkat.bulletinboard.BulletinBoardDigest;
import meerkat.protobuf.BulletinBoardApi.BatchChunk;
import org.springframework.jdbc.core.RowCallbackHandler;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Arbel Deutsch Peled on 19-Dec-15.
 */
public class BatchDataDigestHandler implements RowCallbackHandler {

    private final BulletinBoardDigest digest;

    public BatchDataDigestHandler(BulletinBoardDigest digest) {
        this.digest = digest;
    }

    @Override
    public void processRow(ResultSet rs) throws SQLException {
        try {
            BatchChunk batchChunk = BatchChunk.parseFrom(rs.getBytes(1));
            digest.update(batchChunk.getData().toByteArray());
        } catch (IOException e) {
            //TODO: Log
        }
    }
}
