本文整理匯總了Golang中github.com/HouzuoGuo/tiedot/db.EvalQuery函數的典型用法代碼示例。如果您正苦於以下問題:Golang EvalQuery函數的具體用法?Golang EvalQuery怎麽用?Golang EvalQuery使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了EvalQuery函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetByIpPort
func (ps *ProxyStorage) GetByIpPort(ip string, port int) (Proxy, error) {
var query interface{}
queryString := fmt.Sprintf(`[{"eq" : "%s", "in" : ["Ip"] },{"eq" : "%n", "in" : ["Port"] }]`, ip, port)
json.Unmarshal([]byte(queryString), &query)
queryResult := make(map[int]struct{}) // query result (document IDs) goes into map keys
var proxy Proxy
if err := db.EvalQuery(query, ps.proxyCol, &queryResult); err != nil {
logger.Errf("Error while query eval : %s. Error : %s", query, err)
return proxy, err
}
if len(queryResult) == 0 {
return proxy, errors.New("No proxy found")
}
for k, _ := range queryResult {
//expecting single key
r, err := ps.proxyCol.Read(k)
if err != nil {
return proxy, err
}
err = mapstructure.Decode(r, &proxy)
if err != nil {
return proxy, err
}
}
return proxy, nil
}
示例2: benchmark
// Document CRUD benchmark (insert/read/query/update/delete), intended for catching performance regressions.
func benchmark() {
ids := make([]int, 0, benchSize)
// Prepare a collection with two indexes
tmp := "/tmp/tiedot_bench"
os.RemoveAll(tmp)
if benchCleanup {
defer os.RemoveAll(tmp)
}
benchDB, col := mkTmpDBAndCol(tmp, "tmp")
defer benchDB.Close()
col.Index([]string{"nested", "nested", "str"})
col.Index([]string{"nested", "nested", "int"})
col.Index([]string{"nested", "nested", "float"})
col.Index([]string{"strs"})
col.Index([]string{"ints"})
col.Index([]string{"floats"})
// Benchmark document insert
average("insert", func() {
if _, err := col.Insert(sampleDoc()); err != nil {
fmt.Println("Insert error", err)
}
})
// Collect all document IDs and benchmark document read
col.ForEachDoc(func(id int, _ []byte) bool {
ids = append(ids, id)
return true
})
average("read", func() {
doc, err := col.Read(ids[rand.Intn(benchSize)])
if doc == nil || err != nil {
fmt.Println("Read error", doc, err)
}
})
// Benchmark lookup query (two attributes)
average("lookup", func() {
result := make(map[int]struct{})
if err := db.EvalQuery(sampleQuery(), col, &result); err != nil {
fmt.Println("Query error", err)
}
})
// Benchmark document update
average("update", func() {
if err := col.Update(ids[rand.Intn(benchSize)], sampleDoc()); err != nil {
fmt.Println("Update error", err)
}
})
// Benchmark document delete
var delCount int64
average("delete", func() {
if err := col.Delete(ids[rand.Intn(benchSize)]); err == nil {
atomic.AddInt64(&delCount, 1)
}
})
fmt.Printf("Deleted %d documents\n", delCount)
}
示例3: SearchQuestionListByCourseId
func SearchQuestionListByCourseId(res http.ResponseWriter, req *http.Request) string {
courseId := req.URL.Query()["courseId"]
fmt.Println(courseId)
var query interface{}
json.Unmarshal([]byte(`[{"eq": "`+courseId[0]+`", "in": ["courseId"]}]`), &query)
queryResult := make(map[int]struct{}) // query result (document IDs) goes into map keys
if err := db.EvalQuery(query, qlCl, &queryResult); err != nil {
panic(err)
}
fmt.Println("result length", len(queryResult))
questionList := make([]map[string]interface{}, 0)
for docId, _ := range queryResult {
// Read document
var readBack map[string]interface{}
var err error
if readBack, err = qlCl.Read(docId); err != nil {
return "questionList not found"
}
readBack["questionListId"] = string(strconv.AppendInt(nil, int64(docId), 10))
fmt.Println("read data", readBack)
questionList = append(questionList, readBack)
}
ret, _ := json.Marshal(questionList)
res.WriteHeader(http.StatusOK)
return string(ret)
}
示例4: GetBandsByGenre
func GetBandsByGenre(id string) []DocWithID {
database := GetDB()
defer database.Close()
collection := database.Use(tiedotmartini2.BAND_COL)
var query interface{}
// id2, _ := strconv.ParseInt(id, 10, 64)
q := `{"in": ["albums", "genre_id"], "eq": "` + id + `"}`
// json.Unmarshal([]byte(`"all"`), &query)
err := json.Unmarshal([]byte(q), &query)
if err != nil {
fmt.Println("Unmarshall error on genre search:", err)
}
result := make(map[uint64]struct{})
err = db.EvalQuery(query, collection, &result)
if err != nil {
fmt.Println("Eval error:", err)
os.Exit(1)
}
fmt.Println("Ran query")
var docs []DocWithID
for id2 := range result {
fmt.Println("Found", id2)
var readback map[string]interface{}
collection.Read(id2, &readback)
doc := DocWithID{DocKey: id2, Value: readback}
docs = append(docs, doc)
}
if docs == nil {
fmt.Println("Returning empty value")
}
return docs
}
示例5: GetAll
func GetAll() (pastes []Paste) {
myDB, err1 := GetDb()
if err1 != nil {
log.Fatal("Error on database start - GetAll():", err1)
}
col := myDB.Use(PASTES)
var query interface{}
result := make(map[int]struct{})
err := json.Unmarshal([]byte(`"all"`), &query)
// err := json.Unmarshal([]byte(`{"limit": 5}`), &query)
if err != nil {
log.Fatal("json error:", err)
}
db.EvalQuery(query, col, &result)
var docs []Paste
for id := range result {
doc, _ := col.Read(id)
theTime, _ := time.Parse(time.RFC3339, doc[CREATED].(string))
docObj := Paste{Id: id, Title: doc[TITLE].(string), Content: doc[CONTENT].(string), CreatedOn: theTime}
docs = append(docs, docObj)
}
sort.Sort(ByCreated(docs))
q := docs[0:5]
return q
}
示例6: SaveProxy
func (ps *ProxyStorage) SaveProxy(toSave *Proxy, update bool) (int, error) {
logger.Tracef("Saving proxy to storage: %s", toSave)
var query interface{}
queryString := fmt.Sprintf(`[{"eq" : "%s", "in" : ["Ip"] },{"eq" : "%n", "in" : ["Port"] }]`, toSave.Ip, toSave.Port)
json.Unmarshal([]byte(queryString), &query)
queryResult := make(map[int]struct{}) // query result (document IDs) goes into map keys
if err := db.EvalQuery(query, ps.proxyCol, &queryResult); err != nil {
panic(err)
}
if len(queryResult) > 0 {
logger.Tracef("Exists in database: %s", toSave)
if update {
logger.Tracef("Updating %s", toSave)
for k, _ := range queryResult {
//expecting single key
return k, ps.proxyCol.Update(k, structs.Map(toSave))
}
}
}
return ps.proxyCol.Insert(structs.Map(toSave))
}
示例7: Count
// Execute a query and return number of documents from the result.
func Count(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
var col, q string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "q", &q) {
return
}
var qJson interface{}
if err := json.Unmarshal([]byte(q), &qJson); err != nil {
http.Error(w, fmt.Sprintf("'%v' is not valid JSON.", q), 400)
return
}
dbcol := HttpDB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
queryResult := make(map[int]struct{})
if err := db.EvalQuery(qJson, dbcol, &queryResult); err != nil {
http.Error(w, fmt.Sprint(err), 400)
return
}
w.Write([]byte(strconv.Itoa(len(queryResult))))
}
示例8: All
func (t *TiedotEngine) All(collectionName string) (map[uint64]struct{}, error) {
r := make(map[uint64]struct{})
if err := tiedot.EvalQuery("all", t.tiedot.Use(collectionName), &r); err != nil {
log.Error("Error executing TiedotEngine.All() err=%s", err.Error())
return nil, err
}
return r, nil
}
示例9: getJWT
// Verify user identity and hand out a JWT.
func getJWT(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
addCommonJwtRespHeaders(w, r)
// Verify identity
user := r.FormValue(JWT_USER_ATTR)
if user == "" {
http.Error(w, "Please pass JWT 'user' parameter", http.StatusBadRequest)
return
}
jwtCol := HttpDB.Use(JWT_COL_NAME)
if jwtCol == nil {
http.Error(w, "Server is missing JWT identity collection, please restart the server.", http.StatusInternalServerError)
return
}
userQuery := map[string]interface{}{
"eq": user,
"in": []interface{}{JWT_USER_ATTR}}
userQueryResult := make(map[int]struct{})
if err := db.EvalQuery(userQuery, jwtCol, &userQueryResult); err != nil {
tdlog.CritNoRepeat("Query failed in JWT identity collection : %v", err)
http.Error(w, "Query failed in JWT identity collection", http.StatusInternalServerError)
return
}
// Verify password
pass := r.FormValue(JWT_PASS_ATTR)
for recID := range userQueryResult {
rec, err := jwtCol.Read(recID)
if err != nil {
break
}
if rec[JWT_PASS_ATTR] != pass {
tdlog.CritNoRepeat("JWT: identitify verification failed from request sent by %s", r.RemoteAddr)
break
}
// Successful password match
token := jwt.New(jwt.GetSigningMethod("RS256"))
token.Claims = jwt.MapClaims{
JWT_USER_ATTR: rec[JWT_USER_ATTR],
JWT_COLLECTIONS_ATTR: rec[JWT_COLLECTIONS_ATTR],
JWT_ENDPOINTS_ATTR: rec[JWT_ENDPOINTS_ATTR],
JWT_EXPIRY: time.Now().Add(time.Hour * 72).Unix(),
}
var tokenString string
var e error
if tokenString, e = token.SignedString(privateKey); e != nil {
panic(e)
}
w.Header().Set("Authorization", "Bearer "+tokenString)
w.WriteHeader(http.StatusOK)
return
}
// ... password mismatch
http.Error(w, "Invalid password", http.StatusUnauthorized)
}
示例10: Query
func (repo *GenericRepository) Query(query interface{}) []GenericModel {
col := repo.database.Use(repo.coll)
result := make(map[int]struct{})
if err := db.EvalQuery(query, col, &result); err != nil {
panic(err)
}
generics := make([]GenericModel, 0)
for id := range result {
generics = append(generics, repo.Fetch(id))
}
return generics
}
示例11: Query
func (tdb *DBTiedot) Query(col, querystr string) ([]byte, error) {
var query interface{}
var data []interface{}
json.Unmarshal([]byte(querystr), &query)
queryResult := make(map[uint64]struct{})
colh := tdb.Db.Use(col)
if err := tiedot.EvalQuery(query, colh, &queryResult); err != nil {
return nil, err
}
for id := range queryResult {
var intf interface{}
var subintf interface{}
_, err := colh.Read(id, &intf)
if err != nil {
fmt.Println("Read back failed ", err)
}
//FIXME: wtf do we have to do this?
//fmt.Println("Value of: %s\n",reflect.ValueOf(intf))
val := reflect.ValueOf(intf)
keys := val.MapKeys()
for k := range keys {
if strings.Contains(keys[k].String(), "id") {
continue
}
fmt.Println("We want uuid: ", keys[k].String())
subintf = val.MapIndex(keys[k]).Interface()
data = append(data, subintf)
}
//subintf = val.Elem(;
}
dataBytes, err := json.Marshal(data)
if err != nil {
fmt.Println("Failed to marshal interface{} to raw []bytes: ", err)
return nil, err
}
return dataBytes, nil
}
示例12: Query
func Query(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
var col, q string
if !Require(w, r, "col", &col) {
return
}
if !Require(w, r, "q", &q) {
return
}
var qJson interface{}
if err := json.Unmarshal([]byte(q), &qJson); err != nil {
http.Error(w, fmt.Sprintf("'%v' is not valid JSON.", q), 400)
return
}
V3Sync.RLock()
defer V3Sync.RUnlock()
dbcol := V3DB.Use(col)
if dbcol == nil {
http.Error(w, fmt.Sprintf("Collection '%s' does not exist.", col), 400)
return
}
// Evaluate the query
queryResult := make(map[uint64]struct{})
if err := db.EvalQuery(qJson, dbcol, &queryResult); err != nil {
http.Error(w, fmt.Sprint(err), 400)
return
}
// Construct array of result
resultDocs := make([]map[string]interface{}, len(queryResult))
counter := 0
for docID := range queryResult {
var doc map[string]interface{}
dbcol.Read(docID, &doc)
if doc != nil {
resultDocs[counter] = doc
counter++
}
}
// Serialize the array
resp, err := json.Marshal(resultDocs)
if err != nil {
http.Error(w, fmt.Sprintf("Server error: query returned invalid structure"), 500)
return
}
w.Write([]byte(string(resp)))
}
示例13: GetAll
func GetAll(docType string) []DocWithID {
database := GetDB()
defer database.Close()
collection := database.Use(docType)
var query interface{}
result := make(map[uint64]struct{})
json.Unmarshal([]byte(`"all"`), &query)
db.EvalQuery(query, collection, &result)
var docs []DocWithID
for id := range result {
var doc MyDoc
collection.Read(id, &doc)
docObj := DocWithID{DocKey: id, Value: doc}
docs = append(docs, docObj)
}
return docs
}
示例14: GetUserForNick
func (ds *DataStore) GetUserForNick(nick string) (recs []Record, err error) {
query := createQuery(nick, "nick")
queryResult := make(map[uint64]struct{})
err = db.EvalQuery(query, ds.users, &queryResult)
if err != nil {
return
}
if len(queryResult) == 0 {
err = errors.New("No User Found")
return
}
recs = ds.getUsersForIDs(queryResult)
return
}
示例15: home
func home(w http.ResponseWriter, req *http.Request) {
// local vars
var query interface{}
entries := Entries{}
queryResults := make(map[int]struct{})
// open database
d, err := db.OpenDB(DBdir)
if err != nil {
panic(err)
}
// use collection
docEntries := d.Use("Entries")
// build query from json and convert to interface{}
json.Unmarshal([]byte(`"all"`), &query)
// execute query and pass results to queryResults
if err := db.EvalQuery(query, docEntries, &queryResults); err != nil {
panic(err)
}
// queryResults contains []int of IDs
for id := range queryResults {
entry := Entry{}
readBack, _ := docEntries.Read(id)
// map[string]interface{} TO struct hack
j, _ := json.Marshal(readBack) // struct to json
json.Unmarshal(j, &entry) // json to actual type
entries = append(entries, &entry)
}
// compile template with data
if err := index.Execute(w, entries); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}