Skip to content

Commit

Permalink
update scan time
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored and weissm committed Dec 5, 2023
1 parent 538fc19 commit 19fa49a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
48 changes: 48 additions & 0 deletions OneMore/Commands/Tagging/HashtagProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,54 @@ private Hashtags ReadTags(string sql, SQLiteParameter[] parameters)
}


/// <summary>
/// Records the given tags.
/// </summary>
/// <param name="tags">A collection of Hashtags</param>
public void UpdateContext(Hashtags tags)
{
using var cmd = con.CreateCommand();
cmd.CommandText = "UPDATE hashtags " +
"SET context = @c, lastScan = @s " +
"WHERE tag = @t AND moreID = @m";

cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@c", DbType.String);
cmd.Parameters.Add("@s", DbType.String);
cmd.Parameters.Add("@t", DbType.String);
cmd.Parameters.Add("@m", DbType.String);

using var transaction = con.BeginTransaction();
foreach (var tag in tags)
{
logger.Verbose($"updating tag {tag.Tag}");

cmd.Parameters["@c"].Value = tag.Context;
cmd.Parameters["@s"].Value = tag.LastScan;
cmd.Parameters["@t"].Value = tag.Tag;
cmd.Parameters["@m"].Value = tag.MoreID;

try
{
cmd.ExecuteNonQuery();
}
catch (Exception exc)
{
logger.WriteLine($"error updating tag {tag.Tag} on {tag.PageID}", exc);
}
}

try
{
transaction.Commit();
}
catch (Exception exc)
{
logger.WriteLine("error committing transaction", exc);
}
}


/// <summary>
/// Records the timestamp value that was initialized at construction of this class
/// instance
Expand Down
28 changes: 20 additions & 8 deletions OneMore/Commands/Tagging/HashtagScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public async Task<int> Scan()

private async Task<int> Scan(XElement parent, string path)
{
//logger.WriteLine($"scanning parent {path}");
//logger.Verbose($"scanning parent {path}");

int totalPages = 0;

Expand All @@ -114,7 +114,7 @@ private async Task<int> Scan(XElement parent, string path)
totalPages += pages.Count();

var sectionPath = $"{path}/{section.Attribute("name").Value}";
//logger.WriteLine($"scanning section {sectionPath} ({pages.Count()} pages)");
//logger.Verbose($"scanning section {sectionPath} ({pages.Count()} pages)");

foreach (var page in pages)
{
Expand Down Expand Up @@ -166,6 +166,7 @@ private async Task ScanPage(string pageID, string path)

var saved = provider.ReadPageTags(pageID);
var discovered = new Hashtags();
var updated = new Hashtags();

foreach (var candidate in candidates)
{
Expand All @@ -176,36 +177,47 @@ private async Task ScanPage(string pageID, string path)
}
else
{
//logger.WriteLine($"found tag {found.Tag}");
if (candidate.LastScan.CompareTo(lastTime) > 0)
{
updated.Add(candidate);
}

saved.Remove(found);
}
}

var updated = false;
var updatedAny = false;

if (saved.Any())
{
// remaining saved entries were not matched with candidates
// on page so should be deleted
provider.DeleteTags(saved);
updated = true;
updatedAny = true;
}

if (updated.Any())
{
// tag context updated since last scan
provider.UpdateContext(updated);
updatedAny = true;
}

if (discovered.Any())
{
// discovered entries are new on the page and not found in saved
provider.WriteTags(discovered);
updated = true;
updatedAny = true;
}

// if first time hashtags were discovered on this page then set omPageID
if (scanner.UpdateMeta && updated)
if (scanner.UpdateMeta && updatedAny)
{
page.SetMeta(MetaNames.PageID, scanner.MoreID);
await one.Update(page);
}

if (updated)
if (updatedAny)
{
// will likely rewrite same data but needed in case old page is moved
// TODO: could track moreID+pageID to determine if REPLACE is needed; but then
Expand Down

0 comments on commit 19fa49a

Please sign in to comment.