aboutsummaryrefslogtreecommitdiff
path: root/indexes.go
blob: d8dbb3278a125a05cbd1c26e9980779074253d75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
}