Storage Engine API
hashtab.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include "mongo/base/static_assert.h"
32 #include "mongo/db/operation_context.h"
35 #include "mongo/stdx/functional.h"
36 
37 namespace mongo {
38 
46 
47 public:
48  typedef stdx::function<void(const Namespace& k, NamespaceDetails& v)> IteratorCallback;
49 
50 
51  /* buf must be all zeroes on initialization. */
52  NamespaceHashTable(void* buf, int buflen, const char* name);
53 
54  NamespaceDetails* get(const Namespace& k) const {
55  bool found;
56  int i = _find(k, found);
57  if (found) {
58  return &_nodes(i).value;
59  }
60 
61  return 0;
62  }
63 
64  void kill(OperationContext* opCtx, const Namespace& k) {
65  bool found;
66  int i = _find(k, found);
67  if (i >= 0 && found) {
68  Node* n = &_nodes(i);
69  n = opCtx->recoveryUnit()->writing(n);
70  n->key.kill();
71  n->setUnused();
72  }
73  }
74 
76  bool put(OperationContext* opCtx, const Namespace& k, const NamespaceDetails& value) {
77  bool found;
78  int i = _find(k, found);
79  if (i < 0)
80  return false;
81 
82  Node* n = opCtx->recoveryUnit()->writing(&_nodes(i));
83  if (!found) {
84  n->key = k;
85  n->hash = k.hash();
86  } else {
87  invariant(n->hash == k.hash());
88  }
89 
90  n->value = value;
91  return true;
92  }
93 
94  void iterAll(IteratorCallback callback) {
95  for (int i = 0; i < n; i++) {
96  if (_nodes(i).inUse()) {
97  callback(_nodes(i).key, _nodes(i).value);
98  }
99  }
100  }
101 
102 
103 private:
104 #pragma pack(1)
105  struct Node {
106  int hash;
109 
110  bool inUse() const {
111  return hash != 0;
112  }
113 
114  void setUnused() {
115  hash = 0;
116  }
117  };
118 #pragma pack()
119 
120  MONGO_STATIC_ASSERT(sizeof(Node) == 628);
121 
122 
123  int _find(const Namespace& k, bool& found) const;
124 
125  Node& _nodes(int i) const {
126  Node* nodes = (Node*)_buf;
127  return nodes[i];
128  }
129 
130 
131  const char* _name;
132  void* const _buf;
133 
134  int n; // number of hashtable buckets
135  int maxChain;
136 };
137 
138 } // namespace mongo
void kill(OperationContext *opCtx, const Namespace &k)
Definition: hashtab.h:64
void *const _buf
Definition: hashtab.h:132
void iterAll(IteratorCallback callback)
Definition: hashtab.h:94
MONGO_DISALLOW_COPYING(NamespaceHashTable)
int n
Definition: hashtab.h:134
NamespaceDetails value
Definition: hashtab.h:108
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
MONGO_STATIC_ASSERT(sizeof(Node)==628)
Simple, fixed size hash table used for namespace mapping (effectively the contents of the MMAP V1 ...
Definition: hashtab.h:44
void kill()
Definition: namespace.h:75
BSONObj key
Definition: btree_interface.cpp:334
int maxChain
Definition: hashtab.h:135
int hash
Definition: hashtab.h:106
bool inUse() const
Definition: hashtab.h:110
Namespace key
Definition: hashtab.h:107
Definition: namespace_details.h:47
stdx::function< void(const Namespace &k, NamespaceDetails &v)> IteratorCallback
Definition: hashtab.h:48
NamespaceHashTable(void *buf, int buflen, const char *name)
Definition: hashtab.cpp:75
Definition: hashtab.h:105
int hash() const
Value returned is always > 0.
Definition: namespace.h:99
Node & _nodes(int i) const
Definition: hashtab.h:125
This is used for storing a namespace on disk in a fixed witdh form and should only be used for that...
Definition: namespace.h:49
int _find(const Namespace &k, bool &found) const
Definition: hashtab.cpp:39
void setUnused()
Definition: hashtab.h:114
bool put(OperationContext *opCtx, const Namespace &k, const NamespaceDetails &value)
returns false if too full
Definition: hashtab.h:76
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
const char * _name
Definition: hashtab.h:131