Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CityDoctor
CityDoctor2
Commits
7da614ff
Commit
7da614ff
authored
Jul 29, 2025
by
Luna Riegel
Browse files
Refactor: Implement CityObjectCache with Caffeine
parent
3a45b8d9
Pipeline
#11770
passed with stage
in 1 minute and 40 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/database/CityObjectCache.java
View file @
7da614ff
package
de.hft.stuttgart.citydoctor2.database
;
import
com.github.benmanes.caffeine.cache.Caffeine
;
import
com.github.benmanes.caffeine.cache.LoadingCache
;
import
de.hft.stuttgart.citydoctor2.datastructure.CityObject
;
import
de.hft.stuttgart.citydoctor2.datastructure.GmlId
;
import
de.hft.stuttgart.citydoctor2.utils.Pair
;
...
...
@@ -12,57 +14,35 @@ import java.util.concurrent.ConcurrentHashMap;
/**
*
Soft-reference cache for
*
Cache for CityObjects
*/
public
class
CityObjectCache
{
private
final
Map
<
GmlId
,
SoftReference
<
CityObject
>
>
cache
=
new
ConcurrentHashMap
<>()
;
private
final
LoadingCache
<
GmlId
,
CityObject
>
cache
;
public
void
put
(
GmlId
id
,
CityObject
cityObject
)
{
cache
.
put
(
id
,
new
SoftReference
<>(
cityObject
));
}
public
void
cache
(
List
<?
extends
CityObject
>
objects
)
{
public
CityObjectCache
()
{
cache
=
Caffeine
.
newBuilder
().
maximumSize
(
1000
).
build
(
DatabaseHandler:
:
unmarshallCityObject
);
}
public
void
put
(
CityObject
cityObject
)
{
DatabaseHandler
.
marshallCityObject
(
cityObject
);
cache
.
put
(
cityObject
.
getGmlId
(),
cityObject
);
}
public
CityObject
get
(
GmlId
id
)
{
SoftReference
<
CityObject
>
ref
=
cache
.
get
(
id
);
if
(
ref
==
null
)
{
return
null
;
}
CityObject
cityObject
=
ref
.
get
();
if
(
cityObject
==
null
)
{
cityObject
=
DatabaseHandler
.
unmarshallCityObject
(
id
);
if
(
cityObject
==
null
)
{
cache
.
remove
(
id
);
}
else
{
this
.
put
(
id
,
cityObject
);
}
}
return
cityObject
;
return
cache
.
get
(
id
);
}
public
static
List
<
Pair
<
GmlId
,
List
<
GmlId
>>>
getCollidingObjectsList
()
{
List
<
Pair
<
GmlId
,
List
<
GmlId
>>>
collidingObjects
=
new
ArrayList
<>();
for
(
GmlId
id
:
DatabaseHandler
.
getAllGmlIds
())
{
List
<
GmlId
>
collidingIds
=
DatabaseHandler
.
getBBoxIntersectingObjects
(
id
);
if
(
collidingIds
!=
null
&&
!
collidingIds
.
isEmpty
())
{
collidingObjects
.
add
(
new
Pair
<>(
id
,
collidingIds
));
}
public
List
<
CityObject
>
get
(
List
<
GmlId
>
ids
)
{
List
<
CityObject
>
features
=
new
ArrayList
<>();
for
(
GmlId
id
:
ids
)
{
features
.
add
(
cache
.
get
(
id
));
}
return
collidingObject
s
;
return
feature
s
;
}
public
void
remove
(
GmlId
id
)
{
cache
.
remove
(
id
);
}
public
void
clear
()
{
cache
.
clear
();
}
public
int
size
()
{
return
cache
.
size
();
}
}
CityDoctorParent/CityDoctorModel/src/test/java/de/hft/stuttgart/citydoctor2/database/CityObjectCacheTest.java
0 → 100644
View file @
7da614ff
package
de.hft.stuttgart.citydoctor2.database
;
import
de.hft.stuttgart.citydoctor2.datastructure.Building
;
import
de.hft.stuttgart.citydoctor2.datastructure.GeometryTestUtils
;
import
de.hft.stuttgart.citydoctor2.datastructure.GeometryType
;
import
de.hft.stuttgart.citydoctor2.datastructure.Lod
;
import
org.citygml4j.core.model.building.AbstractBuilding
;
import
org.junit.Before
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
mockito
.
Mockito
.
mock
;
public
class
CityObjectCacheTest
{
@BeforeClass
public
static
void
setUpBeforeClass
()
{
DatabaseHandler
.
setupDatabase
();
}
@Before
public
void
resetDatabase
()
{
try
{
DatabaseHandler
.
setupFeaturesTable
();
}
catch
(
Exception
e
)
{
fail
(
e
.
getMessage
());
}
}
@Test
public
void
testPut
(){
Building
b
=
new
Building
();
b
.
setGmlObject
(
mock
(
AbstractBuilding
.
class
));
b
.
addGeometry
(
GeometryTestUtils
.
createDummyGeometry
(
GeometryType
.
SOLID
,
Lod
.
LOD0
));
assertEquals
(
0
,
DatabaseHandler
.
getFeatureCount
());
CityObjectCache
cache
=
new
CityObjectCache
();
cache
.
put
(
b
);
assertEquals
(
1
,
DatabaseHandler
.
getFeatureCount
());
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment