Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new store engine #41

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test Workflow

on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go (if you're using Go, adjust this for your environment)
uses: actions/setup-go@v4
with:
go-version: '1.23.2'

- name: Run tests
run: |
go test ./...

20 changes: 0 additions & 20 deletions engine/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,7 @@ func HandleQueries(query string) string {
//return showCollections(db.path)
return getCollections()

// trying sqlite query
case "sql":
return querySql(parsedQuery)

default:
return fmt.Errorf("unknown '%s' cation", parsedQuery.Get("action").Str).Error()
}
}

// hmmmmm sql
func querySql(query gjson.Result) string {
qr := query.Get("query").Str
res, _ := db.db.Query(qr)
record := ""
result := "["
for res.Next() {
res.Scan(&record)
result += record + ","
}
if result == "[" {
return "[]"
}
return result[:len(result)-1] + "]"
}
2 changes: 1 addition & 1 deletion engine/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func order(data []string, params gjson.Result) []string {

// not implemented yet
func (ag Aggregate) aggrigate(query gjson.Result) string {
data, err := getData(query)
data, err := db.getData(query)
if err != nil {
return err.Error()
}
Expand Down
88 changes: 21 additions & 67 deletions engine/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import (
"github.com/tidwall/gjson"
)

// gjson.Type => json:5, array:5, int:2, string:3
// gjson.Type:
// Null: 0
// False: 1
// number: 2
// string: 3
// True: 4
// json: 5
// array: 5

// match verifies that data matches the conditions
func match(filter gjson.Result, data string) (result bool, err error) {
Expand All @@ -21,7 +28,7 @@ func match(filter gjson.Result, data string) (result bool, err error) {
dataVal := gjson.Get(data, queryKey.Str)

if queryVal.Type == 5 { // 5:json
// {name:{$eq:"adam"}, age:{$gt: 18}}
// e.g {name:{$eq:"adam"}, age:{$gt: 18}}

queryVal.ForEach(func(sQueryKey, sQueryVal gjson.Result) bool {

Expand Down Expand Up @@ -163,9 +170,10 @@ func match(filter gjson.Result, data string) (result bool, err error) {
}
return result

case "$in": // in array
// works with array
case "$in": // exests in array

// handle Str arr
// handle String array
if dataVal.Type == 3 {
for _, v := range sQueryVal.Array() {
if dataVal.Str == v.Str {
Expand All @@ -176,7 +184,7 @@ func match(filter gjson.Result, data string) (result bool, err error) {
return result
}

// handle Num arr
// handle Number array
for _, v := range sQueryVal.Array() {
if dataVal.Num == v.Num {
return result
Expand All @@ -185,7 +193,7 @@ func match(filter gjson.Result, data string) (result bool, err error) {
result = false
return result

case "$nin": // not in
case "$nin": // not in array
// handle string arr
if dataVal.Type == 3 {
for _, v := range sQueryVal.Array() {
Expand Down Expand Up @@ -326,7 +334,6 @@ func match(filter gjson.Result, data string) (result bool, err error) {
}

err = fmt.Errorf("unknown %s operator", sQueryKey.Str)

result = false
return result
}
Expand All @@ -340,12 +347,19 @@ func match(filter gjson.Result, data string) (result bool, err error) {
if queryVal.Type == 2 {
if queryVal.Num != dataVal.Num {
result = false
return result
}
}

// if queryVal is string : {name: "adam"}
if queryVal.Str != dataVal.Str {
result = false
return result
}

if queryVal.Type != dataVal.Type {
result = false
return result
}

// if result is true then keep iterating
Expand Down Expand Up @@ -379,66 +393,6 @@ func getSubs(dataVal, subQuery gjson.Result) (bool, error) {

func getsub(query gjson.Result) (ids []int64) {

coll := query.Get("collection").Str
if coll == "" {
return nil
}

mtch := query.Get("match")

if mtch.String() == "" {
fmt.Println("match.Str is empty")
}

skip := query.Get("skip").Int()
limit := query.Get("limit").Int()
if limit == 0 {
limit = 100
}

stmt := `select rowid, record from ` + coll

subs := query.Get("subs")

if subs.Raw != "" {
fmt.Println("sub.Row is : ", subs.Raw)
}

rows, err := db.db.Query(stmt)
if err != nil {
return nil
}
defer rows.Close()

record := ""
rowid := 0

for rows.Next() {
if limit == 0 {
break
}

record = ""
rowid = 0
_ = rows.Scan(&rowid, &record)

ok, err := match(mtch, record)
if err != nil {
fmt.Printf("match %s\n", err)
return nil
}

if ok {
if skip != 0 {
skip--
continue
}
ids = append(ids, int64(rowid))
limit--
}
}
//fmt.Println("\n", ids)

return ids
}

Expand Down
Loading
Loading