- Notifications
You must be signed in to change notification settings - Fork 0
Refactor to recursive hash tree structure#18
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
Merged
toothlessdev
merged 4 commits into
develop
from
17-refactor-to-recursive-hash-tree-structureJan 22, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from "./partition"; |
52 changes: 37 additions & 15 deletions
52 packages/patchlogr-core/src/partition/__tests__/partitionByMethod.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 45 additions & 21 deletions
66 packages/patchlogr-core/src/partition/__tests__/partitionByTag.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export type { Hash, HashNode, PartitionedSpec } from "./partition"; | ||
| export { partitionByMethod } from "./partitionByMethod"; | ||
| export { partitionByTag } from "./partitionByTag"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,24 @@ | ||
| export type Partition = { | ||
| hash: string; | ||
| operationKey: string; | ||
| export type Hash = string; | ||
| export type HashInternalNode<K = string, V = unknown> = { | ||
| type: "node"; | ||
| key: K; | ||
| hash: Hash; | ||
| children: HashNode<K, V>[]; | ||
| }; | ||
| export type HashLeafNode<K = string, V = unknown> = { | ||
| type: "leaf"; | ||
| key: K; | ||
| hash: Hash; | ||
| value: V; | ||
| }; | ||
| export type PartitionedSpec = { | ||
| hash: string; | ||
| export type HashNode<K = string, V = unknown> = | ||
| | HashInternalNode<K, V> | ||
| | HashLeafNode<K, V>; | ||
| export type PartitionedSpec<K, V = unknown> = { | ||
| root: HashNode<K, V>; | ||
| metadata: Record<string, unknown>; | ||
| partitions: Map<string, Partition[]>; | ||
| }; |
65 changes: 54 additions & 11 deletions
65 packages/patchlogr-core/src/partition/partitionByMethod.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,70 @@ | ||
| import type { CanonicalSpec, HTTPMethod } from "@patchlogr/types"; | ||
| import type { Partition, PartitionedSpec } from "./partition"; | ||
| import type { | ||
| CanonicalSpec, | ||
| HTTPMethod, | ||
| CanonicalOperation, | ||
| } from "@patchlogr/types"; | ||
| import type { PartitionedSpec, HashNode } from "./partition"; | ||
| import { createSHA256Hash } from "../utils/createHash"; | ||
| import { stableStringify } from "../utils/stableStringify"; | ||
| export function partitionByMethod(spec: CanonicalSpec): PartitionedSpec { | ||
| const partitions = new Map<HTTPMethod, Partition[]>(); | ||
| export function partitionByMethod( | ||
| spec: CanonicalSpec, | ||
| ): PartitionedSpec<string, CanonicalOperation> { | ||
| const methodGroups = new Map< | ||
| HTTPMethod, | ||
| Array<{ key: string; operation: CanonicalOperation }> | ||
| >(); | ||
| Object.entries(spec.operations).forEach(([key, operation]) => { | ||
| const hash = createSHA256Hash(stableStringify(operation)); | ||
| if (!methodGroups.has(operation.method)) { | ||
| methodGroups.set(operation.method, []); | ||
| } | ||
| methodGroups.get(operation.method)?.push({ | ||
| key: key, | ||
| operation, | ||
| }); | ||
| }); | ||
| const methodNodes: HashNode<string, CanonicalOperation>[] = []; | ||
| methodGroups.forEach((operations, method) => { | ||
| const operationLeaves: HashNode<string, CanonicalOperation>[] = | ||
| operations.map(({ key, operation }) => ({ | ||
| type: "leaf", | ||
| key, | ||
| hash: createSHA256Hash(stableStringify(operation)), | ||
| value: operation, | ||
| })); | ||
| const methodHash = createSHA256Hash( | ||
| stableStringify(operationLeaves.map((leaf) => leaf.hash)), | ||
| ); | ||
| if (!partitions.has(operation.method)) | ||
| partitions.set(operation.method, [{ hash, operationKey: key }]); | ||
| else | ||
| partitions.get(operation.method)?.push({ hash, operationKey: key }); | ||
| methodNodes.push({ | ||
| type: "node", | ||
| key: method, | ||
| hash: methodHash, | ||
| children: operationLeaves, | ||
| }); | ||
| }); | ||
| const rootHash = createSHA256Hash( | ||
| stableStringify(methodNodes.map((node) => node.hash)), | ||
| ); | ||
| const root: HashNode<string, CanonicalOperation> = { | ||
| type: "node", | ||
| key: "root", | ||
| hash: rootHash, | ||
| children: methodNodes, | ||
| }; | ||
| return { | ||
| hash: createSHA256Hash(stableStringify(spec)), | ||
| root, | ||
| metadata: { | ||
| ...spec.info, | ||
| ...spec.security, | ||
| }, | ||
| partitions, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,74 @@ | ||
| import type { CanonicalSpec } from "@patchlogr/types"; | ||
| import type { Partition, PartitionedSpec } from "./partition"; | ||
| import type { | ||
| CanonicalSpec, | ||
| CanonicalOperation, | ||
| OperationKey, | ||
| } from "@patchlogr/types"; | ||
| import type { PartitionedSpec, HashNode } from "./partition"; | ||
| import { createSHA256Hash } from "../utils/createHash"; | ||
| import { stableStringify } from "../utils/stableStringify"; | ||
| export const DEFAULT_TAG = "__DEFAULT__"; | ||
| export function partitionByTag(spec: CanonicalSpec): PartitionedSpec { | ||
| const partitions = new Map<string, Partition[]>(); | ||
| export function partitionByTag( | ||
| spec: CanonicalSpec, | ||
| ): PartitionedSpec<string, CanonicalOperation> { | ||
| const tagGroups = new Map< | ||
| string, | ||
| Array<{ key: string; operation: CanonicalOperation }> | ||
| >(); | ||
| Object.entries(spec.operations).forEach(([key, operation]) => { | ||
| const tag = operation.doc?.tags?.[0] || DEFAULT_TAG; | ||
| const hash = createSHA256Hash(stableStringify(operation)); | ||
| if (!partitions.has(tag)) | ||
| partitions.set(tag, [{ hash, operationKey: key }]); | ||
| else partitions.get(tag)?.push({ hash, operationKey: key }); | ||
| if (!tagGroups.has(tag)) { | ||
| tagGroups.set(tag, []); | ||
| } | ||
| tagGroups.get(tag)?.push({ | ||
| key: key as OperationKey, | ||
| operation, | ||
| }); | ||
toothlessdev marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }); | ||
| const tagNodes: HashNode<string, CanonicalOperation>[] = []; | ||
| tagGroups.forEach((operations, tag) => { | ||
| const operationLeaves: HashNode<string, CanonicalOperation>[] = | ||
| operations.map(({ key, operation }) => ({ | ||
| type: "leaf", | ||
| key, | ||
| hash: createSHA256Hash(stableStringify(operation)), | ||
| value: operation, | ||
| })); | ||
| const tagHash = createSHA256Hash( | ||
| stableStringify(operationLeaves.map((leaf) => leaf.hash)), | ||
| ); | ||
| tagNodes.push({ | ||
| type: "node", | ||
| key: tag, | ||
| hash: tagHash, | ||
| children: operationLeaves, | ||
| }); | ||
| }); | ||
| const rootHash = createSHA256Hash( | ||
| stableStringify(tagNodes.map((node) => node.hash)), | ||
| ); | ||
| const root: HashNode<string, CanonicalOperation> = { | ||
| type: "node", | ||
| key: "root", | ||
| hash: rootHash, | ||
| children: tagNodes, | ||
| }; | ||
| return { | ||
| hash: createSHA256Hash(stableStringify(spec)), | ||
| root, | ||
| metadata: { | ||
| ...spec.info, | ||
| ...spec.security, | ||
| }, | ||
| partitions, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.