• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / jorgen.edelbo_338

03 Jul 2024 03:00PM UTC coverage: 90.856% (-0.008%) from 90.864%
jorgen.edelbo_338

Pull #7803

Evergreen

nicola-cab
Merge branch 'next-major' into feature/string-compression
Pull Request #7803: Feature/string compression

103028 of 180606 branches covered (57.05%)

1144 of 1267 new or added lines in 33 files covered. (90.29%)

155 existing lines in 24 files now uncovered.

218583 of 240583 relevant lines covered (90.86%)

7959624.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/realm/string_compressor.hpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#ifndef REALM_STRING_COMPRESSOR_HPP
20
#define REALM_STRING_COMPRESSOR_HPP
21

22
#include <realm/array_unsigned.hpp>
23
#include <realm/utilities.hpp>
24
#include <vector>
25

26
using CompressionSymbol = uint16_t;
27
using CompressedString = std::vector<CompressionSymbol>;
28

29
struct CompressedStringView {
30
    CompressionSymbol* data = 0;
31
    uint32_t size = 0;
NEW
32
    CompressedStringView() = default;
×
33
    CompressedStringView(CompressionSymbol* c_ptr, size_t s)
34
        : data(c_ptr)
5,293,947✔
35
        , size(uint32_t(s))
5,293,947✔
36
    {
10,468,290✔
37
    }
10,468,290✔
38
    explicit CompressedStringView(CompressedString& cs)
39
        : data(cs.data())
460,068✔
40
        , size(uint32_t(cs.size()))
460,068✔
41
    {
923,817✔
42
    }
923,817✔
43
    bool operator==(CompressedStringView& other)
44
    {
923,808✔
45
        if (size != other.size)
923,808✔
NEW
46
            return false;
×
47
        for (size_t i = 0; i < size; ++i) {
1,123,066,866✔
48
            if (data[i] != other.data[i])
1,122,143,058✔
NEW
49
                return false;
×
50
        }
1,122,143,058✔
51
        return true;
923,808✔
52
    }
923,808✔
53
};
54

55
namespace realm {
56
class StringCompressor {
57
public:
58
    StringCompressor(Allocator& alloc, Array& parent, size_t index, bool writable);
59
    void refresh(bool writable);
60
    ~StringCompressor();
61

62
    int compare(CompressedStringView& A, CompressedStringView& B);
63
    int compare(StringData sd, CompressedStringView& B);
64

65
    CompressedString compress(StringData, bool learn);
66
    std::string decompress(CompressedStringView& c_str);
67

68
private:
69
    struct SymbolDef {
70
        CompressionSymbol id = 0;
71
        CompressionSymbol expansion_a = 0;
72
        CompressionSymbol expansion_b = 0;
73
    };
74

75
    struct ExpandedSymbolDef {
76
        SymbolDef def;
77
        std::string_view expansion;
78
        // ^ points into storage managed by m_expansion_storage
79
        // we need the following 2 values to facilitate rollback of allocated storage
80
        uint32_t storage_index;  // index into m_expansion_storage
81
        uint32_t storage_offset; // offset into block.
82
    };
83

84
    void rebuild_internal();
85
    void expand_compression_map();
86
    void add_expansion(SymbolDef def);
87
    std::vector<ExpandedSymbolDef> m_symbols; // map from symbol -> symbolpair, 2 elements pr entry
88
    std::vector<SymbolDef> m_compression_map; // perfect hash from symbolpair to its symbol
89

90
    ArrayUnsigned m_data;
91
    constexpr static size_t storage_chunk_size = 4096;
92
    std::vector<std::string> m_expansion_storage;
93
};
94

95
} // namespace realm
96

97
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc