git clone https://github.com/tiny657/vote
cd vote
./gradlew clean build
./start.sh
./stop.sh
GET /v1/reddit/topics?top20&orderBy=popularity
GET /v1/reddit/topics/new
POST /v1/reddit/topics
{"text": "...."}
POST /v1/reddit/topics/{topicId}/up
POST /v1/reddit/topics/{topicId}/down
- ConcurrentHashMap<topicId, Topic>: All topics are stored. This map is thread-safe.
- max-heap: To sort by upvotes in descending order, I will use max-heap.
- List: To speed up showing top 20, this list is created like a cache.
-
When a new topic is submitted, the code works as follows.
- An AtomicInteger generated globally unique topic id.
- The new topic is stored in the ConcurrentHashMap.
- The new topic is also stored in the max-heap.
- Top 20 list is refreshed to cache top 20 from max-heap.
-
When UP is clicked, the code works as follows.
- The upvote count in a topic is increased.
- Max-heap is refreshed to consider the topic which the upvote count is changed.
- Top 20 list is refreshed from changed max-heap.
-
When DOWN is clicked, the code works as follows.
- The downvote count in a topic is increased.
- We do not need to refresh the max-heap and top 20 list because top 20 is only considered upvote count.
