Skip to content

Commit

Permalink
Added async tests and fixed async adding
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-ludwig committed Nov 7, 2023
1 parent 189394d commit 7b6c376
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
65 changes: 45 additions & 20 deletions SearchKitDemo/Indexer/SearchIndexer+AsyncManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import Foundation

extension SearchIndexer {
/// Manager for SearchIndexer objct that supports async calls to the index
public class AsyncManager {
let index: SearchIndexer
class AsyncManager {
public let index: SearchIndexer


/// Queue for handling async modifications to the index
// fileprivate let modifyQueue = DispatchQueue(label: "com.SearchkitDemo.modifyQueue", attributes: .concurrent)

init(index: SearchIndexer) {
self.index = index
}
Expand All @@ -36,8 +35,9 @@ extension SearchIndexer {
}
}

// MARK: - Search
/// A task nor handling searches
class SearchTask: AsyncManager {
class SearchTask {
private var search: SearchIndexer.ProgressivSearch

let query: String
Expand All @@ -47,7 +47,6 @@ extension SearchIndexer {
init(_ index: SearchIndexer, query: String) {
self.query = query
self.search = index.progressiveSearch(query: query)
super.init(index: index)
}

deinit {
Expand All @@ -70,7 +69,8 @@ extension SearchIndexer {
}
}

class AddTask: AsyncManager {
// MARK: - Add

private let addQueue = DispatchQueue(label: "com.SearchkitDemo.addQueue", attributes: .concurrent)

func addText(
Expand Down Expand Up @@ -100,23 +100,49 @@ extension SearchIndexer {
func addFiles(
urls: [URL],
flushWhenComplete: Bool = false
) {
let dispatchGroup = DispatchGroup()
) async -> [Bool] {
var addedURLs = [Bool]()

for url in urls {
dispatchGroup.enter()
addQueue.async { [weak self] in
guard let self = self else { return }
let _ = self.index.add(fileURL: url, canReplace: false)
dispatchGroup.leave()
await withTaskGroup(of: Bool.self) { taskGroup in
for url in urls {
taskGroup.addTask {
return self.index.add(fileURL: url, canReplace: false)
}
}
}

dispatchGroup.notify(queue: .main) {
if flushWhenComplete {
self.index.flush()

for await results in taskGroup {
addedURLs.append(results)
}
}

return addedURLs


// let dispatchGroup = DispatchGroup()
//
// for url in urls {
// dispatchGroup.enter()
//
// addQueue.async { [weak self] in
//
// guard let self = self else {
// print("self isn't self")
// return
// }
// print("first URL")
// let results = self.index.add(fileURL: url, canReplace: false)
// addedURLs.append(results)
// dispatchGroup.leave()
// }
// }
//
// dispatchGroup.notify(queue: .main) {
// print("test")
// if flushWhenComplete {
// self.index.flush()
// }
// completion(addedURLs)
// }
}

func addFolder(
Expand Down Expand Up @@ -152,7 +178,6 @@ extension SearchIndexer {
}
}
}
}
}
}

Expand Down
22 changes: 21 additions & 1 deletion SearchKitDemoTests/AsyncIndexSearchingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ final class AsyncIndexSearchingTests: XCTestCase {
let filePath = bundleResourceURL(forResource: "APACHE_LICENSE", withExtension: "pdf")
let txtPath = bundleResourceURL(forResource: "the_school_short_story", withExtension: "txt")

let asyncManager = SearchIndexer.AsyncManager.SearchTask

let asyncManager = SearchIndexer.AsyncManager(index: indexer)
let expectation = XCTestExpectation(description: "Async operations completed")
Task {
let result = await asyncManager.addFiles(urls: [filePath, txtPath])
print(result.count)
XCTAssertEqual(result.count, 2)
asyncManager.index.flush()
print(asyncManager.index.documents())
expectation.fulfill()
}

wait(for: [expectation], timeout: 5)
}

func testAsyncPerformance() {
self.measure {
Task {
testAddDocuments()
}
}
}
}

0 comments on commit 7b6c376

Please sign in to comment.