public void Commit(bool freeMemory) { if (_isDirty == false) { return; } using (new L(this)) { log.Debug("writing " + _FileName); int[] keys = _cache.Keys(); Array.Sort(keys); foreach (int k in keys) { WAHBitArray bmp = null; if (_cache.TryGetValue(k, out bmp) && bmp.isDirty) { SaveBitmap(k, bmp); bmp.FreeMemory(); bmp.isDirty = false; } } Flush(); if (freeMemory) { _cache = //new SafeDictionary<int, WAHBitArray>(); new SafeSortedList <int, WAHBitArray>(); log.Debug(" freeing cache"); } _isDirty = false; } }
internal MGRB(SafeSortedList <int, Container> containers) { _containers = containers; var k = _containers.Keys(); var l = k.Length - 1; if (l >= 0) { _size = (k[l] << 16) + _containers.GetValue(l).Size; } }
internal void SavePageList(SafeSortedList <T, PageInfo> _pages, List <int> diskpages) { lock (_fileLock) { T[] keys = _pages.Keys(); int blocknum = 0; // TODO : needed?? //if (_externalStrings) //{ // if (_pagelistalllocblock != null) // _strings.FreeBlocks(_pagelistalllocblock); // blocknum = _strings.SaveData("pagelist", BJSON.ToBJSON(keys, // new BJSONParameters { UseUnicodeStrings = false, UseTypedArrays = false })); // File.WriteAllBytes(_FileName + ".pagelist", Helper.GetBytes(blocknum, false)); //} // save page list int c = (_pages.Count() / Global.PageItemCount) + 1; // allocate pages needed while (c > diskpages.Count) { diskpages.Add(GetNewPageNumber()); } byte[] page = new byte[_PageLength]; for (int i = 0; i < (diskpages.Count - 1); i++) { byte[] block = CreateBlockHeader(1, Global.PageItemCount, diskpages[i + 1]); Buffer.BlockCopy(block, 0, page, 0, block.Length); for (int j = 0; j < Global.PageItemCount; j++) { CreatePageListData(_pages, i * Global.PageItemCount, block.Length, j, page, blocknum); } SeekPage(diskpages[i]); _file.Write(page, 0, page.Length); } c = _pages.Count() % Global.PageItemCount; byte[] lastblock = CreateBlockHeader(1, (ushort)c, -1); Buffer.BlockCopy(lastblock, 0, page, 0, lastblock.Length); int lastoffset = (_pages.Count() / Global.PageItemCount) * Global.PageItemCount; for (int j = 0; j < c; j++) { CreatePageListData(_pages, lastoffset, lastblock.Length, j, page, blocknum); } SeekPage(diskpages[diskpages.Count - 1]); _file.Write(page, 0, page.Length); } }
public MGRB Optimize() { lock (_lock) { var keys = _containers.Keys(); var remove = new List <int>(); for (int i = 0; i < keys.Length; i++) { var k = keys[i]; var c = _containers[k]; if (c.CountOnes() == 0) { remove.Add(k); } //else if (c.CountZeros() < Container.CHGOVER) //{ // _containers[k] = new ZeroContainer(); //} else if (c.ChangeRequired()) { _containers[k] = c.Change(); } } foreach (var k in remove) { _containers.Remove(k); } return(this); } }
internal MGRB(SafeSortedList <int, Container> containers, long size) { _containers = containers; var k = _containers.Keys(); _size = size; if (size <= 0)//== -1) { _size = 0; var l = k.Length - 1; if (l >= 0) { _size = (k[l] << 16) + _containers.GetValue(l).Size; } } }
public void SaveIndex() { //_log.Debug("Total split time (s) = " + _totalsplits); //_log.Debug("Total pages = " + _pageList.Count); int[] keys = _cache.Keys(); Array.Sort(keys); // save index to disk foreach (var i in keys) { var p = _cache[i]; if (p.isDirty) { _index.SavePage(p); p.isDirty = false; } } _index.SavePageList(_pageList, _pageListDiskPages); _index.BitmapFlush(); }
private WAHBitArray ExecutionPlan(string filter, int maxsize) { //_log.Debug("query : " + filter); DateTime dt = FastDateTime.Now; // query indexes string[] words = filter.Split(' '); //bool defaulttoand = true; //if (filter.IndexOfAny(new char[] { '+', '-' }, 0) > 0) // defaulttoand = false; WAHBitArray found = null;// WAHBitArray.Fill(maxsize); foreach (string s in words) { int c; bool not = false; string word = s; if (s == "") { continue; } OPERATION op = OPERATION.AND; //if (defaulttoand) // op = OPERATION.AND; if (word.StartsWith("+")) { op = OPERATION.OR; word = s.Replace("+", ""); } if (word.StartsWith("-")) { op = OPERATION.ANDNOT; word = s.Replace("-", ""); not = true; if (found == null) // leading with - -> "-oak hill" { found = WAHBitArray.Fill(maxsize); } } if (word.Contains("*") || word.Contains("?")) { WAHBitArray wildbits = new WAHBitArray(); // do wildcard search Regex reg = new Regex("^" + word.Replace("*", ".*").Replace("?", ".") + "$", RegexOptions.IgnoreCase); foreach (string key in _words.Keys()) { if (reg.IsMatch(key)) { _words.TryGetValue(key, out c); WAHBitArray ba = _bitmaps.GetBitmap(c); wildbits = DoBitOperation(wildbits, ba, OPERATION.OR, maxsize); } } if (found == null) { found = wildbits; } else { if (not) // "-oak -*l" { found = found.AndNot(wildbits); } else if (op == OPERATION.AND) { found = found.And(wildbits); } else { found = found.Or(wildbits); } } } else if (_words.TryGetValue(word.ToLowerInvariant(), out c)) { // bits logic WAHBitArray ba = _bitmaps.GetBitmap(c); found = DoBitOperation(found, ba, op, maxsize); } else if (op == OPERATION.AND) { found = null; } } if (found == null) { return(new WAHBitArray()); } // remove deleted docs WAHBitArray ret; if (_docMode) { ret = found.AndNot(_deleted.GetBits()); } else { ret = found; } //_log.Debug("query time (ms) = " + FastDateTime.Now.Subtract(dt).TotalMilliseconds); return(ret); }