Suggestions with implemention [closed]
I am working on the implementation where I am creating football games on a schedule. The web application that I am building is for pickup games where players can view the available games and register for available ones. For that purpose, I have implemented as follows, // 1 2 3 4 5 6 7 //...:....|....:....|....:....|....:....|....:....|....:....|....:....|....:. @Transactional private void createNewGames(LocalDate localDate, Games games){ List allVenues = venueRepository.findAll(); // Prepare to collect all games to save in batch List gamesToSave = new ArrayList(); DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("H:mm"); for(Venue venue: allVenues){ HashMap timeSlots = venue.getTimeSlots(); if(timeSlots!=null){ Map.Entry entry; for (entry : timeSlots.entrySet()) { // Access the key String day_ = entry.getKey().toString(); // Access the value List availableTimes = entry.getValue(); for(String time: availableTimes){ try { LocalTime time_ = LocalTime.parse( time, timeFormatter ); LocalDateTime dateTime = localDate.with( java.time.DayOfWeek.valueOf(day_) ).atTime(time_); log.info("Local Date Time: {}", dateTime); gamesToSave.add( Games .builder() .localDateTime(dateTime) .isAvailable(true) .venue(venue) .build() ) ; }catch(Exception ex){ String timeSlotMsg = String.format( "Failed to create game for time slot = %s", time ); String exceptionMsg = String.format( "with exception = %s", ex.getLocalizedMessage() ); log.error("{} {}", timeSlotMsg, exceptionMsg); } } } } } // Save all games in batch after processing all venues and time slots if (!gamesToSave.isEmpty()) { // Assuming saveAll supports batch saving gameRepository.saveAll(gamesToSave); } } So as per my understanding this implementation has ON^3 time complexity which I believe in software engineering is not a good practice. But on the second thought, this method is triggered on a schedule every midnight. So user does not directly interact with this method. However I just wanted to have some idea whether I should leave this implementation as it is right now or try to improve the time complexity of this implementation by possibly redesigning the database. The game entity class currently looks like this, public class Games implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private LocalDateTime localDateTime; @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) @JoinTable( name = "game_users", joinColumns = @JoinColumn(name = "game_id"), inverseJoinColumns = @JoinColumn(name = "user_id") ) private List users; // Many games can be associated with one venue @ManyToOne(fetch = FetchType.LAZY) // Creates a foreign key column in the "games" table @JoinColumn(name = "venue_id", nullable = false) private Venue venue; private boolean isAvailable; } Please let me know if I have been clear in my questioning. To summarize I just want to understand the best practices in the industry and possibly any suggestions on improving my current implementations.
I am working on the implementation where I am creating football games on a schedule. The web application that I am building is for pickup games where players can view the available games and register for available ones. For that purpose, I have implemented as follows,
// 1 2 3 4 5 6 7
//...:....|....:....|....:....|....:....|....:....|....:....|....:....|....:.
@Transactional
private void createNewGames(LocalDate localDate, Games games){
List allVenues = venueRepository.findAll();
// Prepare to collect all games to save in batch
List gamesToSave = new ArrayList<>();
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("H:mm");
for(Venue venue: allVenues){
HashMap> timeSlots = venue.getTimeSlots();
if(timeSlots!=null){
Map.Entry> entry;
for (entry : timeSlots.entrySet()) {
// Access the key
String day_ = entry.getKey().toString();
// Access the value
List availableTimes = entry.getValue();
for(String time: availableTimes){
try {
LocalTime time_ = LocalTime.parse(
time,
timeFormatter
);
LocalDateTime dateTime = localDate.with(
java.time.DayOfWeek.valueOf(day_)
).atTime(time_);
log.info("Local Date Time: {}", dateTime);
gamesToSave.add(
Games
.builder()
.localDateTime(dateTime)
.isAvailable(true)
.venue(venue)
.build()
)
;
}catch(Exception ex){
String timeSlotMsg = String.format(
"Failed to create game for time slot = %s", time
);
String exceptionMsg = String.format(
"with exception = %s", ex.getLocalizedMessage()
);
log.error("{} {}", timeSlotMsg, exceptionMsg);
}
}
}
}
}
// Save all games in batch after processing all venues and time slots
if (!gamesToSave.isEmpty()) {
// Assuming saveAll supports batch saving
gameRepository.saveAll(gamesToSave);
}
}
So as per my understanding this implementation has ON^3 time complexity which I believe in software engineering is not a good practice. But on the second thought, this method is triggered on a schedule every midnight. So user does not directly interact with this method. However I just wanted to have some idea whether I should leave this implementation as it is right now or try to improve the time complexity of this implementation by possibly redesigning the database.
The game entity class currently looks like this,
public class Games implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private LocalDateTime localDateTime;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(
name = "game_users",
joinColumns = @JoinColumn(name = "game_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private List users;
// Many games can be associated with one venue
@ManyToOne(fetch = FetchType.LAZY)
// Creates a foreign key column in the "games" table
@JoinColumn(name = "venue_id", nullable = false)
private Venue venue;
private boolean isAvailable;
}
Please let me know if I have been clear in my questioning. To summarize I just want to understand the best practices in the industry and possibly any suggestions on improving my current implementations.