Skip to content

Commit

Permalink
feat: Entity의 상태를 관리한다
Browse files Browse the repository at this point in the history
  • Loading branch information
sang5c authored and sang5c committed Nov 21, 2024
1 parent 21e0029 commit 89241ae
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### 요구 사항

- [ ] 엔티티의 관리 상태를 추가한다
- [X] 엔티티 관리 상태를 추가한다

## 3단계 - Persistence Context, Dirty Check

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/persistence/entity/EntityEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package persistence.entity;

public class EntityEntry {
private Status status;

private EntityEntry(Status status) {
this.status = status;
}

public static EntityEntry createManaged() {
return new EntityEntry(Status.MANAGED);
}

public void setDeleted() {
this.status = Status.DELETED;
}
}
1 change: 0 additions & 1 deletion src/main/java/persistence/entity/EntityManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jdbc.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EntityManagerImpl implements EntityManager {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/persistence/entity/PersistenceContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@

public class PersistenceContextImpl implements PersistenceContext {

private final Map<EntityKey, Object> entityMap = new HashMap<>();
private final Map<EntityKey, Object> managedEntities = new HashMap<>();
private final Map<EntityKey, EntitySnapshot> entitySnapshots = new HashMap<>();
private final Map<EntityKey, EntityEntry> entityEntries = new HashMap<>();

@Override
public void put(EntityKey entityKey, Object entity) {
entityMap.put(entityKey, entity);
managedEntities.put(entityKey, entity);
entitySnapshots.put(entityKey, EntitySnapshot.from(entity));
entityEntries.put(entityKey, EntityEntry.createManaged());
}

@Override
public boolean contains(EntityKey entityKey) {
return entityMap.containsKey(entityKey);
return managedEntities.containsKey(entityKey);
}

@Override
public Object get(EntityKey entityKey) {
return entityMap.get(entityKey);
return managedEntities.get(entityKey);
}

@Override
public void remove(EntityKey entityKey) {
entityMap.remove(entityKey);
managedEntities.remove(entityKey);
entitySnapshots.remove(entityKey);
entityEntries.get(entityKey).setDeleted();
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/persistence/entity/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package persistence.entity;

public enum Status {
MANAGED,
READ_ONLY,
DELETED,
GONE,
LOADING,
SAVING
}

0 comments on commit 89241ae

Please sign in to comment.