Class: AppEngine::PStore

Inherits:
Object
  • Object
show all
Defined in:
lib/appengine-pstore.rb

Instance Method Summary collapse

Constructor Details

#initialize(dbname) ⇒ PStore

Returns a new instance of PStore.



19
20
21
22
23
# File 'lib/appengine-pstore.rb', line 19

def initialize(dbname)
  @kind = self.class.name
  @parent_key = AppEngine::Datastore::Key.from_path(@kind, dbname)
  @transaction = nil
end

Instance Method Details

#[](name) ⇒ Object

Raises:

  • (PStore::Error)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/appengine-pstore.rb', line 46

def [](name)
  in_transaction
  raise PStore::Error, "in read-only transaction" if @rdonly
  if @cache.key?(name)
    @cache[name]  # return latest put data
  else
    key = AppEngine::Datastore::Key.from_path(@parent_key, @kind, name)
    entity = AppEngine::Datastore.get(@transaction, key)
    load(entity[:value])
  end
end

#[]=(name, value) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/appengine-pstore.rb', line 58

def []=(name, value)
  in_transaction_wr
  entity = AppEngine::Datastore::Entity.new(@kind, name, @parent_key)
  entity[:value] = dump(value)
  AppEngine::Datastore.put(@transaction, entity)
  @cache[name] = value
  value
end

#delete(name) ⇒ Object



67
68
69
70
71
# File 'lib/appengine-pstore.rb', line 67

def delete(name)
  key = AppEngine::Datastore::Key.from_path(@parent_key, @kind, name)
  AppEngine::Datastore.delete(@transaction, key)
  @cache.delete(name)
end

#dump(content) ⇒ Object



77
78
79
# File 'lib/appengine-pstore.rb', line 77

def dump(content)
  Marshal::dump(content)
end

#load(content) ⇒ Object



73
74
75
# File 'lib/appengine-pstore.rb', line 73

def load(content)
  Marshal::load(content)
end

#pathObject



93
94
95
# File 'lib/appengine-pstore.rb', line 93

def path
  @parent_key
end

#root?(key) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/appengine-pstore.rb', line 89

def root?(key)
  in_transaction
end

#rootsObject



81
82
83
84
85
86
87
# File 'lib/appengine-pstore.rb', line 81

def roots
  in_transaction
  query = AppEngine::Datastore::Query.new(@kind, @parent_key)
  query.keysonly.fetch.map {|value|
    value.key.name
  }.concat(@cache.keys).uniq
end

#transaction(readonly = false) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
43
44
# File 'lib/appengine-pstore.rb', line 37

def transaction(readonly = false)
  @rdonly = readonly
  @transaction = AppEngine::Datastore.begin_transaction
  @cache = {}
  yield self
  @transaction.commit
  @transaction = nil
end