Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ set(ARROW_SRCS
src/arrow/util/bit-util.cc
)

if(NOT APPLE)
if(NOT APPLE AND NOT MSVC)
# Localize thirdparty symbols using a linker version script. This hides them
# from the client application. The OS X linker does not support the
# version-script option.
Expand Down
7 changes: 3 additions & 4 deletions cpp/src/arrow/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Status;
class ARROW_EXPORT Buffer {
public:
Buffer(const uint8_t* data, int64_t size)
: is_mutable_(false), data_(data), size_(size), capacity_(size) {}
: is_mutable_(false), data_(data), size_(size), capacity_(size) {}
virtual ~Buffer();

/// An offset into data that is owned by another buffer, but we want to be
Expand All @@ -57,7 +57,7 @@ class ARROW_EXPORT Buffer {
/// in general we expected buffers to be aligned and padded to 64 bytes. In the future
/// we might add utility methods to help determine if a buffer satisfies this contract.
Buffer(const std::shared_ptr<Buffer>& parent, int64_t offset, int64_t size)
: Buffer(parent->data() + offset, size) {
: Buffer(parent->data() + offset, size) {
parent_ = parent;
}

Expand Down Expand Up @@ -112,8 +112,7 @@ std::shared_ptr<Buffer> ARROW_EXPORT SliceMutableBuffer(
/// A Buffer whose contents can be mutated. May or may not own its data.
class ARROW_EXPORT MutableBuffer : public Buffer {
public:
MutableBuffer(uint8_t* data, int64_t size)
: Buffer(data, size) {
MutableBuffer(uint8_t* data, int64_t size) : Buffer(data, size) {
mutable_data_ = data;
is_mutable_ = true;
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ set(ARROW_IO_SRCS
memory.cc
)

if(NOT APPLE)
if(NOT APPLE AND NOT MSVC)
# Localize thirdparty symbols using a linker version script. This hides them
# from the client application. The OS X linker does not support the
# version-script option.
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/io/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,19 @@ class MemoryMappedFile::MemoryMap : public MutableBuffer {
MemoryMappedFile::MemoryMappedFile() {}
MemoryMappedFile::~MemoryMappedFile() {}

Status MemoryMappedFile::Create(
const std::string& path, int64_t size, std::shared_ptr<MemoryMappedFile>* out) {
std::shared_ptr<FileOutputStream> file;
RETURN_NOT_OK(FileOutputStream::Open(path, &file));
#ifdef _MSC_VER
_chsize_s(file->file_descriptor(), static_cast<size_t>(size));
#else
ftruncate(file->file_descriptor(), static_cast<size_t>(size));
#endif
RETURN_NOT_OK(file->Close());
return MemoryMappedFile::Open(path, FileMode::READWRITE, out);
}

Status MemoryMappedFile::Open(const std::string& path, FileMode::type mode,
std::shared_ptr<MemoryMappedFile>* out) {
std::shared_ptr<MemoryMappedFile> result(new MemoryMappedFile());
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/arrow/io/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class ARROW_EXPORT MemoryMappedFile : public ReadWriteFileInterface {
public:
~MemoryMappedFile();

/// Create new file with indicated size, return in read/write mode
static Status Create(
const std::string& path, int64_t size, std::shared_ptr<MemoryMappedFile>* out);

static Status Open(const std::string& path, FileMode::type mode,
std::shared_ptr<MemoryMappedFile>* out);

Expand Down
8 changes: 2 additions & 6 deletions cpp/src/arrow/io/io-file-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,8 @@ TEST_F(TestMemoryMappedFile, WriteRead) {
const int reps = 5;

std::string path = "ipc-write-read-test";
CreateFile(path, reps * buffer_size);

std::shared_ptr<MemoryMappedFile> result;
ASSERT_OK(MemoryMappedFile::Open(path, FileMode::READWRITE, &result));
ASSERT_OK(InitMemoryMap(reps * buffer_size, path, &result));

int64_t position = 0;
std::shared_ptr<Buffer> out_buffer;
Expand All @@ -419,10 +417,8 @@ TEST_F(TestMemoryMappedFile, ReadOnly) {
const int reps = 5;

std::string path = "ipc-read-only-test";
CreateFile(path, reps * buffer_size);

std::shared_ptr<MemoryMappedFile> rwmmap;
ASSERT_OK(MemoryMappedFile::Open(path, FileMode::READWRITE, &rwmmap));
ASSERT_OK(InitMemoryMap(reps * buffer_size, path, &rwmmap));

int64_t position = 0;
for (int i = 0; i < reps; ++i) {
Expand Down
20 changes: 7 additions & 13 deletions cpp/src/arrow/io/test-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,17 @@ class MemoryMapFixture {
}
}

void CreateFile(const std::string path, int64_t size) {
FILE* file = fopen(path.c_str(), "w");
if (file != nullptr) {
tmp_files_.push_back(path);
#ifdef _MSC_VER
_chsize(fileno(file), static_cast<size_t>(size));
#else
ftruncate(fileno(file), static_cast<size_t>(size));
#endif
fclose(file);
}
void CreateFile(const std::string& path, int64_t size) {
std::shared_ptr<MemoryMappedFile> file;
ASSERT_OK(MemoryMappedFile::Create(path, size, &file));
tmp_files_.push_back(path);
}

Status InitMemoryMap(
int64_t size, const std::string& path, std::shared_ptr<MemoryMappedFile>* mmap) {
CreateFile(path, size);
return MemoryMappedFile::Open(path, FileMode::READWRITE, mmap);
RETURN_NOT_OK(MemoryMappedFile::Create(path, size, mmap));
tmp_files_.push_back(path);
return Status::OK();
}

private:
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/ipc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ set(ARROW_IPC_SRCS
writer.cc
)

if(NOT APPLE)
if(NOT MSVC AND NOT APPLE)
# Localize thirdparty symbols using a linker version script. This hides them
# from the client application. The OS X linker does not support the
# version-script option.
Expand Down
16 changes: 8 additions & 8 deletions cpp/src/arrow/ipc/feather-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ typedef std::vector<flatbuffers::Offset<fbs::Column>> ColumnVector;
typedef flatbuffers::FlatBufferBuilder FBB;
typedef flatbuffers::Offset<flatbuffers::String> FBString;

struct ColumnType {
struct ARROW_EXPORT ColumnType {
enum type { PRIMITIVE, CATEGORY, TIMESTAMP, DATE, TIME };
};

struct ArrayMetadata {
struct ARROW_EXPORT ArrayMetadata {
ArrayMetadata() {}

ArrayMetadata(fbs::Type type, int64_t offset, int64_t length, int64_t null_count,
Expand All @@ -69,20 +69,20 @@ struct ArrayMetadata {
int64_t total_bytes;
};

struct CategoryMetadata {
struct ARROW_EXPORT CategoryMetadata {
ArrayMetadata levels;
bool ordered;
};

struct TimestampMetadata {
struct ARROW_EXPORT TimestampMetadata {
TimeUnit unit;

// A timezone name known to the Olson timezone database. For display purposes
// because the actual data is all UTC
std::string timezone;
};

struct TimeMetadata {
struct ARROW_EXPORT TimeMetadata {
TimeUnit unit;
};

Expand All @@ -91,7 +91,7 @@ static constexpr const int kFeatherDefaultAlignment = 8;

class ColumnBuilder;

class TableBuilder {
class ARROW_EXPORT TableBuilder {
public:
explicit TableBuilder(int64_t num_rows);
~TableBuilder() = default;
Expand All @@ -116,7 +116,7 @@ class TableBuilder {
int64_t num_rows_;
};

class TableMetadata {
class ARROW_EXPORT TableMetadata {
public:
TableMetadata() {}
~TableMetadata() = default;
Expand Down Expand Up @@ -186,7 +186,7 @@ static inline void FromFlatbuffer(const fbs::PrimitiveArray* values, ArrayMetada
out->total_bytes = values->total_bytes();
}

class ColumnBuilder {
class ARROW_EXPORT ColumnBuilder {
public:
ColumnBuilder(TableBuilder* parent, const std::string& name);
~ColumnBuilder() = default;
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/ipc/feather.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <vector>

#include "arrow/type.h"
#include "arrow/util/visibility.h"

namespace arrow {

Expand Down
12 changes: 8 additions & 4 deletions cpp/src/arrow/ipc/ipc-read-write-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ TEST_F(TestSchemaMetadata, NestedFields) {
&MakeStruct, &MakeUnion, &MakeDictionary, &MakeDates, &MakeTimestamps, &MakeTimes, \
&MakeFWBinary, &MakeBooleanBatch);

static int g_file_number = 0;

class IpcTestFixture : public io::MemoryMapFixture {
public:
Status DoStandardRoundTrip(const RecordBatch& batch, bool zero_data,
Expand Down Expand Up @@ -163,8 +165,9 @@ class IpcTestFixture : public io::MemoryMapFixture {
}

void CheckRoundtrip(const RecordBatch& batch, int64_t buffer_size) {
std::string path = "test-write-row-batch";
ASSERT_OK(io::MemoryMapFixture::InitMemoryMap(buffer_size, path, &mmap_));
std::stringstream ss;
ss << "test-write-row-batch-" << g_file_number++;
ASSERT_OK(io::MemoryMapFixture::InitMemoryMap(buffer_size, ss.str(), &mmap_));

std::shared_ptr<RecordBatch> result;
ASSERT_OK(DoStandardRoundTrip(batch, true, &result));
Expand Down Expand Up @@ -303,9 +306,10 @@ class RecursionLimits : public ::testing::Test, public io::MemoryMapFixture {
std::vector<std::shared_ptr<Array>> arrays = {array};
*batch = std::make_shared<RecordBatch>(*schema, batch_length, arrays);

std::string path = "test-write-past-max-recursion";
std::stringstream ss;
ss << "test-write-past-max-recursion-" << g_file_number++;
const int memory_map_size = 1 << 20;
io::MemoryMapFixture::InitMemoryMap(memory_map_size, path, &mmap_);
RETURN_NOT_OK(io::MemoryMapFixture::InitMemoryMap(memory_map_size, ss.str(), &mmap_));

if (override_level) {
return WriteRecordBatch(**batch, 0, mmap_.get(), metadata_length, body_length,
Expand Down
27 changes: 14 additions & 13 deletions cpp/src/arrow/ipc/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ using DictionaryMap = std::unordered_map<int64_t, std::shared_ptr<Array>>;
using DictionaryTypeMap = std::unordered_map<int64_t, std::shared_ptr<Field>>;

// Memoization data structure for handling shared dictionaries
class DictionaryMemo {
class ARROW_EXPORT DictionaryMemo {
public:
DictionaryMemo();

Expand Down Expand Up @@ -114,12 +114,12 @@ Status GetDictionaryTypes(const void* opaque_schema, DictionaryTypeMap* id_to_fi

// Construct a complete Schema from the message. May be expensive for very
// large schemas if you are only interested in a few fields
Status GetSchema(const void* opaque_schema, const DictionaryMemo& dictionary_memo,
std::shared_ptr<Schema>* out);
Status ARROW_EXPORT GetSchema(const void* opaque_schema,
const DictionaryMemo& dictionary_memo, std::shared_ptr<Schema>* out);

Status GetTensorMetadata(const void* opaque_tensor, std::shared_ptr<DataType>* type,
std::vector<int64_t>* shape, std::vector<int64_t>* strides,
std::vector<std::string>* dim_names);
Status ARROW_EXPORT GetTensorMetadata(const void* opaque_tensor,
std::shared_ptr<DataType>* type, std::vector<int64_t>* shape,
std::vector<int64_t>* strides, std::vector<std::string>* dim_names);

class ARROW_EXPORT Message {
public:
Expand Down Expand Up @@ -157,18 +157,19 @@ class ARROW_EXPORT Message {
/// \param[in] file the seekable file interface to read from
/// \param[out] message the message read
/// \return Status success or failure
Status ReadMessage(int64_t offset, int32_t metadata_length, io::RandomAccessFile* file,
std::shared_ptr<Message>* message);
Status ARROW_EXPORT ReadMessage(int64_t offset, int32_t metadata_length,
io::RandomAccessFile* file, std::shared_ptr<Message>* message);

/// Read length-prefixed message with as-yet unknown length. Returns nullptr if
/// there are not enough bytes available or the message length is 0 (e.g. EOS
/// in a stream)
Status ReadMessage(io::InputStream* stream, std::shared_ptr<Message>* message);
Status ARROW_EXPORT ReadMessage(
io::InputStream* stream, std::shared_ptr<Message>* message);

/// Write a serialized message with a length-prefix and padding to an 8-byte offset
///
/// <message_size: int32><message: const void*><padding>
Status WriteMessage(
Status ARROW_EXPORT WriteMessage(
const Buffer& message, io::OutputStream* file, int32_t* message_length);

// Serialize arrow::Schema as a Flatbuffer
Expand All @@ -178,14 +179,14 @@ Status WriteMessage(
// dictionary ids
// \param[out] out the serialized arrow::Buffer
// \return Status outcome
Status WriteSchemaMessage(
Status ARROW_EXPORT WriteSchemaMessage(
const Schema& schema, DictionaryMemo* dictionary_memo, std::shared_ptr<Buffer>* out);

Status WriteRecordBatchMessage(int64_t length, int64_t body_length,
Status ARROW_EXPORT WriteRecordBatchMessage(int64_t length, int64_t body_length,
const std::vector<FieldMetadata>& nodes, const std::vector<BufferMetadata>& buffers,
std::shared_ptr<Buffer>* out);

Status WriteTensorMessage(
Status ARROW_EXPORT WriteTensorMessage(
const Tensor& tensor, int64_t buffer_start_offset, std::shared_ptr<Buffer>* out);

Status WriteDictionaryMessage(int64_t id, int64_t length, int64_t body_length,
Expand Down
18 changes: 9 additions & 9 deletions cpp/src/arrow/ipc/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ namespace ipc {
/// including padding to a 64-byte boundary
/// @param(out) body_length: the size of the contiguous buffer block plus
/// padding bytes
Status WriteRecordBatch(const RecordBatch& batch, int64_t buffer_start_offset,
io::OutputStream* dst, int32_t* metadata_length, int64_t* body_length,
MemoryPool* pool, int max_recursion_depth = kMaxNestingDepth,
Status ARROW_EXPORT WriteRecordBatch(const RecordBatch& batch,
int64_t buffer_start_offset, io::OutputStream* dst, int32_t* metadata_length,
int64_t* body_length, MemoryPool* pool, int max_recursion_depth = kMaxNestingDepth,
bool allow_64bit = false);

// Write Array as a DictionaryBatch message
Expand All @@ -79,7 +79,7 @@ Status WriteDictionary(int64_t dictionary_id, const std::shared_ptr<Array>& dict
// Compute the precise number of bytes needed in a contiguous memory segment to
// write the record batch. This involves generating the complete serialized
// Flatbuffers metadata.
Status GetRecordBatchSize(const RecordBatch& batch, int64_t* size);
Status ARROW_EXPORT GetRecordBatchSize(const RecordBatch& batch, int64_t* size);

class ARROW_EXPORT StreamWriter {
public:
Expand Down Expand Up @@ -122,14 +122,14 @@ class ARROW_EXPORT FileWriter : public StreamWriter {

/// EXPERIMENTAL: Write RecordBatch allowing lengths over INT32_MAX. This data
/// may not be readable by all Arrow implementations
Status WriteLargeRecordBatch(const RecordBatch& batch, int64_t buffer_start_offset,
io::OutputStream* dst, int32_t* metadata_length, int64_t* body_length,
MemoryPool* pool);
Status ARROW_EXPORT WriteLargeRecordBatch(const RecordBatch& batch,
int64_t buffer_start_offset, io::OutputStream* dst, int32_t* metadata_length,
int64_t* body_length, MemoryPool* pool);

/// EXPERIMENTAL: Write arrow::Tensor as a contiguous message
/// <metadata size><metadata><tensor data>
Status WriteTensor(const Tensor& tensor, io::OutputStream* dst, int32_t* metadata_length,
int64_t* body_length);
Status ARROW_EXPORT WriteTensor(const Tensor& tensor, io::OutputStream* dst,
int32_t* metadata_length, int64_t* body_length);

} // namespace ipc
} // namespace arrow
Expand Down
10 changes: 7 additions & 3 deletions cpp/src/arrow/util/bit-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static inline int64_t Ceil2Bytes(int64_t size) {
}

static inline bool GetBit(const uint8_t* bits, int64_t i) {
return static_cast<bool>(bits[i / 8] & kBitmask[i % 8]);
return (bits[i / 8] & kBitmask[i % 8]) != 0;
}

static inline bool BitNotSet(const uint8_t* bits, int64_t i) {
Expand All @@ -68,9 +68,13 @@ static inline void SetBit(uint8_t* bits, int64_t i) {
}

static inline void SetBitTo(uint8_t* bits, int64_t i, bool bit_is_set) {
// See https://graphics.stanford.edu/~seander/bithacks.html
// TODO: speed up. See https://graphics.stanford.edu/~seander/bithacks.html
// "Conditionally set or clear bits without branching"
bits[i / 8] ^= static_cast<uint8_t>(-bit_is_set ^ bits[i / 8]) & kBitmask[i % 8];
if (bit_is_set) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we reverting this here again?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't figure out how to port the bit twiddling code in a way that MSVC was happy about. Since we're only using this function in CopyBitmap right now, I left a TODO and figured we can return to it later

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably also want to start a util-benchmark.cc when working on this so we aren't guessing about what's more performant (the Stanford link claims 5-15% faster without branching, but it would be good to know for sure)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created ARROW-764 to remember to look at this. Merging the PR

SetBit(bits, i);
} else {
ClearBit(bits, i);
}
}

static inline int64_t NextPower2(int64_t n) {
Expand Down
6 changes: 4 additions & 2 deletions python/pyarrow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@

from pyarrow.filesystem import Filesystem, HdfsClient, LocalFilesystem
from pyarrow.io import (HdfsFile, NativeFile, PythonFileInterface,
Buffer, InMemoryOutputStream, BufferReader,
frombuffer)
Buffer, BufferReader, InMemoryOutputStream,
MemoryMappedFile, memory_map,
frombuffer, read_tensor, write_tensor,
memory_map, create_memory_map)

from pyarrow.ipc import FileReader, FileWriter, StreamReader, StreamWriter

Expand Down
Loading