Class: SCV::ObjectStore

Inherits:
VCSToolkit::ObjectStore
  • Object
show all
Defined in:
lib/scv/object_store.rb

Overview

Implements VCSToolkit::ObjectStore to store objects on the file system. The directory structure is as follows:

.scv/

  objects/
    59/
      59873e99cef61a60b3826e1cbb9d4b089ae78c2b.json
      ...
    ...

  refs/
    HEAD.json
    master.json
    ...

  blobs/
    59/
      59873e99cef61a60b3826e1cbb9d4b089ae78c2b
      ...
    ...

Each object in ‘.scv/objects/` is stored in a directory with a name of the first two symbols of the object id. The file extension determines the object format. Possible formats are `json` and `json.gz` (`json.gz` will be supported in the future).

For each blob object in ‘.scv/blobs/` there may be a file in `.scv/objects/`. These blobs follow the same naming scheme as the objects, but they are just binary files (not in `json` format).

The refs are named objects (object.named? == true) and can be enumerated.

Instance Method Summary collapse

Constructor Details

#initialize(file_store) ⇒ ObjectStore

Returns a new instance of ObjectStore.



44
45
46
# File 'lib/scv/object_store.rb', line 44

def initialize(file_store)
  @store = file_store
end

Instance Method Details

#each(&block) ⇒ Object



91
92
93
94
95
# File 'lib/scv/object_store.rb', line 91

def each(&block)
  return [] unless @store.directory? 'refs'

  @store.files('refs').map { |name| name.sub /\..*$/, '' }.each &block
end

#fetch(object_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/scv/object_store.rb', line 62

def fetch(object_id)
  object_location = find_object object_id

  return fetch_blob(object_id) if object_location[:type] == :blob

  hash = JSON.parse(@store.fetch(object_location[:path]))
  object_type = hash['object_type'].capitalize

  raise 'Unknown object type' unless Objects.const_defined? object_type

  Objects.const_get(object_type).from_hash hash
end

#key?(object_id) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/scv/object_store.rb', line 87

def key?(object_id)
  not find_object(object_id).nil?
end

#remove(object_id) ⇒ Object

A method not required by VCSToolkit used to remove labels or possibly garbage collection.

Raises:

  • (KeyError)


79
80
81
82
83
84
85
# File 'lib/scv/object_store.rb', line 79

def remove(object_id)
  raise KeyError, 'The object does not exit' unless key? object_id

  location = find_object object_id

  @store.delete_file location[:path]
end

#store(object_id, object) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/scv/object_store.rb', line 48

def store(object_id, object)
  if object.named?
    object_path = get_object_path object_id, named: true
  else
    object_path = get_object_path object_id
  end

  if object.is_a? VCSToolkit::Objects::Blob
    @store.store(get_blob_path(object_id), object.content)
  else
    @store.store(object_path, JSON.generate(object.to_hash))
  end
end