summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2013-05-23 14:15:54 -0700
committerJohn Keiser <jkeiser@opscode.com>2013-05-23 14:15:54 -0700
commit67c15968fc323557cf4acffeb85a6c6ddaa5d9d0 (patch)
tree4349e485201144cb23132e4ee7ee0e4ddacbf104
parentbc2fa1e839adc2bc49813df926b7ab841af3953d (diff)
downloadchef-zero-67c15968fc323557cf4acffeb85a6c6ddaa5d9d0.tar.gz
Add the actual data store
-rw-r--r--lib/chef_zero/data_store/data_already_exists_error.rb29
-rw-r--r--lib/chef_zero/data_store/data_error.rb31
-rw-r--r--lib/chef_zero/data_store/data_not_found_error.rb29
-rw-r--r--lib/chef_zero/data_store/memory_store.rb113
4 files changed, 202 insertions, 0 deletions
diff --git a/lib/chef_zero/data_store/data_already_exists_error.rb b/lib/chef_zero/data_store/data_already_exists_error.rb
new file mode 100644
index 0000000..54116d0
--- /dev/null
+++ b/lib/chef_zero/data_store/data_already_exists_error.rb
@@ -0,0 +1,29 @@
+#
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef_zero/data_store/data_error'
+
+module ChefZero
+ module DataStore
+ class DataAlreadyExistsError < DataError
+ def initialize(path, cause = nil)
+ super
+ end
+ end
+ end
+end
diff --git a/lib/chef_zero/data_store/data_error.rb b/lib/chef_zero/data_store/data_error.rb
new file mode 100644
index 0000000..9822a6b
--- /dev/null
+++ b/lib/chef_zero/data_store/data_error.rb
@@ -0,0 +1,31 @@
+#
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+module ChefZero
+ module DataStore
+ class DataError < StandardError
+ def initialize(path, cause = nil)
+ @path = path
+ @cause = cause
+ end
+
+ attr_reader :path
+ attr_reader :cause
+ end
+ end
+end
diff --git a/lib/chef_zero/data_store/data_not_found_error.rb b/lib/chef_zero/data_store/data_not_found_error.rb
new file mode 100644
index 0000000..a73d767
--- /dev/null
+++ b/lib/chef_zero/data_store/data_not_found_error.rb
@@ -0,0 +1,29 @@
+#
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef_zero/data_store/data_error'
+
+module ChefZero
+ module DataStore
+ class DataNotFoundError < DataError
+ def initialize(path, cause = nil)
+ super
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/chef_zero/data_store/memory_store.rb b/lib/chef_zero/data_store/memory_store.rb
new file mode 100644
index 0000000..25678e0
--- /dev/null
+++ b/lib/chef_zero/data_store/memory_store.rb
@@ -0,0 +1,113 @@
+#
+# Author:: John Keiser (<jkeiser@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef_zero/data_store/data_already_exists_error'
+require 'chef_zero/data_store/data_not_found_error'
+
+module ChefZero
+ module DataStore
+ class MemoryStore
+ def initialize
+ @data = {}
+ end
+
+ def create_dir(path, name, *options)
+ parent = _get(path, options.include?(:recursive))
+
+ if parent.has_key?(name)
+ raise DataAlreadyExistsError.new(path + [name])
+ end
+ parent[name] = {}
+ end
+
+ def create(path, name, data, *options)
+ if !data.is_a?(String)
+ raise "set only works with strings"
+ end
+
+ parent = _get(path, options.include?(:create_dir))
+
+ if parent.has_key?(name)
+ raise DataAlreadyExistsError.new(path + [name])
+ end
+ parent[name] = data
+ end
+
+ def get(path)
+ value = _get(path)
+ if value.is_a?(Hash)
+ raise "get() called on directory #{path.join('/')}"
+ end
+ value
+ end
+
+ def set(path, data, *options)
+ if !data.is_a?(String)
+ raise "set only works with strings: #{path} = #{data.inspect}"
+ end
+
+ # Get the parent
+ parent = _get(path[0..-2], options.include?(:create_dir))
+
+ if !options.include?(:create) && !parent[path[-1]]
+ raise DataNotFoundError.new(path)
+ end
+ parent[path[-1]] = data
+ end
+
+ def delete(path)
+ parent = _get(path[0,path.length-1])
+ parent.delete(path[-1])
+ end
+
+ def list(path)
+ dir = _get(path)
+ if !dir.is_a? Hash
+ raise "list only works with directories (#{path} = #{dir.class}"
+ end
+ dir.keys.sort
+ end
+
+ def exists?(path)
+ begin
+ get(path)
+ return true
+ rescue DataNotFoundError
+ return false
+ end
+ end
+
+ private
+
+ def _get(path, create_dir=false)
+ value = @data
+ path.each_with_index do |path_part, index|
+ if !value.has_key?(path_part)
+ if create_dir
+ value[path_part] = {}
+ else
+ raise DataNotFoundError.new(path[0,index+1])
+ end
+ end
+ value = value[path_part]
+ end
+ value
+ end
+ end
+ end
+end \ No newline at end of file