Commit 71eb4cb

Richard Luby <richluby@gmail.com>
2017-01-25 16:34:32
categories stored as materialized paths
see https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-materialized-paths/ for additional information regarding the materialized paths implementation
1 parent 74bf586
question.go
@@ -9,49 +9,16 @@ import (
 	"strings"
 )
 
-// buildTagPath creates the heirarchical list of tags
-// given the linearly related tag relationship
-// returns the index of the newly created category
-func buildTagPath(tags []string) int {
-	currentChosen := getNearestParentCategory(tags)
-	if currentChosen < 0 { //not found in tree
-		for _, tag := range tags {
-			cat := Category{Parent: len(categories) - 1,
-				Value: tag}
-			categories = append(categories, cat)
-			currentChosen = len(categories) - 1
-		}
-		categories[len(categories)-len(tags)].Parent = -1
-	} else { // some parent found somewhere
-		tagIndex := 0
-		for parentIndex := currentChosen; parentIndex >= 0; parentIndex = categories[parentIndex].Parent {
-			tagIndex++ // counting the depth in the tree
-		}
-		for tagIndex < len(tags) { // everything from here on is unkown and needs creation
-			newCat := Category{Parent: currentChosen,
-				Value: tags[tagIndex]}
-			categories = append(categories, newCat)
-			currentChosen = len(categories) - 1
-			categories[newCat.Parent].Children = append(categories[newCat.Parent].Children, currentChosen)
-			tagIndex++
-		}
-	}
-	return currentChosen
-}
-
 // assignCategory figures out what category
 // this question is a member of
-func assignCategory(file *os.File) int {
+func assignCategory(file *os.File) string {
 	tagPath := strings.Replace(file.Name(), serverConfig.QUESTIONS, "", 1)
 	tags := strings.Split(tagPath, string(os.PathSeparator))
 	tags[len(tags)-1] = strings.Replace(tags[len(tags)-1], ".csv", "", -1)
 	if tags[0] == "" {
 		tags = tags[1:]
 	}
-	for i, tag := range tags{
-		tags[i] = strings.TrimSpace(tag)
-	}
-	return buildTagPath(tags)
+	return strings.Join(tags, CATEGORY_SEPARATOR)
 }
 
 // parseLine parses a csv line and returns a single record
@@ -83,15 +50,17 @@ func LoadFile(file *os.File) error {
 	if !strings.HasSuffix(file.Name(), ".csv") {
 		return nil
 	}
-	catIndex := assignCategory(file)
+	catPath := assignCategory(file)
 	scanner := bufio.NewScanner(file)
 	for scanner.Scan() {
 		if record, err := parseLine(scanner.Text()); err != nil {
 			log.Printf("Error while parsing line: %+v.", err)
 		} else {
 			if record.Answer != "" {
-				record.Category = catIndex
-				categories[catIndex].RecordArray = append(categories[catIndex].RecordArray, record)
+				record.Path = catPath
+				if err := addRecordToDB(&record); err != nil {
+					log.Printf("Error loading record into DB: %+v.", err)
+				}
 			}
 		}
 	}
structures.go
@@ -47,8 +47,8 @@ var EXIT_CODE = EXIT_CODES{
 type Record struct {
 	Question,
 	Reference,
-	Answer string
-	Category,
+	Answer,
+	Path string
 	ID int
 }