ROLLBACK undo redo
I'm building a database using a BST (binary search tree) and I wan开发者_StackOverflowt the user to be able to roll back the last 5 commands. Any Suggestions? I'm using Java.
Have you considered using Berkey DB? It's free and supported nested transactions (which would allow you to have any number of levels of rollback):
http://download.oracle.com/docs/cd/E17076_02/html/gsg_txn/JAVA/nestedtxn.html
Even if you decide to implement your own DB, it might be useful as a reference.
It sounds like you want the Memento pattern. Essentially, you create an object that has all of the information required to:
- From the state of the tree before the operation, repeat the operation. (Redo)
- From the state of the tree after the operation, revert the operation. (Undo)
You'd keep the last five of these around. When the user asks for an undo, take the latest, ask it to revert the operation, then indicate somehow (some index variable, for example) where you are in the list of mementos. You should then be able to move through the list in either direction, undoing and redoing as much as you want.
精彩评论