Skip to content

Port Encodable-based QueryEngine implementation #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 1 addition & 22 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// swift-tools-version: 5.9
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
import PackageDescription

let package = Package(
Expand All @@ -27,7 +26,6 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.20.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.3.0"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.2"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down Expand Up @@ -77,7 +75,6 @@ let package = Package(
.product(name: "Logging", package: "swift-log"),
.product(name: "SystemPackage", package: "swift-system"),
"Helpers",
"Macros",
"SystemSQLite",
]
),
Expand All @@ -100,24 +97,6 @@ let package = Package(
"Helpers",
]
),
.macro(
name: "Macros",
dependencies: [
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
]
),
.testTarget(
name: "MacrosTests",
dependencies: [
"Macros",
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
.systemLibrary(name: "SystemSQLite", pkgConfig: "sqlite3"),
.target(
name: "AsyncProcess",
Expand Down
150 changes: 111 additions & 39 deletions Sources/GeneratorEngine/Cache/CacheKeyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,160 @@
//
//===----------------------------------------------------------------------===//

import Macros

@_exported import protocol Crypto.HashFunction
import struct Foundation.URL
import struct SystemPackage.FilePath

/// Indicates that values of a conforming type can be hashed with an arbitrary hashing function. Unlike `Hashable`,
/// this protocol doesn't utilize random seed values and produces consistent hash values across process launches.
public protocol CacheKeyProtocol {
func hash(with hashFunction: inout some HashFunction)
public protocol CacheKey: Encodable {
}

extension Bool: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension Bool: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
hashFunction.update(data: self ? [1] : [0])
}
}

extension Int: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension Int: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(bufferPointer: $0)
hashFunction.update(data: $0)
}
}
}

extension String: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
var t = String(reflecting: Self.self)
t.withUTF8 {
hashFunction.update(bufferPointer: .init($0))
extension Int8: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
var x = self
x.withUTF8 {
hashFunction.update(bufferPointer: .init($0))
}
}

extension Int16: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension FilePath: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension Int32: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension FilePath.Component: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension Int64: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension URL: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension UInt: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.description.hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension UInt8: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension UInt16: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension UInt32: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension UInt64: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension Optional: CacheKeyProtocol where Wrapped: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension Float: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
if let self {
true.hash(with: &hashFunction)
self.hash(with: &hashFunction)
} else {
false.hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

extension Array: CacheKeyProtocol where Element: CacheKeyProtocol {
public func hash(with hashFunction: inout some HashFunction) {
extension Double: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
for element in self {
element.hash(with: &hashFunction)
withUnsafeBytes(of: self) {
hashFunction.update(data: $0)
}
}
}

@attached(extension, conformances: CacheKeyProtocol, names: named(hash(with:)))
public macro CacheKey() = #externalMacro(module: "Macros", type: "CacheKeyMacro")
extension String: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
var t = String(reflecting: Self.self)
t.withUTF8 {
hashFunction.update(data: $0)
}
var x = self
x.withUTF8 {
hashFunction.update(data: $0)
}
}
}

extension FilePath: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
}
}

extension FilePath.Component: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
}
}

extension URL: CacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.description.hash(with: &hashFunction)
}
}
Loading