perf(snapshot): capture full target states in correlated native read

This commit is contained in:
阿白叔 committed 2026-10-19 14:34:57 +08:00
1 parent 8723076cb6
commit ca8ac2a5d2
4 files changed
+276 -22

No files matched your search

@@ -1,9 +1,17 @@
' Read-only native-host adapter. Appended to the external worker at preparation.
Function QPNativeReadUntouched(ByRef SelectedUids)
Set QPNativeReadUntouched = QPNativeRead(SelectedUids, False)
End Function
Function QPNativeCapture(ByRef SelectedUids)
Set QPNativeCapture = QPNativeRead(SelectedUids, True)
End Function
Function QPNativeRead(ByRef SelectedUids, ByVal CaptureSelected)
Dim Token, Base, ScriptPath, ResultPath, Stream, CommandPath, Text
Dim Project, Sheet, Guid, Safe, Result
Set QPNativeReadUntouched = Nothing
Set QPNativeRead = Nothing
Project = QPProjectPath()
Sheet = QPSheetIdentity()
Guid = CStr(QP_App.Guid())
@@ -12,8 +20,8 @@ Function QPNativeReadUntouched(ByRef SelectedUids)
ScriptPath = Base & ".vbs"
ResultPath = Base & ".tsv"
Safe = Not CBool(QP_NativeMutationPossible) And Not CBool(QP_RestoreTransactionOpen) And Not CBool(QP_NativeCallActive)
Text = QPBuildNativeSnapshotScript(QPReadUtf8(WScript.ScriptFullName), SelectedUids, _
Token, Project, Sheet, Guid, ResultPath, CStr(QP_AttemptToken), Safe)
Text = QPBuildNativeReadScript(QPReadUtf8(WScript.ScriptFullName), SelectedUids, _
Token, Project, Sheet, Guid, ResultPath, CStr(QP_AttemptToken), Safe, CaptureSelected)
' Generated source is ASCII; Unicode values are escaped with QPEncode.
Set Stream = QP_Fso.CreateTextFile(ScriptPath, False, False)
Stream.Write Text
@@ -28,12 +36,16 @@ Function QPNativeReadUntouched(ByRef SelectedUids)
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script command failed"
If Not QP_Fso.FileExists(ResultPath) Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script returned no complete result"
Set Result = QPParseNativeSnapshot(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
If CaptureSelected Then
Set Result = QPParseNativeCapture(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
Else
Set Result = QPParseNativeSnapshot(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
End If
If CStr(QP_App.Guid()) <> Guid Or QPProjectPath() <> Project Or QPSheetIdentity() <> Sheet Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "context changed after native read"
QPProgress "native_snapshot_read", Result("total"), Result("total")
QPLog QP_Root, "INFO", "native_snapshot_verified components=" & CStr(Result("total")) & " token=" & Token
Set QPNativeReadUntouched = Result
Set QPNativeRead = Result
End Function
Function QPNativeCommandPathSupported(ByVal Path)
@@ -46,6 +58,61 @@ Function QPNativeCommandPathSupported(ByVal Path)
QPNativeCommandPathSupported = (Len(Path) > 0)
End Function
Function QPParseNativeCapture(ByVal Text, ByVal Token, ByVal Project, ByVal Sheet, ByRef SelectedUids)
Dim Lines, Index, Fields, State, Uid, Targets, BaseText, SeenEnd, Result, Last
If Left(Text, 6) = "ERROR" & vbTab Then
Set QPParseNativeCapture = QPParseNativeSnapshot(Text, Token, Project, Sheet, SelectedUids)
Exit Function
End If
Lines = Split(Text, vbCrLf)
Fields = Split(Lines(0), vbTab)
If Fields(0) <> "NR2" Then QPNativeReadInvalid "missing selected capture header"
Lines(0) = "NR1" & Mid(Lines(0), 4)
Set Targets = CreateObject("Scripting.Dictionary")
BaseText = Lines(0) & vbCrLf
SeenEnd = False
Last = UBound(Lines)
If Lines(Last) = "" Then Last = Last - 1
For Index = 1 To Last
Fields = Split(Lines(Index), vbTab)
If Fields(0) = "T" Then
If SeenEnd Or UBound(Fields) <> 2 Then QPNativeReadInvalid "invalid selected capture row"
Uid = QPDecode(Fields(1))
If Not SelectedUids.Exists(Uid) Or Targets.Exists(Uid) Then _
QPNativeReadInvalid "duplicate or foreign selected capture UID"
State = QPDecode(Fields(2))
QPValidateNativeSelectedState State, Uid
Targets.Add Uid, State
Else
If Fields(0) = "END" Then SeenEnd = True
BaseText = BaseText & Lines(Index) & vbCrLf
End If
Next
If Targets.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected capture missing target"
Set Result = QPParseNativeSnapshot(BaseText, Token, Project, Sheet, SelectedUids)
Result.Add "selected", Targets
Set QPParseNativeCapture = Result
End Function
Sub QPValidateNativeSelectedState(ByVal State, ByVal Uid)
Dim Lines, Fields, Index, Names, Name
If Right(State, 2) <> vbCrLf Then QPNativeReadInvalid "truncated selected component state"
Lines = Split(State, vbCrLf)
If UBound(Lines) < 2 Then QPNativeReadInvalid "selected component has no attributes"
Fields = Split(Lines(0), vbTab)
If UBound(Fields) <> 7 Then QPNativeReadInvalid "invalid selected component fields"
If Fields(0) <> "C" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "selected component owner mismatch"
Set Names = CreateObject("Scripting.Dictionary")
For Index = 1 To UBound(Lines) - 1
Fields = Split(Lines(Index), vbTab)
If UBound(Fields) <> 11 Then QPNativeReadInvalid "invalid selected attribute fields"
If Fields(0) <> "A" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "selected attribute owner mismatch"
Name = QPDecode(Fields(2))
If Len(Name) = 0 Or Names.Exists(Name) Then QPNativeReadInvalid "duplicate or empty selected attribute name"
Names.Add Name, True
Next
End Sub
Function QPTryNativeVerifyUntouched(ByRef Records)
Dim Selected, Record, Index, Actual
QPTryNativeVerifyUntouched = False
@@ -140,6 +207,10 @@ Sub QPNativeReadInvalid(ByVal Description)
End Sub
Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid)
QPNativeCollect = QPNativeCollectMode(SelectedUids, Token, Project, Sheet, Guid, False)
End Function
Function QPNativeCollectMode(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal CaptureSelected)
Dim Components, Comp, Uid, Seen, Found, Text, Count
QPNativeRequireContext Project, Sheet, Guid
QPInvalidatePinIndex
@@ -147,6 +218,7 @@ Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal S
Set Seen = CreateObject("Scripting.Dictionary")
Set Found = CreateObject("Scripting.Dictionary")
Text = "NR1" & vbTab & Token & vbTab & QPEncode(Project) & vbTab & QPEncode(Sheet) & vbCrLf
If CaptureSelected Then Text = "NR2" & Mid(Text, 4)
Count = 0
QPProgress "native_snapshot_read", 0, Components.Count
For Each Comp In Components
@@ -155,6 +227,7 @@ Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal S
Seen.Add Uid, True
If SelectedUids.Exists(Uid) Then
Found.Add Uid, True
If CaptureSelected Then Text = Text & "T" & vbTab & QPEncode(Uid) & vbTab & QPEncode(QPSnapshotComponent(Comp)) & vbCrLf
Else
Text = Text & QPUntouchedComponent(Comp)
End If
@@ -163,7 +236,7 @@ Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal S
Next
If Found.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected component missing from native read"
QPNativeRequireContext Project, Sheet, Guid
QPNativeCollect = Text & "END" & vbTab & Token & vbTab & CStr(Count) & vbCrLf
QPNativeCollectMode = Text & "END" & vbTab & Token & vbTab & CStr(Count) & vbCrLf
End Function
Sub QPNativeRequireContext(ByVal Project, ByVal Sheet, ByVal Guid)
@@ -172,6 +245,10 @@ Sub QPNativeRequireContext(ByVal Project, ByVal Sheet, ByVal Guid)
End Sub
Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe)
QPBuildNativeSnapshotScript = QPBuildNativeReadScript(Source, SelectedUids, Token, Project, Sheet, Guid, ResultPath, StopToken, Safe, False)
End Function
Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe, ByVal CaptureSelected)
Dim Header, Body, Names, Name, Uid, Pattern, Matches, MonitorJob
Header = Left(Source, InStr(Source, "Set QP_Fso =") - 1)
Set Pattern = CreateObject("VBScript.RegExp")
@@ -197,14 +274,14 @@ Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Tok
Next
Body = Body & "On Error Resume Next" & vbCrLf & _
"Set QP_App=GetObject(,""Viewdraw.Application"")" & vbCrLf & _
"If Err.Number=0 Then QP_NR_Text=QPNativeCollect(QP_NR_Selected," & _
QPNativeExpression(Token) & "," & QPNativeExpression(Project) & "," & QPNativeExpression(Sheet) & "," & QPNativeExpression(Guid) & ")" & vbCrLf & _
"If Err.Number=0 Then QP_NR_Text=QPNativeCollectMode(QP_NR_Selected," & _
QPNativeExpression(Token) & "," & QPNativeExpression(Project) & "," & QPNativeExpression(Sheet) & "," & QPNativeExpression(Guid) & "," & CStr(CBool(CaptureSelected)) & ")" & vbCrLf & _
"QP_NR_Code=Err.Number: QP_NR_Source=Err.Source: QP_NR_Description=Err.Description" & vbCrLf & _
"Err.Clear" & vbCrLf & _
"If QP_NR_Code<>0 Then QP_NR_Text=""ERROR"" & vbTab & " & QPNativeExpression(Token) & _
" & vbTab & CStr(QP_NR_Code) & vbTab & QPEncode(QP_NR_Source) & vbTab & QPEncode(QP_NR_Description) & vbCrLf" & vbCrLf & _
"QPWriteUtf8 " & QPNativeExpression(ResultPath) & ",QP_NR_Text" & vbCrLf & "On Error GoTo 0" & vbCrLf
Names = Split("QPNativeCollect QPNativeRequireContext QPNativeReadInvalid QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Pattern.Global = False
For Each Name In Names
Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*"
@@ -234,7 +311,7 @@ Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Tok
" CStr(Done) & vbTab & CStr(Total) & vbTab & CStr(SafeFlag)" & vbCrLf & _
" Stream.Close:Err.Clear" & vbCrLf & " On Error GoTo 0" & vbCrLf & _
" QP_NR_LastProgress=CurrentTime" & vbCrLf & "End Sub" & vbCrLf
QPBuildNativeSnapshotScript = Body
QPBuildNativeReadScript = Body
End Function
Function QPNativeExpression(ByVal Value)
+33 -12
View File
@@ -1174,15 +1174,6 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
If Not Selectors Is Nothing Then _
Snapshot = Snapshot & vbTab & QPEncode("targets") & vbTab & CStr(Command("target_selectors"))
Snapshot = Snapshot & vbCrLf
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
QPLog QP_Root, "INFO", "stage=snapshot_component uid=" & QPLogField(CStr(Comp.UID))
Snapshot = Snapshot & QPSnapshotComponent(Comp)
QPProgress "capture", Index + 1, Matches.Count
Next
' U remains the seven-field native-helper identity baseline. X/G extend it
' with complete unselected state for every scope, including all-match jobs.
Snapshot = Snapshot & "X" & vbTab & "untouched_full_v1" & vbCrLf
Set MatchedUids = CreateObject("Scripting.Dictionary")
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
@@ -1191,8 +1182,27 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Set NativeRead = Nothing
If QPUseNativeSnapshot() Then
QPRequireProject Command
Set NativeRead = QPNativeReadUntouched(MatchedUids)
Set NativeRead = QPNativeCapture(MatchedUids)
If Not NativeRead Is Nothing Then
If NativeRead("total") <> Components.Count Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "component count changed during capture"
End If
End If
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
Uid = CStr(Comp.UID)
QPLog QP_Root, "INFO", "stage=snapshot_component uid=" & QPLogField(Uid)
If NativeRead Is Nothing Then
Snapshot = Snapshot & QPSnapshotComponent(Comp)
Else
QPRequireNativeCapturedTarget CStr(NativeRead("selected")(Uid)), Command, Uid, Selectors
Snapshot = Snapshot & CStr(NativeRead("selected")(Uid))
End If
QPProgress "capture", Index + 1, Matches.Count
Next
' U remains the seven-field native-helper identity baseline. X/G extend it
' with complete unselected state for every scope, including all-match jobs.
Snapshot = Snapshot & "X" & vbTab & "untouched_full_v1" & vbCrLf
UntouchedCount = 0
If NativeRead Is Nothing Then
For Each Comp In Components
@@ -1204,8 +1214,6 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
End If
Next
Else
If NativeRead("total") <> Components.Count Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "component count changed during capture"
Snapshot = Snapshot & CStr(NativeRead("text"))
End If
QPWriteUtf8 SnapshotPath, Snapshot
@@ -1223,6 +1231,19 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
QPLog QP_Root, "INFO", "awaiting_native_replace count=" & Matches.Count
End Sub
Sub QPRequireNativeCapturedTarget(ByVal State, ByRef Command, ByVal Uid, ByRef Selectors)
Dim Fields, Selector
Fields = Split(QPUntouchedIdentity(State), vbTab)
If QPDecode(Fields(1)) <> Uid Or StrComp(QPDecode(Fields(3)), _
CStr(Command("old_part_number")), vbTextCompare) <> 0 Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "selected component changed during native capture"
If Not Selectors Is Nothing Then
Selector = Selectors(Uid)
If QPDecode(Fields(2)) <> CStr(Selector(0)) Or QPDecode(Fields(4)) <> CStr(Selector(1)) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "selected component changed during native capture"
End If
End Sub
Function QPUseNativeSnapshot()
Dim Reader, Number
QPUseNativeSnapshot = False