1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef OS_COLLECTIONINDEX_H
16 #define OS_COLLECTIONINDEX_H
20 #include "include/memory.h"
22 #include "osd/osd_types.h"
23 #include "include/object.h"
24 #include "common/RWLock.h"
27 CollectionIndex provides an interface for manipulating indexed collections
29 class CollectionIndex {
34 * Object encapsulating a returned path.
36 * A path to an object (existent or non-existent) becomes invalid
37 * when a different object is created in the index. Path stores
38 * a shared_ptr to the CollectionIndex to keep the index alive
39 * during its lifetime.
48 /// Ref to parent Index
49 CollectionIndex* parent_ref;
50 /// coll_t for parent Index
53 /// Normal Constructor
55 string path, ///< [in] Path to return.
57 : full_path(path), parent_ref(ref), parent_coll(parent_ref->coll()) {}
59 /// Debugging Constructor
61 string path, ///< [in] Path to return.
62 const coll_t& coll) ///< [in] collection
63 : full_path(path), parent_coll(coll) {}
65 /// Getter for the stored path.
66 const char *path() const { return full_path.c_str(); }
68 /// Getter for collection
69 const coll_t& coll() const { return parent_coll; }
72 CollectionIndex* get_index() const {
79 /// Type of returned paths
80 typedef ceph::shared_ptr<Path> IndexedPath;
82 static IndexedPath get_testing_path(string path, coll_t collection) {
83 return std::make_shared<Path>(path, collection);
86 static const uint32_t FLAT_INDEX_TAG = 0;
87 static const uint32_t HASH_INDEX_TAG = 1;
88 static const uint32_t HASH_INDEX_TAG_2 = 2;
89 static const uint32_t HOBJECT_WITH_POOL = 3;
91 * For tracking Filestore collection versions.
93 * @return Collection version represented by the Index implementation
95 virtual uint32_t collection_version() = 0;
98 * Returns the collection managed by this CollectionIndex
100 virtual coll_t coll() const = 0;
104 * Initializes the index.
106 * @return Error Code, 0 for success
108 virtual int init() = 0;
111 * Cleanup before replaying journal
113 * Index implemenations may need to perform compound operations
114 * which may leave the collection unstable if interupted. cleanup
115 * is called on mount to allow the CollectionIndex implementation
119 * @return Error Code, 0 for success
121 virtual int cleanup() = 0;
124 * Call when a file is created using a path returned from lookup.
126 * @return Error Code, 0 for success
129 const ghobject_t &oid, ///< [in] Created object.
130 const char *path ///< [in] Path to created object.
134 * Removes oid from the collection
136 * @return Error Code, 0 for success
139 const ghobject_t &oid ///< [in] Object to remove
143 * Gets the IndexedPath for oid.
145 * @return Error Code, 0 for success
148 const ghobject_t &oid, ///< [in] Object to lookup
149 IndexedPath *path, ///< [out] Path to object
150 int *hardlink ///< [out] number of hard links of this object. *hardlink=0 mean object no-exist.
154 * Moves objects matching @e match in the lsb @e bits
156 * dest and this must be the same subclass
158 * @return Error Code, 0 for success
161 uint32_t match, //< [in] value to match
162 uint32_t bits, //< [in] bits to check
163 CollectionIndex* dest //< [in] destination index
164 ) { ceph_abort(); return 0; }
167 /// List contents of collection by hash
168 virtual int collection_list_partial(
169 const ghobject_t &start, ///< [in] object at which to start
170 const ghobject_t &end, ///< [in] list only objects < end
171 int max_count, ///< [in] return at most max_count objects
172 vector<ghobject_t> *ls, ///< [out] Listed objects
173 ghobject_t *next ///< [out] Next object to list
176 /// Call prior to removing directory
177 virtual int prep_delete() { return 0; }
179 CollectionIndex(CephContext* cct, const coll_t& collection)
180 : cct(cct), access_lock("CollectionIndex::access_lock", true, false) {}
183 * Pre-hash the collection, this collection should map to a PG folder.
185 * @param pg_num - pg number of the pool this collection belongs to.
186 * @param expected_num_objs - expected number of objects in this collection.
187 * @Return 0 on success, an error code otherwise.
189 virtual int pre_hash_collection(
190 uint32_t pg_num, ///< [in] pg number of the pool this collection belongs to
191 uint64_t expected_num_objs ///< [in] expected number of objects this collection has
192 ) { ceph_abort(); return 0; }
194 virtual int apply_layout_settings() { ceph_abort(); return 0; }
196 /// Read index-wide settings (should be called after construction)
197 virtual int read_settings() { return 0; }
199 /// Virtual destructor
200 virtual ~CollectionIndex() {}