]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/index.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / clicmd / index.py
1 """
2 Implementation of the 'index' subcommand.
3 """
4 import os
5
6 from ..db import status
7 from ..db.connection import connect
8
9 # Do not repeat documentation of subcommand classes.
10 # pylint: disable=C0111
11 # Using non-top-level imports to avoid eventually unused imports.
12 # pylint: disable=E0012,C0415
13
14 def _num_system_cpus():
15     try:
16         cpus = len(os.sched_getaffinity(0))
17     except NotImplementedError:
18         cpus = None
19
20     return cpus or os.cpu_count()
21
22
23 class UpdateIndex:
24     """\
25     Reindex all new and modified data.
26     """
27
28     @staticmethod
29     def add_args(parser):
30         group = parser.add_argument_group('Filter arguments')
31         group.add_argument('--boundaries-only', action='store_true',
32                            help="""Index only administrative boundaries.""")
33         group.add_argument('--no-boundaries', action='store_true',
34                            help="""Index everything except administrative boundaries.""")
35         group.add_argument('--minrank', '-r', type=int, metavar='RANK', default=0,
36                            help='Minimum/starting rank')
37         group.add_argument('--maxrank', '-R', type=int, metavar='RANK', default=30,
38                            help='Maximum/finishing rank')
39
40     @staticmethod
41     def run(args):
42         from ..indexer.indexer import Indexer
43
44         indexer = Indexer(args.config.get_libpq_dsn(),
45                           args.threads or _num_system_cpus() or 1)
46
47         if not args.no_boundaries:
48             indexer.index_boundaries(args.minrank, args.maxrank)
49         if not args.boundaries_only:
50             indexer.index_by_rank(args.minrank, args.maxrank)
51
52         if not args.no_boundaries and not args.boundaries_only \
53            and args.minrank == 0 and args.maxrank == 30:
54             conn = connect(args.config.get_libpq_dsn())
55             status.set_indexed(conn, True)
56             conn.close()
57
58         return 0