Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: cebf453b82d92772f2a20824f5b39cecdd139809 (plain)
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
/**
 * Delta data access
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 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 Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Get deltas from the mongo document in order to process and publish them
 */

import { DocumentId } from "../../document/Document";
import { PositiveInteger } from "../../numeric";
import { MongoCollection } from "mongodb";
import { DeltaDao } from "./DeltaDao";


export type MongoDeltaType = 'ratedata' | 'data';


/** Manage deltas */
export class MongoDeltaDao implements DeltaDao
{
    /** The ratedata delta type */
    static readonly DELTA_RATEDATA: string = 'ratedata';

    /** The data delta type */
    static readonly DELTA_DATA: string = 'data';


    /**
     * Initialize connection
     *
     * @param _collection Mongo collection
     */
    constructor(
        private readonly _collection: MongoCollection,
    ) {}


    /**
     * Get documents in need of processing
     *
     * @return documents in need of processing
     */
    getUnprocessedDocuments(
        callback: ( data: Record<string, any>[] ) => void,
    ): this
    {
        var self = this;

        this._collection.find(
            { published: false },
            {},
            function( _err, cursor )
            {
                cursor.toArray( function( _err: NullableError, data: any[] )
                {
                    // was the quote found?
                    if ( data.length == 0 )
                    {
                        callback.call( self, [] );

                        return;
                    }

                    // return the quote data
                    callback.call( self, data );
                });
            }
        )

        return this;
    }


    /**
     * Set the document's processed index
     *
     * @param doc_id   - Document whose index will be set
     * @param type     - Delta type
     * @param index    - Index to set
     * @param callback - Callback function
     */
    advanceDeltaIndexByType(
        doc_id:   DocumentId,
        type:     MongoDeltaType,
        index:    PositiveInteger,
        callback: ( err: NullableError, indexAdvanced: boolean ) => void,
    ): this
    {
        var self = this;

        const set_data: Record<string, any> = {};

        set_data[ 'lastPublishDelta.' + type ] = index;

        this._collection.update(
            { id: doc_id },
            { $set: set_data },
            { upsert: true },
            function( err )
            {
                if ( err )
                {
                    callback.call( self, err, false );

                    return;
                }

                callback.call( self, null, true );

                return;
            }
        );

        return this;
    }


    /**
     * Mark a given document as processed. First does a check to make sure that
     * the document does not have a newer update timestamp than the provided one
     *
     * @param doc_id         - The document to mark
     * @param last_update_ts - The last time this document was updated
     *
     * @return true if the document was successfully marked as processed
     */
    markDocumentAsProcessed(
        doc_id:          DocumentId,
        last_update_ts:  UnixTimestamp,
        callback:        ( err: NullableError, indexAdvanced: boolean ) => void,
    ): this
    {
        var self = this;

        this._collection.update(
            { id: doc_id, lastUpdate: { $gt: last_update_ts } },
            { $set: { processed: true } },
            { upsert: false },
            function( err, result )
            {
                if ( err )
                {
                    callback.call( self, err, false );

                    return;
                }

                console.log( '-------', result );

                callback.call( self, null, true );

                return;
            }
        );

        return this;
    }
}