Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,43 @@
import com.mr.domain.playing.entity.enums.MidiType;
import com.mr.domain.playing.exception.PlayingErrorStatus;
import com.mr.global.apipayload.exception.GeneralException;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(
name = "midi_event",
indexes = {
@Index(name = "idx_midi_event_playing_timestamp",
columnList = "playing_id, timestamp_ms")
}
)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MidiEvent {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "midi_event_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "playing_id", nullable = false)
private Playing playing;
public class MidiEventData {

@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private MidiType type;

@Column(name = "pitch", nullable = false)
private Integer pitch;

@Column(name = "velocity", nullable = false)
private Integer velocity;

@Column(name = "timestamp_ms", nullable = false)
private Long timestampMs;

@Builder(access = AccessLevel.PRIVATE)
private MidiEvent(
Playing playing, MidiType type, Integer pitch, Integer velocity, Long timestampMs
private MidiEventData(
MidiType type,
Integer pitch,
Integer velocity,
Long timestampMs
) {
validatePlaying(playing);
validateMidiType(type);
validatePitch(pitch);
validateVelocity(velocity);
Comment on lines +33 to +34

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MidiType을 지금 NOTE_ON, NOTE_OFF만 갖고 있는데 저희 MIDI 데이터를 요 정도만 받아도 되는 건지 궁금해요! (이건 PM님과 논의할 사안인 것 같지만....) 현재는 모든 타입에 pitch와 velocity를 필수로 검증하고 있어, 추후 페달이나 Control Change 이벤트까지 저장한다면 타입별 검증 또는 이벤트 모델 분리가 필요할 것 같아요!!

validateTimestampMs(timestampMs);

this.playing = playing;
this.type = type;
this.pitch = pitch;
this.velocity = velocity;
this.timestampMs = timestampMs;
}

private static void validatePlaying(Playing playing) {
if (playing == null) {
throw new GeneralException(PlayingErrorStatus.MISSING_PLAYING);
}
public static MidiEventData create(
MidiType type,
Integer pitch,
Integer velocity,
Long timestampMs
) {
return new MidiEventData(type, pitch, velocity, timestampMs);
}
Comment thread
ownue marked this conversation as resolved.
Resolved

private static void validateMidiType(MidiType type) {
Expand All @@ -89,16 +65,4 @@ private static void validateTimestampMs(Long timestampMs) {
throw new GeneralException(PlayingErrorStatus.INVALID_TIMESTAMP);
}
}

public static MidiEvent create(
Playing playing, MidiType type, Integer pitch, Integer velocity, Long timestampMs
) {
return MidiEvent.builder()
.playing(playing)
.type(type)
.pitch(pitch)
.velocity(velocity)
.timestampMs(timestampMs)
.build();
}
}
58 changes: 47 additions & 11 deletions src/main/java/com/mr/domain/playing/entity/Playing.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@
import com.mr.domain.playing.exception.PlayingErrorStatus;
import com.mr.global.apipayload.exception.GeneralException;
import com.mr.global.entity.BaseCreatedDeletedEntity;
import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "playing")
Expand Down Expand Up @@ -47,9 +60,6 @@ public class Playing extends BaseCreatedDeletedEntity {
@Column(name = "bpm", nullable = false)
private Integer bpm;

@Column(name = "metronome_enabled", nullable = false)
private boolean metronomeEnabled;

// 실제 연주가 시작된 시간
@Column(name = "started_at")
private LocalDateTime startedAt;
Expand All @@ -67,14 +77,17 @@ public class Playing extends BaseCreatedDeletedEntity {
@Column(name = "is_public", nullable = false)
private boolean isPublic;

@JdbcTypeCode(SqlTypes.JSON)
@Column(name = "midi_data", columnDefinition = "jsonb", nullable = false)
private List<MidiEventData> midiData = new ArrayList<>();

@Builder(access = AccessLevel.PRIVATE)
private Playing(
Long userId,
Long backingTrackId,
PlayingMode mode,
PlayingStatus status,
Integer bpm,
boolean metronomeEnabled,
boolean isPublic
) {

Expand All @@ -86,8 +99,8 @@ private Playing(
this.mode = mode;
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved
this.status = status;
this.bpm = resolveBpm(bpm);
this.metronomeEnabled = metronomeEnabled;
this.isPublic = isPublic;
this.midiData = new ArrayList<>();
}

private static void validateUserId(Long userId) {
Expand Down Expand Up @@ -115,31 +128,54 @@ private static int resolveBpm(Integer bpm) {

// 자유 연주 생성
public static Playing createFreePlay(
Long userId, Integer bpm, boolean metronomeEnabled
Long userId, Integer bpm
) {
return Playing.builder()
.userId(userId)
.mode(PlayingMode.FREE_PLAY)
.status(PlayingStatus.READY)
.bpm(bpm)
.metronomeEnabled(metronomeEnabled)
.isPublic(false)
.build();
}

// 백킹트랙 연주 생성
public static Playing createBackingTrack(
Long userId, Long backingTrackId,
Integer bpm, boolean metronomeEnabled
Long userId, Long backingTrackId, Integer bpm
) {
return Playing.builder()
.userId(userId)
.backingTrackId(backingTrackId)
.mode(PlayingMode.BACKING_TRACK)
.status(PlayingStatus.READY)
.bpm(bpm)
.metronomeEnabled(metronomeEnabled)
.isPublic(false)
.build();
}

public void saveMidiData(List<MidiEventData> midiData) {
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved
validateMidiData(midiData);
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved

this.midiData = new ArrayList<>(midiData);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

midi 데이터를 저장할 때, 컬렉션 래퍼 참조가 끊기는 것을 방지하기 위해 this.midiData.clear(); 후 addAll() 방식으로 기존 컬렉션 객체를 유지하면서 갱신하는 방식은 어떤가요? 저도 비슷한 걸 찾아보다가 의견 어쭙습니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 midiData는 JSON 컬럼에 매핑된 컬렉션이라 Hibernate 영속 컬렉션 래퍼를 유지해야 하는 상황은 아니라고 판단해서 기존 코드대로 ArrayList를 생성해 교체하는 방식을 유지하려고 합니다!
혹시 다른 의견 있으시면 말씀해 주세요

this.midiData.sort(Comparator.comparingLong(MidiEventData::getTimestampMs));
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved

}

private static void validateMidiData(List<MidiEventData> midiData) {
Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved
if (midiData == null || midiData.isEmpty()) {
throw new GeneralException(PlayingErrorStatus.EMPTY_MIDI_EVENTS);
}

if (midiData.stream().anyMatch(Objects::isNull)) {
throw new GeneralException(PlayingErrorStatus.INVALID_MIDI_EVENT);
}
}

public List<MidiEventData> getMidiData() {

if (this.midiData == null) {
return List.of();
}
return List.copyOf(this.midiData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum PlayingErrorStatus implements BaseCode {
INVALID_PITCH_RANGE(HttpStatus.BAD_REQUEST, "PLAYING_400_06", "피치 값은 0~127 사이의 값이어야 합니다."),
INVALID_VELOCITY_RANGE(HttpStatus.BAD_REQUEST, "PLAYING_400_07", "강도는 0~127 사이의 값이어야 합니다."),
INVALID_TIMESTAMP(HttpStatus.BAD_REQUEST, "PLAYING_400_08", "MIDI 이벤트 타임스탬프는 0 이상이어야 합니다."),
EMPTY_MIDI_EVENTS(HttpStatus.BAD_REQUEST, "PLAYING_400_09", "저장할 MIDI 이벤트가 없습니다."),
INVALID_MIDI_EVENT(HttpStatus.BAD_REQUEST,"PLAYING_400_10", "MIDI 이벤트 목록에 유효하지 않은 값이 포함되어 있습니다."),
;

private final HttpStatus status;
Expand Down
Loading