/*************************************************************************** * Copyright (C) 2001-2008 by Jozef Starosczyk * * ixen@copyhandler.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License * * (version 2) as published by the Free Software Foundation; * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ // File was originally based on FileInfo.cpp by Antonio Tejada Lacaci. // Almost everything has changed since then. #include "stdafx.h" #include "TFileInfoArray.h" #include "TFileInfo.h" #include #include #include "../libchcore/TCoreException.h" #include "../libserializer/ISerializerContainer.h" using namespace chcore; using namespace serializer; namespace chengine { /////////////////////////////////////////////////////////////////////// // Array TFileInfoArray::TFileInfoArray() : m_bComplete(false), m_oidLastObjectID(0) { } TFileInfoArray::~TFileInfoArray() { } void TFileInfoArray::Add(const TFileInfoPtr& spFileInfo) { boost::unique_lock lock(m_lock); spFileInfo->SetObjectID(++m_oidLastObjectID); m_vFiles.push_back(spFileInfo); } file_count_t TFileInfoArray::GetCount() const { boost::shared_lock lock(m_lock); return boost::numeric_cast(m_vFiles.size()); } TFileInfoPtr TFileInfoArray::GetAt(file_count_t fcIndex) const { boost::shared_lock lock(m_lock); if (fcIndex >= m_vFiles.size()) throw TCoreException(eErr_InvalidArgument, L"fcIndex", LOCATION); return m_vFiles.at(boost::numeric_cast(fcIndex)); } void TFileInfoArray::Clear() { boost::unique_lock lock(m_lock); m_bComplete = false; for(const TFileInfoPtr& spFileInfo : m_vFiles) { m_setRemovedObjects.Add(spFileInfo->GetObjectID()); } m_vFiles.clear(); } unsigned long long TFileInfoArray::CalculateTotalSize() const { unsigned long long ullSize = 0; boost::shared_lock lock(m_lock); for(const TFileInfoPtr& spFileInfo : m_vFiles) { ullSize += spFileInfo->GetLength64(); } return ullSize; } void TFileInfoArray::SetComplete(bool bComplete) { boost::unique_lock lock(m_lock); m_bComplete = bComplete; } bool TFileInfoArray::IsComplete() const { boost::shared_lock lock(m_lock); return m_bComplete; } unsigned long long TFileInfoArray::CalculatePartialSize(file_count_t fcCount) { unsigned long long ullSize = 0; boost::shared_lock lock(m_lock); if (fcCount > m_vFiles.size()) throw TCoreException(eErr_InvalidArgument, L"fcIndex", LOCATION); for (std::vector::iterator iter = m_vFiles.begin(); iter != m_vFiles.begin() + boost::numeric_cast(fcCount); ++iter) { ullSize += (*iter)->GetLength64(); } return ullSize; } void TFileInfoArray::Store(const ISerializerContainerPtr& spContainer) const { boost::shared_lock lock(m_lock); InitColumns(spContainer); // store only if there is a complete collection of items inside // (this container is used in the directory scanning process. There is no // point storing only partially scanned data in the serializer as we // can't use this data after loading serialized data (dir scan will have // to scan again)). if (m_bComplete) { spContainer->DeleteRows(m_setRemovedObjects); m_setRemovedObjects.Clear(); for(const TFileInfoPtr& spFileInfo : m_vFiles) { spFileInfo->Store(spContainer); } } } void TFileInfoArray::Load(const ISerializerContainerPtr& spContainer, const TBasePathDataContainerPtr& spBasePaths) { InitColumns(spContainer); std::vector vEntries; ISerializerRowReaderPtr spRowReader = spContainer->GetRowReader(); while (spRowReader->Next()) { TFileInfoPtr spFileInfo(new TFileInfo); spFileInfo->Load(spRowReader, spBasePaths); vEntries.push_back(spFileInfo); m_oidLastObjectID = std::max(m_oidLastObjectID, spFileInfo->GetObjectID()); } boost::unique_lock lock(m_lock); m_vFiles = std::move(vEntries); m_bComplete = !m_vFiles.empty(); // we're marking empty/non-empty based on scanned file count; this is due to the // fact, that no scanned files are stored in DB unless scanning is complete; } void TFileInfoArray::InitColumns(const ISerializerContainerPtr& spContainer) const { IColumnsDefinition& rColumns = spContainer->GetColumnsDefinition(); if (rColumns.IsEmpty()) TFileInfo::InitColumns(rColumns); } }