Skip to content

Commit

Permalink
Fix endian handling (#88)
Browse files Browse the repository at this point in the history
 fix endian handling in serialization utils, also some typo and style fixes
  • Loading branch information
Hendrik Muhs committed Jul 23, 2018
1 parent b1e0351 commit a9367e7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion keyvi/include/keyvi/dictionary/fsa/automata.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace keyvi {
namespace dictionary {
namespace fsa {

/// TODO: refator (split) class Automata, so there is no need for param "loadVS" and friend classes
/// TODO: refactor (split) class Automata, so there is no need for param "loadVS" and friend classes
class Automata final {
public:
explicit Automata(const std::string& filename, loading_strategy_types loading_strategy = loading_strategy_types::lazy)
Expand Down
18 changes: 9 additions & 9 deletions keyvi/include/keyvi/dictionary/fsa/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ class Generator final {
ConsumeStack(0);

// handling of last State.
internal::UnpackedState<PersistenceT>* unpackedState = stack_->Get(0);
internal::UnpackedState<PersistenceT>* unpacked_state = stack_->Get(0);

start_state_ = builder_->PersistState(unpackedState);
start_state_ = builder_->PersistState(unpacked_state);

TRACE("wrote start state at %d", start_state_);
TRACE("Check first transition: %d/%d %s", (*unpackedState)[0].label,
persistence_->ReadTransitionLabel(start_state_ + (*unpackedState)[0].label),
(*unpackedState)[0].label == persistence_->ReadTransitionLabel(start_state_ + (*unpackedState)[0].label)
TRACE("Check first transition: %d/%d %s", (*unpacked_state)[0].label,
persistence_->ReadTransitionLabel(start_state_ + (*unpacked_state)[0].label),
(*unpacked_state)[0].label == persistence_->ReadTransitionLabel(start_state_ + (*unpacked_state)[0].label)
? "OK"
: "BROKEN");

Expand Down Expand Up @@ -357,7 +357,7 @@ class Generator final {

inline void FeedStack(const size_t start, const std::string& key) {
for (size_t i = start; i < key.size(); ++i) {
uint32_t ukey = static_cast<uint32_t>(static_cast<unsigned char>(key[i]));
const uint32_t ukey = static_cast<uint32_t>(static_cast<unsigned char>(key[i]));
stack_->Insert(i, ukey, 0);
}

Expand All @@ -370,13 +370,13 @@ class Generator final {
inline void ConsumeStack(const size_t end) {
while (highest_stack_ > end) {
// Get outgoing transitions from the stack.
internal::UnpackedState<PersistenceT>* unpackedState = stack_->Get(highest_stack_);
internal::UnpackedState<PersistenceT>* unpacked_state = stack_->Get(highest_stack_);

OffsetTypeT transitionPointer = builder_->PersistState(unpackedState);
const OffsetTypeT transition_pointer = builder_->PersistState(unpacked_state);

// Save transition_pointer in previous stack, indicate whether it makes
// sense continuing minimization
stack_->PushTransitionPointer(highest_stack_ - 1, transitionPointer, unpackedState->GetNoMinimizationCounter());
stack_->PushTransitionPointer(highest_stack_ - 1, transition_pointer, unpacked_state->GetNoMinimizationCounter());

// Delete state
stack_->Erase(highest_stack_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ class SparseArrayPersistence final {
uint64_t ReadFinalValue(size_t offset) const;

/**
* Flush buffer up to the given position
* @param position the latest position until flush should happen
* Flush all internal buffers
*/
void Flush() {
// make idempotent, so it can be called twice or more);
Expand Down
2 changes: 2 additions & 0 deletions keyvi/include/keyvi/dictionary/util/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#define le16toh(x) (x)
#define htole64(x) (x)
#define le64toh(x) (x)
#define htobe32(x) htonl(x)
#define be32toh(x) ntohl(x)

#else
#include <endian.h>
Expand Down
8 changes: 4 additions & 4 deletions keyvi/include/keyvi/util/serialization_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#ifndef KEYVI_UTIL_SERIALIZATION_UTILS_H_
#define KEYVI_UTIL_SERIALIZATION_UTILS_H_

#include <arpa/inet.h>

#include <string>

#include <boost/lexical_cast.hpp>
Expand All @@ -35,6 +33,8 @@
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>

#include "dictionary/util/endian.h"

namespace keyvi {
namespace util {

Expand All @@ -46,7 +46,7 @@ class SerializationUtils {
boost::property_tree::write_json(string_buffer, properties, false);
std::string header = string_buffer.str();

uint32_t size = ntohl(header.size());
uint32_t size = htobe32(header.size());

stream.write(reinterpret_cast<const char*>(&size), sizeof(uint32_t));
stream << header;
Expand All @@ -55,7 +55,7 @@ class SerializationUtils {
static boost::property_tree::ptree ReadJsonRecord(std::istream& stream) {
uint32_t header_size;
stream.read(reinterpret_cast<char*>(&header_size), sizeof(int));
header_size = htonl(header_size);
header_size = be32toh(header_size);
char* buffer = new char[header_size];
stream.read(buffer, header_size);
std::string buffer_as_string(buffer, header_size);
Expand Down

0 comments on commit a9367e7

Please sign in to comment.