Index stores the given object
(doc Document)
| 118 | |
| 119 | // Index stores the given object |
| 120 | func (e *Engine) Index(doc Document) error { |
| 121 | if doc.Object == nil || doc.Object.Hash == "" { |
| 122 | return errors.New("no object details provided") |
| 123 | } |
| 124 | if e.IsIndexed(doc.Object.Hash) && !doc.Reindex { |
| 125 | return fmt.Errorf("document with hash '%s' already exists", doc.Object.Hash) |
| 126 | } |
| 127 | var l = e.l.With("hash", doc.Object.Hash) |
| 128 | |
| 129 | // populate defaults if necessary |
| 130 | if doc.Object.MD.MimeType == "" { |
| 131 | l.Debug("defaulting to MimeTypeUnknown") |
| 132 | doc.Object.MD.MimeType = models.MimeTypeUnknown |
| 133 | } |
| 134 | if doc.Object.MD.Category == "" { |
| 135 | l.Debug("defaulting to category = 'unknown'") |
| 136 | doc.Object.MD.Category = "unknown" |
| 137 | } |
| 138 | |
| 139 | // queue for index flush |
| 140 | if e.q.IsStopped() { |
| 141 | l.Warnw("queue stopped - waiting and trying again") |
| 142 | time.Sleep(3 * time.Second) |
| 143 | } |
| 144 | if err := e.q.Queue(&queue.Item{Key: doc.Object.Hash, Val: DocData{ |
| 145 | Content: doc.Content, |
| 146 | Metadata: &doc.Object.MD, |
| 147 | Properties: &DocProps{ |
| 148 | Indexed: time.Now().String(), |
| 149 | }, |
| 150 | }}); err != nil { |
| 151 | return fmt.Errorf("could not index object: %s", err.Error()) |
| 152 | } |
| 153 | l.Infow("index requested", |
| 154 | "size", len(doc.Content)) |
| 155 | |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | // IsIndexed checks if the given content hash has already been indexed |
| 160 | func (e *Engine) IsIndexed(hash string) bool { |