shorturl

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2021 License: GPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package shorturl provides functionality for the basic CRUD operations.

Index

Constants

View Source
const (
	// KindName is the datastore kind name for short url.
	KindName = "ShortUrl"
)

Variables

View Source
var (
	// ErrDatastoreInconsistent is returned when the datastore is in an
	// Inconsistent state. Typical example would be when the memcache
	// indicates that there should've been an instance of the short URL
	// stored but the actual instance cannot be found.
	ErrDatastoreInconsistent = errors.New("Datastore Inconsistent")

	ErrDatastoreWriteDisabled = errors.New("Datastore write disabled")

	// ErrNotFound is the error to be raised when the short URL matching the
	// search criteria cannot be found
	ErrNotFound = errors.New("Short URL not found")
)

Error values

Functions

This section is empty.

Types

type ShortURL

type ShortURL struct {
	ID          int64 `datastore:"-"`
	Hash        string
	OriginalURL string `datastore:"OriginalUrl,noindex"`
}

ShortURL is the entity used for storing short URL. Because of the fact that URLs can be more than 500 characters long and that we cannot index string properties longer than 500 characters directly to ensure uniqueness, we have to use the hash code of the URL as a fallback.

We also use multiple hash algorithms just to eliminate the possibility of hash collisions of one particular hashing algorithm. Using SHA512, SHA1, and MD5 simultaneously should make a hash collision statistically extremely. improbable.

func ByID

func ByID(ctx context.Context, id int64) (*ShortURL, error)

ByID loads the short URL by its ID.

func ByURL

func ByURL(ctx context.Context, url string) (*ShortURL, error)

ByURL finds the short URL by its original long URL.

func List

func List(ctx context.Context, start int64, count int) ([]ShortURL, error)

List lists all short URLs shorted by the their IDs.

func Persist

func Persist(ctx context.Context, originalURL string) (*ShortURL, error)

Persist persists the long URL by creating necessary objects.

type UniqueKey

type UniqueKey struct {
	ID int64
}

UniqueKey is an entity whose sole purpose is to ensure the uniqueness of a ShortURL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL