1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "storage/mutation.h"
18 
19 #include "os/log.h"
20 
21 namespace bluetooth {
22 namespace storage {
23 
Mutation(ConfigCache * config,ConfigCache * memory_only_config)24 Mutation::Mutation(ConfigCache* config, ConfigCache* memory_only_config)
25     : config_(config), memory_only_config_(memory_only_config) {
26   ASSERT(config_ != nullptr);
27   ASSERT(memory_only_config_ != nullptr);
28 }
29 
Add(MutationEntry entry)30 void Mutation::Add(MutationEntry entry) {
31   switch (entry.property_type) {
32     case MutationEntry::PropertyType::NORMAL:
33       if (entry.entry_type != MutationEntry::EntryType::SET) {
34         // When an item is removed from normal config, it must be removed from temp config as well
35         memory_only_config_entries_.emplace(entry);
36       }
37       normal_config_entries_.emplace(std::move(entry));
38       break;
39     case MutationEntry::PropertyType::MEMORY_ONLY:
40       memory_only_config_entries_.emplace(std::move(entry));
41       break;
42       // do not write a default case so that when a new enum is defined, compilation would fail automatically
43   }
44 }
45 
Commit()46 void Mutation::Commit() {
47   config_->Commit(normal_config_entries_);
48   memory_only_config_->Commit(memory_only_config_entries_);
49 }
50 
51 }  // namespace storage
52 }  // namespace bluetooth