Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize UrlLocator.domAnchors so that they merge correctly. #708

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions premiser-ui/src/normalizationSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AppearanceView,
ContextTrailItem,
Domain,
DomAnchor,
JustificationView,
JustificationVote,
MediaExcerptCitationIdentifier,
Expand Down Expand Up @@ -38,6 +39,7 @@ import {
} from "howdju-common";

import { applyCustomizations, momentConversion } from "./normalizationUtil";
import { hashString } from "./util";

export const userSchema = new schema.Entity<UserOut>("users");
export const usersSchema = new schema.Array(userSchema);
Expand Down Expand Up @@ -176,10 +178,31 @@ export const sourceExcerptParaphraseSchema =
export const urlSchema = new schema.Entity<UrlOut>("urls");
export const urlsSchema = new schema.Array(urlSchema);

export const domAnchorSchema = new schema.Entity<DomAnchor>(
"domAnchors",
{},
{
idAttribute: domAnchorKey,
}
);

function domAnchorKey(anchor: DomAnchor) {
return [
anchor.urlLocatorId,
anchor.startOffset,
anchor.endOffset,
hashString(anchor.prefixText),
hashString(anchor.exactText),
hashString(anchor.suffixText),
].join("-");
}
export const domAnchorsSchema = new schema.Array(domAnchorSchema);

export const urlLocatorSchema = new schema.Entity<UrlLocatorOut>(
"urlLocators",
{
url: urlSchema,
anchors: domAnchorsSchema,
},
{
processStrategy: (value) =>
Expand Down
8 changes: 5 additions & 3 deletions premiser-ui/src/reducers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
mediaExcerptSpeakerKey,
propositionTagVoteSchema,
domainSchema,
domAnchorSchema,
} from "@/normalizationSchemas";
import { MergeDeep } from "type-fest";

Expand Down Expand Up @@ -91,6 +92,7 @@ export const initialState = {
appearances: {} as SchemaEntityState<typeof appearanceSchema>,
contextTrailItems: {} as SchemaEntityState<typeof contextTrailItemSchema>,
domains: {} as SchemaEntityState<typeof domainSchema>,
domAnchors: {} as SchemaEntityState<typeof domAnchorSchema>,
justifications: {} as SchemaEntityState<
typeof justificationSchema,
{
Expand Down Expand Up @@ -174,7 +176,7 @@ const slice = createSlice({
return;
}
const payload = action.payload as unknown as ApiErrorPayload;
// If a proposition is not found (e.g., another user deleted it), then remove it.
// If a statement is not found (e.g., another user deleted it), then remove it.
if (payload.httpStatusCode === httpStatusCodes.NOT_FOUND) {
const { rootTargetId } = action.meta.requestMeta;
delete state.statements[rootTargetId];
Expand Down Expand Up @@ -548,8 +550,8 @@ export function deepMerge<T1, T2>(x: Partial<T1>, y: Partial<T2>): T1 & T2 {
}

export const deepMergeOptions: DeepMergeOptions = {
// Overwrite arrays. This prevents us from merging arrays of objects, but since we store our
// entities normalized, most objects we care about merging will be top-level entities.
// Union arrays so that we deduplicate related entity IDs. (The default behavior is to
// concatenate arrays elements.)
arrayMerge: (targetArray, sourceArray, _options) =>
union(targetArray, sourceArray),
// Don't copy the properties of Moment objects (or else we lose their methods.)
Expand Down
21 changes: 21 additions & 0 deletions premiser-ui/src/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,24 @@ export function toCompatibleTagVotes(
} as TagVote)
);
}

/**
* Hashes a string into a number.
*
* https://github.com/bryc/code/blob/da36a3e07acfbd07f930a9212a2df9e854ff56e4/jshash/experimental/cyrb53.js
*/
export function hashString(str: string, seed = 0) {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);

return 4294967296 * (2097151 & h2) + (h1 >>> 0);
}
Loading