<commit_before>BEGIN TRANSACTION CreateStream;

    DECLARE @streamIdInternal AS INT;
    DECLARE @latestStreamVersion AS INT;
    DECLARE @latestStreamPosition AS BIGINT;

    BEGIN

        INSERT INTO dbo.Streams
            (Id, IdOriginal)
        VALUES
            (@streamId, @streamIdOriginal);

        SET @streamIdInternal = SCOPE_IDENTITY();

        INSERT INTO dbo.Messages
            (StreamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata)
            SELECT @streamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata
            FROM @newMessages
            ORDER BY StreamVersion;

        SET @latestStreamPosition = ISNULL(SCOPE_IDENTITY(), -1)

        SELECT @latestStreamVersion = MAX(StreamVersion) + @latestStreamVersion + 1
        FROM @newMessages

        SET @latestStreamVersion = ISNULL(@latestStreamVersion, -1)

        UPDATE dbo.Streams
            SET dbo.Streams.[Version] = @latestStreamVersion,
                dbo.Streams.[Position] = @latestStreamPosition
            WHERE dbo.Streams.IdInternal = @streamIdInternal

    END;

COMMIT TRANSACTION CreateStream;

/* Select CurrentVersion, CurrentPosition */

SELECT currentVersion = @latestStreamVersion, currentPosition = @latestStreamPosition

/* Select Metadata */

SELECT dbo.Messages.JsonData<commit_msg>Fixed an oversight while copy / pasting: when no stream the latest stream version logic can be simplified.<commit_after>BEGIN TRANSACTION CreateStream;

    DECLARE @streamIdInternal AS INT;
    DECLARE @latestStreamVersion AS INT;
    DECLARE @latestStreamPosition AS BIGINT;

    BEGIN

        INSERT INTO dbo.Streams
            (Id, IdOriginal)
        VALUES
            (@streamId, @streamIdOriginal);

        SET @streamIdInternal = SCOPE_IDENTITY();

        INSERT INTO dbo.Messages
            (StreamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata)
            SELECT @streamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata
            FROM @newMessages
            ORDER BY StreamVersion;

        SET @latestStreamPosition = ISNULL(SCOPE_IDENTITY(), -1)

        SELECT @latestStreamVersion = MAX(StreamVersion)
        FROM @newMessages

        SET @latestStreamVersion = ISNULL(@latestStreamVersion, -1)

        UPDATE dbo.Streams
            SET dbo.Streams.[Version] = @latestStreamVersion,
                dbo.Streams.[Position] = @latestStreamPosition
            WHERE dbo.Streams.IdInternal = @streamIdInternal

    END;

COMMIT TRANSACTION CreateStream;

/* Select CurrentVersion, CurrentPosition */

SELECT currentVersion = @latestStreamVersion, currentPosition = @latestStreamPosition

/* Select Metadata */

SELECT dbo.Messages.JsonData
{'fraction_non_alphanumeric': 0.07697893972403776, 'fraction_numerical': 0.0018155410312273058, 'mean_word_length': 2.9469914040114613, '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': '3318957', 'n_tokens_mistral': 768, 'n_tokens_neox': 694, 'n_words': 195}