* Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de.
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include <TypeConstants.h>
#include "Entry.h"
#include "EntryListener.h"
#include "IndexImpl.h"
#include "Node.h"
#include "NodeListener.h"
#include "SizeIndex.h"
#include "Volume.h"
#include <util/AutoLock.h>
class SizeIndexPrimaryKey {
public:
SizeIndexPrimaryKey(Node *node, off_t size)
: node(node), size(size) {}
SizeIndexPrimaryKey(Node *node)
: node(node), size(node->GetSize()) {}
SizeIndexPrimaryKey(off_t size)
: node(NULL), size(size) {}
Node *node;
off_t size;
};
class SizeIndexGetPrimaryKey {
public:
inline SizeIndexPrimaryKey operator()(Node *a)
{
return SizeIndexPrimaryKey(a);
}
inline SizeIndexPrimaryKey operator()(Node *a) const
{
return SizeIndexPrimaryKey(a);
}
};
class SizeIndexPrimaryKeyCompare
{
public:
inline int operator()(const SizeIndexPrimaryKey &a,
const SizeIndexPrimaryKey &b) const
{
if (a.node != NULL && a.node == b.node)
return 0;
if (a.size < b.size)
return -1;
if (a.size > b.size)
return 1;
return 0;
}
};
typedef TwoKeyAVLTree<Node*, SizeIndexPrimaryKey,
SizeIndexPrimaryKeyCompare,
SizeIndexGetPrimaryKey>
_NodeTree;
class SizeIndex::NodeTree : public _NodeTree {};
class SizeIndex::IteratorList : public DoublyLinkedList<Iterator> {};
class SizeIndex::Iterator
: public NodeEntryIterator<SizeIndex::NodeTree::Iterator>,
public DoublyLinkedListLinkImpl<Iterator>, public EntryListener,
public NodeListener {
public:
Iterator();
virtual ~Iterator();
virtual Entry *GetCurrent();
virtual Entry *GetCurrent(uint8 *buffer, size_t *keyLength);
virtual status_t Suspend();
virtual status_t Resume();
bool SetTo(SizeIndex *index, off_t size, bool ignoreValue = false);
void Unset();
virtual void EntryRemoved(Entry *entry);
virtual void NodeRemoved(Node *node);
private:
typedef NodeEntryIterator<SizeIndex::NodeTree::Iterator> BaseClass;
private:
SizeIndex *fIndex;
};
SizeIndex::SizeIndex(Volume *volume)
: Index(volume, "size", B_INT64_TYPE, true, sizeof(off_t)),
fNodes(new(nothrow) NodeTree),
fIterators(new(nothrow) IteratorList)
{
if (fInitStatus == B_OK && (!fNodes || !fIterators))
fInitStatus = B_NO_MEMORY;
if (fInitStatus == B_OK) {
fInitStatus = fVolume->AddNodeListener(this,
NULL, NODE_LISTEN_ANY_NODE | NODE_LISTEN_ALL);
}
}
SizeIndex::~SizeIndex()
{
if (fVolume)
fVolume->RemoveNodeListener(this, NULL);
if (fIterators) {
for (Iterator *iterator = fIterators->First();
iterator;
iterator = fIterators->GetNext(iterator)) {
iterator->SetTo(NULL, 0);
}
delete fIterators;
}
if (fNodes)
delete fNodes;
}
int32
SizeIndex::CountEntries() const
{
return fNodes->CountItems();
}
status_t
SizeIndex::Changed(Node *node, off_t oldSize)
{
fVolume->AssertWriteLocked();
status_t error = B_BAD_VALUE;
if (node) {
NodeTree::Iterator it;
Node **foundNode = fNodes->Find(SizeIndexPrimaryKey(node, oldSize),
node, &it);
if (foundNode && *foundNode == node) {
for (Iterator *iterator = fIterators->First();
iterator;
iterator = fIterators->GetNext(iterator)) {
if (iterator->GetCurrentNode() == node)
iterator->NodeRemoved(node);
}
it.Remove();
error = fNodes->Insert(node);
off_t newSize = node->GetSize();
fVolume->UpdateLiveQueries(NULL, node, GetName(), GetType(),
(const uint8*)&oldSize, sizeof(oldSize), (const uint8*)&newSize,
sizeof(newSize));
}
}
return error;
}
void
SizeIndex::NodeAdded(Node *node)
{
if (node)
fNodes->Insert(node);
}
void
SizeIndex::NodeRemoved(Node *node)
{
if (node)
fNodes->Remove(node, node);
}
AbstractIndexEntryIterator *
SizeIndex::InternalGetIterator()
{
Iterator *iterator = new(nothrow) Iterator;
if (iterator) {
if (!iterator->SetTo(this, 0, true)) {
delete iterator;
iterator = NULL;
}
}
return iterator;
}
AbstractIndexEntryIterator *
SizeIndex::InternalFind(const uint8 *key, size_t length)
{
if (!key || length != sizeof(off_t))
return NULL;
Iterator *iterator = new(nothrow) Iterator;
if (iterator) {
if (!iterator->SetTo(this, *(const off_t*)key)) {
delete iterator;
iterator = NULL;
}
}
return iterator;
}
void
SizeIndex::_AddIterator(Iterator *iterator)
{
RecursiveLocker locker(fVolume->GetAttributeIteratorLock());
fIterators->Insert(iterator);
}
void
SizeIndex::_RemoveIterator(Iterator *iterator)
{
RecursiveLocker locker(fVolume->GetAttributeIteratorLock());
fIterators->Remove(iterator);
}
SizeIndex::Iterator::Iterator()
: BaseClass(),
fIndex(NULL)
{
}
SizeIndex::Iterator::~Iterator()
{
SetTo(NULL, 0);
}
Entry *
SizeIndex::Iterator::GetCurrent()
{
return BaseClass::GetCurrent();
}
Entry *
SizeIndex::Iterator::GetCurrent(uint8 *buffer, size_t *keyLength)
{
Entry *entry = GetCurrent();
if (entry) {
*(off_t*)buffer = entry->GetNode()->GetSize();
*keyLength = sizeof(size_t);
}
return entry;
}
status_t
SizeIndex::Iterator::Suspend()
{
status_t error = BaseClass::Suspend();
if (error == B_OK) {
if (fNode) {
error = fIndex->GetVolume()->AddNodeListener(this, fNode,
NODE_LISTEN_REMOVED);
if (error == B_OK && fEntry) {
error = fIndex->GetVolume()->AddEntryListener(this, fEntry,
ENTRY_LISTEN_REMOVED);
if (error != B_OK)
fIndex->GetVolume()->RemoveNodeListener(this, fNode);
}
if (error != B_OK)
BaseClass::Resume();
}
}
return error;
}
status_t
SizeIndex::Iterator::Resume()
{
status_t error = BaseClass::Resume();
if (error == B_OK) {
if (fEntry)
error = fIndex->GetVolume()->RemoveEntryListener(this, fEntry);
if (fNode) {
if (error == B_OK)
error = fIndex->GetVolume()->RemoveNodeListener(this, fNode);
else
fIndex->GetVolume()->RemoveNodeListener(this, fNode);
}
}
return error;
}
bool
SizeIndex::Iterator::SetTo(SizeIndex *index, off_t size, bool ignoreValue)
{
Resume();
Unset();
fIndex = index;
if (fIndex)
fIndex->_AddIterator(this);
fInitialized = fIndex;
if (fIndex) {
bool found = true;
if (ignoreValue)
fIndex->fNodes->GetIterator(&fIterator);
else
found = fIndex->fNodes->FindFirst(size, &fIterator);
if (found) {
if (Node **nodeP = fIterator.GetCurrent()) {
fNode = *nodeP;
fEntry = fNode->GetFirstReferrer();
if (!fEntry)
BaseClass::GetNext();
if (!ignoreValue && fNode && fNode->GetSize() != size)
Unset();
}
}
}
return fEntry;
}
void
SizeIndex::Iterator::Unset()
{
if (fIndex) {
fIndex->_RemoveIterator(this);
fIndex = NULL;
}
BaseClass::Unset();
}
void
SizeIndex::Iterator::EntryRemoved(Entry *)
{
Resume();
fIsNext = BaseClass::GetNext();
Suspend();
}
void
SizeIndex::Iterator::NodeRemoved(Node *)
{
Resume();
fEntry = NULL;
fIsNext = BaseClass::GetNext();
Suspend();
}