Skip to content

Commit 4bfed45

Browse files
authored
Merge pull request #28 from Eigen-DB/improving_config
Improving config
2 parents 36cb548 + 709743c commit 4bfed45

24 files changed

+442
-83
lines changed

.gitignore

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
eigen_db
2-
eigen/vector_space.vec
3-
integration_tests/*.log
4-
integration_tests/logs/
5-
eigen/hnsw_index.bin
6-
eigen/api_key.txt
2+
e2e/*.log
3+
e2e/logs/
4+
eigen/*
75
node_modules/
86
benchmarks/*.log
9-
benchmarks/*_mean_*.csv
7+
benchmarks/*_mean_*.csv
8+
9+
!eigen/.keep

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD [ "./
1212

1313
EXPOSE 8080
1414

15-
CMD ["/bin/bash", "-c", "/app/eigen_db"]

api/endpoints/update_config/api/address.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package api
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6-
"fmt"
6+
"eigen_db/constants"
77
"net/http"
88

99
"github.com/gin-gonic/gin"
@@ -20,14 +20,23 @@ func UpdateAddress(c *gin.Context) {
2020
}
2121

2222
config := cfg.GetConfig()
23-
err := config.SetAPIAddress(body.UpdatedAddress)
24-
if err != nil {
23+
if err := config.SetAPIAddress(body.UpdatedAddress); err != nil {
24+
utils.SendResponse(
25+
c,
26+
http.StatusBadRequest,
27+
"Invalid API address.",
28+
nil,
29+
utils.CreateError("INVALID_API_ADDRESS", err.Error()),
30+
)
31+
return
32+
}
33+
if err := config.WriteToDisk(constants.CONFIG_PATH); err != nil {
2534
utils.SendResponse(
2635
c,
2736
http.StatusInternalServerError,
2837
"An error occured.",
2938
nil,
30-
utils.CreateError("ERROR_UPDATING_API_ADDRESS", fmt.Sprintf("Error: %s", err.Error())),
39+
utils.CreateError("ERROR_UPDATING_API_ADDRESS", err.Error()),
3140
)
3241
return
3342
}

api/endpoints/update_config/api/port.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package api
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6-
"fmt"
6+
"eigen_db/constants"
77
"net/http"
88

99
"github.com/gin-gonic/gin"
@@ -20,14 +20,23 @@ func UpdatePort(c *gin.Context) {
2020
}
2121

2222
config := cfg.GetConfig()
23-
err := config.SetAPIPort(body.UpdatedPort)
24-
if err != nil {
23+
if err := config.SetAPIPort(body.UpdatedPort); err != nil {
24+
utils.SendResponse(
25+
c,
26+
http.StatusBadRequest,
27+
"Invalid API port.",
28+
nil,
29+
utils.CreateError("INVALID_API_PORT", err.Error()),
30+
)
31+
return
32+
}
33+
if err := config.WriteToDisk(constants.CONFIG_PATH); err != nil {
2534
utils.SendResponse(
2635
c,
2736
http.StatusInternalServerError,
2837
"An error occured.",
2938
nil,
30-
utils.CreateError("ERROR_UPDATING_API_PORT", fmt.Sprintf("Error: %s", err.Error())),
39+
utils.CreateError("ERROR_UPDATING_API_PORT", err.Error()),
3140
)
3241
return
3342
}

api/endpoints/update_config/hnsw_params/ef_construction.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package hnsw_params
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6-
"fmt"
6+
"eigen_db/constants"
77
"net/http"
88

99
"github.com/gin-gonic/gin"
@@ -20,14 +20,23 @@ func UpdateEfConstruction(c *gin.Context) {
2020
}
2121

2222
config := cfg.GetConfig()
23-
err := config.SetEfConstruction(body.UpdatedEfConst)
24-
if err != nil {
23+
if err := config.SetEfConstruction(body.UpdatedEfConst); err != nil {
24+
utils.SendResponse(
25+
c,
26+
http.StatusBadRequest,
27+
"Invalid erConstruction parameter.",
28+
nil,
29+
utils.CreateError("INVALID_EF_CONSTRUCTION", err.Error()),
30+
)
31+
return
32+
}
33+
if err := config.WriteToDisk(constants.CONFIG_PATH); err != nil {
2534
utils.SendResponse(
2635
c,
2736
http.StatusInternalServerError,
2837
"An error occured.",
2938
nil,
30-
utils.CreateError("ERROR_UPDATING_EF_CONSTRUCTION", fmt.Sprintf("Error: %s", err.Error())),
39+
utils.CreateError("ERROR_UPDATING_EF_CONSTRUCTION", err.Error()),
3140
)
3241
return
3342
}

api/endpoints/update_config/hnsw_params/m.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package hnsw_params
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6-
"fmt"
6+
"eigen_db/constants"
77
"net/http"
88

99
"github.com/gin-gonic/gin"
@@ -20,14 +20,23 @@ func UpdateM(c *gin.Context) {
2020
}
2121

2222
config := cfg.GetConfig()
23-
err := config.SetM(body.UpdatedM)
24-
if err != nil {
23+
if err := config.SetM(body.UpdatedM); err != nil {
24+
utils.SendResponse(
25+
c,
26+
http.StatusBadRequest,
27+
"Invalid M parameter.",
28+
nil,
29+
utils.CreateError("INVALID_M", err.Error()),
30+
)
31+
return
32+
}
33+
if err := config.WriteToDisk(constants.CONFIG_PATH); err != nil {
2534
utils.SendResponse(
2635
c,
2736
http.StatusInternalServerError,
2837
"An error occured.",
2938
nil,
30-
utils.CreateError("ERROR_UPDATING_M", fmt.Sprintf("Error: %s", err.Error())),
39+
utils.CreateError("ERROR_UPDATING_M", err.Error()),
3140
)
3241
return
3342
}

api/endpoints/update_config/hnsw_params/similarity_metric.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package hnsw_params
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6+
"eigen_db/constants"
67
"eigen_db/types"
7-
"fmt"
88
"net/http"
99

1010
"github.com/gin-gonic/gin"
@@ -20,7 +20,8 @@ func UpdateSimilarityMetric(c *gin.Context) {
2020
return
2121
}
2222

23-
if err := body.UpdatedMetric.Validate(); err != nil {
23+
config := cfg.GetConfig()
24+
if err := config.SetSimilarityMetric(body.UpdatedMetric); err != nil {
2425
utils.SendResponse(
2526
c,
2627
http.StatusBadRequest,
@@ -30,15 +31,13 @@ func UpdateSimilarityMetric(c *gin.Context) {
3031
)
3132
return
3233
}
33-
34-
config := cfg.GetConfig()
35-
if err := config.SetSimilarityMetric(body.UpdatedMetric); err != nil {
34+
if err := config.WriteToDisk(constants.CONFIG_PATH); err != nil {
3635
utils.SendResponse(
3736
c,
3837
http.StatusInternalServerError,
3938
"An error occured.",
4039
nil,
41-
utils.CreateError("ERROR_UPDATING_SIMILARITY_METRIC", fmt.Sprintf("Error: %s", err.Error())),
40+
utils.CreateError("ERROR_UPDATING_SIMILARITY_METRIC", err.Error()),
4241
)
4342
return
4443
}

api/endpoints/update_config/hnsw_params/vector_space_size.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package hnsw_params
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6-
"fmt"
6+
"eigen_db/constants"
77
"net/http"
88

99
"github.com/gin-gonic/gin"
@@ -20,14 +20,23 @@ func UpdateSpaceSize(c *gin.Context) {
2020
}
2121

2222
config := cfg.GetConfig()
23-
err := config.SetSpaceSize(body.UpdatedSize)
24-
if err != nil {
23+
if err := config.SetSpaceSize(body.UpdatedSize); err != nil {
24+
utils.SendResponse(
25+
c,
26+
http.StatusBadRequest,
27+
"Invalid vector space size.",
28+
nil,
29+
utils.CreateError("INVALID_SPACE_SIZE", err.Error()),
30+
)
31+
return
32+
}
33+
if err := config.WriteToDisk(constants.CONFIG_PATH); err != nil {
2534
utils.SendResponse(
2635
c,
2736
http.StatusInternalServerError,
2837
"An error occured.",
2938
nil,
30-
utils.CreateError("ERROR_UPDATING_SPACE_SIZE", fmt.Sprintf("Error: %s", err.Error())),
39+
utils.CreateError("ERROR_UPDATING_SPACE_SIZE", err.Error()),
3140
)
3241
return
3342
}

api/endpoints/update_config/persistence/time_interval.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package persistence
33
import (
44
"eigen_db/api/utils"
55
"eigen_db/cfg"
6-
"fmt"
6+
"eigen_db/constants"
77
"net/http"
88
"time"
99

@@ -21,14 +21,24 @@ func UpdateTimeInterval(c *gin.Context) {
2121
}
2222

2323
config := cfg.GetConfig()
24-
err := config.SetPersistenceTimeInterval(time.Duration(body.UpdatedValueSecs * 1.0e+9))
24+
if err := config.SetPersistenceTimeInterval(time.Duration(body.UpdatedValueSecs * float32(time.Second))); err != nil {
25+
utils.SendResponse(
26+
c,
27+
http.StatusBadRequest,
28+
"Invalid time interval.",
29+
nil,
30+
utils.CreateError("INVALID_TIME_INTERVAL", err.Error()),
31+
)
32+
return
33+
}
34+
err := config.WriteToDisk(constants.CONFIG_PATH)
2535
if err != nil {
2636
utils.SendResponse(
2737
c,
2838
http.StatusInternalServerError,
2939
"An error occured.",
3040
nil,
31-
utils.CreateError("ERROR_UPDATING_PERSISTENCE_TIME_INTERVAL", fmt.Sprintf("Error: %s", err.Error())),
41+
utils.CreateError("ERROR_UPDATING_PERSISTENCE_TIME_INTERVAL", err.Error()),
3242
)
3343
return
3444
}

cfg/config.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cfg
22

33
import (
4+
"errors"
45
"os"
56
"time"
67

@@ -35,15 +36,15 @@ func instantiateConfig() {
3536
config = new(Config)
3637
}
3738

38-
// Returns a pointer to the in-memort config
39+
// Returns a pointer to the in-memory config
3940
func GetConfig() *Config {
4041
return config
4142
}
4243

4344
// Writes the in-memory config to disk as a YAML file at "configPath"
4445
//
4546
// Returns an error if one occured.
46-
func (c *Config) writeToDisk(configPath string) error {
47+
func (c *Config) WriteToDisk(configPath string) error {
4748
cfgYaml, err := yaml.Marshal(config)
4849
if err != nil {
4950
return err
@@ -74,7 +75,7 @@ func (c *Config) populateConfig(configPath string) error {
7475
}
7576

7677
// Config getters and setters:
77-
// NOTE: the setters update the specified value in-memory AND on disk.
78+
// NOTE: the setters update the specified value in-memory ONLY.
7879

7980
func (c *Config) GetPersistenceTimeInterval() time.Duration {
8081
return c.Persistence.TimeInterval
@@ -109,41 +110,65 @@ func (c *Config) GetEfConstruction() int {
109110
}
110111

111112
func (c *Config) SetPersistenceTimeInterval(timeInterval time.Duration) error {
113+
if timeInterval < time.Second*1 {
114+
return errors.New("persistence time interval must be >= 1s")
115+
}
112116
c.Persistence.TimeInterval = timeInterval
113-
return c.writeToDisk(constants.CONFIG_PATH)
117+
return nil
114118
}
115119

116120
func (c *Config) SetAPIPort(port int) error {
121+
if port <= 0 || port > 65535 {
122+
return errors.New("API port must be between 1 and 65535")
123+
}
117124
c.API.Port = port
118-
return c.writeToDisk(constants.CONFIG_PATH)
125+
return nil
119126
}
120127

121128
func (c *Config) SetAPIAddress(address string) error {
129+
if address == "" {
130+
return errors.New("API address cannot be empty")
131+
}
122132
c.API.Address = address
123-
return c.writeToDisk(constants.CONFIG_PATH)
133+
return nil
124134
}
125135

126136
func (c *Config) SetDimensions(dimensions int) error {
137+
if dimensions < 2 {
138+
return errors.New("dimensions must be >= 2")
139+
}
127140
c.HNSWParams.Dimensions = dimensions
128-
return c.writeToDisk(constants.CONFIG_PATH)
141+
return nil
129142
}
130143

131144
func (c *Config) SetSimilarityMetric(similarityMetric t.SimMetric) error {
145+
if err := similarityMetric.Validate(); err != nil {
146+
return errors.New("invalid similarity metric")
147+
}
132148
c.HNSWParams.SimilarityMetric = similarityMetric
133-
return c.writeToDisk(constants.CONFIG_PATH)
149+
return nil
134150
}
135151

136152
func (c *Config) SetSpaceSize(spaceSize uint32) error {
153+
if spaceSize == 0 {
154+
return errors.New("space size must be > 0")
155+
}
137156
c.HNSWParams.SpaceSize = spaceSize
138-
return c.writeToDisk(constants.CONFIG_PATH)
157+
return nil
139158
}
140159

141160
func (c *Config) SetM(M int) error {
161+
if M < 2 {
162+
return errors.New("m must be >= 2")
163+
}
142164
c.HNSWParams.M = M
143-
return c.writeToDisk(constants.CONFIG_PATH)
165+
return nil
144166
}
145167

146168
func (c *Config) SetEfConstruction(efConstruction int) error {
169+
if efConstruction < 0 {
170+
return errors.New("efConstruction must be >= 0")
171+
}
147172
c.HNSWParams.EfConstruction = efConstruction
148-
return c.writeToDisk(constants.CONFIG_PATH)
173+
return nil
149174
}

0 commit comments

Comments
 (0)