<commit_before>//
//  Database+Transaction.swift
//  Torch
//
//  Created by <NAME> on 24/07/16.
//  Copyright © 2016 Brightify. All rights reserved.
//

import Foundation

extension Database {
    public typealias Rollback = () -> Void

    public var transaction: TorchTransactionHelper {
        return TorchTransactionHelper(
            begin: realm.beginWrite,
            commit: { self.commit(errorHandler: self.defaultOnWriteError) },
            commitWithErrorHandler: commit(errorHandler:),
            rollback: rollback)
    }

    @discardableResult
    public func write(_ closure: (@escaping Rollback) -> Void) -> Database {
        return write(defaultOnWriteError, closure: closure)
    }

    @discardableResult
    public func write(_ onWriteError: OnWriteErrorListener, closure: (@escaping Rollback) -> Void) -> Database {
        realm.beginWrite()
        closure(rollback)
        // In case we rolled back
        guard realm.isInWriteTransaction else { return self }
        commit(errorHandler: onWriteError)
        return self
    }

    private func commit(errorHandler: OnWriteErrorListener) {
        do {
            try realm.commitWrite()
        } catch {
            errorHandler(error)
        }
    }

    private func rollback() {
        realm.cancelWrite()
        metadata = [:]
    }
}
<commit_msg>Ensure transaction helper methods don’t crash.<commit_after>//
//  Database+Transaction.swift
//  Torch
//
//  Created by <NAME> on 24/07/16.
//  Copyright © 2016 Brightify. All rights reserved.
//

import Foundation

extension Database {
    public typealias Rollback = () -> Void

    public var transaction: TorchTransactionHelper {
        return TorchTransactionHelper(
            begin: begin,
            commit: { self.commit(errorHandler: self.defaultOnWriteError) },
            commitWithErrorHandler: commit(errorHandler:),
            rollback: rollback)
    }

    @discardableResult
    public func write(_ closure: (@escaping Rollback) -> Void) -> Database {
        return write(defaultOnWriteError, closure: closure)
    }

    @discardableResult
    public func write(_ onWriteError: OnWriteErrorListener, closure: (@escaping Rollback) -> Void) -> Database {
        realm.beginWrite()
        closure(rollback)
        // In case we rolled back
        commit(errorHandler: onWriteError)
        return self
    }

    private func begin() {
        guard !realm.isInWriteTransaction else { return }
        realm.beginWrite()
    }

    private func commit(errorHandler: OnWriteErrorListener) {
        guard realm.isInWriteTransaction else { return }
        do {
            try realm.commitWrite()
        } catch {
            errorHandler(error)
        }
    }

    private func rollback() {
        guard realm.isInWriteTransaction else { return }
        realm.cancelWrite()
        metadata = [:]
    }
}

{'fraction_non_alphanumeric': 0.09343522056269538, 'fraction_numerical': 0.0069468565474122956, 'mean_word_length': 2.3449477351916377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '2877805', 'n_tokens_mistral': 795, 'n_tokens_neox': 759, 'n_words': 200}