<commit_before>//
//  LevelDB.swift
//
//  Copyright 2011-2016 Pave Labs. All rights reserved.
//
//  Modified by: <NAME> <<EMAIL>>
//  Date: Aug 2016
//
//  See LICENCE for details.
//

import Foundation
import CLevelDB

public typealias LevelDBKeyCallback = (String, UnsafeMutablePointer<Bool>) -> Void

public func SearchPathForDirectoriesInDomains(_ directory: FileManager.SearchPathDirectory, _ domainMask: FileManager.SearchPathDomainMask, _ expandTilde: Bool) -> [String] {
    let bundle = Bundle.main
    let bundlePath = bundle.bundlePath
    if domainMask == .userDomainMask {
        switch directory {
        case .libraryDirectory:
            return [bundlePath + "/Library"]
        case .documentDirectory:
            return [bundlePath + "/Documents"]
        default:
            break
        }
    }
    return [""]
}

open class LevelDB {
    public let serialQueue = DispatchQueue(label: "org.amr.leveldb")
    
    var name: String
    var path: String
	public var dictionaryEncoder: (String, [String : Any]) -> Data?
    public var dictionaryDecoder: (String, Data) -> [String : Any]?
    public var encoder: (String, Data) -> Data?
    public var decoder: (String, Data) -> Data?
    public var db: UnsafeMutableRawPointer?
    
    // MARK: - Life cycle
    
    required public init(path: String, name: String) {
        //NSLog("LevelDB init")
        self.name = name
        //NSLog("LevelDB self.name: \(name)")
        self.path = path
        //NSLog("LevelDB path: \(path)")
		self.dictionaryEncoder = { key, value in
				#if DEBUG
					NSLog("No encoder block was set for this database [\(name)]")
					NSLog("Using a convenience encoder/decoder pair using NSKeyedArchiver.")
				#endif
				return Data(bytes: key.cString, count: key.count)
			}
			//NSLog("LevelDB self.encoder")
		self.dictionaryDecoder = {key, data in
			return ["" : ""]
		}
        self.encoder = { key, value in
            #if DEBUG
                NSLog("No encoder block was set for this database [\(name)]")
                NSLog("Using a convenience encoder/decoder pair using NSKeyedArchiver.")
            #endif
            return Data(bytes: key.cString, count: key.count)
        }
        //NSLog("LevelDB self.encoder")
        self.decoder = {key, data in
            return Data()
        }
        //NSLog("LevelDB self.decoder")
        #if os(Linux)
            do {
                let dirpath =  NSURL(fileURLWithPath:path).deletingLastPathComponent?.path ?? ""
                //NSLog("LevelDB dirpath: \(dirpath)")
                let fileManager = FileManager.default
                if !fileManager.fileExists(atPath: dirpath) {
                    try fileManager.createDirectory(atPath: dirpath, withIntermediateDirectories:false, attributes:nil)
                }
                //NSLog("try FileManager.default")
            } catch {
                NSLog("Problem creating parent directory: \(error)")
            }
        #endif
        self.open()
    }
    
    convenience public init(name: String) {
        self.init(path: name, name: name)
    }
    
    deinit {
        self.close()
    }
    
    // MARK: - Class methods
    
    public class func getLibraryPath() -> String {
        #if os(Linux)
            let paths = SearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
            return paths[0]
        #else
            let libraryDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
            return libraryDirectory.absoluteString
        #endif
    }
    
    // MARK: - Accessors
    
    open func description() -> String {
        return "<LevelDB:\(self) path: \(path)>"
    }
    
    open func setValue<T: Codable>(_ value: T, forKey key: String) {
        serialQueue.async {
            guard let db = self.db else {
                NSLog("Database reference is not existent (it has probably been closed)")
                return
            }
            //if let newValue = value {
            do {
                let newData = try JSONEncoder().encode(value)
                var status = 0
                if let data = self.encoder(key, newData) {
                    var localData = data
                    localData.withUnsafeMutableBytes { (mutableBytes: UnsafeMutablePointer<UInt8>) -> () in
                        status = levelDBItemPut(db, key.cString, key.count, mutableBytes, data.count)
                    }
                    if status != 0 {
                        NSLog("setValue: Problem storing key/value pair in database")
                    }
                } else {
                    NSLog("Error: setValue: encoder(key, newValue) returned nil, key: \(key), value: \(value)")
                }
            } catch {
                NSLog("LevelDB setValue error: \(error)")
            }
        }
    }
    
    open subscript<T:Codable>(key: String) -> T? {
        get {
            // return an appropriate subscript value here
            return valueForKey(key)
        }<commit_msg>case .search, .searchReply:<commit_after>//
//  LevelDB.swift
//
//  Copyright 2011-2016 Pave Labs. All rights reserved.
//
//  Modified by: <NAME> <<EMAIL>>
//  Date: Aug 2016
//
//  See LICENCE for details.
//

import Foundation
import CLevelDB

public typealias LevelDBKeyCallback = (String, UnsafeMutablePointer<Bool>) -> Void

public func SearchPathForDirectoriesInDomains(_ directory: FileManager.SearchPathDirectory, _ domainMask: FileManager.SearchPathDomainMask, _ expandTilde: Bool) -> [String] {
    let bundle = Bundle.main
    let bundlePath = bundle.bundlePath
    if domainMask == .userDomainMask {
        switch directory {
        case .libraryDirectory:
            return [bundlePath + "/Library"]
        case .documentDirectory:
            return [bundlePath + "/Documents"]
        default:
            break
        }
    }
    return [""]
}

open class LevelDB {
    public let serialQueue = DispatchQueue(label: "org.amr.leveldb")
    
    var name: String
    var path: String
	public var dictionaryEncoder: (String, [String : Any]) -> Data?
    public var dictionaryDecoder: (String, Data) -> [String : Any]?
    public var encoder: (String, Data) -> Data?
    public var decoder: (String, Data) -> Data?
    public var db: UnsafeMutableRawPointer?
    
    // MARK: - Life cycle
    
    required public init(path: String, name: String) {
        //NSLog("LevelDB init")
        self.name = name
        //NSLog("LevelDB self.name: \(name)")
        self.path = path
        //NSLog("LevelDB path: \(path)")
		self.dictionaryEncoder = { key, value in
				#if DEBUG
					NSLog("No encoder block was set for this database [\(name)]")
					NSLog("Using a convenience encoder/decoder pair using NSKeyedArchiver.")
				#endif
				return Data(bytes: key.cString, count: key.count)
			}
			//NSLog("LevelDB self.encoder")
		self.dictionaryDecoder = {key, data in
			return ["" : ""]
		}
        self.encoder = { key, value in
            #if DEBUG
                NSLog("No encoder block was set for this database [\(name)]")
                NSLog("Using a convenience encoder/decoder pair using NSKeyedArchiver.")
            #endif
            return Data(bytes: key.cString, count: key.count)
        }
        //NSLog("LevelDB self.encoder")
        self.decoder = {key, data in
            return Data()
        }
        //NSLog("LevelDB self.decoder")
        #if os(Linux)
            do {
                let dirpath =  NSURL(fileURLWithPath:path).deletingLastPathComponent?.path ?? ""
                //NSLog("LevelDB dirpath: \(dirpath)")
                let fileManager = FileManager.default
                if !fileManager.fileExists(atPath: dirpath) {
                    try fileManager.createDirectory(atPath: dirpath, withIntermediateDirectories:false, attributes:nil)
                }
                //NSLog("try FileManager.default")
            } catch {
                NSLog("Problem creating parent directory: \(error)")
            }
        #endif
        self.open()
    }
    
    convenience public init(name: String) {
        self.init(path: name, name: name)
    }
    
    deinit {
        self.close()
    }
    
    // MARK: - Class methods
    
    public class func getLibraryPath() -> String {
        #if os(Linux)
            let paths = SearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
            return paths[0]
        #else
            let libraryDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
            return libraryDirectory.absoluteString
        #endif
    }
    
    // MARK: - Accessors
    
    open func description() -> String {
        return "<LevelDB:\(self) path: \(path)>"
    }
    
    open func setValue<T: Codable>(_ value: T, forKey key: String) {
        serialQueue.async {
            guard let db = self.db else {
                NSLog("Database reference is not existent (it has probably been closed)")
                return
            }
            do {
                let newData = try JSONEncoder().encode(value)
                var status = 0
                if let data = self.encoder(key, newData) {
                    var localData = data
                    localData.withUnsafeMutableBytes { (mutableBytes: UnsafeMutablePointer<UInt8>) -> () in
                        status = levelDBItemPut(db, key.cString, key.count, mutableBytes, data.count)
                    }
                    if status != 0 {
                        NSLog("setValue: Problem storing key/value pair in database")
                    }
                } else {
                    NSLog("Error: setValue: encoder(key, newValue) returned nil, key: \(key), value: \(value)")
                }
            } catch {
                NSLog("LevelDB setValue error: \(error)")
            }
        }
    }
    
    open subscript<T:Codable>(key: String) -> T? {
        get {
            // return an appropriate subscript value here
            return valueForKey(key)
        }
{'fraction_non_alphanumeric': 0.10707911957168352, 'fraction_numerical': 0.003172714653975808, 'mean_word_length': 1.8342230963753863, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, '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': '953150', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2706, 'n_words': 746}