Skip to content

Commit

Permalink
Cleanup 2
Browse files Browse the repository at this point in the history
  • Loading branch information
trzysiek committed Dec 20, 2024
1 parent 45239e3 commit 6a08fa1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 49 deletions.
10 changes: 4 additions & 6 deletions quesma/persistence/elastic_with_eviction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
type ElasticDatabaseWithEviction struct {
ctx context.Context
*ElasticJSONDatabase // maybe remove and copy fields here
EvictorInterface
// EvictorInterface
sizeInBytesLimit int64
}

func NewElasticDatabaseWithEviction(cfg config.ElasticsearchConfiguration, indexName string, sizeInBytesLimit int64) *ElasticDatabaseWithEviction {
return &ElasticDatabaseWithEviction{
ctx: context.Background(),
ElasticJSONDatabase: NewElasticJSONDatabase(cfg, indexName),
EvictorInterface: &Evictor{},
sizeInBytesLimit: sizeInBytesLimit,
// EvictorInterface: &Evictor{},
sizeInBytesLimit: sizeInBytesLimit,
}
}

Expand All @@ -45,6 +45,7 @@ func (db *ElasticDatabaseWithEviction) Put(document *JSONWithSize) error {
}
bytesNeeded := dbSize + document.SizeInBytesTotal
if bytesNeeded > db.SizeInBytesLimit() {
return errors.New("elastic database: is full, cannot put document")
/*
TODO: restore after eviction readded
logger.Info().Msgf("elastic database: is full, need %d bytes more. Evicting documents", bytesNeeded-db.SizeInBytesLimit())
Expand All @@ -58,9 +59,6 @@ func (db *ElasticDatabaseWithEviction) Put(document *JSONWithSize) error {
*/
}
if bytesNeeded > db.SizeInBytesLimit() {
return errors.New("elastic database: is full, cannot put document")
}

elasticsearchURL := fmt.Sprintf("%s/_update/%s", db.indexName, document.id)
if printDebugElasticDB {
Expand Down
15 changes: 5 additions & 10 deletions quesma/persistence/evictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
// SPDX-License-Identifier: Elastic-2.0
package persistence

import "fmt"

type EvictorInterface interface {
Evict(documents []*JSONWithSize, sizeNeeded int64) (bytesEvicted int64)
}

// It's only 1 implementation, which looks well suited for ElasticSearch.
// It can be implemented differently.
// TODO: Find out how this might work. My old idea doesn't work now,
// as don't remove entire indices, but delete single documents.
// (It turned out consistency was too eventual to rely on it)
// old comment: It's only 1 implementation, which looks well suited for ElasticSearch. It can be implemented differently.
type Evictor struct{}

func (e *Evictor) Evict(documents []*JSONWithSize, sizeNeeded int64) (bytesEvicted int64) {
if sizeNeeded <= 0 {
return // check if it's empty array or nil
}
fmt.Println("kk dbg SelectToEvict() sizeNeeded:", sizeNeeded)

return
panic("implement me (or remove)")
}
65 changes: 32 additions & 33 deletions quesma/persistence/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,38 @@ import (
"time"
)

// JSONDatabase is an interface for a database that stores JSON data.
// Treat it as `etcd` equivalent rather than `MongoDB`.
// The main usage is to store our configuration data, like
// - schema
// - user settings
// - statistics, etc
//
// For each case, we should have a separate database.
type JSONDatabase interface {
List() (keys []string, err error)
Get(key string) (string, bool, error)
Put(key string, data string) error
}

type DatabaseWithEviction interface { // for sure JSON? maybe not only json? check
Put(doc JSONWithSize) error
Get(id string) (types.JSON, error)
Delete(id string) error
DeleteOld(time.Duration) error
DocCount() (int, error)
SizeInBytes() (int64, error)
SizeInBytesLimit() int64
}

type JSONWithSizeInterface interface {
SizeInBytes() int64
}

type JSONWithSize struct {
types.JSON
id string
SizeInBytesTotal int64
}
type (
// JSONDatabase is an interface for a database that stores JSON data.
// Treat it as `etcd` equivalent rather than `MongoDB`.
// The main usage is to store our configuration data, like
// - schema
// - user settings
// - statistics, etc
//
// For each case, we should have a separate database.
JSONDatabase interface {
List() (keys []string, err error)
Get(key string) (string, bool, error)
Put(key string, data string) error
}
DatabaseWithEviction interface { // for sure JSON? maybe not only json? check
Put(doc JSONWithSize) error
Get(id string) (types.JSON, error)
Delete(id string) error
DeleteOld(time.Duration) error
DocCount() (int, error)
SizeInBytes() (int64, error)
SizeInBytesLimit() int64
}
JSONWithSizeInterface interface {
SizeInBytes() int64
}
JSONWithSize struct {
types.JSON
id string
SizeInBytesTotal int64
}
)

func NewJSONWithSize(data types.JSON, id string, sizeInBytesTotal int64) *JSONWithSize {
return &JSONWithSize{
Expand Down

0 comments on commit 6a08fa1

Please sign in to comment.