Reformat / cleanup
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests.Benchmark;
|
||||
|
||||
public class Address
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Street.
|
||||
/// Gets or sets the Street.
|
||||
/// </summary>
|
||||
public string Street { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the City.
|
||||
/// Gets or sets the City.
|
||||
/// </summary>
|
||||
public string City { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ZipCode.
|
||||
/// Gets or sets the ZipCode.
|
||||
/// </summary>
|
||||
public string ZipCode { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -23,19 +23,22 @@ public class Address
|
||||
public class WorkHistory
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the CompanyName.
|
||||
/// Gets or sets the CompanyName.
|
||||
/// </summary>
|
||||
public string CompanyName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Title.
|
||||
/// Gets or sets the Title.
|
||||
/// </summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the DurationYears.
|
||||
/// Gets or sets the DurationYears.
|
||||
/// </summary>
|
||||
public int DurationYears { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Tags.
|
||||
/// Gets or sets the Tags.
|
||||
/// </summary>
|
||||
public List<string> Tags { get; set; } = new();
|
||||
}
|
||||
@@ -43,41 +46,48 @@ public class WorkHistory
|
||||
public class Person
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Id.
|
||||
/// Gets or sets the Id.
|
||||
/// </summary>
|
||||
public ObjectId Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the FirstName.
|
||||
/// Gets or sets the FirstName.
|
||||
/// </summary>
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the LastName.
|
||||
/// Gets or sets the LastName.
|
||||
/// </summary>
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Age.
|
||||
/// Gets or sets the Age.
|
||||
/// </summary>
|
||||
public int Age { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Bio.
|
||||
/// Gets or sets the Bio.
|
||||
/// </summary>
|
||||
public string? Bio { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the CreatedAt.
|
||||
/// Gets or sets the CreatedAt.
|
||||
/// </summary>
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
// Complex fields
|
||||
/// <summary>
|
||||
/// Gets or sets the Balance.
|
||||
/// Gets or sets the Balance.
|
||||
/// </summary>
|
||||
public decimal Balance { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HomeAddress.
|
||||
/// Gets or sets the HomeAddress.
|
||||
/// </summary>
|
||||
public Address HomeAddress { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the EmploymentHistory.
|
||||
/// Gets or sets the EmploymentHistory.
|
||||
/// </summary>
|
||||
public List<WorkHistory> EmploymentHistory { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,30 @@
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
using ZB.MOM.WW.CBDD.Core.Collections;
|
||||
using System.Buffers;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests.Benchmark;
|
||||
|
||||
public class PersonMapper : ObjectIdMapperBase<Person>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string CollectionName => "people";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ObjectId GetId(Person entity) => entity.Id;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetId(Person entity, ObjectId id) => entity.Id = id;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Serialize(Person entity, BsonSpanWriter writer)
|
||||
{
|
||||
var sizePos = writer.BeginDocument();
|
||||
|
||||
public class PersonMapper : ObjectIdMapperBase<Person>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string CollectionName => "people";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ObjectId GetId(Person entity)
|
||||
{
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetId(Person entity, ObjectId id)
|
||||
{
|
||||
entity.Id = id;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Serialize(Person entity, BsonSpanWriter writer)
|
||||
{
|
||||
int sizePos = writer.BeginDocument();
|
||||
|
||||
writer.WriteObjectId("_id", entity.Id);
|
||||
writer.WriteString("firstname", entity.FirstName);
|
||||
writer.WriteString("lastname", entity.LastName);
|
||||
@@ -30,111 +34,119 @@ public class PersonMapper : ObjectIdMapperBase<Person>
|
||||
else
|
||||
writer.WriteNull("bio");
|
||||
|
||||
writer.WriteInt64("createdat", entity.CreatedAt.Ticks);
|
||||
|
||||
// Complex fields
|
||||
writer.WriteDouble("balance", (double)entity.Balance);
|
||||
|
||||
// Nested Object: Address
|
||||
var addrPos = writer.BeginDocument("homeaddress");
|
||||
writer.WriteInt64("createdat", entity.CreatedAt.Ticks);
|
||||
|
||||
// Complex fields
|
||||
writer.WriteDouble("balance", (double)entity.Balance);
|
||||
|
||||
// Nested Object: Address
|
||||
int addrPos = writer.BeginDocument("homeaddress");
|
||||
writer.WriteString("street", entity.HomeAddress.Street);
|
||||
writer.WriteString("city", entity.HomeAddress.City);
|
||||
writer.WriteString("zipcode", entity.HomeAddress.ZipCode);
|
||||
writer.EndDocument(addrPos);
|
||||
|
||||
// Collection: EmploymentHistory
|
||||
var histPos = writer.BeginArray("employmenthistory");
|
||||
for (int i = 0; i < entity.EmploymentHistory.Count; i++)
|
||||
writer.EndDocument(addrPos);
|
||||
|
||||
// Collection: EmploymentHistory
|
||||
int histPos = writer.BeginArray("employmenthistory");
|
||||
for (var i = 0; i < entity.EmploymentHistory.Count; i++)
|
||||
{
|
||||
var item = entity.EmploymentHistory[i];
|
||||
// Array elements are keys "0", "1", "2"...
|
||||
var itemPos = writer.BeginDocument(i.ToString());
|
||||
|
||||
int itemPos = writer.BeginDocument(i.ToString());
|
||||
|
||||
writer.WriteString("companyname", item.CompanyName);
|
||||
writer.WriteString("title", item.Title);
|
||||
writer.WriteInt32("durationyears", item.DurationYears);
|
||||
|
||||
// Nested Collection: Tags
|
||||
var tagsPos = writer.BeginArray("tags");
|
||||
for (int j = 0; j < item.Tags.Count; j++)
|
||||
{
|
||||
writer.WriteString(j.ToString(), item.Tags[j]);
|
||||
}
|
||||
writer.EndArray(tagsPos);
|
||||
|
||||
writer.WriteInt32("durationyears", item.DurationYears);
|
||||
|
||||
// Nested Collection: Tags
|
||||
int tagsPos = writer.BeginArray("tags");
|
||||
for (var j = 0; j < item.Tags.Count; j++) writer.WriteString(j.ToString(), item.Tags[j]);
|
||||
writer.EndArray(tagsPos);
|
||||
|
||||
writer.EndDocument(itemPos);
|
||||
}
|
||||
writer.EndArray(histPos);
|
||||
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
|
||||
writer.EndArray(histPos);
|
||||
|
||||
writer.EndDocument(sizePos);
|
||||
|
||||
return writer.Position;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Person Deserialize(BsonSpanReader reader)
|
||||
{
|
||||
var person = new Person();
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Person Deserialize(BsonSpanReader reader)
|
||||
{
|
||||
var person = new Person();
|
||||
|
||||
reader.ReadDocumentSize();
|
||||
|
||||
while (reader.Remaining > 0)
|
||||
{
|
||||
var type = reader.ReadBsonType();
|
||||
if (type == BsonType.EndOfDocument)
|
||||
break;
|
||||
|
||||
var name = reader.ReadElementHeader();
|
||||
|
||||
break;
|
||||
|
||||
string name = reader.ReadElementHeader();
|
||||
|
||||
switch (name)
|
||||
{
|
||||
case "_id": person.Id = reader.ReadObjectId(); break;
|
||||
case "firstname": person.FirstName = reader.ReadString(); break;
|
||||
case "lastname": person.LastName = reader.ReadString(); break;
|
||||
case "age": person.Age = reader.ReadInt32(); break;
|
||||
case "bio":
|
||||
case "bio":
|
||||
if (type == BsonType.Null) person.Bio = null;
|
||||
else person.Bio = reader.ReadString();
|
||||
else person.Bio = reader.ReadString();
|
||||
break;
|
||||
case "createdat": person.CreatedAt = new DateTime(reader.ReadInt64()); break;
|
||||
case "balance": person.Balance = (decimal)reader.ReadDouble(); break;
|
||||
|
||||
case "balance": person.Balance = (decimal)reader.ReadDouble(); break;
|
||||
|
||||
case "homeaddress":
|
||||
reader.ReadDocumentSize(); // Enter document
|
||||
while (reader.Remaining > 0)
|
||||
{
|
||||
var addrType = reader.ReadBsonType();
|
||||
if (addrType == BsonType.EndOfDocument) break;
|
||||
var addrName = reader.ReadElementHeader();
|
||||
|
||||
// We assume strict schema for benchmark speed, but should handle skipping
|
||||
string addrName = reader.ReadElementHeader();
|
||||
|
||||
// We assume strict schema for benchmark speed, but should handle skipping
|
||||
if (addrName == "street") person.HomeAddress.Street = reader.ReadString();
|
||||
else if (addrName == "city") person.HomeAddress.City = reader.ReadString();
|
||||
else if (addrName == "zipcode") person.HomeAddress.ZipCode = reader.ReadString();
|
||||
else reader.SkipValue(addrType);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case "employmenthistory":
|
||||
reader.ReadDocumentSize(); // Enter Array
|
||||
while (reader.Remaining > 0)
|
||||
{
|
||||
var arrType = reader.ReadBsonType();
|
||||
if (arrType == BsonType.EndOfDocument) break;
|
||||
reader.ReadElementHeader(); // Array index "0", "1"... ignore
|
||||
|
||||
// Read WorkHistory item
|
||||
reader.ReadElementHeader(); // Array index "0", "1"... ignore
|
||||
|
||||
// Read WorkHistory item
|
||||
var workItem = new WorkHistory();
|
||||
reader.ReadDocumentSize(); // Enter Item Document
|
||||
while (reader.Remaining > 0)
|
||||
{
|
||||
var itemType = reader.ReadBsonType();
|
||||
if (itemType == BsonType.EndOfDocument) break;
|
||||
var itemName = reader.ReadElementHeader();
|
||||
|
||||
if (itemName == "companyname") workItem.CompanyName = reader.ReadString();
|
||||
else if (itemName == "title") workItem.Title = reader.ReadString();
|
||||
else if (itemName == "durationyears") workItem.DurationYears = reader.ReadInt32();
|
||||
string itemName = reader.ReadElementHeader();
|
||||
|
||||
if (itemName == "companyname")
|
||||
{
|
||||
workItem.CompanyName = reader.ReadString();
|
||||
}
|
||||
else if (itemName == "title")
|
||||
{
|
||||
workItem.Title = reader.ReadString();
|
||||
}
|
||||
else if (itemName == "durationyears")
|
||||
{
|
||||
workItem.DurationYears = reader.ReadInt32();
|
||||
}
|
||||
else if (itemName == "tags")
|
||||
{
|
||||
reader.ReadDocumentSize(); // Enter Tags Array
|
||||
@@ -149,18 +161,23 @@ public class PersonMapper : ObjectIdMapperBase<Person>
|
||||
reader.SkipValue(tagType);
|
||||
}
|
||||
}
|
||||
else reader.SkipValue(itemType);
|
||||
else
|
||||
{
|
||||
reader.SkipValue(itemType);
|
||||
}
|
||||
}
|
||||
|
||||
person.EmploymentHistory.Add(workItem);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
reader.SkipValue(type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return person;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user