public void Add(TKey key, ref T value) { index.Add(key, appendOffset); var ptr = accessor.GetPtr(appendOffset, true); Unsafe.Write(ptr, value); appendOffset += valueSize; }
private Array GetKeyArray(MemoryAccessor ma, long index, int count, Type elType) { var arr = Array.CreateInstance(elType, count); Unsafe.CopyBlock(Unsafe.AsPointer(ref arr), ma.GetPtr(GetAbsoluteOffset(index) + keyOffset), (uint)(count * Marshal.SizeOf(elType))); return(arr); }
private void StartSort(long ps) { logger.LogInfo("Copying temp file..."); File.Copy(path, path + ".tmp"); logger.LogInfo("Resize temp file..."); var newFilesSize = (long)index.Count * valueSize + offset; logger.LogInfo($"New file size: {newFilesSize}"); using (var f = File.Open(path + ".tmp", FileMode.OpenOrCreate, FileAccess.ReadWrite)) f.SetLength(newFilesSize); logger.LogInfo("Sorting..."); var tmpAccessor = new MemoryAccessor(path + ".tmp", 0, ps); var sortedIndex = new SortedDictionary <TKey, long>(index); var newIndex = new Dictionary <TKey, long>(); var off = offset; foreach (var kv in sortedIndex) { Unsafe.Write(tmpAccessor.GetPtr(off), accessor.Get <T>(kv.Value)); newIndex.Add(kv.Key, off); off += valueSize; } accessor.Dispose(); tmpAccessor.Dispose(); logger.LogInfo("Replace files..."); File.Delete(path); File.Move(path + ".tmp", path); logger.LogInfo("Reload database..."); accessor = new MemoryAccessor(path, 0, pageSize); fileSize = new FileInfo(path).Length; index = newIndex; }
private string GetKeyString(MemoryAccessor ma, long index, int count) { var sp = new Span <char>(ma.GetPtr(GetAbsoluteOffset(index) + keyOffset), count); return(sp.Slice(0, sp.IndexOf('\0') < 0 ? count : sp.IndexOf('\0')).ToString()); }