Skip to content

Commit

Permalink
⚡ WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Aug 30, 2023
1 parent 4abc520 commit db9e63d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ let package = Package(
.target(name: "VergeMacros", dependencies: ["VergeMacrosPlugin"]),

.target(name: "VergeTiny", dependencies: []),
.target(name: "VergeComparator"),
.target(
name: "Verge",
dependencies: [
"VergeMacros",
"VergeComparator",
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "DequeModule", package: "swift-collections"),
.product(name: "ConcurrencyTaskManager", package: "swift-concurrency-task-manager"),
Expand All @@ -64,6 +66,7 @@ let package = Package(
name: "VergeNormalization",
dependencies: [
"VergeMacros",
"VergeComparator",
.product(name: "HashTreeCollections", package: "swift-collections"),
]
),
Expand Down
1 change: 1 addition & 0 deletions Sources/Verge/Verge.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

@_exported import ConcurrencyTaskManager
@_exported import Combine
@_exported import VergeComparator
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import Foundation
import os.log

// TODO: will rename as Comparator
public protocol Comparison<Input>: Sendable {
associatedtype Input

Expand Down
27 changes: 17 additions & 10 deletions Sources/VergeMacrosPlugin/DatabaseMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ public struct DatabaseTableMacro: Macro {

}

extension DatabaseTableMacro: PeerMacro {
public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {
return []
}

}

extension DatabaseTableMacro: AccessorMacro {
public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
Expand All @@ -230,16 +237,16 @@ extension DatabaseTableMacro: AccessorMacro {
let id = identifier(from: declaration.cast(VariableDeclSyntax.self))

return [
"""
get {
_$\(id)
}
""",
"""
set {
_$\(id) = newValue
}
""" ,
// """
// get {
// _$\(id)
// }
// """,
// """
// set {
// _$\(id) = newValue
// }
// """ ,
]
}

Expand Down
62 changes: 62 additions & 0 deletions Sources/VergeNormalization/Comparison.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import VergeComparator

public enum NormalizedStorageComparisons<Storage: NormalizedStorageType> {

/// True indicates database is not changed
public struct StorageComparison: Comparison {
public typealias Input = Storage

public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {

lhs == rhs

// (lhs._backingStorage.entityUpdatedMarker, lhs._backingStorage.indexUpdatedMarker) == (rhs._backingStorage.entityUpdatedMarker, rhs._backingStorage.indexUpdatedMarker)
}
}

/// Returns true if the table of the entity in database has no changes.
///
/// - Complexity: O(1)
public struct TableComparison<Entity: EntityType>: Comparison {

public typealias Input = Storage

public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {
// lhs._backingStorage.entityBackingStorage.table(Entity.self).updatedMarker == rhs._backingStorage.entityBackingStorage.table(Entity.self).updatedMarker
fatalError()
}

}

/// Returns true if the updates result does not contain the entity.
public struct UpdateComparison<Entity: EntityType>: Comparison {

public typealias Input = Storage


public let entityID: Entity.EntityID

public init(entityID: Entity.EntityID) {
self.entityID = entityID
}

public func callAsFunction(_ lhs: Storage, _ rhs: Storage) -> Bool {

fatalError()

// guard let result = rhs._backingStorage.lastUpdatesResult else {
// return false
// }
// guard !result.wasUpdated(entityID) else {
// return false
// }
// guard !result.wasDeleted(entityID) else {
// return false
// }
// return true

}
}

}

2 changes: 1 addition & 1 deletion Sources/VergeNormalization/NormalizedStorageType.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

public protocol NormalizedStorageType {
public protocol NormalizedStorageType: Equatable {

func finalizeTransaction(transaction: inout ModifyingTransaction<Self>)

Expand Down
6 changes: 3 additions & 3 deletions Sources/VergeNormalization/VergeNormalization+Macros.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

@attached(member, names: arbitrary)
//@attached(member, names: arbitrary)
//@attached(memberAttribute)
@attached(extension, conformances: NormalizedStorageType, Equatable, names: named(Context), named(BBB), arbitrary)
public macro NormalizedStorage() = #externalMacro(module: "VergeMacrosPlugin", type: "DatabaseMacro")

@attached(accessor)
@attached(peer)
public macro TableAccessor() = #externalMacro(module: "VergeMacrosPlugin", type: "DatabaseTableMacro")


Expand All @@ -25,7 +25,7 @@ struct MyDatabase {

private func play() {

var db = MyDatabase.init()
var db = MyDatabase.init(user: .init())

db.performBatchUpdates { t in
t.modifying.user.insert(.init())
Expand Down
41 changes: 39 additions & 2 deletions Sources/VergeNormalizationDerived/DispatcherType+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,49 @@ extension DispatcherType {
_StorageSelector: StorageSelector,
_TableSelector: TableSelector
>(
selector: consuming AbsoluteTableSelector<_StorageSelector, _TableSelector>
selector: consuming AbsoluteTableSelector<_StorageSelector, _TableSelector>,
entityID: _TableSelector.Entity.EntityID
) -> Derived<EntityWrapper<_TableSelector.Entity>>
where
_StorageSelector.Storage == _TableSelector.Storage,
_StorageSelector.Source == Changes<Self.State>
{

derived(SingleEntityPipeline(selector: selector), queue: .passthrough)
// TODO: caching

return derived(
SingleEntityPipeline(
targetIdentifier: entityID,
selector: selector
),
queue: .passthrough
)

}

public func derivedEntity2<
_StorageSelector: StorageSelector,
_TableSelector: TableSelector
>(
selector: consuming AbsoluteTableSelector<_StorageSelector, _TableSelector>,
entityID: _TableSelector.Entity.EntityID
) -> Derived<EntityWrapper<_TableSelector.Entity>>
where
_StorageSelector.Storage == _TableSelector.Storage,
_StorageSelector.Source == Changes<Self.State>
{

// TODO: caching

return derived(
SingleEntityPipeline(
targetIdentifier: entityID,
selector: selector
),
queue: .passthrough
)

}
}

private struct SingleEntityPipeline<
Expand Down Expand Up @@ -48,6 +80,11 @@ where _StorageSelector.Storage == _TableSelector.Storage, _StorageSelector.Sourc
}

func yieldContinuously(_ input: Input) -> Verge.ContinuousResult<EntityWrapper<Entity>> {

guard let previous = input.previous else {
return .new(yield(input))
}

fatalError()
}

Expand Down

0 comments on commit db9e63d

Please sign in to comment.