Beatmup
Beatmup::Python::WritableChunkCollection Class Reference

Writable ChunkCollection implementation for Python. More...

#include <chunk_collection.h>

Inheritance diagram for Beatmup::Python::WritableChunkCollection:
Beatmup::ChunkCollection Beatmup::Object

Public Member Functions

 WritableChunkCollection ()
 
pybind11::buffer & operator[] (const std::string &id)
 
void open ()
 Opens the collection to read chunks from it. More...
 
void close ()
 Closes the collection after a reading session. More...
 
size_t size () const
 Returns the number of chunks available in the collection after it is opened. More...
 
bool chunkExists (const std::string &id) const
 Check if a specific chunk exists. More...
 
chunksize_t chunkSize (const std::string &id) const
 Retrieves size of a specific chunk. More...
 
chunksize_t fetch (const std::string &id, void *data, const chunksize_t limit)
 Reads a chunk. More...
 
void save (const std::string &filename, bool append=false)
 Saves the collection to a file. More...
 
- Public Member Functions inherited from Beatmup::ChunkCollection
template<typename datatype >
datatype read (const std::string &id)
 Reads a chunk and casts it into a given type. More...
 
template<typename datatype >
std::vector< datatype > readVector (const std::string &id)
 Reads a chunk into a vector of a specific type. More...
 
template<>
std::string read (const std::string &id)
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Private Attributes

std::map< std::string, pybind11::buffer > data
 

Detailed Description

Writable ChunkCollection implementation for Python.

Allows to exchange binary data without copying.

Definition at line 35 of file chunk_collection.h.

Constructor & Destructor Documentation

◆ WritableChunkCollection()

Beatmup::Python::WritableChunkCollection::WritableChunkCollection ( )
inline

Definition at line 40 of file chunk_collection.h.

40 {}

Member Function Documentation

◆ operator[]()

pybind11::buffer& Beatmup::Python::WritableChunkCollection::operator[] ( const std::string &  id)
inline

Definition at line 42 of file chunk_collection.h.

42  {
43  return data[id];
44  }
std::map< std::string, pybind11::buffer > data
JNIEnv jlong jstring id

◆ open()

void Beatmup::Python::WritableChunkCollection::open ( )
inlinevirtual

Opens the collection to read chunks from it.

Implements Beatmup::ChunkCollection.

Definition at line 46 of file chunk_collection.h.

46 {}

◆ close()

void Beatmup::Python::WritableChunkCollection::close ( )
inlinevirtual

Closes the collection after a reading session.

Implements Beatmup::ChunkCollection.

Definition at line 47 of file chunk_collection.h.

47 {}

◆ size()

size_t Beatmup::Python::WritableChunkCollection::size ( ) const
inlinevirtual

Returns the number of chunks available in the collection after it is opened.

Implements Beatmup::ChunkCollection.

Definition at line 49 of file chunk_collection.h.

49 { return data.size(); }

◆ chunkExists()

bool Beatmup::Python::WritableChunkCollection::chunkExists ( const std::string &  id) const
inlinevirtual

Check if a specific chunk exists.

Parameters
[in]idThe chunk id
Returns
true if the chunk exists in the collection.

Implements Beatmup::ChunkCollection.

Definition at line 51 of file chunk_collection.h.

51 { return data.find(id) != data.cend(); }

◆ chunkSize()

chunksize_t Python::WritableChunkCollection::chunkSize ( const std::string &  id) const
virtual

Retrieves size of a specific chunk.

Parameters
[in]idThe chunk id
Returns
size of the chunk in bytes, 0 if not found.

Implements Beatmup::ChunkCollection.

Definition at line 104 of file chunk_collection.cpp.

104  {
105  auto it = data.find(id);
106  if (it == data.cend())
107  return 0;
108  auto info = it->second.request();
109  return info.size * info.itemsize;
110 }

◆ fetch()

chunksize_t Python::WritableChunkCollection::fetch ( const std::string &  id,
void *  data,
const chunksize_t  limit 
)
virtual

Reads a chunk.

The collection is expected to be opened.

Parameters
[in]idWanted chunk id.
[out]dataA buffer to write out the wanted chunk content.
[in]limitThe buffer capacity in bytes.
Returns
number of bytes written out to the buffer: if the chunk is found fits the buffer, the chunk size is returned; if the chunk is found and too big, limit is returned (number of bytes actually written); if no chunk found, 0 is returned.

Implements Beatmup::ChunkCollection.

Definition at line 113 of file chunk_collection.cpp.

113  {
114  auto it = this->data.find(id);
115  if (it == this->data.end())
116  return 0;
117  Internal::Copy copy(it->second.request(), data, limit);
118  return limit - copy.remaining();
119 }
Copies data from multidimensional strided tensor to a continuous memory space.
Beatmup::AbstractBitmap * copy

◆ save()

void Python::WritableChunkCollection::save ( const std::string &  filename,
bool  append = false 
)
virtual

Saves the collection to a file.

Parameters
[in]filenameThe name of the file to write chunks to
[in]appendIf true, writing to the end of the file (keeping the existing content). Rewriting the file otherwise.

Implements Beatmup::ChunkCollection.

Definition at line 122 of file chunk_collection.cpp.

122  {
124  for (auto it : data) {
125  Chunk chunk(*this, it.first);
126  writer(it.first, chunk(), chunk.size());
127  }
128 }
Writes chunks to a file.
Definition: chunkfile.h:191
Simply a piece of binary data of a specific size.
Definition: chunkfile.h:210
JNIEnv jlong jstring filename
JNIEnv jlong jstring jboolean append

Member Data Documentation

◆ data

std::map<std::string, pybind11::buffer> Beatmup::Python::WritableChunkCollection::data
private

Definition at line 37 of file chunk_collection.h.


The documentation for this class was generated from the following files: