/
SqlStringIndex.java
167 lines (143 loc) · 5.94 KB
/
SqlStringIndex.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* $Id$
*
* Copyright (c) 2011-2013 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.simsilica.es.sql;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.sql.*;
import com.simsilica.es.StringIndex;
import com.simsilica.util.ReportSystem;
import com.simsilica.util.Reporter;
/**
* Sql-based StringIndex implementation with basic LRU cache.
*
* @author Paul Speed
*/
public class SqlStringIndex implements StringIndex {
private SqlEntityData parent;
private StringTable stringTable;
private Cache<Integer,String> idToString;
private Cache<String,Integer> stringToId;
public SqlStringIndex( SqlEntityData parent, int cacheSize ) {
this.parent = parent;
this.idToString = CacheBuilder.newBuilder().maximumSize(cacheSize).build();
this.stringToId = CacheBuilder.newBuilder().maximumSize(cacheSize).build();
ReportSystem.registerCacheReporter(new CacheReporter());
try {
this.stringTable = StringTable.create(parent.getSession());
} catch( SQLException e ) {
throw new RuntimeException("Error creating string table", e);
}
}
protected SqlSession getSession() throws SQLException {
return parent.getSession();
}
// A safe lookup with no adds... easier than try/catching all
// over the place
protected int lookupId( String s ) {
try {
return stringTable.getStringId(getSession(), s, false);
} catch( SQLException e ) {
throw new RuntimeException("Error getting string ID for:" + s, e);
}
}
@Override
public int getStringId( String s, boolean add ) {
Integer result = stringToId.getIfPresent(s);
if( result != null ) {
return result;
}
// Try a naked lookup
int i = lookupId(s);
if( i < 0 && add ) {
synchronized( this ) {
// Check the cache again... the string may have been added
// while we were waiting for the synch and if so then it will
// still be cached (presuming we aren't tearing through ID lookups
// like madmen.)
result = stringToId.getIfPresent(s);
if( result != null ) {
return result;
}
try {
i = stringTable.getStringId(getSession(), s, add);
if( i < 0 ) {
return -1;
}
// For the easy double-checked locking to work, we
// need to cache inside the synch block. We could have
// just done another DB look-up above and then avoided this
// but this might be a little faster in heavy-contention.
stringToId.put(s, i);
idToString.put(i, s);
return i;
} catch( SQLException e ) {
throw new RuntimeException("Error getting string ID for:" + s, e);
}
}
}
// If we weren't adding above then it might still be negative
if( i < 0 ) {
return -1;
}
stringToId.put(s, i);
idToString.put(i, s);
return i;
}
@Override
public String getString( int id ) {
String result = idToString.getIfPresent(id);
if( result != null ) {
return result;
}
try {
result = stringTable.getString(getSession(), id);
if( result != null ) {
idToString.put(id, result);
stringToId.put(result, id);
}
return result;
} catch( SQLException e ) {
throw new RuntimeException("Error getting string for ID:" + id, e);
}
}
private class CacheReporter implements Reporter {
@Override
public void printReport( String type, java.io.PrintWriter out ) {
out.println("SqlStringIndex->id to string:" + idToString.size()
+ " stats:" + idToString.stats());
out.println("SqlStringIndex->string to id:" + stringToId.size()
+ " stats:" + stringToId.stats());
}
}
}