source: people/peter.buschman/backup_monitoring/dal/builtins/idcache.py@ 1050

Last change on this file since 1050 was 976, checked in by peter, on Dec 6, 2011 at 10:19:33 AM

Raw checkin of current NetBackup / TSM parsing code.

File size: 3.9 KB
Line 
1###
2### IDCache - Cache for fast id lookups to an SQL table
3###
4
5from ...builtins import *
6from ...parsing.autotype import *
7
8class IDCache(object):
9
10 def __init__(self, conn=None, cache=None, static=False, table=None, key_column=None, value_column=None, anchors=None, extras=None, initialize=True, init_append=None):
11
12 self.conn = conn
13 self.table = table
14 self.key_column = key_column
15 self.value_column = value_column
16 self.anchors = anchors
17 self.extras = extras
18
19 if cache is None:
20 self.cache = {}
21 else:
22 self.cache = cache
23
24 self.static = static
25
26 init_sql = "SELECT %s, %s FROM %s" % (key_column, value_column, table)
27 anchor_sql = ""
28
29 if anchors:
30
31 i = 0
32
33 for key, value in anchors.items():
34
35 if i == 0:
36 init_sql = '%s WHERE ' % (init_sql)
37 else:
38 init_sql = '%s AND ' % (init_sql)
39
40 if type(value) in (int, float):
41 init_sql = "%s %s = %s" % (init_sql, key, value)
42 anchor_sql = "%s AND %s = %s" % (anchor_sql, key, value)
43 else:
44 init_sql = "%s %s = '%s'" % (init_sql, key, value)
45 anchor_sql = "%s AND %s = '%s'" % (anchor_sql, key, value)
46
47 i += 1
48
49 if init_append:
50 self.init_sql = '%s %s' % (init_sql, init_append)
51 else:
52 self.init_sql = '%s' % (init_sql)
53
54 self.anchor_sql = anchor_sql
55
56 if initialize:
57 self.initialize()
58
59 #
60 # initilize the cache
61 #
62 def initialize(self):
63
64 cache = self.cache
65 key_column = self.key_column
66 value_column = self.value_column
67 table = self.table
68 anchors = self.anchors
69 curs = self.conn.cursor()
70
71 sql = self.init_sql
72
73 for row in curs.execute(sql):
74 cache[row[key_column]] = row[value_column]
75
76 #
77 # flush the cache
78 #
79 def flush(self):
80 self.cache = {}
81
82 #
83 # return a value
84 #
85 def __getitem__(self, key):
86
87 cache = self.cache
88 anchors = self.anchors
89 anchor_sql = self.anchor_sql
90
91 if key in cache.keys():
92 return cache[key]
93 elif not self.static:
94 key_column = self.key_column
95 value_column = self.value_column
96 table = self.table
97 curs = self.conn.cursor()
98 sql = "SELECT %s FROM %s WHERE %s = '%s'%s" % (value_column, self.table, self.key_column, key, anchor_sql)
99 curs.execute(sql)
100 results = curs.fetchone()
101 if results:
102 value = results[0]
103 cache[key] = value
104 return value
105 else:
106 return None
107 else:
108 return None
109
110 #
111 # set a value
112 #
113 def __setitem__(self, key, value):
114
115 cache = self.cache
116
117 cache[key] = value
118
119 #
120 # delete a value
121 #
122 def __delitem__(self, key):
123
124 cache = self.cache
125
126 if cache.has_key(key):
127 del cache[key]
128
129 #
130 #
131 #
132 def __contains__(self, item):
133
134 cache = self.cache
135
136 if cache.has_key(item):
137 return True
138 else:
139 return False
140
141 #
142 #
143 #
144 def has_key(self, key):
145
146 return key in self
147
148 #
149 #
150 #
151 def keys(self):
152
153 cache = self.cache
154
155 return cache.keys()
156
157 #
158 #
159 #
160 def __iter__(self):
161
162 return iter(self.keys())
163
164 #
165 # actions to take when an IDCache is deleted
166 #
167 def __del__(self):
168
169 cache = self.cache
170
171 if hasattr(cache, 'close'):
172 cache.close()
173
Note: See TracBrowser for help on using the repository browser.