[Liblas-commits] libpc: very simple cache filter

liblas-commits at liblas.org liblas-commits at liblas.org
Mon Mar 7 17:44:56 EST 2011


details:   http://hg.liblas.orglibpc/rev/dacbbd3a3288
changeset: 183:dacbbd3a3288
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Mon Mar 07 14:44:33 2011 -0800
description:
very simple cache filter

diffstat:

 include/libpc/CacheFilter.hpp |   79 ++++++++++++++++++++++++
 src/CMakeLists.txt            |    2 +
 src/CacheFilter.cpp           |  135 ++++++++++++++++++++++++++++++++++++++++++
 test/unit/CMakeLists.txt      |    1 +
 test/unit/CacheFilterTest.cpp |   98 ++++++++++++++++++++++++++++++
 5 files changed, 315 insertions(+), 0 deletions(-)

diffs (truncated from 354 to 300 lines):

diff -r 52486659a751 -r dacbbd3a3288 include/libpc/CacheFilter.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/libpc/CacheFilter.hpp	Mon Mar 07 14:44:33 2011 -0800
@@ -0,0 +1,79 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#ifndef INCLUDED_CACHEFILTER_HPP
+#define INCLUDED_CACHEFILTER_HPP
+
+#include "libpc/Filter.hpp"
+
+namespace libpc
+{
+
+// this is just a very simple MRU filter -- future versions will be smarter
+class LIBPC_DLL CacheFilter : public Filter
+{
+public:
+    CacheFilter(Stage& prevStage);
+    ~CacheFilter();
+
+    const std::string& getName() const;
+
+    // number of points requested from this filter via read()
+    boost::uint64_t getNumPointsRequested() const;
+
+    // num points this filter read from the previous stage
+    boost::uint64_t getNumPointsRead() const;
+
+    // override
+    boost::uint64_t getCurrentPointIndex() const;
+
+    // override
+    void seekToPoint(boost::uint64_t index);
+
+private:
+    boost::uint32_t readBuffer(PointData&);
+
+    boost::uint64_t m_currentPointIndex;
+    boost::uint64_t m_storedPointIndex;
+    PointData* m_storedPointData;
+    boost::uint64_t m_numPointsRequested;
+    boost::uint64_t m_numPointsRead;
+
+    CacheFilter& operator=(const CacheFilter&); // not implemented
+    CacheFilter(const CacheFilter&); // not implemented
+};
+
+} // namespace libpc
+
+#endif
diff -r 52486659a751 -r dacbbd3a3288 src/CMakeLists.txt
--- a/src/CMakeLists.txt	Mon Mar 07 14:37:14 2011 -0600
+++ b/src/CMakeLists.txt	Mon Mar 07 14:44:33 2011 -0800
@@ -24,6 +24,7 @@
   ${LIBPC_HEADERS_DIR}/export.hpp
   ${LIBPC_HEADERS_DIR}/libpc_config.hpp
   ${LIBPC_HEADERS_DIR}/Bounds.hpp
+  ${LIBPC_HEADERS_DIR}/CacheFilter.hpp
   ${LIBPC_HEADERS_DIR}/Color.hpp
   ${LIBPC_HEADERS_DIR}/ColorFilter.hpp
   ${LIBPC_HEADERS_DIR}/CropFilter.hpp
@@ -63,6 +64,7 @@
     ${LIBPC_SOURCES}
   Bounds.cpp
   chipper.cpp
+  CacheFilter.cpp
   Color.cpp
   ColorFilter.cpp
   CropFilter.cpp
diff -r 52486659a751 -r dacbbd3a3288 src/CacheFilter.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CacheFilter.cpp	Mon Mar 07 14:44:33 2011 -0800
@@ -0,0 +1,135 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#include "libpc/CacheFilter.hpp"
+
+namespace libpc
+{
+
+
+CacheFilter::CacheFilter(Stage& prevStage)
+    : Filter(prevStage)
+    , m_currentPointIndex(0)
+    , m_storedPointIndex(0)
+    , m_storedPointData(NULL)
+    , m_numPointsRequested(0)
+    , m_numPointsRead(0)
+{
+    return;
+}
+
+
+CacheFilter::~CacheFilter()
+{
+    delete m_storedPointData;
+}
+
+
+const std::string& CacheFilter::getName() const
+{
+    static std::string name("Cache Filter");
+    return name;
+}
+
+
+boost::uint64_t CacheFilter::getCurrentPointIndex() const
+{
+    return m_currentPointIndex;
+}
+
+
+void CacheFilter::seekToPoint(boost::uint64_t index)
+{
+    m_currentPointIndex = index;
+    m_prevStage.seekToPoint(index);
+}
+
+
+boost::uint64_t CacheFilter::getNumPointsRequested() const
+{
+    return m_numPointsRequested;
+}
+
+
+boost::uint64_t CacheFilter::getNumPointsRead() const
+{
+    return m_numPointsRead;
+}
+
+
+boost::uint32_t CacheFilter::readBuffer(PointData& data)
+{
+    m_numPointsRequested += data.getNumPoints();
+
+    boost::uint64_t pointToRead = getCurrentPointIndex();
+
+    // do we meet the cache-checking criteria?
+    if (data.getNumPoints() == 1 && m_storedPointData != NULL)
+    {
+        // do we have the point we want in the cache?
+        if (pointToRead >= m_storedPointIndex && 
+            pointToRead < m_storedPointIndex + m_storedPointData->getNumPoints())
+        {
+            // the cache has the data we want
+
+            boost::uint32_t index = (boost::uint32_t)(pointToRead - m_storedPointIndex);
+
+            data.copyPointFast(0, index, *m_storedPointData);
+
+            m_currentPointIndex += 1;
+
+            return 1;
+        }
+    }
+
+    // not cached, so read a full block and replace cache with it
+
+    delete m_storedPointData;
+
+    int chunkSize = 1024;
+    m_storedPointData = new PointData(data.getSchemaLayout(), chunkSize);
+
+    m_prevStage.read(*m_storedPointData);
+    m_numPointsRead += chunkSize;
+
+    data.copyPointFast(0, 0, *m_storedPointData);
+
+    m_storedPointIndex = getCurrentPointIndex();
+
+    m_currentPointIndex += 1;
+
+    return 1;
+}
+
+}
diff -r 52486659a751 -r dacbbd3a3288 test/unit/CMakeLists.txt
--- a/test/unit/CMakeLists.txt	Mon Mar 07 14:37:14 2011 -0600
+++ b/test/unit/CMakeLists.txt	Mon Mar 07 14:44:33 2011 -0800
@@ -9,6 +9,7 @@
 
 SET(LIBPC_UNIT_TEST_SRC
 	BoundsTest.cpp
+    CacheFilterTest.cpp
 	ChipperTest.cpp
 	ColorTest.cpp
 	ConfigTest.cpp
diff -r 52486659a751 -r dacbbd3a3288 test/unit/CacheFilterTest.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unit/CacheFilterTest.cpp	Mon Mar 07 14:44:33 2011 -0800
@@ -0,0 +1,98 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#include <boost/test/unit_test.hpp>
+#include <boost/cstdint.hpp>
+
+#include "libpc/FauxReader.hpp"
+#include "libpc/CacheFilter.hpp"
+
+using namespace libpc;
+
+BOOST_AUTO_TEST_SUITE(CacheFilterTest)
+


More information about the Liblas-commits mailing list