Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CFD9A18C5B for ; Fri, 4 Sep 2015 16:27:15 +0000 (UTC) Received: (qmail 26148 invoked by uid 500); 4 Sep 2015 16:27:15 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 26094 invoked by uid 500); 4 Sep 2015 16:27:15 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 26070 invoked by uid 99); 4 Sep 2015 16:27:15 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Sep 2015 16:27:15 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 71DA9DF9C4; Fri, 4 Sep 2015 16:27:15 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Fri, 04 Sep 2015 16:27:16 -0000 Message-Id: In-Reply-To: <32fe56bbbbab49f3a70cddb00c220582@git.apache.org> References: <32fe56bbbbab49f3a70cddb00c220582@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [02/55] [abbrv] ignite git commit: IGNITE-1364: Moved CPP module to Ignite. http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/ignite_environment.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/ignite_environment.cpp b/modules/platform/src/main/cpp/core/src/impl/ignite_environment.cpp new file mode 100644 index 0000000..b20c543 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/ignite_environment.cpp @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/portable/portable_reader_impl.h" +#include "ignite/impl/ignite_environment.h" +#include "ignite/portable/portable.h" + +using namespace ignite::common::concurrent; +using namespace ignite::common::java; +using namespace ignite::impl::interop; +using namespace ignite::impl::portable; +using namespace ignite::portable; + +namespace ignite +{ + namespace impl + { + /** + * OnStart callback. + * + * @param target Target environment. + * @param memPtr Memory pointer. + */ + void IGNITE_CALL OnStart(void* target, long long memPtr) + { + SharedPointer* ptr = static_cast*>(target); + + ptr->Get()->OnStartCallback(memPtr); + } + + /** + * OnStop callback. + * + * @param target Target environment. + */ + void IGNITE_CALL OnStop(void* target) + { + SharedPointer* ptr = static_cast*>(target); + + delete ptr; + } + + IgniteEnvironment::IgniteEnvironment() : ctx(SharedPointer()), latch(new SingleLatch), name(NULL), + metaMgr(new PortableMetadataManager()) + { + // No-op. + } + + IgniteEnvironment::~IgniteEnvironment() + { + delete latch; + + if (name) + delete name; + + delete metaMgr; + } + + JniHandlers IgniteEnvironment::GetJniHandlers(SharedPointer* target) + { + JniHandlers hnds = JniHandlers(); + + hnds.target = target; + + hnds.onStart = OnStart; + hnds.onStop = OnStop; + + hnds.error = NULL; + + return hnds; + } + + void IgniteEnvironment::Initialize(SharedPointer ctx) + { + this->ctx = ctx; + + latch->CountDown(); + } + + char* IgniteEnvironment::InstanceName() + { + return name; + } + + JniContext* IgniteEnvironment::Context() + { + return ctx.Get(); + } + + SharedPointer IgniteEnvironment::AllocateMemory() + { + SharedPointer ptr(new InteropUnpooledMemory(1024)); + + return ptr; + } + + SharedPointer IgniteEnvironment::AllocateMemory(int32_t cap) + { + SharedPointer ptr(new InteropUnpooledMemory(cap)); + + return ptr; + } + + SharedPointer IgniteEnvironment::GetMemory(int64_t memPtr) + { + int8_t* memPtr0 = reinterpret_cast(memPtr); + + int32_t flags = InteropMemory::Flags(memPtr0); + + if (InteropMemory::IsExternal(flags)) + { + SharedPointer ptr(new InteropExternalMemory(memPtr0)); + + return ptr; + } + else + { + SharedPointer ptr(new InteropUnpooledMemory(memPtr0)); + + return ptr; + } + } + + PortableMetadataManager* IgniteEnvironment::GetMetadataManager() + { + return metaMgr; + } + + void IgniteEnvironment::OnStartCallback(long long memPtr) + { + InteropExternalMemory mem(reinterpret_cast(memPtr)); + InteropInputStream stream(&mem); + + PortableReaderImpl reader(&stream); + + int32_t nameLen = reader.ReadString(NULL, 0); + + if (nameLen >= 0) + { + name = new char[nameLen + 1]; + reader.ReadString(name, nameLen + 1); + } + else + name = NULL; + } + } +} + + + + + http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/ignite_impl.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/ignite_impl.cpp b/modules/platform/src/main/cpp/core/src/impl/ignite_impl.cpp new file mode 100644 index 0000000..1aad309 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/ignite_impl.cpp @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/ignite_impl.h" + +using namespace ignite::common::concurrent; +using namespace ignite::common::java; + +namespace ignite +{ + namespace impl + { + IgniteImpl::IgniteImpl(SharedPointer env, jobject javaRef) : env(env), javaRef(javaRef) + { + // No-op. + } + + IgniteImpl::~IgniteImpl() + { + JniContext::Release(javaRef); + } + + char* IgniteImpl::GetName() + { + return env.Get()->InstanceName(); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/interop/interop_input_stream.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/interop/interop_input_stream.cpp b/modules/platform/src/main/cpp/core/src/impl/interop/interop_input_stream.cpp new file mode 100644 index 0000000..72340ee --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/interop/interop_input_stream.cpp @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "ignite/impl/interop/interop_input_stream.h" +#include "ignite/ignite_error.h" + +/** + * Common macro to read a single value. + */ +#define IGNITE_INTEROP_IN_READ(type, len) { \ + EnsureEnoughData(len); \ + type res = *reinterpret_cast(data + pos); \ + Shift(len); \ + return res; \ +} + +/** + * Common macro to read an array. + */ +#define IGNITE_INTEROP_IN_READ_ARRAY(len, shift) { \ + CopyAndShift(reinterpret_cast(res), 0, len << shift); \ +} + +namespace ignite +{ + namespace impl + { + namespace interop + { + union PortableInt32Float + { + int32_t i; + float f; + }; + + union PortableInt64Double + { + int64_t i; + double d; + }; + + InteropInputStream::InteropInputStream(InteropMemory* mem) + { + this->mem = mem; + + data = mem->Data(); + len = mem->Length(); + pos = 0; + } + + int8_t InteropInputStream::ReadInt8() + { + IGNITE_INTEROP_IN_READ(int8_t, 1); + } + + void InteropInputStream::ReadInt8Array(int8_t* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 0); + } + + bool InteropInputStream::ReadBool() + { + return ReadInt8() == 1; + } + + void InteropInputStream::ReadBoolArray(bool* const res, const int32_t len) + { + for (int i = 0; i < len; i++) + *(res + i) = ReadBool(); + } + + int16_t InteropInputStream::ReadInt16() + { + IGNITE_INTEROP_IN_READ(int16_t, 2); + } + + void InteropInputStream::ReadInt16Array(int16_t* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 1); + } + + uint16_t InteropInputStream::ReadUInt16() + { + IGNITE_INTEROP_IN_READ(uint16_t, 2); + } + + void InteropInputStream::ReadUInt16Array(uint16_t* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 1); + } + + int32_t InteropInputStream::ReadInt32() + { + IGNITE_INTEROP_IN_READ(int32_t, 4); + } + + int32_t InteropInputStream::ReadInt32(int32_t pos) + { + int delta = pos + 4 - this->pos; + + if (delta > 0) + EnsureEnoughData(delta); + + return *reinterpret_cast(data + pos); + } + + void InteropInputStream::ReadInt32Array(int32_t* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 2); + } + + int64_t InteropInputStream::ReadInt64() + { + IGNITE_INTEROP_IN_READ(int64_t, 8); + } + + void InteropInputStream::ReadInt64Array(int64_t* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 3); + } + + float InteropInputStream::ReadFloat() + { + PortableInt32Float u; + + u.i = ReadInt32(); + + return u.f; + } + + void InteropInputStream::ReadFloatArray(float* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 2); + } + + double InteropInputStream::ReadDouble() + { + PortableInt64Double u; + + u.i = ReadInt64(); + + return u.d; + } + + void InteropInputStream::ReadDoubleArray(double* const res, const int32_t len) + { + IGNITE_INTEROP_IN_READ_ARRAY(len, 3); + } + + int32_t InteropInputStream::Remaining() + { + return len - pos; + } + + int32_t InteropInputStream::Position() + { + return pos; + } + + void InteropInputStream::Position(int32_t pos) + { + if (pos > len) { + IGNITE_ERROR_FORMATTED_3(IgniteError::IGNITE_ERR_MEMORY, "Requested input stream position is out of bounds", + "memPtr", mem->PointerLong(), "len", len, "pos", pos); + } + + this->pos = pos; + } + + void InteropInputStream::Synchronize() + { + data = mem->Data(); + len = mem->Length(); + } + + void InteropInputStream::EnsureEnoughData(int32_t cnt) + { + if (len - pos < cnt) { + IGNITE_ERROR_FORMATTED_4(IgniteError::IGNITE_ERR_MEMORY, "Not enough data in the stream", + "memPtr", mem->PointerLong(), "len", len, "pos", pos, "requested", cnt); + } + } + + void InteropInputStream::CopyAndShift(int8_t* dest, int32_t off, int32_t cnt) + { + EnsureEnoughData(cnt); + + memcpy(dest + off, data + pos, cnt); + + Shift(cnt); + } + + void InteropInputStream::Shift(int32_t cnt) + { + pos += cnt; + } + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/interop/interop_memory.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/interop/interop_memory.cpp b/modules/platform/src/main/cpp/core/src/impl/interop/interop_memory.cpp new file mode 100644 index 0000000..05ba8b6 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/interop/interop_memory.cpp @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "ignite/impl/interop/interop_memory.h" +#include "ignite/ignite_error.h" + +using namespace ignite::common::java; + +namespace ignite +{ + namespace impl + { + namespace interop + { + int8_t* InteropMemory::Data(int8_t* memPtr) + { + return reinterpret_cast(*reinterpret_cast(memPtr)); + } + + void InteropMemory::Data(int8_t* memPtr, void* ptr) + { + *reinterpret_cast(memPtr) = reinterpret_cast(ptr); + } + + int32_t InteropMemory::Capacity(int8_t* memPtr) + { + return *reinterpret_cast(memPtr + IGNITE_MEM_HDR_OFF_CAP); + } + + void InteropMemory::Capacity(int8_t* memPtr, int32_t val) + { + *reinterpret_cast(memPtr + IGNITE_MEM_HDR_OFF_CAP) = val; + } + + int32_t InteropMemory::Length(int8_t* memPtr) + { + return *reinterpret_cast(memPtr + IGNITE_MEM_HDR_OFF_LEN); + } + + void InteropMemory::Length(int8_t* memPtr, int32_t val) + { + *reinterpret_cast(memPtr + IGNITE_MEM_HDR_OFF_LEN) = val; + } + + int32_t InteropMemory::Flags(int8_t* memPtr) + { + return *reinterpret_cast(memPtr + IGNITE_MEM_HDR_OFF_FLAGS); + } + + void InteropMemory::Flags(int8_t* memPtr, int32_t val) + { + *reinterpret_cast(memPtr + IGNITE_MEM_HDR_OFF_FLAGS) = val; + } + + bool InteropMemory::IsExternal(int8_t* memPtr) + { + return IsExternal(Flags(memPtr)); + } + + bool InteropMemory::IsExternal(int32_t flags) + { + return (flags & IGNITE_MEM_FLAG_EXT) != IGNITE_MEM_FLAG_EXT; + } + + bool InteropMemory::IsPooled(int8_t* memPtr) + { + return IsPooled(Flags(memPtr)); + } + + bool InteropMemory::IsPooled(int32_t flags) + { + return (flags & IGNITE_MEM_FLAG_POOLED) != 0; + } + + bool InteropMemory::IsAcquired(int8_t* memPtr) + { + return IsAcquired(Flags(memPtr)); + } + + bool InteropMemory::IsAcquired(int32_t flags) + { + return (flags & IGNITE_MEM_FLAG_ACQUIRED) != 0; + } + + int8_t* InteropMemory::Pointer() + { + return memPtr; + } + + int64_t InteropMemory::PointerLong() + { + return reinterpret_cast(memPtr); + } + + int8_t* InteropMemory::Data() + { + return Data(memPtr); + } + + int32_t InteropMemory::Capacity() + { + return Capacity(memPtr); + } + + int32_t InteropMemory::Length() + { + return Length(memPtr); + } + + void InteropMemory::Length(int32_t val) + { + Length(memPtr, val); + } + + InteropUnpooledMemory::InteropUnpooledMemory(int32_t cap) + { + memPtr = static_cast(malloc(IGNITE_MEM_HDR_LEN)); + + Data(memPtr, malloc(cap)); + Capacity(memPtr, cap); + Length(memPtr, 0); + Flags(memPtr, IGNITE_MEM_FLAG_EXT); + + owning = true; + } + + InteropUnpooledMemory::InteropUnpooledMemory(int8_t* memPtr) + { + this->memPtr = memPtr; + this->owning = false; + } + + InteropUnpooledMemory::~InteropUnpooledMemory() + { + if (owning) { + free(Data()); + free(memPtr); + } + } + + void InteropUnpooledMemory::Reallocate(int32_t cap) + { + int doubledCap = Capacity() << 1; + + if (doubledCap > cap) + cap = doubledCap; + + Data(memPtr, realloc(Data(memPtr), cap)); + Capacity(memPtr, cap); + } + + InteropExternalMemory::InteropExternalMemory(int8_t* memPtr) + { + this->memPtr = memPtr; + } + + void InteropExternalMemory::Reallocate(int32_t cap) + { + if (JniContext::Reallocate(reinterpret_cast(memPtr), cap) == -1) { + IGNITE_ERROR_FORMATTED_2(IgniteError::IGNITE_ERR_MEMORY, "Failed to reallocate external memory", + "memPtr", PointerLong(), "requestedCapacity", cap) + } + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/interop/interop_output_stream.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/interop/interop_output_stream.cpp b/modules/platform/src/main/cpp/core/src/impl/interop/interop_output_stream.cpp new file mode 100644 index 0000000..ecdfd42 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/interop/interop_output_stream.cpp @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "ignite/impl/interop/interop_output_stream.h" +#include "ignite/ignite_error.h" + +/** + * Common macro to write a single value. + */ +#define IGNITE_INTEROP_OUT_WRITE(val, type, len) { \ + EnsureCapacity(pos + len); \ + *reinterpret_cast(data + pos) = val; \ + Shift(len); \ +} + +/** + * Common macro to write an array. + */ +#define IGNITE_INTEROP_OUT_WRITE_ARRAY(val, len) { \ + CopyAndShift(reinterpret_cast(val), 0, len); \ +} + +namespace ignite +{ + namespace impl + { + namespace interop + { + union PortableFloatInt32 + { + float f; + int32_t i; + }; + + union PortableDoubleInt64 + { + double d; + int64_t i; + }; + + InteropOutputStream::InteropOutputStream(InteropMemory* mem) + { + this->mem = mem; + + data = mem->Data(); + cap = mem->Capacity(); + pos = 0; + } + + void InteropOutputStream::WriteInt8(const int8_t val) + { + IGNITE_INTEROP_OUT_WRITE(val, int8_t, 1); + } + + void InteropOutputStream::WriteInt8(const int8_t val, const int32_t pos) + { + EnsureCapacity(pos + 1); + + *(data + pos) = val; + } + + void InteropOutputStream::WriteInt8Array(const int8_t* val, const int32_t len) + { + IGNITE_INTEROP_OUT_WRITE_ARRAY(val, len); + } + + void InteropOutputStream::WriteBool(const bool val) + { + WriteInt8(val ? 1 : 0); + } + + void InteropOutputStream::WriteBoolArray(const bool* val, const int32_t len) + { + for (int i = 0; i < len; i++) + WriteBool(*(val + i)); + } + + void InteropOutputStream::WriteInt16(const int16_t val) + { + IGNITE_INTEROP_OUT_WRITE(val, int16_t, 2); + } + + void InteropOutputStream::WriteInt16Array(const int16_t* val, const int32_t len) + { + IGNITE_INTEROP_OUT_WRITE_ARRAY(val, len << 1); + } + + void InteropOutputStream::WriteUInt16(const uint16_t val) + { + IGNITE_INTEROP_OUT_WRITE(val, uint16_t, 2); + } + + void InteropOutputStream::WriteUInt16Array(const uint16_t* val, const int32_t len) + { + IGNITE_INTEROP_OUT_WRITE_ARRAY(val, len << 1); + } + + void InteropOutputStream::WriteInt32(const int32_t val) + { + IGNITE_INTEROP_OUT_WRITE(val, int32_t, 4); + } + + void InteropOutputStream::WriteInt32(const int32_t pos, const int32_t val) + { + EnsureCapacity(pos + 4); + + *reinterpret_cast(data + pos) = val; + } + + void InteropOutputStream::WriteInt32Array(const int32_t* val, const int32_t len) + { + IGNITE_INTEROP_OUT_WRITE_ARRAY(val, len << 2); + } + + void InteropOutputStream::WriteInt64(const int64_t val) + { + IGNITE_INTEROP_OUT_WRITE(val, int64_t, 8); + } + + void InteropOutputStream::WriteInt64Array(const int64_t* val, const int32_t len) + { + IGNITE_INTEROP_OUT_WRITE_ARRAY(val, len << 3); + } + + void InteropOutputStream::WriteFloat(const float val) + { + PortableFloatInt32 u; + + u.f = val; + + WriteInt32(u.i); + } + + void InteropOutputStream::WriteFloatArray(const float* val, const int32_t len) + { + for (int i = 0; i < len; i++) + WriteFloat(*(val + i)); + } + + void InteropOutputStream::WriteDouble(const double val) + { + PortableDoubleInt64 u; + + u.d = val; + + WriteInt64(u.i); + } + + void InteropOutputStream::WriteDoubleArray(const double* val, const int32_t len) + { + for (int i = 0; i < len; i++) + WriteDouble(*(val + i)); + } + + int32_t InteropOutputStream::Position() + { + return pos; + } + + void InteropOutputStream::Position(const int32_t val) + { + EnsureCapacity(val); + + pos = val; + } + + void InteropOutputStream::Synchronize() + { + mem->Length(pos); + } + + void InteropOutputStream::EnsureCapacity(int32_t reqCap) { + if (reqCap > cap) { + int newCap = cap << 1; + + if (newCap < reqCap) + newCap = reqCap; + + mem->Reallocate(newCap); + data = mem->Data(); + cap = newCap; + } + } + + void InteropOutputStream::Shift(int32_t cnt) { + pos += cnt; + } + + void InteropOutputStream::CopyAndShift(const int8_t* src, int32_t off, int32_t len) { + EnsureCapacity(pos + len); + + memcpy(data + pos, src + off, len); + + Shift(len); + } + } + } +} + http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_handler.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_handler.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_handler.cpp new file mode 100644 index 0000000..5ca91dc --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_handler.cpp @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/portable/portable_metadata_handler.h" + +using namespace ignite::common::concurrent; + +namespace ignite +{ + namespace impl + { + namespace portable + { + PortableMetadataHandler::PortableMetadataHandler(SPSnap snap) : snap(snap), fieldIds(NULL), fields(NULL) + { + // No-op. + } + + PortableMetadataHandler::~PortableMetadataHandler() + { + if (fieldIds) + delete fieldIds; + + if (fields) + delete fields; + } + + void PortableMetadataHandler::OnFieldWritten(int32_t fieldId, std::string fieldName, int32_t fieldTypeId) + { + if (!snap.Get() || !snap.Get()->ContainsFieldId(fieldId)) + { + if (!HasDifference()) + { + fieldIds = new std::set(); + fields = new std::map(); + } + + fieldIds->insert(fieldId); + (*fields)[fieldName] = fieldTypeId; + } + } + + SPSnap PortableMetadataHandler::GetSnapshot() + { + return snap; + } + + bool PortableMetadataHandler::HasDifference() + { + return fieldIds ? true : false; + } + + std::set* PortableMetadataHandler::GetFieldIds() + { + return fieldIds; + } + + std::map* PortableMetadataHandler::GetFields() + { + return fields; + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_manager.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_manager.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_manager.cpp new file mode 100644 index 0000000..63e92a9 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_manager.cpp @@ -0,0 +1,201 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "ignite/impl/portable/portable_metadata_manager.h" + +using namespace ignite::common::concurrent; + +namespace ignite +{ + namespace impl + { + namespace portable + { + PortableMetadataManager::PortableMetadataManager() : + snapshots(SharedPointer>(new std::map)), + pending(new std::vector()), + cs(new CriticalSection()), + pendingVer(0), ver(0) + { + // No-op. + } + + PortableMetadataManager::~PortableMetadataManager() + { + pending->erase(pending->begin(), pending->end()); + + delete pending; + delete cs; + } + + SharedPointer PortableMetadataManager::GetHandler(int32_t typeId) + { + SharedPointer> snapshots0 = snapshots; + + SPSnap snapshot = (*snapshots0.Get())[typeId]; + + return SharedPointer(new PortableMetadataHandler(snapshot)); + } + + void PortableMetadataManager::SubmitHandler(std::string typeName, int32_t typeId, + PortableMetadataHandler* hnd) + { + Snap* snap = hnd->GetSnapshot().Get(); + + // If this is the very first write of a class or difference exists, + // we need to enqueue it for write. + if (!snap || hnd->HasDifference()) + { + std::set* newFieldIds = new std::set(); + std::map* newFields = new std::map(); + + CopyFields(snap, newFieldIds, newFields); + + if (hnd->HasDifference()) + { + std::set* diffFieldIds = hnd->GetFieldIds(); + std::map* diffFields = hnd->GetFields(); + + for (std::set::iterator it = diffFieldIds->begin(); it != diffFieldIds->end(); ++it) + newFieldIds->insert(*it); + + for (std::map::iterator it = diffFields->begin(); it != diffFields->end(); ++it) + (*newFields)[it->first] = it->second; + } + + Snap* diffSnap = new Snap(typeName, typeId, newFieldIds, newFields); + + cs->Enter(); + + pending->push_back(SPSnap(diffSnap)); + + pendingVer++; + + cs->Leave(); + } + } + + int32_t PortableMetadataManager::GetVersion() + { + Memory::Fence(); + + return ver; + } + + bool PortableMetadataManager::IsUpdatedSince(int32_t oldVer) + { + Memory::Fence(); + + return pendingVer > oldVer; + } + + bool PortableMetadataManager::ProcessPendingUpdates(PortableMetadataUpdater* updater, IgniteError* err) + { + bool success = true; // Optimistically assume that all will be fine. + + cs->Enter(); + + for (std::vector::iterator it = pending->begin(); it != pending->end(); ++it) + { + Snap* pendingSnap = (*it).Get(); + + if (updater->Update(pendingSnap, err)) + { + // Perform copy-on-write update of snapshot collection. + std::map* newSnapshots = new std::map(); + + bool snapshotFound = false; + + for (std::map::iterator snapIt = snapshots.Get()->begin(); + snapIt != snapshots.Get()->end(); ++snapIt) + { + int32_t curTypeId = snapIt->first; + Snap* curSnap = snapIt->second.Get(); + + if (pendingSnap->GetTypeId() == curTypeId) + { + // Have to create snapshot with updated fields. + std::set* newFieldIds = new std::set(); + std::map* newFields = new std::map(); + + // Add old fields. + CopyFields(curSnap, newFieldIds, newFields); + + // Add new fields. + CopyFields(pendingSnap, newFieldIds, newFields); + + // Create new snapshot. + Snap* newSnap = new Snap(pendingSnap->GetTypeName(), pendingSnap->GetTypeId(), + newFieldIds, newFields); + + (*newSnapshots)[curTypeId] = SPSnap(newSnap); + + snapshotFound = true; + } + else + (*newSnapshots)[curTypeId] = snapIt->second; // Just transfer exising snapshot. + } + + // Handle situation when completely new snapshot is found. + if (!snapshotFound) + (*newSnapshots)[pendingSnap->GetTypeId()] = *it; + + snapshots = SharedPointer>(newSnapshots); + } + else + { + // Stop as we cannot move further. + success = false; + + break; + } + } + + if (success) + { + pending->erase(pending->begin(), pending->end()); + + ver = pendingVer; + } + + cs->Leave(); + + return success; + } + + void PortableMetadataManager::CopyFields(Snap* snap, std::set* fieldIds, + std::map* fields) + { + if (snap && snap->HasFields()) + { + std::set* snapFieldIds = snap->GetFieldIds(); + std::map* snapFields = snap->GetFields(); + + for (std::set::iterator oldIt = snapFieldIds->begin(); + oldIt != snapFieldIds->end(); ++oldIt) + fieldIds->insert(*oldIt); + + for (std::map::iterator newFieldsIt = snapFields->begin(); + newFieldsIt != snapFields->end(); ++newFieldsIt) + (*fields)[newFieldsIt->first] = newFieldsIt->second; + } + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_snapshot.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_snapshot.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_snapshot.cpp new file mode 100644 index 0000000..6ce5ab5 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_snapshot.cpp @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/portable/portable_metadata_snapshot.h" + +namespace ignite +{ + namespace impl + { + namespace portable + { + PortableMetadataSnapshot::PortableMetadataSnapshot(std::string typeName, int32_t typeId, + std::set* fieldIds, std::map* fields) : + typeName(typeName), typeId(typeId), fieldIds(fieldIds), fields(fields) + { + // No-op. + } + + PortableMetadataSnapshot::~PortableMetadataSnapshot() + { + delete fieldIds; + delete fields; + } + + bool PortableMetadataSnapshot::ContainsFieldId(int32_t fieldId) + { + return fieldIds && fieldIds->count(fieldId) == 1; + } + + std::string PortableMetadataSnapshot::GetTypeName() + { + return typeName; + } + + int32_t PortableMetadataSnapshot::GetTypeId() + { + return typeId; + } + + bool PortableMetadataSnapshot::HasFields() + { + return !fieldIds->empty(); + } + + std::set* PortableMetadataSnapshot::GetFieldIds() + { + return fieldIds; + } + + std::map* PortableMetadataSnapshot::GetFields() + { + return fields; + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater.cpp new file mode 100644 index 0000000..81c96d7 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater.cpp @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/portable/portable_metadata_updater.h" + +namespace ignite +{ + namespace impl + { + namespace portable + { + PortableMetadataUpdater::~PortableMetadataUpdater() + { + // No-op. + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater_impl.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater_impl.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater_impl.cpp new file mode 100644 index 0000000..07a1758 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_metadata_updater_impl.cpp @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/portable/portable_metadata_updater_impl.h" +#include "ignite/impl/interop/interop_output_stream.h" +#include "ignite/impl/portable/portable_writer_impl.h" +#include "ignite/portable/portable_raw_writer.h" + +using namespace ignite::common::concurrent; +using namespace ignite::common::java; +using namespace ignite::impl; +using namespace ignite::impl::interop; +using namespace ignite::portable; + +namespace ignite +{ + namespace impl + { + namespace portable + { + /** Operation: Clear. */ + const int32_t OP_METADATA = -1; + + PortableMetadataUpdaterImpl::PortableMetadataUpdaterImpl(SharedPointer env, + jobject javaRef) : env(env), javaRef(javaRef) + { + // No-op. + } + + PortableMetadataUpdaterImpl::~PortableMetadataUpdaterImpl() + { + // No-op. + } + + bool PortableMetadataUpdaterImpl::Update(Snap* snap, IgniteError* err) + { + JniErrorInfo jniErr; + + SharedPointer mem = env.Get()->AllocateMemory(); + + InteropOutputStream out(mem.Get()); + PortableWriterImpl writer(&out, NULL); + PortableRawWriter rawWriter(&writer); + + // We always pass only one meta at a time in current implementation for simplicity. + rawWriter.WriteInt32(1); + + rawWriter.WriteInt32(snap->GetTypeId()); + rawWriter.WriteString(snap->GetTypeName()); + rawWriter.WriteString(NULL); // Affinity key is not supported for now. + + if (snap->HasFields()) + { + std::map* fields = snap->GetFields(); + + rawWriter.WriteInt32(static_cast(fields->size())); + + for (std::map::iterator it = fields->begin(); it != fields->end(); ++it) + { + rawWriter.WriteString(it->first); + rawWriter.WriteInt32(it->second); + } + } + else + rawWriter.WriteInt32(0); + + out.Synchronize(); + + long long res = env.Get()->Context()->TargetInStreamOutLong(javaRef, OP_METADATA, mem.Get()->PointerLong(), &jniErr); + + IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err); + + if (jniErr.code == IGNITE_JNI_ERR_SUCCESS) + return res == 1; + else + return false; + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_reader_impl.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_reader_impl.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_reader_impl.cpp new file mode 100644 index 0000000..753ec25 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_reader_impl.cpp @@ -0,0 +1,683 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/interop/interop.h" +#include "ignite/impl/portable/portable_common.h" +#include "ignite/impl/portable/portable_id_resolver.h" +#include "ignite/impl/portable/portable_reader_impl.h" +#include "ignite/impl/portable/portable_utils.h" +#include "ignite/portable/portable_type.h" +#include "ignite/ignite_error.h" + +using namespace ignite::impl::interop; +using namespace ignite::impl::portable; +using namespace ignite::portable; + +namespace ignite +{ + namespace impl + { + namespace portable + { + PortableReaderImpl::PortableReaderImpl(InteropInputStream* stream, PortableIdResolver* idRslvr, + int32_t pos, bool usrType, int32_t typeId, int32_t hashCode, int32_t len, int32_t rawOff) : + stream(stream), idRslvr(idRslvr), pos(pos), usrType(usrType), typeId(typeId), + hashCode(hashCode), len(len), rawOff(rawOff), rawMode(false), + elemIdGen(0), elemId(0), elemCnt(-1), elemRead(0) + { + // No-op. + } + + PortableReaderImpl::PortableReaderImpl(InteropInputStream* stream) : + stream(stream), idRslvr(NULL), pos(0), usrType(false), typeId(0), hashCode(0), + len(0), rawOff(0), rawMode(true), + elemIdGen(0), elemId(0), elemCnt(-1), elemRead(0) + { + // No-op. + } + + int8_t PortableReaderImpl::ReadInt8() + { + return ReadRaw(PortableUtils::ReadInt8); + } + + int32_t PortableReaderImpl::ReadInt8Array(int8_t* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadInt8Array, IGNITE_TYPE_ARRAY_BYTE); + } + + int8_t PortableReaderImpl::ReadInt8(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadInt8, IGNITE_TYPE_BYTE, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadInt8Array(const char* fieldName, int8_t* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadInt8Array, IGNITE_TYPE_ARRAY_BYTE); + } + + bool PortableReaderImpl::ReadBool() + { + return ReadRaw(PortableUtils::ReadBool); + } + + int32_t PortableReaderImpl::ReadBoolArray(bool* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadBoolArray, IGNITE_TYPE_ARRAY_BOOL); + } + + bool PortableReaderImpl::ReadBool(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadBool, IGNITE_TYPE_BOOL, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadBoolArray(const char* fieldName, bool* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadBoolArray, IGNITE_TYPE_ARRAY_BOOL); + } + + int16_t PortableReaderImpl::ReadInt16() + { + return ReadRaw(PortableUtils::ReadInt16); + } + + int32_t PortableReaderImpl::ReadInt16Array(int16_t* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadInt16Array, IGNITE_TYPE_ARRAY_SHORT); + } + + int16_t PortableReaderImpl::ReadInt16(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadInt16, IGNITE_TYPE_SHORT, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadInt16Array(const char* fieldName, int16_t* res, const int32_t len) + { + return ReadArray(fieldName, res, len, PortableUtils::ReadInt16Array, IGNITE_TYPE_ARRAY_SHORT); + } + + uint16_t PortableReaderImpl::ReadUInt16() + { + return ReadRaw(PortableUtils::ReadUInt16); + } + + int32_t PortableReaderImpl::ReadUInt16Array(uint16_t* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadUInt16Array, IGNITE_TYPE_ARRAY_CHAR); + } + + uint16_t PortableReaderImpl::ReadUInt16(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadUInt16, IGNITE_TYPE_CHAR, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadUInt16Array(const char* fieldName, uint16_t* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadUInt16Array, IGNITE_TYPE_ARRAY_CHAR); + } + + int32_t PortableReaderImpl::ReadInt32() + { + return ReadRaw(PortableUtils::ReadInt32); + } + + int32_t PortableReaderImpl::ReadInt32Array(int32_t* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadInt32Array, IGNITE_TYPE_ARRAY_INT); + } + + int32_t PortableReaderImpl::ReadInt32(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadInt32, IGNITE_TYPE_INT, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadInt32Array(const char* fieldName, int32_t* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadInt32Array, IGNITE_TYPE_ARRAY_INT); + } + + int64_t PortableReaderImpl::ReadInt64() + { + return ReadRaw(PortableUtils::ReadInt64); + } + + int32_t PortableReaderImpl::ReadInt64Array(int64_t* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadInt64Array, IGNITE_TYPE_ARRAY_LONG); + } + + int64_t PortableReaderImpl::ReadInt64(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadInt64, IGNITE_TYPE_LONG, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadInt64Array(const char* fieldName, int64_t* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadInt64Array, IGNITE_TYPE_ARRAY_LONG); + } + + float PortableReaderImpl::ReadFloat() + { + return ReadRaw(PortableUtils::ReadFloat); + } + + int32_t PortableReaderImpl::ReadFloatArray(float* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadFloatArray, IGNITE_TYPE_ARRAY_FLOAT); + } + + float PortableReaderImpl::ReadFloat(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadFloat, IGNITE_TYPE_FLOAT, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadFloatArray(const char* fieldName, float* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadFloatArray, IGNITE_TYPE_ARRAY_FLOAT); + } + + double PortableReaderImpl::ReadDouble() + { + return ReadRaw(PortableUtils::ReadDouble); + } + + int32_t PortableReaderImpl::ReadDoubleArray(double* res, const int32_t len) + { + return ReadRawArray(res, len, PortableUtils::ReadDoubleArray, IGNITE_TYPE_ARRAY_DOUBLE); + } + + double PortableReaderImpl::ReadDouble(const char* fieldName) + { + return Read(fieldName, PortableUtils::ReadDouble, IGNITE_TYPE_DOUBLE, static_cast(0)); + } + + int32_t PortableReaderImpl::ReadDoubleArray(const char* fieldName, double* res, const int32_t len) + { + return ReadArray(fieldName, res, len,PortableUtils::ReadDoubleArray, IGNITE_TYPE_ARRAY_DOUBLE); + } + + Guid PortableReaderImpl::ReadGuid() + { + CheckRawMode(true); + CheckSingleMode(true); + + return ReadNullable(stream, PortableUtils::ReadGuid, IGNITE_TYPE_UUID); + } + + int32_t PortableReaderImpl::ReadGuidArray(Guid* res, const int32_t len) + { + CheckRawMode(true); + CheckSingleMode(true); + + return ReadArrayInternal(res, len, stream, ReadGuidArrayInternal, IGNITE_TYPE_ARRAY_UUID); + } + + Guid PortableReaderImpl::ReadGuid(const char* fieldName) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) + return ReadNullable(stream, PortableUtils::ReadGuid, IGNITE_TYPE_UUID); + + return Guid(); + } + + int32_t PortableReaderImpl::ReadGuidArray(const char* fieldName, Guid* res, const int32_t len) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t pos = stream->Position(); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) { + int32_t realLen = ReadArrayInternal(res, len, stream, ReadGuidArrayInternal, IGNITE_TYPE_ARRAY_UUID); + + // If actual read didn't occur return to initial position so that we do not perform + // N jumps to find the field again, where N is total amount of fields. + if (realLen != -1 && (!res || realLen > len)) + stream->Position(pos); + + return realLen; + } + + return -1; + } + + void PortableReaderImpl::ReadGuidArrayInternal(InteropInputStream* stream, Guid* res, const int32_t len) + { + for (int i = 0; i < len; i++) + *(res + i) = ReadNullable(stream, PortableUtils::ReadGuid, IGNITE_TYPE_UUID); + } + + int32_t PortableReaderImpl::ReadString(char* res, const int32_t len) + { + CheckRawMode(true); + CheckSingleMode(true); + + return ReadStringInternal(res, len); + } + + int32_t PortableReaderImpl::ReadString(const char* fieldName, char* res, const int32_t len) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t pos = stream->Position(); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) { + int32_t realLen = ReadStringInternal(res, len); + + // If actual read didn't occur return to initial position so that we do not perform + // N jumps to find the field again, where N is total amount of fields. + if (realLen != -1 && (!res || realLen > len)) + stream->Position(pos); + + return realLen; + } + + return -1; + } + + int32_t PortableReaderImpl::ReadStringArray(int32_t* size) + { + return StartContainerSession(true, IGNITE_TYPE_ARRAY_STRING, size); + } + + int32_t PortableReaderImpl::ReadStringArray(const char* fieldName, int32_t* size) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) + return StartContainerSession(false, IGNITE_TYPE_ARRAY_STRING, size); + else { + *size = -1; + + return ++elemIdGen; + } + } + + int32_t PortableReaderImpl::ReadStringElement(int32_t id, char* res, const int32_t len) + { + CheckSession(id); + + int32_t posBefore = stream->Position(); + + int32_t realLen = ReadStringInternal(res, len); + + int32_t posAfter = stream->Position(); + + if (posAfter > posBefore && ++elemRead == elemCnt) { + elemId = 0; + elemCnt = -1; + elemRead = 0; + } + + return realLen; + } + + int32_t PortableReaderImpl::ReadStringInternal(char* res, const int32_t len) + { + int8_t hdr = stream->ReadInt8(); + + if (hdr == IGNITE_TYPE_STRING) { + bool utf8Mode = stream->ReadBool(); + int32_t realLen = stream->ReadInt32(); + + if (res && len >= realLen) { + if (utf8Mode) + { + for (int i = 0; i < realLen; i++) + *(res + i) = static_cast(stream->ReadInt8()); + } + else + { + for (int i = 0; i < realLen; i++) + *(res + i) = static_cast(stream->ReadUInt16()); + } + + if (len > realLen) + *(res + realLen) = 0; // Set NULL terminator if possible. + } + else + stream->Position(stream->Position() - 6); + + return realLen; + } + else if (hdr != IGNITE_HDR_NULL) + ThrowOnInvalidHeader(IGNITE_TYPE_ARRAY, hdr); + + return -1; + } + + int32_t PortableReaderImpl::ReadArray(int32_t* size) + { + return StartContainerSession(true, IGNITE_TYPE_ARRAY, size); + } + + int32_t PortableReaderImpl::ReadArray(const char* fieldName, int32_t* size) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) + return StartContainerSession(false, IGNITE_TYPE_ARRAY, size); + else { + *size = -1; + + return ++elemIdGen; + } + } + + int32_t PortableReaderImpl::ReadCollection(CollectionType* typ, int32_t* size) + { + int32_t id = StartContainerSession(true, IGNITE_TYPE_COLLECTION, size); + + if (*size == -1) + *typ = IGNITE_COLLECTION_UNDEFINED; + else + *typ = static_cast(stream->ReadInt8()); + + return id; + } + + int32_t PortableReaderImpl::ReadCollection(const char* fieldName, CollectionType* typ, int32_t* size) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) + { + int32_t id = StartContainerSession(false, IGNITE_TYPE_COLLECTION, size); + + if (*size == -1) + *typ = IGNITE_COLLECTION_UNDEFINED; + else + *typ = static_cast(stream->ReadInt8()); + + return id; + } + else { + *typ = IGNITE_COLLECTION_UNDEFINED; + *size = -1; + + return ++elemIdGen; + } + } + + int32_t PortableReaderImpl::ReadMap(MapType* typ, int32_t* size) + { + int32_t id = StartContainerSession(true, IGNITE_TYPE_MAP, size); + + if (*size == -1) + *typ = IGNITE_MAP_UNDEFINED; + else + *typ = static_cast(stream->ReadInt8()); + + return id; + } + + int32_t PortableReaderImpl::ReadMap(const char* fieldName, MapType* typ, int32_t* size) + { + CheckRawMode(false); + CheckSingleMode(true); + + int32_t fieldId = idRslvr->GetFieldId(typeId, fieldName); + int32_t fieldLen = SeekField(fieldId); + + if (fieldLen > 0) + { + int32_t id = StartContainerSession(false, IGNITE_TYPE_MAP, size); + + if (*size == -1) + *typ = IGNITE_MAP_UNDEFINED; + else + *typ = static_cast(stream->ReadInt8()); + + return id; + } + else { + *typ = IGNITE_MAP_UNDEFINED; + *size = -1; + + return ++elemIdGen; + } + } + + bool PortableReaderImpl::HasNextElement(int32_t id) + { + return elemId == id && elemRead < elemCnt; + } + + void PortableReaderImpl::SetRawMode() + { + CheckRawMode(false); + CheckSingleMode(true); + + stream->Position(pos + rawOff); + rawMode = true; + } + + template <> + int8_t PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_BYTE, PortableUtils::ReadInt8, static_cast(0)); + } + + template <> + bool PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_BOOL, PortableUtils::ReadBool, static_cast(0)); + } + + template <> + int16_t PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_SHORT, PortableUtils::ReadInt16, static_cast(0)); + } + + template <> + uint16_t PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_CHAR, PortableUtils::ReadUInt16, static_cast(0)); + } + + template <> + int32_t PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_INT, PortableUtils::ReadInt32, static_cast(0)); + } + + template <> + int64_t PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_LONG, PortableUtils::ReadInt64, static_cast(0)); + } + + template <> + float PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_FLOAT, PortableUtils::ReadFloat, static_cast(0)); + } + + template <> + double PortableReaderImpl::ReadTopObject() + { + return ReadTopObject0(IGNITE_TYPE_DOUBLE, PortableUtils::ReadDouble, static_cast(0)); + } + + template <> + Guid PortableReaderImpl::ReadTopObject() + { + int8_t typeId = stream->ReadInt8(); + + if (typeId == IGNITE_TYPE_UUID) + return PortableUtils::ReadGuid(stream); + else if (typeId == IGNITE_HDR_NULL) + return Guid(); + else { + int32_t pos = stream->Position() - 1; + + IGNITE_ERROR_FORMATTED_3(IgniteError::IGNITE_ERR_PORTABLE, "Invalid header", "position", pos, "expected", IGNITE_TYPE_UUID, "actual", typeId) + } + } + + InteropInputStream* PortableReaderImpl::GetStream() + { + return stream; + } + + int32_t PortableReaderImpl::SeekField(const int32_t fieldId) + { + // We assume that it is very likely that fields are read in the same + // order as they were initially written. So we start seeking field + // from current stream position making a "loop" up to this position. + int32_t marker = stream->Position(); + + for (int32_t curPos = marker; curPos < pos + rawOff;) + { + int32_t curFieldId = stream->ReadInt32(); + int32_t curFieldLen = stream->ReadInt32(); + + if (fieldId == curFieldId) + return curFieldLen; + else { + curPos = stream->Position() + curFieldLen; + + stream->Position(curPos); + } + } + + stream->Position(pos + IGNITE_FULL_HDR_LEN); + + for (int32_t curPos = stream->Position(); curPos < marker;) + { + int32_t curFieldId = stream->ReadInt32(); + int32_t curFieldLen = stream->ReadInt32(); + + if (fieldId == curFieldId) + return curFieldLen; + else { + curPos = stream->Position() + curFieldLen; + + stream->Position(curPos); + } + } + + return -1; + } + + void PortableReaderImpl::CheckRawMode(bool expected) + { + if (expected && !rawMode) { + IGNITE_ERROR_1(IgniteError::IGNITE_ERR_PORTABLE, "Operation can be performed only in raw mode.") + } + else if (!expected && rawMode) { + IGNITE_ERROR_1(IgniteError::IGNITE_ERR_PORTABLE, "Operation cannot be performed in raw mode.") + } + } + + void PortableReaderImpl::CheckSingleMode(bool expected) + { + if (expected && elemId != 0) { + IGNITE_ERROR_1(IgniteError::IGNITE_ERR_PORTABLE, "Operation cannot be performed when container is being read."); + } + else if (!expected && elemId == 0) { + IGNITE_ERROR_1(IgniteError::IGNITE_ERR_PORTABLE, "Operation can be performed only when container is being read."); + } + } + + int32_t PortableReaderImpl::StartContainerSession(bool expRawMode, int8_t expHdr, int32_t* size) + { + CheckRawMode(expRawMode); + CheckSingleMode(true); + + int8_t hdr = stream->ReadInt8(); + + if (hdr == expHdr) + { + int32_t cnt = stream->ReadInt32(); + + if (cnt != 0) + { + elemId = ++elemIdGen; + elemCnt = cnt; + elemRead = 0; + + *size = cnt; + + return elemId; + } + else + { + *size = 0; + + return ++elemIdGen; + } + } + else if (hdr == IGNITE_HDR_NULL) { + *size = -1; + + return ++elemIdGen; + } + else { + ThrowOnInvalidHeader(expHdr, hdr); + + return 0; + } + } + + void PortableReaderImpl::CheckSession(int32_t expSes) + { + if (elemId != expSes) { + IGNITE_ERROR_1(IgniteError::IGNITE_ERR_PORTABLE, "Containter read session has been finished or is not started yet."); + } + } + + void PortableReaderImpl::ThrowOnInvalidHeader(int32_t pos, int8_t expHdr, int8_t hdr) + { + IGNITE_ERROR_FORMATTED_3(IgniteError::IGNITE_ERR_PORTABLE, "Invalid header", "position", pos, "expected", expHdr, "actual", hdr) + } + + void PortableReaderImpl::ThrowOnInvalidHeader(int8_t expHdr, int8_t hdr) + { + int32_t pos = stream->Position() - 1; + + ThrowOnInvalidHeader(pos, expHdr, hdr); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/58a665aa/modules/platform/src/main/cpp/core/src/impl/portable/portable_utils.cpp ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/src/impl/portable/portable_utils.cpp b/modules/platform/src/main/cpp/core/src/impl/portable/portable_utils.cpp new file mode 100644 index 0000000..2f9c259 --- /dev/null +++ b/modules/platform/src/main/cpp/core/src/impl/portable/portable_utils.cpp @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ignite/impl/interop/interop.h" +#include "ignite/impl/portable/portable_utils.h" + +using namespace ignite::impl::interop; +using namespace ignite::impl::portable; + +namespace ignite +{ + namespace impl + { + namespace portable + { + int8_t PortableUtils::ReadInt8(InteropInputStream* stream) + { + return stream->ReadInt8(); + } + + void PortableUtils::WriteInt8(InteropOutputStream* stream, int8_t val) + { + stream->WriteInt8(val); + } + + void PortableUtils::ReadInt8Array(InteropInputStream* stream, int8_t* res, const int32_t len) + { + stream->ReadInt8Array(res, len); + } + + void PortableUtils::WriteInt8Array(InteropOutputStream* stream, const int8_t* val, const int32_t len) + { + stream->WriteInt8Array(val, len); + } + + bool PortableUtils::ReadBool(InteropInputStream* stream) + { + return stream->ReadBool(); + } + + void PortableUtils::WriteBool(InteropOutputStream* stream, bool val) + { + stream->WriteBool(val); + } + + void PortableUtils::ReadBoolArray(InteropInputStream* stream, bool* res, const int32_t len) + { + stream->ReadBoolArray(res, len); + } + + void PortableUtils::WriteBoolArray(InteropOutputStream* stream, const bool* val, const int32_t len) + { + stream->WriteBoolArray(val, len); + } + + int16_t PortableUtils::ReadInt16(InteropInputStream* stream) + { + return stream->ReadInt16(); + } + + void PortableUtils::WriteInt16(InteropOutputStream* stream, int16_t val) + { + stream->WriteInt16(val); + } + + void PortableUtils::ReadInt16Array(InteropInputStream* stream, int16_t* res, const int32_t len) + { + stream->ReadInt16Array(res, len); + } + + void PortableUtils::WriteInt16Array(InteropOutputStream* stream, const int16_t* val, const int32_t len) + { + stream->WriteInt16Array(val, len); + } + + uint16_t PortableUtils::ReadUInt16(InteropInputStream* stream) + { + return stream->ReadUInt16(); + } + + void PortableUtils::WriteUInt16(InteropOutputStream* stream, uint16_t val) + { + stream->WriteUInt16(val); + } + + void PortableUtils::ReadUInt16Array(InteropInputStream* stream, uint16_t* res, const int32_t len) + { + stream->ReadUInt16Array(res, len); + } + + void PortableUtils::WriteUInt16Array(InteropOutputStream* stream, const uint16_t* val, const int32_t len) + { + stream->WriteUInt16Array(val, len); + } + + int32_t PortableUtils::ReadInt32(InteropInputStream* stream) + { + return stream->ReadInt32(); + } + + void PortableUtils::WriteInt32(InteropOutputStream* stream, int32_t val) + { + stream->WriteInt32(val); + } + + void PortableUtils::ReadInt32Array(InteropInputStream* stream, int32_t* res, const int32_t len) + { + stream->ReadInt32Array(res, len); + } + + void PortableUtils::WriteInt32Array(InteropOutputStream* stream, const int32_t* val, const int32_t len) + { + stream->WriteInt32Array(val, len); + } + + int64_t PortableUtils::ReadInt64(InteropInputStream* stream) + { + return stream->ReadInt64(); + } + + void PortableUtils::WriteInt64(InteropOutputStream* stream, int64_t val) + { + stream->WriteInt64(val); + } + + void PortableUtils::ReadInt64Array(InteropInputStream* stream, int64_t* res, const int32_t len) + { + stream->ReadInt64Array(res, len); + } + + void PortableUtils::WriteInt64Array(InteropOutputStream* stream, const int64_t* val, const int32_t len) + { + stream->WriteInt64Array(val, len); + } + + float PortableUtils::ReadFloat(InteropInputStream* stream) + { + return stream->ReadFloat(); + } + + void PortableUtils::WriteFloat(InteropOutputStream* stream, float val) + { + stream->WriteFloat(val); + } + + void PortableUtils::ReadFloatArray(InteropInputStream* stream, float* res, const int32_t len) + { + stream->ReadFloatArray(res, len); + } + + void PortableUtils::WriteFloatArray(InteropOutputStream* stream, const float* val, const int32_t len) + { + stream->WriteFloatArray(val, len); + } + + double PortableUtils::ReadDouble(InteropInputStream* stream) + { + return stream->ReadDouble(); + } + + void PortableUtils::WriteDouble(InteropOutputStream* stream, double val) + { + stream->WriteDouble(val); + } + + void PortableUtils::ReadDoubleArray(InteropInputStream* stream, double* res, const int32_t len) + { + stream->ReadDoubleArray(res, len); + } + + void PortableUtils::WriteDoubleArray(InteropOutputStream* stream, const double* val, const int32_t len) + { + stream->WriteDoubleArray(val, len); + } + + Guid PortableUtils::ReadGuid(interop::InteropInputStream* stream) + { + int64_t most = stream->ReadInt64(); + int64_t least = stream->ReadInt64(); + + return Guid(most, least); + } + + void PortableUtils::WriteGuid(interop::InteropOutputStream* stream, const Guid val) + { + stream->WriteInt64(val.GetMostSignificantBits()); + stream->WriteInt64(val.GetLeastSignificantBits()); + } + + void PortableUtils::WriteString(interop::InteropOutputStream* stream, const char* val, const int32_t len) + { + stream->WriteBool(false); + stream->WriteInt32(len); + + for (int i = 0; i < len; i++) + stream->WriteUInt16(*(val + i)); + } + } + } +} \ No newline at end of file