@@ -192,6 +192,15 @@ class NilmDB(object):
# Return cached value
return self._cached_iset[stream_id]
# TODO: Split add_interval into two pieces, one to add
# and one to flush to disk?
# Need to think about this. Basic problem is that we can't
# mess with intervals once they're in the IntervalSet,
# without mucking with bxinterval internals.
# Maybe add a separate optimization step?
# Join intervals that have a fairly small gap between them
def _add_interval(self, stream_id, interval, start_pos, end_pos):
"""
Add interval to the internal interval cache, and to the database.
@@ -201,7 +210,6 @@ class NilmDB(object):
# interval to that cache.
iset = self._get_intervals(stream_id)
try:
# XXX TODO: Intervals should hold start_pos, end_pos too
iset += DBInterval(interval.start, interval.end,
interval.start, interval.end,
start_pos, end_pos)
@@ -353,7 +361,7 @@ class NilmDB(object):
data.update(newdata)
self.stream_set_metadata(path, data)
def stream_insert(self, path, parser):
def stream_insert(self, path, parser, old_timestamp = None ):
"""Insert new data into the database.
path: Path at which to add the data
parser: nilmdb.layout.Parser instance full of data to insert
@@ -362,10 +370,21 @@ class NilmDB(object):
not len(parser.data)):
raise StreamError("no data provided")
# First check for basic overlap using timestamp info from the parser.
# If we were provided with an old timestamp, the expectation
# is that the client has a contiguous block of time it is sending,
# but it's doing it over multiple calls to stream_insert.
# old_timestamp is the max_timestamp of the previous insert.
# To make things continuous, use that as our starting timestamp
# instead of what the parser found.
if old_timestamp:
min_timestamp = old_timestamp
else:
min_timestamp = parser.min_timestamp
# First check for basic overlap using timestamp info given.
stream_id = self._stream_id(path)
iset = self._get_intervals(stream_id)
interval = Interval(parser.min_timestamp, parser.max_timestamp)
interval = Interval(min_timestamp, parser.max_timestamp)
if iset.intersects(interval):
raise OverlapError("new data overlaps existing data: "
+ str(iset & interval))