Skip to content

Instantly share code, notes, and snippets.

View jjeonghak's full-sized avatar
Preparing Profile …

jjeonghak

Preparing Profile …
View GitHub Profile
@jjeonghak
jjeonghak / broadcast.service.2.js
Created May 1, 2026 18:38
broadcast.service.2.js
// The optimal equilibrium between real-time responsiveness and server load
private static PERIOD_MS = 500;
setInterval(() => {
for (const [userId, connection] of this.activeConnections) {
this.sendPendingEvents(userId, connection);
}
}, BroadcastService.PERIOD_MS);
@jjeonghak
jjeonghak / broadcast.service.js
Created May 1, 2026 18:37
broadcast.service.js
// Core versioning logic (simplified)
for (const connection of activeConnections) {
const missed = eventQueue.filter(
e => e.userId !== connection.userId && connection.version < e.event.version
);
if (missed.length > 0) {
connection.respond(missed.map(e => e.event));
}
}
@jjeonghak
jjeonghak / SurveyAnswerValidator.java
Created May 1, 2026 18:29
SurveyAnswerValidator.java
public void validateArchive(...) {
// Because the engine is dynamic, we must manually verify data integrity.
// Example: Check if the provided ChoiceId actually belongs to the QuestionId.
boolean isValid = questionDetailsList.stream()
.anyMatch(q -> q.getChoices().stream()
.anyMatch(c -> c.getId().equals(targetChoiceId)));
if (!isValid) {
throw new InvalidRequestArgumentException("Inconsistent answer data detected.");
}
@jjeonghak
jjeonghak / SurveyService.java
Created May 1, 2026 18:27
SurveyService.java
private List<SurveyorActivity> createPhotoActivities(...) {
// Using RxJava's zip operator to synchronize S3 uploads with database persistence.
// The activity record is only created if both the upload and data mapping succeed.
return Observable.zip(
Observable.fromIterable(photos), // Binary files
Observable.fromIterable(photoAnswers), // Answer metadata
(file, answer) -> {
AmazonS3Carrier.BucketObjectDetails s3Details =
amazonS3Service.putImageToBucket(answer.getQuestionId(), file);
@jjeonghak
jjeonghak / QuestionMappingRepositoryImpl.java
Created May 1, 2026 18:22
QuestionMappingRepositoryImpl.java
// We fetch questions, choices, and previous answers in one optimized query.
public List<QuestionDetails> fetchSurveySchema(Long gameId) {
List<QuestionData> records = queryFactory
.select(Projections.constructor(QuestionData.class,
QQuestion.question.id,
QQuestion.question.content,
QChoice.choice.id,
QChoice.choice.content))
.from(QQuestionMapping.questionMapping)
.join(QQuestion.question).on(QQuestionMapping.questionId.eq(QQuestion.question.id))