Changeset - 4bc24f91b6f1
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 7 years ago 2018-05-10 06:08:39
drewp@bigasterisk.com
fix floatValue cache
Ignore-this: f5e5c6ffef3f48eebd03ecd19fab99b7
2 files changed with 31 insertions and 16 deletions:
0 comments (0 inline, 0 general)
light9/web/graph.coffee
Show inline comments
 
@@ -245,57 +245,57 @@ class window.SyncedGraph
 
    @applyAndSendPatch(@getObjectPatch(s, p, newObject, g))
 
  
 
  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).
 

	
 
    # helps with memleak? not sure yet. The point was if two matching
 
    # labels get puushed on, we should run only one. So maybe
 
    # appending a serial number is backwards.
 
    @serial = 1 if not @serial
 
    @serial += 1
 
    #label = label + @serial
 
    
 
    @_autoDeps.runHandler(func, label)
 

	
 
  _singleValue: (s, p) ->
 
    @_autoDeps.askedFor(s, p, null, null)
 
    quads = @graph.getQuads(s, p)
 
    console.log('got',quads)
 
    objs = new Set(q.object for q in quads)
 
    
 
    switch objs.size
 
      when 0
 
        throw new Error("no value for "+s+" "+p)
 
        throw new Error("no value for "+s.value+" "+p.value)
 
      when 1
 
        obj = objs.values().next().value
 
        return obj
 
      else
 
        throw new Error("too many different values: " + JSON.stringify(quads))
 

	
 
  floatValue: (s, p) ->
 
    key = s + '|' + p
 
    key = s.value + '|' + p.value
 
    hit = @cachedFloatValues.get(key)
 
    return hit if hit != undefined
 
    #log('float miss', s, p)
 

	
 
    ret = parseFloat(@_singleValue(s, p).value)
 
    @cachedFloatValues.set(key, ret)
 
    return ret
 
    
 
  stringValue: (s, p) ->
 
    @_singleValue(s, p).value
 
    
 
  uriValue: (s, p) ->
 
    @_singleValue(s, p)
 

	
 
  labelOrTail: (uri) ->
 
    try
 
      ret = @stringValue(uri, @Uri('rdfs:label'))
 
    catch
 
      words = uri.value.split('/')
 
      ret = words[words.length-1]
 
    if not ret
 
      ret = uri.value
 
    return ret
 

	
light9/web/rdfdb-synced-graph_test.html
Show inline comments
 
@@ -6,47 +6,62 @@
 
    <script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
 
    <script src="/node_modules/mocha/mocha.js"></script>
 
    <script src="/node_modules/chai/chai.js"></script>
 
    <link rel="stylesheet" media="all" href="/node_modules/mocha/mocha.css">
 
    <link rel="import" href="/lib/polymer/lib/elements/dom-bind.html">
 

	
 
    <link rel="import" href="rdfdb-synced-graph.html">
 
  </head>
 
  <body>
 
    <div id="mocha"><p><a href=".">Index</a></p></div>
 
    <div id="messages"></div>
 
    <div id="fixtures">
 
      <dom-bind>
 
        <template>
 
          <rdfdb-synced-graph id="graph" test-graph="true" graph="{{graph}}"></rdfdb-synced-graph>
 
        </template>
 
      </dom-bind>
 
    </div>
 
    
 
    <script>
 
     mocha.setup('bdd');
 
     const assert = chai.assert;
 
     
 
     describe("rdfdb-synced-graph", () => {
 
         let elem;
 
         beforeEach(() => {
 
             elem = document.querySelector("#graph");
 
             window.g = elem;
 
             elem.graph.clearGraph();
 
         });
 
         it("makes a node", () => {
 
             assert.equal(elem.tagName, "RDFDB-SYNCED-GRAPH");
 
         });
 
         it("loads trig", (done) => {
 
             elem.graph.loadTrig(`
 
                       @prefix : <http://example.com/> .
 
       let elem, U;
 
       beforeEach(() => {
 
         elem = document.querySelector("#graph");
 
         window.g = elem;
 
         elem.graph.clearGraph();
 
         U = elem.graph.Uri.bind(elem.graph);
 
       });
 
       it("makes a node", () => {
 
         assert.equal(elem.tagName, "RDFDB-SYNCED-GRAPH");
 
       });
 
       it("loads trig", (done) => {
 
         elem.graph.loadTrig(`
 
                       @prefix : <http://light9.bigasterisk.com/> .
 
                       :a :b :c :d .
 
                     `, () => {
 
                         assert.equal(elem.graph.quads().length, 1);
 
                         done();
 
                       assert.equal(elem.graph.quads().length, 1);
 
                       done();
 
                     });
 
       });
 
       describe("floatValue read call", () => {
 
         it("loads two values without confusing them in a cache", (done) => {
 
           elem.graph.loadTrig(`
 
               @prefix : <http://light9.bigasterisk.com/> .
 
               :s :a 1 :g .
 
               :s :b 2 :g .
 
             `, () => {
 
               assert.equal(elem.graph.floatValue(U(":s"), U(":a")), 1);
 
               assert.equal(elem.graph.floatValue(U(":s"), U(":b")), 2);
 
               assert.equal(elem.graph.floatValue(U(":s"), U(":a")), 1);
 
               done();
 
             });
 
         });
 
       });
 
     }); 
 
     mocha.run();
 
    </script>
 
  </body>
 
</html>
0 comments (0 inline, 0 general)