<commit_before>//
//  ManagedObjectContext.swift
//  AppwiseCore
//
//  Created by <NAME> on 06/03/2017.
//  Copyright © 2019 Appwise. All rights reserved.
//

import CoreData

public extension NSManagedObjectContext {
	public enum Error: Swift.Error {
		case entityNotFound
		case unsupportedIdentityAttributes
		case unableToCastObjectTo(type: NSManagedObject.Type)
	}

	/// Find the first object matching a given value, using the Groot unique key.
	///
	/// - parameter value: The value to match.
	///
	/// - returns: The found object or nil.
	public func first<T: NSManagedObject>(value: Any) throws -> T? {
		let keys = T.entity().keysForUniquing
		guard keys.count == 1, let key = keys.first else {
			throw Error.unsupportedIdentityAttributes
		}

		return first(key: key, value: value)
	}

	/// Find the first object matching a given value, using a given key.
	///
	/// - parameter key: The key to search.
	/// - parameter value: The value to match.
	///
	/// - returns: The found object or nil.
	public func first<T: NSManagedObject>(key: PartialKeyPath<T>, value: Any) -> T? {
		guard let keyPath = key._kvcKeyPathString else { return nil }
		return first(key: keyPath, value: value)
	}

	private func first<T: NSManagedObject>(key: String, value: Any) -> T? {
		let predicate = NSPredicate(format: "%K = %@", argumentArray: [key, value])
		return first(predicate: predicate)
	}

	/// Find the first object matching a given predicate and sorting.
	///
	/// - parameter sortDescriptor: The descriptor to sort against.
	/// - parameter predicate: The predicate to search with.
	///
	/// - returns: The found object or nil.
	public func first<T: NSManagedObject>(sortDescriptor: NSSortDescriptor? = nil, predicate: NSPredicate? = nil) -> T? {
		T.fetchRequest()
		let request = T.fetchRequest(
			predicate: predicate,
			sortDescriptors: [sortDescriptor].compactMap { $0 },
			limit: 1
		)

		guard let result = try? fetch(request) else { return nil }
		return result.first
	}

	/// Convert an object from one context into an instance from this context.
	///
	/// - parameter item: The object to convert.
	///
	/// - returns: The converted object or nil.
	public func inContext<T: NSManagedObject>(_ item: T) throws -> T {
		if item.objectID.isTemporaryID {
			try item.managedObjectContext?.obtainPermanentIDs(for: [item])
		}

		guard let result = try existingObject(with: item.objectID) as? T else {
			throw Error.unableToCastObjectTo(type: T.self)
		}
		return result
	}

	/// Find all the old unmodified objects in this context, matching a predicate.
	///
	/// - parameter filter: The predicate to match against.
	///
	/// - returns: A list of old objects.
	public func findOldItems<T: NSManagedObject>(filter: NSPredicate? = nil) -> [T] {
		let request = T.fetchRequest(predicate: filter).then {
			$0.returnsObjectsAsFaults = true
			$0.includesPropertyValues = false
		}

		let newIDs = Set<NSManagedObjectID>(
			updatedObjects.compactMap { ($0 as? T)?.objectID } +
			insertedObjects.compactMap { ($0 as? T)?.objectID }
		)

		let fetched: [T] = (try? self.fetch(request)) ?? []
		return fetched.filter { !newIDs.contains($0.objectID) }
	}

	/// Specifies objects that should be removed from their persistent store when changes are committed.
	///
	/// - parameter items: A list of objects
	func delete<T: NSManagedObject>(_ items: [T]) {
		for item in items {<commit_msg>Fix some import issues (Core Data Error 133000)<commit_after>//
//  ManagedObjectContext.swift
//  AppwiseCore
//
//  Created by <NAME> on 06/03/2017.
//  Copyright © 2019 Appwise. All rights reserved.
//

import CoreData

public extension NSManagedObjectContext {
	public enum Error: Swift.Error {
		case entityNotFound
		case unsupportedIdentityAttributes
		case unableToCastObjectTo(type: NSManagedObject.Type)
	}

	/// Find the first object matching a given value, using the Groot unique key.
	///
	/// - parameter value: The value to match.
	///
	/// - returns: The found object or nil.
	public func first<T: NSManagedObject>(value: Any) throws -> T? {
		let keys = T.entity().keysForUniquing
		guard keys.count == 1, let key = keys.first else {
			throw Error.unsupportedIdentityAttributes
		}

		return first(key: key, value: value)
	}

	/// Find the first object matching a given value, using a given key.
	///
	/// - parameter key: The key to search.
	/// - parameter value: The value to match.
	///
	/// - returns: The found object or nil.
	public func first<T: NSManagedObject>(key: PartialKeyPath<T>, value: Any) -> T? {
		guard let keyPath = key._kvcKeyPathString else { return nil }
		return first(key: keyPath, value: value)
	}

	private func first<T: NSManagedObject>(key: String, value: Any) -> T? {
		let predicate = NSPredicate(format: "%K = %@", argumentArray: [key, value])
		return first(predicate: predicate)
	}

	/// Find the first object matching a given predicate and sorting.
	///
	/// - parameter sortDescriptor: The descriptor to sort against.
	/// - parameter predicate: The predicate to search with.
	///
	/// - returns: The found object or nil.
	public func first<T: NSManagedObject>(sortDescriptor: NSSortDescriptor? = nil, predicate: NSPredicate? = nil) -> T? {
		T.fetchRequest()
		let request = T.fetchRequest(
			predicate: predicate,
			sortDescriptors: [sortDescriptor].compactMap { $0 },
			limit: 1
		)

		guard let result = try? fetch(request) else { return nil }
		return result.first
	}

	/// Convert an object from one context into an instance from this context.
	///
	/// - parameter item: The object to convert.
	///
	/// - returns: The converted object or nil.
	public func inContext<T: NSManagedObject>(_ item: T) throws -> T {
		if item.objectID.isTemporaryID {
			try item.managedObjectContext?.obtainPermanentIDs(for: [item])
		}

		guard let result = try object(with: item.objectID) as? T else {
			throw Error.unableToCastObjectTo(type: T.self)
		}
		return result
	}

	/// Find all the old unmodified objects in this context, matching a predicate.
	///
	/// - parameter filter: The predicate to match against.
	///
	/// - returns: A list of old objects.
	public func findOldItems<T: NSManagedObject>(filter: NSPredicate? = nil) -> [T] {
		let request = T.fetchRequest(predicate: filter).then {
			$0.returnsObjectsAsFaults = true
			$0.includesPropertyValues = false
		}

		let newIDs = Set<NSManagedObjectID>(
			updatedObjects.compactMap { ($0 as? T)?.objectID } +
			insertedObjects.compactMap { ($0 as? T)?.objectID }
		)

		let fetched: [T] = (try? self.fetch(request)) ?? []
		return fetched.filter { !newIDs.contains($0.objectID) }
	}

	/// Specifies objects that should be removed from their persistent store when changes are committed.
	///
	/// - parameter items: A list of objects
	func delete<T: NSManagedObject>(_ items: [T]) {
		for item in items {
{'fraction_non_alphanumeric': 0.12338425381903642, 'fraction_numerical': 0.006756756756756757, 'mean_word_length': 3.2931904161412358, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '3737399', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2062, 'n_words': 703}