VOL-1243: Added logic for thread safety

- Thread safety was added at the proxy level
- Refactored the test init in a base_test structure
- Fixed issue with writing to kv
- Added profiling for locking period

Amendments:

- Comment out a cleanup statement causing KV corruption (as per VOL-1293)
- Added missing license

Change-Id: Id6658270dbb8b738abeef9e9e1d349dce36501bc
diff --git a/db/model/node.go b/db/model/node.go
index 4a92208..a3e6ea7 100644
--- a/db/model/node.go
+++ b/db/model/node.go
@@ -331,10 +331,13 @@
 
 func (n *node) getData(rev Revision, depth int) interface{} {
 	msg := rev.Get(depth)
+	var modifiedMsg interface{}
 
 	if n.Proxy != nil {
 		log.Debug("invoking proxy GET Callbacks")
-		msg = n.Proxy.InvokeCallbacks(GET, false, msg)
+		if modifiedMsg = n.Proxy.InvokeCallbacks(GET, false, msg); modifiedMsg != nil {
+			msg = modifiedMsg
+		}
 
 	}
 	return msg
@@ -398,6 +401,7 @@
 			idx, childRev := n.findRevByKey(children, field.Key, keyValue)
 			childNode := childRev.GetNode()
 			childNode.Proxy = n.Proxy
+
 			newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
 
 			if newChildRev.GetHash() == childRev.GetHash() {
@@ -418,19 +422,20 @@
 			children[idx] = newChildRev
 			rev = rev.UpdateChildren(name, children, branch)
 			branch.Latest.Drop(txid, false)
-			n.MakeLatest(branch, rev, nil)
-			return rev
+			n.Root.MakeLatest(branch, rev, nil)
+			return newChildRev
 		} else {
 			log.Errorf("cannot index into container with no keys")
 		}
 	} else {
 		childRev := rev.GetChildren()[name][0]
 		childNode := childRev.GetNode()
+		childNode.Proxy = n.Proxy
 		newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
 		rev = rev.UpdateChildren(name, []Revision{newChildRev}, branch)
 		branch.Latest.Drop(txid, false)
-		n.MakeLatest(branch, rev, nil)
-		return rev
+		n.Root.MakeLatest(branch, rev, nil)
+		return newChildRev
 	}
 	return nil
 }
@@ -460,8 +465,13 @@
 		}
 		rev := branch.Latest.UpdateData(data, branch)
 		changes := []ChangeTuple{{POST_UPDATE, branch.Latest.GetData(), rev.GetData()}}
-		branch.Latest.Drop(branch.Txid, true)
-		n.MakeLatest(branch, rev, changes)
+
+		// FIXME VOL-1293: the following statement corrupts the kv when using a subproxy (commenting for now)
+		// FIXME VOL-1293 cont'd: need to figure out the required conditions otherwise we are not cleaning up entries
+		//branch.Latest.Drop(branch.Txid, true)
+
+		n.Root.Proxy = n.Proxy
+		n.Root.MakeLatest(branch, rev, changes)
 		return rev
 	} else {
 		return branch.Latest
@@ -524,7 +534,7 @@
 					rev := rev.UpdateChildren(name, children, branch)
 					changes := []ChangeTuple{{POST_ADD, nil, rev.GetData()}}
 					branch.Latest.Drop(txid, false)
-					n.MakeLatest(branch, rev, changes)
+					n.Root.MakeLatest(branch, rev, changes)
 					return rev
 				}
 
@@ -547,7 +557,7 @@
 			children[idx] = newChildRev
 			rev := rev.UpdateChildren(name, children, branch)
 			branch.Latest.Drop(txid, false)
-			n.MakeLatest(branch, rev, nil)
+			n.Root.MakeLatest(branch, rev, nil)
 			return rev
 		} else {
 			log.Errorf("cannot add to non-keyed container")
@@ -614,7 +624,7 @@
 				children[idx] = newChildRev
 				rev := rev.UpdateChildren(name, children, branch)
 				branch.Latest.Drop(txid, false)
-				n.MakeLatest(branch, rev, nil)
+				n.Root.MakeLatest(branch, rev, nil)
 				return rev
 			} else {
 				for _, v := range rev.GetChildren()[name] {
@@ -633,7 +643,7 @@
 				children = append(children[:idx], children[idx+1:]...)
 				rev := rev.UpdateChildren(name, children, branch)
 				branch.Latest.Drop(txid, false)
-				n.MakeLatest(branch, rev, postAnnouncement)
+				n.Root.MakeLatest(branch, rev, postAnnouncement)
 				return rev
 			}
 		} else {
@@ -685,7 +695,7 @@
 	rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun)
 
 	if !dryRun {
-		n.MakeLatest(dstBranch, rev, changes)
+		n.Root.MakeLatest(dstBranch, rev, changes)
 		delete(n.Branches, txid)
 	}
 
@@ -742,7 +752,7 @@
 		path = path[1:]
 	}
 	if path == "" {
-		return n.makeProxy(path, exclusive)
+		return n.makeProxy(path, fullPath, exclusive)
 	}
 
 	rev := n.Branches[NONE].Latest
@@ -788,7 +798,7 @@
 	return nil
 }
 
-func (n *node) makeProxy(fullPath string, exclusive bool) *Proxy {
+func (n *node) makeProxy(path string, fullPath string, exclusive bool) *Proxy {
 	r := &root{
 		node:                  n,
 		Callbacks:             n.Root.Callbacks,
@@ -800,7 +810,7 @@
 	}
 
 	if n.Proxy == nil {
-		n.Proxy = NewProxy(r, fullPath, exclusive)
+		n.Proxy = NewProxy(r, path, fullPath, exclusive)
 	} else {
 		if n.Proxy.Exclusive {
 			log.Error("node is already owned exclusively")