chore: hook temporal persistence to dht
This commit is contained in:
@@ -534,8 +534,40 @@ func (pm *persistenceManagerImpl) loadFromLocalStorage(ctx context.Context) erro
|
||||
}
|
||||
|
||||
func (pm *persistenceManagerImpl) loadFromDistributedStorage(ctx context.Context) error {
|
||||
// Similar to local storage but using distributed store
|
||||
// Implementation would be similar to loadFromLocalStorage
|
||||
if pm.distributedStore == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := pm.distributedStore.Retrieve(ctx, pm.generateGraphKey())
|
||||
if err != nil {
|
||||
// No remote snapshot yet
|
||||
return nil
|
||||
}
|
||||
|
||||
var snapshot GraphSnapshot
|
||||
switch raw := data.(type) {
|
||||
case []byte:
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal(raw, &snapshot); err != nil {
|
||||
return fmt.Errorf("failed to decode distributed snapshot: %w", err)
|
||||
}
|
||||
case json.RawMessage:
|
||||
if err := json.Unmarshal(raw, &snapshot); err != nil {
|
||||
return fmt.Errorf("failed to decode distributed snapshot: %w", err)
|
||||
}
|
||||
default:
|
||||
encoded, marshalErr := json.Marshal(raw)
|
||||
if marshalErr != nil {
|
||||
return fmt.Errorf("failed to marshal distributed snapshot payload: %w", marshalErr)
|
||||
}
|
||||
if err := json.Unmarshal(encoded, &snapshot); err != nil {
|
||||
return fmt.Errorf("failed to decode distributed snapshot: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
pm.applySnapshot(&snapshot)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -588,6 +620,51 @@ func (pm *persistenceManagerImpl) createGraphSnapshot() (*GraphSnapshot, error)
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (pm *persistenceManagerImpl) applySnapshot(snapshot *GraphSnapshot) {
|
||||
if snapshot == nil {
|
||||
return
|
||||
}
|
||||
|
||||
pm.graph.mu.Lock()
|
||||
defer pm.graph.mu.Unlock()
|
||||
|
||||
pm.graph.nodes = make(map[string]*TemporalNode, len(snapshot.Nodes))
|
||||
pm.graph.addressToNodes = make(map[string][]*TemporalNode, len(snapshot.Nodes))
|
||||
pm.graph.influences = make(map[string][]string, len(snapshot.Influences))
|
||||
pm.graph.influencedBy = make(map[string][]string, len(snapshot.InfluencedBy))
|
||||
pm.graph.decisions = make(map[string]*DecisionMetadata, len(snapshot.Decisions))
|
||||
pm.graph.decisionToNodes = make(map[string][]*TemporalNode)
|
||||
pm.graph.pathCache = make(map[string][]*DecisionStep)
|
||||
pm.graph.metricsCache = make(map[string]interface{})
|
||||
|
||||
for id, node := range snapshot.Nodes {
|
||||
pm.graph.nodes[id] = node
|
||||
|
||||
addressKey := node.UCXLAddress.String()
|
||||
pm.graph.addressToNodes[addressKey] = append(pm.graph.addressToNodes[addressKey], node)
|
||||
|
||||
if influences, ok := snapshot.Influences[id]; ok {
|
||||
pm.graph.influences[id] = append([]string(nil), influences...)
|
||||
} else {
|
||||
pm.graph.influences[id] = make([]string, 0)
|
||||
}
|
||||
|
||||
if influencedBy, ok := snapshot.InfluencedBy[id]; ok {
|
||||
pm.graph.influencedBy[id] = append([]string(nil), influencedBy...)
|
||||
} else {
|
||||
pm.graph.influencedBy[id] = make([]string, 0)
|
||||
}
|
||||
|
||||
if node.DecisionID != "" {
|
||||
pm.graph.decisionToNodes[node.DecisionID] = append(pm.graph.decisionToNodes[node.DecisionID], node)
|
||||
}
|
||||
}
|
||||
|
||||
for id, decision := range snapshot.Decisions {
|
||||
pm.graph.decisions[id] = decision
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *persistenceManagerImpl) getRemoteSnapshot(ctx context.Context) (*GraphSnapshot, error) {
|
||||
key := pm.generateGraphKey()
|
||||
|
||||
@@ -596,12 +673,27 @@ func (pm *persistenceManagerImpl) getRemoteSnapshot(ctx context.Context) (*Graph
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var snapshot *GraphSnapshot
|
||||
if err := json.Unmarshal(data.([]byte), &snapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal remote snapshot: %w", err)
|
||||
var snapshot GraphSnapshot
|
||||
switch raw := data.(type) {
|
||||
case []byte:
|
||||
if err := json.Unmarshal(raw, &snapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal remote snapshot: %w", err)
|
||||
}
|
||||
case json.RawMessage:
|
||||
if err := json.Unmarshal(raw, &snapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal remote snapshot: %w", err)
|
||||
}
|
||||
default:
|
||||
encoded, marshalErr := json.Marshal(raw)
|
||||
if marshalErr != nil {
|
||||
return nil, fmt.Errorf("failed to marshal remote snapshot payload: %w", marshalErr)
|
||||
}
|
||||
if err := json.Unmarshal(encoded, &snapshot); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal remote snapshot: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return snapshot, nil
|
||||
return &snapshot, nil
|
||||
}
|
||||
|
||||
func (pm *persistenceManagerImpl) performBidirectionalSync(ctx context.Context, local, remote *GraphSnapshot, result *SyncResult) error {
|
||||
|
||||
Reference in New Issue
Block a user