<commit_before>//
//  MetaDataReader.swift
//  AudioPlayer
//
//  Created by <NAME> on 8/7/17.
//  Copyright © 2017 ShortSands. All rights reserved.
//

import AWS

class MetaDataReader {
    
    var oldTestament: TOCAudioBible?
    var newTestament: TOCAudioBible?
    var metaDataVerse: TOCAudioChapter?
    
    init() {
        self.oldTestament = nil
        self.newTestament = nil
    }
    
    deinit {
        print("***** Deinit MetaDataReader *****")
    }

    func read(versionCode: String, complete: @escaping (_ oldTest:TOCAudioBible?, _ newTest:TOCAudioBible?) -> Void) {
        let db = Sqlite3()
        let query = "SELECT a.damId, a.collectionCode, a.mediaType, a.dbpLanguageCode, a.dbpVersionCode" +
                " FROM audio a, audioVersion v" +
                " WHERE a.dbpLanguageCode = v.dbpLanguageCode" +
                " AND a.dbpVersionCode = v.dbpVersionCode" +
                " AND v.versionCode = ?" +
                " ORDER BY mediaType ASC, collectionCode ASC"
                // mediaType sequence Drama, NonDrama
                // collectionCode sequence NT, ON, OT
        do {
            try db.open(dbPath: "Versions.db", copyIfAbsent: true)
            defer { db.close() }
            try db.queryV1(sql: query, values: [versionCode], complete: { resultSet in
                print("LENGTH \(resultSet.count)")
                for row in resultSet {
                    let item = TOCAudioBible(database: db, mediaSource: "FCBH", dbRow: row)
                    print("\(item.toString())")
                    // Because of the sort sequence, the following logic prefers Drama over Non-Drama
                    // Because of the sequenc of IF's, it prefers OT and NT over ON
                    if self.newTestament == nil && item.collectionCode == "NT" {
                        self.newTestament = item
                    }
                    if self.newTestament == nil && item.collectionCode == "ON" {
                        self.newTestament = item
                    }
                    if self.oldTestament == nil && item.collectionCode == "OT" {
                        self.oldTestament = item
                    }
                    if self.oldTestament == nil && item.collectionCode == "ON" {
                        self.oldTestament = item
                    }
                }
                complete(self.oldTestament, self.newTestament)
            })
        } catch let err {
            print("ERROR \(Sqlite3.errorDescription(error: err))")
            complete(nil, nil)
        }
    }
    /**
    * This function will only return results after read has been called.
    */
    func findBook(bookId: String) -> TOCAudioBook? {<commit_msg>Change MetaDataReader to only instantiate TOCAudioBible that will be used.<commit_after>//
//  MetaDataReader.swift
//  AudioPlayer
//
//  Created by <NAME> on 8/7/17.
//  Copyright © 2017 ShortSands. All rights reserved.
//

import AWS

class MetaDataReader {
    
    var oldTestament: TOCAudioBible?
    var newTestament: TOCAudioBible?
    var metaDataVerse: TOCAudioChapter?
    
    init() {
        self.oldTestament = nil
        self.newTestament = nil
    }
    
    deinit {
        print("***** Deinit MetaDataReader *****")
    }

    func read(versionCode: String, complete: @escaping (_ oldTest:TOCAudioBible?, _ newTest:TOCAudioBible?) -> Void) {
        let db = Sqlite3()
        let query = "SELECT a.damId, a.collectionCode, a.mediaType, a.dbpLanguageCode, a.dbpVersionCode" +
                " FROM audio a, audioVersion v" +
                " WHERE a.dbpLanguageCode = v.dbpLanguageCode" +
                " AND a.dbpVersionCode = v.dbpVersionCode" +
                " AND v.versionCode = ?" +
                " ORDER BY mediaType ASC, collectionCode ASC"
                // mediaType sequence Drama, NonDrama
                // collectionCode sequence NT, ON, OT
        do {
            try db.open(dbPath: "Versions.db", copyIfAbsent: true)
            defer { db.close() }
            try db.queryV1(sql: query, values: [versionCode], complete: { resultSet in
                print("LENGTH \(resultSet.count)")
                var oldTestRow: [String?]? = nil
                var newTestRow: [String?]? = nil
                for row in resultSet {
                    // Because of the sort sequence, the following logic prefers Drama over Non-Drama
                    // Because of the sequenc of IF's, it prefers OT and NT over ON
                    let collectionCode = row[1]!
                    if newTestRow == nil && collectionCode == "NT" {
                        newTestRow = row
                    }
                    if newTestRow == nil && collectionCode == "ON" {
                        newTestRow = row
                    }
                    if oldTestRow == nil && collectionCode == "OT" {
                        oldTestRow = row
                    }
                    if oldTestRow == nil && collectionCode == "ON" {
                        oldTestRow = row
                    }
                }
                if let oldRow = oldTestRow {
                    self.oldTestament = TOCAudioBible(database: db, mediaSource: "FCBH", dbRow: oldRow)
                }
                if let newRow = newTestRow {
                    self.newTestament = TOCAudioBible(database: db, mediaSource: "FCBH", dbRow: newRow)
                }
                complete(self.oldTestament, self.newTestament)
            })
        } catch let err {
            print("ERROR \(Sqlite3.errorDescription(error: err))")
            complete(nil, nil)
        }
    }
    /**
    * This function will only return results after read has been called.
    */
    func findBook(bookId: String) -> TOCAudioBook? {
{'fraction_non_alphanumeric': 0.09892584892584892, 'fraction_numerical': 0.003984753984753985, 'mean_word_length': 1.5375824175824175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '140707', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1530, 'n_words': 419}