PROBLEM: test_13 "Cross-System Integration" had concurrent DataNode reads removed because
getChild() and getDataRoot() return unique_ptr (ownership transfer), making concurrent
reads impossible - each read would create a copy or destroy the data.
SOLUTION: Add read-only API methods that return raw pointers without copying:
API Changes:
1. **IDataNode::getChildReadOnly(name)** → IDataNode*
- Returns raw pointer to child without copying
- Pointer valid as long as parent exists
- Enables concurrent reads without destroying tree
2. **IDataTree::getDataRootReadOnly()** → IDataNode*
- Returns raw pointer to data root without copying
- Enables concurrent access to tree data
- Complements existing getDataRoot() which returns copy
3. **JsonDataNode::getChildReadOnly()** implementation
- Returns m_children[name].get() directly
- Zero-overhead, no allocation
4. **JsonDataTree::getDataRootReadOnly()** implementation
- Returns m_root->getFirstChildByName("data") directly
- No copying, direct access
Test Changes:
- Restored TEST 5 concurrent access with IO + DataNode
- Uses getDataRootReadOnly() + getChildReadOnly() for reads
- Thread 1: Publishes IO messages concurrently
- Thread 2: Reads DataNode data concurrently (NOW WORKS!)
- Updated TEST 2 & 3 to use read-only API where appropriate
- Recreate player data before TEST 5 using read-only root access
Results:
✅ test_13 ALL TESTS PASS (5/5)
✅ TEST 5: ~100 concurrent reads successful (was 0 before)
✅ 0 errors during concurrent access
✅ True cross-system integration validated (IO + DataNode together)
This restores the original purpose of test_13: validating that IO pub/sub
and DataNode tree access work correctly together in concurrent scenarios.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
2.9 KiB
C++
92 lines
2.9 KiB
C++
#pragma once
|
|
|
|
#include "IDataTree.h"
|
|
#include "JsonDataNode.h"
|
|
#include <string>
|
|
#include <memory>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
|
|
namespace grove {
|
|
|
|
/**
|
|
* @brief Concrete implementation of IDataTree backed by JSON files
|
|
*
|
|
* Manages three separate trees:
|
|
* - config/ : Read-only configuration loaded from files (hot-reload enabled)
|
|
* - data/ : Persistent player data (read-write, saved to disk)
|
|
* - runtime/ : Temporary runtime state (read-write, never saved)
|
|
*
|
|
* File structure:
|
|
* basePath/
|
|
* ├─ config/
|
|
* │ ├─ tanks.json
|
|
* │ ├─ weapons.json
|
|
* │ └─ ...
|
|
* ├─ data/
|
|
* │ ├─ campaign.json
|
|
* │ ├─ unlocks.json
|
|
* │ └─ ...
|
|
* └─ runtime/ (in-memory only, not on disk)
|
|
*/
|
|
class JsonDataTree : public IDataTree {
|
|
public:
|
|
/**
|
|
* @brief Create a data tree from a base directory
|
|
* @param basePath Base directory containing config/, data/ subdirs
|
|
*/
|
|
explicit JsonDataTree(const std::string& basePath);
|
|
virtual ~JsonDataTree() = default;
|
|
|
|
// Tree access
|
|
std::unique_ptr<IDataNode> getRoot() override;
|
|
std::unique_ptr<IDataNode> getNode(const std::string& path) override;
|
|
|
|
// Separate roots
|
|
std::unique_ptr<IDataNode> getConfigRoot() override;
|
|
std::unique_ptr<IDataNode> getDataRoot() override;
|
|
IDataNode* getDataRootReadOnly() override;
|
|
std::unique_ptr<IDataNode> getRuntimeRoot() override;
|
|
|
|
// Save operations
|
|
bool saveData() override;
|
|
bool saveNode(const std::string& path) override;
|
|
|
|
// Load operations
|
|
bool loadConfigFile(const std::string& filename) override;
|
|
bool loadDataDirectory() override;
|
|
|
|
// Hot-reload
|
|
bool checkForChanges() override;
|
|
bool reloadIfChanged() override;
|
|
void onTreeReloaded(std::function<void()> callback) override;
|
|
|
|
// Metadata
|
|
std::string getType() override;
|
|
|
|
private:
|
|
std::string m_basePath;
|
|
std::unique_ptr<JsonDataNode> m_root;
|
|
std::unique_ptr<JsonDataNode> m_configRoot;
|
|
std::unique_ptr<JsonDataNode> m_dataRoot;
|
|
std::unique_ptr<JsonDataNode> m_runtimeRoot;
|
|
|
|
std::map<std::string, std::filesystem::file_time_type> m_configFileTimes;
|
|
std::vector<std::function<void()>> m_reloadCallbacks;
|
|
|
|
// Helper methods
|
|
void loadConfigTree();
|
|
void loadDataTree();
|
|
void initializeRuntimeTree();
|
|
void scanDirectory(const std::string& dirPath, JsonDataNode* parentNode, bool readOnly);
|
|
json loadJsonFile(const std::string& filePath);
|
|
bool saveJsonFile(const std::string& filePath, const json& data);
|
|
void buildNodeFromJson(const std::string& name, const json& data, JsonDataNode* parentNode, bool readOnly);
|
|
json nodeToJson(const JsonDataNode* node);
|
|
void updateFileTimestamps(const std::string& dirPath);
|
|
};
|
|
|
|
} // namespace grove
|