Skip to content

Commit d6dc540

Browse files
committed
fix and rename alignTo8Bytes early return
1 parent d478aec commit d6dc540

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

Swift/OmFileFormat/OmBufferedWriter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public final class OmBufferedWriter<FileHandle: OmFileWriterBackend> {
4040
}
4141

4242
/// Add empty space if required to align to 64 bits
43-
func alignTo64Bytes() throws {
43+
func alignTo8Bytes() throws {
4444
let bytesToPadd = 8 - totalBytesWritten % 8
45-
if bytesToPadd == 0 {
45+
if bytesToPadd == 8 {
4646
return
4747
}
4848
try reallocate(minimumCapacity: bytesToPadd)

Swift/OmFileFormat/OmFileReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public struct OmFileReader<Backend: OmFileReaderBackend> {
121121

122122
/// Convert to string array reader if the variable is a string array
123123
public func asStringArray(io_size_max: UInt64 = 65536, io_size_merge: UInt64 = 512) -> OmFileReaderStringArray<Backend>? {
124-
guard dataType == .string_array else {
124+
guard self.dataType == .string_array else {
125125
return nil
126126
}
127127

@@ -497,7 +497,7 @@ extension OmFileReaderBackend {
497497
/// The size to decode a single chunk
498498
let bufferSize = om_decoder_read_buffer_size(decoder)
499499

500-
/// Loop over index blocks and read index data
500+
// Loop over index blocks and read index data
501501
while om_decoder_next_index_read(decoder, &indexRead) {
502502
//print("Read index \(indexRead)")
503503
let indexData = self.getData(offset: Int(indexRead.offset), count: Int(indexRead.count))
@@ -506,7 +506,7 @@ extension OmFileReaderBackend {
506506
om_decoder_init_data_read(&dataRead, &indexRead)
507507

508508
var error: OmError_t = ERROR_OK
509-
/// Loop over data blocks and read compressed data chunks
509+
// Loop over data blocks and read compressed data chunks
510510
while om_decoder_next_data_read(decoder, &dataRead, indexData, indexRead.count, &error) {
511511
//print("ENQUEUE chunk index \(dataRead.chunkIndex)")
512512
let dataReadOffset = dataRead.offset

Swift/OmFileFormat/OmFileWriter.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct OmFileWriter<FileHandle: OmFileWriterBackend> {
3535
guard name.count <= UInt16.max else { fatalError() }
3636
guard children.count <= UInt32.max else { fatalError() }
3737
let type = OmType.dataTypeScalar.toC()
38-
try buffer.alignTo64Bytes()
38+
try buffer.alignTo8Bytes()
3939

4040
let stringLength: UInt64
4141
if type == DATA_TYPE_STRING {
@@ -92,7 +92,7 @@ public struct OmFileWriter<FileHandle: OmFileWriterBackend> {
9292
var name = name
9393
return try name.withUTF8{ name in
9494
guard name.count <= UInt16.max else { fatalError() }
95-
try buffer.alignTo64Bytes()
95+
try buffer.alignTo8Bytes()
9696
let size = om_variable_write_numeric_array_size(UInt16(name.count), UInt32(children.count), UInt64(array.dimensions.count))
9797
let offset = UInt64(buffer.totalBytesWritten)
9898
try buffer.reallocate(minimumCapacity: Int(size))
@@ -106,7 +106,7 @@ public struct OmFileWriter<FileHandle: OmFileWriterBackend> {
106106

107107
public func writeTrailer(rootVariable: OmOffsetSize) throws {
108108
try writeHeaderIfRequired()
109-
try buffer.alignTo64Bytes()
109+
try buffer.alignTo8Bytes()
110110

111111
// write length of JSON
112112
let size = om_trailer_size()
@@ -316,19 +316,24 @@ public final class OmFileWriterStringArray<FileHandle: OmFileWriterBackend> {
316316

317317
public init(dimensions: [UInt64], buffer: OmBufferedWriter<FileHandle>) {
318318
self.dimensions = dimensions
319-
// Allocate space for a lookup table. Needs to be number_of_chunks+1 to store start address and for each chunk then end address
320-
self.lookUpTable = .init(repeating: 0, count: Int(dimensions.reduce(1, *) + 1))
319+
let numberOfStrings = dimensions.reduce(1, *)
320+
// Allocate space for a lookup table.
321+
// Needs to be numberOfStrings+1 to store start of first string
322+
// as first element and for each string then the end address
323+
self.lookUpTable = .init(repeating: 0, count: Int(numberOfStrings + 1))
321324
self.currentPosition = 0
322325
self.buffer = buffer
323326
}
324327

328+
/// Write all strings at once. The array must match the dimensions
325329
public func writeData(array: [String]) throws {
326330
// Verify array size matches dimensions
327331
let expectedSize = dimensions.reduce(1, *)
328332
guard array.count == expectedSize && self.currentPosition == 0 else {
329333
throw OmFileFormatSwiftError.omEncoder(error: "String arrays need to be encoded all at once and must match the dimensions")
330334
}
331335

336+
// Store data start address if this is the first time this read is called
332337
let lutOffset = UInt64(buffer.totalBytesWritten)
333338
if self.currentPosition == 0 {
334339
lookUpTable[0] = lutOffset
@@ -352,7 +357,7 @@ public final class OmFileWriterStringArray<FileHandle: OmFileWriterBackend> {
352357
}
353358

354359
public func finalise() throws -> OmFileWriterArrayFinalised {
355-
try buffer.alignTo64Bytes()
360+
try buffer.alignTo8Bytes()
356361
let lutOffset = buffer.totalBytesWritten
357362

358363
// Write uncompressed LUT

Tests/OmFileFormatTests/OmFileFormatTests.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ import Foundation
359359
try fileWriter.writeTrailer(rootVariable: variable)
360360

361361
let readFn = try MmapFile(fn: FileHandle.openFileReading(file: file))
362-
#expect(readFn.count == 336)
362+
#expect(readFn.count == 328)
363363
let bytes = Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: readFn.getData(offset: 0, count: readFn.count)), count: readFn.count, deallocator: .none).map{ UInt8($0) }
364364
#expect(bytes[0..<3] == [79, 77, 3])
365365
#expect(bytes[3..<3+7] == [115, 116, 114, 105, 110, 103, 49]) // string1
@@ -389,7 +389,7 @@ import Foundation
389389
94, 0, 0, 0, 0, 0, 0, 0,
390390
102, 0, 0, 0, 0, 0, 0, 0
391391
]) // LUT
392-
#expect(bytes[104+14*8..<104+14*8+92] == [ // FIXME: Why +14*8 and not +13*8?
392+
#expect(bytes[104+13*8..<104+13*8+92] == [
393393
22, 4, 4, 0, 0, 0, 0, 0, // data type (1), compression (1), size of name (2), number of children (4)
394394
104, 0, 0, 0, 0, 0, 0, 0, // size of LUT
395395
104, 0, 0, 0, 0, 0, 0, 0, // offset of LUT
@@ -403,12 +403,15 @@ import Foundation
403403
1, 0, 0, 0, 0, 0, 0, 0, // chunk3, should be removed
404404
100, 97, 116, 97
405405
]) // array meta
406-
#expect(bytes[336-24..<336] == [79, 77, 3, 0, 0, 0, 0, 0, 216, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 0, 0, 0, 0]) // trailer
406+
#expect(bytes[328-24..<328] == [79, 77, 3, 0, 0, 0, 0, 0, 208, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 0, 0, 0, 0]) // trailer
407407

408408

409409
let read = try OmFileReader(fn: readFn).asStringArray()!
410-
let a = try read.read(range: [0..<3, 0..<2, 0..<2])
411-
#expect(a == data)
410+
let complete_array = try read.read()
411+
#expect(complete_array == data)
412+
413+
let partial_array = try read.read(range: [0..<3, 0..<2, 0..<1])
414+
#expect(partial_array == ["string1", "string3", "string5", "string7", "", "string11"])
412415
}
413416

414417
@Test func writev3() throws {

0 commit comments

Comments
 (0)