[go: up one dir, main page]

File: correlate.cc

package info (click to toggle)
imms 3.1.0~rc8-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,228 kB
  • ctags: 1,243
  • sloc: cpp: 7,839; ansic: 1,845; xml: 252; sh: 189; makefile: 140
file content (284 lines) | stat: -rw-r--r-- 7,454 bytes parent folder | download | duplicates (3)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <assert.h>
#include <math.h>
#include <time.h>
#include <iostream>

#include "flags.h"
#include "correlate.h"
#include "strmanip.h"
#include "immsutil.h"

using std::endl;
using std::cerr;

#define CORRELATION_TIME    (15*30)   // n * 30 ==> n minutes
#define MAX_CORR_STR        "12"
#define MAX_CORRELATION     12
#define SECOND_DEGREE       0.5
#define PROCESSING_TIME     5000000

CorrelationDb::CorrelationDb() : correlate_from(time(0))
{
    gettimeofday(&start, 0);
}

void CorrelationDb::sql_create_tables()
{
    RuntimeErrorBlocker reb;
    try {
        Q("CREATE TABLE C.Correlations ("
                "'x' INTEGER NOT NULL, "
                "'y' INTEGER NOT NULL, "
                "'weight' INTEGER DEFAULT '0');").execute();

        Q("CREATE TEMP TABLE TmpCorr ("
                "'x' INTEGER NOT NULL, "
                "'y' INTEGER NOT NULL, "
                "'weight' INTEGER DEFAULT '0');").execute();

        Q("CREATE UNIQUE INDEX C.Correlations_x_y_i "
                "ON Correlations (x, y);").execute();

        Q("CREATE INDEX C.Correlations_x_i ON Correlations (x);").execute();
        Q("CREATE INDEX C.Correlations_y_i ON Correlations (y);").execute();
    }
    WARNIFFAILED();
}

void CorrelationDb::add_recent(int uid, time_t skipped_at, int flags)
{
    if (uid > -1)
    {
        try {
            Q q("INSERT INTO Journal VALUES (?, ?, ?, ?);");
            q << uid << skipped_at << flags << time(0);
            q.execute();
        }
        WARNIFFAILED();
    }
}

void CorrelationDb::get_related(vector<int> &out, int pivot_sid, int limit)
{
    string query =
        "SELECT pos FROM Filter NATURAL INNER JOIN Library "
            "WHERE sid IN ("
            "SELECT L.sid FROM C.Correlations AS C INNER JOIN Last AS L "
                "ON CASE WHEN C.x = ? THEN C.y ELSE C.x END = L.sid "
            "WHERE C.weight > 0 AND (C.x = ? OR C.y = ?) AND L.last > ? "
            "ORDER BY C.weight DESC LIMIT " + itos(limit) + ");";

    try {
        Q q(query);

        q << pivot_sid << pivot_sid << pivot_sid << (time(0) - HOUR);

        while (q.next())
        {
            int pos;
            q >> pos;
            out.push_back(pos);
        }
    }
    WARNIFFAILED();
}

void CorrelationDb::maybe_expire_recent()
{
    struct timeval now;
    gettimeofday(&now, 0);

    if (usec_diff(start, now) < 10000000)
        return;

    start = now;

    expire_recent(time(0) - CORRELATION_TIME);
}

void CorrelationDb::expire_recent(time_t cutoff)
{
#if 0 && defined(DEBUG)
    cerr << "Running expire recent..." << endl;
    StackTimer t;
#endif

    try {
        AutoTransaction a;

        while (1)
        {
            Q q("SELECT Library.sid, Journal.played, "
                    "Journal.flags, Journal.time "
                    "FROM Journal INNER JOIN Library "
                    "ON Journal.uid = Library.uid "
                    "WHERE Journal.time > ? ORDER BY Journal.time ASC;");
            q << correlate_from;

            if (!q.next())
                break;

            int flags;
            time_t next, played;

            q >> from >> played >> flags >> next;
            from_weight = Flags::deltify(played, flags);

            if (next > cutoff) 
                break;

            correlate_from = next + 1;

            if (from_weight == -1)
                continue;

            while (q.next())
            {
                q >> to >> played >> flags;
                to_weight = Flags::deltify(played, flags);
                expire_recent_helper();
            }
        }

        a.commit();
    }
    WARNIFFAILED();
}

void CorrelationDb::expire_recent_helper()
{
    if (to == from || to_weight == -1)
        return;

    if (from_weight < 0 && to_weight < 0)
        return;

#ifdef DEBUG
    //cerr << string(55, '-') << endl;
    cerr << "processing update between " << std::min(from, to) <<
        " and " << std::max(from, to) << endl;
#endif

    weight = sqrt(abs(from_weight * to_weight));

    if (from_weight < 0 || to_weight < 0)
        weight = -weight;

    // Update the primary link
    update_correlation(from, to, weight);

    struct timeval now;
    gettimeofday(&now, 0);

    if (usec_diff(start, now) > PROCESSING_TIME || fabs(weight) < 2)
        return;
    
    try {
        Q("DELETE FROM TmpCorr;").execute();
    } IGNOREFAILURE();

    {
        string query("INSERT INTO TmpCorr SELECT x, y, weight "
            "FROM C.Correlations "
            "WHERE (x IN (?, ?) OR y IN (?, ?)) AND ");
        query += (weight > 0 ? string("abs") : string("")) + " (weight) > 1;";

        Q q(query);
        q << to << from << to << from;
        q.execute();
    }

    {
        Q q("SELECT * FROM TmpCorr;");
        while (q.next())
        {
            int node1, node2;
            float outer;
            q >> node1 >> node2 >> outer;
            update_secondary_correlations(node1, node2, outer);
        }
    }
}

void CorrelationDb::update_secondary_correlations(int node1, int node2,
        float outer)
{
    // Don't update the primary link again
    if ((node1 == to && node2 == from) || (node1 == from && node2 == to))
        return;

    node1 = (node1 == to ? from : (node1 == from) ? to : node1);
    node2 = (node2 == to ? from : (node2 == from) ? to : node2);

    float scale = outer * SECOND_DEGREE / MAX_CORRELATION;
    update_correlation(node1, node2, weight * scale);

    return;
}

void CorrelationDb::update_correlation(int from, int to, float weight)
{
    if (fabs(weight) < 0.25)
        return;

#if defined(DEBUG) && 0
    cerr << " >> Updating link from " << std::min(from, to) << " to "
        << std::max(from, to) << " by " << weight << endl;
#endif

    int min = std::min(from, to), max = std::max(from, to);

    try {
        Q q("INSERT INTO C.Correlations "
                "('x', 'y', 'weight') VALUES (?, ?, ?);");
        q << min << max << weight;
        q.execute();
        return;
    } IGNOREFAILURE();

    {
        Q q("UPDATE C.Correlations SET weight = "
                "max(min(weight + ?, " MAX_CORR_STR "), -" MAX_CORR_STR ") "
                "WHERE x = ? AND y = ?;");
        q << weight << min << max;
        q.execute();
    }
}

float CorrelationDb::correlate(int sid1, int sid2)
{
    if (sid1 < 0 || sid2 < 0)
        return 0;

    int min = std::min(sid1, sid2), max = std::max(sid1, sid2);
    float correlation = 0;
    
    try {
        Q q("SELECT weight FROM C.Correlations WHERE x = ? AND y = ?;");
        q << min << max;

        if (q.next())
            q >> correlation;
    }
    WARNIFFAILED();

    return correlation;
}