Skip to content

Commit 056344e

Browse files
committed
Refactor Server to utilize a single instance of EntityAttributeProcessor
1 parent 4139d58 commit 056344e

1 file changed

Lines changed: 20 additions & 35 deletions

File tree

opengin/core-api/cmd/server/service.go

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import (
2727
// Server implements the COREService
2828
type Server struct {
2929
pb.UnimplementedCOREServiceServer
30-
mongoRepo *mongorepository.MongoRepository
31-
neo4jRepo *neo4jrepository.Neo4jRepository
32-
postgresRepo *postgres.PostgresRepository
30+
mongoRepo *mongorepository.MongoRepository
31+
neo4jRepo *neo4jrepository.Neo4jRepository
32+
postgresRepo *postgres.PostgresRepository
33+
attributeProcessor *engine.EntityAttributeProcessor
3334
}
3435

3536
// CreateEntity handles entity creation with relationships, metadata and attributes
@@ -66,15 +67,7 @@ func (s *Server) CreateEntity(ctx context.Context, req *pb.Entity) (*pb.Entity,
6667
}
6768

6869
// Handle attributes
69-
processor, err := engine.NewEntityAttributeProcessor(engine.ProcessorDependencies{
70-
PostgresRepo: s.postgresRepo,
71-
Neo4jRepo: s.neo4jRepo,
72-
MongoRepo: s.mongoRepo,
73-
})
74-
if err != nil {
75-
return nil, fmt.Errorf("failed to initialize attribute processor: %v", err)
76-
}
77-
attributeResults := processor.ProcessEntityAttributes(ctx, req, "create", nil)
70+
attributeResults := s.attributeProcessor.ProcessEntityAttributes(ctx, req, "create", nil)
7871

7972
// Check if any attributes failed
8073
for attrName, result := range attributeResults {
@@ -179,16 +172,6 @@ func (s *Server) ReadEntity(ctx context.Context, req *pb.ReadEntityRequest) (*pb
179172

180173
log.Printf("[server.ReadEntity] Processing attributes for entity: %s, attributes: %+v", req.Entity.Id, req.Entity.Attributes)
181174

182-
// Use the EntityAttributeProcessor to read and process attributes
183-
processor, err := engine.NewEntityAttributeProcessor(engine.ProcessorDependencies{
184-
PostgresRepo: s.postgresRepo,
185-
Neo4jRepo: s.neo4jRepo,
186-
MongoRepo: s.mongoRepo,
187-
})
188-
if err != nil {
189-
return nil, fmt.Errorf("failed to initialize attribute processor: %v", err)
190-
}
191-
192175
// Extract fields and record filters from the request attributes based on storage type
193176
fields, recordFilters := extractFieldsFromAttributes(req.Entity.Attributes)
194177
log.Printf("Extracted fields from attributes: %v", fields)
@@ -202,7 +185,7 @@ func (s *Server) ReadEntity(ctx context.Context, req *pb.ReadEntityRequest) (*pb
202185
readOptions := engine.NewReadOptions(filtersMap, fields...)
203186

204187
// Process the entity with attributes to get the results map
205-
attributeResults := processor.ProcessEntityAttributes(ctx, req.Entity, "read", readOptions)
188+
attributeResults := s.attributeProcessor.ProcessEntityAttributes(ctx, req.Entity, "read", readOptions)
206189

207190
log.Printf("[server.ReadEntity] Successfully processed attributes for entity: %s, results: %+v", req.Entity.Id, attributeResults)
208191

@@ -277,19 +260,11 @@ func (s *Server) UpdateEntity(ctx context.Context, req *pb.UpdateEntityRequest)
277260
}
278261

279262
// Handle attributes
280-
processor, err := engine.NewEntityAttributeProcessor(engine.ProcessorDependencies{
281-
PostgresRepo: s.postgresRepo,
282-
Neo4jRepo: s.neo4jRepo,
283-
MongoRepo: s.mongoRepo,
284-
})
285-
if err != nil {
286-
return nil, fmt.Errorf("failed to initialize attribute processor: %v", err)
287-
}
288263
// Note that in the perspective of the attribute this is a creation operation
289264
// The entity is already there but here the attribute is set later.
290265
// There is no alignment of update operation with the attribute.
291266
// TODO: https://github.com/LDFLK/nexoan/issues/286
292-
attributeResults := processor.ProcessEntityAttributes(ctx, req.Entity, "create", nil)
267+
attributeResults := s.attributeProcessor.ProcessEntityAttributes(ctx, req.Entity, "create", nil)
293268

294269
// Check if any attributes failed
295270
for attrName, result := range attributeResults {
@@ -641,16 +616,26 @@ func main() {
641616
}
642617
defer postgresRepo.Close()
643618

619+
attributeProcessor, err := engine.NewEntityAttributeProcessor(engine.ProcessorDependencies{
620+
PostgresRepo: postgresRepo,
621+
Neo4jRepo: neo4jRepo,
622+
MongoRepo: mongoRepo,
623+
})
624+
if err != nil {
625+
log.Fatalf("[service.main] Failed to create attribute processor: %v", err)
626+
}
627+
644628
listener, err := net.Listen("tcp", host+":"+port)
645629
if err != nil {
646630
log.Fatalf("[service.main] Failed to listen: %v", err)
647631
}
648632

649633
grpcServer := grpc.NewServer()
650634
server := &Server{
651-
mongoRepo: mongoRepo,
652-
neo4jRepo: neo4jRepo,
653-
postgresRepo: postgresRepo,
635+
mongoRepo: mongoRepo,
636+
neo4jRepo: neo4jRepo,
637+
postgresRepo: postgresRepo,
638+
attributeProcessor: attributeProcessor,
654639
}
655640

656641
pb.RegisterCOREServiceServer(grpcServer, server)

0 commit comments

Comments
 (0)