/// <summary> Construct from a read-only file. </summary> /// <remarks> /// Used when recovering files written by a previous program /// instance from their locations. /// </remarks> public BlockFile(IFileMemory file, uint fileId) { _pinner = new Pinner(); _file = file ?? throw new ArgumentNullException(nameof(file)); FileId = fileId; _flags = new AppendList <ReadFlag>(); var offset = 0L; while (offset < _file.Length) { var block = AtOffset(offset); ref var header = ref block.Header; if (header.Rank != _flags.Count || header.ContentLength < 0 || header.ContentLength + BlockHeader.Size > _file.Length) { // The header of the block appears broken. Do not include // it in the list, and stop scanning (since we don't know // how far to jump ahead). break; } var thisOffset = offset; _flags.Append(ReadFlag.Triggered(() => VerifyAtOffset(thisOffset))); offset += block.RelativeOffsetToNextBlock; }
public async Task smoke_test() { // Append and read repeatedly, trying to cause an // access violation. for (var c = 0; c < 1000; ++c) { var l = new AppendList <int>(); var t1 = Task.Run(() => { for (var i = 0; i < 100000; ++i) { l.Append(i * 3); } }); var t2 = Task.Run(() => { for (var i = 0; i < 100000; ++i) { while (i >= l.Count) { Thread.Yield(); } Assert.Equal(i * 3, l[i]); } }); await t1; await t2; } }
public void one() { var l = new AppendList <int>(); l.Append(10); Assert.Equal(1, l.Count); Assert.Equal(10, l[0]); }
/// <summary> Construct from raw building blocks. </summary> /// <remarks> /// Used to create a read-only view over a file that is being written to. /// Note that <see cref="_flags"/> may grow over time, as new blocks are /// appended. /// </remarks> internal BlockFile( IFileMemory file, AppendList <ReadFlag> flags, uint fileId) { _file = file ?? throw new ArgumentNullException(nameof(file)); _flags = flags ?? throw new ArgumentNullException(nameof(flags)); FileId = fileId; }
public void two() { var l = new AppendList <int>(); l.Append(10); l.Append(42); Assert.Equal(2, l.Count); Assert.Equal(10, l[0]); Assert.Equal(42, l[1]); }
private void StoreIndexToFile() { try { lock (this) { if (_DBProvider.DelProvider != null) { _DBProvider.DelProvider.IncDeleteStamp(); } } if (_WordTableWriter == null) { return; } int wordIndexWriterCount = _WordTableWriter.Count; if (wordIndexWriterCount <= 0) { return; } Array.Sort(_WordIndexWriterPool, 0, wordIndexWriterCount); for (int index = 0; index < wordIndexWriterCount; index++) { IEnumerable <DocumentPositionList> docList = _WordIndexWriterPool[index].GetDocListForWriter(); if (docList != null) { _IndexFileProxy.AddWordPositionAndDocumentPositionList( _WordIndexWriterPool[index].Word, _WordIndexWriterPool[index].GetFirstDocList(), _WordIndexWriterPool[index].Count, docList); } } _WordTableWriter = null; _WordIndexWriterPool = null; _IndexWriterPoolId = 0; _TempWordIndexWriter = null; _DocPositionAlloc = null; } catch (Exception e) { _IndexFileProxy.CloseIndexWriter(); throw e; } _IndexFileProxy.Collect(); _IndexMerge.Optimize(OptimizationOption.Speedy); }
public bool Contains(object value) { IReadOnlyList <object> mainList = MainList; bool masterContains; var masterList = mainList as IList; if (masterList != null) { masterContains = masterList.Contains(value); } else { masterContains = mainList.Contains(value); } return(masterContains || PrependList.Contains(value) || AppendList.Contains(value)); }
public int IndexOf(object value) { int result; result = PrependList.IndexOf(value); if (result >= 0) { return(result); } result = MainList.IndexOf(value); if (result >= 0) { return(result + PrependList.Count); } result = AppendList.IndexOf(value); if (result >= 0) { return(result + PrependList.Count + MainList.Count); } return(-1); }
public void empty() { var l = new AppendList <int>(); Assert.Equal(0, l.Count); }
/// <summary> /// Index a text for one field /// </summary> /// <param name="text">text</param> /// <param name="documentId">document id</param> /// <param name="analyzer">analyzer</param> private void Index(string text, int documentId, Analysis.IAnalyzer analyzer) { lock (this) { if (_WordTableWriter == null) { _WordTableWriter = new Dictionary <string, int>(65536); } if (_DocPositionAlloc == null) { _DocPositionAlloc = new DocumentPositionAlloc(); } _DocumentCount++; if (_TempWordIndexWriter == null) { _TempWordIndexWriter = new AppendList <int>(65536); } _TempWordIndexWriter.Clear(); foreach (Entity.WordInfo wordInfo in analyzer.Tokenize(text)) { if (wordInfo.Position < 0) { continue; } string internedWord = string.IsInterned(wordInfo.Word); if (internedWord == null) { internedWord = wordInfo.Word; } int index; if (!_WordTableWriter.TryGetValue(internedWord, out index)) { if (_WordIndexWriterPool == null) { _WordIndexWriterPool = new WordIndexWriter[65536]; } if (_IndexWriterPoolId >= _WordIndexWriterPool.Length) { int nextLength = _WordIndexWriterPool.Length * 2; WordIndexWriter[] tempPool = new WordIndexWriter[nextLength]; Array.Copy(_WordIndexWriterPool, tempPool, _WordIndexWriterPool.Length); _WordIndexWriterPool = tempPool; } _WordIndexWriterPool[_IndexWriterPoolId] = new WordIndexWriter(wordInfo.Word, _IndexMode, _DocPositionAlloc); _WordIndexWriterPool[_IndexWriterPoolId].TempDocId = documentId; _WordIndexWriterPool[_IndexWriterPoolId].TempWordCountInThisDoc = 0; _WordIndexWriterPool[_IndexWriterPoolId].TempFirstPosition = wordInfo.Position; _WordIndexWriterPool[_IndexWriterPoolId].TempTotalWordsInDoc = analyzer.Count; _WordTableWriter.Add(wordInfo.Word, _IndexWriterPoolId); _TempWordIndexWriter.Add(_IndexWriterPoolId); index = _IndexWriterPoolId; _IndexWriterPoolId++; } if (_WordIndexWriterPool[index].TempDocId != documentId) { _WordIndexWriterPool[index].TempDocId = documentId; _WordIndexWriterPool[index].TempWordCountInThisDoc = 1; _WordIndexWriterPool[index].TempFirstPosition = wordInfo.Position; _WordIndexWriterPool[index].TempTotalWordsInDoc = analyzer.Count; _TempWordIndexWriter.Add(index); } else { if (_WordIndexWriterPool[index].TempFirstPosition > wordInfo.Position) { _WordIndexWriterPool[index].TempFirstPosition = wordInfo.Position; } _WordIndexWriterPool[index].TempWordCountInThisDoc++; } } foreach (int writeId in _TempWordIndexWriter) { _WordIndexWriterPool[writeId].Index(); } } }
private FileWriter(IFileMemory file, AppendList <ReadFlag> flags, uint fileId) { _file = file; _flags = flags; FileId = fileId; }