VOL-1334 : Fixed concurrency issues
- Semaphores were added at the different layers of the model
- Made the proxy interfaces more robust
- Eliminated problems while retrieving latest data in concurrent mode
Change-Id: I7854105d7effa10e5cb704f5d9917569ab184f84
diff --git a/db/model/branch.go b/db/model/branch.go
index ae0441a..3408f18 100644
--- a/db/model/branch.go
+++ b/db/model/branch.go
@@ -13,12 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package model
+import "sync"
+
// TODO: implement weak references or something equivalent
// TODO: missing proper logging
+// Branch structure is used to classify a collection of transaction based revisions
type Branch struct {
+ sync.RWMutex
Node *node
Txid string
Origin Revision
@@ -26,24 +31,65 @@
Latest Revision
}
+// NewBranch creates a new instance of the Branch structure
func NewBranch(node *node, txid string, origin Revision, autoPrune bool) *Branch {
- cb := &Branch{}
- cb.Node = node
- cb.Txid = txid
- cb.Origin = origin
- cb.Revisions = make(map[string]Revision)
- cb.Latest = origin
+ b := &Branch{}
+ b.Node = node
+ b.Txid = txid
+ b.Origin = origin
+ b.Revisions = make(map[string]Revision)
+ b.Latest = origin
- return cb
+ return b
}
-// TODO: Check if the following are required
-func (cb *Branch) get(hash string) Revision {
- return cb.Revisions[hash]
+// SetLatest assigns the latest revision for this branch
+func (b *Branch) SetLatest(latest Revision) {
+ b.Lock()
+ defer b.Unlock()
+
+ b.Latest = latest
}
-func (cb *Branch) GetLatest() Revision {
- return cb.Latest
+
+// GetLatest retrieves the latest revision of the branch
+func (b *Branch) GetLatest() Revision {
+ b.Lock()
+ defer b.Unlock()
+
+ return b.Latest
}
-func (cb *Branch) GetOrigin() Revision {
- return cb.Origin
+
+// GetOrigin retrieves the original revision of the branch
+func (b *Branch) GetOrigin() Revision {
+ b.Lock()
+ defer b.Unlock()
+
+ return b.Origin
+}
+
+// AddRevision inserts a new revision to the branch
+func (b *Branch) AddRevision(revision Revision) {
+ if revision != nil && b.GetRevision(revision.GetHash()) == nil {
+ b.SetRevision(revision.GetHash(), revision)
+ }
+}
+
+// GetRevision pulls a revision entry at the specified hash
+func (b *Branch) GetRevision(hash string) Revision {
+ b.Lock()
+ defer b.Unlock()
+
+ if revision, ok := b.Revisions[hash]; !ok {
+ return revision
+ }
+
+ return nil
+}
+
+// SetRevision updates a revision entry at the specified hash
+func (b *Branch) SetRevision(hash string, revision Revision) {
+ b.Lock()
+ defer b.Unlock()
+
+ b.Revisions[hash] = revision
}