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 public class XteaController
{ {
@Autowired @Autowired
private XteaService xteaService; private XteaEndpoint xteaService;
@RequestMapping(method = POST) @RequestMapping(method = POST)
public void submit(@RequestBody XteaRequest xteaRequest) public void submit(@RequestBody XteaRequest xteaRequest)

View File

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