Skip to content

Commit c98ba8f

Browse files
committed
rework hash function
1 parent a8c8341 commit c98ba8f

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/db/sqlite_db.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use alive_lock_file::LockResultWithDrop;
22
use derivative::Derivative;
33
use futures::StreamExt;
4-
use itertools::Itertools;
54
use sqlx::{migrate::MigrateDatabase, prelude::*, Sqlite, SqliteConnection};
65
use std::{
76
cell::RefCell,
@@ -23,7 +22,7 @@ use crate::{
2322
utils::{self},
2423
};
2524

26-
use super::{now, DbMessage, DbTrait, EntryId, EntryTrait, MimeDataMap};
25+
use super::{now, DbMessage, DbTrait, EntryId, EntryTrait, MimeDataMap, PRIV_MIME_TYPES_SIMPLE};
2726

2827
type Time = i64;
2928

@@ -49,11 +48,11 @@ pub struct DbSqlite {
4948
lock: Option<LockResultWithDrop>,
5049
}
5150

52-
#[derive(Clone, Eq, Derivative)]
51+
#[derive(Clone, Derivative)]
5352
pub struct Entry {
5453
pub id: EntryId,
5554
pub creation: Time,
56-
// todo: lazelly load image in memory, since we can't search them anyways
55+
// todo: lazelly load image in memory, since we can't search them anyways?
5756
/// (Mime, Content)
5857
pub raw_content: MimeDataMap,
5958
pub is_favorite: bool,
@@ -107,11 +106,21 @@ impl Favorites {
107106
}
108107

109108
fn hash_entry_content<H: Hasher>(data: &MimeDataMap, state: &mut H) {
110-
data.iter()
111-
.sorted_by(|e1, e2| e1.0.cmp(e2.0))
112-
.for_each(|e| {
113-
e.hash(state);
114-
});
109+
for m in PRIV_MIME_TYPES_SIMPLE {
110+
if let Some(content) = data.get(*m) {
111+
if !content.is_empty() {
112+
content.hash(state);
113+
return;
114+
}
115+
}
116+
}
117+
118+
let mut sorted = data.iter().collect::<Vec<_>>();
119+
sorted.sort_by(|(mime1, _), (mime2, _)| mime1.cmp(mime2));
120+
121+
for (_, content) in sorted {
122+
content.hash(state);
123+
}
115124
}
116125

117126
fn get_hash_entry_content(data: &MimeDataMap) -> u64 {
@@ -126,12 +135,6 @@ impl Hash for Entry {
126135
}
127136
}
128137

129-
impl PartialEq for Entry {
130-
fn eq(&self, other: &Self) -> bool {
131-
self.id == other.id
132-
}
133-
}
134-
135138
impl EntryTrait for Entry {
136139
fn is_favorite(&self) -> bool {
137140
self.is_favorite
@@ -424,6 +427,7 @@ impl DbTrait for DbSqlite {
424427

425428
if let Some(id) = self.hashs.get(&hash) {
426429
let entry = self.entries.get_mut(id).unwrap();
430+
427431
let old_creation = entry.creation;
428432
entry.creation = now;
429433
let res = self.times.remove(&old_creation);

0 commit comments

Comments
 (0)