Skip to content

Commit

Permalink
rename some files & fix mach bug
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Dec 27, 2023
1 parent 7d19624 commit 2246a5d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 63 deletions.
77 changes: 35 additions & 42 deletions dblite/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

// match verifies that data matches the conditions
func match(query, data string) (result bool) {
// TODO sould ber return syntax error if op unknown

result = true

gjson.Parse(query).ForEach(func(qk, qv gjson.Result) bool {
Expand All @@ -14,56 +16,48 @@ func match(query, data string) (result bool) {
//fmt.Println("q value type : ", qv.Type)

if qv.Type == 5 {

//fmt.Println("inter ", qv.Type)

qv.ForEach(func(sqk, sqv gjson.Result) bool {

// fmt.Println(" sqv type : ", sqv.Type)
if sqk.String()[0] == '$' {
switch sqk.String() {

case "$gt":
if !(dv.Int() > sqv.Int()) {
result = false
return false
}
return result

case "$lt":
if !(dv.Int() < sqv.Int()) {
result = false
return false
}
return result
switch sqk.String() {
case "$gt":
if !(dv.Int() > sqv.Int()) {
result = false
return false
}
return result

case "$gte":
if !(dv.Int() >= sqv.Int()) {
result = false
return false
}
return result
case "$lt":
if !(dv.Int() < sqv.Int()) {
result = false
return false
}
return result

case "$lte":
if !(dv.Int() <= sqv.Int()) {
result = false
return false
}
return result
case "$gte":
if !(dv.Int() >= sqv.Int()) {
result = false
return false
}
return result

case "$eq":
if dv.Int() != sqv.Int() {
result = false
return false
}
return result
case "$lte":
if !(dv.Int() <= sqv.Int()) {
result = false
return false
}
return result

default:
// ??
//fmt.Println("default")
case "$eq":
if dv.Int() != sqv.Int() {
result = false
return false
}
return result
default:
result = false
return result
}
return result
})

match(qv.String(), dv.String())
Expand All @@ -74,7 +68,6 @@ func match(query, data string) (result bool) {
result = false
return result
}

return result // if true keep iterating
})
return result
Expand Down
20 changes: 9 additions & 11 deletions dblite/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func findOne(query string) (res string) {

collection := gjson.Get(query, "collection").String() // + slash

for i := 0; i <= db.Lid; i++ {
for i := 0; i <= db.lastId; i++ {
if db.indexs[i].coll != collection {
continue
}
Expand All @@ -22,7 +22,6 @@ func findOne(query string) (res string) {
return data
}
}

return "noting mutch"
}

Expand All @@ -32,7 +31,7 @@ func findMany(query string) (res string) {
collection := gjson.Get(query, "collection").String() // + slash

res = "["
for i := 0; i <= db.Lid; i++ {
for i := 0; i <= db.lastId; i++ {
if db.indexs[i].coll != collection {
continue
}
Expand Down Expand Up @@ -70,7 +69,7 @@ func insert(query string) (res string) {
return "there is no data to insert"
}

value, err := sjson.Set(data, "_id", db.Lid+1)
value, err := sjson.Set(data, "_id", db.lastId+1)
if err != nil {
fmt.Println("sjson.Set : ", err)
return "internal error"
Expand All @@ -79,15 +78,15 @@ func insert(query string) (res string) {
// make this return error
db.Insert(collection, value)

return fmt.Sprint("Success Insert, _id: ", db.Lid)
return fmt.Sprint("Success Insert, _id: ", db.lastId)
}

// delete
func deleteOne(query string) string {

collection := gjson.Get(query, "collection").String() // + slash
// check collection
for i := 0; i < db.Lid; i++ {
for i := 0; i < db.lastId; i++ {
if db.indexs[i].size == 0 {
continue
}
Expand All @@ -96,7 +95,6 @@ func deleteOne(query string) string {
filter := gjson.Get(query, "filter").String()
data := db.Get(i, collection)
if match(filter, data) {

return db.Delete(i, collection)
}
}
Expand All @@ -110,9 +108,9 @@ func deleteMany(query string) string {
// check collection

// indx, ok := db.indexs[id]; if !ok { return "no data to delete" }
res := 0
tot := 0

for i := 0; i < db.Lid; i++ {
for i := 0; i < db.lastId; i++ {
if db.indexs[i].size == 0 {
continue
}
Expand All @@ -127,11 +125,11 @@ func deleteMany(query string) string {
if match(filter, data) {

if db.Delete(i, collection) == "delete success!" {
res++
tot++
}
}
}
return str(res) + " items deleted!"
return str(tot) + " items deleted!"
}

// delete by id
Expand Down
20 changes: 10 additions & 10 deletions dblite/kvlite.go → dblite/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Run(path string) *Database {

type Database struct {
page int
Lid int
lastId int
lindexs int
lat int64 // last at

Expand All @@ -48,7 +48,7 @@ type index struct {
// deletes exist value
func (db *Database) Delete(id int, coll string) string {

if id > db.Lid {
if id > db.lastId {
return "Id not exists"
}

Expand Down Expand Up @@ -76,7 +76,7 @@ var str = fmt.Sprint

// updates exist value
func (db *Database) Update(id int, coll, value string) string {
if id > db.Lid {
if id > db.lastId {
return "Id not exists"
}

Expand Down Expand Up @@ -119,17 +119,17 @@ func (db *Database) lastAt() {
// inserts new or update exist value
func (db *Database) Insert(coll, value string) {

db.Lid++
db.lastId++

size := len(value)
page := " 0 "

// TODO use string builder to reduce memory consomption
location := "\ni " + str(db.Lid) + " " + str(db.lat) + " " + str(size) + page + coll + "\n"
location := "\ni " + str(db.lastId) + " " + str(db.lat) + " " + str(size) + page + coll + "\n"

db.pages[db.activeFile].Write([]byte(value + location))

db.indexs[db.Lid] = index{at: db.lat, size: size, coll: coll, page: db.page}
db.indexs[db.lastId] = index{at: db.lat, size: size, coll: coll, page: db.page}

db.lat += int64(size + len(location))
}
Expand All @@ -141,7 +141,7 @@ func (db *Database) Get(id int, coll string) string {
// "i <id> <at> <size> <page> <coll>"

// "i 1 0 33 0 users"
if id > db.Lid {
if id > db.lastId {
return "Id not exists"
}

Expand Down Expand Up @@ -191,8 +191,8 @@ func (db *Database) reIndex() (indexs map[int]index) {

// "i 1 0 33 0 users"
id, _ := strconv.Atoi(pos[1])
if id > db.Lid {
db.Lid = id
if id > db.lastId {
db.lastId = id
}
at, _ := strconv.Atoi(pos[2])
size, _ := strconv.Atoi(pos[3])
Expand All @@ -210,7 +210,7 @@ func (db *Database) reIndex() (indexs map[int]index) {

db.lastAt()

fmt.Println("last id : ", db.Lid)
fmt.Println("last id : ", db.lastId)
return indexs
}

Expand Down
File renamed without changes.

0 comments on commit 2246a5d

Please sign in to comment.