Skip to content

Commit

Permalink
fix: ignore missing namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvduroiu committed Aug 29, 2024
1 parent dbe642c commit b172263
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
15 changes: 3 additions & 12 deletions pinecone/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,9 @@ func (h *Handler) FetchVectors(w http.ResponseWriter, r *http.Request) {
ids := params["ids"]
namespace := params.Get("namespace")

vectors, err := h.Index.Fetch(namespace, ids)
vectors := h.Index.Fetch(namespace, ids)
vectorResponse := make(map[string]*Vector, len(vectors))

if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}

for _, vector := range vectors {
vectorResponse[vector.ID] = vector
}
Expand All @@ -167,7 +162,7 @@ func (h *Handler) UpdateVector(w http.ResponseWriter, r *http.Request) {
return
}

vector, err := h.Index.GetVector(vectorUpdate.Namespace, vectorUpdate.ID)
vector := h.Index.GetVector(vectorUpdate.Namespace, vectorUpdate.ID)

if vector == nil {
w.WriteHeader(http.StatusNotFound)
Expand Down Expand Up @@ -213,11 +208,7 @@ func (h *Handler) ListVectorIDs(w http.ResponseWriter, r *http.Request) {
prefix := params.Get("prefix")
namespace := params.Get("namespace")

vectors, err := h.Index.ListVectorIDs(namespace, prefix)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
vectors := h.Index.ListVectorIDs(namespace, prefix)

response := make(map[string]any)
response["vectors"] = vectors
Expand Down
33 changes: 12 additions & 21 deletions pinecone/index.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pinecone

import (
"errors"
"fmt"
"strings"
)

Expand All @@ -26,16 +24,14 @@ func (i *Index) CreateNamespace(name string) {
i.Namespaces[name] = make(map[string]*Vector)
}

func (i *Index) UpsertVector(namespace string, vector *Vector) error {
func (i *Index) UpsertVector(namespace string, vector *Vector) {
ns, exists := i.Namespaces[namespace]
var err error
if !exists {
err = errors.New(fmt.Sprintf("Namespace not found: %s", namespace))
ns = map[string]*Vector{vector.ID: vector}
} else {
ns[vector.ID] = vector
}

return err
i.Namespaces[namespace] = ns
}

func (i *Index) Query(namespace string, topK int) ([]*Vector, error) {
Expand Down Expand Up @@ -68,7 +64,7 @@ func (i *Index) Query(namespace string, topK int) ([]*Vector, error) {
return vectors, nil
}

func (i *Index) Fetch(namespace string, ids []string) ([]*Vector, error) {
func (i *Index) Fetch(namespace string, ids []string) []*Vector {
vectors := make([]*Vector, 0)

if namespace == "" {
Expand All @@ -83,7 +79,7 @@ func (i *Index) Fetch(namespace string, ids []string) ([]*Vector, error) {
} else {
ns, exists := i.Namespaces[namespace]
if !exists {
return nil, errors.New(fmt.Sprintf("Namespace %s does not exist"))
return vectors
} else {
for _, id := range ids {
vector, exists := ns[id]
Expand All @@ -94,22 +90,17 @@ func (i *Index) Fetch(namespace string, ids []string) ([]*Vector, error) {
}
}

return vectors, nil
return vectors
}

func (i *Index) GetVector(namespace string, id string) (*Vector, error) {
func (i *Index) GetVector(namespace string, id string) *Vector {
ns, exists := i.Namespaces[namespace]
if !exists {
err := errors.New(fmt.Sprintf("Namespace not found: %s", namespace))
return nil, err
return &Vector{}
}
vector, exists := ns[id]
if !exists {
return nil, nil
}

return vector, nil

return vector
}

func (i *Index) DeleteVector(vectorDelete VectorDeleteQuery) {
Expand All @@ -134,13 +125,13 @@ func (i *Index) DeleteVector(vectorDelete VectorDeleteQuery) {
}
}

func (i *Index) ListVectorIDs(namespace string, prefix string) ([]map[string]string, error) {
func (i *Index) ListVectorIDs(namespace string, prefix string) []map[string]string {
vectors := make([]map[string]string, 0)

if namespace != "" {
ns, exists := i.Namespaces[namespace]
if !exists {
return nil, errors.New(fmt.Sprintf("Namespace %s does not exist"))
return vectors
}
for id, _ := range ns {
if strings.HasPrefix(id, prefix) {
Expand All @@ -158,5 +149,5 @@ func (i *Index) ListVectorIDs(namespace string, prefix string) ([]map[string]str

}

return vectors, nil
return vectors
}

0 comments on commit b172263

Please sign in to comment.