Skip to content

Commit

Permalink
Refactor interface/driver getters for IoRead/Write and StorageRead/Wr…
Browse files Browse the repository at this point in the history
…ite.

Add getters where needed and remove the unused driver member from the StorageRead/Write objects. The new getters are only required for testing but they don't compromise the core code.
  • Loading branch information
dwsteele committed Aug 20, 2024
1 parent a71d884 commit 2ce01e5
Show file tree
Hide file tree
Showing 33 changed files with 158 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/command/archive/get/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Archive Get Command
#include "protocol/helper.h"
#include "protocol/parallel.h"
#include "storage/helper.h"
#include "storage/write.intern.h"
#include "storage/write.h"

/***********************************************************************************************************************************
Constants for log messages that are used multiple times to keep them consistent
Expand Down
2 changes: 1 addition & 1 deletion src/command/archive/get/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Archive Get Protocol Handler
#include "common/memContext.h"
#include "config/config.h"
#include "storage/helper.h"
#include "storage/write.intern.h"
#include "storage/write.h"

/**********************************************************************************************************************************/
FN_EXTERN ProtocolServerResult *
Expand Down
2 changes: 1 addition & 1 deletion src/command/restore/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Restore Command
#include "protocol/helper.h"
#include "protocol/parallel.h"
#include "storage/helper.h"
#include "storage/write.intern.h"
#include "storage/write.h"
#include "version.h"

/***********************************************************************************************************************************
Expand Down
16 changes: 8 additions & 8 deletions src/common/io/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ioReadOpen(IoRead *const this)
ASSERT(ioFilterGroupSize(this->pub.filterGroup) == 0 || !ioReadBlock(this));

// Open if the driver has an open function
const bool result = this->pub.interface.open != NULL ? this->pub.interface.open(this->pub.driver) : true;
const bool result = ioReadInterface(this)->open != NULL ? ioReadInterface(this)->open(ioReadDriver(this)) : true;

// Only open the filter group if the read was opened
if (result)
Expand Down Expand Up @@ -92,7 +92,7 @@ ioReadEofDriver(const IoRead *const this)
ASSERT(this != NULL);
ASSERT(this->pub.opened && !this->pub.closed);

FUNCTION_LOG_RETURN(BOOL, this->pub.interface.eof != NULL ? this->pub.interface.eof(this->pub.driver) : false);
FUNCTION_LOG_RETURN(BOOL, ioReadInterface(this)->eof != NULL ? ioReadInterface(this)->eof(ioReadDriver(this)) : false);
}

/**********************************************************************************************************************************/
Expand Down Expand Up @@ -133,7 +133,7 @@ ioReadInternal(IoRead *const this, Buffer *const buffer, const bool block)
if (ioReadBlock(this) && bufRemains(this->input) > bufRemains(buffer))
bufLimitSet(this->input, bufRemains(buffer));

this->pub.interface.read(this->pub.driver, this->input, block);
ioReadInterface(this)->read(ioReadDriver(this), this->input, block);
bufLimitClear(this->input);
}
// Set input to NULL and flush (no need to actually free the buffer here as it will be freed with the mem context)
Expand Down Expand Up @@ -421,8 +421,8 @@ ioReadReady(IoRead *const this, const IoReadReadyParam param)

bool result = true;

if (this->pub.interface.ready != NULL)
result = this->pub.interface.ready(this->pub.driver, param.error);
if (ioReadInterface(this)->ready != NULL)
result = ioReadInterface(this)->ready(ioReadDriver(this), param.error);

FUNCTION_LOG_RETURN(BOOL, result);
}
Expand Down Expand Up @@ -478,8 +478,8 @@ ioReadClose(IoRead *const this)
ioFilterGroupClose(this->pub.filterGroup);

// Close the driver if there is a close function
if (this->pub.interface.close != NULL)
this->pub.interface.close(this->pub.driver);
if (ioReadInterface(this)->close != NULL)
ioReadInterface(this)->close(ioReadDriver(this));

#ifdef DEBUG
this->pub.closed = true;
Expand All @@ -498,5 +498,5 @@ ioReadFd(const IoRead *const this)

ASSERT(this != NULL);

FUNCTION_LOG_RETURN(INT, this->pub.interface.fd == NULL ? -1 : this->pub.interface.fd(this->pub.driver));
FUNCTION_LOG_RETURN(INT, ioReadInterface(this)->fd == NULL ? -1 : ioReadInterface(this)->fd(ioReadDriver(this)));
}
2 changes: 1 addition & 1 deletion src/common/io/read.intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ typedef struct IoReadPub

// Driver for the read object
FN_INLINE_ALWAYS void *
ioReadDriver(IoRead *const this)
ioReadDriver(const IoRead *const this)
{
return THIS_PUB(IoRead)->driver;
}
Expand Down
26 changes: 12 additions & 14 deletions src/common/io/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Object type
struct IoWrite
{
IoWritePub pub; // Publicly accessible variables
void *driver; // Driver object
IoWriteInterface interface; // Driver interface
Buffer *output; // Output buffer

#ifdef DEBUG
Expand Down Expand Up @@ -45,10 +43,10 @@ ioWriteNew(void *const driver, const IoWriteInterface interface)
{
.pub =
{
.driver = objMoveToInterface(driver, this, memContextPrior()),
.interface = interface,
.filterGroup = ioFilterGroupNew(),
},
.driver = objMoveToInterface(driver, this, memContextPrior()),
.interface = interface,
.output = bufNew(ioBufferSize()),
};
}
Expand All @@ -68,8 +66,8 @@ ioWriteOpen(IoWrite *const this)
ASSERT(this != NULL);
ASSERT(!this->opened && !this->closed);

if (this->interface.open != NULL)
this->interface.open(this->driver);
if (ioWriteInterface(this)->open != NULL)
ioWriteInterface(this)->open(ioWriteDriver(this));

// Track whether filters were added to prevent flush() from being called later since flush() won't work with most filters
#ifdef DEBUG
Expand Down Expand Up @@ -108,7 +106,7 @@ ioWrite(IoWrite *const this, const Buffer *const buffer)
// Write data if the buffer is full
if (bufRemains(this->output) == 0)
{
this->interface.write(this->driver, this->output);
ioWriteInterface(this)->write(ioWriteDriver(this), this->output);
bufUsedZero(this->output);
}
}
Expand Down Expand Up @@ -149,8 +147,8 @@ ioWriteReady(IoWrite *const this, const IoWriteReadyParam param)

bool result = true;

if (this->interface.ready != NULL)
result = this->interface.ready(this->driver, param.error);
if (ioWriteInterface(this)->ready != NULL)
result = ioWriteInterface(this)->ready(ioWriteDriver(this), param.error);

FUNCTION_LOG_RETURN(BOOL, result);
}
Expand Down Expand Up @@ -224,7 +222,7 @@ ioWriteFlush(IoWrite *const this)

if (!bufEmpty(this->output))
{
this->interface.write(this->driver, this->output);
ioWriteInterface(this)->write(ioWriteDriver(this), this->output);
bufUsedZero(this->output);
}

Expand All @@ -250,7 +248,7 @@ ioWriteClose(IoWrite *const this)
// Write data if the buffer is full or if this is the last buffer to be written
if (bufRemains(this->output) == 0 || (ioFilterGroupDone(this->pub.filterGroup) && !bufEmpty(this->output)))
{
this->interface.write(this->driver, this->output);
ioWriteInterface(this)->write(ioWriteDriver(this), this->output);
bufUsedZero(this->output);
}
}
Expand All @@ -260,8 +258,8 @@ ioWriteClose(IoWrite *const this)
ioFilterGroupClose(this->pub.filterGroup);

// Close the driver if there is a close function
if (this->interface.close != NULL)
this->interface.close(this->driver);
if (ioWriteInterface(this)->close != NULL)
ioWriteInterface(this)->close(ioWriteDriver(this));

#ifdef DEBUG
this->closed = true;
Expand All @@ -280,5 +278,5 @@ ioWriteFd(const IoWrite *const this)

ASSERT(this != NULL);

FUNCTION_LOG_RETURN(INT, this->interface.fd == NULL ? -1 : this->interface.fd(this->driver));
FUNCTION_LOG_RETURN(INT, ioWriteInterface(this)->fd == NULL ? -1 : ioWriteInterface(this)->fd(ioWriteDriver(this)));
}
5 changes: 0 additions & 5 deletions src/common/io/write.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ typedef struct IoWrite IoWrite;
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
typedef struct IoWritePub
{
IoFilterGroup *filterGroup; // IO filters
} IoWritePub;

// Filter group. Filters must be set before open and cannot be reset
FN_INLINE_ALWAYS IoFilterGroup *
ioWriteFilterGroup(IoWrite *const this)
Expand Down
24 changes: 24 additions & 0 deletions src/common/io/write.intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ typedef struct IoWriteInterface

FN_EXTERN IoWrite *ioWriteNew(void *driver, IoWriteInterface interface);

/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
typedef struct IoWritePub
{
void *driver; // Driver object
IoWriteInterface interface; // Driver interface
IoFilterGroup *filterGroup; // IO filters
} IoWritePub;

// Driver for the write object
FN_INLINE_ALWAYS void *
ioWriteDriver(const IoWrite *const this)
{
return THIS_PUB(IoWrite)->driver;
}

// Interface for the write object
FN_INLINE_ALWAYS const IoWriteInterface *
ioWriteInterface(const IoWrite *const this)
{
return &THIS_PUB(IoWrite)->interface;
}

/***********************************************************************************************************************************
Macros for function logging
***********************************************************************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/storage/azure/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Azure Storage Read
#include "common/log.h"
#include "common/type/object.h"
#include "storage/azure/read.h"
#include "storage/read.intern.h"
#include "storage/read.h"

/***********************************************************************************************************************************
Object type
Expand Down
2 changes: 1 addition & 1 deletion src/storage/azure/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Azure Storage File Write
#include "common/type/object.h"
#include "common/type/xml.h"
#include "storage/azure/write.h"
#include "storage/write.intern.h"
#include "storage/write.h"

/***********************************************************************************************************************************
Azure HTTP headers
Expand Down
2 changes: 1 addition & 1 deletion src/storage/gcs/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GCS Storage Read
#include "common/log.h"
#include "common/type/object.h"
#include "storage/gcs/read.h"
#include "storage/read.intern.h"
#include "storage/read.h"

/***********************************************************************************************************************************
GCS query tokens
Expand Down
2 changes: 1 addition & 1 deletion src/storage/gcs/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GCS Storage File Write
#include "common/type/keyValue.h"
#include "common/type/object.h"
#include "storage/gcs/write.h"
#include "storage/write.intern.h"
#include "storage/write.h"

/***********************************************************************************************************************************
GCS query tokens
Expand Down
2 changes: 1 addition & 1 deletion src/storage/posix/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Posix Storage Read
#include "common/log.h"
#include "common/type/object.h"
#include "storage/posix/read.h"
#include "storage/read.intern.h"
#include "storage/read.h"

/***********************************************************************************************************************************
Object types
Expand Down
2 changes: 1 addition & 1 deletion src/storage/posix/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Posix Storage File write
#include "common/type/object.h"
#include "common/user.h"
#include "storage/posix/write.h"
#include "storage/write.intern.h"
#include "storage/write.h"

/***********************************************************************************************************************************
Object type
Expand Down
2 changes: 0 additions & 2 deletions src/storage/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Object type
struct StorageRead
{
StorageReadPub pub; // Publicly accessible variables
void *driver; // Driver
};

/***********************************************************************************************************************************
Expand Down Expand Up @@ -48,7 +47,6 @@ storageReadNew(void *const driver, const StorageReadInterface *const interface)
.interface = interface,
.io = ioReadNew(driver, interface->ioInterface),
},
.driver = objMoveToInterface(driver, this, memContextPrior()),
};
}
OBJ_NEW_END();
Expand Down
16 changes: 5 additions & 11 deletions src/storage/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ storageReadMove(StorageRead *const this, MemContext *const parentNew)
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
typedef struct StorageReadPub
{
const StorageReadInterface *interface; // File data (name, driver type, etc.)
IoRead *io; // Read interface
} StorageReadPub;

// Should a missing file be ignored?
FN_INLINE_ALWAYS bool
storageReadIgnoreMissing(const StorageRead *const this)
{
return THIS_PUB(StorageRead)->interface->ignoreMissing;
return storageReadInterface(this)->ignoreMissing;
}

// Read interface
Expand All @@ -50,28 +44,28 @@ storageReadIo(const StorageRead *const this)
FN_INLINE_ALWAYS const Variant *
storageReadLimit(const StorageRead *const this)
{
return THIS_PUB(StorageRead)->interface->limit;
return storageReadInterface(this)->limit;
}

// File name
FN_INLINE_ALWAYS const String *
storageReadName(const StorageRead *const this)
{
return THIS_PUB(StorageRead)->interface->name;
return storageReadInterface(this)->name;
}

// Is there a read limit? NULL for no limit.
FN_INLINE_ALWAYS uint64_t
storageReadOffset(const StorageRead *const this)
{
return THIS_PUB(StorageRead)->interface->offset;
return storageReadInterface(this)->offset;
}

// Get file type
FN_INLINE_ALWAYS StringId
storageReadType(const StorageRead *const this)
{
return THIS_PUB(StorageRead)->interface->type;
return storageReadInterface(this)->type;
}

/***********************************************************************************************************************************
Expand Down
20 changes: 18 additions & 2 deletions src/storage/read.intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ typedef struct StorageReadInterface
bool ignoreMissing;
uint64_t offset; // Where to start reading in the file
const Variant *limit; // Limit how many bytes are read (NULL for no limit)
bool version; // Read version
const String *versionId; // File version to read
IoReadInterface ioInterface;
} StorageReadInterface;

#include "storage/read.h"

FN_EXTERN StorageRead *storageReadNew(void *driver, const StorageReadInterface *interface);

/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
typedef struct StorageReadPub
{
const StorageReadInterface *interface; // File data (name, driver type, etc.)
IoRead *io; // Read interface
} StorageReadPub;

// Read interface
FN_INLINE_ALWAYS const StorageReadInterface *
storageReadInterface(const StorageRead *const this)
{
return THIS_PUB(StorageRead)->interface;
}

#endif
2 changes: 1 addition & 1 deletion src/storage/remote/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Remote Storage Read
#include "common/log.h"
#include "common/type/convert.h"
#include "common/type/object.h"
#include "storage/read.intern.h"
#include "storage/read.h"
#include "storage/remote/protocol.h"
#include "storage/remote/read.h"

Expand Down
2 changes: 1 addition & 1 deletion src/storage/remote/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Remote Storage File write
#include "common/type/object.h"
#include "storage/remote/protocol.h"
#include "storage/remote/write.h"
#include "storage/write.intern.h"
#include "storage/write.h"

/***********************************************************************************************************************************
Object type
Expand Down
Loading

0 comments on commit 2ce01e5

Please sign in to comment.