forked from ebean-orm/ebean
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW: Support for lazy add on BeanList (#81)
When just adding new entries to an existing BeanList, there is no need to load the underlying collection FIX: Issue when batch load occurs with lazily added elements
- Loading branch information
Showing
9 changed files
with
348 additions
and
40 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
129 changes: 129 additions & 0 deletions
129
ebean-api/src/main/java/io/ebean/common/BeanListLazyAdd.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,129 @@ | ||
package io.ebean.common; | ||
|
||
import io.ebean.bean.BeanCollection; | ||
import io.ebean.bean.BeanCollectionLoader; | ||
import io.ebean.bean.EntityBean; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* This bean list can perform additions without populating the list. | ||
* This might be useful, if you just want to add entries to an existing collection. | ||
* Works only for lists and only if there is no order column | ||
*/ | ||
public class BeanListLazyAdd<E> extends BeanList<E> { | ||
|
||
public BeanListLazyAdd(BeanCollectionLoader loader, EntityBean ownerBean, String propertyName) { | ||
super(loader, ownerBean, propertyName); | ||
} | ||
|
||
private List<E> lazyAddedEntries; | ||
|
||
@Override | ||
public boolean add(E bean) { | ||
checkReadOnly(); | ||
|
||
lock.lock(); | ||
try { | ||
if (list == null) { | ||
// list is not yet initialized, so we may add elements to a spare list | ||
if (lazyAddedEntries == null) { | ||
lazyAddedEntries = new ArrayList<>(); | ||
} | ||
lazyAddedEntries.add(bean); | ||
} else { | ||
list.add(bean); | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
|
||
if (modifyListening) { | ||
modifyAddition(bean); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean addAll(Collection<? extends E> beans) { | ||
checkReadOnly(); | ||
|
||
lock.lock(); | ||
try { | ||
if (list == null) { | ||
// list is not yet initialized, so we may add elements to a spare list | ||
if (lazyAddedEntries == null) { | ||
lazyAddedEntries = new ArrayList<>(); | ||
} | ||
lazyAddedEntries.addAll(beans); | ||
} else { | ||
list.addAll(beans); | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
|
||
if (modifyListening) { | ||
getModifyHolder().modifyAdditionAll(beans); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
@Override | ||
public void loadFrom(BeanCollection<?> other) { | ||
super.loadFrom(other); | ||
if (lazyAddedEntries != null) { | ||
list.addAll(lazyAddedEntries); | ||
lazyAddedEntries = null; | ||
} | ||
} | ||
|
||
/** | ||
* on init, this happens on all accessor methods except on 'add' and addAll, | ||
* we add the lazy added entries at the end of the list | ||
*/ | ||
@Override | ||
protected void initList(boolean skipLoad, boolean onlyIds) { | ||
if (skipLoad) { | ||
if (lazyAddedEntries != null) { | ||
list = lazyAddedEntries; | ||
lazyAddedEntries = null; | ||
} else { | ||
list = new ArrayList<>(); | ||
} | ||
} else { | ||
lazyLoadCollection(onlyIds); | ||
if (lazyAddedEntries != null) { | ||
list.addAll(lazyAddedEntries); | ||
lazyAddedEntries = null; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<E> getLazyAddedEntries() { | ||
return lazyAddedEntries; | ||
} | ||
|
||
@Override | ||
public boolean isSkipSave() { | ||
return lazyAddedEntries == null && super.isSkipSave(); | ||
} | ||
|
||
public boolean checkEmptyLazyLoad() { | ||
if (list != null) { | ||
return false; | ||
} else if (lazyAddedEntries == null) { | ||
list = new ArrayList<>(); | ||
return true; | ||
} else { | ||
list = lazyAddedEntries; | ||
lazyAddedEntries = null; | ||
return false; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.