разделение поиска на уровне клиента

This commit is contained in:
Alexey Kasyanchuk 2017-01-04 09:59:53 +03:00
parent a3eb638428
commit 6623f9db74
2 changed files with 52 additions and 20 deletions

View File

@ -149,7 +149,7 @@ io.on('connection', function(socket)
}); });
}); });
socket.on('search', function(text, callback) socket.on('searchTorrent', function(text, navigation, callback)
{ {
if(typeof callback != 'function') if(typeof callback != 'function')
return; return;
@ -159,25 +159,37 @@ io.on('connection', function(socket)
return; return;
} }
const index = navigation.index || 0;
const limit = navigation.limit || 10;
let search = {}; let search = {};
socketMysql.query('SELECT * FROM `torrents` WHERE MATCH(`name`) AGAINST(?) LIMIT ?,?', [text, index, limit], function (error, rows, fields) {
console.log(text);
let q = 2;
socketMysql.query('SELECT * FROM `torrents` WHERE MATCH(`name`) AGAINST(?) LIMIT 10', text, function (error, rows, fields) {
rows.forEach((row) => { rows.forEach((row) => {
search[row.hash] = baseRowData(row); search[row.hash] = baseRowData(row);
}); });
if(--q == 0)
callback(Object.keys(search).map(function(key) { callback(Object.keys(search).map(function(key) {
return search[key]; return search[key];
})); }));
}); });
socketMysql.query('SELECT * FROM `files` INNER JOIN torrents ON(torrents.hash = files.hash) WHERE MATCH(`path`) AGAINST(?) LIMIT 10', text, function (error, rows, fields) { });
socket.on('searchFiles', function(text, navigation, callback)
{
if(typeof callback != 'function')
return;
if(!text || text.length <= 2) {
callback(undefined);
return;
}
const index = navigation.index || 0;
const limit = navigation.limit || 10;
let search = {};
socketMysql.query('SELECT * FROM `files` INNER JOIN torrents ON(torrents.hash = files.hash) WHERE MATCH(`path`) AGAINST(?) LIMIT ?,?', [text, index, limit], function (error, rows, fields) {
rows.forEach((row) => { rows.forEach((row) => {
search[row.hash] = baseRowData(row); search[row.hash] = baseRowData(row);
search[row.hash].path = row.path; search[row.hash].path = row.path;
}); });
if(--q == 0)
callback(Object.keys(search).map(function(key) { callback(Object.keys(search).map(function(key) {
return search[key]; return search[key];
})); }));

View File

@ -18,11 +18,31 @@ export default class Search extends Component {
this.setState({ this.setState({
searchingIndicator: true searchingIndicator: true
}); });
window.torrentSocket.emit('search', this.searchValue, (torrents) => { this.searchData = [];
this.searchData = torrents; let queries = 2;
window.torrentSocket.emit('searchTorrent', this.searchValue, {limit: 10}, (torrents) => {
if(torrents) {
this.searchData = this.searchData.concat(torrents);
}
if(--queries == 0) {
this.setState({ this.setState({
searchingIndicator: false searchingIndicator: false
}); });
} else {
this.forceUpdate();
}
});
window.torrentSocket.emit('searchFiles', this.searchValue, {limit: 10}, (torrents) => {
if(torrents) {
this.searchData = this.searchData.concat(torrents);
}
if(--queries == 0) {
this.setState({
searchingIndicator: false
});
} else {
this.forceUpdate();
}
}); });
} }
componentDidMount() { componentDidMount() {
@ -65,7 +85,7 @@ export default class Search extends Component {
{ {
this.stats this.stats
? ?
<div className='fs0-75' style={{color: 'rgba(0, 0, 0, 0.541176)'}}>we have information about {this.stats.torrents} torrents and around {this.stats.files} files and { formatBytes(this.stats.size, 1) } of data</div> <div className='fs0-75 pad0-75' style={{color: 'rgba(0, 0, 0, 0.541176)'}}>we have information about {this.stats.torrents} torrents and around {this.stats.files} files and { formatBytes(this.stats.size, 1) } of data</div>
: :
null null
} }