diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/model/SiteKey.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,135 @@
+package nabble.model;
+
+import fschmidt.db.DbDatabase;
+import fschmidt.db.DbUtils;
+import fschmidt.db.postgres.DbDatabaseImpl;
+import fschmidt.db.util.WeakCacheMap;
+import fschmidt.util.java.Computable;
+import fschmidt.util.java.SimpleCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.Statement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+
+final class SiteKey {
+	private static final Logger logger = LoggerFactory.getLogger(SiteKey.class);
+
+	private final long siteId;
+	private SiteImpl site = null;
+	private SiteGlobal siteGlobal = null;
+//	final Object lastNodeLock = new Object();
+
+	private SiteKey(long siteId) {
+		this.siteId = siteId;
+	}
+
+	public long getId() {
+		return siteId;
+	}
+
+	SiteImpl site() {
+		if( DbUtils.isStale(site) ) {
+			try {
+				site = SiteImpl.getSite(this,siteId);
+			} catch(Db.NoSchema e) {
+				site = null;
+			}
+		}
+		return site;
+	}
+
+	SiteGlobal siteGlobal() {
+		if( DbUtils.isStale(siteGlobal) ) {
+			siteGlobal = SiteGlobal.getSiteGlobal(siteId);
+		}
+		return siteGlobal;
+	}
+
+	String schema() {
+		return "s" + siteId;
+	}
+
+	public DbDatabase getDb() {
+		return Db.db(schema());
+	}
+
+	@Override public String toString() {
+		return "siteKey-"+siteId;
+	}
+
+	private static Computable<Long,SiteKey> cache = new SimpleCache<Long,SiteKey>(new WeakCacheMap<Long,SiteKey>(), new Computable<Long,SiteKey>() {
+		public SiteKey get(Long siteId) {
+			return new SiteKey(siteId);
+		}
+	});
+
+	static SiteKey getInstance(long siteId) {
+		return cache.get(siteId);
+	}
+
+	static List<SiteKey> getSiteKeys(String task) throws SQLException {
+		List<SiteKey> list = new ArrayList<SiteKey>();
+		Connection con = Db.dbGlobal().getConnection();
+		PreparedStatement stmt = con.prepareStatement(
+			"select * from task where task = ?"
+			, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE
+		);
+		stmt.setString(1,task);
+		ResultSet rs = stmt.executeQuery();
+		while( rs.next() ) {
+			list.add( getInstance( rs.getLong("site_id") ) );
+			rs.deleteRow();
+		}
+		rs.close();
+		stmt.close();
+		con.close();
+		return list;
+	}
+
+	void addTask(String task) {
+		try {
+			Connection con = Db.dbGlobal().getConnection();
+			try {
+				PreparedStatement stmt = con.prepareStatement(
+					"insert into task (site_id,task) values (?,?)"
+				);
+				stmt.setLong( 1, getId() );
+				stmt.setString( 2, task );
+				DbDatabaseImpl.executeUpdateIgnoringDuplicateKeys(stmt);
+				stmt.close();
+			} finally {
+				con.close();
+			}
+		} catch(SQLException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	static List<SiteKey> getAllSiteKeys() {
+		try {
+			List<SiteKey> list = new ArrayList<SiteKey>();
+			Connection con = Db.dbGlobal().getConnection();
+			Statement stmt = con.createStatement();
+			ResultSet rs = stmt.executeQuery(
+				"select site_id from site_global"
+			);
+			while( rs.next() ) {
+				list.add( getInstance( rs.getLong("site_id") ) );
+			}
+			rs.close();
+			stmt.close();
+			con.close();
+			return list;
+		} catch(SQLException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+}