Skip to content

Commit

Permalink
Merge pull request #8832 from vespa-engine/geirst/more-datastore-refa…
Browse files Browse the repository at this point in the history
…ctoring

Rename data store getBufferEntry() -> getEntry() and change it to tak...
  • Loading branch information
baldersheim committed Mar 19, 2019
2 parents 57165ee + 7b2d6ca commit e5b87d5
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 76 deletions.
8 changes: 4 additions & 4 deletions searchlib/src/vespa/searchlib/attribute/enumstore.cpp
Expand Up @@ -93,8 +93,7 @@ EnumStoreT<StringEntryType>::writeValues(BufferWriter &writer,
{
for (uint32_t i = 0; i < count; ++i) {
Index idx = idxs[i];
const char *src(_store.getBufferEntry<char>(idx.bufferId(),
idx.offset()) +
const char *src(_store.getEntry<char>(idx) +
EntryBase::size());
size_t sz = strlen(src) + 1;
writer.write(src, sz);
Expand Down Expand Up @@ -135,7 +134,8 @@ EnumStoreT<StringEntryType>::deserialize(const void *src,
LOG_ABORT("Out of enumstore bufferspace");
}
uint64_t offset = buffer.size();
char *dst(_store.getBufferEntry<char>(activeBufferId, offset));
Index newIdx(offset, activeBufferId);
char *dst(_store.getEntry<char>(newIdx));
memcpy(dst, &_nextEnum, sizeof(uint32_t));
uint32_t pos = sizeof(uint32_t);
uint32_t refCount(0);
Expand All @@ -149,7 +149,7 @@ EnumStoreT<StringEntryType>::deserialize(const void *src,
assert(ComparatorType::compare(getValue(idx),
Entry(dst).getValue()) < 0);
}
idx = Index(offset, activeBufferId);
idx = newIdx;
return sz;
}

Expand Down
2 changes: 1 addition & 1 deletion searchlib/src/vespa/searchlib/attribute/enumstore.h
Expand Up @@ -106,7 +106,7 @@ class EnumStoreT : public EnumStoreBase
using EnumStoreBase::TYPE_ID;

Entry getEntry(Index idx) const {
return Entry(const_cast<DataStoreType &>(_store).getBufferEntry<char>(idx.bufferId(), idx.offset()));
return Entry(const_cast<DataStoreType &>(_store).getEntry<char>(idx));
}
void printEntry(vespalib::asciistream & os, const Entry & e) const;

Expand Down
17 changes: 9 additions & 8 deletions searchlib/src/vespa/searchlib/attribute/enumstore.hpp
Expand Up @@ -115,7 +115,7 @@ EnumStoreT<EntryType>::writeValues(BufferWriter &writer, const Index *idxs, size
size_t sz(EntryType::fixedSize());
for (uint32_t i = 0; i < count; ++i) {
Index idx = idxs[i];
const char *src(_store.getBufferEntry<char>(idx.bufferId(), idx.offset()) + EntryBase::size());
const char *src(_store.getEntry<char>(idx) + EntryBase::size());
writer.write(src, sz);
}
}
Expand Down Expand Up @@ -148,7 +148,8 @@ EnumStoreT<EntryType>::deserialize(const void *src, size_t available, Index &idx
HDR_ABORT("not enough space");
}
uint64_t offset = buffer.size();
char *dst(_store.getBufferEntry<char>(activeBufferId, offset));
Index newIdx(offset, activeBufferId);
char *dst(_store.getEntry<char>(newIdx));
memcpy(dst, &_nextEnum, sizeof(uint32_t));
uint32_t pos = sizeof(uint32_t);
uint32_t refCount(0);
Expand All @@ -161,7 +162,7 @@ EnumStoreT<EntryType>::deserialize(const void *src, size_t available, Index &idx
if (idx.valid()) {
assert(ComparatorType::compare(getValue(idx), Entry(dst).getValue()) < 0);
}
idx = Index(offset, activeBufferId);
idx = newIdx;
return sz;
}

Expand Down Expand Up @@ -267,11 +268,11 @@ EnumStoreT<EntryType>::addEnum(Type value, Index &newIdx, Dictionary &dict)
}

uint64_t offset = buffer.size();
char * dst = _store.template getBufferEntry<char>(activeBufferId, offset);
newIdx = Index(offset, activeBufferId);
char * dst = _store.template getEntry<char>(newIdx);
this->insertEntry(dst, this->_nextEnum++, 0, value);
buffer.pushed_back(entrySize);
assert(Index::pad(offset) == 0);
newIdx = Index(offset, activeBufferId);

// update tree with new index
dict.insert(it, newIdx, typename Dictionary::DataType());
Expand Down Expand Up @@ -357,7 +358,7 @@ EnumStoreT<EntryType>::reset(Builder &builder, Dictionary &dict)
{
uint64_t offset = state.size();
Index idx(offset, activeBufferId);
char * dst = _store.template getBufferEntry<char>(activeBufferId, offset);
char * dst = _store.template getEntry<char>(idx);
this->insertEntry(dst, this->_nextEnum++, iter->_refCount, iter->_value);
state.pushed_back(iter->_sz);

Expand Down Expand Up @@ -413,7 +414,8 @@ EnumStoreT<EntryType>::performCompaction(Dictionary &dict, EnumIndexMap & old2Ne
}

uint64_t offset = freeBuf.size();
char * dst = _store.template getBufferEntry<char>(freeBufferIdx, offset);
Index newIdx = Index(offset, freeBufferIdx);
char * dst = _store.template getEntry<char>(newIdx);
// insert entry into free buffer
this->insertEntry(dst, newEnum, refCount, value);
#ifdef LOG_ENUM_STORE
Expand All @@ -424,7 +426,6 @@ EnumStoreT<EntryType>::performCompaction(Dictionary &dict, EnumIndexMap & old2Ne
}
freeBuf.pushed_back(entrySize);
assert(Index::pad(offset) == 0);
Index newIdx = Index(offset, freeBufferIdx);
#ifdef LOG_ENUM_STORE
LOG(info,
"performCompaction(): new index: offset = %" PRIu64
Expand Down
2 changes: 1 addition & 1 deletion searchlib/src/vespa/searchlib/attribute/enumstorebase.h
Expand Up @@ -252,7 +252,7 @@ class EnumStoreBase
virtual ~EnumStoreBase();

EntryBase getEntryBase(Index idx) const {
return EntryBase(const_cast<DataStoreType &>(_store).getBufferEntry<char>(idx.bufferId(), idx.offset()));
return EntryBase(const_cast<DataStoreType &>(_store).getEntry<char>(idx));
}
datastore::BufferState & getBuffer(uint32_t bufferIdx) {
return _store.getBufferState(bufferIdx);
Expand Down
6 changes: 2 additions & 4 deletions searchlib/src/vespa/searchlib/attribute/postingstore.h
Expand Up @@ -167,13 +167,11 @@ class PostingStore : public PostingListTraits<DataT>::PostingStoreBase,
AggregatedType getAggregated(const EntryRef ref) const;

const BitVectorEntry *getBitVectorEntry(RefType ref) const {
return _store.template getBufferEntry<BitVectorEntry>(ref.bufferId(),
ref.offset());
return _store.template getEntry<BitVectorEntry>(ref);
}

BitVectorEntry *getWBitVectorEntry(RefType ref) {
return _store.template getBufferEntry<BitVectorEntry>(ref.bufferId(),
ref.offset());
return _store.template getEntry<BitVectorEntry>(ref);
}

static inline DataT bitVectorWeight();
Expand Down
12 changes: 6 additions & 6 deletions searchlib/src/vespa/searchlib/btree/btreenodestore.h
Expand Up @@ -84,34 +84,34 @@ class BTreeNodeStore

const InternalNodeType *mapInternalRef(EntryRef ref) const {
RefType iRef(ref);
return _store.getBufferEntry<InternalNodeType>(iRef.bufferId(), iRef.offset());
return _store.getEntry<InternalNodeType>(iRef);
}

InternalNodeType *mapInternalRef(EntryRef ref) {
RefType iRef(ref);
return _store.getBufferEntry<InternalNodeType>(iRef.bufferId(), iRef.offset());
return _store.getEntry<InternalNodeType>(iRef);
}

const LeafNodeType *mapLeafRef(EntryRef ref) const {
RefType iRef(ref);
return _store.getBufferEntry<LeafNodeType>(iRef.bufferId(), iRef.offset());
return _store.getEntry<LeafNodeType>(iRef);
}

LeafNodeType *mapLeafRef(EntryRef ref) {
RefType iRef(ref);
return _store.getBufferEntry<LeafNodeType>(iRef.bufferId(), iRef.offset());
return _store.getEntry<LeafNodeType>(iRef);
}

template <typename NodeType>
const NodeType *mapRef(EntryRef ref) const {
RefType iRef(ref);
return _store.getBufferEntry<NodeType>(iRef.bufferId(), iRef.offset());
return _store.getEntry<NodeType>(iRef);
}

template <typename NodeType>
NodeType *mapRef(EntryRef ref) {
RefType iRef(ref);
return _store.getBufferEntry<NodeType>(iRef.bufferId(), iRef.offset());
return _store.getEntry<NodeType>(iRef);
}

LeafNodeTypeRefPair allocNewLeafNode() {
Expand Down
8 changes: 4 additions & 4 deletions searchlib/src/vespa/searchlib/btree/btreestore.h
Expand Up @@ -91,7 +91,7 @@ class BTreeStore
Builder _builder;

BTreeType * getWTreeEntry(RefType ref) {
return _store.getBufferEntry<BTreeType>(ref.bufferId(), ref.offset());
return _store.getEntry<BTreeType>(ref);
}

public:
Expand Down Expand Up @@ -322,11 +322,11 @@ class BTreeStore
}

const BTreeType * getTreeEntry(RefType ref) const {
return _store.getBufferEntry<BTreeType>(ref.bufferId(), ref.offset());
return _store.getEntry<BTreeType>(ref);
}

const KeyDataType * getKeyDataEntry(RefType ref, uint32_t clusterSize) const {
return _store.getBufferEntry<KeyDataType>(ref.bufferId(), ref.offset() * clusterSize);
const KeyDataType * getKeyDataEntry(RefType ref, uint32_t arraySize) const {
return _store.getEntryArray<KeyDataType>(ref, arraySize);
}

void freeze() {
Expand Down
19 changes: 11 additions & 8 deletions searchlib/src/vespa/searchlib/datastore/allocator.hpp
Expand Up @@ -24,10 +24,11 @@ Allocator<EntryT, RefT>::alloc(Args && ... args)
BufferState &state = _store.getBufferState(activeBufferId);
assert(state.isActive());
size_t oldBufferSize = state.size();
EntryT *entry = _store.getBufferEntry<EntryT>(activeBufferId, oldBufferSize);
RefT ref(oldBufferSize, activeBufferId);
EntryT *entry = _store.getEntry<EntryT>(ref);
new (static_cast<void *>(entry)) EntryT(std::forward<Args>(args)...);
state.pushed_back(1);
return HandleType(RefT(oldBufferSize, activeBufferId), entry);
return HandleType(ref, entry);
}

template <typename EntryT, typename RefT>
Expand All @@ -40,13 +41,14 @@ Allocator<EntryT, RefT>::allocArray(ConstArrayRef array)
assert(state.isActive());
assert(state.getArraySize() == array.size());
size_t oldBufferSize = state.size();
EntryT *buf = _store.template getBufferEntry<EntryT>(activeBufferId, oldBufferSize);
assert((oldBufferSize % array.size()) == 0);
RefT ref((oldBufferSize / array.size()), activeBufferId);
EntryT *buf = _store.template getEntryArray<EntryT>(ref, array.size());
for (size_t i = 0; i < array.size(); ++i) {
new (static_cast<void *>(buf + i)) EntryT(array[i]);
}
state.pushed_back(array.size());
assert((oldBufferSize % array.size()) == 0);
return HandleType(RefT((oldBufferSize / array.size()), activeBufferId), buf);
return HandleType(ref, buf);
}

template <typename EntryT, typename RefT>
Expand All @@ -59,13 +61,14 @@ Allocator<EntryT, RefT>::allocArray(size_t size)
assert(state.isActive());
assert(state.getArraySize() == size);
size_t oldBufferSize = state.size();
EntryT *buf = _store.template getBufferEntry<EntryT>(activeBufferId, oldBufferSize);
assert((oldBufferSize % size) == 0);
RefT ref((oldBufferSize / size), activeBufferId);
EntryT *buf = _store.template getEntryArray<EntryT>(ref, size);
for (size_t i = 0; i < size; ++i) {
new (static_cast<void *>(buf + i)) EntryT();
}
state.pushed_back(size);
assert((oldBufferSize % size) == 0);
return HandleType(RefT((oldBufferSize / size), activeBufferId), buf);
return HandleType(ref, buf);
}

}
Expand Down
5 changes: 2 additions & 3 deletions searchlib/src/vespa/searchlib/datastore/array_store.h
Expand Up @@ -57,12 +57,11 @@ class ArrayStore
EntryRef addSmallArray(const ConstArrayRef &array);
EntryRef addLargeArray(const ConstArrayRef &array);
ConstArrayRef getSmallArray(RefT ref, size_t arraySize) const {
size_t bufferOffset = ref.offset() * arraySize;
const EntryT *buf = _store.template getBufferEntry<EntryT>(ref.bufferId(), bufferOffset);
const EntryT *buf = _store.template getEntryArray<EntryT>(ref, arraySize);
return ConstArrayRef(buf, arraySize);
}
ConstArrayRef getLargeArray(RefT ref) const {
const LargeArray *buf = _store.template getBufferEntry<LargeArray>(ref.bufferId(), ref.offset());
const LargeArray *buf = _store.template getEntry<LargeArray>(ref);
return ConstArrayRef(&(*buf)[0], buf->size());
}

Expand Down
5 changes: 3 additions & 2 deletions searchlib/src/vespa/searchlib/datastore/array_store.hpp
Expand Up @@ -93,10 +93,11 @@ ArrayStore<EntryT, RefT>::addLargeArray(const ConstArrayRef &array)
BufferState &state = _store.getBufferState(activeBufferId);
assert(state.isActive());
size_t oldBufferSize = state.size();
LargeArray *buf = _store.template getBufferEntry<LargeArray>(activeBufferId, oldBufferSize);
RefT ref(oldBufferSize, activeBufferId);
LargeArray *buf = _store.template getEntry<LargeArray>(ref);
new (static_cast<void *>(buf)) LargeArray(array.cbegin(), array.cend());
state.pushed_back(1, sizeof(EntryT) * array.size());
return RefT(oldBufferSize, activeBufferId);
return ref;
}

template <typename EntryT, typename RefT>
Expand Down
2 changes: 1 addition & 1 deletion searchlib/src/vespa/searchlib/datastore/datastore.h
Expand Up @@ -95,7 +95,7 @@ class DataStore : public DataStoreT<RefT>
using ParentType::ensureBufferCapacity;
using ParentType::_activeBufferIds;
using ParentType::_freeListLists;
using ParentType::getBufferEntry;
using ParentType::getEntry;
using ParentType::dropBuffers;
using ParentType::initActiveBuffers;
using ParentType::addType;
Expand Down
4 changes: 1 addition & 3 deletions searchlib/src/vespa/searchlib/datastore/datastore.hpp
Expand Up @@ -169,9 +169,7 @@ const EntryType &
DataStore<EntryType, RefT>::getEntry(EntryRef ref) const
{
RefType intRef(ref);
const EntryType *be =
this->template
getBufferEntry<EntryType>(intRef.bufferId(), intRef.offset());
const EntryType *be = this->template getEntry<EntryType>(intRef);
return *be;
}

Expand Down
22 changes: 16 additions & 6 deletions searchlib/src/vespa/searchlib/datastore/datastorebase.h
Expand Up @@ -265,14 +265,24 @@ class DataStoreBase

void clearHoldLists();

template <typename EntryType>
EntryType *getBufferEntry(uint32_t bufferId, size_t offset) {
return static_cast<EntryType *>(_buffers[bufferId].getBuffer()) + offset;
template <typename EntryType, typename RefType>
EntryType *getEntry(RefType ref) {
return static_cast<EntryType *>(_buffers[ref.bufferId()].getBuffer()) + ref.offset();
}

template <typename EntryType>
const EntryType *getBufferEntry(uint32_t bufferId, size_t offset) const {
return static_cast<const EntryType *>(_buffers[bufferId].getBuffer()) + offset;
template <typename EntryType, typename RefType>
const EntryType *getEntry(RefType ref) const {
return static_cast<const EntryType *>(_buffers[ref.bufferId()].getBuffer()) + ref.offset();
}

template <typename EntryType, typename RefType>
EntryType *getEntryArray(RefType ref, size_t arraySize) {
return static_cast<EntryType *>(_buffers[ref.bufferId()].getBuffer()) + (ref.offset() * arraySize);
}

template <typename EntryType, typename RefType>
const EntryType *getEntryArray(RefType ref, size_t arraySize) const {
return static_cast<const EntryType *>(_buffers[ref.bufferId()].getBuffer()) + (ref.offset() * arraySize);
}

void dropBuffers();
Expand Down
Expand Up @@ -59,7 +59,7 @@ FreeListAllocator<EntryT, RefT, ReclaimerT>::alloc(Args && ... args)
BufferState &state = *freeListList._head;
assert(state.isActive());
RefT ref = state.popFreeList();
EntryT *entry = _store.template getBufferEntry<EntryT>(ref.bufferId(), ref.offset());
EntryT *entry = _store.template getEntry<EntryT>(ref);
ReclaimerT::reclaim(entry);
allocator::Assigner<EntryT, Args...>::assign(*entry, std::forward<Args>(args)...);
return HandleType(ref, entry);
Expand All @@ -77,7 +77,7 @@ FreeListAllocator<EntryT, RefT, ReclaimerT>::allocArray(ConstArrayRef array)
assert(state.isActive());
assert(state.getArraySize() == array.size());
RefT ref(state.popFreeList());
EntryT *buf = _store.template getBufferEntry<EntryT>(ref.bufferId(), ref.offset() * array.size());
EntryT *buf = _store.template getEntryArray<EntryT>(ref, array.size());
for (size_t i = 0; i < array.size(); ++i) {
*(buf + i) = array[i];
}
Expand All @@ -96,7 +96,7 @@ FreeListAllocator<EntryT, RefT, ReclaimerT>::allocArray(size_t size)
assert(state.isActive());
assert(state.getArraySize() == size);
RefT ref(state.popFreeList());
EntryT *buf = _store.template getBufferEntry<EntryT>(ref.bufferId(), ref.offset() * size);
EntryT *buf = _store.template getEntryArray<EntryT>(ref, size);
return HandleType(ref, buf);
}

Expand Down
Expand Up @@ -25,8 +25,9 @@ FreeListRawAllocator<EntryT, RefT>::alloc(size_t numElems)
assert(state.getArraySize() == numElems);
RefT ref = state.popFreeList();
// If entry ref is not aligned we must scale the offset according to array size as it was divided when the entry ref was created.
size_t offset = !RefT::isAlignedType ? ref.offset() * state.getArraySize() : ref.offset();
EntryT *entry = _store.template getBufferEntry<EntryT>(ref.bufferId(), offset);
EntryT *entry = !RefT::isAlignedType ?
_store.template getEntryArray<EntryT>(ref, state.getArraySize()) :
_store.template getEntry<EntryT>(ref);
return HandleType(ref, entry);
}

Expand Down
12 changes: 8 additions & 4 deletions searchlib/src/vespa/searchlib/datastore/raw_allocator.hpp
Expand Up @@ -23,16 +23,20 @@ RawAllocator<EntryT, RefT>::alloc(size_t numElems, size_t extraElems)
BufferState &state = _store.getBufferState(activeBufferId);
assert(state.isActive());
size_t oldBufferSize = state.size();
EntryT *buffer = _store.getBufferEntry<EntryT>(activeBufferId, oldBufferSize);
state.pushed_back(numElems);
if (RefT::isAlignedType) {
// AlignedEntryRef constructor scales down offset by alignment
return HandleType(RefT(oldBufferSize, activeBufferId), buffer);
RefT ref(oldBufferSize, activeBufferId);
EntryT *buffer = _store.getEntry<EntryT>(ref);
state.pushed_back(numElems);
return HandleType(ref, buffer);
} else {
// Must perform scaling ourselves, according to array size
size_t arraySize = state.getArraySize();
assert((numElems % arraySize) == 0u);
return HandleType(RefT(oldBufferSize / arraySize, activeBufferId), buffer);
RefT ref((oldBufferSize / arraySize), activeBufferId);
EntryT *buffer = _store.getEntryArray<EntryT>(ref, arraySize);
state.pushed_back(numElems);
return HandleType(ref, buffer);
}
}

Expand Down

0 comments on commit e5b87d5

Please sign in to comment.