From 3ec60b85a310e93701778aae14dd655733f8faf9 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Tue, 23 Mar 2021 09:40:52 -0700 Subject: [PATCH] extract & generalize calendar day indexes My main goal is to support creating indexes for signals as well as recordings. An additional goal is to just shrink db.rs a bit; it's gotten quite large. --- server/db/days.rs | 320 +++++++++++++++++++++++++++++++++++++++++++++ server/db/db.rs | 282 ++------------------------------------- server/db/lib.rs | 1 + server/src/json.rs | 5 +- 4 files changed, 332 insertions(+), 276 deletions(-) create mode 100644 server/db/days.rs diff --git a/server/db/days.rs b/server/db/days.rs new file mode 100644 index 0000000..7d8a51f --- /dev/null +++ b/server/db/days.rs @@ -0,0 +1,320 @@ +// This file is part of Moonfire NVR, a security camera network video recorder. +// Copyright (C) 2021 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt. +// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception. + +//! In-memory indexes by calendar day. + +use crate::recording::{self, Time}; +use failure::Error; +use log::{error, trace}; +use std::cmp; +use std::collections::BTreeMap; +use std::io::Write; +use std::ops::Range; +use std::str; + +/// A calendar day in `YYYY-mm-dd` format. +#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] +pub struct Key([u8; 10]); + +impl Key { + fn new(tm: time::Tm) -> Result { + let mut s = Key([0u8; 10]); + write!(&mut s.0[..], "{}", tm.strftime("%Y-%m-%d")?)?; + Ok(s) + } + + pub fn bounds(&self) -> Range