Signed-off-by: James Munson <jmunson@openpoll.io>
This commit is contained in:
James Munson
2019-06-24 04:45:03 -07:00
parent b0c884eda1
commit 1754508b26
2 changed files with 45 additions and 45 deletions

View File

@@ -42,7 +42,7 @@ import org.springframework.web.bind.annotation.RestController;
public class XteaController
{
@Autowired
private XteaService xteaService;
private XteaEndpoint xteaService;
@RequestMapping(method = POST)
public void submit(@RequestBody XteaRequest xteaRequest)

View File

@@ -37,30 +37,30 @@ import org.sql2o.Query;
import org.sql2o.Sql2o;
@Service
public class XteaService
public class XteaEndpoint
{
private static final String CREATE_SQL = "CREATE TABLE IF NOT EXISTS `xtea` (\n"
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
+ " `region` int(11) NOT NULL,\n"
+ " `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n"
+ " `rev` int(11) NOT NULL,\n"
+ " `key1` int(11) NOT NULL,\n"
+ " `key2` int(11) NOT NULL,\n"
+ " `key3` int(11) NOT NULL,\n"
+ " `key4` int(11) NOT NULL,\n"
+ " PRIMARY KEY (`id`),\n"
+ " KEY `region` (`region`,`time`)\n"
+ ") ENGINE=InnoDB";
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
+ " `region` int(11) NOT NULL,\n"
+ " `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n"
+ " `rev` int(11) NOT NULL,\n"
+ " `key1` int(11) NOT NULL,\n"
+ " `key2` int(11) NOT NULL,\n"
+ " `key3` int(11) NOT NULL,\n"
+ " `key4` int(11) NOT NULL,\n"
+ " PRIMARY KEY (`id`),\n"
+ " KEY `region` (`region`,`time`)\n"
+ ") ENGINE=InnoDB";
private final Sql2o sql2o;
private final Cache<Integer, XteaCache> keyCache = CacheBuilder.newBuilder()
.maximumSize(1024)
.build();
.maximumSize(1024)
.build();
@Autowired
public XteaService(
@Qualifier("Runelite SQL2O") Sql2o sql2o
public XteaEndpoint(
@Qualifier("Runelite SQL2O") Sql2o sql2o
)
{
this.sql2o = sql2o;
@@ -68,18 +68,18 @@ public class XteaService
try (Connection con = sql2o.beginTransaction())
{
con.createQuery(CREATE_SQL)
.executeUpdate();
.executeUpdate();
}
}
private XteaEntry findLatestXtea(Connection con, int region)
{
return con.createQuery("select region, time, key1, key2, key3, key4 from xtea "
+ "where region = :region "
+ "order by time desc "
+ "limit 1")
.addParameter("region", region)
.executeAndFetchFirst(XteaEntry.class);
+ "where region = :region "
+ "order by time desc "
+ "limit 1")
.addParameter("region", region)
.executeAndFetchFirst(XteaEntry.class);
}
public void submit(XteaRequest xteaRequest)
@@ -92,10 +92,10 @@ public class XteaService
XteaCache xteaCache = keyCache.getIfPresent(region);
if (xteaCache == null
|| xteaCache.getKey1() != keys[0]
|| xteaCache.getKey2() != keys[1]
|| xteaCache.getKey3() != keys[2]
|| xteaCache.getKey4() != keys[3])
|| xteaCache.getKey1() != keys[0]
|| xteaCache.getKey2() != keys[1]
|| xteaCache.getKey3() != keys[2]
|| xteaCache.getKey4() != keys[3])
{
cached = false;
keyCache.put(region, new XteaCache(region, keys[0], keys[1], keys[2], keys[3]));
@@ -126,10 +126,10 @@ public class XteaService
// already have these?
// TODO : check if useful / works should check with findLatestXtea
if (xteaEntry != null
&& xteaEntry.getKey1() == keys[0]
&& xteaEntry.getKey2() == keys[1]
&& xteaEntry.getKey3() == keys[2]
&& xteaEntry.getKey4() == keys[3])
&& xteaEntry.getKey1() == keys[0]
&& xteaEntry.getKey2() == keys[1]
&& xteaEntry.getKey3() == keys[2]
&& xteaEntry.getKey4() == keys[3])
{
continue;
}
@@ -138,16 +138,16 @@ public class XteaService
if (query == null)
{
query = con.createQuery("insert into xtea (region, rev, key1, key2, key3, key4) "
+ "values (:region, :rev, :key1, :key2, :key3, :key4)");
+ "values (:region, :rev, :key1, :key2, :key3, :key4)");
}
query.addParameter("region", region)
.addParameter("rev", xteaRequest.getRevision())
.addParameter("key1", keys[0])
.addParameter("key2", keys[1])
.addParameter("key3", keys[2])
.addParameter("key4", keys[3])
.addToBatch();
.addParameter("rev", xteaRequest.getRevision())
.addParameter("key1", keys[0])
.addParameter("key2", keys[1])
.addParameter("key3", keys[2])
.addParameter("key4", keys[3])
.addToBatch();
}
if (query != null)
@@ -163,10 +163,10 @@ public class XteaService
try (Connection con = sql2o.open())
{
return con.createQuery(
"select t1.region, t2.time, t2.rev, t2.key1, t2.key2, t2.key3, t2.key4 from " +
"(select region,max(id) as id from xtea group by region) t1 " +
"join xtea t2 on t1.id = t2.id")
.executeAndFetch(XteaEntry.class);
"select t1.region, t2.time, t2.rev, t2.key1, t2.key2, t2.key3, t2.key4 from " +
"(select region,max(id) as id from xtea group by region) t1 " +
"join xtea t2 on t1.id = t2.id")
.executeAndFetch(XteaEntry.class);
}
}
@@ -175,9 +175,9 @@ public class XteaService
try (Connection con = sql2o.open())
{
return con.createQuery("select region, time, rev, key1, key2, key3, key4 from xtea "
+ "where region = :region order by time desc limit 1")
.addParameter("region", region)
.executeAndFetchFirst(XteaEntry.class);
+ "where region = :region order by time desc limit 1")
.addParameter("region", region)
.executeAndFetchFirst(XteaEntry.class);
}
}
}