blob: 56f36755800c575a10b37434833c473d4a4071db [file] [log] [blame]
alanlxl30f15bd2020-08-11 21:26:12 +10001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FEDERATED_STORAGE_MANAGER_IMPL_H_
6#define FEDERATED_STORAGE_MANAGER_IMPL_H_
7
8#include "federated/storage_manager.h"
9
alanlxl9d26c1c2020-08-21 13:42:36 +100010#include <memory>
alanlxl30f15bd2020-08-11 21:26:12 +100011#include <string>
alanlxl9d26c1c2020-08-21 13:42:36 +100012
alanlxl30f15bd2020-08-11 21:26:12 +100013#include <base/sequence_checker.h>
14
alanlxl9d26c1c2020-08-21 13:42:36 +100015#include "federated/example_database.h"
16#include "federated/session_manager_observer_interface.h"
17#include "federated/session_manager_proxy.h"
18
alanlxl30f15bd2020-08-11 21:26:12 +100019namespace federated {
20
alanlxl9d26c1c2020-08-21 13:42:36 +100021class SessionManagerProxy;
22
23class StorageManagerImpl : public StorageManager,
24 public SessionManagerObserverInterface {
alanlxl30f15bd2020-08-11 21:26:12 +100025 public:
26 StorageManagerImpl() = default;
Qijiang Fan6bc59e12020-11-11 02:51:06 +090027 StorageManagerImpl(const StorageManagerImpl&) = delete;
28 StorageManagerImpl& operator=(const StorageManagerImpl&) = delete;
29
alanlxl30f15bd2020-08-11 21:26:12 +100030 ~StorageManagerImpl() override = default;
31
alanlxl9d26c1c2020-08-21 13:42:36 +100032 // StorageManager:
33 void InitializeSessionManagerProxy(dbus::Bus* bus) override;
alanlxl30f15bd2020-08-11 21:26:12 +100034 bool OnExampleReceived(const std::string& client_name,
35 const std::string& serialized_example) override;
alanlxl30f15bd2020-08-11 21:26:12 +100036 bool PrepareStreamingForClient(const std::string& client_name) override;
alanlxl9d26c1c2020-08-21 13:42:36 +100037 bool GetNextExample(std::string* example, bool* end_of_iterator) override;
38 bool CloseStreaming(bool clean_examples) override;
39
40 // SessionManagerObserverInterface:
41 void OnSessionStarted() override;
42 void OnSessionStopped() override;
alanlxl30f15bd2020-08-11 21:26:12 +100043
44 private:
alanlxl9d26c1c2020-08-21 13:42:36 +100045 friend class StorageManagerImplTest;
46
47 void set_example_database_for_testing(ExampleDatabase* example_database) {
48 example_database_.reset(example_database);
49 }
50
51 void ConnectToDatabaseIfNecessary();
52
53 // Session manager that notifies session state changes.
54 std::unique_ptr<SessionManagerProxy> session_manager_proxy_;
55
56 // The database connection.
57 std::unique_ptr<ExampleDatabase> example_database_;
58
59 // Current login user hash. The database is connected to
60 // /run/daemon-store/federated/<sanitized_username_>/examples.db.
61 std::string sanitized_username_;
62
63 // Which client it is streaming examples for.
64 std::string streaming_client_name_;
65
66 // The last seen (i.e. largest) example id for the streaming_client_name_,
67 // after training job succeeds, examples of this client with id <=
68 // last_seen_example_id_ should be removed from the database.
69 int64_t last_seen_example_id_;
70
alanlxl30f15bd2020-08-11 21:26:12 +100071 SEQUENCE_CHECKER(sequence_checker_);
alanlxl30f15bd2020-08-11 21:26:12 +100072};
73
74} // namespace federated
75
76#endif // FEDERATED_STORAGE_MANAGER_IMPL_H_