Changeset 3456


Ignore:
Timestamp:
08/22/13 12:26:32 (11 years ago)
Author:
mwindhouwer
Message:

mod-SLOOT*

  • added support for caching to disk to DBX
  • use <dbx:cache id="/relative/path/to/cache/entry"/> in a DBX to cache and reuse
  • use active:dbx-cache+entry@data:text/plain,/relative/path/to/cache/entry to clear
Location:
cats/shared/mod-SLOOT/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • cats/shared/mod-SLOOT/trunk/module.xml

    r2029 r3456  
    5151        </rewrite>
    5252        <rewrite>
     53            <match>active:sloot.dbx-cache\+(.*)</match>
     54            <to>active:sloot.accessor+operand@ffcpl:/tools/dbx-cache.acc+$1</to>
     55        </rewrite>
     56        <rewrite>
    5357            <match>active:sloot.dbx(.*)\+operator@([^+]*)(.*)</match>
    5458            <to>active:javascript+operator@ffcpl:/tools/dbx.js+_operator@$2$1$3</to>
  • cats/shared/mod-SLOOT/trunk/nk/src/DBXEngine.java

    r2029 r3456  
    9090                engine.setStrict(strict);
    9191                       
     92                // determine cache directory
     93                String cache = "file:///tmp/sloot-dbx-cache";
     94                if (nkreq.argumentExists("cache")) {
     95                        String val = "" + cache;
     96                        String paramURI = nkreq.getArgument("cache");
     97                        if (paramURI.startsWith("data:text/plain,"))
     98                                val = paramURI.substring(("data:text/plain,").length(),paramURI.length());
     99                        else {
     100                                IXAspect param = (IXAspect)nk.sourceAspect(paramURI, IXAspect.class);
     101                                val = param.getXDA().getText(".",true);
     102                        }
     103                        cache = (val.equals("")?cache:val);
     104                }
     105                engine.setCacheDirectory(cache);
     106
    92107                // get the DBX guide document
    93108                IXAspect optxa = (IXAspect)nk.sourceAspect("this:param:operator", IXAspect.class);
  • cats/shared/mod-SLOOT/trunk/nk/src/dbx/Engine.java

    r2029 r3456  
    33import com.ten60.netkernel.urii.*;
    44import com.ten60.netkernel.urii.aspect.IAspectBoolean;
     5import com.ten60.netkernel.urii.aspect.IAspectReadableBinaryStream;
    56import org.ten60.netkernel.xml.xahelper.*;
    67import org.ten60.netkernel.xml.xda.*;
     
    4748        protected boolean strict = true;
    4849       
     50        // cache directory
     51        protected String cacheDir = "file:///tmp/sloot-dbx-cache";
     52       
    4953        // profile hashmap
    5054        protected SortedMap profiles = new TreeMap();
     
    114118        }
    115119
     120        public void setCacheDirectory(String cacheDir) {
     121                this.cacheDir = cacheDir;
     122        }
     123       
    116124        public IURAspect run() throws Exception {
    117125                long start = System.currentTimeMillis();
     
    276284                                        // silently move on
    277285                                        evalChildren();
     286                } else if (action.equals("cache")) {
     287                    cache();
    278288                                } else
    279289                                        fatal(guide,"unknown DBX element!");
     
    734744                       
    735745                        evalChildren(template);
     746                }
     747               
     748                /** cache the result */
     749                protected void cache() throws Exception {
     750                        // check if there is a when attribute
     751                        String when = guide.getAttributeValue(new QName("when"));
     752                        if ((when!=null) && (!when.trim().equals(""))) {
     753                                if (!xpath2boolean(sxdoc.wrap(result),when.trim())) {
     754                                        debug("dbx:cache@when eval["+when.trim()+"][false]");
     755                                        // no caching
     756                                evalChildren();
     757                                return;
     758                                } else {
     759                        debug("dbx:cache@when eval["+when.trim()+"][true]");
     760                                }
     761                        }
     762                // get cache id
     763                String id = guide.getAttributeValue(new QName("id"));
     764                if (id!=null)
     765                        id = avt(id);
     766                if ((id == null) || id.trim().equals(""))
     767                        fatal(guide,"<dbx:cache/> without a id attribute, or it's empty!");
     768                // lookup the id in the cache
     769                String cacheFile = cacheDir+id;
     770                try {
     771                        InputSource is = new InputSource(((IAspectReadableBinaryStream)nk.sourceAspect(cacheFile,IAspectReadableBinaryStream.class)).getInputStream());
     772                        XdmNode node = sxdoc.build(new SAXSource(is));
     773                        debug("dbx:cache@file["+cacheFile+"][loaded]");
     774                deepCopy(result,node);
     775                node=null;
     776                is=null;
     777            } catch(Exception e) {
     778                // TODO: check if its a not found exception, if not throw the exception
     779                        debug("dbx:cache@file["+cacheFile+"][failed]["+e+"]");
     780                        int children = result.getChildNodes().getLength();
     781                        evalChildren();
     782                        if (result.getChildNodes().getLength() == (children + 1)) {
     783                        // now cache the result
     784                        // DOMAspect dom = new DOMAspect(result.getLastChild());
     785                        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
     786                        Node root = doc.importNode(result.getLastChild(),true);
     787                        doc.appendChild(root);
     788                        DOMAspect dom = new DOMAspect(doc);
     789                        nk.sinkAspect(cacheFile,dom);
     790                        debug("dbx:cache@file["+cacheFile+"][saved]");
     791                } else if (result.getChildNodes().getLength() > (children + 1)) {
     792                                warn(guide,"dbx:cache: more then one new child node has been created, this can't be cached!");
     793                        }// else nothing to cache
     794                        }
    736795                }
    737796               
Note: See TracChangeset for help on using the changeset viewer.