コード例 #1
0
		public void CanReadAndWriteStructsFromTrees()
		{
			var indexedAt = new DateTime(2015, 1, 20);

			var schema = new StructureSchema<IndexingStatsFields>()
				.Add<int>(IndexingStatsFields.Attempts)
				.Add<int>(IndexingStatsFields.Errors)
				.Add<int>(IndexingStatsFields.Successes)
				.Add<byte>(IndexingStatsFields.IsValid)
				.Add<long>(IndexingStatsFields.IndexedAt);

			using (var tx = Env.NewTransaction(TransactionFlags.ReadWrite))
			{
				var tree = Env.CreateTree(tx, "stats");

				var stats = new Structure<IndexingStatsFields>(schema);

				stats.Set(IndexingStatsFields.Attempts, 5);
				stats.Set(IndexingStatsFields.Errors, -1);
				stats.Set(IndexingStatsFields.Successes, 4);
				stats.Set(IndexingStatsFields.IsValid, (byte) 1);
				stats.Set(IndexingStatsFields.IndexedAt, indexedAt.ToBinary());

				tree.WriteStruct("stats/1", stats);

				tx.Commit();
			}

			using (var tx = Env.NewTransaction(TransactionFlags.Read))
			{
				var tree = tx.ReadTree("stats");

				var stats = tree.ReadStruct("stats/1", schema).Reader;

				Assert.Equal(5, stats.ReadInt(IndexingStatsFields.Attempts));
				Assert.Equal(-1, stats.ReadInt(IndexingStatsFields.Errors));
				Assert.Equal(4, stats.ReadInt(IndexingStatsFields.Successes));
				Assert.Equal(1, stats.ReadByte(IndexingStatsFields.IsValid));
				Assert.Equal(indexedAt, DateTime.FromBinary(stats.ReadLong(IndexingStatsFields.IndexedAt)));
			}
		}