Class: Epsilla::DataBase

Inherits:
Base
  • Object
show all
Defined in:
lib/epsilla/database.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Epsilla::Base

Instance Method Details

#create_table(table_name = "MyTable", table_fields = nil) ⇒ Object

create table in the db



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/epsilla/database.rb', line 32

def create_table(table_name = "MyTable", table_fields = nil)
  unless @db_name
      raise Exception("[ERROR] Please use_db() first!")
  end
  unless table_fields
      table_fields = Array.new
  end

  response = client.connection.post do |req|
    req.url "/api/#{@db_name}/schema/tables"
    req.body = {"name": table_name, "fields": table_fields}.to_json
  end

  return response.status, response.body
end

#drop_db(db_name) ⇒ Object

drop db



133
134
135
136
137
138
# File 'lib/epsilla/database.rb', line 133

def drop_db(db_name)
  response = client.connection.delete do |req|
    req.url "/api/#{@db_name}/drop"
  end
  return response.status, response.body
end

#drop_table(table_name) ⇒ Object

drop table



122
123
124
125
126
127
128
129
130
# File 'lib/epsilla/database.rb', line 122

def drop_table(table_name)
  unless @db_name
    raise Exception("[ERROR] Please use_db() first!")
  end     
  response = client.connection.delete do |req|
    req.url "/api/#{@db_name}/schema/tables/#{table_name}"
  end
  return response.status, response.body
end

#get(table_name = "MyTable", response_fields = nil) ⇒ Object

get



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/epsilla/database.rb', line 107

def get(table_name= "MyTable", response_fields = nil)
  unless @db_name
    raise Exception("[ERROR] Please use_db() first!")
  end
  unless response_fields
    response_fields = Array.new
  end
  response = client.connection.post do |req|
    req.url "/api/#{@db_name}/data/get"
    req.body = {"table": table_name, "response": response_fields}.to_json
  end
  return response.status, response.body
end

#insert(table_name = "MyTable", table_records = nil) ⇒ Object

insert data into table



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/epsilla/database.rb', line 62

def insert(table_name = "MyTable", table_records = nil)
  unless @db_name
    raise Exception("[ERROR] Please use_db() first!")
  end
  unless table_records
    table_records = Array.new
  end
  response = client.connection.post do |req|
    req.url "/api/#{@db_name}/data/insert"
    req.body = {"table": table_name, "data": table_records}.to_json
  end
  
  return response.status, response.body
end

#list_tablesObject

get a list of table names in current DB



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/epsilla/database.rb', line 49

def list_tables()
  unless @db_name
    raise Exception("[ERROR] Please use_db() first!")
  end
  
  response = client.connection.post do |req|
    req.url "/api/#{@db_name}/schema/tables/show"
  end
  
  return response.status, response.body      
end

#load_db(db_name, db_path, vector_scale: nil, wal_enabled: false) ⇒ Object

load db



12
13
14
15
16
17
18
19
20
# File 'lib/epsilla/database.rb', line 12

def load_db(db_name, db_path, vector_scale: nil, wal_enabled: false)
  response = client.connection.post do |req|
    req.url "/api/load"
    req.body = {"name": db_name, "path": db_path}.to_json
    req.body["vectorScale"] = vector_scale if vector_scale
    req.body["walEnabled"] = wal_enabled if wal_enabled
  end
  return response.status, response.body
end

#query(table_name = "MyTable", query_field = "", query_vector = nil, response_fields = nil, limit = 1, with_distance = false) ⇒ Object

query



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/epsilla/database.rb', line 88

def query(table_name = "MyTable", query_field = "", query_vector = nil, response_fields = nil, limit = 1, with_distance = false)
  unless @db_name
    raise Exception("[ERROR] Please use_db() first!")
  end
  unless query_vector
    query_vector = ""
  end
  unless response_fields
    response_fields = Array.new
  end

  response = client.connection.post do |req|
    req.url "/api/#{@db_name}/data/query"
    req.body = {"table": table_name, "queryField": query_field, "queryVector": query_vector, "response": response_fields, "limit": limit, "withDistance": with_distance}.to_json
  end
  return response.status, response.body
end

#rebuildObject

rebuild the table



78
79
80
81
82
83
84
85
# File 'lib/epsilla/database.rb', line 78

def rebuild
  # puts "[INFO] waiting until rebuild is finished ..."
  response = client.connection.post do |req|
    req.url '/api/rebuild'
    req.options.timeout = 7200 # 7200s
  end
  return response.status, response.body
end

#unload_db(db_name) ⇒ Object

unload db



23
24
25
26
27
28
29
# File 'lib/epsilla/database.rb', line 23

def unload_db(db_name)
  @db_name = nil
  response = client.connection.post do |req|
    req.url "/api/#{db_name}/unload"
  end
  return response.status, response.body
end

#use_db(db_name) ⇒ Object

use db



7
8
9
# File 'lib/epsilla/database.rb', line 7

def use_db(db_name)
  @db_name = db_name
end