Class: Kurosawa::Database

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

Instance Method Summary collapse

Instance Method Details

#delete(fs, path) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/kurosawa/database.rb', line 152

def delete(fs, path)
	inner_entries = fs.ls("#{path}")

	puts "delete: #{inner_entries.inspect}"

	inner_entries.each do |x|
		fs.del(x)
	end
end

#get_property(fs, path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kurosawa/database.rb', line 43

def get_property(fs, path)
	property_entries = fs.ls("#{path}/$")
	if property_entries.length == 0
		nil
	elsif property_entries.length == 1
		JSON.parse(fs.get(property_entries[0]), symbolize_names: true)
	else
		# conflict
		raise "TODO get_property handle conflict"
	end
end

#random_keyObject



39
40
41
# File 'lib/kurosawa/database.rb', line 39

def random_key
	SecureRandom.hex(4)
end

#read(fs, path, params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kurosawa/database.rb', line 68

def read(fs, path, params)

	puts "read: path=#{path.inspect}"
	prop = get_property(fs, path)
	if prop == nil
		nil
	elsif prop[:type] == "object"
		res = {}

		inner_entries = fs.ls("#{path}")
			.select{ |x| /^\/\$/.match(x) == nil }
			.map{ |x| x.sub(/\/[^\/]+$/, "") }
			.map{ |x| match = /(?<first>^\/[^\/]+)/.match(x.sub(/^#{path}/, "")); match[:first] if match }
			.select{ |x| x != nil}
			.uniq

		puts "read: object: inner: #{fs.ls("#{path}").inspect}"
		puts "read: object: inner: #{inner_entries.inspect}"

		inner_entries.each do |x|
			res[x[1..-1]] = read(fs, path + x, params)
		end

		res

	elsif prop[:type] == "array"
		inner_entries = fs.ls("#{path}")
			.select{ |x| /\/\$/.match(x) == nil }
			.map{ |x| match = /(?<first>^\/[^\/]+)/.match(x.sub(/^#{path}/, "")); match[:first] if match }
			.select{ |x| x != nil}
			.uniq

		puts "read: array: #{inner_entries.inspect}"

		inner_entries.map do |x|
			read(fs, path + x, params)
		end

	else
		inner_entries = fs.ls("#{path}")
			.select{ |x| /\/\$/.match(x) == nil }

		puts "read: string: #{inner_entries.inspect}"

		if inner_entries.length == 1
			JSON.parse(fs.get(inner_entries[0]), symbolize_names: true)[:value] 
		elsif inner_entries.length == 0
			nil
		else
			{"$conflict": inner_entries.map{|x| JSON.parse(fs.get(x), symbolize_names: true) }}
			#raise "read conflict at #{path} #{inner_entries.inspect}"
		end
	end
end

#sanitize_body(body) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/kurosawa/database.rb', line 30

def sanitize_body(body)
	begin
		JSON.parse(body)
	rescue => e
		puts e
		halt 400
	end
end

#sanitize_path(path) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/kurosawa/database.rb', line 20

def sanitize_path(path)
	match = /^\/[a-z0-9\-_\/]*$/i.match(URI.unescape(path))
	if match
		match.to_s.sub(/\/+$/, "")
	else
		p URI.unescape(path)
		halt 400
	end
end

#set_property(fs, path, type:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kurosawa/database.rb', line 55

def set_property(fs, path, type:)
	puts "set property: #{path}"
	property_entries = fs.ls("#{path}/$")
	p property_entries
	if property_entries.length > 0
		property_entries.each do |x|
			fs.del(x)
		end
	end
	fs.put("#{path}/$#{random_key}", {type: type}.to_json)
end

#write(fs, path, body) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kurosawa/database.rb', line 123

def write(fs, path, body)

	puts "write: path=#{path.inspect}"

	if body.is_a? Hash
		set_property(fs, path, type: "object")
		body.each do |k,v|
			write(fs, "#{path}/#{k}", v)
		end

	elsif body.is_a? Array
		set_property(fs, path, type: "array")
		body.each do |value|
			write(fs, "#{path}/#{random_key}", value)
		end

	else
		existing_entries = fs.ls("#{path}")

		if existing_entries.length > 0
			existing_entries.each do |x|
				fs.del(x)
			end
		end
		set_property(fs, path, type: "string")
		fs.put("#{path}/#{random_key}", {value: body.to_s}.to_json)
	end
end