Skip to content

Commit

Permalink
chore: change the API response format to improve the response time (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
duyhungtnn authored Sep 5, 2023
1 parent 0161cdf commit 196c2c9
Show file tree
Hide file tree
Showing 47 changed files with 2,175 additions and 512 deletions.
96 changes: 80 additions & 16 deletions Bucketeer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Bucketeer/Sources/Internal/DI/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class ComponentImpl: Component {
self.dataModule = dataModule
self.evaluationInteractor = EvaluationInteractorImpl(
apiClient: dataModule.apiClient,
evaluationDao: dataModule.evaluationDao,
defaults: dataModule.defaults,
evaluationStorage: dataModule.evaluationStorage,
idGenerator: dataModule.idGenerator,
featureTag: dataModule.config.featureTag
)
Expand Down
16 changes: 12 additions & 4 deletions Bucketeer/Sources/Internal/DI/DataModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ protocol DataModule {
var config: BKTConfig { get }
var userHolder: UserHolder { get }
var apiClient: ApiClient { get }
var evaluationDao: EvaluationDao { get }
var evaluationStorage: EvaluationStorage { get }
var eventDao: EventDao { get }
var defaults: Defaults { get }
var idGenerator: IdGenerator { get }
Expand All @@ -13,18 +13,17 @@ protocol DataModule {
}

final class DataModuleImpl: DataModule {

let user: User
let config: BKTConfig

let sqlite: SQLite
let evaluationDao: EvaluationDao
let eventDao: EventDao

init(user: User, config: BKTConfig) throws {
self.user = user
self.config = config
self.sqlite = try DatabaseOpenHelper.createDatabase(logger: config.logger)
self.evaluationDao = EvaluationDaoImpl(db: sqlite)
self.evaluationDao = EvaluationSQLDao(db: sqlite)
self.eventDao = EventDaoImpl(db: sqlite)
}

Expand All @@ -40,4 +39,13 @@ final class DataModuleImpl: DataModule {
private(set) lazy var userHolder: UserHolder = UserHolder(user: self.user)
private(set) lazy var defaults: Defaults = UserDefaults.standard
private(set) lazy var device: Device = DeviceImpl()
// Evaluation Data Access Layer
private let evaluationDao: EvaluationDao
private lazy var evaluationMemCacheDao: EvaluationMemCacheDao = EvaluationMemCacheDao()
private lazy var evaluationUserDefaultsDao: EvaluationUserDefaultsDao = EvaluationUserDefaultDaoImpl(defaults: defaults)
private(set) lazy var evaluationStorage: EvaluationStorage = EvaluationStorageImpl(
userId: user.id,
evaluationDao: evaluationDao,
evaluationMemCacheDao: evaluationMemCacheDao,
evaluationUserDefaultsDao: evaluationUserDefaultsDao)
}
20 changes: 10 additions & 10 deletions Bucketeer/Sources/Internal/Database/SQLite/SQLite.Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ extension SQLite {
var errorUserInfo: [String: Any] {
switch self {
case .failedToOpen(let info),
.failedToPrepare(let info),
.failedToStep(let info),
.failedToBind(let info),
.failedToFinalize(let info),
.failedToExecute(let info):
.failedToPrepare(let info),
.failedToStep(let info),
.failedToBind(let info),
.failedToFinalize(let info),
.failedToExecute(let info):
return info.userInfo
case .unsupportedType:
return [NSLocalizedDescriptionKey: "Unsupported type"]
Expand All @@ -31,11 +31,11 @@ extension SQLite {
var debugDescription: String {
switch self {
case .failedToOpen(let info),
.failedToPrepare(let info),
.failedToStep(let info),
.failedToBind(let info),
.failedToFinalize(let info),
.failedToExecute(let info):
.failedToPrepare(let info),
.failedToStep(let info),
.failedToBind(let info),
.failedToFinalize(let info),
.failedToExecute(let info):
return info.debugDescription
case .unsupportedType:
return "Unsupported type"
Expand Down
85 changes: 59 additions & 26 deletions Bucketeer/Sources/Internal/Database/SQLite/SQLite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension SQLite {
var _pointer: OpaquePointer?
let result = sqlite3_prepare_v2(pointer, sql, -1, &_pointer, nil)
guard result == SQLITE_OK,
let pointer = _pointer else {
let pointer = _pointer else {
throw Error.failedToPrepare(.init(pointer: pointer, result: result))
}
return .init(pointer: pointer)
Expand All @@ -54,30 +54,30 @@ extension SQLite {

extension SQLite {
var userVersion: Int32 {
get {
do {
let statement = try prepareStatement(sql: "PRAGMA user_version")
try statement.step()
let userVersion = statement.int(at: 0)
try statement.reset()
try statement.finalize()
return userVersion
} catch let error {
logger?.error(error)
return 0
}
}
set {
do {
let statement = try prepareStatement(sql: "PRAGMA user_version = \(newValue)")
repeat {} while try statement.step()
try statement.reset()
try statement.finalize()
} catch let error {
logger?.error(error)
}
}
}
get {
do {
let statement = try prepareStatement(sql: "PRAGMA user_version")
try statement.step()
let userVersion = statement.int(at: 0)
try statement.reset()
try statement.finalize()
return userVersion
} catch let error {
logger?.error(error)
return 0
}
}
set {
do {
let statement = try prepareStatement(sql: "PRAGMA user_version = \(newValue)")
repeat {} while try statement.step()
try statement.reset()
try statement.finalize()
} catch let error {
logger?.error(error)
}
}
}
}

extension SQLite {
Expand All @@ -87,7 +87,7 @@ extension SQLite {
let statement = try prepareStatement(sql: sql)

var models: [Entity.Model] = []
while (try statement.step()) {
while try statement.step() {
do {
let model = try Entity.model(from: statement)
models.append(model)
Expand Down Expand Up @@ -122,4 +122,37 @@ extension SQLite {
try statement.reset()
try statement.finalize()
}

func startTransaction(block: () throws -> Void) throws {
let beginTransactionQuery = "BEGIN;"
let result = sqlite3_exec(pointer, beginTransactionQuery, nil, nil, nil)
guard result == SQLITE_OK else {
debugPrint("Failed to start transaction")
throw Error.failedToExecute(.init(pointer: pointer, result: result))
}

do {
try block()
let commitTransactionQuery = "COMMIT;"
let commitResult = sqlite3_exec(pointer, commitTransactionQuery, nil, nil, nil)
guard commitResult == SQLITE_OK else {
debugPrint("Failed to commit transaction")
throw Error.failedToExecute(.init(pointer: pointer, result: result))
}
} catch {
try rollback()
// forward original error caused the rollback to the caller
throw error
}
}

private func rollback() throws {
let rollbackQuery = "ROLLBACK;"
debugPrint("Transaction rolled back")
let result = sqlite3_exec(pointer, rollbackQuery, nil, nil, nil)
guard result == SQLITE_OK else {
debugPrint("Failed to rollback transaction")
throw Error.failedToExecute(.init(pointer: pointer, result: result))
}
}
}
7 changes: 6 additions & 1 deletion Bucketeer/Sources/Internal/Evaluation/EvaluationDao.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Foundation

// EvaluationDao CURD
protocol EvaluationDao {
func put(userId: String, evaluations: [Evaluation]) throws
func get(userId: String) throws -> [Evaluation]
func deleteAllAndInsert(userId: String, evaluations: [Evaluation]) throws
func deleteAll(userId: String) throws
func deleteByIds(_ ids: [String]) throws
func startTransaction(block: TransactionBlock) throws
}

typealias TransactionBlock = () throws -> Void
Loading

0 comments on commit 196c2c9

Please sign in to comment.