Pull class BPackageInfoSet out of BRepositoryCache

This commit is contained in:
Ingo Weinhold 2011-07-16 23:11:25 +02:00
parent f96d3856e1
commit e35a99be4c
7 changed files with 326 additions and 204 deletions

View File

@ -0,0 +1,2 @@
#include <../os/package/PackageInfoSet.h>

View File

@ -0,0 +1,66 @@
/*
* Copyright 2011, Haiku, Inc.
* Distributed under the terms of the MIT License.
*/
#ifndef _PACKAGE_PACKAGE_INFO_SET_H_
#define _PACKAGE_PACKAGE_INFO_SET_H_
#include <SupportDefs.h>
namespace BPackageKit {
class BPackageInfo;
class BPackageInfoSet {
public:
class Iterator;
public:
BPackageInfoSet();
virtual ~BPackageInfoSet();
status_t Init();
status_t AddInfo(const BPackageInfo& info);
void MakeEmpty();
uint32 CountInfos() const;
Iterator GetIterator() const;
private:
struct PackageInfo;
struct PackageInfoHashDefinition;
struct PackageMap;
friend class Iterator;
private:
PackageMap* fPackageMap;
};
class BPackageInfoSet::Iterator {
public:
Iterator();
Iterator(const BPackageInfoSet* set);
bool HasNext() const;
const BPackageInfo* Next();
private:
friend class BRepositoryCache;
private:
const BPackageInfoSet* fSet;
PackageInfo* fNextInfo;
};
} // namespace BPackageKit
#endif // _PACKAGE_PACKAGE_INFO_SET_H_

View File

@ -9,18 +9,16 @@
#include <Entry.h> #include <Entry.h>
#include <String.h> #include <String.h>
#include <package/PackageInfoSet.h>
#include <package/RepositoryInfo.h> #include <package/RepositoryInfo.h>
namespace BPackageKit { namespace BPackageKit {
class BPackageInfo;
class BRepositoryCache { class BRepositoryCache {
public: public:
class Iterator; typedef BPackageInfoSet::Iterator Iterator;
public: public:
BRepositoryCache(); BRepositoryCache();
@ -38,39 +36,15 @@ public:
Iterator GetIterator() const; Iterator GetIterator() const;
private: private:
struct PackageInfo;
struct PackageInfoHashDefinition;
struct PackageMap;
struct RepositoryContentHandler; struct RepositoryContentHandler;
struct StandardErrorOutput; struct StandardErrorOutput;
friend class Iterator;
private: private:
BEntry fEntry; BEntry fEntry;
BRepositoryInfo fInfo; BRepositoryInfo fInfo;
bool fIsUserSpecific; bool fIsUserSpecific;
PackageMap* fPackageMap; BPackageInfoSet fPackages;
};
class BRepositoryCache::Iterator {
public:
Iterator();
bool HasNext() const;
const BPackageInfo* Next();
private:
Iterator(const BRepositoryCache* cache);
private:
friend class BRepositoryCache;
private:
const BRepositoryCache* fCache;
PackageInfo* fNextInfo;
}; };

View File

@ -68,6 +68,7 @@ BuildPlatformSharedLibrary libpackage_build.so
Job.cpp Job.cpp
JobQueue.cpp JobQueue.cpp
PackageInfo.cpp PackageInfo.cpp
PackageInfoSet.cpp
PackageResolvable.cpp PackageResolvable.cpp
PackageResolvableExpression.cpp PackageResolvableExpression.cpp
PackageRoster.cpp PackageRoster.cpp

View File

@ -55,6 +55,7 @@ SharedLibrary libpackage.so
Job.cpp Job.cpp
JobQueue.cpp JobQueue.cpp
PackageInfo.cpp PackageInfo.cpp
PackageInfoSet.cpp
PackageResolvable.cpp PackageResolvable.cpp
PackageResolvableExpression.cpp PackageResolvableExpression.cpp
PackageRoster.cpp PackageRoster.cpp

View File

@ -0,0 +1,241 @@
/*
* Copyright 2011, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Oliver Tappe <zooey@hirschkaefer.de>
* Ingo Weinhold <ingo_weinhold@gmx.de>
*/
#include <package/PackageInfoSet.h>
#include <new>
#include <util/OpenHashTable.h>
#include <package/PackageInfo.h>
namespace BPackageKit {
// #pragma mark - PackageInfo
struct BPackageInfoSet::PackageInfo : public BPackageInfo {
PackageInfo* hashNext;
PackageInfo* listNext;
PackageInfo(const BPackageInfo& other)
:
BPackageInfo(other),
listNext(NULL)
{
}
};
// #pragma mark - PackageInfoHashDefinition
struct BPackageInfoSet::PackageInfoHashDefinition {
typedef const char* KeyType;
typedef PackageInfo ValueType;
size_t HashKey(const char* key) const
{
return BString::HashValue(key);
}
size_t Hash(const PackageInfo* value) const
{
return value->Name().HashValue();
}
bool Compare(const char* key, const PackageInfo* value) const
{
return value->Name() == key;
}
PackageInfo*& GetLink(PackageInfo* value) const
{
return value->hashNext;
}
};
// #pragma mark - PackageMap
struct BPackageInfoSet::PackageMap
: public BOpenHashTable<PackageInfoHashDefinition> {
PackageMap()
:
fCount(0)
{
}
~PackageMap()
{
PackageInfo* info = Clear(true);
while (info != NULL) {
PackageInfo* next = info->hashNext;
delete info;
info = next;
}
}
void AddPackageInfo(PackageInfo* info)
{
if (PackageInfo* oldInfo = Lookup(info->Name())) {
info->listNext = oldInfo->listNext;
oldInfo->listNext = info;
} else
Insert(info);
fCount++;
}
uint32 CountPackageInfos() const
{
return fCount;
}
private:
uint32 fCount;
};
// #pragma mark - Iterator
BPackageInfoSet::Iterator::Iterator()
:
fSet(NULL),
fNextInfo(NULL)
{
}
BPackageInfoSet::Iterator::Iterator(const BPackageInfoSet* set)
:
fSet(set),
fNextInfo(fSet->fPackageMap->GetIterator().Next())
{
}
bool
BPackageInfoSet::Iterator::HasNext() const
{
return fNextInfo != NULL;
}
const BPackageInfo*
BPackageInfoSet::Iterator::Next()
{
BPackageInfo* result = fNextInfo;
if (fNextInfo != NULL) {
if (fNextInfo->listNext != NULL) {
// get next in list
fNextInfo = fNextInfo->listNext;
} else {
// get next in hash table
PackageMap::Iterator iterator
= fSet->fPackageMap->GetIterator(fNextInfo->Name());
iterator.Next();
fNextInfo = iterator.Next();
}
}
return result;
}
// #pragma mark - BPackageInfoSet
BPackageInfoSet::BPackageInfoSet()
:
fPackageMap(new(std::nothrow) PackageMap)
{
}
BPackageInfoSet::~BPackageInfoSet()
{
MakeEmpty();
delete fPackageMap;
}
status_t
BPackageInfoSet::Init()
{
return fPackageMap->Init();
}
status_t
BPackageInfoSet::AddInfo(const BPackageInfo& _info)
{
if (fPackageMap == NULL)
return B_NO_INIT;
PackageInfo* info = new(std::nothrow) PackageInfo(_info);
if (info == NULL)
return B_NO_MEMORY;
status_t error = info->InitCheck();
if (error != B_OK) {
delete info;
return error;
}
if (PackageInfo* oldInfo = fPackageMap->Lookup(info->Name())) {
// TODO: Check duplicates?
info->listNext = oldInfo->listNext;
oldInfo->listNext = info;
} else
fPackageMap->Insert(info);
return B_OK;
}
void
BPackageInfoSet::MakeEmpty()
{
if (fPackageMap == NULL)
return;
PackageInfo* info = fPackageMap->Clear(true);
while (info != NULL) {
PackageInfo* next = info->hashNext;
delete info;
info = next;
}
}
uint32
BPackageInfoSet::CountInfos() const
{
if (fPackageMap == NULL)
return 0;
return fPackageMap->CountPackageInfos();
}
BPackageInfoSet::Iterator
BPackageInfoSet::GetIterator() const
{
return Iterator(this);
}
} // namespace BPackageKit

View File

@ -18,13 +18,10 @@
#include <FindDirectory.h> #include <FindDirectory.h>
#include <Path.h> #include <Path.h>
#include <util/OpenHashTable.h>
#include <package/hpkg/ErrorOutput.h> #include <package/hpkg/ErrorOutput.h>
#include <package/hpkg/PackageInfoAttributeValue.h> #include <package/hpkg/PackageInfoAttributeValue.h>
#include <package/hpkg/RepositoryContentHandler.h> #include <package/hpkg/RepositoryContentHandler.h>
#include <package/hpkg/RepositoryReader.h> #include <package/hpkg/RepositoryReader.h>
#include <package/HashableString.h>
#include <package/PackageInfo.h> #include <package/PackageInfo.h>
#include <package/RepositoryInfo.h> #include <package/RepositoryInfo.h>
@ -32,107 +29,18 @@
namespace BPackageKit { namespace BPackageKit {
using BPrivate::HashableString;
using namespace BHPKG; using namespace BHPKG;
// #pragma mark - PackageInfo
struct BRepositoryCache::PackageInfo : public BPackageInfo {
PackageInfo* hashNext;
PackageInfo* listNext;
PackageInfo(const BPackageInfo& other)
:
BPackageInfo(other),
listNext(NULL)
{
}
};
// #pragma mark - PackageInfoHashDefinition
struct BRepositoryCache::PackageInfoHashDefinition {
typedef const char* KeyType;
typedef PackageInfo ValueType;
size_t HashKey(const char* key) const
{
return BString::HashValue(key);
}
size_t Hash(const PackageInfo* value) const
{
return value->Name().HashValue();
}
bool Compare(const char* key, const PackageInfo* value) const
{
return value->Name() == key;
}
PackageInfo*& GetLink(PackageInfo* value) const
{
return value->hashNext;
}
};
// #pragma mark - PackageMap
struct BRepositoryCache::PackageMap
: public BOpenHashTable<PackageInfoHashDefinition> {
PackageMap()
:
fCount(0)
{
}
~PackageMap()
{
PackageInfo* info = Clear(true);
while (info != NULL) {
PackageInfo* next = info->hashNext;
delete info;
info = next;
}
}
void AddPackageInfo(PackageInfo* info)
{
if (PackageInfo* oldInfo = Lookup(info->Name())) {
info->listNext = oldInfo->listNext;
oldInfo->listNext = info;
} else
Insert(info);
fCount++;
}
uint32 CountPackageInfos() const
{
return fCount;
}
private:
uint32 fCount;
};
// #pragma mark - RepositoryContentHandler // #pragma mark - RepositoryContentHandler
struct BRepositoryCache::RepositoryContentHandler : BRepositoryContentHandler { struct BRepositoryCache::RepositoryContentHandler : BRepositoryContentHandler {
RepositoryContentHandler(BRepositoryInfo* repositoryInfo, RepositoryContentHandler(BRepositoryInfo* repositoryInfo,
PackageMap* packageMap) BPackageInfoSet& packages)
: :
fRepositoryInfo(repositoryInfo), fRepositoryInfo(repositoryInfo),
fPackageMap(packageMap) fPackages(packages)
{ {
} }
@ -240,21 +148,9 @@ struct BRepositoryCache::RepositoryContentHandler : BRepositoryContentHandler {
if (result != B_OK) if (result != B_OK)
return result; return result;
PackageInfo* info = new(std::nothrow) PackageInfo(fPackageInfo); result = fPackages.AddInfo(fPackageInfo);
if (info == NULL) if (result != B_OK)
return B_NO_MEMORY;
result = info->InitCheck();
if (result != B_OK) {
delete info;
return result; return result;
}
if (PackageInfo* oldInfo = fPackageMap->Lookup(info->Name())) {
info->listNext = oldInfo->listNext;
oldInfo->listNext = info;
} else
fPackageMap->Insert(info);
fPackageInfo.Clear(); fPackageInfo.Clear();
break; break;
@ -281,7 +177,7 @@ struct BRepositoryCache::RepositoryContentHandler : BRepositoryContentHandler {
private: private:
BRepositoryInfo* fRepositoryInfo; BRepositoryInfo* fRepositoryInfo;
BPackageInfo fPackageInfo; BPackageInfo fPackageInfo;
PackageMap* fPackageMap; BPackageInfoSet& fPackages;
}; };
@ -296,67 +192,19 @@ class BRepositoryCache::StandardErrorOutput : public BErrorOutput {
}; };
// #pragma mark - Iterator
BRepositoryCache::Iterator::Iterator()
:
fCache(NULL),
fNextInfo(NULL)
{
}
BRepositoryCache::Iterator::Iterator(const BRepositoryCache* cache)
:
fCache(cache),
fNextInfo(fCache->fPackageMap->GetIterator().Next())
{
}
bool
BRepositoryCache::Iterator::HasNext() const
{
return fNextInfo != NULL;
}
const BPackageInfo*
BRepositoryCache::Iterator::Next()
{
BPackageInfo* result = fNextInfo;
if (fNextInfo != NULL) {
if (fNextInfo->listNext != NULL) {
// get next in list
fNextInfo = fNextInfo->listNext;
} else {
// get next in hash table
PackageMap::Iterator iterator
= fCache->fPackageMap->GetIterator(fNextInfo->Name());
iterator.Next();
fNextInfo = iterator.Next();
}
}
return result;
}
// #pragma mark - BRepositoryCache // #pragma mark - BRepositoryCache
BRepositoryCache::BRepositoryCache() BRepositoryCache::BRepositoryCache()
: :
fIsUserSpecific(false), fIsUserSpecific(false),
fPackageMap(NULL) fPackages()
{ {
} }
BRepositoryCache::~BRepositoryCache() BRepositoryCache::~BRepositoryCache()
{ {
delete fPackageMap;
} }
@ -392,19 +240,11 @@ status_t
BRepositoryCache::SetTo(const BEntry& entry) BRepositoryCache::SetTo(const BEntry& entry)
{ {
// unset // unset
if (fPackageMap != NULL) { fPackages.MakeEmpty();
delete fPackageMap;
fPackageMap = NULL;
}
fEntry.Unset(); fEntry.Unset();
// create the package map // init package info set
fPackageMap = new (std::nothrow) PackageMap; status_t result = fPackages.Init();
if (fPackageMap == NULL)
return B_NO_MEMORY;
status_t result = fPackageMap->Init();
if (result != B_OK) if (result != B_OK)
return result; return result;
@ -421,7 +261,7 @@ BRepositoryCache::SetTo(const BEntry& entry)
if ((result = repositoryReader.Init(repositoryCachePath.Path())) != B_OK) if ((result = repositoryReader.Init(repositoryCachePath.Path())) != B_OK)
return result; return result;
RepositoryContentHandler handler(&fInfo, fPackageMap); RepositoryContentHandler handler(&fInfo, fPackages);
if ((result = repositoryReader.ParseContent(&handler)) != B_OK) if ((result = repositoryReader.ParseContent(&handler)) != B_OK)
return result; return result;
@ -439,17 +279,14 @@ BRepositoryCache::SetTo(const BEntry& entry)
uint32 uint32
BRepositoryCache::CountPackages() const BRepositoryCache::CountPackages() const
{ {
if (fPackageMap == NULL) return fPackages.CountInfos();
return 0;
return fPackageMap->CountPackageInfos();
} }
BRepositoryCache::Iterator BRepositoryCache::Iterator
BRepositoryCache::GetIterator() const BRepositoryCache::GetIterator() const
{ {
return Iterator(this); return fPackages.GetIterator();
} }