Class: Amp::RevlogSupport::IndexVersionNG

Inherits:
Index show all
Defined in:
lib/amp/revlogs/index.rb

Overview

IndexVersionNG

This is the current version of the index. I’m not sure why they call it Version ‘NG’ but they do. An index of this type is not inline.

Direct Known Subclasses

IndexInlineNG

Constant Summary collapse

VERSION_FORMAT =
"N"
INDEX_FORMAT_NG =

The binary format used for pack/unpack

"Q NNNNNN a20 x12"
SHA1_OFFSET =

The distance into the entry to go to find the SHA1 hash

32
BLOCK_SIZE =

The size of a single block in the index

8 + (6 * 4) + 20 + 12

Constants included from Support

Support::REVLOG_DEFAULT_FLAGS, Support::REVLOG_DEFAULT_FORMAT, Support::REVLOG_DEFAULT_VERSION, Support::REVLOG_NG_INLINE_DATA, Support::REVLOG_VERSION_0, Support::REVLOG_VERSION_NG

Instance Attribute Summary

Attributes inherited from Index

#cache, #chunk_cache, #index, #indexfile, #node_map

Instance Method Summary collapse

Methods inherited from Index

#<<, #[], #each, #has_node?, parse, #size

Methods included from Enumerable

#inject, #select_map

Methods included from Support

#compress, #decompress, #get_offset, #get_version, #history_hash, #offset_version

Constructor Details

#initialize(opener, inputfile) ⇒ IndexVersionNG

Initializes the index by parsing the given file.

Parameters:

  • inputfile (String)

    the path to the index file.



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/amp/revlogs/index.rb', line 306

def initialize(opener, inputfile)
  @opener = opener
  @indexfile = inputfile
  @cache = nil
  @index = []
  @node_map = {Node::NULL_ID => Node::NULL_REV}
          
  opened = parse_file
  
  if opened
    first_entry = @index[0]
    type = get_version(first_entry.offset_flags)
    first_entry.offset_flags = offset_version(0, type) #turn off inline
    @index[0] = first_entry
  end
  
  @index << IndexEntry.new(0,0,0,-1,-1,-1,-1,Node::NULL_ID)
end

Instance Method Details

#entry_sizeObject

returns the size of 1 block in this type of index



327
# File 'lib/amp/revlogs/index.rb', line 327

def entry_size; BLOCK_SIZE; end

#inline?Boolean

returns whether or not the index stores data with revision info

Returns:



332
# File 'lib/amp/revlogs/index.rb', line 332

def inline?; false; end

#pack_entry(entry, rev) ⇒ String

Packs up the revision entry for writing to the binary file.

Parameters:

  • entry (IndexEntry)

    this is the entry that has to be formatted into binary.

  • rev (Fixnum)

    this is the index number of the entry - if it’s the first revision (rev == 0) then we treat it slightly differently.

Returns:

  • (String)

    the entry converted into binary suitable for writing.



366
367
368
369
370
371
372
373
# File 'lib/amp/revlogs/index.rb', line 366

def pack_entry(entry, rev)
  entry = IndexEntry.new(*entry) if entry.kind_of? Array
  p = entry.to_s
  if rev == 0 || rev == 1
    p = [version].pack(VERSION_FORMAT) + p[4..-1] # initial entry
  end
  p
end

#parse_fileBoolean

Parses each index entry. Internal use only.

Returns:

  • (Boolean)

    whether the file was opened



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/amp/revlogs/index.rb', line 338

def parse_file
  n = 0
  begin
    @opener.open(@indexfile,"r") do |f|
      until f.eof?
        # read the entry
        entry = IndexEntry.new(f).fix_signs
        # store it in the map
        @node_map[entry.node_id] = n
        # add it to the index
        @index << entry
        n += 1
      end
    end
    return true
  rescue Errno::ENOENT
    return false
  end
end

#versionObject

returns the version number of the index



330
# File 'lib/amp/revlogs/index.rb', line 330

def version; REVLOG_VERSION_NG; end

#write_entry(index_file, entry, journal, data, index_file_handle = nil) ⇒ Object

This method writes the index to file. Pretty 1337h4><.

Parameters:

  • index_file (String)

    the path to the index file.



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/amp/revlogs/index.rb', line 379

def write_entry(index_file, entry, journal, data, index_file_handle = nil)
  curr = self.size - 1
  
  link = (entry.is_a? Array) ? entry[4] : entry.link_rev
  data_file = index_file[0..-3] + ".d"
  
  entry = pack_entry entry, link
  
  @opener.open(data_file, "a+") do |data_file_handle|
    data_file_handle.write data[:compression] if data[:compression].any?
    data_file_handle.write data[:text]
    
    data_file_handle.flush
  end
  
  index_file_handle ||= (opened = true && @opener.open(index_file, "a+"))
  
  index_file_handle.write entry
  index_file_handle.close if opened
  
  #journal << [data_file, offset]
  #journal << [index_file, curr * entry.size]
end