From 290772b4379b535836e8e9561a0abc0f7e405c67 Mon Sep 17 00:00:00 2001 From: lealife Date: Mon, 30 May 2016 11:51:41 +0800 Subject: [PATCH] init index --- app/db/Mgo.go | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++ conf/app.conf | 6 +- 2 files changed, 213 insertions(+), 3 deletions(-) diff --git a/app/db/Mgo.go b/app/db/Mgo.go index 7f8c181..2fd82cf 100644 --- a/app/db/Mgo.go +++ b/app/db/Mgo.go @@ -166,6 +166,216 @@ func Init(url, dbname string) { // session Sessions = Session.DB(dbname).C("sessions") + + collectionNames, err := Session.DB(dbname).CollectionNames() + + // 如果没有表, 则表示是第一次安装 + if (err != nil || len(collectionNames) == 0) { + Log("init db index") + initIndex(dbname) + } +} + +func initIndex(dbname string) { + // 索引 + indexes := map[string][]mgo.Index{ + "notebooks": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "notes": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + mgo.Index{ + Key: []string{"IsTrash"}, + Background: true, + }, + mgo.Index{ + Key: []string{"UrlTitle"}, + Background: true, + }, + mgo.Index{ + Key: []string{"IsBlog"}, + Background: true, + }, + mgo.Index{ + Key: []string{"CreatedTime"}, + Background: true, + }, + mgo.Index{ + Key: []string{"UpdatedTime"}, + Background: true, + }, + mgo.Index{ + Key: []string{"PublicTime"}, + Background: true, + }, + mgo.Index{ + Key: []string{"RecommendTime"}, + Background: true, + }, + mgo.Index{ + Key: []string{"NotebookId"}, + Background: true, + }, + mgo.Index{ + Key: []string{"UserId", "IsBlog"}, + Background: true, + }, + }, + "note_contents": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "note_content_histories": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "share_notes": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + mgo.Index{ + Key: []string{"ToUserId"}, + Background: true, + }, + }, + "share_notebooks": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + mgo.Index{ + Key: []string{"ToUserId"}, + Background: true, + }, + }, + "has_share_notes": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId", "ToUserId"}, + Background: true, + Unique: true, + }, + }, + "users": []mgo.Index{ + mgo.Index{ + Key: []string{"Email"}, + Background: true, + }, + mgo.Index{ + Key: []string{"Username"}, + Background: true, + }, + }, + "groups": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "group_users": []mgo.Index{ + mgo.Index{ + Key: []string{"GroupId"}, + Background: true, + }, + }, + "tags": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "note_tags": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId", "TagId"}, + Background: true, + }, + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "tag_count": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "blog_singles": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "blog_likes": []mgo.Index{ + mgo.Index{ + Key: []string{"NoteId"}, + Background: true, + }, + }, + "blog_comments": []mgo.Index{ + mgo.Index{ + Key: []string{"NoteId"}, + Background: true, + }, + }, + "themes": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "note_images": []mgo.Index{ + mgo.Index{ + Key: []string{"ImageId"}, + Background: true, + }, + mgo.Index{ + Key: []string{"NoteId"}, + Background: true, + }, + }, + "albums": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "files": []mgo.Index{ + mgo.Index{ + Key: []string{"UserId"}, + Background: true, + }, + }, + "attachs": []mgo.Index{ + mgo.Index{ + Key: []string{"NoteId"}, + Background: true, + }, + }, + "sessions": []mgo.Index{ + mgo.Index{ + Key: []string{"SessionId"}, + Background: true, + }, + }, + } + + for collection, ins := range indexes { + col := Session.DB(dbname).C(collection) + for _, in := range ins { + col.EnsureIndex(in) + } + } } func close() { diff --git a/conf/app.conf b/conf/app.conf index 478482e..a38ac9e 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -1,5 +1,5 @@ #------------------------ -# leanote config +# Leanote config #------------------------ http.port=9000 @@ -12,13 +12,13 @@ adminUsername=admin # mongdb db.host=127.0.0.1 db.port=27017 -db.dbname=leanote # required +db.dbname=leanote2 # required db.username= # if not exists, please leave it blank db.password= # if not exists, please leave it blank # or you can set the mongodb url for more complex needs the format is: # mongodb://myuser:mypass@localhost:40001,otherhost:40001/mydb # db.url=mongodb://root:root123@localhost:27017/leanote -# db.urlEnv=${MONGODB_URL} # set url from env. eg. mongodb://root:root123@localhost:27017/leanote +db.urlEnv=${MONGODB_URL} # set url from env, for docker. eg. mongodb://root:root123@localhost:27017/leanote # You Must Change It !! About Security!! app.secret=V85ZzBeTnzpsHyjQX4zukbQ8qqtju9y2aDM55VWxAH9Qop19poekx3xkcDVvrD0y #