Exemplo n.º 1
0
		public void CanReadValuesBackWithoutFilter()
		{
			var state = new StorageState("none", new StorageOptions
			{
				ParanoidChecks = true,
				FilterPolicy = null
			});
			string name;
			using (var file = CreateFile())
			{
				name = file.Name;
				using (var tblBuilder = new TableBuilder(state, file, new TemporaryFiles(state.FileSystem, 1)))
				{
					for (int i = 0; i < 10; i++)
					{
						string k = "tests/" + i.ToString("0000");
						tblBuilder.Add(new InternalKey(k, 1, ItemType.Value).TheInternalKey, new MemoryStream(Encoding.UTF8.GetBytes(k)));
					}

					tblBuilder.Finish();
					file.Flush(true);
				}
			}

			using (var mmf = MemoryMappedFile.CreateFromFile(name, FileMode.Open))
			{
				var length = new FileInfo(name).Length;
				using (var table = new Table(state, new FileData(new MemoryMappedFileAccessor(name, mmf), length)))
				using (var iterator = table.CreateIterator(new ReadOptions()))
				{
					for (int i = 0; i < 10; i++)
					{
						string k = "tests/" + i.ToString("0000");
						iterator.Seek(new InternalKey(k, 100, ItemType.Value).TheInternalKey);
						Assert.True(iterator.IsValid);
						using (var stream = iterator.CreateValueStream())
						using (var reader = new StreamReader(stream))
						{
							Assert.Equal(k, reader.ReadToEnd());
						}
					}
				}
			}
		}
Exemplo n.º 2
0
		private void WriteMemTableToDisk(MemRange range)
		{
			string fileName;
			Slice first = new Slice(), last = new Slice();
			using (var stream = _options.CreateNewTableFile(out fileName))
			using (var builder = new TableBuilder(_options, stream, _options.Storage.CreateTemp))
			using (var it = range.MemTable.NewIterator())
			{
				it.SeekToFirst();
				while (it.IsValid)
				{
					if (first.Array == null)
						first = it.Key;
					last = it.Key;
					using (var valueStream = it.CreateValueStream())
						builder.Add(it.Key, valueStream);
					it.Next();
				}
				builder.Finish();
				_options.Storage.Flush(stream);
			}
			lock (_metadataLocker)
			{
				_options.Status.Ranges = new List<CurrentStatus.SstRange>(_options.Status.Ranges)
					{
						new CurrentStatus.SstRange
							{
								End = last,
								Name = fileName,
								Start = first,
								Count = range.MemTable.Count
							}
					};
				_options.Status.LastCompletedLog = range.LogNumber;
				_options.FlushStatus();
			}
		}
Exemplo n.º 3
0
		/// <summary>
		/// Build a Table file from the contents of *iter.  The generated file
		/// will be named according to meta->number.  On success, the rest of
		/// *meta will be filled with metadata about the generated table.
		/// If no data is present in *iter, meta->file_size will be set to
		/// zero, and no Table file will be produced.
		/// </summary>
		/// <param name="memTable"></param>
		/// <param name="fileNumber"></param>
		/// <returns></returns>
		public FileMetadata BuildTable(MemTable memTable, ulong fileNumber)
		{
			CancellationToken.ThrowIfCancellationRequested();

			TableBuilder builder = null;
			var meta = new FileMetadata
						   {
							   FileNumber = fileNumber
						   };

			var tableFileName = FileSystem.GetTableFileName(fileNumber);
			try
			{
				var iterator = memTable.NewIterator();
				iterator.SeekToFirst();

				if (Log.IsDebugEnabled)
					Log.Debug("Writing table with {0:#,#;;00} items to {1}", memTable.Count, tableFileName);

				if (iterator.IsValid)
				{
					var tableFile = FileSystem.NewWritable(tableFileName);
					builder = new TableBuilder(this, tableFile, new TemporaryFiles(FileSystem, fileNumber));

					meta.SmallestKey = new InternalKey(iterator.Key);
					while (iterator.IsValid)
					{
						CancellationToken.ThrowIfCancellationRequested();
						var key = iterator.Key;

						meta.LargestKey = new InternalKey(key);

						if (Log.IsDebugEnabled)
							Log.Debug("Writing item with key {0}", meta.LargestKey.DebugVal);

						using (var stream = iterator.CreateValueStream())
							builder.Add(key, stream);

						iterator.Next();
					}

					builder.Finish();

					meta.FileSize = builder.FileSize;
					Debug.Assert(meta.FileSize > 0);
				}
			}
			finally
			{
				if (builder != null)
					builder.Dispose();

				if (meta.FileSize == 0)
				{
					FileSystem.DeleteFile(tableFileName);
				}
			}

			return meta;
		}