<commit_before>class Space.eventSourcing.Snapshotter extends Space.Object

  _collection: null
  _versionFrequency: 0

  constructor: (config) ->
    @_collection = config.collection
    @_versionFrequency = config.versionFrequency

  makeSnapshotOf: (aggregate) ->
    id = aggregate.getId()
    currentVersion = aggregate.getVersion()
    data = @_collection.findOne _id: id

    if data? and data.snapshot.version <= currentVersion - @_versionFrequency
      # Update existing snapshot of this aggregate
      @_collection.update {_id: id}, $set: snapshot: aggregate.getSnapshot()
    else if !data?
      # Insert first snapshot of this aggregate
      @_collection.insert _id: id, snapshot: aggregate.getSnapshot()

  getSnapshotOf: (Type, id) ->
    return Type.createFromSnapshot @_collection.findOne(_id: id).snapshot<commit_msg>adds better error message for snapshotter<commit_after>class Space.eventSourcing.Snapshotter extends Space.Object

  ERRORS:
    noSnapshotFound: (id) -> new Error "No snapshot was found for aggregate <#{id}>"

  _collection: null
  _versionFrequency: 0

  constructor: (config) ->
    @_collection = config.collection
    @_versionFrequency = config.versionFrequency

  makeSnapshotOf: (aggregate) ->
    id = aggregate.getId().toString()
    currentVersion = aggregate.getVersion()
    data = @_collection.findOne _id: id

    if data? and data.snapshot.version <= currentVersion - @_versionFrequency
      # Update existing snapshot of this aggregate
      @_collection.update {_id: id}, $set: snapshot: aggregate.getSnapshot()
    else if !data?
      # Insert first snapshot of this aggregate
      @_collection.insert _id: id, snapshot: aggregate.getSnapshot()

  getSnapshotOf: (Type, id) ->
    id = id.toString()
    record = @_collection.findOne(_id: id)
    if record?
      return Type.createFromSnapshot record.snapshot
    else
      throw @ERRORS.noSnapshotFound(id)
{'fraction_non_alphanumeric': 0.09697601668404589, 'fraction_numerical': 0.0010427528675703858, 'mean_word_length': 3.8337531486146097, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '355656', 'n_tokens_mistral': 545, 'n_tokens_neox': 526, 'n_words': 151}