Пример #1
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);
			}
		}