Skip to content

Instantly share code, notes, and snippets.

@MMcM
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MMcM/970f15c626a827aacaa8 to your computer and use it in GitHub Desktop.
Save MMcM/970f15c626a827aacaa8 to your computer and use it in GitHub Desktop.
Stored procedures to access key-value store from SQL layer transaction
package com.foundationdb.example;
import com.foundationdb.Transaction;
import com.foundationdb.directory.DirectoryLayer;
import com.foundationdb.server.store.FDBTransactionService.TransactionState;
import com.foundationdb.server.store.FDBTransactionService;
import com.foundationdb.sql.server.ServerCallContextStack;
import com.foundationdb.sql.server.ServerQueryContext;
import java.util.Arrays;
public class FDBRoutines
{
protected static TransactionState currentTransaction() {
ServerQueryContext context = ServerCallContextStack.getCallingContext();
return ((FDBTransactionService)context.getServer().getTransactionService())
.getTransaction(context.getSession());
}
public static void getValue(byte[] key, byte[][] value) {
value[0] = currentTransaction().getValue(key);
}
public static void setValue(byte[] key, byte[] value) {
currentTransaction().setBytes(key, value);
}
public static void getDirectory(String path, byte[][] key) {
Transaction tr = currentTransaction().getTransaction();
key[0] = new DirectoryLayer()
.createOrOpen(tr, Arrays.asList(path.split("/")))
.get()
.getKey();
}
}
SQLActions[] = {
"BEGIN INSTALL
CREATE OR REPLACE PROCEDURE fdb.get_value(IN k VARCHAR(1024) FOR BIT DATA, OUT v VARCHAR(1024) FOR BIT DATA)
LANGUAGE java PARAMETER STYLE java
EXTERNAL NAME 'thisjar:com.foundationdb.example.FDBRoutines.getValue';
CREATE OR REPLACE PROCEDURE fdb.set_value(IN k VARCHAR(1024) FOR BIT DATA, IN v VARCHAR(1024) FOR BIT DATA)
LANGUAGE java PARAMETER STYLE java
EXTERNAL NAME 'thisjar:com.foundationdb.example.FDBRoutines.setValue';
CREATE OR REPLACE PROCEDURE fdb.get_directory(IN name VARCHAR(1024), OUT k VARCHAR(1024) FOR BIT DATA)
LANGUAGE java PARAMETER STYLE java
EXTERNAL NAME 'thisjar:com.foundationdb.example.FDBRoutines.getDirectory';
END INSTALL",
"BEGIN REMOVE
DROP PROCEDURE fdb.get_value;
DROP PROCEDURE fdb.set_value;
DROP PROCEDURE fdb.get_directory;
END REMOVE"
}
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.foundationdb</groupId>
<artifactId>sql-layer-kv-routine-example</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>FoundationDB SQL Layer FDB Routines</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>https://gist.github.com/MMcM/970f15c626a827aacaa8</url>
</scm>
<dependencies>
<dependency>
<groupId>com.foundationdb</groupId>
<artifactId>fdb-sql-layer</artifactId>
<version>1.9.6-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestSections>
<manifestSection>
<name>install.ddr</name>
<manifestEntries>
<SQLJDeploymentDescriptor>TRUE</SQLJDeploymentDescriptor>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
CALL sqlj.install_jar('/path/to/sql-layer-kv-routine-example/target/sql-layer-kv-routine-example-0.1.0-SNAPSHOT.jar', 'fdbjar', 1);
SET binary_output = hex;
BEGIN; CALL fdb.set_value(x'68656C6C6F', x'776F726C64'); COMMIT;
BEGIN; CALL fdb.get_value(x'68656C6C6F'); COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment