Skip to content

Commit

Permalink
Merge pull request #5 from kamatama41/kotlin
Browse files Browse the repository at this point in the history
Kotlinize
  • Loading branch information
kamatama41 authored Feb 6, 2017
2 parents 9bb34fc + fe3afbb commit 87aa0a2
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 273 deletions.
19 changes: 17 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
buildscript {
ext.kotlin_version = '1.0.6'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

plugins {
id "com.jfrog.bintray" version "1.1"
id "com.github.jruby-gradle.base" version "0.1.5"
id "java"
}
apply plugin: "kotlin"

import com.github.jrubygradle.JRubyExec
repositories {
mavenCentral()
Expand All @@ -13,12 +25,15 @@ configurations {
provided
}

version = "0.2.0"
version = "0.3.0"
sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
compile "org.embulk:embulk-core:0.8.16"
provided "org.embulk:embulk-core:0.8.16"
testCompile "com.kamatama41:embulk-test-helpers:0.1.1"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile "com.kamatama41:embulk-test-helpers:0.1.3"
// Uncomment when using local embulk-test-helpers (and settings.gradle as well)
//testCompile project(':embulk-test-helpers')
}
Expand Down
181 changes: 0 additions & 181 deletions src/main/java/org/embulk/filter/hash/HashFilterPlugin.java

This file was deleted.

140 changes: 140 additions & 0 deletions src/main/kotlin/org/embulk/filter/hash/HashFilterPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.embulk.filter.hash

import com.google.common.base.Optional
import org.embulk.config.Config
import org.embulk.config.ConfigDefault
import org.embulk.config.ConfigSource
import org.embulk.config.Task
import org.embulk.config.TaskSource
import org.embulk.spi.Column
import org.embulk.spi.DataException
import org.embulk.spi.Exec
import org.embulk.spi.FilterPlugin
import org.embulk.spi.Page
import org.embulk.spi.PageBuilder
import org.embulk.spi.PageOutput
import org.embulk.spi.PageReader
import org.embulk.spi.Schema
import org.embulk.spi.type.Types
import java.security.MessageDigest

class HashFilterPlugin : FilterPlugin {

interface PluginTask : Task {
@Config("columns")
fun getColumns(): List<HashColumn>
}

interface HashColumn : Task {
@Config("name")
fun getName(): String

@Config("algorithm")
@ConfigDefault("\"SHA-256\"")
fun getAlgorithm(): Optional<String>

@Config("new_name")
@ConfigDefault("null")
fun getNewName(): Optional<String>
}

override fun transaction(config: ConfigSource, inputSchema: Schema, control: FilterPlugin.Control) {

val task = config.loadConfig(PluginTask::class.java)
val hashColumnMap = convertHashColumnListToMap(task.getColumns())

val builder = Schema.builder()
inputSchema.columns.forEach { column ->
val hashColumn = hashColumnMap[column.name]
if (hashColumn != null) {
builder.add(hashColumn.getNewName().or(column.name), Types.STRING)
} else {
builder.add(column.name, column.type)
}
}
control.run(task.dump(), builder.build())
}

override fun open(taskSource: TaskSource, inputSchema: Schema,
outputSchema: Schema, output: PageOutput): PageOutput {

val task = taskSource.loadTask(PluginTask::class.java)
val hashColumnMap = convertHashColumnListToMap(task.getColumns())
val outputColumnMap = convertColumnListToMap(outputSchema.columns)

return object : PageOutput {
private val reader = PageReader(inputSchema)
private val builder = PageBuilder(Exec.getBufferAllocator(), outputSchema, output)

override fun add(page: Page) {
reader.setPage(page)
while (reader.nextRecord()) {
setValue()
builder.addRecord()
}
}

private fun setValue() {
for (inputColumn in inputSchema.columns) {
if (reader.isNull(inputColumn)) {
builder.setNull(inputColumn)
continue
}

// Write the original data
val inputValue : Any = when (inputColumn.type) {
Types.STRING -> {
reader.getString(inputColumn).apply { builder.setString(inputColumn, this) }
}
Types.BOOLEAN -> {
reader.getBoolean(inputColumn).apply { builder.setBoolean(inputColumn, this) }
}
Types.DOUBLE -> {
reader.getDouble(inputColumn).apply { builder.setDouble(inputColumn, this) }
}
Types.LONG -> {
reader.getLong(inputColumn).apply { builder.setLong(inputColumn, this) }
}
Types.TIMESTAMP -> {
reader.getTimestamp(inputColumn).apply { builder.setTimestamp(inputColumn, this) }
}
Types.JSON -> {
reader.getJson(inputColumn).apply { builder.setJson(inputColumn, this) }
} else -> {
throw DataException("Unexpected type:" + inputColumn.type)
}
}

// Overwrite the column if it's hash column.
hashColumnMap[inputColumn.name]?.let { hashColumn ->
val outputColumn = outputColumnMap[hashColumn.getNewName().or(inputColumn.name)]
val hashedValue = generateHash(inputValue.toString(), hashColumn.getAlgorithm().get())
builder.setString(outputColumn, hashedValue)
}
}
}

private fun generateHash(value: String, algorithm: String): String {
val md = MessageDigest.getInstance(algorithm)
md.update(value.toByteArray())
return md.digest().joinToString("") { "%02x".format(it) }
}

override fun finish() {
builder.finish()
}

override fun close() {
builder.close()
}
}
}

private fun convertHashColumnListToMap(hashColumns: List<HashColumn>?): Map<String, HashColumn> {
return hashColumns!!.associate { Pair(it.getName(), it) }
}

private fun convertColumnListToMap(columns: List<Column>?): Map<String, Column> {
return columns!!.associate { Pair(it.name, it) }
}
}
Loading

0 comments on commit 87aa0a2

Please sign in to comment.