import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.text.*;
import java.util.TimeZone;
class main {
static void dbIndexingCI(Object... params) {
db();
indexConceptFieldsCI(itemPlusArray(db_mainConcepts(), params));
}
static void db() {
conceptsAndBot();
}
// use -10000 for 10 seconds plus slowdown logic
static void db(Integer autoSaveInterval) {
conceptsAndBot(autoSaveInterval);
}
// first parameter can be Concepts
static void indexConceptFieldsCI(Object... params) {
int i = 0;
Concepts concepts;
if (first(params) instanceof Concepts) {
concepts = (Concepts) first(params);
++i;
} else concepts = db_mainConcepts();
for (; i < l(params); i += 2)
indexConceptFieldCI(concepts, (Class) params[i], (String) params[i+1]);
}
static A[] itemPlusArray(A a, A[] l) {
return singlePlusArray(a, l);
}
static volatile Concepts mainConcepts; // Where we create new concepts
static Concepts db_mainConcepts() {
if (mainConcepts == null) {
mainConcepts = new Concepts(getDBProgramID());
mainConcepts.classFinder = _defaultClassFinder();
}
return mainConcepts;
}
volatile static boolean conceptsAndBot_running = false;
static boolean conceptsAndBot_thinOnStart = true;
static void conceptsAndBot() {
conceptsAndBot(null);
}
static void conceptsAndBot(Integer autoSaveInterval) {
if (conceptsAndBot_running) return;
conceptsAndBot_running = true;
try {
ensureDBNotRunning(dbBotStandardName());
} catch (Throwable _e) {
db_mainConcepts().dontSave = true; // SAFETY
throw rethrow(_e); }
db_mainConcepts().persist(autoSaveInterval);
dbBot();
if (conceptsAndBot_thinOnStart) { try {
thinAProgramsBackups(getDBProgramID(), true);
} catch (Throwable __e) { _handleException(__e); }}
}
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(IterableIterator i) {
return first((Iterator) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static Byte first(byte[] l) {
return empty(l) ? null : l[0];
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static ConceptFieldIndexCI indexConceptFieldCI(Class c, String field) {
return indexConceptFieldCI(db_mainConcepts(), c, field);
}
static ConceptFieldIndexCI indexConceptFieldCI(Concepts concepts, Class c, String field) {
ConceptFieldIndexCI idx = getConceptFieldCIIndex(concepts, c, field);
return idx != null ? idx : new ConceptFieldIndexCI(concepts, c, field);
}
static A[] singlePlusArray(A a, A[] l) {
A[] out = newObjectArrayOfSameType(l, l(l)+1);
out[0] = a;
arraycopy(l, 0, out, 1, l(l));
return out;
}
static String getDBProgramID_id;
static String getDBProgramID() {
return nempty(getDBProgramID_id) ? getDBProgramID_id : programIDWithCase();
}
static Object _defaultClassFinder_value = defaultDefaultClassFinder();
static Object _defaultClassFinder() {
return _defaultClassFinder_value;
}
static void ensureDBNotRunning(String name) {
if (hasBot(name)) {
try {
String framesBot = dropSuffix(".", name) + " Frames";
print("Trying to activate frames of running DB: " + framesBot);
if (isOK(sendOpt(framesBot, "activate frames")) && isMainProgram())
cleanKill();
} catch (Throwable __e) { _handleException(__e); }
throw fail("Already running: " + name);
}
}
static void ensureDBNotRunning() {
ensureDBNotRunning(dbBotStandardName());
}
static String dbBotStandardName() {
String home = userHome();
String name = dbBotName(getDBProgramID());
if (neq(home, actualUserHome()))
name += " " + quote(home);
return name + ".";
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static volatile Android3 dbBot_instance;
static Android3 dbBot() {
return dbBot(dbBotStandardName());
}
static Android3 dbBot(String name) {
ensureDBNotRunning(name);
return dbBot_instance = methodsBot2(name, assertNotNull(db_mainConcepts()), db_standardExposedMethods(), db_mainConcepts().lock);
}
static void thinAProgramsBackups(String progID, boolean doIt) {
List files = new ArrayList();
Map ageMap = new HashMap();
java.util.regex.Pattern pat = regexp("^(.*)\\.backup(20\\d\\d)(\\d\\d)(\\d\\d)-(\\d\\d)$");
//print("Processing backups of program " + progID);
File dir = programDir(progID);
for (File f : listFilesNotDirs(dir, newFile(dir, "backups"))) {
String s = f.getName();
java.util.regex.Matcher matcher = pat.matcher(s);
{ if (!(matcher.find())) continue; }
String originalName = matcher.group(1);
{ if (!(eq(originalName, "concepts.structure.gz"))) continue; }
//print("Found backup: " + sfu(matcherGroups(matcher)));
int year = matcherInt(matcher, 2);
int month = matcherInt(matcher, 3);
int day = matcherInt(matcher, 4);
int hour = matcherInt(matcher, 5);
long time = timestampFromYMDH(year, month, day, hour);
double age = ((now()-time)/1000.0/60/60/24);
//print("Age: " + age + " days");
ageMap.put(f, age);
files.add(f);
}
int numDeleted = 0;
sortByMap_inPlace(files, ageMap);
double lastAge = -1;
for (File f : files) {
double age = ageMap.get(f);
if (!thinAProgramsBackups_shouldKeep(age, lastAge)) {
//print("Deleting: " + f);
++numDeleted;
if (doIt) {
print("Deleting: " + f);
f.delete();
}
} else {
//print("Keeping: " + f);
lastAge = age;
}
}
if (numDeleted != 0)
print((doIt ? "Deleted: " : "Would delete: ") + n(numDeleted, "file"));
}
// age = age in days
static boolean thinAProgramsBackups_shouldKeep(double age, double lastAge) {
return defaultAgeBasedBackupRetentionStrategy_shouldKeep(age, lastAge);
}
static volatile PersistableThrowable _handleException_lastException;
static List _handleException_onException = synchroList(ll("printStackTrace2"));
static void _handleException(Throwable e) {
_handleException_lastException = persistableThrowable(e);
Throwable e2 = innerException(e);
if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException)
return;
for (Object f : cloneList(_handleException_onException)) try {
callF(f, e);
} catch (Throwable e3) {
printStackTrace2(e3); // not using pcall here - it could lead to endless loops
}
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
if (o instanceof byte[]) return empty((byte[]) o);
if (o == null) return true;
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(Iterator i) { return i == null || !i.hasNext(); }
static boolean empty(double[] a) { return a == null || a.length == 0; }
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(short[] a) { return a == null || a.length == 0; }
static boolean empty(MultiSet ms) { return ms == null || ms.isEmpty(); }
static boolean empty(File f) { return getFileSize(f) == 0; }
static int iteratorCount_int_close(Iterator i) { try {
int n = 0;
if (i != null) while (i.hasNext()) { i.next(); ++n; }
if (i instanceof AutoCloseable) ((AutoCloseable) i).close();
return n;
} catch (Exception __e) { throw rethrow(__e); } }
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static ConceptFieldIndexCI getConceptFieldCIIndex(Class c, String field) {
return getConceptFieldCIIndex(db_mainConcepts(), c, field);
}
static ConceptFieldIndexCI getConceptFieldCIIndex(Concepts concepts, Class c, String field) {
return (ConceptFieldIndexCI) concepts.getCIFieldIndex(c, field);
}
static A[] newObjectArrayOfSameType(A[] a, int n) {
return (A[]) Array.newInstance(a.getClass().getComponentType(), n);
}
static void arraycopy(Object[] a, Object[] b) {
if (a != null && b != null)
arraycopy(a, 0, b, 0, min(a.length, b.length));
}
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) {
if (n != 0)
System.arraycopy(src, srcPos, dest, destPos, n);
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(Object o) { return !empty(o); }
static String programIDWithCase() {
return nempty(caseID())
? programID() + "/" + quoteUnlessIdentifierOrInteger(caseID())
: programID();
}
static Object defaultDefaultClassFinder() {
return new F1() {
public Class get(String name) {
Class c = findClass_fullName(name);
if (c != null) return c;
if (name.startsWith("loadableUtils.utils$"))
return findClass_fullName("main" + name.substring(19));
return null;
}
};
}
static Throwable printStackTrace2(Throwable e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace2(e));
return e;
}
static void printStackTrace2() {
printStackTrace2(new Throwable());
}
static void printStackTrace2(String msg) {
printStackTrace2(new Throwable(msg));
}
static boolean hasBot(String searchPattern) { try {
DialogIO io = findBot(searchPattern);
if (io != null) {
io.close();
return true;
} else
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static String dropSuffix(String suffix, String s) {
return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal