Skip to content

Commit abf3cc5

Browse files
committed
error when trying to create diffs for data too old for server
1 parent b2d2560 commit abf3cc5

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/osmium/replication/server.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,24 @@ def apply_diffs_to_file(self, infile: str, outfile: str, start_id: int,
305305
return (diffs.id, diffs.newest)
306306

307307
def timestamp_to_sequence(self, timestamp: dt.datetime,
308-
balanced_search: bool = False) -> Optional[int]:
308+
balanced_search: bool = False,
309+
limit_by_oldest_available: bool = False) -> Optional[int]:
309310
""" Get the sequence number of the replication file that contains the
310311
given timestamp. The search algorithm is optimised for replication
311312
servers that publish updates in regular intervals. For servers
312313
with irregular change file publication dates 'balanced_search`
313314
should be set to true so that a standard binary search for the
314315
sequence will be used. The default is good for all known
315316
OSM replication services.
316-
"""
317317
318+
When `limit_by_oldest_available` is set, then the function will
319+
return None when the server replication does not start at 0 and
320+
the given timestamp is older than the oldest available timestamp
321+
on the server. Some replication servers do not keep the full
322+
history and this flag avoids accidentally trying to download older
323+
data. The downside is that the function will never return the
324+
oldest available sequence ID when the flag is set.
325+
"""
318326
# get the current timestamp from the server
319327
upper = self.get_state_info()
320328

@@ -331,8 +339,10 @@ def timestamp_to_sequence(self, timestamp: dt.datetime,
331339
lower = self.get_state_info(lowerid)
332340

333341
if lower is not None and lower.timestamp >= timestamp:
334-
if lower.sequence == 0 or lower.sequence + 1 >= upper.sequence:
335-
return lower.sequence
342+
if lower.sequence == 0:
343+
return 0
344+
if lower.sequence + 1 >= upper.sequence:
345+
return None if limit_by_oldest_available else lower.sequence
336346
upper = lower
337347
lower = None
338348
lowerid = 0

src/osmium/tools/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_sequence(self, svr: ReplicationServer) -> Optional[int]:
3333

3434
assert self.date is not None
3535
log.debug("Looking up sequence ID for timestamp %s" % self.date)
36-
return svr.timestamp_to_sequence(self.date)
36+
return svr.timestamp_to_sequence(self.date, limit_by_oldest_available=True)
3737

3838
def get_end_sequence(self, svr: ReplicationServer) -> Optional[int]:
3939
if self.seq_id is not None:

test/test_pyosmium_up-to-date.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,9 @@ def test_update_with_enddate(test_data, runner, tmp_path):
168168
osmium.apply(newfile, ids)
169169

170170
assert ids.relations == list(range(101, 106))
171+
172+
173+
def test_change_date_too_old_for_replication_source(test_data, runner):
174+
outfile = test_data("n1 v1 t2070-04-05T06:30:00Z")
175+
176+
assert 3 == runner(outfile)

0 commit comments

Comments
 (0)