Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-datastore
Commits
c276ac43
Commit
c276ac43
authored
Jan 27, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added elastigo implementation.
parent
ee9f4948
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
0 deletions
+91
-0
elastigo/datastore.go
elastigo/datastore.go
+91
-0
No files found.
elastigo/datastore.go
0 → 100644
View file @
c276ac43
package
elastigo
import
(
"fmt"
ds
"github.com/jbenet/datastore.go"
"github.com/mattbaird/elastigo/api"
"github.com/mattbaird/elastigo/core"
)
// Currently, elastigo does not allow connecting to multiple elasticsearch
// instances. The elastigo API uses global static variables (ugh).
// See https://github.com/mattbaird/elastigo/issues/22
//
// Thus, we use a global static variable (GlobalInstance), and return an
// error if NewDatastore is called twice with different addresses.
var
GlobalInstance
Address
type
Address
struct
{
Host
string
Port
int
}
// Datastore uses a standard Go map for internal storage.
type
Datastore
struct
{
addr
Address
index
string
}
func
NewDatastore
(
addr
Address
,
index
string
)
(
*
Datastore
,
error
)
{
if
GlobalInstance
.
Host
!=
""
&&
GlobalInstance
!=
addr
{
return
nil
,
fmt
.
Errorf
(
"elastigo only allows one client. See godoc."
)
}
api
.
Domain
=
addr
.
Host
if
addr
.
Port
>
0
{
api
.
Port
=
string
(
addr
.
Port
)
}
GlobalInstance
=
addr
return
&
Datastore
{
addr
:
addr
,
index
:
index
,
},
nil
}
// Returns the ElasticSearch index for given key. If the datastore specifies
// an index, use that. Else, key.Parent
func
(
d
*
Datastore
)
Index
(
key
ds
.
Key
)
string
{
if
len
(
d
.
index
)
>
0
{
return
d
.
index
}
return
key
.
Parent
()
.
BaseNamespace
()
}
// value should be JSON serializable.
func
(
d
*
Datastore
)
Put
(
key
ds
.
Key
,
value
interface
{})
(
err
error
)
{
res
,
err
:=
core
.
Index
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
(),
value
)
if
err
!=
nil
{
return
err
}
if
!
res
.
Ok
{
return
fmt
.
Errorf
(
"Elasticsearch response: NOT OK. %v"
,
res
)
}
return
nil
}
func
(
d
*
Datastore
)
Get
(
key
ds
.
Key
)
(
value
interface
{},
err
error
)
{
res
,
err
:=
core
.
Get
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
())
if
err
!=
nil
{
return
nil
,
err
}
if
!
res
.
Ok
{
return
nil
,
fmt
.
Errorf
(
"Elasticsearch response: NOT OK. %v"
,
res
)
}
return
res
.
Source
,
nil
}
func
(
d
*
Datastore
)
Has
(
key
ds
.
Key
)
(
exists
bool
,
err
error
)
{
return
core
.
Exists
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
())
}
func
(
d
*
Datastore
)
Delete
(
key
ds
.
Key
)
(
err
error
)
{
res
,
err
:=
core
.
Delete
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
(),
0
,
""
)
if
err
!=
nil
{
return
err
}
if
!
res
.
Ok
{
return
fmt
.
Errorf
(
"Elasticsearch response: NOT OK. %v"
,
res
)
}
return
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment