Created
August 24, 2014 07:10
-
-
Save kumagi/9f9356c739246d3e88f4 to your computer and use it in GitHub Desktop.
jubatus recommender client go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package client | |
import ( | |
"net" | |
"fmt" | |
"net/rpc" | |
"github.com/ugorji/go/codec" | |
) | |
const ( | |
string_values = iota | |
num_values | |
binary_values | |
) | |
type Datum [3][]interface{} | |
type IDWithScore struct { | |
id string | |
score float64 | |
} | |
type RecommenderClient struct { | |
client rpc.Client | |
name string | |
} | |
func NewRecommenderClient(host string, name string) (*RecommenderClient, error) { | |
conn, err := net.Dial("tcp", host) | |
if err != nil { | |
return nil, err | |
} | |
mh := new(codec.MsgpackHandle) | |
rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, mh) | |
client := rpc.NewClientWithCodec(rpcCodec) | |
return &RecommenderClient{*client, name}, nil | |
} | |
func NewDatum() Datum { | |
return Datum{} | |
} | |
func (d *Datum) AddString(key string, value string) *Datum { | |
if d[string_values] == nil { | |
d[string_values] = make([]interface{}, 0) | |
} | |
d[string_values] = append(d[string_values], | |
[2]interface{}{key, value}) | |
fmt.Println(*d) | |
return d | |
} | |
func (d *Datum) AddNum(key string, value float64) *Datum { | |
if d[num_values] == nil { | |
d[num_values] = make([]interface{}, 0) | |
} | |
d[num_values] = append(d[num_values], | |
[2]interface{}{key, value}) | |
return d | |
} | |
func (d *Datum) AddBinary(key string, value []byte) *Datum { | |
if d[binary_values] == nil { | |
d[binary_values] = make([]interface{}, 0) | |
} | |
d[binary_values] = append(d[binary_values], | |
[2]interface{}{key, value}) | |
return d | |
} | |
func (r *RecommenderClient) UpdateRow(id string, d Datum) (result bool) { | |
r.client.Call("update_row", []interface{}{r.name, id, d}, &result) | |
return | |
} | |
func (r *RecommenderClient) ClearRow(id string) (result bool) { | |
r.client.Call("clear_row", []interface{}{r.name, id}, result) | |
return | |
} | |
func (r *RecommenderClient) Clear() (result bool) { | |
r.client.Call("clear", []interface{}{r.name}, &result) | |
return | |
} | |
func (r *RecommenderClient) CompleteRowFromID(id string) (result Datum) { | |
r.client.Call("complete_row_from_id", []interface{}{r.name, id}, &result) | |
return | |
} | |
func (r *RecommenderClient) CompleteRowFromDatum(row Datum) (result Datum) { | |
r.client.Call("complete_row_from_datum", []interface{}{r.name, row}, &result) | |
return | |
} | |
func (r *RecommenderClient) SimilarRowFromID(id string, size int) (result []IDWithScore) { | |
r.client.Call("similar_row_from_id", []interface{}{r.name, id, size}, &result) | |
return | |
} | |
func (r *RecommenderClient) SimilarRowFromDatum(d Datum, size int) (result []IDWithScore) { | |
r.client.Call("similar_row_from_datum", []interface{}{r.name, d, size}, &result) | |
return | |
} | |
func (r *RecommenderClient) DecodeRow(id string) (result Datum) { | |
r.client.Call("decode_row", []interface{}{r.name, id}, &result) | |
return | |
} | |
func (r *RecommenderClient) GetAllRows() (result []string) { | |
r.client.Call("get_all_rows", []interface{}{r.name}, &result) | |
return | |
} | |
func (r *RecommenderClient) CalcSimilarity(lhs Datum, rhs Datum) (result float64){ | |
r.client.Call("calc_similarity", []interface{}{r.name, lhs, rhs}, &result) | |
return | |
} | |
func (r *RecommenderClient) CalcL2norm(row Datum) (result float64) { | |
r.client.Call("calc_similarity", []interface{}{r.name, row}, &result) | |
return | |
} | |
func (r *RecommenderClient) GetStatus() (result map[string]map[string]string) { | |
r.client.Call("get_status", []interface{}{r.name}, &result) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment