๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
CS/Database

[Spring Data MongoDB] - Auto Increment Sequence ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

by ์ฃผ๋ฐœ2 2021. 9. 11.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž  [Spring Data MongoDB] - Auto Increment Sequence ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

 

์•ˆ๋…•ํ•˜์„ธ์š”, ์ €๋ฒˆ ์‹œ๊ฐ„์—๋Š” Spring Data MongoDB์—์„œ Auto Increment Sequence ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กด์žฌํ•˜๋Š” ๊ฐ’๋“ค์„ ์ œ๊ฑฐํ•˜๋ฉด์„œ Auto Increment ๊ฐ’๋„ ์ดˆ๊ธฐํ™” ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

MySQL์—์„œ๋Š” ๋‹จ์ˆœ ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ Auto Increment๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, MongoDB์—์„œ๋Š” ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ด๋„ ์ž๋ฃŒ๊ฐ€ ์ž˜ ๋‚˜์˜ค์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.(๊ฒ€์ƒ‰ ๋Šฅ๋ ฅ์ด ๋ถ€์กฑํ•ด์„œ...)

๋”ฐ๋ผ์„œ ์–ด๋–ป๊ฒŒ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผ์„ ํ•˜๋‹ค๊ฐ€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ฐพ๊ฒŒ๋˜์—ˆ๋Š”๋ฐ์š”, ์ƒ๊ฐ์™ธ๋กœ ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

1) Sequence ๋ฉ”์†Œ๋“œ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ํ•จ์ˆ˜์—์„œ 1๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ


๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์œผ๋กœ๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์ง€๋งŒ, ๋ฐ”๋กœ ๋– ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค.

package com.juhyun.shorturl.entity.sequence;

import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static org.springframework.data.mongodb.core.FindAndModifyOptions.options;
import static org.springframework.data.mongodb.core.query.Criteria.where;

@RequiredArgsConstructor
@Service
public class SequenceGeneratorService {

    private final MongoOperations mongoOperations;

    public Long generateSequence(String key) {
        AutoIncrementSequence counter = mongoOperations.findAndModify(Query.query(where("_id").is(key)),
                new Update().inc("seq", 1), options().returnNew(true).upsert(true), AutoIncrementSequence.class);

        return !Objects.isNull(counter) ? counter.getSeq() : 1;
    }

}

์œ„ SequenceGeneratorService ํด๋ž˜์Šค์˜ generateSequence() ๋ฉ”์†Œ๋“œ๋Š” Sequence ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ ํ›„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์œ„ ํ•จ์ˆ˜์—์„œ์˜ ๋ฆฌํ„ด ๊ฐ’์„ ๋‹จ์ˆœํžˆ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ”์ฃผ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.

 

return 1L;

 

์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ํ•ด๊ฒฐ์€ ๋˜์ง€๋งŒ, ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๋Š” ๋“ฑ ๊ทผ๋ณธ์ ์ธ ํ•ด๊ฒฐ์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

 

2) GUI์˜ sequence collection์—์„œ ํ•ด๋‹น ๊ฐ’์„ ์ˆ˜์ •ํ•˜๊ธฐ


์ „ ๊ฒŒ์‹œ๊ธ€์—์„œ Auto Increment ๋ฅผ ์„ค์ •ํ•  ๋•Œ, Entity๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌ๋ฅผ ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

package com.juhyun.shorturl.entity.sequence;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Getter
@Setter
@Document(collection = "auto_sequence")
public class AutoIncrementSequence {

    @Id
    private String id;
    private Long seq;
}

๋”ฐ๋ผ์„œ ์œ„ ํ•„๋“œ๋Š” auto_sequence ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ์ด ๋ฉ๋‹ˆ๋‹ค.

 

 

๋”ฐ๋ผ์„œ ์ปฌ๋ ‰์…˜์—์„œ seq ๊ฐ’์„ ๋ณ€๊ฒฝํ•จ์œผ๋กœ์„œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜ƒ

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€