advanced search (key/bpm/...)
man i hope sqlite is good at opimizing
This commit is contained in:
		
							parent
							
								
									21e1cd87ca
								
							
						
					
					
						commit
						4885653c07
					
				| @ -1,10 +1,11 @@ | |||||||
| # coding: utf-8 | # coding: utf-8 | ||||||
| from __future__ import print_function, unicode_literals | from __future__ import print_function, unicode_literals | ||||||
| 
 | 
 | ||||||
|  | import re | ||||||
| import os | import os | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
| 
 | 
 | ||||||
| from .util import u8safe | from .util import u8safe, html_escape, Pebkac | ||||||
| from .up2k import up2k_wark_from_hashlist | from .up2k import up2k_wark_from_hashlist | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -73,17 +74,47 @@ class U2idx(object): | |||||||
| 
 | 
 | ||||||
|         uq, uv = _sqlize(qobj) |         uq, uv = _sqlize(qobj) | ||||||
| 
 | 
 | ||||||
|         tq = "" |  | ||||||
|         tv = [] |  | ||||||
|         qobj = {} |         qobj = {} | ||||||
|         if "tags" in body: |         if "tags" in body: | ||||||
|             _conv_txt(qobj, body, "tags", "mt.v") |             _conv_txt(qobj, body, "tags", "mt.v") | ||||||
|             tq, tv = _sqlize(qobj) |  | ||||||
| 
 | 
 | ||||||
|         return self.run_query(vols, uq, uv, tq, tv) |         if "adv" in body: | ||||||
|  |             _conv_adv(qobj, body, "adv") | ||||||
| 
 | 
 | ||||||
|     def run_query(self, vols, uq, uv, tq, tv): |         return self.run_query(vols, uq, uv, qobj) | ||||||
|         self.log("qs: {} {} ,  {} {}".format(uq, repr(uv), tq, repr(tv))) | 
 | ||||||
|  |     def run_query(self, vols, uq, uv, targs): | ||||||
|  |         self.log("qs: {} {} ,  {}".format(uq, repr(uv), repr(targs))) | ||||||
|  | 
 | ||||||
|  |         if not targs: | ||||||
|  |             if not uq: | ||||||
|  |                 q = "select * from up" | ||||||
|  |                 v = () | ||||||
|  |             else: | ||||||
|  |                 q = "select * from up where " + uq | ||||||
|  |                 v = tuple(uv) | ||||||
|  |         else: | ||||||
|  |             q = "select up.* from up" | ||||||
|  |             keycmp = "substr(up.w,1,16)" | ||||||
|  |             where = [] | ||||||
|  |             v = [] | ||||||
|  |             ctr = 0 | ||||||
|  |             for tq, tv in sorted(targs.items()): | ||||||
|  |                 ctr += 1 | ||||||
|  |                 tq = tq.split("\n")[0] | ||||||
|  |                 keycmp2 = "mt{}.w".format(ctr) | ||||||
|  |                 q += " inner join mt mt{} on {} = {}".format(ctr, keycmp, keycmp2) | ||||||
|  |                 keycmp = keycmp2 | ||||||
|  |                 where.append(tq.replace("mt.", keycmp[:-1])) | ||||||
|  |                 v.append(tv) | ||||||
|  | 
 | ||||||
|  |             if uq: | ||||||
|  |                 where.append(uq) | ||||||
|  |                 v.extend(uv) | ||||||
|  | 
 | ||||||
|  |             q += " where " + (" and ".join(where)) | ||||||
|  | 
 | ||||||
|  |         self.log("q2: {} {}".format(q, repr(v))) | ||||||
| 
 | 
 | ||||||
|         ret = [] |         ret = [] | ||||||
|         lim = 1000 |         lim = 1000 | ||||||
| @ -93,19 +124,6 @@ class U2idx(object): | |||||||
|             if not cur: |             if not cur: | ||||||
|                 continue |                 continue | ||||||
| 
 | 
 | ||||||
|             if not tq: |  | ||||||
|                 if not uq: |  | ||||||
|                     q = "select * from up" |  | ||||||
|                     v = () |  | ||||||
|                 else: |  | ||||||
|                     q = "select * from up where " + uq |  | ||||||
|                     v = tuple(uv) |  | ||||||
|             else: |  | ||||||
|                 # naive assumption: tags first |  | ||||||
|                 q = "select up.* from up inner join mt on substr(up.w,1,16) = mt.w where {}" |  | ||||||
|                 q = q.format(" and ".join([tq, uq]) if uq else tq) |  | ||||||
|                 v = tuple(tv + uv) |  | ||||||
| 
 |  | ||||||
|             sret = [] |             sret = [] | ||||||
|             c = cur.execute(q, v) |             c = cur.execute(q, v) | ||||||
|             for hit in c: |             for hit in c: | ||||||
| @ -190,6 +208,23 @@ def _conv_txt(q, body, k, sql): | |||||||
|         q[qk + "\n" + v] = u8safe(v) |         q[qk + "\n" + v] = u8safe(v) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def _conv_adv(q, body, k): | ||||||
|  |     ptn = re.compile(r"^(\.?[a-z]+) *(==?|!=|<=?|>=?) *(.*)$") | ||||||
|  | 
 | ||||||
|  |     parts = body[k].split(" ") | ||||||
|  |     parts = [x.strip() for x in parts if x.strip()] | ||||||
|  | 
 | ||||||
|  |     for part in parts: | ||||||
|  |         m = ptn.match(part) | ||||||
|  |         if not m: | ||||||
|  |             p = html_escape(part) | ||||||
|  |             raise Pebkac(400, "invalid argument [" + p + "]") | ||||||
|  | 
 | ||||||
|  |         k, op, v = m.groups() | ||||||
|  |         qk = "mt.k = '{}' and mt.v {} ?".format(k, op) | ||||||
|  |         q[qk + "\n" + v] = u8safe(v) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def _sqlize(qobj): | def _sqlize(qobj): | ||||||
|     keys = [] |     keys = [] | ||||||
|     values = [] |     values = [] | ||||||
|  | |||||||
| @ -422,6 +422,7 @@ input[type="checkbox"]:checked+label { | |||||||
| } | } | ||||||
| #srch_q { | #srch_q { | ||||||
| 	white-space: pre; | 	white-space: pre; | ||||||
|  | 	color: #f80; | ||||||
| } | } | ||||||
| #files td div span { | #files td div span { | ||||||
| 	color: #fff; | 	color: #fff; | ||||||
|  | |||||||
| @ -588,10 +588,14 @@ function autoplay_blocked() { | |||||||
| 	]; | 	]; | ||||||
| 	var oldcfg = []; | 	var oldcfg = []; | ||||||
| 
 | 
 | ||||||
| 	if (document.querySelector('#srch_form.tags')) | 	if (document.querySelector('#srch_form.tags')) { | ||||||
| 		sconf.push(["tags", | 		sconf.push(["tags", | ||||||
| 			["tags", "tags", "tags contains   (^=start, end=$)", "46"] | 			["tags", "tags", "tags contains   (^=start, end=$)", "46"] | ||||||
| 		]); | 		]); | ||||||
|  | 		sconf.push(["adv.", | ||||||
|  | 			["adv", "adv", "key=5A  .bpm>159  .bpm<169", "46"] | ||||||
|  | 		]); | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	var html = []; | 	var html = []; | ||||||
| 	var orig_html = null; | 	var orig_html = null; | ||||||
| @ -654,9 +658,10 @@ function autoplay_blocked() { | |||||||
| 			return; | 			return; | ||||||
| 
 | 
 | ||||||
| 		if (this.status !== 200) { | 		if (this.status !== 200) { | ||||||
| 			alert("http " + this.status + ": " + this.responseText); | 			ebi('srch_q').textContent = "http " + this.status + ": " + this.responseText; | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
|  | 		ebi('srch_q').textContent = ''; | ||||||
| 
 | 
 | ||||||
| 		var res = JSON.parse(this.responseText), | 		var res = JSON.parse(this.responseText), | ||||||
| 			tagord = res.tag_order; | 			tagord = res.tag_order; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 ed
						ed