Skip to content

Commit

Permalink
Format, license header
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Mar 11, 2023
1 parent 9a4fc60 commit bce6fca
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/site/es/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/site/ja/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/site/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,77 +38,70 @@
@SuppressWarnings("ALL")
class CursorOomTest {

private static SqlSessionFactory sqlSessionFactory;
private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader(
"org/apache/ibatis/submitted/cursor_cache_oom/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(
sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/cursor_cache_oom/CreateDB.sql"
);
@BeforeAll
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources
.getResourceAsReader("org/apache/ibatis/submitted/cursor_cache_oom/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

private static Map<CacheKey, Object> getNestedResultObjects(Cursor<User> users) throws IllegalAccessException,
NoSuchFieldException {
DefaultCursor<User> defaultCursor = (DefaultCursor<User>) users;
Field resultSetHandlerField = DefaultCursor.class.getDeclaredField("resultSetHandler");
resultSetHandlerField.setAccessible(true);
DefaultResultSetHandler defaultResultSetHandler =
(DefaultResultSetHandler) resultSetHandlerField
.get(defaultCursor);
Field nestedResultObjectsField = DefaultResultSetHandler.class.getDeclaredField("nestedResultObjects");
nestedResultObjectsField.setAccessible(true);
return (Map<CacheKey, Object>) nestedResultObjectsField
.get(defaultResultSetHandler);
}
// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/cursor_cache_oom/CreateDB.sql");
}

private static List<Cursor<?>> getCursors(SqlSession sqlSession)
throws NoSuchFieldException, IllegalAccessException {
DefaultSqlSession session = (DefaultSqlSession) sqlSession;
Field cursorListField = DefaultSqlSession.class.getDeclaredField("cursorList");
cursorListField.setAccessible(true);
List<Cursor<?>> cursorList =
(List<Cursor<?>>) cursorListField.get(session);
return cursorList;
}
private static Map<CacheKey, Object> getNestedResultObjects(Cursor<User> users)
throws IllegalAccessException, NoSuchFieldException {
DefaultCursor<User> defaultCursor = (DefaultCursor<User>) users;
Field resultSetHandlerField = DefaultCursor.class.getDeclaredField("resultSetHandler");
resultSetHandlerField.setAccessible(true);
DefaultResultSetHandler defaultResultSetHandler = (DefaultResultSetHandler) resultSetHandlerField
.get(defaultCursor);
Field nestedResultObjectsField = DefaultResultSetHandler.class.getDeclaredField("nestedResultObjects");
nestedResultObjectsField.setAccessible(true);
return (Map<CacheKey, Object>) nestedResultObjectsField.get(defaultResultSetHandler);
}

@Test
void shouldNotCacheAllDataForWholeSessionWhileUsingCursor() throws IOException, NoSuchFieldException,
IllegalAccessException {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
try (Cursor<User> users = mapper.fetchUsers()) {
for (User user : users) {
consumeUser(user);
}
Map nestedResultObjects = getNestedResultObjects(users);
private static List<Cursor<?>> getCursors(SqlSession sqlSession) throws NoSuchFieldException, IllegalAccessException {
DefaultSqlSession session = (DefaultSqlSession) sqlSession;
Field cursorListField = DefaultSqlSession.class.getDeclaredField("cursorList");
cursorListField.setAccessible(true);
List<Cursor<?>> cursorList = (List<Cursor<?>>) cursorListField.get(session);
return cursorList;
}

Assertions.assertFalse(nestedResultObjects.isEmpty());
@Test
void shouldNotCacheAllDataForWholeSessionWhileUsingCursor()
throws IOException, NoSuchFieldException, IllegalAccessException {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
try (Cursor<User> users = mapper.fetchUsers()) {
for (User user : users) {
consumeUser(user);
}
Map nestedResultObjects = getNestedResultObjects(users);

// does not pass now
// will be great, if cursor will use constant memory instead of linear one
// Assertions.assertTrue(nestedResultObjects.size() <= 2);
}
Assertions.assertFalse(nestedResultObjects.isEmpty());

List<Cursor<?>> cursorList = getCursors(sqlSession);
// does not pass now
// will be great, if cursor will use constant memory instead of linear one
// Assertions.assertTrue(nestedResultObjects.size() <= 2);
}

// expect that either reference to the cursor itselfis gone or cursor does not contains all the fetched data
// the most preferrable way will be not to cache data, when the row is already processed (see commented
// line above)
Assertions.assertTrue(
cursorList.isEmpty() || getNestedResultObjects((Cursor<User>) cursorList.get(0)).size() <= 2
);
}
}
List<Cursor<?>> cursorList = getCursors(sqlSession);

private void consumeUser(User user) {
// do nothing
// expect that either reference to the cursor itselfis gone or cursor does not contains all the fetched data
// the most preferrable way will be not to cache data, when the row is already processed (see commented
// line above)
Assertions
.assertTrue(cursorList.isEmpty() || getNestedResultObjects((Cursor<User>) cursorList.get(0)).size() <= 2);
}
}

private void consumeUser(User user) {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,31 +17,31 @@

public class User {

private Integer id;
private String name;
private Friend friend;
private Integer id;
private String name;
private Friend friend;

public Integer getId() {
return id;
}
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}

public Friend getFriend() {
return friend;
}
public Friend getFriend() {
return friend;
}

public void setFriend(Friend friend) {
this.friend = friend;
}
public void setFriend(Friend friend) {
this.friend = friend;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class UnmatchedPropTypeTest {

@BeforeAll
static void setUp() throws Exception {
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/unmatched_prop_type/mybatis-config.xml")) {
try (Reader reader = Resources
.getResourceAsReader("org/apache/ibatis/submitted/unmatched_prop_type/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2009-2022 the original author or authors.
-- Copyright 2009-2023 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2009-2022 the original author or authors.
-- Copyright 2009-2023 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2022 the original author or authors.
Copyright 2009-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

0 comments on commit bce6fca

Please sign in to comment.