Skip to content

Commit 0c5fe4c

Browse files
Merge pull request #6 from andreibondarev/backups-endpoint
Backups endpoint
2 parents c5cb684 + c26f67a commit 0c5fe4c

File tree

11 files changed

+260
-2
lines changed

11 files changed

+260
-2
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
weaviate-ruby (0.4.0)
4+
weaviate-ruby (0.5.0)
55
faraday (~> 2.7)
66
graphlient (~> 0.7.0)
77

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ response = client.objects.batch_create(objects: [
134134
}
135135
])
136136
response.data
137+
138+
# Batch delete objects
139+
client.objects.batch_delete(
140+
class_name: "Question",
141+
where: {
142+
valueString: "1",
143+
operator: "Equal",
144+
path: ["id"]
145+
}
146+
)
137147
```
138148

139149
### Querying
@@ -201,6 +211,30 @@ client.classifications.get(
201211
)
202212
```
203213

214+
### Backups
215+
```ruby
216+
client.backups.create(
217+
backend: "filesystem",
218+
id: "my-first-backup",
219+
include: ["Question"]
220+
)
221+
222+
client.backups.get(
223+
backend: "filesystem",
224+
id: "my-first-backup"
225+
)
226+
227+
client.backups.restore(
228+
backend: "filesystem",
229+
id: "my-first-backup"
230+
)
231+
232+
client.backups.restore_status(
233+
backend: "filesystem",
234+
id: "my-first-backup"
235+
)
236+
```
237+
204238
### Nodes
205239
```ruby
206240
client.nodes

lib/weaviate.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Weaviate
1414
autoload :Nodes, "weaviate/nodes"
1515
autoload :Health, "weaviate/health"
1616
autoload :Classifications, "weaviate/classifications"
17+
autoload :Backups, "weaviate/backups"
1718

1819
module Response
1920
autoload :Base, "weaviate/response/base"

lib/weaviate/backups.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
module Weaviate
4+
class Backups < Base
5+
PATH = "backups"
6+
7+
def create(
8+
backend:,
9+
id:,
10+
include: nil,
11+
exclude: nil
12+
)
13+
response = client.connection.post("#{PATH}/#{backend}") do |req|
14+
req.body = {}
15+
req.body["id"] = id
16+
req.body["include"] = include if include
17+
req.body["exclude"] = exclude if exclude
18+
end
19+
20+
response.body
21+
end
22+
23+
def get(
24+
backend:,
25+
id:
26+
)
27+
response = client.connection.get("#{PATH}/#{backend}/#{id}")
28+
response.body
29+
end
30+
31+
def restore(
32+
backend:,
33+
id:,
34+
include: nil,
35+
exclude: nil
36+
)
37+
response = client.connection.post("#{PATH}/#{backend}/#{id}/restore") do |req|
38+
req.body = {}
39+
req.body["include"] = include if include
40+
req.body["exclude"] = exclude if exclude
41+
end
42+
43+
response.body
44+
end
45+
46+
def restore_status(
47+
backend:,
48+
id:
49+
)
50+
response = client.connection.get("#{PATH}/#{backend}/#{id}/restore")
51+
response.body
52+
end
53+
end
54+
end

lib/weaviate/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def ready?
5959
@health.ready?
6060
end
6161

62+
def backups
63+
@backups ||= Weaviate::Backups.new(client: self)
64+
end
65+
6266
def classifications
6367
@classifications ||= Weaviate::Classifications.new(client: self)
6468
end

lib/weaviate/objects.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ def delete(class_name:, id:)
108108
response.success? && response.body.empty?
109109
end
110110

111+
def batch_delete(
112+
class_name:,
113+
where:,
114+
consistency_level: nil,
115+
output: nil,
116+
dry_run: nil
117+
)
118+
path = "batch/#{PATH}"
119+
path << "?consistency_level=#{consistency_level}" unless consistency_level.nil?
120+
121+
response = client.connection.delete(path) do |req|
122+
req.body = {
123+
match: {
124+
class: class_name,
125+
where: where
126+
}
127+
}
128+
req.body["output"] = output unless output.nil?
129+
req.body["dryRun"] = dry_run unless dry_run.nil?
130+
end
131+
132+
response.body
133+
end
134+
111135
# Validate a data object
112136
# def validate
113137
# end

lib/weaviate/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Weaviate
4-
VERSION = "0.4.0"
4+
VERSION = "0.5.0"
55
end

spec/fixtures/backup.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"backend": "filesystem",
3+
"classes": ["Question"],
4+
"id": "my-first-backup",
5+
"path": "/var/lib/weaviate/backups/my-first-backup",
6+
"status": "STARTED"
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"dryRun": false,
3+
"match": {
4+
"class": "Question",
5+
"where": {
6+
"operands": null,
7+
"operator": "Equal",
8+
"path": ["id"],
9+
"valueString": "1"
10+
}
11+
},
12+
"output": "minimal",
13+
"results": {
14+
"failed": 0,
15+
"limit": 10000,
16+
"matches": 1,
17+
"objects": null,
18+
"successful": 1
19+
}
20+
}

spec/weaviate/backups_spec.rb

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Weaviate::Backups do
6+
let(:client) {
7+
Weaviate::Client.new(
8+
scheme: "http",
9+
host: "localhost:8080"
10+
)
11+
}
12+
13+
let(:backups) { client.backups }
14+
let(:backup_fixture) { JSON.parse(File.read("spec/fixtures/backup.json")) }
15+
16+
describe "#create" do
17+
let(:response) { OpenStruct.new(success?: true, body: backup_fixture) }
18+
19+
before do
20+
allow_any_instance_of(Faraday::Connection).to receive(:post)
21+
.with("backups/filesystem")
22+
.and_return(response)
23+
end
24+
25+
it "creates the backup" do
26+
response = backups.create(
27+
backend: "filesystem",
28+
id: "my-first-backup",
29+
include: ["Question"]
30+
)
31+
expect(response["id"]).to eq("my-first-backup")
32+
expect(response["status"]).to eq("STARTED")
33+
end
34+
end
35+
36+
describe "#get" do
37+
let(:response) { OpenStruct.new(success?: true, body: backup_fixture) }
38+
39+
before do
40+
allow_any_instance_of(Faraday::Connection).to receive(:get)
41+
.with("backups/filesystem/my-first-backup")
42+
.and_return(response)
43+
end
44+
45+
it "returns the backup" do
46+
response = backups.get(
47+
backend: "filesystem",
48+
id: "my-first-backup"
49+
)
50+
expect(response["id"]).to eq("my-first-backup")
51+
end
52+
end
53+
54+
describe "#restore" do
55+
let(:response) { OpenStruct.new(success?: true, body: backup_fixture) }
56+
57+
before do
58+
allow_any_instance_of(Faraday::Connection).to receive(:post)
59+
.with("backups/filesystem/my-first-backup/restore")
60+
.and_return(response)
61+
end
62+
63+
it "restores the backup" do
64+
response = backups.restore(
65+
backend: "filesystem",
66+
id: "my-first-backup",
67+
include: ["Question"]
68+
)
69+
expect(response["id"]).to eq("my-first-backup")
70+
expect(response["status"]).to eq("STARTED")
71+
end
72+
end
73+
74+
describe "#restore_status" do
75+
let(:response) { OpenStruct.new(success?: true, body: backup_fixture) }
76+
77+
before do
78+
allow_any_instance_of(Faraday::Connection).to receive(:get)
79+
.with("backups/filesystem/my-first-backup/restore")
80+
.and_return(response)
81+
end
82+
83+
it "returns the restore status" do
84+
response = backups.restore_status(
85+
backend: "filesystem",
86+
id: "my-first-backup"
87+
)
88+
expect(response["id"]).to eq("my-first-backup")
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)