Class: Wgit::Database::DatabaseAdapter

Inherits:
Object
  • Object
show all
Includes:
Assertable
Defined in:
lib/wgit/database/database_adapter.rb

Overview

The parent DatabaseAdapter class that should be inherited from when creating an underlying Database adapter implementation class e.g. Wgit::Database::MongoDB.

Listed in this class are the methods that an implementer class must implement to work with Wgit. Failure to do so will result in a NotImplementedError being raised.

While not required, implementing the method #search_fields=(fields) in an adapter class will allow Wgit::Model.set_search_fields to call it. This allows the search fields to be set in one method call, from within the Wgit::Model class. See this method's docs for more info.

Also listed in this class are common helper methods available to all Database implementer subclasses.

Direct Known Subclasses

InMemory, MongoDB

Constant Summary collapse

NOT_IMPL_ERR =

The NotImplementedError message that gets raised if an implementor class doesn't implement a method required by Wgit.

"The DatabaseAdapter class you're using hasn't \
implemented this method"

Constants included from Assertable

Assertable::DEFAULT_DUCK_FAIL_MSG, Assertable::DEFAULT_REQUIRED_KEYS_MSG, Assertable::DEFAULT_TYPE_FAIL_MSG, Assertable::MIXED_ENUMERABLE_MSG, Assertable::NON_ENUMERABLE_MSG

Instance Method Summary collapse

Methods included from Assertable

#assert_arr_types, #assert_common_arr_types, #assert_required_keys, #assert_respond_to, #assert_types

Constructor Details

#initialize(connection_string = nil) ⇒ DatabaseAdapter

Initializes a DatabaseAdapter instance.

The implementor class should establish a DB connection here using the given connection_string, falling back to ENV['WGIT_CONNECTION_STRING']. Don't forget to call super.

Parameters:

  • connection_string (String) (defaults to: nil)

    The connection string needed to connect to the database.

Raises:

  • (StandardError)

    If a connection string isn't provided, either as a parameter or via the environment.



44
# File 'lib/wgit/database/database_adapter.rb', line 44

def initialize(connection_string = nil); end

Instance Method Details

#bulk_upsert(objs) ⇒ Integer

Bulk upserts the objects in the database collection. You cannot mix collection objs types, all must be Urls or Documents.

Parameters:

Returns:

  • (Integer)

    The total number of newly inserted objects.

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/wgit/database/database_adapter.rb', line 105

def bulk_upsert(objs)
  raise NotImplementedError, NOT_IMPL_ERR
end

#emptyInteger

Deletes everything in the urls and documents collections.

Returns:

  • (Integer)

    The number of deleted records.

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/wgit/database/database_adapter.rb', line 77

def empty
  raise NotImplementedError, NOT_IMPL_ERR
end

#search(query, case_sensitive: false, whole_sentence: true, limit: 10, skip: 0) {|doc| ... } ⇒ Array<Wgit::Document>

Searches the database's Documents for the given query. The Wgit::Model.search_fields should be searched for matches against the given query. Documents should be sorted starting with the most relevant. Each returned Document should have it's score field set for relevance.

Parameters:

  • query (String)

    The text query to search with.

  • case_sensitive (Boolean) (defaults to: false)

    Whether character case must match.

  • whole_sentence (Boolean) (defaults to: true)

    Whether multiple words should be searched for separately.

  • limit (Integer) (defaults to: 10)

    The max number of results to return.

  • skip (Integer) (defaults to: 0)

    The number of results to skip.

Yields:

  • (doc)

    Given each search result (Wgit::Document) returned from the DB.

Returns:

  • (Array<Wgit::Document>)

    The search results obtained from the DB.

Raises:

  • (NotImplementedError)


68
69
70
71
72
# File 'lib/wgit/database/database_adapter.rb', line 68

def search(
  query, case_sensitive: false, whole_sentence: true, limit: 10, skip: 0
)
  raise NotImplementedError, NOT_IMPL_ERR
end

#sizeInteger

Returns the current size of the database.

Returns:

  • (Integer)

    The current size of the DB.

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/wgit/database/database_adapter.rb', line 49

def size
  raise NotImplementedError, NOT_IMPL_ERR
end

#uncrawled_urls(limit: 0, skip: 0) {|url| ... } ⇒ Array<Wgit::Url>

Returns Url records that haven't yet been crawled.

Parameters:

  • limit (Integer) (defaults to: 0)

    The max number of Url's to return. 0 returns all.

  • skip (Integer) (defaults to: 0)

    Skip n amount of Url's.

Yields:

  • (url)

    Given each Url object (Wgit::Url) returned from the DB.

Returns:

  • (Array<Wgit::Url>)

    The uncrawled Urls obtained from the DB.

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/wgit/database/database_adapter.rb', line 87

def uncrawled_urls(limit: 0, skip: 0)
  raise NotImplementedError, NOT_IMPL_ERR
end

#upsert(obj) ⇒ Boolean

Inserts or updates the object in the database.

Parameters:

Returns:

  • (Boolean)

    True if inserted, false if updated.

Raises:

  • (NotImplementedError)


95
96
97
# File 'lib/wgit/database/database_adapter.rb', line 95

def upsert(obj)
  raise NotImplementedError, NOT_IMPL_ERR
end