public WAHBitArray Op(WAHBitArray bits, OPERATION op) { if (bits == null) { throw new InvalidOperationException("Bits is null"); } if (op == OPERATION.AND) { return(_bits.And(bits)); } if (op == OPERATION.OR) { return(_bits.Or(bits)); } return(bits.And(_bits.Not())); }
public WAHBitArray ExecutionPlan(string filter, bool freeCache = false) { _log(string.Format("Hoot::ExecutionPlan start freeCache is {0}", freeCache)); _log(string.Format("query : {0}", filter)); DateTime now = FastDateTime.Now; string[] words = filter.Split(' ').OrderByDescending(w => w.Length).ToArray(); WAHBitArray bits = null; var wildcardMatchers = new List <WildcardMatcher>(); var cacheToFree = new List <Tuple <string, Cache> >(); for (int i = 0; i < words.Length; i++) { string originWord = words[i]; string preparedWord = words[i]; var op = Cache.OPERATION.OR; if (originWord.StartsWith("+")) { op = Cache.OPERATION.AND; preparedWord = originWord.Replace("+", ""); } else if (originWord.StartsWith("-")) { op = Cache.OPERATION.ANDNOT; preparedWord = originWord.Replace("-", ""); } if (originWord.Contains("*") || originWord.Contains("?")) { wildcardMatchers.Add(new WildcardMatcher(originWord.ToLower())); } else { Cache c; var lowerWord = preparedWord.ToLowerInvariant(); if (_index.TryGetValue(lowerWord, out c)) { LoadCacheIfNotLoaded(c); cacheToFree.Add(Tuple.Create(lowerWord, c)); bits = DoBitOperation(bits, c, op); } else if (op == Cache.OPERATION.AND) { var cache = new Cache { isLoaded = true }; cache.SetBit(0, false); bits = DoBitOperation(bits, cache, op); } } if (i == words.Length - 1) { //asc: brutal hack - only for wildcards op = Cache.OPERATION.AND; WAHBitArray wildbits = null; var foundMatcherWords = wildcardMatchers.ToDictionary(w => w, _ => new ConcurrentQueue <string>()); Parallel.ForEach(_index.Keys, w => { foreach (var matcher in wildcardMatchers) { if (matcher.IsMatch(w)) { foundMatcherWords[matcher].Enqueue(w); break; } } }); var loadWatch = Stopwatch.StartNew(); using (var bmp = CreateBitmapStream()) { foreach (string key in foundMatcherWords.Values.SelectMany(_ => _)) { var c = _index[key]; LoadCacheIfNotLoaded(c, bmp); cacheToFree.Add(Tuple.Create(key, c)); } } _log(string.Format("Hoot wildcard load operation: {0} ms", loadWatch.Elapsed.TotalMilliseconds)); var bitWatch = Stopwatch.StartNew(); var matcherPlans = foundMatcherWords.Select(p => { if (p.Value.Count == 0) { var falsePlan = new WAHBitArray(); falsePlan.Set(0, false); return(falsePlan); } WAHBitArray matcherPlan = null; foreach (string word in p.Value) { matcherPlan = DoBitOperation(matcherPlan, _index[word], Cache.OPERATION.OR); } return(matcherPlan); }).Where(p => p != null).ToList(); wildbits = matcherPlans.Aggregate(wildbits, (acc, matcherPlan) => acc != null ? acc.And(matcherPlan) : matcherPlan); _log(string.Format("Hoot wildcard bit operation: {0} ms", bitWatch.Elapsed.TotalMilliseconds)); if (wildbits != null) { bits = bits == null ? wildbits : (op == Cache.OPERATION.AND ? bits.And(wildbits) : bits.Or(wildbits)); } } } if (bits == null) { return(new WAHBitArray()); } // remove deleted docs if (bits.Length > _deleted.Length) { _deleted.Length = bits.Length; } else if (bits.Length < _deleted.Length) { bits.Length = _deleted.Length; } WAHBitArray result = bits.And(_deleted.Not()); _log(string.Format("Hoot::ExecutionPlan freeCache is {0}", freeCache)); if (freeCache) { foreach (var c in cacheToFree) { _log(string.Format("Free cache from ExecutionPlan::ExecutionPlan for {0}", c.Item1)); c.Item2.FreeMemory(unload: true, freeUncompressedMemory: false); } //asc: clean cache buckets cache _hash.Commit(); } _log(string.Format("query time (ms) = {0}", FastDateTime.Now.Subtract(now).TotalMilliseconds)); return(result); }
public WAHBitArray Op(WAHBitArray bits, OPERATION op) { if (bits == null) { throw new InvalidOperationException("Bits is null"); } if (op == OPERATION.AND) return _bits.And(bits); if (op == OPERATION.OR) return _bits.Or(bits); return bits.And(_bits.Not()); }
public WAHBitArray And(WAHBitArray op) { CheckBitArray(op); return op.And(_ba); }
public WAHBitArray And(WAHBitArray op) { CheckBitArray(op); return(op.And(_ba)); }