Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add LRU hash #970

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
branches:
- main
workflow_dispatch:

env:
MT_COMPAT: true

jobs:
CI:
if: ${{ github.repository == 'googleapis/gapic-generator-ruby' }}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
- main
workflow_dispatch:

env:
MT_COMPAT: true


jobs:
tests:
if: ${{ github.repository == 'googleapis/gapic-generator-ruby' }}
Expand Down
2 changes: 1 addition & 1 deletion gapic-common/lib/gapic/generic_lro/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def initialize operation, client:, polling_method_name:, operation_status_field:
#
def results
return error if error?
return response if response?
response if response?
end

##
Expand Down
106 changes: 106 additions & 0 deletions gapic-common/lib/gapic/lru_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Gapic
##
# @private
#
# Linked list based hash maintaining the order of
dazuma marked this conversation as resolved.
Show resolved Hide resolved
# access/creation of the keys.
#
class LruHash
def initialize size = 1
raise ArgumentError, "The size of LRU hash can't be < 1" unless size > 1
@start = nil
@end = nil
@size = size
@cache = {}
end

def get key
return nil unless @cache.key? key
node = @cache[key]
move_to_top node
node.value
end

def put key, value
if @cache.key? key
node = @cache[key]
node.value = value
move_to_top node
else
remove_tail if @cache.size >= @size
new_node = Node.new key, value
insert_at_top new_node
@cache[key] = new_node
end
end

private

def move_to_top node
return if node.equal? @start

if node.equal? @end
@end = node.prev
@end.next = nil
else
node.prev.next = node.next
node.next.prev = node.prev
end

node.prev = nil
node.next = @start
@start.prev = node
@start = node
end

def remove_tail
@cache.delete @end.key
@end = @end.prev
@end.next = nil if @end
end

def insert_at_top node
if @start.nil?
@start = node
@end = node
else
node.next = @start
@start.prev = node
@start = node
end
end

##
# @private
#
# Node class for linked list.
dazuma marked this conversation as resolved.
Show resolved Hide resolved
#
class Node
attr_accessor :key
dazuma marked this conversation as resolved.
Show resolved Hide resolved
attr_accessor :value
attr_accessor :prev
attr_accessor :next

def initialize key, value
@key = key
@value = value
@prev = nil
@next = nil
end
end
end
end
2 changes: 1 addition & 1 deletion gapic-common/lib/gapic/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def initialize grpc_op, client, result_type: nil, metadata_type: nil, options: {
# `Google::Rpc::Status` will be returned.
def results
return error if error?
return response if response?
response if response?
end

##
Expand Down
2 changes: 1 addition & 1 deletion gapic-common/lib/gapic/rest/grpc_transcoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def extract_scalar_value! request_hash, field_path, regex

# Covers the case where in `foo.bar.baz`, `baz` is still a submessage or an array.
return nil if value.is_a?(::Hash) || value.is_a?(::Array)
return value.to_s if value.to_s =~ regex
value.to_s if value.to_s =~ regex
end

# Finds a value in the hash by path.
Expand Down
54 changes: 54 additions & 0 deletions gapic-common/test/gapic/lru_hash_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "test_helper"
require "gapic/lru_hash"

##
# Tests LRU hash class
#
class LruHashTest < Minitest::Test
def test_hash_size_exceed
lru_cache = Gapic::LruHash.new 3
lru_cache.put 1, "one"
lru_cache.put 2, "two"
lru_cache.put 3, "three"
lru_cache.put 4, "four"
assert lru_cache.get(1).nil?
assert_equal "four", lru_cache.get(4)
end

def test_hash_size_value
assert_raises ArgumentError do
Gapic::LruHash.new 0
end
assert_raises ArgumentError do
Gapic::LruHash.new(-1)
end
end

def test_hash_removes_lru_value
lru_cache = Gapic::LruHash.new 3
lru_cache.put 1, "one"
lru_cache.put 2, "two"
lru_cache.put 3, "three"
lru_cache.get 3
lru_cache.get 2
lru_cache.get 1

lru_cache.put 4, "four"
assert lru_cache.get(3).nil?
assert_equal "four", lru_cache.get(4)
end
end