-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolr.js
6 lines (6 loc) · 3.18 KB
/
solr.js
1
2
3
4
5
6
var MSolrConst={CONFIG_COLLECTION_NAME:"msolr",SOLR_URL_KEY:"url",NS_KEY:"ns",COLL_FIELD_KEY:"f",LAST_TIMESTAMP_KEY:"ts"};var MSolrUtil={getLastError:function(a,b,c){a=a.runCommand({getlasterror:1,w:b,wtimeout:c});a.err!=null&&print("Failed to apply changes to all replicas: "+a.err)}};var MSolrServer=function(a,b,c){c=c||{};this.repCount=c.repCount||1;this.timeout=c.timeout||1E3;this.configColl=a;this.configDB=a.getDB();this.loc=b};MSolrServer.prototype.index=function(a,b){a.indexOf(".")==-1?this._indexByDB(a):this._indexByNS(a,b)};MSolrServer.prototype.remove=function(a){a.indexOf(".")==-1?this._removeByDB(a):this._removeByNS(a)};
MSolrServer.prototype._indexByDB=function(a){for(var b=this.configColl.getMongo().getDB(a).getCollectionNames(),c,d=b.length;d--;)c=b[d],/^system\..*/.test(c)||this._indexByNS(a+"."+b[d],null);MSolrUtil.getLastError(this.configDB,this.repCount,this.timeout)};MSolrServer.prototype._removeByDB=function(a){var b={};b[MSolrConst.SOLR_URL_KEY]=this.loc;b[MSolrConst.NS_KEY]=RegExp("^"+a+"\\..+");this.configColl.remove(b);MSolrUtil.getLastError(this.configDB,this.repCount,this.timeout)};
MSolrServer.prototype._indexByNS=function(a,b){var c={},d={};d[MSolrConst.SOLR_URL_KEY]=this.loc;d[MSolrConst.NS_KEY]=a;b!=null&&(c[MSolrConst.COLL_FIELD_KEY]=b);this.configColl.update(d,{$set:c},!0);MSolrUtil.getLastError(this.configDB,this.repCount,this.timeout)};MSolrServer.prototype._removeByNS=function(a){var b={};b[MSolrConst.SOLR_URL_KEY]=this.loc;b[MSolrConst.NS_KEY]=a;this.configColl.remove(b);MSolrUtil.getLastError(this.configDB,this.repCount,this.timeout)};
MSolrServer.prototype.toString=function(){return this.loc};MSolrServer.prototype.tojson=function(){return this.toString()};var MSolr=function(a,b){var c=db.getMongo(),d=a||MSolr.getConfigDBName(c),h=b||MSolrConst.CONFIG_COLLECTION_NAME,e={},f=rs.status(),g=0;this.db=c.getDB(d);this.coll=this.db.getCollection(h);e[MSolrConst.SOLR_URL_KEY]=1;e[MSolrConst.NS_KEY]=1;if(f.errmsg==null){c=f.members;for(d=c.length;d--;)(c[d].state==1||c[d].state==2)&&g++;this.repCount=g}else this.repCount=1;this.coll.ensureIndex(e,{unique:!0})};MSolr._wtimeout=2E3;MSolr.getConfigDBName=function(){return"config"};MSolr.prototype.showConfig=function(){return this.coll.find()};
MSolr.prototype.changeUrl=function(a,b){var c={},d={};c[MSolrConst.SOLR_URL_KEY]=a;d[MSolrConst.SOLR_URL_KEY]=b;this.coll.update(c,{$set:d});MSolrUtil.getLastError(this.db,this.repCount,MSolr._wtimeout)};MSolr.prototype.removeServer=function(a){var b={};b[MSolrConst.SOLR_URL_KEY]=a;this.coll.remove(b);MSolrUtil.getLastError(this.db,this.repCount,MSolr._wtimeout)};MSolr.prototype.server=function(a){return new MSolrServer(this.coll,a,{repCount:this.repCount,timeout:MSolr._wtimeout})};
MSolr.prototype.index=function(a,b,c,d){c==null?this.server(a).index(b):this.server(a).index(b+"."+c,d)};MSolr.prototype.toString=function(){return"config_collection: "+this.coll.getFullName()};MSolr.prototype.tojson=function(){return this.toString()};MSolr.prototype._getConfigColl=function(){return this.coll};MSolr.prototype.reset=function(){return this.coll.remove()};MSolr.DEFAULT=new MSolr;MSolr.showConfig=function(){return MSolr.DEFAULT.showConfig()};MSolr.reset=function(){return MSolr.DEFAULT.reset()};