<commit_before>redis = require 'redis'
Backbone = require 'backbone'
{VIE} = require "#{__dirname}/../extension/palsu/static/deps/vie-2.0.0.js"

isEmpty = (object) ->
    for key of object
        return false
    return true

redisClient = redis.createClient()

Backbone.sync = (method, model, options) ->
    toUUID = () ->
        S4 = () -> ((1 + Math.random()) * 0x10000|0).toString(16).substring 1
        "#{S4()}#{S4()}-#{S4()}-#{S4()}-#{S4()}-#{S4()}#{S4()}#{S4()}"

    if method is 'update'
        console.log "Update entity: ", model.id
        if model.id.substr(0, 7) is "_:bnode"
            # Generate UUID as the URI of the object
            model.id = "urn:uuid:#{toUUID()}"
            console.log "Anonymous entity, saving with URI #{model.id}"
        
        console.log "backbone update: ", model.toJSONLD()
        for predicate, object of model.toJSONLD()
            if predicate is "@"
                continue
            if VIE.RDFa._isReference(object)
                if typeof object is "string"
                    object = [object]
                for reference in object
                    reference = VIE.RDFa._fromReference reference
                    console.log "Adding reference #{predicate}-#{reference} for #{model.id}"
                    redisClient.sadd "#{predicate}-#{reference}", model.id

            redisClient.hset model.id, predicate, JSON.stringify object

    if method is 'read'
        if model instanceof Backbone.Collection
            if model.predicate and model.object
                console.log "Retrieving #{model.predicate} connected to #{model.object}"
                return redisClient.smembers "#{model.predicate}-#{model.object}", (err, subjects) ->
                    if err
                        console.log err
                        return options.error err
                    else if subjects
                        if subjects.length is 0
                            return options.error "Not found"

                        instances = []
                        for subject in subjects
                            itemInstance = VIE.EntityManager.getByJSONLD
                                "@": subject
                            itemInstance.fetch
                                success: (item) ->
                                    instances.push item
                                    if instances.length >= subjects.length
                                        options.success instances
            else
                throw "When seeking Collections, you must provide predicate and object"

        else if model.id
            console.log "Retrieving entity #{model.id}"
            redisClient.hgetall model.id, (err, item) ->
                if err
                    console.log err
                    options.error err
                else if item
                    if isEmpty item
                        return options.error "Not found"
                    jsonld =
                        "@": model.id
                    for predicate, object of item
                        jsonld[predicate] = JSON.parse object
                    options.success VIE.EntityManager.getByJSONLD jsonld
        else
            console.log model
            throw "Unknown entity, please provide ID"
<commit_msg>Compatibility with latest Backbone and VIE<commit_after>redis = require 'redis'
Backbone = require 'backbone'

isEmpty = (object) ->
  for key of object
    return false
  return true

toUUID = ->
  S4 = -> ((1 + Math.random()) * 0x10000|0).toString(16).substring 1
  "#{S4()}#{S4()}-#{S4()}-#{S4()}-#{S4()}-#{S4()}#{S4()}#{S4()}"

exports.createClient = (vie, config) ->
  redisClient = redis.createClient()

  Backbone.sync = (method, model, options) ->
    method = 'update' if method is 'create'
    if method is 'update'
      console.log "Update entity: ", model.getSubjectUri()
      if model.isNew()
        # Generate UUID as the URI of the object
        model['@subject'] = model.id = "urn:uuid:#{toUUID()}"
        console.log "Anonymous entity, saving with URI #{model.getSubjectUri()}"
        
      console.log "backbone update: ", model.toJSONLD()
      for predicate, object of model.toJSONLD()
        continue if predicate is '@subject'
        predicate = model.fromReference predicate
        if model.isReference object
          if typeof object is 'string'
            object = [object]
          for reference in object
            reference = model.fromReference reference
            console.log "Adding reference #{predicate}-#{reference} for #{model.getSubjectUri()}"
            redisClient.sadd "#{predicate}-#{reference}", model.getSubjectUri()

        redisClient.hset model.getSubjectUri(), predicate, JSON.stringify object

    if method is 'read'
      if model instanceof Backbone.Collection
        model.bind 'add', (entity, col) ->
          console.log "Adding #{entity.getSubjectUri()} to collection"
        if model.predicate and model.object
          console.log "Retrieving #{model.predicate} connected to #{model.object}"
          return redisClient.smembers "#{model.predicate}-#{model.object}", (err, subjects) ->
            return options.error err if err
            subjects = subjects ? []
            return options.error 'Not found' if subjects.length is 0
            instances = []
            for subject in subjects
              itemInstance = vie.entities.addOrUpdate
                '@subject': subject
              itemInstance.fetch
                success: (item) ->
                  instances.push item
                  if instances.length >= subjects.length
                    options.success instances
                error: (error) ->
                  console.log "ERR", err
        else
          throw "When seeking Collections, you must provide predicate and object"

      throw "Unknown entity, please provide ID" unless model.getSubjectUri()

      console.log "Retrieving entity #{model.getSubjectUri()}"
      redisClient.hgetall model.getSubjectUri(), (err, item) ->
        return options.error err if err
        item = item ? {}
        return options.error "Not found" if isEmpty item

        jsonld =
          '@subject': model.getSubjectUri()
        for predicate, object of item
          jsonld[predicate] = JSON.parse object
        options.success vie.entities.addOrUpdate jsonld,
          overrideAttributes: true

{'fraction_non_alphanumeric': 0.1037163738143368, 'fraction_numerical': 0.007308350178821334, 'mean_word_length': 1.724269377382465, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '193885', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1629, 'n_words': 471}