private bool AfterFile(IComparator comparator, Slice key, FileMetadata file) { // Empty 'key' occurs before all keys and is therefore never after 'file' return (key.IsEmpty() == false && comparator.Compare(key, file.LargestKey.UserKey) > 0); }
private bool SomeFileOverlapsRange(bool disjointSortedFiles, IList<FileMetadata> files, Slice smallestKey, Slice largestKey) { var userComparator = storageContext.InternalKeyComparator.UserComparator; if (!disjointSortedFiles) { // Need to check against all files return files.Any(file => !AfterFile(userComparator, smallestKey, file) && !BeforeFile(userComparator, largestKey, file)); } var index = 0; if (smallestKey.IsEmpty() == false) { var smallInternalKey = new InternalKey(smallestKey, Format.MaxSequenceNumber, ItemType.ValueForSeek); files.TryFindFile(smallInternalKey.TheInternalKey, storageContext.InternalKeyComparator, out index); } if (index >= files.Count) { // beginning of range is after all files, so no overlap. return false; } return !BeforeFile(userComparator, largestKey, files[index]); }
private bool BeforeFile(IComparator comparator, Slice key, FileMetadata file) { // Empty 'key' occurs after all keys and is therefore never before 'file' return (key.IsEmpty() == false && comparator.Compare(key, file.SmallestKey.UserKey) < 0); }
public void MaybeAddFile(InternalKey key, IIterator iterator, CompactionState compactionState, AsyncLock.LockScope locker, ref Slice lastKey, ref ulong lastSequence) { var drop = false; if (lastKey.IsEmpty() || state.InternalKeyComparator.UserComparator.Compare(key.UserKey, lastKey) != 0) { // First occurrence of this user key lastKey = key.UserKey.Clone(); lastSequence = Format.MaxSequenceNumber; } if (lastSequence <= compactionState.SmallestSnapshot) { // Hidden by an newer entry for same user key drop = true; } else if (key.Type == ItemType.Deletion && key.Sequence <= compactionState.SmallestSnapshot && compactionState.Compaction.IsBaseLevelForKey(key.UserKey)) { // For this user key: // (1) there is no data in higher levels // (2) data in lower levels will have larger sequence numbers // (3) data in layers that are being compacted here and have // smaller sequence numbers will be dropped in the next // few iterations of this loop (by rule (A) above). // Therefore this deletion marker is obsolete and can be dropped. drop = true; } lastSequence = key.Sequence; if (!drop) { if (compactionState.Builder == null) { using (locker.LockAsync().Result) OpenCompactionOutputFileIfNecessary(compactionState, locker); } Debug.Assert(compactionState.Builder != null); if (compactionState.Builder.NumEntries == 0) compactionState.CurrentOutput.SmallestKey = new InternalKey(key.TheInternalKey.Clone()); compactionState.CurrentOutput.LargestKey = new InternalKey(key.TheInternalKey.Clone()); //Console.WriteLine("Adding " + compactionState.CurrentOutput.LargestKey); using (var stream = iterator.CreateValueStream()) compactionState.Builder.Add(key.TheInternalKey, stream); FinishCompactionOutputFileIfNecessary(compactionState, iterator); } }