Line data Source code
1 : /** 2 : Copyright (c) 2023-2024 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef XENOLITH_RESOURCES_STORAGE_XLSTORAGECOMPONENT_H_ 24 : #define XENOLITH_RESOURCES_STORAGE_XLSTORAGECOMPONENT_H_ 25 : 26 : #include "XLCommon.h" 27 : #include "SPDbScheme.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::xenolith::storage { 30 : 31 : class ComponentLoader; 32 : class Server; 33 : 34 : class Component : public db::AllocBase { 35 : public: 36 : virtual ~Component(); 37 : 38 : Component(ComponentLoader &, StringView name); 39 : 40 : virtual void handleChildInit(const Server &, const db::Transaction &); 41 : virtual void handleChildRelease(const Server &, const db::Transaction &); 42 : 43 : virtual void handleStorageTransaction(db::Transaction &); 44 : virtual void handleHeartbeat(const Server &); 45 : 46 42 : StringView getName() const { return _name; } 47 : 48 : protected: 49 : db::String _name; 50 : }; 51 : 52 : class ComponentLoader { 53 : public: 54 : virtual ~ComponentLoader(); 55 : 56 : virtual db::pool_t *getPool() const = 0; 57 : virtual const Server &getServer() const = 0; 58 : virtual const db::Transaction &getTransaction() const = 0; 59 : 60 : virtual void exportComponent(Component *) = 0; 61 : 62 : virtual const db::Scheme * exportScheme(const db::Scheme &) = 0; 63 : 64 : template <typename ... Args> 65 : void define(db::Scheme &scheme, Args && ... args) { 66 : exportScheme(scheme); 67 : scheme.define(std::forward<Args>(args)...); 68 : } 69 : }; 70 : 71 : class ComponentContainer : public Ref { 72 : public: 73 : using TaskCallback = Function<bool(const Server &, const db::Transaction &)>; 74 : 75 : virtual ~ComponentContainer(); 76 : 77 : virtual bool init(StringView); 78 : 79 : virtual void handleStorageInit(ComponentLoader &); 80 : virtual void handleStorageDisposed(const db::Transaction &); 81 : 82 : virtual void handleComponentsLoaded(const Server &serv); 83 : virtual void handleComponentsUnloaded(const Server &serv); 84 : 85 105 : StringView getName() const { return _name; } 86 : 87 : void setServer(const Server *serv) { _server = serv; } 88 : const Server *getServer() const { return _server; } 89 : 90 : bool isLoaded() const { return _loaded; } 91 : 92 : bool perform(TaskCallback &&, Ref * = nullptr) const; 93 : 94 : protected: 95 : bool _loaded = false; 96 : String _name; 97 : const Server *_server = nullptr; 98 : mutable Vector<Pair<TaskCallback, Rc<Ref>>> _pendingTasks; 99 : }; 100 : 101 : } 102 : 103 : #endif /* XENOLITH_RESOURCES_STORAGE_XLSTORAGECOMPONENT_H_ */