1c8b59c046895fa5b6d79f73e0b5817330fcfbfc1A. Unique TensorFlower/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlur
3854f49bd43588c062b046384f239f64a3d819702Manjunath KudlurLicensed under the Apache License, Version 2.0 (the "License");
4854f49bd43588c062b046384f239f64a3d819702Manjunath Kudluryou may not use this file except in compliance with the License.
5854f49bd43588c062b046384f239f64a3d819702Manjunath KudlurYou may obtain a copy of the License at
6854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlur
7854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlur    http://www.apache.org/licenses/LICENSE-2.0
8854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlur
9854f49bd43588c062b046384f239f64a3d819702Manjunath KudlurUnless required by applicable law or agreed to in writing, software
10854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlurdistributed under the License is distributed on an "AS IS" BASIS,
11854f49bd43588c062b046384f239f64a3d819702Manjunath KudlurWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12854f49bd43588c062b046384f239f64a3d819702Manjunath KudlurSee the License for the specific language governing permissions and
13854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlurlimitations under the License.
14854f49bd43588c062b046384f239f64a3d819702Manjunath Kudlur==============================================================================*/
15f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
16f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur#ifndef TENSORFLOW_LIB_IO_TABLE_H_
17f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur#define TENSORFLOW_LIB_IO_TABLE_H_
18f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
19f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur#include <stdint.h>
20f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur#include "tensorflow/core/lib/io/iterator.h"
21f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
22f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurnamespace tensorflow {
23f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurclass RandomAccessFile;
24f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
25f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurnamespace table {
26f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
27f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurclass Block;
28f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurclass BlockHandle;
29f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurclass Footer;
30f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurstruct Options;
31f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
32f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur// A Table is a sorted map from strings to strings.  Tables are
33f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur// immutable and persistent.  A Table may be safely accessed from
34f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur// multiple threads without external synchronization.
35f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlurclass Table {
36f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur public:
37f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // Attempt to open the table that is stored in bytes [0..file_size)
38f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // of "file", and read the metadata entries necessary to allow
39f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // retrieving data from the table.
40f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  //
41f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // If successful, returns ok and sets "*table" to the newly opened
42f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // table.  The client should delete "*table" when no longer needed.
43f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // If there was an error while initializing the table, sets "*table"
44f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // to NULL and returns a non-ok status.  Does not take ownership of
45f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // "*file", but the client must ensure that "file" remains live
46f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // for the duration of the returned table's lifetime.
47f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  static Status Open(const Options& options, RandomAccessFile* file,
4856313def004795f75ef8281a0294c958d28f1e06Vijay Vasudevan                     uint64 file_size, Table** table);
49f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
50f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  ~Table();
51f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
52f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // Returns a new iterator over the table contents.
53f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // The result of NewIterator() is initially invalid (caller must
54f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // call one of the Seek methods on the iterator before using it).
55f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  Iterator* NewIterator() const;
56f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
57f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // Given a key, return an approximate byte offset in the file where
58f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // the data for that key begins (or would begin if the key were
59f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // present in the file).  The returned value is in terms of file
60f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // bytes, and so includes effects like compression of the underlying data.
61f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // E.g., the approximate offset of the last key in the table will
62f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // be close to the file length.
63f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  uint64 ApproximateOffsetOf(const StringPiece& key) const;
64f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
65f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur private:
66f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  struct Rep;
67f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  Rep* rep_;
68f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
69f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  explicit Table(Rep* rep) { rep_ = rep; }
70f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  static Iterator* BlockReader(void*, const StringPiece&);
71f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
72f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // Calls (*handle_result)(arg, ...) with the entry found after a call
73f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // to Seek(key).  May not make such a call if filter policy says
74f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // that key is not present.
75f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  Status InternalGet(const StringPiece& key, void* arg,
7656313def004795f75ef8281a0294c958d28f1e06Vijay Vasudevan                     void (*handle_result)(void* arg, const StringPiece& k,
7756313def004795f75ef8281a0294c958d28f1e06Vijay Vasudevan                                           const StringPiece& v));
78f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
79f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  // No copying allowed
80f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  Table(const Table&);
81f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur  void operator=(const Table&);
82f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur};
83f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
84f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur}  // namespace table
85f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur}  // namespace tensorflow
86f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur
87f41959ccb2d9d4c722fe8fc3351401d53bcf490Manjunath Kudlur#endif  // TENSORFLOW_LIB_IO_TABLE_H_
88