Skip to content

Commit

Permalink
modify MetadataTypeResource.add to take model.MetadataType
Browse files Browse the repository at this point in the history
  • Loading branch information
v0lat1le committed Sep 27, 2016
1 parent 5c7c72b commit 60638e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion datacube/index/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def init_db(self, with_default_types=True, with_permissions=True):
if is_new and with_default_types:
_LOG.info('Adding default metadata types.')
for _, doc in datacube.utils.read_documents(_DEFAULT_METADATA_TYPES_PATH):
self.metadata_types.add(doc, allow_table_lock=True)
self.metadata_types.add(self.metadata_types.from_doc(doc), allow_table_lock=True)

return is_new

Expand Down
23 changes: 11 additions & 12 deletions datacube/index/_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def from_doc(self, definition):
"""
return self._make(definition)

def add(self, definition, allow_table_lock=False):
def add(self, metadata_type, allow_table_lock=False):
"""
:type definition: dict
:param datacube.model.MetadataType metadata_type:
:param allow_table_lock:
Allow an exclusive lock to be taken on the table while creating the indexes.
This will halt other user's requests until completed.
Expand All @@ -43,26 +43,24 @@ def add(self, definition, allow_table_lock=False):
:rtype: datacube.model.MetadataType
"""
# This column duplication is getting out of hand:
MetadataType.validate(definition)

name = definition['name']
MetadataType.validate(metadata_type.definition)

existing = self._db.get_metadata_type_by_name(name)
existing = self._db.get_metadata_type_by_name(metadata_type.name)
if existing:
# They've passed us the same one again. Make sure it matches what is stored.
# TODO: Support for adding/updating search fields?
check_doc_unchanged(
existing.definition,
definition,
'Metadata Type {}'.format(name)
metadata_type.definition,
'Metadata Type {}'.format(metadata_type.name)
)
else:
self._db.add_metadata_type(
name=name,
definition=definition,
name=metadata_type.name,
definition=metadata_type.definition,
concurrently=not allow_table_lock
)
return self.get_by_name(name)
return self.get_by_name(metadata_type.name)

def update_document(self, definition, dry_run=False, allow_unsafe_updates=False):
"""
Expand Down Expand Up @@ -225,7 +223,8 @@ def from_doc(self, definition):
metadata_type = self.metadata_type_resource.get_by_name(metadata_type)
else:
# Otherwise they embedded a document, add it if needed:
metadata_type = self.metadata_type_resource.add(metadata_type, allow_table_lock=False)
metadata_type = self.metadata_type_resource.add(self.metadata_type_resource.from_doc(metadata_type),
allow_table_lock=False)

if not metadata_type:
raise InvalidDocException('Unknown metadata type: %r' % definition['metadata_type'])
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def default_metadata_type_doc(default_metadata_type_docs):
@pytest.fixture
def default_metadata_type(index, default_metadata_type_docs):
for d in default_metadata_type_docs:
index.metadata_types.add(d)
index.metadata_types.add(index.metadata_types.from_doc(d))
return index.metadata_types.get_by_name('eo')


Expand Down

0 comments on commit 60638e3

Please sign in to comment.