Class: Tantiny::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/tantiny/index.rb

Constant Summary collapse

LOCKFILE =
".tantiny.lock"
DEFAULT_WRITER_MEMORY =

5MB

5_000_000
DEFAULT_LIMIT =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, schema, **options) ⇒ Index

Returns a new instance of Index.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tantiny/index.rb', line 32

def initialize(path, schema, **options)
  @path = path
  @schema = schema

  @indexer_memory = options[:writer_memory] || DEFAULT_WRITER_MEMORY
  @exclusive_writer = options[:exclusive_writer] || false

  @active_transaction = Concurrent::ThreadLocalVar.new(false)
  @transaction_semaphore = Mutex.new

  acquire_index_writer if exclusive_writer?
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



45
46
47
# File 'lib/tantiny/index.rb', line 45

def schema
  @schema
end

Class Method Details

.new(path, **options, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tantiny/index.rb', line 9

def self.new(path, **options, &block)
  FileUtils.mkdir_p(path)

  default_tokenizer = options[:tokenizer] || Tokenizer.default
  schema = Schema.new(default_tokenizer, &block)

  object = __new(
    path.to_s,
    schema.default_tokenizer,
    schema.field_tokenizers.transform_keys(&:to_s),
    schema.text_fields.map(&:to_s),
    schema.string_fields.map(&:to_s),
    schema.integer_fields.map(&:to_s),
    schema.double_fields.map(&:to_s),
    schema.date_fields.map(&:to_s),
    schema.facet_fields.map(&:to_s)
  )

  object.send(:initialize, path, schema, **options)

  object
end

Instance Method Details

#<<(document) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tantiny/index.rb', line 67

def <<(document)
  transaction do
    __add_document(
      resolve(document, schema.id_field).to_s,
      slice_document(document, schema.text_fields) { |v| v.to_s },
      slice_document(document, schema.string_fields) { |v| v.to_s },
      slice_document(document, schema.integer_fields) { |v| v.to_i },
      slice_document(document, schema.double_fields) { |v| v.to_f },
      slice_document(document, schema.date_fields) { |v| Helpers.timestamp(v) },
      slice_document(document, schema.facet_fields) { |v| v.to_s }
    )
  end
end

#delete(id) ⇒ Object



81
82
83
84
85
# File 'lib/tantiny/index.rb', line 81

def delete(id)
  transaction do
    __delete_document(id.to_s)
  end
end

#reloadObject



63
64
65
# File 'lib/tantiny/index.rb', line 63

def reload
  __reload
end

#search(query, limit: DEFAULT_LIMIT, **smart_query_options) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/tantiny/index.rb', line 87

def search(query, limit: DEFAULT_LIMIT, **smart_query_options)
  unless query.is_a?(Query)
    fields = schema.text_fields
    query = Query.smart_query(self, fields, query.to_s, **smart_query_options)
  end

  __search(query, limit)
end

#transactionObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tantiny/index.rb', line 47

def transaction
  if inside_transaction?
    yield
  else
    synchronize do
      open_transaction!

      yield

      close_transaction!
    end
  end

  nil
end