Skip to main content

BrowseCrawler

Overview​

BrowseCrawler uses a BFS queue to walk the whole AddressSpace subtree, with two guards (max_depth + max_nodes). The SDK is a purely synchronous implementation with zero third-party dependencies; it does not use Tokio.

API​

MembersCategoryReading and WritingNote
BrowseCrawler::new(&ua)Structure—Accepts only 1 argument; default values: max_depth=10 / max_nodes=100,000
.max_depth(n)Chain-type—Depth Limit
.max_nodes(n)Chain-type—Limited Edition
.node_class_filter(nc)Chain-type—Filter NodeClass by
.progress(cb)Chain-type—Progress callback FnMut(usize, &str)
crawl(root_node_id)MethodsReadReturns Result<CrawlResult, OpcUaError>, Consumption self
CrawlResult.all_nodesFieldReadVec<CrawlNode>
CrawlResult.children_by_parentFieldReadHashMap<String, Vec<String>>
CrawlResult.elapsedFieldReadDuration

CrawlNode Fields: node_id / display_name / browse_name / node_class / parent_node_id (where "root" is an empty string).

Code Example​

use darra_opcua::{BrowseCrawler, NodeClass, Session, well_known_nodes};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://localhost:4840")?;
ua.connect()?;

// 1) Climb the entire tree ObjectsFolder
let crawler = BrowseCrawler::new(&ua)
.max_depth(8)
.max_nodes(50_000)
.node_class_filter(NodeClass::Unspecified)
.progress(|count, nid| print!("\r已抓 {} 个, 当前 {}", count, nid));

let result = crawler.crawl(well_known_nodes::OBJECTS_FOLDER)?;

println!("\n共 {} 节点, 耗时 {:.1}s",
result.all_nodes.len(), result.elapsed.as_secs_f64());

// 2) Variable nodes only (crawl consumes self, reconstructed each round)
let r2 = BrowseCrawler::new(&ua)
.max_depth(10)
.max_nodes(100_000)
.node_class_filter(NodeClass::Variable)
.crawl("ns=2;s=Boilers")?;
for n in &r2.all_nodes {
println!(" {} ({}) parent={}", n.browse_name, n.node_id, n.parent_node_id);
}

// 3) Early termination: `crawl` is a synchronous, blocking call; it does not SDK support CancellationToken,
// The only built-in truncation point is max_depth / max_nodes.
let r3 = BrowseCrawler::new(&ua)
.max_nodes(5_000)
.crawl(well_known_nodes::OBJECTS_FOLDER)?;
println!("截断到 {} 个", r3.all_nodes.len());

Ok(())
}

Performance​

Crawl time depends on the number of server nodes and the network round-trip time — at least one Browse RPC per node. The SDK has no benchmark figures; measure it on your own server. max_nodes is a hard limit; when it is reached, crawling stops.

Best Practices​

  • Restricted to root; do not start from i=84 (Root)
  • Use node_class_filter(...) to save memory
  • Crawl all at once on startup
  • crawl Consume self; reconstruct the crawler on each iteration of the loop
  • Cancel: This crawl is a synchronous, blocking call; it does SDK not provide a CancellationToken. Use max_depth a timeout max_nodes or place the crawl in a custom thread, and let the business logic decide whether to discard the results.

Cross-Language Comparison​

C#PythonJavaC++RustC
BrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerDarraUa_Session_CrawlAddressSpace

The entry point for C is a single function DarraUa_Session_CrawlAddressSpace(session, config, on_node, ctx), accompanied by a callback DarraUa_CrawlerConfig(initialized DarraUa_CrawlerConfig_Init with )/ DarraUa_CrawlNode / callback DarraUa_OnCrawlNode,There is no such DarraUa_BrowseCrawler_* family。

Next Step​