Skip to content

Commit

Permalink
trying msgpack transport: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ryogrid committed Oct 30, 2023
1 parent aca549f commit 83f454c
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
51 changes: 51 additions & 0 deletions demo-client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
}
</style>
<script type="text/javascript" src="./jquery-3.7.1.min.js"></script>
<script src="./jquery.binarytransport.js"></script>
<script src="https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js"></script>
<script type="text/javascript">
var executedLog = "";
/*
$(document).ready(function () {
$("#issue").click(function () {
var queryStr = $('#queryform [name=queryform]').val();
Expand Down Expand Up @@ -52,6 +55,54 @@
});
});
});
*/

$(document).ready(function () {
$("#issue").click(function () {
var queryStr = $('#queryform [name=queryform]').val();
var serverAddress = $('#serveraddress [name=serveraddress]').val();

$.ajax({
type: "POST",
url: "http://" + serverAddress + "/QueryMsgPack",
contentType: "application/json;charset=utf-8",
responseType: "arraybuffer",
dataType: "binary",
data: queryStr,
}).done(function (data) {
console.log(data);
console.log(typeof data);
const decoded = msgpack.decode(data);
console.log(unpacked);

/*
$("#result-error").html(String(data.Error));
var resultStr = "";
if (data.Result != null && data.Result != undefined && data.length != 0) {
for (var ii = 0; ii < data.Result.length; ii++) {
var rowStr = "";
for (var jj = 0; jj < data.Result[ii].C.length; jj++) {
if (typeof data.Result[ii].C[jj] == "string") {
rowStr += '"' + data.Result[ii].C[jj] + '" ,';
} else {
rowStr += data.Result[ii].C[jj] + " ,";
}
}
resultStr += rowStr + "<br />"
}
//$("#result-main").html(resultStr);
resultStr = queryStr + "<br />" + resultStr + "<br />"
executedLog += resultStr;
$("#result-main").html(executedLog);
}
*/
}).fail(function (jqXHR, textStatus, errorThrown) {
$("#result-error").html("Error: " + textStatus + " " + errorThrown);
//$("#result-main").html("<p>No Data</p>");
});
});
});
</script>
</head>
<body>
Expand Down
59 changes: 59 additions & 0 deletions demo-client/jquery.binarytransport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
*
* jquery.binarytransport.js
*
* @description. jQuery ajax transport for making binary data type requests.
* @version 1.0
* @author Henry Algus <[email protected]>
*
*/

(function($, undefined) {
"use strict";

// use this transport for "binary" data type
$.ajaxTransport("+binary", function(options, originalOptions, jqXHR) {
// check for conditions and support for blob / arraybuffer response type
if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) {
return {
// create new XMLHttpRequest
send: function(headers, callback) {
// setup all variables
var xhr = new XMLHttpRequest(),
url = options.url,
type = options.type,
async = options.async !== false,
// blob or arraybuffer. Default is blob
dataType = options.responseType || "blob",
data = options.data || null,
username = options.username || null,
password = options.password || null;

xhr.addEventListener('load', function() {
var data = {};
data[options.dataType] = xhr.response;
// make callback and send data
callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
});
xhr.addEventListener('error', function() {
var data = {};
data[options.dataType] = xhr.response;
// make callback and send data
callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
});

xhr.open(type, url, async, username, password);

// setup custom headers
for (var i in headers) {
xhr.setRequestHeader(i, headers[i]);
}

xhr.responseType = dataType;
xhr.send(data);
},
abort: function() {}
};
}
});
})(window.jQuery);
2 changes: 2 additions & 0 deletions demo-client/msgpack.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go 1.18
require (
github.com/ant0ine/go-json-rest v3.3.2+incompatible
github.com/ryogrid/SamehadaDB/lib v0.0.0-00010101000000-000000000000
github.com/ugorji/go/codec v1.1.7
)

require (
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,12 @@ github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181022190402-e5e69e061d4f/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.5-pre/go.mod h1:tULtS6Gy1AE1yCENaw4Vb//HLH5njI2tfCQDUqRd8fI=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/unrolled/render v0.0.0-20171102162132-65450fb6b2d3/go.mod h1:tu82oB5W2ykJRVioYsB+IQKcft7ryBr7w12qMBUPyXg=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
Expand Down
54 changes: 54 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"bytes"
"fmt"
"github.com/ant0ine/go-json-rest/rest"
"github.com/ryogrid/SamehadaDB/lib/samehada"
"github.com/ryogrid/SamehadaDB/server/signal_handle"
"github.com/ugorji/go/codec"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -62,6 +65,56 @@ func postQuery(w rest.ResponseWriter, req *rest.Request) {
})
}

func postQueryMsgPack(w rest.ResponseWriter, req *rest.Request) {
if signal_handle.IsStopped {
http.Error(w.(http.ResponseWriter), "Server is stopped", http.StatusGone)
return
}

input := QueryInput{}
err := req.DecodeJsonPayload(&input)

if err != nil {
fmt.Println(err)
http.Error(w.(http.ResponseWriter), err.Error(), http.StatusBadRequest)
return
}

if input.Query == "" {
http.Error(w.(http.ResponseWriter), "Query is required", 400)
return
}

err2, results := db.ExecuteSQL(input.Query)
if err2 != nil {
http.Error(w.(http.ResponseWriter), err2.Error(), http.StatusBadRequest)
return
}

rows := make([]Row, 0)
for _, row := range results {
rows = append(rows, Row{row})
}

var w_ io.Writer = new(bytes.Buffer)
var h codec.Handle = new(codec.MsgpackHandle)
var enc = codec.NewEncoder(w_, h)
var err3 = enc.Encode(rows)
if err3 != nil {
fmt.Println(err3)
http.Error(w.(http.ResponseWriter), err.Error(), http.StatusBadRequest)
return
}

dec := codec.NewDecoder(w_.(*bytes.Buffer), h)
var decoded interface{}
dec.Decode(decoded)
fmt.Println(decoded)

w.Header().Set("Content-Type", "application/octent-stream")
w.(http.ResponseWriter).Write(w_.(*bytes.Buffer).Bytes())
}

func launchDBAndListen() {
api := rest.NewApi()

Expand All @@ -83,6 +136,7 @@ func launchDBAndListen() {

router, err := rest.MakeRouter(
&rest.Route{"POST", "/Query", postQuery},
&rest.Route{"POST", "/QueryMsgPack", postQueryMsgPack},
)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 83f454c

Please sign in to comment.