|
|
@@ -204,13 +204,38 @@ class NilmDB(object): |
|
|
|
if iset.intersects(interval): # pragma: no cover (gets caught earlier) |
|
|
|
raise NilmDBError("new interval overlaps existing data") |
|
|
|
|
|
|
|
# Check for adjacency |
|
|
|
# XX TODO |
|
|
|
# Check for adjacency. If there's a stream in the database |
|
|
|
# that ends exactly when this one starts, and the database |
|
|
|
# rows match up, we can make one interval that covers the |
|
|
|
# time range [adjacent.start -> interval.end) |
|
|
|
# and database rows [ adjacent.start_pos -> end_pos ]. |
|
|
|
# Only do this if the resulting interval isn't too large. |
|
|
|
max_merged_rows = 30000000 # a bit more than 1 hour at 8 KHz |
|
|
|
adjacent = iset.find_end(interval.start) |
|
|
|
if (adjacent is not None and |
|
|
|
start_pos == adjacent.db_endpos and |
|
|
|
(end_pos - adjacent.db_startpos) < max_merged_rows): |
|
|
|
# First delete the old one, both from our cache and the |
|
|
|
# database |
|
|
|
iset -= adjacent |
|
|
|
self.con.execute("DELETE FROM ranges WHERE " |
|
|
|
"stream_id=? AND start_time=? AND " |
|
|
|
"end_time=? AND start_pos=? AND " |
|
|
|
"end_pos=?", (stream_id, |
|
|
|
adjacent.db_start, |
|
|
|
adjacent.db_end, |
|
|
|
adjacent.db_startpos, |
|
|
|
adjacent.db_endpos)) |
|
|
|
|
|
|
|
# Now update our interval so the fallthrough add is |
|
|
|
# correct. |
|
|
|
interval.start = adjacent.start |
|
|
|
start_pos = adjacent.db_startpos |
|
|
|
|
|
|
|
# Add the new interval to the cache |
|
|
|
iset.iadd(DBInterval(interval.start, interval.end, |
|
|
|
interval.start, interval.end, |
|
|
|
start_pos, end_pos)) |
|
|
|
iset.iadd_nocheck(DBInterval(interval.start, interval.end, |
|
|
|
interval.start, interval.end, |
|
|
|
start_pos, end_pos)) |
|
|
|
|
|
|
|
# Insert into the database |
|
|
|
self.con.execute("INSERT INTO ranges " |
|
|
@@ -218,6 +243,7 @@ class NilmDB(object): |
|
|
|
"VALUES (?,?,?,?,?)", |
|
|
|
(stream_id, interval.start, interval.end, |
|
|
|
int(start_pos), int(end_pos))) |
|
|
|
|
|
|
|
self.con.commit() |
|
|
|
|
|
|
|
def stream_list(self, path = None, layout = None): |
|
|
|