Changeset - 11affc6d6045
[Not reviewed]
default
0 1 0
Drew Perttula - 7 years ago 2018-06-07 08:11:17
drewp@bigasterisk.com
more graph methods. some cleanup of the code that runs only required handlers, but it's not turned on yet
Ignore-this: eb8e148737af4779a4735384ef6426ce
1 file changed with 52 insertions and 18 deletions:
0 comments (0 inline, 0 general)
light9/web/graph.coffee
Show inline comments
 
@@ -17,12 +17,34 @@ RDF = 'http://www.w3.org/1999/02/22-rdf-
 
patchSizeSummary = (patch) ->
 
  '-' + patch.delQuads.length + ' +' + patch.addQuads.length
 

	
 
# (sloppily shared to rdfdbclient.coffee too)
 
window.patchSizeSummary = patchSizeSummary
 

	
 
patchContainsPreds = (patch, preds) ->
 
  if patch._allPreds == undefined
 
    patch._allPreds = new Set()
 
    for qq in [patch.addQuads, patch.delQuads]
 
      for q in qq
 
        patch._allPreds.add(q.predicate.value)
 

	
 
  for p in preds
 
    if patch._allPreds.has(p.value)
 
      return true
 
  return false
 

	
 
allPatchSubjs = (patch) ->   # returns subjs as Set of strings
 
  out = new Set()
 
  if patch._allSubjs == undefined
 
    patch._allSubjs = new Set()
 
    for qq in [patch.addQuads, patch.delQuads]
 
      for q in qq
 
        patch._allSubjs.add(q.subject.value)
 

	
 
  return patch._allSubjs
 

	
 
class Handler
 
  # a function and the quad patterns it cared about
 
  constructor: (@func, @label) ->
 
    @patterns = [] # s,p,o,g quads that should trigger the next run
 
    @innerHandlers = [] # Handlers requested while this one was running
 
  
 
@@ -71,41 +93,31 @@ class AutoDependencies
 
      for i in [0...depth]
 
        indent += '  '
 
      log("#{indent} \"#{h.label}\" #{h.patterns.length} pats")
 
      for c in h.innerHandlers
 
        prn(c, depth + 1)
 
    prn(@handlers, 0)
 

	
 
  _allPatchSubjs: (patch) ->
 

	
 
    allPatchSubjs = []
 
    for stmt in patch.addQuads
 
      allPatchSubjs.push(stmt.subject)
 
    for stmt in patch.delQuads
 
      allPatchSubjs.push(stmt.subject)
 
    allPatchSubjs = _.uniq(allPatchSubjs)
 
    if _.contains(allPatchSubjs, null) or allPatchSubjs.length == 0
 
      allPatchSubjs = null
 
    log('allPatchSubjs', allPatchSubjs)
 
    
 
  _handlerIsAffected: (child, allPatchSubjs) ->
 
  _handlerIsAffected: (child, patchSubjs) ->
 
    if allPatchSubjs == null
 
      return true
 
    if not child.patterns.length
 
      return false
 
      
 
    for stmt in child.patterns
 
      if _.contains(allPatchSubjs, stmt[0])
 
      if stmt[0] == null # wildcard on subject
 
        return true
 
      if patchSubjs.has(stmt[0].value)
 
        return true
 

	
 
    return false
 
            
 
  graphChanged: (patch) ->
 
    # SyncedGraph is telling us this patch just got applied to the graph.
 

	
 
    #allPatchSubjs = @_allPatchSubjs(patch)
 
    subjs = allPatchSubjs(patch)
 
    
 
    rerunInners = (cur) =>
 
      toRun = cur.innerHandlers.slice()
 
      for child in toRun
 
        #match = @_handlerIsAffected(child, allPatchSubjs)
 
        #log('match', child.label, match)
 
@@ -247,12 +259,18 @@ class window.SyncedGraph
 
      delQuads: existing,
 
      addQuads: [@Quad(s, p, newObject, g)]
 
    }
 

	
 
  patchObject: (s, p, newObject, g) ->
 
    @applyAndSendPatch(@getObjectPatch(s, p, newObject, g))
 

	
 
  clearObjects: (s, p, g) ->
 
    @applyAndSendPatch({
 
      delQuads: @graph.getQuads(s, p, null, g),
 
      addQuads: []
 
    })
 
  
 
  runHandler: (func, label) ->
 
    # runs your func once, tracking graph calls. if a future patch
 
    # matches what you queried, we runHandler your func again (and
 
    # forget your queries from the first time).
 

	
 
@@ -352,17 +370,24 @@ class window.SyncedGraph
 
    return @graph.getQuads(s, p, o).length > 0
 

	
 
  nextNumberedResources: (base, howMany) ->
 
    # base is NamedNode or string
 
    base = base.id if base.id
 
    results = []
 
    # we could cache [base,lastSerial]
 
    for serial in [0..1000]
 

	
 
    # @contains is really slow.
 
    @_nextNumber = new Map() unless @_nextNumber?
 
    start = @_nextNumber.get(base)
 
    if start == undefined
 
      start = 0
 
      
 
    for serial in [start..1000]
 
      uri = @Uri("#{base}#{serial}")
 
      if not @contains(uri, null, null)
 
        results.push(uri)
 
        @_nextNumber.set(base, serial + 1)
 
        if results.length >= howMany
 
          return results
 
    throw new Error("can't make sequential uri with base #{base}")
 

	
 
  nextNumberedResource: (base) ->
 
    @nextNumberedResources(base, 1)[0]
 
@@ -379,7 +404,16 @@ class window.SyncedGraph
 
      parts = u.value.split(/([0-9]+)/)
 
      expanded = parts.map (p) ->
 
        f = parseInt(p)
 
        return p if isNaN(f)
 
        return p.padStart(8, '0')
 
      return expanded.join('')
 
      
 
\ No newline at end of file
 

	
 
  # temporary optimization since autodeps calls too often
 
  @patchContainsPreds: (patch, preds) ->
 
    patchContainsPreds(patch, preds)
 

	
 
  prettyLiteral: (x) ->
 
    if typeof(x) == 'number'
 
      @LiteralRoundedFloat(x)
 
    else
 
      @Literal(x)
0 comments (0 inline, 0 general)