Skip to content

Instantly share code, notes, and snippets.

@markflorisson
Created January 30, 2014 15:53
Show Gist options
  • Save markflorisson/8711624 to your computer and use it in GitHub Desktop.
Save markflorisson/8711624 to your computer and use it in GitHub Desktop.
# Abstract types data descriptors
abstract DDesc{T}
abstract LocalDDesc{T} <: DDesc{T}
abstract OOCDDesc{T} <: LocalDDesc{T}
abstract RemoteDDesc{T} <: DDesc{T}
# ---- Data types ---- #
type DyndDDesc{T} <: DDesc{T}
arr :: Array{T}
end
type BLZDDesc{T} <: OOCDDesc{T}
# handle :: File
pos :: Int64
end
type Connection end
type SQLDDesc{T} <: RemoteDDesc{T}
conn :: Connection
query :: String
end
# ---- Example kernel ---- #
function +(x :: DyndDDesc, y :: DyndDDesc)
DyndDDesc(broadcast(+, x.arr, y.arr))
end
function +(x :: SQLDDesc, y :: SQLDDesc)
SQLDDesc(string(x.query, "+", y.query))
end
# ---- Handle different input sources ---- #
+(x :: DDesc, y :: DDesc) = generic_op(+, x, y)
# Generic implementation that re-describes data
function generic_op(op :: Function, args...)
ddesc_type = determine_ddesc(args...)
args = [ convert_dd(arg) for arg = args ]
return op(args...)
end
# ---- Handle different input sources ---- #
determine_ddesc(x :: DyndDDesc, y :: OOCDDesc) = typeof(y)
determine_ddesc(x :: OOCDDesc, y :: DyndDDesc) = typeof(x)
determine_ddesc(x :: LocalDDesc, y :: SQLDDesc) = typeof(x)
determine_ddesc(x :: SQLDDesc, y :: LocalDDesc) = typeof(y)
# ---- Data movement ---- #
function dd_error(arg :: DDesc, dd_type :: DataType)
throw(ArgumentError(string("Don't know how to convert ", arg,
" to ", dd_type)))
end
convert_dd(arg :: DDesc, dd_type :: DataType) = dd_error(arg, dd_type)
function convert_dd(arg :: LocalDDesc, dd_type :: DataType)
if issubtype(dd_type, OOCDDesc)
return BLZDDesc(0) # put something real here
else
dderror(arg, dd_type)
end
end
function convert_dd(arg :: SQLDDesc, dd_type :: DataType)
if issubtype(dd_type, LocalDDesc)
result = BLZDDesc(0) # put something real here
# pull in data here
else
dderror(arg, dd_type)
end
end
# ---- Test ---- #
dynd = DyndDDesc([1, 2, 3])
println(dynd + dynd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment