-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAL.js
173 lines (143 loc) · 3.48 KB
/
DAL.js
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
module.exports = function(mongoose, url){
var module = {};
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// test();
});
module.process = function(word, docName, text)
{
try{
var query = {'word': word};
//getting array of words, to count times of word occurence in document.
var matches = text.match(new RegExp(word, "g"));
var count;
if(matches)
count = matches.length;
else
count = 1;
// debugger;
Word.findOne(query, function(error, result){
if(result)
{
console.log("RESULt");
console.log(result);
}
if(error)
{
console.err('error finding word in DB');
return 0;
}
if(!result)
{
if(word.length < 30)
addWord(word, docName, count);
}
else{
if(word.length < 30)
editEntry(word, docName, count, result);
}
});
}
catch(err){
debugger;
}
}
module.processDocument = function(url, length)
{
var query = {name: url};
Document.findOne(query, function(error, result){
if(!result && !error)
{
addDocument(url, length);
}
});
//YOU WERE HERE... FUCKING STORE THE DOCUMENT AND ITS LENGTH!!!!!
//CHANGE THE SCHEMA
}
function addDocument(url, length){
var newDoc = new Document({
name: url,
length: length
})
newDoc.save(function(err){
if(!err){
console.log("-------saved " + url + " document to DB!!!!------");
}
});
}
function addWord(word, docName, count)
{
var newWord = new Word({
word: word,
containingDocuments: [{
docName: docName,
count: count
}]
})
newWord.save(function(err)
{
if(err)
{
//todo error handling?
// debugger;
}
else{
console.log('added ' + word + " to DB from " + docName);
// debugger;
}
});
}
function editEntry(word, docName, count, result)
{
//grabbing the ID off the result from before
var query = {_id: result._id};
//creating object that will replace old object in the DB
var newObj = result.toObject();
//stripping the ID off the old one to avoid any collisions,
//found this from http://stackoverflow.com/questions/25831859/cannot-read-property-id-of-undefined-while-using-mongoose-findoneandupdate
delete newObj._id;
var hasVisitedDoc = false;
//can't think of better way to search right now... sadly
for(var i = 0; i < result.containingDocuments.length; i++)
{
if(result.containingDocuments[i].docName === docName){
hasVisitedDoc = true;
break;
}
}
if(!hasVisitedDoc)
{
//add current document to index object
newObj.containingDocuments[newObj.containingDocuments.length] = {
docName: docName,
count: count
};
//overwrite old one!
Word.findOneAndUpdate(query, newObj, function(err, res){
if(err)
{
// error handling?
}
});
}
else{
//do nothing
//here is where i'm going to
console.log('duplicate... do nothing');
}
}
//we have to define the schema of the data to store it with mongoose
var wordSchema = mongoose.Schema({
word: {type: String, unique: true},
containingDocuments: { type: Array, "default": [{}]}
});
var documentSchema = mongoose.Schema({
name: {type: String, unique: true},
length: Number
});
var Word = mongoose.model("Word", wordSchema);
var Document = mongoose.model("Document", documentSchema);
return module;
}