Example #1
0
 def test_seek(self, file_spec, parallelism):
     with contextlib.closing(
             file_spec(self.create_tempfile, random_access=True)) as files:
         keys = []
         with riegeli.RecordWriter(
                 files.writing_open(),
                 close=files.writing_should_close,
                 assumed_pos=files.writing_assumed_pos,
                 options=record_writer_options(parallelism)) as writer:
             for i in range(23):
                 pos = writer.pos
                 key = writer.write_record_with_key(sample_string(i, 10000))
                 if keys:
                     self.assertGreater(pos, keys[-1])
                 self.assertLessEqual(pos, key)
                 keys.append(key)
             writer.close()
             end_pos = writer.pos
         with riegeli.RecordReader(
                 files.reading_open(),
                 close=files.reading_should_close,
                 assumed_pos=files.reading_assumed_pos) as reader:
             reader.seek(keys[9])
             self.assertGreater(reader.pos, keys[8])
             self.assertLessEqual(reader.pos, keys[9])
             reader.seek(keys[9])
             self.assertGreater(reader.pos, keys[8])
             self.assertLessEqual(reader.pos, keys[9])
             reader.seek(keys[11])
             self.assertGreater(reader.pos, keys[10])
             self.assertLessEqual(reader.pos, keys[11])
             self.assertEqual(reader.read_record(),
                              sample_string(11, 10000))
             reader.seek(keys[9])
             self.assertGreater(reader.pos, keys[8])
             self.assertLessEqual(reader.pos, keys[9])
             self.assertEqual(reader.read_record(), sample_string(9, 10000))
             reader.seek(keys[11])
             self.assertGreater(reader.pos, keys[10])
             self.assertLessEqual(reader.pos, keys[11])
             self.assertEqual(reader.read_record(),
                              sample_string(11, 10000))
             reader.seek(keys[13])
             self.assertGreater(reader.pos, keys[12])
             self.assertLessEqual(reader.pos, keys[13])
             self.assertEqual(reader.read_record(),
                              sample_string(13, 10000))
             reader.seek(riegeli.RecordPosition(0, 0))
             self.assertLessEqual(reader.pos, keys[0])
             self.assertEqual(reader.read_record(), sample_string(0, 10000))
             reader.seek(end_pos)
             self.assertLessEqual(reader.pos, end_pos)
             self.assertIsNone(reader.read_record())
             reader.seek(keys[11])
             self.assertGreater(reader.pos, keys[10])
             self.assertLessEqual(reader.pos, keys[11])
             reader.close()
             self.assertGreater(reader.pos, keys[10])
             self.assertLessEqual(reader.pos, keys[11])
Example #2
0
 def test_seek(self, file_spec, parallelism):
     with contextlib.closing(
             file_spec(self.create_tempfile,
                       random_access=RandomAccess.RANDOM_ACCESS)) as files:
         positions = []
         with riegeli.RecordWriter(
                 files.writing_open(),
                 owns_dest=files.writing_should_close,
                 assumed_pos=files.writing_assumed_pos,
                 options=record_writer_options(parallelism)) as writer:
             for i in range(23):
                 pos = writer.pos
                 writer.write_record(sample_string(i, 10000))
                 canonical_pos = writer.last_pos
                 if positions:
                     self.assertGreater(pos, positions[-1])
                 self.assertLessEqual(pos, canonical_pos)
                 positions.append(canonical_pos)
             writer.close()
             end_pos = writer.pos
         with riegeli.RecordReader(
                 files.reading_open(),
                 owns_src=files.reading_should_close,
                 assumed_pos=files.reading_assumed_pos) as reader:
             reader.seek(positions[9])
             self.assertGreater(reader.pos, positions[8])
             self.assertLessEqual(reader.pos, positions[9])
             reader.seek(positions[9])
             self.assertGreater(reader.pos, positions[8])
             self.assertLessEqual(reader.pos, positions[9])
             reader.seek(positions[11])
             self.assertGreater(reader.pos, positions[10])
             self.assertLessEqual(reader.pos, positions[11])
             self.assertEqual(reader.read_record(),
                              sample_string(11, 10000))
             reader.seek(positions[9])
             self.assertGreater(reader.pos, positions[8])
             self.assertLessEqual(reader.pos, positions[9])
             self.assertEqual(reader.read_record(), sample_string(9, 10000))
             reader.seek(positions[11])
             self.assertGreater(reader.pos, positions[10])
             self.assertLessEqual(reader.pos, positions[11])
             self.assertEqual(reader.read_record(),
                              sample_string(11, 10000))
             reader.seek(positions[13])
             self.assertGreater(reader.pos, positions[12])
             self.assertLessEqual(reader.pos, positions[13])
             self.assertEqual(reader.read_record(),
                              sample_string(13, 10000))
             reader.seek(riegeli.RecordPosition(0, 0))
             self.assertLessEqual(reader.pos, positions[0])
             self.assertEqual(reader.read_record(), sample_string(0, 10000))
             reader.seek(end_pos)
             self.assertLessEqual(reader.pos, end_pos)
             self.assertIsNone(reader.read_record())
             reader.seek(positions[11])
             self.assertGreater(reader.pos, positions[10])
             self.assertLessEqual(reader.pos, positions[11])
             reader.close()
             self.assertGreater(reader.pos, positions[10])
             self.assertLessEqual(reader.pos, positions[11])