use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextSQLTemplateIT method testIteratedQuery.
@Test
public void testIteratedQuery() throws Exception {
createFourArtists();
String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
try (ResultIterator<?> it = context.performIteratedQuery(query)) {
long i = 0;
while (it.hasNextRow()) {
i++;
DataRow row = (DataRow) it.nextRow();
assertEquals(3, row.size());
assertEquals("artist" + (1 + i), row.get("ARTIST_NAME"));
}
assertEquals(4, i);
}
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextSharedCacheIT method testCacheRefreshingOnSelect.
/**
* Checks that cache is refreshed when a query "refreshingObjects" property is set to
* true.
*/
@Test
public void testCacheRefreshingOnSelect() throws Exception {
String originalName = artist.getArtistName();
final String newName = "version2";
DataContext context = (DataContext) artist.getObjectContext();
DataRow oldSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
assertNotNull(oldSnapshot);
assertEquals(originalName, oldSnapshot.get("ARTIST_NAME"));
// update artist using raw SQL
SQLTemplate update = sqlTemplateCustomizer.createSQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)");
Map<String, Object> map = new HashMap<>(3);
map.put("newName", newName);
map.put("oldName", originalName);
update.setParams(map);
context.performNonSelectingQuery(update);
// fetch updated artist without refreshing
List artists = ObjectSelect.query(Artist.class).where(Artist.ARTIST_NAME.eq(newName)).select(context);
assertEquals(1, artists.size());
artist = (Artist) artists.get(0);
// check underlying cache
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
assertNotSame(oldSnapshot, freshSnapshot);
assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
// check an artist
assertEquals(newName, artist.getArtistName());
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotEvictedForCommitted.
@Test
public void testSnapshotEvictedForCommitted() throws Exception {
String newName = "version2";
assertEquals(PersistenceState.COMMITTED, artist.getPersistenceState());
context.getObjectStore().getDataRowCache().forgetSnapshot(artist.getObjectId());
assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId()));
// modify object and try to save
artist.setArtistName(newName);
context.commitChanges();
assertEquals(newName, artist.getArtistName());
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
assertNotNull(freshSnapshot);
assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotEvictedForHollow.
@Test
public void testSnapshotEvictedForHollow() throws Exception {
String originalName = artist.getArtistName();
context.invalidateObjects(artist);
assertEquals(PersistenceState.HOLLOW, artist.getPersistenceState());
assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId()));
// resolve object
assertEquals(originalName, artist.getArtistName());
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
assertNotNull(freshSnapshot);
assertEquals(originalName, freshSnapshot.get("ARTIST_NAME"));
}
use of org.apache.cayenne.DataRow in project cayenne by apache.
the class DataRowStoreIT method testMaxSize.
/**
* Tests LRU cache behavior.
*/
@Test
public void testMaxSize() throws Exception {
Map<String, String> props = new HashMap<>();
props.put(Constants.SNAPSHOT_CACHE_SIZE_PROPERTY, String.valueOf(2));
cache = new DataRowStore("cacheXYZ", new DefaultRuntimeProperties(props), null);
assertEquals(2, cache.maximumSize());
assertEquals(0, cache.size());
ObjectId key1 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 1);
Map<ObjectId, DataRow> diff1 = new HashMap<>();
diff1.put(key1, new DataRow(1));
ObjectId key2 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
Map<ObjectId, DataRow> diff2 = new HashMap<>();
diff2.put(key2, new DataRow(1));
ObjectId key3 = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
Map<ObjectId, DataRow> diff3 = new HashMap<>();
diff3.put(key3, new DataRow(1));
cache.processSnapshotChanges(this, diff1, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
assertEquals(1, cache.size());
cache.processSnapshotChanges(this, diff2, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
assertEquals(2, cache.size());
// this addition must overflow the cache, and throw out the first item
cache.processSnapshotChanges(this, diff3, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
assertEquals(2, cache.size());
assertNotNull(cache.getCachedSnapshot(key2));
assertNotNull(cache.getCachedSnapshot(key3));
assertNull(cache.getCachedSnapshot(key1));
}
Aggregations