fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate (#51)
fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate (#51) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #51.
This commit is contained in:
+5
-1
@@ -360,9 +360,13 @@ func (k *KnoxDB) RecordObservation(o ObservationRecord) (obsID int64, isNew bool
|
|||||||
err = tx.QueryRow(
|
err = tx.QueryRow(
|
||||||
`SELECT id, COALESCE(title,''), COALESCE(summary,''), COALESCE(project,''),
|
`SELECT id, COALESCE(title,''), COALESCE(summary,''), COALESCE(project,''),
|
||||||
COALESCE(created_at,''), COALESCE(line_start,0), COALESCE(line_end,0),
|
COALESCE(created_at,''), COALESCE(line_start,0), COALESCE(line_end,0),
|
||||||
COALESCE(content_hash,''), COALESCE(LENGTH(content),0)
|
COALESCE(content_hash,''), COALESCE(LENGTH(CAST(content AS BLOB)),0)
|
||||||
FROM observations WHERE fingerprint=? ORDER BY hcl DESC, id DESC LIMIT 1`, o.Fingerprint,
|
FROM observations WHERE fingerprint=? ORDER BY hcl DESC, id DESC LIMIT 1`, o.Fingerprint,
|
||||||
).Scan(&prevID, &pTitle, &pSummary, &pProject, &pCreated, &pStart, &pEnd, &pContent, &pContentLen)
|
).Scan(&prevID, &pTitle, &pSummary, &pProject, &pCreated, &pStart, &pEnd, &pContent, &pContentLen)
|
||||||
|
// Byte length, not characters. len() counts bytes; LENGTH() on a TEXT value
|
||||||
|
// counts characters, so the two disagree for any body with an em dash or a
|
||||||
|
// curly quote — and every sweep then appends a duplicate of identical
|
||||||
|
// content. The CAST keeps SQLite on the same scale as Go.
|
||||||
sameContent := int64(len(o.Content)) == pContentLen
|
sameContent := int64(len(o.Content)) == pContentLen
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package db_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/david/knox/internal/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestRecordObservationDeduplicatesMultibyteContent pins the byte-vs-character
|
||||||
|
// bug: SQLite's LENGTH() counts characters on TEXT, while Go's len() counts
|
||||||
|
// bytes. Comparing the two made every body containing an em dash, a curly quote
|
||||||
|
// or any non-ASCII text look changed, so each sweep appended a duplicate
|
||||||
|
// observation with an identical content hash.
|
||||||
|
func TestRecordObservationDeduplicatesMultibyteContent(t *testing.T) {
|
||||||
|
kdb, raw := openBoth(t)
|
||||||
|
|
||||||
|
rec := db.ObservationRecord{
|
||||||
|
Fingerprint: "fp-multibyte",
|
||||||
|
SourceID: "test",
|
||||||
|
Title: "Typographic punctuation",
|
||||||
|
Summary: "em dash and curly quotes",
|
||||||
|
Content: "— “quoted” and an em dash — plus ünïcödé",
|
||||||
|
ContentHash: "hash-1",
|
||||||
|
IngesterVersion: "test/v1",
|
||||||
|
Trigger: "test",
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, isNew, err := kdb.RecordObservation(rec); err != nil || !isNew {
|
||||||
|
t.Fatalf("first record = (isNew=%v, err=%v), want a new observation", isNew, err)
|
||||||
|
}
|
||||||
|
if _, isNew, err := kdb.RecordObservation(rec); err != nil || isNew {
|
||||||
|
t.Fatalf("second record = (isNew=%v, err=%v), want a deduped no-op", isNew, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows int
|
||||||
|
if err := raw.QueryRow(
|
||||||
|
`SELECT COUNT(*) FROM observations WHERE fingerprint='fp-multibyte'`,
|
||||||
|
).Scan(&rows); err != nil {
|
||||||
|
t.Fatalf("count: %v", err)
|
||||||
|
}
|
||||||
|
if rows != 1 {
|
||||||
|
t.Fatalf("observations = %d, want 1 (identical multibyte content must not append a version)", rows)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRecordObservationAppendsOnRealChange is the other half of the contract:
|
||||||
|
// fixing the length comparison must not turn dedupe into "ignore everything".
|
||||||
|
func TestRecordObservationAppendsOnRealChange(t *testing.T) {
|
||||||
|
kdb, raw := openBoth(t)
|
||||||
|
|
||||||
|
base := db.ObservationRecord{
|
||||||
|
Fingerprint: "fp-change",
|
||||||
|
SourceID: "test",
|
||||||
|
Content: "first version — with a dash",
|
||||||
|
ContentHash: "hash-a",
|
||||||
|
IngesterVersion: "test/v1",
|
||||||
|
Trigger: "test",
|
||||||
|
}
|
||||||
|
if _, _, err := kdb.RecordObservation(base); err != nil {
|
||||||
|
t.Fatalf("first: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Content = "second version — with a longer dash"
|
||||||
|
base.ContentHash = "hash-b"
|
||||||
|
if _, isNew, err := kdb.RecordObservation(base); err != nil || isNew {
|
||||||
|
t.Fatalf("changed record = (isNew=%v, err=%v), want a new version, not a new fingerprint", isNew, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var versions int
|
||||||
|
if err := raw.QueryRow(
|
||||||
|
`SELECT COUNT(*) FROM observations WHERE fingerprint='fp-change'`,
|
||||||
|
).Scan(&versions); err != nil {
|
||||||
|
t.Fatalf("count: %v", err)
|
||||||
|
}
|
||||||
|
if versions != 2 {
|
||||||
|
t.Fatalf("versions = %d, want 2 (a changed body must still append)", versions)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user