def log_entry(who: str, what: str): if cassy() is None: raise ValueError("db not setup") return cassy().db_insert("logs", { "when": int(round(time.time() * 1000)), "who": who, "what": what })
def save_session(id: uuid.UUID, email: str, first_name: str, surname: str): if len(email) == 0 or len(first_name) == 0 or len(surname) == 0: raise ValueError("session save(): invalid parameter(s)") value_map = { "email": email, "first_name": first_name, "surname": surname, "session": id } cassy().db_insert("session", value_map)
def save_sentence(sentence: Sentence, topic: str): if len(sentence.token_list) == 0 or len(topic) == 0 or len( sentence.sentence_vec) != 300: raise ValueError("invalid sentence or topic") value_map = {"id": sentence.id, "topic": topic} cassy().db_insert("sentence_by_topic", value_map) sentence_json = json.dumps(sentence, cls=JsonSystem) token_map = {"id": sentence.id, "topic": topic, "json_data": sentence_json} cassy().db_insert("sentence_by_id", token_map)
def add_index(sentence_id: uuid.UUID, word: str, tag: str, shard: int, topic: str, offset: int, score: float): # add the index value_set = {"sentence_id": sentence_id, "word": word.lower(), "tag": tag, "shard": shard, "offset": offset, "topic": topic, "score": score} cassy().db_insert("word_index", value_set) # add the unindex # url text, origin text, shard int, word text, kb text, # primary key((url,origin,kb), word, shard) u_value_set = {"sentence_id": sentence_id, "word": word.lower(), "shard": shard} cassy().db_insert("word_unindex", u_value_set)
def save_user(user: User): if len(user.email.strip()) == 0 or len(user.first_name.strip()) == 0 or len(user.surname.strip()) == 0 or \ len(user.password_hash.strip()) == 0: raise ValueError("invalid user object") cassy().db_insert( "user", { "email": user.email.strip().lower(), "first_name": user.first_name, "surname": user.surname, "salt": user.salt, "password_hash": user.password_hash })
def get_session(session: uuid.UUID) -> Session: cols = ["first_name", "surname", "email"] where_map = {"session": session} result_list = cassy().db_select("session", cols, where_map) if len(result_list) == 1: result = result_list[0] return Session(result[0], result[1], result[2], session) return None
def get_user(email: str) -> User: if len(email.strip()) > 0: cols = ["first_name", "surname", "salt", "password_hash"] row_list = cassy().db_select("user", cols, {"email": email.strip().lower()}) if len(row_list) == 1: row = row_list[0] return User(email.strip().lower(), row[0], row[1], row[2], row[3]) return None
def read_unindexes(sentence_id: uuid.UUID): return_list = [] columns = ["word", "shard"] where_map = {"sentence_id": sentence_id} result_rows = cassy().db_select("word_unindex", columns, where_map) for row in result_rows: return_list.append(UnIndex(sentence_id, row[0], row[1])) return return_list
def read_indexes(word: str, topic: str, shard: int): return_list = [] columns = ["sentence_id", "offset", "tag", "score"] where_map = {"word": word.lower(), "shard": shard, "topic": topic} result_rows = cassy().db_select("word_index", columns, where_map) for row in result_rows: return_list.append(Index(row[0], word, row[2], shard, row[1], topic, row[3])) return return_list
def get_sentence_by_id(sentence_id: uuid.UUID) -> (Sentence, str): where_map = {"id": sentence_id} cols = ["json_data", "topic"] row_list = cassy().db_select("sentence_by_id", cols, where_map) if len(row_list) == 1: row = row_list[0] json_dict = json.loads(row[0]) topic = row[1] return sentence_from_dict(json_dict), topic else: return None, ""
def delete_unindex(sentence_id: uuid.UUID): where_map = {"sentence_id": sentence_id} cassy().db_delete("word_unindex", where_map)
def delete_index(topic: str, unidx: UnIndex): where_map = {"topic": topic, "word": unidx.word, "shard": unidx.shard, "sentence_id": unidx.sentence_id} cassy().db_delete("word_index", where_map)
def delete_sentence(sentence_id: uuid.UUID, topic: str) -> str: cassy().db_delete("sentence_by_topic", {"topic": topic, "id": sentence_id}) cassy().db_delete("sentence_by_id", {"id": sentence_id}) return "factoid \"" + str(sentence_id) + "\" removed."
def delete_session(session: uuid.UUID): where_map = {"session": session} cassy().db_delete("session", where_map)