• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
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 
16 // An iterator yields a sequence of key/value pairs from a source.
17 // The following class defines the interface.  Multiple implementations
18 // are provided by this library.  In particular, iterators are provided
19 // to access the contents of a Table or a DB.
20 //
21 // Multiple threads can invoke const methods on an Iterator without
22 // external synchronization, but if any of the threads may call a
23 // non-const method, all threads accessing the same Iterator must use
24 // external synchronization.
25 
26 #ifndef TENSORFLOW_LIB_IO_ITERATOR_H_
27 #define TENSORFLOW_LIB_IO_ITERATOR_H_
28 
29 #include "tensorflow/core/lib/core/status.h"
30 #include "tensorflow/core/lib/core/stringpiece.h"
31 
32 namespace tensorflow {
33 namespace table {
34 
35 class Iterator {
36  public:
37   Iterator();
38   virtual ~Iterator();
39 
40   // An iterator is either positioned at a key/value pair, or
41   // not valid.  This method returns true iff the iterator is valid.
42   virtual bool Valid() const = 0;
43 
44   // Position at the first key in the source.  The iterator is Valid()
45   // after this call iff the source is not empty.
46   virtual void SeekToFirst() = 0;
47 
48   // Position at the first key in the source that is at or past target.
49   // The iterator is Valid() after this call iff the source contains
50   // an entry that comes at or past target.
51   virtual void Seek(const StringPiece& target) = 0;
52 
53   // Moves to the next entry in the source.  After this call, Valid() is
54   // true iff the iterator was not positioned at the last entry in the source.
55   // REQUIRES: Valid()
56   virtual void Next() = 0;
57 
58   // Return the key for the current entry.  The underlying storage for
59   // the returned slice is valid only until the next modification of
60   // the iterator.
61   // REQUIRES: Valid()
62   virtual StringPiece key() const = 0;
63 
64   // Return the value for the current entry.  The underlying storage for
65   // the returned slice is valid only until the next modification of
66   // the iterator.
67   // REQUIRES: Valid()
68   virtual StringPiece value() const = 0;
69 
70   // If an error has occurred, return it.  Else return an ok status.
71   virtual Status status() const = 0;
72 
73   // Clients are allowed to register function/arg1/arg2 triples that
74   // will be invoked when this iterator is destroyed.
75   //
76   // Note that unlike all of the preceding methods, this method is
77   // not abstract and therefore clients should not override it.
78   typedef void (*CleanupFunction)(void* arg1, void* arg2);
79   void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
80 
81  private:
82   struct Cleanup {
83     CleanupFunction function;
84     void* arg1;
85     void* arg2;
86     Cleanup* next;
87   };
88   Cleanup cleanup_;
89 
90   // No copying allowed
91   Iterator(const Iterator&);
92   void operator=(const Iterator&);
93 };
94 
95 // Return an empty iterator (yields nothing).
96 extern Iterator* NewEmptyIterator();
97 
98 // Return an empty iterator with the specified status.
99 extern Iterator* NewErrorIterator(const Status& status);
100 
101 }  // namespace table
102 }  // namespace tensorflow
103 
104 #endif  // TENSORFLOW_LIB_IO_ITERATOR_H_
105