1#!/usr/bin/env python
2#
3# Copyright 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""DB-related operations."""
18
19
20
21__all__ = ['Put', 'Delete']
22
23
24from mapreduce.operation import base
25
26# pylint: disable=protected-access
27
28
29class Put(base.Operation):
30  """Put entity into datastore via mutation_pool.
31
32  See mapreduce.context._MutationPool.
33  """
34
35  def __init__(self, entity):
36    """Constructor.
37
38    Args:
39      entity: an entity to put.
40    """
41    self.entity = entity
42
43  def __call__(self, context):
44    """Perform operation.
45
46    Args:
47      context: mapreduce context as context.Context.
48    """
49    context._mutation_pool.put(self.entity)
50
51
52class Delete(base.Operation):
53  """Delete entity from datastore via mutation_pool.
54
55  See mapreduce.context._MutationPool.
56  """
57
58  def __init__(self, entity):
59    """Constructor.
60
61    Args:
62      entity: a key or model instance to delete.
63    """
64    self.entity = entity
65
66  def __call__(self, context):
67    """Perform operation.
68
69    Args:
70      context: mapreduce context as context.Context.
71    """
72    context._mutation_pool.delete(self.entity)
73