| 476 | } |
| 477 | |
| 478 | DataStorageBridge::NodeQueryResult DataStorageBridge::GetNodes(const NodeQueryParams& params) const |
| 479 | { |
| 480 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 481 | return this->DispatchTask<NodeQueryResult>([this, params]() |
| 482 | { |
| 483 | NodeQueryResult result; |
| 484 | result.nodes = Json::array(); |
| 485 | result.totalCount = 0; |
| 486 | result.limit = params.limit; |
| 487 | result.offset = params.offset; |
| 488 | |
| 489 | auto dataStorage = m_DataStorage.Lock(); |
| 490 | if (dataStorage.IsNull()) |
| 491 | { |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | // Build predicate from query parameters |
| 496 | auto predicate = this->BuildNodePredicate(params); |
| 497 | |
| 498 | // Get filtered nodes using the predicate |
| 499 | auto filteredNodes = dataStorage->GetSubset(predicate); |
| 500 | |
| 501 | // Convert to vector for sorting and pagination |
| 502 | std::vector<DataNode*> matchingNodes; |
| 503 | matchingNodes.reserve(filteredNodes->Size()); |
| 504 | for (auto it = filteredNodes->Begin(); it != filteredNodes->End(); ++it) |
| 505 | { |
| 506 | matchingNodes.push_back(it->Value().GetPointer()); |
| 507 | } |
| 508 | |
| 509 | // Apply sorting if specified |
| 510 | // Sorting is done by JSON result fields: uid, name, path, parent_uid, data_type, children_count, timestamp |
| 511 | if (params.sort.has_value()) |
| 512 | { |
| 513 | const auto& sortSpec = params.sort.value(); |
| 514 | std::sort(matchingNodes.begin(), matchingNodes.end(), |
| 515 | [this, &sortSpec, &dataStorage](DataNode* a, DataNode* b) { |
| 516 | const std::string& field = sortSpec.field; |
| 517 | |
| 518 | // Handle numeric fields (children_count, timestamp) |
| 519 | if (field == "children_count") |
| 520 | { |
| 521 | int countA = this->GetChildrenCount(a); |
| 522 | int countB = this->GetChildrenCount(b); |
| 523 | return sortSpec.ascending ? (countA < countB) : (countA > countB); |
| 524 | } |
| 525 | else if (field == "timestamp") |
| 526 | { |
| 527 | unsigned long timeA = a->GetMTime(); |
| 528 | unsigned long timeB = b->GetMTime(); |
| 529 | return sortSpec.ascending ? (timeA < timeB) : (timeA > timeB); |
| 530 | } |
| 531 | |
| 532 | // Handle string fields |
| 533 | std::string valA, valB; |
| 534 | |
| 535 | if (field == "uid") |