Module: Store::Digest::Meta::LMDB

Includes:
Store::Digest::Meta, Trait::RootDir
Included in:
Driver::LMDB
Defined in:
lib/store/digest/meta/lmdb.rb

Constant Summary collapse

PARAMS =

Return a list of objects matching the given criteria. The result set will be the intersection of all supplied parameters. ‘:type`, `:charset`, `:encoding`, and `:language` are treated like discrete sets, while the rest of the parameters are treated like ranges (two-element arrays). Single values will be coerced into arrays; single range values will be interpreted as an inclusive lower bound. To bound only at the top, use a two-element array with its first value `nil`, like so: `size: [nil, 31337]`. The sorting criteria are the symbols of the other parameters.

Returns:

  • (Array)

    the list

%i[type charset encoding language
size ctime mtime ptime dtime].freeze

Instance Attribute Summary

Attributes included from Trait::RootDir

#dir, #umask

Instance Method Summary collapse

Instance Method Details

#algorithmsArray

Return the set of algorithms initialized in the database.

Returns:

  • (Array)

    the algorithms



463
464
465
466
467
468
469
470
# File 'lib/store/digest/meta/lmdb.rb', line 463

def algorithms
  
  @algorithms ||= @lmdb.transaction do
    if ret = @dbs[:control]['algorithms']
      ret.strip.split(/\s*,+\s*/).map(&:to_sym)
    end
  end
end

#bytesInteger

Return the number of bytes stored in the database (notwithstanding the database itself).

Returns:

  • (Integer)


506
507
508
509
510
511
512
# File 'lib/store/digest/meta/lmdb.rb', line 506

def bytes
  @lmdb.transaction do
    if ret = @dbs[:control]['bytes']
      ret.unpack1 'Q>'
    end
  end
end

#deletedInteger

Return the number of objects whose payloads are deleted but are still on record.

Returns:

  • (Integer)


495
496
497
498
499
500
501
# File 'lib/store/digest/meta/lmdb.rb', line 495

def deleted
  @lmdb.transaction do
    if ret = @dbs[:control]['deleted']
      ret.unpack1 'Q>'
    end
  end
end

#list(type: nil, charset: nil, encoding: nil, language: nil, size: nil, ctime: nil, mtime: nil, ptime: nil, dtime: nil, sort: nil) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/store/digest/meta/lmdb.rb', line 539

def list type: nil, charset: nil, encoding: nil, language: nil,
    size: nil, ctime: nil, mtime: nil, ptime: nil, dtime: nil, sort: nil
  # coerce all the inputs
  params = begin
             b  = binding
             ph = {}
             PARAMS.each do |key|
               val = b.local_variable_get key
               val = case val
                     when nil then []
                     when Time then [val]
                     when DateTime then [val.to_time]
                     when -> (v) { v.respond_to? :to_a } then val.to_a
                     else [val]
                     end
               ph[key] = val unless val.empty?
             end
             ph
           end
  # find the smallest denominator
  index = params.keys.map do |k|
    [k, @dbs[k].size]
  end.sort { |a, b| a[1] <=> b[1] }.map(&:first).first
  out = {}
  @lmdb.transaction do
    if index
      warn params.inspect
      if INTS[index]
        index_get index, *params[index], range: true do |_, v|
          u = URI("ni:///#{primary};")
          u.digest = v
          out[u] ||= get u
        end
      else
        params[index].each do |val|
          index_get index, val do |_, v|
            u = URI("ni:///#{primary};")
            u.digest = v
            out[u] ||= get u
          end
        end
      end
      rest = params.keys - [index]
      unless rest.empty?
        out.select! do |_, obj|
          rest.map do |param|
            if val = obj.send(param)
              warn "#{param} #{params[param]} <=> #{val}"
              if INTS[param]
                min, max = params[param]
                if min && max
                  val >= min && val <= max
                elsif min
                  val >= min
                elsif max
                  val <= max
                end
              else
                params[param].include? val
              end
            else
              false
            end
          end.all?(true)
        end
      end
    else
      # if we aren't filtering at all we can just obtain everything
      @dbs[primary].cursor do |c|
        while rec = c.next
          u = URI("ni:///#{primary};")
          u.digest = rec.first
          out[u] ||= get u
        end
      end
    end
  end

  # now we sort
  out.values
end

#objectsInteger

Return the number of objects in the database.

Returns:

  • (Integer)


484
485
486
487
488
489
490
# File 'lib/store/digest/meta/lmdb.rb', line 484

def objects
  @lmdb.transaction do
    if ret = @dbs[:control]['objects']
      ret.unpack1 'Q>' # 64-bit unsigned network-endian integer
    end
  end
end

#primarySymbol

Return the primary digest algorithm.

Returns:

  • (Symbol)

    the primary algorithm



474
475
476
477
478
479
480
# File 'lib/store/digest/meta/lmdb.rb', line 474

def primary
  @primary ||= @lmdb.transaction do
    if ret = @dbs[:control]['primary']
      ret.strip.to_sym
    end
  end
end

#transaction(&block) ⇒ Object



455
456
457
458
459
# File 'lib/store/digest/meta/lmdb.rb', line 455

def transaction &block
  @lmdb.transaction do
    block.call
  end
end