#include "persifs.h"
#include "memblobindex.h"

#define DEBUG 0

void
memBlobIndex::create(str filename,
                     callback<void, ref<memBlobIndex> >::ref cb,
                     cbe error)
{
  cb(New refcounted<memBlobIndex>());
}

void
memBlobIndex::get(blockFingerprint fp,
                   callback<void, blockAddress>::ref cb,
                   cbe error)
{
  blockAddress *a = entries[fp];

  if (a == NULL) {
#if DEBUG
    warn << "memBlobIndex: get: fp=" << fp << " not in index\n";
#endif
    cb(0);
  } else {
#if DEBUG
    warn << "memBlobIndex: get: fp=" << fp << " addr=" << *a << "\n";
#endif
    cb(*a);
  }
}

void
memBlobIndex::put(blockFingerprint fp, blockAddress addr,
                  callback<void>::ref cb, cbe error)
{
  if (entries[fp] != NULL) {
#if DEBUG
    warn << "memBlobIndex: attempted put: fp=" << fp << ", addr=" << addr
         << "but already exists\n";
#endif
    error(PERR_EXISTS);
  } else {
    entries.insert(fp, addr);
#if DEBUG    
    warn << "memBlobIndex: put: fp=" << fp << ", addr=" << addr << "\n";
#endif
  }

  cb();
}

memBlobIndex::~memBlobIndex()
{
}
