Skip to content

Commit

Permalink
* rename interface BasePost to IPost @ c#/crawler
Browse files Browse the repository at this point in the history
  • Loading branch information
n0099 committed Jul 27, 2024
1 parent 274f15b commit d4e3f0d
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion c#/crawler/src/Db/CrawlerDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void TimestampingEntities() =>
var originalEntityState = e.State; // copy e.State since it might change after any prop value updated
var createdAtProp = e.Property(ie => ie.CreatedAt);
var updatedAtProp = e.Property(ie => ie.UpdatedAt);
var lastSeenAtProp = e.Entity is BasePost ? e.Property(ie => ((BasePost)ie).LastSeenAt) : null;
var lastSeenAtProp = e.Entity is IPost ? e.Property(ie => ((IPost)ie).LastSeenAt) : null;
// ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
switch (originalEntityState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace tbm.Crawler.Db.Post;

public interface BasePost : ICloneable
public interface IPost : ICloneable
{
public interface IParsed : BasePost;
public interface IParsed : IPost;
[Column(TypeName = "bigint")]
public ulong Tid { get; set; }
public long AuthorUid { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Db/Post/IReplyOrSubReplyParsedPost.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace tbm.Crawler.Db.Post;

public interface IReplyOrSubReplyParsedPost : BasePost.IParsed
public interface IReplyOrSubReplyParsedPost : IPost.IParsed
{
public byte AuthorExpGrade { get; set; }
public byte[]? Content { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Db/Post/ReplyPost.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ReSharper disable PropertyCanBeMadeInitOnly.Global
namespace tbm.Crawler.Db.Post;

public class ReplyPost : TimestampedEntity, BasePost
public class ReplyPost : TimestampedEntity, IPost
{
public ulong Tid { get; set; }
public long AuthorUid { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Db/Post/SubReplyPost.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ReSharper disable PropertyCanBeMadeInitOnly.Global
namespace tbm.Crawler.Db.Post;

public class SubReplyPost : TimestampedEntity, BasePost
public class SubReplyPost : TimestampedEntity, IPost
{
public ulong Tid { get; set; }
public long AuthorUid { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Db/Post/ThreadPost.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ReSharper disable PropertyCanBeMadeInitOnly.Global
namespace tbm.Crawler.Db.Post;

public class ThreadPost : TimestampedEntity, BasePost
public class ThreadPost : TimestampedEntity, IPost
{
[Key] public ulong Tid { get; set; }
public long AuthorUid { get; set; }
Expand All @@ -27,7 +27,7 @@ public class ThreadPost : TimestampedEntity, BasePost

public object Clone() => MemberwiseClone();

public class Parsed : ThreadPost, BasePost.IParsed
public class Parsed : ThreadPost, IPost.IParsed
{
public ulong? FirstReplyPid { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Facade/CrawlFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public abstract class CrawlFacade<TPostEntity, TParsedPost, TResponse, TPostProt
Func<ConcurrentDictionary<Uid, User>, UserParser> userParserFactory,
Func<ConcurrentDictionary<Uid, User>, UserSaver> userSaverFactory)
: ICrawlFacade<TPostEntity, TParsedPost>
where TPostEntity : BasePost
where TParsedPost : TPostEntity, BasePost.IParsed
where TPostEntity : IPost
where TParsedPost : TPostEntity, IPost.IParsed
where TResponse : class, IMessage<TResponse>
where TPostProtoBuf : class, IMessage<TPostProtoBuf>
{
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Facade/ICrawlFacade.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace tbm.Crawler.Tieba.Crawl.Facade;

public interface ICrawlFacade<TPost, TParsedPost> : IDisposable
where TPost : BasePost
where TParsedPost : TPost, BasePost.IParsed
where TPost : IPost
where TParsedPost : TPost, IPost.IParsed
{
public delegate void ExceptionHandler(Exception ex);

Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Parser/Post/IPostParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace tbm.Crawler.Tieba.Crawl.Parser.Post;

public interface IPostParser<TPost, in TPostProtoBuf>
where TPost : BasePost.IParsed
where TPost : IPost.IParsed
where TPostProtoBuf : class, IMessage<TPostProtoBuf>
{
public void Parse(
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Parser/Post/PostParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace tbm.Crawler.Tieba.Crawl.Parser.Post;

public abstract class PostParser<TPost, TPostProtoBuf>
: IPostParser<TPost, TPostProtoBuf>
where TPost : BasePost.IParsed
where TPost : IPost.IParsed
where TPostProtoBuf : class, IMessage<TPostProtoBuf>
{
public void Parse(
Expand All @@ -21,7 +21,7 @@ public void Parse(
outPosts = ParseInternal(inPosts, nullableUsers).ToDictionary(PostIdSelector, post => post);
if (outPosts.Values.Any(p => p.AuthorUid == 0))
throw new TiebaException(shouldRetry: true,
"Value of BasePost.AuthorUid is the protoBuf default value 0.");
"Value of IPost.AuthorUid is the protoBuf default value 0.");

var users = new List<TbClient.User>(30);
users.AddRange(nullableUsers.OfType<TbClient.User>()
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/IPostSaver.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace tbm.Crawler.Tieba.Crawl.Saver.Post;

public interface IPostSaver<TPostEntity, TParsedPost>
where TPostEntity : BasePost
where TParsedPost : TPostEntity, BasePost.IParsed
where TPostEntity : IPost
where TParsedPost : TPostEntity, IPost.IParsed
{
public PostType CurrentPostType { get; }
public bool UserFieldUpdateIgnorance(string propName, object? oldValue, object? newValue);
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public abstract class PostSaver<TPostEntity, TParsedPost, TBaseRevision, TPostId
AuthorRevisionSaver.New authorRevisionSaverFactory,
PostType currentPostType)
: SaverWithRevision<TBaseRevision, TPostId>(logger), IPostSaver<TPostEntity, TParsedPost>
where TPostEntity : RowVersionedEntity, BasePost
where TParsedPost : TPostEntity, BasePost.IParsed
where TPostEntity : RowVersionedEntity, IPost
where TParsedPost : TPostEntity, IPost.IParsed
where TBaseRevision : BaseRevisionWithSplitting
where TPostId : struct
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ private Action Save<TPost, TRevision, TValue>(
Func<TValue?, TValue?, bool> isValueChangedPredicate,
Expression<Func<TRevision, LatestAuthorRevisionProjection<TValue>>> latestRevisionProjectionFactory,
Func<(Uid Uid, TValue? Value, Time DiscoveredAt), TRevision> revisionFactory)
where TPost : BasePost
where TPost : IPost
where TRevision : AuthorRevision
{ // only takes the first of multiple post from the same author
var uniquePosts = posts.DistinctBy(p => p.AuthorUid).ToList();
if (uniquePosts.Count != posts.Count)
Helper.LogDifferentValuesSharingTheSameKeyInEntities(logger, posts,
$"{nameof(TPost)}.{nameof(BasePost.AuthorUid)}",
$"{nameof(TPost)}.{nameof(IPost.AuthorUid)}",
p => p.AuthorUid,
postRevisioningFieldSelector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Action SaveFromUser(CrawlerDbContext db, Tid tid, IEnumerable<User> users
if (thread == null) return () => { };

// https://stackoverflow.com/questions/63094891/ef-core-tracking-child-objects-unnecessarily
// detach the unused thread entity to prevent CrawlerDbContext.TimestampingEntities() updating its BasePost.LastSeenAt
// detach the unused thread entity to prevent CrawlerDbContext.TimestampingEntities() updating its IPost.LastSeenAt
db.Entry(thread).State = EntityState.Detached;
var threadLatestReplier = thread.LatestReplier;
if (threadLatestReplier == null) return () => { };
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class SaverChangeSet<TPostEntity, TParsedPost>(
ICollection<TParsedPost> parsed,
IReadOnlyCollection<TPostEntity> existingBefore,
IReadOnlyCollection<TPostEntity> existingAfter)
where TPostEntity : BasePost
where TParsedPost : TPostEntity, BasePost.IParsed
where TPostEntity : IPost
where TParsedPost : TPostEntity, IPost.IParsed
{
public IReadOnlyCollection<(TPostEntity Before, TPostEntity After)> Existing { get; } = existingBefore
.OrderBy(postIdSelector)
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public partial class SaverWithRevision<TBaseRevision, TRevisionId>
protected virtual bool FieldRevisionIgnorance(string propName, object? oldValue, object? newValue) => false;
private static bool GlobalFieldUpdateIgnorance(string propName, object? oldValue, object? newValue) => propName switch
{ // possible rarely respond with the protoBuf default value 0
nameof(BasePost.AuthorUid) when newValue is 0L && oldValue is not null => true,
nameof(IPost.AuthorUid) when newValue is 0L && oldValue is not null => true,
_ => false
};
}
Expand Down Expand Up @@ -96,7 +96,7 @@ on newNavigation.Metadata.Name equals existingNavigation.Metadata.Name
.ForEach(t => t.existingNavigation.CurrentValue = t.newNavigation.CurrentValue);
// rollback changes that overwrite original values with the default value 0 or null
// for all fields of TimestampedEntity and BasePost.LastSeenAt
// for all fields of TimestampedEntity and IPost.LastSeenAt
// this will also affect the entity instance which existingEntity references to it
entityEntry.Properties
.Where(prop => prop.IsModified && IsTimestampingFieldName(prop.Metadata.Name))
Expand Down Expand Up @@ -190,7 +190,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
.ForEach(g => AddSplitRevisionsDelegatesKeyByEntityType.Value[g.Key](db, g));
}

private static bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt)
private static bool IsTimestampingFieldName(string name) => name is nameof(IPost.LastSeenAt)
or nameof(TimestampedEntity.CreatedAt) or nameof(TimestampedEntity.UpdatedAt);

protected record ExistingAndNewEntity<TEntity>(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity;
Expand Down

0 comments on commit d4e3f0d

Please sign in to comment.