Skip to content

Commit

Permalink
xx
Browse files Browse the repository at this point in the history
  • Loading branch information
LenKIM committed Jul 27, 2022
1 parent 373945b commit daf8e96
Show file tree
Hide file tree
Showing 25 changed files with 74 additions and 39 deletions.
Binary file modified .gradle/7.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/checksums/sha1-checksums.bin
Binary file not shown.
6 changes: 4 additions & 2 deletions jpa-jdbc/jdbc/schema/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ CREATE TABLE IF NOT EXISTS users
name VARCHAR(25) NOT NULL,
info json NOT NULL,

version INT NOT NULL
);
version INT NOT NULL,
add_offset INT NOT NULL,
publish_offset INT NOT NULL
);
1 change: 1 addition & 0 deletions jpa-jdbc/jdbc/src/main/java/org/example/entity/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

@Value(staticConstructor = "of")
public class Info {
int seq;
String name;
}
4 changes: 2 additions & 2 deletions jpa-jdbc/jdbc/src/main/java/org/example/entity/Infos.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class Infos {
List<Info> infos;

public void add() {
infos.add(Info.of(UUID.randomUUID().toString()));
public void add(long id) {
infos.add(Info.of(id, UUID.randomUUID().toString()));
}
}
11 changes: 10 additions & 1 deletion jpa-jdbc/jdbc/src/main/java/org/example/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -27,10 +30,16 @@ public class User {
private String name;
private Infos info;

private int addOffset;
private int publishOffset;

@Version
private Integer version;

public void add() {
info.add();
AtomicInteger atomicInteger = new AtomicInteger();
int i = atomicInteger.decrementAndGet();
addOffset = i;
info.add(i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public Info deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
String name = Serializer.getInstance()
.deserialize(json.getAsJsonObject().get("name").getAsString(), String.class);

return Info.of(name);
Integer seq = Serializer.getInstance()
.deserialize(json.getAsJsonObject().get("seq").getAsString(), Integer.class);

return Info.of(seq, name);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package org.example.persistence.util.serializer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.example.entity.Info;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class SerializerTest {
@Test
void test() {
Info base = Info.of("hello");
Info base = Info.of(1, "hello");
String serialized = Serializer.getInstance().serialize(base);
System.out.println("hello = " + serialized);

Info deserialized = Serializer.getInstance().deserialize(serialized, Info.class);
assertThat(deserialized).isEqualTo(base);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class JdbcUserRepositoryTest {
@Test
void name() throws InterruptedException {
while(true) {
Thread.sleep(1000);
Thread.sleep(100);
User user = repository.findById(10L).get();
user.add();
User save = repository.save(user);
System.out.println("save = " + save);
}
}
}
}
12 changes: 7 additions & 5 deletions jpa-jdbc/jdbc2/schema/init.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
CREATE TABLE IF NOT EXISTS users
(
id BIGINT PRIMARY KEY,
name VARCHAR(25) NOT NULL,
info json NOT NULL,
id BIGINT PRIMARY KEY,
name VARCHAR(25) NOT NULL,
info json NOT NULL,

version INT NOT NULL
);
version INT NOT NULL,
add_offset INT NOT NULL,
publish_offset INT NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public String join() {
User user = repository.save(User.builder()
.id(1L)
.name("jangwonik")
.build());
.addOffset(0)
.publishOffset(0)
.build()
);

return repository.save(user).toString();
}
Expand Down
1 change: 1 addition & 0 deletions jpa-jdbc/jdbc2/src/main/java/org/example/entity/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
@Value(staticConstructor = "of")
@ToString
public class Info {
Integer seq;
String name;
}
16 changes: 12 additions & 4 deletions jpa-jdbc/jdbc2/src/main/java/org/example/entity/Infos.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package org.example.entity;

import java.util.List;
import lombok.Value;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Value(staticConstructor = "of")
public class Infos {
List<Info> infos;

public String pop() {
public List<String> pop(int startOffset, int publishOffset) {
if (infos.isEmpty()) {
return "empty";
return new LinkedList<>();
}
Info info = infos.get(0);
infos.remove(0);
return info.toString();

return infos.stream().filter(
i -> i.getSeq() > startOffset
&& info.getSeq() <= publishOffset
).map(Info::toString)
.collect(Collectors.toUnmodifiableList());
}
}
15 changes: 10 additions & 5 deletions jpa-jdbc/jdbc2/src/main/java/org/example/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.example.entity;

import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -13,6 +11,8 @@
import org.springframework.data.annotation.Version;
import org.springframework.data.relational.core.mapping.Table;

import java.util.List;

@Table(name = "users")
@Getter
@Builder
Expand All @@ -25,12 +25,17 @@ public class User {
private Long id;
@With
private String name;
private Infos info;
private Infos infos;

private int addOffset;
private int publishOffset;

@Version
private Integer version;

public String pop() {
return info.pop();
public List<String> pop() {
List<String> pop = infos.pop(addOffset, publishOffset);
publishOffset = addOffset;
return pop;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public Info deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
throws JsonParseException {
String name = Serializer.getInstance()
.deserialize(json.getAsJsonObject().get("name").getAsString(), String.class);

return Info.of(name);
Integer seq = Serializer.getInstance()
.deserialize(json.getAsJsonObject().get("seq").getAsString(), Integer.class);
return Info.of(seq, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
class SerializerTest {
@Test
void test() {
Info base = Info.of("hello");
Info base = Info.of(1,"hello");
String serialized = Serializer.getInstance().serialize(base);
System.out.println("hello = " + serialized);

Info deserialized = Serializer.getInstance().deserialize(serialized, Info.class);
assertThat(deserialized).isEqualTo(base);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.example.repository;

import java.util.LinkedList;
import java.util.List;
import net.bytebuddy.dynamic.scaffold.MethodGraph.Linked;
import org.example.entity.Infos;
import org.example.entity.User;
import org.example.persistence.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.LinkedList;

@SpringBootTest
class Jdbc2UserRepositoryTest {
@Autowired
Expand All @@ -20,19 +19,21 @@ void name1() {
User jangwonik = User.builder()
.id(10L)
.name("jangwonik")
.info(Infos.of(new LinkedList<>()))
.infos(Infos.of(new LinkedList<>()))
.addOffset(0)
.publishOffset(0)
.build();
repository.save(jangwonik);
}

@Test
void name() throws InterruptedException {
while(true) {
Thread.sleep(1000);
while (true) {
Thread.sleep(100);
User user = repository.findById(10L).get();
user.pop();
User save = repository.save(user);
System.out.println(save);
}
}
}
}

0 comments on commit daf8e96

Please sign in to comment.