public static bool TryFindFile(this IList<FileMetadata> files, Slice key, InternalKeyComparator internalKeyComparator, out int fileIndex)
		 {
			 var left = 0;
			 var right = files.Count;

			 while (left < right)
			 {
				 var mid = (left + right) / 2;
				 var file = files[mid];

				 if (internalKeyComparator.Compare(file.LargestKey.TheInternalKey, key) < 0)
				 {
					 // Key at "mid.largest" is < "target".  Therefore all
					 // files at or before "mid" are uninteresting.
					 left = mid + 1;
				 }
				 else
				 {
					 // Key at "mid.largest" is >= "target".  Therefore all files
					 // after "mid" are uninteresting.
					 right = mid;
				 }
			 }

			 fileIndex = right;

			 return fileIndex < files.Count;
		 }
		public LevelFileNumIterator(InternalKeyComparator internalKeyComparator, IList<FileMetadata> files)
		{
			this.internalKeyComparator = internalKeyComparator;
			this.files = files;

			MarkAsInvalid();
		}
		public void T1()
		{
			var storageState = new StorageState("test", new StorageOptions());
			var random = new Random();
			var tables = new List<MemTable>();
			var expectedCount = 0;

			try
			{
				ulong seq = 0;
				for (var i = 0; i < 100; i++)
				{
					var table = new MemTable(storageState);
					for (var j = 0; j < 1000; j++)
					{
						var k = random.Next();
						var key = string.Format("{0:0000000000000000}", k);
						table.Add(seq++, ItemType.Value, key, null);
						expectedCount++;
					}

					tables.Add(table);
				}

				var iterators = tables.Select(table => table.NewIterator()).ToList();
				var comparator = new InternalKeyComparator(new CaseInsensitiveComparator());

				using (var iterator = new MergingIterator(comparator, iterators))
				{
					var actualCount = 0;
					iterator.SeekToFirst();
					Assert.True(iterator.IsValid);

					Slice prev = string.Empty;

					while (iterator.IsValid)
					{
						if (!prev.IsEmpty())
						{
							Assert.True(comparator.Compare(iterator.Key, prev) > 0);
						}

						prev = iterator.Key.Clone();
						iterator.Next();
						actualCount++;
					}

					Assert.Equal(expectedCount, actualCount);
				}
			}
			finally
			{
				foreach (var table in tables)
					table.Dispose();
			}

			
		}
		public MemTable(int writeBatchSize, InternalKeyComparator internalKeyComparator, BufferPool bufferPool)
		{
			CreatedAt = DateTime.UtcNow;
			_bufferPool = bufferPool;
			_memoryAccessor = new UnamangedMemoryAccessor(writeBatchSize);

			_internalKeyComparator = internalKeyComparator;
			_table = new SkipList<InternalKey, UnamangedMemoryAccessor.MemoryHandle>(_internalKeyComparator);
		}
        public FileMetadata GetNextFileToCompactForLevel(List <FileMetadata> files, byte[] lastCompactionKey)
        {
            var comparator = new InternalKeyComparator();

            foreach (FileMetadata file in files)
            {
                if (comparator.Compare(file.SmallestKey, lastCompactionKey) >= 0)
                {
                    return(file);
                }
            }

            return(files.FirstOrDefault());
        }
		public StorageState(string name, StorageOptions options)
		{
			_perfCounters = new PerfCounters(name);
			_cancellationTokenSource = new CancellationTokenSource();
			CancellationToken = _cancellationTokenSource.Token;
			Options = options;
			InternalKeyComparator = new InternalKeyComparator(options.Comparator);
			DatabaseName = name;
			Lock = new AsyncLock();
			FileSystem = new FileSystem(DatabaseName);
			MemTable = new MemTable(this);
			TableCache = new TableCache(this);
			VersionSet = new VersionSet(this);
			Compactor = new BackgroundCompactor(this);
			Snapshooter = new Snapshooter(this);
		}
		private IIterator NewMergingIterator(InternalKeyComparator comparator, IList<IIterator> list)
		{
			if (list.Count == 0)
				return new EmptyIterator();

			return list.Count == 1 ? list.First() : new MergingIterator(comparator, list);
		}