Class: AppEngine::PStore
- Inherits:
-
Object
- Object
- AppEngine::PStore
- Defined in:
- lib/appengine-pstore.rb
Overview
The PStore compatible interface for Google App Engine Datastore.
db = AppEngine::PStore.new(dbname)
db.transaction do |db|
db[key] = value
end
A data is stored to the Datastore as AppEngine::Entity. The stracture of Datastore has following schema.
* dbname: a root key for the Datastore.
* key: A child of the root key.
* value: AppEngine::Entity that contains the value object.
Note: ‘key’ and ‘value’ are marshalled before putting Datastore.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Retrieves a stored value from the Datastore by the name.
-
#[]=(name, value) ⇒ Object
Stores a value to the Datastore by the name.
-
#abort ⇒ Object
Abort the transaction.
-
#commit ⇒ Object
Commit the transaction.
-
#delete(name) ⇒ Object
Delete a value from the Datastore by the name.
- #dump(content) ⇒ Object
-
#initialize(dbname) ⇒ PStore
constructor
Create a database identified by dbname.
- #load(content) ⇒ Object
-
#path ⇒ Object
Returns the database’s name.
-
#root?(key) ⇒ Boolean
Whether the database has key.
-
#roots ⇒ Object
Returns all keys of this database.
-
#transaction(readonly = false) ⇒ Object
Begins a new transaction for the AppEngine::Datastore.
Constructor Details
Instance Method Details
#[](name) ⇒ Object
Retrieves a stored value from the Datastore by the name.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/appengine-pstore.rb', line 117 def [](name) in_transaction # return uncommited data if exist return @uncommited[:added][name] if @uncommited[:added].key?(name) return nil if @uncommited[:deleted].key?(name) key = AppEngine::Datastore::Key.from_path(@parent_key, @kind, dump(name)) begin entity = AppEngine::Datastore.get(@transaction, key) load(entity[:value]) rescue AppEngine::Datastore::EntityNotFound nil end end |
#[]=(name, value) ⇒ Object
Stores a value to the Datastore by the name.
133 134 135 136 137 138 139 140 141 |
# File 'lib/appengine-pstore.rb', line 133 def []=(name, value) in_transaction_wr entity = AppEngine::Datastore::Entity.new(@kind, dump(name), @parent_key) entity[:value] = dump(value) AppEngine::Datastore.put(@transaction, entity) @uncommited[:added][name] = value @uncommited[:deleted].delete(name) value end |
#abort ⇒ Object
Abort the transaction.
110 111 112 113 114 |
# File 'lib/appengine-pstore.rb', line 110 def abort in_transaction @transaction.rollback throw :pstore_abort_transaction end |
#commit ⇒ Object
Commit the transaction.
103 104 105 106 107 |
# File 'lib/appengine-pstore.rb', line 103 def commit in_transaction @transaction.commit throw :pstore_abort_transaction end |
#delete(name) ⇒ Object
Delete a value from the Datastore by the name.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/appengine-pstore.rb', line 144 def delete(name) in_transaction_wr key = AppEngine::Datastore::Key.from_path(@parent_key, @kind, dump(name)) value = self[name] # Datastore.delete requires array keys AppEngine::Datastore.delete(@transaction, [key]) @uncommited[:added].delete(name) @uncommited[:deleted][name] = value value end |
#dump(content) ⇒ Object
159 160 161 |
# File 'lib/appengine-pstore.rb', line 159 def dump(content) Marshal::dump(content) end |
#load(content) ⇒ Object
155 156 157 |
# File 'lib/appengine-pstore.rb', line 155 def load(content) Marshal::load(content) end |
#path ⇒ Object
Returns the database’s name
180 181 182 |
# File 'lib/appengine-pstore.rb', line 180 def path @parent_key.name end |
#root?(key) ⇒ Boolean
Whether the database has key.
174 175 176 177 |
# File 'lib/appengine-pstore.rb', line 174 def root?(key) in_transaction self.roots.include?(key) end |
#roots ⇒ Object
Returns all keys of this database.
164 165 166 167 168 169 170 171 |
# File 'lib/appengine-pstore.rb', line 164 def roots in_transaction query = AppEngine::Datastore::Query.new(@kind, @parent_key) db_keys = query.keysonly.fetch.map {|entity| load(entity.key.name) } (db_keys + @uncommited[:added].keys - @uncommited[:deleted].keys).uniq end |
#transaction(readonly = false) ⇒ Object
Begins a new transaction for the AppEngine::Datastore.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/appengine-pstore.rb', line 79 def transaction(readonly = false) raise ::PStore::Error, "nested transaction" if @transaction @rdonly = readonly @transaction = AppEngine::Datastore.begin_transaction # uncommited entities @uncommited = { :added => {}, :deleted => {} } begin catch(:pstore_abort_transaction) do yield self end rescue Exception @transaction.rollback if @transaction.active? raise ensure @transaction.commit if @transaction.active? @transaction = nil @uncommited = nil end end |