Skip to content

Commit

Permalink
rfe10189: Java Interface for Stored Procedures
Browse files Browse the repository at this point in the history
Experimental java API for calling stored procs.
New classes and methods are marked deprecated in the javadocs
because they may change in a future release.

The java code provided with rfe10189 is included,
and JKF's encoding code.

<release-note>
rfe10189: Java Interface for Stored Procedures

The stored proc feature and API are experimental,
and subject to change in a future release.
For this reason, the new methods are marked as deprecated.

The AllegroGraph server defines a new API for defining Stored
Procedures and they are installed like Custom Services.

The primary API is:
AGRepositoryConnection.callStoredProc(functionName, moduleName, args)

A low-level API is also exposed:
AGHttpRepoClient.callStoredProcEncoded()
AGSerializer.serializeAndEncode()
AGDeserializer.decodeAndDeserialize()
</release-note>

tests added:       to test encoding and stored proc call
tests run:         prepush

Depends on commit for rfe10189 in agraph:
https://gerrit.franz.com:9080/982

Change-Id: I6b735d76342473c8dae8b49a43638a769a5a9664
Reviewed-on: https://gerrit.franz.com:9080/964
Reviewed-by: Kevin Layer <layer@franz.com>
Tested-by: Kevin Layer <layer@franz.com>
  • Loading branch information
Mike Hinchey authored and dklayer committed Dec 3, 2010
1 parent b3f70d3 commit 43c9843
Show file tree
Hide file tree
Showing 15 changed files with 941 additions and 21 deletions.
58 changes: 58 additions & 0 deletions src/com/franz/agraph/http/AGDecoder.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,58 @@
/******************************************************************************
** Copyright (c) 2008-2010 Franz Inc.
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the Eclipse Public License v1.0
** which accompanies this distribution, and is available at
** http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/

package com.franz.agraph.http;

/**
*
* @since v4.2
* @deprecated The stored proc feature and API are experimental, and subject to change in a future release.
*/
public class AGDecoder {

static byte charToCode[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 0, 0, 0, 0,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
};

public static byte[] decode(String sval){
ByteArray retv = new ByteArray();
int state = 0;
byte rem = 0;

for (int i = 0; i < sval.length(); i++) {
char ch = sval.charAt(i);
byte val = charToCode[ch];

switch (state) {
case 0: rem = val; break;

case 1: retv.addbyte((byte) (rem | ((val & 0x3) << 6)));
rem = (byte) (val >> 2);
break;

case 2: retv.addbyte((byte) (rem | ((val & 0xf) << 4)));
rem = (byte) (val >> 4);
break;

case 3: retv.addbyte((byte) (rem | (val << 2)));

}

if (++state > 3) state = 0;
}
return retv.extract();
}

}
130 changes: 130 additions & 0 deletions src/com/franz/agraph/http/AGDeserializer.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,130 @@
/******************************************************************************
** Copyright (c) 2008-2010 Franz Inc.
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the Eclipse Public License v1.0
** which accompanies this distribution, and is available at
** http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/

package com.franz.agraph.http;

import java.util.ArrayList;
import java.util.List;

/**
*
* @since v4.2
* @deprecated The stored proc feature and API are experimental, and subject to change in a future release.
*/
public class AGDeserializer {

/* data to process */
private byte[] data;

private int pos;

private int max;

public static Object decodeAndDeserialize(String data) {
AGDeserializer o = new AGDeserializer( AGDecoder.decode(data) );
return o.deserialize();
}

public AGDeserializer(byte[] givendata) {
data = givendata;
pos = 0;
max = givendata.length;
}

byte nextbyte() {
if (pos >= max) {
throw new RuntimeException("ran off the end");
}
pos++;
return data[pos - 1];
}

int posInteger() {
int result = 0;
int shift = 0;

while (true) {
int val = nextbyte();
int masked;

masked = val & 0x7f;
result = result + (masked << shift);
if ((val & 0x80) == 0)
break;
shift += 7;
}

return result;

}

public Object deserialize() {
byte val = nextbyte();
int length;

switch (val) {
case SerialConstants.SO_BYTEVECTOR: {
length = posInteger();
byte[] res = new byte[length];
for (int i = 0; i < length; i++) {
res[i] = nextbyte();
}
return res;
}

case SerialConstants.SO_VECTOR: {
length = posInteger();
Object[] res = new Object[length];
for (int i = 0; i < length; i++) {
res[i] = deserialize();
}
return res;
}

case SerialConstants.SO_LIST: {
length = posInteger();
List res = new ArrayList(length);
for (int i = 0; i < length; i++) {
res.add( deserialize() );
}
// TODO: extra null needed by lisp side, bug in lisp?
nextbyte();
return res;
}

case SerialConstants.SO_STRING: {
length = posInteger();

StringBuilder res = new StringBuilder();
for (int i = 0; i < length; i++) {
res.append((char) nextbyte());
}
return res.toString();
}

case SerialConstants.SO_POS_INTEGER: {
return posInteger();
}

case SerialConstants.SO_NEG_INTEGER: {
return - posInteger();
}

case SerialConstants.SO_NULL:
return null;

case SerialConstants.SO_END_OF_ITEMS:
return null;

default:
throw new RuntimeException("bad code found by deserializer: " + val);

}
}

}
69 changes: 69 additions & 0 deletions src/com/franz/agraph/http/AGEncoder.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,69 @@
/******************************************************************************
** Copyright (c) 2008-2010 Franz Inc.
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the Eclipse Public License v1.0
** which accompanies this distribution, and is available at
** http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/

package com.franz.agraph.http;

import java.util.ArrayList;

/**
*
* @since v4.2
* @deprecated The stored proc feature and API are experimental, and subject to change in a future release.
*/
public class AGEncoder {

static char codeToChar[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '*', '+'
};

public static String encode(byte[] arr) {
ArrayList<Character> resx = new ArrayList<Character>();

int state = 0;

int rem = 0;

for (byte b : arr) {
switch (state) {
case 0:
resx.add(codeToChar[b & 0x3f]);
rem = (b >> 6) & 0x3;
break;
case 1:
resx.add(codeToChar[((b & 0xf) << 2) | rem]);
rem = (b >> 4) & 0xf;
break;
case 2:
resx.add(codeToChar[((b & 0x3) << 4) | rem]);
resx.add(codeToChar[((b >> 2) & 0x3f)]);
}

state = (state + 1) % 3;

}

if (state != 0) {
resx.add(codeToChar[rem]);
}

// there must be an easier way to turn an ArrayList<Character> into a String.
char[] retstr = new char[resx.size()];
int index = 0;
for (Character c : resx) {
retstr[index++] = c;
}
return new String(retstr);

}

}
Loading

0 comments on commit 43c9843

Please sign in to comment.