The Pedigree Project  0.1
MemoryBackend.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/config/MemoryBackend.h"
21 #include "pedigree/kernel/utilities/new"
22 
23 MemoryBackend::MemoryBackend(const String &configStore)
24  : ConfigurationBackend(configStore), m_Tables(), m_TypeName("MemoryBackend")
25 {
26 }
27 
28 MemoryBackend::~MemoryBackend()
29 {
30  ConfigurationManager::instance().removeBackend(m_ConfigStore);
31 }
32 
33 size_t MemoryBackend::createTable(const String &table)
34 {
35  m_Tables.insert(table, new Table());
36  return 0;
37 }
38 
40  const String &table, const String &key, const ConfigValue &value)
41 {
42  RadixTree<Table *>::LookupType result = m_Tables.lookup(table);
43  if (!result.hasValue())
44  return;
45 
46  ConfigValue *pConfigValue = new ConfigValue(value);
47  result.value()->m_Rows.insert(key, pConfigValue);
48 }
49 
50 ConfigValue &MemoryBackend::select(const String &table, const String &key)
51 {
52  static ConfigValue v;
53  v.type = Invalid;
54 
55  RadixTree<Table *>::LookupType result = m_Tables.lookup(table);
56  if (!result.hasValue())
57  return v;
58 
60  result.value()->m_Rows.lookup(key);
61  if (val.hasValue())
62  return *val.value();
63  else
64  return v;
65 }
66 
68  const String &table, const String &key, ConfigurationWatcher watcher)
69 {
70  RadixTree<Table *>::LookupType result = m_Tables.lookup(table);
71  if (!result.hasValue())
72  return;
73 
75  result.value()->m_Rows.lookup(key);
76  if (val.hasValue())
77  {
78  for (int i = 0; i < MAX_WATCHERS; i++)
79  {
80  if (val.value()->watchers[i] == 0)
81  {
82  val.value()->watchers[i] = watcher;
83  break;
84  }
85  }
86  }
87 }
88 
90  const String &table, const String &key, ConfigurationWatcher watcher)
91 {
92  RadixTree<Table *>::LookupType result = m_Tables.lookup(table);
93  if (!result.hasValue())
94  return;
95 
97  result.value()->m_Rows.lookup(key);
98  if (val.hasValue())
99  {
100  for (int i = 0; i < MAX_WATCHERS; i++)
101  {
102  if (val.value()->watchers[i] == watcher)
103  {
104  val.value()->watchers[i] = 0;
105  break;
106  }
107  }
108  }
109 }
110 
111 const String &MemoryBackend::getTypeName()
112 {
113  return m_TypeName;
114 }
A key/value dictionary for string keys.
Definition: RadixTree.h:45
Definition: String.h:49
Definition: Result.h:36
virtual void unwatch(const String &table, const String &key, ConfigurationWatcher watcher)
virtual void watch(const String &table, const String &key, ConfigurationWatcher watcher)
virtual void insert(const String &table, const String &key, const ConfigValue &value)
virtual ConfigValue & select(const String &table, const String &key)