-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60d368d
commit dcec61a
Showing
42 changed files
with
858 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
...c/main/java/org/robbins/flashcards/cassandra/repository/FlashCardCassandraRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package org.robbins.flashcards.cassandra.repository; | ||
|
||
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraDto; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto; | ||
import org.springframework.data.cassandra.repository.Query; | ||
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraEntity; | ||
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface FlashCardCassandraRepository extends TypedIdCassandraRepository<FlashCardCassandraDto, UUID> { | ||
public interface FlashCardCassandraRepository extends TypedIdCassandraRepository<FlashCardCassandraEntity, UUID> { | ||
|
||
} |
94 changes: 84 additions & 10 deletions
94
...ra/src/main/java/org/robbins/flashcards/cassandra/repository/FlashCardRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,132 @@ | ||
|
||
package org.robbins.flashcards.cassandra.repository; | ||
|
||
import com.datastax.driver.core.BatchStatement; | ||
import com.datastax.driver.core.PreparedStatement; | ||
import com.datastax.driver.core.RegularStatement; | ||
import com.datastax.driver.core.Session; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraDto; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto; | ||
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraEntity; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraEntity; | ||
import org.robbins.flashcards.repository.FlashCardRepository; | ||
import org.robbins.flashcards.repository.TagRepository; | ||
import org.springframework.data.cassandra.core.CassandraOperations; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.inject.Inject; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; | ||
import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | ||
|
||
@Repository | ||
public class FlashCardRepositoryImpl extends AbstractCrudRepositoryImpl<FlashCardCassandraDto, UUID> implements | ||
FlashCardRepository<FlashCardCassandraDto, TagCassandraDto, UUID> { | ||
public class FlashCardRepositoryImpl extends AbstractCrudRepositoryImpl<FlashCardCassandraEntity, UUID> implements | ||
FlashCardRepository<FlashCardCassandraEntity, TagCassandraEntity, UUID> { | ||
|
||
@Inject | ||
private CassandraOperations cassandraOperations; | ||
|
||
@Inject | ||
private FlashCardCassandraRepository repository; | ||
|
||
@Inject | ||
private TagRepository<TagCassandraEntity, UUID> tagRepository; | ||
|
||
private static final String TAG_FLASHCARD_TABLE = "tag_flashcard"; | ||
private static final String FLASHCARD_TABLE = "flashcard"; | ||
private static final String ID = "id"; | ||
private static final String QUESTION = "question"; | ||
private static final String ANSWER = "answer"; | ||
private static final String TAGS = "tags"; | ||
private static final String TAG_ID = "tag_id"; | ||
private static final String FLASHCARD_ID = "flashcard_id"; | ||
|
||
@Override | ||
public FlashCardCassandraRepository getRepository() { | ||
return repository; | ||
} | ||
|
||
@Override | ||
public List<FlashCardCassandraDto> findByTagsIn(Set<TagCassandraDto> tags) { | ||
public FlashCardCassandraEntity save(final FlashCardCassandraEntity flashcard) { | ||
cassandraOperations.execute(flashcardBatch(flashcard)); | ||
|
||
return flashcard; | ||
} | ||
|
||
private BatchStatement flashcardBatch(FlashCardCassandraEntity flashcard) { | ||
Session session = cassandraOperations.getSession(); | ||
|
||
PreparedStatement flashcardStatement = session.prepare(flashcardInsert()); | ||
PreparedStatement tagFlashcardStatement = session.prepare(tagFlashcardInsert()); | ||
|
||
BatchStatement batch = new BatchStatement(); | ||
|
||
// flashcard | ||
batch.add(flashcardStatement.bind( | ||
flashcard.getId(), | ||
flashcard.getQuestion(), | ||
flashcard.getAnswer(), | ||
flashcard.getTags())); | ||
|
||
// tag flashcards | ||
for (Map.Entry<UUID, String> tagEntry : flashcard.getTags().entrySet()) { | ||
batch.add(tagFlashcardStatement.bind( | ||
tagEntry.getKey(), | ||
flashcard.getId(), | ||
flashcard.getQuestion(), | ||
flashcard.getAnswer())); | ||
} | ||
|
||
return batch; | ||
} | ||
|
||
private RegularStatement tagFlashcardInsert() { | ||
return insertInto(TAG_FLASHCARD_TABLE) | ||
.value(TAG_ID, bindMarker()) | ||
.value(FLASHCARD_ID, bindMarker()) | ||
.value(QUESTION, bindMarker()) | ||
.value(ANSWER, bindMarker()); | ||
} | ||
|
||
private RegularStatement flashcardInsert() { | ||
return insertInto(FLASHCARD_TABLE) | ||
.value(ID, bindMarker()) | ||
.value(QUESTION, bindMarker()) | ||
.value(ANSWER, bindMarker()) | ||
.value(TAGS, bindMarker()); | ||
} | ||
|
||
@Override | ||
public List<FlashCardCassandraEntity> findByTagsIn(Set<TagCassandraEntity> tags) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
|
||
@Override | ||
public List<FlashCardCassandraDto> findByTagsIn(Set<TagCassandraDto> tags, PageRequest page) { | ||
public List<FlashCardCassandraEntity> findByTagsIn(Set<TagCassandraEntity> tags, PageRequest page) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
|
||
@Override | ||
public List<FlashCardCassandraDto> findByQuestionLike(String question) { | ||
public List<FlashCardCassandraEntity> findByQuestionLike(String question) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
|
||
@Override | ||
public List<FlashCardCassandraDto> findByQuestionLike(String question, PageRequest page) { | ||
public List<FlashCardCassandraEntity> findByQuestionLike(String question, PageRequest page) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
|
||
@Override | ||
public FlashCardCassandraDto findByQuestion(String question) { | ||
public FlashCardCassandraEntity findByQuestion(String question) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
|
||
@Override | ||
public List<FlashCardCassandraDto> findByTags_Id(UUID tagId) { | ||
public List<FlashCardCassandraEntity> findByTags_Id(UUID tagId) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...dra/src/main/java/org/robbins/flashcards/cassandra/repository/TagCassandraRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package org.robbins.flashcards.cassandra.repository; | ||
|
||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraEntity; | ||
import org.springframework.data.cassandra.repository.Query; | ||
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface TagCassandraRepository extends TypedIdCassandraRepository<TagCassandraDto, UUID> { | ||
public interface TagCassandraRepository extends TypedIdCassandraRepository<TagCassandraEntity, UUID> { | ||
|
||
@Query("SELECT * FROM tag WHERE name = ?0") | ||
public TagCassandraDto findByName(final String name); | ||
public TagCassandraEntity findByName(final String name); | ||
} |
18 changes: 18 additions & 0 deletions
18
...ain/java/org/robbins/flashcards/cassandra/repository/TagFlashcardCassandraRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.robbins.flashcards.cassandra.repository; | ||
|
||
import org.robbins.flashcards.cassandra.repository.domain.TagFlashCardCassandraEntity; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagFlashCardKey; | ||
import org.robbins.flashcards.cassandra.repository.domain.UserCassandraEntity; | ||
import org.springframework.data.cassandra.repository.Query; | ||
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface TagFlashcardCassandraRepository extends TypedIdCassandraRepository<TagFlashCardCassandraEntity, TagFlashCardKey> { | ||
|
||
@Query("SELECT * FROM tag_flashcard WHERE tag_id = ?0") | ||
public List<TagFlashCardCassandraEntity> findByTagId(final UUID tagId); | ||
} |
92 changes: 87 additions & 5 deletions
92
...assandra/src/main/java/org/robbins/flashcards/cassandra/repository/TagRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,116 @@ | ||
|
||
package org.robbins.flashcards.cassandra.repository; | ||
|
||
import com.datastax.driver.core.BatchStatement; | ||
import com.datastax.driver.core.PreparedStatement; | ||
import com.datastax.driver.core.RegularStatement; | ||
import com.datastax.driver.core.Session; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraDto; | ||
import org.robbins.flashcards.cassandra.repository.domain.FlashCardCassandraEntity; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagCassandraEntity; | ||
import org.robbins.flashcards.cassandra.repository.domain.TagFlashCardCassandraEntity; | ||
import org.robbins.flashcards.repository.TagRepository; | ||
import org.springframework.data.cassandra.core.CassandraOperations; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.inject.Inject; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static com.datastax.driver.core.querybuilder.QueryBuilder.*; | ||
|
||
@Repository | ||
public class TagRepositoryImpl extends AbstractCrudRepositoryImpl<TagCassandraDto, UUID> implements | ||
TagRepository<TagCassandraDto, UUID> { | ||
public class TagRepositoryImpl extends AbstractCrudRepositoryImpl<TagCassandraEntity, UUID> implements | ||
TagRepository<TagCassandraEntity, UUID> { | ||
|
||
@Inject | ||
private CassandraOperations cassandraOperations; | ||
|
||
@Inject | ||
private TagCassandraRepository repository; | ||
|
||
@Inject | ||
private TagFlashcardCassandraRepository tagFlashcardCassandraRepository; | ||
|
||
@Inject FlashCardCassandraRepository flashCardCassandraRepository; | ||
|
||
private static final String TAG_TABLE = "tag"; | ||
private static final String FLASHCARD_TABLE = "flashcard"; | ||
private static final String ID = "id"; | ||
private static final String NAME = "name"; | ||
private static final String TAGS = "tags"; | ||
|
||
@Override | ||
public TagCassandraRepository getRepository() { | ||
return repository; | ||
} | ||
|
||
@Override | ||
public TagCassandraDto findByName(final String name) { | ||
public TagCassandraEntity save(final TagCassandraEntity tag) { | ||
cassandraOperations.execute(tagBatch(tag)); | ||
|
||
return tag; | ||
} | ||
|
||
private void updateFlashCardTags(final TagCassandraEntity tag) { | ||
List<TagFlashCardCassandraEntity> tagFlashcards = tagFlashcardCassandraRepository.findByTagId(tag.getId()); | ||
if (tagFlashcards != null && tagFlashcards.size() > 0) { | ||
for (TagFlashCardCassandraEntity tagFlashCard : tagFlashcards) { | ||
FlashCardCassandraEntity flashcard = flashCardCassandraRepository.findOne(tagFlashCard.getId().getFlashCardId()); | ||
if (flashcard != null && flashcard.getTags() != null) { | ||
flashcard.getTags().put(tag.getId(), tag.getName()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private BatchStatement tagBatch(TagCassandraEntity tag) { | ||
Session session = cassandraOperations.getSession(); | ||
|
||
PreparedStatement tagStatement = session.prepare(tagInsert()); | ||
PreparedStatement flashcardStatement = session.prepare(flashcardUpdateTag()); | ||
|
||
BatchStatement batch = new BatchStatement(); | ||
|
||
// flashcard | ||
batch.add(tagStatement.bind( | ||
tag.getId(), | ||
tag.getName())); | ||
|
||
List<TagFlashCardCassandraEntity> tagFlashcards = tagFlashcardCassandraRepository.findByTagId(tag.getId()); | ||
if (tagFlashcards != null && tagFlashcards.size() > 0) { | ||
for (TagFlashCardCassandraEntity tagFlashCard : tagFlashcards) { | ||
FlashCardCassandraEntity flashcard = flashCardCassandraRepository.findOne(tagFlashCard.getId().getFlashCardId()); | ||
if (flashcard != null && flashcard.getTags() != null) { | ||
batch.add(flashcardStatement.bind( | ||
tag.getId(), | ||
tag.getName(), | ||
flashcard.getId())); | ||
} | ||
} | ||
} | ||
return batch; | ||
} | ||
|
||
private RegularStatement tagInsert() { | ||
return insertInto(TAG_TABLE) | ||
.value(ID, bindMarker()) | ||
.value(NAME, bindMarker()); | ||
} | ||
|
||
private RegularStatement flashcardUpdateTag() { | ||
return update(FLASHCARD_TABLE) | ||
.with(put(TAGS, bindMarker(), bindMarker())) | ||
.where(eq(ID, bindMarker())); | ||
} | ||
|
||
@Override | ||
public TagCassandraEntity findByName(final String name) { | ||
return repository.findByName(name); | ||
} | ||
|
||
@Override | ||
public List<TagCassandraDto> findByFlashcards_Id(UUID flashcardId) { | ||
public List<TagCassandraEntity> findByFlashcards_Id(UUID flashcardId) { | ||
throw new NotImplementedException("method not yet implemented in Cassandra repository"); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...ra/src/main/java/org/robbins/flashcards/cassandra/repository/UserCassandraRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package org.robbins.flashcards.cassandra.repository; | ||
|
||
import org.robbins.flashcards.cassandra.repository.domain.UserCassandraDto; | ||
import org.robbins.flashcards.cassandra.repository.domain.UserCassandraEntity; | ||
import org.springframework.data.cassandra.repository.Query; | ||
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.UUID; | ||
|
||
@Repository("userRepository") | ||
public interface UserCassandraRepository extends TypedIdCassandraRepository<UserCassandraDto, UUID> { | ||
public interface UserCassandraRepository extends TypedIdCassandraRepository<UserCassandraEntity, UUID> { | ||
|
||
@Query("SELECT * FROM user WHERE openid = ?0") | ||
public UserCassandraDto findUserByOpenid(final String openid); | ||
public UserCassandraEntity findUserByOpenid(final String openid); | ||
} |
Oops, something went wrong.