Skip to content

Remove unused code in NavigatorTree #961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sources/SwiftDocC/Indexing/Navigator/NavigatorIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ public class NavigatorIndex {
navigatorTree = try NavigatorTree.read(
from: url.appendingPathComponent("navigator.index", isDirectory: false),
bundleIdentifier: bundleIdentifier,
interfaceLanguages: availabilityIndex.interfaceLanguages,
presentationIdentifier: presentationIdentifier,
onNodeRead: onNodeRead)
} else {
Expand Down Expand Up @@ -394,7 +393,7 @@ public class NavigatorIndex {
*/
public func readNavigatorTree(timeout: TimeInterval, delay: TimeInterval = 0.01, queue: DispatchQueue, broadcast: NavigatorTree.BroadcastCallback?) throws {
let indexURL = url.appendingPathComponent("navigator.index")
try navigatorTree.read(from: indexURL, bundleIdentifier: bundleIdentifier, interfaceLanguages: availabilityIndex.interfaceLanguages, timeout: timeout, delay: delay, queue: queue, broadcast: broadcast)
try navigatorTree.read(from: indexURL, bundleIdentifier: bundleIdentifier, timeout: timeout, delay: delay, queue: queue, broadcast: broadcast)
}

// MARK: - Data Query
Expand Down
21 changes: 6 additions & 15 deletions Sources/SwiftDocC/Indexing/Navigator/NavigatorTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,23 @@ public class NavigatorTree {
/// The broadcast callback notifies a listener about the latest items loaded from the disk.
public typealias BroadcastCallback = (_ items: [NavigatorTree.Node], _ isCompleted: Bool, _ error: Error?) -> Void

// A reference to the reading cursor.
internal var readingCursor: ReadingCursor? = nil

/**
Read a tree from disk from a given path.
The read is atomically performed, which means it reads all the content of the file from the disk and process the tree from loaded data.

- Parameters:
- url: The file URL from which the tree should be read.
- bundleIdentifier: The bundle identifier used to generate the mapping topicID to node on tree.
- interfaceLanguages: A set containing the indication about the interface languages a tree contains.
- timeout: The amount of time we can load a batch of items from data, once the timeout time pass,
the reading process will reschedule asynchronously using the given queue.
- delay: The delay to wait before schedule the next read. Default: 0.01 seconds.
- queue: The queue to use.
- presentationIdentifier: Defines if nodes should have a presentation identifier useful in presentation contexts.
- broadcast: The callback to update get updates of the current process.
*/
public func read(from url: URL, bundleIdentifier: String? = nil, interfaceLanguages: Set<InterfaceLanguage>, timeout: TimeInterval, delay: TimeInterval = 0.01, queue: DispatchQueue, presentationIdentifier: String? = nil, broadcast: BroadcastCallback?) throws {
public func read(from url: URL, bundleIdentifier: String? = nil, timeout: TimeInterval, delay: TimeInterval = 0.01, queue: DispatchQueue, presentationIdentifier: String? = nil, broadcast: BroadcastCallback?) throws {
let data = try Data(contentsOf: url)
let readingCursor = ReadingCursor(data: data)
self.readingCursor = readingCursor

func __read() {
let deadline = DispatchTime.now() + timeout
Expand All @@ -141,7 +136,6 @@ public class NavigatorTree {

guard let item = NavigatorItem(rawValue: objectData) else {
broadcast?(processedNodes, false, Error.invalidData)
self.readingCursor = nil
break
}

Expand Down Expand Up @@ -171,7 +165,6 @@ public class NavigatorTree {
queue.async(flags: .barrier) {
broadcast?(processedNodes, true, nil)
}
self.readingCursor = nil
}
}

Expand All @@ -183,6 +176,11 @@ public class NavigatorTree {

self.root = root
}

@available(*, deprecated, renamed: "read(from:bundleIdentifier:timeout:delay:queue:presentationIdentifier:broadcast:)", message: "Use 'read(from:bundleIdentifier:timeout:delay:queue:presentationIdentifier:broadcast:)' instead. This deprecated API will be removed after 6.1 is released")
public func read(from url: URL, bundleIdentifier: String? = nil, interfaceLanguages: Set<InterfaceLanguage>, timeout: TimeInterval, delay: TimeInterval = 0.01, queue: DispatchQueue, presentationIdentifier: String? = nil, broadcast: BroadcastCallback?) throws {
try self.read(from: url, bundleIdentifier: bundleIdentifier, timeout: timeout, delay: delay, queue: queue, presentationIdentifier: presentationIdentifier, broadcast: broadcast)
}

/**
Serialize the node and descendants to the disk.
Expand Down Expand Up @@ -261,33 +259,28 @@ public class NavigatorTree {
- Parameters:
- url: The file URL from which the tree should be read.
- bundleIdentifier: The bundle identifier used to generate the mapping topicID to node on tree.
- interfaceLanguages: A set containing the indication about the interface languages a tree contains.
- atomically: Defines if the read should be atomic.
- presentationIdentifier: Defines if nodes should have a presentation identifier useful in presentation contexts.
- onNodeRead: An action to perform after reading a node. This allows clients to perform arbitrary actions on the node while it is being read from disk. This is useful for clients wanting to attach data to ``NavigatorTree/Node/attributes``.
*/
static func read(
from url: URL,
bundleIdentifier: String? = nil,
interfaceLanguages: Set<InterfaceLanguage>,
atomically: Bool = true,
presentationIdentifier: String? = nil,
onNodeRead: ((NavigatorTree.Node) -> Void)? = nil
) throws -> NavigatorTree {
let interfaceLanguageMap = Dictionary(uniqueKeysWithValues: interfaceLanguages.map{ ($0.mask, $0)})
let path = url.path
if atomically {
return try readAtomically(
from: path,
bundleIdentifier: bundleIdentifier,
interfaceLanguageMap: interfaceLanguageMap,
presentationIdentifier: presentationIdentifier,
onNodeRead: onNodeRead)
}
return try read(
from: path,
bundleIdentifier: bundleIdentifier,
interfaceLanguageMap: interfaceLanguageMap,
presentationIdentifier: presentationIdentifier,
onNodeRead: onNodeRead)
}
Expand All @@ -296,7 +289,6 @@ public class NavigatorTree {
fileprivate static func readAtomically(
from path: String,
bundleIdentifier: String? = nil,
interfaceLanguageMap: [InterfaceLanguage.ID: InterfaceLanguage],
presentationIdentifier: String? = nil,
onNodeRead: ((NavigatorTree.Node) -> Void)? = nil
) throws -> NavigatorTree {
Expand Down Expand Up @@ -347,7 +339,6 @@ public class NavigatorTree {
fileprivate static func read(
from path: String,
bundleIdentifier: String? = nil,
interfaceLanguageMap: [InterfaceLanguage.ID: InterfaceLanguage],
presentationIdentifier: String? = nil,
onNodeRead: ((NavigatorTree.Node) -> Void)? = nil
) throws -> NavigatorTree {
Expand Down
17 changes: 7 additions & 10 deletions Tests/SwiftDocCTests/Indexing/NavigatorIndexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Root

let original = NavigatorTree(root: root)
try original.write(to: indexURL)
let readTree = try NavigatorTree.read(from: indexURL, bundleIdentifier: testBundleIdentifier, interfaceLanguages: [.swift], atomically: true)
let readTree = try NavigatorTree.read(from: indexURL, bundleIdentifier: testBundleIdentifier, atomically: true)

XCTAssertEqual(original.root.countItems(), readTree.root.countItems())
XCTAssertTrue(compare(lhs: original.root, rhs: readTree.root))
Expand All @@ -195,7 +195,7 @@ Root
XCTAssertTrue(validateTree(node: readTree.root, validator: bundleIdentifierValidator), "The tree has bundle identifier missing.")
XCTAssertTrue(validateTree(node: readTree.root, validator: emptyPresentationIdentifierValidator), "The tree has a presentation identifier set which should not be present.")

let treeWithPresentationIdentifier = try NavigatorTree.read(from: indexURL, bundleIdentifier: testBundleIdentifier, interfaceLanguages: [.swift], atomically: true, presentationIdentifier: "com.example.test")
let treeWithPresentationIdentifier = try NavigatorTree.read(from: indexURL, bundleIdentifier: testBundleIdentifier, atomically: true, presentationIdentifier: "com.example.test")

let presentationIdentifierValidator: (NavigatorTree.Node) -> Bool = { node in
return node.presentationIdentifier == "com.example.test"
Expand All @@ -213,7 +213,6 @@ Root
let treeWithAttributes = try NavigatorTree.read(
from: indexURL,
bundleIdentifier: testBundleIdentifier,
interfaceLanguages: [.swift],
atomically: true,
presentationIdentifier: "com.example.test",
onNodeRead: addAttributes
Expand All @@ -232,7 +231,6 @@ Root
let treeWithAttributesNonAtomic = try NavigatorTree.read(
from: indexURL,
bundleIdentifier: testBundleIdentifier,
interfaceLanguages: [.swift],
atomically: false,
presentationIdentifier: "com.example.test",
onNodeRead: addAttributes
Expand All @@ -257,7 +255,6 @@ Root
_ = try NavigatorTree.read(
from: indexURL,
bundleIdentifier: uniqueTestBundleIdentifier,
interfaceLanguages: [.swift],
atomically: true
)

Expand All @@ -275,7 +272,7 @@ Root
try original.write(to: indexURL)

measure {
_ = try! NavigatorTree.read(from: indexURL, interfaceLanguages: [.swift], atomically: true)
_ = try! NavigatorTree.read(from: indexURL, atomically: true)
}
#endif
}
Expand All @@ -297,7 +294,7 @@ Root
let expectation = XCTestExpectation(description: "Load the tree asynchronously.")

let readTree = NavigatorTree()
try! readTree.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main) { (nodes, completed, error) in
try! readTree.read(from: indexURL, timeout: 0.25, queue: DispatchQueue.main) { (nodes, completed, error) in
counter += 1
XCTAssertNil(error)
if completed { expectation.fulfill() }
Expand All @@ -310,7 +307,7 @@ Root

let expectation2 = XCTestExpectation(description: "Load the tree asynchronously, again with presentation identifier.")
let readTreePresentationIdentifier = NavigatorTree()
try! readTreePresentationIdentifier.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main, presentationIdentifier: "com.example.test") { (nodes, completed, error) in
try! readTreePresentationIdentifier.read(from: indexURL, timeout: 0.25, queue: DispatchQueue.main, presentationIdentifier: "com.example.test") { (nodes, completed, error) in
XCTAssertNil(error)
if completed { expectation2.fulfill() }
}
Expand All @@ -336,7 +333,7 @@ Root
let indexURL = targetURL.appendingPathComponent("nav.index")

let readTree = NavigatorTree()
XCTAssertThrowsError(try readTree.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main, broadcast: nil))
XCTAssertThrowsError(try readTree.read(from: indexURL, timeout: 0.25, queue: DispatchQueue.main, broadcast: nil))

try XCTAssertEqual(
RenderIndex.fromURL(targetURL.appendingPathComponent("index.json")),
Expand Down Expand Up @@ -371,7 +368,7 @@ Root
let expectation = XCTestExpectation(description: "Load the tree asynchronously.")

let readTree = NavigatorTree()
try! readTree.read(from: indexURL, interfaceLanguages: [.swift], timeout: 0.25, queue: DispatchQueue.main) { (nodes, completed, error) in
try! readTree.read(from: indexURL, timeout: 0.25, queue: DispatchQueue.main) { (nodes, completed, error) in
counter += 1
XCTAssertNil(error)
if completed { expectation.fulfill() }
Expand Down