From 2f574632c0b8b750ca145ab56aabf829ecf0cd3f Mon Sep 17 00:00:00 2001
From: Aaron <arosen@nicira.com>
Date: Mon, 12 Aug 2013 15:53:21 -0700
Subject: [PATCH] Add run_with_changes() method to Idl

This patch adds the method run_with_changes() to Idl which allows
one to determine the changes that occurred to the database without
having to read the entire table.

Signed-off-by: Aaron Rosen <arosen@nicira.com>
---
 python/ovs/db/idl.py |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
index 55fbcba..aa5ada4 100644
--- a/python/ovs/db/idl.py
+++ b/python/ovs/db/idl.py
@@ -150,9 +150,21 @@ class Idl:
 
         As an alternative to checking the return value, the client may check
         for changes in self.change_seqno."""
+        return self.__process_messages()
+
+    def run_with_changes(self):
+        """This method performs the same operation as run() but returns a
+        list of dicts() that represents the changes that have been processed
+        since last run. In the case of initial sync or the connection to the
+        database is reset this method will return a dict() that reflects the
+        current state of the database."""
+        return self.__process_messages(return_changes=True)
+
+    def __process_messages(self, return_changes=False):
         assert not self.txn
         initial_change_seqno = self.change_seqno
         self._session.run()
+        changes = []
         i = 0
         while i < 50:
             i += 1
@@ -176,6 +188,8 @@ class Idl:
                 and len(msg.params) == 2
                 and msg.params[0] == None):
                 # Database contents changed.
+                if return_changes:
+                    changes.append(msg.params[1])
                 self.__parse_update(msg.params[1])
             elif (msg.type == ovs.jsonrpc.Message.T_REPLY
                   and self._monitor_request_id is not None
@@ -186,6 +200,9 @@ class Idl:
                     self._monitor_request_id = None
                     self.__clear()
                     self.__parse_update(msg.result)
+                    # Return dict() that reflects current state of database.
+                    if return_changes:
+                        return msg.result
                 except error.Error, e:
                     vlog.err("%s: parse error in received schema: %s"
                               % (self._session.get_name(), e))
@@ -218,6 +235,8 @@ class Idl:
                          % (self._session.get_name(),
                              ovs.jsonrpc.Message.type_to_string(msg.type)))
 
+        if return_changes:
+            return changes
         return initial_change_seqno != self.change_seqno
 
     def wait(self, poller):
-- 
1.7.9.5

