refactor: moved server content to /server
This commit is contained in:
		
							
								
								
									
										81
									
								
								server/libs/search-index/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								server/libs/search-index/index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,81 @@ | ||||
| const bunyan = require('bunyan') | ||||
| const level = require('levelup') | ||||
| const down = require('memdown') | ||||
| const SearchIndexAdder = require('search-index-adder') | ||||
| const SearchIndexSearcher = require('search-index-searcher') | ||||
|  | ||||
| module.exports = function (givenOptions, moduleReady) { | ||||
|   const optionsLoaded = function (err, SearchIndex) { | ||||
|     const siUtil = require('./siUtil.js')(SearchIndex.options) | ||||
|     if (err) return moduleReady(err) | ||||
|     SearchIndex.close = siUtil.close | ||||
|     SearchIndex.countDocs = siUtil.countDocs | ||||
|     getAdder(SearchIndex, adderLoaded) | ||||
|   } | ||||
|  | ||||
|   const adderLoaded = function (err, SearchIndex) { | ||||
|     if (err) return moduleReady(err) | ||||
|     getSearcher(SearchIndex, searcherLoaded) | ||||
|   } | ||||
|  | ||||
|   const searcherLoaded = function (err, SearchIndex) { | ||||
|     if (err) return moduleReady(err) | ||||
|     return moduleReady(err, SearchIndex) | ||||
|   } | ||||
|  | ||||
|   getOptions(givenOptions, optionsLoaded) | ||||
| } | ||||
|  | ||||
| const getAdder = function (SearchIndex, done) { | ||||
|   SearchIndexAdder(SearchIndex.options, function (err, searchIndexAdder) { | ||||
|     SearchIndex.add = searchIndexAdder.add | ||||
|     SearchIndex.callbackyAdd = searchIndexAdder.concurrentAdd  // deprecated | ||||
|     SearchIndex.concurrentAdd = searchIndexAdder.concurrentAdd | ||||
|     SearchIndex.createWriteStream = searchIndexAdder.createWriteStream | ||||
|     SearchIndex.dbWriteStream = searchIndexAdder.dbWriteStream | ||||
|     SearchIndex.defaultPipeline = searchIndexAdder.defaultPipeline | ||||
|     SearchIndex.del = searchIndexAdder.deleter | ||||
|     SearchIndex.deleteStream = searchIndexAdder.deleteStream | ||||
|     SearchIndex.flush = searchIndexAdder.flush | ||||
|     done(err, SearchIndex) | ||||
|   }) | ||||
| } | ||||
|  | ||||
| const getSearcher = function (SearchIndex, done) { | ||||
|   SearchIndexSearcher(SearchIndex.options, function (err, searchIndexSearcher) { | ||||
|     SearchIndex.availableFields = searchIndexSearcher.availableFields | ||||
|     SearchIndex.buckets = searchIndexSearcher.bucketStream | ||||
|     SearchIndex.categorize = searchIndexSearcher.categoryStream | ||||
|     SearchIndex.dbReadStream = searchIndexSearcher.dbReadStream | ||||
|     SearchIndex.get = searchIndexSearcher.get | ||||
|     SearchIndex.match = searchIndexSearcher.match | ||||
|     SearchIndex.scan = searchIndexSearcher.scan | ||||
|     SearchIndex.search = searchIndexSearcher.search | ||||
|     SearchIndex.totalHits = searchIndexSearcher.totalHits | ||||
|     done(err, SearchIndex) | ||||
|   }) | ||||
| } | ||||
|  | ||||
| const getOptions = function (options, done) { | ||||
|   var SearchIndex = {} | ||||
|   SearchIndex.options = Object.assign({}, { | ||||
|     indexPath: 'si', | ||||
|     keySeparator: '○', | ||||
|     logLevel: 'error' | ||||
|   }, options) | ||||
|   options.log = bunyan.createLogger({ | ||||
|     name: 'search-index', | ||||
|     level: options.logLevel | ||||
|   }) | ||||
|   if (!options.indexes) { | ||||
|     level(SearchIndex.options.indexPath || 'si', { | ||||
|       valueEncoding: 'json', | ||||
|       db: down | ||||
|     }, function (err, db) { | ||||
|       SearchIndex.options.indexes = db | ||||
|       return done(err, SearchIndex) | ||||
|     }) | ||||
|   } else { | ||||
|     return done(null, SearchIndex) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										36
									
								
								server/libs/search-index/siUtil.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								server/libs/search-index/siUtil.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| 'use strict' | ||||
|  | ||||
| module.exports = function (siOptions) { | ||||
|   var siUtil = {} | ||||
|  | ||||
|   siUtil.countDocs = function (callback) { | ||||
|     var count = 0 | ||||
|     const gte = 'DOCUMENT' + siOptions.keySeparator | ||||
|     const lte = 'DOCUMENT' + siOptions.keySeparator + siOptions.keySeparator | ||||
|     siOptions.indexes.createReadStream({gte: gte, lte: lte}) | ||||
|       .on('data', function (data) { | ||||
|         count++ | ||||
|       }) | ||||
|       .on('error', function (err) { | ||||
|         return callback(err, null) | ||||
|       }) | ||||
|       .on('end', function () { | ||||
|         return callback(null, count) | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|   siUtil.close = function (callback) { | ||||
|     siOptions.indexes.close(function (err) { | ||||
|       while (!siOptions.indexes.isClosed()) { | ||||
|         // log not always working here- investigate | ||||
|         if (siOptions.log) siOptions.log.info('closing...') | ||||
|       } | ||||
|       if (siOptions.indexes.isClosed()) { | ||||
|         if (siOptions.log) siOptions.log.info('closed...') | ||||
|         callback(err) | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
|  | ||||
|   return siUtil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user