~ubuntu-branches/debian/sid/python-feather-format/sid

« back to all changes in this revision

Viewing changes to src/feather/reader.h

  • Committer: Package Import Robot
  • Author(s): ChangZhuo Chen (陳昌倬)
  • Date: 2016-03-30 19:01:30 UTC
  • Revision ID: package-import@ubuntu.com-20160330190130-b7o770i7o2drepi1
Tags: upstream-0.1.0
Import upstream version 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Feather Developers
 
2
//
 
3
// Licensed under the Apache License, Version 2.0 (the "License");
 
4
// you may not use this file except in compliance with the License.
 
5
// You may obtain a copy of the License at
 
6
//
 
7
// http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
9
// Unless required by applicable law or agreed to in writing, software
 
10
// distributed under the License is distributed on an "AS IS" BASIS,
 
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
// See the License for the specific language governing permissions and
 
13
// limitations under the License.
 
14
 
 
15
#ifndef FEATHER_READER_H
 
16
#define FEATHER_READER_H
 
17
 
 
18
#include <string>
 
19
 
 
20
#include "feather/io.h"
 
21
#include "feather/metadata.h"
 
22
#include "feather/types.h"
 
23
 
 
24
namespace feather {
 
25
 
 
26
class Status;
 
27
 
 
28
class Column {
 
29
 public:
 
30
  Column(ColumnType::type type,
 
31
      const std::shared_ptr<metadata::Column>& metadata,
 
32
      const PrimitiveArray& values) :
 
33
      type_(type),
 
34
      metadata_(metadata),
 
35
      values_(values) {}
 
36
 
 
37
  const PrimitiveArray& values() const {
 
38
    return values_;
 
39
  }
 
40
 
 
41
  ColumnType::type type() const {
 
42
    return type_;
 
43
  }
 
44
 
 
45
  const std::shared_ptr<metadata::Column>& metadata() const {
 
46
    return metadata_;
 
47
  }
 
48
 
 
49
  std::string name() const {
 
50
    return metadata_->name();
 
51
  }
 
52
 
 
53
 protected:
 
54
  ColumnType::type type_;
 
55
  std::shared_ptr<metadata::Column> metadata_;
 
56
  PrimitiveArray values_;
 
57
};
 
58
 
 
59
class CategoryColumn : public Column {
 
60
 public:
 
61
  CategoryColumn(const std::shared_ptr<metadata::Column>& metadata,
 
62
      const PrimitiveArray& values,
 
63
      const PrimitiveArray& levels,
 
64
      bool ordered = false) :
 
65
      Column(ColumnType::CATEGORY, metadata, values),
 
66
      levels_(levels),
 
67
      ordered_(ordered) {
 
68
    category_meta_ = static_cast<const metadata::CategoryColumn*>(metadata.get());
 
69
  }
 
70
 
 
71
  const PrimitiveArray& levels() const {
 
72
    return levels_;
 
73
  }
 
74
 
 
75
  bool ordered() const {
 
76
    return ordered_;
 
77
  }
 
78
 
 
79
 private:
 
80
  const metadata::CategoryColumn* category_meta_;
 
81
  PrimitiveArray levels_;
 
82
  bool ordered_;
 
83
};
 
84
 
 
85
class TimestampColumn : public Column {
 
86
 public:
 
87
  TimestampColumn(const std::shared_ptr<metadata::Column>& metadata,
 
88
      const PrimitiveArray& values) :
 
89
      Column(ColumnType::TIMESTAMP, metadata, values)  {
 
90
    timestamp_meta_ = static_cast<const metadata::TimestampColumn*>(metadata.get());
 
91
  }
 
92
 
 
93
  TimeUnit::type unit() const {
 
94
    return timestamp_meta_->unit();
 
95
  }
 
96
 
 
97
  std::string timezone() const {
 
98
    return timestamp_meta_->timezone();
 
99
  }
 
100
 
 
101
 private:
 
102
  const metadata::TimestampColumn* timestamp_meta_;
 
103
};
 
104
 
 
105
class DateColumn : public Column {
 
106
 public:
 
107
  DateColumn(const std::shared_ptr<metadata::Column>& metadata,
 
108
      const PrimitiveArray& values) :
 
109
      Column(ColumnType::DATE, metadata, values)  {
 
110
    date_meta_ = static_cast<const metadata::DateColumn*>(metadata.get());
 
111
  }
 
112
 
 
113
 private:
 
114
  const metadata::DateColumn* date_meta_;
 
115
};
 
116
 
 
117
class TimeColumn : public Column {
 
118
 public:
 
119
  TimeColumn(const std::shared_ptr<metadata::Column>& metadata,
 
120
      const PrimitiveArray& values) :
 
121
      Column(ColumnType::TIME, metadata, values)  {
 
122
    time_meta_ = static_cast<const metadata::TimeColumn*>(metadata.get());
 
123
  }
 
124
 
 
125
  TimeUnit::type unit() const {
 
126
    return time_meta_->unit();
 
127
  }
 
128
 
 
129
 private:
 
130
  const metadata::TimeColumn* time_meta_;
 
131
};
 
132
 
 
133
class TableReader {
 
134
 public:
 
135
  TableReader();
 
136
 
 
137
  Status Open(const std::shared_ptr<RandomAccessReader>& source);
 
138
 
 
139
  static Status OpenFile(const std::string& abspath, std::unique_ptr<TableReader>* out);
 
140
 
 
141
  // Optional table description
 
142
  //
 
143
  // This does not return a const std::string& because a string has to be
 
144
  // copied from the flatbuffer to be able to return a non-flatbuffer type
 
145
  std::string GetDescription() const;
 
146
  bool HasDescription() const;
 
147
 
 
148
  int64_t num_rows() const;
 
149
  int64_t num_columns() const;
 
150
 
 
151
  Status GetColumn(int i, std::shared_ptr<Column>* out);
 
152
 
 
153
 private:
 
154
  Status GetPrimitive(std::shared_ptr<metadata::Column> col_meta,
 
155
      std::shared_ptr<Column>* out);
 
156
  Status GetCategory(std::shared_ptr<metadata::Column> col_meta,
 
157
      std::shared_ptr<Column>* out);
 
158
 
 
159
  Status GetTimestamp(std::shared_ptr<metadata::Column> col_meta,
 
160
      std::shared_ptr<Column>* out);
 
161
 
 
162
  Status GetTime(std::shared_ptr<metadata::Column> col_meta,
 
163
      std::shared_ptr<Column>* out);
 
164
 
 
165
  Status GetDate(std::shared_ptr<metadata::Column> col_meta,
 
166
      std::shared_ptr<Column>* out);
 
167
 
 
168
  // Retrieve a primitive array from the data source
 
169
  //
 
170
  // @returns: a Buffer instance, the precise type will depend on the kind of
 
171
  // input data source (which may or may not have memory-map like semantics)
 
172
  Status GetPrimitiveArray(const ArrayMetadata& meta, PrimitiveArray* out);
 
173
 
 
174
  std::shared_ptr<RandomAccessReader> source_;
 
175
  metadata::Table metadata_;
 
176
};
 
177
 
 
178
} // namespace feather
 
179
 
 
180
#endif // FEATHER_WRITER_H