client examples

This commit is contained in:
Josh Baker 2016-03-06 08:57:11 -07:00
parent e9e6aabf7a
commit dced9f08f5
3 changed files with 126 additions and 3 deletions

View File

@ -11,7 +11,7 @@ Tile38 is an open source (MIT licensed), in-memory geolocation data store, spati
- Spatial index with [search](#searching) methods such as Nearby, Within, and Intersects.
- Realtime [geofencing](#geofencing).
- Variety of client protocols, including [http](#http) (curl), [websockets](#websockets), [telnet](#telnet), and a [native interface](#native-interface).
- Multiple object types including [lat/lon point](#latlon-point), [bounding box](#bounding-box), [Geohash](#geohash), [GeoJSON](#geojson), [QuadKey](#quadkey), and [XYZ tile](#xyz-tile).
- Object types of [lat/lon](#latlon-point), [bbox](#bounding-box), [Geohash](#geohash), [GeoJSON](#geojson), [QuadKey](#quadkey), and [XYZ tile](#xyz-tile).
- Server responses are in json.
- Full [command line interface](#cli).
- Leader / follower [replication](#replication).
@ -202,7 +202,7 @@ set city tempe object {"type":"Polygon","coordinates":[[[0,0],[10,10],[10,0],[0,
An XYZ tile is rectangle bounding area on earth that is represented by an X, Y coordinate and a Z (zoom) level.
Check out [maptiler.org](http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/) for an interactive example.
### QuadKey Tile
### QuadKey
A QuadKey used the same coordinate system as an XYZ tile except that the string representation is a string characters composed of 0, 1, 2, or 3. For a detailed explanation checkout [The Bing Maps Tile System](https://msdn.microsoft.com/en-us/library/bb259689.aspx).
@ -247,7 +247,7 @@ Should be sent to the server as (without quotes):
"$16 get fleet truck1\r\n"
```
The server responds will always respond in JSON, and will include the top level member `ok`. When `ok` is `false` there will also be an accompanied `err` member describing the problem. In nearly every response there will also be an `elapsed` member that is the duration of time that it took to process the request on the server. For more information on this string please refer to the [time.Duration](https://golang.org/pkg/time/#Duration) Go documentation.
The server will always respond in JSON, and will include the top level member `ok`. When `ok` is `false` there will also be an accompanied `err` member describing the problem. In nearly every response there will also be an `elapsed` member that is the duration of time that it took to process the request on the server. For more information on this string please refer to the [time.Duration](https://golang.org/pkg/time/#Duration) Go documentation.
So the response message:
```json

View File

@ -6,3 +6,69 @@ Tile38 Client
Tile38 Client is a [Go](http://golang.org/) client for [Tile38](http://tile38.com/).
## Examples
#### Connection
```go
package main
import "github.com/tidwall/tile38/client"
func main(){
conn, err := client.Dial("localhost:9851")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
resp, err := conn.Do("set fleet truck1 point 33.5123 -112.2693")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp))
}
```
#### Pool
```go
package main
import "github.com/tidwall/tile38/client"
func main(){
pool, err := client.DialPool("localhost:9851")
if err != nil {
log.Fatal(err)
}
defer pool.Close()
// We'll set a point in a background routine
go func() {
conn, err := pool.Get() // get a conn from the pool
if err != nil {
log.Fatal(err)
}
defer conn.Close() // return the conn to the pool
_, err = conn.Do("set fleet truck1 point 33.5123 -112.2693")
if err != nil {
log.Fatal(err)
}
}()
time.Sleep(time.Second / 2) // wait a moment
// Retreive the point we just set.
go func() {
conn, err := pool.Get() // get a conn from the pool
if err != nil {
log.Fatal(err)
}
defer conn.Close() // return the conn to the pool
resp, err := conn.Do("get fleet truck1 point")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp))
}()
time.Sleep(time.Second / 2) // wait a moment
}
```

57
client/conn_test.go Normal file
View File

@ -0,0 +1,57 @@
package client
import (
"fmt"
"log"
"time"
)
func ExampleDial() {
conn, err := Dial("localhost:9851")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
resp, err := conn.Do("set fleet truck1 point 33.5123 -112.2693")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp))
}
func ExampleDialPool() {
pool, err := DialPool("localhost:9851")
if err != nil {
log.Fatal(err)
}
defer pool.Close()
// We'll set a point in a background routine
go func() {
conn, err := pool.Get() // get a conn from the pool
if err != nil {
log.Fatal(err)
}
defer conn.Close() // return the conn to the pool
_, err = conn.Do("set fleet truck1 point 33.5123 -112.2693")
if err != nil {
log.Fatal(err)
}
}()
time.Sleep(time.Second / 2) // wait a moment
// Retreive the point we just set.
go func() {
conn, err := pool.Get() // get a conn from the pool
if err != nil {
log.Fatal(err)
}
defer conn.Close() // return the conn to the pool
resp, err := conn.Do("get fleet truck1 point")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(resp))
}()
time.Sleep(time.Second / 2) // wait a moment
}