Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
JAliEn
JAliEn
Commits
6bbd8d5b
Commit
6bbd8d5b
authored
May 05, 2021
by
Costin Grigoras
Browse files
Implement QUEUE_* dictionary caches
parent
6e3b034d
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/alien/taskQueue/TaskQueueUtils.java
View file @
6bbd8d5b
...
...
@@ -27,6 +27,7 @@ import java.util.Set;
import
java.util.StringTokenizer
;
import
java.util.TreeMap
;
import
java.util.Vector
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
...
...
@@ -2703,61 +2704,73 @@ public class TaskQueueUtils {
}
/**
* @param key
* @param value
* @return value for this key
* @author costing
* @since May 5, 2021
*/
public
static
int
getOrInsertFromLookupTable
(
final
String
key
,
final
String
value
)
{
// FIXME: these values can also be cached
try
(
DBFunctions
db
=
getQueueDB
())
{
if
(
db
==
null
)
return
0
;
db
.
setQueryTimeout
(
60
);
private
static
final
class
LookupTable
extends
GenericLastValuesCache
<
String
,
Integer
>
{
private
static
final
long
serialVersionUID
=
1L
;
final
String
table
=
"QUEUE_"
+
key
.
toUpperCase
();
final
String
id
=
key
+
"id"
;
final
String
q
=
"select "
+
id
+
" from "
+
table
+
" where "
+
key
+
"=?"
;
private
final
String
query
;
private
final
String
insert
;
if
(
logger
.
isLoggable
(
Level
.
FINER
))
logger
.
log
(
Level
.
FINER
,
"Going to get hostId, query: "
+
q
);
/**
* @param key
*
*/
public
LookupTable
(
final
String
key
)
{
query
=
"SELECT "
+
key
.
toLowerCase
()
+
"id FROM QUEUE_"
+
key
+
" WHERE "
+
key
.
toLowerCase
()
+
"=?"
;
insert
=
"INSERT INTO QUEUE_"
+
key
.
toUpperCase
()
+
" ("
+
key
.
toLowerCase
()
+
") VALUES (?)"
;
}
db
.
setReadOnly
(
true
);
db
.
query
(
q
,
false
,
value
);
@Override
protected
int
getMaximumSize
()
{
return
100000
;
}
// the host exists
if
(
db
.
moveNext
())
{
if
(
logger
.
isLoggable
(
Level
.
FINER
))
logger
.
log
(
Level
.
FINER
,
"The host exists: "
+
db
.
geti
(
1
));
@Override
protected
Integer
resolve
(
String
key
)
{
try
(
DBFunctions
db
=
getQueueDB
())
{
if
(
db
==
null
)
return
null
;
return
db
.
geti
(
1
);
}
// host doesn't exist, we insert it
if
(
logger
.
isLoggable
(
Level
.
FINE
))
logger
.
log
(
Level
.
FINE
,
"The host doesn't exist. Inserting..."
);
db
.
setReadOnly
(
true
);
db
.
setQueryTimeout
(
60
);
db
.
setLastGeneratedKey
(
true
);
if
(!
db
.
query
(
query
,
false
,
key
))
return
null
;
final
String
qi
=
"insert into "
+
table
+
" ("
+
key
+
") values (?);"
;
db
.
setReadOnly
(
false
);
final
boolean
ret
=
db
.
query
(
qi
,
false
,
value
);
if
(
db
.
moveNext
())
return
Integer
.
valueOf
(
db
.
geti
(
1
));
if
(
logger
.
isLoggable
(
Level
.
FINE
))
logger
.
log
(
Level
.
FINE
,
qi
+
" with ?="
+
value
+
": "
+
ret
);
db
.
setReadOnly
(
false
);
db
.
setLastGeneratedKey
(
true
);
if
(
ret
)
{
final
int
val
=
db
.
getLastGeneratedKey
().
intValue
();
if
(
logger
.
isLoggable
(
Level
.
FINE
))
logger
.
log
(
Level
.
FINE
,
"Returning: "
+
val
);
if
(!
db
.
query
(
insert
,
false
,
key
))
return
null
;
return
val
;
return
db
.
getLastGeneratedKey
()
;
}
// something went wrong ? :-(
return
0
;
}
}
private
static
ConcurrentHashMap
<
String
,
LookupTable
>
lookupTable
=
new
ConcurrentHashMap
<>();
/**
* @param key
* @param value
* @return value for this key
*/
public
static
int
getOrInsertFromLookupTable
(
final
String
key
,
final
String
value
)
{
LookupTable
cache
=
lookupTable
.
computeIfAbsent
(
key
.
toUpperCase
(),
(
k
)
->
new
LookupTable
(
k
));
Integer
i
=
cache
.
get
(
value
);
if
(
i
!=
null
)
return
i
.
intValue
();
return
0
;
}
/**
* @param host
* @param port
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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