1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*
* MIT License
*
* Copyright (c) 2017 atlarge-research
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package nl.atlarge.opendc.platform
import nl.atlarge.opendc.integration.jpa.transaction
import nl.atlarge.opendc.integration.jpa.schema.Experiment as InternalExperiment
import nl.atlarge.opendc.integration.jpa.schema.ExperimentState
import javax.persistence.EntityManager
import javax.persistence.EntityManagerFactory
/**
* A manager for [Experiment]s received from a JPA database.
*
* @property factory The JPA entity manager factory to create [EntityManager]s to retrieve entities from the database
* from.
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
class JpaExperimentManager(private val factory: EntityManagerFactory): AutoCloseable {
/**
* The entity manager for this experiment.
*/
private var manager: EntityManager = factory.createEntityManager()
/**
* The amount of experiments in the queue. This property makes a call to the database and does therefore not
* run in O(1) time.
*/
val size: Int
get() {
return manager.createQuery("SELECT COUNT(e.id) FROM experiments e WHERE e.state = :s",
java.lang.Long::class.java)
.setParameter("s", ExperimentState.QUEUED)
.singleResult.toInt()
}
/**
* Poll an [Experiment] from the database and claim it.
*
* @return The experiment that has been polled from the database or `null` if there are no experiments in the
* queue.
*/
fun poll(): JpaExperiment? {
var result: JpaExperiment? = null
manager.transaction {
var experiment: InternalExperiment? = null
val results = manager.createQuery("SELECT e FROM experiments e WHERE e.state = :s",
InternalExperiment::class.java)
.setParameter("s", ExperimentState.QUEUED)
.setMaxResults(1)
.resultList
if (!results.isEmpty()) {
experiment = results.first()
experiment!!.state = ExperimentState.CLAIMED
}
result = experiment?.let { JpaExperiment(manager, it) }
}
manager = factory.createEntityManager()
return result
}
/**
* Close this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* `try`-with-resources statement.*
*
* @throws Exception if this resource cannot be closed
*/
override fun close() = manager.close()
}
|