Module: CRUDJT

Extended by:
FFI::Library
Defined in:
lib/crudjt.rb,
lib/lru_cache.rb,
lib/validation.rb,
lib/crudjt/version.rb,
lib/errors/invalid_state.rb,
lib/errors/internal_error.rb

Defined Under Namespace

Modules: Config, Errors, Validation Classes: LRUCache

Constant Summary collapse

ERRORS =
{
  '55JT01' => CRUDJT::Errors::InvalidState,
  'XX000' => CRUDJT::Errors::InternalError
}
VERSION =
"1.0.0-beta.1"

Class Method Summary collapse

Class Method Details

.create(data, ttl: nil, silence_read: nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/crudjt.rb', line 166

def self.create(data, ttl: nil, silence_read: nil)
  if CRUDJT::Config.master?
    CRUDJT.original_create(data, ttl: ttl, silence_read: silence_read)
  else
    # token_service.proto expect int64/32 values
    # it sensative for nil and covert it to 0
    ttl ||= -1
    silence_read ||= -1

    CRUDJT::Config.stub.create_token(Token::CreateTokenRequest.new(packed_data: MessagePack.pack(data), ttl: ttl, silence_read: silence_read)).token
  end
end

.delete(token) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/crudjt.rb', line 253

def self.delete(token)
  if CRUDJT::Config.master?
    original_delete(token)
  else
    CRUDJT::Config.stub.delete_token(Token::DeleteTokenRequest.new(token: token)).result
  end
end

.original_create(hash, ttl: nil, silence_read: nil) ⇒ Object

Raises:

  • (CRUDJT::CRUDJT::ERRORS::InternalError)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/crudjt.rb', line 143

def self.original_create(hash, ttl: nil, silence_read: nil)
  raise CRUDJT::Validation.error_message(CRUDJT::Validation::ERROR_NOT_STARTED) unless CRUDJT::Config.was_started
  CRUDJT::Validation.validate_insertion!(hash, ttl, silence_read)

  ttl ||= -1
  silence_read ||= -1

  packed_data = MessagePack.pack(hash)
  hash_bytesize = packed_data.bytesize
  CRUDJT::Validation.validate_hash_bytesize!(hash_bytesize)

  # Creation buffer with packed data
  buffer = FFI::MemoryPointer.new(:char, hash_bytesize)
  buffer.put_bytes(0, packed_data)

  token = __create(buffer, hash_bytesize, ttl, silence_read)
  raise CRUDJT::CRUDJT::ERRORS::InternalError, 'Something went wrong. Ups' unless token

  @lru_cache.insert(token, packed_data, ttl, silence_read)

  token
end

.original_delete(token) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/crudjt.rb', line 245

def self.original_delete(token)
  raise CRUDJT::Validation.error_message(CRUDJT::Validation::ERROR_NOT_STARTED) unless CRUDJT::Config.was_started
  CRUDJT::Validation.validate_token!(token)

  @lru_cache.delete(token)
  __delete(token)
end

.original_read(token) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/crudjt.rb', line 179

def self.original_read(token)
  raise CRUDJT::Validation.error_message(CRUDJT::Validation::ERROR_NOT_STARTED) unless CRUDJT::Config.was_started
  CRUDJT::Validation.validate_token!(token)

  output = @lru_cache.get(token)
  return output if output

  str = __read(token)
  return if str.nil?

	result = JSON.parse(str)
  raise CRUDJT::ERRORS[result['code']] unless result['ok']
  return if result['data'].nil?

  data = JSON(result['data'])
  @lru_cache.force_insert(token, data)
  data
end

.original_update(token, hash, ttl: nil, silence_read: nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/crudjt.rb', line 208

def self.original_update(token, hash, ttl: nil, silence_read: nil)
  raise CRUDJT::Validation.error_message(CRUDJT::Validation::ERROR_NOT_STARTED) unless CRUDJT::Config.was_started
  CRUDJT::Validation.validate_token!(token)
  CRUDJT::Validation.validate_insertion!(hash, ttl, silence_read)

  packed_data = MessagePack.pack(hash)
  hash_bytesize = packed_data.bytesize
  CRUDJT::Validation.validate_hash_bytesize!(hash_bytesize)

  ttl ||= -1
  silence_read ||= -1

  # Creation buffer with packed data
  buffer = FFI::MemoryPointer.new(:char, hash_bytesize)
  buffer.put_bytes(0, packed_data)

  result = __update(token, buffer, hash_bytesize, ttl, silence_read)
  if result
    @lru_cache.delete(token)
    @lru_cache.insert(token, packed_data, ttl, silence_read)
  end
  result
end

.read(token) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/crudjt.rb', line 198

def self.read(token)
  if CRUDJT::Config.master?
    CRUDJT.original_read(token)
  else
    resp = CRUDJT::Config.stub.read_token(Token::ReadTokenRequest.new(token: token))

    MessagePack.unpack(resp.packed_data)
  end
end

.update(token, data, ttl: nil, silence_read: nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/crudjt.rb', line 232

def self.update(token, data, ttl: nil, silence_read: nil)
  if CRUDJT::Config.master?
    CRUDJT.original_update(token, data, ttl: ttl, silence_read: silence_read)
  else
    # token_service.proto expect int64/32 values
    # it sensative for nil and covert it to 0
    ttl ||= -1
    silence_read ||= -1

    CRUDJT::Config.stub.update_token(Token::UpdateTokenRequest.new(token: token, packed_data: MessagePack.pack(data), ttl: ttl, silence_read: silence_read)).result
  end
end