1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::collections::HashMap;
use std::marker::PhantomData;
use std::mem::take;
use std::path::Path;

use anyhow::{anyhow, Result};
use log::*;

use super::{Dedup, Interaction, Key};
use crate::arrow::*;
use crate::io::{file_size, ObjectWriter};
use crate::util::logging::item_progress;
use crate::util::Timer;

/// Record for a single output rating.
#[derive(TableRow, Debug)]
pub struct TimestampRatingRecord {
    pub user: i32,
    pub item: i32,
    pub rating: f32,
    pub last_rating: f32,
    pub timestamp: i64,
    pub last_time: i64,
    pub nratings: i32,
}

/// Record for a single output rating without time.
#[derive(TableRow, Debug)]
pub struct TimelessRatingRecord {
    pub user: i32,
    pub item: i32,
    pub rating: f32,
    pub nratings: i32,
}

/// Collapse a sequence of ratings into a rating record.
pub trait FromRatingSet {
    fn create(user: i32, item: i32, ratings: Vec<(f32, i64)>) -> Self;
}

impl FromRatingSet for TimestampRatingRecord {
    fn create(user: i32, item: i32, ratings: Vec<(f32, i64)>) -> Self {
        let mut vec = ratings;
        if vec.len() == 1 {
            // fast path
            let (rating, timestamp) = vec[0];
            TimestampRatingRecord {
                user,
                item,
                rating,
                timestamp,
                last_rating: rating,
                last_time: timestamp,
                nratings: 1,
            }
        } else {
            vec.sort_unstable_by_key(|(r, _ts)| (r * 10.0) as i32);
            let (rating, timestamp) = if vec.len() % 2 == 0 {
                let mp_up = vec.len() / 2;
                // we need this and the previous
                let (r1, ts1) = vec[mp_up - 1];
                let (r2, ts2) = vec[mp_up];
                // and average
                ((r1 + r2) * 0.5, (ts1 + ts2) / 2)
            } else {
                vec[vec.len() / 2]
            };
            vec.sort_unstable_by_key(|(_r, ts)| *ts);
            let (last_rating, last_time) = vec[vec.len() - 1];

            TimestampRatingRecord {
                user,
                item,
                rating,
                timestamp,
                last_rating,
                last_time,
                nratings: vec.len() as i32,
            }
        }
    }
}

impl FromRatingSet for TimelessRatingRecord {
    fn create(user: i32, item: i32, ratings: Vec<(f32, i64)>) -> Self {
        let mut vec = ratings;
        if vec.len() == 1 {
            // fast path
            let (rating, _ts) = vec[0];
            TimelessRatingRecord {
                user,
                item,
                rating,
                nratings: 1,
            }
        } else {
            vec.sort_unstable_by_key(|(r, _ts)| (r * 10.0) as i32);
            let (rating, _ts) = if vec.len() % 2 == 0 {
                let mp_up = vec.len() / 2;
                // we need this and the previous
                let (r1, ts1) = vec[mp_up - 1];
                let (r2, ts2) = vec[mp_up];
                // and average
                ((r1 + r2) * 0.5, (ts1 + ts2) / 2)
            } else {
                vec[vec.len() / 2]
            };

            TimelessRatingRecord {
                user,
                item,
                rating,
                nratings: vec.len() as i32,
            }
        }
    }
}

/// Rating deduplicator.
pub struct RatingDedup<R>
where
    R: FromRatingSet,
{
    _phantom: PhantomData<R>,
    table: HashMap<Key, Vec<(f32, i64)>>,
}

impl<I: Interaction, R> Dedup<I> for RatingDedup<R>
where
    R: FromRatingSet + TableRow + Send + Sync + 'static,
{
    fn add_interaction(&mut self, act: I) -> Result<()> {
        let rating = act
            .get_rating()
            .ok_or_else(|| anyhow!("rating deduplicator requires ratings"))?;
        self.record(act.get_user(), act.get_item(), rating, act.get_timestamp());
        Ok(())
    }

    fn save(&mut self, path: &Path) -> Result<usize> {
        self.write_ratings(path)
    }
}

impl<R> Default for RatingDedup<R>
where
    R: FromRatingSet + TableRow + Send + Sync + 'static,
{
    fn default() -> RatingDedup<R> {
        RatingDedup {
            _phantom: PhantomData,
            table: HashMap::new(),
        }
    }
}

impl<R> RatingDedup<R>
where
    R: FromRatingSet + TableRow + Send + Sync + 'static,
{
    /// Add a rating to the deduplicator.
    pub fn record(&mut self, user: i32, item: i32, rating: f32, timestamp: i64) {
        let k = Key::new(user, item);
        // get the vector for this user/item pair
        let vec = self.table.entry(k).or_insert_with(|| Vec::with_capacity(1));
        // and insert our records!
        vec.push((rating, timestamp));
    }

    /// Save the rating table disk.
    pub fn write_ratings<P: AsRef<Path>>(&mut self, path: P) -> Result<usize> {
        let path = path.as_ref();
        info!(
            "writing {} deduplicated ratings to {}",
            friendly::scalar(self.table.len()),
            path.display()
        );
        let mut writer = TableWriter::open(path)?;

        let n = self.table.len() as u64;
        let timer = Timer::new();
        let pb = item_progress(n, "writing ratings");

        // we're going to consume the hashtable.
        let table = take(&mut self.table);
        for (k, vec) in pb.wrap_iter(table.into_iter()) {
            let record = R::create(k.user, k.item, vec);
            writer.write_object(record)?;
        }

        let rv = writer.finish()?;
        pb.finish_and_clear();

        info!(
            "wrote {} ratings in {}, file is {}",
            friendly::scalar(n),
            timer.human_elapsed(),
            friendly::bytes(file_size(path)?)
        );

        Ok(rv)
    }
}