Cleanup previous PR

Return items_ids rather than note_ids as the rest of the API uses
items_ids to reference notes not note_ids. Also allow querying for only
a single field if needed.
This commit is contained in:
Kovid Goyal 2023-09-22 14:45:20 +05:30
parent 4e878b14cc
commit ae029cf06a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 16 additions and 12 deletions

View File

@ -8,6 +8,8 @@ CREATE TABLE notes_db.notes ( id INTEGER PRIMARY KEY AUTOINCREMENT,
UNIQUE(item, colname)
);
CREATE INDEX notes_db.notes_colname_idx ON notes (colname);
CREATE TABLE notes_db.resources (
hash TEXT NOT NULL PRIMARY KEY ON CONFLICT FAIL,
name TEXT NOT NULL UNIQUE ON CONFLICT FAIL

View File

@ -975,8 +975,8 @@ class DB:
def notes_data_for(self, field_name, item_id):
return self.notes.get_note_data(self.conn, field_name, item_id)
def get_notes_id_map(self):
return self.notes.get_note_id_map(self.conn)
def get_all_items_that_have_notes(self, field_name):
return self.notes.get_all_items_that_have_notes(self.conn, field_name)
def set_notes_for(self, field, item_id, doc: str, searchable_text: str, resource_hashes, remove_unused_resources) -> int:
id_val = self.tables[field].id_map[item_id]

View File

@ -686,9 +686,9 @@ class Cache:
' Return all notes data as a dict or None if note does not exist '
return self.backend.notes_data_for(field, item_id)
def get_notes_id_map(self) -> dict:
' Return all item_id for each field assosiated to a notes. '
return self.backend.get_notes_id_map()
def get_all_items_that_have_notes(self, field_name=None) -> set[int] | dict[str, int]:
' Return all item_ids for items that have notes in the specified field or all fields if field_name is None '
return self.backend.get_all_items_that_have_notes(field_name)
@read_api
def field_supports_notes(self, field) -> bool:

View File

@ -9,6 +9,7 @@ import time
import xxhash
from contextlib import suppress
from itertools import count, repeat
from collections import defaultdict
from typing import Optional
from calibre import sanitize_file_name
@ -269,13 +270,13 @@ class Notes:
'resource_hashes': frozenset(self.resources_used_by(conn, note_id)),
}
def get_note_id_map(self, conn):
rslt = {}
for (note_id, field_name) in conn.execute('SELECT id,colname FROM notes_db.notes'):
if field_name not in rslt:
rslt[field_name] = []
rslt[field_name].append(note_id)
return rslt
def get_all_items_that_have_notes(self, conn, field_name=None):
if field_name:
return {item_id for (item_id,) in conn.execute('SELECT item FROM notes_db.notes WHERE colname=?', (field_name,))}
ans = defaultdict(set)
for (note_id, field_name) in conn.execute('SELECT item, colname FROM notes_db.notes'):
ans[field_name].add(note_id)
return ans
def rename_note(self, conn, field_name, old_item_id, new_item_id, new_item_value):
note_id = self.note_id_for(conn, field_name, old_item_id)

View File

@ -117,6 +117,7 @@ def test_cache_api(self: 'NotesTest'):
h1 = cache.add_notes_resource(b'resource1t', 'r1.jpg')
h2 = cache.add_notes_resource(b'resource2t', 'r1.jpg')
cache.set_notes_for('#tags', tag_id, doc, resource_hashes=(h1, h2))
self.ae(cache.get_all_items_that_have_notes(), {'#tags': {tag_id}, 'authors': {author_id}})
self.ae(cache.notes_for('#tags', tag_id), doc)
cache.delete_custom_column('tags')
cache.close()