Browse Source

refactored some query parsing code

pull/2/head
asiekierka 6 years ago
parent
commit
dfabbf4bee
2 changed files with 25 additions and 17 deletions
  1. 6
    17
      app.js
  2. 19
    0
      queryparser.js

+ 6
- 17
app.js View File

@@ -13,7 +13,7 @@ var express = require('express')
13 13
   , bar = require('progress-bar')
14 14
   , async = require('async')
15 15
   , redis = require('redis')
16
-  , queryparser = require('./queryparser.js').QueryParser
16
+  , queryParser = require('./queryparser.js').QueryParser
17 17
   , imageHandler = require('./image.js')
18 18
   , tempurl = require('./tempurl.js')
19 19
   , util = require('./util.js');
@@ -160,23 +160,12 @@ function handleSearch(req, res, query) {
160 160
           } else if(entry.type == "string") {
161 161
             imageDB.imagesBy(entry.key,entry.value,function(images) { stack.push(handleQuery(entry,images,allImages)); next(); });
162 162
           } else if(entry.type == "numeric") {
163
-            var pmin = 0, pmax = 2147483647, pin = parseInt(entry.value);
164
-            if(entry.sign == "eq") { pmin = pin; pmax = pin; }
165
-            else if(entry.sign == "lt") { pmax = pin-1; } else if(entry.sign == "le") { pmax = pin; }
166
-            else if(entry.sign == "gt") { pmin = pin+1; } else if(entry.sign == "ge") { pmin = pin; }
167
-            imageDB.imagesByNum(entry.key,pmin,pmax,function(images) { stack.push(images); next(); });
163
+            var p = queryParser.handleNumeric(entry.sign, parseInt(entry.value));
164
+            imageDB.imagesByNum(entry.key,p.min,p.max,function(images) { stack.push(images); next(); });
168 165
           } else if(entry.type == "linker" && stack.length >= 2) {
169 166
             var p1 = stack.pop()
170 167
               , p2 = stack.pop();
171
-            if(entry.link == "or") stack.push(_.union(p1,p2));
172
-            else if(entry.link == "and") stack.push(_.intersection(p1,p2));
173
-            else if(entry.link == "xor") {
174
-              var p3 = [];
175
-              _.each(_.union(p1,p2),function(val) {
176
-                if(_.contains(p1,val) != _.contains(p2,val)) p3.push(val);
177
-              });
178
-              stack.push(p3);
179
-            }
168
+            stack.push(queryParser.handleLinker(entry.link, p1, p2));
180 169
             next();
181 170
           } else next();
182 171
         }, function() {
@@ -196,12 +185,12 @@ function handleSearch(req, res, query) {
196 185
   });
197 186
 }
198 187
 app.post("/search",express.bodyParser(), function(req,res) {
199
-  var query = queryparser.parse(req.body.query);
188
+  var query = queryParser.parse(req.body.query);
200 189
   console.log(query);
201 190
   handleSearch(req, res, query);
202 191
 });
203 192
 app.get("/search",express.bodyParser(), function(req,res) {
204
-  var query = queryparser.parse(req.query["q"] || req.query["query"]);
193
+  var query = queryParser.parse(req.query["q"] || req.query["query"]);
205 194
   console.log(query);
206 195
   handleSearch(req, res, query);
207 196
 });

+ 19
- 0
queryparser.js View File

@@ -31,6 +31,25 @@ QueryParser.check = function(valueT,sign,value) {
31 31
   return false;
32 32
 }
33 33
 
34
+QueryParser.handleNumeric = function(sign, pin) {
35
+  var pmin = 0, pmax = 2147483647;
36
+  if(sign == "eq") { pmin = pin; pmax = pin; }
37
+  else if(sign == "lt") { pmax = pin-1; } else if(sign == "le") { pmax = pin; }
38
+  else if(sign == "gt") { pmin = pin+1; } else if(sign == "ge") { pmin = pin; }
39
+  return {"min": pmin, "max": pmax};
40
+}
41
+QueryParser.handleLinker = function(link, p1, p2) {
42
+  if(link == "or") return _.union(p1,p2);
43
+  else if(link == "and") return _.intersection(p1,p2);
44
+  else if(link == "xor") {
45
+    var p3 = [];
46
+    _.each(_.union(p1,p2),function(val) {
47
+      if(_.contains(p1,val) != _.contains(p2,val)) p3.push(val);
48
+    });
49
+    return p3;
50
+  }
51
+  else return [];
52
+}
34 53
 // INTERNAL OPTIMIZATION STUFF
35 54
 var lKeys = _(linkers).keys()
36 55
   , lValues = _(linkers).values();

Loading…
Cancel
Save