package store import ( "context" "sort" "time" ) // storeOrder sorts the store: by area (BreakPilot's deliveries last), legal // folder (in the folders' order), title, then id. func storeOrder(out []Document) { area := map[string]int{AreaLegalWeb: 0, AreaProductPublic: 1, AreaInternal: 2, "": 3} kind := map[string]int{} for i, k := range LegalKinds { kind[k] = i } sort.Slice(out, func(i, j int) bool { a, b := out[i], out[j] if area[a.Area] != area[b.Area] { return area[a.Area] < area[b.Area] } if kind[a.LegalKind] != kind[b.LegalKind] { return kind[a.LegalKind] < kind[b.LegalKind] } if a.Title != b.Title { return a.Title < b.Title } return a.ID < b.ID }) } func (m *Memory) ListStoreDocuments(_ context.Context, tenantID string) ([]Document, error) { m.mu.RLock() defer m.mu.RUnlock() out := []Document{} for _, d := range m.documents { if d.TenantID == tenantID && d.Status == DocumentPublished { out = append(out, *d.copy()) } } storeOrder(out) return out, nil } func (m *Memory) GetDocumentByExternalID(_ context.Context, tenantID, externalID string) (*Document, error) { m.mu.RLock() defer m.mu.RUnlock() for _, d := range m.documents { if externalID != "" && d.TenantID == tenantID && d.ExternalID == externalID { return d.copy(), nil } } return nil, ErrNotFound } func (m *Memory) SetDocumentProcessing(_ context.Context, docID, status, errMsg string) (*Document, error) { m.mu.Lock() defer m.mu.Unlock() d, ok := m.documents[docID] if !ok { return nil, ErrNotFound } switch status { case ProcessingNone, ProcessingPending, ProcessingReady, ProcessingFailed: default: return nil, ErrInvalidInput } if len(errMsg) > 500 { errMsg = errMsg[:500] } d.ProcessingStatus, d.ProcessingError, d.UpdatedAt = status, errMsg, time.Now().UTC() return d.copy(), nil } func (m *Memory) SaveDocumentPreparation(_ context.Context, p Preparation) (*Preparation, error) { if !p.HasChunks() && !p.HasConsent() { return nil, ErrInvalidInput } m.mu.Lock() defer m.mu.Unlock() d, ok := m.documents[p.DocumentID] if !ok { return nil, ErrNotFound } cur := m.preparations[p.DocumentID] next := mergePreparation(cur, p) next.DeliveredAt = time.Now().UTC() m.preparations[p.DocumentID] = &next if next.HasChunks() { d.ProcessingStatus, d.ProcessingError = ProcessingReady, "" } out := next out.AssetDigests = append([]string(nil), next.AssetDigests...) return &out, nil } // mergePreparation lays the delivered parts over what is stored: a part that // was not delivered stays as it was. func mergePreparation(cur *Preparation, p Preparation) Preparation { next := p if cur != nil { if !p.HasChunks() { next.Format, next.Revision, next.Pin = cur.Format, cur.Revision, cur.Pin next.ChunkObjectKey, next.ChunkSHA256, next.ChunkBytes, next.ChunkCount = cur.ChunkObjectKey, cur.ChunkSHA256, cur.ChunkBytes, cur.ChunkCount next.AssetDigests = cur.AssetDigests } if !p.HasConsent() { next.ConsentObjectKey, next.ConsentSHA256 = cur.ConsentObjectKey, cur.ConsentSHA256 } } if next.AssetDigests == nil { next.AssetDigests = []string{} } return next } func (m *Memory) GetDocumentPreparation(_ context.Context, docID string) (*Preparation, error) { m.mu.RLock() defer m.mu.RUnlock() p, ok := m.preparations[docID] if !ok { return nil, ErrNotFound } out := *p out.AssetDigests = append([]string(nil), p.AssetDigests...) return &out, nil } func (m *Memory) ListDocumentPreparations(_ context.Context, tenantID string) (map[string]Preparation, error) { m.mu.RLock() defer m.mu.RUnlock() out := map[string]Preparation{} for id, p := range m.preparations { if d, ok := m.documents[id]; ok && d.TenantID == tenantID { cp := *p cp.AssetDigests = append([]string(nil), p.AssetDigests...) out[id] = cp } } return out, nil } func (m *Memory) PutStoreAsset(_ context.Context, a StoreAsset) (*StoreAsset, error) { if !hex64(a.SHA256) || a.ObjectKey == "" { return nil, ErrInvalidInput } m.mu.Lock() defer m.mu.Unlock() if _, ok := m.tenants[a.TenantID]; !ok { return nil, ErrNotFound } key := a.TenantID + "/" + a.SHA256 if cur, ok := m.assets[key]; ok { out := *cur return &out, nil } a.CreatedAt = time.Now().UTC() m.assets[key] = &a out := a return &out, nil } func (m *Memory) GetStoreAsset(_ context.Context, tenantID, sha string) (*StoreAsset, error) { m.mu.RLock() defer m.mu.RUnlock() a, ok := m.assets[tenantID+"/"+sha] if !ok { return nil, ErrNotFound } out := *a return &out, nil } // hex64 reports whether s is a lowercase hex SHA-256. func hex64(s string) bool { if len(s) != 64 { return false } for _, c := range s { if (c < '0' || c > '9') && (c < 'a' || c > 'f') { return false } } return true }