Fix bugs in `db' module
authorJakob Cornell <jakob+gpg@jcornell.net>
Sat, 6 Nov 2021 00:54:13 +0000 (19:54 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Sat, 6 Nov 2021 00:54:36 +0000 (19:54 -0500)
src/disk_jumble/db.py

index 34c24b62dbc3db6e19ef7bb60c86ea5ac531acc2..61bb2fd89f4e578ca0fad5ec7f6f6e3de9afe0b1 100644 (file)
@@ -92,7 +92,7 @@ class Wrapper:
                                incomplete_edge as (
                                        -- join up incomplete piece info and precompute where the hasher left off within the entity
                                        select
-                                               verify_id, seq, entity_id, hasher_state,
+                                               verify_id, seq, slab.entity_id, hasher_state,
                                                entity_offset + (upper(c.disk_sectors) - lower(slab.disk_sectors)) * sector_size as end_off
                                        from
                                                diskjumble.verify_piece_incomplete
@@ -106,20 +106,20 @@ class Wrapper:
                                        where seq >= all (select seq from diskjumble.verify_piece_content where verify_id = p.verify_id)
                                )
                        select
-                               slab_id, disk_id, disk_sectors, entity_id, entity_offset, crypt_key, verify_id, seq, end_off,
+                               slab_id, disk_id, disk_sectors, slab.entity_id, entity_offset, crypt_key, verify_id, seq, end_off,
                                hasher_state
                        from
                                diskjumble.slab
                                natural left join diskjumble.disk
                                left join incomplete_edge on
                                        incomplete_edge.entity_id = slab.entity_id
-                                       and incomplete_edge.end_off % sector_size == 0
+                                       and incomplete_edge.end_off %% sector_size = 0
                                        and incomplete_edge.end_off <@ int8range(
                                                slab.entity_offset,
                                                slab.entity_offset + (upper(disk_sectors) - lower(disk_sectors)) * sector_size
                                        )
                        where disk_id = %s
-                       order by entity_id, entity_offset, slab_id
+                       order by slab.entity_id, entity_offset, slab_id
                        ;
                """
                with self.conn.cursor() as cursor:
@@ -136,7 +136,7 @@ class Wrapper:
 
                                yield (slab, hasher_ref)
 
-       def get_torrent_info(self, disk_id: int) -> Mapping[bytes, bytes]:
+       def get_torrent_info(self, disk_id: int) -> Iterable[tuple[bytes, bytes]]:
                stmt = """
                        with hashed as (
                                select digest(info, 'sha1') as info_hash, info
@@ -152,7 +152,7 @@ class Wrapper:
                with self.conn.cursor() as cursor:
                        cursor.execute(stmt, (disk_id,))
                        for (info_hash, info) in cursor:
-                               yield (info_hash, info)
+                               yield (bytes(info_hash), bytes(info))
 
        def insert_verify_piece(self, ts: dt.datetime, entity_id: bytes, piece_num: int) -> int:
                """Insert new verify piece, returning the ID of the inserted row."""