Skip to content

Commit

Permalink
Compression prototype: transparent encoding/decoding into base64 impl…
Browse files Browse the repository at this point in the history
…emented in xmpdatasource
  • Loading branch information
Rostislav Vasilikhin committed Dec 1, 2015
1 parent 591cd97 commit 81e8d7c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
3 changes: 2 additions & 1 deletion modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ endif()
set(XMP_PUBLIC_DIR "${VMF_3PTY_DIR}/xmp/public/include")
set(LIBXML2_PUBLIC_DIR "${VMF_3PTY_DIR}/libxml2/src/include")
set(LIBJSON_PUBLIC_DIR "${VMF_3PTY_DIR}/libjson/src" "${VMF_3PTY_DIR}/libjson/src/_internal/Source")
set(LIBBASE64_PUBLIC_DIR "${VMF_3PTY_DIR}/libjson/src/_internal/Dependencies/libbase64++")

if(CODE_COVERAGE)
message(STATUS "Enabling code coverage..")
Expand Down Expand Up @@ -63,7 +64,7 @@ file(GLOB VMDATASOURCE_TESTS "${VMDATASOURCE_TESTS_DIR}/*.hpp" "${VMDATASOURCE_T
source_group(vmdatasource\\src FILES ${VMDATASOURCE_SOURCES})
source_group(vmdatasource\\include\\vmf FILES ${VMDATASOURCE_HEADERS})

include_directories(${CMAKE_BINARY_DIR} ${VMFCORE_PUBLIC_DIR} ${VMFCORE_DETAILS_DIR} ${VMDATASOURCE_PUBLIC_DIR} ${XMP_PUBLIC_DIR} ${LIBXML2_PUBLIC_DIR} ${LIBJSON_PUBLIC_DIR})
include_directories(${CMAKE_BINARY_DIR} ${VMFCORE_PUBLIC_DIR} ${VMFCORE_DETAILS_DIR} ${VMDATASOURCE_PUBLIC_DIR} ${XMP_PUBLIC_DIR} ${LIBXML2_PUBLIC_DIR} ${LIBJSON_PUBLIC_DIR} ${LIBBASE64_PUBLIC_DIR})

add_library(${VMF_LIBRARY_NAME} ${VMDATASOURCE_HEADERS} ${VMDATASOURCE_SOURCES} ${VMFCORE_HEADERS} ${VMFCORE_SOURCES} ${VMFCORE_DETAILS} ${XMP_SOURCES} ${LIBXML2_SOURCES} ${LIBJSON_SOURCES})
target_compile_definitions(${VMF_LIBRARY_NAME} PRIVATE $<$<CONFIG:Debug>:JSON_DEBUG> PRIVATE $<$<CONFIG:Release>:NDEBUG>)
Expand Down
66 changes: 62 additions & 4 deletions modules/vmdatasource/src/xmpdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#include "xmpschemasource.hpp"
#include "xmpmetadatasource.hpp"

#include <XMP.incl_cpp>
#include <XMP.hpp>

#include "libbase64++.h"

#define VMF_GLOBAL_NEXT_ID "next-id"
#define VMF_GLOBAL_CHECKSUM "media-checksum"

Expand Down Expand Up @@ -88,6 +93,49 @@ XMPDataSource::XMPDataSource()

}

static const string compressPropName = "compressed";

static void loadXMPstructs(SXMPFiles& xmpFile, std::shared_ptr<SXMPMeta>& xmp)
{
std::shared_ptr<SXMPMeta> compressedXMP = make_shared<SXMPMeta>();
//xmpFile.GetXMP(NULL, &buffer, NULL);
xmpFile.GetXMP(compressedXMP.get());
if(compressedXMP->DoesPropertyExist(VMF_NS, compressPropName.c_str()))
{
string buffer;
compressedXMP->GetProperty(VMF_NS, compressPropName.c_str(), &buffer, NULL);
string decoded = libbase64::decode<string, string::value_type, string::value_type, true>(buffer);
buffer = decoded;
xmp->ParseFromBuffer(buffer.c_str(), buffer.size(), 0);
}
else
{
xmp = compressedXMP;
}
}


static void saveXMPstructs(SXMPFiles& xmpFile, std::shared_ptr<SXMPMeta>& xmp)
{
const bool toCompress = true;
std::shared_ptr<SXMPMeta> compressedXMP = make_shared<SXMPMeta>();
if(toCompress)
{
string buffer;
xmp->SerializeToBuffer(&buffer, 0, 0, NULL);
string encoded = libbase64::encode<string, string::value_type,
string::value_type, true>((const unsigned char*)buffer.c_str(), buffer.size());
buffer = encoded;
compressedXMP->SetProperty(VMF_NS, compressPropName.c_str(), buffer);
}
else
{
compressedXMP = xmp;
}
//xmpFile.PutXMP(buffer.c_str(), buffer.size());
xmpFile.PutXMP(*compressedXMP);
}


void XMPDataSource::openFile(const MetaString& fileName, MetadataStream::OpenMode mode)
{
Expand Down Expand Up @@ -116,7 +164,10 @@ void XMPDataSource::openFile(const MetaString& fileName, MetadataStream::OpenMod
VMF_EXCEPTION(DataStorageException, "Could not open XMP file.");
}
}
xmpFile.GetXMP(xmp.get());

//xmpFile.GetXMP(xmp.get());
loadXMPstructs(xmpFile, xmp);

schemaSource = make_shared<XMPSchemaSource>(xmp);
metadataSource = make_shared<XMPMetadataSource>(xmp);
}
Expand Down Expand Up @@ -237,7 +288,10 @@ void XMPDataSource::remove(const vector<IdType>& ids)
try
{
metadataSource->remove(ids);
xmpFile.PutXMP(*xmp);

//xmpFile.PutXMP(*xmp);
saveXMPstructs(xmpFile, xmp);

closeFile();
openFile(this->metaFileName, this->openMode);
}
Expand Down Expand Up @@ -308,7 +362,9 @@ void XMPDataSource::save(const IdType &id)

void XMPDataSource::pushChanges()
{
xmpFile.PutXMP(*xmp);
//xmpFile.PutXMP(*xmp);
saveXMPstructs(xmpFile, xmp);

closeFile();
openFile(this->metaFileName, this->openMode);
}
Expand Down Expand Up @@ -339,7 +395,9 @@ std::string XMPDataSource::computeChecksum(long long& XMPPacketSize, long long&
{
try
{
xmpFile.GetXMP(xmp.get());
//xmpFile.GetXMP(xmp.get());
loadXMPstructs(xmpFile, xmp);

MetaString checksum;
xmpFile.ComputeChecksum(&checksum, &XMPPacketSize, &XMPPacketOffset);
return checksum;
Expand Down

0 comments on commit 81e8d7c

Please sign in to comment.