summaryrefslogtreecommitdiffstats
path: root/src/os/filestore/CollectionIndex.h
blob: eb43e47dc53080086ddb958eec7d9bef8bfbfb37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#ifndef OS_COLLECTIONINDEX_H
#define OS_COLLECTIONINDEX_H

#include <string>
#include <vector>

#include "osd/osd_types.h"
#include "include/object.h"
#include "common/RWLock.h"

/**
  CollectionIndex provides an interface for manipulating indexed collections
 */
class CollectionIndex {
public:
  CephContext* cct;
protected:
  /**
   * Object encapsulating a returned path.
   *
   * A path to an object (existent or non-existent) becomes invalid
   * when a different object is created in the index.  Path stores
   * a shared_ptr to the CollectionIndex to keep the index alive
   * during its lifetime.
   * @see IndexManager
   * @see self_ref
   * @see set_ref
   */
  class Path {
  public:
    /// Returned path
    string full_path;
    /// Ref to parent Index
    CollectionIndex* parent_ref;
    /// coll_t for parent Index
    coll_t parent_coll;

    /// Normal Constructor
    Path(
      string path,                              ///< [in] Path to return.
      CollectionIndex* ref)
      : full_path(path), parent_ref(ref), parent_coll(parent_ref->coll()) {}

    /// Debugging Constructor
    Path(
      string path,                              ///< [in] Path to return.
      const coll_t& coll)                              ///< [in] collection
      : full_path(path), parent_coll(coll) {}

    /// Getter for the stored path.
    const char *path() const { return full_path.c_str(); }

    /// Getter for collection
    const coll_t& coll() const { return parent_coll; }

    /// Getter for parent
    CollectionIndex* get_index() const {
      return parent_ref;
    }
  };
 public:

  RWLock access_lock;
  /// Type of returned paths
  typedef std::shared_ptr<Path> IndexedPath;

  static IndexedPath get_testing_path(string path, coll_t collection) {
    return std::make_shared<Path>(path, collection);
  }

  static const uint32_t FLAT_INDEX_TAG = 0;
  static const uint32_t HASH_INDEX_TAG = 1;
  static const uint32_t HASH_INDEX_TAG_2 = 2;
  static const uint32_t HOBJECT_WITH_POOL = 3;
  /**
   * For tracking Filestore collection versions.
   *
   * @return Collection version represented by the Index implementation
   */
  virtual uint32_t collection_version() = 0;

  /**
   * Returns the collection managed by this CollectionIndex
   */
  virtual coll_t coll() const = 0;


  /**
   * Initializes the index.
   *
   * @return Error Code, 0 for success
   */
  virtual int init() = 0;

  /**
   * Cleanup before replaying journal
   *
   * Index implementations may need to perform compound operations
   * which may leave the collection unstable if interrupted.  cleanup
   * is called on mount to allow the CollectionIndex implementation
   * to stabilize.
   *
   * @see HashIndex
   * @return Error Code, 0 for success
   */
  virtual int cleanup() = 0;

  /**
   * Call when a file is created using a path returned from lookup.
   *
   * @return Error Code, 0 for success
   */
  virtual int created(
    const ghobject_t &oid, ///< [in] Created object.
    const char *path       ///< [in] Path to created object.
    ) = 0;

  /**
   * Removes oid from the collection
   *
   * @return Error Code, 0 for success
   */
  virtual int unlink(
    const ghobject_t &oid ///< [in] Object to remove
    ) = 0;

  /**
   * Gets the IndexedPath for oid.
   *
   * @return Error Code, 0 for success
   */
  virtual int lookup(
    const ghobject_t &oid, ///< [in] Object to lookup
    IndexedPath *path,	   ///< [out] Path to object
    int *hardlink          ///< [out] number of hard links of this object. *hardlink=0 mean object no-exist.
    ) = 0;

  /**
   * Moves objects matching @e match in the lsb @e bits
   *
   * dest and this must be the same subclass
   *
   * @return Error Code, 0 for success
   */
  virtual int split(
    uint32_t match,                             //< [in] value to match
    uint32_t bits,                              //< [in] bits to check
    CollectionIndex* dest  //< [in] destination index
    ) { ceph_abort(); return 0; }

  virtual int merge(
    uint32_t bits,                              //< [in] common (target) bits
    CollectionIndex* dest  //< [in] destination index
    ) { ceph_abort(); return 0; }


  /// List contents of collection by hash
  virtual int collection_list_partial(
    const ghobject_t &start, ///< [in] object at which to start
    const ghobject_t &end,    ///< [in] list only objects < end
    int max_count,          ///< [in] return at most max_count objects
    vector<ghobject_t> *ls,  ///< [out] Listed objects
    ghobject_t *next         ///< [out] Next object to list
    ) = 0;

  /// Call prior to removing directory
  virtual int prep_delete() { return 0; }

  CollectionIndex(CephContext* cct, const coll_t& collection)
    : cct(cct), access_lock("CollectionIndex::access_lock", true, false) {}

  /*
   * Pre-hash the collection, this collection should map to a PG folder.
   *
   * @param pg_num            - pg number of the pool this collection belongs to.
   * @param expected_num_objs - expected number of objects in this collection.
   * @Return 0 on success, an error code otherwise.
   */
  virtual int pre_hash_collection(
      uint32_t pg_num,            ///< [in] pg number of the pool this collection belongs to
      uint64_t expected_num_objs  ///< [in] expected number of objects this collection has
      ) { ceph_abort(); return 0; }

  virtual int apply_layout_settings(int target_level) { ceph_abort(); return 0; }

  /// Read index-wide settings (should be called after construction)
  virtual int read_settings() { return 0; }

  /// Virtual destructor
  virtual ~CollectionIndex() {}
};

#endif