This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from appmetr/develop
Develop
- Loading branch information
Showing
23 changed files
with
1,029 additions
and
64 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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/appmetr/hercules/annotations/IndexedCollection.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,23 @@ | ||
package com.appmetr.hercules.annotations; | ||
|
||
import com.appmetr.hercules.keys.CollectionKeysExtractor; | ||
import com.appmetr.hercules.serializers.AbstractHerculesSerializer; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface IndexedCollection { | ||
|
||
String name() default ""; | ||
|
||
Class itemClass() default Object.class; | ||
|
||
Class<? extends AbstractHerculesSerializer> serializer() default AbstractHerculesSerializer.class; | ||
|
||
Class<? extends CollectionKeysExtractor> keyExtractorClass() default CollectionKeysExtractor.class; | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/appmetr/hercules/batch/BreakableIterationBatchExecutor.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,32 @@ | ||
package com.appmetr.hercules.batch; | ||
|
||
import com.appmetr.hercules.profile.DataOperationsProfile; | ||
|
||
import java.util.List; | ||
|
||
public class BreakableIterationBatchExecutor<E, K> { | ||
private BatchIterator<E, K> iterator; | ||
private BreakableIterationBatchProcessor<E> processor; | ||
|
||
public BreakableIterationBatchExecutor(BatchIterator<E, K> iterator, BreakableIterationBatchProcessor<E> processor) { | ||
this.iterator = iterator; | ||
this.processor = processor; | ||
} | ||
|
||
public int execute() { | ||
return execute(null); | ||
} | ||
|
||
public int execute(DataOperationsProfile dataOperationsProfile) { | ||
int counter = 0; | ||
|
||
while (iterator.hasNext()) { | ||
List<E> batch = iterator.next(dataOperationsProfile); | ||
counter += batch.size(); | ||
|
||
if (!processor.processBatch(batch)) break; | ||
} | ||
|
||
return counter; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/appmetr/hercules/batch/BreakableIterationBatchProcessor.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,12 @@ | ||
package com.appmetr.hercules.batch; | ||
|
||
import java.util.List; | ||
|
||
public interface BreakableIterationBatchProcessor<E> { | ||
/** | ||
* Process batch with ability to break iterator loop. | ||
* @param batch | ||
* @return true to continue iteration, false to break iteration loop | ||
*/ | ||
boolean processBatch(List<E> batch); | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/appmetr/hercules/keys/CollectionKeysExtractor.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,11 @@ | ||
package com.appmetr.hercules.keys; | ||
|
||
import me.prettyprint.hector.api.Serializer; | ||
|
||
public interface CollectionKeysExtractor<E, K> { | ||
|
||
Iterable<K> extractKeys(E entity); | ||
|
||
Serializer<K> getKeySerializer(); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/appmetr/hercules/keys/EntityCollectionKeyExtractor.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,46 @@ | ||
package com.appmetr.hercules.keys; | ||
|
||
import com.appmetr.hercules.manager.EntityManager; | ||
import me.prettyprint.hector.api.Serializer; | ||
|
||
import javax.inject.Inject; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class EntityCollectionKeyExtractor<E, K> implements CollectionKeysExtractor<E, K> { | ||
|
||
private EntityManager em; | ||
private Field collectionField; | ||
private Class<?> collectionEntityClass; | ||
|
||
@Inject | ||
public EntityCollectionKeyExtractor(Field collectionField, Class<?> collectionEntityClass, EntityManager em) { | ||
this.collectionField = collectionField; | ||
this.collectionEntityClass = collectionEntityClass; | ||
this.em = em; | ||
} | ||
|
||
@Override public Iterable<K> extractKeys(E entity) { | ||
try { | ||
Collection indexedCollection = (Collection) collectionField.get(entity); | ||
if (indexedCollection != null) { | ||
List<K> keys = new ArrayList<K>(indexedCollection.size()); | ||
for (Object entityInIndex : indexedCollection) { | ||
keys.add(em.<Object, K>getPK(entityInIndex)); | ||
} | ||
return keys; | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException("Unable to read indexed collection field for entity of class " + entity.getClass().getName() + " and field " + collectionField.getName()); | ||
} | ||
} | ||
|
||
@Override public Serializer<K> getKeySerializer() { | ||
return em.getPKSerializer(collectionEntityClass); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/appmetr/hercules/keys/SerializableKeyCollectionKeyExtractor.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,39 @@ | ||
package com.appmetr.hercules.keys; | ||
|
||
import me.prettyprint.hector.api.Serializer; | ||
|
||
import javax.inject.Inject; | ||
import java.lang.reflect.Field; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public class SerializableKeyCollectionKeyExtractor<E, K> implements CollectionKeysExtractor<E, K> { | ||
|
||
private Field collectionField; | ||
private Serializer<K> keySerializer; | ||
|
||
@Inject | ||
public SerializableKeyCollectionKeyExtractor(Field collectionField, Serializer<K> serializer) { | ||
this.collectionField = collectionField; | ||
this.keySerializer = serializer; | ||
} | ||
|
||
@Override public Iterable<K> extractKeys(E entity) { | ||
try { | ||
Collection<K> keys = (Collection<K>) collectionField.get(entity); | ||
if (keys!=null){ | ||
return keys; | ||
}else{ | ||
return Collections.emptyList(); | ||
|
||
} | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException("Unable to read indexed collection field for entity of class " + entity.getClass().getName() + " and field " + collectionField.getName()); | ||
} | ||
} | ||
|
||
@Override public Serializer<K> getKeySerializer() { | ||
return keySerializer; | ||
} | ||
|
||
} |
Oops, something went wrong.