<commit_before>package com.netflix.hollow.diff.ui;

import com.netflix.hollow.core.index.key.PrimaryKey;
import com.netflix.hollow.core.read.engine.HollowBlobReader;
import com.netflix.hollow.core.read.engine.HollowReadStateEngine;
import com.netflix.hollow.core.read.engine.HollowTypeReadState;
import com.netflix.hollow.core.schema.HollowObjectSchema;
import com.netflix.hollow.core.schema.HollowObjectSchema.FieldType;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.util.StateEngineRoundTripper;
import com.netflix.hollow.core.write.HollowBlobWriter;
import com.netflix.hollow.core.write.HollowObjectTypeWriteState;
import com.netflix.hollow.core.write.HollowObjectWriteRecord;
import com.netflix.hollow.core.write.HollowTypeWriteState;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.history.ui.jetty.HollowHistoryUIServer;
import com.netflix.hollow.tools.history.HollowHistory;
import com.netflix.hollow.tools.history.keyindex.HollowHistoryKeyIndex;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Test;

public class HistoryUITest {

    @Test
    public void startServerOnPort7777() throws Exception {
        HollowHistory history = createHistory();

        HollowHistoryUIServer server = new HollowHistoryUIServer(history, 7777);
        server.start();
        server.join();
    }


    private HollowWriteStateEngine stateEngine;
    private HollowObjectSchema schema;
    private HollowObjectSchema bSchema;

    private HollowHistory createHistory() throws IOException {
        stateEngine = new HollowWriteStateEngine();

        schema = new HollowObjectSchema("TypeA", 2);
        schema.addField("a1", FieldType.INT);
        schema.addField("a2", FieldType.INT);

        HollowTypeWriteState writeState = new HollowObjectTypeWriteState(schema);

        stateEngine.addTypeState(writeState);

        addRecord(1, 1);
        addRecord(2, 2);
        addRecord(3, 3);
        addRecord(4, 4);
        addRecord(5, 5);
        addRecord(6, 6);

        stateEngine.prepareForWrite();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HollowBlobWriter writer = new HollowBlobWriter(stateEngine);
        writer.writeSnapshot(baos);


        HollowReadStateEngine readStateEngine = new HollowReadStateEngine();
        HollowBlobReader reader = new HollowBlobReader(readStateEngine);
        reader.readSnapshot(new ByteArrayInputStream(baos.toByteArray()));
        HollowHistory history = new HollowHistory(readStateEngine, 0, 10);
        history.getKeyIndex().addTypeIndex("TypeA", "a1");

        stateEngine.prepareForNextCycle();

        addRecord(1, 1);
        addRecord(2, 7);
        addRecord(3, 3);
        addRecord(5, 8);
        addRecord(6, 6);
        addRecord(7, 9);
        addRecord(8, 10);

        stateEngine.prepareForWrite();

        baos = new ByteArrayOutputStream();
        writer.writeDelta(baos);
        reader.applyDelta(new ByteArrayInputStream(baos.toByteArray()));
        history.deltaOccurred(19991231235959999L);

        stateEngine.prepareForNextCycle();

        addRecord(1, 1);
        addRecord(2, 7);
        addRecord(3, 11);
        addRecord(6, 12);
        addRecord(7, 13);
        addRecord(8, 10);

        stateEngine.prepareForWrite();

        baos = new ByteArrayOutputStream();
        writer.writeDelta(baos);
        reader.applyDelta(new ByteArrayInputStream(baos.toByteArray()));
        history.deltaOccurred(20001231235959999L);

        { // do double snapshot and introduce new type
            bSchema = new HollowObjectSchema("TypeB", 2, "b1");
            bSchema.addField("b1", FieldType.INT);
            bSchema.addField("b2", FieldType.INT);

            HollowWriteStateEngine stateEngine2 = new HollowWriteStateEngine();
            stateEngine2.addTypeState(new HollowObjectTypeWriteState(schema));
            stateEngine2.addTypeState(new HollowObjectTypeWriteState(bSchema));
            addRec(stateEngine2, schema, new String[] { "a1", "a2" }, new int[] { 1, 1 });
            addRec(stateEngine2, schema, new String[] { "a1", "a2" }, new int[] { 2, 2 });
            addRec(stateEngine2, bSchema, new String[] { "b1", "b2" }, new int[] { 9, 999 });

            HollowReadStateEngine readStateEngine2 = new HollowReadStateEngine();
            StateEngineRoundTripper.roundTripSnapshot(stateEngine2, readStateEngine2, null);
            setupKeyIndex(readStateEngine2, history);
            history.doubleSnapshotOccurred(readStateEngine2, 20011231235959999L);
        }
<commit_msg>minor cleanup<commit_after>package com.netflix.hollow.diff.ui;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Test;

import com.netflix.hollow.core.index.key.PrimaryKey;
import com.netflix.hollow.core.read.engine.HollowBlobReader;
import com.netflix.hollow.core.read.engine.HollowReadStateEngine;
import com.netflix.hollow.core.read.engine.HollowTypeReadState;
import com.netflix.hollow.core.schema.HollowObjectSchema;
import com.netflix.hollow.core.schema.HollowObjectSchema.FieldType;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.util.StateEngineRoundTripper;
import com.netflix.hollow.core.write.HollowBlobWriter;
import com.netflix.hollow.core.write.HollowObjectTypeWriteState;
import com.netflix.hollow.core.write.HollowObjectWriteRecord;
import com.netflix.hollow.core.write.HollowTypeWriteState;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.history.ui.jetty.HollowHistoryUIServer;
import com.netflix.hollow.tools.history.HollowHistory;
import com.netflix.hollow.tools.history.keyindex.HollowHistoryKeyIndex;

public class HistoryUITest {

    @Test
    public void startServerOnPort7777() throws Exception {
        HollowHistory history = createHistory();

        HollowHistoryUIServer server = new HollowHistoryUIServer(history, 7777);
        server.start();
        server.join();
    }


    private HollowWriteStateEngine stateEngine;
    private HollowObjectSchema schema;
    private HollowObjectSchema bSchema;

    private HollowHistory createHistory() throws IOException {
        stateEngine = new HollowWriteStateEngine();

        schema = new HollowObjectSchema("TypeA", 2);
        schema.addField("a1", FieldType.INT);
        schema.addField("a2", FieldType.INT);

        HollowTypeWriteState writeState = new HollowObjectTypeWriteState(schema);

        stateEngine.addTypeState(writeState);

        addRecord(1, 1);
        addRecord(2, 2);
        addRecord(3, 3);
        addRecord(4, 4);
        addRecord(5, 5);
        addRecord(6, 6);

        stateEngine.prepareForWrite();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HollowBlobWriter writer = new HollowBlobWriter(stateEngine);
        writer.writeSnapshot(baos);


        HollowReadStateEngine readStateEngine = new HollowReadStateEngine();
        HollowBlobReader reader = new HollowBlobReader(readStateEngine);
        reader.readSnapshot(new ByteArrayInputStream(baos.toByteArray()));
        HollowHistory history = new HollowHistory(readStateEngine, 0, 10);
        history.getKeyIndex().addTypeIndex("TypeA", "a1");

        stateEngine.prepareForNextCycle();

        addRecord(1, 1);
        addRecord(2, 7);
        addRecord(3, 3);
        addRecord(5, 8);
        addRecord(6, 6);
        addRecord(7, 9);
        addRecord(8, 10);

        stateEngine.prepareForWrite();

        baos = new ByteArrayOutputStream();
        writer.writeDelta(baos);
        reader.applyDelta(new ByteArrayInputStream(baos.toByteArray()));
        history.deltaOccurred(19991231235959999L);

        stateEngine.prepareForNextCycle();

        addRecord(1, 1);
        addRecord(2, 7);
        addRecord(3, 11);
        addRecord(6, 12);
        addRecord(7, 13);
        addRecord(8, 10);

        stateEngine.prepareForWrite();

        baos = new ByteArrayOutputStream();
        writer.writeDelta(baos);
        reader.applyDelta(new ByteArrayInputStream(baos.toByteArray()));
        history.deltaOccurred(20001231235959999L);

        // Double Snapshot
        bSchema = new HollowObjectSchema("TypeB", 2, "b1");
        bSchema.addField("b1", FieldType.INT);
        bSchema.addField("b2", FieldType.INT);
        { // do double snapshot and introduce new type
            HollowWriteStateEngine stateEngine2 = new HollowWriteStateEngine();
            stateEngine2.addTypeState(new HollowObjectTypeWriteState(schema));
            stateEngine2.addTypeState(new HollowObjectTypeWriteState(bSchema));
            addRec(stateEngine2, schema, new String[] { "a1", "a2" }, new int[] { 1, 1 });
            addRec(stateEngine2, schema, new String[] { "a1", "a2" }, new int[] { 2, 2 });
            addRec(stateEngine2, bSchema, new String[] { "b1", "b2" }, new int[] { 9, 999 });

            HollowReadStateEngine readStateEngine2 = new HollowReadStateEngine();
            StateEngineRoundTripper.roundTripSnapshot(stateEngine2, readStateEngine2, null);
            setupKeyIndex(readStateEngine2, history);
            history.doubleSnapshotOccurred(readStateEngine2, 20011231235959999L);
        }

{'fraction_non_alphanumeric': 0.10668380462724936, 'fraction_numerical': 0.02956298200514139, 'mean_word_length': 3.5106280193236716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4942468', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2687, 'n_words': 483}