Sha256: 851770f58c26abca1d7978c1076db562ff2f7b47052ecd09d89ee3d8000b59ae

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'yard'
require 'ruby-progressbar'

module Yoda
  module Store
    class Registry
      # @return [Adapters::LmdbAdapter, nil]
      attr_reader :adapter

      # @param adapter [Adapters::LmdbAdapter]
      attr_writer :adapter

      # @return [Objects::PatchSet]
      attr_reader :patch_set

      def initialize(adapter = nil)
        @patch_set = Objects::PatchSet.new
        @adapter = adapter
      end

      # @param path [String]
      # @return [Objects::Base, nil]
      def find(path)
        if adapter&.exist?(path)
          patch_set.patch(adapter.get(path))
        else
          patch_set.find(path)
        end
      end

      # @param patch [Patch]
      def add_patch(patch)
        patch_set.register(patch)
      end

      # @param path [String]
      # @return [true, false]
      def has_key?(path)
        adapter&.exists?(path) || patch_set.has_key?(path)
      end

      # Store patch set data to the database.
      # old data in the database are discarded.
      # @param progress [true, false]
      def compress_and_save(progress: false)
        return unless adapter
        el_keys = patch_set.keys
        bar = ProgressBar.create(format: " %c/%C |%w>%i| %e ", total: el_keys.length) if progress

        data = Enumerator.new do |yielder|
          el_keys.each { |key| yielder << [key, patch_set.find(key)] }
        end

        adapter.batch_write(data, bar)
        adapter.sync
        STDERR.puts "saved #{el_keys.length} keys."
        @patch_set = Objects::PatchSet.new
      end

      private

      def keys
        Set.new(adapter&.keys.map(&:to_s) || []).union(patch_set.keys)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yoda-language-server-0.4.0 lib/yoda/store/registry.rb