comparison src/nabble/model/SiteKey.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 package nabble.model;
2
3 import fschmidt.db.DbDatabase;
4 import fschmidt.db.DbUtils;
5 import fschmidt.db.postgres.DbDatabaseImpl;
6 import fschmidt.db.util.WeakCacheMap;
7 import fschmidt.util.java.Computable;
8 import fschmidt.util.java.SimpleCache;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.sql.Connection;
13 import java.sql.Statement;
14 import java.sql.PreparedStatement;
15 import java.sql.ResultSet;
16 import java.sql.SQLException;
17 import java.util.ArrayList;
18 import java.util.List;
19
20
21 final class SiteKey {
22 private static final Logger logger = LoggerFactory.getLogger(SiteKey.class);
23
24 private final long siteId;
25 private SiteImpl site = null;
26 private SiteGlobal siteGlobal = null;
27 // final Object lastNodeLock = new Object();
28
29 private SiteKey(long siteId) {
30 this.siteId = siteId;
31 }
32
33 public long getId() {
34 return siteId;
35 }
36
37 SiteImpl site() {
38 if( DbUtils.isStale(site) ) {
39 try {
40 site = SiteImpl.getSite(this,siteId);
41 } catch(Db.NoSchema e) {
42 site = null;
43 }
44 }
45 return site;
46 }
47
48 SiteGlobal siteGlobal() {
49 if( DbUtils.isStale(siteGlobal) ) {
50 siteGlobal = SiteGlobal.getSiteGlobal(siteId);
51 }
52 return siteGlobal;
53 }
54
55 String schema() {
56 return "s" + siteId;
57 }
58
59 public DbDatabase getDb() {
60 return Db.db(schema());
61 }
62
63 @Override public String toString() {
64 return "siteKey-"+siteId;
65 }
66
67 private static Computable<Long,SiteKey> cache = new SimpleCache<Long,SiteKey>(new WeakCacheMap<Long,SiteKey>(), new Computable<Long,SiteKey>() {
68 public SiteKey get(Long siteId) {
69 return new SiteKey(siteId);
70 }
71 });
72
73 static SiteKey getInstance(long siteId) {
74 return cache.get(siteId);
75 }
76
77 static List<SiteKey> getSiteKeys(String task) throws SQLException {
78 List<SiteKey> list = new ArrayList<SiteKey>();
79 Connection con = Db.dbGlobal().getConnection();
80 PreparedStatement stmt = con.prepareStatement(
81 "select * from task where task = ?"
82 , ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE
83 );
84 stmt.setString(1,task);
85 ResultSet rs = stmt.executeQuery();
86 while( rs.next() ) {
87 list.add( getInstance( rs.getLong("site_id") ) );
88 rs.deleteRow();
89 }
90 rs.close();
91 stmt.close();
92 con.close();
93 return list;
94 }
95
96 void addTask(String task) {
97 try {
98 Connection con = Db.dbGlobal().getConnection();
99 try {
100 PreparedStatement stmt = con.prepareStatement(
101 "insert into task (site_id,task) values (?,?)"
102 );
103 stmt.setLong( 1, getId() );
104 stmt.setString( 2, task );
105 DbDatabaseImpl.executeUpdateIgnoringDuplicateKeys(stmt);
106 stmt.close();
107 } finally {
108 con.close();
109 }
110 } catch(SQLException e) {
111 throw new RuntimeException(e);
112 }
113 }
114
115 static List<SiteKey> getAllSiteKeys() {
116 try {
117 List<SiteKey> list = new ArrayList<SiteKey>();
118 Connection con = Db.dbGlobal().getConnection();
119 Statement stmt = con.createStatement();
120 ResultSet rs = stmt.executeQuery(
121 "select site_id from site_global"
122 );
123 while( rs.next() ) {
124 list.add( getInstance( rs.getLong("site_id") ) );
125 }
126 rs.close();
127 stmt.close();
128 con.close();
129 return list;
130 } catch(SQLException e) {
131 throw new RuntimeException(e);
132 }
133 }
134
135 }