-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpledb_backend.py
68 lines (51 loc) · 2.07 KB
/
simpledb_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import benchmark_utils
from boto.sdb.connection import SDBConnection
import aws_credentials
TABLE_NAME = "data"
# We use single-character attribute names to save space in requests.
VALUE = "v"
# Get a feel for server-side latencies.
sampler = benchmark_utils.BenchmarkTimer()
# Backend driven by SimpleDB. We use one domain called "data".
##############################################################
# NOTE. We currently do not do reference-counting for SimpleDB. That is,
# nuke() is the only way that key-value pairs can be removed from the
# SimpleDBBackend. Reference-counting for SimpleDB would impose lots
# of extra overhead, since SimpleDB does not support atomic increments.
##############################################################
class SimpleDBBackend:
def __init__(self):
self.connection = SDBConnection(aws_credentials.accessKey,
aws_credentials.secretKey)
self.domain = self.connection.get_domain(TABLE_NAME)
def put(self, key, value):
sampler.begin()
try:
self.domain.put_attributes(key, {VALUE:value})
finally:
sampler.end()
def get(self, key):
sampler.begin()
try:
# First try an eventually consistent read.
result = self.domain.get_attributes(key, consistent_read=False)
return result[VALUE]
except KeyError:
# The eventually consistent read failed. Try a strongly consistent
# read.
result = self.domain.get_attributes(key, consistent_read=True)
return result[VALUE]
finally:
sampler.end()
def incRefCount(self, key):
# Not implemented.
pass
def decRefCount(self, key):
# Not implemented.
pass
def nuke(self):
# Delete and re-create the table.
self.connection.delete_domain(TABLE_NAME)
self.domain = self.connection.create_domain(TABLE_NAME)
def flush(self):
pass # No-op.