Shu Zong Chen avatar Shu Zong Chen committed 1176630

haz transactions

Comments (0)

Files changed (2)

simpledb/__init__.py

 				v = f(*args)
 				if v is not None:
 					print v
-			except TypeError:
+			except TypeError as e:
+				print e
 				print "INCORRECT NUM OF ARGS FOR CMD: {0}".format(cmd)

simpledb/engine.py

 
 class DBEngine(object):
 	def __init__(self):
-		self.d = {}
-		self.c = Counter()
+		self._dicts = []
+		self._counts = []
+		self.add_layer()
+
+	def add_layer(self):
+		self._dicts.append({})
+		self._counts.append(Counter())
+
+	def rm_layer(self):
+		if len(self._dicts) > 1:
+			self._dicts.pop()
+			self._counts.pop()
+		else:
+			print 'INVALID ROLLBACK'
+	
+	def commit(self):
+		[self._dicts[0].update(d) for d in self._dicts[1:]]
+		[self._counts[0].update(c) for c in self._counts[1:]]
+		self._dicts = self._dicts[:1]
+		self._counts = self._counts[:1]
+	
+	@property
+	def d(self):
+		return self._dicts[-1]
+	
+	@property
+	def c(self):
+		return self._counts[-1]
+
+	def _get(self, name):
+		i = len(self._dicts) - 1
+		while i >= 0:
+			if name in self._dicts[i]:
+				return self._dicts[i][name]
+			i -= 1
+		return None
+	
+	def _get_count(self, name):
+		return sum(d[name] for d in self._counts)
 
 	def DO_SET(self, name, value):
-		if name in self.d:
-			self.c[self.d[name]] -= 1
+		old_v = self._get(name)
+		if old_v is not None:
+			self.c[old_v] -= 1
 		self.c[value] += 1
 		self.d[name] = value
 
 	def DO_GET(self, name):
-		try:
-			return self.d[name]
-		except KeyError:
+		v = self._get(name)
+		if v is not None:
+			return v
+		else:
 			return 'NULL'
 
 	def DO_UNSET(self, name):
-		if name in self.d:
-			self.c[self.d[name]] -= 1
-		del self.d[name]
+		old_v = self._get(name)
+		if old_v is not None:
+			self.c[old_v] -= 1
+		self.d[name] = None
 
 	def DO_NUMEQUALTO(self, name):
-		return self.c[name]
+		return self._get_count(name)
 
 	def DO_END(self):
 		raise StopProgram
+
+	def DO_BEGIN(self):
+		self.add_layer()
+
+	def DO_ROLLBACK(self):
+		self.rm_layer()
+
+	def DO_COMMIT(self):
+		self.commit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.