func LoadDept(host string, port string) (*Dept, error) { appDb, error := db.NewDatabase(host, port, applicationsSuffix) if error != nil { return nil, error } reviewerDb, error := db.NewDatabase(host, port, reviewersSuffix) if error != nil { return nil, error } commentsDb, error := db.NewDatabase(host, port, commentsSuffix) if error != nil { return nil, error } highlightsDb, error := db.NewDatabase(host, port, highlightsSuffix) if error != nil { return nil, error } scoresDb, error := db.NewDatabase(host, port, scoresSuffix) if error != nil { return nil, error } uploadsDB, error := db.NewDatabase(host, port, "uploads") if error != nil { return nil, error } fromApplicantsDB, error := db.NewDatabase(host, port, "from-applicants") if error != nil { return nil, error } dept := &Dept{&appDb, &reviewerDb, &commentsDb, &highlightsDb, &scoresDb, &uploadsDB, &fromApplicantsDB} for _, deptDB := range dept.databases() { if !deptDB.Exists() { return nil, errors.New(fmt.Sprintf("database %v missing", deptDB.Name)) } } return dept, nil }
func NewDept(host string, port string) (dept *Dept, err error) { _, err = db.NewDatabase(host, port, applicationsSuffix) if err != nil { return nil, err } _, err = db.NewDatabase(host, port, reviewersSuffix) if err != nil { return nil, err } commentsDB, err := db.NewDatabase(host, port, commentsSuffix) if err != nil { return nil, err } highlightsDB, err := db.NewDatabase(host, port, highlightsSuffix) if err != nil { return nil, err } _, err = db.NewDatabase(host, port, "uploads") if err != nil { return nil, err } commentsDesign := map[string]interface{}{ "_id": "_design/myviews", "language": "javascript", "views": map[string]interface{}{ "byAppId": map[string]interface{}{ "map": `function(doc) { emit(doc.appId, doc); }`, }, }, } _, _, err = commentsDB.Insert(commentsDesign) if err != nil { return nil, err } highlightsDesign := map[string]interface{}{ "_id": "_design/myviews", "language": "javascript", "views": map[string]interface{}{ "byReader": map[string]interface{}{ "map": `function(doc) { emit(doc.readerId, { writerId: doc.writerId, appId: doc.appId }); }`, }, "byApp": map[string]interface{}{ "map": `function(doc) { emit(doc.appId, { _rev: doc._rev, _id: doc._id, readerId: doc.readerId }); }`, }, }, } _, _, err = highlightsDB.Insert(highlightsDesign) if err != nil { return nil, err } const averagesMap = `function(doc) { var r = { }; r[doc.label] = { sum: doc.score, len: 1, avg: doc.score }; emit(doc.appId, r); }` const averagesReduce = `function (key, values, rereduce) { var r = { }; for (var i = 0; i < values.length; i++) { for (var label in values[i]) { if (!values[i].hasOwnProperty(label)) { continue; } if (!r.hasOwnProperty(label)) { r[label] = { sum: 0, len: 0 }; } r[label].sum += values[i][label].sum; r[label].len += values[i][label].len; r[label].avg = r[label].sum / r[label].len; } } return r; }` scoresDB, err := db.NewDatabase(host, port, scoresSuffix) if err != nil { return nil, err } scoresDesign := map[string]interface{}{ "_id": "_design/myviews", "language": "javascript", "views": map[string]interface{}{ "byId": map[string]interface{}{ "map": `function(doc) { emit(doc._id, doc); }`, }, "averages": map[string]interface{}{ "map": averagesMap, "reduce": averagesReduce, }, }, } _, _, err = scoresDB.Insert(scoresDesign) if err != nil { return nil, err } dept, err = LoadDept(host, port) return }