From 8a1b0c3272fe5ed84e455d7481c2022dfada6bb0 Mon Sep 17 00:00:00 2001
From: Ashwin <ashwin2007ray@gmail.com>
Date: Tue, 2 Feb 2016 07:12:05 +0000
Subject: [PATCH] Move code into a function

---
 loader/loader.go | 48 ++++++++++++++++++++----------------------------
 query/query.go   |  2 +-
 2 files changed, 21 insertions(+), 29 deletions(-)

diff --git a/loader/loader.go b/loader/loader.go
index 5fa1e799..a3953e11 100644
--- a/loader/loader.go
+++ b/loader/loader.go
@@ -144,48 +144,40 @@ func (s *state) handleNQuads(wg *sync.WaitGroup) {
 	wg.Done()
 }
 
+func (s *state) getUidForString(str string) {
+	_, err := rdf.GetUid(str, s.instanceIdx, s.numInstances)
+	for err != nil {
+		// Just put in a retry loop to tackle temporary errors.
+		if err == posting.E_TMP_ERROR {
+			time.Sleep(time.Microsecond)
+			glog.WithError(err).WithField("nq.Subject", str).
+				Error("Temporary error")
+		} else {
+			glog.WithError(err).WithField("nq.Subject", str).
+				Error("While getting UID")
+			return
+		}
+		_, err = rdf.GetUid(str, s.instanceIdx, s.numInstances)
+	}
+}
+
 func (s *state) handleNQuadsWhileAssign(wg *sync.WaitGroup) {
 	for nq := range s.cnq {
 		if farm.Fingerprint64([]byte(nq.Subject))%s.numInstances != s.instanceIdx {
 			// This instance shouldnt assign UID to this string
 			atomic.AddUint64(&s.ctr.ignored, 1)
 		} else {
-			_, err := rdf.GetUid(nq.Subject, s.instanceIdx, s.numInstances)
-			for err != nil {
-				// Just put in a retry loop to tackle temporary errors.
-				if err == posting.E_TMP_ERROR {
-					time.Sleep(time.Microsecond)
-					glog.WithError(err).WithField("nq.Subject", nq.Subject).
-						Error("Temporary error")
-				} else {
-					glog.WithError(err).WithField("nq.Subject", nq.Subject).
-						Error("While getting UID")
-					return
-				}
-				_, err = rdf.GetUid(nq.Subject, s.instanceIdx, s.numInstances)
-			}
+			s.getUidForString(nq.Subject)
 		}
 
 		if len(nq.ObjectId) == 0 || farm.Fingerprint64([]byte(nq.ObjectId))%s.numInstances != s.instanceIdx {
 			// This instance shouldnt or cant assign UID to this string
 			atomic.AddUint64(&s.ctr.ignored, 1)
 		} else {
-			_, err := rdf.GetUid(nq.ObjectId, s.instanceIdx, s.numInstances)
-			for err != nil {
-				// Just put in a retry loop to tackle temporary errors.
-				if err == posting.E_TMP_ERROR {
-					time.Sleep(time.Microsecond)
-					glog.WithError(err).WithField("nq.Subject", nq.Subject).
-						Error("Temporary error")
-				} else {
-					glog.WithError(err).WithField("nq.ObjectId", nq.ObjectId).
-						Error("While getting UID")
-					return
-				}
-				_, err = rdf.GetUid(nq.ObjectId, s.instanceIdx, s.numInstances)
-			}
+			s.getUidForString(nq.ObjectId)
 		}
 	}
+
 	wg.Done()
 }
 
diff --git a/query/query.go b/query/query.go
index 3ee69a2a..4d2e28fe 100644
--- a/query/query.go
+++ b/query/query.go
@@ -255,7 +255,7 @@ func NewGraph(euid uint64, exid string) (*SubGraph, error) {
 	// This would set the Result field in SubGraph,
 	// and populate the children for attributes.
 	if len(exid) > 0 {
-		u, err := uid.GetOrAssign(exid, 0, 1)  // mod = 0, numInstances = 1 by default
+		u, err := uid.GetOrAssign(exid, 0, 1) // instanceIdx = 0, numInstances = 1 by default
 		if err != nil {
 			x.Err(glog, err).WithField("xid", exid).Error(
 				"While GetOrAssign uid from external id")
-- 
GitLab