# Flooder (?)
"mastotroll.netz.org",
# Testing/developing installations
- "ngrok.io", "ngrok-free.app",
+ "ngrok.io",
+ "ngrok-free.app",
+ "misskeytest.chn.moe",
]
# Array with pending errors needed to be written to database
# URL for fetching peers
get_peers_url = "/api/v1/instance/peers"
+# Cache for redundant SQL queries
+cache = {}
+
# Connect to database
connection = sqlite3.connect("blocks.db")
cursor = connection.cursor()
re.compile("^[a-f0-9]{7}$"),
]
+##### Cache #####
+
+def is_cache_initialized(key: str) -> bool:
+ return key in cache
+
+def set_all_cache_key(key: str, rows: list, value: any):
+ # DEBUG: print(f"DEBUG: key='{key}',rows()={len(rows)},value[]={type(value)} - CALLED!")
+ if not is_cache_initialized(key):
+ # DEBUG: print(f"DEBUG: Cache for key='{key}' not initialized.")
+ cache[key] = {}
+
+ for sub in rows:
+ # DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}'")
+
+ if isinstance(sub, tuple):
+ cache[key][sub[0]] = value
+ else:
+ print(f"WARNING: Unsupported type row[]='{type(row)}'")
+
+ # DEBUG: print("DEBUG: EXIT!")
+
+def set_cache_key(key: str, sub: str, value: any):
+ if not is_cache_initialized(key):
+ print(f"WARNING: Bad method call, key='{key}' is not initialized yet.")
+ raise Exception(f"Cache for key='{key}' is not initialized, but function called")
+
+ cache[key][sub] = value
+
+def is_cache_key_set(key: str, sub: str) -> bool:
+ if not is_cache_initialized(key):
+ print(f"WARNING: Bad method call, key='{key}' is not initialized yet.")
+ raise Exception(f"Cache for key='{key}' is not initialized, but function called")
+
+ return sub in cache[key]
+
def add_peers(rows: dict) -> list:
# DEBUG: print(f"DEBUG: rows()={len(rows)} - CALLED!")
peers = list()
def is_instance_registered(domain: str) -> bool:
# DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
- # Default is not registered
- registered = False
+ if not is_cache_initialized("is_registered"):
+ print(f"DEBUG: Cache for {__name__} not initialized, fetching all rows ...")
+ try:
+ cursor.execute("SELECT domain FROM instances")
- try:
- cursor.execute(
- "SELECT rowid FROM instances WHERE domain = ? LIMIT 1", [domain]
- )
+ # Check Set all
+ set_all_cache_key("is_registered", cursor.fetchall(), True)
+ except BaseException as e:
+ print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
+ sys.exit(255)
- # Check condition
- registered = cursor.fetchone() != None
- except BaseException as e:
- print(f"ERROR: failed SQL query: last_seen='{last_seen}'blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',first_seen='{first_seen}',last_seen='{last_seen}',exception:'{str(e)}'")
- sys.exit(255)
+ # Is cache found?
+ registered = is_cache_key_set("is_registered", domain)
# DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
return registered
),
)
+ set_cache_key("is_registered", domain, True)
+
for key in nodeinfos:
# DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',nodeinfos[key]={nodeinfos[key]}")
if domain in nodeinfos[key]: