aboutsummaryrefslogtreecommitdiff
path: root/indexes.go
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <a.kiryukhin@mail.ru>2019-05-30 14:05:08 +0300
committerAlexander NeonXP Kiryukhin <a.kiryukhin@mail.ru>2019-05-30 14:05:08 +0300
commit2f4b6e90597784a8a7c01027e0ff5c6b69634a96 (patch)
tree5d94daf9fc394425b03baf3915ffd03a0011e442 /indexes.go
parent97fd40df4d85d91f3414dc88aa96222c1d827b49 (diff)
Parallel write
Speedup
Diffstat (limited to 'indexes.go')
-rw-r--r--indexes.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/indexes.go b/indexes.go
new file mode 100644
index 0000000..d8dbb32
--- /dev/null
+++ b/indexes.go
@@ -0,0 +1,96 @@
+package main
+
+import (
+ "context"
+ "log"
+
+ "go.mongodb.org/mongo-driver/mongo"
+ "go.mongodb.org/mongo-driver/mongo/options"
+ "go.mongodb.org/mongo-driver/x/bsonx"
+)
+
+func createIndexes(db *mongo.Database) error {
+ opts := options.CreateIndexes().SetMaxTime(1000)
+ nodes := db.Collection("nodes")
+ log.Println("creating indexes for nodes")
+ created, err := nodes.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
+ {
+ Keys: bsonx.Doc{{"osm_id", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true).SetUnique(false),
+ }, {
+ Keys: bsonx.Doc{{"osm_id", bsonx.Int32(-1)}, {"version", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true).SetUnique(false),
+ }, {
+ Keys: bsonx.Doc{{"tags", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true),
+ },
+ }, opts)
+ if err != nil {
+ return err
+ }
+ log.Println(created)
+ log.Println("creating geoindexes for nodes")
+ if err := geoIndex(nodes, "location"); err != nil {
+ return err
+ }
+
+ log.Println("creating indexes for ways")
+ ways := db.Collection("ways")
+ created, err = ways.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
+ {
+ Keys: bsonx.Doc{{"osm_id", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true).SetUnique(false),
+ }, {
+ Keys: bsonx.Doc{{"osm_id", bsonx.Int32(-1)}, {"version", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true).SetUnique(false),
+ }, {
+ Keys: bsonx.Doc{{"tags", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true),
+ },
+ }, opts)
+ if err != nil {
+ return err
+ }
+ log.Println(created)
+
+ relations := db.Collection("relations")
+ log.Println("creating geoindexes for relations")
+ created, err = relations.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
+ {
+ Keys: bsonx.Doc{{"osm_id", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true).SetUnique(false),
+ }, {
+ Keys: bsonx.Doc{{"osm_id", bsonx.Int32(-1)}, {"version", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true).SetUnique(false),
+ }, {
+ Keys: bsonx.Doc{{"tags", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true),
+ },
+ {
+ Keys: bsonx.Doc{{"members.ref", bsonx.Int32(-1)}},
+ Options: (options.Index()).SetBackground(true).SetSparse(true),
+ },
+ }, opts)
+ if err != nil {
+ return err
+ }
+ log.Println(created)
+ if err := geoIndex(relations, "members.coords"); err != nil {
+ return err
+ }
+ log.Println("indexes created")
+ return nil
+}
+
+func geoIndex(col *mongo.Collection, key string) error {
+ _, err := col.Indexes().CreateOne(
+ context.Background(),
+ mongo.IndexModel{
+ Keys: bsonx.Doc{{
+ Key: key, Value: bsonx.String("2dsphere"),
+ }},
+ Options: options.Index().SetSphereVersion(2).SetSparse(true).SetBackground(true),
+ },
+ )
+ return err
+}